2 * Copyright (C) 1998-2007 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file gui.c ncurses-based interface for paraslash */
10 #include <sys/types.h>
12 #include "gui.cmdline.h"
16 #include "ringbuffer.h"
17 #include "gui_common.h"
24 /** define the array of error lists needed by para_gui */
26 extern const char *status_item_list[NUM_STAT_ITEMS];
27 static char *stat_content[NUM_STAT_ITEMS];
29 #define STANDARD_STATUS_BAR "para_gui " PACKAGE_VERSION " (hit ? for help)"
31 static int signal_pipe;
33 static void finish(int sig);
35 static struct win_data {
41 } top, bot, sb, in, sep;
43 #define RINGBUFFER_SIZE 512
50 #define NUM_LINES(len) (1 + (len) / bot.cols)
52 static unsigned scroll_position;
54 static int cmd_died, curses_active;
57 static int command_pipe = -1;
58 static int audiod_pipe = -1;
59 static struct gui_args_info conf;
61 enum {GETCH_MODE, COMMAND_MODE, EXTERNAL_MODE};
64 #define COLOR_STATUSBAR 32
65 #define COLOR_COMMAND 33
66 #define COLOR_OUTPUT 34
68 #define COLOR_ERRMSG 36
69 #define COLOR_WELCOME 37
70 #define COLOR_SEPARATOR 38
77 const char *description;
78 void (*handler)(void);
84 char postfix[MAXLINE];
90 char content[MAXLINE];
93 static struct gui_theme theme;
98 static void com_help(void);
99 static void com_reread_conf(void);
100 static void com_enlarge_top_win(void);
101 static void com_shrink_top_win(void);
102 static void com_version(void);
103 static void com_quit(void);
104 static void com_refresh(void);
105 static void com_ll_incr(void);
106 static void com_ll_decr(void);
107 static void com_prev_theme(void);
108 static void com_next_theme(void);
109 static void com_scroll_up(void);
110 static void com_scroll_down(void);
111 static void com_page_up(void);
112 static void com_page_down(void);
114 struct gui_command command_list[] = {
118 .description = "print help",
122 .name = "enlarge_win",
123 .description = "enlarge the top window",
124 .handler = com_enlarge_top_win
127 .name = "shrink_win",
128 .description = "shrink the top window",
129 .handler = com_shrink_top_win
132 .name = "reread_conf",
133 .description = "reread configuration file",
134 .handler = com_reread_conf
138 .description = "exit para_gui",
143 .description = "redraw the screen",
144 .handler = com_refresh
147 .name = "next_theme",
148 .description = "switch to next theme",
149 .handler = com_next_theme
152 .name = "prev_theme",
153 .description = "switch to previous stream",
154 .handler = com_prev_theme
158 .description = "increase loglevel (decreases verbosity)",
159 .handler = com_ll_incr
163 .description = "decrease loglevel (increases verbosity)",
164 .handler = com_ll_decr
168 .description = "show the para_gui version",
169 .handler = com_version
173 .description = "scroll up one line",
174 .handler = com_scroll_up
177 .name = "scroll_down",
178 .description = "scroll down one line",
179 .handler = com_scroll_down
183 .description = "scroll up one page",
184 .handler = com_page_up
188 .description = "scroll down one page",
189 .handler = com_page_down
195 static int find_cmd_byname(char *name)
199 for (i = 0; command_list[i].handler; i++)
200 if (!strcmp(command_list[i].name, name))
205 /* taken from mutt */
206 static char *km_keyname(int c)
211 sprintf(buf, "<up>");
215 sprintf(buf, "<down>");
219 sprintf(buf, "<left>");
222 if (c == KEY_RIGHT) {
223 sprintf(buf, "<right>");
226 if (c == KEY_NPAGE) {
227 sprintf(buf, "<npage>");
230 if (c == KEY_PPAGE) {
231 sprintf(buf, "<ppage>");
234 if (c < 256 && c > -128 && iscntrl((unsigned char) c)) {
239 buf[1] = (c + '@') & 0x7f;
242 snprintf(buf, sizeof(buf), "\\%d%d%d", c >> 6,
243 (c >> 3) & 7, c & 7);
244 } else if (c >= KEY_F0 && c < KEY_F(256))
245 sprintf(buf, "<F%d>", c - KEY_F0);
247 snprintf(buf, sizeof(buf), "%c", (unsigned char) c);
249 snprintf(buf, sizeof(buf), "\\x%hx", (unsigned short) c);
253 static char *configfile_exists(void)
255 static char *config_file;
258 if (!conf.config_file_given) {
260 char *home = para_homedir();
261 config_file = make_message("%s/.paraslash/gui.conf",
267 tmp = conf.config_file_arg;
268 return file_exists(tmp)? tmp: NULL;
272 * print num spaces to curses window
274 static void add_spaces(WINDOW* win, unsigned int num)
283 * print aligned string to curses window. This function always prints
286 static int align_str(WINDOW* win, const char *string, unsigned int len,
289 int num; /* of spaces */
294 num = len - strlen(string);
295 str = para_strdup(string);
302 add_spaces(win, num);
303 } else if (align == RIGHT) {
304 add_spaces(win, num);
307 add_spaces(win, num / 2);
308 waddstr(win, str[0]? str: "");
309 add_spaces(win, num - num / 2);
315 __printf_2_3 static void print_in_bar(int color, const char *fmt,...)
321 wattron(in.win, COLOR_PAIR(color));
322 PARA_VSPRINTF(fmt, msg);
324 align_str(in.win, msg, sb.cols, LEFT);
330 * update the status bar
332 static void print_status_bar(void)
337 align_str(sb.win, STANDARD_STATUS_BAR, sb.cols, CENTER);
342 * get the number of the oldest rbe that is (partially) visible. On return,
343 * lines contains the sum of the number of lines of all visable entries. If the
344 * first one is only partially visible, lines is greater than bot.lines.
346 static int first_visible_rbe(unsigned *lines)
350 for (i = scroll_position; i < RINGBUFFER_SIZE; i++) {
351 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
355 // fprintf(stderr, "found: %s\n", rbe->msg);
356 rbe_lines = NUM_LINES(rbe->len);
357 if (rbe_lines > bot.lines)
360 if (*lines >= bot.lines)
363 return RINGBUFFER_SIZE - 1;
366 static int draw_top_rbe(unsigned *lines)
369 int offset, fvr = first_visible_rbe(lines);
370 struct rb_entry *rbe;
374 wmove(bot.win, 0, 0);
375 rbe = ringbuffer_get(bot_win_rb, fvr);
378 /* first rbe might be only partially visible */
379 offset = (*lines - bot.lines) * bot.cols;
380 len = strlen(rbe->msg);
381 if (offset < 0 || len < offset)
383 wattron(bot.win, COLOR_PAIR(rbe->color));
384 waddstr(bot.win, rbe->msg + offset);
385 *lines = NUM_LINES(len - offset);
389 static void redraw_bot_win(void)
394 wmove(bot.win, 0, 0);
396 i = draw_top_rbe(&lines);
399 while (i > 0 && lines < bot.lines) {
400 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, --i);
403 waddstr(bot.win, "\n");
406 lines += NUM_LINES(rbe->len);
407 wattron(bot.win, COLOR_PAIR(rbe->color));
408 waddstr(bot.win, "\n");
409 waddstr(bot.win, rbe->msg);
415 static void rb_add_entry(int color, char *msg)
417 struct rb_entry *old, *new = para_malloc(sizeof(struct rb_entry));
420 new->len = strlen(msg);
422 old = ringbuffer_add(bot_win_rb, new);
423 // fprintf(stderr, "added: %s\n", new->msg);
428 if (scroll_position) {
429 /* discard current scrolling, like xterm does */
434 wattron(bot.win, COLOR_PAIR(color));
435 getyx(bot.win, y, x);
437 waddstr(bot.win, "\n");
438 waddstr(bot.win, msg);
442 * print formated output to bot win and refresh
444 __printf_2_3 static void outputf(int color, const char* fmt,...)
450 PARA_VSPRINTF(fmt, msg);
451 rb_add_entry(color, msg);
455 static int add_output_line(char *line, __a_unused void *data)
459 rb_add_entry(COLOR_OUTPUT, para_strdup(line));
463 void para_log(int ll, const char *fmt,...)
468 if (ll < conf.loglevel_arg || !curses_active)
477 color = COLOR_ERRMSG;
479 PARA_VSPRINTF(fmt, msg);
481 rb_add_entry(color, msg);
485 static void setup_signal_handling(void)
487 signal_pipe = para_signal_init();
488 para_install_sighandler(SIGINT);
489 para_install_sighandler(SIGTERM);
490 para_install_sighandler(SIGCHLD);
491 para_install_sighandler(SIGWINCH);
492 para_install_sighandler(SIGUSR1);
493 // signal(SIGPIPE, SIG_IGN);
494 signal(SIGHUP, SIG_IGN);
497 static void do_exit(int ret)
499 signal(SIGTERM, SIG_IGN);
504 static void shutdown_curses(void)
513 static void finish(int ret)
520 * exit curses and print given message to stdout/stderr
522 __printf_2_3 static void msg_n_exit(int ret, const char* fmt, ...)
525 FILE *outfd = ret? stderr: stdout;
529 vfprintf(outfd, fmt, argp);
534 static void print_welcome(void)
536 int ll = conf.loglevel_arg;
539 outputf(COLOR_WELCOME, "Welcome to para_gui " PACKAGE_VERSION
540 " \"" CODENAME "\". Theme: %s", theme.name);
547 static void init_wins(int top_lines)
551 top.lines = top_lines;
556 bot.lines = LINES - top.lines - 3;
558 bot.begy = top.lines + 1;
573 sep.begy = top.lines;
576 assume_default_colors(theme.default_fg, theme.default_bg);
578 mvwin(top.win, top.begy, top.begx);
579 wresize(top.win, top.lines, top.cols);
581 mvwin(sb.win, sb.begy, sb.begx);
582 wresize(sb.win, sb.lines, sb.cols);
584 mvwin(sep.win, sep.begy, sep.begx);
585 wresize(sep.win, sep.lines, sep.cols);
587 mvwin(bot.win, bot.begy, bot.begx);
588 wresize(bot.win, bot.lines, bot.cols);
590 mvwin(in.win, in.begy, in.begx);
591 wresize(in.win, in.lines, in.cols);
593 sep.win = newwin(sep.lines, sep.cols, sep.begy, sep.begx);
594 top.win = newwin(top.lines, top.cols, top.begy, top.begx);
595 bot.win = newwin(bot.lines, bot.cols, bot.begy, bot.begx);
596 sb.win = newwin(sb.lines, sb.cols, sb.begy, sb.begx);
597 in.win = newwin(in.lines, in.cols, in.begy, in.begx);
598 if (!top.win || !bot.win || !sb.win || !in.win || !sep.win)
599 msg_n_exit(1, "Error: Cannot create curses windows\n");
603 scrollok(bot.win, 1);
604 wattron(sb.win, COLOR_PAIR(COLOR_STATUSBAR));
605 wattron(sep.win, COLOR_PAIR(COLOR_SEPARATOR));
606 wattron(bot.win, COLOR_PAIR(COLOR_BOT));
607 wattron(top.win, COLOR_PAIR(COLOR_TOP));
619 wmove(sep.win, 0, 0);
620 for (i = 1; i <= COLS; i++)
621 waddstr(sep.win, theme.sep_str);
624 wnoutrefresh(top.win);
625 wnoutrefresh(bot.win);
626 //wnoutrefresh(sb.win);
628 wnoutrefresh(in.win);
629 wnoutrefresh(sep.win);
633 static void check_geometry(void)
635 if (LINES < theme.lines_min || COLS < theme.cols_min)
636 msg_n_exit(EXIT_FAILURE, "Error: Terminal (%dx%d) too small"
637 " (need at least %dx%d)\n", COLS, LINES,
638 theme.cols_min, theme.lines_min);
642 * Print stat item #i to curses window
644 static void print_stat_item(int i)
647 struct stat_item_data d = theme.data[i];
648 char *c = stat_content[i];
650 if (!curses_active || !d.len || !c)
652 tmp = make_message("%s%s%s", d.prefix, c, d.postfix);
653 // PARA_DEBUG_LOG("%s: read: %s\n", __func__, tmp);
654 wmove(top.win, d.y * top.lines / 100, d.x * COLS / 100);
656 wattron(top.win, COLOR_PAIR(i + 1));
657 align_str(top.win, tmp, d.len * COLS / 100, d.align);
662 static void print_all_items(void)
668 for (i = 0; i < NUM_STAT_ITEMS; i++)
671 static void clear_all_items(void)
675 for (i = 0; i < NUM_STAT_ITEMS; i++) {
676 free(stat_content[i]);
677 stat_content[i] = para_strdup("");
681 static void init_colors(void)
685 msg_n_exit(EXIT_FAILURE, "Error: No color term\n");
687 for (i = 0; i < NUM_STAT_ITEMS; i++)
688 if (theme.data[i].len)
689 init_pair(i + 1, theme.data[i].fg, theme.data[i].bg);
690 init_pair(COLOR_STATUSBAR, theme.sb_fg, theme.sb_bg);
691 init_pair(COLOR_COMMAND, theme.cmd_fg, theme.cmd_bg);
692 init_pair(COLOR_OUTPUT, theme.output_fg, theme.output_bg);
693 init_pair(COLOR_MSG, theme.msg_fg, theme.msg_bg);
694 init_pair(COLOR_ERRMSG, theme.err_msg_fg, theme.err_msg_bg);
695 init_pair(COLOR_WELCOME, theme.welcome_fg, theme.welcome_bg);
696 init_pair(COLOR_SEPARATOR, theme.sep_fg, theme.sep_bg);
697 init_pair(COLOR_TOP, theme.default_fg, theme.default_bg);
698 init_pair(COLOR_BOT, theme.default_fg, theme.default_bg);
702 * (re-)initialize the curses library FIXME: Error checking
704 static void init_curses(void)
707 if (top.win && refresh() == ERR) { /* refesh is really needed */
708 msg_n_exit(EXIT_FAILURE, "refresh() failed\n");
711 curs_set(0); /* make cursor invisible, ignore errors */
712 // if (noraw() == ERR);
713 // msg_n_exit(EXIT_FAILURE, "can not place terminal out of "
715 nonl(); /* tell curses not to do NL->CR/NL on output */
716 noecho(); /* don't echo input */
717 cbreak(); /* take input chars one at a time, no wait for \n */
721 init_wins(theme.top_lines_default);
723 noecho(); /* don't echo input */
727 static void check_sigchld(void)
732 ret = para_reap_child(&pid);
735 if (pid == cmd_pid) {
739 goto reap_next_child;
743 * print status line if line starts with known command.
745 static int check_stat_line(char *line, __a_unused void *data)
749 // PARA_INFO_LOG("%s: checking: %s\n", __func__, line);
750 i = stat_line_valid(line);
752 line += strlen(status_item_list[i]) + 2;
753 free(stat_content[i]);
754 stat_content[i] = para_strdup(line);
761 * This sucker modifies its first argument. *handler and *arg are
762 * pointers to 0-terminated strings (inside line). Crap.
764 static int split_key_map(char *line, char **handler, char **arg)
766 if (!(*handler = strchr(line + 1, ':')))
770 if (!(*arg = strchr(*handler, ':')))
779 static int check_key_map_args(void)
783 char *tmp = NULL, *handler, *arg;
785 for (i = 0; i < conf.key_map_given; ++i) {
786 s = conf.key_map_arg[i];
790 tmp = para_strdup(s);
791 if (!split_key_map(tmp, &handler, &arg))
793 if (strlen(handler) != 1)
802 if (find_cmd_byname(arg) < 0)
812 * React to various signal-related events
814 static void handle_signal(int sig)
818 msg_n_exit(EXIT_FAILURE,
819 "only the good die young (caught SIGTERM))\n");
829 PARA_WARNING_LOG("%s", "caught SIGINT, reset");
830 /* Nothing to do. SIGINT killed our child, para_client stat.
831 * This get noticed by do_select which resets everything
835 PARA_NOTICE_LOG("%s", "got SIGUSR1, rereading configuration");
844 static int open_audiod_pipe(void)
852 return para_open_audiod_pipe(conf.stat_cmd_arg);
856 * This is the core select loop. Besides the (internal) signal
857 * pipe, the following other fds are checked according to the mode:
859 * GETCH_MODE: check stdin, return when key is pressed
861 * COMMAND_MODE: check command_pipe and stdin. Return when a screen full
862 * of output has been read or when other end has closed command pipe or
863 * when any key is pressed.
865 * EXTERNAL_MODE: Check only signal pipe. Used when an external command
866 * is running. During that thime curses is disabled. Returns when
869 static int do_select(int mode)
873 int max_fileno, cp_numread = 1;
874 char command_buf[4096] = "";
875 int cbo = 0; /* command buf offset */
878 tv.tv_sec = conf.timeout_arg / 1000;
879 tv.tv_usec = (conf.timeout_arg % 1000) * 1000;
880 // ret = refresh_status();
885 audiod_pipe = open_audiod_pipe();
886 if (audiod_pipe >= 0)
887 para_fd_set(audiod_pipe, &rfds, &max_fileno);
889 para_fd_set(signal_pipe, &rfds, &max_fileno);
890 /* command pipe only for COMMAND_MODE */
891 if (command_pipe >= 0 && mode == COMMAND_MODE)
892 para_fd_set(command_pipe, &rfds, &max_fileno);
893 ret = para_select(max_fileno + 1, &rfds, NULL, &tv);
895 goto check_return; /* skip fd checks */
897 if (FD_ISSET(signal_pipe, &rfds)) {
898 int sig_nr = para_next_signal();
900 handle_signal(sig_nr);
902 /* read command pipe if ready */
903 if (command_pipe >= 0 && mode == COMMAND_MODE &&
904 FD_ISSET(command_pipe, &rfds)) {
905 cp_numread = read(command_pipe, command_buf + cbo,
906 sizeof(command_buf) - 1 - cbo);
911 PARA_ERROR_LOG("read error (%d)", cp_numread);
916 if (audiod_pipe >= 0 && FD_ISSET(audiod_pipe, &rfds))
917 if (read_audiod_pipe(audiod_pipe, check_stat_line) <= 0) {
921 free(stat_content[SI_BASENAME]);
922 stat_content[SI_BASENAME] =
923 para_strdup("audiod not running!?");
929 if (cp_numread <= 0 && !cbo) /* command complete */
932 cbo = for_each_line(command_buf, cbo,
933 &add_output_line, NULL);
937 ret = wgetch(top.win);
938 if (ret != ERR && ret != KEY_RESIZE) {
944 kill(cmd_pid, SIGTERM);
949 ret = wgetch(top.win);
950 if (ret != ERR && ret != KEY_RESIZE)
963 * read from command pipe and print data to bot window
965 static int send_output(void)
967 if (command_pipe < 0)
969 if (do_select(COMMAND_MODE) >= 0)
970 PARA_INFO_LOG("%s", "command complete");
972 PARA_NOTICE_LOG("%s", "command aborted");
973 print_in_bar(COLOR_MSG, " ");
977 static int client_cmd_cmdline(char *cmd)
979 int ret, fds[3] = {0, 1, 0};
980 char *c = make_message(BINDIR "/para_client %s", cmd);
982 outputf(COLOR_COMMAND, "%s", c);
983 print_in_bar(COLOR_MSG, "executing client command, hit any key to abort\n");
984 ret = para_exec_cmdline_pid(&cmd_pid, c, fds);
988 command_pipe = fds[1];
989 mark_fd_nonblock(command_pipe);
990 return send_output();
994 * exec command and print output to bot win
996 static int display_cmd(char *cmd)
998 int fds[3] = {0, 1, 0};
1000 print_in_bar(COLOR_MSG, "executing display command, hit any key to abort");
1001 outputf(COLOR_COMMAND, "%s", cmd);
1002 if (para_exec_cmdline_pid(&cmd_pid, cmd, fds) < 0)
1004 command_pipe = fds[1];
1005 mark_fd_nonblock(command_pipe);
1006 return send_output();
1010 * shutdown curses and stat pipe before executing external commands
1012 static int external_cmd(char *cmd)
1014 int fds[3] = {-1, -1, -1};
1019 para_exec_cmdline_pid(&cmd_pid, cmd, fds);
1021 do_select(EXTERNAL_MODE);
1026 static void print_scroll_msg(void)
1028 unsigned lines_total, filled = ringbuffer_filled(bot_win_rb);
1029 int first_rbe = first_visible_rbe(&lines_total);
1030 print_in_bar(COLOR_MSG, "scrolled view: %d-%d/%d\n", filled - first_rbe,
1031 filled - scroll_position, ringbuffer_filled(bot_win_rb));
1034 static void com_page_down(void)
1037 int i = scroll_position;
1038 while (lines < bot.lines && --i > 0) {
1039 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
1042 lines += NUM_LINES(strlen(rbe->msg));
1045 scroll_position = i;
1050 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1053 static void com_page_up(void)
1056 int fvr = first_visible_rbe(&lines);
1057 if (fvr < 0 || fvr + 1 >= ringbuffer_filled(bot_win_rb)) {
1058 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1061 scroll_position = fvr + 1;
1062 for (; scroll_position > 0; scroll_position--) {
1063 fvr = first_visible_rbe(&lines);
1064 if (lines == bot.lines)
1071 static void com_scroll_down(void)
1073 struct rb_entry *rbe;
1076 if (!scroll_position) {
1077 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1081 rbe = ringbuffer_get(bot_win_rb, scroll_position);
1082 rbe_lines = NUM_LINES(rbe->len);
1083 wscrl(bot.win, rbe_lines);
1084 wmove(bot.win, bot.lines - rbe_lines, 0);
1085 wattron(bot.win, COLOR_PAIR(rbe->color));
1086 waddstr(bot.win, rbe->msg);
1091 static void com_scroll_up(void)
1093 struct rb_entry *rbe = NULL;
1095 int i, first_rbe, num_scroll;
1097 /* the entry that is going to vanish */
1098 rbe = ringbuffer_get(bot_win_rb, scroll_position);
1101 num_scroll = NUM_LINES(rbe->len);
1102 first_rbe = first_visible_rbe(&lines);
1103 if (first_rbe < 0 || (first_rbe == ringbuffer_filled(bot_win_rb) - 1))
1106 wscrl(bot.win, -num_scroll);
1107 i = draw_top_rbe(&lines);
1110 while (i > 0 && lines < num_scroll) {
1112 rbe = ringbuffer_get(bot_win_rb, --i);
1115 rbe_lines = NUM_LINES(rbe->len);
1117 // fprintf(stderr, "msg: %s\n", rbe->msg);
1118 wattron(bot.win, COLOR_PAIR(rbe->color));
1119 waddstr(bot.win, "\n");
1120 waddstr(bot.win, rbe->msg);
1129 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1132 static void com_ll_decr(void)
1134 if (conf.loglevel_arg <= DEBUG) {
1135 print_in_bar(COLOR_ERRMSG,
1136 "loglevel already at maximal verbosity\n");
1139 conf.loglevel_arg--;
1140 print_in_bar(COLOR_MSG, "loglevel set to %d\n", conf.loglevel_arg);
1143 static void com_ll_incr(void)
1145 if (conf.loglevel_arg >= EMERG) {
1146 print_in_bar(COLOR_ERRMSG,
1147 "loglevel already at miminal verbosity\n");
1150 conf.loglevel_arg++;
1151 print_in_bar(COLOR_MSG, "loglevel set to %d\n", conf.loglevel_arg);
1155 * reread configuration, terminate on errors
1157 static void com_reread_conf(void)
1159 char *cf =configfile_exists();
1160 struct gui_cmdline_parser_params params = {
1163 .check_required = 0,
1164 .check_ambiguity = 0
1168 PARA_WARNING_LOG("%s", "there is no configuration to read");
1171 PARA_INFO_LOG("%s", "rereading command line options and config file");
1172 gui_cmdline_parser(_argc, _argv, &conf);
1173 gui_cmdline_parser_config_file(cf, &conf, ¶ms);
1174 PARA_NOTICE_LOG("%s", "config file reloaded");
1175 if (check_key_map_args() < 0)
1176 finish(EXIT_FAILURE);
1179 static void com_help(void)
1183 for (i = 0; i < conf.key_map_given; ++i) {
1184 char *handler, *arg, *tmp = para_strdup(conf.key_map_arg[i]);
1185 const char *handler_text = "???", *desc = NULL;
1187 if (!split_key_map(tmp, &handler, &arg)) {
1193 handler_text = "internal";
1194 desc = command_list[find_cmd_byname(arg)].description;
1196 case 'x': handler_text = "external"; break;
1197 case 'd': handler_text = "display "; break;
1198 case 'p': handler_text = "para "; break;
1200 outputf(COLOR_MSG, "%s\t%s\t%s%s\t%s", tmp, handler_text, arg,
1201 strlen(arg) < 8? "\t" : "",
1205 for (i = 0; command_list[i].handler; i++) {
1206 struct gui_command gc = command_list[i];
1208 outputf(COLOR_MSG, "%s\tinternal\t%s\t%s%s", gc.key, gc.name,
1209 strlen(gc.name) < 8? "\t" : "",
1212 print_in_bar(COLOR_MSG, "try \"para_gui -h\" or \"para_client help\" "
1216 static void com_shrink_top_win(void)
1218 if (top.lines <= theme.top_lines_min) {
1219 PARA_WARNING_LOG("%s", "can not decrease top window");
1222 init_wins(top.lines - 1);
1225 print_in_bar(COLOR_MSG, "%s", "decreased top window");
1228 static void com_enlarge_top_win(void)
1230 if (bot.lines < 3) {
1231 PARA_WARNING_LOG("%s", "can not increase top window");
1234 init_wins(top.lines + 1);
1237 print_in_bar(COLOR_MSG, "increased top window");
1240 static void com_version(void)
1242 print_in_bar(COLOR_MSG, "para_gui " PACKAGE_VERSION " \""
1246 static void com_quit(void)
1251 static void com_refresh(void)
1257 static void change_theme(int next)
1263 /* This seems to be needed twice, why? */
1266 PARA_NOTICE_LOG("new theme: %s", theme.name);
1269 static void com_next_theme(void)
1274 static void com_prev_theme(void)
1280 static void handle_command(int c)
1284 /* first check user's key bindings */
1285 for (i = 0; i < conf.key_map_given; ++i) {
1286 char tmp[MAXLINE], *handler, *arg;
1288 strcpy(tmp, conf.key_map_arg[i]);
1289 if (!split_key_map(tmp, &handler, &arg))
1291 if (!strcmp(tmp, km_keyname(c))) {
1292 if (*handler == 'd') {
1296 if (*handler == 'x') {
1300 if (*handler == 'p') {
1301 client_cmd_cmdline(arg);
1304 if (*handler == 'i') {
1305 int num = find_cmd_byname(arg);
1307 command_list[num].handler();
1312 /* not found, check internal key bindings */
1313 for (i = 0; command_list[i].handler; i++) {
1314 if (!strcmp(km_keyname(c), command_list[i].key)) {
1315 command_list[i].handler();
1319 print_in_bar(COLOR_ERRMSG, "key '%s' is not bound, press ? for help",
1323 int main(int argc, char *argv[])
1331 if (gui_cmdline_parser(argc, argv, &conf)) {
1332 fprintf(stderr, "parse error while reading command line\n");
1335 HANDLE_VERSION_FLAG("gui", conf);
1336 init_theme(0, &theme);
1337 top.lines = theme.top_lines_default;
1338 if (check_key_map_args() < 0) {
1339 fprintf(stderr, "invalid key map\n");
1342 cf = configfile_exists();
1343 if (!cf && conf.config_file_given) {
1344 fprintf(stderr, "can not read config file %s\n",
1345 conf.config_file_arg);
1349 struct gui_cmdline_parser_params params = {
1352 .check_required = 0,
1353 .check_ambiguity = 0
1355 gui_cmdline_parser_config_file(cf, &conf, ¶ms);
1357 if (check_key_map_args() < 0) {
1358 fprintf(stderr, "invalid key map in config file\n");
1361 setup_signal_handling();
1362 bot_win_rb = ringbuffer_new(RINGBUFFER_SIZE);
1363 initscr(); /* needed only once, always successful */
1368 ret = do_select(GETCH_MODE);
1371 print_in_bar(COLOR_MSG, " ");
1372 handle_command(ret);