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 {GETCH_MODE, COMMAND_MODE, EXTERNAL_MODE};
60 * Codes for various colors.
62 * Each status item has its own color pair. The ones defined here start at a
63 * higher number so that they do not overlap with these.
66 COLOR_STATUSBAR = NUM_STAT_ITEMS + 1,
79 const char *description;
80 void (*handler)(void);
83 static struct gui_theme theme;
85 static void com_help(void);
86 static void com_reread_conf(void);
87 static void com_enlarge_top_win(void);
88 static void com_shrink_top_win(void);
89 static void com_version(void);
90 __noreturn static void com_quit(void);
91 static void com_refresh(void);
92 static void com_ll_incr(void);
93 static void com_ll_decr(void);
94 static void com_prev_theme(void);
95 static void com_next_theme(void);
96 static void com_scroll_up(void);
97 static void com_scroll_down(void);
98 static void com_page_up(void);
99 static void com_page_down(void);
100 static void com_cancel_scrolling(void);
101 static void com_scroll_top(void);
103 static struct gui_command command_list[] = {
107 .description = "print help",
111 .name = "enlarge_win",
112 .description = "enlarge the top window",
113 .handler = com_enlarge_top_win
116 .name = "shrink_win",
117 .description = "shrink the top window",
118 .handler = com_shrink_top_win
121 .name = "reread_conf",
122 .description = "reread configuration file",
123 .handler = com_reread_conf
127 .description = "exit para_gui",
132 .description = "redraw the screen",
133 .handler = com_refresh
136 .name = "next_theme",
137 .description = "switch to next theme",
138 .handler = com_next_theme
141 .name = "prev_theme",
142 .description = "switch to previous stream",
143 .handler = com_prev_theme
147 .description = "increase loglevel (decreases verbosity)",
148 .handler = com_ll_incr
152 .description = "decrease loglevel (increases verbosity)",
153 .handler = com_ll_decr
157 .description = "show the para_gui version",
158 .handler = com_version
162 .description = "scroll up one line",
163 .handler = com_scroll_up
166 .name = "scroll_down",
167 .description = "scroll down one line",
168 .handler = com_scroll_down
172 .description = "scroll up one page",
173 .handler = com_page_up
177 .description = "scroll down one page",
178 .handler = com_page_down
181 .name = "scroll_top",
182 .description = "scroll to top of buffer",
183 .handler = com_scroll_top
186 .name = "cancel_scroll",
187 .description = "deactivate scroll mode",
188 .handler = com_cancel_scrolling
194 static int find_cmd_byname(char *name)
198 for (i = 0; command_list[i].handler; i++)
199 if (!strcmp(command_list[i].name, name))
205 * Even though ncurses provides getmaxx and getmaxy, these functions/macros are
206 * not described in the XSI Curses standard.
208 static int get_num_lines(struct gui_window *w)
211 __a_unused int cols; /* avoid "set but not used" warnings */
213 getmaxyx(w->win, lines, cols);
217 static int get_num_cols(struct gui_window *w)
219 __a_unused int lines; /* avoid "set but not used" warnings */
222 getmaxyx(w->win, lines, cols);
226 /** Number of lines of the window are occupied by an output line. */
227 #define NUM_LINES(len) (1 + (len) / get_num_cols(&bot))
229 /* isendwin() returns false before initscr() was called */
230 static bool curses_active(void)
232 return top.win && !isendwin();
235 /* taken from mutt */
236 static char *km_keyname(int c)
241 sprintf(buf, "<up>");
245 sprintf(buf, "<down>");
249 sprintf(buf, "<left>");
252 if (c == KEY_RIGHT) {
253 sprintf(buf, "<right>");
256 if (c == KEY_NPAGE) {
257 sprintf(buf, "<npage>");
260 if (c == KEY_PPAGE) {
261 sprintf(buf, "<ppage>");
265 sprintf(buf, "<home>");
269 sprintf(buf, "<end>");
272 if (c < 256 && c > -128 && iscntrl((unsigned char) c)) {
277 buf[1] = (c + '@') & 0x7f;
280 snprintf(buf, sizeof(buf), "\\%d%d%d", c >> 6,
281 (c >> 3) & 7, c & 7);
282 } else if (c >= KEY_F0 && c < KEY_F(256))
283 sprintf(buf, "<F%d>", c - KEY_F0);
285 snprintf(buf, sizeof(buf), "%c", (unsigned char) c);
287 snprintf(buf, sizeof(buf), "\\x%hx", (unsigned short) c);
291 /* Print given number of spaces to curses window. */
292 static void add_spaces(WINDOW* win, unsigned int num)
295 unsigned sz = sizeof(space) - 1; /* number of spaces */
309 * print aligned string to curses window. This function always prints
312 static int align_str(WINDOW* win, char *str, unsigned int len,
315 int ret, i, num; /* of spaces */
320 ret = strwidth(str, &width);
322 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
331 /* replace control characters by spaces */
332 for (i = 0; i < len && str[i]; i++) {
333 if (str[i] == '\n' || str[i] == '\r' || str[i] == '\f')
338 add_spaces(win, num);
339 } else if (align == RIGHT) {
340 add_spaces(win, num);
343 add_spaces(win, num / 2);
344 waddstr(win, str[0]? str: "");
345 add_spaces(win, num - num / 2);
350 __printf_2_3 static void print_in_bar(int color, const char *fmt,...)
355 if (!curses_active())
357 wattron(in.win, COLOR_PAIR(color));
359 xvasprintf(&msg, fmt, ap);
362 align_str(in.win, msg, get_num_cols(&in), LEFT);
367 static void print_status_bar(void)
371 tmp = para_strdup("para_gui " PACKAGE_VERSION " (hit ? for help)");
373 align_str(sb.win, tmp, get_num_cols(&sb), CENTER);
378 * get the number of the oldest rbe that is (partially) visible. On return,
379 * lines contains the sum of the number of lines of all visible entries. If the
380 * first one is only partially visible, lines is greater than bot.lines.
382 static int first_visible_rbe(unsigned *lines)
384 int i, bot_lines = get_num_lines(&bot);
387 for (i = scroll_position; i < RINGBUFFER_SIZE; i++) {
388 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
392 rbe_lines = NUM_LINES(rbe->len);
393 if (rbe_lines > bot_lines)
396 if (*lines >= bot_lines)
399 return RINGBUFFER_SIZE - 1;
403 returns number of first visible rbe, *lines is the number of lines drawn.
405 static int draw_top_rbe(unsigned *lines)
407 int bot_cols, bot_lines, ret, fvr = first_visible_rbe(lines);
408 struct rb_entry *rbe;
409 size_t bytes_to_skip, cells_to_skip, width;
413 wmove(bot.win, 0, 0);
414 rbe = ringbuffer_get(bot_win_rb, fvr);
417 getmaxyx(bot.win, bot_lines, bot_cols);
418 if (*lines > bot_lines) {
419 /* rbe is partially visible multi-line */
420 cells_to_skip = (*lines - bot_lines) * bot_cols;
421 ret = skip_cells(rbe->msg, cells_to_skip, &bytes_to_skip);
424 ret = strwidth(rbe->msg + bytes_to_skip, &width);
431 wattron(bot.win, COLOR_PAIR(rbe->color));
432 waddstr(bot.win, rbe->msg + bytes_to_skip);
433 *lines = NUM_LINES(width);
437 static void redraw_bot_win(void)
440 int i, bot_lines = get_num_lines(&bot);
442 wmove(bot.win, 0, 0);
444 i = draw_top_rbe(&lines);
447 while (i > 0 && lines < bot_lines) {
448 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, --i);
451 waddstr(bot.win, "\n");
454 lines += NUM_LINES(rbe->len);
455 wattron(bot.win, COLOR_PAIR(rbe->color));
456 waddstr(bot.win, "\n");
457 waddstr(bot.win, rbe->msg);
463 static void rb_add_entry(int color, char *msg)
465 struct rb_entry *old, *new;
469 if (strwidth(msg, &len) < 0)
471 new = para_malloc(sizeof(struct rb_entry));
475 old = ringbuffer_add(bot_win_rb, new);
480 if (scroll_position) {
481 /* discard current scrolling, like xterm does */
486 wattron(bot.win, COLOR_PAIR(color));
487 getyx(bot.win, y, x);
489 waddstr(bot.win, "\n");
490 waddstr(bot.win, msg);
494 * print formated output to bot win and refresh
496 __printf_2_3 static void outputf(int color, const char* fmt,...)
501 if (!curses_active())
504 xvasprintf(&msg, fmt, ap);
506 rb_add_entry(color, msg);
510 static int add_output_line(char *line, void *data)
512 int color = *(int *)data? COLOR_ERRMSG : COLOR_OUTPUT;
514 if (!curses_active())
516 rb_add_entry(color, para_strdup(line));
520 static __printf_2_3 void curses_log(int ll, const char *fmt,...)
527 if (curses_active()) {
528 int color = ll <= LL_NOTICE? COLOR_MSG : COLOR_ERRMSG;
530 unsigned bytes = xvasprintf(&msg, fmt, ap);
531 if (bytes > 0 && msg[bytes - 1] == '\n')
532 msg[bytes - 1] = '\0'; /* cut trailing newline */
533 rb_add_entry(color, msg);
535 } else if (cmd_pid <= 0) /* no external command running */
536 vfprintf(stderr, fmt, ap);
539 __printf_2_3 void (*para_log)(int, const char*, ...) = curses_log;
541 static void setup_signal_handling(void)
543 signal_pipe = para_signal_init();
544 para_install_sighandler(SIGINT);
545 para_install_sighandler(SIGTERM);
546 para_install_sighandler(SIGCHLD);
547 para_install_sighandler(SIGWINCH);
548 para_install_sighandler(SIGUSR1);
551 static void shutdown_curses(void)
557 /* disable curses, print a message, kill running processes and exit */
558 __noreturn __printf_2_3 static void die(int exit_code, const char* fmt, ...)
564 vfprintf(stderr, fmt, argp);
566 /* kill every process in the process group and exit */
567 para_sigaction(SIGTERM, SIG_IGN);
573 * Print stat item #i to curses window
575 static void print_stat_item(int i)
578 struct stat_item_data d = theme.data[i];
579 char *c = stat_content[i];
580 int top_lines = get_num_lines(&top);
582 if (!curses_active() || !d.len || !c)
584 tmp = make_message("%s%s%s", d.prefix, c, d.postfix);
585 wmove(top.win, d.y * top_lines / 100, d.x * COLS / 100);
587 wattron(top.win, COLOR_PAIR(i + 1));
588 align_str(top.win, tmp, d.len * COLS / 100, d.align);
593 static int update_item(int item_num, char *buf)
595 char **c = stat_content + item_num;
602 *c = para_strdup("(artist tag not set)");
605 *c = para_strdup("(title tag not set)");
608 *c = para_strdup("????");
611 *c = para_strdup("(album tag not set)");
614 *c = para_strdup("(comment tag not set)");
618 *c = para_strdup(buf);
620 print_stat_item(item_num);
624 static void print_all_items(void)
628 if (!curses_active())
630 FOR_EACH_STATUS_ITEM(i)
634 static void clear_all_items(void)
638 FOR_EACH_STATUS_ITEM(i) {
639 free(stat_content[i]);
640 stat_content[i] = para_strdup("");
644 static void status_post_select(fd_set *rfds)
647 static int bufsize, loaded;
653 if (loaded >= bufsize) {
654 if (bufsize > 1000 * 1000) {
658 bufsize += bufsize + 1000;
659 buf = para_realloc(buf, bufsize);
661 assert(loaded < bufsize);
662 ret = read_nonblock(stat_pipe, buf + loaded, bufsize - loaded,
665 ret2 = for_each_stat_item(buf, loaded, update_item);
666 if (ret < 0 || ret2 < 0) {
668 PARA_NOTICE_LOG("closing stat pipe: %s\n", para_strerror(-ret));
672 free(stat_content[SI_BASENAME]);
673 stat_content[SI_BASENAME] =
674 para_strdup("stat command terminated!?");
678 sz = ret2; /* what is left */
679 if (sz > 0 && sz < loaded)
680 memmove(buf, buf + loaded - sz, sz);
687 static void init_wins(int top_lines)
689 int top_y = 0, bot_y = top_lines + 1, sb_y = LINES - 2,
690 in_y = LINES - 1, sep_y = top_lines;
691 int bot_lines = LINES - top_lines - 3, sb_lines = 1, in_lines = 1,
694 assume_default_colors(theme.default_fg, theme.default_bg);
696 wresize(top.win, top_lines, COLS);
697 mvwin(top.win, top_y, 0);
699 wresize(sb.win, sb_lines, COLS);
700 mvwin(sb.win, sb_y, 0);
702 wresize(sep.win, sep_lines, COLS);
703 mvwin(sep.win, sep_y, 0);
705 wresize(bot.win, bot_lines, COLS);
706 mvwin(bot.win, bot_y, 0);
708 wresize(in.win, in_lines, COLS);
709 mvwin(in.win, in_y, 0);
711 sep.win = newwin(sep_lines, COLS, sep_y, 0);
712 top.win = newwin(top_lines, COLS, top_y, 0);
713 bot.win = newwin(bot_lines, COLS, bot_y, 0);
714 sb.win = newwin(sb_lines, COLS, sb_y, 0);
715 in.win = newwin(in_lines, COLS, in_y, 0);
716 if (!top.win || !bot.win || !sb.win || !in.win || !sep.win)
717 die(EXIT_FAILURE, "Error: Cannot create curses windows\n");
721 scrollok(bot.win, 1);
722 wattron(sb.win, COLOR_PAIR(COLOR_STATUSBAR));
723 wattron(sep.win, COLOR_PAIR(COLOR_SEPARATOR));
724 wattron(bot.win, COLOR_PAIR(COLOR_BOT));
725 wattron(top.win, COLOR_PAIR(COLOR_TOP));
736 wmove(sep.win, 0, 0);
737 whline(sep.win, theme.sep_char, COLS);
741 wnoutrefresh(top.win);
742 wnoutrefresh(bot.win);
744 wnoutrefresh(sb.win);
745 wnoutrefresh(in.win);
746 wnoutrefresh(sep.win);
750 static void init_pair_or_die(short pair, short f, short b)
752 if (init_pair(pair, f, b) == ERR)
753 die(EXIT_FAILURE, "fatal: init_pair() failed\n");
756 static void init_colors_or_die(void)
761 die(EXIT_FAILURE, "fatal: No color term\n");
762 if (start_color() == ERR)
763 die(EXIT_FAILURE, "fatal: failed to start colors\n");
764 FOR_EACH_STATUS_ITEM(i)
765 if (theme.data[i].len)
766 init_pair_or_die(i + 1, theme.data[i].fg,
768 init_pair_or_die(COLOR_STATUSBAR, theme.sb_fg, theme.sb_bg);
769 init_pair_or_die(COLOR_COMMAND, theme.cmd_fg, theme.cmd_bg);
770 init_pair_or_die(COLOR_OUTPUT, theme.output_fg, theme.output_bg);
771 init_pair_or_die(COLOR_MSG, theme.msg_fg, theme.msg_bg);
772 init_pair_or_die(COLOR_ERRMSG, theme.err_msg_fg, theme.err_msg_bg);
773 init_pair_or_die(COLOR_SEPARATOR, theme.sep_fg, theme.sep_bg);
774 init_pair_or_die(COLOR_TOP, theme.default_fg, theme.default_bg);
775 init_pair_or_die(COLOR_BOT, theme.default_fg, theme.default_bg);
778 /* (Re-)initialize the curses library. */
779 static void init_curses(void)
783 if (top.win && refresh() == ERR) /* refresh is really needed */
784 die(EXIT_FAILURE, "refresh() failed\n");
785 if (LINES < theme.lines_min || COLS < theme.cols_min)
786 die(EXIT_FAILURE, "Terminal (%dx%d) too small"
787 " (need at least %dx%d)\n", COLS, LINES,
788 theme.cols_min, theme.lines_min);
789 curs_set(0); /* make cursor invisible, ignore errors */
790 nonl(); /* do not NL->CR/NL on output, always returns OK */
791 /* don't echo input */
793 die(EXIT_FAILURE, "fatal: noecho() failed\n");
794 /* take input chars one at a time, no wait for \n */
796 die(EXIT_FAILURE, "fatal: cbreak() failed\n");
797 init_colors_or_die();
798 clear(); /* ignore non-fatal errors */
799 init_wins(theme.top_lines_default);
800 // noecho(); /* don't echo input */
803 static void check_sigchld(void)
809 ret = para_reap_child(&pid);
814 goto reap_next_child;
818 * This sucker modifies its first argument. *handler and *arg are
819 * pointers to 0-terminated strings (inside line). Crap.
821 static int split_key_map(char *line, char **handler, char **arg)
823 if (!(*handler = strchr(line + 1, ':')))
827 if (!(*arg = strchr(*handler, ':')))
836 static void check_key_map_args_or_die(void)
841 for (i = 0; i < conf.key_map_given; ++i) {
845 tmp = para_strdup(conf.key_map_arg[i]);
846 if (!split_key_map(tmp, &handler, &arg))
848 if (strlen(handler) != 1)
850 if (*handler != 'x' && *handler != 'd' && *handler != 'i'
855 if (find_cmd_byname(arg) < 0)
858 if (i != conf.key_map_given)
859 die(EXIT_FAILURE, "invalid key map: %s\n", conf.key_map_arg[i]);
863 static void parse_config_file_or_die(bool override)
867 struct gui_cmdline_parser_params params = {
868 .override = override,
870 .check_required = !override,
871 .check_ambiguity = 0,
875 if (conf.config_file_given)
876 config_file = para_strdup(conf.config_file_arg);
878 char *home = para_homedir();
879 config_file = make_message("%s/.paraslash/gui.conf", home);
882 if (!file_exists(config_file)) {
883 if (!conf.config_file_given)
886 PARA_EMERG_LOG("config file %s does not exist\n",
892 gui_cmdline_parser_config_file(config_file, &conf, ¶ms);
893 loglevel = get_loglevel_by_name(conf.loglevel_arg);
894 check_key_map_args_or_die();
895 theme_init(conf.theme_arg, &theme);
903 /* reread configuration, terminate on errors */
904 static void reread_conf(void)
907 * gengetopt might print to stderr and exit on errors. So we have to
908 * shutdown curses first.
911 parse_config_file_or_die(true /* override */);
913 print_in_bar(COLOR_MSG, "config file reloaded\n");
917 * React to various signal-related events
919 static void handle_signal(int sig)
923 die(EXIT_FAILURE, "only the good die young (caught SIGTERM)\n");
926 if (curses_active()) {
933 PARA_WARNING_LOG("caught SIGINT, reset\n");
934 /* Nothing to do. SIGINT killed our child which gets noticed
935 * by do_select and resets everything.
939 PARA_NOTICE_LOG("got SIGUSR1, rereading configuration\n");
948 static void status_pre_select(fd_set *rfds, int *max_fileno, struct timeval *tv)
950 static struct timeval next_exec, atm, diff;
951 int ret, fds[3] = {0, 1, 0};
956 /* Avoid busy loop */
957 gettimeofday(&atm, NULL);
958 if (tv_diff(&next_exec, &atm, &diff) > 0) {
959 if (tv_diff(&diff, tv, NULL) < 0)
963 next_exec.tv_sec = atm.tv_sec + 2;
964 ret = para_exec_cmdline_pid(&pid, conf.stat_cmd_arg, fds);
967 ret = mark_fd_nonblocking(fds[1]);
974 para_fd_set(stat_pipe, rfds, max_fileno);
977 #define COMMAND_BUF_SIZE 32768
979 static void command_pre_select(int mode, fd_set *rfds, int *max_fileno)
981 /* check command pipe only in COMMAND_MODE */
982 if (mode != COMMAND_MODE)
984 if (command_fds[0] >= 0)
985 para_fd_set(command_fds[0], rfds, max_fileno);
986 if (command_fds[1] >= 0)
987 para_fd_set(command_fds[1], rfds, max_fileno);
990 static int command_post_select(int mode, fd_set *rfds)
993 static char command_buf[2][COMMAND_BUF_SIZE];
994 static int cbo[2]; /* command buf offsets */
995 static unsigned flags[2]; /* for for_each_line() */
997 if (mode != COMMAND_MODE)
999 for (i = 0; i < 2; i++) {
1001 if (command_fds[i] < 0)
1003 ret = read_nonblock(command_fds[i],
1004 command_buf[i] + cbo[i],
1005 COMMAND_BUF_SIZE - 1 - cbo[i], rfds, &sz);
1008 cbo[i] = for_each_line(flags[i], command_buf[i], cbo[i],
1009 add_output_line, &i);
1010 if (sz != cbo[i]) { /* at least one line found */
1015 PARA_NOTICE_LOG("closing command fd %d: %s",
1016 i, para_strerror(-ret));
1017 close(command_fds[i]);
1018 command_fds[i] = -1;
1021 if (command_fds[!i] < 0) /* both fds closed */
1024 if (cbo[i] == COMMAND_BUF_SIZE - 1) {
1025 PARA_NOTICE_LOG("discarding overlong line");
1027 flags[i] = FELF_DISCARD_FIRST;
1034 * This is the core select loop. Besides the (internal) signal
1035 * pipe, the following other fds are checked according to the mode:
1037 * GETCH_MODE: check stdin, return when key is pressed
1039 * COMMAND_MODE: check command fds and stdin. Return when peer has closed both
1040 * stdout and stderr or when any key is pressed.
1042 * EXTERNAL_MODE: Check only signal pipe. Used when an external command
1043 * is running. During that time curses is disabled. Returns when
1046 static int do_select(int mode)
1049 int ret, max_fileno;
1053 tv.tv_sec = conf.timeout_arg / 1000;
1054 tv.tv_usec = (conf.timeout_arg % 1000) * 1000;
1055 // ret = refresh_status();
1058 status_pre_select(&rfds, &max_fileno, &tv);
1060 para_fd_set(signal_pipe, &rfds, &max_fileno);
1061 command_pre_select(mode, &rfds, &max_fileno);
1062 if (mode == GETCH_MODE || mode == COMMAND_MODE)
1063 para_fd_set(STDIN_FILENO, &rfds, &max_fileno);
1064 ret = para_select(max_fileno + 1, &rfds, NULL, &tv);
1066 goto check_return; /* skip fd checks */
1068 ret = para_next_signal(&rfds);
1071 /* read command pipe if ready */
1072 ret = command_post_select(mode, &rfds);
1075 status_post_select(&rfds);
1079 ret = wgetch(top.win);
1080 if (ret != ERR && ret != KEY_RESIZE) {
1082 kill(cmd_pid, SIGTERM);
1087 ret = wgetch(top.win);
1088 if (ret != ERR && ret != KEY_RESIZE)
1098 /* read from command pipe and print data to bot window */
1099 static void exec_and_display_cmd(const char *cmd)
1101 int ret, fds[3] = {0, 1, 1};
1103 outputf(COLOR_COMMAND, "%s", cmd);
1104 ret = para_exec_cmdline_pid(&cmd_pid, cmd, fds);
1107 ret = mark_fd_nonblocking(fds[1]);
1110 ret = mark_fd_nonblocking(fds[2]);
1113 command_fds[0] = fds[1];
1114 command_fds[1] = fds[2];
1115 if (do_select(COMMAND_MODE) >= 0)
1116 PARA_INFO_LOG("command complete\n");
1118 PARA_NOTICE_LOG("command aborted\n");
1119 print_in_bar(COLOR_MSG, " ");
1122 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
1123 close(command_fds[0]);
1124 close(command_fds[1]);
1127 static void para_cmd(char *cmd)
1131 print_in_bar(COLOR_MSG, "executing client command, hit any key to abort\n");
1132 c = make_message(BINDIR "/para_client -- %s", cmd);
1133 exec_and_display_cmd(c);
1137 static void display_cmd(char *cmd)
1139 print_in_bar(COLOR_MSG, "executing display command, hit any key to abort");
1140 exec_and_display_cmd(cmd);
1144 * shutdown curses and stat pipe before executing external commands
1146 static void external_cmd(char *cmd)
1148 int fds[3] = {-1, -1, -1};
1153 if (para_exec_cmdline_pid(&cmd_pid, cmd, fds) < 0)
1155 do_select(EXTERNAL_MODE);
1159 static void print_scroll_msg(void)
1161 unsigned lines_total, filled = ringbuffer_filled(bot_win_rb);
1162 int first_rbe = first_visible_rbe(&lines_total);
1164 print_in_bar(COLOR_MSG, "scrolled view: %d-%d/%d\n", filled - first_rbe,
1165 filled - scroll_position, ringbuffer_filled(bot_win_rb));
1168 static void com_scroll_top(void)
1170 int i = RINGBUFFER_SIZE - 1, bot_lines = get_num_lines(&bot);
1173 while (i > 0 && !ringbuffer_get(bot_win_rb, i))
1175 /* i is oldest entry */
1176 for (; lines < bot_lines && i >= 0; i--) {
1177 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
1180 lines += NUM_LINES(rbe->len);
1183 if (lines > 0 && scroll_position != i) {
1184 scroll_position = i;
1189 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1192 static void com_cancel_scrolling(void)
1195 if (scroll_position == 0) {
1196 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1199 scroll_position = 0;
1203 static void com_page_down(void)
1206 int i = scroll_position, bot_lines = get_num_lines(&bot);
1208 while (lines < bot_lines && --i > 0) {
1209 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
1212 lines += NUM_LINES(rbe->len);
1215 scroll_position = i;
1220 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1223 static void com_page_up(void)
1226 int fvr = first_visible_rbe(&lines), bot_lines = get_num_lines(&bot);
1228 if (fvr < 0 || fvr + 1 >= ringbuffer_filled(bot_win_rb)) {
1229 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1232 scroll_position = fvr + 1;
1233 for (; scroll_position > 0; scroll_position--) {
1234 first_visible_rbe(&lines);
1235 if (lines == bot_lines)
1242 static void com_scroll_down(void)
1244 struct rb_entry *rbe;
1245 int rbe_lines, bot_lines = get_num_lines(&bot);
1247 if (!scroll_position) {
1248 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1252 rbe = ringbuffer_get(bot_win_rb, scroll_position);
1253 rbe_lines = NUM_LINES(rbe->len);
1254 wscrl(bot.win, rbe_lines);
1255 wmove(bot.win, bot_lines - rbe_lines, 0);
1256 wattron(bot.win, COLOR_PAIR(rbe->color));
1257 waddstr(bot.win, rbe->msg);
1262 static void com_scroll_up(void)
1264 struct rb_entry *rbe = NULL;
1266 int i, first_rbe, num_scroll;
1268 /* the entry that is going to vanish */
1269 rbe = ringbuffer_get(bot_win_rb, scroll_position);
1272 num_scroll = NUM_LINES(rbe->len);
1273 first_rbe = first_visible_rbe(&lines);
1274 if (first_rbe < 0 || (first_rbe == ringbuffer_filled(bot_win_rb) - 1))
1277 wscrl(bot.win, -num_scroll);
1278 i = draw_top_rbe(&lines);
1281 while (i > 0 && lines < num_scroll) {
1283 rbe = ringbuffer_get(bot_win_rb, --i);
1286 rbe_lines = NUM_LINES(rbe->len);
1288 wattron(bot.win, COLOR_PAIR(rbe->color));
1289 waddstr(bot.win, "\n");
1290 waddstr(bot.win, rbe->msg);
1299 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1302 static void com_ll_decr(void)
1304 if (loglevel <= LL_DEBUG) {
1305 print_in_bar(COLOR_ERRMSG,
1306 "loglevel already at maximal verbosity\n");
1310 print_in_bar(COLOR_MSG, "loglevel set to %d\n", loglevel);
1313 static void com_ll_incr(void)
1315 if (loglevel >= LL_EMERG) {
1316 print_in_bar(COLOR_ERRMSG,
1317 "loglevel already at minimal verbosity\n");
1321 print_in_bar(COLOR_MSG, "loglevel set to %d\n", loglevel);
1324 static void com_reread_conf(void)
1329 static void com_help(void)
1333 for (i = 0; i < conf.key_map_given; ++i) {
1334 char *handler, *arg, *tmp = para_strdup(conf.key_map_arg[i]);
1335 const char *handler_text = "???", *desc = NULL;
1337 if (!split_key_map(tmp, &handler, &arg)) {
1343 handler_text = "internal";
1344 desc = command_list[find_cmd_byname(arg)].description;
1346 case 'x': handler_text = "external"; break;
1347 case 'd': handler_text = "display "; break;
1348 case 'p': handler_text = "para "; break;
1350 outputf(COLOR_MSG, "%s\t%s\t%s%s\t%s", tmp, handler_text, arg,
1351 strlen(arg) < 8? "\t" : "",
1355 for (i = 0; command_list[i].handler; i++) {
1356 struct gui_command gc = command_list[i];
1358 outputf(COLOR_MSG, "%s\tinternal\t%s\t%s%s", gc.key, gc.name,
1359 strlen(gc.name) < 8? "\t" : "",
1362 print_in_bar(COLOR_MSG, "try \"para_gui -h\" or \"para_client help\" "
1366 static void com_shrink_top_win(void)
1368 int top_lines = get_num_lines(&top);
1370 if (top_lines <= theme.top_lines_min) {
1371 PARA_WARNING_LOG("can not decrease top window\n");
1374 init_wins(top_lines - 1);
1375 print_in_bar(COLOR_MSG, "%s", "decreased top window");
1378 static void com_enlarge_top_win(void)
1380 int top_lines = get_num_lines(&top), bot_lines = get_num_lines(&bot);
1382 if (bot_lines < 3) {
1383 PARA_WARNING_LOG("can not increase top window\n");
1386 init_wins(top_lines + 1);
1387 print_in_bar(COLOR_MSG, "increased top window");
1390 static void com_version(void)
1392 print_in_bar(COLOR_MSG, "%s", version_single_line("gui"));
1395 __noreturn static void com_quit(void)
1397 die(EXIT_SUCCESS, "%s", "");
1400 static void com_refresh(void)
1406 static void com_next_theme(void)
1412 static void com_prev_theme(void)
1418 static void handle_command(int c)
1422 /* first check user-defined key bindings */
1423 for (i = 0; i < conf.key_map_given; ++i) {
1424 char *tmp, *handler, *arg;
1426 tmp = para_strdup(conf.key_map_arg[i]);
1427 if (!split_key_map(tmp, &handler, &arg)) {
1431 if (strcmp(tmp, km_keyname(c))) {
1435 if (*handler == 'd')
1437 else if (*handler == 'x')
1439 else if (*handler == 'p')
1441 else if (*handler == 'i') {
1442 int num = find_cmd_byname(arg);
1444 command_list[num].handler();
1449 /* not found, check internal key bindings */
1450 for (i = 0; command_list[i].handler; i++) {
1451 if (!strcmp(km_keyname(c), command_list[i].key)) {
1452 command_list[i].handler();
1456 print_in_bar(COLOR_ERRMSG, "key '%s' is not bound, press ? for help",
1460 __noreturn static void print_help_and_die(void)
1462 struct ggo_help h = DEFINE_GGO_HELP(gui);
1463 bool d = conf.detailed_help_given;
1465 ggo_print_help(&h, d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS);
1469 int main(int argc, char *argv[])
1473 gui_cmdline_parser(argc, argv, &conf); /* exits on errors */
1474 loglevel = get_loglevel_by_name(conf.loglevel_arg);
1475 version_handle_flag("gui", conf.version_given);
1476 if (conf.help_given || conf.detailed_help_given)
1477 print_help_and_die();
1478 parse_config_file_or_die(false /* override */);
1479 setup_signal_handling();
1480 bot_win_rb = ringbuffer_new(RINGBUFFER_SIZE);
1481 setlocale(LC_CTYPE, "");
1482 initscr(); /* needed only once, always successful */
1486 ret = do_select(GETCH_MODE);
1489 print_in_bar(COLOR_MSG, " ");
1490 handle_command(ret);