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