2 * Copyright (C) 1998-2013 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>
16 #include "gui.cmdline.h"
20 #include "ringbuffer.h"
28 /** define the array of error lists needed by para_gui */
30 static char *stat_content
[NUM_STAT_ITEMS
];
32 #define STANDARD_STATUS_BAR "para_gui " PACKAGE_VERSION " (hit ? for help)"
34 static int signal_pipe
;
36 static struct win_data
{
42 } top
, bot
, sb
, in
, sep
;
44 #define RINGBUFFER_SIZE 512
50 static struct ringbuffer
*bot_win_rb
;
51 #define NUM_LINES(len) (1 + (len) / bot.cols)
53 static unsigned scroll_position
;
55 static int curses_active
;
58 static int command_fds
[2];
59 static int stat_pipe
= -1;
60 static struct gui_args_info conf
;
62 enum {GETCH_MODE
, COMMAND_MODE
, EXTERNAL_MODE
};
65 #define COLOR_STATUSBAR 52
66 #define COLOR_COMMAND 53
67 #define COLOR_OUTPUT 54
69 #define COLOR_ERRMSG 56
70 #define COLOR_WELCOME 57
71 #define COLOR_SEPARATOR 58
78 const char *description
;
79 void (*handler
)(void);
85 char postfix
[MAXLINE
];
91 char content
[MAXLINE
];
94 static struct gui_theme theme
;
99 static void com_help(void);
100 static void com_reread_conf(void);
101 static void com_enlarge_top_win(void);
102 static void com_shrink_top_win(void);
103 static void com_version(void);
104 __noreturn
static void com_quit(void);
105 static void com_refresh(void);
106 static void com_ll_incr(void);
107 static void com_ll_decr(void);
108 static void com_prev_theme(void);
109 static void com_next_theme(void);
110 static void com_scroll_up(void);
111 static void com_scroll_down(void);
112 static void com_page_up(void);
113 static void com_page_down(void);
114 static void com_cancel_scrolling(void);
115 static void com_scroll_top(void);
117 static struct gui_command command_list
[] = {
121 .description
= "print help",
125 .name
= "enlarge_win",
126 .description
= "enlarge the top window",
127 .handler
= com_enlarge_top_win
130 .name
= "shrink_win",
131 .description
= "shrink the top window",
132 .handler
= com_shrink_top_win
135 .name
= "reread_conf",
136 .description
= "reread configuration file",
137 .handler
= com_reread_conf
141 .description
= "exit para_gui",
146 .description
= "redraw the screen",
147 .handler
= com_refresh
150 .name
= "next_theme",
151 .description
= "switch to next theme",
152 .handler
= com_next_theme
155 .name
= "prev_theme",
156 .description
= "switch to previous stream",
157 .handler
= com_prev_theme
161 .description
= "increase loglevel (decreases verbosity)",
162 .handler
= com_ll_incr
166 .description
= "decrease loglevel (increases verbosity)",
167 .handler
= com_ll_decr
171 .description
= "show the para_gui version",
172 .handler
= com_version
176 .description
= "scroll up one line",
177 .handler
= com_scroll_up
180 .name
= "scroll_down",
181 .description
= "scroll down one line",
182 .handler
= com_scroll_down
186 .description
= "scroll up one page",
187 .handler
= com_page_up
191 .description
= "scroll down one page",
192 .handler
= com_page_down
195 .name
= "scroll_top",
196 .description
= "scroll to top of buffer",
197 .handler
= com_scroll_top
200 .name
= "cancel_scroll",
201 .description
= "deactivate scroll mode",
202 .handler
= com_cancel_scrolling
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>");
248 sprintf(buf
, "<home>");
252 sprintf(buf
, "<end>");
255 if (c
< 256 && c
> -128 && iscntrl((unsigned char) c
)) {
260 buf
[1] = (c
+ '@') & 0x7f;
263 snprintf(buf
, sizeof(buf
), "\\%d%d%d", c
>> 6,
264 (c
>> 3) & 7, c
& 7);
265 } else if (c
>= KEY_F0
&& c
< KEY_F(256))
266 sprintf(buf
, "<F%d>", c
- KEY_F0
);
268 snprintf(buf
, sizeof(buf
), "%c", (unsigned char) c
);
270 snprintf(buf
, sizeof(buf
), "\\x%hx", (unsigned short) c
);
274 static char *configfile_exists(void)
276 static char *config_file
;
279 if (!conf
.config_file_given
) {
281 char *home
= para_homedir();
282 config_file
= make_message("%s/.paraslash/gui.conf",
288 tmp
= conf
.config_file_arg
;
289 return file_exists(tmp
)? tmp
: NULL
;
292 /* Print given number of spaces to curses window. */
293 static void add_spaces(WINDOW
* win
, unsigned int num
)
296 unsigned sz
= sizeof(space
);
310 * print aligned string to curses window. This function always prints
313 static int align_str(WINDOW
* win
, char *str
, unsigned int len
,
316 int ret
, i
, num
; /* of spaces */
321 ret
= strwidth(str
, &width
);
323 PARA_ERROR_LOG("%s\n", para_strerror(-ret
));
332 /* replace control characters by spaces */
333 for (i
= 0; i
< len
&& str
[i
]; i
++) {
334 if (str
[i
] == '\n' || str
[i
] == '\r' || str
[i
] == '\f')
339 add_spaces(win
, num
);
340 } else if (align
== RIGHT
) {
341 add_spaces(win
, num
);
344 add_spaces(win
, num
/ 2);
345 waddstr(win
, str
[0]? str
: "");
346 add_spaces(win
, num
- num
/ 2);
351 __printf_2_3
static void print_in_bar(int color
, const char *fmt
,...)
358 wattron(in
.win
, COLOR_PAIR(color
));
360 xvasprintf(&msg
, fmt
, ap
);
363 align_str(in
.win
, msg
, sb
.cols
, LEFT
);
369 * update the status bar
371 static void print_status_bar(void)
377 tmp
= para_strdup(STANDARD_STATUS_BAR
);
379 align_str(sb
.win
, tmp
, sb
.cols
, CENTER
);
385 * get the number of the oldest rbe that is (partially) visible. On return,
386 * lines contains the sum of the number of lines of all visable entries. If the
387 * first one is only partially visible, lines is greater than bot.lines.
389 static int first_visible_rbe(unsigned *lines
)
393 for (i
= scroll_position
; i
< RINGBUFFER_SIZE
; i
++) {
394 struct rb_entry
*rbe
= ringbuffer_get(bot_win_rb
, i
);
398 // fprintf(stderr, "found: %s\n", rbe->msg);
399 rbe_lines
= NUM_LINES(rbe
->len
);
400 if (rbe_lines
> bot
.lines
)
403 if (*lines
>= bot
.lines
)
406 return RINGBUFFER_SIZE
- 1;
410 returns number of first visible rbe, *lines is the number of lines drawn.
412 static int draw_top_rbe(unsigned *lines
)
414 int ret
, fvr
= first_visible_rbe(lines
);
415 struct rb_entry
*rbe
;
416 size_t bytes_to_skip
, cells_to_skip
, width
;
420 wmove(bot
.win
, 0, 0);
421 rbe
= ringbuffer_get(bot_win_rb
, fvr
);
424 if (*lines
> bot
.lines
) {
425 /* rbe is partially visible multi-line */
426 cells_to_skip
= (*lines
- bot
.lines
) * bot
.cols
;
427 ret
= skip_cells(rbe
->msg
, cells_to_skip
, &bytes_to_skip
);
430 ret
= strwidth(rbe
->msg
+ bytes_to_skip
, &width
);
437 wattron(bot
.win
, COLOR_PAIR(rbe
->color
));
438 waddstr(bot
.win
, rbe
->msg
+ bytes_to_skip
);
439 *lines
= NUM_LINES(width
);
443 static void redraw_bot_win(void)
448 wmove(bot
.win
, 0, 0);
450 i
= draw_top_rbe(&lines
);
453 while (i
> 0 && lines
< bot
.lines
) {
454 struct rb_entry
*rbe
= ringbuffer_get(bot_win_rb
, --i
);
457 waddstr(bot
.win
, "\n");
460 lines
+= NUM_LINES(rbe
->len
);
461 wattron(bot
.win
, COLOR_PAIR(rbe
->color
));
462 waddstr(bot
.win
, "\n");
463 waddstr(bot
.win
, rbe
->msg
);
469 static void rb_add_entry(int color
, char *msg
)
471 struct rb_entry
*old
, *new;
475 if (strwidth(msg
, &len
) < 0)
477 new = para_malloc(sizeof(struct rb_entry
));
481 old
= ringbuffer_add(bot_win_rb
, new);
482 // fprintf(stderr, "added: %s\n", new->msg);
487 if (scroll_position
) {
488 /* discard current scrolling, like xterm does */
493 wattron(bot
.win
, COLOR_PAIR(color
));
494 getyx(bot
.win
, y
, x
);
496 waddstr(bot
.win
, "\n");
497 waddstr(bot
.win
, msg
);
501 * print formated output to bot win and refresh
503 __printf_2_3
static void outputf(int color
, const char* fmt
,...)
511 xvasprintf(&msg
, fmt
, ap
);
513 rb_add_entry(color
, msg
);
517 static int add_output_line(char *line
, void *data
)
519 int color
= *(int *)data
? COLOR_ERRMSG
: COLOR_OUTPUT
;
522 rb_add_entry(color
, para_strdup(line
));
528 __printf_2_3
void curses_log(int ll
, const char *fmt
,...)
534 if (ll
< loglevel
|| !curses_active
)
543 color
= COLOR_ERRMSG
;
546 xvasprintf(&msg
, fmt
, ap
);
549 rb_add_entry(color
, msg
);
552 __printf_2_3
void (*para_log
)(int, const char*, ...) = curses_log
;
554 static void setup_signal_handling(void)
556 signal_pipe
= para_signal_init();
557 para_install_sighandler(SIGINT
);
558 para_install_sighandler(SIGTERM
);
559 para_install_sighandler(SIGCHLD
);
560 para_install_sighandler(SIGWINCH
);
561 para_install_sighandler(SIGUSR1
);
562 para_sigaction(SIGHUP
, SIG_IGN
);
565 /* kill every process in the process group and exit */
566 __noreturn
static void kill_pg_and_die(int ret
)
568 para_sigaction(SIGTERM
, SIG_IGN
);
573 static void shutdown_curses(void)
582 __noreturn
static void finish(int ret
)
585 kill_pg_and_die(ret
);
589 * exit curses and print given message to stdout/stderr
591 __noreturn __printf_2_3
static void msg_n_exit(int ret
, const char* fmt
, ...)
594 FILE *outfd
= ret
? stderr
: stdout
;
598 vfprintf(outfd
, fmt
, argp
);
600 kill_pg_and_die(ret
);
603 static void print_welcome(void)
605 if (loglevel
> LL_NOTICE
)
607 outputf(COLOR_WELCOME
, "Welcome to para_gui " PACKAGE_VERSION
608 " \"" CODENAME
"\". Theme: %s", theme
.name
);
615 static void init_wins(int top_lines
)
619 top
.lines
= top_lines
;
624 bot
.lines
= LINES
- top
.lines
- 3;
626 bot
.begy
= top
.lines
+ 1;
641 sep
.begy
= top
.lines
;
644 assume_default_colors(theme
.default_fg
, theme
.default_bg
);
646 mvwin(top
.win
, top
.begy
, top
.begx
);
647 wresize(top
.win
, top
.lines
, top
.cols
);
649 mvwin(sb
.win
, sb
.begy
, sb
.begx
);
650 wresize(sb
.win
, sb
.lines
, sb
.cols
);
652 mvwin(sep
.win
, sep
.begy
, sep
.begx
);
653 wresize(sep
.win
, sep
.lines
, sep
.cols
);
655 mvwin(bot
.win
, bot
.begy
, bot
.begx
);
656 wresize(bot
.win
, bot
.lines
, bot
.cols
);
658 mvwin(in
.win
, in
.begy
, in
.begx
);
659 wresize(in
.win
, in
.lines
, in
.cols
);
661 sep
.win
= newwin(sep
.lines
, sep
.cols
, sep
.begy
, sep
.begx
);
662 top
.win
= newwin(top
.lines
, top
.cols
, top
.begy
, top
.begx
);
663 bot
.win
= newwin(bot
.lines
, bot
.cols
, bot
.begy
, bot
.begx
);
664 sb
.win
= newwin(sb
.lines
, sb
.cols
, sb
.begy
, sb
.begx
);
665 in
.win
= newwin(in
.lines
, in
.cols
, in
.begy
, in
.begx
);
666 if (!top
.win
|| !bot
.win
|| !sb
.win
|| !in
.win
|| !sep
.win
)
667 msg_n_exit(1, "Error: Cannot create curses windows\n");
671 scrollok(bot
.win
, 1);
672 wattron(sb
.win
, COLOR_PAIR(COLOR_STATUSBAR
));
673 wattron(sep
.win
, COLOR_PAIR(COLOR_SEPARATOR
));
674 wattron(bot
.win
, COLOR_PAIR(COLOR_BOT
));
675 wattron(top
.win
, COLOR_PAIR(COLOR_TOP
));
687 wmove(sep
.win
, 0, 0);
688 for (i
= 1; i
<= COLS
; i
++)
689 waddstr(sep
.win
, theme
.sep_str
);
692 wnoutrefresh(top
.win
);
693 wnoutrefresh(bot
.win
);
694 //wnoutrefresh(sb.win);
696 wnoutrefresh(in
.win
);
697 wnoutrefresh(sep
.win
);
702 * Print stat item #i to curses window
704 static void print_stat_item(int i
)
707 struct stat_item_data d
= theme
.data
[i
];
708 char *c
= stat_content
[i
];
710 if (!curses_active
|| !d
.len
|| !c
)
712 tmp
= make_message("%s%s%s", d
.prefix
, c
, d
.postfix
);
713 wmove(top
.win
, d
.y
* top
.lines
/ 100, d
.x
* COLS
/ 100);
715 wattron(top
.win
, COLOR_PAIR(i
+ 1));
716 align_str(top
.win
, tmp
, d
.len
* COLS
/ 100, d
.align
);
721 static int update_item(int item_num
, char *buf
)
723 char **c
= stat_content
+ item_num
;
730 *c
= para_strdup("(artist tag not set)");
733 *c
= para_strdup("(title tag not set)");
736 *c
= para_strdup("????");
739 *c
= para_strdup("(album tag not set)");
742 *c
= para_strdup("(comment tag not set)");
746 *c
= para_strdup(buf
);
748 print_stat_item(item_num
);
752 static int read_stat_pipe(fd_set
*rfds
)
755 static int bufsize
, loaded
;
761 if (loaded
>= bufsize
) {
762 if (bufsize
> 1000 * 1000) {
766 bufsize
+= bufsize
+ 1000;
767 buf
= para_realloc(buf
, bufsize
);
769 assert(loaded
< bufsize
);
770 ret
= read_nonblock(stat_pipe
, buf
+ loaded
, bufsize
- loaded
,
773 ret2
= for_each_stat_item(buf
, loaded
, update_item
);
774 if (ret
< 0 || ret2
< 0) {
776 return ret2
< 0? ret2
: ret
;
778 sz
= ret2
; /* what is left */
779 if (sz
> 0 && sz
< loaded
)
780 memmove(buf
, buf
+ loaded
- sz
, sz
);
785 static void print_all_items(void)
791 FOR_EACH_STATUS_ITEM(i
)
795 static void clear_all_items(void)
799 FOR_EACH_STATUS_ITEM(i
) {
800 free(stat_content
[i
]);
801 stat_content
[i
] = para_strdup("");
805 static void init_pair_or_die(short pair
, short f
, short b
)
807 if (init_pair(pair
, f
, b
) == ERR
)
808 msg_n_exit(EXIT_FAILURE
, "fatal: init_pair() failed\n");
811 static void init_colors_or_die(void)
816 msg_n_exit(EXIT_FAILURE
, "fatal: No color term\n");
817 if (start_color() == ERR
)
818 msg_n_exit(EXIT_FAILURE
, "fatal: failed to start colors\n");
819 FOR_EACH_STATUS_ITEM(i
)
820 if (theme
.data
[i
].len
)
821 init_pair_or_die(i
+ 1, theme
.data
[i
].fg
,
823 init_pair_or_die(COLOR_STATUSBAR
, theme
.sb_fg
, theme
.sb_bg
);
824 init_pair_or_die(COLOR_COMMAND
, theme
.cmd_fg
, theme
.cmd_bg
);
825 init_pair_or_die(COLOR_OUTPUT
, theme
.output_fg
, theme
.output_bg
);
826 init_pair_or_die(COLOR_MSG
, theme
.msg_fg
, theme
.msg_bg
);
827 init_pair_or_die(COLOR_ERRMSG
, theme
.err_msg_fg
, theme
.err_msg_bg
);
828 init_pair_or_die(COLOR_WELCOME
, theme
.welcome_fg
, theme
.welcome_bg
);
829 init_pair_or_die(COLOR_SEPARATOR
, theme
.sep_fg
, theme
.sep_bg
);
830 init_pair_or_die(COLOR_TOP
, theme
.default_fg
, theme
.default_bg
);
831 init_pair_or_die(COLOR_BOT
, theme
.default_fg
, theme
.default_bg
);
834 /* (Re-)initialize the curses library. */
835 static void init_curses(void)
838 if (top
.win
&& refresh() == ERR
) /* refesh is really needed */
839 msg_n_exit(EXIT_FAILURE
, "refresh() failed\n");
840 if (LINES
< theme
.lines_min
|| COLS
< theme
.cols_min
)
841 msg_n_exit(EXIT_FAILURE
, "Error: Terminal (%dx%d) too small"
842 " (need at least %dx%d)\n", COLS
, LINES
,
843 theme
.cols_min
, theme
.lines_min
);
844 curs_set(0); /* make cursor invisible, ignore errors */
845 nonl(); /* do not NL->CR/NL on output, always returns OK */
846 /* don't echo input */
848 msg_n_exit(EXIT_FAILURE
, "fatal: noecho() failed\n");
849 /* take input chars one at a time, no wait for \n */
851 msg_n_exit(EXIT_FAILURE
, "fatal: cbreak() failed\n");
852 init_colors_or_die();
853 clear(); /* ignore non-fatal errors */
854 init_wins(theme
.top_lines_default
);
856 // noecho(); /* don't echo input */
859 static void check_sigchld(void)
864 ret
= para_reap_child(&pid
);
869 goto reap_next_child
;
873 * This sucker modifies its first argument. *handler and *arg are
874 * pointers to 0-terminated strings (inside line). Crap.
876 static int split_key_map(char *line
, char **handler
, char **arg
)
878 if (!(*handler
= strchr(line
+ 1, ':')))
882 if (!(*arg
= strchr(*handler
, ':')))
891 static int check_key_map_args(void)
895 char *tmp
= NULL
, *handler
, *arg
;
897 for (i
= 0; i
< conf
.key_map_given
; ++i
) {
898 s
= conf
.key_map_arg
[i
];
902 tmp
= para_strdup(s
);
903 if (!split_key_map(tmp
, &handler
, &arg
))
905 if (strlen(handler
) != 1)
914 if (find_cmd_byname(arg
) < 0)
924 * React to various signal-related events
926 static void handle_signal(int sig
)
930 msg_n_exit(EXIT_FAILURE
,
931 "only the good die young (caught SIGTERM))\n");
941 PARA_WARNING_LOG("caught SIGINT, reset");
942 /* Nothing to do. SIGINT killed our child which gets noticed
943 * by do_select and resets everything.
947 PARA_NOTICE_LOG("got SIGUSR1, rereading configuration");
956 static void status_pre_select(fd_set
*rfds
, int *max_fileno
, struct timeval
*tv
)
958 static struct timeval next_exec
, atm
, diff
;
959 int ret
, fds
[3] = {0, 1, 0};
964 /* Avoid busy loop */
965 gettimeofday(&atm
, NULL
);
966 if (tv_diff(&next_exec
, &atm
, &diff
) > 0) {
967 if (tv_diff(&diff
, tv
, NULL
) < 0)
971 next_exec
.tv_sec
= atm
.tv_sec
+ 2;
972 ret
= para_exec_cmdline_pid(&pid
, conf
.stat_cmd_arg
, fds
);
975 ret
= mark_fd_nonblocking(fds
[1]);
982 para_fd_set(stat_pipe
, rfds
, max_fileno
);
985 #define COMMAND_BUF_SIZE 32768
988 * This is the core select loop. Besides the (internal) signal
989 * pipe, the following other fds are checked according to the mode:
991 * GETCH_MODE: check stdin, return when key is pressed
993 * COMMAND_MODE: check command fds and stdin. Return when peer has closed both
994 * stdout and stderr or when any key is pressed.
996 * EXTERNAL_MODE: Check only signal pipe. Used when an external command
997 * is running. During that time curses is disabled. Returns when
1000 static int do_select(int mode
)
1003 int ret
, i
, max_fileno
;
1004 char command_buf
[2][COMMAND_BUF_SIZE
] = {"", ""};
1005 int cbo
[2] = {0, 0}; /* command buf offsets */
1007 unsigned flags
[2] = {0, 0}; /* for for_each_line() */
1010 tv
.tv_sec
= conf
.timeout_arg
/ 1000;
1011 tv
.tv_usec
= (conf
.timeout_arg
% 1000) * 1000;
1012 // ret = refresh_status();
1015 status_pre_select(&rfds
, &max_fileno
, &tv
);
1017 para_fd_set(signal_pipe
, &rfds
, &max_fileno
);
1018 /* command pipe only for COMMAND_MODE */
1019 if (mode
== COMMAND_MODE
) {
1020 if (command_fds
[0] >= 0)
1021 para_fd_set(command_fds
[0], &rfds
, &max_fileno
);
1022 if (command_fds
[1] >= 0)
1023 para_fd_set(command_fds
[1], &rfds
, &max_fileno
);
1025 if (mode
== GETCH_MODE
|| mode
== COMMAND_MODE
)
1026 para_fd_set(STDIN_FILENO
, &rfds
, &max_fileno
);
1027 ret
= para_select(max_fileno
+ 1, &rfds
, NULL
, &tv
);
1029 goto check_return
; /* skip fd checks */
1031 ret
= para_next_signal(&rfds
);
1034 /* read command pipe if ready */
1035 if (mode
== COMMAND_MODE
) {
1036 for (i
= 0; i
< 2; i
++) {
1038 if (command_fds
[i
] < 0)
1040 ret
= read_nonblock(command_fds
[i
],
1041 command_buf
[i
] + cbo
[i
],
1042 COMMAND_BUF_SIZE
- 1 - cbo
[i
], &rfds
, &sz
);
1045 cbo
[i
] = for_each_line(flags
[i
], command_buf
[i
], cbo
[i
],
1046 add_output_line
, &i
);
1047 if (sz
!= cbo
[i
]) { /* at least one line found */
1052 PARA_NOTICE_LOG("closing command fd %d: %s",
1053 i
, para_strerror(-ret
));
1054 close(command_fds
[i
]);
1055 command_fds
[i
] = -1;
1057 if (command_fds
[!i
] < 0) /* both fds closed */
1060 if (cbo
[i
] == COMMAND_BUF_SIZE
- 1) {
1061 PARA_NOTICE_LOG("discarding overlong line");
1063 flags
[i
] = FELF_DISCARD_FIRST
;
1067 ret
= read_stat_pipe(&rfds
);
1069 PARA_NOTICE_LOG("closing stat pipe: %s\n", para_strerror(-ret
));
1073 free(stat_content
[SI_BASENAME
]);
1074 stat_content
[SI_BASENAME
] =
1075 para_strdup("stat command terminated!?");
1081 ret
= wgetch(top
.win
);
1082 if (ret
!= ERR
&& ret
!= KEY_RESIZE
) {
1083 if (command_fds
[0] >= 0) {
1084 close(command_fds
[0]);
1085 command_fds
[0] = -1;
1087 if (command_fds
[1] >= 0) {
1088 close(command_fds
[1]);
1089 command_fds
[1] = -1;
1092 kill(cmd_pid
, SIGTERM
);
1097 ret
= wgetch(top
.win
);
1098 if (ret
!= ERR
&& ret
!= KEY_RESIZE
)
1109 * read from command pipe and print data to bot window
1111 static void send_output(void)
1115 ret
= mark_fd_nonblocking(command_fds
[0]);
1118 ret
= mark_fd_nonblocking(command_fds
[1]);
1121 if (do_select(COMMAND_MODE
) >= 0)
1122 PARA_INFO_LOG("command complete");
1124 PARA_NOTICE_LOG("command aborted");
1125 print_in_bar(COLOR_MSG
, " ");
1128 PARA_ERROR_LOG("%s\n", para_strerror(-ret
));
1129 close(command_fds
[0]);
1130 close(command_fds
[1]);
1133 static void para_cmd(char *cmd
)
1135 int ret
, fds
[3] = {0, 1, 1};
1136 char *c
= make_message(BINDIR
"/para_client -- %s", cmd
);
1138 outputf(COLOR_COMMAND
, "%s", c
);
1139 print_in_bar(COLOR_MSG
, "executing client command, hit any key to abort\n");
1140 ret
= para_exec_cmdline_pid(&cmd_pid
, c
, fds
);
1144 command_fds
[0] = fds
[1];
1145 command_fds
[1] = fds
[2];
1150 * exec command and print output to bot win
1152 static void display_cmd(char *cmd
)
1154 int fds
[3] = {0, 1, 1};
1156 print_in_bar(COLOR_MSG
, "executing display command, hit any key to abort");
1157 outputf(COLOR_COMMAND
, "%s", cmd
);
1158 if (para_exec_cmdline_pid(&cmd_pid
, cmd
, fds
) < 0)
1160 command_fds
[0] = fds
[1];
1161 command_fds
[1] = fds
[2];
1166 * shutdown curses and stat pipe before executing external commands
1168 static void external_cmd(char *cmd
)
1170 int fds
[3] = {-1, -1, -1};
1175 if (para_exec_cmdline_pid(&cmd_pid
, cmd
, fds
) < 0)
1177 do_select(EXTERNAL_MODE
);
1181 static void print_scroll_msg(void)
1183 unsigned lines_total
, filled
= ringbuffer_filled(bot_win_rb
);
1184 int first_rbe
= first_visible_rbe(&lines_total
);
1185 print_in_bar(COLOR_MSG
, "scrolled view: %d-%d/%d\n", filled
- first_rbe
,
1186 filled
- scroll_position
, ringbuffer_filled(bot_win_rb
));
1189 static void com_scroll_top(void)
1191 int i
= RINGBUFFER_SIZE
- 1;
1194 while (i
> 0 && !ringbuffer_get(bot_win_rb
, i
))
1196 /* i is oldest entry */
1197 for (; lines
< bot
.lines
&& i
>= 0; i
--) {
1198 struct rb_entry
*rbe
= ringbuffer_get(bot_win_rb
, i
);
1201 lines
+= NUM_LINES(rbe
->len
);
1204 if (lines
> 0 && scroll_position
!= i
) {
1205 scroll_position
= i
;
1210 print_in_bar(COLOR_ERRMSG
, "top of buffer is shown\n");
1213 static void com_cancel_scrolling(void)
1216 if (scroll_position
== 0) {
1217 print_in_bar(COLOR_ERRMSG
, "bottom of buffer is shown\n");
1220 scroll_position
= 0;
1224 static void com_page_down(void)
1227 int i
= scroll_position
;
1228 while (lines
< bot
.lines
&& --i
> 0) {
1229 struct rb_entry
*rbe
= ringbuffer_get(bot_win_rb
, i
);
1232 lines
+= NUM_LINES(rbe
->len
);
1235 scroll_position
= i
;
1240 print_in_bar(COLOR_ERRMSG
, "bottom of buffer is shown\n");
1243 static void com_page_up(void)
1246 int fvr
= first_visible_rbe(&lines
);
1248 if (fvr
< 0 || fvr
+ 1 >= ringbuffer_filled(bot_win_rb
)) {
1249 print_in_bar(COLOR_ERRMSG
, "top of buffer is shown\n");
1252 scroll_position
= fvr
+ 1;
1253 for (; scroll_position
> 0; scroll_position
--) {
1254 first_visible_rbe(&lines
);
1255 if (lines
== bot
.lines
)
1262 static void com_scroll_down(void)
1264 struct rb_entry
*rbe
;
1267 if (!scroll_position
) {
1268 print_in_bar(COLOR_ERRMSG
, "bottom of buffer is shown\n");
1272 rbe
= ringbuffer_get(bot_win_rb
, scroll_position
);
1273 rbe_lines
= NUM_LINES(rbe
->len
);
1274 wscrl(bot
.win
, rbe_lines
);
1275 wmove(bot
.win
, bot
.lines
- rbe_lines
, 0);
1276 wattron(bot
.win
, COLOR_PAIR(rbe
->color
));
1277 waddstr(bot
.win
, rbe
->msg
);
1282 static void com_scroll_up(void)
1284 struct rb_entry
*rbe
= NULL
;
1286 int i
, first_rbe
, num_scroll
;
1288 /* the entry that is going to vanish */
1289 rbe
= ringbuffer_get(bot_win_rb
, scroll_position
);
1292 num_scroll
= NUM_LINES(rbe
->len
);
1293 first_rbe
= first_visible_rbe(&lines
);
1294 if (first_rbe
< 0 || (first_rbe
== ringbuffer_filled(bot_win_rb
) - 1))
1297 wscrl(bot
.win
, -num_scroll
);
1298 i
= draw_top_rbe(&lines
);
1301 while (i
> 0 && lines
< num_scroll
) {
1303 rbe
= ringbuffer_get(bot_win_rb
, --i
);
1306 rbe_lines
= NUM_LINES(rbe
->len
);
1308 // fprintf(stderr, "msg: %s\n", rbe->msg);
1309 wattron(bot
.win
, COLOR_PAIR(rbe
->color
));
1310 waddstr(bot
.win
, "\n");
1311 waddstr(bot
.win
, rbe
->msg
);
1320 print_in_bar(COLOR_ERRMSG
, "top of buffer is shown\n");
1323 static void com_ll_decr(void)
1325 if (loglevel
<= LL_DEBUG
) {
1326 print_in_bar(COLOR_ERRMSG
,
1327 "loglevel already at maximal verbosity\n");
1331 print_in_bar(COLOR_MSG
, "loglevel set to %d\n", loglevel
);
1334 static void com_ll_incr(void)
1336 if (loglevel
>= LL_EMERG
) {
1337 print_in_bar(COLOR_ERRMSG
,
1338 "loglevel already at minimal verbosity\n");
1342 print_in_bar(COLOR_MSG
, "loglevel set to %d\n", loglevel
);
1346 * reread configuration, terminate on errors
1348 static void com_reread_conf(void)
1350 char *cf
=configfile_exists();
1351 struct gui_cmdline_parser_params params
= {
1354 .check_required
= 0,
1355 .check_ambiguity
= 0,
1360 PARA_WARNING_LOG("there is no configuration to read");
1363 PARA_INFO_LOG("rereading command line options and config file");
1364 gui_cmdline_parser_ext(_argc
, _argv
, &conf
, ¶ms
);
1365 if (gui_cmdline_parser_config_file(cf
, &conf
, ¶ms
) != 0) {
1366 PARA_EMERG_LOG("errors in config file");
1367 finish(EXIT_FAILURE
);
1369 PARA_NOTICE_LOG("config file reloaded");
1370 if (check_key_map_args() < 0)
1371 finish(EXIT_FAILURE
);
1374 static void com_help(void)
1378 for (i
= 0; i
< conf
.key_map_given
; ++i
) {
1379 char *handler
, *arg
, *tmp
= para_strdup(conf
.key_map_arg
[i
]);
1380 const char *handler_text
= "???", *desc
= NULL
;
1382 if (!split_key_map(tmp
, &handler
, &arg
)) {
1388 handler_text
= "internal";
1389 desc
= command_list
[find_cmd_byname(arg
)].description
;
1391 case 'x': handler_text
= "external"; break;
1392 case 'd': handler_text
= "display "; break;
1393 case 'p': handler_text
= "para "; break;
1395 outputf(COLOR_MSG
, "%s\t%s\t%s%s\t%s", tmp
, handler_text
, arg
,
1396 strlen(arg
) < 8? "\t" : "",
1400 for (i
= 0; command_list
[i
].handler
; i
++) {
1401 struct gui_command gc
= command_list
[i
];
1403 outputf(COLOR_MSG
, "%s\tinternal\t%s\t%s%s", gc
.key
, gc
.name
,
1404 strlen(gc
.name
) < 8? "\t" : "",
1407 print_in_bar(COLOR_MSG
, "try \"para_gui -h\" or \"para_client help\" "
1411 static void com_shrink_top_win(void)
1413 if (top
.lines
<= theme
.top_lines_min
) {
1414 PARA_WARNING_LOG("can not decrease top window");
1417 init_wins(top
.lines
- 1);
1420 print_in_bar(COLOR_MSG
, "%s", "decreased top window");
1423 static void com_enlarge_top_win(void)
1425 if (bot
.lines
< 3) {
1426 PARA_WARNING_LOG("can not increase top window");
1429 init_wins(top
.lines
+ 1);
1432 print_in_bar(COLOR_MSG
, "increased top window");
1435 static void com_version(void)
1437 print_in_bar(COLOR_MSG
, "para_gui " PACKAGE_VERSION
" \""
1441 __noreturn
static void com_quit(void)
1446 static void com_refresh(void)
1452 static void change_theme(int next
)
1458 /* This seems to be needed twice, why? */
1461 PARA_NOTICE_LOG("new theme: %s", theme
.name
);
1464 static void com_next_theme(void)
1469 static void com_prev_theme(void)
1475 static void handle_command(int c
)
1479 /* first check user's key bindings */
1480 for (i
= 0; i
< conf
.key_map_given
; ++i
) {
1481 char *tmp
, *handler
, *arg
;
1483 tmp
= para_strdup(conf
.key_map_arg
[i
]);
1484 if (!split_key_map(tmp
, &handler
, &arg
)) {
1488 if (strcmp(tmp
, km_keyname(c
))) {
1492 if (*handler
== 'd')
1494 else if (*handler
== 'x')
1496 else if (*handler
== 'p')
1498 else if (*handler
== 'i') {
1499 int num
= find_cmd_byname(arg
);
1501 command_list
[num
].handler();
1506 /* not found, check internal key bindings */
1507 for (i
= 0; command_list
[i
].handler
; i
++) {
1508 if (!strcmp(km_keyname(c
), command_list
[i
].key
)) {
1509 command_list
[i
].handler();
1513 print_in_bar(COLOR_ERRMSG
, "key '%s' is not bound, press ? for help",
1517 int main(int argc
, char *argv
[])
1525 gui_cmdline_parser(argc
, argv
, &conf
); /* exits on errors */
1526 HANDLE_VERSION_FLAG("gui", conf
);
1527 cf
= configfile_exists();
1528 if (!cf
&& conf
.config_file_given
) {
1529 fprintf(stderr
, "can not read config file %s\n",
1530 conf
.config_file_arg
);
1534 struct gui_cmdline_parser_params params
= {
1537 .check_required
= 0,
1538 .check_ambiguity
= 0,
1541 if (gui_cmdline_parser_config_file(cf
, &conf
, ¶ms
) != 0)
1544 loglevel
= get_loglevel_by_name(conf
.loglevel_arg
);
1545 if (check_key_map_args() < 0) {
1546 fprintf(stderr
, "invalid key map\n");
1549 init_theme_or_die(conf
.theme_arg
, &theme
);
1550 top
.lines
= theme
.top_lines_default
;
1551 setup_signal_handling();
1552 bot_win_rb
= ringbuffer_new(RINGBUFFER_SIZE
);
1553 setlocale(LC_CTYPE
, "");
1554 initscr(); /* needed only once, always successful */
1559 ret
= do_select(GETCH_MODE
);
1562 print_in_bar(COLOR_MSG
, " ");
1563 handle_command(ret
);