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,
84 const char *description;
85 void (*handler)(void);
88 static struct gui_theme theme;
93 static void com_help(void);
94 static void com_reread_conf(void);
95 static void com_enlarge_top_win(void);
96 static void com_shrink_top_win(void);
97 static void com_version(void);
98 __noreturn static void com_quit(void);
99 static void com_refresh(void);
100 static void com_ll_incr(void);
101 static void com_ll_decr(void);
102 static void com_prev_theme(void);
103 static void com_next_theme(void);
104 static void com_scroll_up(void);
105 static void com_scroll_down(void);
106 static void com_page_up(void);
107 static void com_page_down(void);
108 static void com_cancel_scrolling(void);
109 static void com_scroll_top(void);
111 static struct gui_command command_list[] = {
115 .description = "print help",
119 .name = "enlarge_win",
120 .description = "enlarge the top window",
121 .handler = com_enlarge_top_win
124 .name = "shrink_win",
125 .description = "shrink the top window",
126 .handler = com_shrink_top_win
129 .name = "reread_conf",
130 .description = "reread configuration file",
131 .handler = com_reread_conf
135 .description = "exit para_gui",
140 .description = "redraw the screen",
141 .handler = com_refresh
144 .name = "next_theme",
145 .description = "switch to next theme",
146 .handler = com_next_theme
149 .name = "prev_theme",
150 .description = "switch to previous stream",
151 .handler = com_prev_theme
155 .description = "increase loglevel (decreases verbosity)",
156 .handler = com_ll_incr
160 .description = "decrease loglevel (increases verbosity)",
161 .handler = com_ll_decr
165 .description = "show the para_gui version",
166 .handler = com_version
170 .description = "scroll up one line",
171 .handler = com_scroll_up
174 .name = "scroll_down",
175 .description = "scroll down one line",
176 .handler = com_scroll_down
180 .description = "scroll up one page",
181 .handler = com_page_up
185 .description = "scroll down one page",
186 .handler = com_page_down
189 .name = "scroll_top",
190 .description = "scroll to top of buffer",
191 .handler = com_scroll_top
194 .name = "cancel_scroll",
195 .description = "deactivate scroll mode",
196 .handler = com_cancel_scrolling
202 static int find_cmd_byname(char *name)
206 for (i = 0; command_list[i].handler; i++)
207 if (!strcmp(command_list[i].name, name))
212 /* taken from mutt */
213 static char *km_keyname(int c)
218 sprintf(buf, "<up>");
222 sprintf(buf, "<down>");
226 sprintf(buf, "<left>");
229 if (c == KEY_RIGHT) {
230 sprintf(buf, "<right>");
233 if (c == KEY_NPAGE) {
234 sprintf(buf, "<npage>");
237 if (c == KEY_PPAGE) {
238 sprintf(buf, "<ppage>");
242 sprintf(buf, "<home>");
246 sprintf(buf, "<end>");
249 if (c < 256 && c > -128 && iscntrl((unsigned char) c)) {
254 buf[1] = (c + '@') & 0x7f;
257 snprintf(buf, sizeof(buf), "\\%d%d%d", c >> 6,
258 (c >> 3) & 7, c & 7);
259 } else if (c >= KEY_F0 && c < KEY_F(256))
260 sprintf(buf, "<F%d>", c - KEY_F0);
262 snprintf(buf, sizeof(buf), "%c", (unsigned char) c);
264 snprintf(buf, sizeof(buf), "\\x%hx", (unsigned short) c);
268 static char *configfile_exists(void)
270 static char *config_file;
273 if (!conf.config_file_given) {
275 char *home = para_homedir();
276 config_file = make_message("%s/.paraslash/gui.conf",
282 tmp = conf.config_file_arg;
283 return file_exists(tmp)? tmp: NULL;
286 /* Print given number of spaces to curses window. */
287 static void add_spaces(WINDOW* win, unsigned int num)
290 unsigned sz = sizeof(space) - 1; /* number of spaces */
304 * print aligned string to curses window. This function always prints
307 static int align_str(WINDOW* win, char *str, unsigned int len,
310 int ret, i, num; /* of spaces */
315 ret = strwidth(str, &width);
317 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
326 /* replace control characters by spaces */
327 for (i = 0; i < len && str[i]; i++) {
328 if (str[i] == '\n' || str[i] == '\r' || str[i] == '\f')
333 add_spaces(win, num);
334 } else if (align == RIGHT) {
335 add_spaces(win, num);
338 add_spaces(win, num / 2);
339 waddstr(win, str[0]? str: "");
340 add_spaces(win, num - num / 2);
345 __printf_2_3 static void print_in_bar(int color, const char *fmt,...)
352 wattron(in.win, COLOR_PAIR(color));
354 xvasprintf(&msg, fmt, ap);
357 align_str(in.win, msg, in.cols, LEFT);
362 static void print_status_bar(void)
366 tmp = para_strdup("para_gui " PACKAGE_VERSION " (hit ? for help)");
368 align_str(sb.win, tmp, sb.cols, CENTER);
373 * get the number of the oldest rbe that is (partially) visible. On return,
374 * lines contains the sum of the number of lines of all visible entries. If the
375 * first one is only partially visible, lines is greater than bot.lines.
377 static int first_visible_rbe(unsigned *lines)
381 for (i = scroll_position; i < RINGBUFFER_SIZE; i++) {
382 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
386 // fprintf(stderr, "found: %s\n", rbe->msg);
387 rbe_lines = NUM_LINES(rbe->len);
388 if (rbe_lines > bot.lines)
391 if (*lines >= bot.lines)
394 return RINGBUFFER_SIZE - 1;
398 returns number of first visible rbe, *lines is the number of lines drawn.
400 static int draw_top_rbe(unsigned *lines)
402 int ret, fvr = first_visible_rbe(lines);
403 struct rb_entry *rbe;
404 size_t bytes_to_skip, cells_to_skip, width;
408 wmove(bot.win, 0, 0);
409 rbe = ringbuffer_get(bot_win_rb, fvr);
412 if (*lines > bot.lines) {
413 /* rbe is partially visible multi-line */
414 cells_to_skip = (*lines - bot.lines) * bot.cols;
415 ret = skip_cells(rbe->msg, cells_to_skip, &bytes_to_skip);
418 ret = strwidth(rbe->msg + bytes_to_skip, &width);
425 wattron(bot.win, COLOR_PAIR(rbe->color));
426 waddstr(bot.win, rbe->msg + bytes_to_skip);
427 *lines = NUM_LINES(width);
431 static void redraw_bot_win(void)
436 wmove(bot.win, 0, 0);
438 i = draw_top_rbe(&lines);
441 while (i > 0 && lines < bot.lines) {
442 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, --i);
445 waddstr(bot.win, "\n");
448 lines += NUM_LINES(rbe->len);
449 wattron(bot.win, COLOR_PAIR(rbe->color));
450 waddstr(bot.win, "\n");
451 waddstr(bot.win, rbe->msg);
457 static void rb_add_entry(int color, char *msg)
459 struct rb_entry *old, *new;
463 if (strwidth(msg, &len) < 0)
465 new = para_malloc(sizeof(struct rb_entry));
469 old = ringbuffer_add(bot_win_rb, new);
470 // fprintf(stderr, "added: %s\n", new->msg);
475 if (scroll_position) {
476 /* discard current scrolling, like xterm does */
481 wattron(bot.win, COLOR_PAIR(color));
482 getyx(bot.win, y, x);
484 waddstr(bot.win, "\n");
485 waddstr(bot.win, msg);
489 * print formated output to bot win and refresh
491 __printf_2_3 static void outputf(int color, const char* fmt,...)
499 xvasprintf(&msg, fmt, ap);
501 rb_add_entry(color, msg);
505 static int add_output_line(char *line, void *data)
507 int color = *(int *)data? COLOR_ERRMSG : COLOR_OUTPUT;
511 rb_add_entry(color, para_strdup(line));
517 static __printf_2_3 void curses_log(int ll, const char *fmt,...)
523 if (ll < loglevel || !curses_active)
532 color = COLOR_ERRMSG;
535 xvasprintf(&msg, fmt, ap);
538 rb_add_entry(color, msg);
541 __printf_2_3 void (*para_log)(int, const char*, ...) = curses_log;
543 static void setup_signal_handling(void)
545 signal_pipe = para_signal_init();
546 para_install_sighandler(SIGINT);
547 para_install_sighandler(SIGTERM);
548 para_install_sighandler(SIGCHLD);
549 para_install_sighandler(SIGWINCH);
550 para_install_sighandler(SIGUSR1);
553 /* kill every process in the process group and exit */
554 __noreturn static void kill_pg_and_die(int ret)
556 para_sigaction(SIGTERM, SIG_IGN);
561 static void shutdown_curses(void)
566 curses_active = false;
570 __noreturn static void finish(int ret)
573 kill_pg_and_die(ret);
577 * exit curses and print given message to stdout/stderr
579 __noreturn __printf_2_3 static void msg_n_exit(int ret, const char* fmt, ...)
582 FILE *outfd = ret? stderr: stdout;
586 vfprintf(outfd, fmt, argp);
588 kill_pg_and_die(ret);
594 static void init_wins(int top_lines)
596 top.lines = top_lines;
601 bot.lines = LINES - top.lines - 3;
603 bot.begy = top.lines + 1;
618 sep.begy = top.lines;
621 assume_default_colors(theme.default_fg, theme.default_bg);
623 mvwin(top.win, top.begy, top.begx);
624 wresize(top.win, top.lines, top.cols);
626 mvwin(sb.win, sb.begy, sb.begx);
627 wresize(sb.win, sb.lines, sb.cols);
629 mvwin(sep.win, sep.begy, sep.begx);
630 wresize(sep.win, sep.lines, sep.cols);
632 mvwin(bot.win, bot.begy, bot.begx);
633 wresize(bot.win, bot.lines, bot.cols);
635 mvwin(in.win, in.begy, in.begx);
636 wresize(in.win, in.lines, in.cols);
638 sep.win = newwin(sep.lines, sep.cols, sep.begy, sep.begx);
639 top.win = newwin(top.lines, top.cols, top.begy, top.begx);
640 bot.win = newwin(bot.lines, bot.cols, bot.begy, bot.begx);
641 sb.win = newwin(sb.lines, sb.cols, sb.begy, sb.begx);
642 in.win = newwin(in.lines, in.cols, in.begy, in.begx);
643 if (!top.win || !bot.win || !sb.win || !in.win || !sep.win)
644 msg_n_exit(1, "Error: Cannot create curses windows\n");
648 scrollok(bot.win, 1);
649 wattron(sb.win, COLOR_PAIR(COLOR_STATUSBAR));
650 wattron(sep.win, COLOR_PAIR(COLOR_SEPARATOR));
651 wattron(bot.win, COLOR_PAIR(COLOR_BOT));
652 wattron(top.win, COLOR_PAIR(COLOR_TOP));
663 wmove(sep.win, 0, 0);
664 whline(sep.win, theme.sep_char, COLS);
667 wnoutrefresh(top.win);
668 wnoutrefresh(bot.win);
670 wnoutrefresh(sb.win);
671 wnoutrefresh(in.win);
672 wnoutrefresh(sep.win);
677 * Print stat item #i to curses window
679 static void print_stat_item(int i)
682 struct stat_item_data d = theme.data[i];
683 char *c = stat_content[i];
685 if (!curses_active || !d.len || !c)
687 tmp = make_message("%s%s%s", d.prefix, c, d.postfix);
688 wmove(top.win, d.y * top.lines / 100, d.x * COLS / 100);
690 wattron(top.win, COLOR_PAIR(i + 1));
691 align_str(top.win, tmp, d.len * COLS / 100, d.align);
696 static int update_item(int item_num, char *buf)
698 char **c = stat_content + item_num;
705 *c = para_strdup("(artist tag not set)");
708 *c = para_strdup("(title tag not set)");
711 *c = para_strdup("????");
714 *c = para_strdup("(album tag not set)");
717 *c = para_strdup("(comment tag not set)");
721 *c = para_strdup(buf);
723 print_stat_item(item_num);
727 static int read_stat_pipe(fd_set *rfds)
730 static int bufsize, loaded;
736 if (loaded >= bufsize) {
737 if (bufsize > 1000 * 1000) {
741 bufsize += bufsize + 1000;
742 buf = para_realloc(buf, bufsize);
744 assert(loaded < bufsize);
745 ret = read_nonblock(stat_pipe, buf + loaded, bufsize - loaded,
748 ret2 = for_each_stat_item(buf, loaded, update_item);
749 if (ret < 0 || ret2 < 0) {
751 return ret2 < 0? ret2 : ret;
753 sz = ret2; /* what is left */
754 if (sz > 0 && sz < loaded)
755 memmove(buf, buf + loaded - sz, sz);
760 static void print_all_items(void)
766 FOR_EACH_STATUS_ITEM(i)
770 static void clear_all_items(void)
774 FOR_EACH_STATUS_ITEM(i) {
775 free(stat_content[i]);
776 stat_content[i] = para_strdup("");
780 static void init_pair_or_die(short pair, short f, short b)
782 if (init_pair(pair, f, b) == ERR)
783 msg_n_exit(EXIT_FAILURE, "fatal: init_pair() failed\n");
786 static void init_colors_or_die(void)
791 msg_n_exit(EXIT_FAILURE, "fatal: No color term\n");
792 if (start_color() == ERR)
793 msg_n_exit(EXIT_FAILURE, "fatal: failed to start colors\n");
794 FOR_EACH_STATUS_ITEM(i)
795 if (theme.data[i].len)
796 init_pair_or_die(i + 1, theme.data[i].fg,
798 init_pair_or_die(COLOR_STATUSBAR, theme.sb_fg, theme.sb_bg);
799 init_pair_or_die(COLOR_COMMAND, theme.cmd_fg, theme.cmd_bg);
800 init_pair_or_die(COLOR_OUTPUT, theme.output_fg, theme.output_bg);
801 init_pair_or_die(COLOR_MSG, theme.msg_fg, theme.msg_bg);
802 init_pair_or_die(COLOR_ERRMSG, theme.err_msg_fg, theme.err_msg_bg);
803 init_pair_or_die(COLOR_SEPARATOR, theme.sep_fg, theme.sep_bg);
804 init_pair_or_die(COLOR_TOP, theme.default_fg, theme.default_bg);
805 init_pair_or_die(COLOR_BOT, theme.default_fg, theme.default_bg);
808 /* (Re-)initialize the curses library. */
809 static void init_curses(void)
811 curses_active = true;
812 if (top.win && refresh() == ERR) /* refresh is really needed */
813 msg_n_exit(EXIT_FAILURE, "refresh() failed\n");
814 if (LINES < theme.lines_min || COLS < theme.cols_min)
815 msg_n_exit(EXIT_FAILURE, "Error: Terminal (%dx%d) too small"
816 " (need at least %dx%d)\n", COLS, LINES,
817 theme.cols_min, theme.lines_min);
818 curs_set(0); /* make cursor invisible, ignore errors */
819 nonl(); /* do not NL->CR/NL on output, always returns OK */
820 /* don't echo input */
822 msg_n_exit(EXIT_FAILURE, "fatal: noecho() failed\n");
823 /* take input chars one at a time, no wait for \n */
825 msg_n_exit(EXIT_FAILURE, "fatal: cbreak() failed\n");
826 init_colors_or_die();
827 clear(); /* ignore non-fatal errors */
828 init_wins(theme.top_lines_default);
830 // noecho(); /* don't echo input */
833 static void check_sigchld(void)
839 ret = para_reap_child(&pid);
844 goto reap_next_child;
848 * This sucker modifies its first argument. *handler and *arg are
849 * pointers to 0-terminated strings (inside line). Crap.
851 static int split_key_map(char *line, char **handler, char **arg)
853 if (!(*handler = strchr(line + 1, ':')))
857 if (!(*arg = strchr(*handler, ':')))
866 static int check_key_map_args(void)
870 char *tmp = NULL, *handler, *arg;
872 for (i = 0; i < conf.key_map_given; ++i) {
873 s = conf.key_map_arg[i];
877 tmp = para_strdup(s);
878 if (!split_key_map(tmp, &handler, &arg))
880 if (strlen(handler) != 1)
889 if (find_cmd_byname(arg) < 0)
899 * React to various signal-related events
901 static void handle_signal(int sig)
905 msg_n_exit(EXIT_FAILURE,
906 "only the good die young (caught SIGTERM))\n");
916 PARA_WARNING_LOG("caught SIGINT, reset\n");
917 /* Nothing to do. SIGINT killed our child which gets noticed
918 * by do_select and resets everything.
922 PARA_NOTICE_LOG("got SIGUSR1, rereading configuration\n");
931 static void status_pre_select(fd_set *rfds, int *max_fileno, struct timeval *tv)
933 static struct timeval next_exec, atm, diff;
934 int ret, fds[3] = {0, 1, 0};
939 /* Avoid busy loop */
940 gettimeofday(&atm, NULL);
941 if (tv_diff(&next_exec, &atm, &diff) > 0) {
942 if (tv_diff(&diff, tv, NULL) < 0)
946 next_exec.tv_sec = atm.tv_sec + 2;
947 ret = para_exec_cmdline_pid(&pid, conf.stat_cmd_arg, fds);
950 ret = mark_fd_nonblocking(fds[1]);
957 para_fd_set(stat_pipe, rfds, max_fileno);
960 #define COMMAND_BUF_SIZE 32768
963 * This is the core select loop. Besides the (internal) signal
964 * pipe, the following other fds are checked according to the mode:
966 * GETCH_MODE: check stdin, return when key is pressed
968 * COMMAND_MODE: check command fds and stdin. Return when peer has closed both
969 * stdout and stderr or when any key is pressed.
971 * EXTERNAL_MODE: Check only signal pipe. Used when an external command
972 * is running. During that time curses is disabled. Returns when
975 static int do_select(int mode)
978 int ret, i, max_fileno;
979 char command_buf[2][COMMAND_BUF_SIZE] = {"", ""};
980 int cbo[2] = {0, 0}; /* command buf offsets */
982 unsigned flags[2] = {0, 0}; /* for for_each_line() */
985 tv.tv_sec = conf.timeout_arg / 1000;
986 tv.tv_usec = (conf.timeout_arg % 1000) * 1000;
987 // ret = refresh_status();
990 status_pre_select(&rfds, &max_fileno, &tv);
992 para_fd_set(signal_pipe, &rfds, &max_fileno);
993 /* command pipe only for COMMAND_MODE */
994 if (mode == COMMAND_MODE) {
995 if (command_fds[0] >= 0)
996 para_fd_set(command_fds[0], &rfds, &max_fileno);
997 if (command_fds[1] >= 0)
998 para_fd_set(command_fds[1], &rfds, &max_fileno);
1000 if (mode == GETCH_MODE || mode == COMMAND_MODE)
1001 para_fd_set(STDIN_FILENO, &rfds, &max_fileno);
1002 ret = para_select(max_fileno + 1, &rfds, NULL, &tv);
1004 goto check_return; /* skip fd checks */
1006 ret = para_next_signal(&rfds);
1009 /* read command pipe if ready */
1010 if (mode == COMMAND_MODE) {
1011 for (i = 0; i < 2; i++) {
1013 if (command_fds[i] < 0)
1015 ret = read_nonblock(command_fds[i],
1016 command_buf[i] + cbo[i],
1017 COMMAND_BUF_SIZE - 1 - cbo[i], &rfds, &sz);
1020 cbo[i] = for_each_line(flags[i], command_buf[i], cbo[i],
1021 add_output_line, &i);
1022 if (sz != cbo[i]) { /* at least one line found */
1027 PARA_NOTICE_LOG("closing command fd %d: %s\n",
1028 i, para_strerror(-ret));
1029 close(command_fds[i]);
1030 command_fds[i] = -1;
1033 if (command_fds[!i] < 0) /* both fds closed */
1036 if (cbo[i] == COMMAND_BUF_SIZE - 1) {
1037 PARA_NOTICE_LOG("discarding overlong line\n");
1039 flags[i] = FELF_DISCARD_FIRST;
1043 ret = read_stat_pipe(&rfds);
1045 PARA_NOTICE_LOG("closing stat pipe: %s\n", para_strerror(-ret));
1049 free(stat_content[SI_BASENAME]);
1050 stat_content[SI_BASENAME] =
1051 para_strdup("stat command terminated!?");
1057 ret = wgetch(top.win);
1058 if (ret != ERR && ret != KEY_RESIZE) {
1060 kill(cmd_pid, SIGTERM);
1065 ret = wgetch(top.win);
1066 if (ret != ERR && ret != KEY_RESIZE)
1076 /* read from command pipe and print data to bot window */
1077 static void exec_and_display_cmd(const char *cmd)
1079 int ret, fds[3] = {0, 1, 1};
1081 outputf(COLOR_COMMAND, "%s", cmd);
1082 ret = para_exec_cmdline_pid(&cmd_pid, cmd, fds);
1085 ret = mark_fd_nonblocking(fds[1]);
1088 ret = mark_fd_nonblocking(fds[2]);
1091 command_fds[0] = fds[1];
1092 command_fds[1] = fds[2];
1093 if (do_select(COMMAND_MODE) >= 0)
1094 PARA_INFO_LOG("command complete\n");
1096 PARA_NOTICE_LOG("command aborted\n");
1097 print_in_bar(COLOR_MSG, " ");
1100 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
1101 close(command_fds[0]);
1102 close(command_fds[1]);
1105 static void para_cmd(char *cmd)
1109 print_in_bar(COLOR_MSG, "executing client command, hit any key to abort\n");
1110 c = make_message(BINDIR "/para_client -- %s", cmd);
1111 exec_and_display_cmd(c);
1115 static void display_cmd(char *cmd)
1117 print_in_bar(COLOR_MSG, "executing display command, hit any key to abort");
1118 exec_and_display_cmd(cmd);
1122 * shutdown curses and stat pipe before executing external commands
1124 static void external_cmd(char *cmd)
1126 int fds[3] = {-1, -1, -1};
1131 if (para_exec_cmdline_pid(&cmd_pid, cmd, fds) < 0)
1133 do_select(EXTERNAL_MODE);
1137 static void print_scroll_msg(void)
1139 unsigned lines_total, filled = ringbuffer_filled(bot_win_rb);
1140 int first_rbe = first_visible_rbe(&lines_total);
1142 print_in_bar(COLOR_MSG, "scrolled view: %d-%d/%d\n", filled - first_rbe,
1143 filled - scroll_position, ringbuffer_filled(bot_win_rb));
1146 static void com_scroll_top(void)
1148 int i = RINGBUFFER_SIZE - 1;
1151 while (i > 0 && !ringbuffer_get(bot_win_rb, i))
1153 /* i is oldest entry */
1154 for (; lines < bot.lines && i >= 0; i--) {
1155 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
1158 lines += NUM_LINES(rbe->len);
1161 if (lines > 0 && scroll_position != i) {
1162 scroll_position = i;
1167 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1170 static void com_cancel_scrolling(void)
1173 if (scroll_position == 0) {
1174 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1177 scroll_position = 0;
1181 static void com_page_down(void)
1184 int i = scroll_position;
1186 while (lines < bot.lines && --i > 0) {
1187 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
1190 lines += NUM_LINES(rbe->len);
1193 scroll_position = i;
1198 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1201 static void com_page_up(void)
1204 int fvr = first_visible_rbe(&lines);
1206 if (fvr < 0 || fvr + 1 >= ringbuffer_filled(bot_win_rb)) {
1207 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1210 scroll_position = fvr + 1;
1211 for (; scroll_position > 0; scroll_position--) {
1212 first_visible_rbe(&lines);
1213 if (lines == bot.lines)
1220 static void com_scroll_down(void)
1222 struct rb_entry *rbe;
1225 if (!scroll_position) {
1226 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1230 rbe = ringbuffer_get(bot_win_rb, scroll_position);
1231 rbe_lines = NUM_LINES(rbe->len);
1232 wscrl(bot.win, rbe_lines);
1233 wmove(bot.win, bot.lines - rbe_lines, 0);
1234 wattron(bot.win, COLOR_PAIR(rbe->color));
1235 waddstr(bot.win, rbe->msg);
1240 static void com_scroll_up(void)
1242 struct rb_entry *rbe = NULL;
1244 int i, first_rbe, num_scroll;
1246 /* the entry that is going to vanish */
1247 rbe = ringbuffer_get(bot_win_rb, scroll_position);
1250 num_scroll = NUM_LINES(rbe->len);
1251 first_rbe = first_visible_rbe(&lines);
1252 if (first_rbe < 0 || (first_rbe == ringbuffer_filled(bot_win_rb) - 1))
1255 wscrl(bot.win, -num_scroll);
1256 i = draw_top_rbe(&lines);
1259 while (i > 0 && lines < num_scroll) {
1261 rbe = ringbuffer_get(bot_win_rb, --i);
1264 rbe_lines = NUM_LINES(rbe->len);
1266 // fprintf(stderr, "msg: %s\n", rbe->msg);
1267 wattron(bot.win, COLOR_PAIR(rbe->color));
1268 waddstr(bot.win, "\n");
1269 waddstr(bot.win, rbe->msg);
1278 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1281 static void com_ll_decr(void)
1283 if (loglevel <= LL_DEBUG) {
1284 print_in_bar(COLOR_ERRMSG,
1285 "loglevel already at maximal verbosity\n");
1289 print_in_bar(COLOR_MSG, "loglevel set to %d\n", loglevel);
1292 static void com_ll_incr(void)
1294 if (loglevel >= LL_EMERG) {
1295 print_in_bar(COLOR_ERRMSG,
1296 "loglevel already at minimal verbosity\n");
1300 print_in_bar(COLOR_MSG, "loglevel set to %d\n", loglevel);
1304 * reread configuration, terminate on errors
1306 static void com_reread_conf(void)
1308 char *cf = configfile_exists();
1309 struct gui_cmdline_parser_params params = {
1312 .check_required = 0,
1313 .check_ambiguity = 0,
1318 PARA_WARNING_LOG("there is no configuration to read\n");
1321 PARA_INFO_LOG("rereading command line options and config file\n");
1322 gui_cmdline_parser_ext(_argc, _argv, &conf, ¶ms);
1324 * Despite .print_errors is set to 0, gengetopt will print to stderr
1325 * anyway, and exit on errors. So we have to shutdown curses first.
1328 gui_cmdline_parser_config_file(cf, &conf, ¶ms);
1330 PARA_NOTICE_LOG("config file reloaded\n");
1331 if (check_key_map_args() < 0)
1332 finish(EXIT_FAILURE);
1335 static void com_help(void)
1339 for (i = 0; i < conf.key_map_given; ++i) {
1340 char *handler, *arg, *tmp = para_strdup(conf.key_map_arg[i]);
1341 const char *handler_text = "???", *desc = NULL;
1343 if (!split_key_map(tmp, &handler, &arg)) {
1349 handler_text = "internal";
1350 desc = command_list[find_cmd_byname(arg)].description;
1352 case 'x': handler_text = "external"; break;
1353 case 'd': handler_text = "display "; break;
1354 case 'p': handler_text = "para "; break;
1356 outputf(COLOR_MSG, "%s\t%s\t%s%s\t%s", tmp, handler_text, arg,
1357 strlen(arg) < 8? "\t" : "",
1361 for (i = 0; command_list[i].handler; i++) {
1362 struct gui_command gc = command_list[i];
1364 outputf(COLOR_MSG, "%s\tinternal\t%s\t%s%s", gc.key, gc.name,
1365 strlen(gc.name) < 8? "\t" : "",
1368 print_in_bar(COLOR_MSG, "try \"para_gui -h\" or \"para_client help\" "
1372 static void com_shrink_top_win(void)
1374 if (top.lines <= theme.top_lines_min) {
1375 PARA_WARNING_LOG("can not decrease top window\n");
1378 init_wins(top.lines - 1);
1381 print_in_bar(COLOR_MSG, "%s", "decreased top window");
1384 static void com_enlarge_top_win(void)
1386 if (bot.lines < 3) {
1387 PARA_WARNING_LOG("can not increase top window\n");
1390 init_wins(top.lines + 1);
1393 print_in_bar(COLOR_MSG, "increased top window");
1396 static void com_version(void)
1398 print_in_bar(COLOR_MSG, "%s", version_single_line("gui"));
1401 __noreturn static void com_quit(void)
1406 static void com_refresh(void)
1412 static void change_theme(int next)
1418 /* This seems to be needed twice, why? */
1421 PARA_NOTICE_LOG("new theme: %s\n", theme.name);
1424 static void com_next_theme(void)
1429 static void com_prev_theme(void)
1435 static void handle_command(int c)
1439 /* first check user-defined key bindings */
1440 for (i = 0; i < conf.key_map_given; ++i) {
1441 char *tmp, *handler, *arg;
1443 tmp = para_strdup(conf.key_map_arg[i]);
1444 if (!split_key_map(tmp, &handler, &arg)) {
1448 if (strcmp(tmp, km_keyname(c))) {
1452 if (*handler == 'd')
1454 else if (*handler == 'x')
1456 else if (*handler == 'p')
1458 else if (*handler == 'i') {
1459 int num = find_cmd_byname(arg);
1461 command_list[num].handler();
1466 /* not found, check internal key bindings */
1467 for (i = 0; command_list[i].handler; i++) {
1468 if (!strcmp(km_keyname(c), command_list[i].key)) {
1469 command_list[i].handler();
1473 print_in_bar(COLOR_ERRMSG, "key '%s' is not bound, press ? for help",
1477 __noreturn static void print_help_and_die(void)
1479 struct ggo_help h = DEFINE_GGO_HELP(gui);
1480 bool d = conf.detailed_help_given;
1482 ggo_print_help(&h, d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS);
1486 int main(int argc, char *argv[])
1494 gui_cmdline_parser(argc, argv, &conf); /* exits on errors */
1495 loglevel = get_loglevel_by_name(conf.loglevel_arg);
1496 version_handle_flag("gui", conf.version_given);
1497 if (conf.help_given || conf.detailed_help_given)
1498 print_help_and_die();
1499 cf = configfile_exists();
1500 if (!cf && conf.config_file_given) {
1501 fprintf(stderr, "can not read config file %s\n",
1502 conf.config_file_arg);
1506 struct gui_cmdline_parser_params params = {
1509 .check_required = 0,
1510 .check_ambiguity = 0,
1513 gui_cmdline_parser_config_file(cf, &conf, ¶ms);
1514 loglevel = get_loglevel_by_name(conf.loglevel_arg);
1516 if (check_key_map_args() < 0) {
1517 fprintf(stderr, "invalid key map\n");
1520 init_theme_or_die(conf.theme_arg, &theme);
1521 setup_signal_handling();
1522 bot_win_rb = ringbuffer_new(RINGBUFFER_SIZE);
1523 setlocale(LC_CTYPE, "");
1524 initscr(); /* needed only once, always successful */
1528 ret = do_select(GETCH_MODE);
1531 print_in_bar(COLOR_MSG, " ");
1532 handle_command(ret);