f884e8643140fcb0145bb0a7a193a36618e96b9e
[paraslash.git] / gui.c
1 /*
2 * Copyright (C) 1998-2007 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file gui.c ncurses-based interface for paraslash */
8
9 #include <signal.h>
10 #include <sys/types.h>
11 #include <dirent.h>
12 #include "gui.cmdline.h"
13 #include "para.h"
14 #include "gui.h"
15 #include <curses.h>
16 #include "ringbuffer.h"
17 #include "gui_common.h"
18 #include "fd.h"
19 #include "error.h"
20 #include "list.h"
21 #include "sched.h"
22 #include "signal.h"
23
24 /** define the array of error lists needed by para_gui */
25 INIT_GUI_ERRLISTS;
26 extern const char *status_item_list[NUM_STAT_ITEMS];
27 static char *stat_content[NUM_STAT_ITEMS];
28
29 #define STANDARD_STATUS_BAR "para_gui " PACKAGE_VERSION " (hit ? for help)"
30
31 static int signal_pipe;
32
33 static void finish(int sig);
34
35 static struct win_data {
36 WINDOW *win;
37 NCURSES_SIZE_T begx;
38 NCURSES_SIZE_T begy;
39 NCURSES_SIZE_T cols;
40 NCURSES_SIZE_T lines;
41 } top, bot, sb, in, sep;
42
43 #define RINGBUFFER_SIZE 512
44 struct rb_entry {
45 char *msg;
46 size_t len;
47 int color;
48 };
49 void *bot_win_rb;
50 #define NUM_LINES(len) (1 + (len) / bot.cols)
51
52 static unsigned scroll_position;
53
54 static int cmd_died, curses_active;
55 static pid_t cmd_pid;
56
57 static int command_pipe = -1;
58 static int audiod_pipe = -1;
59 static struct gui_args_info conf;
60
61 enum {GETCH_MODE, COMMAND_MODE, EXTERNAL_MODE};
62
63
64 #define COLOR_STATUSBAR 32
65 #define COLOR_COMMAND 33
66 #define COLOR_OUTPUT 34
67 #define COLOR_MSG 35
68 #define COLOR_ERRMSG 36
69 #define COLOR_WELCOME 37
70 #define COLOR_SEPARATOR 38
71 #define COLOR_TOP 39
72 #define COLOR_BOT 40
73
74 struct gui_command {
75 const char *key;
76 const char *name;
77 const char *description;
78 void (*handler)(void);
79 };
80
81 struct stat_item {
82 char name[MAXLINE];
83 char prefix[MAXLINE];
84 char postfix[MAXLINE];
85 unsigned y;
86 unsigned x;
87 unsigned len;
88 int fg, bg;
89 int align;
90 char content[MAXLINE];
91 };
92
93 static struct gui_theme theme;
94
95 int _argc;
96 char **_argv;
97
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 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
114 struct gui_command command_list[] = {
115 {
116 .key = "?",
117 .name = "help",
118 .description = "print help",
119 .handler = com_help
120 }, {
121 .key = "+",
122 .name = "enlarge_win",
123 .description = "enlarge the top window",
124 .handler = com_enlarge_top_win
125 }, {
126 .key = "-",
127 .name = "shrink_win",
128 .description = "shrink the top window",
129 .handler = com_shrink_top_win
130 }, {
131 .key = "r",
132 .name = "reread_conf",
133 .description = "reread configuration file",
134 .handler = com_reread_conf
135 }, {
136 .key = "q",
137 .name = "quit",
138 .description = "exit para_gui",
139 .handler = com_quit
140 }, {
141 .key = "^L",
142 .name = "refresh",
143 .description = "redraw the screen",
144 .handler = com_refresh
145 }, {
146 .key = ".",
147 .name = "next_theme",
148 .description = "switch to next theme",
149 .handler = com_next_theme
150 }, {
151 .key = ",",
152 .name = "prev_theme",
153 .description = "switch to previous stream",
154 .handler = com_prev_theme
155 }, {
156 .key = ">",
157 .name = "ll_incr",
158 .description = "increase loglevel (decreases verbosity)",
159 .handler = com_ll_incr
160 }, {
161 .key = "<",
162 .name = "ll_decr",
163 .description = "decrease loglevel (increases verbosity)",
164 .handler = com_ll_decr
165 }, {
166 .key = "V",
167 .name = "version",
168 .description = "show the para_gui version",
169 .handler = com_version
170 }, {
171 .key = "<up>",
172 .name = "scroll_up",
173 .description = "scroll up one line",
174 .handler = com_scroll_up
175 }, {
176 .key = "<down>",
177 .name = "scroll_down",
178 .description = "scroll down one line",
179 .handler = com_scroll_down
180 }, {
181 .key = "<ppage>",
182 .name = "page_up",
183 .description = "scroll up one page",
184 .handler = com_page_up
185 }, {
186 .key = "<npage>",
187 .name = "page_down",
188 .description = "scroll down one page",
189 .handler = com_page_down
190 }, {
191 .handler = NULL
192 }
193 };
194
195 static int find_cmd_byname(char *name)
196 {
197 int i;
198
199 for (i = 0; command_list[i].handler; i++)
200 if (!strcmp(command_list[i].name, name))
201 return i;
202 return -1;
203 }
204
205 /* taken from mutt */
206 static char *km_keyname(int c)
207 {
208 static char buf[10];
209
210 if (c == KEY_UP) {
211 sprintf(buf, "<up>");
212 return buf;
213 }
214 if (c == KEY_DOWN) {
215 sprintf(buf, "<down>");
216 return buf;
217 }
218 if (c == KEY_LEFT) {
219 sprintf(buf, "<left>");
220 return buf;
221 }
222 if (c == KEY_RIGHT) {
223 sprintf(buf, "<right>");
224 return buf;
225 }
226 if (c == KEY_NPAGE) {
227 sprintf(buf, "<npage>");
228 return buf;
229 }
230 if (c == KEY_PPAGE) {
231 sprintf(buf, "<ppage>");
232 return buf;
233 }
234 if (c < 256 && c > -128 && iscntrl((unsigned char) c)) {
235 if (c < 0)
236 c += 256;
237 if (c < 128) {
238 buf[0] = '^';
239 buf[1] = (c + '@') & 0x7f;
240 buf[2] = 0;
241 } else
242 snprintf(buf, sizeof(buf), "\\%d%d%d", c >> 6,
243 (c >> 3) & 7, c & 7);
244 } else if (c >= KEY_F0 && c < KEY_F(256))
245 sprintf(buf, "<F%d>", c - KEY_F0);
246 else if (isprint(c))
247 snprintf(buf, sizeof(buf), "%c", (unsigned char) c);
248 else
249 snprintf(buf, sizeof(buf), "\\x%hx", (unsigned short) c);
250 return buf;
251 }
252
253 static char *configfile_exists(void)
254 {
255 static char *config_file;
256 char *tmp;
257
258 if (!conf.config_file_given) {
259 if (!config_file) {
260 char *home = para_homedir();
261 config_file = make_message("%s/.paraslash/gui.conf",
262 home);
263 free(home);
264 }
265 tmp = config_file;
266 } else
267 tmp = conf.config_file_arg;
268 return file_exists(tmp)? tmp: NULL;
269 }
270
271 /*
272 * print num spaces to curses window
273 */
274 static void add_spaces(WINDOW* win, unsigned int num)
275 {
276 while (num > 0) {
277 num--;
278 waddstr(win, " ");
279 }
280 }
281
282 /*
283 * print aligned string to curses window. This function always prints
284 * exactly len chars.
285 */
286 static int align_str(WINDOW* win, const char *string, unsigned int len,
287 unsigned int align)
288 {
289 int num; /* of spaces */
290 char *str;
291
292 if (!win || !string)
293 return -1;
294 num = len - strlen(string);
295 str = para_strdup(string);
296 if (num < 0) {
297 str[len] = '\0';
298 num = 0;
299 }
300 if (align == LEFT) {
301 waddstr(win, str);
302 add_spaces(win, num);
303 } else if (align == RIGHT) {
304 add_spaces(win, num);
305 waddstr(win, str);
306 } else {
307 add_spaces(win, num / 2);
308 waddstr(win, str[0]? str: "");
309 add_spaces(win, num - num / 2);
310 }
311 free(str);
312 return 1;
313 }
314
315 __printf_2_3 static void print_in_bar(int color, const char *fmt,...)
316 {
317 char *msg;
318
319 if (!curses_active)
320 return;
321 wattron(in.win, COLOR_PAIR(color));
322 PARA_VSPRINTF(fmt, msg);
323 wmove(in.win, 0, 0);
324 align_str(in.win, msg, sb.cols, LEFT);
325 free(msg);
326 wrefresh(in.win);
327 }
328
329 /*
330 * update the status bar
331 */
332 static void print_status_bar(void)
333 {
334 if (!curses_active)
335 return;
336 wmove(sb.win, 0, 0);
337 align_str(sb.win, STANDARD_STATUS_BAR, sb.cols, CENTER);
338 wrefresh(sb.win);
339 }
340
341 /*
342 * get the number of the oldest rbe that is (partially) visible. On return,
343 * lines contains the sum of the number of lines of all visable entries. If the
344 * first one is only partially visible, lines is greater than bot.lines.
345 */
346 static int first_visible_rbe(unsigned *lines)
347 {
348 int i;
349 *lines = 0;
350 for (i = scroll_position; i < RINGBUFFER_SIZE; i++) {
351 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
352 int rbe_lines;
353 if (!rbe)
354 return i - 1;
355 // fprintf(stderr, "found: %s\n", rbe->msg);
356 rbe_lines = NUM_LINES(rbe->len);
357 if (rbe_lines > bot.lines)
358 return -1;
359 *lines += rbe_lines;
360 if (*lines >= bot.lines)
361 return i;
362 }
363 return RINGBUFFER_SIZE - 1;
364 }
365
366 static int draw_top_rbe(unsigned *lines)
367 {
368 unsigned len;
369 int offset, fvr = first_visible_rbe(lines);
370 struct rb_entry *rbe;
371
372 if (fvr < 0)
373 return -1;
374 wmove(bot.win, 0, 0);
375 rbe = ringbuffer_get(bot_win_rb, fvr);
376 if (!rbe)
377 return -1;
378 /* first rbe might be only partially visible */
379 offset = (*lines - bot.lines) * bot.cols;
380 len = strlen(rbe->msg);
381 if (offset < 0 || len < offset)
382 return -1;
383 wattron(bot.win, COLOR_PAIR(rbe->color));
384 waddstr(bot.win, rbe->msg + offset);
385 *lines = NUM_LINES(len - offset);
386 return fvr;
387 }
388
389 static void redraw_bot_win(void)
390 {
391 unsigned lines;
392 int i;
393
394 wmove(bot.win, 0, 0);
395 wclear(bot.win);
396 i = draw_top_rbe(&lines);
397 if (i <= 0)
398 goto out;
399 while (i > 0 && lines < bot.lines) {
400 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, --i);
401 if (!rbe) {
402 lines++;
403 waddstr(bot.win, "\n");
404 continue;
405 }
406 lines += NUM_LINES(rbe->len);
407 wattron(bot.win, COLOR_PAIR(rbe->color));
408 waddstr(bot.win, "\n");
409 waddstr(bot.win, rbe->msg);
410 }
411 out:
412 wrefresh(bot.win);
413 }
414
415 static void rb_add_entry(int color, char *msg)
416 {
417 struct rb_entry *old, *new = para_malloc(sizeof(struct rb_entry));
418 int x, y;
419 new->color = color;
420 new->len = strlen(msg);
421 new->msg = msg;
422 old = ringbuffer_add(bot_win_rb, new);
423 // fprintf(stderr, "added: %s\n", new->msg);
424 if (old) {
425 free(old->msg);
426 free(old);
427 }
428 if (scroll_position) {
429 /* discard current scrolling, like xterm does */
430 scroll_position = 0;
431 redraw_bot_win();
432 return;
433 }
434 wattron(bot.win, COLOR_PAIR(color));
435 getyx(bot.win, y, x);
436 if (y || x)
437 waddstr(bot.win, "\n");
438 waddstr(bot.win, msg);
439 }
440
441 /*
442 * print formated output to bot win and refresh
443 */
444 __printf_2_3 static void outputf(int color, const char* fmt,...)
445 {
446 char *msg;
447
448 if (!curses_active)
449 return;
450 PARA_VSPRINTF(fmt, msg);
451 rb_add_entry(color, msg);
452 wrefresh(bot.win);
453 }
454
455 static int add_output_line(char *line, __a_unused void *data)
456 {
457 if (!curses_active)
458 return 1;
459 rb_add_entry(COLOR_OUTPUT, para_strdup(line));
460 return 1;
461 }
462
463 void para_log(int ll, const char *fmt,...)
464 {
465 int color;
466 char *msg;
467
468 if (ll < conf.loglevel_arg || !curses_active)
469 return;
470 switch (ll) {
471 case DEBUG:
472 case INFO:
473 case NOTICE:
474 color = COLOR_MSG;
475 break;
476 default:
477 color = COLOR_ERRMSG;
478 }
479 PARA_VSPRINTF(fmt, msg);
480 chop(msg);
481 rb_add_entry(color, msg);
482 wrefresh(bot.win);
483 }
484
485 static void setup_signal_handling(void)
486 {
487 signal_pipe = para_signal_init();
488 para_install_sighandler(SIGINT);
489 para_install_sighandler(SIGTERM);
490 para_install_sighandler(SIGCHLD);
491 para_install_sighandler(SIGWINCH);
492 para_install_sighandler(SIGUSR1);
493 signal(SIGPIPE, SIG_IGN);
494 signal(SIGHUP, SIG_IGN);
495 }
496
497 static void do_exit(int ret)
498 {
499 signal(SIGTERM, SIG_IGN);
500 kill(0, SIGTERM);
501 exit(ret);
502 }
503
504 static void shutdown_curses(void)
505 {
506 if (!curses_active)
507 return;
508 def_prog_mode();
509 curses_active = 0;
510 endwin();
511 }
512
513 static void finish(int ret)
514 {
515 shutdown_curses();
516 do_exit(ret);
517 }
518
519 /*
520 * exit curses and print given message to stdout/stderr
521 */
522 __printf_2_3 static void msg_n_exit(int ret, const char* fmt, ...)
523 {
524 va_list argp;
525 FILE *outfd = ret? stderr: stdout;
526
527 shutdown_curses();
528 va_start(argp, fmt);
529 vfprintf(outfd, fmt, argp);
530 va_end(argp);
531 do_exit(ret);
532 }
533
534 static void print_welcome(void)
535 {
536 int ll = conf.loglevel_arg;
537 if (ll > NOTICE)
538 return;
539 outputf(COLOR_WELCOME, "Welcome to para_gui " PACKAGE_VERSION
540 " \"" CODENAME "\". Theme: %s", theme.name);
541 wclrtoeol(bot.win);
542 }
543
544 /*
545 * init all windows
546 */
547 static void init_wins(int top_lines)
548 {
549 int i;
550
551 top.lines = top_lines;
552 top.cols = COLS;
553 top.begy = 0;
554 top.begx = 0;
555
556 bot.lines = LINES - top.lines - 3;
557 bot.cols = COLS;
558 bot.begy = top.lines + 1;
559 bot.begx = 0;
560
561 sb.lines = 1;
562 sb.cols = COLS;
563 sb.begy = LINES - 2;
564 sb.begx = 0;
565
566 in.lines = 1;
567 in.cols = COLS;
568 in.begy = LINES - 1;
569 in.begx = 0;
570
571 sep.lines = 1;
572 sep.cols = COLS;
573 sep.begy = top.lines;
574 sep.begx = 0;
575
576 assume_default_colors(theme.default_fg, theme.default_bg);
577 if (top.win) {
578 mvwin(top.win, top.begy, top.begx);
579 wresize(top.win, top.lines, top.cols);
580
581 mvwin(sb.win, sb.begy, sb.begx);
582 wresize(sb.win, sb.lines, sb.cols);
583
584 mvwin(sep.win, sep.begy, sep.begx);
585 wresize(sep.win, sep.lines, sep.cols);
586
587 mvwin(bot.win, bot.begy, bot.begx);
588 wresize(bot.win, bot.lines, bot.cols);
589
590 mvwin(in.win, in.begy, in.begx);
591 wresize(in.win, in.lines, in.cols);
592 } else {
593 sep.win = newwin(sep.lines, sep.cols, sep.begy, sep.begx);
594 top.win = newwin(top.lines, top.cols, top.begy, top.begx);
595 bot.win = newwin(bot.lines, bot.cols, bot.begy, bot.begx);
596 sb.win = newwin(sb.lines, sb.cols, sb.begy, sb.begx);
597 in.win = newwin(in.lines, in.cols, in.begy, in.begx);
598 if (!top.win || !bot.win || !sb.win || !in.win || !sep.win)
599 msg_n_exit(1, "Error: Cannot create curses windows\n");
600 wclear(bot.win);
601 wclear(sb.win);
602 wclear(in.win);
603 scrollok(bot.win, 1);
604 wattron(sb.win, COLOR_PAIR(COLOR_STATUSBAR));
605 wattron(sep.win, COLOR_PAIR(COLOR_SEPARATOR));
606 wattron(bot.win, COLOR_PAIR(COLOR_BOT));
607 wattron(top.win, COLOR_PAIR(COLOR_TOP));
608 nodelay(top.win, 1);
609 nodelay(bot.win, 1);
610 nodelay(sb.win, 1);
611 nodelay(in.win, 0);
612
613 keypad(top.win, 1);
614 keypad(bot.win, 1);
615 keypad(sb.win, 1);
616 keypad(in.win, 1);
617 print_status_bar();
618 }
619 wmove(sep.win, 0, 0);
620 for (i = 1; i <= COLS; i++)
621 waddstr(sep.win, theme.sep_str);
622 wclear(top.win);
623 //wclear(bot.win);
624 wnoutrefresh(top.win);
625 wnoutrefresh(bot.win);
626 //wnoutrefresh(sb.win);
627 print_status_bar();
628 wnoutrefresh(in.win);
629 wnoutrefresh(sep.win);
630 doupdate();
631 }
632
633 static void check_geometry(void)
634 {
635 if (LINES < theme.lines_min || COLS < theme.cols_min)
636 msg_n_exit(EXIT_FAILURE, "Error: Terminal (%dx%d) too small"
637 " (need at least %dx%d)\n", COLS, LINES,
638 theme.cols_min, theme.lines_min);
639 }
640
641 /*
642 * Print stat item #i to curses window
643 */
644 static void print_stat_item(int i)
645 {
646 char *tmp;
647 struct stat_item_data d = theme.data[i];
648 char *c = stat_content[i];
649
650 if (!curses_active || !d.len || !c)
651 return;
652 tmp = make_message("%s%s%s", d.prefix, c, d.postfix);
653 // PARA_DEBUG_LOG("%s: read: %s\n", __func__, tmp);
654 wmove(top.win, d.y * top.lines / 100, d.x * COLS / 100);
655 wrefresh(top.win);
656 wattron(top.win, COLOR_PAIR(i + 1));
657 align_str(top.win, tmp, d.len * COLS / 100, d.align);
658 free(tmp);
659 wrefresh(top.win);
660 }
661
662 static void print_all_items(void)
663 {
664 int i;
665
666 if (!curses_active)
667 return;
668 for (i = 0; i < NUM_STAT_ITEMS; i++)
669 print_stat_item(i);
670 }
671 static void clear_all_items(void)
672 {
673 int i;
674
675 for (i = 0; i < NUM_STAT_ITEMS; i++) {
676 free(stat_content[i]);
677 stat_content[i] = para_strdup("");
678 }
679 }
680
681 static void init_colors(void)
682 {
683 int i;
684 if (!has_colors())
685 msg_n_exit(EXIT_FAILURE, "Error: No color term\n");
686 start_color();
687 for (i = 0; i < NUM_STAT_ITEMS; i++)
688 if (theme.data[i].len)
689 init_pair(i + 1, theme.data[i].fg, theme.data[i].bg);
690 init_pair(COLOR_STATUSBAR, theme.sb_fg, theme.sb_bg);
691 init_pair(COLOR_COMMAND, theme.cmd_fg, theme.cmd_bg);
692 init_pair(COLOR_OUTPUT, theme.output_fg, theme.output_bg);
693 init_pair(COLOR_MSG, theme.msg_fg, theme.msg_bg);
694 init_pair(COLOR_ERRMSG, theme.err_msg_fg, theme.err_msg_bg);
695 init_pair(COLOR_WELCOME, theme.welcome_fg, theme.welcome_bg);
696 init_pair(COLOR_SEPARATOR, theme.sep_fg, theme.sep_bg);
697 init_pair(COLOR_TOP, theme.default_fg, theme.default_bg);
698 init_pair(COLOR_BOT, theme.default_fg, theme.default_bg);
699 }
700
701 /*
702 * (re-)initialize the curses library FIXME: Error checking
703 */
704 static void init_curses(void)
705 {
706 curses_active = 1;
707 if (top.win && refresh() == ERR) { /* refesh is really needed */
708 msg_n_exit(EXIT_FAILURE, "refresh() failed\n");
709 }
710 check_geometry();
711 curs_set(0); /* make cursor invisible, ignore errors */
712 // if (noraw() == ERR);
713 // msg_n_exit(EXIT_FAILURE, "can not place terminal out of "
714 // "raw mode\n");
715 nonl(); /* tell curses not to do NL->CR/NL on output */
716 noecho(); /* don't echo input */
717 cbreak(); /* take input chars one at a time, no wait for \n */
718 //reset_prog_mode();
719 init_colors();
720 clear();
721 init_wins(theme.top_lines_default);
722 print_all_items();
723 noecho(); /* don't echo input */
724 }
725
726
727 static void check_sigchld(void)
728 {
729 pid_t pid;
730 reap_next_child:
731 pid = para_reap_child();
732 if (pid <= 0)
733 return;
734 if (pid == cmd_pid) {
735 cmd_pid = 0;
736 cmd_died = 1;
737 }
738 goto reap_next_child;
739 }
740
741 /*
742 * print status line if line starts with known command.
743 */
744 static int check_stat_line(char *line, __a_unused void *data)
745 {
746 int i;
747
748 // PARA_INFO_LOG("%s: checking: %s\n", __func__, line);
749 i = stat_line_valid(line);
750 if (i >= 0) {
751 line += strlen(status_item_list[i]) + 1;
752 free(stat_content[i]);
753 stat_content[i] = para_strdup(line);
754 print_stat_item(i);
755 }
756 return 1;
757 }
758
759 /*
760 * This sucker modifies its first argument. *handler and *arg are
761 * pointers to 0-terminated strings (inside line). Crap.
762 */
763 static int split_key_map(char *line, char **handler, char **arg)
764 {
765 if (!(*handler = strchr(line + 1, ':')))
766 goto err_out;
767 **handler = '\0';
768 (*handler)++;
769 if (!(*arg = strchr(*handler, ':')))
770 goto err_out;
771 **arg = '\0';
772 (*arg)++;
773 return 1;
774 err_out:
775 return 0;
776 }
777
778 static int check_key_map_args(void)
779 {
780 char *s;
781 int i, ret = -1;
782 char *tmp = NULL, *handler, *arg;
783
784 for (i = 0; i < conf.key_map_given; ++i) {
785 s = conf.key_map_arg[i];
786 if (!(*s))
787 goto err_out;
788 free(tmp);
789 tmp = para_strdup(s);
790 if (!split_key_map(tmp, &handler, &arg))
791 goto err_out;
792 if (strlen(handler) != 1)
793 goto err_out;
794 if (*handler != 'x'
795 && *handler != 'd'
796 && *handler != 'i'
797 && *handler != 'p')
798 goto err_out;
799 if (*handler != 'i')
800 continue;
801 if (find_cmd_byname(arg) < 0)
802 goto err_out;
803 }
804 ret = 0;
805 err_out:
806 free(tmp);
807 return ret;
808 }
809
810 /*
811 * React to various signal-related events
812 */
813 static void handle_signal(int sig)
814 {
815 switch (sig) {
816 case SIGTERM:
817 msg_n_exit(EXIT_FAILURE,
818 "only the good die young (caught SIGTERM))\n");
819 return;
820 case SIGWINCH:
821 if (curses_active) {
822 shutdown_curses();
823 init_curses();
824 redraw_bot_win();
825 }
826 return;
827 case SIGINT:
828 PARA_WARNING_LOG("%s", "caught SIGINT, reset");
829 /* Nothing to do. SIGINT killed our child, para_client stat.
830 * This get noticed by do_select which resets everything
831 */
832 return;
833 case SIGUSR1:
834 PARA_NOTICE_LOG("%s", "got SIGUSR1, rereading configuration");
835 com_reread_conf();
836 return;
837 case SIGCHLD:
838 check_sigchld();
839 return;
840 }
841 }
842
843 static int open_audiod_pipe(void)
844 {
845 static int init = 1;
846
847 if (init)
848 init = 0;
849 else
850 sleep(1);
851 return para_open_audiod_pipe(conf.stat_cmd_arg);
852 }
853
854 /*
855 * This is the core select loop. Besides the (internal) signal
856 * pipe, the following other fds are checked according to the mode:
857 *
858 * GETCH_MODE: check stdin, return when key is pressed
859 *
860 * COMMAND_MODE: check command_pipe and stdin. Return when a screen full
861 * of output has been read or when other end has closed command pipe or
862 * when any key is pressed.
863 *
864 * EXTERNAL_MODE: Check only signal pipe. Used when an external command
865 * is running. During that thime curses is disabled. Returns when
866 * cmd_pid == 0.
867 */
868 static int do_select(int mode)
869 {
870 fd_set rfds;
871 int ret;
872 int max_fileno, cp_numread = 1;
873 char command_buf[4096] = "";
874 int cbo = 0; /* command buf offset */
875 struct timeval tv;
876 repeat:
877 tv.tv_sec = conf.timeout_arg / 1000;
878 tv.tv_usec = (conf.timeout_arg % 1000) * 1000;
879 // ret = refresh_status();
880 FD_ZERO(&rfds);
881 max_fileno = 0;
882 /* audiod pipe */
883 if (audiod_pipe < 0)
884 audiod_pipe = open_audiod_pipe();
885 if (audiod_pipe >= 0)
886 para_fd_set(audiod_pipe, &rfds, &max_fileno);
887 /* signal pipe */
888 para_fd_set(signal_pipe, &rfds, &max_fileno);
889 /* command pipe only for COMMAND_MODE */
890 if (command_pipe >= 0 && mode == COMMAND_MODE)
891 para_fd_set(command_pipe, &rfds, &max_fileno);
892 ret = para_select(max_fileno + 1, &rfds, NULL, &tv);
893 if (ret <= 0)
894 goto check_return; /* skip fd checks */
895 /* signals */
896 if (FD_ISSET(signal_pipe, &rfds)) {
897 int sig_nr = para_next_signal();
898 if (sig_nr > 0)
899 handle_signal(sig_nr);
900 }
901 /* read command pipe if ready */
902 if (command_pipe >= 0 && mode == COMMAND_MODE &&
903 FD_ISSET(command_pipe, &rfds)) {
904 cp_numread = read(command_pipe, command_buf + cbo,
905 sizeof(command_buf) - 1 - cbo);
906 if (cp_numread >= 0)
907 cbo += cp_numread;
908 else {
909 if (cp_numread < 0)
910 PARA_ERROR_LOG("read error (%d)", cp_numread);
911 close(command_pipe);
912 command_pipe = -1;
913 }
914 }
915 if (audiod_pipe >= 0 && FD_ISSET(audiod_pipe, &rfds))
916 if (read_audiod_pipe(audiod_pipe, check_stat_line) <= 0) {
917 close(audiod_pipe);
918 audiod_pipe = -1;
919 clear_all_items();
920 free(stat_content[SI_STATUS_BAR]);
921 stat_content[SI_STATUS_BAR] =
922 para_strdup("audiod not running!?");
923 print_all_items();
924 }
925 check_return:
926 switch (mode) {
927 case COMMAND_MODE:
928 if (cp_numread <= 0 && !cbo) /* command complete */
929 return 0;
930 if (cbo)
931 cbo = for_each_line(command_buf, cbo,
932 &add_output_line, NULL);
933 if (cp_numread <= 0)
934 cbo = 0;
935 wrefresh(bot.win);
936 ret = wgetch(top.win);
937 if (ret != ERR && ret != KEY_RESIZE) {
938 if (command_pipe) {
939 close(command_pipe);
940 command_pipe = -1;
941 }
942 if (cmd_pid)
943 kill(cmd_pid, SIGTERM);
944 return -1;
945 }
946 break;
947 case GETCH_MODE:
948 ret = wgetch(top.win);
949 if (ret != ERR && ret != KEY_RESIZE)
950 return ret;
951 break;
952 case EXTERNAL_MODE:
953 if (cmd_died) {
954 cmd_died = 0;
955 return 0;
956 }
957 }
958 goto repeat;
959 }
960
961 /*
962 * read from command pipe and print data to bot window
963 */
964 static int send_output(void)
965 {
966 if (command_pipe < 0)
967 return 0;
968 if (do_select(COMMAND_MODE) >= 0)
969 PARA_INFO_LOG("%s", "command complete");
970 else
971 PARA_NOTICE_LOG("%s", "command aborted");
972 print_in_bar(COLOR_MSG, " ");
973 return 1;
974 }
975
976 static int client_cmd_cmdline(char *cmd)
977 {
978 int ret, fds[3] = {0, 1, 0};
979 char *c = make_message(BINDIR "/para_client %s", cmd);
980
981 outputf(COLOR_COMMAND, "%s", c);
982 print_in_bar(COLOR_MSG, "executing client command, hit any key to abort\n");
983 ret = para_exec_cmdline_pid(&cmd_pid, c, fds);
984 free(c);
985 if (ret < 0)
986 return -1;
987 command_pipe = fds[1];
988 mark_fd_nonblock(command_pipe);
989 return send_output();
990 }
991
992 /*
993 * exec command and print output to bot win
994 */
995 static int display_cmd(char *cmd)
996 {
997 int fds[3] = {0, 1, 0};
998
999 print_in_bar(COLOR_MSG, "executing display command, hit any key to abort");
1000 outputf(COLOR_COMMAND, "%s", cmd);
1001 if (para_exec_cmdline_pid(&cmd_pid, cmd, fds) < 0)
1002 return -1;
1003 command_pipe = fds[1];
1004 mark_fd_nonblock(command_pipe);
1005 return send_output();
1006 }
1007
1008 /*
1009 * shutdown curses and stat pipe before executing external commands
1010 */
1011 static int external_cmd(char *cmd)
1012 {
1013 int fds[3] = {-1, -1, -1};
1014
1015 if (cmd_pid)
1016 return -1;
1017 shutdown_curses();
1018 para_exec_cmdline_pid(&cmd_pid, cmd, fds);
1019 cmd_died = 0;
1020 do_select(EXTERNAL_MODE);
1021 init_curses();
1022 return 0;
1023 }
1024
1025 static void print_scroll_msg(void)
1026 {
1027 unsigned lines_total, filled = ringbuffer_filled(bot_win_rb);
1028 int first_rbe = first_visible_rbe(&lines_total);
1029 print_in_bar(COLOR_MSG, "scrolled view: %d-%d/%d\n", filled - first_rbe,
1030 filled - scroll_position, ringbuffer_filled(bot_win_rb));
1031 }
1032
1033 static void com_page_down(void)
1034 {
1035 unsigned lines = 0;
1036 int i = scroll_position;
1037 while (lines < bot.lines && --i > 0) {
1038 struct rb_entry *rbe = ringbuffer_get(bot_win_rb, i);
1039 if (!rbe)
1040 break;
1041 lines += NUM_LINES(strlen(rbe->msg));
1042 }
1043 if (lines) {
1044 scroll_position = i;
1045 redraw_bot_win();
1046 print_scroll_msg();
1047 return;
1048 }
1049 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1050 }
1051
1052 static void com_page_up(void)
1053 {
1054 unsigned lines;
1055 int fvr = first_visible_rbe(&lines);
1056 if (fvr < 0 || fvr + 1 >= ringbuffer_filled(bot_win_rb)) {
1057 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1058 return;
1059 }
1060 scroll_position = fvr + 1;
1061 for (; scroll_position > 0; scroll_position--) {
1062 fvr = first_visible_rbe(&lines);
1063 if (lines == bot.lines)
1064 break;
1065 }
1066 redraw_bot_win();
1067 print_scroll_msg();
1068 }
1069
1070 static void com_scroll_down(void)
1071 {
1072 struct rb_entry *rbe;
1073 int rbe_lines;
1074
1075 if (!scroll_position) {
1076 print_in_bar(COLOR_ERRMSG, "bottom of buffer is shown\n");
1077 return;
1078 }
1079 scroll_position--;
1080 rbe = ringbuffer_get(bot_win_rb, scroll_position);
1081 rbe_lines = NUM_LINES(rbe->len);
1082 wscrl(bot.win, rbe_lines);
1083 wmove(bot.win, bot.lines - rbe_lines, 0);
1084 wattron(bot.win, COLOR_PAIR(rbe->color));
1085 waddstr(bot.win, rbe->msg);
1086 wrefresh(bot.win);
1087 print_scroll_msg();
1088 }
1089
1090 static void com_scroll_up(void)
1091 {
1092 struct rb_entry *rbe = NULL;
1093 unsigned lines;
1094 int i, first_rbe, num_scroll;
1095
1096 /* the entry that is going to vanish */
1097 rbe = ringbuffer_get(bot_win_rb, scroll_position);
1098 if (!rbe)
1099 goto err_out;
1100 num_scroll = NUM_LINES(rbe->len);
1101 first_rbe = first_visible_rbe(&lines);
1102 if (first_rbe < 0 || (first_rbe == ringbuffer_filled(bot_win_rb) - 1))
1103 goto err_out;
1104 scroll_position++;
1105 wscrl(bot.win, -num_scroll);
1106 i = draw_top_rbe(&lines);
1107 if (i < 0)
1108 goto err_out;
1109 while (i > 0 && lines < num_scroll) {
1110 int rbe_lines;
1111 rbe = ringbuffer_get(bot_win_rb, --i);
1112 if (!rbe)
1113 break;
1114 rbe_lines = NUM_LINES(rbe->len);
1115 lines += rbe_lines;
1116 // fprintf(stderr, "msg: %s\n", rbe->msg);
1117 wattron(bot.win, COLOR_PAIR(rbe->color));
1118 waddstr(bot.win, "\n");
1119 waddstr(bot.win, rbe->msg);
1120 if (!i)
1121 break;
1122 i--;
1123 }
1124 wrefresh(bot.win);
1125 print_scroll_msg();
1126 return;
1127 err_out:
1128 print_in_bar(COLOR_ERRMSG, "top of buffer is shown\n");
1129 }
1130
1131 static void com_ll_decr(void)
1132 {
1133 if (conf.loglevel_arg <= DEBUG) {
1134 print_in_bar(COLOR_ERRMSG,
1135 "loglevel already at maximal verbosity\n");
1136 return;
1137 }
1138 conf.loglevel_arg--;
1139 print_in_bar(COLOR_MSG, "loglevel set to %d\n", conf.loglevel_arg);
1140 }
1141
1142 static void com_ll_incr(void)
1143 {
1144 if (conf.loglevel_arg >= EMERG) {
1145 print_in_bar(COLOR_ERRMSG,
1146 "loglevel already at miminal verbosity\n");
1147 return;
1148 }
1149 conf.loglevel_arg++;
1150 print_in_bar(COLOR_MSG, "loglevel set to %d\n", conf.loglevel_arg);
1151 }
1152
1153 /*
1154 * reread configuration, terminate on errors
1155 */
1156 static void com_reread_conf(void)
1157 {
1158 char *cf =configfile_exists();
1159 struct gui_cmdline_parser_params params = {
1160 .override = 1,
1161 .initialize = 1,
1162 .check_required = 0,
1163 .check_ambiguity = 0
1164 };
1165
1166 if (!cf) {
1167 PARA_WARNING_LOG("%s", "there is no configuration to read");
1168 return;
1169 }
1170 PARA_INFO_LOG("%s", "rereading command line options and config file");
1171 gui_cmdline_parser(_argc, _argv, &conf);
1172 gui_cmdline_parser_config_file(cf, &conf, &params);
1173 PARA_NOTICE_LOG("%s", "config file reloaded");
1174 if (check_key_map_args() < 0)
1175 finish(EXIT_FAILURE);
1176 }
1177
1178 static void com_help(void)
1179 {
1180 int i;
1181
1182 for (i = 0; i < conf.key_map_given; ++i) {
1183 char *handler, *arg, *tmp = para_strdup(conf.key_map_arg[i]);
1184 const char *handler_text = "???", *desc = NULL;
1185
1186 if (!split_key_map(tmp, &handler, &arg)) {
1187 free(tmp);
1188 return;
1189 }
1190 switch (*handler) {
1191 case 'i':
1192 handler_text = "internal";
1193 desc = command_list[find_cmd_byname(arg)].description;
1194 break;
1195 case 'x': handler_text = "external"; break;
1196 case 'd': handler_text = "display "; break;
1197 case 'p': handler_text = "para "; break;
1198 }
1199 outputf(COLOR_MSG, "%s\t%s\t%s%s\t%s", tmp, handler_text, arg,
1200 strlen(arg) < 8? "\t" : "",
1201 desc? desc : "");
1202 free(tmp);
1203 }
1204 for (i = 0; command_list[i].handler; i++) {
1205 struct gui_command gc = command_list[i];
1206
1207 outputf(COLOR_MSG, "%s\tinternal\t%s\t%s%s", gc.key, gc.name,
1208 strlen(gc.name) < 8? "\t" : "",
1209 gc.description);
1210 }
1211 print_in_bar(COLOR_MSG, "try \"para_gui -h\" or \"para_client help\" "
1212 "for more info");
1213 }
1214
1215 static void com_shrink_top_win(void)
1216 {
1217 if (top.lines <= theme.top_lines_min) {
1218 PARA_WARNING_LOG("%s", "can not decrease top window");
1219 return;
1220 }
1221 init_wins(top.lines - 1);
1222 wclear(top.win);
1223 print_all_items();
1224 print_in_bar(COLOR_MSG, "%s", "decreased top window");
1225 }
1226
1227 static void com_enlarge_top_win(void)
1228 {
1229 if (bot.lines < 3) {
1230 PARA_WARNING_LOG("%s", "can not increase top window");
1231 return;
1232 }
1233 init_wins(top.lines + 1);
1234 wclear(top.win);
1235 print_all_items();
1236 print_in_bar(COLOR_MSG, "increased top window");
1237 }
1238
1239 static void com_version(void)
1240 {
1241 print_in_bar(COLOR_MSG, "para_gui " PACKAGE_VERSION " \""
1242 CODENAME "\"");
1243 }
1244
1245 static void com_quit(void)
1246 {
1247 finish(0);
1248 }
1249
1250 static void com_refresh(void)
1251 {
1252 shutdown_curses();
1253 init_curses();
1254 }
1255
1256 static void change_theme(int next)
1257 {
1258 if (next)
1259 next_theme(&theme);
1260 else
1261 prev_theme(&theme);
1262 /* This seems to be needed twice, why? */
1263 com_refresh();
1264 com_refresh();
1265 PARA_NOTICE_LOG("new theme: %s", theme.name);
1266 }
1267
1268 static void com_next_theme(void)
1269 {
1270 change_theme(1);
1271 }
1272
1273 static void com_prev_theme(void)
1274 {
1275 change_theme(0);
1276 }
1277
1278
1279 static void handle_command(int c)
1280 {
1281 int i;
1282
1283 /* first check user's key bindings */
1284 for (i = 0; i < conf.key_map_given; ++i) {
1285 char tmp[MAXLINE], *handler, *arg;
1286
1287 strcpy(tmp, conf.key_map_arg[i]);
1288 if (!split_key_map(tmp, &handler, &arg))
1289 return;
1290 if (!strcmp(tmp, km_keyname(c))) {
1291 if (*handler == 'd') {
1292 display_cmd(arg);
1293 return;
1294 }
1295 if (*handler == 'x') {
1296 external_cmd(arg);
1297 return;
1298 }
1299 if (*handler == 'p') {
1300 client_cmd_cmdline(arg);
1301 return;
1302 }
1303 if (*handler == 'i') {
1304 int num = find_cmd_byname(arg);
1305 if (num >= 0)
1306 command_list[num].handler();
1307 return;
1308 }
1309 }
1310 }
1311 /* not found, check internal key bindings */
1312 for (i = 0; command_list[i].handler; i++) {
1313 if (!strcmp(km_keyname(c), command_list[i].key)) {
1314 command_list[i].handler();
1315 return;
1316 }
1317 }
1318 print_in_bar(COLOR_ERRMSG, "key '%s' is not bound, press ? for help",
1319 km_keyname(c));
1320 }
1321
1322 int main(int argc, char *argv[])
1323 {
1324 int ret;
1325 char *cf;
1326
1327 _argc = argc;
1328 _argv = argv;
1329
1330 if (gui_cmdline_parser(argc, argv, &conf)) {
1331 fprintf(stderr, "parse error while reading command line\n");
1332 exit(EXIT_FAILURE);
1333 }
1334 HANDLE_VERSION_FLAG("gui", conf);
1335 init_theme(0, &theme);
1336 top.lines = theme.top_lines_default;
1337 if (check_key_map_args() < 0) {
1338 fprintf(stderr, "invalid key map\n");
1339 exit(EXIT_FAILURE);
1340 }
1341 cf = configfile_exists();
1342 if (!cf && conf.config_file_given) {
1343 fprintf(stderr, "can not read config file %s\n",
1344 conf.config_file_arg);
1345 exit(EXIT_FAILURE);
1346 }
1347 if (cf) {
1348 struct gui_cmdline_parser_params params = {
1349 .override = 0,
1350 .initialize = 0,
1351 .check_required = 0,
1352 .check_ambiguity = 0
1353 };
1354 gui_cmdline_parser_config_file(cf, &conf, &params);
1355 }
1356 if (check_key_map_args() < 0) {
1357 fprintf(stderr, "invalid key map in config file\n");
1358 exit(EXIT_FAILURE);
1359 }
1360 setup_signal_handling();
1361 bot_win_rb = ringbuffer_new(RINGBUFFER_SIZE);
1362 initscr(); /* needed only once, always successful */
1363 init_curses();
1364 print_welcome();
1365 for (;;) {
1366 print_status_bar();
1367 ret = do_select(GETCH_MODE);
1368 if (!ret)
1369 continue;
1370 print_in_bar(COLOR_MSG, " ");
1371 handle_command(ret);
1372 }
1373 }