2 * Copyright (C) 1998-2008 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. */
10 #include <sys/types.h>
12 #include "gui.cmdline.h"
17 #include "ringbuffer.h"
24 /** define the array of error lists needed by para_gui */
26 static char *stat_content[NUM_STAT_ITEMS];
28 #define STANDARD_STATUS_BAR "para_gui " PACKAGE_VERSION " (hit ? for help)"
30 static int signal_pipe;
32 static struct win_data {
38 } top, bot, sb, in, sep;
40 #define RINGBUFFER_SIZE 512
46 struct ringbuffer *bot_win_rb;
47 #define NUM_LINES(len) (1 + (len) / bot.cols)
49 static unsigned scroll_position;
51 static int cmd_died, curses_active;
54 static int command_pipe = -1;
55 static int audiod_pipe = -1;
56 static struct gui_args_info conf;
58 enum {GETCH_MODE, COMMAND_MODE, EXTERNAL_MODE};
61 #define COLOR_STATUSBAR 52
62 #define COLOR_COMMAND 53
63 #define COLOR_OUTPUT 54
65 #define COLOR_ERRMSG 56
66 #define COLOR_WELCOME 57
67 #define COLOR_SEPARATOR 58
74 const char *description;
75 void (*handler)(void);
81 char postfix[MAXLINE];
87 char content[MAXLINE];
90 static struct gui_theme theme;
95 static void com_help(void);
96 static void com_reread_conf(void);
97 static void com_enlarge_top_win(void);
98 static void com_shrink_top_win(void);
99 static void com_version(void);
100 static void com_quit(void);
101 static void com_refresh(void);
102 static void com_ll_incr(void);
103 static void com_ll_decr(void);
104 static void com_prev_theme(void);
105 static void com_next_theme(void);
106 static void com_scroll_up(void);
107 static void com_scroll_down(void);
108 static void com_page_up(void);
109 static void com_page_down(void);
111 struct gui_command command_list[] = {
115 .description = "print help",
119 .name = "enlarge_win",
120 .description = "enlarge the top window",
121 .handler = com_enlarge_top_win
124 .name = "shrink_win",
125 .description = "shrink the top window",
126 .handler = com_shrink_top_win
129 .name = "reread_conf",
130 .description = "reread configuration file",
131 .handler = com_reread_conf
135 .description = "exit para_gui",
140 .description = "redraw the screen",
141 .handler = com_refresh
144 .name = "next_theme",
145 .description = "switch to next theme",
146 .handler = com_next_theme
149 .name = "prev_theme",
150 .description = "switch to previous stream",
151 .handler = com_prev_theme
155 .description = "increase loglevel (decreases verbosity)",
156 .handler = com_ll_incr
160 .description = "decrease loglevel (increases verbosity)",
161 .handler = com_ll_decr
165 .description = "show the para_gui version",
166 .handler = com_version
170 .description = "scroll up one line",
171 .handler = com_scroll_up
174 .name = "scroll_down",
175 .description = "scroll down one line",
176 .handler = com_scroll_down
180 .description = "scroll up one page",
181 .handler = com_page_up
185 .description = "scroll down one page",
186 .handler = com_page_down
192 static int para_open_audiod_pipe(char *cmd)
194 int fds[3] = {0, 1, 0};
196 int ret = para_exec_cmdline_pid(&pid, cmd, fds);
199 ret = mark_fd_nonblocking(fds[1]);
206 static int read_audiod_pipe(int fd, line_handler_t *line_handler)
208 static char buf[4096];
209 const ssize_t bufsize = sizeof(buf) - 1;
210 static ssize_t loaded;
213 if (loaded >= bufsize)
215 ret = read(fd, buf + loaded, bufsize - loaded);
219 loaded = for_each_line(buf, loaded, line_handler, NULL);
224 static int find_cmd_byname(char *name)
228 for (i = 0; command_list[i].handler; i++)
229 if (!strcmp(command_list[i].name, name))
234 /* taken from mutt */
235 static char *km_keyname(int c)
240 sprintf(buf, "<up>");
244 sprintf(buf, "<down>");
248 sprintf(buf, "<left>");
251 if (c == KEY_RIGHT) {
252 sprintf(buf, "<right>");
255 if (c == KEY_NPAGE) {
256 sprintf(buf, "<npage>");
259 if (c == KEY_PPAGE) {
260 sprintf(buf, "<ppage>");
263 if (c < 256 && c > -128 && iscntrl((unsigned char) c)) {
268 buf[1] = (c + '@') & 0x7f;
271 snprintf(buf, sizeof(buf), "\\%d%d%d", c >> 6,
272 (c >> 3) & 7, c & 7);
273 } else if (c >= KEY_F0 && c < KEY_F(256))
274 sprintf(buf, "<F%d>", c - KEY_F0);
276 snprintf(buf, sizeof(buf), "%c", (unsigned char) c);
278 snprintf(buf, sizeof(buf), "\\x%hx", (unsigned short) c);
282 static char *configfile_exists(void)
284 static char *config_file;
287 if (!conf.config_file_given) {
289 char *home = para_homedir();
290 config_file = make_message("%s/.paraslash/gui.conf",
296 tmp = conf.config_file_arg;
297 return file_exists(tmp)? tmp: NULL;
301 * print num spaces to curses window
303 static void add_spaces(WINDOW* win, unsigned int num)
312 * print aligned string to curses window. This function always prints
315 static int align_str(WINDOW* win, const char *string, unsigned int len,
318 int num; /* of spaces */
323 num = len - strlen(string);
324 str = para_strdup(string);
331 add_spaces(win, num);
332 } else if (align == RIGHT) {
333 add_spaces(win, num);
336 add_spaces(win, num / 2);
337 waddstr(win, str[0]? str: "");
338 add_spaces(win, num - num / 2);
344 __printf_2_3 static void print_in_bar(int color, const char *fmt,...)
350 wattron(in.win, COLOR_PAIR(color));
351 PARA_VSPRINTF(fmt, msg);
353 align_str(in.win, msg, sb.cols, LEFT);
359 * update the status bar
361 static void print_status_bar(void)
366 align_str(sb.win, STANDARD_STATUS_BAR, sb.cols, CENTER);
371 * get the number of the oldest rbe that is (partially) visible. On return,
372 * lines contains the sum of the number of lines of all visable entries. If the
373 * first one is only partially visible, lines is greater than bot.lines.
375 static int first_visible_rbe(unsigned *lines)
379 for (i = scroll_position; i < RINGBUFFER_SIZE; i++) {
380 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
384 // fprintf(stderr, "found: %s\n", rbe->msg);
385 rbe_lines = NUM_LINES(rbe->len);
386 if (rbe_lines > bot.lines)
389 if (*lines >= bot.lines)
392 return RINGBUFFER_SIZE - 1;
395 static int draw_top_rbe(unsigned *lines)
398 int offset, fvr = first_visible_rbe(lines);
399 struct rb_entry *rbe;
403 wmove(bot.win, 0, 0);
404 rbe = ringbuffer_get(bot_win_rb, fvr);
407 /* first rbe might be only partially visible */
408 offset = (*lines - bot.lines) * bot.cols;
409 len = strlen(rbe->msg);
410 if (offset < 0 || len < offset)
412 wattron(bot.win, COLOR_PAIR(rbe->color));
413 waddstr(bot.win, rbe->msg + offset);
414 *lines = NUM_LINES(len - offset);
418 static void redraw_bot_win(void)
423 wmove(bot.win, 0, 0);
425 i = draw_top_rbe(&lines);
428 while (i > 0 && lines < bot.lines) {
429 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, --i);
432 waddstr(bot.win, "\n");
435 lines += NUM_LINES(rbe->len);
436 wattron(bot.win, COLOR_PAIR(rbe->color));
437 waddstr(bot.win, "\n");
438 waddstr(bot.win, rbe->msg);
444 static void rb_add_entry(int color, char *msg)
446 struct rb_entry *old, *new = para_malloc(sizeof(struct rb_entry));
449 new->len = strlen(msg);
451 old = ringbuffer_add(bot_win_rb, new);
452 // fprintf(stderr, "added: %s\n", new->msg);
457 if (scroll_position) {
458 /* discard current scrolling, like xterm does */
463 wattron(bot.win, COLOR_PAIR(color));
464 getyx(bot.win, y, x);
466 waddstr(bot.win, "\n");
467 waddstr(bot.win, msg);
471 * print formated output to bot win and refresh
473 __printf_2_3 static void outputf(int color, const char* fmt,...)
479 PARA_VSPRINTF(fmt, msg);
480 rb_add_entry(color, msg);
484 static int add_output_line(char *line, __a_unused void *data)
488 rb_add_entry(COLOR_OUTPUT, para_strdup(line));
492 void para_log(int ll, const char *fmt,...)
497 if (ll < conf.loglevel_arg || !curses_active)
506 color = COLOR_ERRMSG;
508 PARA_VSPRINTF(fmt, msg);
510 rb_add_entry(color, msg);
514 static void setup_signal_handling(void)
516 signal_pipe = para_signal_init();
517 para_install_sighandler(SIGINT);
518 para_install_sighandler(SIGTERM);
519 para_install_sighandler(SIGCHLD);
520 para_install_sighandler(SIGWINCH);
521 para_install_sighandler(SIGUSR1);
522 // signal(SIGPIPE, SIG_IGN);
523 signal(SIGHUP, SIG_IGN);
526 __noreturn static void do_exit(int ret)
528 signal(SIGTERM, SIG_IGN);
533 static void shutdown_curses(void)
542 __noreturn static void finish(int ret)
549 * exit curses and print given message to stdout/stderr
551 __noreturn __printf_2_3 static void msg_n_exit(int ret, const char* fmt, ...)
554 FILE *outfd = ret? stderr: stdout;
558 vfprintf(outfd, fmt, argp);
563 static void print_welcome(void)
565 int ll = conf.loglevel_arg;
568 outputf(COLOR_WELCOME, "Welcome to para_gui " PACKAGE_VERSION
569 " \"" CODENAME "\". Theme: %s", theme.name);
576 static void init_wins(int top_lines)
580 top.lines = top_lines;
585 bot.lines = LINES - top.lines - 3;
587 bot.begy = top.lines + 1;
602 sep.begy = top.lines;
605 assume_default_colors(theme.default_fg, theme.default_bg);
607 mvwin(top.win, top.begy, top.begx);
608 wresize(top.win, top.lines, top.cols);
610 mvwin(sb.win, sb.begy, sb.begx);
611 wresize(sb.win, sb.lines, sb.cols);
613 mvwin(sep.win, sep.begy, sep.begx);
614 wresize(sep.win, sep.lines, sep.cols);
616 mvwin(bot.win, bot.begy, bot.begx);
617 wresize(bot.win, bot.lines, bot.cols);
619 mvwin(in.win, in.begy, in.begx);
620 wresize(in.win, in.lines, in.cols);
622 sep.win = newwin(sep.lines, sep.cols, sep.begy, sep.begx);
623 top.win = newwin(top.lines, top.cols, top.begy, top.begx);
624 bot.win = newwin(bot.lines, bot.cols, bot.begy, bot.begx);
625 sb.win = newwin(sb.lines, sb.cols, sb.begy, sb.begx);
626 in.win = newwin(in.lines, in.cols, in.begy, in.begx);
627 if (!top.win || !bot.win || !sb.win || !in.win || !sep.win)
628 msg_n_exit(1, "Error: Cannot create curses windows\n");
632 scrollok(bot.win, 1);
633 wattron(sb.win, COLOR_PAIR(COLOR_STATUSBAR));
634 wattron(sep.win, COLOR_PAIR(COLOR_SEPARATOR));
635 wattron(bot.win, COLOR_PAIR(COLOR_BOT));
636 wattron(top.win, COLOR_PAIR(COLOR_TOP));
648 wmove(sep.win, 0, 0);
649 for (i = 1; i <= COLS; i++)
650 waddstr(sep.win, theme.sep_str);
653 wnoutrefresh(top.win);
654 wnoutrefresh(bot.win);
655 //wnoutrefresh(sb.win);
657 wnoutrefresh(in.win);
658 wnoutrefresh(sep.win);
662 static void check_geometry(void)
664 if (LINES < theme.lines_min || COLS < theme.cols_min)
665 msg_n_exit(EXIT_FAILURE, "Error: Terminal (%dx%d) too small"
666 " (need at least %dx%d)\n", COLS, LINES,
667 theme.cols_min, theme.lines_min);
671 * Print stat item #i to curses window
673 static void print_stat_item(int i)
676 struct stat_item_data d = theme.data[i];
677 char *c = stat_content[i];
679 if (!curses_active || !d.len || !c)
681 tmp = make_message("%s%s%s", d.prefix, c, d.postfix);
682 wmove(top.win, d.y * top.lines / 100, d.x * COLS / 100);
684 wattron(top.win, COLOR_PAIR(i + 1));
685 align_str(top.win, tmp, d.len * COLS / 100, d.align);
690 static void print_all_items(void)
696 FOR_EACH_STATUS_ITEM(i)
700 static void clear_all_items(void)
704 FOR_EACH_STATUS_ITEM(i) {
705 free(stat_content[i]);
706 stat_content[i] = para_strdup("");
710 static void init_colors(void)
715 msg_n_exit(EXIT_FAILURE, "Error: No color term\n");
717 FOR_EACH_STATUS_ITEM(i)
718 if (theme.data[i].len)
719 init_pair(i + 1, theme.data[i].fg, theme.data[i].bg);
720 init_pair(COLOR_STATUSBAR, theme.sb_fg, theme.sb_bg);
721 init_pair(COLOR_COMMAND, theme.cmd_fg, theme.cmd_bg);
722 init_pair(COLOR_OUTPUT, theme.output_fg, theme.output_bg);
723 init_pair(COLOR_MSG, theme.msg_fg, theme.msg_bg);
724 init_pair(COLOR_ERRMSG, theme.err_msg_fg, theme.err_msg_bg);
725 init_pair(COLOR_WELCOME, theme.welcome_fg, theme.welcome_bg);
726 init_pair(COLOR_SEPARATOR, theme.sep_fg, theme.sep_bg);
727 init_pair(COLOR_TOP, theme.default_fg, theme.default_bg);
728 init_pair(COLOR_BOT, theme.default_fg, theme.default_bg);
732 * (re-)initialize the curses library FIXME: Error checking
734 static void init_curses(void)
737 if (top.win && refresh() == ERR) { /* refesh is really needed */
738 msg_n_exit(EXIT_FAILURE, "refresh() failed\n");
741 curs_set(0); /* make cursor invisible, ignore errors */
742 // if (noraw() == ERR);
743 // msg_n_exit(EXIT_FAILURE, "can not place terminal out of "
745 nonl(); /* tell curses not to do NL->CR/NL on output */
746 noecho(); /* don't echo input */
747 cbreak(); /* take input chars one at a time, no wait for \n */
751 init_wins(theme.top_lines_default);
753 noecho(); /* don't echo input */
757 static void check_sigchld(void)
762 ret = para_reap_child(&pid);
765 if (pid == cmd_pid) {
769 goto reap_next_child;
773 * print status line if line starts with known command.
775 static int check_stat_line(char *line, __a_unused void *data)
779 // PARA_INFO_LOG("%s: checking: %s\n", __func__, line);
780 i = stat_line_valid(line);
782 line += strlen(status_item_list[i]) + 1;
785 free(stat_content[i]);
786 stat_content[i] = para_strdup(line);
793 * This sucker modifies its first argument. *handler and *arg are
794 * pointers to 0-terminated strings (inside line). Crap.
796 static int split_key_map(char *line, char **handler, char **arg)
798 if (!(*handler = strchr(line + 1, ':')))
802 if (!(*arg = strchr(*handler, ':')))
811 static int check_key_map_args(void)
815 char *tmp = NULL, *handler, *arg;
817 for (i = 0; i < conf.key_map_given; ++i) {
818 s = conf.key_map_arg[i];
822 tmp = para_strdup(s);
823 if (!split_key_map(tmp, &handler, &arg))
825 if (strlen(handler) != 1)
834 if (find_cmd_byname(arg) < 0)
844 * React to various signal-related events
846 static void handle_signal(int sig)
850 msg_n_exit(EXIT_FAILURE,
851 "only the good die young (caught SIGTERM))\n");
861 PARA_WARNING_LOG("caught SIGINT, reset");
862 /* Nothing to do. SIGINT killed our child, para_client stat.
863 * This get noticed by do_select which resets everything
867 PARA_NOTICE_LOG("got SIGUSR1, rereading configuration");
876 static int open_audiod_pipe(void)
884 return para_open_audiod_pipe(conf.stat_cmd_arg);
888 * This is the core select loop. Besides the (internal) signal
889 * pipe, the following other fds are checked according to the mode:
891 * GETCH_MODE: check stdin, return when key is pressed
893 * COMMAND_MODE: check command_pipe and stdin. Return when a screen full
894 * of output has been read or when other end has closed command pipe or
895 * when any key is pressed.
897 * EXTERNAL_MODE: Check only signal pipe. Used when an external command
898 * is running. During that thime curses is disabled. Returns when
901 static int do_select(int mode)
905 int max_fileno, cp_numread = 1;
906 char command_buf[4096] = "";
907 int cbo = 0; /* command buf offset */
910 tv.tv_sec = conf.timeout_arg / 1000;
911 tv.tv_usec = (conf.timeout_arg % 1000) * 1000;
912 // ret = refresh_status();
917 audiod_pipe = open_audiod_pipe();
918 if (audiod_pipe >= 0)
919 para_fd_set(audiod_pipe, &rfds, &max_fileno);
921 para_fd_set(signal_pipe, &rfds, &max_fileno);
922 /* command pipe only for COMMAND_MODE */
923 if (command_pipe >= 0 && mode == COMMAND_MODE)
924 para_fd_set(command_pipe, &rfds, &max_fileno);
925 ret = para_select(max_fileno + 1, &rfds, NULL, &tv);
927 goto check_return; /* skip fd checks */
929 if (FD_ISSET(signal_pipe, &rfds)) {
930 int sig_nr = para_next_signal();
932 handle_signal(sig_nr);
934 /* read command pipe if ready */
935 if (command_pipe >= 0 && mode == COMMAND_MODE &&
936 FD_ISSET(command_pipe, &rfds)) {
937 cp_numread = read(command_pipe, command_buf + cbo,
938 sizeof(command_buf) - 1 - cbo);
943 PARA_ERROR_LOG("read error (%d)", cp_numread);
948 if (audiod_pipe >= 0 && FD_ISSET(audiod_pipe, &rfds))
949 if (read_audiod_pipe(audiod_pipe, check_stat_line) <= 0) {
953 free(stat_content[SI_BASENAME]);
954 stat_content[SI_BASENAME] =
955 para_strdup("audiod not running!?");
961 if (cp_numread <= 0 && !cbo) /* command complete */
964 cbo = for_each_line(command_buf, cbo,
965 &add_output_line, NULL);
969 ret = wgetch(top.win);
970 if (ret != ERR && ret != KEY_RESIZE) {
976 kill(cmd_pid, SIGTERM);
981 ret = wgetch(top.win);
982 if (ret != ERR && ret != KEY_RESIZE)
995 * read from command pipe and print data to bot window
997 static int send_output(void)
1001 if (command_pipe < 0)
1003 ret = mark_fd_nonblocking(command_pipe);
1005 close(command_pipe);
1008 if (do_select(COMMAND_MODE) >= 0)
1009 PARA_INFO_LOG("command complete");
1011 PARA_NOTICE_LOG("command aborted");
1012 print_in_bar(COLOR_MSG, " ");
1016 static int client_cmd_cmdline(char *cmd)
1018 int ret, fds[3] = {0, 1, 0};
1019 char *c = make_message(BINDIR "/para_client -- %s", cmd);
1021 outputf(COLOR_COMMAND, "%s", c);
1022 print_in_bar(COLOR_MSG, "executing client command, hit any key to abort\n");
1023 ret = para_exec_cmdline_pid(&cmd_pid, c, fds);
1027 command_pipe = fds[1];
1028 return send_output();
1032 * exec command and print output to bot win
1034 static int display_cmd(char *cmd)
1036 int fds[3] = {0, 1, 0};
1038 print_in_bar(COLOR_MSG, "executing display command, hit any key to abort");
1039 outputf(COLOR_COMMAND, "%s", cmd);
1040 if (para_exec_cmdline_pid(&cmd_pid, cmd, fds) < 0)
1042 command_pipe = fds[1];
1043 return send_output();
1047 * shutdown curses and stat pipe before executing external commands
1049 static int external_cmd(char *cmd)
1051 int fds[3] = {-1, -1, -1};
1056 para_exec_cmdline_pid(&cmd_pid, cmd, fds);
1058 do_select(EXTERNAL_MODE);
1063 static void print_scroll_msg(void)
1065 unsigned lines_total, filled = ringbuffer_filled(bot_win_rb);
1066 int first_rbe = first_visible_rbe(&lines_total);
1067 print_in_bar(COLOR_MSG, "scrolled view: %d-%d/%d\n", filled - first_rbe,
1068 filled - scroll_position, ringbuffer_filled(bot_win_rb));
1071 static void com_page_down(void)
1074 int i = scroll_position;
1075 while (lines < bot.lines && --i > 0) {
1076 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
1079 lines += NUM_LINES(strlen(rbe->msg));
1082 scroll_position = i;
1087 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1090 static void com_page_up(void)
1093 int fvr = first_visible_rbe(&lines);
1094 if (fvr < 0 || fvr + 1 >= ringbuffer_filled(bot_win_rb)) {
1095 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1098 scroll_position = fvr + 1;
1099 for (; scroll_position > 0; scroll_position--) {
1100 fvr = first_visible_rbe(&lines);
1101 if (lines == bot.lines)
1108 static void com_scroll_down(void)
1110 struct rb_entry *rbe;
1113 if (!scroll_position) {
1114 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1118 rbe = ringbuffer_get(bot_win_rb, scroll_position);
1119 rbe_lines = NUM_LINES(rbe->len);
1120 wscrl(bot.win, rbe_lines);
1121 wmove(bot.win, bot.lines - rbe_lines, 0);
1122 wattron(bot.win, COLOR_PAIR(rbe->color));
1123 waddstr(bot.win, rbe->msg);
1128 static void com_scroll_up(void)
1130 struct rb_entry *rbe = NULL;
1132 int i, first_rbe, num_scroll;
1134 /* the entry that is going to vanish */
1135 rbe = ringbuffer_get(bot_win_rb, scroll_position);
1138 num_scroll = NUM_LINES(rbe->len);
1139 first_rbe = first_visible_rbe(&lines);
1140 if (first_rbe < 0 || (first_rbe == ringbuffer_filled(bot_win_rb) - 1))
1143 wscrl(bot.win, -num_scroll);
1144 i = draw_top_rbe(&lines);
1147 while (i > 0 && lines < num_scroll) {
1149 rbe = ringbuffer_get(bot_win_rb, --i);
1152 rbe_lines = NUM_LINES(rbe->len);
1154 // fprintf(stderr, "msg: %s\n", rbe->msg);
1155 wattron(bot.win, COLOR_PAIR(rbe->color));
1156 waddstr(bot.win, "\n");
1157 waddstr(bot.win, rbe->msg);
1166 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1169 static void com_ll_decr(void)
1171 if (conf.loglevel_arg <= DEBUG) {
1172 print_in_bar(COLOR_ERRMSG,
1173 "loglevel already at maximal verbosity\n");
1176 conf.loglevel_arg--;
1177 print_in_bar(COLOR_MSG, "loglevel set to %d\n", conf.loglevel_arg);
1180 static void com_ll_incr(void)
1182 if (conf.loglevel_arg >= EMERG) {
1183 print_in_bar(COLOR_ERRMSG,
1184 "loglevel already at miminal verbosity\n");
1187 conf.loglevel_arg++;
1188 print_in_bar(COLOR_MSG, "loglevel set to %d\n", conf.loglevel_arg);
1192 * reread configuration, terminate on errors
1194 static void com_reread_conf(void)
1196 char *cf =configfile_exists();
1197 struct gui_cmdline_parser_params params = {
1200 .check_required = 0,
1201 .check_ambiguity = 0
1205 PARA_WARNING_LOG("there is no configuration to read");
1208 PARA_INFO_LOG("rereading command line options and config file");
1209 gui_cmdline_parser(_argc, _argv, &conf);
1210 gui_cmdline_parser_config_file(cf, &conf, ¶ms);
1211 PARA_NOTICE_LOG("config file reloaded");
1212 if (check_key_map_args() < 0)
1213 finish(EXIT_FAILURE);
1216 static void com_help(void)
1220 for (i = 0; i < conf.key_map_given; ++i) {
1221 char *handler, *arg, *tmp = para_strdup(conf.key_map_arg[i]);
1222 const char *handler_text = "???", *desc = NULL;
1224 if (!split_key_map(tmp, &handler, &arg)) {
1230 handler_text = "internal";
1231 desc = command_list[find_cmd_byname(arg)].description;
1233 case 'x': handler_text = "external"; break;
1234 case 'd': handler_text = "display "; break;
1235 case 'p': handler_text = "para "; break;
1237 outputf(COLOR_MSG, "%s\t%s\t%s%s\t%s", tmp, handler_text, arg,
1238 strlen(arg) < 8? "\t" : "",
1242 for (i = 0; command_list[i].handler; i++) {
1243 struct gui_command gc = command_list[i];
1245 outputf(COLOR_MSG, "%s\tinternal\t%s\t%s%s", gc.key, gc.name,
1246 strlen(gc.name) < 8? "\t" : "",
1249 print_in_bar(COLOR_MSG, "try \"para_gui -h\" or \"para_client help\" "
1253 static void com_shrink_top_win(void)
1255 if (top.lines <= theme.top_lines_min) {
1256 PARA_WARNING_LOG("can not decrease top window");
1259 init_wins(top.lines - 1);
1262 print_in_bar(COLOR_MSG, "%s", "decreased top window");
1265 static void com_enlarge_top_win(void)
1267 if (bot.lines < 3) {
1268 PARA_WARNING_LOG("can not increase top window");
1271 init_wins(top.lines + 1);
1274 print_in_bar(COLOR_MSG, "increased top window");
1277 static void com_version(void)
1279 print_in_bar(COLOR_MSG, "para_gui " PACKAGE_VERSION " \""
1283 __noreturn static void com_quit(void)
1288 static void com_refresh(void)
1294 static void change_theme(int next)
1300 /* This seems to be needed twice, why? */
1303 PARA_NOTICE_LOG("new theme: %s", theme.name);
1306 static void com_next_theme(void)
1311 static void com_prev_theme(void)
1317 static void handle_command(int c)
1321 /* first check user's key bindings */
1322 for (i = 0; i < conf.key_map_given; ++i) {
1323 char tmp[MAXLINE], *handler, *arg;
1325 strcpy(tmp, conf.key_map_arg[i]);
1326 if (!split_key_map(tmp, &handler, &arg))
1328 if (!strcmp(tmp, km_keyname(c))) {
1329 if (*handler == 'd') {
1333 if (*handler == 'x') {
1337 if (*handler == 'p') {
1338 client_cmd_cmdline(arg);
1341 if (*handler == 'i') {
1342 int num = find_cmd_byname(arg);
1344 command_list[num].handler();
1349 /* not found, check internal key bindings */
1350 for (i = 0; command_list[i].handler; i++) {
1351 if (!strcmp(km_keyname(c), command_list[i].key)) {
1352 command_list[i].handler();
1356 print_in_bar(COLOR_ERRMSG, "key '%s' is not bound, press ? for help",
1360 int main(int argc, char *argv[])
1368 if (gui_cmdline_parser(argc, argv, &conf)) {
1369 fprintf(stderr, "parse error while reading command line\n");
1372 HANDLE_VERSION_FLAG("gui", conf);
1373 init_theme(0, &theme);
1374 top.lines = theme.top_lines_default;
1375 if (check_key_map_args() < 0) {
1376 fprintf(stderr, "invalid key map\n");
1379 cf = configfile_exists();
1380 if (!cf && conf.config_file_given) {
1381 fprintf(stderr, "can not read config file %s\n",
1382 conf.config_file_arg);
1386 struct gui_cmdline_parser_params params = {
1389 .check_required = 0,
1390 .check_ambiguity = 0
1392 gui_cmdline_parser_config_file(cf, &conf, ¶ms);
1394 if (check_key_map_args() < 0) {
1395 fprintf(stderr, "invalid key map in config file\n");
1398 setup_signal_handling();
1399 bot_win_rb = ringbuffer_new(RINGBUFFER_SIZE);
1400 initscr(); /* needed only once, always successful */
1405 ret = do_select(GETCH_MODE);
1408 print_in_bar(COLOR_MSG, " ");
1409 handle_command(ret);