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;
57 CMDS_IDLE, /* no command running */
58 CMDS_DCMD, /* para or display command running */
59 CMDS_XCMD, /* external command running */
63 * Codes for various colors.
65 * Each status item has its own color pair. The ones defined here start at a
66 * higher number so that they do not overlap with these.
69 COLOR_STATUSBAR = NUM_STAT_ITEMS + 1,
82 const char *description;
83 void (*handler)(void);
86 static struct gui_theme theme;
88 static void com_help(void);
89 static void com_reread_conf(void);
90 static void com_enlarge_top_win(void);
91 static void com_shrink_top_win(void);
92 static void com_version(void);
93 __noreturn static void com_quit(void);
94 static void com_refresh(void);
95 static void com_ll_incr(void);
96 static void com_ll_decr(void);
97 static void com_prev_theme(void);
98 static void com_next_theme(void);
99 static void com_scroll_up(void);
100 static void com_scroll_down(void);
101 static void com_page_up(void);
102 static void com_page_down(void);
103 static void com_cancel_scrolling(void);
104 static void com_scroll_top(void);
106 static struct gui_command command_list[] = {
110 .description = "print help",
114 .name = "enlarge_win",
115 .description = "enlarge the top window",
116 .handler = com_enlarge_top_win
119 .name = "shrink_win",
120 .description = "shrink the top window",
121 .handler = com_shrink_top_win
124 .name = "reread_conf",
125 .description = "reread configuration file",
126 .handler = com_reread_conf
130 .description = "exit para_gui",
135 .description = "redraw the screen",
136 .handler = com_refresh
139 .name = "next_theme",
140 .description = "switch to next theme",
141 .handler = com_next_theme
144 .name = "prev_theme",
145 .description = "switch to previous stream",
146 .handler = com_prev_theme
150 .description = "increase loglevel (decreases verbosity)",
151 .handler = com_ll_incr
155 .description = "decrease loglevel (increases verbosity)",
156 .handler = com_ll_decr
160 .description = "show the para_gui version",
161 .handler = com_version
165 .description = "scroll up one line",
166 .handler = com_scroll_up
169 .name = "scroll_down",
170 .description = "scroll down one line",
171 .handler = com_scroll_down
175 .description = "scroll up one page",
176 .handler = com_page_up
180 .description = "scroll down one page",
181 .handler = com_page_down
184 .name = "scroll_top",
185 .description = "scroll to top of buffer",
186 .handler = com_scroll_top
189 .name = "cancel_scroll",
190 .description = "deactivate scroll mode",
191 .handler = com_cancel_scrolling
197 static int find_cmd_byname(char *name)
201 for (i = 0; command_list[i].handler; i++)
202 if (!strcmp(command_list[i].name, name))
208 * Even though ncurses provides getmaxx and getmaxy, these functions/macros are
209 * not described in the XSI Curses standard.
211 static int get_num_lines(struct gui_window *w)
214 __a_unused int cols; /* avoid "set but not used" warnings */
216 getmaxyx(w->win, lines, cols);
220 static int get_num_cols(struct gui_window *w)
222 __a_unused int lines; /* avoid "set but not used" warnings */
225 getmaxyx(w->win, lines, cols);
229 /** Number of lines of the window are occupied by an output line. */
230 #define NUM_LINES(len) (1 + (len) / get_num_cols(&bot))
232 /* isendwin() returns false before initscr() was called */
233 static bool curses_active(void)
235 return top.win && !isendwin();
238 /* taken from mutt */
239 static char *km_keyname(int c)
244 sprintf(buf, "<up>");
248 sprintf(buf, "<down>");
252 sprintf(buf, "<left>");
255 if (c == KEY_RIGHT) {
256 sprintf(buf, "<right>");
259 if (c == KEY_NPAGE) {
260 sprintf(buf, "<npage>");
263 if (c == KEY_PPAGE) {
264 sprintf(buf, "<ppage>");
268 sprintf(buf, "<home>");
272 sprintf(buf, "<end>");
275 if (c < 256 && c > -128 && iscntrl((unsigned char) c)) {
280 buf[1] = (c + '@') & 0x7f;
283 snprintf(buf, sizeof(buf), "\\%d%d%d", c >> 6,
284 (c >> 3) & 7, c & 7);
285 } else if (c >= KEY_F0 && c < KEY_F(256))
286 sprintf(buf, "<F%d>", c - KEY_F0);
288 snprintf(buf, sizeof(buf), "%c", (unsigned char) c);
290 snprintf(buf, sizeof(buf), "\\x%hx", (unsigned short) c);
294 /* Print given number of spaces to curses window. */
295 static void add_spaces(WINDOW* win, unsigned int num)
298 unsigned sz = sizeof(space) - 1; /* number of spaces */
312 * print aligned string to curses window. This function always prints
315 static int align_str(WINDOW* win, char *str, unsigned int len,
318 int ret, i, num; /* of spaces */
323 ret = strwidth(str, &width);
325 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
334 /* replace control characters by spaces */
335 for (i = 0; i < len && str[i]; i++) {
336 if (str[i] == '\n' || str[i] == '\r' || str[i] == '\f')
341 add_spaces(win, num);
342 } else if (align == RIGHT) {
343 add_spaces(win, num);
346 add_spaces(win, num / 2);
347 waddstr(win, str[0]? str: "");
348 add_spaces(win, num - num / 2);
353 __printf_2_3 static void print_in_bar(int color, const char *fmt,...)
358 if (!curses_active())
360 wattron(in.win, COLOR_PAIR(color));
362 xvasprintf(&msg, fmt, ap);
365 align_str(in.win, msg, get_num_cols(&in), LEFT);
370 static void print_status_bar(void)
374 tmp = para_strdup("para_gui " PACKAGE_VERSION " (hit ? for help)");
376 align_str(sb.win, tmp, get_num_cols(&sb), CENTER);
381 * get the number of the oldest rbe that is (partially) visible. On return,
382 * lines contains the sum of the number of lines of all visible entries. If the
383 * first one is only partially visible, lines is greater than bot.lines.
385 static int first_visible_rbe(unsigned *lines)
387 int i, bot_lines = get_num_lines(&bot);
390 for (i = scroll_position; i < RINGBUFFER_SIZE; i++) {
391 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
395 rbe_lines = NUM_LINES(rbe->len);
396 if (rbe_lines > bot_lines)
399 if (*lines >= bot_lines)
402 return RINGBUFFER_SIZE - 1;
406 returns number of first visible rbe, *lines is the number of lines drawn.
408 static int draw_top_rbe(unsigned *lines)
410 int bot_cols, bot_lines, ret, fvr = first_visible_rbe(lines);
411 struct rb_entry *rbe;
412 size_t bytes_to_skip, cells_to_skip, width;
416 wmove(bot.win, 0, 0);
417 rbe = ringbuffer_get(bot_win_rb, fvr);
420 getmaxyx(bot.win, bot_lines, bot_cols);
421 if (*lines > bot_lines) {
422 /* rbe is partially visible multi-line */
423 cells_to_skip = (*lines - bot_lines) * bot_cols;
424 ret = skip_cells(rbe->msg, cells_to_skip, &bytes_to_skip);
427 ret = strwidth(rbe->msg + bytes_to_skip, &width);
434 wattron(bot.win, COLOR_PAIR(rbe->color));
435 waddstr(bot.win, rbe->msg + bytes_to_skip);
436 *lines = NUM_LINES(width);
440 static void redraw_bot_win(void)
443 int i, bot_lines = get_num_lines(&bot);
445 wmove(bot.win, 0, 0);
447 i = draw_top_rbe(&lines);
450 while (i > 0 && lines < bot_lines) {
451 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, --i);
454 waddstr(bot.win, "\n");
457 lines += NUM_LINES(rbe->len);
458 wattron(bot.win, COLOR_PAIR(rbe->color));
459 waddstr(bot.win, "\n");
460 waddstr(bot.win, rbe->msg);
466 static void rb_add_entry(int color, char *msg)
468 struct rb_entry *old, *new;
472 if (strwidth(msg, &len) < 0)
474 new = para_malloc(sizeof(struct rb_entry));
478 old = ringbuffer_add(bot_win_rb, new);
483 if (scroll_position) {
484 /* discard current scrolling, like xterm does */
489 wattron(bot.win, COLOR_PAIR(color));
490 getyx(bot.win, y, x);
492 waddstr(bot.win, "\n");
493 waddstr(bot.win, msg);
497 * print formated output to bot win and refresh
499 __printf_2_3 static void outputf(int color, const char* fmt,...)
504 if (!curses_active())
507 xvasprintf(&msg, fmt, ap);
509 rb_add_entry(color, msg);
513 static int add_output_line(char *line, void *data)
515 int color = *(int *)data? COLOR_ERRMSG : COLOR_OUTPUT;
517 if (!curses_active())
519 rb_add_entry(color, para_strdup(line));
523 static __printf_2_3 void curses_log(int ll, const char *fmt,...)
530 if (curses_active()) {
531 int color = ll <= LL_NOTICE? COLOR_MSG : COLOR_ERRMSG;
533 unsigned bytes = xvasprintf(&msg, fmt, ap);
534 if (bytes > 0 && msg[bytes - 1] == '\n')
535 msg[bytes - 1] = '\0'; /* cut trailing newline */
536 rb_add_entry(color, msg);
538 } else if (cmd_pid <= 0) /* no external command running */
539 vfprintf(stderr, fmt, ap);
542 __printf_2_3 void (*para_log)(int, const char*, ...) = curses_log;
544 static void setup_signal_handling(void)
546 signal_pipe = para_signal_init();
547 para_install_sighandler(SIGINT);
548 para_install_sighandler(SIGTERM);
549 para_install_sighandler(SIGCHLD);
550 para_install_sighandler(SIGWINCH);
551 para_install_sighandler(SIGUSR1);
554 static void shutdown_curses(void)
560 /* disable curses, print a message, kill running processes and exit */
561 __noreturn __printf_2_3 static void die(int exit_code, const char* fmt, ...)
567 vfprintf(stderr, fmt, argp);
569 /* kill every process in the process group and exit */
570 para_sigaction(SIGTERM, SIG_IGN);
576 * Print stat item #i to curses window
578 static void print_stat_item(int i)
581 struct stat_item_data d = theme.data[i];
582 char *c = stat_content[i];
583 int top_lines = get_num_lines(&top);
585 if (!curses_active() || !d.len || !c)
587 tmp = make_message("%s%s%s", d.prefix, c, d.postfix);
588 wmove(top.win, d.y * top_lines / 100, d.x * COLS / 100);
590 wattron(top.win, COLOR_PAIR(i + 1));
591 align_str(top.win, tmp, d.len * COLS / 100, d.align);
596 static int update_item(int item_num, char *buf)
598 char **c = stat_content + item_num;
605 *c = para_strdup("(artist tag not set)");
608 *c = para_strdup("(title tag not set)");
611 *c = para_strdup("????");
614 *c = para_strdup("(album tag not set)");
617 *c = para_strdup("(comment tag not set)");
621 *c = para_strdup(buf);
623 print_stat_item(item_num);
627 static void print_all_items(void)
631 if (!curses_active())
633 FOR_EACH_STATUS_ITEM(i)
637 static void clear_all_items(void)
641 FOR_EACH_STATUS_ITEM(i) {
642 free(stat_content[i]);
643 stat_content[i] = para_strdup("");
647 static struct timeval next_exec;
649 static void status_pre_select(fd_set *rfds, int *max_fileno, struct timeval *tv)
651 struct timeval atm, diff;
654 return para_fd_set(stat_pipe, rfds, max_fileno);
655 gettimeofday(&atm, NULL);
656 if (tv_diff(&next_exec, &atm, &diff) > 0) {
660 tv->tv_sec = tv->tv_usec = 0; /* min delay */
663 static void status_post_select(fd_set *rfds)
666 static int bufsize, loaded;
673 int fds[3] = {0, 1, 0};
674 /* Avoid busy loop */
675 gettimeofday(&atm, NULL);
676 if (tv_diff(&next_exec, &atm, NULL) > 0)
678 next_exec.tv_sec = atm.tv_sec + 2;
679 ret = para_exec_cmdline_pid(&pid, conf.stat_cmd_arg, fds);
682 ret = mark_fd_nonblocking(fds[1]);
691 if (loaded >= bufsize) {
692 if (bufsize > 1000 * 1000) {
696 bufsize += bufsize + 1000;
697 buf = para_realloc(buf, bufsize);
699 assert(loaded < bufsize);
700 ret = read_nonblock(stat_pipe, buf + loaded, bufsize - loaded,
703 ret2 = for_each_stat_item(buf, loaded, update_item);
704 if (ret < 0 || ret2 < 0) {
706 PARA_NOTICE_LOG("closing stat pipe: %s\n", para_strerror(-ret));
710 free(stat_content[SI_BASENAME]);
711 stat_content[SI_BASENAME] =
712 para_strdup("stat command terminated!?");
716 sz = ret2; /* what is left */
717 if (sz > 0 && sz < loaded)
718 memmove(buf, buf + loaded - sz, sz);
725 static void init_wins(int top_lines)
727 int top_y = 0, bot_y = top_lines + 1, sb_y = LINES - 2,
728 in_y = LINES - 1, sep_y = top_lines;
729 int bot_lines = LINES - top_lines - 3, sb_lines = 1, in_lines = 1,
732 assume_default_colors(theme.default_fg, theme.default_bg);
734 wresize(top.win, top_lines, COLS);
735 mvwin(top.win, top_y, 0);
737 wresize(sb.win, sb_lines, COLS);
738 mvwin(sb.win, sb_y, 0);
740 wresize(sep.win, sep_lines, COLS);
741 mvwin(sep.win, sep_y, 0);
743 wresize(bot.win, bot_lines, COLS);
744 mvwin(bot.win, bot_y, 0);
746 wresize(in.win, in_lines, COLS);
747 mvwin(in.win, in_y, 0);
749 sep.win = newwin(sep_lines, COLS, sep_y, 0);
750 top.win = newwin(top_lines, COLS, top_y, 0);
751 bot.win = newwin(bot_lines, COLS, bot_y, 0);
752 sb.win = newwin(sb_lines, COLS, sb_y, 0);
753 in.win = newwin(in_lines, COLS, in_y, 0);
754 if (!top.win || !bot.win || !sb.win || !in.win || !sep.win)
755 die(EXIT_FAILURE, "Error: Cannot create curses windows\n");
759 scrollok(bot.win, 1);
760 wattron(sb.win, COLOR_PAIR(COLOR_STATUSBAR));
761 wattron(sep.win, COLOR_PAIR(COLOR_SEPARATOR));
762 wattron(bot.win, COLOR_PAIR(COLOR_BOT));
763 wattron(top.win, COLOR_PAIR(COLOR_TOP));
774 wmove(sep.win, 0, 0);
775 whline(sep.win, theme.sep_char, COLS);
779 wnoutrefresh(top.win);
780 wnoutrefresh(bot.win);
782 wnoutrefresh(sb.win);
783 wnoutrefresh(in.win);
784 wnoutrefresh(sep.win);
788 static void init_pair_or_die(short pair, short f, short b)
790 if (init_pair(pair, f, b) == ERR)
791 die(EXIT_FAILURE, "fatal: init_pair() failed\n");
794 static void init_colors_or_die(void)
799 die(EXIT_FAILURE, "fatal: No color term\n");
800 if (start_color() == ERR)
801 die(EXIT_FAILURE, "fatal: failed to start colors\n");
802 FOR_EACH_STATUS_ITEM(i)
803 if (theme.data[i].len)
804 init_pair_or_die(i + 1, theme.data[i].fg,
806 init_pair_or_die(COLOR_STATUSBAR, theme.sb_fg, theme.sb_bg);
807 init_pair_or_die(COLOR_COMMAND, theme.cmd_fg, theme.cmd_bg);
808 init_pair_or_die(COLOR_OUTPUT, theme.output_fg, theme.output_bg);
809 init_pair_or_die(COLOR_MSG, theme.msg_fg, theme.msg_bg);
810 init_pair_or_die(COLOR_ERRMSG, theme.err_msg_fg, theme.err_msg_bg);
811 init_pair_or_die(COLOR_SEPARATOR, theme.sep_fg, theme.sep_bg);
812 init_pair_or_die(COLOR_TOP, theme.default_fg, theme.default_bg);
813 init_pair_or_die(COLOR_BOT, theme.default_fg, theme.default_bg);
816 /* (Re-)initialize the curses library. */
817 static void init_curses(void)
821 if (top.win && refresh() == ERR) /* refresh is really needed */
822 die(EXIT_FAILURE, "refresh() failed\n");
823 if (LINES < theme.lines_min || COLS < theme.cols_min)
824 die(EXIT_FAILURE, "Terminal (%dx%d) too small"
825 " (need at least %dx%d)\n", COLS, LINES,
826 theme.cols_min, theme.lines_min);
827 curs_set(0); /* make cursor invisible, ignore errors */
828 nonl(); /* do not NL->CR/NL on output, always returns OK */
829 /* don't echo input */
831 die(EXIT_FAILURE, "fatal: noecho() failed\n");
832 /* take input chars one at a time, no wait for \n */
834 die(EXIT_FAILURE, "fatal: cbreak() failed\n");
835 init_colors_or_die();
836 clear(); /* ignore non-fatal errors */
837 init_wins(theme.top_lines_default);
838 // noecho(); /* don't echo input */
841 static void check_sigchld(void)
847 ret = para_reap_child(&pid);
850 if (pid == cmd_pid) {
853 print_in_bar(COLOR_MSG, " ");
855 goto reap_next_child;
859 * This sucker modifies its first argument. *handler and *arg are
860 * pointers to 0-terminated strings (inside line). Crap.
862 static int split_key_map(char *line, char **handler, char **arg)
864 if (!(*handler = strchr(line + 1, ':')))
868 if (!(*arg = strchr(*handler, ':')))
877 static void check_key_map_args_or_die(void)
882 for (i = 0; i < conf.key_map_given; ++i) {
886 tmp = para_strdup(conf.key_map_arg[i]);
887 if (!split_key_map(tmp, &handler, &arg))
889 if (strlen(handler) != 1)
891 if (*handler != 'x' && *handler != 'd' && *handler != 'i'
896 if (find_cmd_byname(arg) < 0)
899 if (i != conf.key_map_given)
900 die(EXIT_FAILURE, "invalid key map: %s\n", conf.key_map_arg[i]);
904 static void parse_config_file_or_die(bool override)
908 struct gui_cmdline_parser_params params = {
909 .override = override,
911 .check_required = !override,
912 .check_ambiguity = 0,
916 if (conf.config_file_given)
917 config_file = para_strdup(conf.config_file_arg);
919 char *home = para_homedir();
920 config_file = make_message("%s/.paraslash/gui.conf", home);
923 if (!file_exists(config_file)) {
924 if (!conf.config_file_given)
927 PARA_EMERG_LOG("config file %s does not exist\n",
933 gui_cmdline_parser_config_file(config_file, &conf, ¶ms);
934 loglevel = get_loglevel_by_name(conf.loglevel_arg);
935 check_key_map_args_or_die();
936 theme_init(conf.theme_arg, &theme);
944 /* reread configuration, terminate on errors */
945 static void reread_conf(void)
948 * gengetopt might print to stderr and exit on errors. So we have to
949 * shutdown curses first.
952 parse_config_file_or_die(true /* override */);
954 print_in_bar(COLOR_MSG, "config file reloaded\n");
958 * React to various signal-related events
960 static void signal_post_select(fd_set *rfds)
962 int ret = para_next_signal(rfds);
967 die(EXIT_FAILURE, "only the good die young (caught SIGTERM)\n");
970 if (curses_active()) {
977 PARA_WARNING_LOG("caught SIGINT, reset\n");
978 /* Nothing to do. SIGINT killed our child which gets noticed
979 * by do_select and resets everything.
983 PARA_NOTICE_LOG("got SIGUSR1, rereading configuration\n");
992 #define COMMAND_BUF_SIZE 32768
994 static enum cmd_status cmd_status(void)
996 if (command_fds[0] >= 0 || command_fds[1] >= 0)
1003 static void command_pre_select(fd_set *rfds, int *max_fileno)
1005 enum cmd_status cmds = cmd_status();
1007 if (cmds != CMDS_DCMD)
1009 if (command_fds[0] >= 0)
1010 para_fd_set(command_fds[0], rfds, max_fileno);
1011 if (command_fds[1] >= 0)
1012 para_fd_set(command_fds[1], rfds, max_fileno);
1015 static void command_post_select(fd_set *rfds)
1018 static char command_buf[2][COMMAND_BUF_SIZE];
1019 static int cbo[2]; /* command buf offsets */
1020 static unsigned flags[2]; /* for for_each_line() */
1021 enum cmd_status cmds = cmd_status();
1023 if (cmds != CMDS_DCMD)
1025 for (i = 0; i < 2; i++) {
1027 if (command_fds[i] < 0)
1029 ret = read_nonblock(command_fds[i],
1030 command_buf[i] + cbo[i],
1031 COMMAND_BUF_SIZE - 1 - cbo[i], rfds, &sz);
1034 cbo[i] = for_each_line(flags[i], command_buf[i], cbo[i],
1035 add_output_line, &i);
1036 if (sz != cbo[i]) { /* at least one line found */
1040 if (ret < 0 || cmd_pid == 0) {
1042 PARA_NOTICE_LOG("closing command fd %d: %s",
1043 i, para_strerror(-ret));
1044 close(command_fds[i]);
1045 command_fds[i] = -1;
1048 if (command_fds[!i] < 0) /* both fds closed */
1051 if (cbo[i] == COMMAND_BUF_SIZE - 1) {
1052 PARA_NOTICE_LOG("discarding overlong line");
1054 flags[i] = FELF_DISCARD_FIRST;
1059 static void input_pre_select(fd_set *rfds, int *max_fileno)
1061 enum cmd_status cmds = cmd_status();
1063 if (cmds != CMDS_XCMD)
1064 para_fd_set(STDIN_FILENO, rfds, max_fileno);
1067 /* read from command pipe and print data to bot window */
1068 static void exec_and_display_cmd(const char *cmd)
1070 int ret, fds[3] = {0, 1, 1};
1072 outputf(COLOR_COMMAND, "%s", cmd);
1073 ret = para_exec_cmdline_pid(&cmd_pid, cmd, fds);
1076 ret = mark_fd_nonblocking(fds[1]);
1079 ret = mark_fd_nonblocking(fds[2]);
1082 command_fds[0] = fds[1];
1083 command_fds[1] = fds[2];
1086 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
1087 close(command_fds[0]);
1088 close(command_fds[1]);
1091 static void para_cmd(char *cmd)
1095 print_in_bar(COLOR_MSG, "executing client command, hit any key to abort\n");
1096 c = make_message(BINDIR "/para_client -- %s", cmd);
1097 exec_and_display_cmd(c);
1101 static void display_cmd(char *cmd)
1103 print_in_bar(COLOR_MSG, "executing display command, hit any key to abort");
1104 exec_and_display_cmd(cmd);
1108 * shutdown curses and stat pipe before executing external commands
1110 static void external_cmd(char *cmd)
1112 int fds[3] = {-1, -1, -1};
1117 para_exec_cmdline_pid(&cmd_pid, cmd, fds);
1120 static void handle_command(int c)
1124 /* first check user-defined key bindings */
1125 for (i = 0; i < conf.key_map_given; ++i) {
1126 char *tmp, *handler, *arg;
1128 tmp = para_strdup(conf.key_map_arg[i]);
1129 if (!split_key_map(tmp, &handler, &arg)) {
1133 if (strcmp(tmp, km_keyname(c))) {
1137 if (*handler == 'd')
1139 else if (*handler == 'x')
1141 else if (*handler == 'p')
1143 else if (*handler == 'i') {
1144 int num = find_cmd_byname(arg);
1146 command_list[num].handler();
1151 /* not found, check internal key bindings */
1152 for (i = 0; command_list[i].handler; i++) {
1153 if (!strcmp(km_keyname(c), command_list[i].key)) {
1154 command_list[i].handler();
1158 print_in_bar(COLOR_ERRMSG, "key '%s' is not bound, press ? for help",
1162 static void input_post_select(void)
1165 enum cmd_status cmds = cmd_status();
1167 if (cmds == CMDS_XCMD)
1169 ret = wgetch(top.win);
1170 if (ret == ERR || ret == KEY_RESIZE)
1172 if (cmds == CMDS_IDLE)
1173 return handle_command(ret);
1175 kill(cmd_pid, SIGTERM);
1178 static void signal_pre_select(fd_set *rfds, int *max_fileno)
1180 para_fd_set(signal_pipe, rfds, max_fileno);
1184 * This is the core select loop. It checks the following fds:
1188 * - stdout/stderr of display or internal commands
1190 __noreturn static void do_select(void)
1193 int ret, max_fileno;
1197 tv.tv_sec = conf.timeout_arg / 1000;
1198 tv.tv_usec = (conf.timeout_arg % 1000) * 1000;
1201 status_pre_select(&rfds, &max_fileno, &tv);
1202 signal_pre_select(&rfds, &max_fileno);
1203 command_pre_select(&rfds, &max_fileno);
1204 input_pre_select(&rfds, &max_fileno);
1205 ret = para_select(max_fileno + 1, &rfds, NULL, &tv);
1207 goto repeat; /* skip fd checks */
1208 signal_post_select(&rfds);
1209 command_post_select(&rfds);
1210 status_post_select(&rfds);
1211 input_post_select();
1215 static void print_scroll_msg(void)
1217 unsigned lines_total, filled = ringbuffer_filled(bot_win_rb);
1218 int first_rbe = first_visible_rbe(&lines_total);
1220 print_in_bar(COLOR_MSG, "scrolled view: %d-%d/%d\n", filled - first_rbe,
1221 filled - scroll_position, ringbuffer_filled(bot_win_rb));
1224 static void com_scroll_top(void)
1226 int i = RINGBUFFER_SIZE - 1, bot_lines = get_num_lines(&bot);
1229 while (i > 0 && !ringbuffer_get(bot_win_rb, i))
1231 /* i is oldest entry */
1232 for (; lines < bot_lines && i >= 0; i--) {
1233 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
1236 lines += NUM_LINES(rbe->len);
1239 if (lines > 0 && scroll_position != i) {
1240 scroll_position = i;
1245 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1248 static void com_cancel_scrolling(void)
1251 if (scroll_position == 0) {
1252 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1255 scroll_position = 0;
1259 static void com_page_down(void)
1262 int i = scroll_position, bot_lines = get_num_lines(&bot);
1264 while (lines < bot_lines && --i > 0) {
1265 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
1268 lines += NUM_LINES(rbe->len);
1271 scroll_position = i;
1276 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1279 static void com_page_up(void)
1282 int fvr = first_visible_rbe(&lines), bot_lines = get_num_lines(&bot);
1284 if (fvr < 0 || fvr + 1 >= ringbuffer_filled(bot_win_rb)) {
1285 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1288 scroll_position = fvr + 1;
1289 for (; scroll_position > 0; scroll_position--) {
1290 first_visible_rbe(&lines);
1291 if (lines == bot_lines)
1298 static void com_scroll_down(void)
1300 struct rb_entry *rbe;
1301 int rbe_lines, bot_lines = get_num_lines(&bot);
1303 if (!scroll_position) {
1304 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1308 rbe = ringbuffer_get(bot_win_rb, scroll_position);
1309 rbe_lines = NUM_LINES(rbe->len);
1310 wscrl(bot.win, rbe_lines);
1311 wmove(bot.win, bot_lines - rbe_lines, 0);
1312 wattron(bot.win, COLOR_PAIR(rbe->color));
1313 waddstr(bot.win, rbe->msg);
1318 static void com_scroll_up(void)
1320 struct rb_entry *rbe = NULL;
1322 int i, first_rbe, num_scroll;
1324 /* the entry that is going to vanish */
1325 rbe = ringbuffer_get(bot_win_rb, scroll_position);
1328 num_scroll = NUM_LINES(rbe->len);
1329 first_rbe = first_visible_rbe(&lines);
1330 if (first_rbe < 0 || (first_rbe == ringbuffer_filled(bot_win_rb) - 1))
1333 wscrl(bot.win, -num_scroll);
1334 i = draw_top_rbe(&lines);
1337 while (i > 0 && lines < num_scroll) {
1339 rbe = ringbuffer_get(bot_win_rb, --i);
1342 rbe_lines = NUM_LINES(rbe->len);
1344 wattron(bot.win, COLOR_PAIR(rbe->color));
1345 waddstr(bot.win, "\n");
1346 waddstr(bot.win, rbe->msg);
1355 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1358 static void com_ll_decr(void)
1360 if (loglevel <= LL_DEBUG) {
1361 print_in_bar(COLOR_ERRMSG,
1362 "loglevel already at maximal verbosity\n");
1366 print_in_bar(COLOR_MSG, "loglevel set to %d\n", loglevel);
1369 static void com_ll_incr(void)
1371 if (loglevel >= LL_EMERG) {
1372 print_in_bar(COLOR_ERRMSG,
1373 "loglevel already at minimal verbosity\n");
1377 print_in_bar(COLOR_MSG, "loglevel set to %d\n", loglevel);
1380 static void com_reread_conf(void)
1385 static void com_help(void)
1389 for (i = 0; i < conf.key_map_given; ++i) {
1390 char *handler, *arg, *tmp = para_strdup(conf.key_map_arg[i]);
1391 const char *handler_text = "???", *desc = NULL;
1393 if (!split_key_map(tmp, &handler, &arg)) {
1399 handler_text = "internal";
1400 desc = command_list[find_cmd_byname(arg)].description;
1402 case 'x': handler_text = "external"; break;
1403 case 'd': handler_text = "display "; break;
1404 case 'p': handler_text = "para "; break;
1406 outputf(COLOR_MSG, "%s\t%s\t%s%s\t%s", tmp, handler_text, arg,
1407 strlen(arg) < 8? "\t" : "",
1411 for (i = 0; command_list[i].handler; i++) {
1412 struct gui_command gc = command_list[i];
1414 outputf(COLOR_MSG, "%s\tinternal\t%s\t%s%s", gc.key, gc.name,
1415 strlen(gc.name) < 8? "\t" : "",
1418 print_in_bar(COLOR_MSG, "try \"para_gui -h\" or \"para_client help\" "
1422 static void com_shrink_top_win(void)
1424 int top_lines = get_num_lines(&top);
1426 if (top_lines <= theme.top_lines_min) {
1427 PARA_WARNING_LOG("can not decrease top window\n");
1430 init_wins(top_lines - 1);
1431 print_in_bar(COLOR_MSG, "%s", "decreased top window");
1434 static void com_enlarge_top_win(void)
1436 int top_lines = get_num_lines(&top), bot_lines = get_num_lines(&bot);
1438 if (bot_lines < 3) {
1439 PARA_WARNING_LOG("can not increase top window\n");
1442 init_wins(top_lines + 1);
1443 print_in_bar(COLOR_MSG, "increased top window");
1446 static void com_version(void)
1448 print_in_bar(COLOR_MSG, "%s", version_single_line("gui"));
1451 __noreturn static void com_quit(void)
1453 die(EXIT_SUCCESS, "%s", "");
1456 static void com_refresh(void)
1462 static void com_next_theme(void)
1468 static void com_prev_theme(void)
1474 __noreturn static void print_help_and_die(void)
1476 struct ggo_help h = DEFINE_GGO_HELP(gui);
1477 bool d = conf.detailed_help_given;
1479 ggo_print_help(&h, d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS);
1483 int main(int argc, char *argv[])
1485 gui_cmdline_parser(argc, argv, &conf); /* exits on errors */
1486 loglevel = get_loglevel_by_name(conf.loglevel_arg);
1487 version_handle_flag("gui", conf.version_given);
1488 if (conf.help_given || conf.detailed_help_given)
1489 print_help_and_die();
1490 parse_config_file_or_die(false /* override */);
1491 setup_signal_handling();
1492 bot_win_rb = ringbuffer_new(RINGBUFFER_SIZE);
1493 setlocale(LC_CTYPE, "");
1494 initscr(); /* needed only once, always successful */