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 {
39 } top, bot, sb, in, sep;
41 #define RINGBUFFER_SIZE 512
47 static struct ringbuffer *bot_win_rb;
48 #define NUM_LINES(len) (1 + (len) / bot.cols)
50 static unsigned scroll_position;
54 static int command_fds[2] = {-1, -1};
55 static int stat_pipe = -1;
56 static struct gui_args_info conf;
58 enum {GETCH_MODE, COMMAND_MODE, EXTERNAL_MODE};
62 * Codes for various colors.
64 * Each status item has its own color pair. The ones defined here start at a
65 * higher number so that they do not overlap with these.
68 COLOR_STATUSBAR = NUM_STAT_ITEMS + 1,
81 const char *description;
82 void (*handler)(void);
85 static struct gui_theme theme;
90 static void com_help(void);
91 static void com_reread_conf(void);
92 static void com_enlarge_top_win(void);
93 static void com_shrink_top_win(void);
94 static void com_version(void);
95 __noreturn static void com_quit(void);
96 static void com_refresh(void);
97 static void com_ll_incr(void);
98 static void com_ll_decr(void);
99 static void com_prev_theme(void);
100 static void com_next_theme(void);
101 static void com_scroll_up(void);
102 static void com_scroll_down(void);
103 static void com_page_up(void);
104 static void com_page_down(void);
105 static void com_cancel_scrolling(void);
106 static void com_scroll_top(void);
108 static struct gui_command command_list[] = {
112 .description = "print help",
116 .name = "enlarge_win",
117 .description = "enlarge the top window",
118 .handler = com_enlarge_top_win
121 .name = "shrink_win",
122 .description = "shrink the top window",
123 .handler = com_shrink_top_win
126 .name = "reread_conf",
127 .description = "reread configuration file",
128 .handler = com_reread_conf
132 .description = "exit para_gui",
137 .description = "redraw the screen",
138 .handler = com_refresh
141 .name = "next_theme",
142 .description = "switch to next theme",
143 .handler = com_next_theme
146 .name = "prev_theme",
147 .description = "switch to previous stream",
148 .handler = com_prev_theme
152 .description = "increase loglevel (decreases verbosity)",
153 .handler = com_ll_incr
157 .description = "decrease loglevel (increases verbosity)",
158 .handler = com_ll_decr
162 .description = "show the para_gui version",
163 .handler = com_version
167 .description = "scroll up one line",
168 .handler = com_scroll_up
171 .name = "scroll_down",
172 .description = "scroll down one line",
173 .handler = com_scroll_down
177 .description = "scroll up one page",
178 .handler = com_page_up
182 .description = "scroll down one page",
183 .handler = com_page_down
186 .name = "scroll_top",
187 .description = "scroll to top of buffer",
188 .handler = com_scroll_top
191 .name = "cancel_scroll",
192 .description = "deactivate scroll mode",
193 .handler = com_cancel_scrolling
199 static int find_cmd_byname(char *name)
203 for (i = 0; command_list[i].handler; i++)
204 if (!strcmp(command_list[i].name, name))
209 /* isendwin() returns false before initscr() was called */
210 static bool curses_active(void)
212 return top.win && !isendwin();
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,...)
353 if (!curses_active())
355 wattron(in.win, COLOR_PAIR(color));
357 xvasprintf(&msg, fmt, ap);
360 align_str(in.win, msg, in.cols, LEFT);
365 static void print_status_bar(void)
369 tmp = para_strdup("para_gui " PACKAGE_VERSION " (hit ? for help)");
371 align_str(sb.win, tmp, sb.cols, CENTER);
376 * get the number of the oldest rbe that is (partially) visible. On return,
377 * lines contains the sum of the number of lines of all visible entries. If the
378 * first one is only partially visible, lines is greater than bot.lines.
380 static int first_visible_rbe(unsigned *lines)
384 for (i = scroll_position; i < RINGBUFFER_SIZE; i++) {
385 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
389 // fprintf(stderr, "found: %s\n", rbe->msg);
390 rbe_lines = NUM_LINES(rbe->len);
391 if (rbe_lines > bot.lines)
394 if (*lines >= bot.lines)
397 return RINGBUFFER_SIZE - 1;
401 returns number of first visible rbe, *lines is the number of lines drawn.
403 static int draw_top_rbe(unsigned *lines)
405 int ret, fvr = first_visible_rbe(lines);
406 struct rb_entry *rbe;
407 size_t bytes_to_skip, cells_to_skip, width;
411 wmove(bot.win, 0, 0);
412 rbe = ringbuffer_get(bot_win_rb, fvr);
415 if (*lines > bot.lines) {
416 /* rbe is partially visible multi-line */
417 cells_to_skip = (*lines - bot.lines) * bot.cols;
418 ret = skip_cells(rbe->msg, cells_to_skip, &bytes_to_skip);
421 ret = strwidth(rbe->msg + bytes_to_skip, &width);
428 wattron(bot.win, COLOR_PAIR(rbe->color));
429 waddstr(bot.win, rbe->msg + bytes_to_skip);
430 *lines = NUM_LINES(width);
434 static void redraw_bot_win(void)
439 wmove(bot.win, 0, 0);
441 i = draw_top_rbe(&lines);
444 while (i > 0 && lines < bot.lines) {
445 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, --i);
448 waddstr(bot.win, "\n");
451 lines += NUM_LINES(rbe->len);
452 wattron(bot.win, COLOR_PAIR(rbe->color));
453 waddstr(bot.win, "\n");
454 waddstr(bot.win, rbe->msg);
460 static void rb_add_entry(int color, char *msg)
462 struct rb_entry *old, *new;
466 if (strwidth(msg, &len) < 0)
468 new = para_malloc(sizeof(struct rb_entry));
472 old = ringbuffer_add(bot_win_rb, new);
473 // fprintf(stderr, "added: %s\n", new->msg);
478 if (scroll_position) {
479 /* discard current scrolling, like xterm does */
484 wattron(bot.win, COLOR_PAIR(color));
485 getyx(bot.win, y, x);
487 waddstr(bot.win, "\n");
488 waddstr(bot.win, msg);
492 * print formated output to bot win and refresh
494 __printf_2_3 static void outputf(int color, const char* fmt,...)
499 if (!curses_active())
502 xvasprintf(&msg, fmt, ap);
504 rb_add_entry(color, msg);
508 static int add_output_line(char *line, void *data)
510 int color = *(int *)data? COLOR_ERRMSG : COLOR_OUTPUT;
512 if (!curses_active())
514 rb_add_entry(color, para_strdup(line));
520 static __printf_2_3 void curses_log(int ll, const char *fmt,...)
527 if (ll < loglevel || !curses_active())
536 color = COLOR_ERRMSG;
539 bytes = xvasprintf(&msg, fmt, ap);
541 if (bytes > 0 && msg[bytes - 1] == '\n')
542 msg[bytes - 1] = '\0'; /* cut trailing newline */
543 rb_add_entry(color, msg);
546 __printf_2_3 void (*para_log)(int, const char*, ...) = curses_log;
548 static void setup_signal_handling(void)
550 signal_pipe = para_signal_init();
551 para_install_sighandler(SIGINT);
552 para_install_sighandler(SIGTERM);
553 para_install_sighandler(SIGCHLD);
554 para_install_sighandler(SIGWINCH);
555 para_install_sighandler(SIGUSR1);
558 /* kill every process in the process group and exit */
559 __noreturn static void kill_pg_and_die(int ret)
561 para_sigaction(SIGTERM, SIG_IGN);
566 static void shutdown_curses(void)
572 __noreturn static void finish(int ret)
575 kill_pg_and_die(ret);
579 * exit curses and print given message to stdout/stderr
581 __noreturn __printf_2_3 static void msg_n_exit(int ret, const char* fmt, ...)
584 FILE *outfd = ret? stderr: stdout;
588 vfprintf(outfd, fmt, argp);
590 kill_pg_and_die(ret);
596 static void init_wins(int top_lines)
598 int top_y = 0, bot_y = top_lines + 1, sb_y = LINES - 2,
599 in_y = LINES - 1, sep_y = top_lines;
601 top.lines = top_lines;
602 bot.lines = LINES - top.lines - 3;
603 sb.lines = in.lines = sep.lines = 1;
605 top.cols = bot.cols = sb.cols = in.cols = sep.cols = COLS;
607 assume_default_colors(theme.default_fg, theme.default_bg);
609 wresize(top.win, top.lines, top.cols);
610 mvwin(top.win, top_y, 0);
612 wresize(sb.win, sb.lines, sb.cols);
613 mvwin(sb.win, sb_y, 0);
615 wresize(sep.win, sep.lines, sep.cols);
616 mvwin(sep.win, sep_y, 0);
618 wresize(bot.win, bot.lines, bot.cols);
619 mvwin(bot.win, bot_y, 0);
621 wresize(in.win, in.lines, in.cols);
622 mvwin(in.win, in_y, 0);
624 sep.win = newwin(sep.lines, sep.cols, sep_y, 0);
625 top.win = newwin(top.lines, top.cols, top_y, 0);
626 bot.win = newwin(bot.lines, bot.cols, bot_y, 0);
627 sb.win = newwin(sb.lines, sb.cols, sb_y, 0);
628 in.win = newwin(in.lines, in.cols, in_y, 0);
629 if (!top.win || !bot.win || !sb.win || !in.win || !sep.win)
630 msg_n_exit(1, "Error: Cannot create curses windows\n");
634 scrollok(bot.win, 1);
635 wattron(sb.win, COLOR_PAIR(COLOR_STATUSBAR));
636 wattron(sep.win, COLOR_PAIR(COLOR_SEPARATOR));
637 wattron(bot.win, COLOR_PAIR(COLOR_BOT));
638 wattron(top.win, COLOR_PAIR(COLOR_TOP));
649 wmove(sep.win, 0, 0);
650 whline(sep.win, theme.sep_char, COLS);
653 wnoutrefresh(top.win);
654 wnoutrefresh(bot.win);
656 wnoutrefresh(sb.win);
657 wnoutrefresh(in.win);
658 wnoutrefresh(sep.win);
663 * Print stat item #i to curses window
665 static void print_stat_item(int i)
668 struct stat_item_data d = theme.data[i];
669 char *c = stat_content[i];
671 if (!curses_active() || !d.len || !c)
673 tmp = make_message("%s%s%s", d.prefix, c, d.postfix);
674 wmove(top.win, d.y * top.lines / 100, d.x * COLS / 100);
676 wattron(top.win, COLOR_PAIR(i + 1));
677 align_str(top.win, tmp, d.len * COLS / 100, d.align);
682 static int update_item(int item_num, char *buf)
684 char **c = stat_content + item_num;
691 *c = para_strdup("(artist tag not set)");
694 *c = para_strdup("(title tag not set)");
697 *c = para_strdup("????");
700 *c = para_strdup("(album tag not set)");
703 *c = para_strdup("(comment tag not set)");
707 *c = para_strdup(buf);
709 print_stat_item(item_num);
713 static int read_stat_pipe(fd_set *rfds)
716 static int bufsize, loaded;
722 if (loaded >= bufsize) {
723 if (bufsize > 1000 * 1000) {
727 bufsize += bufsize + 1000;
728 buf = para_realloc(buf, bufsize);
730 assert(loaded < bufsize);
731 ret = read_nonblock(stat_pipe, buf + loaded, bufsize - loaded,
734 ret2 = for_each_stat_item(buf, loaded, update_item);
735 if (ret < 0 || ret2 < 0) {
737 return ret2 < 0? ret2 : ret;
739 sz = ret2; /* what is left */
740 if (sz > 0 && sz < loaded)
741 memmove(buf, buf + loaded - sz, sz);
746 static void print_all_items(void)
750 if (!curses_active())
752 FOR_EACH_STATUS_ITEM(i)
756 static void clear_all_items(void)
760 FOR_EACH_STATUS_ITEM(i) {
761 free(stat_content[i]);
762 stat_content[i] = para_strdup("");
766 static void init_pair_or_die(short pair, short f, short b)
768 if (init_pair(pair, f, b) == ERR)
769 msg_n_exit(EXIT_FAILURE, "fatal: init_pair() failed\n");
772 static void init_colors_or_die(void)
777 msg_n_exit(EXIT_FAILURE, "fatal: No color term\n");
778 if (start_color() == ERR)
779 msg_n_exit(EXIT_FAILURE, "fatal: failed to start colors\n");
780 FOR_EACH_STATUS_ITEM(i)
781 if (theme.data[i].len)
782 init_pair_or_die(i + 1, theme.data[i].fg,
784 init_pair_or_die(COLOR_STATUSBAR, theme.sb_fg, theme.sb_bg);
785 init_pair_or_die(COLOR_COMMAND, theme.cmd_fg, theme.cmd_bg);
786 init_pair_or_die(COLOR_OUTPUT, theme.output_fg, theme.output_bg);
787 init_pair_or_die(COLOR_MSG, theme.msg_fg, theme.msg_bg);
788 init_pair_or_die(COLOR_ERRMSG, theme.err_msg_fg, theme.err_msg_bg);
789 init_pair_or_die(COLOR_SEPARATOR, theme.sep_fg, theme.sep_bg);
790 init_pair_or_die(COLOR_TOP, theme.default_fg, theme.default_bg);
791 init_pair_or_die(COLOR_BOT, theme.default_fg, theme.default_bg);
794 /* (Re-)initialize the curses library. */
795 static void init_curses(void)
799 if (top.win && refresh() == ERR) /* refresh is really needed */
800 msg_n_exit(EXIT_FAILURE, "refresh() failed\n");
801 if (LINES < theme.lines_min || COLS < theme.cols_min)
802 msg_n_exit(EXIT_FAILURE, "Error: Terminal (%dx%d) too small"
803 " (need at least %dx%d)\n", COLS, LINES,
804 theme.cols_min, theme.lines_min);
805 curs_set(0); /* make cursor invisible, ignore errors */
806 nonl(); /* do not NL->CR/NL on output, always returns OK */
807 /* don't echo input */
809 msg_n_exit(EXIT_FAILURE, "fatal: noecho() failed\n");
810 /* take input chars one at a time, no wait for \n */
812 msg_n_exit(EXIT_FAILURE, "fatal: cbreak() failed\n");
813 init_colors_or_die();
814 clear(); /* ignore non-fatal errors */
815 init_wins(theme.top_lines_default);
817 // noecho(); /* don't echo input */
820 static void check_sigchld(void)
826 ret = para_reap_child(&pid);
831 goto reap_next_child;
835 * This sucker modifies its first argument. *handler and *arg are
836 * pointers to 0-terminated strings (inside line). Crap.
838 static int split_key_map(char *line, char **handler, char **arg)
840 if (!(*handler = strchr(line + 1, ':')))
844 if (!(*arg = strchr(*handler, ':')))
853 static int check_key_map_args(void)
857 char *tmp = NULL, *handler, *arg;
859 for (i = 0; i < conf.key_map_given; ++i) {
860 s = conf.key_map_arg[i];
864 tmp = para_strdup(s);
865 if (!split_key_map(tmp, &handler, &arg))
867 if (strlen(handler) != 1)
876 if (find_cmd_byname(arg) < 0)
886 * React to various signal-related events
888 static void handle_signal(int sig)
892 msg_n_exit(EXIT_FAILURE,
893 "only the good die young (caught SIGTERM))\n");
896 if (curses_active()) {
903 PARA_WARNING_LOG("caught SIGINT, reset\n");
904 /* Nothing to do. SIGINT killed our child which gets noticed
905 * by do_select and resets everything.
909 PARA_NOTICE_LOG("got SIGUSR1, rereading configuration\n");
918 static void status_pre_select(fd_set *rfds, int *max_fileno, struct timeval *tv)
920 static struct timeval next_exec, atm, diff;
921 int ret, fds[3] = {0, 1, 0};
926 /* Avoid busy loop */
927 gettimeofday(&atm, NULL);
928 if (tv_diff(&next_exec, &atm, &diff) > 0) {
929 if (tv_diff(&diff, tv, NULL) < 0)
933 next_exec.tv_sec = atm.tv_sec + 2;
934 ret = para_exec_cmdline_pid(&pid, conf.stat_cmd_arg, fds);
937 ret = mark_fd_nonblocking(fds[1]);
944 para_fd_set(stat_pipe, rfds, max_fileno);
947 #define COMMAND_BUF_SIZE 32768
950 * This is the core select loop. Besides the (internal) signal
951 * pipe, the following other fds are checked according to the mode:
953 * GETCH_MODE: check stdin, return when key is pressed
955 * COMMAND_MODE: check command fds and stdin. Return when peer has closed both
956 * stdout and stderr or when any key is pressed.
958 * EXTERNAL_MODE: Check only signal pipe. Used when an external command
959 * is running. During that time curses is disabled. Returns when
962 static int do_select(int mode)
965 int ret, i, max_fileno;
966 char command_buf[2][COMMAND_BUF_SIZE] = {"", ""};
967 int cbo[2] = {0, 0}; /* command buf offsets */
969 unsigned flags[2] = {0, 0}; /* for for_each_line() */
972 tv.tv_sec = conf.timeout_arg / 1000;
973 tv.tv_usec = (conf.timeout_arg % 1000) * 1000;
974 // ret = refresh_status();
977 status_pre_select(&rfds, &max_fileno, &tv);
979 para_fd_set(signal_pipe, &rfds, &max_fileno);
980 /* command pipe only for COMMAND_MODE */
981 if (mode == COMMAND_MODE) {
982 if (command_fds[0] >= 0)
983 para_fd_set(command_fds[0], &rfds, &max_fileno);
984 if (command_fds[1] >= 0)
985 para_fd_set(command_fds[1], &rfds, &max_fileno);
987 if (mode == GETCH_MODE || mode == COMMAND_MODE)
988 para_fd_set(STDIN_FILENO, &rfds, &max_fileno);
989 ret = para_select(max_fileno + 1, &rfds, NULL, &tv);
991 goto check_return; /* skip fd checks */
993 ret = para_next_signal(&rfds);
996 /* read command pipe if ready */
997 if (mode == COMMAND_MODE) {
998 for (i = 0; i < 2; i++) {
1000 if (command_fds[i] < 0)
1002 ret = read_nonblock(command_fds[i],
1003 command_buf[i] + cbo[i],
1004 COMMAND_BUF_SIZE - 1 - cbo[i], &rfds, &sz);
1007 cbo[i] = for_each_line(flags[i], command_buf[i], cbo[i],
1008 add_output_line, &i);
1009 if (sz != cbo[i]) { /* at least one line found */
1014 PARA_NOTICE_LOG("closing command fd %d: %s\n",
1015 i, para_strerror(-ret));
1016 close(command_fds[i]);
1017 command_fds[i] = -1;
1020 if (command_fds[!i] < 0) /* both fds closed */
1023 if (cbo[i] == COMMAND_BUF_SIZE - 1) {
1024 PARA_NOTICE_LOG("discarding overlong line\n");
1026 flags[i] = FELF_DISCARD_FIRST;
1030 ret = read_stat_pipe(&rfds);
1032 PARA_NOTICE_LOG("closing stat pipe: %s\n", para_strerror(-ret));
1036 free(stat_content[SI_BASENAME]);
1037 stat_content[SI_BASENAME] =
1038 para_strdup("stat command terminated!?");
1044 ret = wgetch(top.win);
1045 if (ret != ERR && ret != KEY_RESIZE) {
1047 kill(cmd_pid, SIGTERM);
1052 ret = wgetch(top.win);
1053 if (ret != ERR && ret != KEY_RESIZE)
1063 /* read from command pipe and print data to bot window */
1064 static void exec_and_display_cmd(const char *cmd)
1066 int ret, fds[3] = {0, 1, 1};
1068 outputf(COLOR_COMMAND, "%s", cmd);
1069 ret = para_exec_cmdline_pid(&cmd_pid, cmd, fds);
1072 ret = mark_fd_nonblocking(fds[1]);
1075 ret = mark_fd_nonblocking(fds[2]);
1078 command_fds[0] = fds[1];
1079 command_fds[1] = fds[2];
1080 if (do_select(COMMAND_MODE) >= 0)
1081 PARA_INFO_LOG("command complete\n");
1083 PARA_NOTICE_LOG("command aborted\n");
1084 print_in_bar(COLOR_MSG, " ");
1087 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
1088 close(command_fds[0]);
1089 close(command_fds[1]);
1092 static void para_cmd(char *cmd)
1096 print_in_bar(COLOR_MSG, "executing client command, hit any key to abort\n");
1097 c = make_message(BINDIR "/para_client -- %s", cmd);
1098 exec_and_display_cmd(c);
1102 static void display_cmd(char *cmd)
1104 print_in_bar(COLOR_MSG, "executing display command, hit any key to abort");
1105 exec_and_display_cmd(cmd);
1109 * shutdown curses and stat pipe before executing external commands
1111 static void external_cmd(char *cmd)
1113 int fds[3] = {-1, -1, -1};
1118 if (para_exec_cmdline_pid(&cmd_pid, cmd, fds) < 0)
1120 do_select(EXTERNAL_MODE);
1124 static void print_scroll_msg(void)
1126 unsigned lines_total, filled = ringbuffer_filled(bot_win_rb);
1127 int first_rbe = first_visible_rbe(&lines_total);
1129 print_in_bar(COLOR_MSG, "scrolled view: %d-%d/%d\n", filled - first_rbe,
1130 filled - scroll_position, ringbuffer_filled(bot_win_rb));
1133 static void com_scroll_top(void)
1135 int i = RINGBUFFER_SIZE - 1;
1138 while (i > 0 && !ringbuffer_get(bot_win_rb, i))
1140 /* i is oldest entry */
1141 for (; lines < bot.lines && i >= 0; i--) {
1142 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
1145 lines += NUM_LINES(rbe->len);
1148 if (lines > 0 && scroll_position != i) {
1149 scroll_position = i;
1154 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1157 static void com_cancel_scrolling(void)
1160 if (scroll_position == 0) {
1161 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1164 scroll_position = 0;
1168 static void com_page_down(void)
1171 int i = scroll_position;
1173 while (lines < bot.lines && --i > 0) {
1174 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
1177 lines += NUM_LINES(rbe->len);
1180 scroll_position = i;
1185 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1188 static void com_page_up(void)
1191 int fvr = first_visible_rbe(&lines);
1193 if (fvr < 0 || fvr + 1 >= ringbuffer_filled(bot_win_rb)) {
1194 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1197 scroll_position = fvr + 1;
1198 for (; scroll_position > 0; scroll_position--) {
1199 first_visible_rbe(&lines);
1200 if (lines == bot.lines)
1207 static void com_scroll_down(void)
1209 struct rb_entry *rbe;
1212 if (!scroll_position) {
1213 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1217 rbe = ringbuffer_get(bot_win_rb, scroll_position);
1218 rbe_lines = NUM_LINES(rbe->len);
1219 wscrl(bot.win, rbe_lines);
1220 wmove(bot.win, bot.lines - rbe_lines, 0);
1221 wattron(bot.win, COLOR_PAIR(rbe->color));
1222 waddstr(bot.win, rbe->msg);
1227 static void com_scroll_up(void)
1229 struct rb_entry *rbe = NULL;
1231 int i, first_rbe, num_scroll;
1233 /* the entry that is going to vanish */
1234 rbe = ringbuffer_get(bot_win_rb, scroll_position);
1237 num_scroll = NUM_LINES(rbe->len);
1238 first_rbe = first_visible_rbe(&lines);
1239 if (first_rbe < 0 || (first_rbe == ringbuffer_filled(bot_win_rb) - 1))
1242 wscrl(bot.win, -num_scroll);
1243 i = draw_top_rbe(&lines);
1246 while (i > 0 && lines < num_scroll) {
1248 rbe = ringbuffer_get(bot_win_rb, --i);
1251 rbe_lines = NUM_LINES(rbe->len);
1253 // fprintf(stderr, "msg: %s\n", rbe->msg);
1254 wattron(bot.win, COLOR_PAIR(rbe->color));
1255 waddstr(bot.win, "\n");
1256 waddstr(bot.win, rbe->msg);
1265 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1268 static void com_ll_decr(void)
1270 if (loglevel <= LL_DEBUG) {
1271 print_in_bar(COLOR_ERRMSG,
1272 "loglevel already at maximal verbosity\n");
1276 print_in_bar(COLOR_MSG, "loglevel set to %d\n", loglevel);
1279 static void com_ll_incr(void)
1281 if (loglevel >= LL_EMERG) {
1282 print_in_bar(COLOR_ERRMSG,
1283 "loglevel already at minimal verbosity\n");
1287 print_in_bar(COLOR_MSG, "loglevel set to %d\n", loglevel);
1291 * reread configuration, terminate on errors
1293 static void com_reread_conf(void)
1295 char *cf = configfile_exists();
1296 struct gui_cmdline_parser_params params = {
1299 .check_required = 0,
1300 .check_ambiguity = 0,
1305 PARA_WARNING_LOG("there is no configuration to read\n");
1308 PARA_INFO_LOG("rereading command line options and config file\n");
1309 gui_cmdline_parser_ext(_argc, _argv, &conf, ¶ms);
1311 * Despite .print_errors is set to 0, gengetopt will print to stderr
1312 * anyway, and exit on errors. So we have to shutdown curses first.
1315 gui_cmdline_parser_config_file(cf, &conf, ¶ms);
1317 PARA_NOTICE_LOG("config file reloaded\n");
1318 if (check_key_map_args() < 0)
1319 finish(EXIT_FAILURE);
1322 static void com_help(void)
1326 for (i = 0; i < conf.key_map_given; ++i) {
1327 char *handler, *arg, *tmp = para_strdup(conf.key_map_arg[i]);
1328 const char *handler_text = "???", *desc = NULL;
1330 if (!split_key_map(tmp, &handler, &arg)) {
1336 handler_text = "internal";
1337 desc = command_list[find_cmd_byname(arg)].description;
1339 case 'x': handler_text = "external"; break;
1340 case 'd': handler_text = "display "; break;
1341 case 'p': handler_text = "para "; break;
1343 outputf(COLOR_MSG, "%s\t%s\t%s%s\t%s", tmp, handler_text, arg,
1344 strlen(arg) < 8? "\t" : "",
1348 for (i = 0; command_list[i].handler; i++) {
1349 struct gui_command gc = command_list[i];
1351 outputf(COLOR_MSG, "%s\tinternal\t%s\t%s%s", gc.key, gc.name,
1352 strlen(gc.name) < 8? "\t" : "",
1355 print_in_bar(COLOR_MSG, "try \"para_gui -h\" or \"para_client help\" "
1359 static void com_shrink_top_win(void)
1361 if (top.lines <= theme.top_lines_min) {
1362 PARA_WARNING_LOG("can not decrease top window\n");
1365 init_wins(top.lines - 1);
1368 print_in_bar(COLOR_MSG, "%s", "decreased top window");
1371 static void com_enlarge_top_win(void)
1373 if (bot.lines < 3) {
1374 PARA_WARNING_LOG("can not increase top window\n");
1377 init_wins(top.lines + 1);
1380 print_in_bar(COLOR_MSG, "increased top window");
1383 static void com_version(void)
1385 print_in_bar(COLOR_MSG, "%s", version_single_line("gui"));
1388 __noreturn static void com_quit(void)
1393 static void com_refresh(void)
1399 static void com_next_theme(void)
1405 static void com_prev_theme(void)
1411 static void handle_command(int c)
1415 /* first check user-defined key bindings */
1416 for (i = 0; i < conf.key_map_given; ++i) {
1417 char *tmp, *handler, *arg;
1419 tmp = para_strdup(conf.key_map_arg[i]);
1420 if (!split_key_map(tmp, &handler, &arg)) {
1424 if (strcmp(tmp, km_keyname(c))) {
1428 if (*handler == 'd')
1430 else if (*handler == 'x')
1432 else if (*handler == 'p')
1434 else if (*handler == 'i') {
1435 int num = find_cmd_byname(arg);
1437 command_list[num].handler();
1442 /* not found, check internal key bindings */
1443 for (i = 0; command_list[i].handler; i++) {
1444 if (!strcmp(km_keyname(c), command_list[i].key)) {
1445 command_list[i].handler();
1449 print_in_bar(COLOR_ERRMSG, "key '%s' is not bound, press ? for help",
1453 __noreturn static void print_help_and_die(void)
1455 struct ggo_help h = DEFINE_GGO_HELP(gui);
1456 bool d = conf.detailed_help_given;
1458 ggo_print_help(&h, d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS);
1462 int main(int argc, char *argv[])
1470 gui_cmdline_parser(argc, argv, &conf); /* exits on errors */
1471 loglevel = get_loglevel_by_name(conf.loglevel_arg);
1472 version_handle_flag("gui", conf.version_given);
1473 if (conf.help_given || conf.detailed_help_given)
1474 print_help_and_die();
1475 cf = configfile_exists();
1476 if (!cf && conf.config_file_given) {
1477 fprintf(stderr, "can not read config file %s\n",
1478 conf.config_file_arg);
1482 struct gui_cmdline_parser_params params = {
1485 .check_required = 0,
1486 .check_ambiguity = 0,
1489 gui_cmdline_parser_config_file(cf, &conf, ¶ms);
1490 loglevel = get_loglevel_by_name(conf.loglevel_arg);
1492 if (check_key_map_args() < 0) {
1493 fprintf(stderr, "invalid key map\n");
1496 theme_init(conf.theme_arg, &theme);
1497 setup_signal_handling();
1498 bot_win_rb = ringbuffer_new(RINGBUFFER_SIZE);
1499 setlocale(LC_CTYPE, "");
1500 initscr(); /* needed only once, always successful */
1504 ret = do_select(GETCH_MODE);
1507 print_in_bar(COLOR_MSG, " ");
1508 handle_command(ret);