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 {
37 } top, bot, sb, in, sep;
39 #define RINGBUFFER_SIZE 512
45 static struct ringbuffer *bot_win_rb;
47 static unsigned scroll_position;
51 static int command_fds[2] = {-1, -1};
52 static int stat_pipe = -1;
53 static struct gui_args_info conf;
57 CMDS_IDLE, /* no command running */
58 CMDS_DCMD, /* para or display command running */
59 CMDS_XCMD, /* external command running */
63 * Codes for various colors.
65 * Each status item has its own color pair. The ones defined here start at a
66 * higher number so that they do not overlap with these.
69 COLOR_STATUSBAR = NUM_STAT_ITEMS + 1,
82 const char *description;
83 void (*handler)(void);
86 static struct gui_theme theme;
88 #define GUI_COMMANDS \
89 GUI_COMMAND(help, "?", "print help") \
90 GUI_COMMAND(enlarge_top_win, "+", "enlarge the top window") \
91 GUI_COMMAND(shrink_top_win, "-", "shrink the top window") \
92 GUI_COMMAND(reread_conf, "r", "reread configuration file") \
93 GUI_COMMAND(quit, "q", "exit para_gui") \
94 GUI_COMMAND(refresh, "^L", "redraw the screen") \
95 GUI_COMMAND(next_theme, ".", "switch to next theme") \
96 GUI_COMMAND(prev_theme, ",", "switch to previous theme") \
97 GUI_COMMAND(ll_incr, ">", "increase loglevel (decreases verbosity)") \
98 GUI_COMMAND(ll_decr, "<", "decrease loglevel (increases verbosity)") \
99 GUI_COMMAND(version, "V", "show the para_gui version") \
100 GUI_COMMAND(scroll_up, "<up>", "scroll up one line") \
101 GUI_COMMAND(scroll_down, "<down>", "scroll_down") \
102 GUI_COMMAND(page_up, "<ppage>", "scroll up one page") \
103 GUI_COMMAND(page_down, "<npage>", "scroll down one page") \
104 GUI_COMMAND(scroll_top, "<home>", "scroll to top of buffer") \
105 GUI_COMMAND(cancel_scroll, "<end>", "deactivate scroll mode") \
107 /* declare command handlers */
108 #define GUI_COMMAND(_c, _k, _d) \
109 static void com_ ## _c(void);
114 /* define command array */
115 #define GUI_COMMAND(_c, _k, _d) \
120 .handler = com_ ## _c \
123 static struct gui_command command_list[] = {GUI_COMMANDS {.name = NULL}};
125 static int find_cmd_byname(char *name)
129 for (i = 0; command_list[i].handler; i++)
130 if (!strcmp(command_list[i].name, name))
136 * Even though ncurses provides getmaxx and getmaxy, these functions/macros are
137 * not described in the XSI Curses standard.
139 static int get_num_lines(struct gui_window *w)
142 __a_unused int cols; /* avoid "set but not used" warnings */
144 getmaxyx(w->win, lines, cols);
148 static int get_num_cols(struct gui_window *w)
150 __a_unused int lines; /* avoid "set but not used" warnings */
153 getmaxyx(w->win, lines, cols);
157 /** Number of lines of the window are occupied by an output line. */
158 #define NUM_LINES(len) (1 + (len) / get_num_cols(&bot))
160 /* isendwin() returns false before initscr() was called */
161 static bool curses_active(void)
163 return top.win && !isendwin();
166 /* taken from mutt */
167 static char *km_keyname(int c)
172 sprintf(buf, "<up>");
176 sprintf(buf, "<down>");
180 sprintf(buf, "<left>");
183 if (c == KEY_RIGHT) {
184 sprintf(buf, "<right>");
187 if (c == KEY_NPAGE) {
188 sprintf(buf, "<npage>");
191 if (c == KEY_PPAGE) {
192 sprintf(buf, "<ppage>");
196 sprintf(buf, "<home>");
200 sprintf(buf, "<end>");
203 if (c < 256 && c > -128 && iscntrl((unsigned char) c)) {
208 buf[1] = (c + '@') & 0x7f;
211 snprintf(buf, sizeof(buf), "\\%d%d%d", c >> 6,
212 (c >> 3) & 7, c & 7);
213 } else if (c >= KEY_F0 && c < KEY_F(256))
214 sprintf(buf, "<F%d>", c - KEY_F0);
216 snprintf(buf, sizeof(buf), "%c", (unsigned char) c);
218 snprintf(buf, sizeof(buf), "\\x%hx", (unsigned short) c);
222 /* Print given number of spaces to curses window. */
223 static void add_spaces(WINDOW* win, unsigned int num)
226 unsigned sz = sizeof(space) - 1; /* number of spaces */
240 * print aligned string to curses window. This function always prints
243 static int align_str(WINDOW* win, char *str, unsigned int len,
246 int ret, i, num; /* of spaces */
251 ret = strwidth(str, &width);
253 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
262 /* replace control characters by spaces */
263 for (i = 0; i < len && str[i]; i++) {
264 if (str[i] == '\n' || str[i] == '\r' || str[i] == '\f')
269 add_spaces(win, num);
270 } else if (align == RIGHT) {
271 add_spaces(win, num);
274 add_spaces(win, num / 2);
275 waddstr(win, str[0]? str: "");
276 add_spaces(win, num - num / 2);
281 __printf_2_3 static void print_in_bar(int color, const char *fmt,...)
286 if (!curses_active())
288 wattron(in.win, COLOR_PAIR(color));
290 xvasprintf(&msg, fmt, ap);
293 align_str(in.win, msg, get_num_cols(&in), LEFT);
298 static void print_status_bar(void)
302 tmp = para_strdup("para_gui " PACKAGE_VERSION " (hit ? for help)");
304 align_str(sb.win, tmp, get_num_cols(&sb), CENTER);
309 * get the number of the oldest rbe that is (partially) visible. On return,
310 * lines contains the sum of the number of lines of all visible entries. If the
311 * first one is only partially visible, lines is greater than bot.lines.
313 static int first_visible_rbe(unsigned *lines)
315 int i, bot_lines = get_num_lines(&bot);
318 for (i = scroll_position; i < RINGBUFFER_SIZE; i++) {
319 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
323 rbe_lines = NUM_LINES(rbe->len);
324 if (rbe_lines > bot_lines)
327 if (*lines >= bot_lines)
330 return RINGBUFFER_SIZE - 1;
334 returns number of first visible rbe, *lines is the number of lines drawn.
336 static int draw_top_rbe(unsigned *lines)
338 int bot_cols, bot_lines, ret, fvr = first_visible_rbe(lines);
339 struct rb_entry *rbe;
340 size_t bytes_to_skip, cells_to_skip, width;
344 wmove(bot.win, 0, 0);
345 rbe = ringbuffer_get(bot_win_rb, fvr);
348 getmaxyx(bot.win, bot_lines, bot_cols);
349 if (*lines > bot_lines) {
350 /* rbe is partially visible multi-line */
351 cells_to_skip = (*lines - bot_lines) * bot_cols;
352 ret = skip_cells(rbe->msg, cells_to_skip, &bytes_to_skip);
355 ret = strwidth(rbe->msg + bytes_to_skip, &width);
362 wattron(bot.win, COLOR_PAIR(rbe->color));
363 waddstr(bot.win, rbe->msg + bytes_to_skip);
364 *lines = NUM_LINES(width);
368 static void redraw_bot_win(void)
371 int i, bot_lines = get_num_lines(&bot);
373 wmove(bot.win, 0, 0);
375 i = draw_top_rbe(&lines);
378 while (i > 0 && lines < bot_lines) {
379 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, --i);
382 waddstr(bot.win, "\n");
385 lines += NUM_LINES(rbe->len);
386 wattron(bot.win, COLOR_PAIR(rbe->color));
387 waddstr(bot.win, "\n");
388 waddstr(bot.win, rbe->msg);
394 static void rb_add_entry(int color, char *msg)
396 struct rb_entry *old, *new;
400 if (strwidth(msg, &len) < 0)
402 new = para_malloc(sizeof(struct rb_entry));
406 old = ringbuffer_add(bot_win_rb, new);
411 if (scroll_position) {
412 /* discard current scrolling, like xterm does */
417 wattron(bot.win, COLOR_PAIR(color));
418 getyx(bot.win, y, x);
420 waddstr(bot.win, "\n");
421 waddstr(bot.win, msg);
425 * print formated output to bot win and refresh
427 __printf_2_3 static void outputf(int color, const char* fmt,...)
432 if (!curses_active())
435 xvasprintf(&msg, fmt, ap);
437 rb_add_entry(color, msg);
441 static int add_output_line(char *line, void *data)
443 int color = *(int *)data? COLOR_ERRMSG : COLOR_OUTPUT;
445 if (!curses_active())
447 rb_add_entry(color, para_strdup(line));
451 static __printf_2_3 void curses_log(int ll, const char *fmt,...)
458 if (curses_active()) {
459 int color = ll <= LL_NOTICE? COLOR_MSG : COLOR_ERRMSG;
461 unsigned bytes = xvasprintf(&msg, fmt, ap);
462 if (bytes > 0 && msg[bytes - 1] == '\n')
463 msg[bytes - 1] = '\0'; /* cut trailing newline */
464 rb_add_entry(color, msg);
466 } else if (cmd_pid <= 0) /* no external command running */
467 vfprintf(stderr, fmt, ap);
470 __printf_2_3 void (*para_log)(int, const char*, ...) = curses_log;
472 static void setup_signal_handling(void)
474 signal_pipe = para_signal_init();
475 para_install_sighandler(SIGINT);
476 para_install_sighandler(SIGTERM);
477 para_install_sighandler(SIGCHLD);
478 para_install_sighandler(SIGWINCH);
479 para_install_sighandler(SIGUSR1);
482 static void shutdown_curses(void)
488 /* disable curses, print a message, kill running processes and exit */
489 __noreturn __printf_2_3 static void die(int exit_code, const char* fmt, ...)
495 vfprintf(stderr, fmt, argp);
497 /* kill every process in the process group and exit */
498 para_sigaction(SIGTERM, SIG_IGN);
504 * Print stat item #i to curses window
506 static void print_stat_item(int i)
509 struct stat_item_data d = theme.data[i];
510 char *c = stat_content[i];
511 int top_lines = get_num_lines(&top);
513 if (!curses_active() || !d.len || !c)
515 tmp = make_message("%s%s%s", d.prefix, c, d.postfix);
516 wmove(top.win, d.y * top_lines / 100, d.x * COLS / 100);
518 wattron(top.win, COLOR_PAIR(i + 1));
519 align_str(top.win, tmp, d.len * COLS / 100, d.align);
524 static int update_item(int item_num, char *buf)
526 char **c = stat_content + item_num;
533 *c = para_strdup("(artist tag not set)");
536 *c = para_strdup("(title tag not set)");
539 *c = para_strdup("????");
542 *c = para_strdup("(album tag not set)");
545 *c = para_strdup("(comment tag not set)");
549 *c = para_strdup(buf);
551 print_stat_item(item_num);
555 static void print_all_items(void)
559 if (!curses_active())
561 FOR_EACH_STATUS_ITEM(i)
565 static void clear_all_items(void)
569 FOR_EACH_STATUS_ITEM(i) {
570 free(stat_content[i]);
571 stat_content[i] = para_strdup("");
575 static struct timeval next_exec;
577 static void status_pre_select(fd_set *rfds, int *max_fileno, struct timeval *tv)
579 struct timeval atm, diff;
582 return para_fd_set(stat_pipe, rfds, max_fileno);
583 gettimeofday(&atm, NULL);
584 if (tv_diff(&next_exec, &atm, &diff) > 0) {
588 tv->tv_sec = tv->tv_usec = 0; /* min delay */
591 static void status_post_select(fd_set *rfds)
594 static int bufsize, loaded;
601 int fds[3] = {0, 1, 0};
602 /* Avoid busy loop */
603 gettimeofday(&atm, NULL);
604 if (tv_diff(&next_exec, &atm, NULL) > 0)
606 next_exec.tv_sec = atm.tv_sec + 2;
607 ret = para_exec_cmdline_pid(&pid, conf.stat_cmd_arg, fds);
610 ret = mark_fd_nonblocking(fds[1]);
619 if (loaded >= bufsize) {
620 if (bufsize > 1000 * 1000) {
624 bufsize += bufsize + 1000;
625 buf = para_realloc(buf, bufsize);
627 assert(loaded < bufsize);
628 ret = read_nonblock(stat_pipe, buf + loaded, bufsize - loaded,
631 ret2 = for_each_stat_item(buf, loaded, update_item);
632 if (ret < 0 || ret2 < 0) {
634 PARA_NOTICE_LOG("closing stat pipe: %s\n", para_strerror(-ret));
638 free(stat_content[SI_BASENAME]);
639 stat_content[SI_BASENAME] =
640 para_strdup("stat command terminated!?");
644 sz = ret2; /* what is left */
645 if (sz > 0 && sz < loaded)
646 memmove(buf, buf + loaded - sz, sz);
653 static void init_wins(int top_lines)
655 int top_y = 0, bot_y = top_lines + 1, sb_y = LINES - 2,
656 in_y = LINES - 1, sep_y = top_lines;
657 int bot_lines = LINES - top_lines - 3, sb_lines = 1, in_lines = 1,
660 assume_default_colors(theme.default_fg, theme.default_bg);
662 wresize(top.win, top_lines, COLS);
663 mvwin(top.win, top_y, 0);
665 wresize(sb.win, sb_lines, COLS);
666 mvwin(sb.win, sb_y, 0);
668 wresize(sep.win, sep_lines, COLS);
669 mvwin(sep.win, sep_y, 0);
671 wresize(bot.win, bot_lines, COLS);
672 mvwin(bot.win, bot_y, 0);
674 wresize(in.win, in_lines, COLS);
675 mvwin(in.win, in_y, 0);
677 sep.win = newwin(sep_lines, COLS, sep_y, 0);
678 top.win = newwin(top_lines, COLS, top_y, 0);
679 bot.win = newwin(bot_lines, COLS, bot_y, 0);
680 sb.win = newwin(sb_lines, COLS, sb_y, 0);
681 in.win = newwin(in_lines, COLS, in_y, 0);
682 if (!top.win || !bot.win || !sb.win || !in.win || !sep.win)
683 die(EXIT_FAILURE, "Error: Cannot create curses windows\n");
687 scrollok(bot.win, 1);
688 wattron(sb.win, COLOR_PAIR(COLOR_STATUSBAR));
689 wattron(sep.win, COLOR_PAIR(COLOR_SEPARATOR));
690 wattron(bot.win, COLOR_PAIR(COLOR_BOT));
691 wattron(top.win, COLOR_PAIR(COLOR_TOP));
702 wmove(sep.win, 0, 0);
703 whline(sep.win, theme.sep_char, COLS);
707 wnoutrefresh(top.win);
708 wnoutrefresh(bot.win);
710 wnoutrefresh(sb.win);
711 wnoutrefresh(in.win);
712 wnoutrefresh(sep.win);
716 static void init_pair_or_die(short pair, short f, short b)
718 if (init_pair(pair, f, b) == ERR)
719 die(EXIT_FAILURE, "fatal: init_pair() failed\n");
722 static void init_colors_or_die(void)
727 die(EXIT_FAILURE, "fatal: No color term\n");
728 if (start_color() == ERR)
729 die(EXIT_FAILURE, "fatal: failed to start colors\n");
730 FOR_EACH_STATUS_ITEM(i)
731 if (theme.data[i].len)
732 init_pair_or_die(i + 1, theme.data[i].fg,
734 init_pair_or_die(COLOR_STATUSBAR, theme.sb_fg, theme.sb_bg);
735 init_pair_or_die(COLOR_COMMAND, theme.cmd_fg, theme.cmd_bg);
736 init_pair_or_die(COLOR_OUTPUT, theme.output_fg, theme.output_bg);
737 init_pair_or_die(COLOR_MSG, theme.msg_fg, theme.msg_bg);
738 init_pair_or_die(COLOR_ERRMSG, theme.err_msg_fg, theme.err_msg_bg);
739 init_pair_or_die(COLOR_SEPARATOR, theme.sep_fg, theme.sep_bg);
740 init_pair_or_die(COLOR_TOP, theme.default_fg, theme.default_bg);
741 init_pair_or_die(COLOR_BOT, theme.default_fg, theme.default_bg);
744 /* (Re-)initialize the curses library. */
745 static void init_curses(void)
749 if (top.win && refresh() == ERR) /* refresh is really needed */
750 die(EXIT_FAILURE, "refresh() failed\n");
751 if (LINES < theme.lines_min || COLS < theme.cols_min)
752 die(EXIT_FAILURE, "Terminal (%dx%d) too small"
753 " (need at least %dx%d)\n", COLS, LINES,
754 theme.cols_min, theme.lines_min);
755 curs_set(0); /* make cursor invisible, ignore errors */
756 nonl(); /* do not NL->CR/NL on output, always returns OK */
757 /* don't echo input */
759 die(EXIT_FAILURE, "fatal: noecho() failed\n");
760 /* take input chars one at a time, no wait for \n */
762 die(EXIT_FAILURE, "fatal: cbreak() failed\n");
763 init_colors_or_die();
764 clear(); /* ignore non-fatal errors */
765 init_wins(theme.top_lines_default);
766 // noecho(); /* don't echo input */
769 static void check_sigchld(void)
775 ret = para_reap_child(&pid);
778 if (pid == cmd_pid) {
781 print_in_bar(COLOR_MSG, " ");
783 goto reap_next_child;
787 * This sucker modifies its first argument. *handler and *arg are
788 * pointers to 0-terminated strings (inside line). Crap.
790 static int split_key_map(char *line, char **handler, char **arg)
792 if (!(*handler = strchr(line + 1, ':')))
796 if (!(*arg = strchr(*handler, ':')))
805 static void check_key_map_args_or_die(void)
810 for (i = 0; i < conf.key_map_given; ++i) {
814 tmp = para_strdup(conf.key_map_arg[i]);
815 if (!split_key_map(tmp, &handler, &arg))
817 if (strlen(handler) != 1)
819 if (*handler != 'x' && *handler != 'd' && *handler != 'i'
824 if (find_cmd_byname(arg) < 0)
827 if (i != conf.key_map_given)
828 die(EXIT_FAILURE, "invalid key map: %s\n", conf.key_map_arg[i]);
832 static void parse_config_file_or_die(bool override)
836 struct gui_cmdline_parser_params params = {
837 .override = override,
839 .check_required = !override,
840 .check_ambiguity = 0,
844 if (conf.config_file_given)
845 config_file = para_strdup(conf.config_file_arg);
847 char *home = para_homedir();
848 config_file = make_message("%s/.paraslash/gui.conf", home);
851 if (!file_exists(config_file)) {
852 if (!conf.config_file_given)
855 PARA_EMERG_LOG("config file %s does not exist\n",
861 gui_cmdline_parser_config_file(config_file, &conf, ¶ms);
862 loglevel = get_loglevel_by_name(conf.loglevel_arg);
863 check_key_map_args_or_die();
864 theme_init(conf.theme_arg, &theme);
872 /* reread configuration, terminate on errors */
873 static void reread_conf(void)
876 * gengetopt might print to stderr and exit on errors. So we have to
877 * shutdown curses first.
880 parse_config_file_or_die(true /* override */);
882 print_in_bar(COLOR_MSG, "config file reloaded\n");
886 * React to various signal-related events
888 static void signal_post_select(fd_set *rfds)
890 int ret = para_next_signal(rfds);
895 die(EXIT_FAILURE, "only the good die young (caught SIGTERM)\n");
898 if (curses_active()) {
905 PARA_WARNING_LOG("caught SIGINT, reset\n");
906 /* Nothing to do. SIGINT killed our child which gets noticed
907 * by do_select and resets everything.
911 PARA_NOTICE_LOG("got SIGUSR1, rereading configuration\n");
920 #define COMMAND_BUF_SIZE 32768
922 static enum cmd_status cmd_status(void)
924 if (command_fds[0] >= 0 || command_fds[1] >= 0)
931 static void command_pre_select(fd_set *rfds, int *max_fileno)
933 enum cmd_status cmds = cmd_status();
935 if (cmds != CMDS_DCMD)
937 if (command_fds[0] >= 0)
938 para_fd_set(command_fds[0], rfds, max_fileno);
939 if (command_fds[1] >= 0)
940 para_fd_set(command_fds[1], rfds, max_fileno);
943 static void command_post_select(fd_set *rfds)
946 static char command_buf[2][COMMAND_BUF_SIZE];
947 static int cbo[2]; /* command buf offsets */
948 static unsigned flags[2]; /* for for_each_line() */
949 enum cmd_status cmds = cmd_status();
951 if (cmds != CMDS_DCMD)
953 for (i = 0; i < 2; i++) {
955 if (command_fds[i] < 0)
957 ret = read_nonblock(command_fds[i],
958 command_buf[i] + cbo[i],
959 COMMAND_BUF_SIZE - 1 - cbo[i], rfds, &sz);
962 cbo[i] = for_each_line(flags[i], command_buf[i], cbo[i],
963 add_output_line, &i);
964 if (sz != cbo[i]) { /* at least one line found */
968 if (ret < 0 || cmd_pid == 0) {
970 PARA_NOTICE_LOG("closing command fd %d: %s",
971 i, para_strerror(-ret));
972 close(command_fds[i]);
976 if (command_fds[!i] < 0) /* both fds closed */
979 if (cbo[i] == COMMAND_BUF_SIZE - 1) {
980 PARA_NOTICE_LOG("discarding overlong line");
982 flags[i] = FELF_DISCARD_FIRST;
987 static void input_pre_select(fd_set *rfds, int *max_fileno)
989 enum cmd_status cmds = cmd_status();
991 if (cmds != CMDS_XCMD)
992 para_fd_set(STDIN_FILENO, rfds, max_fileno);
995 /* read from command pipe and print data to bot window */
996 static void exec_and_display_cmd(const char *cmd)
998 int ret, fds[3] = {0, 1, 1};
1000 outputf(COLOR_COMMAND, "%s", cmd);
1001 ret = para_exec_cmdline_pid(&cmd_pid, cmd, fds);
1004 ret = mark_fd_nonblocking(fds[1]);
1007 ret = mark_fd_nonblocking(fds[2]);
1010 command_fds[0] = fds[1];
1011 command_fds[1] = fds[2];
1014 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
1015 close(command_fds[0]);
1016 close(command_fds[1]);
1019 static void para_cmd(char *cmd)
1023 print_in_bar(COLOR_MSG, "executing client command, hit any key to abort\n");
1024 c = make_message(BINDIR "/para_client -- %s", cmd);
1025 exec_and_display_cmd(c);
1029 static void display_cmd(char *cmd)
1031 print_in_bar(COLOR_MSG, "executing display command, hit any key to abort");
1032 exec_and_display_cmd(cmd);
1036 * shutdown curses and stat pipe before executing external commands
1038 static void external_cmd(char *cmd)
1040 int fds[3] = {-1, -1, -1};
1045 para_exec_cmdline_pid(&cmd_pid, cmd, fds);
1048 static void handle_command(int c)
1052 /* first check user-defined key bindings */
1053 for (i = 0; i < conf.key_map_given; ++i) {
1054 char *tmp, *handler, *arg;
1056 tmp = para_strdup(conf.key_map_arg[i]);
1057 if (!split_key_map(tmp, &handler, &arg)) {
1061 if (strcmp(tmp, km_keyname(c))) {
1065 if (*handler == 'd')
1067 else if (*handler == 'x')
1069 else if (*handler == 'p')
1071 else if (*handler == 'i') {
1072 int num = find_cmd_byname(arg);
1074 command_list[num].handler();
1079 /* not found, check internal key bindings */
1080 for (i = 0; command_list[i].handler; i++) {
1081 if (!strcmp(km_keyname(c), command_list[i].key)) {
1082 command_list[i].handler();
1086 print_in_bar(COLOR_ERRMSG, "key '%s' is not bound, press ? for help",
1090 static void input_post_select(void)
1093 enum cmd_status cmds = cmd_status();
1095 if (cmds == CMDS_XCMD)
1097 ret = wgetch(top.win);
1098 if (ret == ERR || ret == KEY_RESIZE)
1100 if (cmds == CMDS_IDLE)
1101 return handle_command(ret);
1103 kill(cmd_pid, SIGTERM);
1106 static void signal_pre_select(fd_set *rfds, int *max_fileno)
1108 para_fd_set(signal_pipe, rfds, max_fileno);
1112 * This is the core select loop. It checks the following fds:
1116 * - stdout/stderr of display or internal commands
1118 __noreturn static void do_select(void)
1121 int ret, max_fileno;
1125 tv.tv_sec = conf.timeout_arg / 1000;
1126 tv.tv_usec = (conf.timeout_arg % 1000) * 1000;
1129 status_pre_select(&rfds, &max_fileno, &tv);
1130 signal_pre_select(&rfds, &max_fileno);
1131 command_pre_select(&rfds, &max_fileno);
1132 input_pre_select(&rfds, &max_fileno);
1133 ret = para_select(max_fileno + 1, &rfds, NULL, &tv);
1135 goto repeat; /* skip fd checks */
1136 signal_post_select(&rfds);
1137 command_post_select(&rfds);
1138 status_post_select(&rfds);
1139 input_post_select();
1143 static void print_scroll_msg(void)
1145 unsigned lines_total, filled = ringbuffer_filled(bot_win_rb);
1146 int first_rbe = first_visible_rbe(&lines_total);
1148 print_in_bar(COLOR_MSG, "scrolled view: %d-%d/%d\n", filled - first_rbe,
1149 filled - scroll_position, ringbuffer_filled(bot_win_rb));
1152 static void com_scroll_top(void)
1154 int i = RINGBUFFER_SIZE - 1, bot_lines = get_num_lines(&bot);
1157 while (i > 0 && !ringbuffer_get(bot_win_rb, i))
1159 /* i is oldest entry */
1160 for (; lines < bot_lines && i >= 0; i--) {
1161 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
1164 lines += NUM_LINES(rbe->len);
1167 if (lines > 0 && scroll_position != i) {
1168 scroll_position = i;
1173 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1176 static void com_cancel_scroll(void)
1179 if (scroll_position == 0) {
1180 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1183 scroll_position = 0;
1187 static void com_page_down(void)
1190 int i = scroll_position, bot_lines = get_num_lines(&bot);
1192 while (lines < bot_lines && --i > 0) {
1193 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
1196 lines += NUM_LINES(rbe->len);
1199 scroll_position = i;
1204 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1207 static void com_page_up(void)
1210 int fvr = first_visible_rbe(&lines), bot_lines = get_num_lines(&bot);
1212 if (fvr < 0 || fvr + 1 >= ringbuffer_filled(bot_win_rb)) {
1213 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1216 scroll_position = fvr + 1;
1217 for (; scroll_position > 0; scroll_position--) {
1218 first_visible_rbe(&lines);
1219 if (lines == bot_lines)
1226 static void com_scroll_down(void)
1228 struct rb_entry *rbe;
1229 int rbe_lines, bot_lines = get_num_lines(&bot);
1231 if (!scroll_position) {
1232 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1236 rbe = ringbuffer_get(bot_win_rb, scroll_position);
1237 rbe_lines = NUM_LINES(rbe->len);
1238 wscrl(bot.win, rbe_lines);
1239 wmove(bot.win, bot_lines - rbe_lines, 0);
1240 wattron(bot.win, COLOR_PAIR(rbe->color));
1241 waddstr(bot.win, rbe->msg);
1246 static void com_scroll_up(void)
1248 struct rb_entry *rbe = NULL;
1250 int i, first_rbe, num_scroll;
1252 /* the entry that is going to vanish */
1253 rbe = ringbuffer_get(bot_win_rb, scroll_position);
1256 num_scroll = NUM_LINES(rbe->len);
1257 first_rbe = first_visible_rbe(&lines);
1258 if (first_rbe < 0 || (first_rbe == ringbuffer_filled(bot_win_rb) - 1))
1261 wscrl(bot.win, -num_scroll);
1262 i = draw_top_rbe(&lines);
1265 while (i > 0 && lines < num_scroll) {
1267 rbe = ringbuffer_get(bot_win_rb, --i);
1270 rbe_lines = NUM_LINES(rbe->len);
1272 wattron(bot.win, COLOR_PAIR(rbe->color));
1273 waddstr(bot.win, "\n");
1274 waddstr(bot.win, rbe->msg);
1283 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1286 static void com_ll_decr(void)
1288 if (loglevel <= LL_DEBUG) {
1289 print_in_bar(COLOR_ERRMSG,
1290 "loglevel already at maximal verbosity\n");
1294 print_in_bar(COLOR_MSG, "loglevel set to %d\n", loglevel);
1297 static void com_ll_incr(void)
1299 if (loglevel >= LL_EMERG) {
1300 print_in_bar(COLOR_ERRMSG,
1301 "loglevel already at minimal verbosity\n");
1305 print_in_bar(COLOR_MSG, "loglevel set to %d\n", loglevel);
1308 static void com_reread_conf(void)
1313 static void com_help(void)
1317 for (i = 0; i < conf.key_map_given; ++i) {
1318 char *handler, *arg, *tmp = para_strdup(conf.key_map_arg[i]);
1319 const char *handler_text = "???", *desc = NULL;
1321 if (!split_key_map(tmp, &handler, &arg)) {
1327 handler_text = "internal";
1328 desc = command_list[find_cmd_byname(arg)].description;
1330 case 'x': handler_text = "external"; break;
1331 case 'd': handler_text = "display "; break;
1332 case 'p': handler_text = "para "; break;
1334 outputf(COLOR_MSG, "%s\t%s\t%s%s\t%s", tmp, handler_text, arg,
1335 strlen(arg) < 8? "\t" : "",
1339 for (i = 0; command_list[i].handler; i++) {
1340 struct gui_command gc = command_list[i];
1342 outputf(COLOR_MSG, "%s\tinternal\t%s\t%s%s", gc.key, gc.name,
1343 strlen(gc.name) < 8? "\t" : "",
1346 print_in_bar(COLOR_MSG, "try \"para_gui -h\" or \"para_client help\" "
1350 static void com_shrink_top_win(void)
1352 int top_lines = get_num_lines(&top);
1354 if (top_lines <= theme.top_lines_min) {
1355 PARA_WARNING_LOG("can not decrease top window\n");
1358 init_wins(top_lines - 1);
1359 print_in_bar(COLOR_MSG, "%s", "decreased top window");
1362 static void com_enlarge_top_win(void)
1364 int top_lines = get_num_lines(&top), bot_lines = get_num_lines(&bot);
1366 if (bot_lines < 3) {
1367 PARA_WARNING_LOG("can not increase top window\n");
1370 init_wins(top_lines + 1);
1371 print_in_bar(COLOR_MSG, "increased top window");
1374 static void com_version(void)
1376 print_in_bar(COLOR_MSG, "%s", version_single_line("gui"));
1379 __noreturn static void com_quit(void)
1381 die(EXIT_SUCCESS, "%s", "");
1384 static void com_refresh(void)
1390 static void com_next_theme(void)
1396 static void com_prev_theme(void)
1402 __noreturn static void print_help_and_die(void)
1404 struct ggo_help h = DEFINE_GGO_HELP(gui);
1405 bool d = conf.detailed_help_given;
1407 ggo_print_help(&h, d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS);
1411 int main(int argc, char *argv[])
1413 gui_cmdline_parser(argc, argv, &conf); /* exits on errors */
1414 loglevel = get_loglevel_by_name(conf.loglevel_arg);
1415 version_handle_flag("gui", conf.version_given);
1416 if (conf.help_given || conf.detailed_help_given)
1417 print_help_and_die();
1418 parse_config_file_or_die(false /* override */);
1419 setup_signal_handling();
1420 bot_win_rb = ringbuffer_new(RINGBUFFER_SIZE);
1421 setlocale(LC_CTYPE, "");
1422 initscr(); /* needed only once, always successful */