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 */
9 #include "gui.cmdline.h"
13 #include "ringbuffer.h"
14 #include "gui_common.h"
21 /** define the array of error lists needed by para_gui */
23 extern const char *status_item_list[NUM_STAT_ITEMS];
24 static char *stat_content[NUM_STAT_ITEMS];
26 #define STANDARD_STATUS_BAR "para_gui " PACKAGE_VERSION " (hit ? for help)"
28 static int signal_pipe;
30 static void finish(int sig);
32 static struct win_data {
38 } top, bot, sb, in, sep;
40 #define RINGBUFFER_SIZE 512
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 32
62 #define COLOR_COMMAND 33
63 #define COLOR_OUTPUT 34
65 #define COLOR_ERRMSG 36
66 #define COLOR_WELCOME 37
67 #define COLOR_SEPARATOR 38
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 find_cmd_byname(char *name)
196 for (i = 0; command_list[i].handler; i++)
197 if (!strcmp(command_list[i].name, name))
202 /* taken from mutt */
203 static char *km_keyname(int c)
208 sprintf(buf, "<up>");
212 sprintf(buf, "<down>");
216 sprintf(buf, "<left>");
219 if (c == KEY_RIGHT) {
220 sprintf(buf, "<right>");
223 if (c == KEY_NPAGE) {
224 sprintf(buf, "<npage>");
227 if (c == KEY_PPAGE) {
228 sprintf(buf, "<ppage>");
231 if (c < 256 && c > -128 && iscntrl((unsigned char) c)) {
236 buf[1] = (c + '@') & 0x7f;
239 snprintf(buf, sizeof(buf), "\\%d%d%d", c >> 6,
240 (c >> 3) & 7, c & 7);
241 } else if (c >= KEY_F0 && c < KEY_F(256))
242 sprintf(buf, "<F%d>", c - KEY_F0);
244 snprintf(buf, sizeof(buf), "%c", (unsigned char) c);
246 snprintf(buf, sizeof(buf), "\\x%hx", (unsigned short) c);
250 static char *configfile_exists(void)
252 static char *config_file;
255 if (!conf.config_file_given) {
257 char *home = para_homedir();
258 config_file = make_message("%s/.paraslash/gui.conf",
264 tmp = conf.config_file_arg;
265 return file_exists(tmp)? tmp: NULL;
269 * print num spaces to curses window
271 static void add_spaces(WINDOW* win, unsigned int num)
280 * print aligned string to curses window. This function always prints
283 static int align_str(WINDOW* win, const char *string, unsigned int len,
286 int num; /* of spaces */
291 num = len - strlen(string);
292 str = para_strdup(string);
299 add_spaces(win, num);
300 } else if (align == RIGHT) {
301 add_spaces(win, num);
304 add_spaces(win, num / 2);
305 waddstr(win, str[0]? str: "");
306 add_spaces(win, num - num / 2);
312 __printf_2_3 static void print_in_bar(int color, const char *fmt,...)
318 wattron(in.win, COLOR_PAIR(color));
319 PARA_VSPRINTF(fmt, msg);
321 align_str(in.win, msg, sb.cols, LEFT);
327 * update the status bar
329 static void print_status_bar(void)
334 align_str(sb.win, STANDARD_STATUS_BAR, sb.cols, CENTER);
339 * get the number of the oldest rbe that is (partially) visible. On return,
340 * lines contains the sum of the number of lines of all visable entries. If the
341 * first one is only partially visible, lines is greater than bot.lines.
343 static int first_visible_rbe(unsigned *lines)
347 for (i = scroll_position; i < RINGBUFFER_SIZE; i++) {
348 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
352 // fprintf(stderr, "found: %s\n", rbe->msg);
353 rbe_lines = NUM_LINES(rbe->len);
354 if (rbe_lines > bot.lines)
357 if (*lines >= bot.lines)
360 return RINGBUFFER_SIZE - 1;
363 static int draw_top_rbe(unsigned *lines)
366 int offset, fvr = first_visible_rbe(lines);
367 struct rb_entry *rbe;
371 wmove(bot.win, 0, 0);
372 rbe = ringbuffer_get(bot_win_rb, fvr);
375 /* first rbe might be only partially visible */
376 offset = (*lines - bot.lines) * bot.cols;
377 len = strlen(rbe->msg);
378 if (offset < 0 || len < offset)
380 wattron(bot.win, COLOR_PAIR(rbe->color));
381 waddstr(bot.win, rbe->msg + offset);
382 *lines = NUM_LINES(len - offset);
386 static void redraw_bot_win(void)
391 wmove(bot.win, 0, 0);
393 i = draw_top_rbe(&lines);
396 while (i > 0 && lines < bot.lines) {
397 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, --i);
400 waddstr(bot.win, "\n");
403 lines += NUM_LINES(rbe->len);
404 wattron(bot.win, COLOR_PAIR(rbe->color));
405 waddstr(bot.win, "\n");
406 waddstr(bot.win, rbe->msg);
412 static void rb_add_entry(int color, char *msg)
414 struct rb_entry *old, *new = para_malloc(sizeof(struct rb_entry));
417 new->len = strlen(msg);
419 old = ringbuffer_add(bot_win_rb, new);
420 // fprintf(stderr, "added: %s\n", new->msg);
425 if (scroll_position) {
426 /* discard current scrolling, like xterm does */
431 wattron(bot.win, COLOR_PAIR(color));
432 getyx(bot.win, y, x);
434 waddstr(bot.win, "\n");
435 waddstr(bot.win, msg);
439 * print formated output to bot win and refresh
441 __printf_2_3 static void outputf(int color, const char* fmt,...)
447 PARA_VSPRINTF(fmt, msg);
448 rb_add_entry(color, msg);
452 static int add_output_line(char *line, __a_unused void *data)
456 rb_add_entry(COLOR_OUTPUT, para_strdup(line));
460 void para_log(int ll, const char *fmt,...)
465 if (ll < conf.loglevel_arg || !curses_active)
474 color = COLOR_ERRMSG;
476 PARA_VSPRINTF(fmt, msg);
478 rb_add_entry(color, msg);
482 static void setup_signal_handling(void)
484 signal_pipe = para_signal_init();
485 para_install_sighandler(SIGINT);
486 para_install_sighandler(SIGTERM);
487 para_install_sighandler(SIGCHLD);
488 para_install_sighandler(SIGWINCH);
489 para_install_sighandler(SIGUSR1);
490 signal(SIGPIPE, SIG_IGN);
491 signal(SIGHUP, SIG_IGN);
494 static void do_exit(int ret)
496 signal(SIGTERM, SIG_IGN);
501 static void shutdown_curses(void)
510 static void finish(int ret)
517 * exit curses and print given message to stdout/stderr
519 __printf_2_3 static void msg_n_exit(int ret, const char* fmt, ...)
522 FILE *outfd = ret? stderr: stdout;
526 vfprintf(outfd, fmt, argp);
531 static void print_welcome(void)
533 int ll = conf.loglevel_arg;
536 outputf(COLOR_WELCOME, "Welcome to para_gui " PACKAGE_VERSION
537 " \"" CODENAME "\". Theme: %s", theme.name);
544 static void init_wins(int top_lines)
548 top.lines = top_lines;
553 bot.lines = LINES - top.lines - 3;
555 bot.begy = top.lines + 1;
570 sep.begy = top.lines;
573 assume_default_colors(theme.default_fg, theme.default_bg);
575 mvwin(top.win, top.begy, top.begx);
576 wresize(top.win, top.lines, top.cols);
578 mvwin(sb.win, sb.begy, sb.begx);
579 wresize(sb.win, sb.lines, sb.cols);
581 mvwin(sep.win, sep.begy, sep.begx);
582 wresize(sep.win, sep.lines, sep.cols);
584 mvwin(bot.win, bot.begy, bot.begx);
585 wresize(bot.win, bot.lines, bot.cols);
587 mvwin(in.win, in.begy, in.begx);
588 wresize(in.win, in.lines, in.cols);
590 sep.win = newwin(sep.lines, sep.cols, sep.begy, sep.begx);
591 top.win = newwin(top.lines, top.cols, top.begy, top.begx);
592 bot.win = newwin(bot.lines, bot.cols, bot.begy, bot.begx);
593 sb.win = newwin(sb.lines, sb.cols, sb.begy, sb.begx);
594 in.win = newwin(in.lines, in.cols, in.begy, in.begx);
595 if (!top.win || !bot.win || !sb.win || !in.win || !sep.win)
596 msg_n_exit(1, "Error: Cannot create curses windows\n");
600 scrollok(bot.win, 1);
601 wattron(sb.win, COLOR_PAIR(COLOR_STATUSBAR));
602 wattron(sep.win, COLOR_PAIR(COLOR_SEPARATOR));
603 wattron(bot.win, COLOR_PAIR(COLOR_BOT));
604 wattron(top.win, COLOR_PAIR(COLOR_TOP));
616 wmove(sep.win, 0, 0);
617 for (i = 1; i <= COLS; i++)
618 waddstr(sep.win, theme.sep_str);
621 wnoutrefresh(top.win);
622 wnoutrefresh(bot.win);
623 //wnoutrefresh(sb.win);
625 wnoutrefresh(in.win);
626 wnoutrefresh(sep.win);
630 static void check_geometry(void)
632 if (LINES < theme.lines_min || COLS < theme.cols_min)
633 msg_n_exit(EXIT_FAILURE, "Error: Terminal (%dx%d) too small"
634 " (need at least %dx%d)\n", COLS, LINES,
635 theme.cols_min, theme.lines_min);
639 * Print stat item #i to curses window
641 static void print_stat_item(int i)
644 struct stat_item_data d = theme.data[i];
645 char *c = stat_content[i];
647 if (!curses_active || !d.len || !c)
649 tmp = make_message("%s%s%s", d.prefix, c, d.postfix);
650 // PARA_DEBUG_LOG("%s: read: %s\n", __func__, tmp);
651 wmove(top.win, d.y * top.lines / 100, d.x * COLS / 100);
653 wattron(top.win, COLOR_PAIR(i + 1));
654 align_str(top.win, tmp, d.len * COLS / 100, d.align);
659 static void print_all_items(void)
665 for (i = 0; i < NUM_STAT_ITEMS; i++)
668 static void clear_all_items(void)
672 for (i = 0; i < NUM_STAT_ITEMS; i++) {
673 free(stat_content[i]);
674 stat_content[i] = para_strdup("");
678 static void init_colors(void)
682 msg_n_exit(EXIT_FAILURE, "Error: No color term\n");
684 for (i = 0; i < NUM_STAT_ITEMS; i++)
685 if (theme.data[i].len)
686 init_pair(i + 1, theme.data[i].fg, theme.data[i].bg);
687 init_pair(COLOR_STATUSBAR, theme.sb_fg, theme.sb_bg);
688 init_pair(COLOR_COMMAND, theme.cmd_fg, theme.cmd_bg);
689 init_pair(COLOR_OUTPUT, theme.output_fg, theme.output_bg);
690 init_pair(COLOR_MSG, theme.msg_fg, theme.msg_bg);
691 init_pair(COLOR_ERRMSG, theme.err_msg_fg, theme.err_msg_bg);
692 init_pair(COLOR_WELCOME, theme.welcome_fg, theme.welcome_bg);
693 init_pair(COLOR_SEPARATOR, theme.sep_fg, theme.sep_bg);
694 init_pair(COLOR_TOP, theme.default_fg, theme.default_bg);
695 init_pair(COLOR_BOT, theme.default_fg, theme.default_bg);
699 * (re-)initialize the curses library FIXME: Error checking
701 static void init_curses(void)
704 if (top.win && refresh() == ERR) { /* refesh is really needed */
705 msg_n_exit(EXIT_FAILURE, "refresh() failed\n");
708 curs_set(0); /* make cursor invisible, ignore errors */
709 // if (noraw() == ERR);
710 // msg_n_exit(EXIT_FAILURE, "can not place terminal out of "
712 nonl(); /* tell curses not to do NL->CR/NL on output */
713 noecho(); /* don't echo input */
714 cbreak(); /* take input chars one at a time, no wait for \n */
718 init_wins(theme.top_lines_default);
720 noecho(); /* don't echo input */
724 static void check_sigchld(void)
728 pid = para_reap_child();
731 if (pid == cmd_pid) {
735 goto reap_next_child;
739 * print status line if line starts with known command.
741 static int check_stat_line(char *line, __a_unused void *data)
745 // PARA_INFO_LOG("%s: checking: %s\n", __func__, line);
746 i = stat_line_valid(line);
748 line += strlen(status_item_list[i]) + 1;
749 free(stat_content[i]);
750 stat_content[i] = para_strdup(line);
757 * This sucker modifies its first argument. *handler and *arg are
758 * pointers to 0-terminated strings (inside line). Crap.
760 static int split_key_map(char *line, char **handler, char **arg)
762 if (!(*handler = strchr(line + 1, ':')))
766 if (!(*arg = strchr(*handler, ':')))
775 static int check_key_map_args(void)
779 char *tmp = NULL, *handler, *arg;
781 for (i = 0; i < conf.key_map_given; ++i) {
782 s = conf.key_map_arg[i];
786 tmp = para_strdup(s);
787 if (!split_key_map(tmp, &handler, &arg))
789 if (strlen(handler) != 1)
798 if (find_cmd_byname(arg) < 0)
808 * React to various signal-related events
810 static void handle_signal(int sig)
814 msg_n_exit(EXIT_FAILURE,
815 "only the good die young (caught SIGTERM))\n");
825 PARA_WARNING_LOG("%s", "caught SIGINT, reset");
826 /* Nothing to do. SIGINT killed our child, para_client stat.
827 * This get noticed by do_select which resets everything
831 PARA_NOTICE_LOG("%s", "got SIGUSR1, rereading configuration");
840 static int open_audiod_pipe(void)
848 return para_open_audiod_pipe(conf.stat_cmd_arg);
852 * This is the core select loop. Besides the (internal) signal
853 * pipe, the following other fds are checked according to the mode:
855 * GETCH_MODE: check stdin, return when key is pressed
857 * COMMAND_MODE: check command_pipe and stdin. Return when a screen full
858 * of output has been read or when other end has closed command pipe or
859 * when any key is pressed.
861 * EXTERNAL_MODE: Check only signal pipe. Used when an external command
862 * is running. During that thime curses is disabled. Returns when
865 static int do_select(int mode)
869 int max_fileno, cp_numread = 1;
870 char command_buf[4096] = "";
871 int cbo = 0; /* command buf offset */
874 tv.tv_sec = conf.timeout_arg / 1000;
875 tv.tv_usec = (conf.timeout_arg % 1000) * 1000;
876 // ret = refresh_status();
881 audiod_pipe = open_audiod_pipe();
882 if (audiod_pipe >= 0)
883 para_fd_set(audiod_pipe, &rfds, &max_fileno);
885 para_fd_set(signal_pipe, &rfds, &max_fileno);
886 /* command pipe only for COMMAND_MODE */
887 if (command_pipe >= 0 && mode == COMMAND_MODE)
888 para_fd_set(command_pipe, &rfds, &max_fileno);
889 ret = para_select(max_fileno + 1, &rfds, NULL, &tv);
891 goto check_return; /* skip fd checks */
893 if (FD_ISSET(signal_pipe, &rfds)) {
894 int sig_nr = para_next_signal();
896 handle_signal(sig_nr);
898 /* read command pipe if ready */
899 if (command_pipe >= 0 && mode == COMMAND_MODE &&
900 FD_ISSET(command_pipe, &rfds)) {
901 cp_numread = read(command_pipe, command_buf + cbo,
902 sizeof(command_buf) - 1 - cbo);
907 PARA_ERROR_LOG("read error (%d)", cp_numread);
912 if (audiod_pipe >= 0 && FD_ISSET(audiod_pipe, &rfds))
913 if (read_audiod_pipe(audiod_pipe, check_stat_line) <= 0) {
917 free(stat_content[SI_STATUS_BAR]);
918 stat_content[SI_STATUS_BAR] =
919 para_strdup("audiod not running!?");
925 if (cp_numread <= 0 && !cbo) /* command complete */
928 cbo = for_each_line(command_buf, cbo,
929 &add_output_line, NULL);
933 ret = wgetch(top.win);
934 if (ret != ERR && ret != KEY_RESIZE) {
940 kill(cmd_pid, SIGTERM);
945 ret = wgetch(top.win);
946 if (ret != ERR && ret != KEY_RESIZE)
959 * read from command pipe and print data to bot window
961 static int send_output(void)
963 if (command_pipe < 0)
965 if (do_select(COMMAND_MODE) >= 0)
966 PARA_INFO_LOG("%s", "command complete");
968 PARA_NOTICE_LOG("%s", "command aborted");
969 print_in_bar(COLOR_MSG, " ");
973 static int client_cmd_cmdline(char *cmd)
975 int ret, fds[3] = {0, 1, 0};
976 char *c = make_message(BINDIR "/para_client %s", cmd);
978 outputf(COLOR_COMMAND, "%s", c);
979 print_in_bar(COLOR_MSG, "executing client command, hit any key to abort\n");
980 ret = para_exec_cmdline_pid(&cmd_pid, c, fds);
984 command_pipe = fds[1];
985 mark_fd_nonblock(command_pipe);
986 return send_output();
990 * exec command and print output to bot win
992 static int display_cmd(char *cmd)
994 int fds[3] = {0, 1, 0};
996 print_in_bar(COLOR_MSG, "executing display command, hit any key to abort");
997 outputf(COLOR_COMMAND, "%s", cmd);
998 if (para_exec_cmdline_pid(&cmd_pid, cmd, fds) < 0)
1000 command_pipe = fds[1];
1001 mark_fd_nonblock(command_pipe);
1002 return send_output();
1006 * shutdown curses and stat pipe before executing external commands
1008 static int external_cmd(char *cmd)
1010 int fds[3] = {-1, -1, -1};
1015 para_exec_cmdline_pid(&cmd_pid, cmd, fds);
1017 do_select(EXTERNAL_MODE);
1022 static void print_scroll_msg(void)
1024 unsigned lines_total, filled = ringbuffer_filled(bot_win_rb);
1025 int first_rbe = first_visible_rbe(&lines_total);
1026 print_in_bar(COLOR_MSG, "scrolled view: %d-%d/%d\n", filled - first_rbe,
1027 filled - scroll_position, ringbuffer_filled(bot_win_rb));
1030 static void com_page_down(void)
1033 int i = scroll_position;
1034 while (lines < bot.lines && --i > 0) {
1035 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
1038 lines += NUM_LINES(strlen(rbe->msg));
1041 scroll_position = i;
1046 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1049 static void com_page_up(void)
1052 int fvr = first_visible_rbe(&lines);
1053 if (fvr < 0 || fvr + 1 >= ringbuffer_filled(bot_win_rb)) {
1054 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1057 scroll_position = fvr + 1;
1058 for (; scroll_position > 0; scroll_position--) {
1059 fvr = first_visible_rbe(&lines);
1060 if (lines == bot.lines)
1067 static void com_scroll_down(void)
1069 struct rb_entry *rbe;
1072 if (!scroll_position) {
1073 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1077 rbe = ringbuffer_get(bot_win_rb, scroll_position);
1078 rbe_lines = NUM_LINES(rbe->len);
1079 wscrl(bot.win, rbe_lines);
1080 wmove(bot.win, bot.lines - rbe_lines, 0);
1081 wattron(bot.win, COLOR_PAIR(rbe->color));
1082 waddstr(bot.win, rbe->msg);
1087 static void com_scroll_up(void)
1089 struct rb_entry *rbe = NULL;
1091 int i, first_rbe, num_scroll;
1093 /* the entry that is going to vanish */
1094 rbe = ringbuffer_get(bot_win_rb, scroll_position);
1097 num_scroll = NUM_LINES(rbe->len);
1098 first_rbe = first_visible_rbe(&lines);
1099 if (first_rbe < 0 || (first_rbe == ringbuffer_filled(bot_win_rb) - 1))
1102 wscrl(bot.win, -num_scroll);
1103 i = draw_top_rbe(&lines);
1106 while (i > 0 && lines < num_scroll) {
1108 rbe = ringbuffer_get(bot_win_rb, --i);
1111 rbe_lines = NUM_LINES(rbe->len);
1113 // fprintf(stderr, "msg: %s\n", rbe->msg);
1114 wattron(bot.win, COLOR_PAIR(rbe->color));
1115 waddstr(bot.win, "\n");
1116 waddstr(bot.win, rbe->msg);
1125 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1128 static void com_ll_decr(void)
1130 if (conf.loglevel_arg <= DEBUG) {
1131 print_in_bar(COLOR_ERRMSG,
1132 "loglevel already at maximal verbosity\n");
1135 conf.loglevel_arg--;
1136 print_in_bar(COLOR_MSG, "loglevel set to %d\n", conf.loglevel_arg);
1139 static void com_ll_incr(void)
1141 if (conf.loglevel_arg >= EMERG) {
1142 print_in_bar(COLOR_ERRMSG,
1143 "loglevel already at miminal verbosity\n");
1146 conf.loglevel_arg++;
1147 print_in_bar(COLOR_MSG, "loglevel set to %d\n", conf.loglevel_arg);
1151 * reread configuration, terminate on errors
1153 static void com_reread_conf(void)
1155 char *cf =configfile_exists();
1156 struct gui_cmdline_parser_params params = {
1159 .check_required = 0,
1160 .check_ambiguity = 0
1164 PARA_WARNING_LOG("%s", "there is no configuration to read");
1167 PARA_INFO_LOG("%s", "rereading command line options and config file");
1168 gui_cmdline_parser(_argc, _argv, &conf);
1169 gui_cmdline_parser_config_file(cf, &conf, ¶ms);
1170 PARA_NOTICE_LOG("%s", "config file reloaded");
1171 if (check_key_map_args() < 0)
1172 finish(EXIT_FAILURE);
1175 static void com_help(void)
1179 for (i = 0; i < conf.key_map_given; ++i) {
1180 char *handler, *arg, *tmp = para_strdup(conf.key_map_arg[i]);
1181 const char *handler_text = "???", *desc = NULL;
1183 if (!split_key_map(tmp, &handler, &arg)) {
1189 handler_text = "internal";
1190 desc = command_list[find_cmd_byname(arg)].description;
1192 case 'x': handler_text = "external"; break;
1193 case 'd': handler_text = "display "; break;
1194 case 'p': handler_text = "para "; break;
1196 outputf(COLOR_MSG, "%s\t%s\t%s%s\t%s", tmp, handler_text, arg,
1197 strlen(arg) < 8? "\t" : "",
1201 for (i = 0; command_list[i].handler; i++) {
1202 struct gui_command gc = command_list[i];
1204 outputf(COLOR_MSG, "%s\tinternal\t%s\t%s%s", gc.key, gc.name,
1205 strlen(gc.name) < 8? "\t" : "",
1208 print_in_bar(COLOR_MSG, "try \"para_gui -h\" or \"para_client help\" "
1212 static void com_shrink_top_win(void)
1214 if (top.lines <= theme.top_lines_min) {
1215 PARA_WARNING_LOG("%s", "can not decrease top window");
1218 init_wins(top.lines - 1);
1221 print_in_bar(COLOR_MSG, "%s", "decreased top window");
1224 static void com_enlarge_top_win(void)
1226 if (bot.lines < 3) {
1227 PARA_WARNING_LOG("%s", "can not increase top window");
1230 init_wins(top.lines + 1);
1233 print_in_bar(COLOR_MSG, "increased top window");
1236 static void com_version(void)
1238 print_in_bar(COLOR_MSG, "para_gui " PACKAGE_VERSION " \""
1242 static void com_quit(void)
1247 static void com_refresh(void)
1253 static void change_theme(int next)
1259 /* This seems to be needed twice, why? */
1262 PARA_NOTICE_LOG("new theme: %s", theme.name);
1265 static void com_next_theme(void)
1270 static void com_prev_theme(void)
1276 static void handle_command(int c)
1280 /* first check user's key bindings */
1281 for (i = 0; i < conf.key_map_given; ++i) {
1282 char tmp[MAXLINE], *handler, *arg;
1284 strcpy(tmp, conf.key_map_arg[i]);
1285 if (!split_key_map(tmp, &handler, &arg))
1287 if (!strcmp(tmp, km_keyname(c))) {
1288 if (*handler == 'd') {
1292 if (*handler == 'x') {
1296 if (*handler == 'p') {
1297 client_cmd_cmdline(arg);
1300 if (*handler == 'i') {
1301 int num = find_cmd_byname(arg);
1303 command_list[num].handler();
1308 /* not found, check internal key bindings */
1309 for (i = 0; command_list[i].handler; i++) {
1310 if (!strcmp(km_keyname(c), command_list[i].key)) {
1311 command_list[i].handler();
1315 print_in_bar(COLOR_ERRMSG, "key '%s' is not bound, press ? for help",
1319 int main(int argc, char *argv[])
1327 if (gui_cmdline_parser(argc, argv, &conf)) {
1328 fprintf(stderr, "parse error while reading command line\n");
1331 HANDLE_VERSION_FLAG("gui", conf);
1332 init_theme(0, &theme);
1333 top.lines = theme.top_lines_default;
1334 if (check_key_map_args() < 0) {
1335 fprintf(stderr, "invalid key map\n");
1338 cf = configfile_exists();
1339 if (!cf && conf.config_file_given) {
1340 fprintf(stderr, "can not read config file %s\n",
1341 conf.config_file_arg);
1345 struct gui_cmdline_parser_params params = {
1348 .check_required = 0,
1349 .check_ambiguity = 0
1351 gui_cmdline_parser_config_file(cf, &conf, ¶ms);
1353 if (check_key_map_args() < 0) {
1354 fprintf(stderr, "invalid key map in config file\n");
1357 setup_signal_handling();
1358 bot_win_rb = ringbuffer_new(RINGBUFFER_SIZE);
1359 initscr(); /* needed only once, always successful */
1364 ret = do_select(GETCH_MODE);
1367 print_in_bar(COLOR_MSG, " ");
1368 handle_command(ret);