2 * Copyright (C) 1998-2011 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file gui.c Curses-based interface for paraslash. */
11 #include <sys/types.h>
15 #include "gui.cmdline.h"
19 #include "ringbuffer.h"
26 /** define the array of error lists needed by para_gui */
28 static char *stat_content[NUM_STAT_ITEMS];
30 #define STANDARD_STATUS_BAR "para_gui " PACKAGE_VERSION " (hit ? for help)"
32 static int signal_pipe;
34 static struct win_data {
40 } top, bot, sb, in, sep;
42 #define RINGBUFFER_SIZE 512
48 static struct ringbuffer *bot_win_rb;
49 #define NUM_LINES(len) (1 + (len) / bot.cols)
51 static unsigned scroll_position;
53 static int cmd_died, curses_active;
56 static int command_pipe = -1;
57 static int stat_pipe = -1;
58 static struct gui_args_info conf;
60 enum {GETCH_MODE, COMMAND_MODE, EXTERNAL_MODE};
63 #define COLOR_STATUSBAR 52
64 #define COLOR_COMMAND 53
65 #define COLOR_OUTPUT 54
67 #define COLOR_ERRMSG 56
68 #define COLOR_WELCOME 57
69 #define COLOR_SEPARATOR 58
76 const char *description;
77 void (*handler)(void);
83 char postfix[MAXLINE];
89 char content[MAXLINE];
92 static struct gui_theme theme;
97 static void com_help(void);
98 static void com_reread_conf(void);
99 static void com_enlarge_top_win(void);
100 static void com_shrink_top_win(void);
101 static void com_version(void);
102 static void com_quit(void);
103 static void com_refresh(void);
104 static void com_ll_incr(void);
105 static void com_ll_decr(void);
106 static void com_prev_theme(void);
107 static void com_next_theme(void);
108 static void com_scroll_up(void);
109 static void com_scroll_down(void);
110 static void com_page_up(void);
111 static void com_page_down(void);
113 static struct gui_command command_list[] = {
117 .description = "print help",
121 .name = "enlarge_win",
122 .description = "enlarge the top window",
123 .handler = com_enlarge_top_win
126 .name = "shrink_win",
127 .description = "shrink the top window",
128 .handler = com_shrink_top_win
131 .name = "reread_conf",
132 .description = "reread configuration file",
133 .handler = com_reread_conf
137 .description = "exit para_gui",
142 .description = "redraw the screen",
143 .handler = com_refresh
146 .name = "next_theme",
147 .description = "switch to next theme",
148 .handler = com_next_theme
151 .name = "prev_theme",
152 .description = "switch to previous stream",
153 .handler = com_prev_theme
157 .description = "increase loglevel (decreases verbosity)",
158 .handler = com_ll_incr
162 .description = "decrease loglevel (increases verbosity)",
163 .handler = com_ll_decr
167 .description = "show the para_gui version",
168 .handler = com_version
172 .description = "scroll up one line",
173 .handler = com_scroll_up
176 .name = "scroll_down",
177 .description = "scroll down one line",
178 .handler = com_scroll_down
182 .description = "scroll up one page",
183 .handler = com_page_up
187 .description = "scroll down one page",
188 .handler = com_page_down
194 static int find_cmd_byname(char *name)
198 for (i = 0; command_list[i].handler; i++)
199 if (!strcmp(command_list[i].name, name))
204 /* taken from mutt */
205 static char *km_keyname(int c)
210 sprintf(buf, "<up>");
214 sprintf(buf, "<down>");
218 sprintf(buf, "<left>");
221 if (c == KEY_RIGHT) {
222 sprintf(buf, "<right>");
225 if (c == KEY_NPAGE) {
226 sprintf(buf, "<npage>");
229 if (c == KEY_PPAGE) {
230 sprintf(buf, "<ppage>");
233 if (c < 256 && c > -128 && iscntrl((unsigned char) c)) {
238 buf[1] = (c + '@') & 0x7f;
241 snprintf(buf, sizeof(buf), "\\%d%d%d", c >> 6,
242 (c >> 3) & 7, c & 7);
243 } else if (c >= KEY_F0 && c < KEY_F(256))
244 sprintf(buf, "<F%d>", c - KEY_F0);
246 snprintf(buf, sizeof(buf), "%c", (unsigned char) c);
248 snprintf(buf, sizeof(buf), "\\x%hx", (unsigned short) c);
252 static char *configfile_exists(void)
254 static char *config_file;
257 if (!conf.config_file_given) {
259 char *home = para_homedir();
260 config_file = make_message("%s/.paraslash/gui.conf",
266 tmp = conf.config_file_arg;
267 return file_exists(tmp)? tmp: NULL;
271 * print num spaces to curses window
273 static void add_spaces(WINDOW* win, unsigned int num)
282 * print aligned string to curses window. This function always prints
285 static int align_str(WINDOW* win, char *str, unsigned int len,
288 int i, num; /* of spaces */
292 num = len - strlen(str);
297 /* replace control characters by spaces */
298 for (i = 0; i < len && str[i]; i++) {
299 if (str[i] == '\n' || str[i] == '\r' || str[i] == '\f')
304 add_spaces(win, num);
305 } else if (align == RIGHT) {
306 add_spaces(win, num);
309 add_spaces(win, num / 2);
310 waddstr(win, str[0]? str: "");
311 add_spaces(win, num - num / 2);
316 __printf_2_3 static void print_in_bar(int color, const char *fmt,...)
322 wattron(in.win, COLOR_PAIR(color));
323 PARA_VSPRINTF(fmt, msg);
325 align_str(in.win, msg, sb.cols, LEFT);
331 * update the status bar
333 static void print_status_bar(void)
339 tmp = para_strdup(STANDARD_STATUS_BAR);
341 align_str(sb.win, tmp, sb.cols, CENTER);
347 * get the number of the oldest rbe that is (partially) visible. On return,
348 * lines contains the sum of the number of lines of all visable entries. If the
349 * first one is only partially visible, lines is greater than bot.lines.
351 static int first_visible_rbe(unsigned *lines)
355 for (i = scroll_position; i < RINGBUFFER_SIZE; i++) {
356 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
360 // fprintf(stderr, "found: %s\n", rbe->msg);
361 rbe_lines = NUM_LINES(rbe->len);
362 if (rbe_lines > bot.lines)
365 if (*lines >= bot.lines)
368 return RINGBUFFER_SIZE - 1;
371 static int draw_top_rbe(unsigned *lines)
374 int offset, fvr = first_visible_rbe(lines);
375 struct rb_entry *rbe;
379 wmove(bot.win, 0, 0);
380 rbe = ringbuffer_get(bot_win_rb, fvr);
383 /* first rbe might be only partially visible */
384 offset = (*lines - bot.lines) * bot.cols;
385 len = strlen(rbe->msg);
386 if (offset < 0 || len < offset)
388 wattron(bot.win, COLOR_PAIR(rbe->color));
389 waddstr(bot.win, rbe->msg + offset);
390 *lines = NUM_LINES(len - offset);
394 static void redraw_bot_win(void)
399 wmove(bot.win, 0, 0);
401 i = draw_top_rbe(&lines);
404 while (i > 0 && lines < bot.lines) {
405 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, --i);
408 waddstr(bot.win, "\n");
411 lines += NUM_LINES(rbe->len);
412 wattron(bot.win, COLOR_PAIR(rbe->color));
413 waddstr(bot.win, "\n");
414 waddstr(bot.win, rbe->msg);
420 static void rb_add_entry(int color, char *msg)
422 struct rb_entry *old, *new = para_malloc(sizeof(struct rb_entry));
425 new->len = strlen(msg);
427 old = ringbuffer_add(bot_win_rb, new);
428 // fprintf(stderr, "added: %s\n", new->msg);
433 if (scroll_position) {
434 /* discard current scrolling, like xterm does */
439 wattron(bot.win, COLOR_PAIR(color));
440 getyx(bot.win, y, x);
442 waddstr(bot.win, "\n");
443 waddstr(bot.win, msg);
447 * print formated output to bot win and refresh
449 __printf_2_3 static void outputf(int color, const char* fmt,...)
455 PARA_VSPRINTF(fmt, msg);
456 rb_add_entry(color, msg);
460 static int add_output_line(char *line, __a_unused void *data)
464 rb_add_entry(COLOR_OUTPUT, para_strdup(line));
470 __printf_2_3 void para_log(int ll, const char *fmt,...)
475 if (ll < loglevel || !curses_active)
484 color = COLOR_ERRMSG;
486 PARA_VSPRINTF(fmt, msg);
488 rb_add_entry(color, msg);
492 static void setup_signal_handling(void)
494 signal_pipe = para_signal_init();
495 para_install_sighandler(SIGINT);
496 para_install_sighandler(SIGTERM);
497 para_install_sighandler(SIGCHLD);
498 para_install_sighandler(SIGWINCH);
499 para_install_sighandler(SIGUSR1);
500 para_sigaction(SIGHUP, SIG_IGN);
503 __noreturn static void do_exit(int ret)
505 para_sigaction(SIGTERM, SIG_IGN);
510 static void shutdown_curses(void)
519 __noreturn static void finish(int ret)
526 * exit curses and print given message to stdout/stderr
528 __noreturn __printf_2_3 static void msg_n_exit(int ret, const char* fmt, ...)
531 FILE *outfd = ret? stderr: stdout;
535 vfprintf(outfd, fmt, argp);
540 static void print_welcome(void)
542 if (loglevel > LL_NOTICE)
544 outputf(COLOR_WELCOME, "Welcome to para_gui " PACKAGE_VERSION
545 " \"" CODENAME "\". Theme: %s", theme.name);
552 static void init_wins(int top_lines)
556 top.lines = top_lines;
561 bot.lines = LINES - top.lines - 3;
563 bot.begy = top.lines + 1;
578 sep.begy = top.lines;
581 assume_default_colors(theme.default_fg, theme.default_bg);
583 mvwin(top.win, top.begy, top.begx);
584 wresize(top.win, top.lines, top.cols);
586 mvwin(sb.win, sb.begy, sb.begx);
587 wresize(sb.win, sb.lines, sb.cols);
589 mvwin(sep.win, sep.begy, sep.begx);
590 wresize(sep.win, sep.lines, sep.cols);
592 mvwin(bot.win, bot.begy, bot.begx);
593 wresize(bot.win, bot.lines, bot.cols);
595 mvwin(in.win, in.begy, in.begx);
596 wresize(in.win, in.lines, in.cols);
598 sep.win = newwin(sep.lines, sep.cols, sep.begy, sep.begx);
599 top.win = newwin(top.lines, top.cols, top.begy, top.begx);
600 bot.win = newwin(bot.lines, bot.cols, bot.begy, bot.begx);
601 sb.win = newwin(sb.lines, sb.cols, sb.begy, sb.begx);
602 in.win = newwin(in.lines, in.cols, in.begy, in.begx);
603 if (!top.win || !bot.win || !sb.win || !in.win || !sep.win)
604 msg_n_exit(1, "Error: Cannot create curses windows\n");
608 scrollok(bot.win, 1);
609 wattron(sb.win, COLOR_PAIR(COLOR_STATUSBAR));
610 wattron(sep.win, COLOR_PAIR(COLOR_SEPARATOR));
611 wattron(bot.win, COLOR_PAIR(COLOR_BOT));
612 wattron(top.win, COLOR_PAIR(COLOR_TOP));
624 wmove(sep.win, 0, 0);
625 for (i = 1; i <= COLS; i++)
626 waddstr(sep.win, theme.sep_str);
629 wnoutrefresh(top.win);
630 wnoutrefresh(bot.win);
631 //wnoutrefresh(sb.win);
633 wnoutrefresh(in.win);
634 wnoutrefresh(sep.win);
638 static void check_geometry(void)
640 if (LINES < theme.lines_min || COLS < theme.cols_min)
641 msg_n_exit(EXIT_FAILURE, "Error: Terminal (%dx%d) too small"
642 " (need at least %dx%d)\n", COLS, LINES,
643 theme.cols_min, theme.lines_min);
647 * Print stat item #i to curses window
649 static void print_stat_item(int i)
652 struct stat_item_data d = theme.data[i];
653 char *c = stat_content[i];
655 if (!curses_active || !d.len || !c)
657 tmp = make_message("%s%s%s", d.prefix, c, d.postfix);
658 wmove(top.win, d.y * top.lines / 100, d.x * COLS / 100);
660 wattron(top.win, COLOR_PAIR(i + 1));
661 align_str(top.win, tmp, d.len * COLS / 100, d.align);
666 static int update_item(int item_num, char *buf)
668 char **c = stat_content + item_num;
675 *c = para_strdup("(artist tag not set)");
678 *c = para_strdup("(title tag not set)");
681 *c = para_strdup("????");
684 *c = para_strdup("(album tag not set)");
687 *c = para_strdup("(comment tag not set)");
691 *c = para_strdup(buf);
693 print_stat_item(item_num);
697 static int read_stat_pipe(fd_set *rfds)
700 static int bufsize, loaded;
706 if (loaded >= bufsize) {
707 if (bufsize > 1000 * 1000) {
711 bufsize += bufsize + 1000;
712 buf = para_realloc(buf, bufsize);
714 assert(loaded < bufsize);
715 ret = read_nonblock(stat_pipe, buf + loaded, bufsize - loaded,
718 ret2 = for_each_stat_item(buf, loaded, update_item);
719 if (ret < 0 || ret2 < 0) {
721 return ret2 < 0? ret2 : ret;
723 sz = ret2; /* what is left */
724 if (sz > 0 && sz < loaded)
725 memmove(buf, buf + loaded - sz, sz);
730 static void print_all_items(void)
736 FOR_EACH_STATUS_ITEM(i)
740 static void clear_all_items(void)
744 FOR_EACH_STATUS_ITEM(i) {
745 free(stat_content[i]);
746 stat_content[i] = para_strdup("");
750 static void init_colors(void)
755 msg_n_exit(EXIT_FAILURE, "Error: No color term\n");
757 FOR_EACH_STATUS_ITEM(i)
758 if (theme.data[i].len)
759 init_pair(i + 1, theme.data[i].fg, theme.data[i].bg);
760 init_pair(COLOR_STATUSBAR, theme.sb_fg, theme.sb_bg);
761 init_pair(COLOR_COMMAND, theme.cmd_fg, theme.cmd_bg);
762 init_pair(COLOR_OUTPUT, theme.output_fg, theme.output_bg);
763 init_pair(COLOR_MSG, theme.msg_fg, theme.msg_bg);
764 init_pair(COLOR_ERRMSG, theme.err_msg_fg, theme.err_msg_bg);
765 init_pair(COLOR_WELCOME, theme.welcome_fg, theme.welcome_bg);
766 init_pair(COLOR_SEPARATOR, theme.sep_fg, theme.sep_bg);
767 init_pair(COLOR_TOP, theme.default_fg, theme.default_bg);
768 init_pair(COLOR_BOT, theme.default_fg, theme.default_bg);
772 * (re-)initialize the curses library FIXME: Error checking
774 static void init_curses(void)
777 if (top.win && refresh() == ERR) { /* refesh is really needed */
778 msg_n_exit(EXIT_FAILURE, "refresh() failed\n");
781 curs_set(0); /* make cursor invisible, ignore errors */
782 // if (noraw() == ERR);
783 // msg_n_exit(EXIT_FAILURE, "can not place terminal out of "
785 nonl(); /* tell curses not to do NL->CR/NL on output */
786 noecho(); /* don't echo input */
787 cbreak(); /* take input chars one at a time, no wait for \n */
791 init_wins(theme.top_lines_default);
793 noecho(); /* don't echo input */
797 static void check_sigchld(void)
802 ret = para_reap_child(&pid);
805 if (pid == cmd_pid) {
809 goto reap_next_child;
813 * This sucker modifies its first argument. *handler and *arg are
814 * pointers to 0-terminated strings (inside line). Crap.
816 static int split_key_map(char *line, char **handler, char **arg)
818 if (!(*handler = strchr(line + 1, ':')))
822 if (!(*arg = strchr(*handler, ':')))
831 static int check_key_map_args(void)
835 char *tmp = NULL, *handler, *arg;
837 for (i = 0; i < conf.key_map_given; ++i) {
838 s = conf.key_map_arg[i];
842 tmp = para_strdup(s);
843 if (!split_key_map(tmp, &handler, &arg))
845 if (strlen(handler) != 1)
854 if (find_cmd_byname(arg) < 0)
864 * React to various signal-related events
866 static void handle_signal(int sig)
870 msg_n_exit(EXIT_FAILURE,
871 "only the good die young (caught SIGTERM))\n");
881 PARA_WARNING_LOG("caught SIGINT, reset");
882 /* Nothing to do. SIGINT killed our child which gets noticed
883 * by do_select and resets everything.
887 PARA_NOTICE_LOG("got SIGUSR1, rereading configuration");
896 static int open_stat_pipe(void)
899 int ret, fds[3] = {0, 1, 0};
906 * Sleep a bit to avoid a busy loop. As the call to sleep() may
907 * be interrupted by SIGCHLD, we simply wait until the call
912 ret = para_exec_cmdline_pid(&pid, conf.stat_cmd_arg, fds);
915 ret = mark_fd_nonblocking(fds[1]);
923 * This is the core select loop. Besides the (internal) signal
924 * pipe, the following other fds are checked according to the mode:
926 * GETCH_MODE: check stdin, return when key is pressed
928 * COMMAND_MODE: check command_pipe and stdin. Return when a screen full
929 * of output has been read or when other end has closed command pipe or
930 * when any key is pressed.
932 * EXTERNAL_MODE: Check only signal pipe. Used when an external command
933 * is running. During that time curses is disabled. Returns when
936 static int do_select(int mode)
941 char command_buf[4096] = "";
942 int cbo = 0; /* command buf offset */
945 tv.tv_sec = conf.timeout_arg / 1000;
946 tv.tv_usec = (conf.timeout_arg % 1000) * 1000;
947 // ret = refresh_status();
951 stat_pipe = open_stat_pipe();
953 para_fd_set(stat_pipe, &rfds, &max_fileno);
955 para_fd_set(signal_pipe, &rfds, &max_fileno);
956 /* command pipe only for COMMAND_MODE */
957 if (command_pipe >= 0 && mode == COMMAND_MODE)
958 para_fd_set(command_pipe, &rfds, &max_fileno);
959 ret = para_select(max_fileno + 1, &rfds, NULL, &tv);
961 goto check_return; /* skip fd checks */
963 ret = para_next_signal(&rfds);
966 /* read command pipe if ready */
967 if (command_pipe >= 0 && mode == COMMAND_MODE) {
969 ret = read_nonblock(command_pipe, command_buf + cbo,
970 sizeof(command_buf) - 1 - cbo, &rfds, &sz);
973 cbo = for_each_line(command_buf, cbo, &add_output_line, NULL);
977 PARA_NOTICE_LOG("closing command pipe: %s",
978 para_strerror(-ret));
984 ret = read_stat_pipe(&rfds);
986 PARA_NOTICE_LOG("closing stat pipe: %s\n", para_strerror(-ret));
990 free(stat_content[SI_BASENAME]);
991 stat_content[SI_BASENAME] =
992 para_strdup("stat command terminated!?");
998 ret = wgetch(top.win);
999 if (ret != ERR && ret != KEY_RESIZE) {
1001 close(command_pipe);
1005 kill(cmd_pid, SIGTERM);
1010 ret = wgetch(top.win);
1011 if (ret != ERR && ret != KEY_RESIZE)
1024 * read from command pipe and print data to bot window
1026 static int send_output(void)
1030 if (command_pipe < 0)
1032 ret = mark_fd_nonblocking(command_pipe);
1034 close(command_pipe);
1037 if (do_select(COMMAND_MODE) >= 0)
1038 PARA_INFO_LOG("command complete");
1040 PARA_NOTICE_LOG("command aborted");
1041 print_in_bar(COLOR_MSG, " ");
1045 static int client_cmd_cmdline(char *cmd)
1047 int ret, fds[3] = {0, 1, 0};
1048 char *c = make_message(BINDIR "/para_client -- %s", cmd);
1050 outputf(COLOR_COMMAND, "%s", c);
1051 print_in_bar(COLOR_MSG, "executing client command, hit any key to abort\n");
1052 ret = para_exec_cmdline_pid(&cmd_pid, c, fds);
1056 command_pipe = fds[1];
1057 return send_output();
1061 * exec command and print output to bot win
1063 static int display_cmd(char *cmd)
1065 int fds[3] = {0, 1, 0};
1067 print_in_bar(COLOR_MSG, "executing display command, hit any key to abort");
1068 outputf(COLOR_COMMAND, "%s", cmd);
1069 if (para_exec_cmdline_pid(&cmd_pid, cmd, fds) < 0)
1071 command_pipe = fds[1];
1072 return send_output();
1076 * shutdown curses and stat pipe before executing external commands
1078 static int external_cmd(char *cmd)
1080 int fds[3] = {-1, -1, -1};
1085 para_exec_cmdline_pid(&cmd_pid, cmd, fds);
1087 do_select(EXTERNAL_MODE);
1092 static void print_scroll_msg(void)
1094 unsigned lines_total, filled = ringbuffer_filled(bot_win_rb);
1095 int first_rbe = first_visible_rbe(&lines_total);
1096 print_in_bar(COLOR_MSG, "scrolled view: %d-%d/%d\n", filled - first_rbe,
1097 filled - scroll_position, ringbuffer_filled(bot_win_rb));
1100 static void com_page_down(void)
1103 int i = scroll_position;
1104 while (lines < bot.lines && --i > 0) {
1105 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
1108 lines += NUM_LINES(strlen(rbe->msg));
1111 scroll_position = i;
1116 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1119 static void com_page_up(void)
1122 int fvr = first_visible_rbe(&lines);
1124 if (fvr < 0 || fvr + 1 >= ringbuffer_filled(bot_win_rb)) {
1125 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1128 scroll_position = fvr + 1;
1129 for (; scroll_position > 0; scroll_position--) {
1130 first_visible_rbe(&lines);
1131 if (lines == bot.lines)
1138 static void com_scroll_down(void)
1140 struct rb_entry *rbe;
1143 if (!scroll_position) {
1144 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1148 rbe = ringbuffer_get(bot_win_rb, scroll_position);
1149 rbe_lines = NUM_LINES(rbe->len);
1150 wscrl(bot.win, rbe_lines);
1151 wmove(bot.win, bot.lines - rbe_lines, 0);
1152 wattron(bot.win, COLOR_PAIR(rbe->color));
1153 waddstr(bot.win, rbe->msg);
1158 static void com_scroll_up(void)
1160 struct rb_entry *rbe = NULL;
1162 int i, first_rbe, num_scroll;
1164 /* the entry that is going to vanish */
1165 rbe = ringbuffer_get(bot_win_rb, scroll_position);
1168 num_scroll = NUM_LINES(rbe->len);
1169 first_rbe = first_visible_rbe(&lines);
1170 if (first_rbe < 0 || (first_rbe == ringbuffer_filled(bot_win_rb) - 1))
1173 wscrl(bot.win, -num_scroll);
1174 i = draw_top_rbe(&lines);
1177 while (i > 0 && lines < num_scroll) {
1179 rbe = ringbuffer_get(bot_win_rb, --i);
1182 rbe_lines = NUM_LINES(rbe->len);
1184 // fprintf(stderr, "msg: %s\n", rbe->msg);
1185 wattron(bot.win, COLOR_PAIR(rbe->color));
1186 waddstr(bot.win, "\n");
1187 waddstr(bot.win, rbe->msg);
1196 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1199 static void com_ll_decr(void)
1201 if (loglevel <= LL_DEBUG) {
1202 print_in_bar(COLOR_ERRMSG,
1203 "loglevel already at maximal verbosity\n");
1207 print_in_bar(COLOR_MSG, "loglevel set to %d\n", loglevel);
1210 static void com_ll_incr(void)
1212 if (loglevel >= LL_EMERG) {
1213 print_in_bar(COLOR_ERRMSG,
1214 "loglevel already at minimal verbosity\n");
1218 print_in_bar(COLOR_MSG, "loglevel set to %d\n", loglevel);
1222 * reread configuration, terminate on errors
1224 static void com_reread_conf(void)
1226 char *cf =configfile_exists();
1227 struct gui_cmdline_parser_params params = {
1230 .check_required = 0,
1231 .check_ambiguity = 0
1235 PARA_WARNING_LOG("there is no configuration to read");
1238 PARA_INFO_LOG("rereading command line options and config file");
1239 gui_cmdline_parser(_argc, _argv, &conf);
1240 gui_cmdline_parser_config_file(cf, &conf, ¶ms);
1241 PARA_NOTICE_LOG("config file reloaded");
1242 if (check_key_map_args() < 0)
1243 finish(EXIT_FAILURE);
1246 static void com_help(void)
1250 for (i = 0; i < conf.key_map_given; ++i) {
1251 char *handler, *arg, *tmp = para_strdup(conf.key_map_arg[i]);
1252 const char *handler_text = "???", *desc = NULL;
1254 if (!split_key_map(tmp, &handler, &arg)) {
1260 handler_text = "internal";
1261 desc = command_list[find_cmd_byname(arg)].description;
1263 case 'x': handler_text = "external"; break;
1264 case 'd': handler_text = "display "; break;
1265 case 'p': handler_text = "para "; break;
1267 outputf(COLOR_MSG, "%s\t%s\t%s%s\t%s", tmp, handler_text, arg,
1268 strlen(arg) < 8? "\t" : "",
1272 for (i = 0; command_list[i].handler; i++) {
1273 struct gui_command gc = command_list[i];
1275 outputf(COLOR_MSG, "%s\tinternal\t%s\t%s%s", gc.key, gc.name,
1276 strlen(gc.name) < 8? "\t" : "",
1279 print_in_bar(COLOR_MSG, "try \"para_gui -h\" or \"para_client help\" "
1283 static void com_shrink_top_win(void)
1285 if (top.lines <= theme.top_lines_min) {
1286 PARA_WARNING_LOG("can not decrease top window");
1289 init_wins(top.lines - 1);
1292 print_in_bar(COLOR_MSG, "%s", "decreased top window");
1295 static void com_enlarge_top_win(void)
1297 if (bot.lines < 3) {
1298 PARA_WARNING_LOG("can not increase top window");
1301 init_wins(top.lines + 1);
1304 print_in_bar(COLOR_MSG, "increased top window");
1307 static void com_version(void)
1309 print_in_bar(COLOR_MSG, "para_gui " PACKAGE_VERSION " \""
1313 __noreturn static void com_quit(void)
1318 static void com_refresh(void)
1324 static void change_theme(int next)
1330 /* This seems to be needed twice, why? */
1333 PARA_NOTICE_LOG("new theme: %s", theme.name);
1336 static void com_next_theme(void)
1341 static void com_prev_theme(void)
1347 static void handle_command(int c)
1351 /* first check user's key bindings */
1352 for (i = 0; i < conf.key_map_given; ++i) {
1353 char *tmp, *handler, *arg;
1355 tmp = para_strdup(conf.key_map_arg[i]);
1356 if (!split_key_map(tmp, &handler, &arg)) {
1360 if (strcmp(tmp, km_keyname(c))) {
1364 if (*handler == 'd')
1366 else if (*handler == 'x')
1368 else if (*handler == 'p')
1369 client_cmd_cmdline(arg);
1370 else if (*handler == 'i') {
1371 int num = find_cmd_byname(arg);
1373 command_list[num].handler();
1378 /* not found, check internal key bindings */
1379 for (i = 0; command_list[i].handler; i++) {
1380 if (!strcmp(km_keyname(c), command_list[i].key)) {
1381 command_list[i].handler();
1385 print_in_bar(COLOR_ERRMSG, "key '%s' is not bound, press ? for help",
1389 int main(int argc, char *argv[])
1397 if (gui_cmdline_parser(argc, argv, &conf)) {
1398 fprintf(stderr, "parse error while reading command line\n");
1401 HANDLE_VERSION_FLAG("gui", conf);
1402 init_theme(0, &theme);
1403 top.lines = theme.top_lines_default;
1404 if (check_key_map_args() < 0) {
1405 fprintf(stderr, "invalid key map\n");
1408 cf = configfile_exists();
1409 if (!cf && conf.config_file_given) {
1410 fprintf(stderr, "can not read config file %s\n",
1411 conf.config_file_arg);
1415 struct gui_cmdline_parser_params params = {
1418 .check_required = 0,
1419 .check_ambiguity = 0
1421 gui_cmdline_parser_config_file(cf, &conf, ¶ms);
1423 loglevel = get_loglevel_by_name(conf.loglevel_arg);
1424 if (check_key_map_args() < 0) {
1425 fprintf(stderr, "invalid key map in config file\n");
1428 setup_signal_handling();
1429 bot_win_rb = ringbuffer_new(RINGBUFFER_SIZE);
1430 initscr(); /* needed only once, always successful */
1435 ret = do_select(GETCH_MODE);
1438 print_in_bar(COLOR_MSG, " ");
1439 handle_command(ret);