2 * Copyright (C) 1998-2013 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>
14 #include "gui.cmdline.h"
18 #include "ringbuffer.h"
26 /** define the array of error lists needed by para_gui */
28 static char *stat_content[NUM_STAT_ITEMS];
30 #define STANDARD_STATUS_BAR "para_gui " PACKAGE_VERSION " (hit ? for help)"
32 static int signal_pipe;
34 static struct win_data {
40 } top, bot, sb, in, sep;
42 #define RINGBUFFER_SIZE 512
48 static struct ringbuffer *bot_win_rb;
49 #define NUM_LINES(len) (1 + (len) / bot.cols)
51 static unsigned scroll_position;
53 static int curses_active;
56 static int command_fds[2];
57 static int stat_pipe = -1;
58 static struct gui_args_info conf;
60 enum {GETCH_MODE, COMMAND_MODE, EXTERNAL_MODE};
63 #define COLOR_STATUSBAR 52
64 #define COLOR_COMMAND 53
65 #define COLOR_OUTPUT 54
67 #define COLOR_ERRMSG 56
68 #define COLOR_WELCOME 57
69 #define COLOR_SEPARATOR 58
76 const char *description;
77 void (*handler)(void);
83 char postfix[MAXLINE];
89 char content[MAXLINE];
92 static struct gui_theme theme;
97 static void com_help(void);
98 static void com_reread_conf(void);
99 static void com_enlarge_top_win(void);
100 static void com_shrink_top_win(void);
101 static void com_version(void);
102 __noreturn static void com_quit(void);
103 static void com_refresh(void);
104 static void com_ll_incr(void);
105 static void com_ll_decr(void);
106 static void com_prev_theme(void);
107 static void com_next_theme(void);
108 static void com_scroll_up(void);
109 static void com_scroll_down(void);
110 static void com_page_up(void);
111 static void com_page_down(void);
112 static void com_cancel_scrolling(void);
113 static void com_scroll_top(void);
115 static struct gui_command command_list[] = {
119 .description = "print help",
123 .name = "enlarge_win",
124 .description = "enlarge the top window",
125 .handler = com_enlarge_top_win
128 .name = "shrink_win",
129 .description = "shrink the top window",
130 .handler = com_shrink_top_win
133 .name = "reread_conf",
134 .description = "reread configuration file",
135 .handler = com_reread_conf
139 .description = "exit para_gui",
144 .description = "redraw the screen",
145 .handler = com_refresh
148 .name = "next_theme",
149 .description = "switch to next theme",
150 .handler = com_next_theme
153 .name = "prev_theme",
154 .description = "switch to previous stream",
155 .handler = com_prev_theme
159 .description = "increase loglevel (decreases verbosity)",
160 .handler = com_ll_incr
164 .description = "decrease loglevel (increases verbosity)",
165 .handler = com_ll_decr
169 .description = "show the para_gui version",
170 .handler = com_version
174 .description = "scroll up one line",
175 .handler = com_scroll_up
178 .name = "scroll_down",
179 .description = "scroll down one line",
180 .handler = com_scroll_down
184 .description = "scroll up one page",
185 .handler = com_page_up
189 .description = "scroll down one page",
190 .handler = com_page_down
193 .name = "scroll_top",
194 .description = "scroll to top of buffer",
195 .handler = com_scroll_top
198 .name = "cancel_scroll",
199 .description = "deactivate scroll mode",
200 .handler = com_cancel_scrolling
206 static int find_cmd_byname(char *name)
210 for (i = 0; command_list[i].handler; i++)
211 if (!strcmp(command_list[i].name, name))
216 /* taken from mutt */
217 static char *km_keyname(int c)
222 sprintf(buf, "<up>");
226 sprintf(buf, "<down>");
230 sprintf(buf, "<left>");
233 if (c == KEY_RIGHT) {
234 sprintf(buf, "<right>");
237 if (c == KEY_NPAGE) {
238 sprintf(buf, "<npage>");
241 if (c == KEY_PPAGE) {
242 sprintf(buf, "<ppage>");
246 sprintf(buf, "<home>");
250 sprintf(buf, "<end>");
253 if (c < 256 && c > -128 && iscntrl((unsigned char) c)) {
258 buf[1] = (c + '@') & 0x7f;
261 snprintf(buf, sizeof(buf), "\\%d%d%d", c >> 6,
262 (c >> 3) & 7, c & 7);
263 } else if (c >= KEY_F0 && c < KEY_F(256))
264 sprintf(buf, "<F%d>", c - KEY_F0);
266 snprintf(buf, sizeof(buf), "%c", (unsigned char) c);
268 snprintf(buf, sizeof(buf), "\\x%hx", (unsigned short) c);
272 static char *configfile_exists(void)
274 static char *config_file;
277 if (!conf.config_file_given) {
279 char *home = para_homedir();
280 config_file = make_message("%s/.paraslash/gui.conf",
286 tmp = conf.config_file_arg;
287 return file_exists(tmp)? tmp: NULL;
291 * print num spaces to curses window
293 static void add_spaces(WINDOW* win, unsigned int num)
302 * print aligned string to curses window. This function always prints
305 static int align_str(WINDOW* win, char *str, unsigned int len,
308 int i, num; /* of spaces */
312 num = len - strlen(str);
317 /* replace control characters by spaces */
318 for (i = 0; i < len && str[i]; i++) {
319 if (str[i] == '\n' || str[i] == '\r' || str[i] == '\f')
324 add_spaces(win, num);
325 } else if (align == RIGHT) {
326 add_spaces(win, num);
329 add_spaces(win, num / 2);
330 waddstr(win, str[0]? str: "");
331 add_spaces(win, num - num / 2);
336 __printf_2_3 static void print_in_bar(int color, const char *fmt,...)
343 wattron(in.win, COLOR_PAIR(color));
345 xvasprintf(&msg, fmt, ap);
348 align_str(in.win, msg, sb.cols, LEFT);
354 * update the status bar
356 static void print_status_bar(void)
362 tmp = para_strdup(STANDARD_STATUS_BAR);
364 align_str(sb.win, tmp, sb.cols, CENTER);
370 * get the number of the oldest rbe that is (partially) visible. On return,
371 * lines contains the sum of the number of lines of all visable entries. If the
372 * first one is only partially visible, lines is greater than bot.lines.
374 static int first_visible_rbe(unsigned *lines)
378 for (i = scroll_position; i < RINGBUFFER_SIZE; i++) {
379 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
383 // fprintf(stderr, "found: %s\n", rbe->msg);
384 rbe_lines = NUM_LINES(rbe->len);
385 if (rbe_lines > bot.lines)
388 if (*lines >= bot.lines)
391 return RINGBUFFER_SIZE - 1;
394 static int draw_top_rbe(unsigned *lines)
397 int offset, fvr = first_visible_rbe(lines);
398 struct rb_entry *rbe;
402 wmove(bot.win, 0, 0);
403 rbe = ringbuffer_get(bot_win_rb, fvr);
406 len = strlen(rbe->msg);
407 if (*lines > bot.lines) {
408 /* first rbe is only partially visible */
409 offset = (*lines - bot.lines) * bot.cols;
410 assert(offset <= len);
413 wattron(bot.win, COLOR_PAIR(rbe->color));
414 waddstr(bot.win, rbe->msg + offset);
415 *lines = NUM_LINES(len - offset);
419 static void redraw_bot_win(void)
424 wmove(bot.win, 0, 0);
426 i = draw_top_rbe(&lines);
429 while (i > 0 && lines < bot.lines) {
430 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, --i);
433 waddstr(bot.win, "\n");
436 lines += NUM_LINES(rbe->len);
437 wattron(bot.win, COLOR_PAIR(rbe->color));
438 waddstr(bot.win, "\n");
439 waddstr(bot.win, rbe->msg);
445 static void rb_add_entry(int color, char *msg)
447 struct rb_entry *old, *new = para_malloc(sizeof(struct rb_entry));
450 new->len = strlen(msg);
452 old = ringbuffer_add(bot_win_rb, new);
453 // fprintf(stderr, "added: %s\n", new->msg);
458 if (scroll_position) {
459 /* discard current scrolling, like xterm does */
464 wattron(bot.win, COLOR_PAIR(color));
465 getyx(bot.win, y, x);
467 waddstr(bot.win, "\n");
468 waddstr(bot.win, msg);
472 * print formated output to bot win and refresh
474 __printf_2_3 static void outputf(int color, const char* fmt,...)
482 xvasprintf(&msg, fmt, ap);
484 rb_add_entry(color, msg);
488 static int add_output_line(char *line, void *data)
490 int color = *(int *)data? COLOR_ERRMSG : COLOR_OUTPUT;
493 rb_add_entry(color, para_strdup(line));
499 __printf_2_3 void curses_log(int ll, const char *fmt,...)
505 if (ll < loglevel || !curses_active)
514 color = COLOR_ERRMSG;
517 xvasprintf(&msg, fmt, ap);
520 rb_add_entry(color, msg);
523 __printf_2_3 void (*para_log)(int, const char*, ...) = curses_log;
525 static void setup_signal_handling(void)
527 signal_pipe = para_signal_init();
528 para_install_sighandler(SIGINT);
529 para_install_sighandler(SIGTERM);
530 para_install_sighandler(SIGCHLD);
531 para_install_sighandler(SIGWINCH);
532 para_install_sighandler(SIGUSR1);
533 para_sigaction(SIGHUP, SIG_IGN);
536 /* kill every process in the process group and exit */
537 __noreturn static void kill_pg_and_die(int ret)
539 para_sigaction(SIGTERM, SIG_IGN);
544 static void shutdown_curses(void)
553 __noreturn static void finish(int ret)
556 kill_pg_and_die(ret);
560 * exit curses and print given message to stdout/stderr
562 __noreturn __printf_2_3 static void msg_n_exit(int ret, const char* fmt, ...)
565 FILE *outfd = ret? stderr: stdout;
569 vfprintf(outfd, fmt, argp);
571 kill_pg_and_die(ret);
574 static void print_welcome(void)
576 if (loglevel > LL_NOTICE)
578 outputf(COLOR_WELCOME, "Welcome to para_gui " PACKAGE_VERSION
579 " \"" CODENAME "\". Theme: %s", theme.name);
586 static void init_wins(int top_lines)
590 top.lines = top_lines;
595 bot.lines = LINES - top.lines - 3;
597 bot.begy = top.lines + 1;
612 sep.begy = top.lines;
615 assume_default_colors(theme.default_fg, theme.default_bg);
617 mvwin(top.win, top.begy, top.begx);
618 wresize(top.win, top.lines, top.cols);
620 mvwin(sb.win, sb.begy, sb.begx);
621 wresize(sb.win, sb.lines, sb.cols);
623 mvwin(sep.win, sep.begy, sep.begx);
624 wresize(sep.win, sep.lines, sep.cols);
626 mvwin(bot.win, bot.begy, bot.begx);
627 wresize(bot.win, bot.lines, bot.cols);
629 mvwin(in.win, in.begy, in.begx);
630 wresize(in.win, in.lines, in.cols);
632 sep.win = newwin(sep.lines, sep.cols, sep.begy, sep.begx);
633 top.win = newwin(top.lines, top.cols, top.begy, top.begx);
634 bot.win = newwin(bot.lines, bot.cols, bot.begy, bot.begx);
635 sb.win = newwin(sb.lines, sb.cols, sb.begy, sb.begx);
636 in.win = newwin(in.lines, in.cols, in.begy, in.begx);
637 if (!top.win || !bot.win || !sb.win || !in.win || !sep.win)
638 msg_n_exit(1, "Error: Cannot create curses windows\n");
642 scrollok(bot.win, 1);
643 wattron(sb.win, COLOR_PAIR(COLOR_STATUSBAR));
644 wattron(sep.win, COLOR_PAIR(COLOR_SEPARATOR));
645 wattron(bot.win, COLOR_PAIR(COLOR_BOT));
646 wattron(top.win, COLOR_PAIR(COLOR_TOP));
658 wmove(sep.win, 0, 0);
659 for (i = 1; i <= COLS; i++)
660 waddstr(sep.win, theme.sep_str);
663 wnoutrefresh(top.win);
664 wnoutrefresh(bot.win);
665 //wnoutrefresh(sb.win);
667 wnoutrefresh(in.win);
668 wnoutrefresh(sep.win);
673 * Print stat item #i to curses window
675 static void print_stat_item(int i)
678 struct stat_item_data d = theme.data[i];
679 char *c = stat_content[i];
681 if (!curses_active || !d.len || !c)
683 tmp = make_message("%s%s%s", d.prefix, c, d.postfix);
684 wmove(top.win, d.y * top.lines / 100, d.x * COLS / 100);
686 wattron(top.win, COLOR_PAIR(i + 1));
687 align_str(top.win, tmp, d.len * COLS / 100, d.align);
692 static int update_item(int item_num, char *buf)
694 char **c = stat_content + item_num;
701 *c = para_strdup("(artist tag not set)");
704 *c = para_strdup("(title tag not set)");
707 *c = para_strdup("????");
710 *c = para_strdup("(album tag not set)");
713 *c = para_strdup("(comment tag not set)");
717 *c = para_strdup(buf);
719 print_stat_item(item_num);
723 static int read_stat_pipe(fd_set *rfds)
726 static int bufsize, loaded;
732 if (loaded >= bufsize) {
733 if (bufsize > 1000 * 1000) {
737 bufsize += bufsize + 1000;
738 buf = para_realloc(buf, bufsize);
740 assert(loaded < bufsize);
741 ret = read_nonblock(stat_pipe, buf + loaded, bufsize - loaded,
744 ret2 = for_each_stat_item(buf, loaded, update_item);
745 if (ret < 0 || ret2 < 0) {
747 return ret2 < 0? ret2 : ret;
749 sz = ret2; /* what is left */
750 if (sz > 0 && sz < loaded)
751 memmove(buf, buf + loaded - sz, sz);
756 static void print_all_items(void)
762 FOR_EACH_STATUS_ITEM(i)
766 static void clear_all_items(void)
770 FOR_EACH_STATUS_ITEM(i) {
771 free(stat_content[i]);
772 stat_content[i] = para_strdup("");
776 static void init_pair_or_die(short pair, short f, short b)
778 if (init_pair(pair, f, b) == ERR)
779 msg_n_exit(EXIT_FAILURE, "fatal: init_pair() failed\n");
782 static void init_colors_or_die(void)
787 msg_n_exit(EXIT_FAILURE, "fatal: No color term\n");
788 if (start_color() == ERR)
789 msg_n_exit(EXIT_FAILURE, "fatal: failed to start colors\n");
790 FOR_EACH_STATUS_ITEM(i)
791 if (theme.data[i].len)
792 init_pair_or_die(i + 1, theme.data[i].fg,
794 init_pair_or_die(COLOR_STATUSBAR, theme.sb_fg, theme.sb_bg);
795 init_pair_or_die(COLOR_COMMAND, theme.cmd_fg, theme.cmd_bg);
796 init_pair_or_die(COLOR_OUTPUT, theme.output_fg, theme.output_bg);
797 init_pair_or_die(COLOR_MSG, theme.msg_fg, theme.msg_bg);
798 init_pair_or_die(COLOR_ERRMSG, theme.err_msg_fg, theme.err_msg_bg);
799 init_pair_or_die(COLOR_WELCOME, theme.welcome_fg, theme.welcome_bg);
800 init_pair_or_die(COLOR_SEPARATOR, theme.sep_fg, theme.sep_bg);
801 init_pair_or_die(COLOR_TOP, theme.default_fg, theme.default_bg);
802 init_pair_or_die(COLOR_BOT, theme.default_fg, theme.default_bg);
805 /* (Re-)initialize the curses library. */
806 static void init_curses(void)
809 if (top.win && refresh() == ERR) /* refesh is really needed */
810 msg_n_exit(EXIT_FAILURE, "refresh() failed\n");
811 if (LINES < theme.lines_min || COLS < theme.cols_min)
812 msg_n_exit(EXIT_FAILURE, "Error: Terminal (%dx%d) too small"
813 " (need at least %dx%d)\n", COLS, LINES,
814 theme.cols_min, theme.lines_min);
815 curs_set(0); /* make cursor invisible, ignore errors */
816 nonl(); /* do not NL->CR/NL on output, always returns OK */
817 /* don't echo input */
819 msg_n_exit(EXIT_FAILURE, "fatal: noecho() failed\n");
820 /* take input chars one at a time, no wait for \n */
822 msg_n_exit(EXIT_FAILURE, "fatal: cbreak() failed\n");
823 init_colors_or_die();
824 clear(); /* ignore non-fatal errors */
825 init_wins(theme.top_lines_default);
827 // noecho(); /* don't echo input */
830 static void check_sigchld(void)
835 ret = para_reap_child(&pid);
840 goto reap_next_child;
844 * This sucker modifies its first argument. *handler and *arg are
845 * pointers to 0-terminated strings (inside line). Crap.
847 static int split_key_map(char *line, char **handler, char **arg)
849 if (!(*handler = strchr(line + 1, ':')))
853 if (!(*arg = strchr(*handler, ':')))
862 static int check_key_map_args(void)
866 char *tmp = NULL, *handler, *arg;
868 for (i = 0; i < conf.key_map_given; ++i) {
869 s = conf.key_map_arg[i];
873 tmp = para_strdup(s);
874 if (!split_key_map(tmp, &handler, &arg))
876 if (strlen(handler) != 1)
885 if (find_cmd_byname(arg) < 0)
895 * React to various signal-related events
897 static void handle_signal(int sig)
901 msg_n_exit(EXIT_FAILURE,
902 "only the good die young (caught SIGTERM))\n");
912 PARA_WARNING_LOG("caught SIGINT, reset");
913 /* Nothing to do. SIGINT killed our child which gets noticed
914 * by do_select and resets everything.
918 PARA_NOTICE_LOG("got SIGUSR1, rereading configuration");
927 static int open_stat_pipe(void)
930 int ret, fds[3] = {0, 1, 0};
937 * Sleep a bit to avoid a busy loop. As the call to sleep() may
938 * be interrupted by SIGCHLD, we simply wait until the call
943 ret = para_exec_cmdline_pid(&pid, conf.stat_cmd_arg, fds);
946 ret = mark_fd_nonblocking(fds[1]);
953 #define COMMAND_BUF_SIZE 32768
956 * This is the core select loop. Besides the (internal) signal
957 * pipe, the following other fds are checked according to the mode:
959 * GETCH_MODE: check stdin, return when key is pressed
961 * COMMAND_MODE: check command fds and stdin. Return when peer has closed both
962 * stdout and stderr or when any key is pressed.
964 * EXTERNAL_MODE: Check only signal pipe. Used when an external command
965 * is running. During that time curses is disabled. Returns when
968 static int do_select(int mode)
971 int ret, i, max_fileno;
972 char command_buf[2][COMMAND_BUF_SIZE] = {"", ""};
973 int cbo[2] = {0, 0}; /* command buf offsets */
977 tv.tv_sec = conf.timeout_arg / 1000;
978 tv.tv_usec = (conf.timeout_arg % 1000) * 1000;
979 // ret = refresh_status();
983 stat_pipe = open_stat_pipe();
985 para_fd_set(stat_pipe, &rfds, &max_fileno);
987 para_fd_set(signal_pipe, &rfds, &max_fileno);
988 /* command pipe only for COMMAND_MODE */
989 if (mode == COMMAND_MODE) {
990 if (command_fds[0] >= 0)
991 para_fd_set(command_fds[0], &rfds, &max_fileno);
992 if (command_fds[1] >= 0)
993 para_fd_set(command_fds[1], &rfds, &max_fileno);
995 if (mode == GETCH_MODE || mode == COMMAND_MODE)
996 para_fd_set(STDIN_FILENO, &rfds, &max_fileno);
997 ret = para_select(max_fileno + 1, &rfds, NULL, &tv);
999 goto check_return; /* skip fd checks */
1001 ret = para_next_signal(&rfds);
1004 /* read command pipe if ready */
1005 if (mode == COMMAND_MODE) {
1006 for (i = 0; i < 2; i++) {
1008 if (command_fds[i] < 0)
1010 ret = read_nonblock(command_fds[i],
1011 command_buf[i] + cbo[i],
1012 COMMAND_BUF_SIZE - 1 - cbo[i], &rfds, &sz);
1015 cbo[i] = for_each_line(command_buf[i], cbo[i],
1016 add_output_line, &i);
1020 PARA_NOTICE_LOG("closing command fd %d: %s",
1021 i, para_strerror(-ret));
1022 close(command_fds[i]);
1023 command_fds[i] = -1;
1024 if (command_fds[!i] < 0) /* both fds closed */
1027 if (cbo[i] == COMMAND_BUF_SIZE - 1) {
1028 PARA_NOTICE_LOG("discarding overlong line");
1033 ret = read_stat_pipe(&rfds);
1035 PARA_NOTICE_LOG("closing stat pipe: %s\n", para_strerror(-ret));
1039 free(stat_content[SI_BASENAME]);
1040 stat_content[SI_BASENAME] =
1041 para_strdup("stat command terminated!?");
1047 ret = wgetch(top.win);
1048 if (ret != ERR && ret != KEY_RESIZE) {
1049 if (command_fds[0] >= 0) {
1050 close(command_fds[0]);
1051 command_fds[0] = -1;
1053 if (command_fds[1] >= 0) {
1054 close(command_fds[1]);
1055 command_fds[1] = -1;
1058 kill(cmd_pid, SIGTERM);
1063 ret = wgetch(top.win);
1064 if (ret != ERR && ret != KEY_RESIZE)
1075 * read from command pipe and print data to bot window
1077 static void send_output(void)
1081 ret = mark_fd_nonblocking(command_fds[0]);
1084 ret = mark_fd_nonblocking(command_fds[1]);
1087 if (do_select(COMMAND_MODE) >= 0)
1088 PARA_INFO_LOG("command complete");
1090 PARA_NOTICE_LOG("command aborted");
1091 print_in_bar(COLOR_MSG, " ");
1094 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
1095 close(command_fds[0]);
1096 close(command_fds[1]);
1099 static void para_cmd(char *cmd)
1101 int ret, fds[3] = {0, 1, 1};
1102 char *c = make_message(BINDIR "/para_client -- %s", cmd);
1104 outputf(COLOR_COMMAND, "%s", c);
1105 print_in_bar(COLOR_MSG, "executing client command, hit any key to abort\n");
1106 ret = para_exec_cmdline_pid(&cmd_pid, c, fds);
1110 command_fds[0] = fds[1];
1111 command_fds[1] = fds[2];
1116 * exec command and print output to bot win
1118 static void display_cmd(char *cmd)
1120 int fds[3] = {0, 1, 1};
1122 print_in_bar(COLOR_MSG, "executing display command, hit any key to abort");
1123 outputf(COLOR_COMMAND, "%s", cmd);
1124 if (para_exec_cmdline_pid(&cmd_pid, cmd, fds) < 0)
1126 command_fds[0] = fds[1];
1127 command_fds[1] = fds[2];
1132 * shutdown curses and stat pipe before executing external commands
1134 static void external_cmd(char *cmd)
1136 int fds[3] = {-1, -1, -1};
1141 if (para_exec_cmdline_pid(&cmd_pid, cmd, fds) < 0)
1143 do_select(EXTERNAL_MODE);
1147 static void print_scroll_msg(void)
1149 unsigned lines_total, filled = ringbuffer_filled(bot_win_rb);
1150 int first_rbe = first_visible_rbe(&lines_total);
1151 print_in_bar(COLOR_MSG, "scrolled view: %d-%d/%d\n", filled - first_rbe,
1152 filled - scroll_position, ringbuffer_filled(bot_win_rb));
1155 static void com_scroll_top(void)
1157 int i = RINGBUFFER_SIZE - 1;
1160 while (i > 0 && !ringbuffer_get(bot_win_rb, i))
1162 /* i is oldest entry */
1163 for (; lines < bot.lines && i >= 0; i--) {
1164 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
1167 lines += NUM_LINES(strlen(rbe->msg));
1170 if (lines > 0 && scroll_position != i) {
1171 scroll_position = i;
1176 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1179 static void com_cancel_scrolling(void)
1182 if (scroll_position == 0) {
1183 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1186 scroll_position = 0;
1190 static void com_page_down(void)
1193 int i = scroll_position;
1194 while (lines < bot.lines && --i > 0) {
1195 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
1198 lines += NUM_LINES(strlen(rbe->msg));
1201 scroll_position = i;
1206 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1209 static void com_page_up(void)
1212 int fvr = first_visible_rbe(&lines);
1214 if (fvr < 0 || fvr + 1 >= ringbuffer_filled(bot_win_rb)) {
1215 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1218 scroll_position = fvr + 1;
1219 for (; scroll_position > 0; scroll_position--) {
1220 first_visible_rbe(&lines);
1221 if (lines == bot.lines)
1228 static void com_scroll_down(void)
1230 struct rb_entry *rbe;
1233 if (!scroll_position) {
1234 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1238 rbe = ringbuffer_get(bot_win_rb, scroll_position);
1239 rbe_lines = NUM_LINES(rbe->len);
1240 wscrl(bot.win, rbe_lines);
1241 wmove(bot.win, bot.lines - rbe_lines, 0);
1242 wattron(bot.win, COLOR_PAIR(rbe->color));
1243 waddstr(bot.win, rbe->msg);
1248 static void com_scroll_up(void)
1250 struct rb_entry *rbe = NULL;
1252 int i, first_rbe, num_scroll;
1254 /* the entry that is going to vanish */
1255 rbe = ringbuffer_get(bot_win_rb, scroll_position);
1258 num_scroll = NUM_LINES(rbe->len);
1259 first_rbe = first_visible_rbe(&lines);
1260 if (first_rbe < 0 || (first_rbe == ringbuffer_filled(bot_win_rb) - 1))
1263 wscrl(bot.win, -num_scroll);
1264 i = draw_top_rbe(&lines);
1267 while (i > 0 && lines < num_scroll) {
1269 rbe = ringbuffer_get(bot_win_rb, --i);
1272 rbe_lines = NUM_LINES(rbe->len);
1274 // fprintf(stderr, "msg: %s\n", rbe->msg);
1275 wattron(bot.win, COLOR_PAIR(rbe->color));
1276 waddstr(bot.win, "\n");
1277 waddstr(bot.win, rbe->msg);
1286 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1289 static void com_ll_decr(void)
1291 if (loglevel <= LL_DEBUG) {
1292 print_in_bar(COLOR_ERRMSG,
1293 "loglevel already at maximal verbosity\n");
1297 print_in_bar(COLOR_MSG, "loglevel set to %d\n", loglevel);
1300 static void com_ll_incr(void)
1302 if (loglevel >= LL_EMERG) {
1303 print_in_bar(COLOR_ERRMSG,
1304 "loglevel already at minimal verbosity\n");
1308 print_in_bar(COLOR_MSG, "loglevel set to %d\n", loglevel);
1312 * reread configuration, terminate on errors
1314 static void com_reread_conf(void)
1316 char *cf =configfile_exists();
1317 struct gui_cmdline_parser_params params = {
1320 .check_required = 0,
1321 .check_ambiguity = 0,
1326 PARA_WARNING_LOG("there is no configuration to read");
1329 PARA_INFO_LOG("rereading command line options and config file");
1330 gui_cmdline_parser_ext(_argc, _argv, &conf, ¶ms);
1331 if (gui_cmdline_parser_config_file(cf, &conf, ¶ms) != 0) {
1332 PARA_EMERG_LOG("errors in config file");
1333 finish(EXIT_FAILURE);
1335 PARA_NOTICE_LOG("config file reloaded");
1336 if (check_key_map_args() < 0)
1337 finish(EXIT_FAILURE);
1340 static void com_help(void)
1344 for (i = 0; i < conf.key_map_given; ++i) {
1345 char *handler, *arg, *tmp = para_strdup(conf.key_map_arg[i]);
1346 const char *handler_text = "???", *desc = NULL;
1348 if (!split_key_map(tmp, &handler, &arg)) {
1354 handler_text = "internal";
1355 desc = command_list[find_cmd_byname(arg)].description;
1357 case 'x': handler_text = "external"; break;
1358 case 'd': handler_text = "display "; break;
1359 case 'p': handler_text = "para "; break;
1361 outputf(COLOR_MSG, "%s\t%s\t%s%s\t%s", tmp, handler_text, arg,
1362 strlen(arg) < 8? "\t" : "",
1366 for (i = 0; command_list[i].handler; i++) {
1367 struct gui_command gc = command_list[i];
1369 outputf(COLOR_MSG, "%s\tinternal\t%s\t%s%s", gc.key, gc.name,
1370 strlen(gc.name) < 8? "\t" : "",
1373 print_in_bar(COLOR_MSG, "try \"para_gui -h\" or \"para_client help\" "
1377 static void com_shrink_top_win(void)
1379 if (top.lines <= theme.top_lines_min) {
1380 PARA_WARNING_LOG("can not decrease top window");
1383 init_wins(top.lines - 1);
1386 print_in_bar(COLOR_MSG, "%s", "decreased top window");
1389 static void com_enlarge_top_win(void)
1391 if (bot.lines < 3) {
1392 PARA_WARNING_LOG("can not increase top window");
1395 init_wins(top.lines + 1);
1398 print_in_bar(COLOR_MSG, "increased top window");
1401 static void com_version(void)
1403 print_in_bar(COLOR_MSG, "para_gui " PACKAGE_VERSION " \""
1407 __noreturn static void com_quit(void)
1412 static void com_refresh(void)
1418 static void change_theme(int next)
1424 /* This seems to be needed twice, why? */
1427 PARA_NOTICE_LOG("new theme: %s", theme.name);
1430 static void com_next_theme(void)
1435 static void com_prev_theme(void)
1441 static void handle_command(int c)
1445 /* first check user's key bindings */
1446 for (i = 0; i < conf.key_map_given; ++i) {
1447 char *tmp, *handler, *arg;
1449 tmp = para_strdup(conf.key_map_arg[i]);
1450 if (!split_key_map(tmp, &handler, &arg)) {
1454 if (strcmp(tmp, km_keyname(c))) {
1458 if (*handler == 'd')
1460 else if (*handler == 'x')
1462 else if (*handler == 'p')
1464 else if (*handler == 'i') {
1465 int num = find_cmd_byname(arg);
1467 command_list[num].handler();
1472 /* not found, check internal key bindings */
1473 for (i = 0; command_list[i].handler; i++) {
1474 if (!strcmp(km_keyname(c), command_list[i].key)) {
1475 command_list[i].handler();
1479 print_in_bar(COLOR_ERRMSG, "key '%s' is not bound, press ? for help",
1483 int main(int argc, char *argv[])
1491 gui_cmdline_parser(argc, argv, &conf); /* exits on errors */
1492 HANDLE_VERSION_FLAG("gui", conf);
1493 cf = configfile_exists();
1494 if (!cf && conf.config_file_given) {
1495 fprintf(stderr, "can not read config file %s\n",
1496 conf.config_file_arg);
1500 struct gui_cmdline_parser_params params = {
1503 .check_required = 0,
1504 .check_ambiguity = 0,
1507 if (gui_cmdline_parser_config_file(cf, &conf, ¶ms) != 0)
1510 loglevel = get_loglevel_by_name(conf.loglevel_arg);
1511 if (check_key_map_args() < 0) {
1512 fprintf(stderr, "invalid key map\n");
1515 init_theme_or_die(conf.theme_arg, &theme);
1516 top.lines = theme.top_lines_default;
1517 setup_signal_handling();
1518 bot_win_rb = ringbuffer_new(RINGBUFFER_SIZE);
1519 initscr(); /* needed only once, always successful */
1524 ret = do_select(GETCH_MODE);
1527 print_in_bar(COLOR_MSG, " ");
1528 handle_command(ret);