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);
112 static void com_cancel_scrolling(void);
113 static void com_scroll_top(void);
115 static struct gui_command command_list
[] = {
119 .description
= "print help",
123 .name
= "enlarge_win",
124 .description
= "enlarge the top window",
125 .handler
= com_enlarge_top_win
128 .name
= "shrink_win",
129 .description
= "shrink the top window",
130 .handler
= com_shrink_top_win
133 .name
= "reread_conf",
134 .description
= "reread configuration file",
135 .handler
= com_reread_conf
139 .description
= "exit para_gui",
144 .description
= "redraw the screen",
145 .handler
= com_refresh
148 .name
= "next_theme",
149 .description
= "switch to next theme",
150 .handler
= com_next_theme
153 .name
= "prev_theme",
154 .description
= "switch to previous stream",
155 .handler
= com_prev_theme
159 .description
= "increase loglevel (decreases verbosity)",
160 .handler
= com_ll_incr
164 .description
= "decrease loglevel (increases verbosity)",
165 .handler
= com_ll_decr
169 .description
= "show the para_gui version",
170 .handler
= com_version
174 .description
= "scroll up one line",
175 .handler
= com_scroll_up
178 .name
= "scroll_down",
179 .description
= "scroll down one line",
180 .handler
= com_scroll_down
184 .description
= "scroll up one page",
185 .handler
= com_page_up
189 .description
= "scroll down one page",
190 .handler
= com_page_down
193 .name
= "scroll_top",
194 .description
= "scroll to top of buffer",
195 .handler
= com_scroll_top
198 .name
= "cancel_scroll",
199 .description
= "deactivate scroll mode",
200 .handler
= com_cancel_scrolling
206 static int find_cmd_byname(char *name
)
210 for (i
= 0; command_list
[i
].handler
; i
++)
211 if (!strcmp(command_list
[i
].name
, name
))
216 /* taken from mutt */
217 static char *km_keyname(int c
)
222 sprintf(buf
, "<up>");
226 sprintf(buf
, "<down>");
230 sprintf(buf
, "<left>");
233 if (c
== KEY_RIGHT
) {
234 sprintf(buf
, "<right>");
237 if (c
== KEY_NPAGE
) {
238 sprintf(buf
, "<npage>");
241 if (c
== KEY_PPAGE
) {
242 sprintf(buf
, "<ppage>");
246 sprintf(buf
, "<home>");
250 sprintf(buf
, "<end>");
253 if (c
< 256 && c
> -128 && iscntrl((unsigned char) c
)) {
258 buf
[1] = (c
+ '@') & 0x7f;
261 snprintf(buf
, sizeof(buf
), "\\%d%d%d", c
>> 6,
262 (c
>> 3) & 7, c
& 7);
263 } else if (c
>= KEY_F0
&& c
< KEY_F(256))
264 sprintf(buf
, "<F%d>", c
- KEY_F0
);
266 snprintf(buf
, sizeof(buf
), "%c", (unsigned char) c
);
268 snprintf(buf
, sizeof(buf
), "\\x%hx", (unsigned short) c
);
272 static char *configfile_exists(void)
274 static char *config_file
;
277 if (!conf
.config_file_given
) {
279 char *home
= para_homedir();
280 config_file
= make_message("%s/.paraslash/gui.conf",
286 tmp
= conf
.config_file_arg
;
287 return file_exists(tmp
)? tmp
: NULL
;
291 * print num spaces to curses window
293 static void add_spaces(WINDOW
* win
, unsigned int num
)
302 * print aligned string to curses window. This function always prints
305 static int align_str(WINDOW
* win
, char *str
, unsigned int len
,
308 int i
, num
; /* of spaces */
312 num
= len
- strlen(str
);
317 /* replace control characters by spaces */
318 for (i
= 0; i
< len
&& str
[i
]; i
++) {
319 if (str
[i
] == '\n' || str
[i
] == '\r' || str
[i
] == '\f')
324 add_spaces(win
, num
);
325 } else if (align
== RIGHT
) {
326 add_spaces(win
, num
);
329 add_spaces(win
, num
/ 2);
330 waddstr(win
, str
[0]? str
: "");
331 add_spaces(win
, num
- num
/ 2);
336 __printf_2_3
static void print_in_bar(int color
, const char *fmt
,...)
342 wattron(in
.win
, COLOR_PAIR(color
));
343 PARA_VSPRINTF(fmt
, msg
);
345 align_str(in
.win
, msg
, sb
.cols
, LEFT
);
351 * update the status bar
353 static void print_status_bar(void)
359 tmp
= para_strdup(STANDARD_STATUS_BAR
);
361 align_str(sb
.win
, tmp
, sb
.cols
, CENTER
);
367 * get the number of the oldest rbe that is (partially) visible. On return,
368 * lines contains the sum of the number of lines of all visable entries. If the
369 * first one is only partially visible, lines is greater than bot.lines.
371 static int first_visible_rbe(unsigned *lines
)
375 for (i
= scroll_position
; i
< RINGBUFFER_SIZE
; i
++) {
376 struct rb_entry
*rbe
= ringbuffer_get(bot_win_rb
, i
);
380 // fprintf(stderr, "found: %s\n", rbe->msg);
381 rbe_lines
= NUM_LINES(rbe
->len
);
382 if (rbe_lines
> bot
.lines
)
385 if (*lines
>= bot
.lines
)
388 return RINGBUFFER_SIZE
- 1;
391 static int draw_top_rbe(unsigned *lines
)
394 int offset
, fvr
= first_visible_rbe(lines
);
395 struct rb_entry
*rbe
;
399 wmove(bot
.win
, 0, 0);
400 rbe
= ringbuffer_get(bot_win_rb
, fvr
);
403 /* first rbe might be only partially visible */
404 offset
= (*lines
- bot
.lines
) * bot
.cols
;
405 len
= strlen(rbe
->msg
);
406 if (offset
< 0 || len
< offset
)
408 wattron(bot
.win
, COLOR_PAIR(rbe
->color
));
409 waddstr(bot
.win
, rbe
->msg
+ offset
);
410 *lines
= NUM_LINES(len
- offset
);
414 static void redraw_bot_win(void)
419 wmove(bot
.win
, 0, 0);
421 i
= draw_top_rbe(&lines
);
424 while (i
> 0 && lines
< bot
.lines
) {
425 struct rb_entry
*rbe
= ringbuffer_get(bot_win_rb
, --i
);
428 waddstr(bot
.win
, "\n");
431 lines
+= NUM_LINES(rbe
->len
);
432 wattron(bot
.win
, COLOR_PAIR(rbe
->color
));
433 waddstr(bot
.win
, "\n");
434 waddstr(bot
.win
, rbe
->msg
);
440 static void rb_add_entry(int color
, char *msg
)
442 struct rb_entry
*old
, *new = para_malloc(sizeof(struct rb_entry
));
445 new->len
= strlen(msg
);
447 old
= ringbuffer_add(bot_win_rb
, new);
448 // fprintf(stderr, "added: %s\n", new->msg);
453 if (scroll_position
) {
454 /* discard current scrolling, like xterm does */
459 wattron(bot
.win
, COLOR_PAIR(color
));
460 getyx(bot
.win
, y
, x
);
462 waddstr(bot
.win
, "\n");
463 waddstr(bot
.win
, msg
);
467 * print formated output to bot win and refresh
469 __printf_2_3
static void outputf(int color
, const char* fmt
,...)
475 PARA_VSPRINTF(fmt
, msg
);
476 rb_add_entry(color
, msg
);
480 static int add_output_line(char *line
, __a_unused
void *data
)
484 rb_add_entry(COLOR_OUTPUT
, para_strdup(line
));
490 __printf_2_3
void para_log(int ll
, const char *fmt
,...)
495 if (ll
< loglevel
|| !curses_active
)
504 color
= COLOR_ERRMSG
;
506 PARA_VSPRINTF(fmt
, msg
);
508 rb_add_entry(color
, msg
);
512 static void setup_signal_handling(void)
514 signal_pipe
= para_signal_init();
515 para_install_sighandler(SIGINT
);
516 para_install_sighandler(SIGTERM
);
517 para_install_sighandler(SIGCHLD
);
518 para_install_sighandler(SIGWINCH
);
519 para_install_sighandler(SIGUSR1
);
520 para_sigaction(SIGHUP
, SIG_IGN
);
523 __noreturn
static void do_exit(int ret
)
525 para_sigaction(SIGTERM
, SIG_IGN
);
530 static void shutdown_curses(void)
539 __noreturn
static void finish(int ret
)
546 * exit curses and print given message to stdout/stderr
548 __noreturn __printf_2_3
static void msg_n_exit(int ret
, const char* fmt
, ...)
551 FILE *outfd
= ret
? stderr
: stdout
;
555 vfprintf(outfd
, fmt
, argp
);
560 static void print_welcome(void)
562 if (loglevel
> LL_NOTICE
)
564 outputf(COLOR_WELCOME
, "Welcome to para_gui " PACKAGE_VERSION
565 " \"" CODENAME
"\". Theme: %s", theme
.name
);
572 static void init_wins(int top_lines
)
576 top
.lines
= top_lines
;
581 bot
.lines
= LINES
- top
.lines
- 3;
583 bot
.begy
= top
.lines
+ 1;
598 sep
.begy
= top
.lines
;
601 assume_default_colors(theme
.default_fg
, theme
.default_bg
);
603 mvwin(top
.win
, top
.begy
, top
.begx
);
604 wresize(top
.win
, top
.lines
, top
.cols
);
606 mvwin(sb
.win
, sb
.begy
, sb
.begx
);
607 wresize(sb
.win
, sb
.lines
, sb
.cols
);
609 mvwin(sep
.win
, sep
.begy
, sep
.begx
);
610 wresize(sep
.win
, sep
.lines
, sep
.cols
);
612 mvwin(bot
.win
, bot
.begy
, bot
.begx
);
613 wresize(bot
.win
, bot
.lines
, bot
.cols
);
615 mvwin(in
.win
, in
.begy
, in
.begx
);
616 wresize(in
.win
, in
.lines
, in
.cols
);
618 sep
.win
= newwin(sep
.lines
, sep
.cols
, sep
.begy
, sep
.begx
);
619 top
.win
= newwin(top
.lines
, top
.cols
, top
.begy
, top
.begx
);
620 bot
.win
= newwin(bot
.lines
, bot
.cols
, bot
.begy
, bot
.begx
);
621 sb
.win
= newwin(sb
.lines
, sb
.cols
, sb
.begy
, sb
.begx
);
622 in
.win
= newwin(in
.lines
, in
.cols
, in
.begy
, in
.begx
);
623 if (!top
.win
|| !bot
.win
|| !sb
.win
|| !in
.win
|| !sep
.win
)
624 msg_n_exit(1, "Error: Cannot create curses windows\n");
628 scrollok(bot
.win
, 1);
629 wattron(sb
.win
, COLOR_PAIR(COLOR_STATUSBAR
));
630 wattron(sep
.win
, COLOR_PAIR(COLOR_SEPARATOR
));
631 wattron(bot
.win
, COLOR_PAIR(COLOR_BOT
));
632 wattron(top
.win
, COLOR_PAIR(COLOR_TOP
));
644 wmove(sep
.win
, 0, 0);
645 for (i
= 1; i
<= COLS
; i
++)
646 waddstr(sep
.win
, theme
.sep_str
);
649 wnoutrefresh(top
.win
);
650 wnoutrefresh(bot
.win
);
651 //wnoutrefresh(sb.win);
653 wnoutrefresh(in
.win
);
654 wnoutrefresh(sep
.win
);
658 static void check_geometry(void)
660 if (LINES
< theme
.lines_min
|| COLS
< theme
.cols_min
)
661 msg_n_exit(EXIT_FAILURE
, "Error: Terminal (%dx%d) too small"
662 " (need at least %dx%d)\n", COLS
, LINES
,
663 theme
.cols_min
, theme
.lines_min
);
667 * Print stat item #i to curses window
669 static void print_stat_item(int i
)
672 struct stat_item_data d
= theme
.data
[i
];
673 char *c
= stat_content
[i
];
675 if (!curses_active
|| !d
.len
|| !c
)
677 tmp
= make_message("%s%s%s", d
.prefix
, c
, d
.postfix
);
678 wmove(top
.win
, d
.y
* top
.lines
/ 100, d
.x
* COLS
/ 100);
680 wattron(top
.win
, COLOR_PAIR(i
+ 1));
681 align_str(top
.win
, tmp
, d
.len
* COLS
/ 100, d
.align
);
686 static int update_item(int item_num
, char *buf
)
688 char **c
= stat_content
+ item_num
;
695 *c
= para_strdup("(artist tag not set)");
698 *c
= para_strdup("(title tag not set)");
701 *c
= para_strdup("????");
704 *c
= para_strdup("(album tag not set)");
707 *c
= para_strdup("(comment tag not set)");
711 *c
= para_strdup(buf
);
713 print_stat_item(item_num
);
717 static int read_stat_pipe(fd_set
*rfds
)
720 static int bufsize
, loaded
;
726 if (loaded
>= bufsize
) {
727 if (bufsize
> 1000 * 1000) {
731 bufsize
+= bufsize
+ 1000;
732 buf
= para_realloc(buf
, bufsize
);
734 assert(loaded
< bufsize
);
735 ret
= read_nonblock(stat_pipe
, buf
+ loaded
, bufsize
- loaded
,
738 ret2
= for_each_stat_item(buf
, loaded
, update_item
);
739 if (ret
< 0 || ret2
< 0) {
741 return ret2
< 0? ret2
: ret
;
743 sz
= ret2
; /* what is left */
744 if (sz
> 0 && sz
< loaded
)
745 memmove(buf
, buf
+ loaded
- sz
, sz
);
750 static void print_all_items(void)
756 FOR_EACH_STATUS_ITEM(i
)
760 static void clear_all_items(void)
764 FOR_EACH_STATUS_ITEM(i
) {
765 free(stat_content
[i
]);
766 stat_content
[i
] = para_strdup("");
770 static void init_colors(void)
775 msg_n_exit(EXIT_FAILURE
, "Error: No color term\n");
777 FOR_EACH_STATUS_ITEM(i
)
778 if (theme
.data
[i
].len
)
779 init_pair(i
+ 1, theme
.data
[i
].fg
, theme
.data
[i
].bg
);
780 init_pair(COLOR_STATUSBAR
, theme
.sb_fg
, theme
.sb_bg
);
781 init_pair(COLOR_COMMAND
, theme
.cmd_fg
, theme
.cmd_bg
);
782 init_pair(COLOR_OUTPUT
, theme
.output_fg
, theme
.output_bg
);
783 init_pair(COLOR_MSG
, theme
.msg_fg
, theme
.msg_bg
);
784 init_pair(COLOR_ERRMSG
, theme
.err_msg_fg
, theme
.err_msg_bg
);
785 init_pair(COLOR_WELCOME
, theme
.welcome_fg
, theme
.welcome_bg
);
786 init_pair(COLOR_SEPARATOR
, theme
.sep_fg
, theme
.sep_bg
);
787 init_pair(COLOR_TOP
, theme
.default_fg
, theme
.default_bg
);
788 init_pair(COLOR_BOT
, theme
.default_fg
, theme
.default_bg
);
792 * (re-)initialize the curses library FIXME: Error checking
794 static void init_curses(void)
797 if (top
.win
&& refresh() == ERR
) { /* refesh is really needed */
798 msg_n_exit(EXIT_FAILURE
, "refresh() failed\n");
801 curs_set(0); /* make cursor invisible, ignore errors */
802 // if (noraw() == ERR);
803 // msg_n_exit(EXIT_FAILURE, "can not place terminal out of "
805 nonl(); /* tell curses not to do NL->CR/NL on output */
806 noecho(); /* don't echo input */
807 cbreak(); /* take input chars one at a time, no wait for \n */
811 init_wins(theme
.top_lines_default
);
813 noecho(); /* don't echo input */
817 static void check_sigchld(void)
822 ret
= para_reap_child(&pid
);
825 if (pid
== cmd_pid
) {
829 goto reap_next_child
;
833 * This sucker modifies its first argument. *handler and *arg are
834 * pointers to 0-terminated strings (inside line). Crap.
836 static int split_key_map(char *line
, char **handler
, char **arg
)
838 if (!(*handler
= strchr(line
+ 1, ':')))
842 if (!(*arg
= strchr(*handler
, ':')))
851 static int check_key_map_args(void)
855 char *tmp
= NULL
, *handler
, *arg
;
857 for (i
= 0; i
< conf
.key_map_given
; ++i
) {
858 s
= conf
.key_map_arg
[i
];
862 tmp
= para_strdup(s
);
863 if (!split_key_map(tmp
, &handler
, &arg
))
865 if (strlen(handler
) != 1)
874 if (find_cmd_byname(arg
) < 0)
884 * React to various signal-related events
886 static void handle_signal(int sig
)
890 msg_n_exit(EXIT_FAILURE
,
891 "only the good die young (caught SIGTERM))\n");
901 PARA_WARNING_LOG("caught SIGINT, reset");
902 /* Nothing to do. SIGINT killed our child which gets noticed
903 * by do_select and resets everything.
907 PARA_NOTICE_LOG("got SIGUSR1, rereading configuration");
916 static int open_stat_pipe(void)
919 int ret
, fds
[3] = {0, 1, 0};
926 * Sleep a bit to avoid a busy loop. As the call to sleep() may
927 * be interrupted by SIGCHLD, we simply wait until the call
932 ret
= para_exec_cmdline_pid(&pid
, conf
.stat_cmd_arg
, fds
);
935 ret
= mark_fd_nonblocking(fds
[1]);
943 * This is the core select loop. Besides the (internal) signal
944 * pipe, the following other fds are checked according to the mode:
946 * GETCH_MODE: check stdin, return when key is pressed
948 * COMMAND_MODE: check command_pipe and stdin. Return when a screen full
949 * of output has been read or when other end has closed command pipe or
950 * when any key is pressed.
952 * EXTERNAL_MODE: Check only signal pipe. Used when an external command
953 * is running. During that time curses is disabled. Returns when
956 static int do_select(int mode
)
961 char command_buf
[4096] = "";
962 int cbo
= 0; /* command buf offset */
965 tv
.tv_sec
= conf
.timeout_arg
/ 1000;
966 tv
.tv_usec
= (conf
.timeout_arg
% 1000) * 1000;
967 // ret = refresh_status();
971 stat_pipe
= open_stat_pipe();
973 para_fd_set(stat_pipe
, &rfds
, &max_fileno
);
975 para_fd_set(signal_pipe
, &rfds
, &max_fileno
);
976 /* command pipe only for COMMAND_MODE */
977 if (command_pipe
>= 0 && mode
== COMMAND_MODE
)
978 para_fd_set(command_pipe
, &rfds
, &max_fileno
);
979 ret
= para_select(max_fileno
+ 1, &rfds
, NULL
, &tv
);
981 goto check_return
; /* skip fd checks */
983 ret
= para_next_signal(&rfds
);
986 /* read command pipe if ready */
987 if (command_pipe
>= 0 && mode
== COMMAND_MODE
) {
989 ret
= read_nonblock(command_pipe
, command_buf
+ cbo
,
990 sizeof(command_buf
) - 1 - cbo
, &rfds
, &sz
);
993 cbo
= for_each_line(command_buf
, cbo
, &add_output_line
, NULL
);
997 PARA_NOTICE_LOG("closing command pipe: %s",
998 para_strerror(-ret
));
1004 ret
= read_stat_pipe(&rfds
);
1006 PARA_NOTICE_LOG("closing stat pipe: %s\n", para_strerror(-ret
));
1010 free(stat_content
[SI_BASENAME
]);
1011 stat_content
[SI_BASENAME
] =
1012 para_strdup("stat command terminated!?");
1018 ret
= wgetch(top
.win
);
1019 if (ret
!= ERR
&& ret
!= KEY_RESIZE
) {
1021 close(command_pipe
);
1025 kill(cmd_pid
, SIGTERM
);
1030 ret
= wgetch(top
.win
);
1031 if (ret
!= ERR
&& ret
!= KEY_RESIZE
)
1044 * read from command pipe and print data to bot window
1046 static int send_output(void)
1050 if (command_pipe
< 0)
1052 ret
= mark_fd_nonblocking(command_pipe
);
1054 close(command_pipe
);
1057 if (do_select(COMMAND_MODE
) >= 0)
1058 PARA_INFO_LOG("command complete");
1060 PARA_NOTICE_LOG("command aborted");
1061 print_in_bar(COLOR_MSG
, " ");
1065 static int client_cmd_cmdline(char *cmd
)
1067 int ret
, fds
[3] = {0, 1, 0};
1068 char *c
= make_message(BINDIR
"/para_client -- %s", cmd
);
1070 outputf(COLOR_COMMAND
, "%s", c
);
1071 print_in_bar(COLOR_MSG
, "executing client command, hit any key to abort\n");
1072 ret
= para_exec_cmdline_pid(&cmd_pid
, c
, fds
);
1076 command_pipe
= fds
[1];
1077 return send_output();
1081 * exec command and print output to bot win
1083 static int display_cmd(char *cmd
)
1085 int fds
[3] = {0, 1, 0};
1087 print_in_bar(COLOR_MSG
, "executing display command, hit any key to abort");
1088 outputf(COLOR_COMMAND
, "%s", cmd
);
1089 if (para_exec_cmdline_pid(&cmd_pid
, cmd
, fds
) < 0)
1091 command_pipe
= fds
[1];
1092 return send_output();
1096 * shutdown curses and stat pipe before executing external commands
1098 static int external_cmd(char *cmd
)
1100 int fds
[3] = {-1, -1, -1};
1105 para_exec_cmdline_pid(&cmd_pid
, cmd
, fds
);
1107 do_select(EXTERNAL_MODE
);
1112 static void print_scroll_msg(void)
1114 unsigned lines_total
, filled
= ringbuffer_filled(bot_win_rb
);
1115 int first_rbe
= first_visible_rbe(&lines_total
);
1116 print_in_bar(COLOR_MSG
, "scrolled view: %d-%d/%d\n", filled
- first_rbe
,
1117 filled
- scroll_position
, ringbuffer_filled(bot_win_rb
));
1120 static void com_scroll_top(void)
1122 int i
= RINGBUFFER_SIZE
- 1;
1125 while (i
> 0 && !ringbuffer_get(bot_win_rb
, i
))
1127 /* i is oldest entry */
1128 for (; lines
< bot
.lines
&& i
>= 0; i
--) {
1129 struct rb_entry
*rbe
= ringbuffer_get(bot_win_rb
, i
);
1132 lines
+= NUM_LINES(strlen(rbe
->msg
));
1135 if (lines
> 0 && scroll_position
!= i
) {
1136 scroll_position
= i
;
1141 print_in_bar(COLOR_ERRMSG
, "top of buffer is shown\n");
1144 static void com_cancel_scrolling(void)
1147 if (scroll_position
== 0) {
1148 print_in_bar(COLOR_ERRMSG
, "bottom of buffer is shown\n");
1151 scroll_position
= 0;
1155 static void com_page_down(void)
1158 int i
= scroll_position
;
1159 while (lines
< bot
.lines
&& --i
> 0) {
1160 struct rb_entry
*rbe
= ringbuffer_get(bot_win_rb
, i
);
1163 lines
+= NUM_LINES(strlen(rbe
->msg
));
1166 scroll_position
= i
;
1171 print_in_bar(COLOR_ERRMSG
, "bottom of buffer is shown\n");
1174 static void com_page_up(void)
1177 int fvr
= first_visible_rbe(&lines
);
1179 if (fvr
< 0 || fvr
+ 1 >= ringbuffer_filled(bot_win_rb
)) {
1180 print_in_bar(COLOR_ERRMSG
, "top of buffer is shown\n");
1183 scroll_position
= fvr
+ 1;
1184 for (; scroll_position
> 0; scroll_position
--) {
1185 first_visible_rbe(&lines
);
1186 if (lines
== bot
.lines
)
1193 static void com_scroll_down(void)
1195 struct rb_entry
*rbe
;
1198 if (!scroll_position
) {
1199 print_in_bar(COLOR_ERRMSG
, "bottom of buffer is shown\n");
1203 rbe
= ringbuffer_get(bot_win_rb
, scroll_position
);
1204 rbe_lines
= NUM_LINES(rbe
->len
);
1205 wscrl(bot
.win
, rbe_lines
);
1206 wmove(bot
.win
, bot
.lines
- rbe_lines
, 0);
1207 wattron(bot
.win
, COLOR_PAIR(rbe
->color
));
1208 waddstr(bot
.win
, rbe
->msg
);
1213 static void com_scroll_up(void)
1215 struct rb_entry
*rbe
= NULL
;
1217 int i
, first_rbe
, num_scroll
;
1219 /* the entry that is going to vanish */
1220 rbe
= ringbuffer_get(bot_win_rb
, scroll_position
);
1223 num_scroll
= NUM_LINES(rbe
->len
);
1224 first_rbe
= first_visible_rbe(&lines
);
1225 if (first_rbe
< 0 || (first_rbe
== ringbuffer_filled(bot_win_rb
) - 1))
1228 wscrl(bot
.win
, -num_scroll
);
1229 i
= draw_top_rbe(&lines
);
1232 while (i
> 0 && lines
< num_scroll
) {
1234 rbe
= ringbuffer_get(bot_win_rb
, --i
);
1237 rbe_lines
= NUM_LINES(rbe
->len
);
1239 // fprintf(stderr, "msg: %s\n", rbe->msg);
1240 wattron(bot
.win
, COLOR_PAIR(rbe
->color
));
1241 waddstr(bot
.win
, "\n");
1242 waddstr(bot
.win
, rbe
->msg
);
1251 print_in_bar(COLOR_ERRMSG
, "top of buffer is shown\n");
1254 static void com_ll_decr(void)
1256 if (loglevel
<= LL_DEBUG
) {
1257 print_in_bar(COLOR_ERRMSG
,
1258 "loglevel already at maximal verbosity\n");
1262 print_in_bar(COLOR_MSG
, "loglevel set to %d\n", loglevel
);
1265 static void com_ll_incr(void)
1267 if (loglevel
>= LL_EMERG
) {
1268 print_in_bar(COLOR_ERRMSG
,
1269 "loglevel already at minimal verbosity\n");
1273 print_in_bar(COLOR_MSG
, "loglevel set to %d\n", loglevel
);
1277 * reread configuration, terminate on errors
1279 static void com_reread_conf(void)
1281 char *cf
=configfile_exists();
1282 struct gui_cmdline_parser_params params
= {
1285 .check_required
= 0,
1286 .check_ambiguity
= 0,
1291 PARA_WARNING_LOG("there is no configuration to read");
1294 PARA_INFO_LOG("rereading command line options and config file");
1295 gui_cmdline_parser_ext(_argc
, _argv
, &conf
, ¶ms
);
1296 if (gui_cmdline_parser_config_file(cf
, &conf
, ¶ms
) != 0) {
1297 PARA_EMERG_LOG("errors in config file");
1298 finish(EXIT_FAILURE
);
1300 PARA_NOTICE_LOG("config file reloaded");
1301 if (check_key_map_args() < 0)
1302 finish(EXIT_FAILURE
);
1305 static void com_help(void)
1309 for (i
= 0; i
< conf
.key_map_given
; ++i
) {
1310 char *handler
, *arg
, *tmp
= para_strdup(conf
.key_map_arg
[i
]);
1311 const char *handler_text
= "???", *desc
= NULL
;
1313 if (!split_key_map(tmp
, &handler
, &arg
)) {
1319 handler_text
= "internal";
1320 desc
= command_list
[find_cmd_byname(arg
)].description
;
1322 case 'x': handler_text
= "external"; break;
1323 case 'd': handler_text
= "display "; break;
1324 case 'p': handler_text
= "para "; break;
1326 outputf(COLOR_MSG
, "%s\t%s\t%s%s\t%s", tmp
, handler_text
, arg
,
1327 strlen(arg
) < 8? "\t" : "",
1331 for (i
= 0; command_list
[i
].handler
; i
++) {
1332 struct gui_command gc
= command_list
[i
];
1334 outputf(COLOR_MSG
, "%s\tinternal\t%s\t%s%s", gc
.key
, gc
.name
,
1335 strlen(gc
.name
) < 8? "\t" : "",
1338 print_in_bar(COLOR_MSG
, "try \"para_gui -h\" or \"para_client help\" "
1342 static void com_shrink_top_win(void)
1344 if (top
.lines
<= theme
.top_lines_min
) {
1345 PARA_WARNING_LOG("can not decrease top window");
1348 init_wins(top
.lines
- 1);
1351 print_in_bar(COLOR_MSG
, "%s", "decreased top window");
1354 static void com_enlarge_top_win(void)
1356 if (bot
.lines
< 3) {
1357 PARA_WARNING_LOG("can not increase top window");
1360 init_wins(top
.lines
+ 1);
1363 print_in_bar(COLOR_MSG
, "increased top window");
1366 static void com_version(void)
1368 print_in_bar(COLOR_MSG
, "para_gui " PACKAGE_VERSION
" \""
1372 __noreturn
static void com_quit(void)
1377 static void com_refresh(void)
1383 static void change_theme(int next
)
1389 /* This seems to be needed twice, why? */
1392 PARA_NOTICE_LOG("new theme: %s", theme
.name
);
1395 static void com_next_theme(void)
1400 static void com_prev_theme(void)
1406 static void handle_command(int c
)
1410 /* first check user's key bindings */
1411 for (i
= 0; i
< conf
.key_map_given
; ++i
) {
1412 char *tmp
, *handler
, *arg
;
1414 tmp
= para_strdup(conf
.key_map_arg
[i
]);
1415 if (!split_key_map(tmp
, &handler
, &arg
)) {
1419 if (strcmp(tmp
, km_keyname(c
))) {
1423 if (*handler
== 'd')
1425 else if (*handler
== 'x')
1427 else if (*handler
== 'p')
1428 client_cmd_cmdline(arg
);
1429 else if (*handler
== 'i') {
1430 int num
= find_cmd_byname(arg
);
1432 command_list
[num
].handler();
1437 /* not found, check internal key bindings */
1438 for (i
= 0; command_list
[i
].handler
; i
++) {
1439 if (!strcmp(km_keyname(c
), command_list
[i
].key
)) {
1440 command_list
[i
].handler();
1444 print_in_bar(COLOR_ERRMSG
, "key '%s' is not bound, press ? for help",
1448 int main(int argc
, char *argv
[])
1456 if (gui_cmdline_parser(argc
, argv
, &conf
) != 0)
1458 HANDLE_VERSION_FLAG("gui", conf
);
1459 cf
= configfile_exists();
1460 if (!cf
&& conf
.config_file_given
) {
1461 fprintf(stderr
, "can not read config file %s\n",
1462 conf
.config_file_arg
);
1466 struct gui_cmdline_parser_params params
= {
1469 .check_required
= 0,
1470 .check_ambiguity
= 0,
1473 if (gui_cmdline_parser_config_file(cf
, &conf
, ¶ms
) != 0)
1476 loglevel
= get_loglevel_by_name(conf
.loglevel_arg
);
1477 if (check_key_map_args() < 0) {
1478 fprintf(stderr
, "invalid key map\n");
1481 init_theme_or_die(conf
.theme_arg
, &theme
);
1482 top
.lines
= theme
.top_lines_default
;
1483 setup_signal_handling();
1484 bot_win_rb
= ringbuffer_new(RINGBUFFER_SIZE
);
1485 initscr(); /* needed only once, always successful */
1490 ret
= do_select(GETCH_MODE
);
1493 print_in_bar(COLOR_MSG
, " ");
1494 handle_command(ret
);