X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=blobdiff_plain;f=gui.c;h=a4f1717c2a996568d2fe36cc3a010f165ff1bcd9;hp=8d84e463d41dcdb0426b905b2208f5118c149f2c;hb=3b0a8fdb191309652e71e104a8f3705d55a93d3e;hpb=96fbecf492a660afbe7cbd5d45ae1dbe02f3acbf diff --git a/gui.c b/gui.c index 8d84e463..a4f1717c 100644 --- a/gui.c +++ b/gui.c @@ -1,8 +1,4 @@ -/* - * Copyright (C) 1998-2014 Andre Noll - * - * Licensed under the GPL v2. For licencing details see COPYING. - */ +/* Copyright (C) 1998 Andre Noll , see file COPYING. */ /** \file gui.c Curses-based interface for paraslash. */ @@ -12,10 +8,12 @@ #include #include #include +#include -#include "gui.cmdline.h" +#include "gui.lsg.h" #include "para.h" #include "gui.h" +#include "lsu.h" #include "string.h" #include "ringbuffer.h" #include "fd.h" @@ -23,18 +21,30 @@ #include "list.h" #include "sched.h" #include "signal.h" -#include "ggo.h" #include "version.h" -/** define the array of error lists needed by para_gui */ -INIT_GUI_ERRLISTS; +/** Array of error strings. */ +DEFINE_PARA_ERRLIST; + +static struct lls_parse_result *cmdline_lpr, *lpr; + +#define CMD_PTR (lls_cmd(0, gui_suite)) +#define OPT_RESULT(_name) (lls_opt_result(LSG_GUI_PARA_GUI_OPT_ ## _name, lpr)) +#define OPT_GIVEN(_name) (lls_opt_given(OPT_RESULT(_name))) +#define OPT_STRING_VAL(_name) (lls_string_val(0, OPT_RESULT(_name))) +#define OPT_UINT32_VAL(_name) (lls_uint32_val(0, OPT_RESULT(_name))) +#define FOR_EACH_KEY_MAP(_i) for (_i = 0; _i < OPT_GIVEN(KEY_MAP); _i++) + static char *stat_content[NUM_STAT_ITEMS]; static struct gui_window { WINDOW *win; + bool needs_update; } top, bot, sb, in, sep; +/** How many lines of output to remember. */ #define RINGBUFFER_SIZE 512 + struct rb_entry { char *msg; size_t len; @@ -43,17 +53,15 @@ struct rb_entry { static struct ringbuffer *bot_win_rb; static unsigned scroll_position; - static pid_t exec_pid; - static int exec_fds[2] = {-1, -1}; -static struct gui_args_info conf; static int loglevel; +/** Type of the process currently being executed. */ enum exec_status { - EXEC_IDLE, /* no command running */ - EXEC_DCMD, /* para or display command running */ - EXEC_XCMD, /* external command running */ + EXEC_IDLE, /**< No process running. */ + EXEC_DCMD, /**< para or display process running. */ + EXEC_XCMD, /**< External process running. */ }; /** @@ -120,27 +128,29 @@ GUI_COMMANDS static struct gui_command command_list[] = {GUI_COMMANDS {.name = NULL}}; struct input_task { - struct task task; + struct task *task; }; struct status_task { - struct task task; + struct task *task; + pid_t pid; char *buf; int bufsize, loaded; struct timeval next_exec; int fd; }; +/** Stdout/stderr of the executing process is read in chunks of this size. */ #define COMMAND_BUF_SIZE 32768 struct exec_task { - struct task task; + struct task *task; char command_buf[2][COMMAND_BUF_SIZE]; /* stdout/stderr of command */ int cbo[2]; /* command buf offsets */ unsigned flags[2]; /* passed to for_each_line() */ }; -static int find_cmd_byname(char *name) +static int find_cmd_byname(const char *name) { int i; @@ -238,10 +248,10 @@ static char *km_keyname(int c) } /* Print given number of spaces to curses window. */ -static void add_spaces(WINDOW* win, unsigned int num) +static void add_spaces(WINDOW *win, unsigned int num) { - char space[] = " "; - unsigned sz = sizeof(space) - 1; /* number of spaces */ + const char space[] = " "; + const unsigned sz = sizeof(space) - 1; /* number of spaces */ while (num >= sz) { waddstr(win, space); @@ -249,53 +259,57 @@ static void add_spaces(WINDOW* win, unsigned int num) } if (num > 0) { assert(num < sz); - space[num] = '\0'; - waddstr(win, space); + waddstr(win, space + sz - num); } } /* - * print aligned string to curses window. This function always prints + * Print aligned string to curses window. This function always prints * exactly len chars. */ -static int align_str(WINDOW* win, char *str, unsigned int len, +static int align_str(WINDOW *win, const char *str, unsigned int len, unsigned int align) { - int ret, i, num; /* of spaces */ + int ret, num; /* of spaces */ size_t width; + char *sstr; /* sanitized string */ if (!win || !str) return 0; - ret = strwidth(str, &width); + ret = sanitize_str(str, len, &sstr, &width); if (ret < 0) { PARA_ERROR_LOG("%s\n", para_strerror(-ret)); width = 0; - str[0] = '\0'; + sstr = para_strdup(NULL); } + assert(width <= len); num = len - width; - if (num < 0) { - str[len] = '\0'; - num = 0; - } - /* replace control characters by spaces */ - for (i = 0; i < len && str[i]; i++) { - if (str[i] == '\n' || str[i] == '\r' || str[i] == '\f') - str[i] = ' '; - } if (align == LEFT) { - waddstr(win, str); + waddstr(win, sstr); add_spaces(win, num); } else if (align == RIGHT) { add_spaces(win, num); - waddstr(win, str); + waddstr(win, sstr); } else { add_spaces(win, num / 2); - waddstr(win, str[0]? str: ""); + waddstr(win, sstr); add_spaces(win, num - num / 2); } + free(sstr); return 1; } +static void refresh_window(struct gui_window *gw) +{ + gw->needs_update = true; +} + +static bool window_update_needed(void) +{ + return top.needs_update || bot.needs_update || sb.needs_update || + in.needs_update || sep.needs_update; +} + __printf_2_3 static void print_in_bar(int color, const char *fmt,...) { char *msg; @@ -310,7 +324,7 @@ __printf_2_3 static void print_in_bar(int color, const char *fmt,...) wmove(in.win, 0, 0); align_str(in.win, msg, get_num_cols(&in), LEFT); free(msg); - wrefresh(in.win); + refresh_window(&in); } static void print_status_bar(void) @@ -406,7 +420,7 @@ static void redraw_bot_win(void) waddstr(bot.win, rbe->msg); } out: - wrefresh(bot.win); + refresh_window(&bot); } static void rb_add_entry(int color, char *msg) @@ -439,10 +453,8 @@ static void rb_add_entry(int color, char *msg) waddstr(bot.win, msg); } -/* - * print formated output to bot win and refresh - */ -__printf_2_3 static void outputf(int color, const char* fmt,...) +/* Print formatted output to bot win and refresh. */ +__printf_2_3 static void outputf(int color, const char *fmt,...) { char *msg; va_list ap; @@ -453,7 +465,7 @@ __printf_2_3 static void outputf(int color, const char* fmt,...) xvasprintf(&msg, fmt, ap); va_end(ap); rb_add_entry(color, msg); - wrefresh(bot.win); + refresh_window(&bot); } static int add_output_line(char *line, void *data) @@ -480,37 +492,55 @@ static __printf_2_3 void curses_log(int ll, const char *fmt,...) if (bytes > 0 && msg[bytes - 1] == '\n') msg[bytes - 1] = '\0'; /* cut trailing newline */ rb_add_entry(color, msg); - wrefresh(bot.win); + refresh_window(&bot); } else if (exec_pid <= 0) /* no external command running */ vfprintf(stderr, fmt, ap); va_end(ap); } -__printf_2_3 void (*para_log)(int, const char*, ...) = curses_log; +/** The log function of para_gui, always set to curses_log(). */ +__printf_2_3 void (*para_log)(int, const char *, ...) = curses_log; + +/* Call endwin() to reset the terminal into non-visual mode. */ static void shutdown_curses(void) { - def_prog_mode(); + /* + * If para_gui received a terminating signal in external mode, the + * terminal can be in an unusable state at this point because the child + * process might not have caught the signal. In this case endwin() has + * already been called and must not be called again. So we first return + * to program mode, then call endwin(). + */ + if (!curses_active()) + reset_prog_mode(); endwin(); } -/* disable curses, print a message, kill running processes and exit */ -__noreturn __printf_2_3 static void die(int exit_code, const char* fmt, ...) +/* Disable curses, print a message, kill running processes and exit. */ +__noreturn __printf_2_3 static void die(int exit_code, const char *fmt, ...) { va_list argp; + /* Kill every process in our process group. */ + para_sigaction(SIGTERM, SIG_IGN); + kill(0, SIGTERM); + /* Wait up to two seconds for child processes to die. */ + alarm(2); + while (waitpid(0, NULL, 0) >= 0) + ; /* nothing */ + alarm(0); + /* mousemask() exists only in ncurses */ +#ifdef NCURSES_MOUSE_VERSION + mousemask(~(mmask_t)0, NULL); /* Avoid bad terminal state with xterm. */ +#endif shutdown_curses(); va_start(argp, fmt); vfprintf(stderr, fmt, argp); va_end(argp); - /* kill every process in the process group and exit */ - para_sigaction(SIGTERM, SIG_IGN); - kill(0, SIGTERM); exit(exit_code); } -/* - * Print stat item #i to curses window - */ +/* Print stat item #i to curses window. */ static void print_stat_item(int i) { char *tmp; @@ -522,11 +552,10 @@ static void print_stat_item(int i) return; tmp = make_message("%s%s%s", d.prefix, c, d.postfix); wmove(top.win, d.y * top_lines / 100, d.x * COLS / 100); - wrefresh(top.win); wattron(top.win, COLOR_PAIR(i + 1)); align_str(top.win, tmp, d.len * COLS / 100, d.align); free(tmp); - wrefresh(top.win); + refresh_window(&top); } static int update_item(int item_num, char *buf) @@ -537,19 +566,19 @@ static int update_item(int item_num, char *buf) if (buf && buf[0]) goto dup; switch (item_num) { - case SI_ARTIST: + case SI_artist: *c = para_strdup("(artist tag not set)"); goto print; - case SI_TITLE: + case SI_title: *c = para_strdup("(title tag not set)"); goto print; - case SI_YEAR: + case SI_year: *c = para_strdup("????"); goto print; - case SI_ALBUM: + case SI_album: *c = para_strdup("(album tag not set)"); goto print; - case SI_COMMENT: + case SI_comment: *c = para_strdup("(comment tag not set)"); goto print; } @@ -580,29 +609,43 @@ static void clear_all_items(void) } } -static void status_pre_select(struct sched *s, struct task *t) +static void status_pre_select(struct sched *s, void *context) { - struct status_task *st = container_of(t, struct status_task, task); + struct status_task *st = context; if (st->fd >= 0) - return para_fd_set(st->fd, &s->rfds, &s->max_fileno); - sched_request_barrier_or_min_delay(&st->next_exec, s); + para_fd_set(st->fd, &s->rfds, &s->max_fileno); + if (task_get_notification(st->task) < 0) + return sched_min_delay(s); + if (st->fd < 0) + sched_request_barrier_or_min_delay(&st->next_exec, s); } -static int status_post_select(struct sched *s, struct task *t) +static int status_post_select(struct sched *s, void *context) { - struct status_task *st = container_of(t, struct status_task, task); + struct status_task *st = context; size_t sz; - pid_t pid; int ret, ret2; + ret = task_get_notification(st->task); + if (ret == -E_GUI_SIGCHLD && st->pid > 0) { + int exit_status; + if (waitpid(st->pid, &exit_status, WNOHANG) == st->pid) { + st->pid = 0; + PARA_ERROR_LOG("stat command exit status: %d", + exit_status); + } + } if (st->fd < 0) { int fds[3] = {0, 1, 0}; + if (st->pid > 0) + return 0; /* Avoid busy loop */ if (tv_diff(&st->next_exec, now, NULL) > 0) return 0; st->next_exec.tv_sec = now->tv_sec + 2; - ret = para_exec_cmdline_pid(&pid, conf.stat_cmd_arg, fds); + ret = para_exec_cmdline_pid(&st->pid, + OPT_STRING_VAL(STAT_CMD), fds); if (ret < 0) return 0; ret = mark_fd_nonblocking(fds[1]); @@ -629,12 +672,13 @@ static int status_post_select(struct sched *s, struct task *t) ret2 = for_each_stat_item(st->buf, st->loaded, update_item); if (ret < 0 || ret2 < 0) { st->loaded = 0; - PARA_NOTICE_LOG("closing stat pipe: %s\n", para_strerror(-ret)); + PARA_NOTICE_LOG("closing stat pipe: %s\n", + para_strerror(ret < 0? -ret : -ret2)); close(st->fd); st->fd = -1; clear_all_items(); - free(stat_content[SI_BASENAME]); - stat_content[SI_BASENAME] = + free(stat_content[SI_basename]); + stat_content[SI_basename] = para_strdup("stat command terminated!?"); print_all_items(); return 0; @@ -646,9 +690,7 @@ static int status_post_select(struct sched *s, struct task *t) return 0; } -/* - * init all windows - */ +/* Initialize all windows. */ static void init_wins(int top_lines) { int top_y = 0, bot_y = top_lines + 1, sb_y = LINES - 2, @@ -656,7 +698,7 @@ static void init_wins(int top_lines) int bot_lines = LINES - top_lines - 3, sb_lines = 1, in_lines = 1, sep_lines = 1; - assume_default_colors(theme.default_fg, theme.default_bg); + assume_default_colors(theme.dflt.fg, theme.dflt.bg); if (top.win) { wresize(top.win, top_lines, COLS); mvwin(top.win, top_y, 0); @@ -728,16 +770,16 @@ static void init_colors_or_die(void) die(EXIT_FAILURE, "fatal: failed to start colors\n"); FOR_EACH_STATUS_ITEM(i) if (theme.data[i].len) - init_pair_or_die(i + 1, theme.data[i].fg, - theme.data[i].bg); - init_pair_or_die(COLOR_STATUSBAR, theme.sb_fg, theme.sb_bg); - init_pair_or_die(COLOR_COMMAND, theme.cmd_fg, theme.cmd_bg); - init_pair_or_die(COLOR_OUTPUT, theme.output_fg, theme.output_bg); - init_pair_or_die(COLOR_MSG, theme.msg_fg, theme.msg_bg); - init_pair_or_die(COLOR_ERRMSG, theme.err_msg_fg, theme.err_msg_bg); - init_pair_or_die(COLOR_SEPARATOR, theme.sep_fg, theme.sep_bg); - init_pair_or_die(COLOR_TOP, theme.default_fg, theme.default_bg); - init_pair_or_die(COLOR_BOT, theme.default_fg, theme.default_bg); + init_pair_or_die(i + 1, theme.data[i].color.fg, + theme.data[i].color.bg); + init_pair_or_die(COLOR_STATUSBAR, theme.sb.fg, theme.sb.bg); + init_pair_or_die(COLOR_COMMAND, theme.cmd.fg, theme.cmd.bg); + init_pair_or_die(COLOR_OUTPUT, theme.output.fg, theme.output.bg); + init_pair_or_die(COLOR_MSG, theme.msg.fg, theme.msg.bg); + init_pair_or_die(COLOR_ERRMSG, theme.err_msg.fg, theme.err_msg.bg); + init_pair_or_die(COLOR_SEPARATOR, theme.sep.fg, theme.sep.bg); + init_pair_or_die(COLOR_TOP, theme.dflt.fg, theme.dflt.bg); + init_pair_or_die(COLOR_BOT, theme.dflt.fg, theme.dflt.bg); } /* (Re-)initialize the curses library. */ @@ -745,7 +787,7 @@ static void init_curses(void) { if (curses_active()) return; - if (top.win && refresh() == ERR) /* refresh is really needed */ + if (refresh() == ERR) /* refresh is really needed */ die(EXIT_FAILURE, "refresh() failed\n"); if (LINES < theme.lines_min || COLS < theme.cols_min) die(EXIT_FAILURE, "Terminal (%dx%d) too small" @@ -765,23 +807,6 @@ static void init_curses(void) // noecho(); /* don't echo input */ } -static void check_sigchld(void) -{ - int ret; - pid_t pid; - -reap_next_child: - ret = para_reap_child(&pid); - if (ret <= 0) - return; - if (pid == exec_pid) { - exec_pid = 0; - init_curses(); - print_in_bar(COLOR_MSG, " "); - } - goto reap_next_child; -} - /* * This sucker modifies its first argument. *handler and *arg are * pointers to 0-terminated strings (inside line). Crap. @@ -805,12 +830,13 @@ static void check_key_map_args_or_die(void) { int i; char *tmp = NULL; + const struct lls_opt_result *lor = OPT_RESULT(KEY_MAP); - for (i = 0; i < conf.key_map_given; ++i) { + FOR_EACH_KEY_MAP(i) { char *handler, *arg; free(tmp); - tmp = para_strdup(conf.key_map_arg[i]); + tmp = para_strdup(lls_string_val(i, lor)); if (!split_key_map(tmp, &handler, &arg)) break; if (strlen(handler) != 1) @@ -823,68 +849,50 @@ static void check_key_map_args_or_die(void) if (find_cmd_byname(arg) < 0) break; } - if (i != conf.key_map_given) - die(EXIT_FAILURE, "invalid key map: %s\n", conf.key_map_arg[i]); + if (i != OPT_GIVEN(KEY_MAP)) + die(EXIT_FAILURE, "invalid key map: %s\n", + lls_string_val(i, lor)); free(tmp); } -static void parse_config_file_or_die(bool override) -{ - bool err; - char *config_file; - struct gui_cmdline_parser_params params = { - .override = override, - .initialize = 0, - .check_required = !override, - .check_ambiguity = 0, - .print_errors = 1, - }; - - if (conf.config_file_given) - config_file = para_strdup(conf.config_file_arg); - else { - char *home = para_homedir(); - config_file = make_message("%s/.paraslash/gui.conf", home); - free(home); - } - if (!file_exists(config_file)) { - if (!conf.config_file_given) - err = false; - else { - PARA_EMERG_LOG("config file %s does not exist\n", - config_file); - err = true; - } - goto out; +static void parse_config_file_or_die(bool reload) +{ + int ret; + unsigned flags = MCF_DONT_FREE; + + if (lpr != cmdline_lpr) + lls_free_parse_result(lpr, CMD_PTR); + lpr = cmdline_lpr; + if (reload) + flags |= MCF_OVERRIDE; + ret = lsu_merge_config_file_options(OPT_STRING_VAL(CONFIG_FILE), + "gui.conf", &lpr, CMD_PTR, gui_suite, flags); + if (ret < 0) { + PARA_EMERG_LOG("failed to parse config file: %s\n", + para_strerror(-ret)); + exit(EXIT_FAILURE); } - gui_cmdline_parser_config_file(config_file, &conf, ¶ms); - loglevel = get_loglevel_by_name(conf.loglevel_arg); + loglevel = OPT_UINT32_VAL(LOGLEVEL); check_key_map_args_or_die(); - theme_init(conf.theme_arg, &theme); - err = false; -out: - free(config_file); - if (err) - exit(EXIT_FAILURE); + theme_init(OPT_STRING_VAL(THEME), &theme); } -/* reread configuration, terminate on errors */ +/* Reread configuration, terminate on errors. */ static void reread_conf(void) { /* - * gengetopt might print to stderr and exit on errors. So we have to - * shutdown curses first. + * If the reload of the config file fails, we are about to exit. In + * this case we print the error message to stderr rather than to the + * curses window. So we have to shutdown curses first. */ shutdown_curses(); - parse_config_file_or_die(true /* override */); + parse_config_file_or_die(true); init_curses(); print_in_bar(COLOR_MSG, "config file reloaded\n"); } -/* - * React to various signal-related events - */ -static int signal_post_select(struct sched *s, __a_unused struct task *t) +/* React to various signal-related events. */ +static int signal_post_select(struct sched *s, __a_unused void *context) { int ret = para_next_signal(&s->rfds); @@ -893,8 +901,8 @@ static int signal_post_select(struct sched *s, __a_unused struct task *t) switch (ret) { case SIGTERM: die(EXIT_FAILURE, "only the good die young (caught SIGTERM)\n"); - return 1; case SIGWINCH: + PARA_NOTICE_LOG("got SIGWINCH\n"); if (curses_active()) { shutdown_curses(); init_curses(); @@ -902,17 +910,13 @@ static int signal_post_select(struct sched *s, __a_unused struct task *t) } return 1; case SIGINT: - PARA_WARNING_LOG("caught SIGINT, reset\n"); - /* Nothing to do. SIGINT killed our child which gets noticed - * by do_select and resets everything. - */ return 1; case SIGUSR1: PARA_NOTICE_LOG("got SIGUSR1, rereading configuration\n"); reread_conf(); return 1; case SIGCHLD: - check_sigchld(); + task_notify_all(s, E_GUI_SIGCHLD); return 1; } return 1; @@ -927,26 +931,32 @@ static enum exec_status exec_status(void) return EXEC_IDLE; } -static void exec_pre_select(struct sched *s, __a_unused struct task *t) +static void exec_pre_select(struct sched *s, void *context) { - enum exec_status es = exec_status(); - - if (es != EXEC_DCMD) - return; + struct exec_task *et = context; if (exec_fds[0] >= 0) para_fd_set(exec_fds[0], &s->rfds, &s->max_fileno); if (exec_fds[1] >= 0) para_fd_set(exec_fds[1], &s->rfds, &s->max_fileno); + if (task_get_notification(et->task) < 0) + sched_min_delay(s); } -static int exec_post_select(struct sched *s, struct task *t) +static int exec_post_select(struct sched *s, void *context) { - struct exec_task *ct = container_of(t, struct exec_task, task); + struct exec_task *ct = context; int i, ret; - enum exec_status es = exec_status(); - if (es != EXEC_DCMD) - return 0; + ret = task_get_notification(ct->task); + if (ret == -E_GUI_SIGCHLD && exec_pid > 0) { + int exit_status; + if (waitpid(exec_pid, &exit_status, WNOHANG) == exec_pid) { + exec_pid = 0; + init_curses(); + PARA_INFO_LOG("command exit status: %d", exit_status); + print_in_bar(COLOR_MSG, " "); + } + } for (i = 0; i < 2; i++) { size_t sz; if (exec_fds[i] < 0) @@ -959,7 +969,7 @@ static int exec_post_select(struct sched *s, struct task *t) ct->cbo[i] = for_each_line(ct->flags[i], ct->command_buf[i], ct->cbo[i], add_output_line, &i); if (sz != ct->cbo[i]) { /* at least one line found */ - wrefresh(bot.win); + refresh_window(&bot); ct->flags[i] = 0; } if (ret < 0 || exec_pid == 0) { @@ -982,13 +992,15 @@ static int exec_post_select(struct sched *s, struct task *t) return 0; } -static void input_pre_select(struct sched *s, __a_unused struct task *t) +static void input_pre_select(struct sched *s, __a_unused void *context) { if (exec_status() != EXEC_XCMD) para_fd_set(STDIN_FILENO, &s->rfds, &s->max_fileno); + if (window_update_needed()) + sched_min_delay(s); } -/* read from command pipe and print data to bot window */ +/* Read from command pipe and print data to bot window. */ static void exec_and_display(const char *file_and_args) { int ret, fds[3] = {0, 1, 1}; @@ -1022,9 +1034,7 @@ static void exec_para(const char *args) free(file_and_args); } -/* - * shutdown curses and stat pipe before executing external commands - */ +/* Shutdown curses and stat pipe before executing external commands. */ static void exec_external(char *file_and_args) { int fds[3] = {-1, -1, -1}; @@ -1038,12 +1048,13 @@ static void exec_external(char *file_and_args) static void handle_command(int c) { int i; + const struct lls_opt_result *lor = OPT_RESULT(KEY_MAP); /* first check user-defined key bindings */ - for (i = 0; i < conf.key_map_given; ++i) { + FOR_EACH_KEY_MAP(i) { char *tmp, *handler, *arg; - tmp = para_strdup(conf.key_map_arg[i]); + tmp = para_strdup(lls_string_val(i, lor)); if (!split_key_map(tmp, &handler, &arg)) { free(tmp); return; @@ -1077,37 +1088,47 @@ static void handle_command(int c) km_keyname(c)); } -static int input_post_select(__a_unused struct sched *s, __a_unused struct task *t) +static int input_post_select(__a_unused struct sched *s, + __a_unused void *context) { int ret; enum exec_status exs = exec_status(); if (exs == EXEC_XCMD) return 0; + if (window_update_needed()) { + if (top.needs_update) + assert(wnoutrefresh(top.win) == OK); + if (bot.needs_update) + assert(wnoutrefresh(bot.win) == OK); + if (sep.needs_update) + assert(wnoutrefresh(sep.win) == OK); + if (sb.needs_update) + assert(wnoutrefresh(sb.win) == OK); + if (in.needs_update) + assert(wnoutrefresh(in.win) == OK); + doupdate(); + top.needs_update = bot.needs_update = sb.needs_update = + in.needs_update = sep.needs_update = false; + } ret = wgetch(top.win); - if (ret == ERR || ret == KEY_RESIZE) + if (ret == ERR) return 0; - if (exs == EXEC_IDLE) { - handle_command(ret); + if (ret == KEY_RESIZE) /* already handled in signal_post_select() */ return 0; - } - if (exec_pid != 0) + if (exs == EXEC_IDLE) + handle_command(ret); + else if (exec_pid > 0) kill(exec_pid, SIGTERM); return 0; } -static void signal_pre_select(struct sched *s, struct task *t) -{ - struct signal_task *st = container_of(t, struct signal_task, task); - para_fd_set(st->fd, &s->rfds, &s->max_fileno); -} - static void print_scroll_msg(void) { unsigned lines_total, filled = ringbuffer_filled(bot_win_rb); int first_rbe = first_visible_rbe(&lines_total); - print_in_bar(COLOR_MSG, "scrolled view: %d-%d/%d\n", filled - first_rbe, + print_in_bar(COLOR_MSG, "scrolled view: %u-%u/%u\n", filled - first_rbe, filled - scroll_position, ringbuffer_filled(bot_win_rb)); } @@ -1201,7 +1222,7 @@ static void com_scroll_down(void) wmove(bot.win, bot_lines - rbe_lines, 0); wattron(bot.win, COLOR_PAIR(rbe->color)); waddstr(bot.win, rbe->msg); - wrefresh(bot.win); + refresh_window(&bot); print_scroll_msg(); } @@ -1238,7 +1259,7 @@ static void com_scroll_up(void) break; i--; } - wrefresh(bot.win); + refresh_window(&bot); print_scroll_msg(); return; err_out: @@ -1275,9 +1296,10 @@ static void com_reread_conf(void) static void com_help(void) { int i; + const struct lls_opt_result *lor = OPT_RESULT(KEY_MAP); - for (i = 0; i < conf.key_map_given; ++i) { - char *handler, *arg, *tmp = para_strdup(conf.key_map_arg[i]); + FOR_EACH_KEY_MAP(i) { + char *handler, *arg, *tmp = para_strdup(lls_string_val(i, lor)); const char *handler_text = "???", *desc = NULL; if (!split_key_map(tmp, &handler, &arg)) { @@ -1361,77 +1383,121 @@ static void com_prev_theme(void) com_refresh(); } -__noreturn static void print_help_and_die(void) -{ - struct ggo_help h = DEFINE_GGO_HELP(gui); - bool d = conf.detailed_help_given; - - ggo_print_help(&h, d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS); - exit(0); -} - static int setup_tasks_and_schedule(void) { - struct sched sched = { - .default_timeout = { - .tv_sec = conf.timeout_arg / 1000, - .tv_usec = (conf.timeout_arg % 1000) * 1000, - }, - }; - struct exec_task exec_task = { - .task = { - .status = "exec", - .pre_select = exec_pre_select, - .post_select = exec_post_select, - }, - }; - struct status_task status_task = { - .task = { - .status = "status", - .pre_select = status_pre_select, - .post_select = status_post_select, - }, - .fd = -1 - }; - struct input_task input_task = { - .task = { - .status = "input", - .pre_select = input_pre_select, - .post_select = input_post_select, - }, - }; - struct signal_task signal_task = { - .task = { - .status = "signal", - .pre_select = signal_pre_select, - .post_select = signal_post_select, - }, - }; - signal_task.fd = para_signal_init(); + int ret; + struct exec_task exec_task = {.task = NULL}; + struct status_task status_task = {.fd = -1}; + struct input_task input_task = {.task = NULL}; + struct signal_task *signal_task; + struct sched sched = {.default_timeout = {.tv_sec = 1}}; + + exec_task.task = task_register(&(struct task_info) { + .name = "exec", + .pre_select = exec_pre_select, + .post_select = exec_post_select, + .context = &exec_task, + }, &sched); + + status_task.task = task_register(&(struct task_info) { + .name = "status", + .pre_select = status_pre_select, + .post_select = status_post_select, + .context = &status_task, + }, &sched); + + input_task.task = task_register(&(struct task_info) { + .name = "input", + .pre_select = input_pre_select, + .post_select = input_post_select, + .context = &input_task, + }, &sched); + + signal_task = signal_init_or_die(); para_install_sighandler(SIGINT); para_install_sighandler(SIGTERM); para_install_sighandler(SIGCHLD); - para_install_sighandler(SIGWINCH); para_install_sighandler(SIGUSR1); - - register_task(&sched, &exec_task.task); - register_task(&sched, &status_task.task); - register_task(&sched, &input_task.task); - register_task(&sched, &signal_task.task); - return schedule(&sched); + para_install_sighandler(SIGWINCH); + signal_task->task = task_register(&(struct task_info) { + .name = "signal", + .pre_select = signal_pre_select, + .post_select = signal_post_select, + .context = signal_task, + }, &sched); + ret = schedule(&sched); + sched_shutdown(&sched); + signal_shutdown(signal_task); + return ret; +} + +static void handle_help_flags(void) +{ + char *help; + + if (OPT_GIVEN(DETAILED_HELP)) + help = lls_long_help(CMD_PTR); + else if (OPT_GIVEN(HELP)) + help = lls_short_help(CMD_PTR); + else + return; + printf("%s\n", help); + free(help); + exit(EXIT_SUCCESS); } +/** + * The main function of para_gui. + * + * \param argc Usual argument count. + * \param argv Usual argument vector. + * + * After initialization para_gui registers the following tasks to the paraslash + * scheduler: status, exec, signal, input. + * + * The status task executes the para_audioc stat command to obtain the status + * of para_server and para_audiod, and displays this information in the top + * window of para_gui. + * + * The exec task is responsible for printing the output of the currently + * running executable to the bottom window. + * + * The signal task performs various actions according to signals received. For + * example, it reloads the configuration file on SIGUSR1, and it shuts down the + * curses system on SIGTERM to restore the terminal settings before exit. + * + * The input task reads single key strokes from stdin. For each key pressed, it + * executes the command handler associated with this key. + * + * \return \p EXIT_SUCCESS or \p EXIT_FAILURE. + */ int main(int argc, char *argv[]) { - gui_cmdline_parser(argc, argv, &conf); /* exits on errors */ - loglevel = get_loglevel_by_name(conf.loglevel_arg); - version_handle_flag("gui", conf.version_given); - if (conf.help_given || conf.detailed_help_given) - print_help_and_die(); - parse_config_file_or_die(false /* override */); + int ret; + char *errctx; + + ret = lls(lls_parse(argc, argv, CMD_PTR, &cmdline_lpr, &errctx)); + if (ret < 0) + goto out; + lpr = cmdline_lpr; + loglevel = OPT_UINT32_VAL(LOGLEVEL); + version_handle_flag("gui", OPT_GIVEN(VERSION)); + handle_help_flags(); + parse_config_file_or_die(false); bot_win_rb = ringbuffer_new(RINGBUFFER_SIZE); setlocale(LC_CTYPE, ""); initscr(); /* needed only once, always successful */ init_curses(); - return setup_tasks_and_schedule() < 0? EXIT_FAILURE : EXIT_SUCCESS; + ret = setup_tasks_and_schedule(); +out: + lls_free_parse_result(lpr, CMD_PTR); + if (lpr != cmdline_lpr) + lls_free_parse_result(cmdline_lpr, CMD_PTR); + if (ret < 0) { + if (errctx) + PARA_ERROR_LOG("%s\n", errctx); + free(errctx); + PARA_EMERG_LOG("%s\n", para_strerror(-ret)); + } + return ret < 0? EXIT_FAILURE : EXIT_SUCCESS; }