From 51b1505f5950528de257b18f870225feac8fa515 Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Sun, 5 Jan 2014 20:48:29 +0000 Subject: [PATCH] gui: Replace global variable curses_active by a function. There is no need to store this information as we can easily compute it. shutdown_curses() needed to be changed to cope with early failures: On fatal errors that happen after initscr() was called but before the top window exists, we must shut down the curses subsystem albeit curses_active() returns false in this case. --- gui.c | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/gui.c b/gui.c index c4678452..02d67bc6 100644 --- a/gui.c +++ b/gui.c @@ -49,7 +49,6 @@ static struct ringbuffer *bot_win_rb; static unsigned scroll_position; -static bool curses_active; static pid_t cmd_pid; static int command_fds[2] = {-1, -1}; @@ -207,6 +206,12 @@ static int find_cmd_byname(char *name) return -1; } +/* isendwin() returns false before initscr() was called */ +static bool curses_active(void) +{ + return top.win && !isendwin(); +} + /* taken from mutt */ static char *km_keyname(int c) { @@ -345,7 +350,7 @@ __printf_2_3 static void print_in_bar(int color, const char *fmt,...) char *msg; va_list ap; - if (!curses_active) + if (!curses_active()) return; wattron(in.win, COLOR_PAIR(color)); va_start(ap, fmt); @@ -491,7 +496,7 @@ __printf_2_3 static void outputf(int color, const char* fmt,...) char *msg; va_list ap; - if (!curses_active) + if (!curses_active()) return; va_start(ap, fmt); xvasprintf(&msg, fmt, ap); @@ -504,7 +509,7 @@ static int add_output_line(char *line, void *data) { int color = *(int *)data? COLOR_ERRMSG : COLOR_OUTPUT; - if (!curses_active) + if (!curses_active()) return 1; rb_add_entry(color, para_strdup(line)); return 1; @@ -519,7 +524,7 @@ static __printf_2_3 void curses_log(int ll, const char *fmt,...) va_list ap; unsigned bytes; - if (ll < loglevel || !curses_active) + if (ll < loglevel || !curses_active()) return; switch (ll) { case LL_DEBUG: @@ -560,10 +565,7 @@ __noreturn static void kill_pg_and_die(int ret) static void shutdown_curses(void) { - if (!curses_active) - return; def_prog_mode(); - curses_active = false; endwin(); } @@ -666,7 +668,7 @@ static void print_stat_item(int i) struct stat_item_data d = theme.data[i]; char *c = stat_content[i]; - if (!curses_active || !d.len || !c) + if (!curses_active() || !d.len || !c) return; tmp = make_message("%s%s%s", d.prefix, c, d.postfix); wmove(top.win, d.y * top.lines / 100, d.x * COLS / 100); @@ -745,7 +747,7 @@ static void print_all_items(void) { int i; - if (!curses_active) + if (!curses_active()) return; FOR_EACH_STATUS_ITEM(i) print_stat_item(i); @@ -792,7 +794,8 @@ static void init_colors_or_die(void) /* (Re-)initialize the curses library. */ static void init_curses(void) { - curses_active = true; + if (curses_active()) + return; if (top.win && refresh() == ERR) /* refresh is really needed */ msg_n_exit(EXIT_FAILURE, "refresh() failed\n"); if (LINES < theme.lines_min || COLS < theme.cols_min) @@ -890,7 +893,7 @@ static void handle_signal(int sig) "only the good die young (caught SIGTERM))\n"); return; case SIGWINCH: - if (curses_active) { + if (curses_active()) { shutdown_curses(); init_curses(); redraw_bot_win(); -- 2.39.5