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 int signal_pipe;
35 static struct gui_window {
37 } top, bot, sb, in, sep;
39 #define RINGBUFFER_SIZE 512
45 static struct ringbuffer *bot_win_rb;
47 static unsigned scroll_position;
51 static int command_fds[2] = {-1, -1};
52 static int stat_pipe = -1;
53 static struct gui_args_info conf;
56 enum gui_select_mode{GETCH_MODE, COMMAND_MODE, EXTERNAL_MODE};
59 * Codes for various colors.
61 * Each status item has its own color pair. The ones defined here start at a
62 * higher number so that they do not overlap with these.
65 COLOR_STATUSBAR = NUM_STAT_ITEMS + 1,
78 const char *description;
79 void (*handler)(void);
82 static struct gui_theme theme;
84 static void com_help(void);
85 static void com_reread_conf(void);
86 static void com_enlarge_top_win(void);
87 static void com_shrink_top_win(void);
88 static void com_version(void);
89 __noreturn static void com_quit(void);
90 static void com_refresh(void);
91 static void com_ll_incr(void);
92 static void com_ll_decr(void);
93 static void com_prev_theme(void);
94 static void com_next_theme(void);
95 static void com_scroll_up(void);
96 static void com_scroll_down(void);
97 static void com_page_up(void);
98 static void com_page_down(void);
99 static void com_cancel_scrolling(void);
100 static void com_scroll_top(void);
102 static struct gui_command command_list[] = {
106 .description = "print help",
110 .name = "enlarge_win",
111 .description = "enlarge the top window",
112 .handler = com_enlarge_top_win
115 .name = "shrink_win",
116 .description = "shrink the top window",
117 .handler = com_shrink_top_win
120 .name = "reread_conf",
121 .description = "reread configuration file",
122 .handler = com_reread_conf
126 .description = "exit para_gui",
131 .description = "redraw the screen",
132 .handler = com_refresh
135 .name = "next_theme",
136 .description = "switch to next theme",
137 .handler = com_next_theme
140 .name = "prev_theme",
141 .description = "switch to previous stream",
142 .handler = com_prev_theme
146 .description = "increase loglevel (decreases verbosity)",
147 .handler = com_ll_incr
151 .description = "decrease loglevel (increases verbosity)",
152 .handler = com_ll_decr
156 .description = "show the para_gui version",
157 .handler = com_version
161 .description = "scroll up one line",
162 .handler = com_scroll_up
165 .name = "scroll_down",
166 .description = "scroll down one line",
167 .handler = com_scroll_down
171 .description = "scroll up one page",
172 .handler = com_page_up
176 .description = "scroll down one page",
177 .handler = com_page_down
180 .name = "scroll_top",
181 .description = "scroll to top of buffer",
182 .handler = com_scroll_top
185 .name = "cancel_scroll",
186 .description = "deactivate scroll mode",
187 .handler = com_cancel_scrolling
193 static int find_cmd_byname(char *name)
197 for (i = 0; command_list[i].handler; i++)
198 if (!strcmp(command_list[i].name, name))
204 * Even though ncurses provides getmaxx and getmaxy, these functions/macros are
205 * not described in the XSI Curses standard.
207 static int get_num_lines(struct gui_window *w)
210 __a_unused int cols; /* avoid "set but not used" warnings */
212 getmaxyx(w->win, lines, cols);
216 static int get_num_cols(struct gui_window *w)
218 __a_unused int lines; /* avoid "set but not used" warnings */
221 getmaxyx(w->win, lines, cols);
225 /** Number of lines of the window are occupied by an output line. */
226 #define NUM_LINES(len) (1 + (len) / get_num_cols(&bot))
228 /* isendwin() returns false before initscr() was called */
229 static bool curses_active(void)
231 return top.win && !isendwin();
234 /* taken from mutt */
235 static char *km_keyname(int c)
240 sprintf(buf, "<up>");
244 sprintf(buf, "<down>");
248 sprintf(buf, "<left>");
251 if (c == KEY_RIGHT) {
252 sprintf(buf, "<right>");
255 if (c == KEY_NPAGE) {
256 sprintf(buf, "<npage>");
259 if (c == KEY_PPAGE) {
260 sprintf(buf, "<ppage>");
264 sprintf(buf, "<home>");
268 sprintf(buf, "<end>");
271 if (c < 256 && c > -128 && iscntrl((unsigned char) c)) {
276 buf[1] = (c + '@') & 0x7f;
279 snprintf(buf, sizeof(buf), "\\%d%d%d", c >> 6,
280 (c >> 3) & 7, c & 7);
281 } else if (c >= KEY_F0 && c < KEY_F(256))
282 sprintf(buf, "<F%d>", c - KEY_F0);
284 snprintf(buf, sizeof(buf), "%c", (unsigned char) c);
286 snprintf(buf, sizeof(buf), "\\x%hx", (unsigned short) c);
290 /* Print given number of spaces to curses window. */
291 static void add_spaces(WINDOW* win, unsigned int num)
294 unsigned sz = sizeof(space) - 1; /* number of spaces */
308 * print aligned string to curses window. This function always prints
311 static int align_str(WINDOW* win, char *str, unsigned int len,
314 int ret, i, num; /* of spaces */
319 ret = strwidth(str, &width);
321 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
330 /* replace control characters by spaces */
331 for (i = 0; i < len && str[i]; i++) {
332 if (str[i] == '\n' || str[i] == '\r' || str[i] == '\f')
337 add_spaces(win, num);
338 } else if (align == RIGHT) {
339 add_spaces(win, num);
342 add_spaces(win, num / 2);
343 waddstr(win, str[0]? str: "");
344 add_spaces(win, num - num / 2);
349 __printf_2_3 static void print_in_bar(int color, const char *fmt,...)
354 if (!curses_active())
356 wattron(in.win, COLOR_PAIR(color));
358 xvasprintf(&msg, fmt, ap);
361 align_str(in.win, msg, get_num_cols(&in), LEFT);
366 static void print_status_bar(void)
370 tmp = para_strdup("para_gui " PACKAGE_VERSION " (hit ? for help)");
372 align_str(sb.win, tmp, get_num_cols(&sb), CENTER);
377 * get the number of the oldest rbe that is (partially) visible. On return,
378 * lines contains the sum of the number of lines of all visible entries. If the
379 * first one is only partially visible, lines is greater than bot.lines.
381 static int first_visible_rbe(unsigned *lines)
383 int i, bot_lines = get_num_lines(&bot);
386 for (i = scroll_position; i < RINGBUFFER_SIZE; i++) {
387 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
391 rbe_lines = NUM_LINES(rbe->len);
392 if (rbe_lines > bot_lines)
395 if (*lines >= bot_lines)
398 return RINGBUFFER_SIZE - 1;
402 returns number of first visible rbe, *lines is the number of lines drawn.
404 static int draw_top_rbe(unsigned *lines)
406 int bot_cols, bot_lines, ret, fvr = first_visible_rbe(lines);
407 struct rb_entry *rbe;
408 size_t bytes_to_skip, cells_to_skip, width;
412 wmove(bot.win, 0, 0);
413 rbe = ringbuffer_get(bot_win_rb, fvr);
416 getmaxyx(bot.win, bot_lines, bot_cols);
417 if (*lines > bot_lines) {
418 /* rbe is partially visible multi-line */
419 cells_to_skip = (*lines - bot_lines) * bot_cols;
420 ret = skip_cells(rbe->msg, cells_to_skip, &bytes_to_skip);
423 ret = strwidth(rbe->msg + bytes_to_skip, &width);
430 wattron(bot.win, COLOR_PAIR(rbe->color));
431 waddstr(bot.win, rbe->msg + bytes_to_skip);
432 *lines = NUM_LINES(width);
436 static void redraw_bot_win(void)
439 int i, bot_lines = get_num_lines(&bot);
441 wmove(bot.win, 0, 0);
443 i = draw_top_rbe(&lines);
446 while (i > 0 && lines < bot_lines) {
447 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, --i);
450 waddstr(bot.win, "\n");
453 lines += NUM_LINES(rbe->len);
454 wattron(bot.win, COLOR_PAIR(rbe->color));
455 waddstr(bot.win, "\n");
456 waddstr(bot.win, rbe->msg);
462 static void rb_add_entry(int color, char *msg)
464 struct rb_entry *old, *new;
468 if (strwidth(msg, &len) < 0)
470 new = para_malloc(sizeof(struct rb_entry));
474 old = ringbuffer_add(bot_win_rb, new);
479 if (scroll_position) {
480 /* discard current scrolling, like xterm does */
485 wattron(bot.win, COLOR_PAIR(color));
486 getyx(bot.win, y, x);
488 waddstr(bot.win, "\n");
489 waddstr(bot.win, msg);
493 * print formated output to bot win and refresh
495 __printf_2_3 static void outputf(int color, const char* fmt,...)
500 if (!curses_active())
503 xvasprintf(&msg, fmt, ap);
505 rb_add_entry(color, msg);
509 static int add_output_line(char *line, void *data)
511 int color = *(int *)data? COLOR_ERRMSG : COLOR_OUTPUT;
513 if (!curses_active())
515 rb_add_entry(color, para_strdup(line));
519 static __printf_2_3 void curses_log(int ll, const char *fmt,...)
526 if (curses_active()) {
527 int color = ll <= LL_NOTICE? COLOR_MSG : COLOR_ERRMSG;
529 unsigned bytes = xvasprintf(&msg, fmt, ap);
530 if (bytes > 0 && msg[bytes - 1] == '\n')
531 msg[bytes - 1] = '\0'; /* cut trailing newline */
532 rb_add_entry(color, msg);
534 } else if (cmd_pid <= 0) /* no external command running */
535 vfprintf(stderr, fmt, ap);
538 __printf_2_3 void (*para_log)(int, const char*, ...) = curses_log;
540 static void setup_signal_handling(void)
542 signal_pipe = para_signal_init();
543 para_install_sighandler(SIGINT);
544 para_install_sighandler(SIGTERM);
545 para_install_sighandler(SIGCHLD);
546 para_install_sighandler(SIGWINCH);
547 para_install_sighandler(SIGUSR1);
550 static void shutdown_curses(void)
556 /* disable curses, print a message, kill running processes and exit */
557 __noreturn __printf_2_3 static void die(int exit_code, const char* fmt, ...)
563 vfprintf(stderr, fmt, argp);
565 /* kill every process in the process group and exit */
566 para_sigaction(SIGTERM, SIG_IGN);
572 * Print stat item #i to curses window
574 static void print_stat_item(int i)
577 struct stat_item_data d = theme.data[i];
578 char *c = stat_content[i];
579 int top_lines = get_num_lines(&top);
581 if (!curses_active() || !d.len || !c)
583 tmp = make_message("%s%s%s", d.prefix, c, d.postfix);
584 wmove(top.win, d.y * top_lines / 100, d.x * COLS / 100);
586 wattron(top.win, COLOR_PAIR(i + 1));
587 align_str(top.win, tmp, d.len * COLS / 100, d.align);
592 static int update_item(int item_num, char *buf)
594 char **c = stat_content + item_num;
601 *c = para_strdup("(artist tag not set)");
604 *c = para_strdup("(title tag not set)");
607 *c = para_strdup("????");
610 *c = para_strdup("(album tag not set)");
613 *c = para_strdup("(comment tag not set)");
617 *c = para_strdup(buf);
619 print_stat_item(item_num);
623 static void print_all_items(void)
627 if (!curses_active())
629 FOR_EACH_STATUS_ITEM(i)
633 static void clear_all_items(void)
637 FOR_EACH_STATUS_ITEM(i) {
638 free(stat_content[i]);
639 stat_content[i] = para_strdup("");
643 static void status_post_select(fd_set *rfds)
646 static int bufsize, loaded;
652 if (loaded >= bufsize) {
653 if (bufsize > 1000 * 1000) {
657 bufsize += bufsize + 1000;
658 buf = para_realloc(buf, bufsize);
660 assert(loaded < bufsize);
661 ret = read_nonblock(stat_pipe, buf + loaded, bufsize - loaded,
664 ret2 = for_each_stat_item(buf, loaded, update_item);
665 if (ret < 0 || ret2 < 0) {
667 PARA_NOTICE_LOG("closing stat pipe: %s\n", para_strerror(-ret));
671 free(stat_content[SI_BASENAME]);
672 stat_content[SI_BASENAME] =
673 para_strdup("stat command terminated!?");
677 sz = ret2; /* what is left */
678 if (sz > 0 && sz < loaded)
679 memmove(buf, buf + loaded - sz, sz);
686 static void init_wins(int top_lines)
688 int top_y = 0, bot_y = top_lines + 1, sb_y = LINES - 2,
689 in_y = LINES - 1, sep_y = top_lines;
690 int bot_lines = LINES - top_lines - 3, sb_lines = 1, in_lines = 1,
693 assume_default_colors(theme.default_fg, theme.default_bg);
695 wresize(top.win, top_lines, COLS);
696 mvwin(top.win, top_y, 0);
698 wresize(sb.win, sb_lines, COLS);
699 mvwin(sb.win, sb_y, 0);
701 wresize(sep.win, sep_lines, COLS);
702 mvwin(sep.win, sep_y, 0);
704 wresize(bot.win, bot_lines, COLS);
705 mvwin(bot.win, bot_y, 0);
707 wresize(in.win, in_lines, COLS);
708 mvwin(in.win, in_y, 0);
710 sep.win = newwin(sep_lines, COLS, sep_y, 0);
711 top.win = newwin(top_lines, COLS, top_y, 0);
712 bot.win = newwin(bot_lines, COLS, bot_y, 0);
713 sb.win = newwin(sb_lines, COLS, sb_y, 0);
714 in.win = newwin(in_lines, COLS, in_y, 0);
715 if (!top.win || !bot.win || !sb.win || !in.win || !sep.win)
716 die(EXIT_FAILURE, "Error: Cannot create curses windows\n");
720 scrollok(bot.win, 1);
721 wattron(sb.win, COLOR_PAIR(COLOR_STATUSBAR));
722 wattron(sep.win, COLOR_PAIR(COLOR_SEPARATOR));
723 wattron(bot.win, COLOR_PAIR(COLOR_BOT));
724 wattron(top.win, COLOR_PAIR(COLOR_TOP));
735 wmove(sep.win, 0, 0);
736 whline(sep.win, theme.sep_char, COLS);
740 wnoutrefresh(top.win);
741 wnoutrefresh(bot.win);
743 wnoutrefresh(sb.win);
744 wnoutrefresh(in.win);
745 wnoutrefresh(sep.win);
749 static void init_pair_or_die(short pair, short f, short b)
751 if (init_pair(pair, f, b) == ERR)
752 die(EXIT_FAILURE, "fatal: init_pair() failed\n");
755 static void init_colors_or_die(void)
760 die(EXIT_FAILURE, "fatal: No color term\n");
761 if (start_color() == ERR)
762 die(EXIT_FAILURE, "fatal: failed to start colors\n");
763 FOR_EACH_STATUS_ITEM(i)
764 if (theme.data[i].len)
765 init_pair_or_die(i + 1, theme.data[i].fg,
767 init_pair_or_die(COLOR_STATUSBAR, theme.sb_fg, theme.sb_bg);
768 init_pair_or_die(COLOR_COMMAND, theme.cmd_fg, theme.cmd_bg);
769 init_pair_or_die(COLOR_OUTPUT, theme.output_fg, theme.output_bg);
770 init_pair_or_die(COLOR_MSG, theme.msg_fg, theme.msg_bg);
771 init_pair_or_die(COLOR_ERRMSG, theme.err_msg_fg, theme.err_msg_bg);
772 init_pair_or_die(COLOR_SEPARATOR, theme.sep_fg, theme.sep_bg);
773 init_pair_or_die(COLOR_TOP, theme.default_fg, theme.default_bg);
774 init_pair_or_die(COLOR_BOT, theme.default_fg, theme.default_bg);
777 /* (Re-)initialize the curses library. */
778 static void init_curses(void)
782 if (top.win && refresh() == ERR) /* refresh is really needed */
783 die(EXIT_FAILURE, "refresh() failed\n");
784 if (LINES < theme.lines_min || COLS < theme.cols_min)
785 die(EXIT_FAILURE, "Terminal (%dx%d) too small"
786 " (need at least %dx%d)\n", COLS, LINES,
787 theme.cols_min, theme.lines_min);
788 curs_set(0); /* make cursor invisible, ignore errors */
789 nonl(); /* do not NL->CR/NL on output, always returns OK */
790 /* don't echo input */
792 die(EXIT_FAILURE, "fatal: noecho() failed\n");
793 /* take input chars one at a time, no wait for \n */
795 die(EXIT_FAILURE, "fatal: cbreak() failed\n");
796 init_colors_or_die();
797 clear(); /* ignore non-fatal errors */
798 init_wins(theme.top_lines_default);
799 // noecho(); /* don't echo input */
802 static void check_sigchld(void)
808 ret = para_reap_child(&pid);
811 if (pid == cmd_pid) {
814 print_in_bar(COLOR_MSG, " ");
816 goto reap_next_child;
820 * This sucker modifies its first argument. *handler and *arg are
821 * pointers to 0-terminated strings (inside line). Crap.
823 static int split_key_map(char *line, char **handler, char **arg)
825 if (!(*handler = strchr(line + 1, ':')))
829 if (!(*arg = strchr(*handler, ':')))
838 static void check_key_map_args_or_die(void)
843 for (i = 0; i < conf.key_map_given; ++i) {
847 tmp = para_strdup(conf.key_map_arg[i]);
848 if (!split_key_map(tmp, &handler, &arg))
850 if (strlen(handler) != 1)
852 if (*handler != 'x' && *handler != 'd' && *handler != 'i'
857 if (find_cmd_byname(arg) < 0)
860 if (i != conf.key_map_given)
861 die(EXIT_FAILURE, "invalid key map: %s\n", conf.key_map_arg[i]);
865 static void parse_config_file_or_die(bool override)
869 struct gui_cmdline_parser_params params = {
870 .override = override,
872 .check_required = !override,
873 .check_ambiguity = 0,
877 if (conf.config_file_given)
878 config_file = para_strdup(conf.config_file_arg);
880 char *home = para_homedir();
881 config_file = make_message("%s/.paraslash/gui.conf", home);
884 if (!file_exists(config_file)) {
885 if (!conf.config_file_given)
888 PARA_EMERG_LOG("config file %s does not exist\n",
894 gui_cmdline_parser_config_file(config_file, &conf, ¶ms);
895 loglevel = get_loglevel_by_name(conf.loglevel_arg);
896 check_key_map_args_or_die();
897 theme_init(conf.theme_arg, &theme);
905 /* reread configuration, terminate on errors */
906 static void reread_conf(void)
909 * gengetopt might print to stderr and exit on errors. So we have to
910 * shutdown curses first.
913 parse_config_file_or_die(true /* override */);
915 print_in_bar(COLOR_MSG, "config file reloaded\n");
919 * React to various signal-related events
921 static void signal_post_select(fd_set *rfds)
923 int ret = para_next_signal(rfds);
928 die(EXIT_FAILURE, "only the good die young (caught SIGTERM)\n");
931 if (curses_active()) {
938 PARA_WARNING_LOG("caught SIGINT, reset\n");
939 /* Nothing to do. SIGINT killed our child which gets noticed
940 * by do_select and resets everything.
944 PARA_NOTICE_LOG("got SIGUSR1, rereading configuration\n");
953 static void status_pre_select(fd_set *rfds, int *max_fileno, struct timeval *tv)
955 static struct timeval next_exec, atm, diff;
956 int ret, fds[3] = {0, 1, 0};
961 /* Avoid busy loop */
962 gettimeofday(&atm, NULL);
963 if (tv_diff(&next_exec, &atm, &diff) > 0) {
964 if (tv_diff(&diff, tv, NULL) < 0)
968 next_exec.tv_sec = atm.tv_sec + 2;
969 ret = para_exec_cmdline_pid(&pid, conf.stat_cmd_arg, fds);
972 ret = mark_fd_nonblocking(fds[1]);
979 para_fd_set(stat_pipe, rfds, max_fileno);
982 #define COMMAND_BUF_SIZE 32768
984 static enum gui_select_mode get_select_mode(void)
986 if (command_fds[0] >= 0 || command_fds[1] >= 0)
989 return EXTERNAL_MODE;
993 static void command_pre_select(fd_set *rfds, int *max_fileno)
995 enum gui_select_mode mode = get_select_mode();
997 if (mode != COMMAND_MODE)
999 if (command_fds[0] >= 0)
1000 para_fd_set(command_fds[0], rfds, max_fileno);
1001 if (command_fds[1] >= 0)
1002 para_fd_set(command_fds[1], rfds, max_fileno);
1005 static void command_post_select(fd_set *rfds)
1008 static char command_buf[2][COMMAND_BUF_SIZE];
1009 static int cbo[2]; /* command buf offsets */
1010 static unsigned flags[2]; /* for for_each_line() */
1011 enum gui_select_mode mode = get_select_mode();
1013 if (mode != COMMAND_MODE)
1015 for (i = 0; i < 2; i++) {
1017 if (command_fds[i] < 0)
1019 ret = read_nonblock(command_fds[i],
1020 command_buf[i] + cbo[i],
1021 COMMAND_BUF_SIZE - 1 - cbo[i], rfds, &sz);
1024 cbo[i] = for_each_line(flags[i], command_buf[i], cbo[i],
1025 add_output_line, &i);
1026 if (sz != cbo[i]) { /* at least one line found */
1030 if (ret < 0 || cmd_pid == 0) {
1032 PARA_NOTICE_LOG("closing command fd %d: %s",
1033 i, para_strerror(-ret));
1034 close(command_fds[i]);
1035 command_fds[i] = -1;
1038 if (command_fds[!i] < 0) /* both fds closed */
1041 if (cbo[i] == COMMAND_BUF_SIZE - 1) {
1042 PARA_NOTICE_LOG("discarding overlong line");
1044 flags[i] = FELF_DISCARD_FIRST;
1049 static void input_pre_select(fd_set *rfds, int *max_fileno)
1051 enum gui_select_mode mode = get_select_mode();
1053 if (mode == GETCH_MODE || mode == COMMAND_MODE)
1054 para_fd_set(STDIN_FILENO, rfds, max_fileno);
1057 /* read from command pipe and print data to bot window */
1058 static void exec_and_display_cmd(const char *cmd)
1060 int ret, fds[3] = {0, 1, 1};
1062 outputf(COLOR_COMMAND, "%s", cmd);
1063 ret = para_exec_cmdline_pid(&cmd_pid, cmd, fds);
1066 ret = mark_fd_nonblocking(fds[1]);
1069 ret = mark_fd_nonblocking(fds[2]);
1072 command_fds[0] = fds[1];
1073 command_fds[1] = fds[2];
1076 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
1077 close(command_fds[0]);
1078 close(command_fds[1]);
1081 static void para_cmd(char *cmd)
1085 print_in_bar(COLOR_MSG, "executing client command, hit any key to abort\n");
1086 c = make_message(BINDIR "/para_client -- %s", cmd);
1087 exec_and_display_cmd(c);
1091 static void display_cmd(char *cmd)
1093 print_in_bar(COLOR_MSG, "executing display command, hit any key to abort");
1094 exec_and_display_cmd(cmd);
1098 * shutdown curses and stat pipe before executing external commands
1100 static void external_cmd(char *cmd)
1102 int fds[3] = {-1, -1, -1};
1107 para_exec_cmdline_pid(&cmd_pid, cmd, fds);
1110 static void handle_command(int c)
1114 /* first check user-defined key bindings */
1115 for (i = 0; i < conf.key_map_given; ++i) {
1116 char *tmp, *handler, *arg;
1118 tmp = para_strdup(conf.key_map_arg[i]);
1119 if (!split_key_map(tmp, &handler, &arg)) {
1123 if (strcmp(tmp, km_keyname(c))) {
1127 if (*handler == 'd')
1129 else if (*handler == 'x')
1131 else if (*handler == 'p')
1133 else if (*handler == 'i') {
1134 int num = find_cmd_byname(arg);
1136 command_list[num].handler();
1141 /* not found, check internal key bindings */
1142 for (i = 0; command_list[i].handler; i++) {
1143 if (!strcmp(km_keyname(c), command_list[i].key)) {
1144 command_list[i].handler();
1148 print_in_bar(COLOR_ERRMSG, "key '%s' is not bound, press ? for help",
1152 static void input_post_select(void)
1155 enum gui_select_mode mode = get_select_mode();
1157 if (mode == EXTERNAL_MODE)
1159 ret = wgetch(top.win);
1160 if (ret == ERR || ret == KEY_RESIZE)
1162 if (mode == GETCH_MODE)
1163 return handle_command(ret);
1165 kill(cmd_pid, SIGTERM);
1168 static void signal_pre_select(fd_set *rfds, int *max_fileno)
1170 para_fd_set(signal_pipe, rfds, max_fileno);
1174 * This is the core select loop. It checks the following fds:
1178 * - stdout/stderr of display or internal commands
1180 __noreturn static void do_select(void)
1183 int ret, max_fileno;
1187 tv.tv_sec = conf.timeout_arg / 1000;
1188 tv.tv_usec = (conf.timeout_arg % 1000) * 1000;
1191 status_pre_select(&rfds, &max_fileno, &tv);
1192 signal_pre_select(&rfds, &max_fileno);
1193 command_pre_select(&rfds, &max_fileno);
1194 input_pre_select(&rfds, &max_fileno);
1195 ret = para_select(max_fileno + 1, &rfds, NULL, &tv);
1197 goto repeat; /* skip fd checks */
1198 signal_post_select(&rfds);
1199 command_post_select(&rfds);
1200 status_post_select(&rfds);
1201 input_post_select();
1205 static void print_scroll_msg(void)
1207 unsigned lines_total, filled = ringbuffer_filled(bot_win_rb);
1208 int first_rbe = first_visible_rbe(&lines_total);
1210 print_in_bar(COLOR_MSG, "scrolled view: %d-%d/%d\n", filled - first_rbe,
1211 filled - scroll_position, ringbuffer_filled(bot_win_rb));
1214 static void com_scroll_top(void)
1216 int i = RINGBUFFER_SIZE - 1, bot_lines = get_num_lines(&bot);
1219 while (i > 0 && !ringbuffer_get(bot_win_rb, i))
1221 /* i is oldest entry */
1222 for (; lines < bot_lines && i >= 0; i--) {
1223 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
1226 lines += NUM_LINES(rbe->len);
1229 if (lines > 0 && scroll_position != i) {
1230 scroll_position = i;
1235 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1238 static void com_cancel_scrolling(void)
1241 if (scroll_position == 0) {
1242 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1245 scroll_position = 0;
1249 static void com_page_down(void)
1252 int i = scroll_position, bot_lines = get_num_lines(&bot);
1254 while (lines < bot_lines && --i > 0) {
1255 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
1258 lines += NUM_LINES(rbe->len);
1261 scroll_position = i;
1266 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1269 static void com_page_up(void)
1272 int fvr = first_visible_rbe(&lines), bot_lines = get_num_lines(&bot);
1274 if (fvr < 0 || fvr + 1 >= ringbuffer_filled(bot_win_rb)) {
1275 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1278 scroll_position = fvr + 1;
1279 for (; scroll_position > 0; scroll_position--) {
1280 first_visible_rbe(&lines);
1281 if (lines == bot_lines)
1288 static void com_scroll_down(void)
1290 struct rb_entry *rbe;
1291 int rbe_lines, bot_lines = get_num_lines(&bot);
1293 if (!scroll_position) {
1294 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1298 rbe = ringbuffer_get(bot_win_rb, scroll_position);
1299 rbe_lines = NUM_LINES(rbe->len);
1300 wscrl(bot.win, rbe_lines);
1301 wmove(bot.win, bot_lines - rbe_lines, 0);
1302 wattron(bot.win, COLOR_PAIR(rbe->color));
1303 waddstr(bot.win, rbe->msg);
1308 static void com_scroll_up(void)
1310 struct rb_entry *rbe = NULL;
1312 int i, first_rbe, num_scroll;
1314 /* the entry that is going to vanish */
1315 rbe = ringbuffer_get(bot_win_rb, scroll_position);
1318 num_scroll = NUM_LINES(rbe->len);
1319 first_rbe = first_visible_rbe(&lines);
1320 if (first_rbe < 0 || (first_rbe == ringbuffer_filled(bot_win_rb) - 1))
1323 wscrl(bot.win, -num_scroll);
1324 i = draw_top_rbe(&lines);
1327 while (i > 0 && lines < num_scroll) {
1329 rbe = ringbuffer_get(bot_win_rb, --i);
1332 rbe_lines = NUM_LINES(rbe->len);
1334 wattron(bot.win, COLOR_PAIR(rbe->color));
1335 waddstr(bot.win, "\n");
1336 waddstr(bot.win, rbe->msg);
1345 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1348 static void com_ll_decr(void)
1350 if (loglevel <= LL_DEBUG) {
1351 print_in_bar(COLOR_ERRMSG,
1352 "loglevel already at maximal verbosity\n");
1356 print_in_bar(COLOR_MSG, "loglevel set to %d\n", loglevel);
1359 static void com_ll_incr(void)
1361 if (loglevel >= LL_EMERG) {
1362 print_in_bar(COLOR_ERRMSG,
1363 "loglevel already at minimal verbosity\n");
1367 print_in_bar(COLOR_MSG, "loglevel set to %d\n", loglevel);
1370 static void com_reread_conf(void)
1375 static void com_help(void)
1379 for (i = 0; i < conf.key_map_given; ++i) {
1380 char *handler, *arg, *tmp = para_strdup(conf.key_map_arg[i]);
1381 const char *handler_text = "???", *desc = NULL;
1383 if (!split_key_map(tmp, &handler, &arg)) {
1389 handler_text = "internal";
1390 desc = command_list[find_cmd_byname(arg)].description;
1392 case 'x': handler_text = "external"; break;
1393 case 'd': handler_text = "display "; break;
1394 case 'p': handler_text = "para "; break;
1396 outputf(COLOR_MSG, "%s\t%s\t%s%s\t%s", tmp, handler_text, arg,
1397 strlen(arg) < 8? "\t" : "",
1401 for (i = 0; command_list[i].handler; i++) {
1402 struct gui_command gc = command_list[i];
1404 outputf(COLOR_MSG, "%s\tinternal\t%s\t%s%s", gc.key, gc.name,
1405 strlen(gc.name) < 8? "\t" : "",
1408 print_in_bar(COLOR_MSG, "try \"para_gui -h\" or \"para_client help\" "
1412 static void com_shrink_top_win(void)
1414 int top_lines = get_num_lines(&top);
1416 if (top_lines <= theme.top_lines_min) {
1417 PARA_WARNING_LOG("can not decrease top window\n");
1420 init_wins(top_lines - 1);
1421 print_in_bar(COLOR_MSG, "%s", "decreased top window");
1424 static void com_enlarge_top_win(void)
1426 int top_lines = get_num_lines(&top), bot_lines = get_num_lines(&bot);
1428 if (bot_lines < 3) {
1429 PARA_WARNING_LOG("can not increase top window\n");
1432 init_wins(top_lines + 1);
1433 print_in_bar(COLOR_MSG, "increased top window");
1436 static void com_version(void)
1438 print_in_bar(COLOR_MSG, "%s", version_single_line("gui"));
1441 __noreturn static void com_quit(void)
1443 die(EXIT_SUCCESS, "%s", "");
1446 static void com_refresh(void)
1452 static void com_next_theme(void)
1458 static void com_prev_theme(void)
1464 __noreturn static void print_help_and_die(void)
1466 struct ggo_help h = DEFINE_GGO_HELP(gui);
1467 bool d = conf.detailed_help_given;
1469 ggo_print_help(&h, d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS);
1473 int main(int argc, char *argv[])
1475 gui_cmdline_parser(argc, argv, &conf); /* exits on errors */
1476 loglevel = get_loglevel_by_name(conf.loglevel_arg);
1477 version_handle_flag("gui", conf.version_given);
1478 if (conf.help_given || conf.detailed_help_given)
1479 print_help_and_die();
1480 parse_config_file_or_die(false /* override */);
1481 setup_signal_handling();
1482 bot_win_rb = ringbuffer_new(RINGBUFFER_SIZE);
1483 setlocale(LC_CTYPE, "");
1484 initscr(); /* needed only once, always successful */