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. */
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 audiod_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 para_open_audiod_pipe(char *cmd
)
196 int fds
[3] = {0, 1, 0};
198 int ret
= para_exec_cmdline_pid(&pid
, cmd
, fds
);
201 ret
= mark_fd_nonblocking(fds
[1]);
208 static int find_cmd_byname(char *name
)
212 for (i
= 0; command_list
[i
].handler
; i
++)
213 if (!strcmp(command_list
[i
].name
, name
))
218 /* taken from mutt */
219 static char *km_keyname(int c
)
224 sprintf(buf
, "<up>");
228 sprintf(buf
, "<down>");
232 sprintf(buf
, "<left>");
235 if (c
== KEY_RIGHT
) {
236 sprintf(buf
, "<right>");
239 if (c
== KEY_NPAGE
) {
240 sprintf(buf
, "<npage>");
243 if (c
== KEY_PPAGE
) {
244 sprintf(buf
, "<ppage>");
247 if (c
< 256 && c
> -128 && iscntrl((unsigned char) c
)) {
252 buf
[1] = (c
+ '@') & 0x7f;
255 snprintf(buf
, sizeof(buf
), "\\%d%d%d", c
>> 6,
256 (c
>> 3) & 7, c
& 7);
257 } else if (c
>= KEY_F0
&& c
< KEY_F(256))
258 sprintf(buf
, "<F%d>", c
- KEY_F0
);
260 snprintf(buf
, sizeof(buf
), "%c", (unsigned char) c
);
262 snprintf(buf
, sizeof(buf
), "\\x%hx", (unsigned short) c
);
266 static char *configfile_exists(void)
268 static char *config_file
;
271 if (!conf
.config_file_given
) {
273 char *home
= para_homedir();
274 config_file
= make_message("%s/.paraslash/gui.conf",
280 tmp
= conf
.config_file_arg
;
281 return file_exists(tmp
)? tmp
: NULL
;
285 * print num spaces to curses window
287 static void add_spaces(WINDOW
* win
, unsigned int num
)
296 * print aligned string to curses window. This function always prints
299 static int align_str(WINDOW
* win
, char *str
, unsigned int len
,
302 int i
, num
; /* of spaces */
306 num
= len
- strlen(str
);
311 /* replace newlines by spaces */
312 for (i
= 0; i
< len
&& str
[i
]; i
++) {
318 add_spaces(win
, num
);
319 } else if (align
== RIGHT
) {
320 add_spaces(win
, num
);
323 add_spaces(win
, num
/ 2);
324 waddstr(win
, str
[0]? str
: "");
325 add_spaces(win
, num
- num
/ 2);
330 __printf_2_3
static void print_in_bar(int color
, const char *fmt
,...)
336 wattron(in
.win
, COLOR_PAIR(color
));
337 PARA_VSPRINTF(fmt
, msg
);
339 align_str(in
.win
, msg
, sb
.cols
, LEFT
);
345 * update the status bar
347 static void print_status_bar(void)
353 tmp
= para_strdup(STANDARD_STATUS_BAR
);
355 align_str(sb
.win
, tmp
, sb
.cols
, CENTER
);
361 * get the number of the oldest rbe that is (partially) visible. On return,
362 * lines contains the sum of the number of lines of all visable entries. If the
363 * first one is only partially visible, lines is greater than bot.lines.
365 static int first_visible_rbe(unsigned *lines
)
369 for (i
= scroll_position
; i
< RINGBUFFER_SIZE
; i
++) {
370 struct rb_entry
*rbe
= ringbuffer_get(bot_win_rb
, i
);
374 // fprintf(stderr, "found: %s\n", rbe->msg);
375 rbe_lines
= NUM_LINES(rbe
->len
);
376 if (rbe_lines
> bot
.lines
)
379 if (*lines
>= bot
.lines
)
382 return RINGBUFFER_SIZE
- 1;
385 static int draw_top_rbe(unsigned *lines
)
388 int offset
, fvr
= first_visible_rbe(lines
);
389 struct rb_entry
*rbe
;
393 wmove(bot
.win
, 0, 0);
394 rbe
= ringbuffer_get(bot_win_rb
, fvr
);
397 /* first rbe might be only partially visible */
398 offset
= (*lines
- bot
.lines
) * bot
.cols
;
399 len
= strlen(rbe
->msg
);
400 if (offset
< 0 || len
< offset
)
402 wattron(bot
.win
, COLOR_PAIR(rbe
->color
));
403 waddstr(bot
.win
, rbe
->msg
+ offset
);
404 *lines
= NUM_LINES(len
- offset
);
408 static void redraw_bot_win(void)
413 wmove(bot
.win
, 0, 0);
415 i
= draw_top_rbe(&lines
);
418 while (i
> 0 && lines
< bot
.lines
) {
419 struct rb_entry
*rbe
= ringbuffer_get(bot_win_rb
, --i
);
422 waddstr(bot
.win
, "\n");
425 lines
+= NUM_LINES(rbe
->len
);
426 wattron(bot
.win
, COLOR_PAIR(rbe
->color
));
427 waddstr(bot
.win
, "\n");
428 waddstr(bot
.win
, rbe
->msg
);
434 static void rb_add_entry(int color
, char *msg
)
436 struct rb_entry
*old
, *new = para_malloc(sizeof(struct rb_entry
));
439 new->len
= strlen(msg
);
441 old
= ringbuffer_add(bot_win_rb
, new);
442 // fprintf(stderr, "added: %s\n", new->msg);
447 if (scroll_position
) {
448 /* discard current scrolling, like xterm does */
453 wattron(bot
.win
, COLOR_PAIR(color
));
454 getyx(bot
.win
, y
, x
);
456 waddstr(bot
.win
, "\n");
457 waddstr(bot
.win
, msg
);
461 * print formated output to bot win and refresh
463 __printf_2_3
static void outputf(int color
, const char* fmt
,...)
469 PARA_VSPRINTF(fmt
, msg
);
470 rb_add_entry(color
, msg
);
474 static int add_output_line(char *line
, __a_unused
void *data
)
478 rb_add_entry(COLOR_OUTPUT
, para_strdup(line
));
484 __printf_2_3
void para_log(int ll
, const char *fmt
,...)
489 if (ll
< loglevel
|| !curses_active
)
498 color
= COLOR_ERRMSG
;
500 PARA_VSPRINTF(fmt
, msg
);
502 rb_add_entry(color
, msg
);
506 static void setup_signal_handling(void)
508 signal_pipe
= para_signal_init();
509 para_install_sighandler(SIGINT
);
510 para_install_sighandler(SIGTERM
);
511 para_install_sighandler(SIGCHLD
);
512 para_install_sighandler(SIGWINCH
);
513 para_install_sighandler(SIGUSR1
);
514 para_sigaction(SIGHUP
, SIG_IGN
);
517 __noreturn
static void do_exit(int ret
)
519 para_sigaction(SIGTERM
, SIG_IGN
);
524 static void shutdown_curses(void)
533 __noreturn
static void finish(int ret
)
540 * exit curses and print given message to stdout/stderr
542 __noreturn __printf_2_3
static void msg_n_exit(int ret
, const char* fmt
, ...)
545 FILE *outfd
= ret
? stderr
: stdout
;
549 vfprintf(outfd
, fmt
, argp
);
554 static void print_welcome(void)
556 if (loglevel
> LL_NOTICE
)
558 outputf(COLOR_WELCOME
, "Welcome to para_gui " PACKAGE_VERSION
559 " \"" CODENAME
"\". Theme: %s", theme
.name
);
566 static void init_wins(int top_lines
)
570 top
.lines
= top_lines
;
575 bot
.lines
= LINES
- top
.lines
- 3;
577 bot
.begy
= top
.lines
+ 1;
592 sep
.begy
= top
.lines
;
595 assume_default_colors(theme
.default_fg
, theme
.default_bg
);
597 mvwin(top
.win
, top
.begy
, top
.begx
);
598 wresize(top
.win
, top
.lines
, top
.cols
);
600 mvwin(sb
.win
, sb
.begy
, sb
.begx
);
601 wresize(sb
.win
, sb
.lines
, sb
.cols
);
603 mvwin(sep
.win
, sep
.begy
, sep
.begx
);
604 wresize(sep
.win
, sep
.lines
, sep
.cols
);
606 mvwin(bot
.win
, bot
.begy
, bot
.begx
);
607 wresize(bot
.win
, bot
.lines
, bot
.cols
);
609 mvwin(in
.win
, in
.begy
, in
.begx
);
610 wresize(in
.win
, in
.lines
, in
.cols
);
612 sep
.win
= newwin(sep
.lines
, sep
.cols
, sep
.begy
, sep
.begx
);
613 top
.win
= newwin(top
.lines
, top
.cols
, top
.begy
, top
.begx
);
614 bot
.win
= newwin(bot
.lines
, bot
.cols
, bot
.begy
, bot
.begx
);
615 sb
.win
= newwin(sb
.lines
, sb
.cols
, sb
.begy
, sb
.begx
);
616 in
.win
= newwin(in
.lines
, in
.cols
, in
.begy
, in
.begx
);
617 if (!top
.win
|| !bot
.win
|| !sb
.win
|| !in
.win
|| !sep
.win
)
618 msg_n_exit(1, "Error: Cannot create curses windows\n");
622 scrollok(bot
.win
, 1);
623 wattron(sb
.win
, COLOR_PAIR(COLOR_STATUSBAR
));
624 wattron(sep
.win
, COLOR_PAIR(COLOR_SEPARATOR
));
625 wattron(bot
.win
, COLOR_PAIR(COLOR_BOT
));
626 wattron(top
.win
, COLOR_PAIR(COLOR_TOP
));
638 wmove(sep
.win
, 0, 0);
639 for (i
= 1; i
<= COLS
; i
++)
640 waddstr(sep
.win
, theme
.sep_str
);
643 wnoutrefresh(top
.win
);
644 wnoutrefresh(bot
.win
);
645 //wnoutrefresh(sb.win);
647 wnoutrefresh(in
.win
);
648 wnoutrefresh(sep
.win
);
652 static void check_geometry(void)
654 if (LINES
< theme
.lines_min
|| COLS
< theme
.cols_min
)
655 msg_n_exit(EXIT_FAILURE
, "Error: Terminal (%dx%d) too small"
656 " (need at least %dx%d)\n", COLS
, LINES
,
657 theme
.cols_min
, theme
.lines_min
);
661 * Print stat item #i to curses window
663 static void print_stat_item(int i
)
666 struct stat_item_data d
= theme
.data
[i
];
667 char *c
= stat_content
[i
];
669 if (!curses_active
|| !d
.len
|| !c
)
671 tmp
= make_message("%s%s%s", d
.prefix
, c
, d
.postfix
);
672 wmove(top
.win
, d
.y
* top
.lines
/ 100, d
.x
* COLS
/ 100);
674 wattron(top
.win
, COLOR_PAIR(i
+ 1));
675 align_str(top
.win
, tmp
, d
.len
* COLS
/ 100, d
.align
);
680 static int update_item(int item_num
, char *buf
)
682 char **c
= stat_content
+ item_num
;
689 *c
= para_strdup("(artist tag not set)");
692 *c
= para_strdup("(title tag not set)");
695 *c
= para_strdup("????");
698 *c
= para_strdup("(album tag not set)");
701 *c
= para_strdup("(comment tag not set)");
705 *c
= para_strdup(buf
);
707 print_stat_item(item_num
);
711 static int read_audiod_pipe(int fd
)
714 static int bufsize
, loaded
;
717 if (loaded
>= bufsize
) {
718 if (bufsize
> 1000 * 1000) {
722 bufsize
+= bufsize
+ 1000;
723 buf
= para_realloc(buf
, bufsize
);
725 assert(loaded
< bufsize
);
726 ret
= read(fd
, buf
+ loaded
, bufsize
- loaded
);
730 ret
= for_each_stat_item(buf
, loaded
, update_item
);
737 static void print_all_items(void)
743 FOR_EACH_STATUS_ITEM(i
)
747 static void clear_all_items(void)
751 FOR_EACH_STATUS_ITEM(i
) {
752 free(stat_content
[i
]);
753 stat_content
[i
] = para_strdup("");
757 static void init_colors(void)
762 msg_n_exit(EXIT_FAILURE
, "Error: No color term\n");
764 FOR_EACH_STATUS_ITEM(i
)
765 if (theme
.data
[i
].len
)
766 init_pair(i
+ 1, theme
.data
[i
].fg
, theme
.data
[i
].bg
);
767 init_pair(COLOR_STATUSBAR
, theme
.sb_fg
, theme
.sb_bg
);
768 init_pair(COLOR_COMMAND
, theme
.cmd_fg
, theme
.cmd_bg
);
769 init_pair(COLOR_OUTPUT
, theme
.output_fg
, theme
.output_bg
);
770 init_pair(COLOR_MSG
, theme
.msg_fg
, theme
.msg_bg
);
771 init_pair(COLOR_ERRMSG
, theme
.err_msg_fg
, theme
.err_msg_bg
);
772 init_pair(COLOR_WELCOME
, theme
.welcome_fg
, theme
.welcome_bg
);
773 init_pair(COLOR_SEPARATOR
, theme
.sep_fg
, theme
.sep_bg
);
774 init_pair(COLOR_TOP
, theme
.default_fg
, theme
.default_bg
);
775 init_pair(COLOR_BOT
, theme
.default_fg
, theme
.default_bg
);
779 * (re-)initialize the curses library FIXME: Error checking
781 static void init_curses(void)
784 if (top
.win
&& refresh() == ERR
) { /* refesh is really needed */
785 msg_n_exit(EXIT_FAILURE
, "refresh() failed\n");
788 curs_set(0); /* make cursor invisible, ignore errors */
789 // if (noraw() == ERR);
790 // msg_n_exit(EXIT_FAILURE, "can not place terminal out of "
792 nonl(); /* tell curses not to do NL->CR/NL on output */
793 noecho(); /* don't echo input */
794 cbreak(); /* take input chars one at a time, no wait for \n */
798 init_wins(theme
.top_lines_default
);
800 noecho(); /* don't echo input */
804 static void check_sigchld(void)
809 ret
= para_reap_child(&pid
);
812 if (pid
== cmd_pid
) {
816 goto reap_next_child
;
820 * This sucker modifies its first argument. *handler and *arg are
821 * pointers to 0-terminated strings (inside line). Crap.
823 static int split_key_map(char *line
, char **handler
, char **arg
)
825 if (!(*handler
= strchr(line
+ 1, ':')))
829 if (!(*arg
= strchr(*handler
, ':')))
838 static int check_key_map_args(void)
842 char *tmp
= NULL
, *handler
, *arg
;
844 for (i
= 0; i
< conf
.key_map_given
; ++i
) {
845 s
= conf
.key_map_arg
[i
];
849 tmp
= para_strdup(s
);
850 if (!split_key_map(tmp
, &handler
, &arg
))
852 if (strlen(handler
) != 1)
861 if (find_cmd_byname(arg
) < 0)
871 * React to various signal-related events
873 static void handle_signal(int sig
)
877 msg_n_exit(EXIT_FAILURE
,
878 "only the good die young (caught SIGTERM))\n");
888 PARA_WARNING_LOG("caught SIGINT, reset");
889 /* Nothing to do. SIGINT killed our child which gets noticed
890 * by do_select and resets everything.
894 PARA_NOTICE_LOG("got SIGUSR1, rereading configuration");
903 static int open_audiod_pipe(void)
911 return para_open_audiod_pipe(conf
.stat_cmd_arg
);
915 * This is the core select loop. Besides the (internal) signal
916 * pipe, the following other fds are checked according to the mode:
918 * GETCH_MODE: check stdin, return when key is pressed
920 * COMMAND_MODE: check command_pipe and stdin. Return when a screen full
921 * of output has been read or when other end has closed command pipe or
922 * when any key is pressed.
924 * EXTERNAL_MODE: Check only signal pipe. Used when an external command
925 * is running. During that time curses is disabled. Returns when
928 static int do_select(int mode
)
932 int max_fileno
, cp_numread
= 1;
933 char command_buf
[4096] = "";
934 int cbo
= 0; /* command buf offset */
937 tv
.tv_sec
= conf
.timeout_arg
/ 1000;
938 tv
.tv_usec
= (conf
.timeout_arg
% 1000) * 1000;
939 // ret = refresh_status();
944 audiod_pipe
= open_audiod_pipe();
945 if (audiod_pipe
>= 0)
946 para_fd_set(audiod_pipe
, &rfds
, &max_fileno
);
948 para_fd_set(signal_pipe
, &rfds
, &max_fileno
);
949 /* command pipe only for COMMAND_MODE */
950 if (command_pipe
>= 0 && mode
== COMMAND_MODE
)
951 para_fd_set(command_pipe
, &rfds
, &max_fileno
);
952 ret
= para_select(max_fileno
+ 1, &rfds
, NULL
, &tv
);
954 goto check_return
; /* skip fd checks */
956 if (FD_ISSET(signal_pipe
, &rfds
)) {
957 int sig_nr
= para_next_signal();
959 handle_signal(sig_nr
);
961 /* read command pipe if ready */
962 if (command_pipe
>= 0 && mode
== COMMAND_MODE
&&
963 FD_ISSET(command_pipe
, &rfds
)) {
964 cp_numread
= read(command_pipe
, command_buf
+ cbo
,
965 sizeof(command_buf
) - 1 - cbo
);
970 PARA_ERROR_LOG("read error (%d)", cp_numread
);
975 if (audiod_pipe
>= 0 && FD_ISSET(audiod_pipe
, &rfds
))
976 if (read_audiod_pipe(audiod_pipe
) <= 0) {
980 free(stat_content
[SI_BASENAME
]);
981 stat_content
[SI_BASENAME
] =
982 para_strdup("audiod not running!?");
988 if (cp_numread
<= 0 && !cbo
) /* command complete */
991 cbo
= for_each_line(command_buf
, cbo
,
992 &add_output_line
, NULL
);
996 ret
= wgetch(top
.win
);
997 if (ret
!= ERR
&& ret
!= KEY_RESIZE
) {
1003 kill(cmd_pid
, SIGTERM
);
1008 ret
= wgetch(top
.win
);
1009 if (ret
!= ERR
&& ret
!= KEY_RESIZE
)
1022 * read from command pipe and print data to bot window
1024 static int send_output(void)
1028 if (command_pipe
< 0)
1030 ret
= mark_fd_nonblocking(command_pipe
);
1032 close(command_pipe
);
1035 if (do_select(COMMAND_MODE
) >= 0)
1036 PARA_INFO_LOG("command complete");
1038 PARA_NOTICE_LOG("command aborted");
1039 print_in_bar(COLOR_MSG
, " ");
1043 static int client_cmd_cmdline(char *cmd
)
1045 int ret
, fds
[3] = {0, 1, 0};
1046 char *c
= make_message(BINDIR
"/para_client -- %s", cmd
);
1048 outputf(COLOR_COMMAND
, "%s", c
);
1049 print_in_bar(COLOR_MSG
, "executing client command, hit any key to abort\n");
1050 ret
= para_exec_cmdline_pid(&cmd_pid
, c
, fds
);
1054 command_pipe
= fds
[1];
1055 return send_output();
1059 * exec command and print output to bot win
1061 static int display_cmd(char *cmd
)
1063 int fds
[3] = {0, 1, 0};
1065 print_in_bar(COLOR_MSG
, "executing display command, hit any key to abort");
1066 outputf(COLOR_COMMAND
, "%s", cmd
);
1067 if (para_exec_cmdline_pid(&cmd_pid
, cmd
, fds
) < 0)
1069 command_pipe
= fds
[1];
1070 return send_output();
1074 * shutdown curses and stat pipe before executing external commands
1076 static int external_cmd(char *cmd
)
1078 int fds
[3] = {-1, -1, -1};
1083 para_exec_cmdline_pid(&cmd_pid
, cmd
, fds
);
1085 do_select(EXTERNAL_MODE
);
1090 static void print_scroll_msg(void)
1092 unsigned lines_total
, filled
= ringbuffer_filled(bot_win_rb
);
1093 int first_rbe
= first_visible_rbe(&lines_total
);
1094 print_in_bar(COLOR_MSG
, "scrolled view: %d-%d/%d\n", filled
- first_rbe
,
1095 filled
- scroll_position
, ringbuffer_filled(bot_win_rb
));
1098 static void com_page_down(void)
1101 int i
= scroll_position
;
1102 while (lines
< bot
.lines
&& --i
> 0) {
1103 struct rb_entry
*rbe
= ringbuffer_get(bot_win_rb
, i
);
1106 lines
+= NUM_LINES(strlen(rbe
->msg
));
1109 scroll_position
= i
;
1114 print_in_bar(COLOR_ERRMSG
, "bottom of buffer is shown\n");
1117 static void com_page_up(void)
1120 int fvr
= first_visible_rbe(&lines
);
1122 if (fvr
< 0 || fvr
+ 1 >= ringbuffer_filled(bot_win_rb
)) {
1123 print_in_bar(COLOR_ERRMSG
, "top of buffer is shown\n");
1126 scroll_position
= fvr
+ 1;
1127 for (; scroll_position
> 0; scroll_position
--) {
1128 first_visible_rbe(&lines
);
1129 if (lines
== bot
.lines
)
1136 static void com_scroll_down(void)
1138 struct rb_entry
*rbe
;
1141 if (!scroll_position
) {
1142 print_in_bar(COLOR_ERRMSG
, "bottom of buffer is shown\n");
1146 rbe
= ringbuffer_get(bot_win_rb
, scroll_position
);
1147 rbe_lines
= NUM_LINES(rbe
->len
);
1148 wscrl(bot
.win
, rbe_lines
);
1149 wmove(bot
.win
, bot
.lines
- rbe_lines
, 0);
1150 wattron(bot
.win
, COLOR_PAIR(rbe
->color
));
1151 waddstr(bot
.win
, rbe
->msg
);
1156 static void com_scroll_up(void)
1158 struct rb_entry
*rbe
= NULL
;
1160 int i
, first_rbe
, num_scroll
;
1162 /* the entry that is going to vanish */
1163 rbe
= ringbuffer_get(bot_win_rb
, scroll_position
);
1166 num_scroll
= NUM_LINES(rbe
->len
);
1167 first_rbe
= first_visible_rbe(&lines
);
1168 if (first_rbe
< 0 || (first_rbe
== ringbuffer_filled(bot_win_rb
) - 1))
1171 wscrl(bot
.win
, -num_scroll
);
1172 i
= draw_top_rbe(&lines
);
1175 while (i
> 0 && lines
< num_scroll
) {
1177 rbe
= ringbuffer_get(bot_win_rb
, --i
);
1180 rbe_lines
= NUM_LINES(rbe
->len
);
1182 // fprintf(stderr, "msg: %s\n", rbe->msg);
1183 wattron(bot
.win
, COLOR_PAIR(rbe
->color
));
1184 waddstr(bot
.win
, "\n");
1185 waddstr(bot
.win
, rbe
->msg
);
1194 print_in_bar(COLOR_ERRMSG
, "top of buffer is shown\n");
1197 static void com_ll_decr(void)
1199 if (loglevel
<= LL_DEBUG
) {
1200 print_in_bar(COLOR_ERRMSG
,
1201 "loglevel already at maximal verbosity\n");
1205 print_in_bar(COLOR_MSG
, "loglevel set to %d\n", loglevel
);
1208 static void com_ll_incr(void)
1210 if (loglevel
>= LL_EMERG
) {
1211 print_in_bar(COLOR_ERRMSG
,
1212 "loglevel already at minimal verbosity\n");
1216 print_in_bar(COLOR_MSG
, "loglevel set to %d\n", loglevel
);
1220 * reread configuration, terminate on errors
1222 static void com_reread_conf(void)
1224 char *cf
=configfile_exists();
1225 struct gui_cmdline_parser_params params
= {
1228 .check_required
= 0,
1229 .check_ambiguity
= 0
1233 PARA_WARNING_LOG("there is no configuration to read");
1236 PARA_INFO_LOG("rereading command line options and config file");
1237 gui_cmdline_parser(_argc
, _argv
, &conf
);
1238 gui_cmdline_parser_config_file(cf
, &conf
, ¶ms
);
1239 PARA_NOTICE_LOG("config file reloaded");
1240 if (check_key_map_args() < 0)
1241 finish(EXIT_FAILURE
);
1244 static void com_help(void)
1248 for (i
= 0; i
< conf
.key_map_given
; ++i
) {
1249 char *handler
, *arg
, *tmp
= para_strdup(conf
.key_map_arg
[i
]);
1250 const char *handler_text
= "???", *desc
= NULL
;
1252 if (!split_key_map(tmp
, &handler
, &arg
)) {
1258 handler_text
= "internal";
1259 desc
= command_list
[find_cmd_byname(arg
)].description
;
1261 case 'x': handler_text
= "external"; break;
1262 case 'd': handler_text
= "display "; break;
1263 case 'p': handler_text
= "para "; break;
1265 outputf(COLOR_MSG
, "%s\t%s\t%s%s\t%s", tmp
, handler_text
, arg
,
1266 strlen(arg
) < 8? "\t" : "",
1270 for (i
= 0; command_list
[i
].handler
; i
++) {
1271 struct gui_command gc
= command_list
[i
];
1273 outputf(COLOR_MSG
, "%s\tinternal\t%s\t%s%s", gc
.key
, gc
.name
,
1274 strlen(gc
.name
) < 8? "\t" : "",
1277 print_in_bar(COLOR_MSG
, "try \"para_gui -h\" or \"para_client help\" "
1281 static void com_shrink_top_win(void)
1283 if (top
.lines
<= theme
.top_lines_min
) {
1284 PARA_WARNING_LOG("can not decrease top window");
1287 init_wins(top
.lines
- 1);
1290 print_in_bar(COLOR_MSG
, "%s", "decreased top window");
1293 static void com_enlarge_top_win(void)
1295 if (bot
.lines
< 3) {
1296 PARA_WARNING_LOG("can not increase top window");
1299 init_wins(top
.lines
+ 1);
1302 print_in_bar(COLOR_MSG
, "increased top window");
1305 static void com_version(void)
1307 print_in_bar(COLOR_MSG
, "para_gui " PACKAGE_VERSION
" \""
1311 __noreturn
static void com_quit(void)
1316 static void com_refresh(void)
1322 static void change_theme(int next
)
1328 /* This seems to be needed twice, why? */
1331 PARA_NOTICE_LOG("new theme: %s", theme
.name
);
1334 static void com_next_theme(void)
1339 static void com_prev_theme(void)
1345 static void handle_command(int c
)
1349 /* first check user's key bindings */
1350 for (i
= 0; i
< conf
.key_map_given
; ++i
) {
1351 char tmp
[MAXLINE
], *handler
, *arg
;
1353 strcpy(tmp
, conf
.key_map_arg
[i
]);
1354 if (!split_key_map(tmp
, &handler
, &arg
))
1356 if (!strcmp(tmp
, km_keyname(c
))) {
1357 if (*handler
== 'd') {
1361 if (*handler
== 'x') {
1365 if (*handler
== 'p') {
1366 client_cmd_cmdline(arg
);
1369 if (*handler
== 'i') {
1370 int num
= find_cmd_byname(arg
);
1372 command_list
[num
].handler();
1377 /* not found, check internal key bindings */
1378 for (i
= 0; command_list
[i
].handler
; i
++) {
1379 if (!strcmp(km_keyname(c
), command_list
[i
].key
)) {
1380 command_list
[i
].handler();
1384 print_in_bar(COLOR_ERRMSG
, "key '%s' is not bound, press ? for help",
1388 int main(int argc
, char *argv
[])
1396 if (gui_cmdline_parser(argc
, argv
, &conf
)) {
1397 fprintf(stderr
, "parse error while reading command line\n");
1400 HANDLE_VERSION_FLAG("gui", conf
);
1401 init_theme(0, &theme
);
1402 top
.lines
= theme
.top_lines_default
;
1403 if (check_key_map_args() < 0) {
1404 fprintf(stderr
, "invalid key map\n");
1407 cf
= configfile_exists();
1408 if (!cf
&& conf
.config_file_given
) {
1409 fprintf(stderr
, "can not read config file %s\n",
1410 conf
.config_file_arg
);
1414 struct gui_cmdline_parser_params params
= {
1417 .check_required
= 0,
1418 .check_ambiguity
= 0
1420 gui_cmdline_parser_config_file(cf
, &conf
, ¶ms
);
1422 loglevel
= get_loglevel_by_name(conf
.loglevel_arg
);
1423 if (check_key_map_args() < 0) {
1424 fprintf(stderr
, "invalid key map in config file\n");
1427 setup_signal_handling();
1428 bot_win_rb
= ringbuffer_new(RINGBUFFER_SIZE
);
1429 initscr(); /* needed only once, always successful */
1434 ret
= do_select(GETCH_MODE
);
1437 print_in_bar(COLOR_MSG
, " ");
1438 handle_command(ret
);