2 * Copyright (C) 1998-2014 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file gui.c Curses-based interface for paraslash. */
11 #include <sys/types.h>
16 #include "gui.cmdline.h"
20 #include "ringbuffer.h"
29 /** define the array of error lists needed by para_gui */
31 static char *stat_content[NUM_STAT_ITEMS];
33 static int signal_pipe;
35 static struct gui_window {
39 } top, bot, sb, in, sep;
41 #define RINGBUFFER_SIZE 512
47 static struct ringbuffer *bot_win_rb;
48 #define NUM_LINES(len) (1 + (len) / bot.cols)
50 static unsigned scroll_position;
54 static int command_fds[2] = {-1, -1};
55 static int stat_pipe = -1;
56 static struct gui_args_info conf;
58 enum {GETCH_MODE, COMMAND_MODE, EXTERNAL_MODE};
62 * Codes for various colors.
64 * Each status item has its own color pair. The ones defined here start at a
65 * higher number so that they do not overlap with these.
68 COLOR_STATUSBAR = NUM_STAT_ITEMS + 1,
81 const char *description;
82 void (*handler)(void);
85 static struct gui_theme theme;
87 static void com_help(void);
88 static void com_reread_conf(void);
89 static void com_enlarge_top_win(void);
90 static void com_shrink_top_win(void);
91 static void com_version(void);
92 __noreturn static void com_quit(void);
93 static void com_refresh(void);
94 static void com_ll_incr(void);
95 static void com_ll_decr(void);
96 static void com_prev_theme(void);
97 static void com_next_theme(void);
98 static void com_scroll_up(void);
99 static void com_scroll_down(void);
100 static void com_page_up(void);
101 static void com_page_down(void);
102 static void com_cancel_scrolling(void);
103 static void com_scroll_top(void);
105 static struct gui_command command_list[] = {
109 .description = "print help",
113 .name = "enlarge_win",
114 .description = "enlarge the top window",
115 .handler = com_enlarge_top_win
118 .name = "shrink_win",
119 .description = "shrink the top window",
120 .handler = com_shrink_top_win
123 .name = "reread_conf",
124 .description = "reread configuration file",
125 .handler = com_reread_conf
129 .description = "exit para_gui",
134 .description = "redraw the screen",
135 .handler = com_refresh
138 .name = "next_theme",
139 .description = "switch to next theme",
140 .handler = com_next_theme
143 .name = "prev_theme",
144 .description = "switch to previous stream",
145 .handler = com_prev_theme
149 .description = "increase loglevel (decreases verbosity)",
150 .handler = com_ll_incr
154 .description = "decrease loglevel (increases verbosity)",
155 .handler = com_ll_decr
159 .description = "show the para_gui version",
160 .handler = com_version
164 .description = "scroll up one line",
165 .handler = com_scroll_up
168 .name = "scroll_down",
169 .description = "scroll down one line",
170 .handler = com_scroll_down
174 .description = "scroll up one page",
175 .handler = com_page_up
179 .description = "scroll down one page",
180 .handler = com_page_down
183 .name = "scroll_top",
184 .description = "scroll to top of buffer",
185 .handler = com_scroll_top
188 .name = "cancel_scroll",
189 .description = "deactivate scroll mode",
190 .handler = com_cancel_scrolling
196 static int find_cmd_byname(char *name)
200 for (i = 0; command_list[i].handler; i++)
201 if (!strcmp(command_list[i].name, name))
206 /* isendwin() returns false before initscr() was called */
207 static bool curses_active(void)
209 return top.win && !isendwin();
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,...)
350 if (!curses_active())
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 rbe_lines = NUM_LINES(rbe->len);
387 if (rbe_lines > bot.lines)
390 if (*lines >= bot.lines)
393 return RINGBUFFER_SIZE - 1;
397 returns number of first visible rbe, *lines is the number of lines drawn.
399 static int draw_top_rbe(unsigned *lines)
401 int ret, fvr = first_visible_rbe(lines);
402 struct rb_entry *rbe;
403 size_t bytes_to_skip, cells_to_skip, width;
407 wmove(bot.win, 0, 0);
408 rbe = ringbuffer_get(bot_win_rb, fvr);
411 if (*lines > bot.lines) {
412 /* rbe is partially visible multi-line */
413 cells_to_skip = (*lines - bot.lines) * bot.cols;
414 ret = skip_cells(rbe->msg, cells_to_skip, &bytes_to_skip);
417 ret = strwidth(rbe->msg + bytes_to_skip, &width);
424 wattron(bot.win, COLOR_PAIR(rbe->color));
425 waddstr(bot.win, rbe->msg + bytes_to_skip);
426 *lines = NUM_LINES(width);
430 static void redraw_bot_win(void)
435 wmove(bot.win, 0, 0);
437 i = draw_top_rbe(&lines);
440 while (i > 0 && lines < bot.lines) {
441 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, --i);
444 waddstr(bot.win, "\n");
447 lines += NUM_LINES(rbe->len);
448 wattron(bot.win, COLOR_PAIR(rbe->color));
449 waddstr(bot.win, "\n");
450 waddstr(bot.win, rbe->msg);
456 static void rb_add_entry(int color, char *msg)
458 struct rb_entry *old, *new;
462 if (strwidth(msg, &len) < 0)
464 new = para_malloc(sizeof(struct rb_entry));
468 old = ringbuffer_add(bot_win_rb, new);
473 if (scroll_position) {
474 /* discard current scrolling, like xterm does */
479 wattron(bot.win, COLOR_PAIR(color));
480 getyx(bot.win, y, x);
482 waddstr(bot.win, "\n");
483 waddstr(bot.win, msg);
487 * print formated output to bot win and refresh
489 __printf_2_3 static void outputf(int color, const char* fmt,...)
494 if (!curses_active())
497 xvasprintf(&msg, fmt, ap);
499 rb_add_entry(color, msg);
503 static int add_output_line(char *line, void *data)
505 int color = *(int *)data? COLOR_ERRMSG : COLOR_OUTPUT;
507 if (!curses_active())
509 rb_add_entry(color, para_strdup(line));
515 static __printf_2_3 void curses_log(int ll, const char *fmt,...)
522 if (curses_active()) {
523 int color = ll <= LL_NOTICE? COLOR_MSG : COLOR_ERRMSG;
525 unsigned bytes = xvasprintf(&msg, fmt, ap);
526 if (bytes > 0 && msg[bytes - 1] == '\n')
527 msg[bytes - 1] = '\0'; /* cut trailing newline */
528 rb_add_entry(color, msg);
530 } else if (cmd_pid <= 0) /* no external command running */
531 vfprintf(stderr, fmt, ap);
534 __printf_2_3 void (*para_log)(int, const char*, ...) = curses_log;
536 static void setup_signal_handling(void)
538 signal_pipe = para_signal_init();
539 para_install_sighandler(SIGINT);
540 para_install_sighandler(SIGTERM);
541 para_install_sighandler(SIGCHLD);
542 para_install_sighandler(SIGWINCH);
543 para_install_sighandler(SIGUSR1);
546 static void shutdown_curses(void)
552 /* disable curses, print a message, kill running processes and exit */
553 __noreturn __printf_2_3 static void die(int exit_code, const char* fmt, ...)
559 vfprintf(stderr, fmt, argp);
561 /* kill every process in the process group and exit */
562 para_sigaction(SIGTERM, SIG_IGN);
568 * Print stat item #i to curses window
570 static void print_stat_item(int i)
573 struct stat_item_data d = theme.data[i];
574 char *c = stat_content[i];
576 if (!curses_active() || !d.len || !c)
578 tmp = make_message("%s%s%s", d.prefix, c, d.postfix);
579 wmove(top.win, d.y * top.lines / 100, d.x * COLS / 100);
581 wattron(top.win, COLOR_PAIR(i + 1));
582 align_str(top.win, tmp, d.len * COLS / 100, d.align);
587 static int update_item(int item_num, char *buf)
589 char **c = stat_content + item_num;
596 *c = para_strdup("(artist tag not set)");
599 *c = para_strdup("(title tag not set)");
602 *c = para_strdup("????");
605 *c = para_strdup("(album tag not set)");
608 *c = para_strdup("(comment tag not set)");
612 *c = para_strdup(buf);
614 print_stat_item(item_num);
618 static int read_stat_pipe(fd_set *rfds)
621 static int bufsize, loaded;
627 if (loaded >= bufsize) {
628 if (bufsize > 1000 * 1000) {
632 bufsize += bufsize + 1000;
633 buf = para_realloc(buf, bufsize);
635 assert(loaded < bufsize);
636 ret = read_nonblock(stat_pipe, buf + loaded, bufsize - loaded,
639 ret2 = for_each_stat_item(buf, loaded, update_item);
640 if (ret < 0 || ret2 < 0) {
642 return ret2 < 0? ret2 : ret;
644 sz = ret2; /* what is left */
645 if (sz > 0 && sz < loaded)
646 memmove(buf, buf + loaded - sz, sz);
651 static void print_all_items(void)
655 if (!curses_active())
657 FOR_EACH_STATUS_ITEM(i)
661 static void clear_all_items(void)
665 FOR_EACH_STATUS_ITEM(i) {
666 free(stat_content[i]);
667 stat_content[i] = para_strdup("");
674 static void init_wins(int top_lines)
676 int top_y = 0, bot_y = top_lines + 1, sb_y = LINES - 2,
677 in_y = LINES - 1, sep_y = top_lines;
679 top.lines = top_lines;
680 bot.lines = LINES - top.lines - 3;
681 sb.lines = in.lines = sep.lines = 1;
683 top.cols = bot.cols = sb.cols = in.cols = sep.cols = COLS;
685 assume_default_colors(theme.default_fg, theme.default_bg);
687 wresize(top.win, top.lines, top.cols);
688 mvwin(top.win, top_y, 0);
690 wresize(sb.win, sb.lines, sb.cols);
691 mvwin(sb.win, sb_y, 0);
693 wresize(sep.win, sep.lines, sep.cols);
694 mvwin(sep.win, sep_y, 0);
696 wresize(bot.win, bot.lines, bot.cols);
697 mvwin(bot.win, bot_y, 0);
699 wresize(in.win, in.lines, in.cols);
700 mvwin(in.win, in_y, 0);
702 sep.win = newwin(sep.lines, sep.cols, sep_y, 0);
703 top.win = newwin(top.lines, top.cols, top_y, 0);
704 bot.win = newwin(bot.lines, bot.cols, bot_y, 0);
705 sb.win = newwin(sb.lines, sb.cols, sb_y, 0);
706 in.win = newwin(in.lines, in.cols, in_y, 0);
707 if (!top.win || !bot.win || !sb.win || !in.win || !sep.win)
708 die(EXIT_FAILURE, "Error: Cannot create curses windows\n");
712 scrollok(bot.win, 1);
713 wattron(sb.win, COLOR_PAIR(COLOR_STATUSBAR));
714 wattron(sep.win, COLOR_PAIR(COLOR_SEPARATOR));
715 wattron(bot.win, COLOR_PAIR(COLOR_BOT));
716 wattron(top.win, COLOR_PAIR(COLOR_TOP));
727 wmove(sep.win, 0, 0);
728 whline(sep.win, theme.sep_char, COLS);
732 wnoutrefresh(top.win);
733 wnoutrefresh(bot.win);
735 wnoutrefresh(sb.win);
736 wnoutrefresh(in.win);
737 wnoutrefresh(sep.win);
741 static void init_pair_or_die(short pair, short f, short b)
743 if (init_pair(pair, f, b) == ERR)
744 die(EXIT_FAILURE, "fatal: init_pair() failed\n");
747 static void init_colors_or_die(void)
752 die(EXIT_FAILURE, "fatal: No color term\n");
753 if (start_color() == ERR)
754 die(EXIT_FAILURE, "fatal: failed to start colors\n");
755 FOR_EACH_STATUS_ITEM(i)
756 if (theme.data[i].len)
757 init_pair_or_die(i + 1, theme.data[i].fg,
759 init_pair_or_die(COLOR_STATUSBAR, theme.sb_fg, theme.sb_bg);
760 init_pair_or_die(COLOR_COMMAND, theme.cmd_fg, theme.cmd_bg);
761 init_pair_or_die(COLOR_OUTPUT, theme.output_fg, theme.output_bg);
762 init_pair_or_die(COLOR_MSG, theme.msg_fg, theme.msg_bg);
763 init_pair_or_die(COLOR_ERRMSG, theme.err_msg_fg, theme.err_msg_bg);
764 init_pair_or_die(COLOR_SEPARATOR, theme.sep_fg, theme.sep_bg);
765 init_pair_or_die(COLOR_TOP, theme.default_fg, theme.default_bg);
766 init_pair_or_die(COLOR_BOT, theme.default_fg, theme.default_bg);
769 /* (Re-)initialize the curses library. */
770 static void init_curses(void)
774 if (top.win && refresh() == ERR) /* refresh is really needed */
775 die(EXIT_FAILURE, "refresh() failed\n");
776 if (LINES < theme.lines_min || COLS < theme.cols_min)
777 die(EXIT_FAILURE, "Terminal (%dx%d) too small"
778 " (need at least %dx%d)\n", COLS, LINES,
779 theme.cols_min, theme.lines_min);
780 curs_set(0); /* make cursor invisible, ignore errors */
781 nonl(); /* do not NL->CR/NL on output, always returns OK */
782 /* don't echo input */
784 die(EXIT_FAILURE, "fatal: noecho() failed\n");
785 /* take input chars one at a time, no wait for \n */
787 die(EXIT_FAILURE, "fatal: cbreak() failed\n");
788 init_colors_or_die();
789 clear(); /* ignore non-fatal errors */
790 init_wins(theme.top_lines_default);
791 // noecho(); /* don't echo input */
794 static void check_sigchld(void)
800 ret = para_reap_child(&pid);
805 goto reap_next_child;
809 * This sucker modifies its first argument. *handler and *arg are
810 * pointers to 0-terminated strings (inside line). Crap.
812 static int split_key_map(char *line, char **handler, char **arg)
814 if (!(*handler = strchr(line + 1, ':')))
818 if (!(*arg = strchr(*handler, ':')))
827 static void check_key_map_args_or_die(void)
832 for (i = 0; i < conf.key_map_given; ++i) {
836 tmp = para_strdup(conf.key_map_arg[i]);
837 if (!split_key_map(tmp, &handler, &arg))
839 if (strlen(handler) != 1)
841 if (*handler != 'x' && *handler != 'd' && *handler != 'i'
846 if (find_cmd_byname(arg) < 0)
849 if (i != conf.key_map_given)
850 die(EXIT_FAILURE, "invalid key map: %s\n", conf.key_map_arg[i]);
855 * React to various signal-related events
857 static void handle_signal(int sig)
861 die(EXIT_FAILURE, "only the good die young (caught SIGTERM)\n");
864 if (curses_active()) {
871 PARA_WARNING_LOG("caught SIGINT, reset\n");
872 /* Nothing to do. SIGINT killed our child which gets noticed
873 * by do_select and resets everything.
877 PARA_NOTICE_LOG("got SIGUSR1, rereading configuration\n");
886 static void status_pre_select(fd_set *rfds, int *max_fileno, struct timeval *tv)
888 static struct timeval next_exec, atm, diff;
889 int ret, fds[3] = {0, 1, 0};
894 /* Avoid busy loop */
895 gettimeofday(&atm, NULL);
896 if (tv_diff(&next_exec, &atm, &diff) > 0) {
897 if (tv_diff(&diff, tv, NULL) < 0)
901 next_exec.tv_sec = atm.tv_sec + 2;
902 ret = para_exec_cmdline_pid(&pid, conf.stat_cmd_arg, fds);
905 ret = mark_fd_nonblocking(fds[1]);
912 para_fd_set(stat_pipe, rfds, max_fileno);
915 #define COMMAND_BUF_SIZE 32768
918 * This is the core select loop. Besides the (internal) signal
919 * pipe, the following other fds are checked according to the mode:
921 * GETCH_MODE: check stdin, return when key is pressed
923 * COMMAND_MODE: check command fds and stdin. Return when peer has closed both
924 * stdout and stderr or when any key is pressed.
926 * EXTERNAL_MODE: Check only signal pipe. Used when an external command
927 * is running. During that time curses is disabled. Returns when
930 static int do_select(int mode)
933 int ret, i, max_fileno;
934 char command_buf[2][COMMAND_BUF_SIZE] = {"", ""};
935 int cbo[2] = {0, 0}; /* command buf offsets */
937 unsigned flags[2] = {0, 0}; /* for for_each_line() */
940 tv.tv_sec = conf.timeout_arg / 1000;
941 tv.tv_usec = (conf.timeout_arg % 1000) * 1000;
942 // ret = refresh_status();
945 status_pre_select(&rfds, &max_fileno, &tv);
947 para_fd_set(signal_pipe, &rfds, &max_fileno);
948 /* command pipe only for COMMAND_MODE */
949 if (mode == COMMAND_MODE) {
950 if (command_fds[0] >= 0)
951 para_fd_set(command_fds[0], &rfds, &max_fileno);
952 if (command_fds[1] >= 0)
953 para_fd_set(command_fds[1], &rfds, &max_fileno);
955 if (mode == GETCH_MODE || mode == COMMAND_MODE)
956 para_fd_set(STDIN_FILENO, &rfds, &max_fileno);
957 ret = para_select(max_fileno + 1, &rfds, NULL, &tv);
959 goto check_return; /* skip fd checks */
961 ret = para_next_signal(&rfds);
964 /* read command pipe if ready */
965 if (mode == COMMAND_MODE) {
966 for (i = 0; i < 2; i++) {
968 if (command_fds[i] < 0)
970 ret = read_nonblock(command_fds[i],
971 command_buf[i] + cbo[i],
972 COMMAND_BUF_SIZE - 1 - cbo[i], &rfds, &sz);
975 cbo[i] = for_each_line(flags[i], command_buf[i], cbo[i],
976 add_output_line, &i);
977 if (sz != cbo[i]) { /* at least one line found */
982 PARA_NOTICE_LOG("closing command fd %d: %s\n",
983 i, para_strerror(-ret));
984 close(command_fds[i]);
988 if (command_fds[!i] < 0) /* both fds closed */
991 if (cbo[i] == COMMAND_BUF_SIZE - 1) {
992 PARA_NOTICE_LOG("discarding overlong line\n");
994 flags[i] = FELF_DISCARD_FIRST;
998 ret = read_stat_pipe(&rfds);
1000 PARA_NOTICE_LOG("closing stat pipe: %s\n", para_strerror(-ret));
1004 free(stat_content[SI_BASENAME]);
1005 stat_content[SI_BASENAME] =
1006 para_strdup("stat command terminated!?");
1012 ret = wgetch(top.win);
1013 if (ret != ERR && ret != KEY_RESIZE) {
1015 kill(cmd_pid, SIGTERM);
1020 ret = wgetch(top.win);
1021 if (ret != ERR && ret != KEY_RESIZE)
1031 /* read from command pipe and print data to bot window */
1032 static void exec_and_display_cmd(const char *cmd)
1034 int ret, fds[3] = {0, 1, 1};
1036 outputf(COLOR_COMMAND, "%s", cmd);
1037 ret = para_exec_cmdline_pid(&cmd_pid, cmd, fds);
1040 ret = mark_fd_nonblocking(fds[1]);
1043 ret = mark_fd_nonblocking(fds[2]);
1046 command_fds[0] = fds[1];
1047 command_fds[1] = fds[2];
1048 if (do_select(COMMAND_MODE) >= 0)
1049 PARA_INFO_LOG("command complete\n");
1051 PARA_NOTICE_LOG("command aborted\n");
1052 print_in_bar(COLOR_MSG, " ");
1055 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
1056 close(command_fds[0]);
1057 close(command_fds[1]);
1060 static void para_cmd(char *cmd)
1064 print_in_bar(COLOR_MSG, "executing client command, hit any key to abort\n");
1065 c = make_message(BINDIR "/para_client -- %s", cmd);
1066 exec_and_display_cmd(c);
1070 static void display_cmd(char *cmd)
1072 print_in_bar(COLOR_MSG, "executing display command, hit any key to abort");
1073 exec_and_display_cmd(cmd);
1077 * shutdown curses and stat pipe before executing external commands
1079 static void external_cmd(char *cmd)
1081 int fds[3] = {-1, -1, -1};
1086 if (para_exec_cmdline_pid(&cmd_pid, cmd, fds) < 0)
1088 do_select(EXTERNAL_MODE);
1092 static void print_scroll_msg(void)
1094 unsigned lines_total, filled = ringbuffer_filled(bot_win_rb);
1095 int first_rbe = first_visible_rbe(&lines_total);
1097 print_in_bar(COLOR_MSG, "scrolled view: %d-%d/%d\n", filled - first_rbe,
1098 filled - scroll_position, ringbuffer_filled(bot_win_rb));
1101 static void com_scroll_top(void)
1103 int i = RINGBUFFER_SIZE - 1;
1106 while (i > 0 && !ringbuffer_get(bot_win_rb, i))
1108 /* i is oldest entry */
1109 for (; lines < bot.lines && i >= 0; i--) {
1110 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
1113 lines += NUM_LINES(rbe->len);
1116 if (lines > 0 && scroll_position != i) {
1117 scroll_position = i;
1122 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1125 static void com_cancel_scrolling(void)
1128 if (scroll_position == 0) {
1129 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1132 scroll_position = 0;
1136 static void com_page_down(void)
1139 int i = scroll_position;
1141 while (lines < bot.lines && --i > 0) {
1142 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
1145 lines += NUM_LINES(rbe->len);
1148 scroll_position = i;
1153 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1156 static void com_page_up(void)
1159 int fvr = first_visible_rbe(&lines);
1161 if (fvr < 0 || fvr + 1 >= ringbuffer_filled(bot_win_rb)) {
1162 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1165 scroll_position = fvr + 1;
1166 for (; scroll_position > 0; scroll_position--) {
1167 first_visible_rbe(&lines);
1168 if (lines == bot.lines)
1175 static void com_scroll_down(void)
1177 struct rb_entry *rbe;
1180 if (!scroll_position) {
1181 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1185 rbe = ringbuffer_get(bot_win_rb, scroll_position);
1186 rbe_lines = NUM_LINES(rbe->len);
1187 wscrl(bot.win, rbe_lines);
1188 wmove(bot.win, bot.lines - rbe_lines, 0);
1189 wattron(bot.win, COLOR_PAIR(rbe->color));
1190 waddstr(bot.win, rbe->msg);
1195 static void com_scroll_up(void)
1197 struct rb_entry *rbe = NULL;
1199 int i, first_rbe, num_scroll;
1201 /* the entry that is going to vanish */
1202 rbe = ringbuffer_get(bot_win_rb, scroll_position);
1205 num_scroll = NUM_LINES(rbe->len);
1206 first_rbe = first_visible_rbe(&lines);
1207 if (first_rbe < 0 || (first_rbe == ringbuffer_filled(bot_win_rb) - 1))
1210 wscrl(bot.win, -num_scroll);
1211 i = draw_top_rbe(&lines);
1214 while (i > 0 && lines < num_scroll) {
1216 rbe = ringbuffer_get(bot_win_rb, --i);
1219 rbe_lines = NUM_LINES(rbe->len);
1221 wattron(bot.win, COLOR_PAIR(rbe->color));
1222 waddstr(bot.win, "\n");
1223 waddstr(bot.win, rbe->msg);
1232 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1235 static void com_ll_decr(void)
1237 if (loglevel <= LL_DEBUG) {
1238 print_in_bar(COLOR_ERRMSG,
1239 "loglevel already at maximal verbosity\n");
1243 print_in_bar(COLOR_MSG, "loglevel set to %d\n", loglevel);
1246 static void com_ll_incr(void)
1248 if (loglevel >= LL_EMERG) {
1249 print_in_bar(COLOR_ERRMSG,
1250 "loglevel already at minimal verbosity\n");
1254 print_in_bar(COLOR_MSG, "loglevel set to %d\n", loglevel);
1258 * reread configuration, terminate on errors
1260 static void com_reread_conf(void)
1262 char *cf = configfile_exists();
1263 struct gui_cmdline_parser_params params = {
1266 .check_required = 0,
1267 .check_ambiguity = 0,
1272 PARA_WARNING_LOG("there is no configuration to read\n");
1275 PARA_INFO_LOG("rereading command line options and config file\n");
1277 * Despite .print_errors is set to 0, gengetopt will print to stderr
1278 * anyway, and exit on errors. So we have to shutdown curses first.
1281 gui_cmdline_parser_config_file(cf, &conf, ¶ms);
1283 PARA_NOTICE_LOG("config file reloaded\n");
1284 check_key_map_args_or_die();
1287 static void com_help(void)
1291 for (i = 0; i < conf.key_map_given; ++i) {
1292 char *handler, *arg, *tmp = para_strdup(conf.key_map_arg[i]);
1293 const char *handler_text = "???", *desc = NULL;
1295 if (!split_key_map(tmp, &handler, &arg)) {
1301 handler_text = "internal";
1302 desc = command_list[find_cmd_byname(arg)].description;
1304 case 'x': handler_text = "external"; break;
1305 case 'd': handler_text = "display "; break;
1306 case 'p': handler_text = "para "; break;
1308 outputf(COLOR_MSG, "%s\t%s\t%s%s\t%s", tmp, handler_text, arg,
1309 strlen(arg) < 8? "\t" : "",
1313 for (i = 0; command_list[i].handler; i++) {
1314 struct gui_command gc = command_list[i];
1316 outputf(COLOR_MSG, "%s\tinternal\t%s\t%s%s", gc.key, gc.name,
1317 strlen(gc.name) < 8? "\t" : "",
1320 print_in_bar(COLOR_MSG, "try \"para_gui -h\" or \"para_client help\" "
1324 static void com_shrink_top_win(void)
1326 if (top.lines <= theme.top_lines_min) {
1327 PARA_WARNING_LOG("can not decrease top window\n");
1330 init_wins(top.lines - 1);
1331 print_in_bar(COLOR_MSG, "%s", "decreased top window");
1334 static void com_enlarge_top_win(void)
1336 if (bot.lines < 3) {
1337 PARA_WARNING_LOG("can not increase top window\n");
1340 init_wins(top.lines + 1);
1341 print_in_bar(COLOR_MSG, "increased top window");
1344 static void com_version(void)
1346 print_in_bar(COLOR_MSG, "%s", version_single_line("gui"));
1349 __noreturn static void com_quit(void)
1351 die(EXIT_SUCCESS, "%s", "");
1354 static void com_refresh(void)
1360 static void com_next_theme(void)
1366 static void com_prev_theme(void)
1372 static void handle_command(int c)
1376 /* first check user-defined key bindings */
1377 for (i = 0; i < conf.key_map_given; ++i) {
1378 char *tmp, *handler, *arg;
1380 tmp = para_strdup(conf.key_map_arg[i]);
1381 if (!split_key_map(tmp, &handler, &arg)) {
1385 if (strcmp(tmp, km_keyname(c))) {
1389 if (*handler == 'd')
1391 else if (*handler == 'x')
1393 else if (*handler == 'p')
1395 else if (*handler == 'i') {
1396 int num = find_cmd_byname(arg);
1398 command_list[num].handler();
1403 /* not found, check internal key bindings */
1404 for (i = 0; command_list[i].handler; i++) {
1405 if (!strcmp(km_keyname(c), command_list[i].key)) {
1406 command_list[i].handler();
1410 print_in_bar(COLOR_ERRMSG, "key '%s' is not bound, press ? for help",
1414 __noreturn static void print_help_and_die(void)
1416 struct ggo_help h = DEFINE_GGO_HELP(gui);
1417 bool d = conf.detailed_help_given;
1419 ggo_print_help(&h, d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS);
1423 int main(int argc, char *argv[])
1428 gui_cmdline_parser(argc, argv, &conf); /* exits on errors */
1429 loglevel = get_loglevel_by_name(conf.loglevel_arg);
1430 version_handle_flag("gui", conf.version_given);
1431 if (conf.help_given || conf.detailed_help_given)
1432 print_help_and_die();
1433 cf = configfile_exists();
1434 if (!cf && conf.config_file_given)
1435 die(EXIT_FAILURE, "can not read config file %s\n",
1436 conf.config_file_arg);
1438 struct gui_cmdline_parser_params params = {
1441 .check_required = 0,
1442 .check_ambiguity = 0,
1445 gui_cmdline_parser_config_file(cf, &conf, ¶ms);
1446 loglevel = get_loglevel_by_name(conf.loglevel_arg);
1448 check_key_map_args_or_die();
1449 theme_init(conf.theme_arg, &theme);
1450 setup_signal_handling();
1451 bot_win_rb = ringbuffer_new(RINGBUFFER_SIZE);
1452 setlocale(LC_CTYPE, "");
1453 initscr(); /* needed only once, always successful */
1457 ret = do_select(GETCH_MODE);
1460 print_in_bar(COLOR_MSG, " ");
1461 handle_command(ret);