2 * Copyright (C) 1998 Andre Noll <maan@tuebingen.mpg.de>
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 /** How many lines of output to remember. */
39 #define RINGBUFFER_SIZE 512
46 static struct ringbuffer *bot_win_rb;
48 static unsigned scroll_position;
50 static pid_t exec_pid;
52 static int exec_fds[2] = {-1, -1};
53 static struct gui_args_info conf;
56 /** Type of the process currently being executed. */
58 EXEC_IDLE, /**< No process running. */
59 EXEC_DCMD, /**< para or display process running. */
60 EXEC_XCMD, /**< External process running. */
64 * Codes for various colors.
66 * Each status item has its own color pair. The ones defined here start at a
67 * higher number so that they do not overlap with these.
70 COLOR_STATUSBAR = NUM_STAT_ITEMS + 1,
83 const char *description;
84 void (*handler)(void);
87 static struct gui_theme theme;
89 #define GUI_COMMANDS \
90 GUI_COMMAND(help, "?", "print help") \
91 GUI_COMMAND(enlarge_top_win, "+", "enlarge the top window") \
92 GUI_COMMAND(shrink_top_win, "-", "shrink the top window") \
93 GUI_COMMAND(reread_conf, "r", "reread configuration file") \
94 GUI_COMMAND(quit, "q", "exit para_gui") \
95 GUI_COMMAND(refresh, "^L", "redraw the screen") \
96 GUI_COMMAND(next_theme, ".", "switch to next theme") \
97 GUI_COMMAND(prev_theme, ",", "switch to previous theme") \
98 GUI_COMMAND(ll_incr, ">", "increase loglevel (decreases verbosity)") \
99 GUI_COMMAND(ll_decr, "<", "decrease loglevel (increases verbosity)") \
100 GUI_COMMAND(version, "V", "show the para_gui version") \
101 GUI_COMMAND(scroll_up, "<up>", "scroll up one line") \
102 GUI_COMMAND(scroll_down, "<down>", "scroll_down") \
103 GUI_COMMAND(page_up, "<ppage>", "scroll up one page") \
104 GUI_COMMAND(page_down, "<npage>", "scroll down one page") \
105 GUI_COMMAND(scroll_top, "<home>", "scroll to top of buffer") \
106 GUI_COMMAND(cancel_scroll, "<end>", "deactivate scroll mode") \
108 /* declare command handlers */
109 #define GUI_COMMAND(_c, _k, _d) \
110 static void com_ ## _c(void);
115 /* define command array */
116 #define GUI_COMMAND(_c, _k, _d) \
121 .handler = com_ ## _c \
124 static struct gui_command command_list[] = {GUI_COMMANDS {.name = NULL}};
135 struct timeval next_exec;
139 /** Stdout/stderr of the executing process is read in chunks of this size. */
140 #define COMMAND_BUF_SIZE 32768
144 char command_buf[2][COMMAND_BUF_SIZE]; /* stdout/stderr of command */
145 int cbo[2]; /* command buf offsets */
146 unsigned flags[2]; /* passed to for_each_line() */
149 static int find_cmd_byname(char *name)
153 for (i = 0; command_list[i].handler; i++)
154 if (!strcmp(command_list[i].name, name))
160 * Even though ncurses provides getmaxx and getmaxy, these functions/macros are
161 * not described in the XSI Curses standard.
163 static int get_num_lines(struct gui_window *w)
166 __a_unused int cols; /* avoid "set but not used" warnings */
168 getmaxyx(w->win, lines, cols);
172 static int get_num_cols(struct gui_window *w)
174 __a_unused int lines; /* avoid "set but not used" warnings */
177 getmaxyx(w->win, lines, cols);
181 /** Number of lines of the window are occupied by an output line. */
182 #define NUM_LINES(len) (1 + (len) / get_num_cols(&bot))
184 /* isendwin() returns false before initscr() was called */
185 static bool curses_active(void)
187 return top.win && !isendwin();
190 /* taken from mutt */
191 static char *km_keyname(int c)
196 sprintf(buf, "<up>");
200 sprintf(buf, "<down>");
204 sprintf(buf, "<left>");
207 if (c == KEY_RIGHT) {
208 sprintf(buf, "<right>");
211 if (c == KEY_NPAGE) {
212 sprintf(buf, "<npage>");
215 if (c == KEY_PPAGE) {
216 sprintf(buf, "<ppage>");
220 sprintf(buf, "<home>");
224 sprintf(buf, "<end>");
227 if (c < 256 && c > -128 && iscntrl((unsigned char) c)) {
232 buf[1] = (c + '@') & 0x7f;
235 snprintf(buf, sizeof(buf), "\\%d%d%d", c >> 6,
236 (c >> 3) & 7, c & 7);
237 } else if (c >= KEY_F0 && c < KEY_F(256))
238 sprintf(buf, "<F%d>", c - KEY_F0);
240 snprintf(buf, sizeof(buf), "%c", (unsigned char) c);
242 snprintf(buf, sizeof(buf), "\\x%hx", (unsigned short) c);
246 /* Print given number of spaces to curses window. */
247 static void add_spaces(WINDOW* win, unsigned int num)
249 const char space[] = " ";
250 const unsigned sz = sizeof(space) - 1; /* number of spaces */
258 waddstr(win, space + sz - num);
263 * print aligned string to curses window. This function always prints
266 static int align_str(WINDOW* win, const char *str, unsigned int len,
269 int ret, num; /* of spaces */
271 char *sstr; /* sanitized string */
275 ret = sanitize_str(str, len, &sstr, &width);
277 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
279 sstr = para_strdup(NULL);
281 assert(width <= len);
285 add_spaces(win, num);
286 } else if (align == RIGHT) {
287 add_spaces(win, num);
290 add_spaces(win, num / 2);
292 add_spaces(win, num - num / 2);
298 static void refresh_window(struct gui_window *gw)
300 gw->needs_update = true;
303 static bool window_update_needed(void)
305 return top.needs_update || bot.needs_update || sb.needs_update ||
306 in.needs_update || sep.needs_update;
309 __printf_2_3 static void print_in_bar(int color, const char *fmt,...)
314 if (!curses_active())
316 wattron(in.win, COLOR_PAIR(color));
318 xvasprintf(&msg, fmt, ap);
321 align_str(in.win, msg, get_num_cols(&in), LEFT);
326 static void print_status_bar(void)
330 tmp = para_strdup("para_gui " PACKAGE_VERSION " (hit ? for help)");
332 align_str(sb.win, tmp, get_num_cols(&sb), CENTER);
337 * get the number of the oldest rbe that is (partially) visible. On return,
338 * lines contains the sum of the number of lines of all visible entries. If the
339 * first one is only partially visible, lines is greater than bot.lines.
341 static int first_visible_rbe(unsigned *lines)
343 int i, bot_lines = get_num_lines(&bot);
346 for (i = scroll_position; i < RINGBUFFER_SIZE; i++) {
347 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
351 rbe_lines = NUM_LINES(rbe->len);
352 if (rbe_lines > bot_lines)
355 if (*lines >= bot_lines)
358 return RINGBUFFER_SIZE - 1;
362 returns number of first visible rbe, *lines is the number of lines drawn.
364 static int draw_top_rbe(unsigned *lines)
366 int bot_cols, bot_lines, ret, fvr = first_visible_rbe(lines);
367 struct rb_entry *rbe;
368 size_t bytes_to_skip, cells_to_skip, width;
372 wmove(bot.win, 0, 0);
373 rbe = ringbuffer_get(bot_win_rb, fvr);
376 getmaxyx(bot.win, bot_lines, bot_cols);
377 if (*lines > bot_lines) {
378 /* rbe is partially visible multi-line */
379 cells_to_skip = (*lines - bot_lines) * bot_cols;
380 ret = skip_cells(rbe->msg, cells_to_skip, &bytes_to_skip);
383 ret = strwidth(rbe->msg + bytes_to_skip, &width);
390 wattron(bot.win, COLOR_PAIR(rbe->color));
391 waddstr(bot.win, rbe->msg + bytes_to_skip);
392 *lines = NUM_LINES(width);
396 static void redraw_bot_win(void)
399 int i, bot_lines = get_num_lines(&bot);
401 wmove(bot.win, 0, 0);
403 i = draw_top_rbe(&lines);
406 while (i > 0 && lines < bot_lines) {
407 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, --i);
410 waddstr(bot.win, "\n");
413 lines += NUM_LINES(rbe->len);
414 wattron(bot.win, COLOR_PAIR(rbe->color));
415 waddstr(bot.win, "\n");
416 waddstr(bot.win, rbe->msg);
419 refresh_window(&bot);
422 static void rb_add_entry(int color, char *msg)
424 struct rb_entry *old, *new;
428 if (strwidth(msg, &len) < 0)
430 new = para_malloc(sizeof(struct rb_entry));
434 old = ringbuffer_add(bot_win_rb, new);
439 if (scroll_position) {
440 /* discard current scrolling, like xterm does */
445 wattron(bot.win, COLOR_PAIR(color));
446 getyx(bot.win, y, x);
448 waddstr(bot.win, "\n");
449 waddstr(bot.win, msg);
453 * print formated output to bot win and refresh
455 __printf_2_3 static void outputf(int color, const char* fmt,...)
460 if (!curses_active())
463 xvasprintf(&msg, fmt, ap);
465 rb_add_entry(color, msg);
466 refresh_window(&bot);
469 static int add_output_line(char *line, void *data)
471 int color = *(int *)data? COLOR_ERRMSG : COLOR_OUTPUT;
473 if (!curses_active())
475 rb_add_entry(color, para_strdup(line));
479 static __printf_2_3 void curses_log(int ll, const char *fmt,...)
486 if (curses_active()) {
487 int color = ll <= LL_NOTICE? COLOR_MSG : COLOR_ERRMSG;
489 unsigned bytes = xvasprintf(&msg, fmt, ap);
490 if (bytes > 0 && msg[bytes - 1] == '\n')
491 msg[bytes - 1] = '\0'; /* cut trailing newline */
492 rb_add_entry(color, msg);
493 refresh_window(&bot);
494 } else if (exec_pid <= 0) /* no external command running */
495 vfprintf(stderr, fmt, ap);
498 /** The log function of para_gui, always set to curses_log(). */
499 __printf_2_3 void (*para_log)(int, const char*, ...) = curses_log;
501 static void shutdown_curses(void)
507 /* disable curses, print a message, kill running processes and exit */
508 __noreturn __printf_2_3 static void die(int exit_code, const char* fmt, ...)
512 /* mousemask() exists only in ncurses */
513 #ifdef NCURSES_MOUSE_VERSION
514 mousemask(~(mmask_t)0, NULL); /* Avoid bad terminal state with xterm. */
518 vfprintf(stderr, fmt, argp);
520 /* kill every process in the process group and exit */
521 para_sigaction(SIGTERM, SIG_IGN);
527 * Print stat item #i to curses window
529 static void print_stat_item(int i)
532 struct stat_item_data d = theme.data[i];
533 char *c = stat_content[i];
534 int top_lines = get_num_lines(&top);
536 if (!curses_active() || !d.len || !c)
538 tmp = make_message("%s%s%s", d.prefix, c, d.postfix);
539 wmove(top.win, d.y * top_lines / 100, d.x * COLS / 100);
540 wattron(top.win, COLOR_PAIR(i + 1));
541 align_str(top.win, tmp, d.len * COLS / 100, d.align);
543 refresh_window(&top);
546 static int update_item(int item_num, char *buf)
548 char **c = stat_content + item_num;
555 *c = para_strdup("(artist tag not set)");
558 *c = para_strdup("(title tag not set)");
561 *c = para_strdup("????");
564 *c = para_strdup("(album tag not set)");
567 *c = para_strdup("(comment tag not set)");
571 *c = para_strdup(buf);
573 print_stat_item(item_num);
577 static void print_all_items(void)
581 if (!curses_active())
583 FOR_EACH_STATUS_ITEM(i)
587 static void clear_all_items(void)
591 FOR_EACH_STATUS_ITEM(i) {
592 free(stat_content[i]);
593 stat_content[i] = para_strdup("");
597 static void status_pre_select(struct sched *s, void *context)
599 struct status_task *st = context;
602 para_fd_set(st->fd, &s->rfds, &s->max_fileno);
603 if (task_get_notification(st->task) < 0)
604 return sched_min_delay(s);
606 sched_request_barrier_or_min_delay(&st->next_exec, s);
609 static int status_post_select(struct sched *s, void *context)
611 struct status_task *st = context;
615 ret = task_get_notification(st->task);
616 if (ret == -E_GUI_SIGCHLD && st->pid > 0) {
618 if (waitpid(st->pid, &exit_status, WNOHANG) == st->pid) {
620 PARA_ERROR_LOG("stat command exit status: %d",
625 int fds[3] = {0, 1, 0};
628 /* Avoid busy loop */
629 if (tv_diff(&st->next_exec, now, NULL) > 0)
631 st->next_exec.tv_sec = now->tv_sec + 2;
632 ret = para_exec_cmdline_pid(&st->pid, conf.stat_cmd_arg, fds);
635 ret = mark_fd_nonblocking(fds[1]);
644 if (st->loaded >= st->bufsize) {
645 if (st->bufsize > 1000 * 1000) {
649 st->bufsize += st->bufsize + 1000;
650 st->buf = para_realloc(st->buf, st->bufsize);
652 assert(st->loaded < st->bufsize);
653 ret = read_nonblock(st->fd, st->buf + st->loaded,
654 st->bufsize - st->loaded, &s->rfds, &sz);
656 ret2 = for_each_stat_item(st->buf, st->loaded, update_item);
657 if (ret < 0 || ret2 < 0) {
659 PARA_NOTICE_LOG("closing stat pipe: %s\n",
660 para_strerror(ret < 0? -ret : -ret2));
664 free(stat_content[SI_BASENAME]);
665 stat_content[SI_BASENAME] =
666 para_strdup("stat command terminated!?");
670 sz = ret2; /* what is left */
671 if (sz > 0 && sz < st->loaded)
672 memmove(st->buf, st->buf + st->loaded - sz, sz);
680 static void init_wins(int top_lines)
682 int top_y = 0, bot_y = top_lines + 1, sb_y = LINES - 2,
683 in_y = LINES - 1, sep_y = top_lines;
684 int bot_lines = LINES - top_lines - 3, sb_lines = 1, in_lines = 1,
687 assume_default_colors(theme.dflt.fg, theme.dflt.bg);
689 wresize(top.win, top_lines, COLS);
690 mvwin(top.win, top_y, 0);
692 wresize(sb.win, sb_lines, COLS);
693 mvwin(sb.win, sb_y, 0);
695 wresize(sep.win, sep_lines, COLS);
696 mvwin(sep.win, sep_y, 0);
698 wresize(bot.win, bot_lines, COLS);
699 mvwin(bot.win, bot_y, 0);
701 wresize(in.win, in_lines, COLS);
702 mvwin(in.win, in_y, 0);
704 sep.win = newwin(sep_lines, COLS, sep_y, 0);
705 top.win = newwin(top_lines, COLS, top_y, 0);
706 bot.win = newwin(bot_lines, COLS, bot_y, 0);
707 sb.win = newwin(sb_lines, COLS, sb_y, 0);
708 in.win = newwin(in_lines, COLS, in_y, 0);
709 if (!top.win || !bot.win || !sb.win || !in.win || !sep.win)
710 die(EXIT_FAILURE, "Error: Cannot create curses windows\n");
714 scrollok(bot.win, 1);
715 wattron(sb.win, COLOR_PAIR(COLOR_STATUSBAR));
716 wattron(sep.win, COLOR_PAIR(COLOR_SEPARATOR));
717 wattron(bot.win, COLOR_PAIR(COLOR_BOT));
718 wattron(top.win, COLOR_PAIR(COLOR_TOP));
729 wmove(sep.win, 0, 0);
730 whline(sep.win, theme.sep_char, COLS);
734 wnoutrefresh(top.win);
735 wnoutrefresh(bot.win);
737 wnoutrefresh(sb.win);
738 wnoutrefresh(in.win);
739 wnoutrefresh(sep.win);
743 static void init_pair_or_die(short pair, short f, short b)
745 if (init_pair(pair, f, b) == ERR)
746 die(EXIT_FAILURE, "fatal: init_pair() failed\n");
749 static void init_colors_or_die(void)
754 die(EXIT_FAILURE, "fatal: No color term\n");
755 if (start_color() == ERR)
756 die(EXIT_FAILURE, "fatal: failed to start colors\n");
757 FOR_EACH_STATUS_ITEM(i)
758 if (theme.data[i].len)
759 init_pair_or_die(i + 1, theme.data[i].color.fg,
760 theme.data[i].color.bg);
761 init_pair_or_die(COLOR_STATUSBAR, theme.sb.fg, theme.sb.bg);
762 init_pair_or_die(COLOR_COMMAND, theme.cmd.fg, theme.cmd.bg);
763 init_pair_or_die(COLOR_OUTPUT, theme.output.fg, theme.output.bg);
764 init_pair_or_die(COLOR_MSG, theme.msg.fg, theme.msg.bg);
765 init_pair_or_die(COLOR_ERRMSG, theme.err_msg.fg, theme.err_msg.bg);
766 init_pair_or_die(COLOR_SEPARATOR, theme.sep.fg, theme.sep.bg);
767 init_pair_or_die(COLOR_TOP, theme.dflt.fg, theme.dflt.bg);
768 init_pair_or_die(COLOR_BOT, theme.dflt.fg, theme.dflt.bg);
771 /* (Re-)initialize the curses library. */
772 static void init_curses(void)
776 if (refresh() == ERR) /* refresh is really needed */
777 die(EXIT_FAILURE, "refresh() failed\n");
778 if (LINES < theme.lines_min || COLS < theme.cols_min)
779 die(EXIT_FAILURE, "Terminal (%dx%d) too small"
780 " (need at least %dx%d)\n", COLS, LINES,
781 theme.cols_min, theme.lines_min);
782 curs_set(0); /* make cursor invisible, ignore errors */
783 nonl(); /* do not NL->CR/NL on output, always returns OK */
784 /* don't echo input */
786 die(EXIT_FAILURE, "fatal: noecho() failed\n");
787 /* take input chars one at a time, no wait for \n */
789 die(EXIT_FAILURE, "fatal: cbreak() failed\n");
790 init_colors_or_die();
791 clear(); /* ignore non-fatal errors */
792 init_wins(theme.top_lines_default);
793 // noecho(); /* don't echo input */
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",
872 * When the gengetopt config file parser is called more than once, any
873 * key map arguments found in the config file are _appended_ to the old
874 * values, even though we turn on ->override. We want the new arguments
875 * to replace the old ones, so we must empty the key_map_arg array
876 * first. Unfortunately, this also clears any key map arguments given
877 * at the command line.
881 for (i = 0; i < conf.key_map_given; i++) {
882 free(conf.key_map_arg[i]);
883 conf.key_map_arg[i] = NULL;
885 conf.key_map_given = 0;
888 gui_cmdline_parser_config_file(config_file, &conf, ¶ms);
889 loglevel = get_loglevel_by_name(conf.loglevel_arg);
890 check_key_map_args_or_die();
896 theme_init(conf.theme_arg, &theme);
899 /* reread configuration, terminate on errors */
900 static void reread_conf(void)
903 * gengetopt might print to stderr and exit on errors. So we have to
904 * shutdown curses first.
907 parse_config_file_or_die(true /* override */);
909 print_in_bar(COLOR_MSG, "config file reloaded\n");
913 * React to various signal-related events
915 static int signal_post_select(struct sched *s, __a_unused void *context)
917 int ret = para_next_signal(&s->rfds);
923 die(EXIT_FAILURE, "only the good die young (caught SIGTERM)\n");
926 PARA_WARNING_LOG("caught SIGINT, reset\n");
927 /* Nothing to do. SIGINT killed our child which gets noticed
928 * by do_select and resets everything.
932 PARA_NOTICE_LOG("got SIGUSR1, rereading configuration\n");
936 task_notify_all(s, E_GUI_SIGCHLD);
942 static enum exec_status exec_status(void)
944 if (exec_fds[0] >= 0 || exec_fds[1] >= 0)
951 static void exec_pre_select(struct sched *s, void *context)
953 struct exec_task *et = context;
954 if (exec_fds[0] >= 0)
955 para_fd_set(exec_fds[0], &s->rfds, &s->max_fileno);
956 if (exec_fds[1] >= 0)
957 para_fd_set(exec_fds[1], &s->rfds, &s->max_fileno);
958 if (task_get_notification(et->task) < 0)
962 static int exec_post_select(struct sched *s, void *context)
964 struct exec_task *ct = context;
967 ret = task_get_notification(ct->task);
968 if (ret == -E_GUI_SIGCHLD && exec_pid > 0) {
970 if (waitpid(exec_pid, &exit_status, WNOHANG) == exec_pid) {
973 PARA_INFO_LOG("command exit status: %d", exit_status);
974 print_in_bar(COLOR_MSG, " ");
977 for (i = 0; i < 2; i++) {
981 ret = read_nonblock(exec_fds[i],
982 ct->command_buf[i] + ct->cbo[i],
983 COMMAND_BUF_SIZE - 1 - ct->cbo[i], &s->rfds, &sz);
986 ct->cbo[i] = for_each_line(ct->flags[i], ct->command_buf[i],
987 ct->cbo[i], add_output_line, &i);
988 if (sz != ct->cbo[i]) { /* at least one line found */
989 refresh_window(&bot);
992 if (ret < 0 || exec_pid == 0) {
994 PARA_NOTICE_LOG("closing command fd %d: %s",
995 i, para_strerror(-ret));
1000 if (exec_fds[!i] < 0) /* both fds closed */
1003 if (ct->cbo[i] == COMMAND_BUF_SIZE - 1) {
1004 PARA_NOTICE_LOG("discarding overlong line");
1006 ct->flags[i] = FELF_DISCARD_FIRST;
1012 static void input_pre_select(struct sched *s, __a_unused void *context)
1014 if (exec_status() != EXEC_XCMD)
1015 para_fd_set(STDIN_FILENO, &s->rfds, &s->max_fileno);
1016 if (window_update_needed())
1020 /* read from command pipe and print data to bot window */
1021 static void exec_and_display(const char *file_and_args)
1023 int ret, fds[3] = {0, 1, 1};
1025 outputf(COLOR_COMMAND, "%s", file_and_args);
1026 ret = para_exec_cmdline_pid(&exec_pid, file_and_args, fds);
1029 ret = mark_fd_nonblocking(fds[1]);
1032 ret = mark_fd_nonblocking(fds[2]);
1035 exec_fds[0] = fds[1];
1036 exec_fds[1] = fds[2];
1037 print_in_bar(COLOR_MSG, "hit any key to abort\n");
1040 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
1045 static void exec_para(const char *args)
1047 char *file_and_args;
1049 file_and_args = make_message(BINDIR "/para_client -- %s", args);
1050 exec_and_display(file_and_args);
1051 free(file_and_args);
1055 * shutdown curses and stat pipe before executing external commands
1057 static void exec_external(char *file_and_args)
1059 int fds[3] = {-1, -1, -1};
1064 para_exec_cmdline_pid(&exec_pid, file_and_args, fds);
1067 static void handle_command(int c)
1071 /* first check user-defined key bindings */
1072 for (i = 0; i < conf.key_map_given; ++i) {
1073 char *tmp, *handler, *arg;
1075 tmp = para_strdup(conf.key_map_arg[i]);
1076 if (!split_key_map(tmp, &handler, &arg)) {
1080 if (strcmp(tmp, km_keyname(c))) {
1084 if (*handler == 'd')
1085 exec_and_display(arg);
1086 else if (*handler == 'x')
1088 else if (*handler == 'p')
1090 else if (*handler == 'i') {
1091 int num = find_cmd_byname(arg);
1093 command_list[num].handler();
1098 /* not found, check internal key bindings */
1099 for (i = 0; command_list[i].handler; i++) {
1100 if (!strcmp(km_keyname(c), command_list[i].key)) {
1101 command_list[i].handler();
1105 print_in_bar(COLOR_ERRMSG, "key '%s' is not bound, press ? for help",
1109 static int input_post_select(__a_unused struct sched *s, __a_unused void *context)
1112 enum exec_status exs = exec_status();
1114 if (exs == EXEC_XCMD)
1116 if (window_update_needed()) {
1117 if (top.needs_update)
1118 assert(wnoutrefresh(top.win) == OK);
1119 if (bot.needs_update)
1120 assert(wnoutrefresh(bot.win) == OK);
1121 if (sep.needs_update)
1122 assert(wnoutrefresh(sep.win) == OK);
1123 if (sb.needs_update)
1124 assert(wnoutrefresh(sb.win) == OK);
1125 if (in.needs_update)
1126 assert(wnoutrefresh(in.win) == OK);
1128 top.needs_update = bot.needs_update = sb.needs_update =
1129 in.needs_update = sep.needs_update = false;
1131 ret = wgetch(top.win);
1134 if (ret == KEY_RESIZE) {
1135 if (curses_active()) {
1142 if (exs == EXEC_IDLE)
1143 handle_command(ret);
1144 else if (exec_pid > 0)
1145 kill(exec_pid, SIGTERM);
1149 static void print_scroll_msg(void)
1151 unsigned lines_total, filled = ringbuffer_filled(bot_win_rb);
1152 int first_rbe = first_visible_rbe(&lines_total);
1154 print_in_bar(COLOR_MSG, "scrolled view: %d-%d/%d\n", filled - first_rbe,
1155 filled - scroll_position, ringbuffer_filled(bot_win_rb));
1158 static void com_scroll_top(void)
1160 int i = RINGBUFFER_SIZE - 1, bot_lines = get_num_lines(&bot);
1163 while (i > 0 && !ringbuffer_get(bot_win_rb, i))
1165 /* i is oldest entry */
1166 for (; lines < bot_lines && i >= 0; i--) {
1167 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
1170 lines += NUM_LINES(rbe->len);
1173 if (lines > 0 && scroll_position != i) {
1174 scroll_position = i;
1179 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1182 static void com_cancel_scroll(void)
1185 if (scroll_position == 0) {
1186 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1189 scroll_position = 0;
1193 static void com_page_down(void)
1196 int i = scroll_position, bot_lines = get_num_lines(&bot);
1198 while (lines < bot_lines && --i > 0) {
1199 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
1202 lines += NUM_LINES(rbe->len);
1205 scroll_position = i;
1210 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1213 static void com_page_up(void)
1216 int fvr = first_visible_rbe(&lines), bot_lines = get_num_lines(&bot);
1218 if (fvr < 0 || fvr + 1 >= ringbuffer_filled(bot_win_rb)) {
1219 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1222 scroll_position = fvr + 1;
1223 for (; scroll_position > 0; scroll_position--) {
1224 first_visible_rbe(&lines);
1225 if (lines == bot_lines)
1232 static void com_scroll_down(void)
1234 struct rb_entry *rbe;
1235 int rbe_lines, bot_lines = get_num_lines(&bot);
1237 if (!scroll_position) {
1238 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1242 rbe = ringbuffer_get(bot_win_rb, scroll_position);
1243 rbe_lines = NUM_LINES(rbe->len);
1244 wscrl(bot.win, rbe_lines);
1245 wmove(bot.win, bot_lines - rbe_lines, 0);
1246 wattron(bot.win, COLOR_PAIR(rbe->color));
1247 waddstr(bot.win, rbe->msg);
1248 refresh_window(&bot);
1252 static void com_scroll_up(void)
1254 struct rb_entry *rbe = NULL;
1256 int i, first_rbe, num_scroll;
1258 /* the entry that is going to vanish */
1259 rbe = ringbuffer_get(bot_win_rb, scroll_position);
1262 num_scroll = NUM_LINES(rbe->len);
1263 first_rbe = first_visible_rbe(&lines);
1264 if (first_rbe < 0 || (first_rbe == ringbuffer_filled(bot_win_rb) - 1))
1267 wscrl(bot.win, -num_scroll);
1268 i = draw_top_rbe(&lines);
1271 while (i > 0 && lines < num_scroll) {
1273 rbe = ringbuffer_get(bot_win_rb, --i);
1276 rbe_lines = NUM_LINES(rbe->len);
1278 wattron(bot.win, COLOR_PAIR(rbe->color));
1279 waddstr(bot.win, "\n");
1280 waddstr(bot.win, rbe->msg);
1285 refresh_window(&bot);
1289 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1292 static void com_ll_decr(void)
1294 if (loglevel <= LL_DEBUG) {
1295 print_in_bar(COLOR_ERRMSG,
1296 "loglevel already at maximal verbosity\n");
1300 print_in_bar(COLOR_MSG, "loglevel set to %d\n", loglevel);
1303 static void com_ll_incr(void)
1305 if (loglevel >= LL_EMERG) {
1306 print_in_bar(COLOR_ERRMSG,
1307 "loglevel already at minimal verbosity\n");
1311 print_in_bar(COLOR_MSG, "loglevel set to %d\n", loglevel);
1314 static void com_reread_conf(void)
1319 static void com_help(void)
1323 for (i = 0; i < conf.key_map_given; ++i) {
1324 char *handler, *arg, *tmp = para_strdup(conf.key_map_arg[i]);
1325 const char *handler_text = "???", *desc = NULL;
1327 if (!split_key_map(tmp, &handler, &arg)) {
1333 handler_text = "internal";
1334 desc = command_list[find_cmd_byname(arg)].description;
1336 case 'x': handler_text = "external"; break;
1337 case 'd': handler_text = "display "; break;
1338 case 'p': handler_text = "para "; break;
1340 outputf(COLOR_MSG, "%s\t%s\t%s%s\t%s", tmp, handler_text, arg,
1341 strlen(arg) < 8? "\t" : "",
1345 for (i = 0; command_list[i].handler; i++) {
1346 struct gui_command gc = command_list[i];
1348 outputf(COLOR_MSG, "%s\tinternal\t%s\t%s%s", gc.key, gc.name,
1349 strlen(gc.name) < 8? "\t" : "",
1352 print_in_bar(COLOR_MSG, "try \"para_gui -h\" or \"para_client help\" "
1356 static void com_shrink_top_win(void)
1358 int top_lines = get_num_lines(&top);
1360 if (top_lines <= theme.top_lines_min) {
1361 PARA_WARNING_LOG("can not decrease top window\n");
1364 init_wins(top_lines - 1);
1365 print_in_bar(COLOR_MSG, "%s", "decreased top window");
1368 static void com_enlarge_top_win(void)
1370 int top_lines = get_num_lines(&top), bot_lines = get_num_lines(&bot);
1372 if (bot_lines < 3) {
1373 PARA_WARNING_LOG("can not increase top window\n");
1376 init_wins(top_lines + 1);
1377 print_in_bar(COLOR_MSG, "increased top window");
1380 static void com_version(void)
1382 print_in_bar(COLOR_MSG, "%s", version_single_line("gui"));
1385 __noreturn static void com_quit(void)
1387 die(EXIT_SUCCESS, "%s", "");
1390 static void com_refresh(void)
1396 static void com_next_theme(void)
1402 static void com_prev_theme(void)
1408 __noreturn static void print_help_and_die(void)
1410 struct ggo_help h = DEFINE_GGO_HELP(gui);
1411 bool d = conf.detailed_help_given;
1413 ggo_print_help(&h, d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS);
1417 static int setup_tasks_and_schedule(void)
1420 struct exec_task exec_task = {.task = NULL};
1421 struct status_task status_task = {.fd = -1};
1422 struct input_task input_task = {.task = NULL};
1423 struct signal_task *signal_task;
1424 struct sched sched = {
1425 .default_timeout = {
1426 .tv_sec = conf.timeout_arg / 1000,
1427 .tv_usec = (conf.timeout_arg % 1000) * 1000,
1431 exec_task.task = task_register(&(struct task_info) {
1433 .pre_select = exec_pre_select,
1434 .post_select = exec_post_select,
1435 .context = &exec_task,
1438 status_task.task = task_register(&(struct task_info) {
1440 .pre_select = status_pre_select,
1441 .post_select = status_post_select,
1442 .context = &status_task,
1445 input_task.task = task_register(&(struct task_info) {
1447 .pre_select = input_pre_select,
1448 .post_select = input_post_select,
1449 .context = &input_task,
1452 signal_task = signal_init_or_die();
1453 para_install_sighandler(SIGINT);
1454 para_install_sighandler(SIGTERM);
1455 para_install_sighandler(SIGCHLD);
1456 para_install_sighandler(SIGUSR1);
1457 signal_task->task = task_register(&(struct task_info) {
1459 .pre_select = signal_pre_select,
1460 .post_select = signal_post_select,
1461 .context = signal_task,
1463 ret = schedule(&sched);
1464 sched_shutdown(&sched);
1465 signal_shutdown(signal_task);
1470 * The main function of para_gui.
1472 * \param argc Usual argument count.
1473 * \param argv Usual argument vector.
1475 * After initialization para_gui registers the following tasks to the paraslash
1476 * scheduler: status, exec, signal, input.
1478 * The status task executes the para_audioc stat command to obtain the status
1479 * of para_server and para_audiod, and displays this information in the top
1480 * window of para_gui.
1482 * The exec task is responsible for printing the output of the currently
1483 * running executable to the bottom window.
1485 * The signal task performs suitable actions according to any signals received.
1486 * For example it refreshes all windows on terminal size changes and resets the
1487 * terminal on \p SIGTERM.
1489 * The input task reads single key strokes from stdin. For each key pressed, it
1490 * executes the command handler associated with this key.
1492 * \return \p EXIT_SUCCESS or \p EXIT_FAILURE.
1494 int main(int argc, char *argv[])
1496 gui_cmdline_parser(argc, argv, &conf); /* exits on errors */
1497 loglevel = get_loglevel_by_name(conf.loglevel_arg);
1498 version_handle_flag("gui", conf.version_given);
1499 if (conf.help_given || conf.detailed_help_given)
1500 print_help_and_die();
1501 parse_config_file_or_die(false /* override */);
1502 bot_win_rb = ringbuffer_new(RINGBUFFER_SIZE);
1503 setlocale(LC_CTYPE, "");
1504 initscr(); /* needed only once, always successful */
1506 return setup_tasks_and_schedule() < 0? EXIT_FAILURE : EXIT_SUCCESS;