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;
47 static pid_t exec_pid;
49 static int exec_fds[2] = {-1, -1};
50 static struct gui_args_info conf;
54 EXEC_IDLE, /* no command running */
55 EXEC_DCMD, /* para or display command running */
56 EXEC_XCMD, /* external command running */
60 * Codes for various colors.
62 * Each status item has its own color pair. The ones defined here start at a
63 * higher number so that they do not overlap with these.
66 COLOR_STATUSBAR = NUM_STAT_ITEMS + 1,
79 const char *description;
80 void (*handler)(void);
83 static struct gui_theme theme;
85 #define GUI_COMMANDS \
86 GUI_COMMAND(help, "?", "print help") \
87 GUI_COMMAND(enlarge_top_win, "+", "enlarge the top window") \
88 GUI_COMMAND(shrink_top_win, "-", "shrink the top window") \
89 GUI_COMMAND(reread_conf, "r", "reread configuration file") \
90 GUI_COMMAND(quit, "q", "exit para_gui") \
91 GUI_COMMAND(refresh, "^L", "redraw the screen") \
92 GUI_COMMAND(next_theme, ".", "switch to next theme") \
93 GUI_COMMAND(prev_theme, ",", "switch to previous theme") \
94 GUI_COMMAND(ll_incr, ">", "increase loglevel (decreases verbosity)") \
95 GUI_COMMAND(ll_decr, "<", "decrease loglevel (increases verbosity)") \
96 GUI_COMMAND(version, "V", "show the para_gui version") \
97 GUI_COMMAND(scroll_up, "<up>", "scroll up one line") \
98 GUI_COMMAND(scroll_down, "<down>", "scroll_down") \
99 GUI_COMMAND(page_up, "<ppage>", "scroll up one page") \
100 GUI_COMMAND(page_down, "<npage>", "scroll down one page") \
101 GUI_COMMAND(scroll_top, "<home>", "scroll to top of buffer") \
102 GUI_COMMAND(cancel_scroll, "<end>", "deactivate scroll mode") \
104 /* declare command handlers */
105 #define GUI_COMMAND(_c, _k, _d) \
106 static void com_ ## _c(void);
111 /* define command array */
112 #define GUI_COMMAND(_c, _k, _d) \
117 .handler = com_ ## _c \
120 static struct gui_command command_list[] = {GUI_COMMANDS {.name = NULL}};
130 struct timeval next_exec;
134 #define COMMAND_BUF_SIZE 32768
138 char command_buf[2][COMMAND_BUF_SIZE]; /* stdout/stderr of command */
139 int cbo[2]; /* command buf offsets */
140 unsigned flags[2]; /* passed to for_each_line() */
143 static int find_cmd_byname(char *name)
147 for (i = 0; command_list[i].handler; i++)
148 if (!strcmp(command_list[i].name, name))
154 * Even though ncurses provides getmaxx and getmaxy, these functions/macros are
155 * not described in the XSI Curses standard.
157 static int get_num_lines(struct gui_window *w)
160 __a_unused int cols; /* avoid "set but not used" warnings */
162 getmaxyx(w->win, lines, cols);
166 static int get_num_cols(struct gui_window *w)
168 __a_unused int lines; /* avoid "set but not used" warnings */
171 getmaxyx(w->win, lines, cols);
175 /** Number of lines of the window are occupied by an output line. */
176 #define NUM_LINES(len) (1 + (len) / get_num_cols(&bot))
178 /* isendwin() returns false before initscr() was called */
179 static bool curses_active(void)
181 return top.win && !isendwin();
184 /* taken from mutt */
185 static char *km_keyname(int c)
190 sprintf(buf, "<up>");
194 sprintf(buf, "<down>");
198 sprintf(buf, "<left>");
201 if (c == KEY_RIGHT) {
202 sprintf(buf, "<right>");
205 if (c == KEY_NPAGE) {
206 sprintf(buf, "<npage>");
209 if (c == KEY_PPAGE) {
210 sprintf(buf, "<ppage>");
214 sprintf(buf, "<home>");
218 sprintf(buf, "<end>");
221 if (c < 256 && c > -128 && iscntrl((unsigned char) c)) {
226 buf[1] = (c + '@') & 0x7f;
229 snprintf(buf, sizeof(buf), "\\%d%d%d", c >> 6,
230 (c >> 3) & 7, c & 7);
231 } else if (c >= KEY_F0 && c < KEY_F(256))
232 sprintf(buf, "<F%d>", c - KEY_F0);
234 snprintf(buf, sizeof(buf), "%c", (unsigned char) c);
236 snprintf(buf, sizeof(buf), "\\x%hx", (unsigned short) c);
240 /* Print given number of spaces to curses window. */
241 static void add_spaces(WINDOW* win, unsigned int num)
244 unsigned sz = sizeof(space) - 1; /* number of spaces */
258 * print aligned string to curses window. This function always prints
261 static int align_str(WINDOW* win, char *str, unsigned int len,
264 int ret, i, num; /* of spaces */
269 ret = strwidth(str, &width);
271 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
280 /* replace control characters by spaces */
281 for (i = 0; i < len && str[i]; i++) {
282 if (str[i] == '\n' || str[i] == '\r' || str[i] == '\f')
287 add_spaces(win, num);
288 } else if (align == RIGHT) {
289 add_spaces(win, num);
292 add_spaces(win, num / 2);
293 waddstr(win, str[0]? str: "");
294 add_spaces(win, num - num / 2);
299 __printf_2_3 static void print_in_bar(int color, const char *fmt,...)
304 if (!curses_active())
306 wattron(in.win, COLOR_PAIR(color));
308 xvasprintf(&msg, fmt, ap);
311 align_str(in.win, msg, get_num_cols(&in), LEFT);
316 static void print_status_bar(void)
320 tmp = para_strdup("para_gui " PACKAGE_VERSION " (hit ? for help)");
322 align_str(sb.win, tmp, get_num_cols(&sb), CENTER);
327 * get the number of the oldest rbe that is (partially) visible. On return,
328 * lines contains the sum of the number of lines of all visible entries. If the
329 * first one is only partially visible, lines is greater than bot.lines.
331 static int first_visible_rbe(unsigned *lines)
333 int i, bot_lines = get_num_lines(&bot);
336 for (i = scroll_position; i < RINGBUFFER_SIZE; i++) {
337 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
341 rbe_lines = NUM_LINES(rbe->len);
342 if (rbe_lines > bot_lines)
345 if (*lines >= bot_lines)
348 return RINGBUFFER_SIZE - 1;
352 returns number of first visible rbe, *lines is the number of lines drawn.
354 static int draw_top_rbe(unsigned *lines)
356 int bot_cols, bot_lines, ret, fvr = first_visible_rbe(lines);
357 struct rb_entry *rbe;
358 size_t bytes_to_skip, cells_to_skip, width;
362 wmove(bot.win, 0, 0);
363 rbe = ringbuffer_get(bot_win_rb, fvr);
366 getmaxyx(bot.win, bot_lines, bot_cols);
367 if (*lines > bot_lines) {
368 /* rbe is partially visible multi-line */
369 cells_to_skip = (*lines - bot_lines) * bot_cols;
370 ret = skip_cells(rbe->msg, cells_to_skip, &bytes_to_skip);
373 ret = strwidth(rbe->msg + bytes_to_skip, &width);
380 wattron(bot.win, COLOR_PAIR(rbe->color));
381 waddstr(bot.win, rbe->msg + bytes_to_skip);
382 *lines = NUM_LINES(width);
386 static void redraw_bot_win(void)
389 int i, bot_lines = get_num_lines(&bot);
391 wmove(bot.win, 0, 0);
393 i = draw_top_rbe(&lines);
396 while (i > 0 && lines < bot_lines) {
397 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, --i);
400 waddstr(bot.win, "\n");
403 lines += NUM_LINES(rbe->len);
404 wattron(bot.win, COLOR_PAIR(rbe->color));
405 waddstr(bot.win, "\n");
406 waddstr(bot.win, rbe->msg);
412 static void rb_add_entry(int color, char *msg)
414 struct rb_entry *old, *new;
418 if (strwidth(msg, &len) < 0)
420 new = para_malloc(sizeof(struct rb_entry));
424 old = ringbuffer_add(bot_win_rb, new);
429 if (scroll_position) {
430 /* discard current scrolling, like xterm does */
435 wattron(bot.win, COLOR_PAIR(color));
436 getyx(bot.win, y, x);
438 waddstr(bot.win, "\n");
439 waddstr(bot.win, msg);
443 * print formated output to bot win and refresh
445 __printf_2_3 static void outputf(int color, const char* fmt,...)
450 if (!curses_active())
453 xvasprintf(&msg, fmt, ap);
455 rb_add_entry(color, msg);
459 static int add_output_line(char *line, void *data)
461 int color = *(int *)data? COLOR_ERRMSG : COLOR_OUTPUT;
463 if (!curses_active())
465 rb_add_entry(color, para_strdup(line));
469 static __printf_2_3 void curses_log(int ll, const char *fmt,...)
476 if (curses_active()) {
477 int color = ll <= LL_NOTICE? COLOR_MSG : COLOR_ERRMSG;
479 unsigned bytes = xvasprintf(&msg, fmt, ap);
480 if (bytes > 0 && msg[bytes - 1] == '\n')
481 msg[bytes - 1] = '\0'; /* cut trailing newline */
482 rb_add_entry(color, msg);
484 } else if (exec_pid <= 0) /* no external command running */
485 vfprintf(stderr, fmt, ap);
488 __printf_2_3 void (*para_log)(int, const char*, ...) = curses_log;
490 static void shutdown_curses(void)
496 /* disable curses, print a message, kill running processes and exit */
497 __noreturn __printf_2_3 static void die(int exit_code, const char* fmt, ...)
503 vfprintf(stderr, fmt, argp);
505 /* kill every process in the process group and exit */
506 para_sigaction(SIGTERM, SIG_IGN);
512 * Print stat item #i to curses window
514 static void print_stat_item(int i)
517 struct stat_item_data d = theme.data[i];
518 char *c = stat_content[i];
519 int top_lines = get_num_lines(&top);
521 if (!curses_active() || !d.len || !c)
523 tmp = make_message("%s%s%s", d.prefix, c, d.postfix);
524 wmove(top.win, d.y * top_lines / 100, d.x * COLS / 100);
526 wattron(top.win, COLOR_PAIR(i + 1));
527 align_str(top.win, tmp, d.len * COLS / 100, d.align);
532 static int update_item(int item_num, char *buf)
534 char **c = stat_content + item_num;
541 *c = para_strdup("(artist tag not set)");
544 *c = para_strdup("(title tag not set)");
547 *c = para_strdup("????");
550 *c = para_strdup("(album tag not set)");
553 *c = para_strdup("(comment tag not set)");
557 *c = para_strdup(buf);
559 print_stat_item(item_num);
563 static void print_all_items(void)
567 if (!curses_active())
569 FOR_EACH_STATUS_ITEM(i)
573 static void clear_all_items(void)
577 FOR_EACH_STATUS_ITEM(i) {
578 free(stat_content[i]);
579 stat_content[i] = para_strdup("");
583 static void status_pre_select(struct sched *s, struct task *t)
585 struct status_task *st = container_of(t, struct status_task, task);
588 return para_fd_set(st->fd, &s->rfds, &s->max_fileno);
589 sched_request_barrier_or_min_delay(&st->next_exec, s);
592 static int status_post_select(struct sched *s, struct task *t)
594 struct status_task *st = container_of(t, struct status_task, task);
600 int fds[3] = {0, 1, 0};
601 /* Avoid busy loop */
602 if (tv_diff(&st->next_exec, now, NULL) > 0)
604 st->next_exec.tv_sec = now->tv_sec + 2;
605 ret = para_exec_cmdline_pid(&pid, conf.stat_cmd_arg, fds);
608 ret = mark_fd_nonblocking(fds[1]);
617 if (st->loaded >= st->bufsize) {
618 if (st->bufsize > 1000 * 1000) {
622 st->bufsize += st->bufsize + 1000;
623 st->buf = para_realloc(st->buf, st->bufsize);
625 assert(st->loaded < st->bufsize);
626 ret = read_nonblock(st->fd, st->buf + st->loaded,
627 st->bufsize - st->loaded, &s->rfds, &sz);
629 ret2 = for_each_stat_item(st->buf, st->loaded, update_item);
630 if (ret < 0 || ret2 < 0) {
632 PARA_NOTICE_LOG("closing stat pipe: %s\n", para_strerror(-ret));
636 free(stat_content[SI_BASENAME]);
637 stat_content[SI_BASENAME] =
638 para_strdup("stat command terminated!?");
642 sz = ret2; /* what is left */
643 if (sz > 0 && sz < st->loaded)
644 memmove(st->buf, st->buf + st->loaded - sz, sz);
652 static void init_wins(int top_lines)
654 int top_y = 0, bot_y = top_lines + 1, sb_y = LINES - 2,
655 in_y = LINES - 1, sep_y = top_lines;
656 int bot_lines = LINES - top_lines - 3, sb_lines = 1, in_lines = 1,
659 assume_default_colors(theme.default_fg, theme.default_bg);
661 wresize(top.win, top_lines, COLS);
662 mvwin(top.win, top_y, 0);
664 wresize(sb.win, sb_lines, COLS);
665 mvwin(sb.win, sb_y, 0);
667 wresize(sep.win, sep_lines, COLS);
668 mvwin(sep.win, sep_y, 0);
670 wresize(bot.win, bot_lines, COLS);
671 mvwin(bot.win, bot_y, 0);
673 wresize(in.win, in_lines, COLS);
674 mvwin(in.win, in_y, 0);
676 sep.win = newwin(sep_lines, COLS, sep_y, 0);
677 top.win = newwin(top_lines, COLS, top_y, 0);
678 bot.win = newwin(bot_lines, COLS, bot_y, 0);
679 sb.win = newwin(sb_lines, COLS, sb_y, 0);
680 in.win = newwin(in_lines, COLS, in_y, 0);
681 if (!top.win || !bot.win || !sb.win || !in.win || !sep.win)
682 die(EXIT_FAILURE, "Error: Cannot create curses windows\n");
686 scrollok(bot.win, 1);
687 wattron(sb.win, COLOR_PAIR(COLOR_STATUSBAR));
688 wattron(sep.win, COLOR_PAIR(COLOR_SEPARATOR));
689 wattron(bot.win, COLOR_PAIR(COLOR_BOT));
690 wattron(top.win, COLOR_PAIR(COLOR_TOP));
701 wmove(sep.win, 0, 0);
702 whline(sep.win, theme.sep_char, COLS);
706 wnoutrefresh(top.win);
707 wnoutrefresh(bot.win);
709 wnoutrefresh(sb.win);
710 wnoutrefresh(in.win);
711 wnoutrefresh(sep.win);
715 static void init_pair_or_die(short pair, short f, short b)
717 if (init_pair(pair, f, b) == ERR)
718 die(EXIT_FAILURE, "fatal: init_pair() failed\n");
721 static void init_colors_or_die(void)
726 die(EXIT_FAILURE, "fatal: No color term\n");
727 if (start_color() == ERR)
728 die(EXIT_FAILURE, "fatal: failed to start colors\n");
729 FOR_EACH_STATUS_ITEM(i)
730 if (theme.data[i].len)
731 init_pair_or_die(i + 1, theme.data[i].fg,
733 init_pair_or_die(COLOR_STATUSBAR, theme.sb_fg, theme.sb_bg);
734 init_pair_or_die(COLOR_COMMAND, theme.cmd_fg, theme.cmd_bg);
735 init_pair_or_die(COLOR_OUTPUT, theme.output_fg, theme.output_bg);
736 init_pair_or_die(COLOR_MSG, theme.msg_fg, theme.msg_bg);
737 init_pair_or_die(COLOR_ERRMSG, theme.err_msg_fg, theme.err_msg_bg);
738 init_pair_or_die(COLOR_SEPARATOR, theme.sep_fg, theme.sep_bg);
739 init_pair_or_die(COLOR_TOP, theme.default_fg, theme.default_bg);
740 init_pair_or_die(COLOR_BOT, theme.default_fg, theme.default_bg);
743 /* (Re-)initialize the curses library. */
744 static void init_curses(void)
748 if (top.win && refresh() == ERR) /* refresh is really needed */
749 die(EXIT_FAILURE, "refresh() failed\n");
750 if (LINES < theme.lines_min || COLS < theme.cols_min)
751 die(EXIT_FAILURE, "Terminal (%dx%d) too small"
752 " (need at least %dx%d)\n", COLS, LINES,
753 theme.cols_min, theme.lines_min);
754 curs_set(0); /* make cursor invisible, ignore errors */
755 nonl(); /* do not NL->CR/NL on output, always returns OK */
756 /* don't echo input */
758 die(EXIT_FAILURE, "fatal: noecho() failed\n");
759 /* take input chars one at a time, no wait for \n */
761 die(EXIT_FAILURE, "fatal: cbreak() failed\n");
762 init_colors_or_die();
763 clear(); /* ignore non-fatal errors */
764 init_wins(theme.top_lines_default);
765 // noecho(); /* don't echo input */
768 static void check_sigchld(void)
774 ret = para_reap_child(&pid);
777 if (pid == exec_pid) {
780 print_in_bar(COLOR_MSG, " ");
782 goto reap_next_child;
786 * This sucker modifies its first argument. *handler and *arg are
787 * pointers to 0-terminated strings (inside line). Crap.
789 static int split_key_map(char *line, char **handler, char **arg)
791 if (!(*handler = strchr(line + 1, ':')))
795 if (!(*arg = strchr(*handler, ':')))
804 static void check_key_map_args_or_die(void)
809 for (i = 0; i < conf.key_map_given; ++i) {
813 tmp = para_strdup(conf.key_map_arg[i]);
814 if (!split_key_map(tmp, &handler, &arg))
816 if (strlen(handler) != 1)
818 if (*handler != 'x' && *handler != 'd' && *handler != 'i'
823 if (find_cmd_byname(arg) < 0)
826 if (i != conf.key_map_given)
827 die(EXIT_FAILURE, "invalid key map: %s\n", conf.key_map_arg[i]);
831 static void parse_config_file_or_die(bool override)
835 struct gui_cmdline_parser_params params = {
836 .override = override,
838 .check_required = !override,
839 .check_ambiguity = 0,
843 if (conf.config_file_given)
844 config_file = para_strdup(conf.config_file_arg);
846 char *home = para_homedir();
847 config_file = make_message("%s/.paraslash/gui.conf", home);
850 if (!file_exists(config_file)) {
851 if (!conf.config_file_given)
854 PARA_EMERG_LOG("config file %s does not exist\n",
860 gui_cmdline_parser_config_file(config_file, &conf, ¶ms);
861 loglevel = get_loglevel_by_name(conf.loglevel_arg);
862 check_key_map_args_or_die();
863 theme_init(conf.theme_arg, &theme);
871 /* reread configuration, terminate on errors */
872 static void reread_conf(void)
875 * gengetopt might print to stderr and exit on errors. So we have to
876 * shutdown curses first.
879 parse_config_file_or_die(true /* override */);
881 print_in_bar(COLOR_MSG, "config file reloaded\n");
885 * React to various signal-related events
887 static int signal_post_select(struct sched *s, __a_unused struct task *t)
889 int ret = para_next_signal(&s->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");
921 static enum exec_status exec_status(void)
923 if (exec_fds[0] >= 0 || exec_fds[1] >= 0)
930 static void exec_pre_select(struct sched *s, __a_unused struct task *t)
932 enum exec_status es = exec_status();
936 if (exec_fds[0] >= 0)
937 para_fd_set(exec_fds[0], &s->rfds, &s->max_fileno);
938 if (exec_fds[1] >= 0)
939 para_fd_set(exec_fds[1], &s->rfds, &s->max_fileno);
942 static int exec_post_select(struct sched *s, struct task *t)
944 struct exec_task *ct = container_of(t, struct exec_task, task);
946 enum exec_status es = exec_status();
950 for (i = 0; i < 2; i++) {
954 ret = read_nonblock(exec_fds[i],
955 ct->command_buf[i] + ct->cbo[i],
956 COMMAND_BUF_SIZE - 1 - ct->cbo[i], &s->rfds, &sz);
959 ct->cbo[i] = for_each_line(ct->flags[i], ct->command_buf[i],
960 ct->cbo[i], add_output_line, &i);
961 if (sz != ct->cbo[i]) { /* at least one line found */
965 if (ret < 0 || exec_pid == 0) {
967 PARA_NOTICE_LOG("closing command fd %d: %s",
968 i, para_strerror(-ret));
973 if (exec_fds[!i] < 0) /* both fds closed */
976 if (ct->cbo[i] == COMMAND_BUF_SIZE - 1) {
977 PARA_NOTICE_LOG("discarding overlong line");
979 ct->flags[i] = FELF_DISCARD_FIRST;
985 static void input_pre_select(struct sched *s, __a_unused struct task *t)
987 if (exec_status() != EXEC_XCMD)
988 para_fd_set(STDIN_FILENO, &s->rfds, &s->max_fileno);
991 /* read from command pipe and print data to bot window */
992 static void exec_and_display(const char *file_and_args)
994 int ret, fds[3] = {0, 1, 1};
996 outputf(COLOR_COMMAND, "%s", file_and_args);
997 ret = para_exec_cmdline_pid(&exec_pid, file_and_args, fds);
1000 ret = mark_fd_nonblocking(fds[1]);
1003 ret = mark_fd_nonblocking(fds[2]);
1006 exec_fds[0] = fds[1];
1007 exec_fds[1] = fds[2];
1008 print_in_bar(COLOR_MSG, "hit any key to abort\n");
1011 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
1016 static void exec_para(const char *args)
1018 char *file_and_args;
1020 file_and_args = make_message(BINDIR "/para_client -- %s", args);
1021 exec_and_display(file_and_args);
1022 free(file_and_args);
1026 * shutdown curses and stat pipe before executing external commands
1028 static void exec_external(char *file_and_args)
1030 int fds[3] = {-1, -1, -1};
1035 para_exec_cmdline_pid(&exec_pid, file_and_args, fds);
1038 static void handle_command(int c)
1042 /* first check user-defined key bindings */
1043 for (i = 0; i < conf.key_map_given; ++i) {
1044 char *tmp, *handler, *arg;
1046 tmp = para_strdup(conf.key_map_arg[i]);
1047 if (!split_key_map(tmp, &handler, &arg)) {
1051 if (strcmp(tmp, km_keyname(c))) {
1055 if (*handler == 'd')
1056 exec_and_display(arg);
1057 else if (*handler == 'x')
1059 else if (*handler == 'p')
1061 else if (*handler == 'i') {
1062 int num = find_cmd_byname(arg);
1064 command_list[num].handler();
1069 /* not found, check internal key bindings */
1070 for (i = 0; command_list[i].handler; i++) {
1071 if (!strcmp(km_keyname(c), command_list[i].key)) {
1072 command_list[i].handler();
1076 print_in_bar(COLOR_ERRMSG, "key '%s' is not bound, press ? for help",
1080 static int input_post_select(__a_unused struct sched *s, __a_unused struct task *t)
1083 enum exec_status exs = exec_status();
1085 if (exs == EXEC_XCMD)
1087 ret = wgetch(top.win);
1088 if (ret == ERR || ret == KEY_RESIZE)
1090 if (exs == EXEC_IDLE) {
1091 handle_command(ret);
1095 kill(exec_pid, SIGTERM);
1099 static void signal_pre_select(struct sched *s, struct task *t)
1101 struct signal_task *st = container_of(t, struct signal_task, task);
1102 para_fd_set(st->fd, &s->rfds, &s->max_fileno);
1105 static void print_scroll_msg(void)
1107 unsigned lines_total, filled = ringbuffer_filled(bot_win_rb);
1108 int first_rbe = first_visible_rbe(&lines_total);
1110 print_in_bar(COLOR_MSG, "scrolled view: %d-%d/%d\n", filled - first_rbe,
1111 filled - scroll_position, ringbuffer_filled(bot_win_rb));
1114 static void com_scroll_top(void)
1116 int i = RINGBUFFER_SIZE - 1, bot_lines = get_num_lines(&bot);
1119 while (i > 0 && !ringbuffer_get(bot_win_rb, i))
1121 /* i is oldest entry */
1122 for (; lines < bot_lines && i >= 0; i--) {
1123 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
1126 lines += NUM_LINES(rbe->len);
1129 if (lines > 0 && scroll_position != i) {
1130 scroll_position = i;
1135 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1138 static void com_cancel_scroll(void)
1141 if (scroll_position == 0) {
1142 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1145 scroll_position = 0;
1149 static void com_page_down(void)
1152 int i = scroll_position, bot_lines = get_num_lines(&bot);
1154 while (lines < bot_lines && --i > 0) {
1155 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
1158 lines += NUM_LINES(rbe->len);
1161 scroll_position = i;
1166 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1169 static void com_page_up(void)
1172 int fvr = first_visible_rbe(&lines), bot_lines = get_num_lines(&bot);
1174 if (fvr < 0 || fvr + 1 >= ringbuffer_filled(bot_win_rb)) {
1175 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1178 scroll_position = fvr + 1;
1179 for (; scroll_position > 0; scroll_position--) {
1180 first_visible_rbe(&lines);
1181 if (lines == bot_lines)
1188 static void com_scroll_down(void)
1190 struct rb_entry *rbe;
1191 int rbe_lines, bot_lines = get_num_lines(&bot);
1193 if (!scroll_position) {
1194 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1198 rbe = ringbuffer_get(bot_win_rb, scroll_position);
1199 rbe_lines = NUM_LINES(rbe->len);
1200 wscrl(bot.win, rbe_lines);
1201 wmove(bot.win, bot_lines - rbe_lines, 0);
1202 wattron(bot.win, COLOR_PAIR(rbe->color));
1203 waddstr(bot.win, rbe->msg);
1208 static void com_scroll_up(void)
1210 struct rb_entry *rbe = NULL;
1212 int i, first_rbe, num_scroll;
1214 /* the entry that is going to vanish */
1215 rbe = ringbuffer_get(bot_win_rb, scroll_position);
1218 num_scroll = NUM_LINES(rbe->len);
1219 first_rbe = first_visible_rbe(&lines);
1220 if (first_rbe < 0 || (first_rbe == ringbuffer_filled(bot_win_rb) - 1))
1223 wscrl(bot.win, -num_scroll);
1224 i = draw_top_rbe(&lines);
1227 while (i > 0 && lines < num_scroll) {
1229 rbe = ringbuffer_get(bot_win_rb, --i);
1232 rbe_lines = NUM_LINES(rbe->len);
1234 wattron(bot.win, COLOR_PAIR(rbe->color));
1235 waddstr(bot.win, "\n");
1236 waddstr(bot.win, rbe->msg);
1245 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1248 static void com_ll_decr(void)
1250 if (loglevel <= LL_DEBUG) {
1251 print_in_bar(COLOR_ERRMSG,
1252 "loglevel already at maximal verbosity\n");
1256 print_in_bar(COLOR_MSG, "loglevel set to %d\n", loglevel);
1259 static void com_ll_incr(void)
1261 if (loglevel >= LL_EMERG) {
1262 print_in_bar(COLOR_ERRMSG,
1263 "loglevel already at minimal verbosity\n");
1267 print_in_bar(COLOR_MSG, "loglevel set to %d\n", loglevel);
1270 static void com_reread_conf(void)
1275 static void com_help(void)
1279 for (i = 0; i < conf.key_map_given; ++i) {
1280 char *handler, *arg, *tmp = para_strdup(conf.key_map_arg[i]);
1281 const char *handler_text = "???", *desc = NULL;
1283 if (!split_key_map(tmp, &handler, &arg)) {
1289 handler_text = "internal";
1290 desc = command_list[find_cmd_byname(arg)].description;
1292 case 'x': handler_text = "external"; break;
1293 case 'd': handler_text = "display "; break;
1294 case 'p': handler_text = "para "; break;
1296 outputf(COLOR_MSG, "%s\t%s\t%s%s\t%s", tmp, handler_text, arg,
1297 strlen(arg) < 8? "\t" : "",
1301 for (i = 0; command_list[i].handler; i++) {
1302 struct gui_command gc = command_list[i];
1304 outputf(COLOR_MSG, "%s\tinternal\t%s\t%s%s", gc.key, gc.name,
1305 strlen(gc.name) < 8? "\t" : "",
1308 print_in_bar(COLOR_MSG, "try \"para_gui -h\" or \"para_client help\" "
1312 static void com_shrink_top_win(void)
1314 int top_lines = get_num_lines(&top);
1316 if (top_lines <= theme.top_lines_min) {
1317 PARA_WARNING_LOG("can not decrease top window\n");
1320 init_wins(top_lines - 1);
1321 print_in_bar(COLOR_MSG, "%s", "decreased top window");
1324 static void com_enlarge_top_win(void)
1326 int top_lines = get_num_lines(&top), bot_lines = get_num_lines(&bot);
1328 if (bot_lines < 3) {
1329 PARA_WARNING_LOG("can not increase top window\n");
1332 init_wins(top_lines + 1);
1333 print_in_bar(COLOR_MSG, "increased top window");
1336 static void com_version(void)
1338 print_in_bar(COLOR_MSG, "%s", version_single_line("gui"));
1341 __noreturn static void com_quit(void)
1343 die(EXIT_SUCCESS, "%s", "");
1346 static void com_refresh(void)
1352 static void com_next_theme(void)
1358 static void com_prev_theme(void)
1364 __noreturn static void print_help_and_die(void)
1366 struct ggo_help h = DEFINE_GGO_HELP(gui);
1367 bool d = conf.detailed_help_given;
1369 ggo_print_help(&h, d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS);
1373 static int setup_tasks_and_schedule(void)
1375 struct sched sched = {
1376 .default_timeout = {
1377 .tv_sec = conf.timeout_arg / 1000,
1378 .tv_usec = (conf.timeout_arg % 1000) * 1000,
1381 struct exec_task exec_task = {
1384 .pre_select = exec_pre_select,
1385 .post_select = exec_post_select,
1388 struct status_task status_task = {
1391 .pre_select = status_pre_select,
1392 .post_select = status_post_select,
1396 struct input_task input_task = {
1399 .pre_select = input_pre_select,
1400 .post_select = input_post_select,
1403 struct signal_task signal_task = {
1406 .pre_select = signal_pre_select,
1407 .post_select = signal_post_select,
1410 signal_task.fd = para_signal_init();
1411 para_install_sighandler(SIGINT);
1412 para_install_sighandler(SIGTERM);
1413 para_install_sighandler(SIGCHLD);
1414 para_install_sighandler(SIGWINCH);
1415 para_install_sighandler(SIGUSR1);
1417 register_task(&sched, &exec_task.task);
1418 register_task(&sched, &status_task.task);
1419 register_task(&sched, &input_task.task);
1420 register_task(&sched, &signal_task.task);
1421 return schedule(&sched);
1424 int main(int argc, char *argv[])
1426 gui_cmdline_parser(argc, argv, &conf); /* exits on errors */
1427 loglevel = get_loglevel_by_name(conf.loglevel_arg);
1428 version_handle_flag("gui", conf.version_given);
1429 if (conf.help_given || conf.detailed_help_given)
1430 print_help_and_die();
1431 parse_config_file_or_die(false /* override */);
1432 bot_win_rb = ringbuffer_new(RINGBUFFER_SIZE);
1433 setlocale(LC_CTYPE, "");
1434 initscr(); /* needed only once, always successful */
1436 return setup_tasks_and_schedule() < 0? EXIT_FAILURE : EXIT_SUCCESS;