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 {
41 } top, bot, sb, in, sep;
43 #define RINGBUFFER_SIZE 512
49 static struct ringbuffer *bot_win_rb;
50 #define NUM_LINES(len) (1 + (len) / bot.cols)
52 static unsigned scroll_position;
54 static bool curses_active;
57 static int command_fds[2] = {-1, -1};
58 static int stat_pipe = -1;
59 static struct gui_args_info conf;
61 enum {GETCH_MODE, COMMAND_MODE, EXTERNAL_MODE};
65 * Codes for various colors.
67 * Each status item has its own color pair. The ones defined here start at a
68 * higher number so that they do not overlap with these.
71 COLOR_STATUSBAR = NUM_STAT_ITEMS + 1,
85 const char *description;
86 void (*handler)(void);
89 static struct gui_theme theme;
94 static void com_help(void);
95 static void com_reread_conf(void);
96 static void com_enlarge_top_win(void);
97 static void com_shrink_top_win(void);
98 static void com_version(void);
99 __noreturn static void com_quit(void);
100 static void com_refresh(void);
101 static void com_ll_incr(void);
102 static void com_ll_decr(void);
103 static void com_prev_theme(void);
104 static void com_next_theme(void);
105 static void com_scroll_up(void);
106 static void com_scroll_down(void);
107 static void com_page_up(void);
108 static void com_page_down(void);
109 static void com_cancel_scrolling(void);
110 static void com_scroll_top(void);
112 static struct gui_command command_list[] = {
116 .description = "print help",
120 .name = "enlarge_win",
121 .description = "enlarge the top window",
122 .handler = com_enlarge_top_win
125 .name = "shrink_win",
126 .description = "shrink the top window",
127 .handler = com_shrink_top_win
130 .name = "reread_conf",
131 .description = "reread configuration file",
132 .handler = com_reread_conf
136 .description = "exit para_gui",
141 .description = "redraw the screen",
142 .handler = com_refresh
145 .name = "next_theme",
146 .description = "switch to next theme",
147 .handler = com_next_theme
150 .name = "prev_theme",
151 .description = "switch to previous stream",
152 .handler = com_prev_theme
156 .description = "increase loglevel (decreases verbosity)",
157 .handler = com_ll_incr
161 .description = "decrease loglevel (increases verbosity)",
162 .handler = com_ll_decr
166 .description = "show the para_gui version",
167 .handler = com_version
171 .description = "scroll up one line",
172 .handler = com_scroll_up
175 .name = "scroll_down",
176 .description = "scroll down one line",
177 .handler = com_scroll_down
181 .description = "scroll up one page",
182 .handler = com_page_up
186 .description = "scroll down one page",
187 .handler = com_page_down
190 .name = "scroll_top",
191 .description = "scroll to top of buffer",
192 .handler = com_scroll_top
195 .name = "cancel_scroll",
196 .description = "deactivate scroll mode",
197 .handler = com_cancel_scrolling
203 static int find_cmd_byname(char *name)
207 for (i = 0; command_list[i].handler; i++)
208 if (!strcmp(command_list[i].name, name))
213 /* taken from mutt */
214 static char *km_keyname(int c)
219 sprintf(buf, "<up>");
223 sprintf(buf, "<down>");
227 sprintf(buf, "<left>");
230 if (c == KEY_RIGHT) {
231 sprintf(buf, "<right>");
234 if (c == KEY_NPAGE) {
235 sprintf(buf, "<npage>");
238 if (c == KEY_PPAGE) {
239 sprintf(buf, "<ppage>");
243 sprintf(buf, "<home>");
247 sprintf(buf, "<end>");
250 if (c < 256 && c > -128 && iscntrl((unsigned char) c)) {
255 buf[1] = (c + '@') & 0x7f;
258 snprintf(buf, sizeof(buf), "\\%d%d%d", c >> 6,
259 (c >> 3) & 7, c & 7);
260 } else if (c >= KEY_F0 && c < KEY_F(256))
261 sprintf(buf, "<F%d>", c - KEY_F0);
263 snprintf(buf, sizeof(buf), "%c", (unsigned char) c);
265 snprintf(buf, sizeof(buf), "\\x%hx", (unsigned short) c);
269 static char *configfile_exists(void)
271 static char *config_file;
274 if (!conf.config_file_given) {
276 char *home = para_homedir();
277 config_file = make_message("%s/.paraslash/gui.conf",
283 tmp = conf.config_file_arg;
284 return file_exists(tmp)? tmp: NULL;
287 /* Print given number of spaces to curses window. */
288 static void add_spaces(WINDOW* win, unsigned int num)
291 unsigned sz = sizeof(space) - 1; /* number of spaces */
305 * print aligned string to curses window. This function always prints
308 static int align_str(WINDOW* win, char *str, unsigned int len,
311 int ret, i, num; /* of spaces */
316 ret = strwidth(str, &width);
318 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
327 /* replace control characters by spaces */
328 for (i = 0; i < len && str[i]; i++) {
329 if (str[i] == '\n' || str[i] == '\r' || str[i] == '\f')
334 add_spaces(win, num);
335 } else if (align == RIGHT) {
336 add_spaces(win, num);
339 add_spaces(win, num / 2);
340 waddstr(win, str[0]? str: "");
341 add_spaces(win, num - num / 2);
346 __printf_2_3 static void print_in_bar(int color, const char *fmt,...)
353 wattron(in.win, COLOR_PAIR(color));
355 xvasprintf(&msg, fmt, ap);
358 align_str(in.win, msg, in.cols, LEFT);
364 * update the status bar
366 static void print_status_bar(void)
372 tmp = para_strdup("para_gui " PACKAGE_VERSION " (hit ? for help)");
374 align_str(sb.win, tmp, sb.cols, CENTER);
380 * get the number of the oldest rbe that is (partially) visible. On return,
381 * lines contains the sum of the number of lines of all visible entries. If the
382 * first one is only partially visible, lines is greater than bot.lines.
384 static int first_visible_rbe(unsigned *lines)
388 for (i = scroll_position; i < RINGBUFFER_SIZE; i++) {
389 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
393 // fprintf(stderr, "found: %s\n", rbe->msg);
394 rbe_lines = NUM_LINES(rbe->len);
395 if (rbe_lines > bot.lines)
398 if (*lines >= bot.lines)
401 return RINGBUFFER_SIZE - 1;
405 returns number of first visible rbe, *lines is the number of lines drawn.
407 static int draw_top_rbe(unsigned *lines)
409 int ret, fvr = first_visible_rbe(lines);
410 struct rb_entry *rbe;
411 size_t bytes_to_skip, cells_to_skip, width;
415 wmove(bot.win, 0, 0);
416 rbe = ringbuffer_get(bot_win_rb, fvr);
419 if (*lines > bot.lines) {
420 /* rbe is partially visible multi-line */
421 cells_to_skip = (*lines - bot.lines) * bot.cols;
422 ret = skip_cells(rbe->msg, cells_to_skip, &bytes_to_skip);
425 ret = strwidth(rbe->msg + bytes_to_skip, &width);
432 wattron(bot.win, COLOR_PAIR(rbe->color));
433 waddstr(bot.win, rbe->msg + bytes_to_skip);
434 *lines = NUM_LINES(width);
438 static void redraw_bot_win(void)
443 wmove(bot.win, 0, 0);
445 i = draw_top_rbe(&lines);
448 while (i > 0 && lines < bot.lines) {
449 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, --i);
452 waddstr(bot.win, "\n");
455 lines += NUM_LINES(rbe->len);
456 wattron(bot.win, COLOR_PAIR(rbe->color));
457 waddstr(bot.win, "\n");
458 waddstr(bot.win, rbe->msg);
464 static void rb_add_entry(int color, char *msg)
466 struct rb_entry *old, *new;
470 if (strwidth(msg, &len) < 0)
472 new = para_malloc(sizeof(struct rb_entry));
476 old = ringbuffer_add(bot_win_rb, new);
477 // fprintf(stderr, "added: %s\n", new->msg);
482 if (scroll_position) {
483 /* discard current scrolling, like xterm does */
488 wattron(bot.win, COLOR_PAIR(color));
489 getyx(bot.win, y, x);
491 waddstr(bot.win, "\n");
492 waddstr(bot.win, msg);
496 * print formated output to bot win and refresh
498 __printf_2_3 static void outputf(int color, const char* fmt,...)
506 xvasprintf(&msg, fmt, ap);
508 rb_add_entry(color, msg);
512 static int add_output_line(char *line, void *data)
514 int color = *(int *)data? COLOR_ERRMSG : COLOR_OUTPUT;
518 rb_add_entry(color, para_strdup(line));
524 static __printf_2_3 void curses_log(int ll, const char *fmt,...)
530 if (ll < loglevel || !curses_active)
539 color = COLOR_ERRMSG;
542 xvasprintf(&msg, fmt, ap);
545 rb_add_entry(color, msg);
548 __printf_2_3 void (*para_log)(int, const char*, ...) = curses_log;
550 static void setup_signal_handling(void)
552 signal_pipe = para_signal_init();
553 para_install_sighandler(SIGINT);
554 para_install_sighandler(SIGTERM);
555 para_install_sighandler(SIGCHLD);
556 para_install_sighandler(SIGWINCH);
557 para_install_sighandler(SIGUSR1);
560 /* kill every process in the process group and exit */
561 __noreturn static void kill_pg_and_die(int ret)
563 para_sigaction(SIGTERM, SIG_IGN);
568 static void shutdown_curses(void)
573 curses_active = false;
577 __noreturn static void finish(int ret)
580 kill_pg_and_die(ret);
584 * exit curses and print given message to stdout/stderr
586 __noreturn __printf_2_3 static void msg_n_exit(int ret, const char* fmt, ...)
589 FILE *outfd = ret? stderr: stdout;
593 vfprintf(outfd, fmt, argp);
595 kill_pg_and_die(ret);
598 static void print_welcome(void)
600 if (loglevel > LL_NOTICE)
602 outputf(COLOR_WELCOME, "Welcome to %s. Theme: %s",
603 version_single_line("gui"), theme.name);
610 static void init_wins(int top_lines)
614 top.lines = top_lines;
619 bot.lines = LINES - top.lines - 3;
621 bot.begy = top.lines + 1;
636 sep.begy = top.lines;
639 assume_default_colors(theme.default_fg, theme.default_bg);
641 mvwin(top.win, top.begy, top.begx);
642 wresize(top.win, top.lines, top.cols);
644 mvwin(sb.win, sb.begy, sb.begx);
645 wresize(sb.win, sb.lines, sb.cols);
647 mvwin(sep.win, sep.begy, sep.begx);
648 wresize(sep.win, sep.lines, sep.cols);
650 mvwin(bot.win, bot.begy, bot.begx);
651 wresize(bot.win, bot.lines, bot.cols);
653 mvwin(in.win, in.begy, in.begx);
654 wresize(in.win, in.lines, in.cols);
656 sep.win = newwin(sep.lines, sep.cols, sep.begy, sep.begx);
657 top.win = newwin(top.lines, top.cols, top.begy, top.begx);
658 bot.win = newwin(bot.lines, bot.cols, bot.begy, bot.begx);
659 sb.win = newwin(sb.lines, sb.cols, sb.begy, sb.begx);
660 in.win = newwin(in.lines, in.cols, in.begy, in.begx);
661 if (!top.win || !bot.win || !sb.win || !in.win || !sep.win)
662 msg_n_exit(1, "Error: Cannot create curses windows\n");
666 scrollok(bot.win, 1);
667 wattron(sb.win, COLOR_PAIR(COLOR_STATUSBAR));
668 wattron(sep.win, COLOR_PAIR(COLOR_SEPARATOR));
669 wattron(bot.win, COLOR_PAIR(COLOR_BOT));
670 wattron(top.win, COLOR_PAIR(COLOR_TOP));
681 wmove(sep.win, 0, 0);
682 for (i = 1; i <= COLS; i++)
683 waddstr(sep.win, theme.sep_str);
686 wnoutrefresh(top.win);
687 wnoutrefresh(bot.win);
688 //wnoutrefresh(sb.win);
690 wnoutrefresh(in.win);
691 wnoutrefresh(sep.win);
696 * Print stat item #i to curses window
698 static void print_stat_item(int i)
701 struct stat_item_data d = theme.data[i];
702 char *c = stat_content[i];
704 if (!curses_active || !d.len || !c)
706 tmp = make_message("%s%s%s", d.prefix, c, d.postfix);
707 wmove(top.win, d.y * top.lines / 100, d.x * COLS / 100);
709 wattron(top.win, COLOR_PAIR(i + 1));
710 align_str(top.win, tmp, d.len * COLS / 100, d.align);
715 static int update_item(int item_num, char *buf)
717 char **c = stat_content + item_num;
724 *c = para_strdup("(artist tag not set)");
727 *c = para_strdup("(title tag not set)");
730 *c = para_strdup("????");
733 *c = para_strdup("(album tag not set)");
736 *c = para_strdup("(comment tag not set)");
740 *c = para_strdup(buf);
742 print_stat_item(item_num);
746 static int read_stat_pipe(fd_set *rfds)
749 static int bufsize, loaded;
755 if (loaded >= bufsize) {
756 if (bufsize > 1000 * 1000) {
760 bufsize += bufsize + 1000;
761 buf = para_realloc(buf, bufsize);
763 assert(loaded < bufsize);
764 ret = read_nonblock(stat_pipe, buf + loaded, bufsize - loaded,
767 ret2 = for_each_stat_item(buf, loaded, update_item);
768 if (ret < 0 || ret2 < 0) {
770 return ret2 < 0? ret2 : ret;
772 sz = ret2; /* what is left */
773 if (sz > 0 && sz < loaded)
774 memmove(buf, buf + loaded - sz, sz);
779 static void print_all_items(void)
785 FOR_EACH_STATUS_ITEM(i)
789 static void clear_all_items(void)
793 FOR_EACH_STATUS_ITEM(i) {
794 free(stat_content[i]);
795 stat_content[i] = para_strdup("");
799 static void init_pair_or_die(short pair, short f, short b)
801 if (init_pair(pair, f, b) == ERR)
802 msg_n_exit(EXIT_FAILURE, "fatal: init_pair() failed\n");
805 static void init_colors_or_die(void)
810 msg_n_exit(EXIT_FAILURE, "fatal: No color term\n");
811 if (start_color() == ERR)
812 msg_n_exit(EXIT_FAILURE, "fatal: failed to start colors\n");
813 FOR_EACH_STATUS_ITEM(i)
814 if (theme.data[i].len)
815 init_pair_or_die(i + 1, theme.data[i].fg,
817 init_pair_or_die(COLOR_STATUSBAR, theme.sb_fg, theme.sb_bg);
818 init_pair_or_die(COLOR_COMMAND, theme.cmd_fg, theme.cmd_bg);
819 init_pair_or_die(COLOR_OUTPUT, theme.output_fg, theme.output_bg);
820 init_pair_or_die(COLOR_MSG, theme.msg_fg, theme.msg_bg);
821 init_pair_or_die(COLOR_ERRMSG, theme.err_msg_fg, theme.err_msg_bg);
822 init_pair_or_die(COLOR_WELCOME, theme.welcome_fg, theme.welcome_bg);
823 init_pair_or_die(COLOR_SEPARATOR, theme.sep_fg, theme.sep_bg);
824 init_pair_or_die(COLOR_TOP, theme.default_fg, theme.default_bg);
825 init_pair_or_die(COLOR_BOT, theme.default_fg, theme.default_bg);
828 /* (Re-)initialize the curses library. */
829 static void init_curses(void)
831 curses_active = true;
832 if (top.win && refresh() == ERR) /* refresh is really needed */
833 msg_n_exit(EXIT_FAILURE, "refresh() failed\n");
834 if (LINES < theme.lines_min || COLS < theme.cols_min)
835 msg_n_exit(EXIT_FAILURE, "Error: Terminal (%dx%d) too small"
836 " (need at least %dx%d)\n", COLS, LINES,
837 theme.cols_min, theme.lines_min);
838 curs_set(0); /* make cursor invisible, ignore errors */
839 nonl(); /* do not NL->CR/NL on output, always returns OK */
840 /* don't echo input */
842 msg_n_exit(EXIT_FAILURE, "fatal: noecho() failed\n");
843 /* take input chars one at a time, no wait for \n */
845 msg_n_exit(EXIT_FAILURE, "fatal: cbreak() failed\n");
846 init_colors_or_die();
847 clear(); /* ignore non-fatal errors */
848 init_wins(theme.top_lines_default);
850 // noecho(); /* don't echo input */
853 static void check_sigchld(void)
859 ret = para_reap_child(&pid);
864 goto reap_next_child;
868 * This sucker modifies its first argument. *handler and *arg are
869 * pointers to 0-terminated strings (inside line). Crap.
871 static int split_key_map(char *line, char **handler, char **arg)
873 if (!(*handler = strchr(line + 1, ':')))
877 if (!(*arg = strchr(*handler, ':')))
886 static int check_key_map_args(void)
890 char *tmp = NULL, *handler, *arg;
892 for (i = 0; i < conf.key_map_given; ++i) {
893 s = conf.key_map_arg[i];
897 tmp = para_strdup(s);
898 if (!split_key_map(tmp, &handler, &arg))
900 if (strlen(handler) != 1)
909 if (find_cmd_byname(arg) < 0)
919 * React to various signal-related events
921 static void handle_signal(int sig)
925 msg_n_exit(EXIT_FAILURE,
926 "only the good die young (caught SIGTERM))\n");
936 PARA_WARNING_LOG("caught SIGINT, reset\n");
937 /* Nothing to do. SIGINT killed our child which gets noticed
938 * by do_select and resets everything.
942 PARA_NOTICE_LOG("got SIGUSR1, rereading configuration\n");
951 static void status_pre_select(fd_set *rfds, int *max_fileno, struct timeval *tv)
953 static struct timeval next_exec, atm, diff;
954 int ret, fds[3] = {0, 1, 0};
959 /* Avoid busy loop */
960 gettimeofday(&atm, NULL);
961 if (tv_diff(&next_exec, &atm, &diff) > 0) {
962 if (tv_diff(&diff, tv, NULL) < 0)
966 next_exec.tv_sec = atm.tv_sec + 2;
967 ret = para_exec_cmdline_pid(&pid, conf.stat_cmd_arg, fds);
970 ret = mark_fd_nonblocking(fds[1]);
977 para_fd_set(stat_pipe, rfds, max_fileno);
980 #define COMMAND_BUF_SIZE 32768
983 * This is the core select loop. Besides the (internal) signal
984 * pipe, the following other fds are checked according to the mode:
986 * GETCH_MODE: check stdin, return when key is pressed
988 * COMMAND_MODE: check command fds and stdin. Return when peer has closed both
989 * stdout and stderr or when any key is pressed.
991 * EXTERNAL_MODE: Check only signal pipe. Used when an external command
992 * is running. During that time curses is disabled. Returns when
995 static int do_select(int mode)
998 int ret, i, max_fileno;
999 char command_buf[2][COMMAND_BUF_SIZE] = {"", ""};
1000 int cbo[2] = {0, 0}; /* command buf offsets */
1002 unsigned flags[2] = {0, 0}; /* for for_each_line() */
1005 tv.tv_sec = conf.timeout_arg / 1000;
1006 tv.tv_usec = (conf.timeout_arg % 1000) * 1000;
1007 // ret = refresh_status();
1010 status_pre_select(&rfds, &max_fileno, &tv);
1012 para_fd_set(signal_pipe, &rfds, &max_fileno);
1013 /* command pipe only for COMMAND_MODE */
1014 if (mode == COMMAND_MODE) {
1015 if (command_fds[0] >= 0)
1016 para_fd_set(command_fds[0], &rfds, &max_fileno);
1017 if (command_fds[1] >= 0)
1018 para_fd_set(command_fds[1], &rfds, &max_fileno);
1020 if (mode == GETCH_MODE || mode == COMMAND_MODE)
1021 para_fd_set(STDIN_FILENO, &rfds, &max_fileno);
1022 ret = para_select(max_fileno + 1, &rfds, NULL, &tv);
1024 goto check_return; /* skip fd checks */
1026 ret = para_next_signal(&rfds);
1029 /* read command pipe if ready */
1030 if (mode == COMMAND_MODE) {
1031 for (i = 0; i < 2; i++) {
1033 if (command_fds[i] < 0)
1035 ret = read_nonblock(command_fds[i],
1036 command_buf[i] + cbo[i],
1037 COMMAND_BUF_SIZE - 1 - cbo[i], &rfds, &sz);
1040 cbo[i] = for_each_line(flags[i], command_buf[i], cbo[i],
1041 add_output_line, &i);
1042 if (sz != cbo[i]) { /* at least one line found */
1047 PARA_NOTICE_LOG("closing command fd %d: %s\n",
1048 i, para_strerror(-ret));
1049 close(command_fds[i]);
1050 command_fds[i] = -1;
1053 if (command_fds[!i] < 0) /* both fds closed */
1056 if (cbo[i] == COMMAND_BUF_SIZE - 1) {
1057 PARA_NOTICE_LOG("discarding overlong line\n");
1059 flags[i] = FELF_DISCARD_FIRST;
1063 ret = read_stat_pipe(&rfds);
1065 PARA_NOTICE_LOG("closing stat pipe: %s\n", para_strerror(-ret));
1069 free(stat_content[SI_BASENAME]);
1070 stat_content[SI_BASENAME] =
1071 para_strdup("stat command terminated!?");
1077 ret = wgetch(top.win);
1078 if (ret != ERR && ret != KEY_RESIZE) {
1080 kill(cmd_pid, SIGTERM);
1085 ret = wgetch(top.win);
1086 if (ret != ERR && ret != KEY_RESIZE)
1096 /* read from command pipe and print data to bot window */
1097 static void exec_and_display_cmd(const char *cmd)
1099 int ret, fds[3] = {0, 1, 1};
1101 outputf(COLOR_COMMAND, "%s", cmd);
1102 ret = para_exec_cmdline_pid(&cmd_pid, cmd, fds);
1105 ret = mark_fd_nonblocking(fds[1]);
1108 ret = mark_fd_nonblocking(fds[2]);
1111 command_fds[0] = fds[1];
1112 command_fds[1] = fds[2];
1113 if (do_select(COMMAND_MODE) >= 0)
1114 PARA_INFO_LOG("command complete\n");
1116 PARA_NOTICE_LOG("command aborted\n");
1117 print_in_bar(COLOR_MSG, " ");
1120 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
1121 close(command_fds[0]);
1122 close(command_fds[1]);
1125 static void para_cmd(char *cmd)
1129 print_in_bar(COLOR_MSG, "executing client command, hit any key to abort\n");
1130 c = make_message(BINDIR "/para_client -- %s", cmd);
1131 exec_and_display_cmd(c);
1135 static void display_cmd(char *cmd)
1137 print_in_bar(COLOR_MSG, "executing display command, hit any key to abort");
1138 exec_and_display_cmd(cmd);
1142 * shutdown curses and stat pipe before executing external commands
1144 static void external_cmd(char *cmd)
1146 int fds[3] = {-1, -1, -1};
1151 if (para_exec_cmdline_pid(&cmd_pid, cmd, fds) < 0)
1153 do_select(EXTERNAL_MODE);
1157 static void print_scroll_msg(void)
1159 unsigned lines_total, filled = ringbuffer_filled(bot_win_rb);
1160 int first_rbe = first_visible_rbe(&lines_total);
1162 print_in_bar(COLOR_MSG, "scrolled view: %d-%d/%d\n", filled - first_rbe,
1163 filled - scroll_position, ringbuffer_filled(bot_win_rb));
1166 static void com_scroll_top(void)
1168 int i = RINGBUFFER_SIZE - 1;
1171 while (i > 0 && !ringbuffer_get(bot_win_rb, i))
1173 /* i is oldest entry */
1174 for (; lines < bot.lines && i >= 0; i--) {
1175 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
1178 lines += NUM_LINES(rbe->len);
1181 if (lines > 0 && scroll_position != i) {
1182 scroll_position = i;
1187 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1190 static void com_cancel_scrolling(void)
1193 if (scroll_position == 0) {
1194 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1197 scroll_position = 0;
1201 static void com_page_down(void)
1204 int i = scroll_position;
1206 while (lines < bot.lines && --i > 0) {
1207 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
1210 lines += NUM_LINES(rbe->len);
1213 scroll_position = i;
1218 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1221 static void com_page_up(void)
1224 int fvr = first_visible_rbe(&lines);
1226 if (fvr < 0 || fvr + 1 >= ringbuffer_filled(bot_win_rb)) {
1227 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1230 scroll_position = fvr + 1;
1231 for (; scroll_position > 0; scroll_position--) {
1232 first_visible_rbe(&lines);
1233 if (lines == bot.lines)
1240 static void com_scroll_down(void)
1242 struct rb_entry *rbe;
1245 if (!scroll_position) {
1246 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1250 rbe = ringbuffer_get(bot_win_rb, scroll_position);
1251 rbe_lines = NUM_LINES(rbe->len);
1252 wscrl(bot.win, rbe_lines);
1253 wmove(bot.win, bot.lines - rbe_lines, 0);
1254 wattron(bot.win, COLOR_PAIR(rbe->color));
1255 waddstr(bot.win, rbe->msg);
1260 static void com_scroll_up(void)
1262 struct rb_entry *rbe = NULL;
1264 int i, first_rbe, num_scroll;
1266 /* the entry that is going to vanish */
1267 rbe = ringbuffer_get(bot_win_rb, scroll_position);
1270 num_scroll = NUM_LINES(rbe->len);
1271 first_rbe = first_visible_rbe(&lines);
1272 if (first_rbe < 0 || (first_rbe == ringbuffer_filled(bot_win_rb) - 1))
1275 wscrl(bot.win, -num_scroll);
1276 i = draw_top_rbe(&lines);
1279 while (i > 0 && lines < num_scroll) {
1281 rbe = ringbuffer_get(bot_win_rb, --i);
1284 rbe_lines = NUM_LINES(rbe->len);
1286 // fprintf(stderr, "msg: %s\n", rbe->msg);
1287 wattron(bot.win, COLOR_PAIR(rbe->color));
1288 waddstr(bot.win, "\n");
1289 waddstr(bot.win, rbe->msg);
1298 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1301 static void com_ll_decr(void)
1303 if (loglevel <= LL_DEBUG) {
1304 print_in_bar(COLOR_ERRMSG,
1305 "loglevel already at maximal verbosity\n");
1309 print_in_bar(COLOR_MSG, "loglevel set to %d\n", loglevel);
1312 static void com_ll_incr(void)
1314 if (loglevel >= LL_EMERG) {
1315 print_in_bar(COLOR_ERRMSG,
1316 "loglevel already at minimal verbosity\n");
1320 print_in_bar(COLOR_MSG, "loglevel set to %d\n", loglevel);
1324 * reread configuration, terminate on errors
1326 static void com_reread_conf(void)
1328 char *cf = configfile_exists();
1329 struct gui_cmdline_parser_params params = {
1332 .check_required = 0,
1333 .check_ambiguity = 0,
1338 PARA_WARNING_LOG("there is no configuration to read\n");
1341 PARA_INFO_LOG("rereading command line options and config file\n");
1342 gui_cmdline_parser_ext(_argc, _argv, &conf, ¶ms);
1344 * Despite .print_errors is set to 0, gengetopt will print to stderr
1345 * anyway, and exit on errors. So we have to shutdown curses first.
1348 gui_cmdline_parser_config_file(cf, &conf, ¶ms);
1350 PARA_NOTICE_LOG("config file reloaded\n");
1351 if (check_key_map_args() < 0)
1352 finish(EXIT_FAILURE);
1355 static void com_help(void)
1359 for (i = 0; i < conf.key_map_given; ++i) {
1360 char *handler, *arg, *tmp = para_strdup(conf.key_map_arg[i]);
1361 const char *handler_text = "???", *desc = NULL;
1363 if (!split_key_map(tmp, &handler, &arg)) {
1369 handler_text = "internal";
1370 desc = command_list[find_cmd_byname(arg)].description;
1372 case 'x': handler_text = "external"; break;
1373 case 'd': handler_text = "display "; break;
1374 case 'p': handler_text = "para "; break;
1376 outputf(COLOR_MSG, "%s\t%s\t%s%s\t%s", tmp, handler_text, arg,
1377 strlen(arg) < 8? "\t" : "",
1381 for (i = 0; command_list[i].handler; i++) {
1382 struct gui_command gc = command_list[i];
1384 outputf(COLOR_MSG, "%s\tinternal\t%s\t%s%s", gc.key, gc.name,
1385 strlen(gc.name) < 8? "\t" : "",
1388 print_in_bar(COLOR_MSG, "try \"para_gui -h\" or \"para_client help\" "
1392 static void com_shrink_top_win(void)
1394 if (top.lines <= theme.top_lines_min) {
1395 PARA_WARNING_LOG("can not decrease top window\n");
1398 init_wins(top.lines - 1);
1401 print_in_bar(COLOR_MSG, "%s", "decreased top window");
1404 static void com_enlarge_top_win(void)
1406 if (bot.lines < 3) {
1407 PARA_WARNING_LOG("can not increase top window\n");
1410 init_wins(top.lines + 1);
1413 print_in_bar(COLOR_MSG, "increased top window");
1416 static void com_version(void)
1418 print_in_bar(COLOR_MSG, "%s", version_single_line("gui"));
1421 __noreturn static void com_quit(void)
1426 static void com_refresh(void)
1432 static void change_theme(int next)
1438 /* This seems to be needed twice, why? */
1441 PARA_NOTICE_LOG("new theme: %s\n", theme.name);
1444 static void com_next_theme(void)
1449 static void com_prev_theme(void)
1455 static void handle_command(int c)
1459 /* first check user-defined key bindings */
1460 for (i = 0; i < conf.key_map_given; ++i) {
1461 char *tmp, *handler, *arg;
1463 tmp = para_strdup(conf.key_map_arg[i]);
1464 if (!split_key_map(tmp, &handler, &arg)) {
1468 if (strcmp(tmp, km_keyname(c))) {
1472 if (*handler == 'd')
1474 else if (*handler == 'x')
1476 else if (*handler == 'p')
1478 else if (*handler == 'i') {
1479 int num = find_cmd_byname(arg);
1481 command_list[num].handler();
1486 /* not found, check internal key bindings */
1487 for (i = 0; command_list[i].handler; i++) {
1488 if (!strcmp(km_keyname(c), command_list[i].key)) {
1489 command_list[i].handler();
1493 print_in_bar(COLOR_ERRMSG, "key '%s' is not bound, press ? for help",
1497 __noreturn static void print_help_and_die(void)
1499 struct ggo_help h = DEFINE_GGO_HELP(gui);
1500 bool d = conf.detailed_help_given;
1502 ggo_print_help(&h, d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS);
1506 int main(int argc, char *argv[])
1514 gui_cmdline_parser(argc, argv, &conf); /* exits on errors */
1515 loglevel = get_loglevel_by_name(conf.loglevel_arg);
1516 version_handle_flag("gui", conf.version_given);
1517 if (conf.help_given || conf.detailed_help_given)
1518 print_help_and_die();
1519 cf = configfile_exists();
1520 if (!cf && conf.config_file_given) {
1521 fprintf(stderr, "can not read config file %s\n",
1522 conf.config_file_arg);
1526 struct gui_cmdline_parser_params params = {
1529 .check_required = 0,
1530 .check_ambiguity = 0,
1533 gui_cmdline_parser_config_file(cf, &conf, ¶ms);
1534 loglevel = get_loglevel_by_name(conf.loglevel_arg);
1536 if (check_key_map_args() < 0) {
1537 fprintf(stderr, "invalid key map\n");
1540 init_theme_or_die(conf.theme_arg, &theme);
1541 setup_signal_handling();
1542 bot_win_rb = ringbuffer_new(RINGBUFFER_SIZE);
1543 setlocale(LC_CTYPE, "");
1544 initscr(); /* needed only once, always successful */
1549 ret = do_select(GETCH_MODE);
1552 print_in_bar(COLOR_MSG, " ");
1553 handle_command(ret);