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>
15 #include "gui.cmdline.h"
19 #include "ringbuffer.h"
27 /** define the array of error lists needed by para_gui */
29 static char *stat_content
[NUM_STAT_ITEMS
];
31 #define STANDARD_STATUS_BAR "para_gui " PACKAGE_VERSION " (hit ? for help)"
33 static int signal_pipe
;
35 static struct win_data
{
41 } top
, bot
, sb
, in
, sep
;
43 #define RINGBUFFER_SIZE 512
49 static struct ringbuffer
*bot_win_rb
;
50 #define NUM_LINES(len) (1 + (len) / bot.cols)
52 static unsigned scroll_position
;
54 static int curses_active
;
57 static int command_fds
[2];
58 static int stat_pipe
= -1;
59 static struct gui_args_info conf
;
61 enum {GETCH_MODE
, COMMAND_MODE
, EXTERNAL_MODE
};
64 #define COLOR_STATUSBAR 52
65 #define COLOR_COMMAND 53
66 #define COLOR_OUTPUT 54
68 #define COLOR_ERRMSG 56
69 #define COLOR_WELCOME 57
70 #define COLOR_SEPARATOR 58
77 const char *description
;
78 void (*handler
)(void);
84 char postfix
[MAXLINE
];
90 char content
[MAXLINE
];
93 static struct gui_theme theme
;
98 static void com_help(void);
99 static void com_reread_conf(void);
100 static void com_enlarge_top_win(void);
101 static void com_shrink_top_win(void);
102 static void com_version(void);
103 __noreturn
static void com_quit(void);
104 static void com_refresh(void);
105 static void com_ll_incr(void);
106 static void com_ll_decr(void);
107 static void com_prev_theme(void);
108 static void com_next_theme(void);
109 static void com_scroll_up(void);
110 static void com_scroll_down(void);
111 static void com_page_up(void);
112 static void com_page_down(void);
113 static void com_cancel_scrolling(void);
114 static void com_scroll_top(void);
116 static struct gui_command command_list
[] = {
120 .description
= "print help",
124 .name
= "enlarge_win",
125 .description
= "enlarge the top window",
126 .handler
= com_enlarge_top_win
129 .name
= "shrink_win",
130 .description
= "shrink the top window",
131 .handler
= com_shrink_top_win
134 .name
= "reread_conf",
135 .description
= "reread configuration file",
136 .handler
= com_reread_conf
140 .description
= "exit para_gui",
145 .description
= "redraw the screen",
146 .handler
= com_refresh
149 .name
= "next_theme",
150 .description
= "switch to next theme",
151 .handler
= com_next_theme
154 .name
= "prev_theme",
155 .description
= "switch to previous stream",
156 .handler
= com_prev_theme
160 .description
= "increase loglevel (decreases verbosity)",
161 .handler
= com_ll_incr
165 .description
= "decrease loglevel (increases verbosity)",
166 .handler
= com_ll_decr
170 .description
= "show the para_gui version",
171 .handler
= com_version
175 .description
= "scroll up one line",
176 .handler
= com_scroll_up
179 .name
= "scroll_down",
180 .description
= "scroll down one line",
181 .handler
= com_scroll_down
185 .description
= "scroll up one page",
186 .handler
= com_page_up
190 .description
= "scroll down one page",
191 .handler
= com_page_down
194 .name
= "scroll_top",
195 .description
= "scroll to top of buffer",
196 .handler
= com_scroll_top
199 .name
= "cancel_scroll",
200 .description
= "deactivate scroll mode",
201 .handler
= com_cancel_scrolling
207 static int find_cmd_byname(char *name
)
211 for (i
= 0; command_list
[i
].handler
; i
++)
212 if (!strcmp(command_list
[i
].name
, name
))
217 /* taken from mutt */
218 static char *km_keyname(int c
)
223 sprintf(buf
, "<up>");
227 sprintf(buf
, "<down>");
231 sprintf(buf
, "<left>");
234 if (c
== KEY_RIGHT
) {
235 sprintf(buf
, "<right>");
238 if (c
== KEY_NPAGE
) {
239 sprintf(buf
, "<npage>");
242 if (c
== KEY_PPAGE
) {
243 sprintf(buf
, "<ppage>");
247 sprintf(buf
, "<home>");
251 sprintf(buf
, "<end>");
254 if (c
< 256 && c
> -128 && iscntrl((unsigned char) c
)) {
259 buf
[1] = (c
+ '@') & 0x7f;
262 snprintf(buf
, sizeof(buf
), "\\%d%d%d", c
>> 6,
263 (c
>> 3) & 7, c
& 7);
264 } else if (c
>= KEY_F0
&& c
< KEY_F(256))
265 sprintf(buf
, "<F%d>", c
- KEY_F0
);
267 snprintf(buf
, sizeof(buf
), "%c", (unsigned char) c
);
269 snprintf(buf
, sizeof(buf
), "\\x%hx", (unsigned short) c
);
273 static char *configfile_exists(void)
275 static char *config_file
;
278 if (!conf
.config_file_given
) {
280 char *home
= para_homedir();
281 config_file
= make_message("%s/.paraslash/gui.conf",
287 tmp
= conf
.config_file_arg
;
288 return file_exists(tmp
)? tmp
: NULL
;
291 /* Print given number of spaces to curses window. */
292 static void add_spaces(WINDOW
* win
, unsigned int num
)
295 unsigned sz
= sizeof(space
);
309 * print aligned string to curses window. This function always prints
312 static int align_str(WINDOW
* win
, char *str
, unsigned int len
,
315 int i
, num
; /* of spaces */
319 num
= len
- strlen(str
);
324 /* replace control characters by spaces */
325 for (i
= 0; i
< len
&& str
[i
]; i
++) {
326 if (str
[i
] == '\n' || str
[i
] == '\r' || str
[i
] == '\f')
331 add_spaces(win
, num
);
332 } else if (align
== RIGHT
) {
333 add_spaces(win
, num
);
336 add_spaces(win
, num
/ 2);
337 waddstr(win
, str
[0]? str
: "");
338 add_spaces(win
, num
- num
/ 2);
343 __printf_2_3
static void print_in_bar(int color
, const char *fmt
,...)
350 wattron(in
.win
, COLOR_PAIR(color
));
352 xvasprintf(&msg
, fmt
, ap
);
355 align_str(in
.win
, msg
, sb
.cols
, LEFT
);
361 * update the status bar
363 static void print_status_bar(void)
369 tmp
= para_strdup(STANDARD_STATUS_BAR
);
371 align_str(sb
.win
, tmp
, sb
.cols
, CENTER
);
377 * get the number of the oldest rbe that is (partially) visible. On return,
378 * lines contains the sum of the number of lines of all visable entries. If the
379 * first one is only partially visible, lines is greater than bot.lines.
381 static int first_visible_rbe(unsigned *lines
)
385 for (i
= scroll_position
; i
< RINGBUFFER_SIZE
; i
++) {
386 struct rb_entry
*rbe
= ringbuffer_get(bot_win_rb
, i
);
390 // fprintf(stderr, "found: %s\n", rbe->msg);
391 rbe_lines
= NUM_LINES(rbe
->len
);
392 if (rbe_lines
> bot
.lines
)
395 if (*lines
>= bot
.lines
)
398 return RINGBUFFER_SIZE
- 1;
401 static int draw_top_rbe(unsigned *lines
)
404 int offset
, fvr
= first_visible_rbe(lines
);
405 struct rb_entry
*rbe
;
409 wmove(bot
.win
, 0, 0);
410 rbe
= ringbuffer_get(bot_win_rb
, fvr
);
413 len
= strlen(rbe
->msg
);
414 if (*lines
> bot
.lines
) {
415 /* first rbe is only partially visible */
416 offset
= (*lines
- bot
.lines
) * bot
.cols
;
417 assert(offset
<= len
);
420 wattron(bot
.win
, COLOR_PAIR(rbe
->color
));
421 waddstr(bot
.win
, rbe
->msg
+ offset
);
422 *lines
= NUM_LINES(len
- offset
);
426 static void redraw_bot_win(void)
431 wmove(bot
.win
, 0, 0);
433 i
= draw_top_rbe(&lines
);
436 while (i
> 0 && lines
< bot
.lines
) {
437 struct rb_entry
*rbe
= ringbuffer_get(bot_win_rb
, --i
);
440 waddstr(bot
.win
, "\n");
443 lines
+= NUM_LINES(rbe
->len
);
444 wattron(bot
.win
, COLOR_PAIR(rbe
->color
));
445 waddstr(bot
.win
, "\n");
446 waddstr(bot
.win
, rbe
->msg
);
452 static void rb_add_entry(int color
, char *msg
)
454 struct rb_entry
*old
, *new = para_malloc(sizeof(struct rb_entry
));
457 new->len
= strlen(msg
);
459 old
= ringbuffer_add(bot_win_rb
, new);
460 // fprintf(stderr, "added: %s\n", new->msg);
465 if (scroll_position
) {
466 /* discard current scrolling, like xterm does */
471 wattron(bot
.win
, COLOR_PAIR(color
));
472 getyx(bot
.win
, y
, x
);
474 waddstr(bot
.win
, "\n");
475 waddstr(bot
.win
, msg
);
479 * print formated output to bot win and refresh
481 __printf_2_3
static void outputf(int color
, const char* fmt
,...)
489 xvasprintf(&msg
, fmt
, ap
);
491 rb_add_entry(color
, msg
);
495 static int add_output_line(char *line
, void *data
)
497 int color
= *(int *)data
? COLOR_ERRMSG
: COLOR_OUTPUT
;
500 rb_add_entry(color
, para_strdup(line
));
506 __printf_2_3
void curses_log(int ll
, const char *fmt
,...)
512 if (ll
< loglevel
|| !curses_active
)
521 color
= COLOR_ERRMSG
;
524 xvasprintf(&msg
, fmt
, ap
);
527 rb_add_entry(color
, msg
);
530 __printf_2_3
void (*para_log
)(int, const char*, ...) = curses_log
;
532 static void setup_signal_handling(void)
534 signal_pipe
= para_signal_init();
535 para_install_sighandler(SIGINT
);
536 para_install_sighandler(SIGTERM
);
537 para_install_sighandler(SIGCHLD
);
538 para_install_sighandler(SIGWINCH
);
539 para_install_sighandler(SIGUSR1
);
540 para_sigaction(SIGHUP
, SIG_IGN
);
543 /* kill every process in the process group and exit */
544 __noreturn
static void kill_pg_and_die(int ret
)
546 para_sigaction(SIGTERM
, SIG_IGN
);
551 static void shutdown_curses(void)
560 __noreturn
static void finish(int ret
)
563 kill_pg_and_die(ret
);
567 * exit curses and print given message to stdout/stderr
569 __noreturn __printf_2_3
static void msg_n_exit(int ret
, const char* fmt
, ...)
572 FILE *outfd
= ret
? stderr
: stdout
;
576 vfprintf(outfd
, fmt
, argp
);
578 kill_pg_and_die(ret
);
581 static void print_welcome(void)
583 if (loglevel
> LL_NOTICE
)
585 outputf(COLOR_WELCOME
, "Welcome to para_gui " PACKAGE_VERSION
586 " \"" CODENAME
"\". Theme: %s", theme
.name
);
593 static void init_wins(int top_lines
)
597 top
.lines
= top_lines
;
602 bot
.lines
= LINES
- top
.lines
- 3;
604 bot
.begy
= top
.lines
+ 1;
619 sep
.begy
= top
.lines
;
622 assume_default_colors(theme
.default_fg
, theme
.default_bg
);
624 mvwin(top
.win
, top
.begy
, top
.begx
);
625 wresize(top
.win
, top
.lines
, top
.cols
);
627 mvwin(sb
.win
, sb
.begy
, sb
.begx
);
628 wresize(sb
.win
, sb
.lines
, sb
.cols
);
630 mvwin(sep
.win
, sep
.begy
, sep
.begx
);
631 wresize(sep
.win
, sep
.lines
, sep
.cols
);
633 mvwin(bot
.win
, bot
.begy
, bot
.begx
);
634 wresize(bot
.win
, bot
.lines
, bot
.cols
);
636 mvwin(in
.win
, in
.begy
, in
.begx
);
637 wresize(in
.win
, in
.lines
, in
.cols
);
639 sep
.win
= newwin(sep
.lines
, sep
.cols
, sep
.begy
, sep
.begx
);
640 top
.win
= newwin(top
.lines
, top
.cols
, top
.begy
, top
.begx
);
641 bot
.win
= newwin(bot
.lines
, bot
.cols
, bot
.begy
, bot
.begx
);
642 sb
.win
= newwin(sb
.lines
, sb
.cols
, sb
.begy
, sb
.begx
);
643 in
.win
= newwin(in
.lines
, in
.cols
, in
.begy
, in
.begx
);
644 if (!top
.win
|| !bot
.win
|| !sb
.win
|| !in
.win
|| !sep
.win
)
645 msg_n_exit(1, "Error: Cannot create curses windows\n");
649 scrollok(bot
.win
, 1);
650 wattron(sb
.win
, COLOR_PAIR(COLOR_STATUSBAR
));
651 wattron(sep
.win
, COLOR_PAIR(COLOR_SEPARATOR
));
652 wattron(bot
.win
, COLOR_PAIR(COLOR_BOT
));
653 wattron(top
.win
, COLOR_PAIR(COLOR_TOP
));
665 wmove(sep
.win
, 0, 0);
666 for (i
= 1; i
<= COLS
; i
++)
667 waddstr(sep
.win
, theme
.sep_str
);
670 wnoutrefresh(top
.win
);
671 wnoutrefresh(bot
.win
);
672 //wnoutrefresh(sb.win);
674 wnoutrefresh(in
.win
);
675 wnoutrefresh(sep
.win
);
680 * Print stat item #i to curses window
682 static void print_stat_item(int i
)
685 struct stat_item_data d
= theme
.data
[i
];
686 char *c
= stat_content
[i
];
688 if (!curses_active
|| !d
.len
|| !c
)
690 tmp
= make_message("%s%s%s", d
.prefix
, c
, d
.postfix
);
691 wmove(top
.win
, d
.y
* top
.lines
/ 100, d
.x
* COLS
/ 100);
693 wattron(top
.win
, COLOR_PAIR(i
+ 1));
694 align_str(top
.win
, tmp
, d
.len
* COLS
/ 100, d
.align
);
699 static int update_item(int item_num
, char *buf
)
701 char **c
= stat_content
+ item_num
;
708 *c
= para_strdup("(artist tag not set)");
711 *c
= para_strdup("(title tag not set)");
714 *c
= para_strdup("????");
717 *c
= para_strdup("(album tag not set)");
720 *c
= para_strdup("(comment tag not set)");
724 *c
= para_strdup(buf
);
726 print_stat_item(item_num
);
730 static int read_stat_pipe(fd_set
*rfds
)
733 static int bufsize
, loaded
;
739 if (loaded
>= bufsize
) {
740 if (bufsize
> 1000 * 1000) {
744 bufsize
+= bufsize
+ 1000;
745 buf
= para_realloc(buf
, bufsize
);
747 assert(loaded
< bufsize
);
748 ret
= read_nonblock(stat_pipe
, buf
+ loaded
, bufsize
- loaded
,
751 ret2
= for_each_stat_item(buf
, loaded
, update_item
);
752 if (ret
< 0 || ret2
< 0) {
754 return ret2
< 0? ret2
: ret
;
756 sz
= ret2
; /* what is left */
757 if (sz
> 0 && sz
< loaded
)
758 memmove(buf
, buf
+ loaded
- sz
, sz
);
763 static void print_all_items(void)
769 FOR_EACH_STATUS_ITEM(i
)
773 static void clear_all_items(void)
777 FOR_EACH_STATUS_ITEM(i
) {
778 free(stat_content
[i
]);
779 stat_content
[i
] = para_strdup("");
783 static void init_pair_or_die(short pair
, short f
, short b
)
785 if (init_pair(pair
, f
, b
) == ERR
)
786 msg_n_exit(EXIT_FAILURE
, "fatal: init_pair() failed\n");
789 static void init_colors_or_die(void)
794 msg_n_exit(EXIT_FAILURE
, "fatal: No color term\n");
795 if (start_color() == ERR
)
796 msg_n_exit(EXIT_FAILURE
, "fatal: failed to start colors\n");
797 FOR_EACH_STATUS_ITEM(i
)
798 if (theme
.data
[i
].len
)
799 init_pair_or_die(i
+ 1, theme
.data
[i
].fg
,
801 init_pair_or_die(COLOR_STATUSBAR
, theme
.sb_fg
, theme
.sb_bg
);
802 init_pair_or_die(COLOR_COMMAND
, theme
.cmd_fg
, theme
.cmd_bg
);
803 init_pair_or_die(COLOR_OUTPUT
, theme
.output_fg
, theme
.output_bg
);
804 init_pair_or_die(COLOR_MSG
, theme
.msg_fg
, theme
.msg_bg
);
805 init_pair_or_die(COLOR_ERRMSG
, theme
.err_msg_fg
, theme
.err_msg_bg
);
806 init_pair_or_die(COLOR_WELCOME
, theme
.welcome_fg
, theme
.welcome_bg
);
807 init_pair_or_die(COLOR_SEPARATOR
, theme
.sep_fg
, theme
.sep_bg
);
808 init_pair_or_die(COLOR_TOP
, theme
.default_fg
, theme
.default_bg
);
809 init_pair_or_die(COLOR_BOT
, theme
.default_fg
, theme
.default_bg
);
812 /* (Re-)initialize the curses library. */
813 static void init_curses(void)
816 if (top
.win
&& refresh() == ERR
) /* refesh is really needed */
817 msg_n_exit(EXIT_FAILURE
, "refresh() failed\n");
818 if (LINES
< theme
.lines_min
|| COLS
< theme
.cols_min
)
819 msg_n_exit(EXIT_FAILURE
, "Error: Terminal (%dx%d) too small"
820 " (need at least %dx%d)\n", COLS
, LINES
,
821 theme
.cols_min
, theme
.lines_min
);
822 curs_set(0); /* make cursor invisible, ignore errors */
823 nonl(); /* do not NL->CR/NL on output, always returns OK */
824 /* don't echo input */
826 msg_n_exit(EXIT_FAILURE
, "fatal: noecho() failed\n");
827 /* take input chars one at a time, no wait for \n */
829 msg_n_exit(EXIT_FAILURE
, "fatal: cbreak() failed\n");
830 init_colors_or_die();
831 clear(); /* ignore non-fatal errors */
832 init_wins(theme
.top_lines_default
);
834 // noecho(); /* don't echo input */
837 static void check_sigchld(void)
842 ret
= para_reap_child(&pid
);
847 goto reap_next_child
;
851 * This sucker modifies its first argument. *handler and *arg are
852 * pointers to 0-terminated strings (inside line). Crap.
854 static int split_key_map(char *line
, char **handler
, char **arg
)
856 if (!(*handler
= strchr(line
+ 1, ':')))
860 if (!(*arg
= strchr(*handler
, ':')))
869 static int check_key_map_args(void)
873 char *tmp
= NULL
, *handler
, *arg
;
875 for (i
= 0; i
< conf
.key_map_given
; ++i
) {
876 s
= conf
.key_map_arg
[i
];
880 tmp
= para_strdup(s
);
881 if (!split_key_map(tmp
, &handler
, &arg
))
883 if (strlen(handler
) != 1)
892 if (find_cmd_byname(arg
) < 0)
902 * React to various signal-related events
904 static void handle_signal(int sig
)
908 msg_n_exit(EXIT_FAILURE
,
909 "only the good die young (caught SIGTERM))\n");
919 PARA_WARNING_LOG("caught SIGINT, reset");
920 /* Nothing to do. SIGINT killed our child which gets noticed
921 * by do_select and resets everything.
925 PARA_NOTICE_LOG("got SIGUSR1, rereading configuration");
934 static void status_pre_select(fd_set
*rfds
, int *max_fileno
, struct timeval
*tv
)
936 static struct timeval next_exec
, atm
, diff
;
937 int ret
, fds
[3] = {0, 1, 0};
942 /* Avoid busy loop */
943 gettimeofday(&atm
, NULL
);
944 if (tv_diff(&next_exec
, &atm
, &diff
) > 0) {
945 if (tv_diff(&diff
, tv
, NULL
) < 0)
949 next_exec
.tv_sec
= atm
.tv_sec
+ 2;
950 ret
= para_exec_cmdline_pid(&pid
, conf
.stat_cmd_arg
, fds
);
953 ret
= mark_fd_nonblocking(fds
[1]);
960 para_fd_set(stat_pipe
, rfds
, max_fileno
);
963 #define COMMAND_BUF_SIZE 32768
966 * This is the core select loop. Besides the (internal) signal
967 * pipe, the following other fds are checked according to the mode:
969 * GETCH_MODE: check stdin, return when key is pressed
971 * COMMAND_MODE: check command fds and stdin. Return when peer has closed both
972 * stdout and stderr or when any key is pressed.
974 * EXTERNAL_MODE: Check only signal pipe. Used when an external command
975 * is running. During that time curses is disabled. Returns when
978 static int do_select(int mode
)
981 int ret
, i
, max_fileno
;
982 char command_buf
[2][COMMAND_BUF_SIZE
] = {"", ""};
983 int cbo
[2] = {0, 0}; /* command buf offsets */
987 tv
.tv_sec
= conf
.timeout_arg
/ 1000;
988 tv
.tv_usec
= (conf
.timeout_arg
% 1000) * 1000;
989 // ret = refresh_status();
992 status_pre_select(&rfds
, &max_fileno
, &tv
);
994 para_fd_set(signal_pipe
, &rfds
, &max_fileno
);
995 /* command pipe only for COMMAND_MODE */
996 if (mode
== COMMAND_MODE
) {
997 if (command_fds
[0] >= 0)
998 para_fd_set(command_fds
[0], &rfds
, &max_fileno
);
999 if (command_fds
[1] >= 0)
1000 para_fd_set(command_fds
[1], &rfds
, &max_fileno
);
1002 if (mode
== GETCH_MODE
|| mode
== COMMAND_MODE
)
1003 para_fd_set(STDIN_FILENO
, &rfds
, &max_fileno
);
1004 ret
= para_select(max_fileno
+ 1, &rfds
, NULL
, &tv
);
1006 goto check_return
; /* skip fd checks */
1008 ret
= para_next_signal(&rfds
);
1011 /* read command pipe if ready */
1012 if (mode
== COMMAND_MODE
) {
1013 for (i
= 0; i
< 2; i
++) {
1015 if (command_fds
[i
] < 0)
1017 ret
= read_nonblock(command_fds
[i
],
1018 command_buf
[i
] + cbo
[i
],
1019 COMMAND_BUF_SIZE
- 1 - cbo
[i
], &rfds
, &sz
);
1022 cbo
[i
] = for_each_line(command_buf
[i
], cbo
[i
],
1023 add_output_line
, &i
);
1027 PARA_NOTICE_LOG("closing command fd %d: %s",
1028 i
, para_strerror(-ret
));
1029 close(command_fds
[i
]);
1030 command_fds
[i
] = -1;
1031 if (command_fds
[!i
] < 0) /* both fds closed */
1034 if (cbo
[i
] == COMMAND_BUF_SIZE
- 1) {
1035 PARA_NOTICE_LOG("discarding overlong line");
1040 ret
= read_stat_pipe(&rfds
);
1042 PARA_NOTICE_LOG("closing stat pipe: %s\n", para_strerror(-ret
));
1046 free(stat_content
[SI_BASENAME
]);
1047 stat_content
[SI_BASENAME
] =
1048 para_strdup("stat command terminated!?");
1054 ret
= wgetch(top
.win
);
1055 if (ret
!= ERR
&& ret
!= KEY_RESIZE
) {
1056 if (command_fds
[0] >= 0) {
1057 close(command_fds
[0]);
1058 command_fds
[0] = -1;
1060 if (command_fds
[1] >= 0) {
1061 close(command_fds
[1]);
1062 command_fds
[1] = -1;
1065 kill(cmd_pid
, SIGTERM
);
1070 ret
= wgetch(top
.win
);
1071 if (ret
!= ERR
&& ret
!= KEY_RESIZE
)
1082 * read from command pipe and print data to bot window
1084 static void send_output(void)
1088 ret
= mark_fd_nonblocking(command_fds
[0]);
1091 ret
= mark_fd_nonblocking(command_fds
[1]);
1094 if (do_select(COMMAND_MODE
) >= 0)
1095 PARA_INFO_LOG("command complete");
1097 PARA_NOTICE_LOG("command aborted");
1098 print_in_bar(COLOR_MSG
, " ");
1101 PARA_ERROR_LOG("%s\n", para_strerror(-ret
));
1102 close(command_fds
[0]);
1103 close(command_fds
[1]);
1106 static void para_cmd(char *cmd
)
1108 int ret
, fds
[3] = {0, 1, 1};
1109 char *c
= make_message(BINDIR
"/para_client -- %s", cmd
);
1111 outputf(COLOR_COMMAND
, "%s", c
);
1112 print_in_bar(COLOR_MSG
, "executing client command, hit any key to abort\n");
1113 ret
= para_exec_cmdline_pid(&cmd_pid
, c
, fds
);
1117 command_fds
[0] = fds
[1];
1118 command_fds
[1] = fds
[2];
1123 * exec command and print output to bot win
1125 static void display_cmd(char *cmd
)
1127 int fds
[3] = {0, 1, 1};
1129 print_in_bar(COLOR_MSG
, "executing display command, hit any key to abort");
1130 outputf(COLOR_COMMAND
, "%s", cmd
);
1131 if (para_exec_cmdline_pid(&cmd_pid
, cmd
, fds
) < 0)
1133 command_fds
[0] = fds
[1];
1134 command_fds
[1] = fds
[2];
1139 * shutdown curses and stat pipe before executing external commands
1141 static void external_cmd(char *cmd
)
1143 int fds
[3] = {-1, -1, -1};
1148 if (para_exec_cmdline_pid(&cmd_pid
, cmd
, fds
) < 0)
1150 do_select(EXTERNAL_MODE
);
1154 static void print_scroll_msg(void)
1156 unsigned lines_total
, filled
= ringbuffer_filled(bot_win_rb
);
1157 int first_rbe
= first_visible_rbe(&lines_total
);
1158 print_in_bar(COLOR_MSG
, "scrolled view: %d-%d/%d\n", filled
- first_rbe
,
1159 filled
- scroll_position
, ringbuffer_filled(bot_win_rb
));
1162 static void com_scroll_top(void)
1164 int i
= RINGBUFFER_SIZE
- 1;
1167 while (i
> 0 && !ringbuffer_get(bot_win_rb
, i
))
1169 /* i is oldest entry */
1170 for (; lines
< bot
.lines
&& i
>= 0; i
--) {
1171 struct rb_entry
*rbe
= ringbuffer_get(bot_win_rb
, i
);
1174 lines
+= NUM_LINES(strlen(rbe
->msg
));
1177 if (lines
> 0 && scroll_position
!= i
) {
1178 scroll_position
= i
;
1183 print_in_bar(COLOR_ERRMSG
, "top of buffer is shown\n");
1186 static void com_cancel_scrolling(void)
1189 if (scroll_position
== 0) {
1190 print_in_bar(COLOR_ERRMSG
, "bottom of buffer is shown\n");
1193 scroll_position
= 0;
1197 static void com_page_down(void)
1200 int i
= scroll_position
;
1201 while (lines
< bot
.lines
&& --i
> 0) {
1202 struct rb_entry
*rbe
= ringbuffer_get(bot_win_rb
, i
);
1205 lines
+= NUM_LINES(strlen(rbe
->msg
));
1208 scroll_position
= i
;
1213 print_in_bar(COLOR_ERRMSG
, "bottom of buffer is shown\n");
1216 static void com_page_up(void)
1219 int fvr
= first_visible_rbe(&lines
);
1221 if (fvr
< 0 || fvr
+ 1 >= ringbuffer_filled(bot_win_rb
)) {
1222 print_in_bar(COLOR_ERRMSG
, "top of buffer is shown\n");
1225 scroll_position
= fvr
+ 1;
1226 for (; scroll_position
> 0; scroll_position
--) {
1227 first_visible_rbe(&lines
);
1228 if (lines
== bot
.lines
)
1235 static void com_scroll_down(void)
1237 struct rb_entry
*rbe
;
1240 if (!scroll_position
) {
1241 print_in_bar(COLOR_ERRMSG
, "bottom of buffer is shown\n");
1245 rbe
= ringbuffer_get(bot_win_rb
, scroll_position
);
1246 rbe_lines
= NUM_LINES(rbe
->len
);
1247 wscrl(bot
.win
, rbe_lines
);
1248 wmove(bot
.win
, bot
.lines
- rbe_lines
, 0);
1249 wattron(bot
.win
, COLOR_PAIR(rbe
->color
));
1250 waddstr(bot
.win
, rbe
->msg
);
1255 static void com_scroll_up(void)
1257 struct rb_entry
*rbe
= NULL
;
1259 int i
, first_rbe
, num_scroll
;
1261 /* the entry that is going to vanish */
1262 rbe
= ringbuffer_get(bot_win_rb
, scroll_position
);
1265 num_scroll
= NUM_LINES(rbe
->len
);
1266 first_rbe
= first_visible_rbe(&lines
);
1267 if (first_rbe
< 0 || (first_rbe
== ringbuffer_filled(bot_win_rb
) - 1))
1270 wscrl(bot
.win
, -num_scroll
);
1271 i
= draw_top_rbe(&lines
);
1274 while (i
> 0 && lines
< num_scroll
) {
1276 rbe
= ringbuffer_get(bot_win_rb
, --i
);
1279 rbe_lines
= NUM_LINES(rbe
->len
);
1281 // fprintf(stderr, "msg: %s\n", rbe->msg);
1282 wattron(bot
.win
, COLOR_PAIR(rbe
->color
));
1283 waddstr(bot
.win
, "\n");
1284 waddstr(bot
.win
, rbe
->msg
);
1293 print_in_bar(COLOR_ERRMSG
, "top of buffer is shown\n");
1296 static void com_ll_decr(void)
1298 if (loglevel
<= LL_DEBUG
) {
1299 print_in_bar(COLOR_ERRMSG
,
1300 "loglevel already at maximal verbosity\n");
1304 print_in_bar(COLOR_MSG
, "loglevel set to %d\n", loglevel
);
1307 static void com_ll_incr(void)
1309 if (loglevel
>= LL_EMERG
) {
1310 print_in_bar(COLOR_ERRMSG
,
1311 "loglevel already at minimal verbosity\n");
1315 print_in_bar(COLOR_MSG
, "loglevel set to %d\n", loglevel
);
1319 * reread configuration, terminate on errors
1321 static void com_reread_conf(void)
1323 char *cf
=configfile_exists();
1324 struct gui_cmdline_parser_params params
= {
1327 .check_required
= 0,
1328 .check_ambiguity
= 0,
1333 PARA_WARNING_LOG("there is no configuration to read");
1336 PARA_INFO_LOG("rereading command line options and config file");
1337 gui_cmdline_parser_ext(_argc
, _argv
, &conf
, ¶ms
);
1338 if (gui_cmdline_parser_config_file(cf
, &conf
, ¶ms
) != 0) {
1339 PARA_EMERG_LOG("errors in config file");
1340 finish(EXIT_FAILURE
);
1342 PARA_NOTICE_LOG("config file reloaded");
1343 if (check_key_map_args() < 0)
1344 finish(EXIT_FAILURE
);
1347 static void com_help(void)
1351 for (i
= 0; i
< conf
.key_map_given
; ++i
) {
1352 char *handler
, *arg
, *tmp
= para_strdup(conf
.key_map_arg
[i
]);
1353 const char *handler_text
= "???", *desc
= NULL
;
1355 if (!split_key_map(tmp
, &handler
, &arg
)) {
1361 handler_text
= "internal";
1362 desc
= command_list
[find_cmd_byname(arg
)].description
;
1364 case 'x': handler_text
= "external"; break;
1365 case 'd': handler_text
= "display "; break;
1366 case 'p': handler_text
= "para "; break;
1368 outputf(COLOR_MSG
, "%s\t%s\t%s%s\t%s", tmp
, handler_text
, arg
,
1369 strlen(arg
) < 8? "\t" : "",
1373 for (i
= 0; command_list
[i
].handler
; i
++) {
1374 struct gui_command gc
= command_list
[i
];
1376 outputf(COLOR_MSG
, "%s\tinternal\t%s\t%s%s", gc
.key
, gc
.name
,
1377 strlen(gc
.name
) < 8? "\t" : "",
1380 print_in_bar(COLOR_MSG
, "try \"para_gui -h\" or \"para_client help\" "
1384 static void com_shrink_top_win(void)
1386 if (top
.lines
<= theme
.top_lines_min
) {
1387 PARA_WARNING_LOG("can not decrease top window");
1390 init_wins(top
.lines
- 1);
1393 print_in_bar(COLOR_MSG
, "%s", "decreased top window");
1396 static void com_enlarge_top_win(void)
1398 if (bot
.lines
< 3) {
1399 PARA_WARNING_LOG("can not increase top window");
1402 init_wins(top
.lines
+ 1);
1405 print_in_bar(COLOR_MSG
, "increased top window");
1408 static void com_version(void)
1410 print_in_bar(COLOR_MSG
, "para_gui " PACKAGE_VERSION
" \""
1414 __noreturn
static void com_quit(void)
1419 static void com_refresh(void)
1425 static void change_theme(int next
)
1431 /* This seems to be needed twice, why? */
1434 PARA_NOTICE_LOG("new theme: %s", theme
.name
);
1437 static void com_next_theme(void)
1442 static void com_prev_theme(void)
1448 static void handle_command(int c
)
1452 /* first check user's key bindings */
1453 for (i
= 0; i
< conf
.key_map_given
; ++i
) {
1454 char *tmp
, *handler
, *arg
;
1456 tmp
= para_strdup(conf
.key_map_arg
[i
]);
1457 if (!split_key_map(tmp
, &handler
, &arg
)) {
1461 if (strcmp(tmp
, km_keyname(c
))) {
1465 if (*handler
== 'd')
1467 else if (*handler
== 'x')
1469 else if (*handler
== 'p')
1471 else if (*handler
== 'i') {
1472 int num
= find_cmd_byname(arg
);
1474 command_list
[num
].handler();
1479 /* not found, check internal key bindings */
1480 for (i
= 0; command_list
[i
].handler
; i
++) {
1481 if (!strcmp(km_keyname(c
), command_list
[i
].key
)) {
1482 command_list
[i
].handler();
1486 print_in_bar(COLOR_ERRMSG
, "key '%s' is not bound, press ? for help",
1490 int main(int argc
, char *argv
[])
1498 gui_cmdline_parser(argc
, argv
, &conf
); /* exits on errors */
1499 HANDLE_VERSION_FLAG("gui", conf
);
1500 cf
= configfile_exists();
1501 if (!cf
&& conf
.config_file_given
) {
1502 fprintf(stderr
, "can not read config file %s\n",
1503 conf
.config_file_arg
);
1507 struct gui_cmdline_parser_params params
= {
1510 .check_required
= 0,
1511 .check_ambiguity
= 0,
1514 if (gui_cmdline_parser_config_file(cf
, &conf
, ¶ms
) != 0)
1517 loglevel
= get_loglevel_by_name(conf
.loglevel_arg
);
1518 if (check_key_map_args() < 0) {
1519 fprintf(stderr
, "invalid key map\n");
1522 init_theme_or_die(conf
.theme_arg
, &theme
);
1523 top
.lines
= theme
.top_lines_default
;
1524 setup_signal_handling();
1525 bot_win_rb
= ringbuffer_new(RINGBUFFER_SIZE
);
1526 initscr(); /* needed only once, always successful */
1531 ret
= do_select(GETCH_MODE
);
1534 print_in_bar(COLOR_MSG
, " ");
1535 handle_command(ret
);