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"
29 /** define the array of error lists needed by para_gui */
31 static char *stat_content
[NUM_STAT_ITEMS
];
33 #define STANDARD_STATUS_BAR "para_gui " PACKAGE_VERSION " (hit ? for help)"
35 static int signal_pipe
;
37 static struct win_data
{
43 } top
, bot
, sb
, in
, sep
;
45 #define RINGBUFFER_SIZE 512
51 static struct ringbuffer
*bot_win_rb
;
52 #define NUM_LINES(len) (1 + (len) / bot.cols)
54 static unsigned scroll_position
;
56 static int curses_active
;
59 static int command_fds
[2];
60 static int stat_pipe
= -1;
61 static struct gui_args_info conf
;
63 enum {GETCH_MODE
, COMMAND_MODE
, EXTERNAL_MODE
};
66 #define COLOR_STATUSBAR 52
67 #define COLOR_COMMAND 53
68 #define COLOR_OUTPUT 54
70 #define COLOR_ERRMSG 56
71 #define COLOR_WELCOME 57
72 #define COLOR_SEPARATOR 58
79 const char *description
;
80 void (*handler
)(void);
86 char postfix
[MAXLINE
];
92 char content
[MAXLINE
];
95 static struct gui_theme theme
;
100 static void com_help(void);
101 static void com_reread_conf(void);
102 static void com_enlarge_top_win(void);
103 static void com_shrink_top_win(void);
104 static void com_version(void);
105 __noreturn
static void com_quit(void);
106 static void com_refresh(void);
107 static void com_ll_incr(void);
108 static void com_ll_decr(void);
109 static void com_prev_theme(void);
110 static void com_next_theme(void);
111 static void com_scroll_up(void);
112 static void com_scroll_down(void);
113 static void com_page_up(void);
114 static void com_page_down(void);
115 static void com_cancel_scrolling(void);
116 static void com_scroll_top(void);
118 static struct gui_command command_list
[] = {
122 .description
= "print help",
126 .name
= "enlarge_win",
127 .description
= "enlarge the top window",
128 .handler
= com_enlarge_top_win
131 .name
= "shrink_win",
132 .description
= "shrink the top window",
133 .handler
= com_shrink_top_win
136 .name
= "reread_conf",
137 .description
= "reread configuration file",
138 .handler
= com_reread_conf
142 .description
= "exit para_gui",
147 .description
= "redraw the screen",
148 .handler
= com_refresh
151 .name
= "next_theme",
152 .description
= "switch to next theme",
153 .handler
= com_next_theme
156 .name
= "prev_theme",
157 .description
= "switch to previous stream",
158 .handler
= com_prev_theme
162 .description
= "increase loglevel (decreases verbosity)",
163 .handler
= com_ll_incr
167 .description
= "decrease loglevel (increases verbosity)",
168 .handler
= com_ll_decr
172 .description
= "show the para_gui version",
173 .handler
= com_version
177 .description
= "scroll up one line",
178 .handler
= com_scroll_up
181 .name
= "scroll_down",
182 .description
= "scroll down one line",
183 .handler
= com_scroll_down
187 .description
= "scroll up one page",
188 .handler
= com_page_up
192 .description
= "scroll down one page",
193 .handler
= com_page_down
196 .name
= "scroll_top",
197 .description
= "scroll to top of buffer",
198 .handler
= com_scroll_top
201 .name
= "cancel_scroll",
202 .description
= "deactivate scroll mode",
203 .handler
= com_cancel_scrolling
209 static int find_cmd_byname(char *name
)
213 for (i
= 0; command_list
[i
].handler
; i
++)
214 if (!strcmp(command_list
[i
].name
, name
))
219 /* taken from mutt */
220 static char *km_keyname(int c
)
225 sprintf(buf
, "<up>");
229 sprintf(buf
, "<down>");
233 sprintf(buf
, "<left>");
236 if (c
== KEY_RIGHT
) {
237 sprintf(buf
, "<right>");
240 if (c
== KEY_NPAGE
) {
241 sprintf(buf
, "<npage>");
244 if (c
== KEY_PPAGE
) {
245 sprintf(buf
, "<ppage>");
249 sprintf(buf
, "<home>");
253 sprintf(buf
, "<end>");
256 if (c
< 256 && c
> -128 && iscntrl((unsigned char) c
)) {
261 buf
[1] = (c
+ '@') & 0x7f;
264 snprintf(buf
, sizeof(buf
), "\\%d%d%d", c
>> 6,
265 (c
>> 3) & 7, c
& 7);
266 } else if (c
>= KEY_F0
&& c
< KEY_F(256))
267 sprintf(buf
, "<F%d>", c
- KEY_F0
);
269 snprintf(buf
, sizeof(buf
), "%c", (unsigned char) c
);
271 snprintf(buf
, sizeof(buf
), "\\x%hx", (unsigned short) c
);
275 static char *configfile_exists(void)
277 static char *config_file
;
280 if (!conf
.config_file_given
) {
282 char *home
= para_homedir();
283 config_file
= make_message("%s/.paraslash/gui.conf",
289 tmp
= conf
.config_file_arg
;
290 return file_exists(tmp
)? tmp
: NULL
;
293 /* Print given number of spaces to curses window. */
294 static void add_spaces(WINDOW
* win
, unsigned int num
)
297 unsigned sz
= sizeof(space
) - 1; /* number of spaces */
311 * print aligned string to curses window. This function always prints
314 static int align_str(WINDOW
* win
, char *str
, unsigned int len
,
317 int ret
, i
, num
; /* of spaces */
322 ret
= strwidth(str
, &width
);
324 PARA_ERROR_LOG("%s\n", para_strerror(-ret
));
333 /* replace control characters by spaces */
334 for (i
= 0; i
< len
&& str
[i
]; i
++) {
335 if (str
[i
] == '\n' || str
[i
] == '\r' || str
[i
] == '\f')
340 add_spaces(win
, num
);
341 } else if (align
== RIGHT
) {
342 add_spaces(win
, num
);
345 add_spaces(win
, num
/ 2);
346 waddstr(win
, str
[0]? str
: "");
347 add_spaces(win
, num
- num
/ 2);
352 __printf_2_3
static void print_in_bar(int color
, const char *fmt
,...)
359 wattron(in
.win
, COLOR_PAIR(color
));
361 xvasprintf(&msg
, fmt
, ap
);
364 align_str(in
.win
, msg
, sb
.cols
, LEFT
);
370 * update the status bar
372 static void print_status_bar(void)
378 tmp
= para_strdup(STANDARD_STATUS_BAR
);
380 align_str(sb
.win
, tmp
, sb
.cols
, CENTER
);
386 * get the number of the oldest rbe that is (partially) visible. On return,
387 * lines contains the sum of the number of lines of all visable entries. If the
388 * first one is only partially visible, lines is greater than bot.lines.
390 static int first_visible_rbe(unsigned *lines
)
394 for (i
= scroll_position
; i
< RINGBUFFER_SIZE
; i
++) {
395 struct rb_entry
*rbe
= ringbuffer_get(bot_win_rb
, i
);
399 // fprintf(stderr, "found: %s\n", rbe->msg);
400 rbe_lines
= NUM_LINES(rbe
->len
);
401 if (rbe_lines
> bot
.lines
)
404 if (*lines
>= bot
.lines
)
407 return RINGBUFFER_SIZE
- 1;
411 returns number of first visible rbe, *lines is the number of lines drawn.
413 static int draw_top_rbe(unsigned *lines
)
415 int ret
, fvr
= first_visible_rbe(lines
);
416 struct rb_entry
*rbe
;
417 size_t bytes_to_skip
, cells_to_skip
, width
;
421 wmove(bot
.win
, 0, 0);
422 rbe
= ringbuffer_get(bot_win_rb
, fvr
);
425 if (*lines
> bot
.lines
) {
426 /* rbe is partially visible multi-line */
427 cells_to_skip
= (*lines
- bot
.lines
) * bot
.cols
;
428 ret
= skip_cells(rbe
->msg
, cells_to_skip
, &bytes_to_skip
);
431 ret
= strwidth(rbe
->msg
+ bytes_to_skip
, &width
);
438 wattron(bot
.win
, COLOR_PAIR(rbe
->color
));
439 waddstr(bot
.win
, rbe
->msg
+ bytes_to_skip
);
440 *lines
= NUM_LINES(width
);
444 static void redraw_bot_win(void)
449 wmove(bot
.win
, 0, 0);
451 i
= draw_top_rbe(&lines
);
454 while (i
> 0 && lines
< bot
.lines
) {
455 struct rb_entry
*rbe
= ringbuffer_get(bot_win_rb
, --i
);
458 waddstr(bot
.win
, "\n");
461 lines
+= NUM_LINES(rbe
->len
);
462 wattron(bot
.win
, COLOR_PAIR(rbe
->color
));
463 waddstr(bot
.win
, "\n");
464 waddstr(bot
.win
, rbe
->msg
);
470 static void rb_add_entry(int color
, char *msg
)
472 struct rb_entry
*old
, *new;
476 if (strwidth(msg
, &len
) < 0)
478 new = para_malloc(sizeof(struct rb_entry
));
482 old
= ringbuffer_add(bot_win_rb
, new);
483 // fprintf(stderr, "added: %s\n", new->msg);
488 if (scroll_position
) {
489 /* discard current scrolling, like xterm does */
494 wattron(bot
.win
, COLOR_PAIR(color
));
495 getyx(bot
.win
, y
, x
);
497 waddstr(bot
.win
, "\n");
498 waddstr(bot
.win
, msg
);
502 * print formated output to bot win and refresh
504 __printf_2_3
static void outputf(int color
, const char* fmt
,...)
512 xvasprintf(&msg
, fmt
, ap
);
514 rb_add_entry(color
, msg
);
518 static int add_output_line(char *line
, void *data
)
520 int color
= *(int *)data
? COLOR_ERRMSG
: COLOR_OUTPUT
;
523 rb_add_entry(color
, para_strdup(line
));
529 static __printf_2_3
void curses_log(int ll
, const char *fmt
,...)
535 if (ll
< loglevel
|| !curses_active
)
544 color
= COLOR_ERRMSG
;
547 xvasprintf(&msg
, fmt
, ap
);
550 rb_add_entry(color
, msg
);
553 __printf_2_3
void (*para_log
)(int, const char*, ...) = curses_log
;
555 static void setup_signal_handling(void)
557 signal_pipe
= para_signal_init();
558 para_install_sighandler(SIGINT
);
559 para_install_sighandler(SIGTERM
);
560 para_install_sighandler(SIGCHLD
);
561 para_install_sighandler(SIGWINCH
);
562 para_install_sighandler(SIGUSR1
);
563 para_sigaction(SIGHUP
, SIG_IGN
);
566 /* kill every process in the process group and exit */
567 __noreturn
static void kill_pg_and_die(int ret
)
569 para_sigaction(SIGTERM
, SIG_IGN
);
574 static void shutdown_curses(void)
583 __noreturn
static void finish(int ret
)
586 kill_pg_and_die(ret
);
590 * exit curses and print given message to stdout/stderr
592 __noreturn __printf_2_3
static void msg_n_exit(int ret
, const char* fmt
, ...)
595 FILE *outfd
= ret
? stderr
: stdout
;
599 vfprintf(outfd
, fmt
, argp
);
601 kill_pg_and_die(ret
);
604 static void print_welcome(void)
606 if (loglevel
> LL_NOTICE
)
608 outputf(COLOR_WELCOME
, "Welcome to %s. Theme: %s",
609 version_single_line("gui"), theme
.name
);
616 static void init_wins(int top_lines
)
620 top
.lines
= top_lines
;
625 bot
.lines
= LINES
- top
.lines
- 3;
627 bot
.begy
= top
.lines
+ 1;
642 sep
.begy
= top
.lines
;
645 assume_default_colors(theme
.default_fg
, theme
.default_bg
);
647 mvwin(top
.win
, top
.begy
, top
.begx
);
648 wresize(top
.win
, top
.lines
, top
.cols
);
650 mvwin(sb
.win
, sb
.begy
, sb
.begx
);
651 wresize(sb
.win
, sb
.lines
, sb
.cols
);
653 mvwin(sep
.win
, sep
.begy
, sep
.begx
);
654 wresize(sep
.win
, sep
.lines
, sep
.cols
);
656 mvwin(bot
.win
, bot
.begy
, bot
.begx
);
657 wresize(bot
.win
, bot
.lines
, bot
.cols
);
659 mvwin(in
.win
, in
.begy
, in
.begx
);
660 wresize(in
.win
, in
.lines
, in
.cols
);
662 sep
.win
= newwin(sep
.lines
, sep
.cols
, sep
.begy
, sep
.begx
);
663 top
.win
= newwin(top
.lines
, top
.cols
, top
.begy
, top
.begx
);
664 bot
.win
= newwin(bot
.lines
, bot
.cols
, bot
.begy
, bot
.begx
);
665 sb
.win
= newwin(sb
.lines
, sb
.cols
, sb
.begy
, sb
.begx
);
666 in
.win
= newwin(in
.lines
, in
.cols
, in
.begy
, in
.begx
);
667 if (!top
.win
|| !bot
.win
|| !sb
.win
|| !in
.win
|| !sep
.win
)
668 msg_n_exit(1, "Error: Cannot create curses windows\n");
672 scrollok(bot
.win
, 1);
673 wattron(sb
.win
, COLOR_PAIR(COLOR_STATUSBAR
));
674 wattron(sep
.win
, COLOR_PAIR(COLOR_SEPARATOR
));
675 wattron(bot
.win
, COLOR_PAIR(COLOR_BOT
));
676 wattron(top
.win
, COLOR_PAIR(COLOR_TOP
));
688 wmove(sep
.win
, 0, 0);
689 for (i
= 1; i
<= COLS
; i
++)
690 waddstr(sep
.win
, theme
.sep_str
);
693 wnoutrefresh(top
.win
);
694 wnoutrefresh(bot
.win
);
695 //wnoutrefresh(sb.win);
697 wnoutrefresh(in
.win
);
698 wnoutrefresh(sep
.win
);
703 * Print stat item #i to curses window
705 static void print_stat_item(int i
)
708 struct stat_item_data d
= theme
.data
[i
];
709 char *c
= stat_content
[i
];
711 if (!curses_active
|| !d
.len
|| !c
)
713 tmp
= make_message("%s%s%s", d
.prefix
, c
, d
.postfix
);
714 wmove(top
.win
, d
.y
* top
.lines
/ 100, d
.x
* COLS
/ 100);
716 wattron(top
.win
, COLOR_PAIR(i
+ 1));
717 align_str(top
.win
, tmp
, d
.len
* COLS
/ 100, d
.align
);
722 static int update_item(int item_num
, char *buf
)
724 char **c
= stat_content
+ item_num
;
731 *c
= para_strdup("(artist tag not set)");
734 *c
= para_strdup("(title tag not set)");
737 *c
= para_strdup("????");
740 *c
= para_strdup("(album tag not set)");
743 *c
= para_strdup("(comment tag not set)");
747 *c
= para_strdup(buf
);
749 print_stat_item(item_num
);
753 static int read_stat_pipe(fd_set
*rfds
)
756 static int bufsize
, loaded
;
762 if (loaded
>= bufsize
) {
763 if (bufsize
> 1000 * 1000) {
767 bufsize
+= bufsize
+ 1000;
768 buf
= para_realloc(buf
, bufsize
);
770 assert(loaded
< bufsize
);
771 ret
= read_nonblock(stat_pipe
, buf
+ loaded
, bufsize
- loaded
,
774 ret2
= for_each_stat_item(buf
, loaded
, update_item
);
775 if (ret
< 0 || ret2
< 0) {
777 return ret2
< 0? ret2
: ret
;
779 sz
= ret2
; /* what is left */
780 if (sz
> 0 && sz
< loaded
)
781 memmove(buf
, buf
+ loaded
- sz
, sz
);
786 static void print_all_items(void)
792 FOR_EACH_STATUS_ITEM(i
)
796 static void clear_all_items(void)
800 FOR_EACH_STATUS_ITEM(i
) {
801 free(stat_content
[i
]);
802 stat_content
[i
] = para_strdup("");
806 static void init_pair_or_die(short pair
, short f
, short b
)
808 if (init_pair(pair
, f
, b
) == ERR
)
809 msg_n_exit(EXIT_FAILURE
, "fatal: init_pair() failed\n");
812 static void init_colors_or_die(void)
817 msg_n_exit(EXIT_FAILURE
, "fatal: No color term\n");
818 if (start_color() == ERR
)
819 msg_n_exit(EXIT_FAILURE
, "fatal: failed to start colors\n");
820 FOR_EACH_STATUS_ITEM(i
)
821 if (theme
.data
[i
].len
)
822 init_pair_or_die(i
+ 1, theme
.data
[i
].fg
,
824 init_pair_or_die(COLOR_STATUSBAR
, theme
.sb_fg
, theme
.sb_bg
);
825 init_pair_or_die(COLOR_COMMAND
, theme
.cmd_fg
, theme
.cmd_bg
);
826 init_pair_or_die(COLOR_OUTPUT
, theme
.output_fg
, theme
.output_bg
);
827 init_pair_or_die(COLOR_MSG
, theme
.msg_fg
, theme
.msg_bg
);
828 init_pair_or_die(COLOR_ERRMSG
, theme
.err_msg_fg
, theme
.err_msg_bg
);
829 init_pair_or_die(COLOR_WELCOME
, theme
.welcome_fg
, theme
.welcome_bg
);
830 init_pair_or_die(COLOR_SEPARATOR
, theme
.sep_fg
, theme
.sep_bg
);
831 init_pair_or_die(COLOR_TOP
, theme
.default_fg
, theme
.default_bg
);
832 init_pair_or_die(COLOR_BOT
, theme
.default_fg
, theme
.default_bg
);
835 /* (Re-)initialize the curses library. */
836 static void init_curses(void)
839 if (top
.win
&& refresh() == ERR
) /* refesh is really needed */
840 msg_n_exit(EXIT_FAILURE
, "refresh() failed\n");
841 if (LINES
< theme
.lines_min
|| COLS
< theme
.cols_min
)
842 msg_n_exit(EXIT_FAILURE
, "Error: Terminal (%dx%d) too small"
843 " (need at least %dx%d)\n", COLS
, LINES
,
844 theme
.cols_min
, theme
.lines_min
);
845 curs_set(0); /* make cursor invisible, ignore errors */
846 nonl(); /* do not NL->CR/NL on output, always returns OK */
847 /* don't echo input */
849 msg_n_exit(EXIT_FAILURE
, "fatal: noecho() failed\n");
850 /* take input chars one at a time, no wait for \n */
852 msg_n_exit(EXIT_FAILURE
, "fatal: cbreak() failed\n");
853 init_colors_or_die();
854 clear(); /* ignore non-fatal errors */
855 init_wins(theme
.top_lines_default
);
857 // noecho(); /* don't echo input */
860 static void check_sigchld(void)
865 ret
= para_reap_child(&pid
);
870 goto reap_next_child
;
874 * This sucker modifies its first argument. *handler and *arg are
875 * pointers to 0-terminated strings (inside line). Crap.
877 static int split_key_map(char *line
, char **handler
, char **arg
)
879 if (!(*handler
= strchr(line
+ 1, ':')))
883 if (!(*arg
= strchr(*handler
, ':')))
892 static int check_key_map_args(void)
896 char *tmp
= NULL
, *handler
, *arg
;
898 for (i
= 0; i
< conf
.key_map_given
; ++i
) {
899 s
= conf
.key_map_arg
[i
];
903 tmp
= para_strdup(s
);
904 if (!split_key_map(tmp
, &handler
, &arg
))
906 if (strlen(handler
) != 1)
915 if (find_cmd_byname(arg
) < 0)
925 * React to various signal-related events
927 static void handle_signal(int sig
)
931 msg_n_exit(EXIT_FAILURE
,
932 "only the good die young (caught SIGTERM))\n");
942 PARA_WARNING_LOG("caught SIGINT, reset");
943 /* Nothing to do. SIGINT killed our child which gets noticed
944 * by do_select and resets everything.
948 PARA_NOTICE_LOG("got SIGUSR1, rereading configuration");
957 static void status_pre_select(fd_set
*rfds
, int *max_fileno
, struct timeval
*tv
)
959 static struct timeval next_exec
, atm
, diff
;
960 int ret
, fds
[3] = {0, 1, 0};
965 /* Avoid busy loop */
966 gettimeofday(&atm
, NULL
);
967 if (tv_diff(&next_exec
, &atm
, &diff
) > 0) {
968 if (tv_diff(&diff
, tv
, NULL
) < 0)
972 next_exec
.tv_sec
= atm
.tv_sec
+ 2;
973 ret
= para_exec_cmdline_pid(&pid
, conf
.stat_cmd_arg
, fds
);
976 ret
= mark_fd_nonblocking(fds
[1]);
983 para_fd_set(stat_pipe
, rfds
, max_fileno
);
986 #define COMMAND_BUF_SIZE 32768
989 * This is the core select loop. Besides the (internal) signal
990 * pipe, the following other fds are checked according to the mode:
992 * GETCH_MODE: check stdin, return when key is pressed
994 * COMMAND_MODE: check command fds and stdin. Return when peer has closed both
995 * stdout and stderr or when any key is pressed.
997 * EXTERNAL_MODE: Check only signal pipe. Used when an external command
998 * is running. During that time curses is disabled. Returns when
1001 static int do_select(int mode
)
1004 int ret
, i
, max_fileno
;
1005 char command_buf
[2][COMMAND_BUF_SIZE
] = {"", ""};
1006 int cbo
[2] = {0, 0}; /* command buf offsets */
1008 unsigned flags
[2] = {0, 0}; /* for for_each_line() */
1011 tv
.tv_sec
= conf
.timeout_arg
/ 1000;
1012 tv
.tv_usec
= (conf
.timeout_arg
% 1000) * 1000;
1013 // ret = refresh_status();
1016 status_pre_select(&rfds
, &max_fileno
, &tv
);
1018 para_fd_set(signal_pipe
, &rfds
, &max_fileno
);
1019 /* command pipe only for COMMAND_MODE */
1020 if (mode
== COMMAND_MODE
) {
1021 if (command_fds
[0] >= 0)
1022 para_fd_set(command_fds
[0], &rfds
, &max_fileno
);
1023 if (command_fds
[1] >= 0)
1024 para_fd_set(command_fds
[1], &rfds
, &max_fileno
);
1026 if (mode
== GETCH_MODE
|| mode
== COMMAND_MODE
)
1027 para_fd_set(STDIN_FILENO
, &rfds
, &max_fileno
);
1028 ret
= para_select(max_fileno
+ 1, &rfds
, NULL
, &tv
);
1030 goto check_return
; /* skip fd checks */
1032 ret
= para_next_signal(&rfds
);
1035 /* read command pipe if ready */
1036 if (mode
== COMMAND_MODE
) {
1037 for (i
= 0; i
< 2; i
++) {
1039 if (command_fds
[i
] < 0)
1041 ret
= read_nonblock(command_fds
[i
],
1042 command_buf
[i
] + cbo
[i
],
1043 COMMAND_BUF_SIZE
- 1 - cbo
[i
], &rfds
, &sz
);
1046 cbo
[i
] = for_each_line(flags
[i
], command_buf
[i
], cbo
[i
],
1047 add_output_line
, &i
);
1048 if (sz
!= cbo
[i
]) { /* at least one line found */
1053 PARA_NOTICE_LOG("closing command fd %d: %s",
1054 i
, para_strerror(-ret
));
1055 close(command_fds
[i
]);
1056 command_fds
[i
] = -1;
1058 if (command_fds
[!i
] < 0) /* both fds closed */
1061 if (cbo
[i
] == COMMAND_BUF_SIZE
- 1) {
1062 PARA_NOTICE_LOG("discarding overlong line");
1064 flags
[i
] = FELF_DISCARD_FIRST
;
1068 ret
= read_stat_pipe(&rfds
);
1070 PARA_NOTICE_LOG("closing stat pipe: %s\n", para_strerror(-ret
));
1074 free(stat_content
[SI_BASENAME
]);
1075 stat_content
[SI_BASENAME
] =
1076 para_strdup("stat command terminated!?");
1082 ret
= wgetch(top
.win
);
1083 if (ret
!= ERR
&& ret
!= KEY_RESIZE
) {
1084 if (command_fds
[0] >= 0) {
1085 close(command_fds
[0]);
1086 command_fds
[0] = -1;
1088 if (command_fds
[1] >= 0) {
1089 close(command_fds
[1]);
1090 command_fds
[1] = -1;
1093 kill(cmd_pid
, SIGTERM
);
1098 ret
= wgetch(top
.win
);
1099 if (ret
!= ERR
&& ret
!= KEY_RESIZE
)
1110 * read from command pipe and print data to bot window
1112 static void send_output(void)
1116 ret
= mark_fd_nonblocking(command_fds
[0]);
1119 ret
= mark_fd_nonblocking(command_fds
[1]);
1122 if (do_select(COMMAND_MODE
) >= 0)
1123 PARA_INFO_LOG("command complete");
1125 PARA_NOTICE_LOG("command aborted");
1126 print_in_bar(COLOR_MSG
, " ");
1129 PARA_ERROR_LOG("%s\n", para_strerror(-ret
));
1130 close(command_fds
[0]);
1131 close(command_fds
[1]);
1134 static void para_cmd(char *cmd
)
1136 int ret
, fds
[3] = {0, 1, 1};
1137 char *c
= make_message(BINDIR
"/para_client -- %s", cmd
);
1139 outputf(COLOR_COMMAND
, "%s", c
);
1140 print_in_bar(COLOR_MSG
, "executing client command, hit any key to abort\n");
1141 ret
= para_exec_cmdline_pid(&cmd_pid
, c
, fds
);
1145 command_fds
[0] = fds
[1];
1146 command_fds
[1] = fds
[2];
1151 * exec command and print output to bot win
1153 static void display_cmd(char *cmd
)
1155 int fds
[3] = {0, 1, 1};
1157 print_in_bar(COLOR_MSG
, "executing display command, hit any key to abort");
1158 outputf(COLOR_COMMAND
, "%s", cmd
);
1159 if (para_exec_cmdline_pid(&cmd_pid
, cmd
, fds
) < 0)
1161 command_fds
[0] = fds
[1];
1162 command_fds
[1] = fds
[2];
1167 * shutdown curses and stat pipe before executing external commands
1169 static void external_cmd(char *cmd
)
1171 int fds
[3] = {-1, -1, -1};
1176 if (para_exec_cmdline_pid(&cmd_pid
, cmd
, fds
) < 0)
1178 do_select(EXTERNAL_MODE
);
1182 static void print_scroll_msg(void)
1184 unsigned lines_total
, filled
= ringbuffer_filled(bot_win_rb
);
1185 int first_rbe
= first_visible_rbe(&lines_total
);
1186 print_in_bar(COLOR_MSG
, "scrolled view: %d-%d/%d\n", filled
- first_rbe
,
1187 filled
- scroll_position
, ringbuffer_filled(bot_win_rb
));
1190 static void com_scroll_top(void)
1192 int i
= RINGBUFFER_SIZE
- 1;
1195 while (i
> 0 && !ringbuffer_get(bot_win_rb
, i
))
1197 /* i is oldest entry */
1198 for (; lines
< bot
.lines
&& i
>= 0; i
--) {
1199 struct rb_entry
*rbe
= ringbuffer_get(bot_win_rb
, i
);
1202 lines
+= NUM_LINES(rbe
->len
);
1205 if (lines
> 0 && scroll_position
!= i
) {
1206 scroll_position
= i
;
1211 print_in_bar(COLOR_ERRMSG
, "top of buffer is shown\n");
1214 static void com_cancel_scrolling(void)
1217 if (scroll_position
== 0) {
1218 print_in_bar(COLOR_ERRMSG
, "bottom of buffer is shown\n");
1221 scroll_position
= 0;
1225 static void com_page_down(void)
1228 int i
= scroll_position
;
1229 while (lines
< bot
.lines
&& --i
> 0) {
1230 struct rb_entry
*rbe
= ringbuffer_get(bot_win_rb
, i
);
1233 lines
+= NUM_LINES(rbe
->len
);
1236 scroll_position
= i
;
1241 print_in_bar(COLOR_ERRMSG
, "bottom of buffer is shown\n");
1244 static void com_page_up(void)
1247 int fvr
= first_visible_rbe(&lines
);
1249 if (fvr
< 0 || fvr
+ 1 >= ringbuffer_filled(bot_win_rb
)) {
1250 print_in_bar(COLOR_ERRMSG
, "top of buffer is shown\n");
1253 scroll_position
= fvr
+ 1;
1254 for (; scroll_position
> 0; scroll_position
--) {
1255 first_visible_rbe(&lines
);
1256 if (lines
== bot
.lines
)
1263 static void com_scroll_down(void)
1265 struct rb_entry
*rbe
;
1268 if (!scroll_position
) {
1269 print_in_bar(COLOR_ERRMSG
, "bottom of buffer is shown\n");
1273 rbe
= ringbuffer_get(bot_win_rb
, scroll_position
);
1274 rbe_lines
= NUM_LINES(rbe
->len
);
1275 wscrl(bot
.win
, rbe_lines
);
1276 wmove(bot
.win
, bot
.lines
- rbe_lines
, 0);
1277 wattron(bot
.win
, COLOR_PAIR(rbe
->color
));
1278 waddstr(bot
.win
, rbe
->msg
);
1283 static void com_scroll_up(void)
1285 struct rb_entry
*rbe
= NULL
;
1287 int i
, first_rbe
, num_scroll
;
1289 /* the entry that is going to vanish */
1290 rbe
= ringbuffer_get(bot_win_rb
, scroll_position
);
1293 num_scroll
= NUM_LINES(rbe
->len
);
1294 first_rbe
= first_visible_rbe(&lines
);
1295 if (first_rbe
< 0 || (first_rbe
== ringbuffer_filled(bot_win_rb
) - 1))
1298 wscrl(bot
.win
, -num_scroll
);
1299 i
= draw_top_rbe(&lines
);
1302 while (i
> 0 && lines
< num_scroll
) {
1304 rbe
= ringbuffer_get(bot_win_rb
, --i
);
1307 rbe_lines
= NUM_LINES(rbe
->len
);
1309 // fprintf(stderr, "msg: %s\n", rbe->msg);
1310 wattron(bot
.win
, COLOR_PAIR(rbe
->color
));
1311 waddstr(bot
.win
, "\n");
1312 waddstr(bot
.win
, rbe
->msg
);
1321 print_in_bar(COLOR_ERRMSG
, "top of buffer is shown\n");
1324 static void com_ll_decr(void)
1326 if (loglevel
<= LL_DEBUG
) {
1327 print_in_bar(COLOR_ERRMSG
,
1328 "loglevel already at maximal verbosity\n");
1332 print_in_bar(COLOR_MSG
, "loglevel set to %d\n", loglevel
);
1335 static void com_ll_incr(void)
1337 if (loglevel
>= LL_EMERG
) {
1338 print_in_bar(COLOR_ERRMSG
,
1339 "loglevel already at minimal verbosity\n");
1343 print_in_bar(COLOR_MSG
, "loglevel set to %d\n", loglevel
);
1347 * reread configuration, terminate on errors
1349 static void com_reread_conf(void)
1351 char *cf
=configfile_exists();
1352 struct gui_cmdline_parser_params params
= {
1355 .check_required
= 0,
1356 .check_ambiguity
= 0,
1361 PARA_WARNING_LOG("there is no configuration to read");
1364 PARA_INFO_LOG("rereading command line options and config file");
1365 gui_cmdline_parser_ext(_argc
, _argv
, &conf
, ¶ms
);
1366 gui_cmdline_parser_config_file(cf
, &conf
, ¶ms
);
1367 PARA_NOTICE_LOG("config file reloaded");
1368 if (check_key_map_args() < 0)
1369 finish(EXIT_FAILURE
);
1372 static void com_help(void)
1376 for (i
= 0; i
< conf
.key_map_given
; ++i
) {
1377 char *handler
, *arg
, *tmp
= para_strdup(conf
.key_map_arg
[i
]);
1378 const char *handler_text
= "???", *desc
= NULL
;
1380 if (!split_key_map(tmp
, &handler
, &arg
)) {
1386 handler_text
= "internal";
1387 desc
= command_list
[find_cmd_byname(arg
)].description
;
1389 case 'x': handler_text
= "external"; break;
1390 case 'd': handler_text
= "display "; break;
1391 case 'p': handler_text
= "para "; break;
1393 outputf(COLOR_MSG
, "%s\t%s\t%s%s\t%s", tmp
, handler_text
, arg
,
1394 strlen(arg
) < 8? "\t" : "",
1398 for (i
= 0; command_list
[i
].handler
; i
++) {
1399 struct gui_command gc
= command_list
[i
];
1401 outputf(COLOR_MSG
, "%s\tinternal\t%s\t%s%s", gc
.key
, gc
.name
,
1402 strlen(gc
.name
) < 8? "\t" : "",
1405 print_in_bar(COLOR_MSG
, "try \"para_gui -h\" or \"para_client help\" "
1409 static void com_shrink_top_win(void)
1411 if (top
.lines
<= theme
.top_lines_min
) {
1412 PARA_WARNING_LOG("can not decrease top window");
1415 init_wins(top
.lines
- 1);
1418 print_in_bar(COLOR_MSG
, "%s", "decreased top window");
1421 static void com_enlarge_top_win(void)
1423 if (bot
.lines
< 3) {
1424 PARA_WARNING_LOG("can not increase top window");
1427 init_wins(top
.lines
+ 1);
1430 print_in_bar(COLOR_MSG
, "increased top window");
1433 static void com_version(void)
1435 print_in_bar(COLOR_MSG
, "%s", version_single_line("gui"));
1438 __noreturn
static void com_quit(void)
1443 static void com_refresh(void)
1449 static void change_theme(int next
)
1455 /* This seems to be needed twice, why? */
1458 PARA_NOTICE_LOG("new theme: %s", theme
.name
);
1461 static void com_next_theme(void)
1466 static void com_prev_theme(void)
1472 static void handle_command(int c
)
1476 /* first check user's key bindings */
1477 for (i
= 0; i
< conf
.key_map_given
; ++i
) {
1478 char *tmp
, *handler
, *arg
;
1480 tmp
= para_strdup(conf
.key_map_arg
[i
]);
1481 if (!split_key_map(tmp
, &handler
, &arg
)) {
1485 if (strcmp(tmp
, km_keyname(c
))) {
1489 if (*handler
== 'd')
1491 else if (*handler
== 'x')
1493 else if (*handler
== 'p')
1495 else if (*handler
== 'i') {
1496 int num
= find_cmd_byname(arg
);
1498 command_list
[num
].handler();
1503 /* not found, check internal key bindings */
1504 for (i
= 0; command_list
[i
].handler
; i
++) {
1505 if (!strcmp(km_keyname(c
), command_list
[i
].key
)) {
1506 command_list
[i
].handler();
1510 print_in_bar(COLOR_ERRMSG
, "key '%s' is not bound, press ? for help",
1514 __noreturn
static void print_help_and_die(void)
1516 struct ggo_help h
= DEFINE_GGO_HELP(gui
);
1517 bool d
= conf
.detailed_help_given
;
1519 ggo_print_help(&h
, d
? GPH_STANDARD_FLAGS_DETAILED
: GPH_STANDARD_FLAGS
);
1523 int main(int argc
, char *argv
[])
1531 gui_cmdline_parser(argc
, argv
, &conf
); /* exits on errors */
1532 loglevel
= get_loglevel_by_name(conf
.loglevel_arg
);
1533 version_handle_flag("gui", conf
.version_given
);
1534 if (conf
.help_given
|| conf
.detailed_help_given
)
1535 print_help_and_die();
1536 cf
= configfile_exists();
1537 if (!cf
&& conf
.config_file_given
) {
1538 fprintf(stderr
, "can not read config file %s\n",
1539 conf
.config_file_arg
);
1543 struct gui_cmdline_parser_params params
= {
1546 .check_required
= 0,
1547 .check_ambiguity
= 0,
1550 gui_cmdline_parser_config_file(cf
, &conf
, ¶ms
);
1551 loglevel
= get_loglevel_by_name(conf
.loglevel_arg
);
1553 if (check_key_map_args() < 0) {
1554 fprintf(stderr
, "invalid key map\n");
1557 init_theme_or_die(conf
.theme_arg
, &theme
);
1558 top
.lines
= theme
.top_lines_default
;
1559 setup_signal_handling();
1560 bot_win_rb
= ringbuffer_new(RINGBUFFER_SIZE
);
1561 setlocale(LC_CTYPE
, "");
1562 initscr(); /* needed only once, always successful */
1567 ret
= do_select(GETCH_MODE
);
1570 print_in_bar(COLOR_MSG
, " ");
1571 handle_command(ret
);