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