]> git.tuebingen.mpg.de Git - paraslash.git/blob - gui.c
Doxify para_gui.
[paraslash.git] / gui.c
1 /*
2  * Copyright (C) 1998-2014 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file gui.c Curses-based interface for paraslash. */
8
9 #include <regex.h>
10 #include <signal.h>
11 #include <sys/types.h>
12 #include <curses.h>
13 #include <locale.h>
14 #include <sys/time.h>
15
16 #include "gui.cmdline.h"
17 #include "para.h"
18 #include "gui.h"
19 #include "string.h"
20 #include "ringbuffer.h"
21 #include "fd.h"
22 #include "error.h"
23 #include "list.h"
24 #include "sched.h"
25 #include "signal.h"
26 #include "ggo.h"
27 #include "version.h"
28
29 /** define the array of error lists needed by para_gui */
30 INIT_GUI_ERRLISTS;
31 static char *stat_content[NUM_STAT_ITEMS];
32
33 static struct gui_window {
34         WINDOW *win;
35         bool needs_update;
36 } top, bot, sb, in, sep;
37
38 /** How many lines of output to remember. */
39 #define RINGBUFFER_SIZE 512
40
41 struct rb_entry {
42         char *msg;
43         size_t len;
44         int color;
45 };
46 static struct ringbuffer *bot_win_rb;
47
48 static unsigned scroll_position;
49
50 static pid_t exec_pid;
51
52 static int exec_fds[2] = {-1, -1};
53 static struct gui_args_info conf;
54 static int loglevel;
55
56 /** Type of the process currently being executed. */
57 enum exec_status {
58         EXEC_IDLE, /**< No process running. */
59         EXEC_DCMD, /**< para or display process running. */
60         EXEC_XCMD, /**< External process running. */
61 };
62
63 /**
64  * Codes for various colors.
65  *
66  * Each status item has its own color pair. The ones defined here start at a
67  * higher number so that they do not overlap with these.
68  */
69 enum gui_color_pair {
70         COLOR_STATUSBAR = NUM_STAT_ITEMS + 1,
71         COLOR_COMMAND,
72         COLOR_OUTPUT,
73         COLOR_MSG,
74         COLOR_ERRMSG,
75         COLOR_SEPARATOR,
76         COLOR_TOP,
77         COLOR_BOT,
78 };
79
80 struct gui_command {
81         const char *key;
82         const char *name;
83         const char *description;
84         void (*handler)(void);
85 };
86
87 static struct gui_theme theme;
88
89 #define GUI_COMMANDS \
90         GUI_COMMAND(help, "?", "print help") \
91         GUI_COMMAND(enlarge_top_win, "+", "enlarge the top window") \
92         GUI_COMMAND(shrink_top_win, "-", "shrink the top window") \
93         GUI_COMMAND(reread_conf, "r", "reread configuration file") \
94         GUI_COMMAND(quit, "q", "exit para_gui") \
95         GUI_COMMAND(refresh, "^L", "redraw the screen") \
96         GUI_COMMAND(next_theme, ".", "switch to next theme") \
97         GUI_COMMAND(prev_theme, ",", "switch to previous theme") \
98         GUI_COMMAND(ll_incr, ">", "increase loglevel (decreases verbosity)") \
99         GUI_COMMAND(ll_decr, "<", "decrease loglevel (increases verbosity)") \
100         GUI_COMMAND(version, "V", "show the para_gui version") \
101         GUI_COMMAND(scroll_up, "<up>", "scroll up one line") \
102         GUI_COMMAND(scroll_down, "<down>", "scroll_down") \
103         GUI_COMMAND(page_up, "<ppage>", "scroll up one page") \
104         GUI_COMMAND(page_down, "<npage>", "scroll down one page") \
105         GUI_COMMAND(scroll_top, "<home>", "scroll to top of buffer") \
106         GUI_COMMAND(cancel_scroll, "<end>", "deactivate scroll mode") \
107
108 /* declare command handlers */
109 #define GUI_COMMAND(_c, _k, _d) \
110         static void com_ ## _c(void);
111 GUI_COMMANDS
112
113 #undef GUI_COMMAND
114
115 /* define command array */
116 #define GUI_COMMAND(_c, _k, _d) \
117         { \
118                 .key = _k, \
119                 .name = #_c, \
120                 .description = _d, \
121                 .handler = com_ ## _c \
122         },
123
124 static struct gui_command command_list[] = {GUI_COMMANDS {.name = NULL}};
125
126 struct input_task {
127         struct task task;
128 };
129
130 struct status_task {
131         struct task task;
132         char *buf;
133         int bufsize, loaded;
134         struct timeval next_exec;
135         int fd;
136 };
137
138 /** Stdout/stderr of the executing process is read in chunks of this size. */
139 #define COMMAND_BUF_SIZE 32768
140
141 struct exec_task {
142         struct task task;
143         char command_buf[2][COMMAND_BUF_SIZE]; /* stdout/stderr of command */
144         int cbo[2]; /* command buf offsets */
145         unsigned flags[2]; /* passed to for_each_line() */
146 };
147
148 static int find_cmd_byname(char *name)
149 {
150         int i;
151
152         for (i = 0; command_list[i].handler; i++)
153                 if (!strcmp(command_list[i].name, name))
154                         return i;
155         return -1;
156 }
157
158 /*
159  * Even though ncurses provides getmaxx and getmaxy, these functions/macros are
160  * not described in the XSI Curses standard.
161  */
162 static int get_num_lines(struct gui_window *w)
163 {
164         int lines;
165         __a_unused int cols; /* avoid "set but not used" warnings */
166
167         getmaxyx(w->win, lines, cols);
168         return lines;
169 }
170
171 static int get_num_cols(struct gui_window *w)
172 {
173         __a_unused int lines; /* avoid "set but not used" warnings */
174         int cols;
175
176         getmaxyx(w->win, lines, cols);
177         return cols;
178 }
179
180 /** Number of lines of the window are occupied by an output line. */
181 #define NUM_LINES(len) (1 + (len) / get_num_cols(&bot))
182
183 /* isendwin() returns false before initscr() was called */
184 static bool curses_active(void)
185 {
186         return top.win && !isendwin();
187 }
188
189 /* taken from mutt */
190 static char *km_keyname(int c)
191 {
192         static char buf[10];
193
194         if (c == KEY_UP) {
195                 sprintf(buf, "<up>");
196                 return buf;
197         }
198         if (c == KEY_DOWN) {
199                 sprintf(buf, "<down>");
200                 return buf;
201         }
202         if (c == KEY_LEFT) {
203                 sprintf(buf, "<left>");
204                 return buf;
205         }
206         if (c == KEY_RIGHT) {
207                 sprintf(buf, "<right>");
208                 return buf;
209         }
210         if (c == KEY_NPAGE) {
211                 sprintf(buf, "<npage>");
212                 return buf;
213         }
214         if (c == KEY_PPAGE) {
215                 sprintf(buf, "<ppage>");
216                 return buf;
217         }
218         if (c == KEY_HOME) {
219                 sprintf(buf, "<home>");
220                 return buf;
221         }
222         if (c == KEY_END) {
223                 sprintf(buf, "<end>");
224                 return buf;
225         }
226         if (c < 256 && c > -128 && iscntrl((unsigned char) c)) {
227                 if (c < 0)
228                         c += 256;
229                 if (c < 128) {
230                         buf[0] = '^';
231                         buf[1] = (c + '@') & 0x7f;
232                         buf[2] = 0;
233                 } else
234                         snprintf(buf, sizeof(buf), "\\%d%d%d", c >> 6,
235                                 (c >> 3) & 7, c & 7);
236         } else if (c >= KEY_F0 && c < KEY_F(256))
237                 sprintf(buf, "<F%d>", c - KEY_F0);
238         else if (isprint(c))
239                 snprintf(buf, sizeof(buf), "%c", (unsigned char) c);
240         else
241                 snprintf(buf, sizeof(buf), "\\x%hx", (unsigned short) c);
242         return buf;
243 }
244
245 /* Print given number of spaces to curses window. */
246 static void add_spaces(WINDOW* win, unsigned int num)
247 {
248         char space[] = "                                ";
249         unsigned sz = sizeof(space) - 1; /* number of spaces */
250
251         while (num >= sz)  {
252                 waddstr(win, space);
253                 num -= sz;
254         }
255         if (num > 0) {
256                 assert(num < sz);
257                 space[num] = '\0';
258                 waddstr(win, space);
259         }
260 }
261
262 /*
263  * print aligned string to curses window. This function always prints
264  * exactly len chars.
265  */
266 static int align_str(WINDOW* win, char *str, unsigned int len,
267                 unsigned int align)
268 {
269         int ret, i, num; /* of spaces */
270         size_t width;
271
272         if (!win || !str)
273                 return 0;
274         ret = strwidth(str, &width);
275         if (ret < 0) {
276                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
277                 width = 0;
278                 str[0] = '\0';
279         }
280         num = len - width;
281         if (num < 0) {
282                 str[len] = '\0';
283                 num = 0;
284         }
285         /* replace control characters by spaces */
286         for (i = 0; i < len && str[i]; i++) {
287                 if (str[i] == '\n' || str[i] == '\r' || str[i] == '\f')
288                         str[i] = ' ';
289         }
290         if (align == LEFT) {
291                 waddstr(win, str);
292                 add_spaces(win, num);
293         } else if (align == RIGHT) {
294                 add_spaces(win, num);
295                 waddstr(win, str);
296         } else {
297                 add_spaces(win, num / 2);
298                 waddstr(win, str[0]? str: "");
299                 add_spaces(win, num - num / 2);
300         }
301         return 1;
302 }
303
304 static void refresh_window(struct gui_window *gw)
305 {
306         gw->needs_update = true;
307 }
308
309 static bool window_update_needed(void)
310 {
311         return top.needs_update || bot.needs_update || sb.needs_update ||
312                 in.needs_update || sep.needs_update;
313 }
314
315 __printf_2_3 static void print_in_bar(int color, const char *fmt,...)
316 {
317         char *msg;
318         va_list ap;
319
320         if (!curses_active())
321                 return;
322         wattron(in.win, COLOR_PAIR(color));
323         va_start(ap, fmt);
324         xvasprintf(&msg, fmt, ap);
325         va_end(ap);
326         wmove(in.win, 0, 0);
327         align_str(in.win, msg, get_num_cols(&in), LEFT);
328         free(msg);
329         refresh_window(&in);
330 }
331
332 static void print_status_bar(void)
333 {
334         char *tmp;
335
336         tmp = para_strdup("para_gui " PACKAGE_VERSION " (hit ? for help)");
337         wmove(sb.win, 0, 0);
338         align_str(sb.win, tmp, get_num_cols(&sb), CENTER);
339         free(tmp);
340 }
341
342 /*
343  * get the number of the oldest rbe that is (partially) visible. On return,
344  * lines contains the sum of the number of lines of all visible entries. If the
345  * first one is only partially visible, lines is greater than bot.lines.
346  */
347 static int first_visible_rbe(unsigned *lines)
348 {
349         int i, bot_lines = get_num_lines(&bot);
350
351         *lines = 0;
352         for (i = scroll_position; i < RINGBUFFER_SIZE; i++) {
353                 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
354                 int rbe_lines;
355                 if (!rbe)
356                         return i - 1;
357                 rbe_lines = NUM_LINES(rbe->len);
358                 if (rbe_lines > bot_lines)
359                         return -1;
360                 *lines += rbe_lines;
361                 if (*lines >= bot_lines)
362                         return i;
363         }
364         return RINGBUFFER_SIZE - 1;
365 }
366
367 /*
368 returns number of first visible rbe, *lines is the number of lines drawn.
369  */
370 static int draw_top_rbe(unsigned *lines)
371 {
372         int bot_cols, bot_lines, ret, fvr = first_visible_rbe(lines);
373         struct rb_entry *rbe;
374         size_t bytes_to_skip, cells_to_skip, width;
375
376         if (fvr < 0)
377                 return -1;
378         wmove(bot.win, 0, 0);
379         rbe = ringbuffer_get(bot_win_rb, fvr);
380         if (!rbe)
381                 return -1;
382         getmaxyx(bot.win, bot_lines, bot_cols);
383         if (*lines > bot_lines) {
384                 /* rbe is partially visible multi-line */
385                 cells_to_skip = (*lines - bot_lines) * bot_cols;
386                 ret = skip_cells(rbe->msg, cells_to_skip, &bytes_to_skip);
387                 if (ret < 0)
388                         return ret;
389                 ret = strwidth(rbe->msg + bytes_to_skip, &width);
390                 if (ret < 0)
391                         return ret;
392         } else {
393                 bytes_to_skip = 0;
394                 width = rbe->len;
395         }
396         wattron(bot.win, COLOR_PAIR(rbe->color));
397         waddstr(bot.win, rbe->msg + bytes_to_skip);
398         *lines = NUM_LINES(width);
399         return fvr;
400 }
401
402 static void redraw_bot_win(void)
403 {
404         unsigned lines;
405         int i, bot_lines = get_num_lines(&bot);
406
407         wmove(bot.win, 0, 0);
408         wclear(bot.win);
409         i = draw_top_rbe(&lines);
410         if (i <= 0)
411                 goto out;
412         while (i > 0 && lines < bot_lines) {
413                 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, --i);
414                 if (!rbe) {
415                         lines++;
416                         waddstr(bot.win, "\n");
417                         continue;
418                 }
419                 lines += NUM_LINES(rbe->len);
420                 wattron(bot.win, COLOR_PAIR(rbe->color));
421                 waddstr(bot.win, "\n");
422                 waddstr(bot.win, rbe->msg);
423         }
424 out:
425         refresh_window(&bot);
426 }
427
428 static void rb_add_entry(int color, char *msg)
429 {
430         struct rb_entry *old, *new;
431         int x, y;
432         size_t len;
433
434         if (strwidth(msg, &len) < 0)
435                 return;
436         new = para_malloc(sizeof(struct rb_entry));
437         new->color = color;
438         new->len = len;
439         new->msg = msg;
440         old = ringbuffer_add(bot_win_rb, new);
441         if (old) {
442                 free(old->msg);
443                 free(old);
444         }
445         if (scroll_position) {
446                 /* discard current scrolling, like xterm does */
447                 scroll_position = 0;
448                 redraw_bot_win();
449                 return;
450         }
451         wattron(bot.win, COLOR_PAIR(color));
452         getyx(bot.win, y, x);
453         if (y || x)
454                 waddstr(bot.win, "\n");
455         waddstr(bot.win, msg);
456 }
457
458 /*
459  * print formated output to bot win and refresh
460  */
461 __printf_2_3 static void outputf(int color, const char* fmt,...)
462 {
463         char *msg;
464         va_list ap;
465
466         if (!curses_active())
467                 return;
468         va_start(ap, fmt);
469         xvasprintf(&msg, fmt, ap);
470         va_end(ap);
471         rb_add_entry(color, msg);
472         refresh_window(&bot);
473 }
474
475 static int add_output_line(char *line, void *data)
476 {
477         int color = *(int *)data? COLOR_ERRMSG : COLOR_OUTPUT;
478
479         if (!curses_active())
480                 return 1;
481         rb_add_entry(color, para_strdup(line));
482         return 1;
483 }
484
485 static __printf_2_3 void curses_log(int ll, const char *fmt,...)
486 {
487         va_list ap;
488
489         if (ll < loglevel)
490                 return;
491         va_start(ap, fmt);
492         if (curses_active()) {
493                 int color = ll <= LL_NOTICE? COLOR_MSG : COLOR_ERRMSG;
494                 char *msg;
495                 unsigned bytes = xvasprintf(&msg, fmt, ap);
496                 if (bytes > 0 && msg[bytes - 1] == '\n')
497                         msg[bytes - 1] = '\0'; /* cut trailing newline */
498                 rb_add_entry(color, msg);
499                 refresh_window(&bot);
500         } else if (exec_pid <= 0) /* no external command running */
501                 vfprintf(stderr, fmt, ap);
502         va_end(ap);
503 }
504 /** The log function of para_gui, always set to curses_log(). */
505 __printf_2_3 void (*para_log)(int, const char*, ...) = curses_log;
506
507 static void shutdown_curses(void)
508 {
509         def_prog_mode();
510         endwin();
511 }
512
513 /* disable curses, print a message, kill running processes and exit */
514 __noreturn __printf_2_3 static void die(int exit_code, const char* fmt, ...)
515 {
516         va_list argp;
517
518         shutdown_curses();
519         va_start(argp, fmt);
520         vfprintf(stderr, fmt, argp);
521         va_end(argp);
522         /* kill every process in the process group and exit */
523         para_sigaction(SIGTERM, SIG_IGN);
524         kill(0, SIGTERM);
525         exit(exit_code);
526 }
527
528 /*
529  * Print stat item #i to curses window
530  */
531 static void print_stat_item(int i)
532 {
533         char *tmp;
534         struct stat_item_data d = theme.data[i];
535         char *c = stat_content[i];
536         int top_lines = get_num_lines(&top);
537
538         if (!curses_active() || !d.len || !c)
539                 return;
540         tmp = make_message("%s%s%s", d.prefix, c, d.postfix);
541         wmove(top.win, d.y * top_lines / 100, d.x * COLS / 100);
542         wattron(top.win, COLOR_PAIR(i + 1));
543         align_str(top.win, tmp, d.len * COLS / 100, d.align);
544         free(tmp);
545         refresh_window(&top);
546 }
547
548 static int update_item(int item_num, char *buf)
549 {
550         char **c = stat_content + item_num;
551
552         free(*c);
553         if (buf && buf[0])
554                 goto dup;
555         switch (item_num) {
556         case SI_ARTIST:
557                 *c = para_strdup("(artist tag not set)");
558                 goto print;
559         case SI_TITLE:
560                 *c = para_strdup("(title tag not set)");
561                 goto print;
562         case SI_YEAR:
563                 *c = para_strdup("????");
564                 goto print;
565         case SI_ALBUM:
566                 *c = para_strdup("(album tag not set)");
567                 goto print;
568         case SI_COMMENT:
569                 *c = para_strdup("(comment tag not set)");
570                 goto print;
571         }
572 dup:
573         *c = para_strdup(buf);
574 print:
575         print_stat_item(item_num);
576         return 1;
577 }
578
579 static void print_all_items(void)
580 {
581         int i;
582
583         if (!curses_active())
584                 return;
585         FOR_EACH_STATUS_ITEM(i)
586                 print_stat_item(i);
587 }
588
589 static void clear_all_items(void)
590 {
591         int i;
592
593         FOR_EACH_STATUS_ITEM(i) {
594                 free(stat_content[i]);
595                 stat_content[i] = para_strdup("");
596         }
597 }
598
599 static void status_pre_select(struct sched *s, struct task *t)
600 {
601         struct status_task *st = container_of(t, struct status_task, task);
602
603         if (st->fd >= 0)
604                 return para_fd_set(st->fd, &s->rfds, &s->max_fileno);
605         sched_request_barrier_or_min_delay(&st->next_exec, s);
606 }
607
608 static int status_post_select(struct sched *s, struct task *t)
609 {
610         struct status_task *st = container_of(t, struct status_task, task);
611         size_t sz;
612         pid_t pid;
613         int ret, ret2;
614
615         if (st->fd < 0) {
616                 int fds[3] = {0, 1, 0};
617                 /* Avoid busy loop */
618                 if (tv_diff(&st->next_exec, now, NULL) > 0)
619                         return 0;
620                 st->next_exec.tv_sec = now->tv_sec + 2;
621                 ret = para_exec_cmdline_pid(&pid, conf.stat_cmd_arg, fds);
622                 if (ret < 0)
623                         return 0;
624                 ret = mark_fd_nonblocking(fds[1]);
625                 if (ret < 0) {
626                         close(fds[1]);
627                         return 0;
628                 }
629                 st->fd = fds[1];
630                 return 0;
631         }
632
633         if (st->loaded >= st->bufsize) {
634                 if (st->bufsize > 1000 * 1000) {
635                         st->loaded = 0;
636                         return 0;
637                 }
638                 st->bufsize += st->bufsize + 1000;
639                 st->buf = para_realloc(st->buf, st->bufsize);
640         }
641         assert(st->loaded < st->bufsize);
642         ret = read_nonblock(st->fd, st->buf + st->loaded,
643                 st->bufsize - st->loaded, &s->rfds, &sz);
644         st->loaded += sz;
645         ret2 = for_each_stat_item(st->buf, st->loaded, update_item);
646         if (ret < 0 || ret2 < 0) {
647                 st->loaded = 0;
648                 PARA_NOTICE_LOG("closing stat pipe: %s\n", para_strerror(-ret));
649                 close(st->fd);
650                 st->fd = -1;
651                 clear_all_items();
652                 free(stat_content[SI_BASENAME]);
653                 stat_content[SI_BASENAME] =
654                         para_strdup("stat command terminated!?");
655                 print_all_items();
656                 return 0;
657         }
658         sz = ret2; /* what is left */
659         if (sz > 0 && sz < st->loaded)
660                 memmove(st->buf, st->buf + st->loaded - sz, sz);
661         st->loaded = sz;
662         return 0;
663 }
664
665 /*
666  * init all windows
667  */
668 static void init_wins(int top_lines)
669 {
670         int top_y = 0, bot_y = top_lines + 1, sb_y = LINES - 2,
671                 in_y = LINES - 1, sep_y = top_lines;
672         int bot_lines = LINES - top_lines - 3, sb_lines = 1, in_lines = 1,
673                 sep_lines = 1;
674
675         assume_default_colors(theme.default_fg, theme.default_bg);
676         if (top.win) {
677                 wresize(top.win, top_lines, COLS);
678                 mvwin(top.win, top_y, 0);
679
680                 wresize(sb.win, sb_lines, COLS);
681                 mvwin(sb.win, sb_y, 0);
682
683                 wresize(sep.win, sep_lines, COLS);
684                 mvwin(sep.win, sep_y, 0);
685
686                 wresize(bot.win, bot_lines, COLS);
687                 mvwin(bot.win, bot_y, 0);
688
689                 wresize(in.win, in_lines, COLS);
690                 mvwin(in.win, in_y, 0);
691         } else {
692                 sep.win = newwin(sep_lines, COLS, sep_y, 0);
693                 top.win = newwin(top_lines, COLS, top_y, 0);
694                 bot.win = newwin(bot_lines, COLS, bot_y, 0);
695                 sb.win = newwin(sb_lines, COLS, sb_y, 0);
696                 in.win = newwin(in_lines, COLS, in_y, 0);
697                 if (!top.win || !bot.win || !sb.win || !in.win || !sep.win)
698                         die(EXIT_FAILURE, "Error: Cannot create curses windows\n");
699                 wclear(bot.win);
700                 wclear(sb.win);
701                 wclear(in.win);
702                 scrollok(bot.win, 1);
703                 wattron(sb.win, COLOR_PAIR(COLOR_STATUSBAR));
704                 wattron(sep.win, COLOR_PAIR(COLOR_SEPARATOR));
705                 wattron(bot.win, COLOR_PAIR(COLOR_BOT));
706                 wattron(top.win, COLOR_PAIR(COLOR_TOP));
707                 nodelay(top.win, 1);
708                 nodelay(bot.win, 1);
709                 nodelay(sb.win, 1);
710                 nodelay(in.win, 0);
711
712                 keypad(top.win, 1);
713                 keypad(bot.win, 1);
714                 keypad(sb.win, 1);
715                 keypad(in.win, 1);
716         }
717         wmove(sep.win, 0, 0);
718         whline(sep.win, theme.sep_char, COLS);
719         wclear(top.win);
720         print_all_items();
721         //wclear(bot.win);
722         wnoutrefresh(top.win);
723         wnoutrefresh(bot.win);
724         print_status_bar();
725         wnoutrefresh(sb.win);
726         wnoutrefresh(in.win);
727         wnoutrefresh(sep.win);
728         doupdate();
729 }
730
731 static void init_pair_or_die(short pair, short f, short b)
732 {
733         if (init_pair(pair, f, b) == ERR)
734                 die(EXIT_FAILURE, "fatal: init_pair() failed\n");
735 }
736
737 static void init_colors_or_die(void)
738 {
739         int i;
740
741         if (!has_colors())
742                 die(EXIT_FAILURE, "fatal: No color term\n");
743         if (start_color() == ERR)
744                 die(EXIT_FAILURE, "fatal: failed to start colors\n");
745         FOR_EACH_STATUS_ITEM(i)
746                 if (theme.data[i].len)
747                         init_pair_or_die(i + 1, theme.data[i].fg,
748                                 theme.data[i].bg);
749         init_pair_or_die(COLOR_STATUSBAR, theme.sb_fg, theme.sb_bg);
750         init_pair_or_die(COLOR_COMMAND, theme.cmd_fg, theme.cmd_bg);
751         init_pair_or_die(COLOR_OUTPUT, theme.output_fg, theme.output_bg);
752         init_pair_or_die(COLOR_MSG, theme.msg_fg, theme.msg_bg);
753         init_pair_or_die(COLOR_ERRMSG, theme.err_msg_fg, theme.err_msg_bg);
754         init_pair_or_die(COLOR_SEPARATOR, theme.sep_fg, theme.sep_bg);
755         init_pair_or_die(COLOR_TOP, theme.default_fg, theme.default_bg);
756         init_pair_or_die(COLOR_BOT, theme.default_fg, theme.default_bg);
757 }
758
759 /* (Re-)initialize the curses library. */
760 static void init_curses(void)
761 {
762         if (curses_active())
763                 return;
764         if (top.win && refresh() == ERR) /* refresh is really needed */
765                 die(EXIT_FAILURE, "refresh() failed\n");
766         if (LINES < theme.lines_min || COLS < theme.cols_min)
767                 die(EXIT_FAILURE, "Terminal (%dx%d) too small"
768                         " (need at least %dx%d)\n", COLS, LINES,
769                         theme.cols_min, theme.lines_min);
770         curs_set(0); /* make cursor invisible, ignore errors */
771         nonl(); /* do not NL->CR/NL on output, always returns OK */
772         /* don't echo input */
773         if (noecho() == ERR)
774                 die(EXIT_FAILURE, "fatal: noecho() failed\n");
775         /* take input chars one at a time, no wait for \n */
776         if (cbreak() == ERR)
777                 die(EXIT_FAILURE, "fatal: cbreak() failed\n");
778         init_colors_or_die();
779         clear(); /* ignore non-fatal errors */
780         init_wins(theme.top_lines_default);
781         // noecho(); /* don't echo input */
782 }
783
784 static void check_sigchld(void)
785 {
786         int ret;
787         pid_t pid;
788
789 reap_next_child:
790         ret = para_reap_child(&pid);
791         if (ret <= 0)
792                 return;
793         if (pid == exec_pid) {
794                 exec_pid = 0;
795                 init_curses();
796                 print_in_bar(COLOR_MSG, " ");
797         }
798         goto reap_next_child;
799 }
800
801 /*
802  * This sucker modifies its first argument. *handler and *arg are
803  * pointers to 0-terminated strings (inside line). Crap.
804  */
805 static int split_key_map(char *line, char **handler, char **arg)
806 {
807         if (!(*handler = strchr(line + 1, ':')))
808                 goto err_out;
809         **handler = '\0';
810         (*handler)++;
811         if (!(*arg = strchr(*handler, ':')))
812                 goto err_out;
813         **arg = '\0';
814         (*arg)++;
815         return 1;
816 err_out:
817         return 0;
818 }
819
820 static void check_key_map_args_or_die(void)
821 {
822         int i;
823         char *tmp = NULL;
824
825         for (i = 0; i < conf.key_map_given; ++i) {
826                 char *handler, *arg;
827
828                 free(tmp);
829                 tmp = para_strdup(conf.key_map_arg[i]);
830                 if (!split_key_map(tmp, &handler, &arg))
831                         break;
832                 if (strlen(handler) != 1)
833                         break;
834                 if (*handler != 'x' && *handler != 'd' && *handler != 'i'
835                                 && *handler != 'p')
836                         break;
837                 if (*handler != 'i')
838                         continue;
839                 if (find_cmd_byname(arg) < 0)
840                         break;
841         }
842         if (i != conf.key_map_given)
843                 die(EXIT_FAILURE, "invalid key map: %s\n", conf.key_map_arg[i]);
844         free(tmp);
845 }
846
847 static void parse_config_file_or_die(bool override)
848 {
849         bool err;
850         char *config_file;
851         struct gui_cmdline_parser_params params = {
852                 .override = override,
853                 .initialize = 0,
854                 .check_required = !override,
855                 .check_ambiguity = 0,
856                 .print_errors = 1,
857         };
858
859         if (conf.config_file_given)
860                 config_file = para_strdup(conf.config_file_arg);
861         else {
862                 char *home = para_homedir();
863                 config_file = make_message("%s/.paraslash/gui.conf", home);
864                 free(home);
865         }
866         if (!file_exists(config_file)) {
867                 if (!conf.config_file_given)
868                         err = false;
869                 else {
870                         PARA_EMERG_LOG("config file %s does not exist\n",
871                                 config_file);
872                         err = true;
873                 }
874                 goto out;
875         }
876         gui_cmdline_parser_config_file(config_file, &conf, &params);
877         loglevel = get_loglevel_by_name(conf.loglevel_arg);
878         check_key_map_args_or_die();
879         theme_init(conf.theme_arg, &theme);
880         err = false;
881 out:
882         free(config_file);
883         if (err)
884                 exit(EXIT_FAILURE);
885 }
886
887 /* reread configuration, terminate on errors */
888 static void reread_conf(void)
889 {
890         /*
891          * gengetopt might print to stderr and exit on errors. So we have to
892          * shutdown curses first.
893          */
894         shutdown_curses();
895         parse_config_file_or_die(true /* override */);
896         init_curses();
897         print_in_bar(COLOR_MSG, "config file reloaded\n");
898 }
899
900 /*
901  * React to various signal-related events
902  */
903 static int signal_post_select(struct sched *s, __a_unused struct task *t)
904 {
905         int ret = para_next_signal(&s->rfds);
906
907         if (ret <= 0)
908                 return 0;
909         switch (ret) {
910         case SIGTERM:
911                 die(EXIT_FAILURE, "only the good die young (caught SIGTERM)\n");
912                 return 1;
913         case SIGWINCH:
914                 if (curses_active()) {
915                         shutdown_curses();
916                         init_curses();
917                         redraw_bot_win();
918                 }
919                 return 1;
920         case SIGINT:
921                 PARA_WARNING_LOG("caught SIGINT, reset\n");
922                 /* Nothing to do. SIGINT killed our child which gets noticed
923                  * by do_select and resets everything.
924                  */
925                 return 1;
926         case SIGUSR1:
927                 PARA_NOTICE_LOG("got SIGUSR1, rereading configuration\n");
928                 reread_conf();
929                 return 1;
930         case SIGCHLD:
931                 check_sigchld();
932                 return 1;
933         }
934         return 1;
935 }
936
937 static enum exec_status exec_status(void)
938 {
939         if (exec_fds[0] >= 0 || exec_fds[1] >= 0)
940                 return EXEC_DCMD;
941         if (exec_pid > 0)
942                 return EXEC_XCMD;
943         return EXEC_IDLE;
944 }
945
946 static void exec_pre_select(struct sched *s, __a_unused struct task *t)
947 {
948         enum exec_status es = exec_status();
949
950         if (es != EXEC_DCMD)
951                 return;
952         if (exec_fds[0] >= 0)
953                 para_fd_set(exec_fds[0], &s->rfds, &s->max_fileno);
954         if (exec_fds[1] >= 0)
955                 para_fd_set(exec_fds[1], &s->rfds, &s->max_fileno);
956 }
957
958 static int exec_post_select(struct sched *s, struct task *t)
959 {
960         struct exec_task *ct = container_of(t, struct exec_task, task);
961         int i, ret;
962         enum exec_status es = exec_status();
963
964         if (es != EXEC_DCMD)
965                 return 0;
966         for (i = 0; i < 2; i++) {
967                 size_t sz;
968                 if (exec_fds[i] < 0)
969                         continue;
970                 ret = read_nonblock(exec_fds[i],
971                         ct->command_buf[i] + ct->cbo[i],
972                         COMMAND_BUF_SIZE - 1 - ct->cbo[i], &s->rfds, &sz);
973                 ct->cbo[i] += sz;
974                 sz = ct->cbo[i];
975                 ct->cbo[i] = for_each_line(ct->flags[i], ct->command_buf[i],
976                         ct->cbo[i], add_output_line, &i);
977                 if (sz != ct->cbo[i]) { /* at least one line found */
978                         refresh_window(&bot);
979                         ct->flags[i] = 0;
980                 }
981                 if (ret < 0 || exec_pid == 0) {
982                         if (ret < 0)
983                                 PARA_NOTICE_LOG("closing command fd %d: %s",
984                                         i, para_strerror(-ret));
985                         close(exec_fds[i]);
986                         exec_fds[i] = -1;
987                         ct->flags[i] = 0;
988                         ct->cbo[i] = 0;
989                         if (exec_fds[!i] < 0) /* both fds closed */
990                                 return 1;
991                 }
992                 if (ct->cbo[i] == COMMAND_BUF_SIZE - 1) {
993                         PARA_NOTICE_LOG("discarding overlong line");
994                         ct->cbo[i] = 0;
995                         ct->flags[i] = FELF_DISCARD_FIRST;
996                 }
997         }
998         return 0;
999 }
1000
1001 static void input_pre_select(struct sched *s, __a_unused struct task *t)
1002 {
1003         if (exec_status() != EXEC_XCMD)
1004                 para_fd_set(STDIN_FILENO, &s->rfds, &s->max_fileno);
1005         if (window_update_needed())
1006                 sched_min_delay(s);
1007 }
1008
1009 /* read from command pipe and print data to bot window */
1010 static void exec_and_display(const char *file_and_args)
1011 {
1012         int ret, fds[3] = {0, 1, 1};
1013
1014         outputf(COLOR_COMMAND, "%s", file_and_args);
1015         ret = para_exec_cmdline_pid(&exec_pid, file_and_args, fds);
1016         if (ret < 0)
1017                 return;
1018         ret = mark_fd_nonblocking(fds[1]);
1019         if (ret < 0)
1020                 goto fail;
1021         ret = mark_fd_nonblocking(fds[2]);
1022         if (ret < 0)
1023                 goto fail;
1024         exec_fds[0] = fds[1];
1025         exec_fds[1] = fds[2];
1026         print_in_bar(COLOR_MSG, "hit any key to abort\n");
1027         return;
1028 fail:
1029         PARA_ERROR_LOG("%s\n", para_strerror(-ret));
1030         close(exec_fds[0]);
1031         close(exec_fds[1]);
1032 }
1033
1034 static void exec_para(const char *args)
1035 {
1036         char *file_and_args;
1037
1038         file_and_args = make_message(BINDIR "/para_client -- %s", args);
1039         exec_and_display(file_and_args);
1040         free(file_and_args);
1041 }
1042
1043 /*
1044  * shutdown curses and stat pipe before executing external commands
1045  */
1046 static void exec_external(char *file_and_args)
1047 {
1048         int fds[3] = {-1, -1, -1};
1049
1050         if (exec_pid)
1051                 return;
1052         shutdown_curses();
1053         para_exec_cmdline_pid(&exec_pid, file_and_args, fds);
1054 }
1055
1056 static void handle_command(int c)
1057 {
1058         int i;
1059
1060         /* first check user-defined key bindings */
1061         for (i = 0; i < conf.key_map_given; ++i) {
1062                 char *tmp, *handler, *arg;
1063
1064                 tmp = para_strdup(conf.key_map_arg[i]);
1065                 if (!split_key_map(tmp, &handler, &arg)) {
1066                         free(tmp);
1067                         return;
1068                 }
1069                 if (strcmp(tmp, km_keyname(c))) {
1070                         free(tmp);
1071                         continue;
1072                 }
1073                 if (*handler == 'd')
1074                         exec_and_display(arg);
1075                 else if (*handler == 'x')
1076                         exec_external(arg);
1077                 else if (*handler == 'p')
1078                         exec_para(arg);
1079                 else if (*handler == 'i') {
1080                         int num = find_cmd_byname(arg);
1081                         if (num >= 0)
1082                                 command_list[num].handler();
1083                 }
1084                 free(tmp);
1085                 return;
1086         }
1087         /* not found, check internal key bindings */
1088         for (i = 0; command_list[i].handler; i++) {
1089                 if (!strcmp(km_keyname(c), command_list[i].key)) {
1090                         command_list[i].handler();
1091                         return;
1092                 }
1093         }
1094         print_in_bar(COLOR_ERRMSG, "key '%s' is not bound, press ? for help",
1095                 km_keyname(c));
1096 }
1097
1098 static int input_post_select(__a_unused struct sched *s, __a_unused struct task *t)
1099 {
1100         int ret;
1101         enum exec_status exs = exec_status();
1102
1103         if (exs == EXEC_XCMD)
1104                 return 0;
1105         if (window_update_needed()) {
1106                 if (top.needs_update)
1107                         assert(wnoutrefresh(top.win) == OK);
1108                 if (bot.needs_update)
1109                         assert(wnoutrefresh(bot.win) == OK);
1110                 if (sep.needs_update)
1111                         assert(wnoutrefresh(sep.win) == OK);
1112                 if (sb.needs_update)
1113                         assert(wnoutrefresh(sb.win) == OK);
1114                 if (in.needs_update)
1115                         assert(wnoutrefresh(in.win) == OK);
1116                 doupdate();
1117                 top.needs_update = bot.needs_update = sb.needs_update =
1118                         in.needs_update = sep.needs_update = false;
1119         }
1120         ret = wgetch(top.win);
1121         if (ret == ERR || ret == KEY_RESIZE)
1122                 return 0;
1123         if (exs == EXEC_IDLE) {
1124                 handle_command(ret);
1125                 return 0;
1126         }
1127         if (exec_pid != 0)
1128                 kill(exec_pid, SIGTERM);
1129         return 0;
1130 }
1131
1132 static void signal_pre_select(struct sched *s, struct task *t)
1133 {
1134         struct signal_task *st = container_of(t, struct signal_task, task);
1135         para_fd_set(st->fd, &s->rfds, &s->max_fileno);
1136 }
1137
1138 static void print_scroll_msg(void)
1139 {
1140         unsigned lines_total, filled = ringbuffer_filled(bot_win_rb);
1141         int first_rbe = first_visible_rbe(&lines_total);
1142
1143         print_in_bar(COLOR_MSG, "scrolled view: %d-%d/%d\n", filled - first_rbe,
1144                 filled - scroll_position, ringbuffer_filled(bot_win_rb));
1145 }
1146
1147 static void com_scroll_top(void)
1148 {
1149         int i = RINGBUFFER_SIZE - 1, bot_lines = get_num_lines(&bot);
1150         unsigned lines = 0;
1151
1152         while (i > 0 && !ringbuffer_get(bot_win_rb, i))
1153                 i--;
1154         /* i is oldest entry */
1155         for (; lines < bot_lines && i >= 0; i--) {
1156                 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
1157                 if (!rbe)
1158                         break;
1159                 lines += NUM_LINES(rbe->len);
1160         }
1161         i++;
1162         if (lines > 0 && scroll_position != i) {
1163                 scroll_position = i;
1164                 redraw_bot_win();
1165                 print_scroll_msg();
1166                 return;
1167         }
1168         print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1169 }
1170
1171 static void com_cancel_scroll(void)
1172 {
1173
1174         if (scroll_position == 0) {
1175                 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1176                 return;
1177         }
1178         scroll_position = 0;
1179         redraw_bot_win();
1180 }
1181
1182 static void com_page_down(void)
1183 {
1184         unsigned lines = 0;
1185         int i = scroll_position, bot_lines = get_num_lines(&bot);
1186
1187         while (lines < bot_lines && --i > 0) {
1188                 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
1189                 if (!rbe)
1190                         break;
1191                 lines += NUM_LINES(rbe->len);
1192         }
1193         if (lines) {
1194                 scroll_position = i;
1195                 redraw_bot_win();
1196                 print_scroll_msg();
1197                 return;
1198         }
1199         print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1200 }
1201
1202 static void com_page_up(void)
1203 {
1204         unsigned lines;
1205         int fvr = first_visible_rbe(&lines), bot_lines = get_num_lines(&bot);
1206
1207         if (fvr < 0 || fvr + 1 >= ringbuffer_filled(bot_win_rb)) {
1208                 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1209                 return;
1210         }
1211         scroll_position = fvr + 1;
1212         for (; scroll_position > 0; scroll_position--) {
1213                 first_visible_rbe(&lines);
1214                 if (lines == bot_lines)
1215                         break;
1216         }
1217         redraw_bot_win();
1218         print_scroll_msg();
1219 }
1220
1221 static void com_scroll_down(void)
1222 {
1223         struct rb_entry *rbe;
1224         int rbe_lines, bot_lines = get_num_lines(&bot);
1225
1226         if (!scroll_position) {
1227                 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1228                 return;
1229         }
1230         scroll_position--;
1231         rbe = ringbuffer_get(bot_win_rb, scroll_position);
1232         rbe_lines = NUM_LINES(rbe->len);
1233         wscrl(bot.win, rbe_lines);
1234         wmove(bot.win, bot_lines - rbe_lines, 0);
1235         wattron(bot.win, COLOR_PAIR(rbe->color));
1236         waddstr(bot.win, rbe->msg);
1237         refresh_window(&bot);
1238         print_scroll_msg();
1239 }
1240
1241 static void com_scroll_up(void)
1242 {
1243         struct rb_entry *rbe = NULL;
1244         unsigned lines;
1245         int i, first_rbe, num_scroll;
1246
1247         /* the entry that is going to vanish */
1248         rbe = ringbuffer_get(bot_win_rb, scroll_position);
1249         if (!rbe)
1250                 goto err_out;
1251         num_scroll = NUM_LINES(rbe->len);
1252         first_rbe = first_visible_rbe(&lines);
1253         if (first_rbe < 0 || (first_rbe == ringbuffer_filled(bot_win_rb) - 1))
1254                 goto err_out;
1255         scroll_position++;
1256         wscrl(bot.win, -num_scroll);
1257         i = draw_top_rbe(&lines);
1258         if (i < 0)
1259                 goto err_out;
1260         while (i > 0 && lines < num_scroll) {
1261                 int rbe_lines;
1262                 rbe = ringbuffer_get(bot_win_rb, --i);
1263                 if (!rbe)
1264                         break;
1265                 rbe_lines = NUM_LINES(rbe->len);
1266                 lines += rbe_lines;
1267                 wattron(bot.win, COLOR_PAIR(rbe->color));
1268                 waddstr(bot.win, "\n");
1269                 waddstr(bot.win, rbe->msg);
1270                 if (!i)
1271                         break;
1272                 i--;
1273         }
1274         refresh_window(&bot);
1275         print_scroll_msg();
1276         return;
1277 err_out:
1278         print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1279 }
1280
1281 static void com_ll_decr(void)
1282 {
1283         if (loglevel <= LL_DEBUG) {
1284                 print_in_bar(COLOR_ERRMSG,
1285                         "loglevel already at maximal verbosity\n");
1286                 return;
1287         }
1288         loglevel--;
1289         print_in_bar(COLOR_MSG, "loglevel set to %d\n", loglevel);
1290 }
1291
1292 static void com_ll_incr(void)
1293 {
1294         if (loglevel >= LL_EMERG) {
1295                 print_in_bar(COLOR_ERRMSG,
1296                         "loglevel already at minimal verbosity\n");
1297                 return;
1298         }
1299         loglevel++;
1300         print_in_bar(COLOR_MSG, "loglevel set to %d\n", loglevel);
1301 }
1302
1303 static void com_reread_conf(void)
1304 {
1305         reread_conf();
1306 }
1307
1308 static void com_help(void)
1309 {
1310         int i;
1311
1312         for (i = 0; i < conf.key_map_given; ++i) {
1313                 char *handler, *arg, *tmp = para_strdup(conf.key_map_arg[i]);
1314                 const char *handler_text = "???", *desc = NULL;
1315
1316                 if (!split_key_map(tmp, &handler, &arg)) {
1317                         free(tmp);
1318                         return;
1319                 }
1320                 switch (*handler) {
1321                         case 'i':
1322                                 handler_text = "internal";
1323                                 desc = command_list[find_cmd_byname(arg)].description;
1324                                 break;
1325                         case 'x': handler_text = "external"; break;
1326                         case 'd': handler_text = "display "; break;
1327                         case 'p': handler_text = "para    "; break;
1328                 }
1329                 outputf(COLOR_MSG, "%s\t%s\t%s%s\t%s", tmp, handler_text, arg,
1330                         strlen(arg) < 8? "\t" : "",
1331                         desc? desc : "");
1332                 free(tmp);
1333         }
1334         for (i = 0; command_list[i].handler; i++) {
1335                 struct gui_command gc = command_list[i];
1336
1337                 outputf(COLOR_MSG, "%s\tinternal\t%s\t%s%s", gc.key, gc.name,
1338                         strlen(gc.name) < 8? "\t" : "",
1339                         gc.description);
1340         }
1341         print_in_bar(COLOR_MSG, "try \"para_gui -h\" or \"para_client help\" "
1342                 "for more info");
1343 }
1344
1345 static void com_shrink_top_win(void)
1346 {
1347         int top_lines = get_num_lines(&top);
1348
1349         if (top_lines <= theme.top_lines_min) {
1350                 PARA_WARNING_LOG("can not decrease top window\n");
1351                 return;
1352         }
1353         init_wins(top_lines - 1);
1354         print_in_bar(COLOR_MSG, "%s", "decreased top window");
1355 }
1356
1357 static void com_enlarge_top_win(void)
1358 {
1359         int top_lines = get_num_lines(&top), bot_lines = get_num_lines(&bot);
1360
1361         if (bot_lines < 3) {
1362                 PARA_WARNING_LOG("can not increase top window\n");
1363                 return;
1364         }
1365         init_wins(top_lines + 1);
1366         print_in_bar(COLOR_MSG, "increased top window");
1367 }
1368
1369 static void com_version(void)
1370 {
1371         print_in_bar(COLOR_MSG, "%s", version_single_line("gui"));
1372 }
1373
1374 __noreturn static void com_quit(void)
1375 {
1376         die(EXIT_SUCCESS, "%s", "");
1377 }
1378
1379 static void com_refresh(void)
1380 {
1381         shutdown_curses();
1382         init_curses();
1383 }
1384
1385 static void com_next_theme(void)
1386 {
1387         theme_next(&theme);
1388         com_refresh();
1389 }
1390
1391 static void com_prev_theme(void)
1392 {
1393         theme_prev(&theme);
1394         com_refresh();
1395 }
1396
1397 __noreturn static void print_help_and_die(void)
1398 {
1399         struct ggo_help h = DEFINE_GGO_HELP(gui);
1400         bool d = conf.detailed_help_given;
1401
1402         ggo_print_help(&h, d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS);
1403         exit(0);
1404 }
1405
1406 static int setup_tasks_and_schedule(void)
1407 {
1408         struct sched sched = {
1409                 .default_timeout = {
1410                         .tv_sec = conf.timeout_arg  / 1000,
1411                         .tv_usec = (conf.timeout_arg % 1000) * 1000,
1412                 },
1413         };
1414         struct exec_task exec_task = {
1415                 .task = {
1416                         .status = "exec",
1417                         .pre_select = exec_pre_select,
1418                         .post_select = exec_post_select,
1419                 },
1420         };
1421         struct status_task status_task = {
1422                 .task = {
1423                         .status = "status",
1424                         .pre_select = status_pre_select,
1425                         .post_select = status_post_select,
1426                 },
1427                 .fd = -1
1428         };
1429         struct input_task input_task = {
1430                 .task = {
1431                         .status = "input",
1432                         .pre_select = input_pre_select,
1433                         .post_select = input_post_select,
1434                 },
1435         };
1436         struct signal_task signal_task = {
1437                 .task = {
1438                         .status = "signal",
1439                         .pre_select = signal_pre_select,
1440                         .post_select = signal_post_select,
1441                 },
1442         };
1443         signal_task.fd = para_signal_init();
1444         para_install_sighandler(SIGINT);
1445         para_install_sighandler(SIGTERM);
1446         para_install_sighandler(SIGCHLD);
1447         para_install_sighandler(SIGWINCH);
1448         para_install_sighandler(SIGUSR1);
1449
1450         register_task(&sched, &exec_task.task);
1451         register_task(&sched, &status_task.task);
1452         register_task(&sched, &input_task.task);
1453         register_task(&sched, &signal_task.task);
1454         return schedule(&sched);
1455 }
1456
1457 /**
1458  * The main function of para_gui.
1459  *
1460  * \param argc Usual argument count.
1461  * \param argv Usual argument vector.
1462  *
1463  * After initialization para_gui registers the following tasks to the paraslash
1464  * scheduler: status, exec, signal, input.
1465  *
1466  * The status task executes the para_audioc stat command to obtain the status
1467  * of para_server and para_audiod, and displays this information in the top
1468  * window of para_gui.
1469  *
1470  * The exec task is responsible for printing the output of the currently
1471  * running executable to the bottom window.
1472  *
1473  * The signal task performs suitable actions according to any signals received.
1474  * For example it refreshes all windows on terminal size changes and resets the
1475  * terminal on \p SIGTERM.
1476  *
1477  * The input task reads single key strokes from stdin. For each key pressed, it
1478  * executes the command handler associated with this key.
1479  *
1480  * \return \p EXIT_SUCCESS or \p EXIT_FAILURE.
1481  */
1482 int main(int argc, char *argv[])
1483 {
1484         gui_cmdline_parser(argc, argv, &conf); /* exits on errors */
1485         loglevel = get_loglevel_by_name(conf.loglevel_arg);
1486         version_handle_flag("gui", conf.version_given);
1487         if (conf.help_given || conf.detailed_help_given)
1488                 print_help_and_die();
1489         parse_config_file_or_die(false /* override */);
1490         bot_win_rb = ringbuffer_new(RINGBUFFER_SIZE);
1491         setlocale(LC_CTYPE, "");
1492         initscr(); /* needed only once, always successful */
1493         init_curses();
1494         return setup_tasks_and_schedule() < 0? EXIT_FAILURE : EXIT_SUCCESS;
1495 }