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 {
36 } top, bot, sb, in, sep;
38 #define RINGBUFFER_SIZE 512
44 static struct ringbuffer *bot_win_rb;
46 static unsigned scroll_position;
48 static pid_t exec_pid;
50 static int exec_fds[2] = {-1, -1};
51 static struct gui_args_info conf;
55 EXEC_IDLE, /* no command running */
56 EXEC_DCMD, /* para or display command running */
57 EXEC_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}};
131 struct timeval next_exec;
135 #define COMMAND_BUF_SIZE 32768
139 char command_buf[2][COMMAND_BUF_SIZE]; /* stdout/stderr of command */
140 int cbo[2]; /* command buf offsets */
141 unsigned flags[2]; /* passed to for_each_line() */
144 static int find_cmd_byname(char *name)
148 for (i = 0; command_list[i].handler; i++)
149 if (!strcmp(command_list[i].name, name))
155 * Even though ncurses provides getmaxx and getmaxy, these functions/macros are
156 * not described in the XSI Curses standard.
158 static int get_num_lines(struct gui_window *w)
161 __a_unused int cols; /* avoid "set but not used" warnings */
163 getmaxyx(w->win, lines, cols);
167 static int get_num_cols(struct gui_window *w)
169 __a_unused int lines; /* avoid "set but not used" warnings */
172 getmaxyx(w->win, lines, cols);
176 /** Number of lines of the window are occupied by an output line. */
177 #define NUM_LINES(len) (1 + (len) / get_num_cols(&bot))
179 /* isendwin() returns false before initscr() was called */
180 static bool curses_active(void)
182 return top.win && !isendwin();
185 /* taken from mutt */
186 static char *km_keyname(int c)
191 sprintf(buf, "<up>");
195 sprintf(buf, "<down>");
199 sprintf(buf, "<left>");
202 if (c == KEY_RIGHT) {
203 sprintf(buf, "<right>");
206 if (c == KEY_NPAGE) {
207 sprintf(buf, "<npage>");
210 if (c == KEY_PPAGE) {
211 sprintf(buf, "<ppage>");
215 sprintf(buf, "<home>");
219 sprintf(buf, "<end>");
222 if (c < 256 && c > -128 && iscntrl((unsigned char) c)) {
227 buf[1] = (c + '@') & 0x7f;
230 snprintf(buf, sizeof(buf), "\\%d%d%d", c >> 6,
231 (c >> 3) & 7, c & 7);
232 } else if (c >= KEY_F0 && c < KEY_F(256))
233 sprintf(buf, "<F%d>", c - KEY_F0);
235 snprintf(buf, sizeof(buf), "%c", (unsigned char) c);
237 snprintf(buf, sizeof(buf), "\\x%hx", (unsigned short) c);
241 /* Print given number of spaces to curses window. */
242 static void add_spaces(WINDOW* win, unsigned int num)
245 unsigned sz = sizeof(space) - 1; /* number of spaces */
259 * print aligned string to curses window. This function always prints
262 static int align_str(WINDOW* win, char *str, unsigned int len,
265 int ret, i, num; /* of spaces */
270 ret = strwidth(str, &width);
272 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
281 /* replace control characters by spaces */
282 for (i = 0; i < len && str[i]; i++) {
283 if (str[i] == '\n' || str[i] == '\r' || str[i] == '\f')
288 add_spaces(win, num);
289 } else if (align == RIGHT) {
290 add_spaces(win, num);
293 add_spaces(win, num / 2);
294 waddstr(win, str[0]? str: "");
295 add_spaces(win, num - num / 2);
300 static void refresh_window(struct gui_window *gw)
302 gw->needs_update = true;
305 static bool window_update_needed(void)
307 return top.needs_update || bot.needs_update || sb.needs_update ||
308 in.needs_update || sep.needs_update;
311 __printf_2_3 static void print_in_bar(int color, const char *fmt,...)
316 if (!curses_active())
318 wattron(in.win, COLOR_PAIR(color));
320 xvasprintf(&msg, fmt, ap);
323 align_str(in.win, msg, get_num_cols(&in), LEFT);
328 static void print_status_bar(void)
332 tmp = para_strdup("para_gui " PACKAGE_VERSION " (hit ? for help)");
334 align_str(sb.win, tmp, get_num_cols(&sb), CENTER);
339 * get the number of the oldest rbe that is (partially) visible. On return,
340 * lines contains the sum of the number of lines of all visible entries. If the
341 * first one is only partially visible, lines is greater than bot.lines.
343 static int first_visible_rbe(unsigned *lines)
345 int i, bot_lines = get_num_lines(&bot);
348 for (i = scroll_position; i < RINGBUFFER_SIZE; i++) {
349 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
353 rbe_lines = NUM_LINES(rbe->len);
354 if (rbe_lines > bot_lines)
357 if (*lines >= bot_lines)
360 return RINGBUFFER_SIZE - 1;
364 returns number of first visible rbe, *lines is the number of lines drawn.
366 static int draw_top_rbe(unsigned *lines)
368 int bot_cols, bot_lines, ret, fvr = first_visible_rbe(lines);
369 struct rb_entry *rbe;
370 size_t bytes_to_skip, cells_to_skip, width;
374 wmove(bot.win, 0, 0);
375 rbe = ringbuffer_get(bot_win_rb, fvr);
378 getmaxyx(bot.win, bot_lines, bot_cols);
379 if (*lines > bot_lines) {
380 /* rbe is partially visible multi-line */
381 cells_to_skip = (*lines - bot_lines) * bot_cols;
382 ret = skip_cells(rbe->msg, cells_to_skip, &bytes_to_skip);
385 ret = strwidth(rbe->msg + bytes_to_skip, &width);
392 wattron(bot.win, COLOR_PAIR(rbe->color));
393 waddstr(bot.win, rbe->msg + bytes_to_skip);
394 *lines = NUM_LINES(width);
398 static void redraw_bot_win(void)
401 int i, bot_lines = get_num_lines(&bot);
403 wmove(bot.win, 0, 0);
405 i = draw_top_rbe(&lines);
408 while (i > 0 && lines < bot_lines) {
409 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, --i);
412 waddstr(bot.win, "\n");
415 lines += NUM_LINES(rbe->len);
416 wattron(bot.win, COLOR_PAIR(rbe->color));
417 waddstr(bot.win, "\n");
418 waddstr(bot.win, rbe->msg);
421 refresh_window(&bot);
424 static void rb_add_entry(int color, char *msg)
426 struct rb_entry *old, *new;
430 if (strwidth(msg, &len) < 0)
432 new = para_malloc(sizeof(struct rb_entry));
436 old = ringbuffer_add(bot_win_rb, new);
441 if (scroll_position) {
442 /* discard current scrolling, like xterm does */
447 wattron(bot.win, COLOR_PAIR(color));
448 getyx(bot.win, y, x);
450 waddstr(bot.win, "\n");
451 waddstr(bot.win, msg);
455 * print formated output to bot win and refresh
457 __printf_2_3 static void outputf(int color, const char* fmt,...)
462 if (!curses_active())
465 xvasprintf(&msg, fmt, ap);
467 rb_add_entry(color, msg);
468 refresh_window(&bot);
471 static int add_output_line(char *line, void *data)
473 int color = *(int *)data? COLOR_ERRMSG : COLOR_OUTPUT;
475 if (!curses_active())
477 rb_add_entry(color, para_strdup(line));
481 static __printf_2_3 void curses_log(int ll, const char *fmt,...)
488 if (curses_active()) {
489 int color = ll <= LL_NOTICE? COLOR_MSG : COLOR_ERRMSG;
491 unsigned bytes = xvasprintf(&msg, fmt, ap);
492 if (bytes > 0 && msg[bytes - 1] == '\n')
493 msg[bytes - 1] = '\0'; /* cut trailing newline */
494 rb_add_entry(color, msg);
495 refresh_window(&bot);
496 } else if (exec_pid <= 0) /* no external command running */
497 vfprintf(stderr, fmt, ap);
500 __printf_2_3 void (*para_log)(int, const char*, ...) = curses_log;
502 static void shutdown_curses(void)
508 /* disable curses, print a message, kill running processes and exit */
509 __noreturn __printf_2_3 static void die(int exit_code, const char* fmt, ...)
515 vfprintf(stderr, fmt, argp);
517 /* kill every process in the process group and exit */
518 para_sigaction(SIGTERM, SIG_IGN);
524 * Print stat item #i to curses window
526 static void print_stat_item(int i)
529 struct stat_item_data d = theme.data[i];
530 char *c = stat_content[i];
531 int top_lines = get_num_lines(&top);
533 if (!curses_active() || !d.len || !c)
535 tmp = make_message("%s%s%s", d.prefix, c, d.postfix);
536 wmove(top.win, d.y * top_lines / 100, d.x * COLS / 100);
537 wattron(top.win, COLOR_PAIR(i + 1));
538 align_str(top.win, tmp, d.len * COLS / 100, d.align);
540 refresh_window(&top);
543 static int update_item(int item_num, char *buf)
545 char **c = stat_content + item_num;
552 *c = para_strdup("(artist tag not set)");
555 *c = para_strdup("(title tag not set)");
558 *c = para_strdup("????");
561 *c = para_strdup("(album tag not set)");
564 *c = para_strdup("(comment tag not set)");
568 *c = para_strdup(buf);
570 print_stat_item(item_num);
574 static void print_all_items(void)
578 if (!curses_active())
580 FOR_EACH_STATUS_ITEM(i)
584 static void clear_all_items(void)
588 FOR_EACH_STATUS_ITEM(i) {
589 free(stat_content[i]);
590 stat_content[i] = para_strdup("");
594 static void status_pre_select(struct sched *s, struct task *t)
596 struct status_task *st = container_of(t, struct status_task, task);
599 return para_fd_set(st->fd, &s->rfds, &s->max_fileno);
600 sched_request_barrier_or_min_delay(&st->next_exec, s);
603 static int status_post_select(struct sched *s, struct task *t)
605 struct status_task *st = container_of(t, struct status_task, task);
611 int fds[3] = {0, 1, 0};
612 /* Avoid busy loop */
613 if (tv_diff(&st->next_exec, now, NULL) > 0)
615 st->next_exec.tv_sec = now->tv_sec + 2;
616 ret = para_exec_cmdline_pid(&pid, conf.stat_cmd_arg, fds);
619 ret = mark_fd_nonblocking(fds[1]);
628 if (st->loaded >= st->bufsize) {
629 if (st->bufsize > 1000 * 1000) {
633 st->bufsize += st->bufsize + 1000;
634 st->buf = para_realloc(st->buf, st->bufsize);
636 assert(st->loaded < st->bufsize);
637 ret = read_nonblock(st->fd, st->buf + st->loaded,
638 st->bufsize - st->loaded, &s->rfds, &sz);
640 ret2 = for_each_stat_item(st->buf, st->loaded, update_item);
641 if (ret < 0 || ret2 < 0) {
643 PARA_NOTICE_LOG("closing stat pipe: %s\n", para_strerror(-ret));
647 free(stat_content[SI_BASENAME]);
648 stat_content[SI_BASENAME] =
649 para_strdup("stat command terminated!?");
653 sz = ret2; /* what is left */
654 if (sz > 0 && sz < st->loaded)
655 memmove(st->buf, st->buf + st->loaded - sz, sz);
663 static void init_wins(int top_lines)
665 int top_y = 0, bot_y = top_lines + 1, sb_y = LINES - 2,
666 in_y = LINES - 1, sep_y = top_lines;
667 int bot_lines = LINES - top_lines - 3, sb_lines = 1, in_lines = 1,
670 assume_default_colors(theme.default_fg, theme.default_bg);
672 wresize(top.win, top_lines, COLS);
673 mvwin(top.win, top_y, 0);
675 wresize(sb.win, sb_lines, COLS);
676 mvwin(sb.win, sb_y, 0);
678 wresize(sep.win, sep_lines, COLS);
679 mvwin(sep.win, sep_y, 0);
681 wresize(bot.win, bot_lines, COLS);
682 mvwin(bot.win, bot_y, 0);
684 wresize(in.win, in_lines, COLS);
685 mvwin(in.win, in_y, 0);
687 sep.win = newwin(sep_lines, COLS, sep_y, 0);
688 top.win = newwin(top_lines, COLS, top_y, 0);
689 bot.win = newwin(bot_lines, COLS, bot_y, 0);
690 sb.win = newwin(sb_lines, COLS, sb_y, 0);
691 in.win = newwin(in_lines, COLS, in_y, 0);
692 if (!top.win || !bot.win || !sb.win || !in.win || !sep.win)
693 die(EXIT_FAILURE, "Error: Cannot create curses windows\n");
697 scrollok(bot.win, 1);
698 wattron(sb.win, COLOR_PAIR(COLOR_STATUSBAR));
699 wattron(sep.win, COLOR_PAIR(COLOR_SEPARATOR));
700 wattron(bot.win, COLOR_PAIR(COLOR_BOT));
701 wattron(top.win, COLOR_PAIR(COLOR_TOP));
712 wmove(sep.win, 0, 0);
713 whline(sep.win, theme.sep_char, COLS);
717 wnoutrefresh(top.win);
718 wnoutrefresh(bot.win);
720 wnoutrefresh(sb.win);
721 wnoutrefresh(in.win);
722 wnoutrefresh(sep.win);
726 static void init_pair_or_die(short pair, short f, short b)
728 if (init_pair(pair, f, b) == ERR)
729 die(EXIT_FAILURE, "fatal: init_pair() failed\n");
732 static void init_colors_or_die(void)
737 die(EXIT_FAILURE, "fatal: No color term\n");
738 if (start_color() == ERR)
739 die(EXIT_FAILURE, "fatal: failed to start colors\n");
740 FOR_EACH_STATUS_ITEM(i)
741 if (theme.data[i].len)
742 init_pair_or_die(i + 1, theme.data[i].fg,
744 init_pair_or_die(COLOR_STATUSBAR, theme.sb_fg, theme.sb_bg);
745 init_pair_or_die(COLOR_COMMAND, theme.cmd_fg, theme.cmd_bg);
746 init_pair_or_die(COLOR_OUTPUT, theme.output_fg, theme.output_bg);
747 init_pair_or_die(COLOR_MSG, theme.msg_fg, theme.msg_bg);
748 init_pair_or_die(COLOR_ERRMSG, theme.err_msg_fg, theme.err_msg_bg);
749 init_pair_or_die(COLOR_SEPARATOR, theme.sep_fg, theme.sep_bg);
750 init_pair_or_die(COLOR_TOP, theme.default_fg, theme.default_bg);
751 init_pair_or_die(COLOR_BOT, theme.default_fg, theme.default_bg);
754 /* (Re-)initialize the curses library. */
755 static void init_curses(void)
759 if (top.win && refresh() == ERR) /* refresh is really needed */
760 die(EXIT_FAILURE, "refresh() failed\n");
761 if (LINES < theme.lines_min || COLS < theme.cols_min)
762 die(EXIT_FAILURE, "Terminal (%dx%d) too small"
763 " (need at least %dx%d)\n", COLS, LINES,
764 theme.cols_min, theme.lines_min);
765 curs_set(0); /* make cursor invisible, ignore errors */
766 nonl(); /* do not NL->CR/NL on output, always returns OK */
767 /* don't echo input */
769 die(EXIT_FAILURE, "fatal: noecho() failed\n");
770 /* take input chars one at a time, no wait for \n */
772 die(EXIT_FAILURE, "fatal: cbreak() failed\n");
773 init_colors_or_die();
774 clear(); /* ignore non-fatal errors */
775 init_wins(theme.top_lines_default);
776 // noecho(); /* don't echo input */
779 static void check_sigchld(void)
785 ret = para_reap_child(&pid);
788 if (pid == exec_pid) {
791 print_in_bar(COLOR_MSG, " ");
793 goto reap_next_child;
797 * This sucker modifies its first argument. *handler and *arg are
798 * pointers to 0-terminated strings (inside line). Crap.
800 static int split_key_map(char *line, char **handler, char **arg)
802 if (!(*handler = strchr(line + 1, ':')))
806 if (!(*arg = strchr(*handler, ':')))
815 static void check_key_map_args_or_die(void)
820 for (i = 0; i < conf.key_map_given; ++i) {
824 tmp = para_strdup(conf.key_map_arg[i]);
825 if (!split_key_map(tmp, &handler, &arg))
827 if (strlen(handler) != 1)
829 if (*handler != 'x' && *handler != 'd' && *handler != 'i'
834 if (find_cmd_byname(arg) < 0)
837 if (i != conf.key_map_given)
838 die(EXIT_FAILURE, "invalid key map: %s\n", conf.key_map_arg[i]);
842 static void parse_config_file_or_die(bool override)
846 struct gui_cmdline_parser_params params = {
847 .override = override,
849 .check_required = !override,
850 .check_ambiguity = 0,
854 if (conf.config_file_given)
855 config_file = para_strdup(conf.config_file_arg);
857 char *home = para_homedir();
858 config_file = make_message("%s/.paraslash/gui.conf", home);
861 if (!file_exists(config_file)) {
862 if (!conf.config_file_given)
865 PARA_EMERG_LOG("config file %s does not exist\n",
871 gui_cmdline_parser_config_file(config_file, &conf, ¶ms);
872 loglevel = get_loglevel_by_name(conf.loglevel_arg);
873 check_key_map_args_or_die();
874 theme_init(conf.theme_arg, &theme);
882 /* reread configuration, terminate on errors */
883 static void reread_conf(void)
886 * gengetopt might print to stderr and exit on errors. So we have to
887 * shutdown curses first.
890 parse_config_file_or_die(true /* override */);
892 print_in_bar(COLOR_MSG, "config file reloaded\n");
896 * React to various signal-related events
898 static int signal_post_select(struct sched *s, __a_unused struct task *t)
900 int ret = para_next_signal(&s->rfds);
906 die(EXIT_FAILURE, "only the good die young (caught SIGTERM)\n");
909 if (curses_active()) {
916 PARA_WARNING_LOG("caught SIGINT, reset\n");
917 /* Nothing to do. SIGINT killed our child which gets noticed
918 * by do_select and resets everything.
922 PARA_NOTICE_LOG("got SIGUSR1, rereading configuration\n");
932 static enum exec_status exec_status(void)
934 if (exec_fds[0] >= 0 || exec_fds[1] >= 0)
941 static void exec_pre_select(struct sched *s, __a_unused struct task *t)
943 enum exec_status es = exec_status();
947 if (exec_fds[0] >= 0)
948 para_fd_set(exec_fds[0], &s->rfds, &s->max_fileno);
949 if (exec_fds[1] >= 0)
950 para_fd_set(exec_fds[1], &s->rfds, &s->max_fileno);
953 static int exec_post_select(struct sched *s, struct task *t)
955 struct exec_task *ct = container_of(t, struct exec_task, task);
957 enum exec_status es = exec_status();
961 for (i = 0; i < 2; i++) {
965 ret = read_nonblock(exec_fds[i],
966 ct->command_buf[i] + ct->cbo[i],
967 COMMAND_BUF_SIZE - 1 - ct->cbo[i], &s->rfds, &sz);
970 ct->cbo[i] = for_each_line(ct->flags[i], ct->command_buf[i],
971 ct->cbo[i], add_output_line, &i);
972 if (sz != ct->cbo[i]) { /* at least one line found */
973 refresh_window(&bot);
976 if (ret < 0 || exec_pid == 0) {
978 PARA_NOTICE_LOG("closing command fd %d: %s",
979 i, para_strerror(-ret));
984 if (exec_fds[!i] < 0) /* both fds closed */
987 if (ct->cbo[i] == COMMAND_BUF_SIZE - 1) {
988 PARA_NOTICE_LOG("discarding overlong line");
990 ct->flags[i] = FELF_DISCARD_FIRST;
996 static void input_pre_select(struct sched *s, __a_unused struct task *t)
998 if (exec_status() != EXEC_XCMD)
999 para_fd_set(STDIN_FILENO, &s->rfds, &s->max_fileno);
1000 if (window_update_needed())
1004 /* read from command pipe and print data to bot window */
1005 static void exec_and_display(const char *file_and_args)
1007 int ret, fds[3] = {0, 1, 1};
1009 outputf(COLOR_COMMAND, "%s", file_and_args);
1010 ret = para_exec_cmdline_pid(&exec_pid, file_and_args, fds);
1013 ret = mark_fd_nonblocking(fds[1]);
1016 ret = mark_fd_nonblocking(fds[2]);
1019 exec_fds[0] = fds[1];
1020 exec_fds[1] = fds[2];
1021 print_in_bar(COLOR_MSG, "hit any key to abort\n");
1024 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
1029 static void exec_para(const char *args)
1031 char *file_and_args;
1033 file_and_args = make_message(BINDIR "/para_client -- %s", args);
1034 exec_and_display(file_and_args);
1035 free(file_and_args);
1039 * shutdown curses and stat pipe before executing external commands
1041 static void exec_external(char *file_and_args)
1043 int fds[3] = {-1, -1, -1};
1048 para_exec_cmdline_pid(&exec_pid, file_and_args, fds);
1051 static void handle_command(int c)
1055 /* first check user-defined key bindings */
1056 for (i = 0; i < conf.key_map_given; ++i) {
1057 char *tmp, *handler, *arg;
1059 tmp = para_strdup(conf.key_map_arg[i]);
1060 if (!split_key_map(tmp, &handler, &arg)) {
1064 if (strcmp(tmp, km_keyname(c))) {
1068 if (*handler == 'd')
1069 exec_and_display(arg);
1070 else if (*handler == 'x')
1072 else if (*handler == 'p')
1074 else if (*handler == 'i') {
1075 int num = find_cmd_byname(arg);
1077 command_list[num].handler();
1082 /* not found, check internal key bindings */
1083 for (i = 0; command_list[i].handler; i++) {
1084 if (!strcmp(km_keyname(c), command_list[i].key)) {
1085 command_list[i].handler();
1089 print_in_bar(COLOR_ERRMSG, "key '%s' is not bound, press ? for help",
1093 static int input_post_select(__a_unused struct sched *s, __a_unused struct task *t)
1096 enum exec_status exs = exec_status();
1098 if (exs == EXEC_XCMD)
1100 if (window_update_needed()) {
1101 if (top.needs_update)
1102 assert(wnoutrefresh(top.win) == OK);
1103 if (bot.needs_update)
1104 assert(wnoutrefresh(bot.win) == OK);
1105 if (sep.needs_update)
1106 assert(wnoutrefresh(sep.win) == OK);
1107 if (sb.needs_update)
1108 assert(wnoutrefresh(sb.win) == OK);
1109 if (in.needs_update)
1110 assert(wnoutrefresh(in.win) == OK);
1112 top.needs_update = bot.needs_update = sb.needs_update =
1113 in.needs_update = sep.needs_update = false;
1115 ret = wgetch(top.win);
1116 if (ret == ERR || ret == KEY_RESIZE)
1118 if (exs == EXEC_IDLE) {
1119 handle_command(ret);
1123 kill(exec_pid, SIGTERM);
1127 static void signal_pre_select(struct sched *s, struct task *t)
1129 struct signal_task *st = container_of(t, struct signal_task, task);
1130 para_fd_set(st->fd, &s->rfds, &s->max_fileno);
1133 static void print_scroll_msg(void)
1135 unsigned lines_total, filled = ringbuffer_filled(bot_win_rb);
1136 int first_rbe = first_visible_rbe(&lines_total);
1138 print_in_bar(COLOR_MSG, "scrolled view: %d-%d/%d\n", filled - first_rbe,
1139 filled - scroll_position, ringbuffer_filled(bot_win_rb));
1142 static void com_scroll_top(void)
1144 int i = RINGBUFFER_SIZE - 1, bot_lines = get_num_lines(&bot);
1147 while (i > 0 && !ringbuffer_get(bot_win_rb, i))
1149 /* i is oldest entry */
1150 for (; lines < bot_lines && i >= 0; i--) {
1151 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
1154 lines += NUM_LINES(rbe->len);
1157 if (lines > 0 && scroll_position != i) {
1158 scroll_position = i;
1163 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1166 static void com_cancel_scroll(void)
1169 if (scroll_position == 0) {
1170 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1173 scroll_position = 0;
1177 static void com_page_down(void)
1180 int i = scroll_position, bot_lines = get_num_lines(&bot);
1182 while (lines < bot_lines && --i > 0) {
1183 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
1186 lines += NUM_LINES(rbe->len);
1189 scroll_position = i;
1194 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1197 static void com_page_up(void)
1200 int fvr = first_visible_rbe(&lines), bot_lines = get_num_lines(&bot);
1202 if (fvr < 0 || fvr + 1 >= ringbuffer_filled(bot_win_rb)) {
1203 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1206 scroll_position = fvr + 1;
1207 for (; scroll_position > 0; scroll_position--) {
1208 first_visible_rbe(&lines);
1209 if (lines == bot_lines)
1216 static void com_scroll_down(void)
1218 struct rb_entry *rbe;
1219 int rbe_lines, bot_lines = get_num_lines(&bot);
1221 if (!scroll_position) {
1222 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1226 rbe = ringbuffer_get(bot_win_rb, scroll_position);
1227 rbe_lines = NUM_LINES(rbe->len);
1228 wscrl(bot.win, rbe_lines);
1229 wmove(bot.win, bot_lines - rbe_lines, 0);
1230 wattron(bot.win, COLOR_PAIR(rbe->color));
1231 waddstr(bot.win, rbe->msg);
1232 refresh_window(&bot);
1236 static void com_scroll_up(void)
1238 struct rb_entry *rbe = NULL;
1240 int i, first_rbe, num_scroll;
1242 /* the entry that is going to vanish */
1243 rbe = ringbuffer_get(bot_win_rb, scroll_position);
1246 num_scroll = NUM_LINES(rbe->len);
1247 first_rbe = first_visible_rbe(&lines);
1248 if (first_rbe < 0 || (first_rbe == ringbuffer_filled(bot_win_rb) - 1))
1251 wscrl(bot.win, -num_scroll);
1252 i = draw_top_rbe(&lines);
1255 while (i > 0 && lines < num_scroll) {
1257 rbe = ringbuffer_get(bot_win_rb, --i);
1260 rbe_lines = NUM_LINES(rbe->len);
1262 wattron(bot.win, COLOR_PAIR(rbe->color));
1263 waddstr(bot.win, "\n");
1264 waddstr(bot.win, rbe->msg);
1269 refresh_window(&bot);
1273 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1276 static void com_ll_decr(void)
1278 if (loglevel <= LL_DEBUG) {
1279 print_in_bar(COLOR_ERRMSG,
1280 "loglevel already at maximal verbosity\n");
1284 print_in_bar(COLOR_MSG, "loglevel set to %d\n", loglevel);
1287 static void com_ll_incr(void)
1289 if (loglevel >= LL_EMERG) {
1290 print_in_bar(COLOR_ERRMSG,
1291 "loglevel already at minimal verbosity\n");
1295 print_in_bar(COLOR_MSG, "loglevel set to %d\n", loglevel);
1298 static void com_reread_conf(void)
1303 static void com_help(void)
1307 for (i = 0; i < conf.key_map_given; ++i) {
1308 char *handler, *arg, *tmp = para_strdup(conf.key_map_arg[i]);
1309 const char *handler_text = "???", *desc = NULL;
1311 if (!split_key_map(tmp, &handler, &arg)) {
1317 handler_text = "internal";
1318 desc = command_list[find_cmd_byname(arg)].description;
1320 case 'x': handler_text = "external"; break;
1321 case 'd': handler_text = "display "; break;
1322 case 'p': handler_text = "para "; break;
1324 outputf(COLOR_MSG, "%s\t%s\t%s%s\t%s", tmp, handler_text, arg,
1325 strlen(arg) < 8? "\t" : "",
1329 for (i = 0; command_list[i].handler; i++) {
1330 struct gui_command gc = command_list[i];
1332 outputf(COLOR_MSG, "%s\tinternal\t%s\t%s%s", gc.key, gc.name,
1333 strlen(gc.name) < 8? "\t" : "",
1336 print_in_bar(COLOR_MSG, "try \"para_gui -h\" or \"para_client help\" "
1340 static void com_shrink_top_win(void)
1342 int top_lines = get_num_lines(&top);
1344 if (top_lines <= theme.top_lines_min) {
1345 PARA_WARNING_LOG("can not decrease top window\n");
1348 init_wins(top_lines - 1);
1349 print_in_bar(COLOR_MSG, "%s", "decreased top window");
1352 static void com_enlarge_top_win(void)
1354 int top_lines = get_num_lines(&top), bot_lines = get_num_lines(&bot);
1356 if (bot_lines < 3) {
1357 PARA_WARNING_LOG("can not increase top window\n");
1360 init_wins(top_lines + 1);
1361 print_in_bar(COLOR_MSG, "increased top window");
1364 static void com_version(void)
1366 print_in_bar(COLOR_MSG, "%s", version_single_line("gui"));
1369 __noreturn static void com_quit(void)
1371 die(EXIT_SUCCESS, "%s", "");
1374 static void com_refresh(void)
1380 static void com_next_theme(void)
1386 static void com_prev_theme(void)
1392 __noreturn static void print_help_and_die(void)
1394 struct ggo_help h = DEFINE_GGO_HELP(gui);
1395 bool d = conf.detailed_help_given;
1397 ggo_print_help(&h, d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS);
1401 static int setup_tasks_and_schedule(void)
1403 struct sched sched = {
1404 .default_timeout = {
1405 .tv_sec = conf.timeout_arg / 1000,
1406 .tv_usec = (conf.timeout_arg % 1000) * 1000,
1409 struct exec_task exec_task = {
1412 .pre_select = exec_pre_select,
1413 .post_select = exec_post_select,
1416 struct status_task status_task = {
1419 .pre_select = status_pre_select,
1420 .post_select = status_post_select,
1424 struct input_task input_task = {
1427 .pre_select = input_pre_select,
1428 .post_select = input_post_select,
1431 struct signal_task signal_task = {
1434 .pre_select = signal_pre_select,
1435 .post_select = signal_post_select,
1438 signal_task.fd = para_signal_init();
1439 para_install_sighandler(SIGINT);
1440 para_install_sighandler(SIGTERM);
1441 para_install_sighandler(SIGCHLD);
1442 para_install_sighandler(SIGWINCH);
1443 para_install_sighandler(SIGUSR1);
1445 register_task(&sched, &exec_task.task);
1446 register_task(&sched, &status_task.task);
1447 register_task(&sched, &input_task.task);
1448 register_task(&sched, &signal_task.task);
1449 return schedule(&sched);
1452 int main(int argc, char *argv[])
1454 gui_cmdline_parser(argc, argv, &conf); /* exits on errors */
1455 loglevel = get_loglevel_by_name(conf.loglevel_arg);
1456 version_handle_flag("gui", conf.version_given);
1457 if (conf.help_given || conf.detailed_help_given)
1458 print_help_and_die();
1459 parse_config_file_or_die(false /* override */);
1460 bot_win_rb = ringbuffer_new(RINGBUFFER_SIZE);
1461 setlocale(LC_CTYPE, "");
1462 initscr(); /* needed only once, always successful */
1464 return setup_tasks_and_schedule() < 0? EXIT_FAILURE : EXIT_SUCCESS;