properly handle alloc and free in noncurses

master
karlstav 6 years ago
parent fd5a0bdcce
commit 5d262c0eeb
  1. 2
      cava.c
  2. 80
      output/terminal_noncurses.c

@ -549,7 +549,7 @@ as of 0.4.0 all options are specified in config file, see in '/home/username/.co
#endif #endif
case OUTPUT_NONCURSES: case OUTPUT_NONCURSES:
get_terminal_dim_noncurses(&width, &lines); get_terminal_dim_noncurses(&width, &lines);
init_terminal_noncurses(p.col, p.bgcol, width, lines, p.bar_width); init_terminal_noncurses(inAtty, p.col, p.bgcol, width, lines, p.bar_width);
height = (lines - 1) * 8; height = (lines - 1) * 8;
break; break;

@ -8,13 +8,13 @@
#include <unistd.h> #include <unistd.h>
#include <wchar.h> #include <wchar.h>
wchar_t *line_buffer; wchar_t *frame_buffer;
wchar_t *barstring[8]; wchar_t *barstring[8];
wchar_t *wspacestring; wchar_t *spacestring;
int buf_length; int buf_length;
char *ttyline_buffer; char *ttyframe_buffer;
char ttybarstring[8][100]; char *ttybarstring[8];
char ttyspacestring[100]; char *ttyspacestring;
int ttybuf_length; int ttybuf_length;
int setecho(int fd, int onoff) { int setecho(int fd, int onoff) {
@ -35,31 +35,50 @@ int setecho(int fd, int onoff) {
return 0; return 0;
} }
int init_terminal_noncurses(int col, int bgcol, int w, int h, int bar_width) { int init_terminal_noncurses(int tty, int col, int bgcol, int width, int lines, int bar_width) {
int n, i; if (tty) {
ttybuf_length = sizeof(char) * width * lines * 10;
ttyframe_buffer = (char *)malloc(ttybuf_length);
ttyspacestring = (char *)malloc(sizeof(char) * bar_width);
// clearing barstrings
for (int n = 0; n < 8; n++) {
ttybarstring[n] = (char *)malloc(sizeof(char) * bar_width);
ttybarstring[n][0] = '\0';
}
ttyspacestring[0] = '\0';
ttyframe_buffer[0] = '\0';
ttybuf_length = sizeof(char) * w * h * 10; // creating barstrings for drawing
ttyline_buffer = (char *)malloc(ttybuf_length); for (int n = 0; n < bar_width; n++) {
strcat(ttybarstring[0], "8");
strcat(ttybarstring[1], "1");
strcat(ttybarstring[2], "2");
strcat(ttybarstring[3], "3");
strcat(ttybarstring[4], "4");
strcat(ttybarstring[5], "5");
strcat(ttybarstring[6], "6");
strcat(ttybarstring[7], "7");
strcat(ttyspacestring, " ");
}
} else if (!tty) {
buf_length = sizeof(wchar_t) * w * h * 10; buf_length = sizeof(wchar_t) * width * lines * 10;
line_buffer = (wchar_t *)malloc(buf_length); frame_buffer = (wchar_t *)malloc(buf_length);
wspacestring = (wchar_t *)malloc(sizeof(wchar_t) * bar_width); spacestring = (wchar_t *)malloc(sizeof(wchar_t) * bar_width);
// clearing barstrings // clearing barstrings
for (n = 0; n < 8; n++) { for (int n = 0; n < 8; n++) {
barstring[n] = (wchar_t *)malloc(sizeof(wchar_t) * bar_width); barstring[n] = (wchar_t *)malloc(sizeof(wchar_t) * bar_width);
ttybarstring[n][0] = '\0';
barstring[n][0] = '\0'; barstring[n][0] = '\0';
ttyspacestring[0] = '\0';
} }
wspacestring[0] = '\0'; spacestring[0] = '\0';
line_buffer[0] = '\0'; frame_buffer[0] = '\0';
ttyline_buffer[0] = '\0';
// creating barstrings for drawing // creating barstrings for drawing
for (n = 0; n < bar_width; n++) { for (int n = 0; n < bar_width; n++) {
wcscat(barstring[0], L"\u2588"); wcscat(barstring[0], L"\u2588");
wcscat(barstring[1], L"\u2581"); wcscat(barstring[1], L"\u2581");
wcscat(barstring[2], L"\u2582"); wcscat(barstring[2], L"\u2582");
@ -68,17 +87,8 @@ int init_terminal_noncurses(int col, int bgcol, int w, int h, int bar_width) {
wcscat(barstring[5], L"\u2585"); wcscat(barstring[5], L"\u2585");
wcscat(barstring[6], L"\u2586"); wcscat(barstring[6], L"\u2586");
wcscat(barstring[7], L"\u2587"); wcscat(barstring[7], L"\u2587");
wcscat(wspacestring, L" "); wcscat(spacestring, L" ");
}
strcat(ttybarstring[0], "8");
strcat(ttybarstring[1], "1");
strcat(ttybarstring[2], "2");
strcat(ttybarstring[3], "3");
strcat(ttybarstring[4], "4");
strcat(ttybarstring[5], "5");
strcat(ttybarstring[6], "6");
strcat(ttybarstring[7], "7");
strcat(ttyspacestring, " ");
} }
col += 30; col += 30;
@ -281,6 +291,14 @@ int draw_terminal_noncurses(int tty, int h, int w, int bars, int bar_width, int
// general: cleanup // general: cleanup
void cleanup_terminal_noncurses(void) { void cleanup_terminal_noncurses(void) {
free(frame_buffer);
free(ttyframe_buffer);
free(spacestring);
free(ttyspacestring);
for (int i = 0; i < 8; i++) {
free(barstring[i]);
free(ttybarstring[i]);
}
setecho(STDIN_FILENO, 1); setecho(STDIN_FILENO, 1);
printf("\033[0m\n"); printf("\033[0m\n");
system("setfont >/dev/null 2>&1"); system("setfont >/dev/null 2>&1");

Loading…
Cancel
Save