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 win_data {
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 int 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};
66 #define COLOR_STATUSBAR 52
67 #define COLOR_COMMAND 53
68 #define COLOR_OUTPUT 54
70 #define COLOR_ERRMSG 56
71 #define COLOR_WELCOME 57
72 #define COLOR_SEPARATOR 58
79 const char *description;
80 void (*handler)(void);
83 static struct gui_theme theme;
88 static void com_help(void);
89 static void com_reread_conf(void);
90 static void com_enlarge_top_win(void);
91 static void com_shrink_top_win(void);
92 static void com_version(void);
93 __noreturn static void com_quit(void);
94 static void com_refresh(void);
95 static void com_ll_incr(void);
96 static void com_ll_decr(void);
97 static void com_prev_theme(void);
98 static void com_next_theme(void);
99 static void com_scroll_up(void);
100 static void com_scroll_down(void);
101 static void com_page_up(void);
102 static void com_page_down(void);
103 static void com_cancel_scrolling(void);
104 static void com_scroll_top(void);
106 static struct gui_command command_list[] = {
110 .description = "print help",
114 .name = "enlarge_win",
115 .description = "enlarge the top window",
116 .handler = com_enlarge_top_win
119 .name = "shrink_win",
120 .description = "shrink the top window",
121 .handler = com_shrink_top_win
124 .name = "reread_conf",
125 .description = "reread configuration file",
126 .handler = com_reread_conf
130 .description = "exit para_gui",
135 .description = "redraw the screen",
136 .handler = com_refresh
139 .name = "next_theme",
140 .description = "switch to next theme",
141 .handler = com_next_theme
144 .name = "prev_theme",
145 .description = "switch to previous stream",
146 .handler = com_prev_theme
150 .description = "increase loglevel (decreases verbosity)",
151 .handler = com_ll_incr
155 .description = "decrease loglevel (increases verbosity)",
156 .handler = com_ll_decr
160 .description = "show the para_gui version",
161 .handler = com_version
165 .description = "scroll up one line",
166 .handler = com_scroll_up
169 .name = "scroll_down",
170 .description = "scroll down one line",
171 .handler = com_scroll_down
175 .description = "scroll up one page",
176 .handler = com_page_up
180 .description = "scroll down one page",
181 .handler = com_page_down
184 .name = "scroll_top",
185 .description = "scroll to top of buffer",
186 .handler = com_scroll_top
189 .name = "cancel_scroll",
190 .description = "deactivate scroll mode",
191 .handler = com_cancel_scrolling
197 static int find_cmd_byname(char *name)
201 for (i = 0; command_list[i].handler; i++)
202 if (!strcmp(command_list[i].name, name))
207 /* taken from mutt */
208 static char *km_keyname(int c)
213 sprintf(buf, "<up>");
217 sprintf(buf, "<down>");
221 sprintf(buf, "<left>");
224 if (c == KEY_RIGHT) {
225 sprintf(buf, "<right>");
228 if (c == KEY_NPAGE) {
229 sprintf(buf, "<npage>");
232 if (c == KEY_PPAGE) {
233 sprintf(buf, "<ppage>");
237 sprintf(buf, "<home>");
241 sprintf(buf, "<end>");
244 if (c < 256 && c > -128 && iscntrl((unsigned char) c)) {
249 buf[1] = (c + '@') & 0x7f;
252 snprintf(buf, sizeof(buf), "\\%d%d%d", c >> 6,
253 (c >> 3) & 7, c & 7);
254 } else if (c >= KEY_F0 && c < KEY_F(256))
255 sprintf(buf, "<F%d>", c - KEY_F0);
257 snprintf(buf, sizeof(buf), "%c", (unsigned char) c);
259 snprintf(buf, sizeof(buf), "\\x%hx", (unsigned short) c);
263 static char *configfile_exists(void)
265 static char *config_file;
268 if (!conf.config_file_given) {
270 char *home = para_homedir();
271 config_file = make_message("%s/.paraslash/gui.conf",
277 tmp = conf.config_file_arg;
278 return file_exists(tmp)? tmp: NULL;
281 /* Print given number of spaces to curses window. */
282 static void add_spaces(WINDOW* win, unsigned int num)
285 unsigned sz = sizeof(space) - 1; /* number of spaces */
299 * print aligned string to curses window. This function always prints
302 static int align_str(WINDOW* win, char *str, unsigned int len,
305 int ret, i, num; /* of spaces */
310 ret = strwidth(str, &width);
312 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
321 /* replace control characters by spaces */
322 for (i = 0; i < len && str[i]; i++) {
323 if (str[i] == '\n' || str[i] == '\r' || str[i] == '\f')
328 add_spaces(win, num);
329 } else if (align == RIGHT) {
330 add_spaces(win, num);
333 add_spaces(win, num / 2);
334 waddstr(win, str[0]? str: "");
335 add_spaces(win, num - num / 2);
340 __printf_2_3 static void print_in_bar(int color, const char *fmt,...)
347 wattron(in.win, COLOR_PAIR(color));
349 xvasprintf(&msg, fmt, ap);
352 align_str(in.win, msg, in.cols, LEFT);
358 * update the status bar
360 static void print_status_bar(void)
366 tmp = para_strdup(STANDARD_STATUS_BAR);
368 align_str(sb.win, tmp, sb.cols, CENTER);
374 * get the number of the oldest rbe that is (partially) visible. On return,
375 * lines contains the sum of the number of lines of all visible entries. If the
376 * first one is only partially visible, lines is greater than bot.lines.
378 static int first_visible_rbe(unsigned *lines)
382 for (i = scroll_position; i < RINGBUFFER_SIZE; i++) {
383 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
387 // fprintf(stderr, "found: %s\n", rbe->msg);
388 rbe_lines = NUM_LINES(rbe->len);
389 if (rbe_lines > bot.lines)
392 if (*lines >= bot.lines)
395 return RINGBUFFER_SIZE - 1;
399 returns number of first visible rbe, *lines is the number of lines drawn.
401 static int draw_top_rbe(unsigned *lines)
403 int ret, fvr = first_visible_rbe(lines);
404 struct rb_entry *rbe;
405 size_t bytes_to_skip, cells_to_skip, width;
409 wmove(bot.win, 0, 0);
410 rbe = ringbuffer_get(bot_win_rb, fvr);
413 if (*lines > bot.lines) {
414 /* rbe is partially visible multi-line */
415 cells_to_skip = (*lines - bot.lines) * bot.cols;
416 ret = skip_cells(rbe->msg, cells_to_skip, &bytes_to_skip);
419 ret = strwidth(rbe->msg + bytes_to_skip, &width);
426 wattron(bot.win, COLOR_PAIR(rbe->color));
427 waddstr(bot.win, rbe->msg + bytes_to_skip);
428 *lines = NUM_LINES(width);
432 static void redraw_bot_win(void)
437 wmove(bot.win, 0, 0);
439 i = draw_top_rbe(&lines);
442 while (i > 0 && lines < bot.lines) {
443 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, --i);
446 waddstr(bot.win, "\n");
449 lines += NUM_LINES(rbe->len);
450 wattron(bot.win, COLOR_PAIR(rbe->color));
451 waddstr(bot.win, "\n");
452 waddstr(bot.win, rbe->msg);
458 static void rb_add_entry(int color, char *msg)
460 struct rb_entry *old, *new;
464 if (strwidth(msg, &len) < 0)
466 new = para_malloc(sizeof(struct rb_entry));
470 old = ringbuffer_add(bot_win_rb, new);
471 // fprintf(stderr, "added: %s\n", new->msg);
476 if (scroll_position) {
477 /* discard current scrolling, like xterm does */
482 wattron(bot.win, COLOR_PAIR(color));
483 getyx(bot.win, y, x);
485 waddstr(bot.win, "\n");
486 waddstr(bot.win, msg);
490 * print formated output to bot win and refresh
492 __printf_2_3 static void outputf(int color, const char* fmt,...)
500 xvasprintf(&msg, fmt, ap);
502 rb_add_entry(color, msg);
506 static int add_output_line(char *line, void *data)
508 int color = *(int *)data? COLOR_ERRMSG : COLOR_OUTPUT;
512 rb_add_entry(color, para_strdup(line));
518 static __printf_2_3 void curses_log(int ll, const char *fmt,...)
524 if (ll < loglevel || !curses_active)
533 color = COLOR_ERRMSG;
536 xvasprintf(&msg, fmt, ap);
539 rb_add_entry(color, msg);
542 __printf_2_3 void (*para_log)(int, const char*, ...) = curses_log;
544 static void setup_signal_handling(void)
546 signal_pipe = para_signal_init();
547 para_install_sighandler(SIGINT);
548 para_install_sighandler(SIGTERM);
549 para_install_sighandler(SIGCHLD);
550 para_install_sighandler(SIGWINCH);
551 para_install_sighandler(SIGUSR1);
554 /* kill every process in the process group and exit */
555 __noreturn static void kill_pg_and_die(int ret)
557 para_sigaction(SIGTERM, SIG_IGN);
562 static void shutdown_curses(void)
571 __noreturn static void finish(int ret)
574 kill_pg_and_die(ret);
578 * exit curses and print given message to stdout/stderr
580 __noreturn __printf_2_3 static void msg_n_exit(int ret, const char* fmt, ...)
583 FILE *outfd = ret? stderr: stdout;
587 vfprintf(outfd, fmt, argp);
589 kill_pg_and_die(ret);
592 static void print_welcome(void)
594 if (loglevel > LL_NOTICE)
596 outputf(COLOR_WELCOME, "Welcome to %s. Theme: %s",
597 version_single_line("gui"), theme.name);
604 static void init_wins(int top_lines)
608 top.lines = top_lines;
613 bot.lines = LINES - top.lines - 3;
615 bot.begy = top.lines + 1;
630 sep.begy = top.lines;
633 assume_default_colors(theme.default_fg, theme.default_bg);
635 mvwin(top.win, top.begy, top.begx);
636 wresize(top.win, top.lines, top.cols);
638 mvwin(sb.win, sb.begy, sb.begx);
639 wresize(sb.win, sb.lines, sb.cols);
641 mvwin(sep.win, sep.begy, sep.begx);
642 wresize(sep.win, sep.lines, sep.cols);
644 mvwin(bot.win, bot.begy, bot.begx);
645 wresize(bot.win, bot.lines, bot.cols);
647 mvwin(in.win, in.begy, in.begx);
648 wresize(in.win, in.lines, in.cols);
650 sep.win = newwin(sep.lines, sep.cols, sep.begy, sep.begx);
651 top.win = newwin(top.lines, top.cols, top.begy, top.begx);
652 bot.win = newwin(bot.lines, bot.cols, bot.begy, bot.begx);
653 sb.win = newwin(sb.lines, sb.cols, sb.begy, sb.begx);
654 in.win = newwin(in.lines, in.cols, in.begy, in.begx);
655 if (!top.win || !bot.win || !sb.win || !in.win || !sep.win)
656 msg_n_exit(1, "Error: Cannot create curses windows\n");
660 scrollok(bot.win, 1);
661 wattron(sb.win, COLOR_PAIR(COLOR_STATUSBAR));
662 wattron(sep.win, COLOR_PAIR(COLOR_SEPARATOR));
663 wattron(bot.win, COLOR_PAIR(COLOR_BOT));
664 wattron(top.win, COLOR_PAIR(COLOR_TOP));
676 wmove(sep.win, 0, 0);
677 for (i = 1; i <= COLS; i++)
678 waddstr(sep.win, theme.sep_str);
681 wnoutrefresh(top.win);
682 wnoutrefresh(bot.win);
683 //wnoutrefresh(sb.win);
685 wnoutrefresh(in.win);
686 wnoutrefresh(sep.win);
691 * Print stat item #i to curses window
693 static void print_stat_item(int i)
696 struct stat_item_data d = theme.data[i];
697 char *c = stat_content[i];
699 if (!curses_active || !d.len || !c)
701 tmp = make_message("%s%s%s", d.prefix, c, d.postfix);
702 wmove(top.win, d.y * top.lines / 100, d.x * COLS / 100);
704 wattron(top.win, COLOR_PAIR(i + 1));
705 align_str(top.win, tmp, d.len * COLS / 100, d.align);
710 static int update_item(int item_num, char *buf)
712 char **c = stat_content + item_num;
719 *c = para_strdup("(artist tag not set)");
722 *c = para_strdup("(title tag not set)");
725 *c = para_strdup("????");
728 *c = para_strdup("(album tag not set)");
731 *c = para_strdup("(comment tag not set)");
735 *c = para_strdup(buf);
737 print_stat_item(item_num);
741 static int read_stat_pipe(fd_set *rfds)
744 static int bufsize, loaded;
750 if (loaded >= bufsize) {
751 if (bufsize > 1000 * 1000) {
755 bufsize += bufsize + 1000;
756 buf = para_realloc(buf, bufsize);
758 assert(loaded < bufsize);
759 ret = read_nonblock(stat_pipe, buf + loaded, bufsize - loaded,
762 ret2 = for_each_stat_item(buf, loaded, update_item);
763 if (ret < 0 || ret2 < 0) {
765 return ret2 < 0? ret2 : ret;
767 sz = ret2; /* what is left */
768 if (sz > 0 && sz < loaded)
769 memmove(buf, buf + loaded - sz, sz);
774 static void print_all_items(void)
780 FOR_EACH_STATUS_ITEM(i)
784 static void clear_all_items(void)
788 FOR_EACH_STATUS_ITEM(i) {
789 free(stat_content[i]);
790 stat_content[i] = para_strdup("");
794 static void init_pair_or_die(short pair, short f, short b)
796 if (init_pair(pair, f, b) == ERR)
797 msg_n_exit(EXIT_FAILURE, "fatal: init_pair() failed\n");
800 static void init_colors_or_die(void)
805 msg_n_exit(EXIT_FAILURE, "fatal: No color term\n");
806 if (start_color() == ERR)
807 msg_n_exit(EXIT_FAILURE, "fatal: failed to start colors\n");
808 FOR_EACH_STATUS_ITEM(i)
809 if (theme.data[i].len)
810 init_pair_or_die(i + 1, theme.data[i].fg,
812 init_pair_or_die(COLOR_STATUSBAR, theme.sb_fg, theme.sb_bg);
813 init_pair_or_die(COLOR_COMMAND, theme.cmd_fg, theme.cmd_bg);
814 init_pair_or_die(COLOR_OUTPUT, theme.output_fg, theme.output_bg);
815 init_pair_or_die(COLOR_MSG, theme.msg_fg, theme.msg_bg);
816 init_pair_or_die(COLOR_ERRMSG, theme.err_msg_fg, theme.err_msg_bg);
817 init_pair_or_die(COLOR_WELCOME, theme.welcome_fg, theme.welcome_bg);
818 init_pair_or_die(COLOR_SEPARATOR, theme.sep_fg, theme.sep_bg);
819 init_pair_or_die(COLOR_TOP, theme.default_fg, theme.default_bg);
820 init_pair_or_die(COLOR_BOT, theme.default_fg, theme.default_bg);
823 /* (Re-)initialize the curses library. */
824 static void init_curses(void)
827 if (top.win && refresh() == ERR) /* refresh is really needed */
828 msg_n_exit(EXIT_FAILURE, "refresh() failed\n");
829 if (LINES < theme.lines_min || COLS < theme.cols_min)
830 msg_n_exit(EXIT_FAILURE, "Error: Terminal (%dx%d) too small"
831 " (need at least %dx%d)\n", COLS, LINES,
832 theme.cols_min, theme.lines_min);
833 curs_set(0); /* make cursor invisible, ignore errors */
834 nonl(); /* do not NL->CR/NL on output, always returns OK */
835 /* don't echo input */
837 msg_n_exit(EXIT_FAILURE, "fatal: noecho() failed\n");
838 /* take input chars one at a time, no wait for \n */
840 msg_n_exit(EXIT_FAILURE, "fatal: cbreak() failed\n");
841 init_colors_or_die();
842 clear(); /* ignore non-fatal errors */
843 init_wins(theme.top_lines_default);
845 // noecho(); /* don't echo input */
848 static void check_sigchld(void)
854 ret = para_reap_child(&pid);
859 goto reap_next_child;
863 * This sucker modifies its first argument. *handler and *arg are
864 * pointers to 0-terminated strings (inside line). Crap.
866 static int split_key_map(char *line, char **handler, char **arg)
868 if (!(*handler = strchr(line + 1, ':')))
872 if (!(*arg = strchr(*handler, ':')))
881 static int check_key_map_args(void)
885 char *tmp = NULL, *handler, *arg;
887 for (i = 0; i < conf.key_map_given; ++i) {
888 s = conf.key_map_arg[i];
892 tmp = para_strdup(s);
893 if (!split_key_map(tmp, &handler, &arg))
895 if (strlen(handler) != 1)
904 if (find_cmd_byname(arg) < 0)
914 * React to various signal-related events
916 static void handle_signal(int sig)
920 msg_n_exit(EXIT_FAILURE,
921 "only the good die young (caught SIGTERM))\n");
931 PARA_WARNING_LOG("caught SIGINT, reset\n");
932 /* Nothing to do. SIGINT killed our child which gets noticed
933 * by do_select and resets everything.
937 PARA_NOTICE_LOG("got SIGUSR1, rereading configuration\n");
946 static void status_pre_select(fd_set *rfds, int *max_fileno, struct timeval *tv)
948 static struct timeval next_exec, atm, diff;
949 int ret, fds[3] = {0, 1, 0};
954 /* Avoid busy loop */
955 gettimeofday(&atm, NULL);
956 if (tv_diff(&next_exec, &atm, &diff) > 0) {
957 if (tv_diff(&diff, tv, NULL) < 0)
961 next_exec.tv_sec = atm.tv_sec + 2;
962 ret = para_exec_cmdline_pid(&pid, conf.stat_cmd_arg, fds);
965 ret = mark_fd_nonblocking(fds[1]);
972 para_fd_set(stat_pipe, rfds, max_fileno);
975 #define COMMAND_BUF_SIZE 32768
978 * This is the core select loop. Besides the (internal) signal
979 * pipe, the following other fds are checked according to the mode:
981 * GETCH_MODE: check stdin, return when key is pressed
983 * COMMAND_MODE: check command fds and stdin. Return when peer has closed both
984 * stdout and stderr or when any key is pressed.
986 * EXTERNAL_MODE: Check only signal pipe. Used when an external command
987 * is running. During that time curses is disabled. Returns when
990 static int do_select(int mode)
993 int ret, i, max_fileno;
994 char command_buf[2][COMMAND_BUF_SIZE] = {"", ""};
995 int cbo[2] = {0, 0}; /* command buf offsets */
997 unsigned flags[2] = {0, 0}; /* for for_each_line() */
1000 tv.tv_sec = conf.timeout_arg / 1000;
1001 tv.tv_usec = (conf.timeout_arg % 1000) * 1000;
1002 // ret = refresh_status();
1005 status_pre_select(&rfds, &max_fileno, &tv);
1007 para_fd_set(signal_pipe, &rfds, &max_fileno);
1008 /* command pipe only for COMMAND_MODE */
1009 if (mode == COMMAND_MODE) {
1010 if (command_fds[0] >= 0)
1011 para_fd_set(command_fds[0], &rfds, &max_fileno);
1012 if (command_fds[1] >= 0)
1013 para_fd_set(command_fds[1], &rfds, &max_fileno);
1015 if (mode == GETCH_MODE || mode == COMMAND_MODE)
1016 para_fd_set(STDIN_FILENO, &rfds, &max_fileno);
1017 ret = para_select(max_fileno + 1, &rfds, NULL, &tv);
1019 goto check_return; /* skip fd checks */
1021 ret = para_next_signal(&rfds);
1024 /* read command pipe if ready */
1025 if (mode == COMMAND_MODE) {
1026 for (i = 0; i < 2; i++) {
1028 if (command_fds[i] < 0)
1030 ret = read_nonblock(command_fds[i],
1031 command_buf[i] + cbo[i],
1032 COMMAND_BUF_SIZE - 1 - cbo[i], &rfds, &sz);
1035 cbo[i] = for_each_line(flags[i], command_buf[i], cbo[i],
1036 add_output_line, &i);
1037 if (sz != cbo[i]) { /* at least one line found */
1042 PARA_NOTICE_LOG("closing command fd %d: %s\n",
1043 i, para_strerror(-ret));
1044 close(command_fds[i]);
1045 command_fds[i] = -1;
1048 if (command_fds[!i] < 0) /* both fds closed */
1051 if (cbo[i] == COMMAND_BUF_SIZE - 1) {
1052 PARA_NOTICE_LOG("discarding overlong line\n");
1054 flags[i] = FELF_DISCARD_FIRST;
1058 ret = read_stat_pipe(&rfds);
1060 PARA_NOTICE_LOG("closing stat pipe: %s\n", para_strerror(-ret));
1064 free(stat_content[SI_BASENAME]);
1065 stat_content[SI_BASENAME] =
1066 para_strdup("stat command terminated!?");
1072 ret = wgetch(top.win);
1073 if (ret != ERR && ret != KEY_RESIZE) {
1074 if (command_fds[0] >= 0) {
1075 close(command_fds[0]);
1076 command_fds[0] = -1;
1078 if (command_fds[1] >= 0) {
1079 close(command_fds[1]);
1080 command_fds[1] = -1;
1083 kill(cmd_pid, SIGTERM);
1088 ret = wgetch(top.win);
1089 if (ret != ERR && ret != KEY_RESIZE)
1100 * read from command pipe and print data to bot window
1102 static void send_output(void)
1106 ret = mark_fd_nonblocking(command_fds[0]);
1109 ret = mark_fd_nonblocking(command_fds[1]);
1112 if (do_select(COMMAND_MODE) >= 0)
1113 PARA_INFO_LOG("command complete\n");
1115 PARA_NOTICE_LOG("command aborted\n");
1116 print_in_bar(COLOR_MSG, " ");
1119 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
1120 close(command_fds[0]);
1121 close(command_fds[1]);
1124 static void para_cmd(char *cmd)
1126 int ret, fds[3] = {0, 1, 1};
1127 char *c = make_message(BINDIR "/para_client -- %s", cmd);
1129 outputf(COLOR_COMMAND, "%s", c);
1130 print_in_bar(COLOR_MSG, "executing client command, hit any key to abort\n");
1131 ret = para_exec_cmdline_pid(&cmd_pid, c, fds);
1135 command_fds[0] = fds[1];
1136 command_fds[1] = fds[2];
1141 * exec command and print output to bot win
1143 static void display_cmd(char *cmd)
1145 int fds[3] = {0, 1, 1};
1147 print_in_bar(COLOR_MSG, "executing display command, hit any key to abort");
1148 outputf(COLOR_COMMAND, "%s", cmd);
1149 if (para_exec_cmdline_pid(&cmd_pid, cmd, fds) < 0)
1151 command_fds[0] = fds[1];
1152 command_fds[1] = fds[2];
1157 * shutdown curses and stat pipe before executing external commands
1159 static void external_cmd(char *cmd)
1161 int fds[3] = {-1, -1, -1};
1166 if (para_exec_cmdline_pid(&cmd_pid, cmd, fds) < 0)
1168 do_select(EXTERNAL_MODE);
1172 static void print_scroll_msg(void)
1174 unsigned lines_total, filled = ringbuffer_filled(bot_win_rb);
1175 int first_rbe = first_visible_rbe(&lines_total);
1177 print_in_bar(COLOR_MSG, "scrolled view: %d-%d/%d\n", filled - first_rbe,
1178 filled - scroll_position, ringbuffer_filled(bot_win_rb));
1181 static void com_scroll_top(void)
1183 int i = RINGBUFFER_SIZE - 1;
1186 while (i > 0 && !ringbuffer_get(bot_win_rb, i))
1188 /* i is oldest entry */
1189 for (; lines < bot.lines && i >= 0; i--) {
1190 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
1193 lines += NUM_LINES(rbe->len);
1196 if (lines > 0 && scroll_position != i) {
1197 scroll_position = i;
1202 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1205 static void com_cancel_scrolling(void)
1208 if (scroll_position == 0) {
1209 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1212 scroll_position = 0;
1216 static void com_page_down(void)
1219 int i = scroll_position;
1221 while (lines < bot.lines && --i > 0) {
1222 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
1225 lines += NUM_LINES(rbe->len);
1228 scroll_position = i;
1233 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1236 static void com_page_up(void)
1239 int fvr = first_visible_rbe(&lines);
1241 if (fvr < 0 || fvr + 1 >= ringbuffer_filled(bot_win_rb)) {
1242 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1245 scroll_position = fvr + 1;
1246 for (; scroll_position > 0; scroll_position--) {
1247 first_visible_rbe(&lines);
1248 if (lines == bot.lines)
1255 static void com_scroll_down(void)
1257 struct rb_entry *rbe;
1260 if (!scroll_position) {
1261 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1265 rbe = ringbuffer_get(bot_win_rb, scroll_position);
1266 rbe_lines = NUM_LINES(rbe->len);
1267 wscrl(bot.win, rbe_lines);
1268 wmove(bot.win, bot.lines - rbe_lines, 0);
1269 wattron(bot.win, COLOR_PAIR(rbe->color));
1270 waddstr(bot.win, rbe->msg);
1275 static void com_scroll_up(void)
1277 struct rb_entry *rbe = NULL;
1279 int i, first_rbe, num_scroll;
1281 /* the entry that is going to vanish */
1282 rbe = ringbuffer_get(bot_win_rb, scroll_position);
1285 num_scroll = NUM_LINES(rbe->len);
1286 first_rbe = first_visible_rbe(&lines);
1287 if (first_rbe < 0 || (first_rbe == ringbuffer_filled(bot_win_rb) - 1))
1290 wscrl(bot.win, -num_scroll);
1291 i = draw_top_rbe(&lines);
1294 while (i > 0 && lines < num_scroll) {
1296 rbe = ringbuffer_get(bot_win_rb, --i);
1299 rbe_lines = NUM_LINES(rbe->len);
1301 // fprintf(stderr, "msg: %s\n", rbe->msg);
1302 wattron(bot.win, COLOR_PAIR(rbe->color));
1303 waddstr(bot.win, "\n");
1304 waddstr(bot.win, rbe->msg);
1313 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1316 static void com_ll_decr(void)
1318 if (loglevel <= LL_DEBUG) {
1319 print_in_bar(COLOR_ERRMSG,
1320 "loglevel already at maximal verbosity\n");
1324 print_in_bar(COLOR_MSG, "loglevel set to %d\n", loglevel);
1327 static void com_ll_incr(void)
1329 if (loglevel >= LL_EMERG) {
1330 print_in_bar(COLOR_ERRMSG,
1331 "loglevel already at minimal verbosity\n");
1335 print_in_bar(COLOR_MSG, "loglevel set to %d\n", loglevel);
1339 * reread configuration, terminate on errors
1341 static void com_reread_conf(void)
1343 char *cf = configfile_exists();
1344 struct gui_cmdline_parser_params params = {
1347 .check_required = 0,
1348 .check_ambiguity = 0,
1353 PARA_WARNING_LOG("there is no configuration to read\n");
1356 PARA_INFO_LOG("rereading command line options and config file\n");
1357 gui_cmdline_parser_ext(_argc, _argv, &conf, ¶ms);
1359 * Despite .print_errors is set to 0, gengetopt will print to stderr
1360 * anyway, and exit on errors. So we have to shutdown curses first.
1363 gui_cmdline_parser_config_file(cf, &conf, ¶ms);
1365 PARA_NOTICE_LOG("config file reloaded\n");
1366 if (check_key_map_args() < 0)
1367 finish(EXIT_FAILURE);
1370 static void com_help(void)
1374 for (i = 0; i < conf.key_map_given; ++i) {
1375 char *handler, *arg, *tmp = para_strdup(conf.key_map_arg[i]);
1376 const char *handler_text = "???", *desc = NULL;
1378 if (!split_key_map(tmp, &handler, &arg)) {
1384 handler_text = "internal";
1385 desc = command_list[find_cmd_byname(arg)].description;
1387 case 'x': handler_text = "external"; break;
1388 case 'd': handler_text = "display "; break;
1389 case 'p': handler_text = "para "; break;
1391 outputf(COLOR_MSG, "%s\t%s\t%s%s\t%s", tmp, handler_text, arg,
1392 strlen(arg) < 8? "\t" : "",
1396 for (i = 0; command_list[i].handler; i++) {
1397 struct gui_command gc = command_list[i];
1399 outputf(COLOR_MSG, "%s\tinternal\t%s\t%s%s", gc.key, gc.name,
1400 strlen(gc.name) < 8? "\t" : "",
1403 print_in_bar(COLOR_MSG, "try \"para_gui -h\" or \"para_client help\" "
1407 static void com_shrink_top_win(void)
1409 if (top.lines <= theme.top_lines_min) {
1410 PARA_WARNING_LOG("can not decrease top window\n");
1413 init_wins(top.lines - 1);
1416 print_in_bar(COLOR_MSG, "%s", "decreased top window");
1419 static void com_enlarge_top_win(void)
1421 if (bot.lines < 3) {
1422 PARA_WARNING_LOG("can not increase top window\n");
1425 init_wins(top.lines + 1);
1428 print_in_bar(COLOR_MSG, "increased top window");
1431 static void com_version(void)
1433 print_in_bar(COLOR_MSG, "%s", version_single_line("gui"));
1436 __noreturn static void com_quit(void)
1441 static void com_refresh(void)
1447 static void change_theme(int next)
1453 /* This seems to be needed twice, why? */
1456 PARA_NOTICE_LOG("new theme: %s\n", theme.name);
1459 static void com_next_theme(void)
1464 static void com_prev_theme(void)
1470 static void handle_command(int c)
1474 /* first check user-defined key bindings */
1475 for (i = 0; i < conf.key_map_given; ++i) {
1476 char *tmp, *handler, *arg;
1478 tmp = para_strdup(conf.key_map_arg[i]);
1479 if (!split_key_map(tmp, &handler, &arg)) {
1483 if (strcmp(tmp, km_keyname(c))) {
1487 if (*handler == 'd')
1489 else if (*handler == 'x')
1491 else if (*handler == 'p')
1493 else if (*handler == 'i') {
1494 int num = find_cmd_byname(arg);
1496 command_list[num].handler();
1501 /* not found, check internal key bindings */
1502 for (i = 0; command_list[i].handler; i++) {
1503 if (!strcmp(km_keyname(c), command_list[i].key)) {
1504 command_list[i].handler();
1508 print_in_bar(COLOR_ERRMSG, "key '%s' is not bound, press ? for help",
1512 __noreturn static void print_help_and_die(void)
1514 struct ggo_help h = DEFINE_GGO_HELP(gui);
1515 bool d = conf.detailed_help_given;
1517 ggo_print_help(&h, d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS);
1521 int main(int argc, char *argv[])
1529 gui_cmdline_parser(argc, argv, &conf); /* exits on errors */
1530 loglevel = get_loglevel_by_name(conf.loglevel_arg);
1531 version_handle_flag("gui", conf.version_given);
1532 if (conf.help_given || conf.detailed_help_given)
1533 print_help_and_die();
1534 cf = configfile_exists();
1535 if (!cf && conf.config_file_given) {
1536 fprintf(stderr, "can not read config file %s\n",
1537 conf.config_file_arg);
1541 struct gui_cmdline_parser_params params = {
1544 .check_required = 0,
1545 .check_ambiguity = 0,
1548 gui_cmdline_parser_config_file(cf, &conf, ¶ms);
1549 loglevel = get_loglevel_by_name(conf.loglevel_arg);
1551 if (check_key_map_args() < 0) {
1552 fprintf(stderr, "invalid key map\n");
1555 init_theme_or_die(conf.theme_arg, &theme);
1556 top.lines = theme.top_lines_default;
1557 setup_signal_handling();
1558 bot_win_rb = ringbuffer_new(RINGBUFFER_SIZE);
1559 setlocale(LC_CTYPE, "");
1560 initscr(); /* needed only once, always successful */
1565 ret = do_select(GETCH_MODE);
1568 print_in_bar(COLOR_MSG, " ");
1569 handle_command(ret);