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 struct gui_window {
35 } top, bot, sb, in, sep;
37 #define RINGBUFFER_SIZE 512
43 static struct ringbuffer *bot_win_rb;
45 static unsigned scroll_position;
49 static int command_fds[2] = {-1, -1};
50 static int stat_pipe = -1;
51 static struct gui_args_info conf;
55 CMDS_IDLE, /* no command running */
56 CMDS_DCMD, /* para or display command running */
57 CMDS_XCMD, /* external command running */
61 * Codes for various colors.
63 * Each status item has its own color pair. The ones defined here start at a
64 * higher number so that they do not overlap with these.
67 COLOR_STATUSBAR = NUM_STAT_ITEMS + 1,
80 const char *description;
81 void (*handler)(void);
84 static struct gui_theme theme;
86 #define GUI_COMMANDS \
87 GUI_COMMAND(help, "?", "print help") \
88 GUI_COMMAND(enlarge_top_win, "+", "enlarge the top window") \
89 GUI_COMMAND(shrink_top_win, "-", "shrink the top window") \
90 GUI_COMMAND(reread_conf, "r", "reread configuration file") \
91 GUI_COMMAND(quit, "q", "exit para_gui") \
92 GUI_COMMAND(refresh, "^L", "redraw the screen") \
93 GUI_COMMAND(next_theme, ".", "switch to next theme") \
94 GUI_COMMAND(prev_theme, ",", "switch to previous theme") \
95 GUI_COMMAND(ll_incr, ">", "increase loglevel (decreases verbosity)") \
96 GUI_COMMAND(ll_decr, "<", "decrease loglevel (increases verbosity)") \
97 GUI_COMMAND(version, "V", "show the para_gui version") \
98 GUI_COMMAND(scroll_up, "<up>", "scroll up one line") \
99 GUI_COMMAND(scroll_down, "<down>", "scroll_down") \
100 GUI_COMMAND(page_up, "<ppage>", "scroll up one page") \
101 GUI_COMMAND(page_down, "<npage>", "scroll down one page") \
102 GUI_COMMAND(scroll_top, "<home>", "scroll to top of buffer") \
103 GUI_COMMAND(cancel_scroll, "<end>", "deactivate scroll mode") \
105 /* declare command handlers */
106 #define GUI_COMMAND(_c, _k, _d) \
107 static void com_ ## _c(void);
112 /* define command array */
113 #define GUI_COMMAND(_c, _k, _d) \
118 .handler = com_ ## _c \
121 static struct gui_command command_list[] = {GUI_COMMANDS {.name = NULL}};
135 static int find_cmd_byname(char *name)
139 for (i = 0; command_list[i].handler; i++)
140 if (!strcmp(command_list[i].name, name))
146 * Even though ncurses provides getmaxx and getmaxy, these functions/macros are
147 * not described in the XSI Curses standard.
149 static int get_num_lines(struct gui_window *w)
152 __a_unused int cols; /* avoid "set but not used" warnings */
154 getmaxyx(w->win, lines, cols);
158 static int get_num_cols(struct gui_window *w)
160 __a_unused int lines; /* avoid "set but not used" warnings */
163 getmaxyx(w->win, lines, cols);
167 /** Number of lines of the window are occupied by an output line. */
168 #define NUM_LINES(len) (1 + (len) / get_num_cols(&bot))
170 /* isendwin() returns false before initscr() was called */
171 static bool curses_active(void)
173 return top.win && !isendwin();
176 /* taken from mutt */
177 static char *km_keyname(int c)
182 sprintf(buf, "<up>");
186 sprintf(buf, "<down>");
190 sprintf(buf, "<left>");
193 if (c == KEY_RIGHT) {
194 sprintf(buf, "<right>");
197 if (c == KEY_NPAGE) {
198 sprintf(buf, "<npage>");
201 if (c == KEY_PPAGE) {
202 sprintf(buf, "<ppage>");
206 sprintf(buf, "<home>");
210 sprintf(buf, "<end>");
213 if (c < 256 && c > -128 && iscntrl((unsigned char) c)) {
218 buf[1] = (c + '@') & 0x7f;
221 snprintf(buf, sizeof(buf), "\\%d%d%d", c >> 6,
222 (c >> 3) & 7, c & 7);
223 } else if (c >= KEY_F0 && c < KEY_F(256))
224 sprintf(buf, "<F%d>", c - KEY_F0);
226 snprintf(buf, sizeof(buf), "%c", (unsigned char) c);
228 snprintf(buf, sizeof(buf), "\\x%hx", (unsigned short) c);
232 /* Print given number of spaces to curses window. */
233 static void add_spaces(WINDOW* win, unsigned int num)
236 unsigned sz = sizeof(space) - 1; /* number of spaces */
250 * print aligned string to curses window. This function always prints
253 static int align_str(WINDOW* win, char *str, unsigned int len,
256 int ret, i, num; /* of spaces */
261 ret = strwidth(str, &width);
263 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
272 /* replace control characters by spaces */
273 for (i = 0; i < len && str[i]; i++) {
274 if (str[i] == '\n' || str[i] == '\r' || str[i] == '\f')
279 add_spaces(win, num);
280 } else if (align == RIGHT) {
281 add_spaces(win, num);
284 add_spaces(win, num / 2);
285 waddstr(win, str[0]? str: "");
286 add_spaces(win, num - num / 2);
291 __printf_2_3 static void print_in_bar(int color, const char *fmt,...)
296 if (!curses_active())
298 wattron(in.win, COLOR_PAIR(color));
300 xvasprintf(&msg, fmt, ap);
303 align_str(in.win, msg, get_num_cols(&in), LEFT);
308 static void print_status_bar(void)
312 tmp = para_strdup("para_gui " PACKAGE_VERSION " (hit ? for help)");
314 align_str(sb.win, tmp, get_num_cols(&sb), CENTER);
319 * get the number of the oldest rbe that is (partially) visible. On return,
320 * lines contains the sum of the number of lines of all visible entries. If the
321 * first one is only partially visible, lines is greater than bot.lines.
323 static int first_visible_rbe(unsigned *lines)
325 int i, bot_lines = get_num_lines(&bot);
328 for (i = scroll_position; i < RINGBUFFER_SIZE; i++) {
329 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
333 rbe_lines = NUM_LINES(rbe->len);
334 if (rbe_lines > bot_lines)
337 if (*lines >= bot_lines)
340 return RINGBUFFER_SIZE - 1;
344 returns number of first visible rbe, *lines is the number of lines drawn.
346 static int draw_top_rbe(unsigned *lines)
348 int bot_cols, bot_lines, ret, fvr = first_visible_rbe(lines);
349 struct rb_entry *rbe;
350 size_t bytes_to_skip, cells_to_skip, width;
354 wmove(bot.win, 0, 0);
355 rbe = ringbuffer_get(bot_win_rb, fvr);
358 getmaxyx(bot.win, bot_lines, bot_cols);
359 if (*lines > bot_lines) {
360 /* rbe is partially visible multi-line */
361 cells_to_skip = (*lines - bot_lines) * bot_cols;
362 ret = skip_cells(rbe->msg, cells_to_skip, &bytes_to_skip);
365 ret = strwidth(rbe->msg + bytes_to_skip, &width);
372 wattron(bot.win, COLOR_PAIR(rbe->color));
373 waddstr(bot.win, rbe->msg + bytes_to_skip);
374 *lines = NUM_LINES(width);
378 static void redraw_bot_win(void)
381 int i, bot_lines = get_num_lines(&bot);
383 wmove(bot.win, 0, 0);
385 i = draw_top_rbe(&lines);
388 while (i > 0 && lines < bot_lines) {
389 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, --i);
392 waddstr(bot.win, "\n");
395 lines += NUM_LINES(rbe->len);
396 wattron(bot.win, COLOR_PAIR(rbe->color));
397 waddstr(bot.win, "\n");
398 waddstr(bot.win, rbe->msg);
404 static void rb_add_entry(int color, char *msg)
406 struct rb_entry *old, *new;
410 if (strwidth(msg, &len) < 0)
412 new = para_malloc(sizeof(struct rb_entry));
416 old = ringbuffer_add(bot_win_rb, new);
421 if (scroll_position) {
422 /* discard current scrolling, like xterm does */
427 wattron(bot.win, COLOR_PAIR(color));
428 getyx(bot.win, y, x);
430 waddstr(bot.win, "\n");
431 waddstr(bot.win, msg);
435 * print formated output to bot win and refresh
437 __printf_2_3 static void outputf(int color, const char* fmt,...)
442 if (!curses_active())
445 xvasprintf(&msg, fmt, ap);
447 rb_add_entry(color, msg);
451 static int add_output_line(char *line, void *data)
453 int color = *(int *)data? COLOR_ERRMSG : COLOR_OUTPUT;
455 if (!curses_active())
457 rb_add_entry(color, para_strdup(line));
461 static __printf_2_3 void curses_log(int ll, const char *fmt,...)
468 if (curses_active()) {
469 int color = ll <= LL_NOTICE? COLOR_MSG : COLOR_ERRMSG;
471 unsigned bytes = xvasprintf(&msg, fmt, ap);
472 if (bytes > 0 && msg[bytes - 1] == '\n')
473 msg[bytes - 1] = '\0'; /* cut trailing newline */
474 rb_add_entry(color, msg);
476 } else if (cmd_pid <= 0) /* no external command running */
477 vfprintf(stderr, fmt, ap);
480 __printf_2_3 void (*para_log)(int, const char*, ...) = curses_log;
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(struct sched *s, __a_unused struct task *t)
580 return para_fd_set(stat_pipe, &s->rfds, &s->max_fileno);
581 sched_request_barrier_or_min_delay(&next_exec, s);
584 static int status_post_select(struct sched *s, __a_unused struct task *t)
587 static int bufsize, loaded;
593 int fds[3] = {0, 1, 0};
594 /* Avoid busy loop */
595 if (tv_diff(&next_exec, now, NULL) > 0)
597 next_exec.tv_sec = now->tv_sec + 2;
598 ret = para_exec_cmdline_pid(&pid, conf.stat_cmd_arg, fds);
601 ret = mark_fd_nonblocking(fds[1]);
610 if (loaded >= bufsize) {
611 if (bufsize > 1000 * 1000) {
615 bufsize += bufsize + 1000;
616 buf = para_realloc(buf, bufsize);
618 assert(loaded < bufsize);
619 ret = read_nonblock(stat_pipe, buf + loaded, bufsize - loaded,
622 ret2 = for_each_stat_item(buf, loaded, update_item);
623 if (ret < 0 || ret2 < 0) {
625 PARA_NOTICE_LOG("closing stat pipe: %s\n", para_strerror(-ret));
629 free(stat_content[SI_BASENAME]);
630 stat_content[SI_BASENAME] =
631 para_strdup("stat command terminated!?");
635 sz = ret2; /* what is left */
636 if (sz > 0 && sz < loaded)
637 memmove(buf, buf + loaded - sz, sz);
645 static void init_wins(int top_lines)
647 int top_y = 0, bot_y = top_lines + 1, sb_y = LINES - 2,
648 in_y = LINES - 1, sep_y = top_lines;
649 int bot_lines = LINES - top_lines - 3, sb_lines = 1, in_lines = 1,
652 assume_default_colors(theme.default_fg, theme.default_bg);
654 wresize(top.win, top_lines, COLS);
655 mvwin(top.win, top_y, 0);
657 wresize(sb.win, sb_lines, COLS);
658 mvwin(sb.win, sb_y, 0);
660 wresize(sep.win, sep_lines, COLS);
661 mvwin(sep.win, sep_y, 0);
663 wresize(bot.win, bot_lines, COLS);
664 mvwin(bot.win, bot_y, 0);
666 wresize(in.win, in_lines, COLS);
667 mvwin(in.win, in_y, 0);
669 sep.win = newwin(sep_lines, COLS, sep_y, 0);
670 top.win = newwin(top_lines, COLS, top_y, 0);
671 bot.win = newwin(bot_lines, COLS, bot_y, 0);
672 sb.win = newwin(sb_lines, COLS, sb_y, 0);
673 in.win = newwin(in_lines, COLS, in_y, 0);
674 if (!top.win || !bot.win || !sb.win || !in.win || !sep.win)
675 die(EXIT_FAILURE, "Error: Cannot create curses windows\n");
679 scrollok(bot.win, 1);
680 wattron(sb.win, COLOR_PAIR(COLOR_STATUSBAR));
681 wattron(sep.win, COLOR_PAIR(COLOR_SEPARATOR));
682 wattron(bot.win, COLOR_PAIR(COLOR_BOT));
683 wattron(top.win, COLOR_PAIR(COLOR_TOP));
694 wmove(sep.win, 0, 0);
695 whline(sep.win, theme.sep_char, COLS);
699 wnoutrefresh(top.win);
700 wnoutrefresh(bot.win);
702 wnoutrefresh(sb.win);
703 wnoutrefresh(in.win);
704 wnoutrefresh(sep.win);
708 static void init_pair_or_die(short pair, short f, short b)
710 if (init_pair(pair, f, b) == ERR)
711 die(EXIT_FAILURE, "fatal: init_pair() failed\n");
714 static void init_colors_or_die(void)
719 die(EXIT_FAILURE, "fatal: No color term\n");
720 if (start_color() == ERR)
721 die(EXIT_FAILURE, "fatal: failed to start colors\n");
722 FOR_EACH_STATUS_ITEM(i)
723 if (theme.data[i].len)
724 init_pair_or_die(i + 1, theme.data[i].fg,
726 init_pair_or_die(COLOR_STATUSBAR, theme.sb_fg, theme.sb_bg);
727 init_pair_or_die(COLOR_COMMAND, theme.cmd_fg, theme.cmd_bg);
728 init_pair_or_die(COLOR_OUTPUT, theme.output_fg, theme.output_bg);
729 init_pair_or_die(COLOR_MSG, theme.msg_fg, theme.msg_bg);
730 init_pair_or_die(COLOR_ERRMSG, theme.err_msg_fg, theme.err_msg_bg);
731 init_pair_or_die(COLOR_SEPARATOR, theme.sep_fg, theme.sep_bg);
732 init_pair_or_die(COLOR_TOP, theme.default_fg, theme.default_bg);
733 init_pair_or_die(COLOR_BOT, theme.default_fg, theme.default_bg);
736 /* (Re-)initialize the curses library. */
737 static void init_curses(void)
741 if (top.win && refresh() == ERR) /* refresh is really needed */
742 die(EXIT_FAILURE, "refresh() failed\n");
743 if (LINES < theme.lines_min || COLS < theme.cols_min)
744 die(EXIT_FAILURE, "Terminal (%dx%d) too small"
745 " (need at least %dx%d)\n", COLS, LINES,
746 theme.cols_min, theme.lines_min);
747 curs_set(0); /* make cursor invisible, ignore errors */
748 nonl(); /* do not NL->CR/NL on output, always returns OK */
749 /* don't echo input */
751 die(EXIT_FAILURE, "fatal: noecho() failed\n");
752 /* take input chars one at a time, no wait for \n */
754 die(EXIT_FAILURE, "fatal: cbreak() failed\n");
755 init_colors_or_die();
756 clear(); /* ignore non-fatal errors */
757 init_wins(theme.top_lines_default);
758 // noecho(); /* don't echo input */
761 static void check_sigchld(void)
767 ret = para_reap_child(&pid);
770 if (pid == cmd_pid) {
773 print_in_bar(COLOR_MSG, " ");
775 goto reap_next_child;
779 * This sucker modifies its first argument. *handler and *arg are
780 * pointers to 0-terminated strings (inside line). Crap.
782 static int split_key_map(char *line, char **handler, char **arg)
784 if (!(*handler = strchr(line + 1, ':')))
788 if (!(*arg = strchr(*handler, ':')))
797 static void check_key_map_args_or_die(void)
802 for (i = 0; i < conf.key_map_given; ++i) {
806 tmp = para_strdup(conf.key_map_arg[i]);
807 if (!split_key_map(tmp, &handler, &arg))
809 if (strlen(handler) != 1)
811 if (*handler != 'x' && *handler != 'd' && *handler != 'i'
816 if (find_cmd_byname(arg) < 0)
819 if (i != conf.key_map_given)
820 die(EXIT_FAILURE, "invalid key map: %s\n", conf.key_map_arg[i]);
824 static void parse_config_file_or_die(bool override)
828 struct gui_cmdline_parser_params params = {
829 .override = override,
831 .check_required = !override,
832 .check_ambiguity = 0,
836 if (conf.config_file_given)
837 config_file = para_strdup(conf.config_file_arg);
839 char *home = para_homedir();
840 config_file = make_message("%s/.paraslash/gui.conf", home);
843 if (!file_exists(config_file)) {
844 if (!conf.config_file_given)
847 PARA_EMERG_LOG("config file %s does not exist\n",
853 gui_cmdline_parser_config_file(config_file, &conf, ¶ms);
854 loglevel = get_loglevel_by_name(conf.loglevel_arg);
855 check_key_map_args_or_die();
856 theme_init(conf.theme_arg, &theme);
864 /* reread configuration, terminate on errors */
865 static void reread_conf(void)
868 * gengetopt might print to stderr and exit on errors. So we have to
869 * shutdown curses first.
872 parse_config_file_or_die(true /* override */);
874 print_in_bar(COLOR_MSG, "config file reloaded\n");
878 * React to various signal-related events
880 static int signal_post_select(struct sched *s, __a_unused struct task *t)
882 int ret = para_next_signal(&s->rfds);
888 die(EXIT_FAILURE, "only the good die young (caught SIGTERM)\n");
891 if (curses_active()) {
898 PARA_WARNING_LOG("caught SIGINT, reset\n");
899 /* Nothing to do. SIGINT killed our child which gets noticed
900 * by do_select and resets everything.
904 PARA_NOTICE_LOG("got SIGUSR1, rereading configuration\n");
914 #define COMMAND_BUF_SIZE 32768
916 static enum cmd_status cmd_status(void)
918 if (command_fds[0] >= 0 || command_fds[1] >= 0)
925 static void command_pre_select(struct sched *s, __a_unused struct task *t)
927 enum cmd_status cmds = cmd_status();
929 if (cmds != CMDS_DCMD)
931 if (command_fds[0] >= 0)
932 para_fd_set(command_fds[0], &s->rfds, &s->max_fileno);
933 if (command_fds[1] >= 0)
934 para_fd_set(command_fds[1], &s->rfds, &s->max_fileno);
937 static int command_post_select(struct sched *s, __a_unused struct task *t)
940 static char command_buf[2][COMMAND_BUF_SIZE];
941 static int cbo[2]; /* command buf offsets */
942 static unsigned flags[2]; /* for for_each_line() */
943 enum cmd_status cmds = cmd_status();
945 if (cmds != CMDS_DCMD)
947 for (i = 0; i < 2; i++) {
949 if (command_fds[i] < 0)
951 ret = read_nonblock(command_fds[i],
952 command_buf[i] + cbo[i],
953 COMMAND_BUF_SIZE - 1 - cbo[i], &s->rfds, &sz);
956 cbo[i] = for_each_line(flags[i], command_buf[i], cbo[i],
957 add_output_line, &i);
958 if (sz != cbo[i]) { /* at least one line found */
962 if (ret < 0 || cmd_pid == 0) {
964 PARA_NOTICE_LOG("closing command fd %d: %s",
965 i, para_strerror(-ret));
966 close(command_fds[i]);
970 if (command_fds[!i] < 0) /* both fds closed */
973 if (cbo[i] == COMMAND_BUF_SIZE - 1) {
974 PARA_NOTICE_LOG("discarding overlong line");
976 flags[i] = FELF_DISCARD_FIRST;
982 static void input_pre_select(struct sched *s, __a_unused struct task *t)
984 enum cmd_status cmds = cmd_status();
986 if (cmds != CMDS_XCMD)
987 para_fd_set(STDIN_FILENO, &s->rfds, &s->max_fileno);
990 /* read from command pipe and print data to bot window */
991 static void exec_and_display_cmd(const char *cmd)
993 int ret, fds[3] = {0, 1, 1};
995 outputf(COLOR_COMMAND, "%s", cmd);
996 ret = para_exec_cmdline_pid(&cmd_pid, cmd, fds);
999 ret = mark_fd_nonblocking(fds[1]);
1002 ret = mark_fd_nonblocking(fds[2]);
1005 command_fds[0] = fds[1];
1006 command_fds[1] = fds[2];
1009 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
1010 close(command_fds[0]);
1011 close(command_fds[1]);
1014 static void para_cmd(char *cmd)
1018 print_in_bar(COLOR_MSG, "executing client command, hit any key to abort\n");
1019 c = make_message(BINDIR "/para_client -- %s", cmd);
1020 exec_and_display_cmd(c);
1024 static void display_cmd(char *cmd)
1026 print_in_bar(COLOR_MSG, "executing display command, hit any key to abort");
1027 exec_and_display_cmd(cmd);
1031 * shutdown curses and stat pipe before executing external commands
1033 static void external_cmd(char *cmd)
1035 int fds[3] = {-1, -1, -1};
1040 para_exec_cmdline_pid(&cmd_pid, cmd, fds);
1043 static void handle_command(int c)
1047 /* first check user-defined key bindings */
1048 for (i = 0; i < conf.key_map_given; ++i) {
1049 char *tmp, *handler, *arg;
1051 tmp = para_strdup(conf.key_map_arg[i]);
1052 if (!split_key_map(tmp, &handler, &arg)) {
1056 if (strcmp(tmp, km_keyname(c))) {
1060 if (*handler == 'd')
1062 else if (*handler == 'x')
1064 else if (*handler == 'p')
1066 else if (*handler == 'i') {
1067 int num = find_cmd_byname(arg);
1069 command_list[num].handler();
1074 /* not found, check internal key bindings */
1075 for (i = 0; command_list[i].handler; i++) {
1076 if (!strcmp(km_keyname(c), command_list[i].key)) {
1077 command_list[i].handler();
1081 print_in_bar(COLOR_ERRMSG, "key '%s' is not bound, press ? for help",
1085 static int input_post_select(__a_unused struct sched *s, __a_unused struct task *t)
1088 enum cmd_status cmds = cmd_status();
1090 if (cmds == CMDS_XCMD)
1092 ret = wgetch(top.win);
1093 if (ret == ERR || ret == KEY_RESIZE)
1095 if (cmds == CMDS_IDLE) {
1096 handle_command(ret);
1100 kill(cmd_pid, SIGTERM);
1104 static void signal_pre_select(struct sched *s, struct task *t)
1106 struct signal_task *st = container_of(t, struct signal_task, task);
1107 para_fd_set(st->fd, &s->rfds, &s->max_fileno);
1110 static void print_scroll_msg(void)
1112 unsigned lines_total, filled = ringbuffer_filled(bot_win_rb);
1113 int first_rbe = first_visible_rbe(&lines_total);
1115 print_in_bar(COLOR_MSG, "scrolled view: %d-%d/%d\n", filled - first_rbe,
1116 filled - scroll_position, ringbuffer_filled(bot_win_rb));
1119 static void com_scroll_top(void)
1121 int i = RINGBUFFER_SIZE - 1, bot_lines = get_num_lines(&bot);
1124 while (i > 0 && !ringbuffer_get(bot_win_rb, i))
1126 /* i is oldest entry */
1127 for (; lines < bot_lines && i >= 0; i--) {
1128 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
1131 lines += NUM_LINES(rbe->len);
1134 if (lines > 0 && scroll_position != i) {
1135 scroll_position = i;
1140 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1143 static void com_cancel_scroll(void)
1146 if (scroll_position == 0) {
1147 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1150 scroll_position = 0;
1154 static void com_page_down(void)
1157 int i = scroll_position, bot_lines = get_num_lines(&bot);
1159 while (lines < bot_lines && --i > 0) {
1160 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
1163 lines += NUM_LINES(rbe->len);
1166 scroll_position = i;
1171 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1174 static void com_page_up(void)
1177 int fvr = first_visible_rbe(&lines), bot_lines = get_num_lines(&bot);
1179 if (fvr < 0 || fvr + 1 >= ringbuffer_filled(bot_win_rb)) {
1180 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1183 scroll_position = fvr + 1;
1184 for (; scroll_position > 0; scroll_position--) {
1185 first_visible_rbe(&lines);
1186 if (lines == bot_lines)
1193 static void com_scroll_down(void)
1195 struct rb_entry *rbe;
1196 int rbe_lines, bot_lines = get_num_lines(&bot);
1198 if (!scroll_position) {
1199 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1203 rbe = ringbuffer_get(bot_win_rb, scroll_position);
1204 rbe_lines = NUM_LINES(rbe->len);
1205 wscrl(bot.win, rbe_lines);
1206 wmove(bot.win, bot_lines - rbe_lines, 0);
1207 wattron(bot.win, COLOR_PAIR(rbe->color));
1208 waddstr(bot.win, rbe->msg);
1213 static void com_scroll_up(void)
1215 struct rb_entry *rbe = NULL;
1217 int i, first_rbe, num_scroll;
1219 /* the entry that is going to vanish */
1220 rbe = ringbuffer_get(bot_win_rb, scroll_position);
1223 num_scroll = NUM_LINES(rbe->len);
1224 first_rbe = first_visible_rbe(&lines);
1225 if (first_rbe < 0 || (first_rbe == ringbuffer_filled(bot_win_rb) - 1))
1228 wscrl(bot.win, -num_scroll);
1229 i = draw_top_rbe(&lines);
1232 while (i > 0 && lines < num_scroll) {
1234 rbe = ringbuffer_get(bot_win_rb, --i);
1237 rbe_lines = NUM_LINES(rbe->len);
1239 wattron(bot.win, COLOR_PAIR(rbe->color));
1240 waddstr(bot.win, "\n");
1241 waddstr(bot.win, rbe->msg);
1250 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1253 static void com_ll_decr(void)
1255 if (loglevel <= LL_DEBUG) {
1256 print_in_bar(COLOR_ERRMSG,
1257 "loglevel already at maximal verbosity\n");
1261 print_in_bar(COLOR_MSG, "loglevel set to %d\n", loglevel);
1264 static void com_ll_incr(void)
1266 if (loglevel >= LL_EMERG) {
1267 print_in_bar(COLOR_ERRMSG,
1268 "loglevel already at minimal verbosity\n");
1272 print_in_bar(COLOR_MSG, "loglevel set to %d\n", loglevel);
1275 static void com_reread_conf(void)
1280 static void com_help(void)
1284 for (i = 0; i < conf.key_map_given; ++i) {
1285 char *handler, *arg, *tmp = para_strdup(conf.key_map_arg[i]);
1286 const char *handler_text = "???", *desc = NULL;
1288 if (!split_key_map(tmp, &handler, &arg)) {
1294 handler_text = "internal";
1295 desc = command_list[find_cmd_byname(arg)].description;
1297 case 'x': handler_text = "external"; break;
1298 case 'd': handler_text = "display "; break;
1299 case 'p': handler_text = "para "; break;
1301 outputf(COLOR_MSG, "%s\t%s\t%s%s\t%s", tmp, handler_text, arg,
1302 strlen(arg) < 8? "\t" : "",
1306 for (i = 0; command_list[i].handler; i++) {
1307 struct gui_command gc = command_list[i];
1309 outputf(COLOR_MSG, "%s\tinternal\t%s\t%s%s", gc.key, gc.name,
1310 strlen(gc.name) < 8? "\t" : "",
1313 print_in_bar(COLOR_MSG, "try \"para_gui -h\" or \"para_client help\" "
1317 static void com_shrink_top_win(void)
1319 int top_lines = get_num_lines(&top);
1321 if (top_lines <= theme.top_lines_min) {
1322 PARA_WARNING_LOG("can not decrease top window\n");
1325 init_wins(top_lines - 1);
1326 print_in_bar(COLOR_MSG, "%s", "decreased top window");
1329 static void com_enlarge_top_win(void)
1331 int top_lines = get_num_lines(&top), bot_lines = get_num_lines(&bot);
1333 if (bot_lines < 3) {
1334 PARA_WARNING_LOG("can not increase top window\n");
1337 init_wins(top_lines + 1);
1338 print_in_bar(COLOR_MSG, "increased top window");
1341 static void com_version(void)
1343 print_in_bar(COLOR_MSG, "%s", version_single_line("gui"));
1346 __noreturn static void com_quit(void)
1348 die(EXIT_SUCCESS, "%s", "");
1351 static void com_refresh(void)
1357 static void com_next_theme(void)
1363 static void com_prev_theme(void)
1369 __noreturn static void print_help_and_die(void)
1371 struct ggo_help h = DEFINE_GGO_HELP(gui);
1372 bool d = conf.detailed_help_given;
1374 ggo_print_help(&h, d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS);
1378 static int setup_tasks_and_schedule(void)
1380 struct sched sched = {
1381 .default_timeout = {
1382 .tv_sec = conf.timeout_arg / 1000,
1383 .tv_usec = (conf.timeout_arg % 1000) * 1000,
1386 struct cmd_task cmd_task = {
1389 .pre_select = command_pre_select,
1390 .post_select = command_post_select,
1393 struct status_task status_task = {
1396 .pre_select = status_pre_select,
1397 .post_select = status_post_select,
1400 struct input_task input_task = {
1403 .pre_select = input_pre_select,
1404 .post_select = input_post_select,
1407 struct signal_task signal_task = {
1410 .pre_select = signal_pre_select,
1411 .post_select = signal_post_select,
1414 signal_task.fd = para_signal_init();
1415 para_install_sighandler(SIGINT);
1416 para_install_sighandler(SIGTERM);
1417 para_install_sighandler(SIGCHLD);
1418 para_install_sighandler(SIGWINCH);
1419 para_install_sighandler(SIGUSR1);
1421 register_task(&sched, &cmd_task.task);
1422 register_task(&sched, &status_task.task);
1423 register_task(&sched, &input_task.task);
1424 register_task(&sched, &signal_task.task);
1425 return schedule(&sched);
1428 int main(int argc, char *argv[])
1430 gui_cmdline_parser(argc, argv, &conf); /* exits on errors */
1431 loglevel = get_loglevel_by_name(conf.loglevel_arg);
1432 version_handle_flag("gui", conf.version_given);
1433 if (conf.help_given || conf.detailed_help_given)
1434 print_help_and_die();
1435 parse_config_file_or_die(false /* override */);
1436 bot_win_rb = ringbuffer_new(RINGBUFFER_SIZE);
1437 setlocale(LC_CTYPE, "");
1438 initscr(); /* needed only once, always successful */
1440 return setup_tasks_and_schedule() < 0? EXIT_FAILURE : EXIT_SUCCESS;