0babfa83baba464cb6ef09906c93e88ebd3dd8b7
2 * Copyright (C) 1998-2006 Andre Noll <maan@systemlinux.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
19 /** \file gui.c ncurses-based interface for paraslash */
21 #include "gui.cmdline.h"
24 #include "ringbuffer.h"
29 /** define the array of error lists needed by para_gui */
31 extern const char *status_item_list
[NUM_STAT_ITEMS
];
32 static char *stat_content
[NUM_STAT_ITEMS
];
34 #define STANDARD_STATUS_BAR "para_gui " VERSION " (hit ? for help)"
36 static int signal_pipe
;
38 static void finish(int sig
);
40 static struct win_data
{
46 } top
, bot
, sb
, in
, sep
;
48 #define RINGBUFFER_SIZE 512
55 #define NUM_LINES(len) (1 + (len) / bot.cols)
57 static unsigned scroll_position
;
59 static int external_cmd_died
, curses_active
;
60 static pid_t external_cmd_pid
;
62 static int command_pipe
= -1;
63 static int audiod_pipe
= -1;
64 static struct gengetopt_args_info conf
;
66 enum {GETCH_MODE
, COMMAND_MODE
, EXTERNAL_MODE
};
69 #define COLOR_STATUSBAR 32
70 #define COLOR_COMMAND 33
71 #define COLOR_OUTPUT 34
73 #define COLOR_ERRMSG 36
74 #define COLOR_WELCOME 37
75 #define COLOR_SEPARATOR 38
82 const char *description
;
83 void (*handler
)(void);
89 char postfix
[MAXLINE
];
95 char content
[MAXLINE
];
98 static struct gui_theme theme
;
103 static void com_help(void);
104 static void com_reread_conf(void);
105 static void com_enlarge_top_win(void);
106 static void com_shrink_top_win(void);
107 static void com_version(void);
108 static void com_quit(void);
109 static void com_refresh(void);
110 static void com_ll_incr(void);
111 static void com_ll_decr(void);
112 static void com_prev_theme(void);
113 static void com_next_theme(void);
114 static void com_scroll_up(void);
115 static void com_scroll_down(void);
116 static void com_page_up(void);
117 static void com_page_down(void);
119 struct gui_command command_list
[] = {
123 .description
= "print help",
127 .name
= "enlarge_win",
128 .description
= "enlarge the top window",
129 .handler
= com_enlarge_top_win
132 .name
= "shrink_win",
133 .description
= "shrink the top window",
134 .handler
= com_shrink_top_win
137 .name
= "reread_conf",
138 .description
= "reread configuration file",
139 .handler
= com_reread_conf
143 .description
= "exit para_gui",
148 .description
= "redraw the screen",
149 .handler
= com_refresh
152 .name
= "next_theme",
153 .description
= "switch to next theme",
154 .handler
= com_next_theme
157 .name
= "prev_theme",
158 .description
= "switch to previous stream",
159 .handler
= com_prev_theme
163 .description
= "increase loglevel (decreases verbosity)",
164 .handler
= com_ll_incr
168 .description
= "decrease loglevel (increases verbosity)",
169 .handler
= com_ll_decr
173 .description
= "show the para_gui version",
174 .handler
= com_version
178 .description
= "scroll up one line",
179 .handler
= com_scroll_up
182 .name
= "scroll_down",
183 .description
= "scroll down one line",
184 .handler
= com_scroll_down
188 .description
= "scroll up one page",
189 .handler
= com_page_up
193 .description
= "scroll down one page",
194 .handler
= com_page_down
200 static int find_cmd_byname(char *name
)
204 for (i
= 0; command_list
[i
].handler
; i
++)
205 if (!strcmp(command_list
[i
].name
, name
))
210 /* taken from mutt */
211 static char *km_keyname(int c
)
216 sprintf(buf
, "<up>");
220 sprintf(buf
, "<down>");
224 sprintf(buf
, "<left>");
227 if (c
== KEY_RIGHT
) {
228 sprintf(buf
, "<right>");
231 if (c
== KEY_NPAGE
) {
232 sprintf(buf
, "<npage>");
235 if (c
== KEY_PPAGE
) {
236 sprintf(buf
, "<ppage>");
239 if (c
< 256 && c
> -128 && iscntrl((unsigned char) c
)) {
244 buf
[1] = (c
+ '@') & 0x7f;
247 snprintf(buf
, sizeof(buf
), "\\%d%d%d", c
>> 6,
248 (c
>> 3) & 7, c
& 7);
249 } else if (c
>= KEY_F0
&& c
< KEY_F(256))
250 sprintf(buf
, "<F%d>", c
- KEY_F0
);
252 snprintf(buf
, sizeof(buf
), "%c", (unsigned char) c
);
254 snprintf(buf
, sizeof(buf
), "\\x%hx", (unsigned short) c
);
258 static char *configfile_exists(void)
260 static char *config_file
;
263 if (!conf
.config_file_given
) {
265 char *home
= para_homedir();
266 config_file
= make_message("%s/.paraslash/gui.conf",
272 tmp
= conf
.config_file_arg
;
273 return file_exists(tmp
)? tmp
: NULL
;
277 * print num spaces to curses window
279 static void add_spaces(WINDOW
* win
, unsigned int num
)
288 * print aligned string to curses window. This function always prints
291 static int align_str(WINDOW
* win
, const char *string
, unsigned int len
,
294 int num
; /* of spaces */
299 num
= len
- strlen(string
);
300 str
= para_strdup(string
);
307 add_spaces(win
, num
);
308 } else if (align
== RIGHT
) {
309 add_spaces(win
, num
);
312 add_spaces(win
, num
/ 2);
313 waddstr(win
, str
[0]? str
: "");
314 add_spaces(win
, num
- num
/ 2);
320 __printf_2_3
static void print_in_bar(int color
, const char *fmt
,...)
326 wattron(in
.win
, COLOR_PAIR(color
));
327 PARA_VSPRINTF(fmt
, msg
);
329 align_str(in
.win
, msg
, sb
.cols
, LEFT
);
335 * update the status bar
337 static void print_status_bar(void)
342 align_str(sb
.win
,STANDARD_STATUS_BAR
, sb
.cols
, CENTER
);
347 * get the number of the oldest rbe that is (partially) visible. On return,
348 * lines contains the sum of the number of lines of all visable entries. If the
349 * first one is only partially visible, lines is greater than bot.lines.
351 static int first_visible_rbe(unsigned *lines
)
355 for (i
= scroll_position
; i
< RINGBUFFER_SIZE
; i
++) {
356 struct rb_entry
*rbe
= ringbuffer_get(bot_win_rb
, i
);
360 // fprintf(stderr, "found: %s\n", rbe->msg);
361 rbe_lines
= NUM_LINES(rbe
->len
);
362 if (rbe_lines
> bot
.lines
)
365 if (*lines
>= bot
.lines
)
368 return RINGBUFFER_SIZE
- 1;
371 static int draw_top_rbe(unsigned *lines
)
374 int offset
, fvr
= first_visible_rbe(lines
);
375 struct rb_entry
*rbe
;
379 wmove(bot
.win
, 0, 0);
380 rbe
= ringbuffer_get(bot_win_rb
, fvr
);
383 /* first rbe might be only partially visible */
384 offset
= (*lines
- bot
.lines
) * bot
.cols
;
385 len
= strlen(rbe
->msg
);
386 if (offset
< 0 || len
< offset
)
388 wattron(bot
.win
, COLOR_PAIR(rbe
->color
));
389 waddstr(bot
.win
, rbe
->msg
+ offset
);
390 *lines
= NUM_LINES(len
- offset
);
394 static void redraw_bot_win(void)
399 wmove(bot
.win
, 0, 0);
401 i
= draw_top_rbe(&lines
);
404 while (i
> 0 && lines
< bot
.lines
) {
405 struct rb_entry
*rbe
= ringbuffer_get(bot_win_rb
, --i
);
408 waddstr(bot
.win
, "\n");
411 lines
+= NUM_LINES(rbe
->len
);
412 wattron(bot
.win
, COLOR_PAIR(rbe
->color
));
413 waddstr(bot
.win
, "\n");
414 waddstr(bot
.win
, rbe
->msg
);
420 static void rb_add_entry(int color
, char *msg
)
422 struct rb_entry
*old
, *new = para_malloc(sizeof(struct rb_entry
));
425 new->len
= strlen(msg
);
427 old
= ringbuffer_add(bot_win_rb
, new);
428 // fprintf(stderr, "added: %s\n", new->msg);
433 if (scroll_position
) {
434 /* discard current scrolling, like xterm does */
439 wattron(bot
.win
, COLOR_PAIR(color
));
440 getyx(bot
.win
, y
, x
);
442 waddstr(bot
.win
, "\n");
443 waddstr(bot
.win
, msg
);
447 * print formated output to bot win and refresh
449 __printf_2_3
static void outputf(int color
, const char* fmt
,...)
455 PARA_VSPRINTF(fmt
, msg
);
456 rb_add_entry(color
, msg
);
460 static void add_output_line(char *line
)
464 rb_add_entry(COLOR_OUTPUT
, para_strdup(line
));
467 void para_log(int ll
, const char *fmt
,...)
472 if (ll
< conf
.loglevel_arg
|| !curses_active
)
481 color
= COLOR_ERRMSG
;
483 PARA_VSPRINTF(fmt
, msg
);
485 rb_add_entry(color
, msg
);
489 static void setup_signal_handling(void)
491 signal_pipe
= para_signal_init();
492 para_install_sighandler(SIGINT
);
493 para_install_sighandler(SIGTERM
);
494 para_install_sighandler(SIGCHLD
);
495 para_install_sighandler(SIGWINCH
);
496 para_install_sighandler(SIGUSR1
);
497 signal(SIGPIPE
, SIG_IGN
);
498 signal(SIGHUP
, SIG_IGN
);
501 static void do_exit(int ret
)
503 signal(SIGTERM
, SIG_IGN
);
508 static void shutdown_curses(void)
517 static void finish(int ret
)
524 * exit curses and print given message to stdout/stderr
526 __printf_2_3
static void msg_n_exit(int ret
, const char* fmt
, ...)
529 FILE *outfd
= ret
? stderr
: stdout
;
533 vfprintf(outfd
, fmt
, argp
);
538 static void print_welcome(void)
540 int ll
= conf
.loglevel_arg
;
543 outputf(COLOR_WELCOME
, "Welcome to para_gui " VERSION
544 " \"" CODENAME
"\". Theme: %s", theme
.name
);
551 static void init_wins(int top_lines
)
555 top
.lines
= top_lines
;
560 bot
.lines
= LINES
- top
.lines
- 3;
562 bot
.begy
= top
.lines
+ 1;
577 sep
.begy
= top
.lines
;
580 assume_default_colors(theme
.default_fg
, theme
.default_bg
);
582 mvwin(top
.win
, top
.begy
, top
.begx
);
583 wresize(top
.win
, top
.lines
, top
.cols
);
585 mvwin(sb
.win
, sb
.begy
, sb
.begx
);
586 wresize(sb
.win
, sb
.lines
, sb
.cols
);
588 mvwin(sep
.win
, sep
.begy
, sep
.begx
);
589 wresize(sep
.win
, sep
.lines
, sep
.cols
);
591 mvwin(bot
.win
, bot
.begy
, bot
.begx
);
592 wresize(bot
.win
, bot
.lines
, bot
.cols
);
594 mvwin(in
.win
, in
.begy
, in
.begx
);
595 wresize(in
.win
, in
.lines
, in
.cols
);
597 sep
.win
= newwin(sep
.lines
, sep
.cols
, sep
.begy
, sep
.begx
);
598 top
.win
= newwin(top
.lines
, top
.cols
, top
.begy
, top
.begx
);
599 bot
.win
= newwin(bot
.lines
, bot
.cols
, bot
.begy
, bot
.begx
);
600 sb
.win
= newwin(sb
.lines
, sb
.cols
, sb
.begy
, sb
.begx
);
601 in
.win
= newwin(in
.lines
, in
.cols
, in
.begy
, in
.begx
);
602 if (!top
.win
|| !bot
.win
|| !sb
.win
|| !in
.win
|| !sep
.win
)
603 msg_n_exit(1, "Error: Cannot create curses windows\n");
607 scrollok(bot
.win
, 1);
608 wattron(sb
.win
, COLOR_PAIR(COLOR_STATUSBAR
));
609 wattron(sep
.win
, COLOR_PAIR(COLOR_SEPARATOR
));
610 wattron(bot
.win
, COLOR_PAIR(COLOR_BOT
));
611 wattron(top
.win
, COLOR_PAIR(COLOR_TOP
));
623 wmove(sep
.win
, 0, 0);
624 for (i
= 1; i
<= COLS
; i
++)
625 waddstr(sep
.win
, theme
.sep_str
);
628 wnoutrefresh(top
.win
);
629 wnoutrefresh(bot
.win
);
630 //wnoutrefresh(sb.win);
632 wnoutrefresh(in
.win
);
633 wnoutrefresh(sep
.win
);
637 static void check_geometry(void)
639 if (LINES
< theme
.lines_min
|| COLS
< theme
.cols_min
)
640 msg_n_exit(EXIT_FAILURE
, "Error: Terminal (%dx%d) too small"
641 " (need at least %dx%d)\n", COLS
, LINES
,
642 theme
.cols_min
, theme
.lines_min
);
646 * Print stat item #i to curses window
648 static void print_stat_item(int i
)
651 struct stat_item_data d
= theme
.data
[i
];
652 char *c
= stat_content
[i
];
654 if (!curses_active
|| !d
.len
|| !c
)
656 tmp
= make_message("%s%s%s", d
.prefix
, c
, d
.postfix
);
657 // PARA_DEBUG_LOG("%s: read: %s\n", __func__, tmp);
658 wmove(top
.win
, d
.y
* top
.lines
/ 100, d
.x
* COLS
/ 100);
660 wattron(top
.win
, COLOR_PAIR(i
+ 1));
661 align_str(top
.win
, tmp
, d
.len
* COLS
/ 100, d
.align
);
666 static void print_all_items(void)
672 for (i
= 0; i
< NUM_STAT_ITEMS
; i
++)
675 static void clear_all_items(void)
679 for (i
= 0; i
< NUM_STAT_ITEMS
; i
++) {
680 free(stat_content
[i
]);
681 stat_content
[i
] = para_strdup("");
685 static void init_colors(void)
689 msg_n_exit(EXIT_FAILURE
, "Error: No color term\n");
691 for (i
= 0; i
< NUM_STAT_ITEMS
; i
++)
692 if (theme
.data
[i
].len
)
693 init_pair(i
+ 1, theme
.data
[i
].fg
, theme
.data
[i
].bg
);
694 init_pair(COLOR_STATUSBAR
, theme
.sb_fg
, theme
.sb_bg
);
695 init_pair(COLOR_COMMAND
, theme
.cmd_fg
, theme
.cmd_bg
);
696 init_pair(COLOR_OUTPUT
, theme
.output_fg
, theme
.output_bg
);
697 init_pair(COLOR_MSG
, theme
.msg_fg
, theme
.msg_bg
);
698 init_pair(COLOR_ERRMSG
, theme
.err_msg_fg
, theme
.err_msg_bg
);
699 init_pair(COLOR_WELCOME
, theme
.welcome_fg
, theme
.welcome_bg
);
700 init_pair(COLOR_SEPARATOR
, theme
.sep_fg
, theme
.sep_bg
);
701 init_pair(COLOR_TOP
, theme
.default_fg
, theme
.default_bg
);
702 init_pair(COLOR_BOT
, theme
.default_fg
, theme
.default_bg
);
706 * (re-)initialize the curses library FIXME: Error checking
708 static void init_curses(void)
711 if (top
.win
&& refresh() == ERR
) { /* refesh is really needed */
712 msg_n_exit(EXIT_FAILURE
, "refresh() failed\n");
715 curs_set(0); /* make cursor invisible, ignore errors */
716 // if (noraw() == ERR);
717 // msg_n_exit(EXIT_FAILURE, "can not place terminal out of "
719 nonl(); /* tell curses not to do NL->CR/NL on output */
720 noecho(); /* don't echo input */
721 cbreak(); /* take input chars one at a time, no wait for \n */
725 init_wins(theme
.top_lines_default
);
727 noecho(); /* don't echo input */
731 static void check_sigchld(void)
735 pid
= para_reap_child();
738 if (pid
== external_cmd_pid
) {
739 external_cmd_pid
= 0;
740 external_cmd_died
= 1;
742 goto reap_next_child
;
746 * print status line if line starts with known command.
748 static void check_stat_line(char *line
)
752 // PARA_INFO_LOG("%s: checking: %s\n", __func__, line);
753 i
= stat_line_valid(line
);
755 line
+= strlen(status_item_list
[i
]) + 1;
756 free(stat_content
[i
]);
757 stat_content
[i
] = para_strdup(line
);
764 * This sucker modifies its first argument. *handler and *arg are
765 * pointers to 0-terminated strings (inside line). Crap.
767 static int split_key_map(char *line
, char **handler
, char **arg
)
769 if (!(*handler
= strchr(line
+ 1, ':')))
773 if (!(*arg
= strchr(*handler
, ':')))
782 static int check_key_map_args(void)
786 char *tmp
= NULL
, *handler
, *arg
;
788 for (i
= 0; i
< conf
.key_map_given
; ++i
) {
789 s
= conf
.key_map_arg
[i
];
793 tmp
= para_strdup(s
);
794 if (!split_key_map(tmp
, &handler
, &arg
))
796 if (strlen(handler
) != 1)
805 if (find_cmd_byname(arg
) < 0)
815 * React to various signal-related events
817 static void handle_signal(int sig
)
821 msg_n_exit(EXIT_FAILURE
,
822 "only the good die young (caught SIGTERM))\n");
832 PARA_WARNING_LOG("%s", "caught SIGINT, reset");
833 /* Nothing to do. SIGINT killed our child, para_client stat.
834 * This get noticed by do_select which resets everything
838 PARA_NOTICE_LOG("%s", "got SIGUSR1, rereading configuration");
847 static int open_audiod_pipe(void)
855 return para_open_audiod_pipe(conf
.stat_cmd_arg
);
859 * This is the core select loop. Besides the (internal) signal
860 * pipe, the following other fds are checked according to the mode:
862 * GETCH_MODE: check stdin, return when key is pressed
864 * COMMAND_MODE: check command_pipe and stdin. Return when a screen full
865 * of output has been read or when other end has closed command pipe or
866 * when any key is pressed.
868 * EXTERNAL_MODE: Check only signal pipe. Used when an external command
869 * is running. During that thime curses is disabled. Returns when
870 * external_cmd_pid == 0.
872 static int do_select(int mode
)
876 int max_fileno
, cp_numread
= 1;
877 char command_buf
[STRINGSIZE
] = "";
878 int cbo
= 0; /* command buf offset */
881 tv
.tv_sec
= conf
.timeout_arg
/ 1000;
882 tv
.tv_usec
= (conf
.timeout_arg
% 1000) * 1000;
883 // ret = refresh_status();
888 audiod_pipe
= open_audiod_pipe();
889 /* FIXME: para_fd_set */
890 if (audiod_pipe
>= 0) {
891 FD_SET(audiod_pipe
, &rfds
);
892 max_fileno
= PARA_MAX(max_fileno
, audiod_pipe
);
896 FD_SET(signal_pipe
, &rfds
);
897 max_fileno
= PARA_MAX(max_fileno
, signal_pipe
);
898 /* command pipe only for COMMAND_MODE */
899 if (command_pipe
>= 0 && mode
== COMMAND_MODE
) {
900 FD_SET(command_pipe
, &rfds
);
901 max_fileno
= PARA_MAX(max_fileno
, command_pipe
);
904 FD_SET(STDIN_FILENO
, &rfds
);
905 ret
= para_select(max_fileno
+ 1, &rfds
, NULL
, &tv
);
906 // PARA_DEBUG_LOG("select returned %d\n", ret);
908 goto check_return
; /* skip fd checks */
910 if (FD_ISSET(signal_pipe
, &rfds
)) {
911 int sig_nr
= para_next_signal();
913 handle_signal(sig_nr
);
915 /* read command pipe if ready */
916 if (command_pipe
>= 0 && mode
== COMMAND_MODE
&&
917 FD_ISSET(command_pipe
, &rfds
)) {
918 cp_numread
= read(command_pipe
, command_buf
+ cbo
,
919 STRINGSIZE
- 1 - cbo
);
924 PARA_ERROR_LOG("read error (%d)", cp_numread
);
929 if (audiod_pipe
>= 0 && FD_ISSET(audiod_pipe
, &rfds
))
930 if (read_audiod_pipe(audiod_pipe
, check_stat_line
) <= 0) {
934 free(stat_content
[SI_STATUS_BAR
]);
935 stat_content
[SI_STATUS_BAR
] =
936 para_strdup("audiod not running!?");
942 if (cp_numread
<= 0 && !cbo
) /* command complete */
945 cbo
= for_each_line(command_buf
, cbo
, &add_output_line
);
951 ret
= wgetch(top
.win
);
952 if (ret
!= ERR
&& ret
!= KEY_RESIZE
)
956 if (external_cmd_died
) {
957 external_cmd_died
= 0;
965 * read from command pipe and print data to bot window
967 static int send_output(void)
969 if (command_pipe
< 0)
971 if (do_select(COMMAND_MODE
) >= 0)
972 PARA_INFO_LOG("%s", "command complete");
974 PARA_NOTICE_LOG("%s", "command aborted");
975 print_in_bar(COLOR_MSG
, " ");
979 static int client_cmd_cmdline(char *cmd
)
982 int ret
, fds
[3] = {0, 1, 0};
983 char *c
= make_message(BINDIR
"/para_client %s", cmd
);
985 outputf(COLOR_COMMAND
, "%s", c
);
986 print_in_bar(COLOR_MSG
, "executing client command, hit q to abort\n");
987 ret
= para_exec_cmdline_pid(&pid
, c
, fds
);
991 command_pipe
= fds
[1];
992 return send_output();
996 * exec command and print output to bot win
998 static int display_cmd(char *cmd
)
1001 int fds
[3] = {0, 1, 0};
1003 print_in_bar(COLOR_MSG
, "executing display command, hit q to abort");
1004 outputf(COLOR_COMMAND
, "%s", cmd
);
1005 if (para_exec_cmdline_pid(&pid
, cmd
, fds
) < 0)
1007 command_pipe
= fds
[1];
1008 return send_output();
1012 * shutdown curses and stat pipe before executing external commands
1014 static int external_cmd(char *cmd
)
1016 int fds
[3] = {-1, -1, -1};
1018 if (external_cmd_pid
)
1021 para_exec_cmdline_pid(&external_cmd_pid
, cmd
, fds
);
1022 do_select(EXTERNAL_MODE
);
1027 static void print_scroll_msg(void)
1029 unsigned lines_total
, filled
= ringbuffer_filled(bot_win_rb
);
1030 int first_rbe
= first_visible_rbe(&lines_total
);
1031 print_in_bar(COLOR_MSG
, "scrolled view: %d-%d/%d\n", filled
- first_rbe
,
1032 filled
- scroll_position
, ringbuffer_filled(bot_win_rb
));
1035 static void com_page_down(void)
1038 int i
= scroll_position
;
1039 while (lines
< bot
.lines
&& --i
> 0) {
1040 struct rb_entry
*rbe
= ringbuffer_get(bot_win_rb
, i
);
1043 lines
+= NUM_LINES(strlen(rbe
->msg
));
1046 scroll_position
= i
;
1051 print_in_bar(COLOR_ERRMSG
, "bottom of buffer is shown\n");
1054 static void com_page_up(void)
1057 int fvr
= first_visible_rbe(&lines
);
1058 if (fvr
< 0 || fvr
+ 1 >= ringbuffer_filled(bot_win_rb
)) {
1059 print_in_bar(COLOR_ERRMSG
, "top of buffer is shown\n");
1062 scroll_position
= fvr
+ 1;
1063 for (; scroll_position
> 0; scroll_position
--) {
1064 fvr
= first_visible_rbe(&lines
);
1065 if (lines
== bot
.lines
)
1072 static void com_scroll_down(void)
1074 struct rb_entry
*rbe
;
1077 if (!scroll_position
) {
1078 print_in_bar(COLOR_ERRMSG
, "bottom of buffer is shown\n");
1082 rbe
= ringbuffer_get(bot_win_rb
, scroll_position
);
1083 rbe_lines
= NUM_LINES(rbe
->len
);
1084 wscrl(bot
.win
, rbe_lines
);
1085 wmove(bot
.win
, bot
.lines
- rbe_lines
, 0);
1086 wattron(bot
.win
, COLOR_PAIR(rbe
->color
));
1087 waddstr(bot
.win
, rbe
->msg
);
1092 static void com_scroll_up(void)
1094 struct rb_entry
*rbe
= NULL
;
1096 int i
, first_rbe
, scroll
;
1098 /* the entry that is going to vanish */
1099 rbe
= ringbuffer_get(bot_win_rb
, scroll_position
);
1102 scroll
= NUM_LINES(rbe
->len
);
1103 first_rbe
= first_visible_rbe(&lines
);
1104 if (first_rbe
< 0 || (first_rbe
== ringbuffer_filled(bot_win_rb
) - 1))
1107 wscrl(bot
.win
, -scroll
);
1108 i
= draw_top_rbe(&lines
);
1111 while (i
> 0 && lines
< scroll
) {
1113 rbe
= ringbuffer_get(bot_win_rb
, --i
);
1116 rbe_lines
= NUM_LINES(rbe
->len
);
1118 // fprintf(stderr, "msg: %s\n", rbe->msg);
1119 wattron(bot
.win
, COLOR_PAIR(rbe
->color
));
1120 waddstr(bot
.win
, "\n");
1121 waddstr(bot
.win
, rbe
->msg
);
1130 print_in_bar(COLOR_ERRMSG
, "top of buffer is shown\n");
1133 static void com_ll_decr(void)
1135 if (conf
.loglevel_arg
<= DEBUG
) {
1136 print_in_bar(COLOR_ERRMSG
,
1137 "loglevel already at maximal verbosity\n");
1140 conf
.loglevel_arg
--;
1141 print_in_bar(COLOR_MSG
, "loglevel set to %d\n", conf
.loglevel_arg
);
1144 static void com_ll_incr(void)
1146 if (conf
.loglevel_arg
>= EMERG
) {
1147 print_in_bar(COLOR_ERRMSG
,
1148 "loglevel already at miminal verbosity\n");
1151 conf
.loglevel_arg
++;
1152 print_in_bar(COLOR_MSG
, "loglevel set to %d\n", conf
.loglevel_arg
);
1156 * reread configuration, terminate on errors
1158 static void com_reread_conf(void)
1160 char *cf
=configfile_exists();
1163 PARA_WARNING_LOG("%s", "there is no configuration to read");
1166 PARA_INFO_LOG("%s", "rereading command line options and config file");
1167 cmdline_parser(_argc
, _argv
, &conf
);
1168 cmdline_parser_configfile(cf
, &conf
, 1, 1, 0);
1169 PARA_NOTICE_LOG("%s", "configuration read");
1170 if (check_key_map_args() < 0)
1171 finish(EXIT_FAILURE
);
1174 static void com_help(void)
1178 for (i
= 0; i
< conf
.key_map_given
; ++i
) {
1179 char *handler
, *arg
, *tmp
= para_strdup(conf
.key_map_arg
[i
]);
1180 const char *handler_text
= "???", *desc
= NULL
;
1182 if (!split_key_map(tmp
, &handler
, &arg
)) {
1188 handler_text
= "internal";
1189 desc
= command_list
[find_cmd_byname(arg
)].description
;
1191 case 'x': handler_text
= "external"; break;
1192 case 'd': handler_text
= "display "; break;
1193 case 'p': handler_text
= "para "; break;
1195 outputf(COLOR_MSG
, "%s\t%s\t%s%s\t%s", tmp
, handler_text
, arg
,
1196 strlen(arg
) < 8? "\t" : "",
1200 for (i
= 0; command_list
[i
].handler
; i
++) {
1201 struct gui_command gc
= command_list
[i
];
1203 outputf(COLOR_MSG
, "%s\tinternal\t%s\t%s%s", gc
.key
, gc
.name
,
1204 strlen(gc
.name
) < 8? "\t" : "",
1207 print_in_bar(COLOR_MSG
, "try \"para_gui -h\" or \"para_client help\" "
1211 static void com_shrink_top_win(void)
1213 if (top
.lines
<= theme
.top_lines_min
) {
1214 PARA_WARNING_LOG("%s", "can not decrease top window");
1217 init_wins(top
.lines
- 1);
1220 print_in_bar(COLOR_MSG
, "%s", "decreased top window");
1223 static void com_enlarge_top_win(void)
1225 if (bot
.lines
< 3) {
1226 PARA_WARNING_LOG("%s", "can not increase top window");
1229 init_wins(top
.lines
+ 1);
1232 print_in_bar(COLOR_MSG
, "increased top window");
1235 static void com_version(void)
1237 print_in_bar(COLOR_MSG
, "para_gui " VERSION
" \"" CODENAME
"\"");
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 (cmdline_parser(argc
, argv
, &conf
)) {
1326 fprintf(stderr
, "parse error while reading command line\n");
1329 init_theme(0, &theme
);
1330 top
.lines
= theme
.top_lines_default
;
1331 if (check_key_map_args() < 0) {
1332 fprintf(stderr
, "invalid key map\n");
1335 cf
= configfile_exists();
1336 if (!cf
&& conf
.config_file_given
) {
1337 fprintf(stderr
, "can not read config file %s\n",
1338 conf
.config_file_arg
);
1342 cmdline_parser_configfile(cf
, &conf
, 0, 0, 0);
1343 if (check_key_map_args() < 0) {
1344 fprintf(stderr
, "invalid key map in config file\n");
1347 setup_signal_handling();
1348 bot_win_rb
= ringbuffer_new(RINGBUFFER_SIZE
);
1349 initscr(); /* needed only once, always successful */
1354 ret
= do_select(GETCH_MODE
);
1357 print_in_bar(COLOR_MSG
, " ");
1358 handle_command(ret
);