2 * Copyright (C) 1998-2009 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file gui.c Curses-based interface for paraslash. */
10 #include <sys/types.h>
12 #include "gui.cmdline.h"
17 #include "ringbuffer.h"
24 /** define the array of error lists needed by para_gui */
26 static char *stat_content
[NUM_STAT_ITEMS
];
28 #define STANDARD_STATUS_BAR "para_gui " PACKAGE_VERSION " (hit ? for help)"
30 static int signal_pipe
;
32 static struct win_data
{
38 } top
, bot
, sb
, in
, sep
;
40 #define RINGBUFFER_SIZE 512
46 struct ringbuffer
*bot_win_rb
;
47 #define NUM_LINES(len) (1 + (len) / bot.cols)
49 static unsigned scroll_position
;
51 static int cmd_died
, curses_active
;
54 static int command_pipe
= -1;
55 static int audiod_pipe
= -1;
56 static struct gui_args_info conf
;
58 enum {GETCH_MODE
, COMMAND_MODE
, EXTERNAL_MODE
};
61 #define COLOR_STATUSBAR 52
62 #define COLOR_COMMAND 53
63 #define COLOR_OUTPUT 54
65 #define COLOR_ERRMSG 56
66 #define COLOR_WELCOME 57
67 #define COLOR_SEPARATOR 58
74 const char *description
;
75 void (*handler
)(void);
81 char postfix
[MAXLINE
];
87 char content
[MAXLINE
];
90 static struct gui_theme theme
;
95 static void com_help(void);
96 static void com_reread_conf(void);
97 static void com_enlarge_top_win(void);
98 static void com_shrink_top_win(void);
99 static void com_version(void);
100 static void com_quit(void);
101 static void com_refresh(void);
102 static void com_ll_incr(void);
103 static void com_ll_decr(void);
104 static void com_prev_theme(void);
105 static void com_next_theme(void);
106 static void com_scroll_up(void);
107 static void com_scroll_down(void);
108 static void com_page_up(void);
109 static void com_page_down(void);
111 struct gui_command command_list
[] = {
115 .description
= "print help",
119 .name
= "enlarge_win",
120 .description
= "enlarge the top window",
121 .handler
= com_enlarge_top_win
124 .name
= "shrink_win",
125 .description
= "shrink the top window",
126 .handler
= com_shrink_top_win
129 .name
= "reread_conf",
130 .description
= "reread configuration file",
131 .handler
= com_reread_conf
135 .description
= "exit para_gui",
140 .description
= "redraw the screen",
141 .handler
= com_refresh
144 .name
= "next_theme",
145 .description
= "switch to next theme",
146 .handler
= com_next_theme
149 .name
= "prev_theme",
150 .description
= "switch to previous stream",
151 .handler
= com_prev_theme
155 .description
= "increase loglevel (decreases verbosity)",
156 .handler
= com_ll_incr
160 .description
= "decrease loglevel (increases verbosity)",
161 .handler
= com_ll_decr
165 .description
= "show the para_gui version",
166 .handler
= com_version
170 .description
= "scroll up one line",
171 .handler
= com_scroll_up
174 .name
= "scroll_down",
175 .description
= "scroll down one line",
176 .handler
= com_scroll_down
180 .description
= "scroll up one page",
181 .handler
= com_page_up
185 .description
= "scroll down one page",
186 .handler
= com_page_down
192 static int para_open_audiod_pipe(char *cmd
)
194 int fds
[3] = {0, 1, 0};
196 int ret
= para_exec_cmdline_pid(&pid
, cmd
, fds
);
199 ret
= mark_fd_nonblocking(fds
[1]);
206 static int read_audiod_pipe(int fd
, line_handler_t
*line_handler
)
208 static char buf
[4096];
209 const ssize_t bufsize
= sizeof(buf
) - 1;
210 static ssize_t loaded
;
213 if (loaded
>= bufsize
)
215 ret
= read(fd
, buf
+ loaded
, bufsize
- loaded
);
219 loaded
= for_each_line(buf
, loaded
, line_handler
, NULL
);
224 static int find_cmd_byname(char *name
)
228 for (i
= 0; command_list
[i
].handler
; i
++)
229 if (!strcmp(command_list
[i
].name
, name
))
234 /* taken from mutt */
235 static char *km_keyname(int c
)
240 sprintf(buf
, "<up>");
244 sprintf(buf
, "<down>");
248 sprintf(buf
, "<left>");
251 if (c
== KEY_RIGHT
) {
252 sprintf(buf
, "<right>");
255 if (c
== KEY_NPAGE
) {
256 sprintf(buf
, "<npage>");
259 if (c
== KEY_PPAGE
) {
260 sprintf(buf
, "<ppage>");
263 if (c
< 256 && c
> -128 && iscntrl((unsigned char) c
)) {
268 buf
[1] = (c
+ '@') & 0x7f;
271 snprintf(buf
, sizeof(buf
), "\\%d%d%d", c
>> 6,
272 (c
>> 3) & 7, c
& 7);
273 } else if (c
>= KEY_F0
&& c
< KEY_F(256))
274 sprintf(buf
, "<F%d>", c
- KEY_F0
);
276 snprintf(buf
, sizeof(buf
), "%c", (unsigned char) c
);
278 snprintf(buf
, sizeof(buf
), "\\x%hx", (unsigned short) c
);
282 static char *configfile_exists(void)
284 static char *config_file
;
287 if (!conf
.config_file_given
) {
289 char *home
= para_homedir();
290 config_file
= make_message("%s/.paraslash/gui.conf",
296 tmp
= conf
.config_file_arg
;
297 return file_exists(tmp
)? tmp
: NULL
;
301 * print num spaces to curses window
303 static void add_spaces(WINDOW
* win
, unsigned int num
)
312 * print aligned string to curses window. This function always prints
315 static int align_str(WINDOW
* win
, const char *string
, unsigned int len
,
318 int num
; /* of spaces */
323 num
= len
- strlen(string
);
324 str
= para_strdup(string
);
331 add_spaces(win
, num
);
332 } else if (align
== RIGHT
) {
333 add_spaces(win
, num
);
336 add_spaces(win
, num
/ 2);
337 waddstr(win
, str
[0]? str
: "");
338 add_spaces(win
, num
- num
/ 2);
344 __printf_2_3
static void print_in_bar(int color
, const char *fmt
,...)
350 wattron(in
.win
, COLOR_PAIR(color
));
351 PARA_VSPRINTF(fmt
, msg
);
353 align_str(in
.win
, msg
, sb
.cols
, LEFT
);
359 * update the status bar
361 static void print_status_bar(void)
366 align_str(sb
.win
, STANDARD_STATUS_BAR
, sb
.cols
, CENTER
);
371 * get the number of the oldest rbe that is (partially) visible. On return,
372 * lines contains the sum of the number of lines of all visable entries. If the
373 * first one is only partially visible, lines is greater than bot.lines.
375 static int first_visible_rbe(unsigned *lines
)
379 for (i
= scroll_position
; i
< RINGBUFFER_SIZE
; i
++) {
380 struct rb_entry
*rbe
= ringbuffer_get(bot_win_rb
, i
);
384 // fprintf(stderr, "found: %s\n", rbe->msg);
385 rbe_lines
= NUM_LINES(rbe
->len
);
386 if (rbe_lines
> bot
.lines
)
389 if (*lines
>= bot
.lines
)
392 return RINGBUFFER_SIZE
- 1;
395 static int draw_top_rbe(unsigned *lines
)
398 int offset
, fvr
= first_visible_rbe(lines
);
399 struct rb_entry
*rbe
;
403 wmove(bot
.win
, 0, 0);
404 rbe
= ringbuffer_get(bot_win_rb
, fvr
);
407 /* first rbe might be only partially visible */
408 offset
= (*lines
- bot
.lines
) * bot
.cols
;
409 len
= strlen(rbe
->msg
);
410 if (offset
< 0 || len
< offset
)
412 wattron(bot
.win
, COLOR_PAIR(rbe
->color
));
413 waddstr(bot
.win
, rbe
->msg
+ offset
);
414 *lines
= NUM_LINES(len
- offset
);
418 static void redraw_bot_win(void)
423 wmove(bot
.win
, 0, 0);
425 i
= draw_top_rbe(&lines
);
428 while (i
> 0 && lines
< bot
.lines
) {
429 struct rb_entry
*rbe
= ringbuffer_get(bot_win_rb
, --i
);
432 waddstr(bot
.win
, "\n");
435 lines
+= NUM_LINES(rbe
->len
);
436 wattron(bot
.win
, COLOR_PAIR(rbe
->color
));
437 waddstr(bot
.win
, "\n");
438 waddstr(bot
.win
, rbe
->msg
);
444 static void rb_add_entry(int color
, char *msg
)
446 struct rb_entry
*old
, *new = para_malloc(sizeof(struct rb_entry
));
449 new->len
= strlen(msg
);
451 old
= ringbuffer_add(bot_win_rb
, new);
452 // fprintf(stderr, "added: %s\n", new->msg);
457 if (scroll_position
) {
458 /* discard current scrolling, like xterm does */
463 wattron(bot
.win
, COLOR_PAIR(color
));
464 getyx(bot
.win
, y
, x
);
466 waddstr(bot
.win
, "\n");
467 waddstr(bot
.win
, msg
);
471 * print formated output to bot win and refresh
473 __printf_2_3
static void outputf(int color
, const char* fmt
,...)
479 PARA_VSPRINTF(fmt
, msg
);
480 rb_add_entry(color
, msg
);
484 static int add_output_line(char *line
, __a_unused
void *data
)
488 rb_add_entry(COLOR_OUTPUT
, para_strdup(line
));
494 __printf_2_3
void para_log(int ll
, const char *fmt
,...)
499 if (ll
< loglevel
|| !curses_active
)
508 color
= COLOR_ERRMSG
;
510 PARA_VSPRINTF(fmt
, msg
);
512 rb_add_entry(color
, msg
);
516 static void setup_signal_handling(void)
518 signal_pipe
= para_signal_init();
519 para_install_sighandler(SIGINT
);
520 para_install_sighandler(SIGTERM
);
521 para_install_sighandler(SIGCHLD
);
522 para_install_sighandler(SIGWINCH
);
523 para_install_sighandler(SIGUSR1
);
524 // signal(SIGPIPE, SIG_IGN);
525 signal(SIGHUP
, SIG_IGN
);
528 __noreturn
static void do_exit(int ret
)
530 signal(SIGTERM
, SIG_IGN
);
535 static void shutdown_curses(void)
544 __noreturn
static void finish(int ret
)
551 * exit curses and print given message to stdout/stderr
553 __noreturn __printf_2_3
static void msg_n_exit(int ret
, const char* fmt
, ...)
556 FILE *outfd
= ret
? stderr
: stdout
;
560 vfprintf(outfd
, fmt
, argp
);
565 static void print_welcome(void)
567 if (loglevel
> LL_NOTICE
)
569 outputf(COLOR_WELCOME
, "Welcome to para_gui " PACKAGE_VERSION
570 " \"" CODENAME
"\". Theme: %s", theme
.name
);
577 static void init_wins(int top_lines
)
581 top
.lines
= top_lines
;
586 bot
.lines
= LINES
- top
.lines
- 3;
588 bot
.begy
= top
.lines
+ 1;
603 sep
.begy
= top
.lines
;
606 assume_default_colors(theme
.default_fg
, theme
.default_bg
);
608 mvwin(top
.win
, top
.begy
, top
.begx
);
609 wresize(top
.win
, top
.lines
, top
.cols
);
611 mvwin(sb
.win
, sb
.begy
, sb
.begx
);
612 wresize(sb
.win
, sb
.lines
, sb
.cols
);
614 mvwin(sep
.win
, sep
.begy
, sep
.begx
);
615 wresize(sep
.win
, sep
.lines
, sep
.cols
);
617 mvwin(bot
.win
, bot
.begy
, bot
.begx
);
618 wresize(bot
.win
, bot
.lines
, bot
.cols
);
620 mvwin(in
.win
, in
.begy
, in
.begx
);
621 wresize(in
.win
, in
.lines
, in
.cols
);
623 sep
.win
= newwin(sep
.lines
, sep
.cols
, sep
.begy
, sep
.begx
);
624 top
.win
= newwin(top
.lines
, top
.cols
, top
.begy
, top
.begx
);
625 bot
.win
= newwin(bot
.lines
, bot
.cols
, bot
.begy
, bot
.begx
);
626 sb
.win
= newwin(sb
.lines
, sb
.cols
, sb
.begy
, sb
.begx
);
627 in
.win
= newwin(in
.lines
, in
.cols
, in
.begy
, in
.begx
);
628 if (!top
.win
|| !bot
.win
|| !sb
.win
|| !in
.win
|| !sep
.win
)
629 msg_n_exit(1, "Error: Cannot create curses windows\n");
633 scrollok(bot
.win
, 1);
634 wattron(sb
.win
, COLOR_PAIR(COLOR_STATUSBAR
));
635 wattron(sep
.win
, COLOR_PAIR(COLOR_SEPARATOR
));
636 wattron(bot
.win
, COLOR_PAIR(COLOR_BOT
));
637 wattron(top
.win
, COLOR_PAIR(COLOR_TOP
));
649 wmove(sep
.win
, 0, 0);
650 for (i
= 1; i
<= COLS
; i
++)
651 waddstr(sep
.win
, theme
.sep_str
);
654 wnoutrefresh(top
.win
);
655 wnoutrefresh(bot
.win
);
656 //wnoutrefresh(sb.win);
658 wnoutrefresh(in
.win
);
659 wnoutrefresh(sep
.win
);
663 static void check_geometry(void)
665 if (LINES
< theme
.lines_min
|| COLS
< theme
.cols_min
)
666 msg_n_exit(EXIT_FAILURE
, "Error: Terminal (%dx%d) too small"
667 " (need at least %dx%d)\n", COLS
, LINES
,
668 theme
.cols_min
, theme
.lines_min
);
672 * Print stat item #i to curses window
674 static void print_stat_item(int i
)
677 struct stat_item_data d
= theme
.data
[i
];
678 char *c
= stat_content
[i
];
680 if (!curses_active
|| !d
.len
|| !c
)
682 tmp
= make_message("%s%s%s", d
.prefix
, c
, d
.postfix
);
683 wmove(top
.win
, d
.y
* top
.lines
/ 100, d
.x
* COLS
/ 100);
685 wattron(top
.win
, COLOR_PAIR(i
+ 1));
686 align_str(top
.win
, tmp
, d
.len
* COLS
/ 100, d
.align
);
691 static void print_all_items(void)
697 FOR_EACH_STATUS_ITEM(i
)
701 static void clear_all_items(void)
705 FOR_EACH_STATUS_ITEM(i
) {
706 free(stat_content
[i
]);
707 stat_content
[i
] = para_strdup("");
711 static void init_colors(void)
716 msg_n_exit(EXIT_FAILURE
, "Error: No color term\n");
718 FOR_EACH_STATUS_ITEM(i
)
719 if (theme
.data
[i
].len
)
720 init_pair(i
+ 1, theme
.data
[i
].fg
, theme
.data
[i
].bg
);
721 init_pair(COLOR_STATUSBAR
, theme
.sb_fg
, theme
.sb_bg
);
722 init_pair(COLOR_COMMAND
, theme
.cmd_fg
, theme
.cmd_bg
);
723 init_pair(COLOR_OUTPUT
, theme
.output_fg
, theme
.output_bg
);
724 init_pair(COLOR_MSG
, theme
.msg_fg
, theme
.msg_bg
);
725 init_pair(COLOR_ERRMSG
, theme
.err_msg_fg
, theme
.err_msg_bg
);
726 init_pair(COLOR_WELCOME
, theme
.welcome_fg
, theme
.welcome_bg
);
727 init_pair(COLOR_SEPARATOR
, theme
.sep_fg
, theme
.sep_bg
);
728 init_pair(COLOR_TOP
, theme
.default_fg
, theme
.default_bg
);
729 init_pair(COLOR_BOT
, theme
.default_fg
, theme
.default_bg
);
733 * (re-)initialize the curses library FIXME: Error checking
735 static void init_curses(void)
738 if (top
.win
&& refresh() == ERR
) { /* refesh is really needed */
739 msg_n_exit(EXIT_FAILURE
, "refresh() failed\n");
742 curs_set(0); /* make cursor invisible, ignore errors */
743 // if (noraw() == ERR);
744 // msg_n_exit(EXIT_FAILURE, "can not place terminal out of "
746 nonl(); /* tell curses not to do NL->CR/NL on output */
747 noecho(); /* don't echo input */
748 cbreak(); /* take input chars one at a time, no wait for \n */
752 init_wins(theme
.top_lines_default
);
754 noecho(); /* don't echo input */
758 static void check_sigchld(void)
763 ret
= para_reap_child(&pid
);
766 if (pid
== cmd_pid
) {
770 goto reap_next_child
;
774 * print status line if line starts with known command.
776 static int check_stat_line(char *line
, __a_unused
void *data
)
780 // PARA_INFO_LOG("%s: checking: %s\n", __func__, line);
781 i
= stat_line_valid(line
);
783 line
+= strlen(status_item_list
[i
]) + 1;
786 free(stat_content
[i
]);
787 stat_content
[i
] = para_strdup(line
);
794 * This sucker modifies its first argument. *handler and *arg are
795 * pointers to 0-terminated strings (inside line). Crap.
797 static int split_key_map(char *line
, char **handler
, char **arg
)
799 if (!(*handler
= strchr(line
+ 1, ':')))
803 if (!(*arg
= strchr(*handler
, ':')))
812 static int check_key_map_args(void)
816 char *tmp
= NULL
, *handler
, *arg
;
818 for (i
= 0; i
< conf
.key_map_given
; ++i
) {
819 s
= conf
.key_map_arg
[i
];
823 tmp
= para_strdup(s
);
824 if (!split_key_map(tmp
, &handler
, &arg
))
826 if (strlen(handler
) != 1)
835 if (find_cmd_byname(arg
) < 0)
845 * React to various signal-related events
847 static void handle_signal(int sig
)
851 msg_n_exit(EXIT_FAILURE
,
852 "only the good die young (caught SIGTERM))\n");
862 PARA_WARNING_LOG("caught SIGINT, reset");
863 /* Nothing to do. SIGINT killed our child, para_client stat.
864 * This get noticed by do_select which resets everything
868 PARA_NOTICE_LOG("got SIGUSR1, rereading configuration");
877 static int open_audiod_pipe(void)
885 return para_open_audiod_pipe(conf
.stat_cmd_arg
);
889 * This is the core select loop. Besides the (internal) signal
890 * pipe, the following other fds are checked according to the mode:
892 * GETCH_MODE: check stdin, return when key is pressed
894 * COMMAND_MODE: check command_pipe and stdin. Return when a screen full
895 * of output has been read or when other end has closed command pipe or
896 * when any key is pressed.
898 * EXTERNAL_MODE: Check only signal pipe. Used when an external command
899 * is running. During that time curses is disabled. Returns when
902 static int do_select(int mode
)
906 int max_fileno
, cp_numread
= 1;
907 char command_buf
[4096] = "";
908 int cbo
= 0; /* command buf offset */
911 tv
.tv_sec
= conf
.timeout_arg
/ 1000;
912 tv
.tv_usec
= (conf
.timeout_arg
% 1000) * 1000;
913 // ret = refresh_status();
918 audiod_pipe
= open_audiod_pipe();
919 if (audiod_pipe
>= 0)
920 para_fd_set(audiod_pipe
, &rfds
, &max_fileno
);
922 para_fd_set(signal_pipe
, &rfds
, &max_fileno
);
923 /* command pipe only for COMMAND_MODE */
924 if (command_pipe
>= 0 && mode
== COMMAND_MODE
)
925 para_fd_set(command_pipe
, &rfds
, &max_fileno
);
926 ret
= para_select(max_fileno
+ 1, &rfds
, NULL
, &tv
);
928 goto check_return
; /* skip fd checks */
930 if (FD_ISSET(signal_pipe
, &rfds
)) {
931 int sig_nr
= para_next_signal();
933 handle_signal(sig_nr
);
935 /* read command pipe if ready */
936 if (command_pipe
>= 0 && mode
== COMMAND_MODE
&&
937 FD_ISSET(command_pipe
, &rfds
)) {
938 cp_numread
= read(command_pipe
, command_buf
+ cbo
,
939 sizeof(command_buf
) - 1 - cbo
);
944 PARA_ERROR_LOG("read error (%d)", cp_numread
);
949 if (audiod_pipe
>= 0 && FD_ISSET(audiod_pipe
, &rfds
))
950 if (read_audiod_pipe(audiod_pipe
, check_stat_line
) <= 0) {
954 free(stat_content
[SI_BASENAME
]);
955 stat_content
[SI_BASENAME
] =
956 para_strdup("audiod not running!?");
962 if (cp_numread
<= 0 && !cbo
) /* command complete */
965 cbo
= for_each_line(command_buf
, cbo
,
966 &add_output_line
, NULL
);
970 ret
= wgetch(top
.win
);
971 if (ret
!= ERR
&& ret
!= KEY_RESIZE
) {
977 kill(cmd_pid
, SIGTERM
);
982 ret
= wgetch(top
.win
);
983 if (ret
!= ERR
&& ret
!= KEY_RESIZE
)
996 * read from command pipe and print data to bot window
998 static int send_output(void)
1002 if (command_pipe
< 0)
1004 ret
= mark_fd_nonblocking(command_pipe
);
1006 close(command_pipe
);
1009 if (do_select(COMMAND_MODE
) >= 0)
1010 PARA_INFO_LOG("command complete");
1012 PARA_NOTICE_LOG("command aborted");
1013 print_in_bar(COLOR_MSG
, " ");
1017 static int client_cmd_cmdline(char *cmd
)
1019 int ret
, fds
[3] = {0, 1, 0};
1020 char *c
= make_message(BINDIR
"/para_client -- %s", cmd
);
1022 outputf(COLOR_COMMAND
, "%s", c
);
1023 print_in_bar(COLOR_MSG
, "executing client command, hit any key to abort\n");
1024 ret
= para_exec_cmdline_pid(&cmd_pid
, c
, fds
);
1028 command_pipe
= fds
[1];
1029 return send_output();
1033 * exec command and print output to bot win
1035 static int display_cmd(char *cmd
)
1037 int fds
[3] = {0, 1, 0};
1039 print_in_bar(COLOR_MSG
, "executing display command, hit any key to abort");
1040 outputf(COLOR_COMMAND
, "%s", cmd
);
1041 if (para_exec_cmdline_pid(&cmd_pid
, cmd
, fds
) < 0)
1043 command_pipe
= fds
[1];
1044 return send_output();
1048 * shutdown curses and stat pipe before executing external commands
1050 static int external_cmd(char *cmd
)
1052 int fds
[3] = {-1, -1, -1};
1057 para_exec_cmdline_pid(&cmd_pid
, cmd
, fds
);
1059 do_select(EXTERNAL_MODE
);
1064 static void print_scroll_msg(void)
1066 unsigned lines_total
, filled
= ringbuffer_filled(bot_win_rb
);
1067 int first_rbe
= first_visible_rbe(&lines_total
);
1068 print_in_bar(COLOR_MSG
, "scrolled view: %d-%d/%d\n", filled
- first_rbe
,
1069 filled
- scroll_position
, ringbuffer_filled(bot_win_rb
));
1072 static void com_page_down(void)
1075 int i
= scroll_position
;
1076 while (lines
< bot
.lines
&& --i
> 0) {
1077 struct rb_entry
*rbe
= ringbuffer_get(bot_win_rb
, i
);
1080 lines
+= NUM_LINES(strlen(rbe
->msg
));
1083 scroll_position
= i
;
1088 print_in_bar(COLOR_ERRMSG
, "bottom of buffer is shown\n");
1091 static void com_page_up(void)
1094 int fvr
= first_visible_rbe(&lines
);
1096 if (fvr
< 0 || fvr
+ 1 >= ringbuffer_filled(bot_win_rb
)) {
1097 print_in_bar(COLOR_ERRMSG
, "top of buffer is shown\n");
1100 scroll_position
= fvr
+ 1;
1101 for (; scroll_position
> 0; scroll_position
--) {
1102 first_visible_rbe(&lines
);
1103 if (lines
== bot
.lines
)
1110 static void com_scroll_down(void)
1112 struct rb_entry
*rbe
;
1115 if (!scroll_position
) {
1116 print_in_bar(COLOR_ERRMSG
, "bottom of buffer is shown\n");
1120 rbe
= ringbuffer_get(bot_win_rb
, scroll_position
);
1121 rbe_lines
= NUM_LINES(rbe
->len
);
1122 wscrl(bot
.win
, rbe_lines
);
1123 wmove(bot
.win
, bot
.lines
- rbe_lines
, 0);
1124 wattron(bot
.win
, COLOR_PAIR(rbe
->color
));
1125 waddstr(bot
.win
, rbe
->msg
);
1130 static void com_scroll_up(void)
1132 struct rb_entry
*rbe
= NULL
;
1134 int i
, first_rbe
, num_scroll
;
1136 /* the entry that is going to vanish */
1137 rbe
= ringbuffer_get(bot_win_rb
, scroll_position
);
1140 num_scroll
= NUM_LINES(rbe
->len
);
1141 first_rbe
= first_visible_rbe(&lines
);
1142 if (first_rbe
< 0 || (first_rbe
== ringbuffer_filled(bot_win_rb
) - 1))
1145 wscrl(bot
.win
, -num_scroll
);
1146 i
= draw_top_rbe(&lines
);
1149 while (i
> 0 && lines
< num_scroll
) {
1151 rbe
= ringbuffer_get(bot_win_rb
, --i
);
1154 rbe_lines
= NUM_LINES(rbe
->len
);
1156 // fprintf(stderr, "msg: %s\n", rbe->msg);
1157 wattron(bot
.win
, COLOR_PAIR(rbe
->color
));
1158 waddstr(bot
.win
, "\n");
1159 waddstr(bot
.win
, rbe
->msg
);
1168 print_in_bar(COLOR_ERRMSG
, "top of buffer is shown\n");
1171 static void com_ll_decr(void)
1173 if (loglevel
<= LL_DEBUG
) {
1174 print_in_bar(COLOR_ERRMSG
,
1175 "loglevel already at maximal verbosity\n");
1179 print_in_bar(COLOR_MSG
, "loglevel set to %d\n", loglevel
);
1182 static void com_ll_incr(void)
1184 if (loglevel
>= LL_EMERG
) {
1185 print_in_bar(COLOR_ERRMSG
,
1186 "loglevel already at minimal verbosity\n");
1190 print_in_bar(COLOR_MSG
, "loglevel set to %d\n", loglevel
);
1194 * reread configuration, terminate on errors
1196 static void com_reread_conf(void)
1198 char *cf
=configfile_exists();
1199 struct gui_cmdline_parser_params params
= {
1202 .check_required
= 0,
1203 .check_ambiguity
= 0
1207 PARA_WARNING_LOG("there is no configuration to read");
1210 PARA_INFO_LOG("rereading command line options and config file");
1211 gui_cmdline_parser(_argc
, _argv
, &conf
);
1212 gui_cmdline_parser_config_file(cf
, &conf
, ¶ms
);
1213 PARA_NOTICE_LOG("config file reloaded");
1214 if (check_key_map_args() < 0)
1215 finish(EXIT_FAILURE
);
1218 static void com_help(void)
1222 for (i
= 0; i
< conf
.key_map_given
; ++i
) {
1223 char *handler
, *arg
, *tmp
= para_strdup(conf
.key_map_arg
[i
]);
1224 const char *handler_text
= "???", *desc
= NULL
;
1226 if (!split_key_map(tmp
, &handler
, &arg
)) {
1232 handler_text
= "internal";
1233 desc
= command_list
[find_cmd_byname(arg
)].description
;
1235 case 'x': handler_text
= "external"; break;
1236 case 'd': handler_text
= "display "; break;
1237 case 'p': handler_text
= "para "; break;
1239 outputf(COLOR_MSG
, "%s\t%s\t%s%s\t%s", tmp
, handler_text
, arg
,
1240 strlen(arg
) < 8? "\t" : "",
1244 for (i
= 0; command_list
[i
].handler
; i
++) {
1245 struct gui_command gc
= command_list
[i
];
1247 outputf(COLOR_MSG
, "%s\tinternal\t%s\t%s%s", gc
.key
, gc
.name
,
1248 strlen(gc
.name
) < 8? "\t" : "",
1251 print_in_bar(COLOR_MSG
, "try \"para_gui -h\" or \"para_client help\" "
1255 static void com_shrink_top_win(void)
1257 if (top
.lines
<= theme
.top_lines_min
) {
1258 PARA_WARNING_LOG("can not decrease top window");
1261 init_wins(top
.lines
- 1);
1264 print_in_bar(COLOR_MSG
, "%s", "decreased top window");
1267 static void com_enlarge_top_win(void)
1269 if (bot
.lines
< 3) {
1270 PARA_WARNING_LOG("can not increase top window");
1273 init_wins(top
.lines
+ 1);
1276 print_in_bar(COLOR_MSG
, "increased top window");
1279 static void com_version(void)
1281 print_in_bar(COLOR_MSG
, "para_gui " PACKAGE_VERSION
" \""
1285 __noreturn
static void com_quit(void)
1290 static void com_refresh(void)
1296 static void change_theme(int next
)
1302 /* This seems to be needed twice, why? */
1305 PARA_NOTICE_LOG("new theme: %s", theme
.name
);
1308 static void com_next_theme(void)
1313 static void com_prev_theme(void)
1319 static void handle_command(int c
)
1323 /* first check user's key bindings */
1324 for (i
= 0; i
< conf
.key_map_given
; ++i
) {
1325 char tmp
[MAXLINE
], *handler
, *arg
;
1327 strcpy(tmp
, conf
.key_map_arg
[i
]);
1328 if (!split_key_map(tmp
, &handler
, &arg
))
1330 if (!strcmp(tmp
, km_keyname(c
))) {
1331 if (*handler
== 'd') {
1335 if (*handler
== 'x') {
1339 if (*handler
== 'p') {
1340 client_cmd_cmdline(arg
);
1343 if (*handler
== 'i') {
1344 int num
= find_cmd_byname(arg
);
1346 command_list
[num
].handler();
1351 /* not found, check internal key bindings */
1352 for (i
= 0; command_list
[i
].handler
; i
++) {
1353 if (!strcmp(km_keyname(c
), command_list
[i
].key
)) {
1354 command_list
[i
].handler();
1358 print_in_bar(COLOR_ERRMSG
, "key '%s' is not bound, press ? for help",
1362 int main(int argc
, char *argv
[])
1370 if (gui_cmdline_parser(argc
, argv
, &conf
)) {
1371 fprintf(stderr
, "parse error while reading command line\n");
1374 HANDLE_VERSION_FLAG("gui", conf
);
1375 init_theme(0, &theme
);
1376 top
.lines
= theme
.top_lines_default
;
1377 if (check_key_map_args() < 0) {
1378 fprintf(stderr
, "invalid key map\n");
1381 cf
= configfile_exists();
1382 if (!cf
&& conf
.config_file_given
) {
1383 fprintf(stderr
, "can not read config file %s\n",
1384 conf
.config_file_arg
);
1388 struct gui_cmdline_parser_params params
= {
1391 .check_required
= 0,
1392 .check_ambiguity
= 0
1394 gui_cmdline_parser_config_file(cf
, &conf
, ¶ms
);
1396 loglevel
= get_loglevel_by_name(conf
.loglevel_arg
);
1397 if (check_key_map_args() < 0) {
1398 fprintf(stderr
, "invalid key map in config file\n");
1401 setup_signal_handling();
1402 bot_win_rb
= ringbuffer_new(RINGBUFFER_SIZE
);
1403 initscr(); /* needed only once, always successful */
1408 ret
= do_select(GETCH_MODE
);
1411 print_in_bar(COLOR_MSG
, " ");
1412 handle_command(ret
);