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 struct gui_args_info conf;
54 CMDS_IDLE, /* no command running */
55 CMDS_DCMD, /* para or display command running */
56 CMDS_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;
138 static int find_cmd_byname(char *name)
142 for (i = 0; command_list[i].handler; i++)
143 if (!strcmp(command_list[i].name, name))
149 * Even though ncurses provides getmaxx and getmaxy, these functions/macros are
150 * not described in the XSI Curses standard.
152 static int get_num_lines(struct gui_window *w)
155 __a_unused int cols; /* avoid "set but not used" warnings */
157 getmaxyx(w->win, lines, cols);
161 static int get_num_cols(struct gui_window *w)
163 __a_unused int lines; /* avoid "set but not used" warnings */
166 getmaxyx(w->win, lines, cols);
170 /** Number of lines of the window are occupied by an output line. */
171 #define NUM_LINES(len) (1 + (len) / get_num_cols(&bot))
173 /* isendwin() returns false before initscr() was called */
174 static bool curses_active(void)
176 return top.win && !isendwin();
179 /* taken from mutt */
180 static char *km_keyname(int c)
185 sprintf(buf, "<up>");
189 sprintf(buf, "<down>");
193 sprintf(buf, "<left>");
196 if (c == KEY_RIGHT) {
197 sprintf(buf, "<right>");
200 if (c == KEY_NPAGE) {
201 sprintf(buf, "<npage>");
204 if (c == KEY_PPAGE) {
205 sprintf(buf, "<ppage>");
209 sprintf(buf, "<home>");
213 sprintf(buf, "<end>");
216 if (c < 256 && c > -128 && iscntrl((unsigned char) c)) {
221 buf[1] = (c + '@') & 0x7f;
224 snprintf(buf, sizeof(buf), "\\%d%d%d", c >> 6,
225 (c >> 3) & 7, c & 7);
226 } else if (c >= KEY_F0 && c < KEY_F(256))
227 sprintf(buf, "<F%d>", c - KEY_F0);
229 snprintf(buf, sizeof(buf), "%c", (unsigned char) c);
231 snprintf(buf, sizeof(buf), "\\x%hx", (unsigned short) c);
235 /* Print given number of spaces to curses window. */
236 static void add_spaces(WINDOW* win, unsigned int num)
239 unsigned sz = sizeof(space) - 1; /* number of spaces */
253 * print aligned string to curses window. This function always prints
256 static int align_str(WINDOW* win, char *str, unsigned int len,
259 int ret, i, num; /* of spaces */
264 ret = strwidth(str, &width);
266 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
275 /* replace control characters by spaces */
276 for (i = 0; i < len && str[i]; i++) {
277 if (str[i] == '\n' || str[i] == '\r' || str[i] == '\f')
282 add_spaces(win, num);
283 } else if (align == RIGHT) {
284 add_spaces(win, num);
287 add_spaces(win, num / 2);
288 waddstr(win, str[0]? str: "");
289 add_spaces(win, num - num / 2);
294 __printf_2_3 static void print_in_bar(int color, const char *fmt,...)
299 if (!curses_active())
301 wattron(in.win, COLOR_PAIR(color));
303 xvasprintf(&msg, fmt, ap);
306 align_str(in.win, msg, get_num_cols(&in), LEFT);
311 static void print_status_bar(void)
315 tmp = para_strdup("para_gui " PACKAGE_VERSION " (hit ? for help)");
317 align_str(sb.win, tmp, get_num_cols(&sb), CENTER);
322 * get the number of the oldest rbe that is (partially) visible. On return,
323 * lines contains the sum of the number of lines of all visible entries. If the
324 * first one is only partially visible, lines is greater than bot.lines.
326 static int first_visible_rbe(unsigned *lines)
328 int i, bot_lines = get_num_lines(&bot);
331 for (i = scroll_position; i < RINGBUFFER_SIZE; i++) {
332 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
336 rbe_lines = NUM_LINES(rbe->len);
337 if (rbe_lines > bot_lines)
340 if (*lines >= bot_lines)
343 return RINGBUFFER_SIZE - 1;
347 returns number of first visible rbe, *lines is the number of lines drawn.
349 static int draw_top_rbe(unsigned *lines)
351 int bot_cols, bot_lines, ret, fvr = first_visible_rbe(lines);
352 struct rb_entry *rbe;
353 size_t bytes_to_skip, cells_to_skip, width;
357 wmove(bot.win, 0, 0);
358 rbe = ringbuffer_get(bot_win_rb, fvr);
361 getmaxyx(bot.win, bot_lines, bot_cols);
362 if (*lines > bot_lines) {
363 /* rbe is partially visible multi-line */
364 cells_to_skip = (*lines - bot_lines) * bot_cols;
365 ret = skip_cells(rbe->msg, cells_to_skip, &bytes_to_skip);
368 ret = strwidth(rbe->msg + bytes_to_skip, &width);
375 wattron(bot.win, COLOR_PAIR(rbe->color));
376 waddstr(bot.win, rbe->msg + bytes_to_skip);
377 *lines = NUM_LINES(width);
381 static void redraw_bot_win(void)
384 int i, bot_lines = get_num_lines(&bot);
386 wmove(bot.win, 0, 0);
388 i = draw_top_rbe(&lines);
391 while (i > 0 && lines < bot_lines) {
392 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, --i);
395 waddstr(bot.win, "\n");
398 lines += NUM_LINES(rbe->len);
399 wattron(bot.win, COLOR_PAIR(rbe->color));
400 waddstr(bot.win, "\n");
401 waddstr(bot.win, rbe->msg);
407 static void rb_add_entry(int color, char *msg)
409 struct rb_entry *old, *new;
413 if (strwidth(msg, &len) < 0)
415 new = para_malloc(sizeof(struct rb_entry));
419 old = ringbuffer_add(bot_win_rb, new);
424 if (scroll_position) {
425 /* discard current scrolling, like xterm does */
430 wattron(bot.win, COLOR_PAIR(color));
431 getyx(bot.win, y, x);
433 waddstr(bot.win, "\n");
434 waddstr(bot.win, msg);
438 * print formated output to bot win and refresh
440 __printf_2_3 static void outputf(int color, const char* fmt,...)
445 if (!curses_active())
448 xvasprintf(&msg, fmt, ap);
450 rb_add_entry(color, msg);
454 static int add_output_line(char *line, void *data)
456 int color = *(int *)data? COLOR_ERRMSG : COLOR_OUTPUT;
458 if (!curses_active())
460 rb_add_entry(color, para_strdup(line));
464 static __printf_2_3 void curses_log(int ll, const char *fmt,...)
471 if (curses_active()) {
472 int color = ll <= LL_NOTICE? COLOR_MSG : COLOR_ERRMSG;
474 unsigned bytes = xvasprintf(&msg, fmt, ap);
475 if (bytes > 0 && msg[bytes - 1] == '\n')
476 msg[bytes - 1] = '\0'; /* cut trailing newline */
477 rb_add_entry(color, msg);
479 } else if (cmd_pid <= 0) /* no external command running */
480 vfprintf(stderr, fmt, ap);
483 __printf_2_3 void (*para_log)(int, const char*, ...) = curses_log;
485 static void shutdown_curses(void)
491 /* disable curses, print a message, kill running processes and exit */
492 __noreturn __printf_2_3 static void die(int exit_code, const char* fmt, ...)
498 vfprintf(stderr, fmt, argp);
500 /* kill every process in the process group and exit */
501 para_sigaction(SIGTERM, SIG_IGN);
507 * Print stat item #i to curses window
509 static void print_stat_item(int i)
512 struct stat_item_data d = theme.data[i];
513 char *c = stat_content[i];
514 int top_lines = get_num_lines(&top);
516 if (!curses_active() || !d.len || !c)
518 tmp = make_message("%s%s%s", d.prefix, c, d.postfix);
519 wmove(top.win, d.y * top_lines / 100, d.x * COLS / 100);
521 wattron(top.win, COLOR_PAIR(i + 1));
522 align_str(top.win, tmp, d.len * COLS / 100, d.align);
527 static int update_item(int item_num, char *buf)
529 char **c = stat_content + item_num;
536 *c = para_strdup("(artist tag not set)");
539 *c = para_strdup("(title tag not set)");
542 *c = para_strdup("????");
545 *c = para_strdup("(album tag not set)");
548 *c = para_strdup("(comment tag not set)");
552 *c = para_strdup(buf);
554 print_stat_item(item_num);
558 static void print_all_items(void)
562 if (!curses_active())
564 FOR_EACH_STATUS_ITEM(i)
568 static void clear_all_items(void)
572 FOR_EACH_STATUS_ITEM(i) {
573 free(stat_content[i]);
574 stat_content[i] = para_strdup("");
578 static void status_pre_select(struct sched *s, struct task *t)
580 struct status_task *st = container_of(t, struct status_task, task);
583 return para_fd_set(st->fd, &s->rfds, &s->max_fileno);
584 sched_request_barrier_or_min_delay(&st->next_exec, s);
587 static int status_post_select(struct sched *s, struct task *t)
589 struct status_task *st = container_of(t, struct status_task, task);
595 int fds[3] = {0, 1, 0};
596 /* Avoid busy loop */
597 if (tv_diff(&st->next_exec, now, NULL) > 0)
599 st->next_exec.tv_sec = now->tv_sec + 2;
600 ret = para_exec_cmdline_pid(&pid, conf.stat_cmd_arg, fds);
603 ret = mark_fd_nonblocking(fds[1]);
612 if (st->loaded >= st->bufsize) {
613 if (st->bufsize > 1000 * 1000) {
617 st->bufsize += st->bufsize + 1000;
618 st->buf = para_realloc(st->buf, st->bufsize);
620 assert(st->loaded < st->bufsize);
621 ret = read_nonblock(st->fd, st->buf + st->loaded,
622 st->bufsize - st->loaded, &s->rfds, &sz);
624 ret2 = for_each_stat_item(st->buf, st->loaded, update_item);
625 if (ret < 0 || ret2 < 0) {
627 PARA_NOTICE_LOG("closing stat pipe: %s\n", para_strerror(-ret));
631 free(stat_content[SI_BASENAME]);
632 stat_content[SI_BASENAME] =
633 para_strdup("stat command terminated!?");
637 sz = ret2; /* what is left */
638 if (sz > 0 && sz < st->loaded)
639 memmove(st->buf, st->buf + st->loaded - sz, sz);
647 static void init_wins(int top_lines)
649 int top_y = 0, bot_y = top_lines + 1, sb_y = LINES - 2,
650 in_y = LINES - 1, sep_y = top_lines;
651 int bot_lines = LINES - top_lines - 3, sb_lines = 1, in_lines = 1,
654 assume_default_colors(theme.default_fg, theme.default_bg);
656 wresize(top.win, top_lines, COLS);
657 mvwin(top.win, top_y, 0);
659 wresize(sb.win, sb_lines, COLS);
660 mvwin(sb.win, sb_y, 0);
662 wresize(sep.win, sep_lines, COLS);
663 mvwin(sep.win, sep_y, 0);
665 wresize(bot.win, bot_lines, COLS);
666 mvwin(bot.win, bot_y, 0);
668 wresize(in.win, in_lines, COLS);
669 mvwin(in.win, in_y, 0);
671 sep.win = newwin(sep_lines, COLS, sep_y, 0);
672 top.win = newwin(top_lines, COLS, top_y, 0);
673 bot.win = newwin(bot_lines, COLS, bot_y, 0);
674 sb.win = newwin(sb_lines, COLS, sb_y, 0);
675 in.win = newwin(in_lines, COLS, in_y, 0);
676 if (!top.win || !bot.win || !sb.win || !in.win || !sep.win)
677 die(EXIT_FAILURE, "Error: Cannot create curses windows\n");
681 scrollok(bot.win, 1);
682 wattron(sb.win, COLOR_PAIR(COLOR_STATUSBAR));
683 wattron(sep.win, COLOR_PAIR(COLOR_SEPARATOR));
684 wattron(bot.win, COLOR_PAIR(COLOR_BOT));
685 wattron(top.win, COLOR_PAIR(COLOR_TOP));
696 wmove(sep.win, 0, 0);
697 whline(sep.win, theme.sep_char, COLS);
701 wnoutrefresh(top.win);
702 wnoutrefresh(bot.win);
704 wnoutrefresh(sb.win);
705 wnoutrefresh(in.win);
706 wnoutrefresh(sep.win);
710 static void init_pair_or_die(short pair, short f, short b)
712 if (init_pair(pair, f, b) == ERR)
713 die(EXIT_FAILURE, "fatal: init_pair() failed\n");
716 static void init_colors_or_die(void)
721 die(EXIT_FAILURE, "fatal: No color term\n");
722 if (start_color() == ERR)
723 die(EXIT_FAILURE, "fatal: failed to start colors\n");
724 FOR_EACH_STATUS_ITEM(i)
725 if (theme.data[i].len)
726 init_pair_or_die(i + 1, theme.data[i].fg,
728 init_pair_or_die(COLOR_STATUSBAR, theme.sb_fg, theme.sb_bg);
729 init_pair_or_die(COLOR_COMMAND, theme.cmd_fg, theme.cmd_bg);
730 init_pair_or_die(COLOR_OUTPUT, theme.output_fg, theme.output_bg);
731 init_pair_or_die(COLOR_MSG, theme.msg_fg, theme.msg_bg);
732 init_pair_or_die(COLOR_ERRMSG, theme.err_msg_fg, theme.err_msg_bg);
733 init_pair_or_die(COLOR_SEPARATOR, theme.sep_fg, theme.sep_bg);
734 init_pair_or_die(COLOR_TOP, theme.default_fg, theme.default_bg);
735 init_pair_or_die(COLOR_BOT, theme.default_fg, theme.default_bg);
738 /* (Re-)initialize the curses library. */
739 static void init_curses(void)
743 if (top.win && refresh() == ERR) /* refresh is really needed */
744 die(EXIT_FAILURE, "refresh() failed\n");
745 if (LINES < theme.lines_min || COLS < theme.cols_min)
746 die(EXIT_FAILURE, "Terminal (%dx%d) too small"
747 " (need at least %dx%d)\n", COLS, LINES,
748 theme.cols_min, theme.lines_min);
749 curs_set(0); /* make cursor invisible, ignore errors */
750 nonl(); /* do not NL->CR/NL on output, always returns OK */
751 /* don't echo input */
753 die(EXIT_FAILURE, "fatal: noecho() failed\n");
754 /* take input chars one at a time, no wait for \n */
756 die(EXIT_FAILURE, "fatal: cbreak() failed\n");
757 init_colors_or_die();
758 clear(); /* ignore non-fatal errors */
759 init_wins(theme.top_lines_default);
760 // noecho(); /* don't echo input */
763 static void check_sigchld(void)
769 ret = para_reap_child(&pid);
772 if (pid == cmd_pid) {
775 print_in_bar(COLOR_MSG, " ");
777 goto reap_next_child;
781 * This sucker modifies its first argument. *handler and *arg are
782 * pointers to 0-terminated strings (inside line). Crap.
784 static int split_key_map(char *line, char **handler, char **arg)
786 if (!(*handler = strchr(line + 1, ':')))
790 if (!(*arg = strchr(*handler, ':')))
799 static void check_key_map_args_or_die(void)
804 for (i = 0; i < conf.key_map_given; ++i) {
808 tmp = para_strdup(conf.key_map_arg[i]);
809 if (!split_key_map(tmp, &handler, &arg))
811 if (strlen(handler) != 1)
813 if (*handler != 'x' && *handler != 'd' && *handler != 'i'
818 if (find_cmd_byname(arg) < 0)
821 if (i != conf.key_map_given)
822 die(EXIT_FAILURE, "invalid key map: %s\n", conf.key_map_arg[i]);
826 static void parse_config_file_or_die(bool override)
830 struct gui_cmdline_parser_params params = {
831 .override = override,
833 .check_required = !override,
834 .check_ambiguity = 0,
838 if (conf.config_file_given)
839 config_file = para_strdup(conf.config_file_arg);
841 char *home = para_homedir();
842 config_file = make_message("%s/.paraslash/gui.conf", home);
845 if (!file_exists(config_file)) {
846 if (!conf.config_file_given)
849 PARA_EMERG_LOG("config file %s does not exist\n",
855 gui_cmdline_parser_config_file(config_file, &conf, ¶ms);
856 loglevel = get_loglevel_by_name(conf.loglevel_arg);
857 check_key_map_args_or_die();
858 theme_init(conf.theme_arg, &theme);
866 /* reread configuration, terminate on errors */
867 static void reread_conf(void)
870 * gengetopt might print to stderr and exit on errors. So we have to
871 * shutdown curses first.
874 parse_config_file_or_die(true /* override */);
876 print_in_bar(COLOR_MSG, "config file reloaded\n");
880 * React to various signal-related events
882 static int signal_post_select(struct sched *s, __a_unused struct task *t)
884 int ret = para_next_signal(&s->rfds);
890 die(EXIT_FAILURE, "only the good die young (caught SIGTERM)\n");
893 if (curses_active()) {
900 PARA_WARNING_LOG("caught SIGINT, reset\n");
901 /* Nothing to do. SIGINT killed our child which gets noticed
902 * by do_select and resets everything.
906 PARA_NOTICE_LOG("got SIGUSR1, rereading configuration\n");
916 #define COMMAND_BUF_SIZE 32768
918 static enum cmd_status cmd_status(void)
920 if (command_fds[0] >= 0 || command_fds[1] >= 0)
927 static void command_pre_select(struct sched *s, __a_unused struct task *t)
929 enum cmd_status cmds = cmd_status();
931 if (cmds != CMDS_DCMD)
933 if (command_fds[0] >= 0)
934 para_fd_set(command_fds[0], &s->rfds, &s->max_fileno);
935 if (command_fds[1] >= 0)
936 para_fd_set(command_fds[1], &s->rfds, &s->max_fileno);
939 static int command_post_select(struct sched *s, __a_unused struct task *t)
942 static char command_buf[2][COMMAND_BUF_SIZE];
943 static int cbo[2]; /* command buf offsets */
944 static unsigned flags[2]; /* for for_each_line() */
945 enum cmd_status cmds = cmd_status();
947 if (cmds != CMDS_DCMD)
949 for (i = 0; i < 2; i++) {
951 if (command_fds[i] < 0)
953 ret = read_nonblock(command_fds[i],
954 command_buf[i] + cbo[i],
955 COMMAND_BUF_SIZE - 1 - cbo[i], &s->rfds, &sz);
958 cbo[i] = for_each_line(flags[i], command_buf[i], cbo[i],
959 add_output_line, &i);
960 if (sz != cbo[i]) { /* at least one line found */
964 if (ret < 0 || cmd_pid == 0) {
966 PARA_NOTICE_LOG("closing command fd %d: %s",
967 i, para_strerror(-ret));
968 close(command_fds[i]);
972 if (command_fds[!i] < 0) /* both fds closed */
975 if (cbo[i] == COMMAND_BUF_SIZE - 1) {
976 PARA_NOTICE_LOG("discarding overlong line");
978 flags[i] = FELF_DISCARD_FIRST;
984 static void input_pre_select(struct sched *s, __a_unused struct task *t)
986 enum cmd_status cmds = cmd_status();
988 if (cmds != CMDS_XCMD)
989 para_fd_set(STDIN_FILENO, &s->rfds, &s->max_fileno);
992 /* read from command pipe and print data to bot window */
993 static void exec_and_display_cmd(const char *cmd)
995 int ret, fds[3] = {0, 1, 1};
997 outputf(COLOR_COMMAND, "%s", cmd);
998 ret = para_exec_cmdline_pid(&cmd_pid, cmd, fds);
1001 ret = mark_fd_nonblocking(fds[1]);
1004 ret = mark_fd_nonblocking(fds[2]);
1007 command_fds[0] = fds[1];
1008 command_fds[1] = fds[2];
1011 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
1012 close(command_fds[0]);
1013 close(command_fds[1]);
1016 static void para_cmd(char *cmd)
1020 print_in_bar(COLOR_MSG, "executing client command, hit any key to abort\n");
1021 c = make_message(BINDIR "/para_client -- %s", cmd);
1022 exec_and_display_cmd(c);
1026 static void display_cmd(char *cmd)
1028 print_in_bar(COLOR_MSG, "executing display command, hit any key to abort");
1029 exec_and_display_cmd(cmd);
1033 * shutdown curses and stat pipe before executing external commands
1035 static void external_cmd(char *cmd)
1037 int fds[3] = {-1, -1, -1};
1042 para_exec_cmdline_pid(&cmd_pid, cmd, fds);
1045 static void handle_command(int c)
1049 /* first check user-defined key bindings */
1050 for (i = 0; i < conf.key_map_given; ++i) {
1051 char *tmp, *handler, *arg;
1053 tmp = para_strdup(conf.key_map_arg[i]);
1054 if (!split_key_map(tmp, &handler, &arg)) {
1058 if (strcmp(tmp, km_keyname(c))) {
1062 if (*handler == 'd')
1064 else if (*handler == 'x')
1066 else if (*handler == 'p')
1068 else if (*handler == 'i') {
1069 int num = find_cmd_byname(arg);
1071 command_list[num].handler();
1076 /* not found, check internal key bindings */
1077 for (i = 0; command_list[i].handler; i++) {
1078 if (!strcmp(km_keyname(c), command_list[i].key)) {
1079 command_list[i].handler();
1083 print_in_bar(COLOR_ERRMSG, "key '%s' is not bound, press ? for help",
1087 static int input_post_select(__a_unused struct sched *s, __a_unused struct task *t)
1090 enum cmd_status cmds = cmd_status();
1092 if (cmds == CMDS_XCMD)
1094 ret = wgetch(top.win);
1095 if (ret == ERR || ret == KEY_RESIZE)
1097 if (cmds == CMDS_IDLE) {
1098 handle_command(ret);
1102 kill(cmd_pid, SIGTERM);
1106 static void signal_pre_select(struct sched *s, struct task *t)
1108 struct signal_task *st = container_of(t, struct signal_task, task);
1109 para_fd_set(st->fd, &s->rfds, &s->max_fileno);
1112 static void print_scroll_msg(void)
1114 unsigned lines_total, filled = ringbuffer_filled(bot_win_rb);
1115 int first_rbe = first_visible_rbe(&lines_total);
1117 print_in_bar(COLOR_MSG, "scrolled view: %d-%d/%d\n", filled - first_rbe,
1118 filled - scroll_position, ringbuffer_filled(bot_win_rb));
1121 static void com_scroll_top(void)
1123 int i = RINGBUFFER_SIZE - 1, bot_lines = get_num_lines(&bot);
1126 while (i > 0 && !ringbuffer_get(bot_win_rb, i))
1128 /* i is oldest entry */
1129 for (; lines < bot_lines && i >= 0; i--) {
1130 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
1133 lines += NUM_LINES(rbe->len);
1136 if (lines > 0 && scroll_position != i) {
1137 scroll_position = i;
1142 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1145 static void com_cancel_scroll(void)
1148 if (scroll_position == 0) {
1149 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1152 scroll_position = 0;
1156 static void com_page_down(void)
1159 int i = scroll_position, bot_lines = get_num_lines(&bot);
1161 while (lines < bot_lines && --i > 0) {
1162 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
1165 lines += NUM_LINES(rbe->len);
1168 scroll_position = i;
1173 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1176 static void com_page_up(void)
1179 int fvr = first_visible_rbe(&lines), bot_lines = get_num_lines(&bot);
1181 if (fvr < 0 || fvr + 1 >= ringbuffer_filled(bot_win_rb)) {
1182 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1185 scroll_position = fvr + 1;
1186 for (; scroll_position > 0; scroll_position--) {
1187 first_visible_rbe(&lines);
1188 if (lines == bot_lines)
1195 static void com_scroll_down(void)
1197 struct rb_entry *rbe;
1198 int rbe_lines, bot_lines = get_num_lines(&bot);
1200 if (!scroll_position) {
1201 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1205 rbe = ringbuffer_get(bot_win_rb, scroll_position);
1206 rbe_lines = NUM_LINES(rbe->len);
1207 wscrl(bot.win, rbe_lines);
1208 wmove(bot.win, bot_lines - rbe_lines, 0);
1209 wattron(bot.win, COLOR_PAIR(rbe->color));
1210 waddstr(bot.win, rbe->msg);
1215 static void com_scroll_up(void)
1217 struct rb_entry *rbe = NULL;
1219 int i, first_rbe, num_scroll;
1221 /* the entry that is going to vanish */
1222 rbe = ringbuffer_get(bot_win_rb, scroll_position);
1225 num_scroll = NUM_LINES(rbe->len);
1226 first_rbe = first_visible_rbe(&lines);
1227 if (first_rbe < 0 || (first_rbe == ringbuffer_filled(bot_win_rb) - 1))
1230 wscrl(bot.win, -num_scroll);
1231 i = draw_top_rbe(&lines);
1234 while (i > 0 && lines < num_scroll) {
1236 rbe = ringbuffer_get(bot_win_rb, --i);
1239 rbe_lines = NUM_LINES(rbe->len);
1241 wattron(bot.win, COLOR_PAIR(rbe->color));
1242 waddstr(bot.win, "\n");
1243 waddstr(bot.win, rbe->msg);
1252 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1255 static void com_ll_decr(void)
1257 if (loglevel <= LL_DEBUG) {
1258 print_in_bar(COLOR_ERRMSG,
1259 "loglevel already at maximal verbosity\n");
1263 print_in_bar(COLOR_MSG, "loglevel set to %d\n", loglevel);
1266 static void com_ll_incr(void)
1268 if (loglevel >= LL_EMERG) {
1269 print_in_bar(COLOR_ERRMSG,
1270 "loglevel already at minimal verbosity\n");
1274 print_in_bar(COLOR_MSG, "loglevel set to %d\n", loglevel);
1277 static void com_reread_conf(void)
1282 static void com_help(void)
1286 for (i = 0; i < conf.key_map_given; ++i) {
1287 char *handler, *arg, *tmp = para_strdup(conf.key_map_arg[i]);
1288 const char *handler_text = "???", *desc = NULL;
1290 if (!split_key_map(tmp, &handler, &arg)) {
1296 handler_text = "internal";
1297 desc = command_list[find_cmd_byname(arg)].description;
1299 case 'x': handler_text = "external"; break;
1300 case 'd': handler_text = "display "; break;
1301 case 'p': handler_text = "para "; break;
1303 outputf(COLOR_MSG, "%s\t%s\t%s%s\t%s", tmp, handler_text, arg,
1304 strlen(arg) < 8? "\t" : "",
1308 for (i = 0; command_list[i].handler; i++) {
1309 struct gui_command gc = command_list[i];
1311 outputf(COLOR_MSG, "%s\tinternal\t%s\t%s%s", gc.key, gc.name,
1312 strlen(gc.name) < 8? "\t" : "",
1315 print_in_bar(COLOR_MSG, "try \"para_gui -h\" or \"para_client help\" "
1319 static void com_shrink_top_win(void)
1321 int top_lines = get_num_lines(&top);
1323 if (top_lines <= theme.top_lines_min) {
1324 PARA_WARNING_LOG("can not decrease top window\n");
1327 init_wins(top_lines - 1);
1328 print_in_bar(COLOR_MSG, "%s", "decreased top window");
1331 static void com_enlarge_top_win(void)
1333 int top_lines = get_num_lines(&top), bot_lines = get_num_lines(&bot);
1335 if (bot_lines < 3) {
1336 PARA_WARNING_LOG("can not increase top window\n");
1339 init_wins(top_lines + 1);
1340 print_in_bar(COLOR_MSG, "increased top window");
1343 static void com_version(void)
1345 print_in_bar(COLOR_MSG, "%s", version_single_line("gui"));
1348 __noreturn static void com_quit(void)
1350 die(EXIT_SUCCESS, "%s", "");
1353 static void com_refresh(void)
1359 static void com_next_theme(void)
1365 static void com_prev_theme(void)
1371 __noreturn static void print_help_and_die(void)
1373 struct ggo_help h = DEFINE_GGO_HELP(gui);
1374 bool d = conf.detailed_help_given;
1376 ggo_print_help(&h, d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS);
1380 static int setup_tasks_and_schedule(void)
1382 struct sched sched = {
1383 .default_timeout = {
1384 .tv_sec = conf.timeout_arg / 1000,
1385 .tv_usec = (conf.timeout_arg % 1000) * 1000,
1388 struct cmd_task cmd_task = {
1391 .pre_select = command_pre_select,
1392 .post_select = command_post_select,
1395 struct status_task status_task = {
1398 .pre_select = status_pre_select,
1399 .post_select = status_post_select,
1403 struct input_task input_task = {
1406 .pre_select = input_pre_select,
1407 .post_select = input_post_select,
1410 struct signal_task signal_task = {
1413 .pre_select = signal_pre_select,
1414 .post_select = signal_post_select,
1417 signal_task.fd = para_signal_init();
1418 para_install_sighandler(SIGINT);
1419 para_install_sighandler(SIGTERM);
1420 para_install_sighandler(SIGCHLD);
1421 para_install_sighandler(SIGWINCH);
1422 para_install_sighandler(SIGUSR1);
1424 register_task(&sched, &cmd_task.task);
1425 register_task(&sched, &status_task.task);
1426 register_task(&sched, &input_task.task);
1427 register_task(&sched, &signal_task.task);
1428 return schedule(&sched);
1431 int main(int argc, char *argv[])
1433 gui_cmdline_parser(argc, argv, &conf); /* exits on errors */
1434 loglevel = get_loglevel_by_name(conf.loglevel_arg);
1435 version_handle_flag("gui", conf.version_given);
1436 if (conf.help_given || conf.detailed_help_given)
1437 print_help_and_die();
1438 parse_config_file_or_die(false /* override */);
1439 bot_win_rb = ringbuffer_new(RINGBUFFER_SIZE);
1440 setlocale(LC_CTYPE, "");
1441 initscr(); /* needed only once, always successful */
1443 return setup_tasks_and_schedule() < 0? EXIT_FAILURE : EXIT_SUCCESS;