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 #define STANDARD_STATUS_BAR "para_gui " PACKAGE_VERSION " (hit ? for help)"
35 static int signal_pipe;
37 static struct gui_window {
43 } top, bot, sb, in, sep;
45 #define RINGBUFFER_SIZE 512
51 static struct ringbuffer *bot_win_rb;
52 #define NUM_LINES(len) (1 + (len) / bot.cols)
54 static unsigned scroll_position;
56 static bool curses_active;
59 static int command_fds[2] = {-1, -1};
60 static int stat_pipe = -1;
61 static struct gui_args_info conf;
63 enum {GETCH_MODE, COMMAND_MODE, EXTERNAL_MODE};
67 * Codes for various colors.
69 * Each status item has its own color pair. The ones defined here start at a
70 * higher number so that they do not overlap with these.
73 COLOR_STATUSBAR = NUM_STAT_ITEMS + 1,
87 const char *description;
88 void (*handler)(void);
91 static struct gui_theme theme;
96 static void com_help(void);
97 static void com_reread_conf(void);
98 static void com_enlarge_top_win(void);
99 static void com_shrink_top_win(void);
100 static void com_version(void);
101 __noreturn static void com_quit(void);
102 static void com_refresh(void);
103 static void com_ll_incr(void);
104 static void com_ll_decr(void);
105 static void com_prev_theme(void);
106 static void com_next_theme(void);
107 static void com_scroll_up(void);
108 static void com_scroll_down(void);
109 static void com_page_up(void);
110 static void com_page_down(void);
111 static void com_cancel_scrolling(void);
112 static void com_scroll_top(void);
114 static struct gui_command command_list[] = {
118 .description = "print help",
122 .name = "enlarge_win",
123 .description = "enlarge the top window",
124 .handler = com_enlarge_top_win
127 .name = "shrink_win",
128 .description = "shrink the top window",
129 .handler = com_shrink_top_win
132 .name = "reread_conf",
133 .description = "reread configuration file",
134 .handler = com_reread_conf
138 .description = "exit para_gui",
143 .description = "redraw the screen",
144 .handler = com_refresh
147 .name = "next_theme",
148 .description = "switch to next theme",
149 .handler = com_next_theme
152 .name = "prev_theme",
153 .description = "switch to previous stream",
154 .handler = com_prev_theme
158 .description = "increase loglevel (decreases verbosity)",
159 .handler = com_ll_incr
163 .description = "decrease loglevel (increases verbosity)",
164 .handler = com_ll_decr
168 .description = "show the para_gui version",
169 .handler = com_version
173 .description = "scroll up one line",
174 .handler = com_scroll_up
177 .name = "scroll_down",
178 .description = "scroll down one line",
179 .handler = com_scroll_down
183 .description = "scroll up one page",
184 .handler = com_page_up
188 .description = "scroll down one page",
189 .handler = com_page_down
192 .name = "scroll_top",
193 .description = "scroll to top of buffer",
194 .handler = com_scroll_top
197 .name = "cancel_scroll",
198 .description = "deactivate scroll mode",
199 .handler = com_cancel_scrolling
205 static int find_cmd_byname(char *name)
209 for (i = 0; command_list[i].handler; i++)
210 if (!strcmp(command_list[i].name, name))
215 /* taken from mutt */
216 static char *km_keyname(int c)
221 sprintf(buf, "<up>");
225 sprintf(buf, "<down>");
229 sprintf(buf, "<left>");
232 if (c == KEY_RIGHT) {
233 sprintf(buf, "<right>");
236 if (c == KEY_NPAGE) {
237 sprintf(buf, "<npage>");
240 if (c == KEY_PPAGE) {
241 sprintf(buf, "<ppage>");
245 sprintf(buf, "<home>");
249 sprintf(buf, "<end>");
252 if (c < 256 && c > -128 && iscntrl((unsigned char) c)) {
257 buf[1] = (c + '@') & 0x7f;
260 snprintf(buf, sizeof(buf), "\\%d%d%d", c >> 6,
261 (c >> 3) & 7, c & 7);
262 } else if (c >= KEY_F0 && c < KEY_F(256))
263 sprintf(buf, "<F%d>", c - KEY_F0);
265 snprintf(buf, sizeof(buf), "%c", (unsigned char) c);
267 snprintf(buf, sizeof(buf), "\\x%hx", (unsigned short) c);
271 static char *configfile_exists(void)
273 static char *config_file;
276 if (!conf.config_file_given) {
278 char *home = para_homedir();
279 config_file = make_message("%s/.paraslash/gui.conf",
285 tmp = conf.config_file_arg;
286 return file_exists(tmp)? tmp: NULL;
289 /* Print given number of spaces to curses window. */
290 static void add_spaces(WINDOW* win, unsigned int num)
293 unsigned sz = sizeof(space) - 1; /* number of spaces */
307 * print aligned string to curses window. This function always prints
310 static int align_str(WINDOW* win, char *str, unsigned int len,
313 int ret, i, num; /* of spaces */
318 ret = strwidth(str, &width);
320 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
329 /* replace control characters by spaces */
330 for (i = 0; i < len && str[i]; i++) {
331 if (str[i] == '\n' || str[i] == '\r' || str[i] == '\f')
336 add_spaces(win, num);
337 } else if (align == RIGHT) {
338 add_spaces(win, num);
341 add_spaces(win, num / 2);
342 waddstr(win, str[0]? str: "");
343 add_spaces(win, num - num / 2);
348 __printf_2_3 static void print_in_bar(int color, const char *fmt,...)
355 wattron(in.win, COLOR_PAIR(color));
357 xvasprintf(&msg, fmt, ap);
360 align_str(in.win, msg, in.cols, LEFT);
366 * update the status bar
368 static void print_status_bar(void)
374 tmp = para_strdup(STANDARD_STATUS_BAR);
376 align_str(sb.win, tmp, sb.cols, CENTER);
382 * get the number of the oldest rbe that is (partially) visible. On return,
383 * lines contains the sum of the number of lines of all visible entries. If the
384 * first one is only partially visible, lines is greater than bot.lines.
386 static int first_visible_rbe(unsigned *lines)
390 for (i = scroll_position; i < RINGBUFFER_SIZE; i++) {
391 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
395 // fprintf(stderr, "found: %s\n", rbe->msg);
396 rbe_lines = NUM_LINES(rbe->len);
397 if (rbe_lines > bot.lines)
400 if (*lines >= bot.lines)
403 return RINGBUFFER_SIZE - 1;
407 returns number of first visible rbe, *lines is the number of lines drawn.
409 static int draw_top_rbe(unsigned *lines)
411 int ret, fvr = first_visible_rbe(lines);
412 struct rb_entry *rbe;
413 size_t bytes_to_skip, cells_to_skip, width;
417 wmove(bot.win, 0, 0);
418 rbe = ringbuffer_get(bot_win_rb, fvr);
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)
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);
479 // fprintf(stderr, "added: %s\n", new->msg);
484 if (scroll_position) {
485 /* discard current scrolling, like xterm does */
490 wattron(bot.win, COLOR_PAIR(color));
491 getyx(bot.win, y, x);
493 waddstr(bot.win, "\n");
494 waddstr(bot.win, msg);
498 * print formated output to bot win and refresh
500 __printf_2_3 static void outputf(int color, const char* fmt,...)
508 xvasprintf(&msg, fmt, ap);
510 rb_add_entry(color, msg);
514 static int add_output_line(char *line, void *data)
516 int color = *(int *)data? COLOR_ERRMSG : COLOR_OUTPUT;
520 rb_add_entry(color, para_strdup(line));
526 static __printf_2_3 void curses_log(int ll, const char *fmt,...)
532 if (ll < loglevel || !curses_active)
541 color = COLOR_ERRMSG;
544 xvasprintf(&msg, fmt, ap);
547 rb_add_entry(color, msg);
550 __printf_2_3 void (*para_log)(int, const char*, ...) = curses_log;
552 static void setup_signal_handling(void)
554 signal_pipe = para_signal_init();
555 para_install_sighandler(SIGINT);
556 para_install_sighandler(SIGTERM);
557 para_install_sighandler(SIGCHLD);
558 para_install_sighandler(SIGWINCH);
559 para_install_sighandler(SIGUSR1);
562 /* kill every process in the process group and exit */
563 __noreturn static void kill_pg_and_die(int ret)
565 para_sigaction(SIGTERM, SIG_IGN);
570 static void shutdown_curses(void)
575 curses_active = false;
579 __noreturn static void finish(int ret)
582 kill_pg_and_die(ret);
586 * exit curses and print given message to stdout/stderr
588 __noreturn __printf_2_3 static void msg_n_exit(int ret, const char* fmt, ...)
591 FILE *outfd = ret? stderr: stdout;
595 vfprintf(outfd, fmt, argp);
597 kill_pg_and_die(ret);
600 static void print_welcome(void)
602 if (loglevel > LL_NOTICE)
604 outputf(COLOR_WELCOME, "Welcome to %s. Theme: %s",
605 version_single_line("gui"), theme.name);
612 static void init_wins(int top_lines)
616 top.lines = top_lines;
621 bot.lines = LINES - top.lines - 3;
623 bot.begy = top.lines + 1;
638 sep.begy = top.lines;
641 assume_default_colors(theme.default_fg, theme.default_bg);
643 mvwin(top.win, top.begy, top.begx);
644 wresize(top.win, top.lines, top.cols);
646 mvwin(sb.win, sb.begy, sb.begx);
647 wresize(sb.win, sb.lines, sb.cols);
649 mvwin(sep.win, sep.begy, sep.begx);
650 wresize(sep.win, sep.lines, sep.cols);
652 mvwin(bot.win, bot.begy, bot.begx);
653 wresize(bot.win, bot.lines, bot.cols);
655 mvwin(in.win, in.begy, in.begx);
656 wresize(in.win, in.lines, in.cols);
658 sep.win = newwin(sep.lines, sep.cols, sep.begy, sep.begx);
659 top.win = newwin(top.lines, top.cols, top.begy, top.begx);
660 bot.win = newwin(bot.lines, bot.cols, bot.begy, bot.begx);
661 sb.win = newwin(sb.lines, sb.cols, sb.begy, sb.begx);
662 in.win = newwin(in.lines, in.cols, in.begy, in.begx);
663 if (!top.win || !bot.win || !sb.win || !in.win || !sep.win)
664 msg_n_exit(1, "Error: Cannot create curses windows\n");
668 scrollok(bot.win, 1);
669 wattron(sb.win, COLOR_PAIR(COLOR_STATUSBAR));
670 wattron(sep.win, COLOR_PAIR(COLOR_SEPARATOR));
671 wattron(bot.win, COLOR_PAIR(COLOR_BOT));
672 wattron(top.win, COLOR_PAIR(COLOR_TOP));
684 wmove(sep.win, 0, 0);
685 for (i = 1; i <= COLS; i++)
686 waddstr(sep.win, theme.sep_str);
689 wnoutrefresh(top.win);
690 wnoutrefresh(bot.win);
691 //wnoutrefresh(sb.win);
693 wnoutrefresh(in.win);
694 wnoutrefresh(sep.win);
699 * Print stat item #i to curses window
701 static void print_stat_item(int i)
704 struct stat_item_data d = theme.data[i];
705 char *c = stat_content[i];
707 if (!curses_active || !d.len || !c)
709 tmp = make_message("%s%s%s", d.prefix, c, d.postfix);
710 wmove(top.win, d.y * top.lines / 100, d.x * COLS / 100);
712 wattron(top.win, COLOR_PAIR(i + 1));
713 align_str(top.win, tmp, d.len * COLS / 100, d.align);
718 static int update_item(int item_num, char *buf)
720 char **c = stat_content + item_num;
727 *c = para_strdup("(artist tag not set)");
730 *c = para_strdup("(title tag not set)");
733 *c = para_strdup("????");
736 *c = para_strdup("(album tag not set)");
739 *c = para_strdup("(comment tag not set)");
743 *c = para_strdup(buf);
745 print_stat_item(item_num);
749 static int read_stat_pipe(fd_set *rfds)
752 static int bufsize, loaded;
758 if (loaded >= bufsize) {
759 if (bufsize > 1000 * 1000) {
763 bufsize += bufsize + 1000;
764 buf = para_realloc(buf, bufsize);
766 assert(loaded < bufsize);
767 ret = read_nonblock(stat_pipe, buf + loaded, bufsize - loaded,
770 ret2 = for_each_stat_item(buf, loaded, update_item);
771 if (ret < 0 || ret2 < 0) {
773 return ret2 < 0? ret2 : ret;
775 sz = ret2; /* what is left */
776 if (sz > 0 && sz < loaded)
777 memmove(buf, buf + loaded - sz, sz);
782 static void print_all_items(void)
788 FOR_EACH_STATUS_ITEM(i)
792 static void clear_all_items(void)
796 FOR_EACH_STATUS_ITEM(i) {
797 free(stat_content[i]);
798 stat_content[i] = para_strdup("");
802 static void init_pair_or_die(short pair, short f, short b)
804 if (init_pair(pair, f, b) == ERR)
805 msg_n_exit(EXIT_FAILURE, "fatal: init_pair() failed\n");
808 static void init_colors_or_die(void)
813 msg_n_exit(EXIT_FAILURE, "fatal: No color term\n");
814 if (start_color() == ERR)
815 msg_n_exit(EXIT_FAILURE, "fatal: failed to start colors\n");
816 FOR_EACH_STATUS_ITEM(i)
817 if (theme.data[i].len)
818 init_pair_or_die(i + 1, theme.data[i].fg,
820 init_pair_or_die(COLOR_STATUSBAR, theme.sb_fg, theme.sb_bg);
821 init_pair_or_die(COLOR_COMMAND, theme.cmd_fg, theme.cmd_bg);
822 init_pair_or_die(COLOR_OUTPUT, theme.output_fg, theme.output_bg);
823 init_pair_or_die(COLOR_MSG, theme.msg_fg, theme.msg_bg);
824 init_pair_or_die(COLOR_ERRMSG, theme.err_msg_fg, theme.err_msg_bg);
825 init_pair_or_die(COLOR_WELCOME, theme.welcome_fg, theme.welcome_bg);
826 init_pair_or_die(COLOR_SEPARATOR, theme.sep_fg, theme.sep_bg);
827 init_pair_or_die(COLOR_TOP, theme.default_fg, theme.default_bg);
828 init_pair_or_die(COLOR_BOT, theme.default_fg, theme.default_bg);
831 /* (Re-)initialize the curses library. */
832 static void init_curses(void)
834 curses_active = true;
835 if (top.win && refresh() == ERR) /* refresh is really needed */
836 msg_n_exit(EXIT_FAILURE, "refresh() failed\n");
837 if (LINES < theme.lines_min || COLS < theme.cols_min)
838 msg_n_exit(EXIT_FAILURE, "Error: Terminal (%dx%d) too small"
839 " (need at least %dx%d)\n", COLS, LINES,
840 theme.cols_min, theme.lines_min);
841 curs_set(0); /* make cursor invisible, ignore errors */
842 nonl(); /* do not NL->CR/NL on output, always returns OK */
843 /* don't echo input */
845 msg_n_exit(EXIT_FAILURE, "fatal: noecho() failed\n");
846 /* take input chars one at a time, no wait for \n */
848 msg_n_exit(EXIT_FAILURE, "fatal: cbreak() failed\n");
849 init_colors_or_die();
850 clear(); /* ignore non-fatal errors */
851 init_wins(theme.top_lines_default);
853 // noecho(); /* don't echo input */
856 static void check_sigchld(void)
862 ret = para_reap_child(&pid);
867 goto reap_next_child;
871 * This sucker modifies its first argument. *handler and *arg are
872 * pointers to 0-terminated strings (inside line). Crap.
874 static int split_key_map(char *line, char **handler, char **arg)
876 if (!(*handler = strchr(line + 1, ':')))
880 if (!(*arg = strchr(*handler, ':')))
889 static int check_key_map_args(void)
893 char *tmp = NULL, *handler, *arg;
895 for (i = 0; i < conf.key_map_given; ++i) {
896 s = conf.key_map_arg[i];
900 tmp = para_strdup(s);
901 if (!split_key_map(tmp, &handler, &arg))
903 if (strlen(handler) != 1)
912 if (find_cmd_byname(arg) < 0)
922 * React to various signal-related events
924 static void handle_signal(int sig)
928 msg_n_exit(EXIT_FAILURE,
929 "only the good die young (caught SIGTERM))\n");
939 PARA_WARNING_LOG("caught SIGINT, reset\n");
940 /* Nothing to do. SIGINT killed our child which gets noticed
941 * by do_select and resets everything.
945 PARA_NOTICE_LOG("got SIGUSR1, rereading configuration\n");
954 static void status_pre_select(fd_set *rfds, int *max_fileno, struct timeval *tv)
956 static struct timeval next_exec, atm, diff;
957 int ret, fds[3] = {0, 1, 0};
962 /* Avoid busy loop */
963 gettimeofday(&atm, NULL);
964 if (tv_diff(&next_exec, &atm, &diff) > 0) {
965 if (tv_diff(&diff, tv, NULL) < 0)
969 next_exec.tv_sec = atm.tv_sec + 2;
970 ret = para_exec_cmdline_pid(&pid, conf.stat_cmd_arg, fds);
973 ret = mark_fd_nonblocking(fds[1]);
980 para_fd_set(stat_pipe, rfds, max_fileno);
983 #define COMMAND_BUF_SIZE 32768
986 * This is the core select loop. Besides the (internal) signal
987 * pipe, the following other fds are checked according to the mode:
989 * GETCH_MODE: check stdin, return when key is pressed
991 * COMMAND_MODE: check command fds and stdin. Return when peer has closed both
992 * stdout and stderr or when any key is pressed.
994 * EXTERNAL_MODE: Check only signal pipe. Used when an external command
995 * is running. During that time curses is disabled. Returns when
998 static int do_select(int mode)
1001 int ret, i, max_fileno;
1002 char command_buf[2][COMMAND_BUF_SIZE] = {"", ""};
1003 int cbo[2] = {0, 0}; /* command buf offsets */
1005 unsigned flags[2] = {0, 0}; /* for for_each_line() */
1008 tv.tv_sec = conf.timeout_arg / 1000;
1009 tv.tv_usec = (conf.timeout_arg % 1000) * 1000;
1010 // ret = refresh_status();
1013 status_pre_select(&rfds, &max_fileno, &tv);
1015 para_fd_set(signal_pipe, &rfds, &max_fileno);
1016 /* command pipe only for COMMAND_MODE */
1017 if (mode == COMMAND_MODE) {
1018 if (command_fds[0] >= 0)
1019 para_fd_set(command_fds[0], &rfds, &max_fileno);
1020 if (command_fds[1] >= 0)
1021 para_fd_set(command_fds[1], &rfds, &max_fileno);
1023 if (mode == GETCH_MODE || mode == COMMAND_MODE)
1024 para_fd_set(STDIN_FILENO, &rfds, &max_fileno);
1025 ret = para_select(max_fileno + 1, &rfds, NULL, &tv);
1027 goto check_return; /* skip fd checks */
1029 ret = para_next_signal(&rfds);
1032 /* read command pipe if ready */
1033 if (mode == COMMAND_MODE) {
1034 for (i = 0; i < 2; i++) {
1036 if (command_fds[i] < 0)
1038 ret = read_nonblock(command_fds[i],
1039 command_buf[i] + cbo[i],
1040 COMMAND_BUF_SIZE - 1 - cbo[i], &rfds, &sz);
1043 cbo[i] = for_each_line(flags[i], command_buf[i], cbo[i],
1044 add_output_line, &i);
1045 if (sz != cbo[i]) { /* at least one line found */
1050 PARA_NOTICE_LOG("closing command fd %d: %s\n",
1051 i, para_strerror(-ret));
1052 close(command_fds[i]);
1053 command_fds[i] = -1;
1056 if (command_fds[!i] < 0) /* both fds closed */
1059 if (cbo[i] == COMMAND_BUF_SIZE - 1) {
1060 PARA_NOTICE_LOG("discarding overlong line\n");
1062 flags[i] = FELF_DISCARD_FIRST;
1066 ret = read_stat_pipe(&rfds);
1068 PARA_NOTICE_LOG("closing stat pipe: %s\n", para_strerror(-ret));
1072 free(stat_content[SI_BASENAME]);
1073 stat_content[SI_BASENAME] =
1074 para_strdup("stat command terminated!?");
1080 ret = wgetch(top.win);
1081 if (ret != ERR && ret != KEY_RESIZE) {
1083 kill(cmd_pid, SIGTERM);
1088 ret = wgetch(top.win);
1089 if (ret != ERR && ret != KEY_RESIZE)
1099 /* read from command pipe and print data to bot window */
1100 static void exec_and_display_cmd(const char *cmd)
1102 int ret, fds[3] = {0, 1, 1};
1104 outputf(COLOR_COMMAND, "%s", cmd);
1105 ret = para_exec_cmdline_pid(&cmd_pid, cmd, fds);
1108 ret = mark_fd_nonblocking(fds[1]);
1111 ret = mark_fd_nonblocking(fds[2]);
1114 command_fds[0] = fds[1];
1115 command_fds[1] = fds[2];
1116 if (do_select(COMMAND_MODE) >= 0)
1117 PARA_INFO_LOG("command complete\n");
1119 PARA_NOTICE_LOG("command aborted\n");
1120 print_in_bar(COLOR_MSG, " ");
1123 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
1124 close(command_fds[0]);
1125 close(command_fds[1]);
1128 static void para_cmd(char *cmd)
1132 print_in_bar(COLOR_MSG, "executing client command, hit any key to abort\n");
1133 c = make_message(BINDIR "/para_client -- %s", cmd);
1134 exec_and_display_cmd(c);
1138 static void display_cmd(char *cmd)
1140 print_in_bar(COLOR_MSG, "executing display command, hit any key to abort");
1141 exec_and_display_cmd(cmd);
1145 * shutdown curses and stat pipe before executing external commands
1147 static void external_cmd(char *cmd)
1149 int fds[3] = {-1, -1, -1};
1154 if (para_exec_cmdline_pid(&cmd_pid, cmd, fds) < 0)
1156 do_select(EXTERNAL_MODE);
1160 static void print_scroll_msg(void)
1162 unsigned lines_total, filled = ringbuffer_filled(bot_win_rb);
1163 int first_rbe = first_visible_rbe(&lines_total);
1165 print_in_bar(COLOR_MSG, "scrolled view: %d-%d/%d\n", filled - first_rbe,
1166 filled - scroll_position, ringbuffer_filled(bot_win_rb));
1169 static void com_scroll_top(void)
1171 int i = RINGBUFFER_SIZE - 1;
1174 while (i > 0 && !ringbuffer_get(bot_win_rb, i))
1176 /* i is oldest entry */
1177 for (; lines < bot.lines && i >= 0; i--) {
1178 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
1181 lines += NUM_LINES(rbe->len);
1184 if (lines > 0 && scroll_position != i) {
1185 scroll_position = i;
1190 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1193 static void com_cancel_scrolling(void)
1196 if (scroll_position == 0) {
1197 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1200 scroll_position = 0;
1204 static void com_page_down(void)
1207 int i = scroll_position;
1209 while (lines < bot.lines && --i > 0) {
1210 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
1213 lines += NUM_LINES(rbe->len);
1216 scroll_position = i;
1221 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1224 static void com_page_up(void)
1227 int fvr = first_visible_rbe(&lines);
1229 if (fvr < 0 || fvr + 1 >= ringbuffer_filled(bot_win_rb)) {
1230 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1233 scroll_position = fvr + 1;
1234 for (; scroll_position > 0; scroll_position--) {
1235 first_visible_rbe(&lines);
1236 if (lines == bot.lines)
1243 static void com_scroll_down(void)
1245 struct rb_entry *rbe;
1248 if (!scroll_position) {
1249 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1253 rbe = ringbuffer_get(bot_win_rb, scroll_position);
1254 rbe_lines = NUM_LINES(rbe->len);
1255 wscrl(bot.win, rbe_lines);
1256 wmove(bot.win, bot.lines - rbe_lines, 0);
1257 wattron(bot.win, COLOR_PAIR(rbe->color));
1258 waddstr(bot.win, rbe->msg);
1263 static void com_scroll_up(void)
1265 struct rb_entry *rbe = NULL;
1267 int i, first_rbe, num_scroll;
1269 /* the entry that is going to vanish */
1270 rbe = ringbuffer_get(bot_win_rb, scroll_position);
1273 num_scroll = NUM_LINES(rbe->len);
1274 first_rbe = first_visible_rbe(&lines);
1275 if (first_rbe < 0 || (first_rbe == ringbuffer_filled(bot_win_rb) - 1))
1278 wscrl(bot.win, -num_scroll);
1279 i = draw_top_rbe(&lines);
1282 while (i > 0 && lines < num_scroll) {
1284 rbe = ringbuffer_get(bot_win_rb, --i);
1287 rbe_lines = NUM_LINES(rbe->len);
1289 // fprintf(stderr, "msg: %s\n", rbe->msg);
1290 wattron(bot.win, COLOR_PAIR(rbe->color));
1291 waddstr(bot.win, "\n");
1292 waddstr(bot.win, rbe->msg);
1301 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1304 static void com_ll_decr(void)
1306 if (loglevel <= LL_DEBUG) {
1307 print_in_bar(COLOR_ERRMSG,
1308 "loglevel already at maximal verbosity\n");
1312 print_in_bar(COLOR_MSG, "loglevel set to %d\n", loglevel);
1315 static void com_ll_incr(void)
1317 if (loglevel >= LL_EMERG) {
1318 print_in_bar(COLOR_ERRMSG,
1319 "loglevel already at minimal verbosity\n");
1323 print_in_bar(COLOR_MSG, "loglevel set to %d\n", loglevel);
1327 * reread configuration, terminate on errors
1329 static void com_reread_conf(void)
1331 char *cf = configfile_exists();
1332 struct gui_cmdline_parser_params params = {
1335 .check_required = 0,
1336 .check_ambiguity = 0,
1341 PARA_WARNING_LOG("there is no configuration to read\n");
1344 PARA_INFO_LOG("rereading command line options and config file\n");
1345 gui_cmdline_parser_ext(_argc, _argv, &conf, ¶ms);
1347 * Despite .print_errors is set to 0, gengetopt will print to stderr
1348 * anyway, and exit on errors. So we have to shutdown curses first.
1351 gui_cmdline_parser_config_file(cf, &conf, ¶ms);
1353 PARA_NOTICE_LOG("config file reloaded\n");
1354 if (check_key_map_args() < 0)
1355 finish(EXIT_FAILURE);
1358 static void com_help(void)
1362 for (i = 0; i < conf.key_map_given; ++i) {
1363 char *handler, *arg, *tmp = para_strdup(conf.key_map_arg[i]);
1364 const char *handler_text = "???", *desc = NULL;
1366 if (!split_key_map(tmp, &handler, &arg)) {
1372 handler_text = "internal";
1373 desc = command_list[find_cmd_byname(arg)].description;
1375 case 'x': handler_text = "external"; break;
1376 case 'd': handler_text = "display "; break;
1377 case 'p': handler_text = "para "; break;
1379 outputf(COLOR_MSG, "%s\t%s\t%s%s\t%s", tmp, handler_text, arg,
1380 strlen(arg) < 8? "\t" : "",
1384 for (i = 0; command_list[i].handler; i++) {
1385 struct gui_command gc = command_list[i];
1387 outputf(COLOR_MSG, "%s\tinternal\t%s\t%s%s", gc.key, gc.name,
1388 strlen(gc.name) < 8? "\t" : "",
1391 print_in_bar(COLOR_MSG, "try \"para_gui -h\" or \"para_client help\" "
1395 static void com_shrink_top_win(void)
1397 if (top.lines <= theme.top_lines_min) {
1398 PARA_WARNING_LOG("can not decrease top window\n");
1401 init_wins(top.lines - 1);
1404 print_in_bar(COLOR_MSG, "%s", "decreased top window");
1407 static void com_enlarge_top_win(void)
1409 if (bot.lines < 3) {
1410 PARA_WARNING_LOG("can not increase top window\n");
1413 init_wins(top.lines + 1);
1416 print_in_bar(COLOR_MSG, "increased top window");
1419 static void com_version(void)
1421 print_in_bar(COLOR_MSG, "%s", version_single_line("gui"));
1424 __noreturn static void com_quit(void)
1429 static void com_refresh(void)
1435 static void change_theme(int next)
1441 /* This seems to be needed twice, why? */
1444 PARA_NOTICE_LOG("new theme: %s\n", theme.name);
1447 static void com_next_theme(void)
1452 static void com_prev_theme(void)
1458 static void handle_command(int c)
1462 /* first check user-defined key bindings */
1463 for (i = 0; i < conf.key_map_given; ++i) {
1464 char *tmp, *handler, *arg;
1466 tmp = para_strdup(conf.key_map_arg[i]);
1467 if (!split_key_map(tmp, &handler, &arg)) {
1471 if (strcmp(tmp, km_keyname(c))) {
1475 if (*handler == 'd')
1477 else if (*handler == 'x')
1479 else if (*handler == 'p')
1481 else if (*handler == 'i') {
1482 int num = find_cmd_byname(arg);
1484 command_list[num].handler();
1489 /* not found, check internal key bindings */
1490 for (i = 0; command_list[i].handler; i++) {
1491 if (!strcmp(km_keyname(c), command_list[i].key)) {
1492 command_list[i].handler();
1496 print_in_bar(COLOR_ERRMSG, "key '%s' is not bound, press ? for help",
1500 __noreturn static void print_help_and_die(void)
1502 struct ggo_help h = DEFINE_GGO_HELP(gui);
1503 bool d = conf.detailed_help_given;
1505 ggo_print_help(&h, d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS);
1509 int main(int argc, char *argv[])
1517 gui_cmdline_parser(argc, argv, &conf); /* exits on errors */
1518 loglevel = get_loglevel_by_name(conf.loglevel_arg);
1519 version_handle_flag("gui", conf.version_given);
1520 if (conf.help_given || conf.detailed_help_given)
1521 print_help_and_die();
1522 cf = configfile_exists();
1523 if (!cf && conf.config_file_given) {
1524 fprintf(stderr, "can not read config file %s\n",
1525 conf.config_file_arg);
1529 struct gui_cmdline_parser_params params = {
1532 .check_required = 0,
1533 .check_ambiguity = 0,
1536 gui_cmdline_parser_config_file(cf, &conf, ¶ms);
1537 loglevel = get_loglevel_by_name(conf.loglevel_arg);
1539 if (check_key_map_args() < 0) {
1540 fprintf(stderr, "invalid key map\n");
1543 init_theme_or_die(conf.theme_arg, &theme);
1544 setup_signal_handling();
1545 bot_win_rb = ringbuffer_new(RINGBUFFER_SIZE);
1546 setlocale(LC_CTYPE, "");
1547 initscr(); /* needed only once, always successful */
1552 ret = do_select(GETCH_MODE);
1555 print_in_bar(COLOR_MSG, " ");
1556 handle_command(ret);