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;
55 enum {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 static char *configfile_exists(void)
292 static char *config_file;
295 if (!conf.config_file_given) {
297 char *home = para_homedir();
298 config_file = make_message("%s/.paraslash/gui.conf",
304 tmp = conf.config_file_arg;
305 return file_exists(tmp)? tmp: NULL;
308 /* Print given number of spaces to curses window. */
309 static void add_spaces(WINDOW* win, unsigned int num)
312 unsigned sz = sizeof(space) - 1; /* number of spaces */
326 * print aligned string to curses window. This function always prints
329 static int align_str(WINDOW* win, char *str, unsigned int len,
332 int ret, i, num; /* of spaces */
337 ret = strwidth(str, &width);
339 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
348 /* replace control characters by spaces */
349 for (i = 0; i < len && str[i]; i++) {
350 if (str[i] == '\n' || str[i] == '\r' || str[i] == '\f')
355 add_spaces(win, num);
356 } else if (align == RIGHT) {
357 add_spaces(win, num);
360 add_spaces(win, num / 2);
361 waddstr(win, str[0]? str: "");
362 add_spaces(win, num - num / 2);
367 __printf_2_3 static void print_in_bar(int color, const char *fmt,...)
372 if (!curses_active())
374 wattron(in.win, COLOR_PAIR(color));
376 xvasprintf(&msg, fmt, ap);
379 align_str(in.win, msg, get_num_cols(&in), LEFT);
384 static void print_status_bar(void)
388 tmp = para_strdup("para_gui " PACKAGE_VERSION " (hit ? for help)");
390 align_str(sb.win, tmp, get_num_cols(&sb), CENTER);
395 * get the number of the oldest rbe that is (partially) visible. On return,
396 * lines contains the sum of the number of lines of all visible entries. If the
397 * first one is only partially visible, lines is greater than bot.lines.
399 static int first_visible_rbe(unsigned *lines)
401 int i, bot_lines = get_num_lines(&bot);
404 for (i = scroll_position; i < RINGBUFFER_SIZE; i++) {
405 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
409 rbe_lines = NUM_LINES(rbe->len);
410 if (rbe_lines > bot_lines)
413 if (*lines >= bot_lines)
416 return RINGBUFFER_SIZE - 1;
420 returns number of first visible rbe, *lines is the number of lines drawn.
422 static int draw_top_rbe(unsigned *lines)
424 int bot_cols, bot_lines, ret, fvr = first_visible_rbe(lines);
425 struct rb_entry *rbe;
426 size_t bytes_to_skip, cells_to_skip, width;
430 wmove(bot.win, 0, 0);
431 rbe = ringbuffer_get(bot_win_rb, fvr);
434 getmaxyx(bot.win, bot_lines, bot_cols);
435 if (*lines > bot_lines) {
436 /* rbe is partially visible multi-line */
437 cells_to_skip = (*lines - bot_lines) * bot_cols;
438 ret = skip_cells(rbe->msg, cells_to_skip, &bytes_to_skip);
441 ret = strwidth(rbe->msg + bytes_to_skip, &width);
448 wattron(bot.win, COLOR_PAIR(rbe->color));
449 waddstr(bot.win, rbe->msg + bytes_to_skip);
450 *lines = NUM_LINES(width);
454 static void redraw_bot_win(void)
457 int i, bot_lines = get_num_lines(&bot);
459 wmove(bot.win, 0, 0);
461 i = draw_top_rbe(&lines);
464 while (i > 0 && lines < bot_lines) {
465 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, --i);
468 waddstr(bot.win, "\n");
471 lines += NUM_LINES(rbe->len);
472 wattron(bot.win, COLOR_PAIR(rbe->color));
473 waddstr(bot.win, "\n");
474 waddstr(bot.win, rbe->msg);
480 static void rb_add_entry(int color, char *msg)
482 struct rb_entry *old, *new;
486 if (strwidth(msg, &len) < 0)
488 new = para_malloc(sizeof(struct rb_entry));
492 old = ringbuffer_add(bot_win_rb, new);
497 if (scroll_position) {
498 /* discard current scrolling, like xterm does */
503 wattron(bot.win, COLOR_PAIR(color));
504 getyx(bot.win, y, x);
506 waddstr(bot.win, "\n");
507 waddstr(bot.win, msg);
511 * print formated output to bot win and refresh
513 __printf_2_3 static void outputf(int color, const char* fmt,...)
518 if (!curses_active())
521 xvasprintf(&msg, fmt, ap);
523 rb_add_entry(color, msg);
527 static int add_output_line(char *line, void *data)
529 int color = *(int *)data? COLOR_ERRMSG : COLOR_OUTPUT;
531 if (!curses_active())
533 rb_add_entry(color, para_strdup(line));
539 static __printf_2_3 void curses_log(int ll, const char *fmt,...)
546 if (curses_active()) {
547 int color = ll <= LL_NOTICE? COLOR_MSG : COLOR_ERRMSG;
549 unsigned bytes = xvasprintf(&msg, fmt, ap);
550 if (bytes > 0 && msg[bytes - 1] == '\n')
551 msg[bytes - 1] = '\0'; /* cut trailing newline */
552 rb_add_entry(color, msg);
554 } else if (cmd_pid <= 0) /* no external command running */
555 vfprintf(stderr, fmt, ap);
558 __printf_2_3 void (*para_log)(int, const char*, ...) = curses_log;
560 static void setup_signal_handling(void)
562 signal_pipe = para_signal_init();
563 para_install_sighandler(SIGINT);
564 para_install_sighandler(SIGTERM);
565 para_install_sighandler(SIGCHLD);
566 para_install_sighandler(SIGWINCH);
567 para_install_sighandler(SIGUSR1);
570 static void shutdown_curses(void)
576 /* disable curses, print a message, kill running processes and exit */
577 __noreturn __printf_2_3 static void die(int exit_code, const char* fmt, ...)
583 vfprintf(stderr, fmt, argp);
585 /* kill every process in the process group and exit */
586 para_sigaction(SIGTERM, SIG_IGN);
592 * Print stat item #i to curses window
594 static void print_stat_item(int i)
597 struct stat_item_data d = theme.data[i];
598 char *c = stat_content[i];
599 int top_lines = get_num_lines(&top);
601 if (!curses_active() || !d.len || !c)
603 tmp = make_message("%s%s%s", d.prefix, c, d.postfix);
604 wmove(top.win, d.y * top_lines / 100, d.x * COLS / 100);
606 wattron(top.win, COLOR_PAIR(i + 1));
607 align_str(top.win, tmp, d.len * COLS / 100, d.align);
612 static int update_item(int item_num, char *buf)
614 char **c = stat_content + item_num;
621 *c = para_strdup("(artist tag not set)");
624 *c = para_strdup("(title tag not set)");
627 *c = para_strdup("????");
630 *c = para_strdup("(album tag not set)");
633 *c = para_strdup("(comment tag not set)");
637 *c = para_strdup(buf);
639 print_stat_item(item_num);
643 static int read_stat_pipe(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 return ret2 < 0? ret2 : ret;
669 sz = ret2; /* what is left */
670 if (sz > 0 && sz < loaded)
671 memmove(buf, buf + loaded - sz, sz);
676 static void print_all_items(void)
680 if (!curses_active())
682 FOR_EACH_STATUS_ITEM(i)
686 static void clear_all_items(void)
690 FOR_EACH_STATUS_ITEM(i) {
691 free(stat_content[i]);
692 stat_content[i] = para_strdup("");
699 static void init_wins(int top_lines)
701 int top_y = 0, bot_y = top_lines + 1, sb_y = LINES - 2,
702 in_y = LINES - 1, sep_y = top_lines;
703 int bot_lines = LINES - top_lines - 3, sb_lines = 1, in_lines = 1,
706 assume_default_colors(theme.default_fg, theme.default_bg);
708 wresize(top.win, top_lines, COLS);
709 mvwin(top.win, top_y, 0);
711 wresize(sb.win, sb_lines, COLS);
712 mvwin(sb.win, sb_y, 0);
714 wresize(sep.win, sep_lines, COLS);
715 mvwin(sep.win, sep_y, 0);
717 wresize(bot.win, bot_lines, COLS);
718 mvwin(bot.win, bot_y, 0);
720 wresize(in.win, in_lines, COLS);
721 mvwin(in.win, in_y, 0);
723 sep.win = newwin(sep_lines, COLS, sep_y, 0);
724 top.win = newwin(top_lines, COLS, top_y, 0);
725 bot.win = newwin(bot_lines, COLS, bot_y, 0);
726 sb.win = newwin(sb_lines, COLS, sb_y, 0);
727 in.win = newwin(in_lines, COLS, in_y, 0);
728 if (!top.win || !bot.win || !sb.win || !in.win || !sep.win)
729 die(EXIT_FAILURE, "Error: Cannot create curses windows\n");
733 scrollok(bot.win, 1);
734 wattron(sb.win, COLOR_PAIR(COLOR_STATUSBAR));
735 wattron(sep.win, COLOR_PAIR(COLOR_SEPARATOR));
736 wattron(bot.win, COLOR_PAIR(COLOR_BOT));
737 wattron(top.win, COLOR_PAIR(COLOR_TOP));
748 wmove(sep.win, 0, 0);
749 whline(sep.win, theme.sep_char, COLS);
753 wnoutrefresh(top.win);
754 wnoutrefresh(bot.win);
756 wnoutrefresh(sb.win);
757 wnoutrefresh(in.win);
758 wnoutrefresh(sep.win);
762 static void init_pair_or_die(short pair, short f, short b)
764 if (init_pair(pair, f, b) == ERR)
765 die(EXIT_FAILURE, "fatal: init_pair() failed\n");
768 static void init_colors_or_die(void)
773 die(EXIT_FAILURE, "fatal: No color term\n");
774 if (start_color() == ERR)
775 die(EXIT_FAILURE, "fatal: failed to start colors\n");
776 FOR_EACH_STATUS_ITEM(i)
777 if (theme.data[i].len)
778 init_pair_or_die(i + 1, theme.data[i].fg,
780 init_pair_or_die(COLOR_STATUSBAR, theme.sb_fg, theme.sb_bg);
781 init_pair_or_die(COLOR_COMMAND, theme.cmd_fg, theme.cmd_bg);
782 init_pair_or_die(COLOR_OUTPUT, theme.output_fg, theme.output_bg);
783 init_pair_or_die(COLOR_MSG, theme.msg_fg, theme.msg_bg);
784 init_pair_or_die(COLOR_ERRMSG, theme.err_msg_fg, theme.err_msg_bg);
785 init_pair_or_die(COLOR_SEPARATOR, theme.sep_fg, theme.sep_bg);
786 init_pair_or_die(COLOR_TOP, theme.default_fg, theme.default_bg);
787 init_pair_or_die(COLOR_BOT, theme.default_fg, theme.default_bg);
790 /* (Re-)initialize the curses library. */
791 static void init_curses(void)
795 if (top.win && refresh() == ERR) /* refresh is really needed */
796 die(EXIT_FAILURE, "refresh() failed\n");
797 if (LINES < theme.lines_min || COLS < theme.cols_min)
798 die(EXIT_FAILURE, "Terminal (%dx%d) too small"
799 " (need at least %dx%d)\n", COLS, LINES,
800 theme.cols_min, theme.lines_min);
801 curs_set(0); /* make cursor invisible, ignore errors */
802 nonl(); /* do not NL->CR/NL on output, always returns OK */
803 /* don't echo input */
805 die(EXIT_FAILURE, "fatal: noecho() failed\n");
806 /* take input chars one at a time, no wait for \n */
808 die(EXIT_FAILURE, "fatal: cbreak() failed\n");
809 init_colors_or_die();
810 clear(); /* ignore non-fatal errors */
811 init_wins(theme.top_lines_default);
812 // noecho(); /* don't echo input */
815 static void check_sigchld(void)
821 ret = para_reap_child(&pid);
826 goto reap_next_child;
830 * This sucker modifies its first argument. *handler and *arg are
831 * pointers to 0-terminated strings (inside line). Crap.
833 static int split_key_map(char *line, char **handler, char **arg)
835 if (!(*handler = strchr(line + 1, ':')))
839 if (!(*arg = strchr(*handler, ':')))
848 static void check_key_map_args_or_die(void)
853 for (i = 0; i < conf.key_map_given; ++i) {
857 tmp = para_strdup(conf.key_map_arg[i]);
858 if (!split_key_map(tmp, &handler, &arg))
860 if (strlen(handler) != 1)
862 if (*handler != 'x' && *handler != 'd' && *handler != 'i'
867 if (find_cmd_byname(arg) < 0)
870 if (i != conf.key_map_given)
871 die(EXIT_FAILURE, "invalid key map: %s\n", conf.key_map_arg[i]);
876 * React to various signal-related events
878 static void handle_signal(int sig)
882 die(EXIT_FAILURE, "only the good die young (caught SIGTERM)\n");
885 if (curses_active()) {
892 PARA_WARNING_LOG("caught SIGINT, reset\n");
893 /* Nothing to do. SIGINT killed our child which gets noticed
894 * by do_select and resets everything.
898 PARA_NOTICE_LOG("got SIGUSR1, rereading configuration\n");
907 static void status_pre_select(fd_set *rfds, int *max_fileno, struct timeval *tv)
909 static struct timeval next_exec, atm, diff;
910 int ret, fds[3] = {0, 1, 0};
915 /* Avoid busy loop */
916 gettimeofday(&atm, NULL);
917 if (tv_diff(&next_exec, &atm, &diff) > 0) {
918 if (tv_diff(&diff, tv, NULL) < 0)
922 next_exec.tv_sec = atm.tv_sec + 2;
923 ret = para_exec_cmdline_pid(&pid, conf.stat_cmd_arg, fds);
926 ret = mark_fd_nonblocking(fds[1]);
933 para_fd_set(stat_pipe, rfds, max_fileno);
936 #define COMMAND_BUF_SIZE 32768
939 * This is the core select loop. Besides the (internal) signal
940 * pipe, the following other fds are checked according to the mode:
942 * GETCH_MODE: check stdin, return when key is pressed
944 * COMMAND_MODE: check command fds and stdin. Return when peer has closed both
945 * stdout and stderr or when any key is pressed.
947 * EXTERNAL_MODE: Check only signal pipe. Used when an external command
948 * is running. During that time curses is disabled. Returns when
951 static int do_select(int mode)
954 int ret, i, max_fileno;
955 char command_buf[2][COMMAND_BUF_SIZE] = {"", ""};
956 int cbo[2] = {0, 0}; /* command buf offsets */
958 unsigned flags[2] = {0, 0}; /* for for_each_line() */
961 tv.tv_sec = conf.timeout_arg / 1000;
962 tv.tv_usec = (conf.timeout_arg % 1000) * 1000;
963 // ret = refresh_status();
966 status_pre_select(&rfds, &max_fileno, &tv);
968 para_fd_set(signal_pipe, &rfds, &max_fileno);
969 /* command pipe only for COMMAND_MODE */
970 if (mode == COMMAND_MODE) {
971 if (command_fds[0] >= 0)
972 para_fd_set(command_fds[0], &rfds, &max_fileno);
973 if (command_fds[1] >= 0)
974 para_fd_set(command_fds[1], &rfds, &max_fileno);
976 if (mode == GETCH_MODE || mode == COMMAND_MODE)
977 para_fd_set(STDIN_FILENO, &rfds, &max_fileno);
978 ret = para_select(max_fileno + 1, &rfds, NULL, &tv);
980 goto check_return; /* skip fd checks */
982 ret = para_next_signal(&rfds);
985 /* read command pipe if ready */
986 if (mode == COMMAND_MODE) {
987 for (i = 0; i < 2; i++) {
989 if (command_fds[i] < 0)
991 ret = read_nonblock(command_fds[i],
992 command_buf[i] + cbo[i],
993 COMMAND_BUF_SIZE - 1 - cbo[i], &rfds, &sz);
996 cbo[i] = for_each_line(flags[i], command_buf[i], cbo[i],
997 add_output_line, &i);
998 if (sz != cbo[i]) { /* at least one line found */
1003 PARA_NOTICE_LOG("closing command fd %d: %s\n",
1004 i, para_strerror(-ret));
1005 close(command_fds[i]);
1006 command_fds[i] = -1;
1009 if (command_fds[!i] < 0) /* both fds closed */
1012 if (cbo[i] == COMMAND_BUF_SIZE - 1) {
1013 PARA_NOTICE_LOG("discarding overlong line\n");
1015 flags[i] = FELF_DISCARD_FIRST;
1019 ret = read_stat_pipe(&rfds);
1021 PARA_NOTICE_LOG("closing stat pipe: %s\n", para_strerror(-ret));
1025 free(stat_content[SI_BASENAME]);
1026 stat_content[SI_BASENAME] =
1027 para_strdup("stat command terminated!?");
1033 ret = wgetch(top.win);
1034 if (ret != ERR && ret != KEY_RESIZE) {
1036 kill(cmd_pid, SIGTERM);
1041 ret = wgetch(top.win);
1042 if (ret != ERR && ret != KEY_RESIZE)
1052 /* read from command pipe and print data to bot window */
1053 static void exec_and_display_cmd(const char *cmd)
1055 int ret, fds[3] = {0, 1, 1};
1057 outputf(COLOR_COMMAND, "%s", cmd);
1058 ret = para_exec_cmdline_pid(&cmd_pid, cmd, fds);
1061 ret = mark_fd_nonblocking(fds[1]);
1064 ret = mark_fd_nonblocking(fds[2]);
1067 command_fds[0] = fds[1];
1068 command_fds[1] = fds[2];
1069 if (do_select(COMMAND_MODE) >= 0)
1070 PARA_INFO_LOG("command complete\n");
1072 PARA_NOTICE_LOG("command aborted\n");
1073 print_in_bar(COLOR_MSG, " ");
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 if (para_exec_cmdline_pid(&cmd_pid, cmd, fds) < 0)
1109 do_select(EXTERNAL_MODE);
1113 static void print_scroll_msg(void)
1115 unsigned lines_total, filled = ringbuffer_filled(bot_win_rb);
1116 int first_rbe = first_visible_rbe(&lines_total);
1118 print_in_bar(COLOR_MSG, "scrolled view: %d-%d/%d\n", filled - first_rbe,
1119 filled - scroll_position, ringbuffer_filled(bot_win_rb));
1122 static void com_scroll_top(void)
1124 int i = RINGBUFFER_SIZE - 1, bot_lines = get_num_lines(&bot);
1127 while (i > 0 && !ringbuffer_get(bot_win_rb, i))
1129 /* i is oldest entry */
1130 for (; lines < bot_lines && i >= 0; i--) {
1131 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
1134 lines += NUM_LINES(rbe->len);
1137 if (lines > 0 && scroll_position != i) {
1138 scroll_position = i;
1143 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1146 static void com_cancel_scrolling(void)
1149 if (scroll_position == 0) {
1150 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1153 scroll_position = 0;
1157 static void com_page_down(void)
1160 int i = scroll_position, bot_lines = get_num_lines(&bot);
1162 while (lines < bot_lines && --i > 0) {
1163 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
1166 lines += NUM_LINES(rbe->len);
1169 scroll_position = i;
1174 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1177 static void com_page_up(void)
1180 int fvr = first_visible_rbe(&lines), bot_lines = get_num_lines(&bot);
1182 if (fvr < 0 || fvr + 1 >= ringbuffer_filled(bot_win_rb)) {
1183 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1186 scroll_position = fvr + 1;
1187 for (; scroll_position > 0; scroll_position--) {
1188 first_visible_rbe(&lines);
1189 if (lines == bot_lines)
1196 static void com_scroll_down(void)
1198 struct rb_entry *rbe;
1199 int rbe_lines, bot_lines = get_num_lines(&bot);
1201 if (!scroll_position) {
1202 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1206 rbe = ringbuffer_get(bot_win_rb, scroll_position);
1207 rbe_lines = NUM_LINES(rbe->len);
1208 wscrl(bot.win, rbe_lines);
1209 wmove(bot.win, bot_lines - rbe_lines, 0);
1210 wattron(bot.win, COLOR_PAIR(rbe->color));
1211 waddstr(bot.win, rbe->msg);
1216 static void com_scroll_up(void)
1218 struct rb_entry *rbe = NULL;
1220 int i, first_rbe, num_scroll;
1222 /* the entry that is going to vanish */
1223 rbe = ringbuffer_get(bot_win_rb, scroll_position);
1226 num_scroll = NUM_LINES(rbe->len);
1227 first_rbe = first_visible_rbe(&lines);
1228 if (first_rbe < 0 || (first_rbe == ringbuffer_filled(bot_win_rb) - 1))
1231 wscrl(bot.win, -num_scroll);
1232 i = draw_top_rbe(&lines);
1235 while (i > 0 && lines < num_scroll) {
1237 rbe = ringbuffer_get(bot_win_rb, --i);
1240 rbe_lines = NUM_LINES(rbe->len);
1242 wattron(bot.win, COLOR_PAIR(rbe->color));
1243 waddstr(bot.win, "\n");
1244 waddstr(bot.win, rbe->msg);
1253 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1256 static void com_ll_decr(void)
1258 if (loglevel <= LL_DEBUG) {
1259 print_in_bar(COLOR_ERRMSG,
1260 "loglevel already at maximal verbosity\n");
1264 print_in_bar(COLOR_MSG, "loglevel set to %d\n", loglevel);
1267 static void com_ll_incr(void)
1269 if (loglevel >= LL_EMERG) {
1270 print_in_bar(COLOR_ERRMSG,
1271 "loglevel already at minimal verbosity\n");
1275 print_in_bar(COLOR_MSG, "loglevel set to %d\n", loglevel);
1279 * reread configuration, terminate on errors
1281 static void com_reread_conf(void)
1283 char *cf = configfile_exists();
1284 struct gui_cmdline_parser_params params = {
1287 .check_required = 0,
1288 .check_ambiguity = 0,
1293 PARA_WARNING_LOG("there is no configuration to read\n");
1296 PARA_INFO_LOG("rereading command line options and config file\n");
1298 * Despite .print_errors is set to 0, gengetopt will print to stderr
1299 * anyway, and exit on errors. So we have to shutdown curses first.
1302 gui_cmdline_parser_config_file(cf, &conf, ¶ms);
1304 PARA_NOTICE_LOG("config file reloaded\n");
1305 check_key_map_args_or_die();
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 static void handle_command(int c)
1401 /* first check user-defined key bindings */
1402 for (i = 0; i < conf.key_map_given; ++i) {
1403 char *tmp, *handler, *arg;
1405 tmp = para_strdup(conf.key_map_arg[i]);
1406 if (!split_key_map(tmp, &handler, &arg)) {
1410 if (strcmp(tmp, km_keyname(c))) {
1414 if (*handler == 'd')
1416 else if (*handler == 'x')
1418 else if (*handler == 'p')
1420 else if (*handler == 'i') {
1421 int num = find_cmd_byname(arg);
1423 command_list[num].handler();
1428 /* not found, check internal key bindings */
1429 for (i = 0; command_list[i].handler; i++) {
1430 if (!strcmp(km_keyname(c), command_list[i].key)) {
1431 command_list[i].handler();
1435 print_in_bar(COLOR_ERRMSG, "key '%s' is not bound, press ? for help",
1439 __noreturn static void print_help_and_die(void)
1441 struct ggo_help h = DEFINE_GGO_HELP(gui);
1442 bool d = conf.detailed_help_given;
1444 ggo_print_help(&h, d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS);
1448 int main(int argc, char *argv[])
1453 gui_cmdline_parser(argc, argv, &conf); /* exits on errors */
1454 loglevel = get_loglevel_by_name(conf.loglevel_arg);
1455 version_handle_flag("gui", conf.version_given);
1456 if (conf.help_given || conf.detailed_help_given)
1457 print_help_and_die();
1458 cf = configfile_exists();
1459 if (!cf && conf.config_file_given)
1460 die(EXIT_FAILURE, "can not read config file %s\n",
1461 conf.config_file_arg);
1463 struct gui_cmdline_parser_params params = {
1466 .check_required = 0,
1467 .check_ambiguity = 0,
1470 gui_cmdline_parser_config_file(cf, &conf, ¶ms);
1471 loglevel = get_loglevel_by_name(conf.loglevel_arg);
1473 check_key_map_args_or_die();
1474 theme_init(conf.theme_arg, &theme);
1475 setup_signal_handling();
1476 bot_win_rb = ringbuffer_new(RINGBUFFER_SIZE);
1477 setlocale(LC_CTYPE, "");
1478 initscr(); /* needed only once, always successful */
1482 ret = do_select(GETCH_MODE);
1485 print_in_bar(COLOR_MSG, " ");
1486 handle_command(ret);