From 5d262c0eeb58d9bc62718f8789d66be0e7e5b162 Mon Sep 17 00:00:00 2001 From: karlstav Date: Wed, 27 May 2020 21:57:59 +0200 Subject: [PATCH] properly handle alloc and free in noncurses --- cava.c | 2 +- output/terminal_noncurses.c | 108 +++++++++++++++++++++--------------- 2 files changed, 64 insertions(+), 46 deletions(-) diff --git a/cava.c b/cava.c index 60daa0c..437f75f 100644 --- a/cava.c +++ b/cava.c @@ -549,7 +549,7 @@ as of 0.4.0 all options are specified in config file, see in '/home/username/.co #endif case OUTPUT_NONCURSES: 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; break; diff --git a/output/terminal_noncurses.c b/output/terminal_noncurses.c index cd867e9..f39635e 100644 --- a/output/terminal_noncurses.c +++ b/output/terminal_noncurses.c @@ -8,13 +8,13 @@ #include #include -wchar_t *line_buffer; +wchar_t *frame_buffer; wchar_t *barstring[8]; -wchar_t *wspacestring; +wchar_t *spacestring; int buf_length; -char *ttyline_buffer; -char ttybarstring[8][100]; -char ttyspacestring[100]; +char *ttyframe_buffer; +char *ttybarstring[8]; +char *ttyspacestring; int ttybuf_length; int setecho(int fd, int onoff) { @@ -35,50 +35,60 @@ int setecho(int fd, int onoff) { 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; - - ttybuf_length = sizeof(char) * w * h * 10; - ttyline_buffer = (char *)malloc(ttybuf_length); + if (tty) { - buf_length = sizeof(wchar_t) * w * h * 10; - line_buffer = (wchar_t *)malloc(buf_length); - wspacestring = (wchar_t *)malloc(sizeof(wchar_t) * bar_width); + ttybuf_length = sizeof(char) * width * lines * 10; + ttyframe_buffer = (char *)malloc(ttybuf_length); + ttyspacestring = (char *)malloc(sizeof(char) * bar_width); - // clearing barstrings - for (n = 0; n < 8; n++) { - barstring[n] = (wchar_t *)malloc(sizeof(wchar_t) * bar_width); - ttybarstring[n][0] = '\0'; - barstring[n][0] = '\0'; + // clearing barstrings + for (int n = 0; n < 8; n++) { + ttybarstring[n] = (char *)malloc(sizeof(char) * bar_width); + ttybarstring[n][0] = '\0'; + } ttyspacestring[0] = '\0'; - } - wspacestring[0] = '\0'; - line_buffer[0] = '\0'; - ttyline_buffer[0] = '\0'; - - // creating barstrings for drawing - for (n = 0; n < bar_width; n++) { - - wcscat(barstring[0], L"\u2588"); - wcscat(barstring[1], L"\u2581"); - wcscat(barstring[2], L"\u2582"); - wcscat(barstring[3], L"\u2583"); - wcscat(barstring[4], L"\u2584"); - wcscat(barstring[5], L"\u2585"); - wcscat(barstring[6], L"\u2586"); - wcscat(barstring[7], L"\u2587"); - wcscat(wspacestring, 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, " "); + ttyframe_buffer[0] = '\0'; + + // creating barstrings for drawing + 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) * width * lines * 10; + frame_buffer = (wchar_t *)malloc(buf_length); + spacestring = (wchar_t *)malloc(sizeof(wchar_t) * bar_width); + + // clearing barstrings + for (int n = 0; n < 8; n++) { + barstring[n] = (wchar_t *)malloc(sizeof(wchar_t) * bar_width); + barstring[n][0] = '\0'; + } + spacestring[0] = '\0'; + frame_buffer[0] = '\0'; + + // creating barstrings for drawing + for (int n = 0; n < bar_width; n++) { + wcscat(barstring[0], L"\u2588"); + wcscat(barstring[1], L"\u2581"); + wcscat(barstring[2], L"\u2582"); + wcscat(barstring[3], L"\u2583"); + wcscat(barstring[4], L"\u2584"); + wcscat(barstring[5], L"\u2585"); + wcscat(barstring[6], L"\u2586"); + wcscat(barstring[7], L"\u2587"); + wcscat(spacestring, L" "); + } } col += 30; @@ -281,6 +291,14 @@ int draw_terminal_noncurses(int tty, int h, int w, int bars, int bar_width, int // general: cleanup 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); printf("\033[0m\n"); system("setfont >/dev/null 2>&1");