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"
19 /** define the array of error lists needed by para_gui */
21 extern const char *status_item_list
[NUM_STAT_ITEMS
];
22 static char *stat_content
[NUM_STAT_ITEMS
];
24 #define STANDARD_STATUS_BAR "para_gui " PACKAGE_VERSION " (hit ? for help)"
26 static int signal_pipe
;
28 static void finish(int sig
);
30 static struct win_data
{
36 } top
, bot
, sb
, in
, sep
;
38 #define RINGBUFFER_SIZE 512
45 #define NUM_LINES(len) (1 + (len) / bot.cols)
47 static unsigned scroll_position
;
49 static int cmd_died
, curses_active
;
52 static int command_pipe
= -1;
53 static int audiod_pipe
= -1;
54 static struct gui_args_info conf
;
56 enum {GETCH_MODE
, COMMAND_MODE
, EXTERNAL_MODE
};
59 #define COLOR_STATUSBAR 32
60 #define COLOR_COMMAND 33
61 #define COLOR_OUTPUT 34
63 #define COLOR_ERRMSG 36
64 #define COLOR_WELCOME 37
65 #define COLOR_SEPARATOR 38
72 const char *description
;
73 void (*handler
)(void);
79 char postfix
[MAXLINE
];
85 char content
[MAXLINE
];
88 static struct gui_theme theme
;
93 static void com_help(void);
94 static void com_reread_conf(void);
95 static void com_enlarge_top_win(void);
96 static void com_shrink_top_win(void);
97 static void com_version(void);
98 static void com_quit(void);
99 static void com_refresh(void);
100 static void com_ll_incr(void);
101 static void com_ll_decr(void);
102 static void com_prev_theme(void);
103 static void com_next_theme(void);
104 static void com_scroll_up(void);
105 static void com_scroll_down(void);
106 static void com_page_up(void);
107 static void com_page_down(void);
109 struct gui_command command_list
[] = {
113 .description
= "print help",
117 .name
= "enlarge_win",
118 .description
= "enlarge the top window",
119 .handler
= com_enlarge_top_win
122 .name
= "shrink_win",
123 .description
= "shrink the top window",
124 .handler
= com_shrink_top_win
127 .name
= "reread_conf",
128 .description
= "reread configuration file",
129 .handler
= com_reread_conf
133 .description
= "exit para_gui",
138 .description
= "redraw the screen",
139 .handler
= com_refresh
142 .name
= "next_theme",
143 .description
= "switch to next theme",
144 .handler
= com_next_theme
147 .name
= "prev_theme",
148 .description
= "switch to previous stream",
149 .handler
= com_prev_theme
153 .description
= "increase loglevel (decreases verbosity)",
154 .handler
= com_ll_incr
158 .description
= "decrease loglevel (increases verbosity)",
159 .handler
= com_ll_decr
163 .description
= "show the para_gui version",
164 .handler
= com_version
168 .description
= "scroll up one line",
169 .handler
= com_scroll_up
172 .name
= "scroll_down",
173 .description
= "scroll down one line",
174 .handler
= com_scroll_down
178 .description
= "scroll up one page",
179 .handler
= com_page_up
183 .description
= "scroll down one page",
184 .handler
= com_page_down
190 static int find_cmd_byname(char *name
)
194 for (i
= 0; command_list
[i
].handler
; i
++)
195 if (!strcmp(command_list
[i
].name
, name
))
200 /* taken from mutt */
201 static char *km_keyname(int c
)
206 sprintf(buf
, "<up>");
210 sprintf(buf
, "<down>");
214 sprintf(buf
, "<left>");
217 if (c
== KEY_RIGHT
) {
218 sprintf(buf
, "<right>");
221 if (c
== KEY_NPAGE
) {
222 sprintf(buf
, "<npage>");
225 if (c
== KEY_PPAGE
) {
226 sprintf(buf
, "<ppage>");
229 if (c
< 256 && c
> -128 && iscntrl((unsigned char) c
)) {
234 buf
[1] = (c
+ '@') & 0x7f;
237 snprintf(buf
, sizeof(buf
), "\\%d%d%d", c
>> 6,
238 (c
>> 3) & 7, c
& 7);
239 } else if (c
>= KEY_F0
&& c
< KEY_F(256))
240 sprintf(buf
, "<F%d>", c
- KEY_F0
);
242 snprintf(buf
, sizeof(buf
), "%c", (unsigned char) c
);
244 snprintf(buf
, sizeof(buf
), "\\x%hx", (unsigned short) c
);
248 static char *configfile_exists(void)
250 static char *config_file
;
253 if (!conf
.config_file_given
) {
255 char *home
= para_homedir();
256 config_file
= make_message("%s/.paraslash/gui.conf",
262 tmp
= conf
.config_file_arg
;
263 return file_exists(tmp
)? tmp
: NULL
;
267 * print num spaces to curses window
269 static void add_spaces(WINDOW
* win
, unsigned int num
)
278 * print aligned string to curses window. This function always prints
281 static int align_str(WINDOW
* win
, const char *string
, unsigned int len
,
284 int num
; /* of spaces */
289 num
= len
- strlen(string
);
290 str
= para_strdup(string
);
297 add_spaces(win
, num
);
298 } else if (align
== RIGHT
) {
299 add_spaces(win
, num
);
302 add_spaces(win
, num
/ 2);
303 waddstr(win
, str
[0]? str
: "");
304 add_spaces(win
, num
- num
/ 2);
310 __printf_2_3
static void print_in_bar(int color
, const char *fmt
,...)
316 wattron(in
.win
, COLOR_PAIR(color
));
317 PARA_VSPRINTF(fmt
, msg
);
319 align_str(in
.win
, msg
, sb
.cols
, LEFT
);
325 * update the status bar
327 static void print_status_bar(void)
332 align_str(sb
.win
, STANDARD_STATUS_BAR
, sb
.cols
, CENTER
);
337 * get the number of the oldest rbe that is (partially) visible. On return,
338 * lines contains the sum of the number of lines of all visable entries. If the
339 * first one is only partially visible, lines is greater than bot.lines.
341 static int first_visible_rbe(unsigned *lines
)
345 for (i
= scroll_position
; i
< RINGBUFFER_SIZE
; i
++) {
346 struct rb_entry
*rbe
= ringbuffer_get(bot_win_rb
, i
);
350 // fprintf(stderr, "found: %s\n", rbe->msg);
351 rbe_lines
= NUM_LINES(rbe
->len
);
352 if (rbe_lines
> bot
.lines
)
355 if (*lines
>= bot
.lines
)
358 return RINGBUFFER_SIZE
- 1;
361 static int draw_top_rbe(unsigned *lines
)
364 int offset
, fvr
= first_visible_rbe(lines
);
365 struct rb_entry
*rbe
;
369 wmove(bot
.win
, 0, 0);
370 rbe
= ringbuffer_get(bot_win_rb
, fvr
);
373 /* first rbe might be only partially visible */
374 offset
= (*lines
- bot
.lines
) * bot
.cols
;
375 len
= strlen(rbe
->msg
);
376 if (offset
< 0 || len
< offset
)
378 wattron(bot
.win
, COLOR_PAIR(rbe
->color
));
379 waddstr(bot
.win
, rbe
->msg
+ offset
);
380 *lines
= NUM_LINES(len
- offset
);
384 static void redraw_bot_win(void)
389 wmove(bot
.win
, 0, 0);
391 i
= draw_top_rbe(&lines
);
394 while (i
> 0 && lines
< bot
.lines
) {
395 struct rb_entry
*rbe
= ringbuffer_get(bot_win_rb
, --i
);
398 waddstr(bot
.win
, "\n");
401 lines
+= NUM_LINES(rbe
->len
);
402 wattron(bot
.win
, COLOR_PAIR(rbe
->color
));
403 waddstr(bot
.win
, "\n");
404 waddstr(bot
.win
, rbe
->msg
);
410 static void rb_add_entry(int color
, char *msg
)
412 struct rb_entry
*old
, *new = para_malloc(sizeof(struct rb_entry
));
415 new->len
= strlen(msg
);
417 old
= ringbuffer_add(bot_win_rb
, new);
418 // fprintf(stderr, "added: %s\n", new->msg);
423 if (scroll_position
) {
424 /* discard current scrolling, like xterm does */
429 wattron(bot
.win
, COLOR_PAIR(color
));
430 getyx(bot
.win
, y
, x
);
432 waddstr(bot
.win
, "\n");
433 waddstr(bot
.win
, msg
);
437 * print formated output to bot win and refresh
439 __printf_2_3
static void outputf(int color
, const char* fmt
,...)
445 PARA_VSPRINTF(fmt
, msg
);
446 rb_add_entry(color
, msg
);
450 static int add_output_line(char *line
, __a_unused
void *data
)
454 rb_add_entry(COLOR_OUTPUT
, para_strdup(line
));
458 void para_log(int ll
, const char *fmt
,...)
463 if (ll
< conf
.loglevel_arg
|| !curses_active
)
472 color
= COLOR_ERRMSG
;
474 PARA_VSPRINTF(fmt
, msg
);
476 rb_add_entry(color
, msg
);
480 static void setup_signal_handling(void)
482 signal_pipe
= para_signal_init();
483 para_install_sighandler(SIGINT
);
484 para_install_sighandler(SIGTERM
);
485 para_install_sighandler(SIGCHLD
);
486 para_install_sighandler(SIGWINCH
);
487 para_install_sighandler(SIGUSR1
);
488 signal(SIGPIPE
, SIG_IGN
);
489 signal(SIGHUP
, SIG_IGN
);
492 static void do_exit(int ret
)
494 signal(SIGTERM
, SIG_IGN
);
499 static void shutdown_curses(void)
508 static void finish(int ret
)
515 * exit curses and print given message to stdout/stderr
517 __printf_2_3
static void msg_n_exit(int ret
, const char* fmt
, ...)
520 FILE *outfd
= ret
? stderr
: stdout
;
524 vfprintf(outfd
, fmt
, argp
);
529 static void print_welcome(void)
531 int ll
= conf
.loglevel_arg
;
534 outputf(COLOR_WELCOME
, "Welcome to para_gui " PACKAGE_VERSION
535 " \"" CODENAME
"\". Theme: %s", theme
.name
);
542 static void init_wins(int top_lines
)
546 top
.lines
= top_lines
;
551 bot
.lines
= LINES
- top
.lines
- 3;
553 bot
.begy
= top
.lines
+ 1;
568 sep
.begy
= top
.lines
;
571 assume_default_colors(theme
.default_fg
, theme
.default_bg
);
573 mvwin(top
.win
, top
.begy
, top
.begx
);
574 wresize(top
.win
, top
.lines
, top
.cols
);
576 mvwin(sb
.win
, sb
.begy
, sb
.begx
);
577 wresize(sb
.win
, sb
.lines
, sb
.cols
);
579 mvwin(sep
.win
, sep
.begy
, sep
.begx
);
580 wresize(sep
.win
, sep
.lines
, sep
.cols
);
582 mvwin(bot
.win
, bot
.begy
, bot
.begx
);
583 wresize(bot
.win
, bot
.lines
, bot
.cols
);
585 mvwin(in
.win
, in
.begy
, in
.begx
);
586 wresize(in
.win
, in
.lines
, in
.cols
);
588 sep
.win
= newwin(sep
.lines
, sep
.cols
, sep
.begy
, sep
.begx
);
589 top
.win
= newwin(top
.lines
, top
.cols
, top
.begy
, top
.begx
);
590 bot
.win
= newwin(bot
.lines
, bot
.cols
, bot
.begy
, bot
.begx
);
591 sb
.win
= newwin(sb
.lines
, sb
.cols
, sb
.begy
, sb
.begx
);
592 in
.win
= newwin(in
.lines
, in
.cols
, in
.begy
, in
.begx
);
593 if (!top
.win
|| !bot
.win
|| !sb
.win
|| !in
.win
|| !sep
.win
)
594 msg_n_exit(1, "Error: Cannot create curses windows\n");
598 scrollok(bot
.win
, 1);
599 wattron(sb
.win
, COLOR_PAIR(COLOR_STATUSBAR
));
600 wattron(sep
.win
, COLOR_PAIR(COLOR_SEPARATOR
));
601 wattron(bot
.win
, COLOR_PAIR(COLOR_BOT
));
602 wattron(top
.win
, COLOR_PAIR(COLOR_TOP
));
614 wmove(sep
.win
, 0, 0);
615 for (i
= 1; i
<= COLS
; i
++)
616 waddstr(sep
.win
, theme
.sep_str
);
619 wnoutrefresh(top
.win
);
620 wnoutrefresh(bot
.win
);
621 //wnoutrefresh(sb.win);
623 wnoutrefresh(in
.win
);
624 wnoutrefresh(sep
.win
);
628 static void check_geometry(void)
630 if (LINES
< theme
.lines_min
|| COLS
< theme
.cols_min
)
631 msg_n_exit(EXIT_FAILURE
, "Error: Terminal (%dx%d) too small"
632 " (need at least %dx%d)\n", COLS
, LINES
,
633 theme
.cols_min
, theme
.lines_min
);
637 * Print stat item #i to curses window
639 static void print_stat_item(int i
)
642 struct stat_item_data d
= theme
.data
[i
];
643 char *c
= stat_content
[i
];
645 if (!curses_active
|| !d
.len
|| !c
)
647 tmp
= make_message("%s%s%s", d
.prefix
, c
, d
.postfix
);
648 // PARA_DEBUG_LOG("%s: read: %s\n", __func__, tmp);
649 wmove(top
.win
, d
.y
* top
.lines
/ 100, d
.x
* COLS
/ 100);
651 wattron(top
.win
, COLOR_PAIR(i
+ 1));
652 align_str(top
.win
, tmp
, d
.len
* COLS
/ 100, d
.align
);
657 static void print_all_items(void)
663 for (i
= 0; i
< NUM_STAT_ITEMS
; i
++)
666 static void clear_all_items(void)
670 for (i
= 0; i
< NUM_STAT_ITEMS
; i
++) {
671 free(stat_content
[i
]);
672 stat_content
[i
] = para_strdup("");
676 static void init_colors(void)
680 msg_n_exit(EXIT_FAILURE
, "Error: No color term\n");
682 for (i
= 0; i
< NUM_STAT_ITEMS
; i
++)
683 if (theme
.data
[i
].len
)
684 init_pair(i
+ 1, theme
.data
[i
].fg
, theme
.data
[i
].bg
);
685 init_pair(COLOR_STATUSBAR
, theme
.sb_fg
, theme
.sb_bg
);
686 init_pair(COLOR_COMMAND
, theme
.cmd_fg
, theme
.cmd_bg
);
687 init_pair(COLOR_OUTPUT
, theme
.output_fg
, theme
.output_bg
);
688 init_pair(COLOR_MSG
, theme
.msg_fg
, theme
.msg_bg
);
689 init_pair(COLOR_ERRMSG
, theme
.err_msg_fg
, theme
.err_msg_bg
);
690 init_pair(COLOR_WELCOME
, theme
.welcome_fg
, theme
.welcome_bg
);
691 init_pair(COLOR_SEPARATOR
, theme
.sep_fg
, theme
.sep_bg
);
692 init_pair(COLOR_TOP
, theme
.default_fg
, theme
.default_bg
);
693 init_pair(COLOR_BOT
, theme
.default_fg
, theme
.default_bg
);
697 * (re-)initialize the curses library FIXME: Error checking
699 static void init_curses(void)
702 if (top
.win
&& refresh() == ERR
) { /* refesh is really needed */
703 msg_n_exit(EXIT_FAILURE
, "refresh() failed\n");
706 curs_set(0); /* make cursor invisible, ignore errors */
707 // if (noraw() == ERR);
708 // msg_n_exit(EXIT_FAILURE, "can not place terminal out of "
710 nonl(); /* tell curses not to do NL->CR/NL on output */
711 noecho(); /* don't echo input */
712 cbreak(); /* take input chars one at a time, no wait for \n */
716 init_wins(theme
.top_lines_default
);
718 noecho(); /* don't echo input */
722 static void check_sigchld(void)
726 pid
= para_reap_child();
729 if (pid
== cmd_pid
) {
733 goto reap_next_child
;
737 * print status line if line starts with known command.
739 static int check_stat_line(char *line
, __a_unused
void *data
)
743 // PARA_INFO_LOG("%s: checking: %s\n", __func__, line);
744 i
= stat_line_valid(line
);
746 line
+= strlen(status_item_list
[i
]) + 1;
747 free(stat_content
[i
]);
748 stat_content
[i
] = para_strdup(line
);
755 * This sucker modifies its first argument. *handler and *arg are
756 * pointers to 0-terminated strings (inside line). Crap.
758 static int split_key_map(char *line
, char **handler
, char **arg
)
760 if (!(*handler
= strchr(line
+ 1, ':')))
764 if (!(*arg
= strchr(*handler
, ':')))
773 static int check_key_map_args(void)
777 char *tmp
= NULL
, *handler
, *arg
;
779 for (i
= 0; i
< conf
.key_map_given
; ++i
) {
780 s
= conf
.key_map_arg
[i
];
784 tmp
= para_strdup(s
);
785 if (!split_key_map(tmp
, &handler
, &arg
))
787 if (strlen(handler
) != 1)
796 if (find_cmd_byname(arg
) < 0)
806 * React to various signal-related events
808 static void handle_signal(int sig
)
812 msg_n_exit(EXIT_FAILURE
,
813 "only the good die young (caught SIGTERM))\n");
823 PARA_WARNING_LOG("%s", "caught SIGINT, reset");
824 /* Nothing to do. SIGINT killed our child, para_client stat.
825 * This get noticed by do_select which resets everything
829 PARA_NOTICE_LOG("%s", "got SIGUSR1, rereading configuration");
838 static int open_audiod_pipe(void)
846 return para_open_audiod_pipe(conf
.stat_cmd_arg
);
850 * This is the core select loop. Besides the (internal) signal
851 * pipe, the following other fds are checked according to the mode:
853 * GETCH_MODE: check stdin, return when key is pressed
855 * COMMAND_MODE: check command_pipe and stdin. Return when a screen full
856 * of output has been read or when other end has closed command pipe or
857 * when any key is pressed.
859 * EXTERNAL_MODE: Check only signal pipe. Used when an external command
860 * is running. During that thime curses is disabled. Returns when
863 static int do_select(int mode
)
867 int max_fileno
, cp_numread
= 1;
868 char command_buf
[4096] = "";
869 int cbo
= 0; /* command buf offset */
872 tv
.tv_sec
= conf
.timeout_arg
/ 1000;
873 tv
.tv_usec
= (conf
.timeout_arg
% 1000) * 1000;
874 // ret = refresh_status();
879 audiod_pipe
= open_audiod_pipe();
880 if (audiod_pipe
>= 0)
881 para_fd_set(audiod_pipe
, &rfds
, &max_fileno
);
883 para_fd_set(signal_pipe
, &rfds
, &max_fileno
);
884 /* command pipe only for COMMAND_MODE */
885 if (command_pipe
>= 0 && mode
== COMMAND_MODE
)
886 para_fd_set(command_pipe
, &rfds
, &max_fileno
);
887 ret
= para_select(max_fileno
+ 1, &rfds
, NULL
, &tv
);
889 goto check_return
; /* skip fd checks */
891 if (FD_ISSET(signal_pipe
, &rfds
)) {
892 int sig_nr
= para_next_signal();
894 handle_signal(sig_nr
);
896 /* read command pipe if ready */
897 if (command_pipe
>= 0 && mode
== COMMAND_MODE
&&
898 FD_ISSET(command_pipe
, &rfds
)) {
899 cp_numread
= read(command_pipe
, command_buf
+ cbo
,
900 sizeof(command_buf
) - 1 - cbo
);
905 PARA_ERROR_LOG("read error (%d)", cp_numread
);
910 if (audiod_pipe
>= 0 && FD_ISSET(audiod_pipe
, &rfds
))
911 if (read_audiod_pipe(audiod_pipe
, check_stat_line
) <= 0) {
915 free(stat_content
[SI_STATUS_BAR
]);
916 stat_content
[SI_STATUS_BAR
] =
917 para_strdup("audiod not running!?");
923 if (cp_numread
<= 0 && !cbo
) /* command complete */
926 cbo
= for_each_line(command_buf
, cbo
,
927 &add_output_line
, NULL
);
931 ret
= wgetch(top
.win
);
932 if (ret
!= ERR
&& ret
!= KEY_RESIZE
) {
938 kill(cmd_pid
, SIGTERM
);
943 ret
= wgetch(top
.win
);
944 if (ret
!= ERR
&& ret
!= KEY_RESIZE
)
957 * read from command pipe and print data to bot window
959 static int send_output(void)
961 if (command_pipe
< 0)
963 if (do_select(COMMAND_MODE
) >= 0)
964 PARA_INFO_LOG("%s", "command complete");
966 PARA_NOTICE_LOG("%s", "command aborted");
967 print_in_bar(COLOR_MSG
, " ");
971 static int client_cmd_cmdline(char *cmd
)
973 int ret
, fds
[3] = {0, 1, 0};
974 char *c
= make_message(BINDIR
"/para_client %s", cmd
);
976 outputf(COLOR_COMMAND
, "%s", c
);
977 print_in_bar(COLOR_MSG
, "executing client command, hit any key to abort\n");
978 ret
= para_exec_cmdline_pid(&cmd_pid
, c
, fds
);
982 command_pipe
= fds
[1];
983 mark_fd_nonblock(command_pipe
);
984 return send_output();
988 * exec command and print output to bot win
990 static int display_cmd(char *cmd
)
992 int fds
[3] = {0, 1, 0};
994 print_in_bar(COLOR_MSG
, "executing display command, hit any key to abort");
995 outputf(COLOR_COMMAND
, "%s", cmd
);
996 if (para_exec_cmdline_pid(&cmd_pid
, cmd
, fds
) < 0)
998 command_pipe
= fds
[1];
999 mark_fd_nonblock(command_pipe
);
1000 return send_output();
1004 * shutdown curses and stat pipe before executing external commands
1006 static int external_cmd(char *cmd
)
1008 int fds
[3] = {-1, -1, -1};
1013 para_exec_cmdline_pid(&cmd_pid
, cmd
, fds
);
1015 do_select(EXTERNAL_MODE
);
1020 static void print_scroll_msg(void)
1022 unsigned lines_total
, filled
= ringbuffer_filled(bot_win_rb
);
1023 int first_rbe
= first_visible_rbe(&lines_total
);
1024 print_in_bar(COLOR_MSG
, "scrolled view: %d-%d/%d\n", filled
- first_rbe
,
1025 filled
- scroll_position
, ringbuffer_filled(bot_win_rb
));
1028 static void com_page_down(void)
1031 int i
= scroll_position
;
1032 while (lines
< bot
.lines
&& --i
> 0) {
1033 struct rb_entry
*rbe
= ringbuffer_get(bot_win_rb
, i
);
1036 lines
+= NUM_LINES(strlen(rbe
->msg
));
1039 scroll_position
= i
;
1044 print_in_bar(COLOR_ERRMSG
, "bottom of buffer is shown\n");
1047 static void com_page_up(void)
1050 int fvr
= first_visible_rbe(&lines
);
1051 if (fvr
< 0 || fvr
+ 1 >= ringbuffer_filled(bot_win_rb
)) {
1052 print_in_bar(COLOR_ERRMSG
, "top of buffer is shown\n");
1055 scroll_position
= fvr
+ 1;
1056 for (; scroll_position
> 0; scroll_position
--) {
1057 fvr
= first_visible_rbe(&lines
);
1058 if (lines
== bot
.lines
)
1065 static void com_scroll_down(void)
1067 struct rb_entry
*rbe
;
1070 if (!scroll_position
) {
1071 print_in_bar(COLOR_ERRMSG
, "bottom of buffer is shown\n");
1075 rbe
= ringbuffer_get(bot_win_rb
, scroll_position
);
1076 rbe_lines
= NUM_LINES(rbe
->len
);
1077 wscrl(bot
.win
, rbe_lines
);
1078 wmove(bot
.win
, bot
.lines
- rbe_lines
, 0);
1079 wattron(bot
.win
, COLOR_PAIR(rbe
->color
));
1080 waddstr(bot
.win
, rbe
->msg
);
1085 static void com_scroll_up(void)
1087 struct rb_entry
*rbe
= NULL
;
1089 int i
, first_rbe
, num_scroll
;
1091 /* the entry that is going to vanish */
1092 rbe
= ringbuffer_get(bot_win_rb
, scroll_position
);
1095 num_scroll
= NUM_LINES(rbe
->len
);
1096 first_rbe
= first_visible_rbe(&lines
);
1097 if (first_rbe
< 0 || (first_rbe
== ringbuffer_filled(bot_win_rb
) - 1))
1100 wscrl(bot
.win
, -num_scroll
);
1101 i
= draw_top_rbe(&lines
);
1104 while (i
> 0 && lines
< num_scroll
) {
1106 rbe
= ringbuffer_get(bot_win_rb
, --i
);
1109 rbe_lines
= NUM_LINES(rbe
->len
);
1111 // fprintf(stderr, "msg: %s\n", rbe->msg);
1112 wattron(bot
.win
, COLOR_PAIR(rbe
->color
));
1113 waddstr(bot
.win
, "\n");
1114 waddstr(bot
.win
, rbe
->msg
);
1123 print_in_bar(COLOR_ERRMSG
, "top of buffer is shown\n");
1126 static void com_ll_decr(void)
1128 if (conf
.loglevel_arg
<= DEBUG
) {
1129 print_in_bar(COLOR_ERRMSG
,
1130 "loglevel already at maximal verbosity\n");
1133 conf
.loglevel_arg
--;
1134 print_in_bar(COLOR_MSG
, "loglevel set to %d\n", conf
.loglevel_arg
);
1137 static void com_ll_incr(void)
1139 if (conf
.loglevel_arg
>= EMERG
) {
1140 print_in_bar(COLOR_ERRMSG
,
1141 "loglevel already at miminal verbosity\n");
1144 conf
.loglevel_arg
++;
1145 print_in_bar(COLOR_MSG
, "loglevel set to %d\n", conf
.loglevel_arg
);
1149 * reread configuration, terminate on errors
1151 static void com_reread_conf(void)
1153 char *cf
=configfile_exists();
1154 struct gui_cmdline_parser_params params
= {
1157 .check_required
= 0,
1158 .check_ambiguity
= 0
1162 PARA_WARNING_LOG("%s", "there is no configuration to read");
1165 PARA_INFO_LOG("%s", "rereading command line options and config file");
1166 gui_cmdline_parser(_argc
, _argv
, &conf
);
1167 gui_cmdline_parser_config_file(cf
, &conf
, ¶ms
);
1168 PARA_NOTICE_LOG("%s", "config file reloaded");
1169 if (check_key_map_args() < 0)
1170 finish(EXIT_FAILURE
);
1173 static void com_help(void)
1177 for (i
= 0; i
< conf
.key_map_given
; ++i
) {
1178 char *handler
, *arg
, *tmp
= para_strdup(conf
.key_map_arg
[i
]);
1179 const char *handler_text
= "???", *desc
= NULL
;
1181 if (!split_key_map(tmp
, &handler
, &arg
)) {
1187 handler_text
= "internal";
1188 desc
= command_list
[find_cmd_byname(arg
)].description
;
1190 case 'x': handler_text
= "external"; break;
1191 case 'd': handler_text
= "display "; break;
1192 case 'p': handler_text
= "para "; break;
1194 outputf(COLOR_MSG
, "%s\t%s\t%s%s\t%s", tmp
, handler_text
, arg
,
1195 strlen(arg
) < 8? "\t" : "",
1199 for (i
= 0; command_list
[i
].handler
; i
++) {
1200 struct gui_command gc
= command_list
[i
];
1202 outputf(COLOR_MSG
, "%s\tinternal\t%s\t%s%s", gc
.key
, gc
.name
,
1203 strlen(gc
.name
) < 8? "\t" : "",
1206 print_in_bar(COLOR_MSG
, "try \"para_gui -h\" or \"para_client help\" "
1210 static void com_shrink_top_win(void)
1212 if (top
.lines
<= theme
.top_lines_min
) {
1213 PARA_WARNING_LOG("%s", "can not decrease top window");
1216 init_wins(top
.lines
- 1);
1219 print_in_bar(COLOR_MSG
, "%s", "decreased top window");
1222 static void com_enlarge_top_win(void)
1224 if (bot
.lines
< 3) {
1225 PARA_WARNING_LOG("%s", "can not increase top window");
1228 init_wins(top
.lines
+ 1);
1231 print_in_bar(COLOR_MSG
, "increased top window");
1234 static void com_version(void)
1236 print_in_bar(COLOR_MSG
, "para_gui " PACKAGE_VERSION
" \""
1240 static void com_quit(void)
1245 static void com_refresh(void)
1251 static void change_theme(int next
)
1257 /* This seems to be needed twice, why? */
1260 PARA_NOTICE_LOG("new theme: %s", theme
.name
);
1263 static void com_next_theme(void)
1268 static void com_prev_theme(void)
1274 static void handle_command(int c
)
1278 /* first check user's key bindings */
1279 for (i
= 0; i
< conf
.key_map_given
; ++i
) {
1280 char tmp
[MAXLINE
], *handler
, *arg
;
1282 strcpy(tmp
, conf
.key_map_arg
[i
]);
1283 if (!split_key_map(tmp
, &handler
, &arg
))
1285 if (!strcmp(tmp
, km_keyname(c
))) {
1286 if (*handler
== 'd') {
1290 if (*handler
== 'x') {
1294 if (*handler
== 'p') {
1295 client_cmd_cmdline(arg
);
1298 if (*handler
== 'i') {
1299 int num
= find_cmd_byname(arg
);
1301 command_list
[num
].handler();
1306 /* not found, check internal key bindings */
1307 for (i
= 0; command_list
[i
].handler
; i
++) {
1308 if (!strcmp(km_keyname(c
), command_list
[i
].key
)) {
1309 command_list
[i
].handler();
1313 print_in_bar(COLOR_ERRMSG
, "key '%s' is not bound, press ? for help",
1317 int main(int argc
, char *argv
[])
1325 if (gui_cmdline_parser(argc
, argv
, &conf
)) {
1326 fprintf(stderr
, "parse error while reading command line\n");
1329 HANDLE_VERSION_FLAG("gui", conf
);
1330 init_theme(0, &theme
);
1331 top
.lines
= theme
.top_lines_default
;
1332 if (check_key_map_args() < 0) {
1333 fprintf(stderr
, "invalid key map\n");
1336 cf
= configfile_exists();
1337 if (!cf
&& conf
.config_file_given
) {
1338 fprintf(stderr
, "can not read config file %s\n",
1339 conf
.config_file_arg
);
1343 struct gui_cmdline_parser_params params
= {
1346 .check_required
= 0,
1347 .check_ambiguity
= 0
1349 gui_cmdline_parser_config_file(cf
, &conf
, ¶ms
);
1351 if (check_key_map_args() < 0) {
1352 fprintf(stderr
, "invalid key map in config file\n");
1355 setup_signal_handling();
1356 bot_win_rb
= ringbuffer_new(RINGBUFFER_SIZE
);
1357 initscr(); /* needed only once, always successful */
1362 ret
= do_select(GETCH_MODE
);
1365 print_in_bar(COLOR_MSG
, " ");
1366 handle_command(ret
);