2 * Copyright (C) 1998-2014 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file gui.c Curses-based interface for paraslash. */
11 #include <sys/types.h>
16 #include "gui.cmdline.h"
20 #include "ringbuffer.h"
29 /** define the array of error lists needed by para_gui */
31 static char *stat_content[NUM_STAT_ITEMS];
33 static struct gui_window {
36 } top, bot, sb, in, sep;
38 /** How many lines of output to remember. */
39 #define RINGBUFFER_SIZE 512
46 static struct ringbuffer *bot_win_rb;
48 static unsigned scroll_position;
50 static pid_t exec_pid;
52 static int exec_fds[2] = {-1, -1};
53 static struct gui_args_info conf;
56 /** Type of the process currently being executed. */
58 EXEC_IDLE, /**< No process running. */
59 EXEC_DCMD, /**< para or display process running. */
60 EXEC_XCMD, /**< External process running. */
64 * Codes for various colors.
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.
70 COLOR_STATUSBAR = NUM_STAT_ITEMS + 1,
83 const char *description;
84 void (*handler)(void);
87 static struct gui_theme theme;
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") \
108 /* declare command handlers */
109 #define GUI_COMMAND(_c, _k, _d) \
110 static void com_ ## _c(void);
115 /* define command array */
116 #define GUI_COMMAND(_c, _k, _d) \
121 .handler = com_ ## _c \
124 static struct gui_command command_list[] = {GUI_COMMANDS {.name = NULL}};
134 struct timeval next_exec;
138 /** Stdout/stderr of the executing process is read in chunks of this size. */
139 #define COMMAND_BUF_SIZE 32768
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() */
148 static int find_cmd_byname(char *name)
152 for (i = 0; command_list[i].handler; i++)
153 if (!strcmp(command_list[i].name, name))
159 * Even though ncurses provides getmaxx and getmaxy, these functions/macros are
160 * not described in the XSI Curses standard.
162 static int get_num_lines(struct gui_window *w)
165 __a_unused int cols; /* avoid "set but not used" warnings */
167 getmaxyx(w->win, lines, cols);
171 static int get_num_cols(struct gui_window *w)
173 __a_unused int lines; /* avoid "set but not used" warnings */
176 getmaxyx(w->win, lines, cols);
180 /** Number of lines of the window are occupied by an output line. */
181 #define NUM_LINES(len) (1 + (len) / get_num_cols(&bot))
183 /* isendwin() returns false before initscr() was called */
184 static bool curses_active(void)
186 return top.win && !isendwin();
189 /* taken from mutt */
190 static char *km_keyname(int c)
195 sprintf(buf, "<up>");
199 sprintf(buf, "<down>");
203 sprintf(buf, "<left>");
206 if (c == KEY_RIGHT) {
207 sprintf(buf, "<right>");
210 if (c == KEY_NPAGE) {
211 sprintf(buf, "<npage>");
214 if (c == KEY_PPAGE) {
215 sprintf(buf, "<ppage>");
219 sprintf(buf, "<home>");
223 sprintf(buf, "<end>");
226 if (c < 256 && c > -128 && iscntrl((unsigned char) c)) {
231 buf[1] = (c + '@') & 0x7f;
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);
239 snprintf(buf, sizeof(buf), "%c", (unsigned char) c);
241 snprintf(buf, sizeof(buf), "\\x%hx", (unsigned short) c);
245 /* Print given number of spaces to curses window. */
246 static void add_spaces(WINDOW* win, unsigned int num)
249 unsigned sz = sizeof(space) - 1; /* number of spaces */
263 * print aligned string to curses window. This function always prints
266 static int align_str(WINDOW* win, char *str, unsigned int len,
269 int ret, i, num; /* of spaces */
274 ret = strwidth(str, &width);
276 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
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')
292 add_spaces(win, num);
293 } else if (align == RIGHT) {
294 add_spaces(win, num);
297 add_spaces(win, num / 2);
298 waddstr(win, str[0]? str: "");
299 add_spaces(win, num - num / 2);
304 static void refresh_window(struct gui_window *gw)
306 gw->needs_update = true;
309 static bool window_update_needed(void)
311 return top.needs_update || bot.needs_update || sb.needs_update ||
312 in.needs_update || sep.needs_update;
315 __printf_2_3 static void print_in_bar(int color, const char *fmt,...)
320 if (!curses_active())
322 wattron(in.win, COLOR_PAIR(color));
324 xvasprintf(&msg, fmt, ap);
327 align_str(in.win, msg, get_num_cols(&in), LEFT);
332 static void print_status_bar(void)
336 tmp = para_strdup("para_gui " PACKAGE_VERSION " (hit ? for help)");
338 align_str(sb.win, tmp, get_num_cols(&sb), CENTER);
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.
347 static int first_visible_rbe(unsigned *lines)
349 int i, bot_lines = get_num_lines(&bot);
352 for (i = scroll_position; i < RINGBUFFER_SIZE; i++) {
353 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
357 rbe_lines = NUM_LINES(rbe->len);
358 if (rbe_lines > bot_lines)
361 if (*lines >= bot_lines)
364 return RINGBUFFER_SIZE - 1;
368 returns number of first visible rbe, *lines is the number of lines drawn.
370 static int draw_top_rbe(unsigned *lines)
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;
378 wmove(bot.win, 0, 0);
379 rbe = ringbuffer_get(bot_win_rb, fvr);
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);
389 ret = strwidth(rbe->msg + bytes_to_skip, &width);
396 wattron(bot.win, COLOR_PAIR(rbe->color));
397 waddstr(bot.win, rbe->msg + bytes_to_skip);
398 *lines = NUM_LINES(width);
402 static void redraw_bot_win(void)
405 int i, bot_lines = get_num_lines(&bot);
407 wmove(bot.win, 0, 0);
409 i = draw_top_rbe(&lines);
412 while (i > 0 && lines < bot_lines) {
413 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, --i);
416 waddstr(bot.win, "\n");
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);
425 refresh_window(&bot);
428 static void rb_add_entry(int color, char *msg)
430 struct rb_entry *old, *new;
434 if (strwidth(msg, &len) < 0)
436 new = para_malloc(sizeof(struct rb_entry));
440 old = ringbuffer_add(bot_win_rb, new);
445 if (scroll_position) {
446 /* discard current scrolling, like xterm does */
451 wattron(bot.win, COLOR_PAIR(color));
452 getyx(bot.win, y, x);
454 waddstr(bot.win, "\n");
455 waddstr(bot.win, msg);
459 * print formated output to bot win and refresh
461 __printf_2_3 static void outputf(int color, const char* fmt,...)
466 if (!curses_active())
469 xvasprintf(&msg, fmt, ap);
471 rb_add_entry(color, msg);
472 refresh_window(&bot);
475 static int add_output_line(char *line, void *data)
477 int color = *(int *)data? COLOR_ERRMSG : COLOR_OUTPUT;
479 if (!curses_active())
481 rb_add_entry(color, para_strdup(line));
485 static __printf_2_3 void curses_log(int ll, const char *fmt,...)
492 if (curses_active()) {
493 int color = ll <= LL_NOTICE? COLOR_MSG : COLOR_ERRMSG;
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);
504 /** The log function of para_gui, always set to curses_log(). */
505 __printf_2_3 void (*para_log)(int, const char*, ...) = curses_log;
507 static void shutdown_curses(void)
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, ...)
520 vfprintf(stderr, fmt, argp);
522 /* kill every process in the process group and exit */
523 para_sigaction(SIGTERM, SIG_IGN);
529 * Print stat item #i to curses window
531 static void print_stat_item(int i)
534 struct stat_item_data d = theme.data[i];
535 char *c = stat_content[i];
536 int top_lines = get_num_lines(&top);
538 if (!curses_active() || !d.len || !c)
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);
545 refresh_window(&top);
548 static int update_item(int item_num, char *buf)
550 char **c = stat_content + item_num;
557 *c = para_strdup("(artist tag not set)");
560 *c = para_strdup("(title tag not set)");
563 *c = para_strdup("????");
566 *c = para_strdup("(album tag not set)");
569 *c = para_strdup("(comment tag not set)");
573 *c = para_strdup(buf);
575 print_stat_item(item_num);
579 static void print_all_items(void)
583 if (!curses_active())
585 FOR_EACH_STATUS_ITEM(i)
589 static void clear_all_items(void)
593 FOR_EACH_STATUS_ITEM(i) {
594 free(stat_content[i]);
595 stat_content[i] = para_strdup("");
599 static void status_pre_select(struct sched *s, struct task *t)
601 struct status_task *st = container_of(t, struct status_task, task);
604 return para_fd_set(st->fd, &s->rfds, &s->max_fileno);
605 sched_request_barrier_or_min_delay(&st->next_exec, s);
608 static int status_post_select(struct sched *s, struct task *t)
610 struct status_task *st = container_of(t, struct status_task, task);
616 int fds[3] = {0, 1, 0};
617 /* Avoid busy loop */
618 if (tv_diff(&st->next_exec, now, NULL) > 0)
620 st->next_exec.tv_sec = now->tv_sec + 2;
621 ret = para_exec_cmdline_pid(&pid, conf.stat_cmd_arg, fds);
624 ret = mark_fd_nonblocking(fds[1]);
633 if (st->loaded >= st->bufsize) {
634 if (st->bufsize > 1000 * 1000) {
638 st->bufsize += st->bufsize + 1000;
639 st->buf = para_realloc(st->buf, st->bufsize);
641 assert(st->loaded < st->bufsize);
642 ret = read_nonblock(st->fd, st->buf + st->loaded,
643 st->bufsize - st->loaded, &s->rfds, &sz);
645 ret2 = for_each_stat_item(st->buf, st->loaded, update_item);
646 if (ret < 0 || ret2 < 0) {
648 PARA_NOTICE_LOG("closing stat pipe: %s\n", para_strerror(-ret));
652 free(stat_content[SI_BASENAME]);
653 stat_content[SI_BASENAME] =
654 para_strdup("stat command terminated!?");
658 sz = ret2; /* what is left */
659 if (sz > 0 && sz < st->loaded)
660 memmove(st->buf, st->buf + st->loaded - sz, sz);
668 static void init_wins(int top_lines)
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,
675 assume_default_colors(theme.default_fg, theme.default_bg);
677 wresize(top.win, top_lines, COLS);
678 mvwin(top.win, top_y, 0);
680 wresize(sb.win, sb_lines, COLS);
681 mvwin(sb.win, sb_y, 0);
683 wresize(sep.win, sep_lines, COLS);
684 mvwin(sep.win, sep_y, 0);
686 wresize(bot.win, bot_lines, COLS);
687 mvwin(bot.win, bot_y, 0);
689 wresize(in.win, in_lines, COLS);
690 mvwin(in.win, in_y, 0);
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");
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));
717 wmove(sep.win, 0, 0);
718 whline(sep.win, theme.sep_char, COLS);
722 wnoutrefresh(top.win);
723 wnoutrefresh(bot.win);
725 wnoutrefresh(sb.win);
726 wnoutrefresh(in.win);
727 wnoutrefresh(sep.win);
731 static void init_pair_or_die(short pair, short f, short b)
733 if (init_pair(pair, f, b) == ERR)
734 die(EXIT_FAILURE, "fatal: init_pair() failed\n");
737 static void init_colors_or_die(void)
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,
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);
759 /* (Re-)initialize the curses library. */
760 static void init_curses(void)
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 */
774 die(EXIT_FAILURE, "fatal: noecho() failed\n");
775 /* take input chars one at a time, no wait for \n */
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 */
784 static void check_sigchld(void)
790 ret = para_reap_child(&pid);
793 if (pid == exec_pid) {
796 print_in_bar(COLOR_MSG, " ");
798 goto reap_next_child;
802 * This sucker modifies its first argument. *handler and *arg are
803 * pointers to 0-terminated strings (inside line). Crap.
805 static int split_key_map(char *line, char **handler, char **arg)
807 if (!(*handler = strchr(line + 1, ':')))
811 if (!(*arg = strchr(*handler, ':')))
820 static void check_key_map_args_or_die(void)
825 for (i = 0; i < conf.key_map_given; ++i) {
829 tmp = para_strdup(conf.key_map_arg[i]);
830 if (!split_key_map(tmp, &handler, &arg))
832 if (strlen(handler) != 1)
834 if (*handler != 'x' && *handler != 'd' && *handler != 'i'
839 if (find_cmd_byname(arg) < 0)
842 if (i != conf.key_map_given)
843 die(EXIT_FAILURE, "invalid key map: %s\n", conf.key_map_arg[i]);
847 static void parse_config_file_or_die(bool override)
851 struct gui_cmdline_parser_params params = {
852 .override = override,
854 .check_required = !override,
855 .check_ambiguity = 0,
859 if (conf.config_file_given)
860 config_file = para_strdup(conf.config_file_arg);
862 char *home = para_homedir();
863 config_file = make_message("%s/.paraslash/gui.conf", home);
866 if (!file_exists(config_file)) {
867 if (!conf.config_file_given)
870 PARA_EMERG_LOG("config file %s does not exist\n",
876 gui_cmdline_parser_config_file(config_file, &conf, ¶ms);
877 loglevel = get_loglevel_by_name(conf.loglevel_arg);
878 check_key_map_args_or_die();
879 theme_init(conf.theme_arg, &theme);
887 /* reread configuration, terminate on errors */
888 static void reread_conf(void)
891 * gengetopt might print to stderr and exit on errors. So we have to
892 * shutdown curses first.
895 parse_config_file_or_die(true /* override */);
897 print_in_bar(COLOR_MSG, "config file reloaded\n");
901 * React to various signal-related events
903 static int signal_post_select(struct sched *s, __a_unused struct task *t)
905 int ret = para_next_signal(&s->rfds);
911 die(EXIT_FAILURE, "only the good die young (caught SIGTERM)\n");
914 if (curses_active()) {
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.
927 PARA_NOTICE_LOG("got SIGUSR1, rereading configuration\n");
937 static enum exec_status exec_status(void)
939 if (exec_fds[0] >= 0 || exec_fds[1] >= 0)
946 static void exec_pre_select(struct sched *s, __a_unused struct task *t)
948 enum exec_status es = exec_status();
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);
958 static int exec_post_select(struct sched *s, struct task *t)
960 struct exec_task *ct = container_of(t, struct exec_task, task);
962 enum exec_status es = exec_status();
966 for (i = 0; i < 2; i++) {
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);
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);
981 if (ret < 0 || exec_pid == 0) {
983 PARA_NOTICE_LOG("closing command fd %d: %s",
984 i, para_strerror(-ret));
989 if (exec_fds[!i] < 0) /* both fds closed */
992 if (ct->cbo[i] == COMMAND_BUF_SIZE - 1) {
993 PARA_NOTICE_LOG("discarding overlong line");
995 ct->flags[i] = FELF_DISCARD_FIRST;
1001 static void input_pre_select(struct sched *s, __a_unused struct task *t)
1003 if (exec_status() != EXEC_XCMD)
1004 para_fd_set(STDIN_FILENO, &s->rfds, &s->max_fileno);
1005 if (window_update_needed())
1009 /* read from command pipe and print data to bot window */
1010 static void exec_and_display(const char *file_and_args)
1012 int ret, fds[3] = {0, 1, 1};
1014 outputf(COLOR_COMMAND, "%s", file_and_args);
1015 ret = para_exec_cmdline_pid(&exec_pid, file_and_args, fds);
1018 ret = mark_fd_nonblocking(fds[1]);
1021 ret = mark_fd_nonblocking(fds[2]);
1024 exec_fds[0] = fds[1];
1025 exec_fds[1] = fds[2];
1026 print_in_bar(COLOR_MSG, "hit any key to abort\n");
1029 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
1034 static void exec_para(const char *args)
1036 char *file_and_args;
1038 file_and_args = make_message(BINDIR "/para_client -- %s", args);
1039 exec_and_display(file_and_args);
1040 free(file_and_args);
1044 * shutdown curses and stat pipe before executing external commands
1046 static void exec_external(char *file_and_args)
1048 int fds[3] = {-1, -1, -1};
1053 para_exec_cmdline_pid(&exec_pid, file_and_args, fds);
1056 static void handle_command(int c)
1060 /* first check user-defined key bindings */
1061 for (i = 0; i < conf.key_map_given; ++i) {
1062 char *tmp, *handler, *arg;
1064 tmp = para_strdup(conf.key_map_arg[i]);
1065 if (!split_key_map(tmp, &handler, &arg)) {
1069 if (strcmp(tmp, km_keyname(c))) {
1073 if (*handler == 'd')
1074 exec_and_display(arg);
1075 else if (*handler == 'x')
1077 else if (*handler == 'p')
1079 else if (*handler == 'i') {
1080 int num = find_cmd_byname(arg);
1082 command_list[num].handler();
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();
1094 print_in_bar(COLOR_ERRMSG, "key '%s' is not bound, press ? for help",
1098 static int input_post_select(__a_unused struct sched *s, __a_unused struct task *t)
1101 enum exec_status exs = exec_status();
1103 if (exs == EXEC_XCMD)
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);
1117 top.needs_update = bot.needs_update = sb.needs_update =
1118 in.needs_update = sep.needs_update = false;
1120 ret = wgetch(top.win);
1121 if (ret == ERR || ret == KEY_RESIZE)
1123 if (exs == EXEC_IDLE) {
1124 handle_command(ret);
1128 kill(exec_pid, SIGTERM);
1132 static void signal_pre_select(struct sched *s, struct task *t)
1134 struct signal_task *st = container_of(t, struct signal_task, task);
1135 para_fd_set(st->fd, &s->rfds, &s->max_fileno);
1138 static void print_scroll_msg(void)
1140 unsigned lines_total, filled = ringbuffer_filled(bot_win_rb);
1141 int first_rbe = first_visible_rbe(&lines_total);
1143 print_in_bar(COLOR_MSG, "scrolled view: %d-%d/%d\n", filled - first_rbe,
1144 filled - scroll_position, ringbuffer_filled(bot_win_rb));
1147 static void com_scroll_top(void)
1149 int i = RINGBUFFER_SIZE - 1, bot_lines = get_num_lines(&bot);
1152 while (i > 0 && !ringbuffer_get(bot_win_rb, 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);
1159 lines += NUM_LINES(rbe->len);
1162 if (lines > 0 && scroll_position != i) {
1163 scroll_position = i;
1168 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1171 static void com_cancel_scroll(void)
1174 if (scroll_position == 0) {
1175 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1178 scroll_position = 0;
1182 static void com_page_down(void)
1185 int i = scroll_position, bot_lines = get_num_lines(&bot);
1187 while (lines < bot_lines && --i > 0) {
1188 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
1191 lines += NUM_LINES(rbe->len);
1194 scroll_position = i;
1199 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1202 static void com_page_up(void)
1205 int fvr = first_visible_rbe(&lines), bot_lines = get_num_lines(&bot);
1207 if (fvr < 0 || fvr + 1 >= ringbuffer_filled(bot_win_rb)) {
1208 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1211 scroll_position = fvr + 1;
1212 for (; scroll_position > 0; scroll_position--) {
1213 first_visible_rbe(&lines);
1214 if (lines == bot_lines)
1221 static void com_scroll_down(void)
1223 struct rb_entry *rbe;
1224 int rbe_lines, bot_lines = get_num_lines(&bot);
1226 if (!scroll_position) {
1227 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
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);
1241 static void com_scroll_up(void)
1243 struct rb_entry *rbe = NULL;
1245 int i, first_rbe, num_scroll;
1247 /* the entry that is going to vanish */
1248 rbe = ringbuffer_get(bot_win_rb, scroll_position);
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))
1256 wscrl(bot.win, -num_scroll);
1257 i = draw_top_rbe(&lines);
1260 while (i > 0 && lines < num_scroll) {
1262 rbe = ringbuffer_get(bot_win_rb, --i);
1265 rbe_lines = NUM_LINES(rbe->len);
1267 wattron(bot.win, COLOR_PAIR(rbe->color));
1268 waddstr(bot.win, "\n");
1269 waddstr(bot.win, rbe->msg);
1274 refresh_window(&bot);
1278 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1281 static void com_ll_decr(void)
1283 if (loglevel <= LL_DEBUG) {
1284 print_in_bar(COLOR_ERRMSG,
1285 "loglevel already at maximal verbosity\n");
1289 print_in_bar(COLOR_MSG, "loglevel set to %d\n", loglevel);
1292 static void com_ll_incr(void)
1294 if (loglevel >= LL_EMERG) {
1295 print_in_bar(COLOR_ERRMSG,
1296 "loglevel already at minimal verbosity\n");
1300 print_in_bar(COLOR_MSG, "loglevel set to %d\n", loglevel);
1303 static void com_reread_conf(void)
1308 static void com_help(void)
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;
1316 if (!split_key_map(tmp, &handler, &arg)) {
1322 handler_text = "internal";
1323 desc = command_list[find_cmd_byname(arg)].description;
1325 case 'x': handler_text = "external"; break;
1326 case 'd': handler_text = "display "; break;
1327 case 'p': handler_text = "para "; break;
1329 outputf(COLOR_MSG, "%s\t%s\t%s%s\t%s", tmp, handler_text, arg,
1330 strlen(arg) < 8? "\t" : "",
1334 for (i = 0; command_list[i].handler; i++) {
1335 struct gui_command gc = command_list[i];
1337 outputf(COLOR_MSG, "%s\tinternal\t%s\t%s%s", gc.key, gc.name,
1338 strlen(gc.name) < 8? "\t" : "",
1341 print_in_bar(COLOR_MSG, "try \"para_gui -h\" or \"para_client help\" "
1345 static void com_shrink_top_win(void)
1347 int top_lines = get_num_lines(&top);
1349 if (top_lines <= theme.top_lines_min) {
1350 PARA_WARNING_LOG("can not decrease top window\n");
1353 init_wins(top_lines - 1);
1354 print_in_bar(COLOR_MSG, "%s", "decreased top window");
1357 static void com_enlarge_top_win(void)
1359 int top_lines = get_num_lines(&top), bot_lines = get_num_lines(&bot);
1361 if (bot_lines < 3) {
1362 PARA_WARNING_LOG("can not increase top window\n");
1365 init_wins(top_lines + 1);
1366 print_in_bar(COLOR_MSG, "increased top window");
1369 static void com_version(void)
1371 print_in_bar(COLOR_MSG, "%s", version_single_line("gui"));
1374 __noreturn static void com_quit(void)
1376 die(EXIT_SUCCESS, "%s", "");
1379 static void com_refresh(void)
1385 static void com_next_theme(void)
1391 static void com_prev_theme(void)
1397 __noreturn static void print_help_and_die(void)
1399 struct ggo_help h = DEFINE_GGO_HELP(gui);
1400 bool d = conf.detailed_help_given;
1402 ggo_print_help(&h, d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS);
1406 static int setup_tasks_and_schedule(void)
1408 struct sched sched = {
1409 .default_timeout = {
1410 .tv_sec = conf.timeout_arg / 1000,
1411 .tv_usec = (conf.timeout_arg % 1000) * 1000,
1414 struct exec_task exec_task = {
1417 .pre_select = exec_pre_select,
1418 .post_select = exec_post_select,
1421 struct status_task status_task = {
1424 .pre_select = status_pre_select,
1425 .post_select = status_post_select,
1429 struct input_task input_task = {
1432 .pre_select = input_pre_select,
1433 .post_select = input_post_select,
1436 struct signal_task signal_task = {
1439 .pre_select = signal_pre_select,
1440 .post_select = signal_post_select,
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);
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);
1458 * The main function of para_gui.
1460 * \param argc Usual argument count.
1461 * \param argv Usual argument vector.
1463 * After initialization para_gui registers the following tasks to the paraslash
1464 * scheduler: status, exec, signal, input.
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.
1470 * The exec task is responsible for printing the output of the currently
1471 * running executable to the bottom window.
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.
1477 * The input task reads single key strokes from stdin. For each key pressed, it
1478 * executes the command handler associated with this key.
1480 * \return \p EXIT_SUCCESS or \p EXIT_FAILURE.
1482 int main(int argc, char *argv[])
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 */
1494 return setup_tasks_and_schedule() < 0? EXIT_FAILURE : EXIT_SUCCESS;