]> git.tuebingen.mpg.de Git - paraslash.git/commitdiff
gui: Replace global variable curses_active by a function.
authorAndre Noll <maan@systemlinux.org>
Sun, 5 Jan 2014 20:48:29 +0000 (20:48 +0000)
committerAndre Noll <maan@systemlinux.org>
Sun, 4 May 2014 13:48:54 +0000 (15:48 +0200)
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

diff --git a/gui.c b/gui.c
index c467845251d72b74bdaa3ce0b270532fecb69aaa..02d67bc67e182422528c9214a76cc3114c1bbdd0 100644 (file)
--- a/gui.c
+++ b/gui.c
@@ -49,7 +49,6 @@ static struct ringbuffer *bot_win_rb;
 
 static unsigned scroll_position;
 
 
 static unsigned scroll_position;
 
-static bool curses_active;
 static pid_t cmd_pid;
 
 static int command_fds[2] = {-1, -1};
 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;
 }
 
        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)
 {
 /* 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;
 
        char *msg;
        va_list ap;
 
-       if (!curses_active)
+       if (!curses_active())
                return;
        wattron(in.win, COLOR_PAIR(color));
        va_start(ap, fmt);
                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;
 
        char *msg;
        va_list ap;
 
-       if (!curses_active)
+       if (!curses_active())
                return;
        va_start(ap, fmt);
        xvasprintf(&msg, fmt, ap);
                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;
 
 {
        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;
                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;
 
        va_list ap;
        unsigned bytes;
 
-       if (ll < loglevel || !curses_active)
+       if (ll < loglevel || !curses_active())
                return;
        switch (ll) {
                case LL_DEBUG:
                return;
        switch (ll) {
                case LL_DEBUG:
@@ -560,10 +565,7 @@ __noreturn static void kill_pg_and_die(int ret)
 
 static void shutdown_curses(void)
 {
 
 static void shutdown_curses(void)
 {
-       if (!curses_active)
-               return;
        def_prog_mode();
        def_prog_mode();
-       curses_active = false;
        endwin();
 }
 
        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];
 
        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);
                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;
 
 {
        int i;
 
-       if (!curses_active)
+       if (!curses_active())
                return;
        FOR_EACH_STATUS_ITEM(i)
                print_stat_item(i);
                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)
 {
 /* (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)
        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:
                        "only the good die young (caught SIGTERM))\n");
                return;
        case SIGWINCH:
-               if (curses_active) {
+               if (curses_active()) {
                        shutdown_curses();
                        init_curses();
                        redraw_bot_win();
                        shutdown_curses();
                        init_curses();
                        redraw_bot_win();