2 * Copyright (C) 1998-2012 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>
15 #include "gui.cmdline.h"
19 #include "ringbuffer.h"
27 /** define the array of error lists needed by para_gui */
29 static char *stat_content[NUM_STAT_ITEMS];
31 #define STANDARD_STATUS_BAR "para_gui " PACKAGE_VERSION " (hit ? for help)"
33 static int signal_pipe;
35 static struct win_data {
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 int cmd_died, curses_active;
57 static int command_fds[2];
58 static int stat_pipe = -1;
59 static struct gui_args_info conf;
61 enum {GETCH_MODE, COMMAND_MODE, EXTERNAL_MODE};
64 #define COLOR_STATUSBAR 52
65 #define COLOR_COMMAND 53
66 #define COLOR_OUTPUT 54
68 #define COLOR_ERRMSG 56
69 #define COLOR_WELCOME 57
70 #define COLOR_SEPARATOR 58
77 const char *description;
78 void (*handler)(void);
84 char postfix[MAXLINE];
90 char content[MAXLINE];
93 static struct gui_theme theme;
98 static void com_help(void);
99 static void com_reread_conf(void);
100 static void com_enlarge_top_win(void);
101 static void com_shrink_top_win(void);
102 static void com_version(void);
103 __noreturn static void com_quit(void);
104 static void com_refresh(void);
105 static void com_ll_incr(void);
106 static void com_ll_decr(void);
107 static void com_prev_theme(void);
108 static void com_next_theme(void);
109 static void com_scroll_up(void);
110 static void com_scroll_down(void);
111 static void com_page_up(void);
112 static void com_page_down(void);
113 static void com_cancel_scrolling(void);
114 static void com_scroll_top(void);
116 static struct gui_command command_list[] = {
120 .description = "print help",
124 .name = "enlarge_win",
125 .description = "enlarge the top window",
126 .handler = com_enlarge_top_win
129 .name = "shrink_win",
130 .description = "shrink the top window",
131 .handler = com_shrink_top_win
134 .name = "reread_conf",
135 .description = "reread configuration file",
136 .handler = com_reread_conf
140 .description = "exit para_gui",
145 .description = "redraw the screen",
146 .handler = com_refresh
149 .name = "next_theme",
150 .description = "switch to next theme",
151 .handler = com_next_theme
154 .name = "prev_theme",
155 .description = "switch to previous stream",
156 .handler = com_prev_theme
160 .description = "increase loglevel (decreases verbosity)",
161 .handler = com_ll_incr
165 .description = "decrease loglevel (increases verbosity)",
166 .handler = com_ll_decr
170 .description = "show the para_gui version",
171 .handler = com_version
175 .description = "scroll up one line",
176 .handler = com_scroll_up
179 .name = "scroll_down",
180 .description = "scroll down one line",
181 .handler = com_scroll_down
185 .description = "scroll up one page",
186 .handler = com_page_up
190 .description = "scroll down one page",
191 .handler = com_page_down
194 .name = "scroll_top",
195 .description = "scroll to top of buffer",
196 .handler = com_scroll_top
199 .name = "cancel_scroll",
200 .description = "deactivate scroll mode",
201 .handler = com_cancel_scrolling
207 static int find_cmd_byname(char *name)
211 for (i = 0; command_list[i].handler; i++)
212 if (!strcmp(command_list[i].name, name))
217 /* taken from mutt */
218 static char *km_keyname(int c)
223 sprintf(buf, "<up>");
227 sprintf(buf, "<down>");
231 sprintf(buf, "<left>");
234 if (c == KEY_RIGHT) {
235 sprintf(buf, "<right>");
238 if (c == KEY_NPAGE) {
239 sprintf(buf, "<npage>");
242 if (c == KEY_PPAGE) {
243 sprintf(buf, "<ppage>");
247 sprintf(buf, "<home>");
251 sprintf(buf, "<end>");
254 if (c < 256 && c > -128 && iscntrl((unsigned char) c)) {
259 buf[1] = (c + '@') & 0x7f;
262 snprintf(buf, sizeof(buf), "\\%d%d%d", c >> 6,
263 (c >> 3) & 7, c & 7);
264 } else if (c >= KEY_F0 && c < KEY_F(256))
265 sprintf(buf, "<F%d>", c - KEY_F0);
267 snprintf(buf, sizeof(buf), "%c", (unsigned char) c);
269 snprintf(buf, sizeof(buf), "\\x%hx", (unsigned short) c);
273 static char *configfile_exists(void)
275 static char *config_file;
278 if (!conf.config_file_given) {
280 char *home = para_homedir();
281 config_file = make_message("%s/.paraslash/gui.conf",
287 tmp = conf.config_file_arg;
288 return file_exists(tmp)? tmp: NULL;
292 * print num spaces to curses window
294 static void add_spaces(WINDOW* win, unsigned int num)
303 * print aligned string to curses window. This function always prints
306 static int align_str(WINDOW* win, char *str, unsigned int len,
309 int ret, i, num; /* of spaces */
314 ret = strwidth(str, &width);
316 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
325 /* replace control characters by spaces */
326 for (i = 0; i < len && str[i]; i++) {
327 if (str[i] == '\n' || str[i] == '\r' || str[i] == '\f')
332 add_spaces(win, num);
333 } else if (align == RIGHT) {
334 add_spaces(win, num);
337 add_spaces(win, num / 2);
338 waddstr(win, str[0]? str: "");
339 add_spaces(win, num - num / 2);
344 __printf_2_3 static void print_in_bar(int color, const char *fmt,...)
351 wattron(in.win, COLOR_PAIR(color));
353 xvasprintf(&msg, fmt, ap);
356 align_str(in.win, msg, sb.cols, LEFT);
362 * update the status bar
364 static void print_status_bar(void)
370 tmp = para_strdup(STANDARD_STATUS_BAR);
372 align_str(sb.win, tmp, sb.cols, CENTER);
378 * get the number of the oldest rbe that is (partially) visible. On return,
379 * lines contains the sum of the number of lines of all visable entries. If the
380 * first one is only partially visible, lines is greater than bot.lines.
382 static int first_visible_rbe(unsigned *lines)
386 for (i = scroll_position; i < RINGBUFFER_SIZE; i++) {
387 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
391 // fprintf(stderr, "found: %s\n", rbe->msg);
392 rbe_lines = NUM_LINES(rbe->len);
393 if (rbe_lines > bot.lines)
396 if (*lines >= bot.lines)
399 return RINGBUFFER_SIZE - 1;
403 returns number of first visible rbe, *lines is the number of lines drawn.
405 static int draw_top_rbe(unsigned *lines)
407 int ret, fvr = first_visible_rbe(lines);
408 struct rb_entry *rbe;
409 size_t bytes_to_skip, cells_to_skip, width;
413 wmove(bot.win, 0, 0);
414 rbe = ringbuffer_get(bot_win_rb, fvr);
417 if (*lines > bot.lines) {
418 /* rbe is partially visible multi-line */
419 cells_to_skip = (*lines - bot.lines) * bot.cols;
420 ret = skip_cells(rbe->msg, cells_to_skip, &bytes_to_skip);
423 ret = strwidth(rbe->msg + bytes_to_skip, &width);
430 wattron(bot.win, COLOR_PAIR(rbe->color));
431 waddstr(bot.win, rbe->msg + bytes_to_skip);
432 *lines = NUM_LINES(width);
436 static void redraw_bot_win(void)
441 wmove(bot.win, 0, 0);
443 i = draw_top_rbe(&lines);
446 while (i > 0 && lines < bot.lines) {
447 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, --i);
450 waddstr(bot.win, "\n");
453 lines += NUM_LINES(rbe->len);
454 wattron(bot.win, COLOR_PAIR(rbe->color));
455 waddstr(bot.win, "\n");
456 waddstr(bot.win, rbe->msg);
462 static void rb_add_entry(int color, char *msg)
464 struct rb_entry *old, *new;
468 if (strwidth(msg, &len) < 0)
470 new = para_malloc(sizeof(struct rb_entry));
474 old = ringbuffer_add(bot_win_rb, new);
475 // fprintf(stderr, "added: %s\n", new->msg);
480 if (scroll_position) {
481 /* discard current scrolling, like xterm does */
486 wattron(bot.win, COLOR_PAIR(color));
487 getyx(bot.win, y, x);
489 waddstr(bot.win, "\n");
490 waddstr(bot.win, msg);
494 * print formated output to bot win and refresh
496 __printf_2_3 static void outputf(int color, const char* fmt,...)
504 xvasprintf(&msg, fmt, ap);
506 rb_add_entry(color, msg);
510 static int add_output_line(char *line, void *data)
512 int color = *(int *)data? COLOR_ERRMSG : COLOR_OUTPUT;
515 rb_add_entry(color, para_strdup(line));
521 __printf_2_3 void curses_log(int ll, const char *fmt,...)
527 if (ll < loglevel || !curses_active)
536 color = COLOR_ERRMSG;
539 xvasprintf(&msg, fmt, ap);
542 rb_add_entry(color, msg);
545 __printf_2_3 void (*para_log)(int, const char*, ...) = curses_log;
547 static void setup_signal_handling(void)
549 signal_pipe = para_signal_init();
550 para_install_sighandler(SIGINT);
551 para_install_sighandler(SIGTERM);
552 para_install_sighandler(SIGCHLD);
553 para_install_sighandler(SIGWINCH);
554 para_install_sighandler(SIGUSR1);
555 para_sigaction(SIGHUP, SIG_IGN);
558 __noreturn static void do_exit(int ret)
560 para_sigaction(SIGTERM, SIG_IGN);
565 static void shutdown_curses(void)
574 __noreturn static void finish(int ret)
581 * exit curses and print given message to stdout/stderr
583 __noreturn __printf_2_3 static void msg_n_exit(int ret, const char* fmt, ...)
586 FILE *outfd = ret? stderr: stdout;
590 vfprintf(outfd, fmt, argp);
595 static void print_welcome(void)
597 if (loglevel > LL_NOTICE)
599 outputf(COLOR_WELCOME, "Welcome to para_gui " PACKAGE_VERSION
600 " \"" CODENAME "\". Theme: %s", theme.name);
607 static void init_wins(int top_lines)
611 top.lines = top_lines;
616 bot.lines = LINES - top.lines - 3;
618 bot.begy = top.lines + 1;
633 sep.begy = top.lines;
636 assume_default_colors(theme.default_fg, theme.default_bg);
638 mvwin(top.win, top.begy, top.begx);
639 wresize(top.win, top.lines, top.cols);
641 mvwin(sb.win, sb.begy, sb.begx);
642 wresize(sb.win, sb.lines, sb.cols);
644 mvwin(sep.win, sep.begy, sep.begx);
645 wresize(sep.win, sep.lines, sep.cols);
647 mvwin(bot.win, bot.begy, bot.begx);
648 wresize(bot.win, bot.lines, bot.cols);
650 mvwin(in.win, in.begy, in.begx);
651 wresize(in.win, in.lines, in.cols);
653 sep.win = newwin(sep.lines, sep.cols, sep.begy, sep.begx);
654 top.win = newwin(top.lines, top.cols, top.begy, top.begx);
655 bot.win = newwin(bot.lines, bot.cols, bot.begy, bot.begx);
656 sb.win = newwin(sb.lines, sb.cols, sb.begy, sb.begx);
657 in.win = newwin(in.lines, in.cols, in.begy, in.begx);
658 if (!top.win || !bot.win || !sb.win || !in.win || !sep.win)
659 msg_n_exit(1, "Error: Cannot create curses windows\n");
663 scrollok(bot.win, 1);
664 wattron(sb.win, COLOR_PAIR(COLOR_STATUSBAR));
665 wattron(sep.win, COLOR_PAIR(COLOR_SEPARATOR));
666 wattron(bot.win, COLOR_PAIR(COLOR_BOT));
667 wattron(top.win, COLOR_PAIR(COLOR_TOP));
679 wmove(sep.win, 0, 0);
680 for (i = 1; i <= COLS; i++)
681 waddstr(sep.win, theme.sep_str);
684 wnoutrefresh(top.win);
685 wnoutrefresh(bot.win);
686 //wnoutrefresh(sb.win);
688 wnoutrefresh(in.win);
689 wnoutrefresh(sep.win);
694 * Print stat item #i to curses window
696 static void print_stat_item(int i)
699 struct stat_item_data d = theme.data[i];
700 char *c = stat_content[i];
702 if (!curses_active || !d.len || !c)
704 tmp = make_message("%s%s%s", d.prefix, c, d.postfix);
705 wmove(top.win, d.y * top.lines / 100, d.x * COLS / 100);
707 wattron(top.win, COLOR_PAIR(i + 1));
708 align_str(top.win, tmp, d.len * COLS / 100, d.align);
713 static int update_item(int item_num, char *buf)
715 char **c = stat_content + item_num;
722 *c = para_strdup("(artist tag not set)");
725 *c = para_strdup("(title tag not set)");
728 *c = para_strdup("????");
731 *c = para_strdup("(album tag not set)");
734 *c = para_strdup("(comment tag not set)");
738 *c = para_strdup(buf);
740 print_stat_item(item_num);
744 static int read_stat_pipe(fd_set *rfds)
747 static int bufsize, loaded;
753 if (loaded >= bufsize) {
754 if (bufsize > 1000 * 1000) {
758 bufsize += bufsize + 1000;
759 buf = para_realloc(buf, bufsize);
761 assert(loaded < bufsize);
762 ret = read_nonblock(stat_pipe, buf + loaded, bufsize - loaded,
765 ret2 = for_each_stat_item(buf, loaded, update_item);
766 if (ret < 0 || ret2 < 0) {
768 return ret2 < 0? ret2 : ret;
770 sz = ret2; /* what is left */
771 if (sz > 0 && sz < loaded)
772 memmove(buf, buf + loaded - sz, sz);
777 static void print_all_items(void)
783 FOR_EACH_STATUS_ITEM(i)
787 static void clear_all_items(void)
791 FOR_EACH_STATUS_ITEM(i) {
792 free(stat_content[i]);
793 stat_content[i] = para_strdup("");
797 static void init_pair_or_die(short pair, short f, short b)
799 if (init_pair(pair, f, b) == ERR)
800 msg_n_exit(EXIT_FAILURE, "fatal: init_pair() failed\n");
803 static void init_colors_or_die(void)
808 msg_n_exit(EXIT_FAILURE, "fatal: No color term\n");
809 if (start_color() == ERR)
810 msg_n_exit(EXIT_FAILURE, "fatal: failed to start colors\n");
811 FOR_EACH_STATUS_ITEM(i)
812 if (theme.data[i].len)
813 init_pair_or_die(i + 1, theme.data[i].fg,
815 init_pair_or_die(COLOR_STATUSBAR, theme.sb_fg, theme.sb_bg);
816 init_pair_or_die(COLOR_COMMAND, theme.cmd_fg, theme.cmd_bg);
817 init_pair_or_die(COLOR_OUTPUT, theme.output_fg, theme.output_bg);
818 init_pair_or_die(COLOR_MSG, theme.msg_fg, theme.msg_bg);
819 init_pair_or_die(COLOR_ERRMSG, theme.err_msg_fg, theme.err_msg_bg);
820 init_pair_or_die(COLOR_WELCOME, theme.welcome_fg, theme.welcome_bg);
821 init_pair_or_die(COLOR_SEPARATOR, theme.sep_fg, theme.sep_bg);
822 init_pair_or_die(COLOR_TOP, theme.default_fg, theme.default_bg);
823 init_pair_or_die(COLOR_BOT, theme.default_fg, theme.default_bg);
826 /* (Re-)initialize the curses library. */
827 static void init_curses(void)
830 if (top.win && refresh() == ERR) /* refesh is really needed */
831 msg_n_exit(EXIT_FAILURE, "refresh() failed\n");
832 if (LINES < theme.lines_min || COLS < theme.cols_min)
833 msg_n_exit(EXIT_FAILURE, "Error: Terminal (%dx%d) too small"
834 " (need at least %dx%d)\n", COLS, LINES,
835 theme.cols_min, theme.lines_min);
836 curs_set(0); /* make cursor invisible, ignore errors */
837 nonl(); /* do not NL->CR/NL on output, always returns OK */
838 /* don't echo input */
840 msg_n_exit(EXIT_FAILURE, "fatal: noecho() failed\n");
841 /* take input chars one at a time, no wait for \n */
843 msg_n_exit(EXIT_FAILURE, "fatal: cbreak() failed\n");
844 init_colors_or_die();
845 clear(); /* ignore non-fatal errors */
846 init_wins(theme.top_lines_default);
848 // noecho(); /* don't echo input */
851 static void check_sigchld(void)
856 ret = para_reap_child(&pid);
859 if (pid == cmd_pid) {
863 goto reap_next_child;
867 * This sucker modifies its first argument. *handler and *arg are
868 * pointers to 0-terminated strings (inside line). Crap.
870 static int split_key_map(char *line, char **handler, char **arg)
872 if (!(*handler = strchr(line + 1, ':')))
876 if (!(*arg = strchr(*handler, ':')))
885 static int check_key_map_args(void)
889 char *tmp = NULL, *handler, *arg;
891 for (i = 0; i < conf.key_map_given; ++i) {
892 s = conf.key_map_arg[i];
896 tmp = para_strdup(s);
897 if (!split_key_map(tmp, &handler, &arg))
899 if (strlen(handler) != 1)
908 if (find_cmd_byname(arg) < 0)
918 * React to various signal-related events
920 static void handle_signal(int sig)
924 msg_n_exit(EXIT_FAILURE,
925 "only the good die young (caught SIGTERM))\n");
935 PARA_WARNING_LOG("caught SIGINT, reset");
936 /* Nothing to do. SIGINT killed our child which gets noticed
937 * by do_select and resets everything.
941 PARA_NOTICE_LOG("got SIGUSR1, rereading configuration");
950 static int open_stat_pipe(void)
953 int ret, fds[3] = {0, 1, 0};
960 * Sleep a bit to avoid a busy loop. As the call to sleep() may
961 * be interrupted by SIGCHLD, we simply wait until the call
966 ret = para_exec_cmdline_pid(&pid, conf.stat_cmd_arg, fds);
969 ret = mark_fd_nonblocking(fds[1]);
976 #define COMMAND_BUF_SIZE 4096
979 * This is the core select loop. Besides the (internal) signal
980 * pipe, the following other fds are checked according to the mode:
982 * GETCH_MODE: check stdin, return when key is pressed
984 * COMMAND_MODE: check command fds and stdin. Return when peer has closed both
985 * stdout and stderr or when any key is pressed.
987 * EXTERNAL_MODE: Check only signal pipe. Used when an external command
988 * is running. During that time curses is disabled. Returns when
991 static int do_select(int mode)
994 int ret, i, max_fileno;
995 char command_buf[2][COMMAND_BUF_SIZE] = {"", ""};
996 int cbo[2] = {0, 0}; /* command buf offsets */
1000 tv.tv_sec = conf.timeout_arg / 1000;
1001 tv.tv_usec = (conf.timeout_arg % 1000) * 1000;
1002 // ret = refresh_status();
1006 stat_pipe = open_stat_pipe();
1008 para_fd_set(stat_pipe, &rfds, &max_fileno);
1010 para_fd_set(signal_pipe, &rfds, &max_fileno);
1011 /* command pipe only for COMMAND_MODE */
1012 if (mode == COMMAND_MODE) {
1013 if (command_fds[0] >= 0)
1014 para_fd_set(command_fds[0], &rfds, &max_fileno);
1015 if (command_fds[1] >= 0)
1016 para_fd_set(command_fds[1], &rfds, &max_fileno);
1018 ret = para_select(max_fileno + 1, &rfds, NULL, &tv);
1020 goto check_return; /* skip fd checks */
1022 ret = para_next_signal(&rfds);
1025 /* read command pipe if ready */
1026 if (mode == COMMAND_MODE) {
1027 for (i = 0; i < 2; i++) {
1029 if (command_fds[i] < 0)
1031 ret = read_nonblock(command_fds[i],
1032 command_buf[i] + cbo[i],
1033 COMMAND_BUF_SIZE - 1 - cbo[i], &rfds, &sz);
1036 cbo[i] = for_each_line(command_buf[i], cbo[i],
1037 add_output_line, &i);
1041 PARA_NOTICE_LOG("closing command fd %d: %s",
1042 i, para_strerror(-ret));
1043 close(command_fds[i]);
1044 command_fds[i] = -1;
1045 if (command_fds[!i] < 0) /* both fds closed */
1050 ret = read_stat_pipe(&rfds);
1052 PARA_NOTICE_LOG("closing stat pipe: %s\n", para_strerror(-ret));
1056 free(stat_content[SI_BASENAME]);
1057 stat_content[SI_BASENAME] =
1058 para_strdup("stat command terminated!?");
1064 ret = wgetch(top.win);
1065 if (ret != ERR && ret != KEY_RESIZE) {
1066 if (command_fds[0] >= 0) {
1067 close(command_fds[0]);
1068 command_fds[0] = -1;
1070 if (command_fds[1] >= 0) {
1071 close(command_fds[1]);
1072 command_fds[1] = -1;
1075 kill(cmd_pid, SIGTERM);
1080 ret = wgetch(top.win);
1081 if (ret != ERR && ret != KEY_RESIZE)
1094 * read from command pipe and print data to bot window
1096 static void send_output(void)
1100 ret = mark_fd_nonblocking(command_fds[0]);
1103 ret = mark_fd_nonblocking(command_fds[1]);
1106 if (do_select(COMMAND_MODE) >= 0)
1107 PARA_INFO_LOG("command complete");
1109 PARA_NOTICE_LOG("command aborted");
1110 print_in_bar(COLOR_MSG, " ");
1113 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
1114 close(command_fds[0]);
1115 close(command_fds[1]);
1118 static void para_cmd(char *cmd)
1120 int ret, fds[3] = {0, 1, 1};
1121 char *c = make_message(BINDIR "/para_client -- %s", cmd);
1123 outputf(COLOR_COMMAND, "%s", c);
1124 print_in_bar(COLOR_MSG, "executing client command, hit any key to abort\n");
1125 ret = para_exec_cmdline_pid(&cmd_pid, c, fds);
1129 command_fds[0] = fds[1];
1130 command_fds[1] = fds[2];
1135 * exec command and print output to bot win
1137 static void display_cmd(char *cmd)
1139 int fds[3] = {0, 1, 1};
1141 print_in_bar(COLOR_MSG, "executing display command, hit any key to abort");
1142 outputf(COLOR_COMMAND, "%s", cmd);
1143 if (para_exec_cmdline_pid(&cmd_pid, cmd, fds) < 0)
1145 command_fds[0] = fds[1];
1146 command_fds[1] = fds[2];
1151 * shutdown curses and stat pipe before executing external commands
1153 static void external_cmd(char *cmd)
1155 int fds[3] = {-1, -1, -1};
1160 if (para_exec_cmdline_pid(&cmd_pid, cmd, fds) < 0)
1163 do_select(EXTERNAL_MODE);
1167 static void print_scroll_msg(void)
1169 unsigned lines_total, filled = ringbuffer_filled(bot_win_rb);
1170 int first_rbe = first_visible_rbe(&lines_total);
1171 print_in_bar(COLOR_MSG, "scrolled view: %d-%d/%d\n", filled - first_rbe,
1172 filled - scroll_position, ringbuffer_filled(bot_win_rb));
1175 static void com_scroll_top(void)
1177 int i = RINGBUFFER_SIZE - 1;
1180 while (i > 0 && !ringbuffer_get(bot_win_rb, i))
1182 /* i is oldest entry */
1183 for (; lines < bot.lines && i >= 0; i--) {
1184 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
1187 lines += NUM_LINES(rbe->len);
1190 if (lines > 0 && scroll_position != i) {
1191 scroll_position = i;
1196 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1199 static void com_cancel_scrolling(void)
1202 if (scroll_position == 0) {
1203 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1206 scroll_position = 0;
1210 static void com_page_down(void)
1213 int i = scroll_position;
1214 while (lines < bot.lines && --i > 0) {
1215 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
1218 lines += NUM_LINES(rbe->len);
1221 scroll_position = i;
1226 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1229 static void com_page_up(void)
1232 int fvr = first_visible_rbe(&lines);
1234 if (fvr < 0 || fvr + 1 >= ringbuffer_filled(bot_win_rb)) {
1235 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1238 scroll_position = fvr + 1;
1239 for (; scroll_position > 0; scroll_position--) {
1240 first_visible_rbe(&lines);
1241 if (lines == bot.lines)
1248 static void com_scroll_down(void)
1250 struct rb_entry *rbe;
1253 if (!scroll_position) {
1254 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1258 rbe = ringbuffer_get(bot_win_rb, scroll_position);
1259 rbe_lines = NUM_LINES(rbe->len);
1260 wscrl(bot.win, rbe_lines);
1261 wmove(bot.win, bot.lines - rbe_lines, 0);
1262 wattron(bot.win, COLOR_PAIR(rbe->color));
1263 waddstr(bot.win, rbe->msg);
1268 static void com_scroll_up(void)
1270 struct rb_entry *rbe = NULL;
1272 int i, first_rbe, num_scroll;
1274 /* the entry that is going to vanish */
1275 rbe = ringbuffer_get(bot_win_rb, scroll_position);
1278 num_scroll = NUM_LINES(rbe->len);
1279 first_rbe = first_visible_rbe(&lines);
1280 if (first_rbe < 0 || (first_rbe == ringbuffer_filled(bot_win_rb) - 1))
1283 wscrl(bot.win, -num_scroll);
1284 i = draw_top_rbe(&lines);
1287 while (i > 0 && lines < num_scroll) {
1289 rbe = ringbuffer_get(bot_win_rb, --i);
1292 rbe_lines = NUM_LINES(rbe->len);
1294 // fprintf(stderr, "msg: %s\n", rbe->msg);
1295 wattron(bot.win, COLOR_PAIR(rbe->color));
1296 waddstr(bot.win, "\n");
1297 waddstr(bot.win, rbe->msg);
1306 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1309 static void com_ll_decr(void)
1311 if (loglevel <= LL_DEBUG) {
1312 print_in_bar(COLOR_ERRMSG,
1313 "loglevel already at maximal verbosity\n");
1317 print_in_bar(COLOR_MSG, "loglevel set to %d\n", loglevel);
1320 static void com_ll_incr(void)
1322 if (loglevel >= LL_EMERG) {
1323 print_in_bar(COLOR_ERRMSG,
1324 "loglevel already at minimal verbosity\n");
1328 print_in_bar(COLOR_MSG, "loglevel set to %d\n", loglevel);
1332 * reread configuration, terminate on errors
1334 static void com_reread_conf(void)
1336 char *cf =configfile_exists();
1337 struct gui_cmdline_parser_params params = {
1340 .check_required = 0,
1341 .check_ambiguity = 0,
1346 PARA_WARNING_LOG("there is no configuration to read");
1349 PARA_INFO_LOG("rereading command line options and config file");
1350 gui_cmdline_parser_ext(_argc, _argv, &conf, ¶ms);
1351 if (gui_cmdline_parser_config_file(cf, &conf, ¶ms) != 0) {
1352 PARA_EMERG_LOG("errors in config file");
1353 finish(EXIT_FAILURE);
1355 PARA_NOTICE_LOG("config file reloaded");
1356 if (check_key_map_args() < 0)
1357 finish(EXIT_FAILURE);
1360 static void com_help(void)
1364 for (i = 0; i < conf.key_map_given; ++i) {
1365 char *handler, *arg, *tmp = para_strdup(conf.key_map_arg[i]);
1366 const char *handler_text = "???", *desc = NULL;
1368 if (!split_key_map(tmp, &handler, &arg)) {
1374 handler_text = "internal";
1375 desc = command_list[find_cmd_byname(arg)].description;
1377 case 'x': handler_text = "external"; break;
1378 case 'd': handler_text = "display "; break;
1379 case 'p': handler_text = "para "; break;
1381 outputf(COLOR_MSG, "%s\t%s\t%s%s\t%s", tmp, handler_text, arg,
1382 strlen(arg) < 8? "\t" : "",
1386 for (i = 0; command_list[i].handler; i++) {
1387 struct gui_command gc = command_list[i];
1389 outputf(COLOR_MSG, "%s\tinternal\t%s\t%s%s", gc.key, gc.name,
1390 strlen(gc.name) < 8? "\t" : "",
1393 print_in_bar(COLOR_MSG, "try \"para_gui -h\" or \"para_client help\" "
1397 static void com_shrink_top_win(void)
1399 if (top.lines <= theme.top_lines_min) {
1400 PARA_WARNING_LOG("can not decrease top window");
1403 init_wins(top.lines - 1);
1406 print_in_bar(COLOR_MSG, "%s", "decreased top window");
1409 static void com_enlarge_top_win(void)
1411 if (bot.lines < 3) {
1412 PARA_WARNING_LOG("can not increase top window");
1415 init_wins(top.lines + 1);
1418 print_in_bar(COLOR_MSG, "increased top window");
1421 static void com_version(void)
1423 print_in_bar(COLOR_MSG, "para_gui " PACKAGE_VERSION " \""
1427 __noreturn static void com_quit(void)
1432 static void com_refresh(void)
1438 static void change_theme(int next)
1444 /* This seems to be needed twice, why? */
1447 PARA_NOTICE_LOG("new theme: %s", theme.name);
1450 static void com_next_theme(void)
1455 static void com_prev_theme(void)
1461 static void handle_command(int c)
1465 /* first check user's key bindings */
1466 for (i = 0; i < conf.key_map_given; ++i) {
1467 char *tmp, *handler, *arg;
1469 tmp = para_strdup(conf.key_map_arg[i]);
1470 if (!split_key_map(tmp, &handler, &arg)) {
1474 if (strcmp(tmp, km_keyname(c))) {
1478 if (*handler == 'd')
1480 else if (*handler == 'x')
1482 else if (*handler == 'p')
1484 else if (*handler == 'i') {
1485 int num = find_cmd_byname(arg);
1487 command_list[num].handler();
1492 /* not found, check internal key bindings */
1493 for (i = 0; command_list[i].handler; i++) {
1494 if (!strcmp(km_keyname(c), command_list[i].key)) {
1495 command_list[i].handler();
1499 print_in_bar(COLOR_ERRMSG, "key '%s' is not bound, press ? for help",
1503 int main(int argc, char *argv[])
1511 gui_cmdline_parser(argc, argv, &conf); /* exits on errors */
1512 HANDLE_VERSION_FLAG("gui", conf);
1513 cf = configfile_exists();
1514 if (!cf && conf.config_file_given) {
1515 fprintf(stderr, "can not read config file %s\n",
1516 conf.config_file_arg);
1520 struct gui_cmdline_parser_params params = {
1523 .check_required = 0,
1524 .check_ambiguity = 0,
1527 if (gui_cmdline_parser_config_file(cf, &conf, ¶ms) != 0)
1530 loglevel = get_loglevel_by_name(conf.loglevel_arg);
1531 if (check_key_map_args() < 0) {
1532 fprintf(stderr, "invalid key map\n");
1535 init_theme_or_die(conf.theme_arg, &theme);
1536 top.lines = theme.top_lines_default;
1537 setup_signal_handling();
1538 bot_win_rb = ringbuffer_new(RINGBUFFER_SIZE);
1539 setlocale(LC_CTYPE, "");
1540 initscr(); /* needed only once, always successful */
1545 ret = do_select(GETCH_MODE);
1548 print_in_bar(COLOR_MSG, " ");
1549 handle_command(ret);