+ if (!pt->playing)
+ return 0;
+ /* set up decoding filter */
+ af = audio_format_name(pt->audio_format_num);
+ tmp = make_message("%sdec", af);
+ ret = filter_setup(tmp, &pt->fn.conf, &filter_lpr);
+ freep(&tmp);
+ if (ret < 0)
+ goto fail;
+ pt->fn.filter_num = ret;
+ pt->fn.lpr = filter_lpr;
+ decoder = filter_get(ret);
+ pt->fn.btrn = btr_new_node(&(struct btr_node_description)
+ EMBRACE(.name = filter_name(pt->fn.filter_num),
+ .parent = pt->rn.btrn, .handler = decoder->execute,
+ .context = &pt->fn));
+ if (decoder->open)
+ decoder->open(&pt->fn);
+ PARA_INFO_LOG("buffer tree:\n");
+ btr_log_tree(pt->rn.btrn, LL_INFO);
+
+ /* setup default writer */
+ pt->wn.wid = check_writer_arg_or_die(NULL, &writer_lpr);
+ pt->wn.lpr = writer_lpr;
+ /* success, register tasks */
+ pt->rn.task = task_register(
+ &(struct task_info) {
+ .name = lls_command_name(AFH_RECV_CMD),
+ .pre_select = AFH_RECV->pre_select,
+ .post_select = AFH_RECV->post_select,
+ .context = &pt->rn
+ }, &sched);
+ sprintf(buf, "%s decoder", af);
+ pt->fn.task = task_register(
+ &(struct task_info) {
+ .name = buf,
+ .pre_select = decoder->pre_select,
+ .post_select = decoder->post_select,
+ .context = &pt->fn
+ }, &sched);
+ register_writer_node(&pt->wn, pt->fn.btrn, &sched);
+ return 1;
+fail:
+ wipe_receiver_node();
+ return ret;
+}
+
+static int next_valid_file(void)
+{
+ int i, j = pt->current_file;
+ unsigned num_inputs = lls_num_inputs(play_lpr);
+
+ for (i = 0; i < num_inputs; i++) {
+ j = (j + 1) % num_inputs;
+ if (!pt->invalid[j])
+ return j;
+ }
+ return -E_NO_VALID_FILES;
+}
+
+static int load_next_file(void)
+{
+ int ret;
+
+again:
+ if (pt->rq == CRT_NONE) {
+ pt->start_chunk = 0;
+ ret = next_valid_file();
+ if (ret < 0)
+ return ret;
+ pt->next_file = ret;
+ } else if (pt->rq == CRT_REPOS)
+ pt->next_file = pt->current_file;
+ ret = load_file();
+ if (ret < 0) {
+ PARA_ERROR_LOG("%s: marking file as invalid\n",
+ para_strerror(-ret));
+ pt->invalid[pt->next_file] = true;
+ pt->rq = CRT_NONE;
+ goto again;
+ }
+ pt->current_file = pt->next_file;
+ pt->rq = CRT_NONE;
+ return ret;
+}
+
+static void kill_stream(void)
+{
+ if (pt->wn.task)
+ task_notify(pt->wn.task, E_EOF);
+}
+
+#ifdef HAVE_READLINE
+
+/* only called from com_prev(), nec. only if we have readline */
+static int previous_valid_file(void)
+{
+ int i, j = pt->current_file;
+ unsigned num_inputs = lls_num_inputs(play_lpr);
+
+ for (i = 0; i < num_inputs; i++) {
+ j--;
+ if (j < 0)
+ j = num_inputs - 1;
+ if (!pt->invalid[j])
+ return j;
+ }
+ return -E_NO_VALID_FILES;
+}
+
+#include "interactive.h"
+
+/*
+ * Define the default (internal) key mappings and helper functions to get the
+ * key sequence or the command from a key id, which is what we obtain from
+ * i9e/readline when the key is pressed.
+ *
+ * In some of these helper functions we could return pointers to the constant
+ * arrays defined below. However, for others we can not, so let's better be
+ * consistent and allocate all returned strings on the heap.
+ */
+
+#define INTERNAL_KEYMAP_ENTRIES \
+ KEYMAP_ENTRY("^", "jmp 0"), \
+ KEYMAP_ENTRY("1", "jmp 10"), \
+ KEYMAP_ENTRY("2", "jmp 21"), \
+ KEYMAP_ENTRY("3", "jmp 32"), \
+ KEYMAP_ENTRY("4", "jmp 43"), \
+ KEYMAP_ENTRY("5", "jmp 54"), \
+ KEYMAP_ENTRY("6", "jmp 65"), \
+ KEYMAP_ENTRY("7", "jmp 76"), \
+ KEYMAP_ENTRY("8", "jmp 87"), \
+ KEYMAP_ENTRY("9", "jmp 98"), \
+ KEYMAP_ENTRY("+", "next"), \
+ KEYMAP_ENTRY("-", "prev"), \
+ KEYMAP_ENTRY(":", "bg"), \
+ KEYMAP_ENTRY("i", "info"), \
+ KEYMAP_ENTRY("l", "ls"), \
+ KEYMAP_ENTRY("s", "play"), \
+ KEYMAP_ENTRY("p", "pause"), \
+ KEYMAP_ENTRY("q", "quit"), \
+ KEYMAP_ENTRY("?", "help"), \
+ KEYMAP_ENTRY("\033[D", "ff -10"), \
+ KEYMAP_ENTRY("\033[C", "ff 10"), \
+ KEYMAP_ENTRY("\033[A", "ff 60"), \
+ KEYMAP_ENTRY("\033[B", "ff -60"), \
+
+#define KEYMAP_ENTRY(a, b) a
+static const char *default_keyseqs[] = {INTERNAL_KEYMAP_ENTRIES};
+#undef KEYMAP_ENTRY
+#define KEYMAP_ENTRY(a, b) b
+static const char *default_commands[] = {INTERNAL_KEYMAP_ENTRIES};
+#undef KEYMAP_ENTRY
+#define NUM_INTERNALLY_MAPPED_KEYS ARRAY_SIZE(default_commands)
+#define NUM_MAPPED_KEYS (NUM_INTERNALLY_MAPPED_KEYS + OPT_GIVEN(KEY_MAP))
+#define FOR_EACH_MAPPED_KEY(i) for (i = 0; i < NUM_MAPPED_KEYS; i++)
+
+static inline bool is_internal_key(int key)
+{
+ return key < NUM_INTERNALLY_MAPPED_KEYS;
+}
+
+/* for internal keys, the key id is just the array index. */
+static inline int get_internal_key_map_idx(int key)
+{
+ assert(is_internal_key(key));
+ return key;
+}
+
+/*
+ * For user-defined keys, we have to subtract NUM_INTERNALLY_MAPPED_KEYS. The
+ * difference is the index to the array of user defined key maps.
+ */
+static inline int get_user_key_map_idx(int key)
+{
+ assert(!is_internal_key(key));
+ return key - NUM_INTERNALLY_MAPPED_KEYS;
+}
+
+static inline int get_key_map_idx(int key)
+{
+ return is_internal_key(key)?
+ get_internal_key_map_idx(key) : get_user_key_map_idx(key);
+}
+
+static inline const char *get_user_key_map_arg(int key)
+{
+ return lls_string_val(get_user_key_map_idx(key), OPT_RESULT(KEY_MAP));
+}
+
+static inline char *get_internal_key_map_seq(int key)
+{
+ return para_strdup(default_keyseqs[get_internal_key_map_idx(key)]);
+}
+
+static char *get_user_key_map_seq(int key)
+{
+ const char *kma = get_user_key_map_arg(key);
+ const char *p = strchr(kma + 1, ':');
+ char *result;
+ int len;
+
+ if (!p)
+ return NULL;
+ len = p - kma;
+ result = para_malloc(len + 1);
+ memcpy(result, kma, len);
+ result[len] = '\0';
+ return result;
+}
+
+static char *get_key_map_seq(int key)
+{
+ return is_internal_key(key)?
+ get_internal_key_map_seq(key) : get_user_key_map_seq(key);
+}
+
+static char *get_key_map_seq_safe(int key)
+{
+ const char hex[] = "0123456789abcdef";
+ char *seq = get_key_map_seq(key), *sseq;
+ size_t n, len = strlen(seq);
+
+ if (len == 1 && isprint(*seq))
+ return seq;
+ sseq = para_malloc(2 + 2 * len + 1);
+ sseq[0] = '0';
+ sseq[1] = 'x';
+ for (n = 0; n < len; n++) {
+ uint8_t val = (seq[n] & 0xf0) >> 4;
+ sseq[2 + 2 * n] = hex[val];
+ val = seq[n] & 0xf;
+ sseq[2 + 2 * n + 1] = hex[val];
+ }
+ free(seq);
+ sseq[2 + 2 * n] = '\0';
+ return sseq;
+}
+
+static inline char *get_internal_key_map_cmd(int key)
+{
+ return para_strdup(default_commands[get_internal_key_map_idx(key)]);
+}
+
+static char *get_user_key_map_cmd(int key)
+{
+ const char *kma = get_user_key_map_arg(key);
+ const char *p = strchr(kma + 1, ':');
+
+ if (!p)
+ return NULL;
+ return para_strdup(p + 1);
+}
+
+static char *get_key_map_cmd(int key)
+{
+ return is_internal_key(key)?
+ get_internal_key_map_cmd(key) : get_user_key_map_cmd(key);
+}
+
+static char **get_mapped_keyseqs(void)
+{
+ char **result;
+ int i;
+
+ result = para_malloc((NUM_MAPPED_KEYS + 1) * sizeof(char *));
+ FOR_EACH_MAPPED_KEY(i) {
+ char *seq = get_key_map_seq(i);
+ result[i] = seq;
+ }
+ result[i] = NULL;
+ return result;
+}
+
+static struct i9e_completer pp_completers[];
+
+I9E_DUMMY_COMPLETER(jmp);
+I9E_DUMMY_COMPLETER(next);
+I9E_DUMMY_COMPLETER(prev);
+I9E_DUMMY_COMPLETER(fg);
+I9E_DUMMY_COMPLETER(bg);
+I9E_DUMMY_COMPLETER(ls);
+I9E_DUMMY_COMPLETER(info);
+I9E_DUMMY_COMPLETER(play);
+I9E_DUMMY_COMPLETER(pause);
+I9E_DUMMY_COMPLETER(tasks);
+I9E_DUMMY_COMPLETER(quit);
+I9E_DUMMY_COMPLETER(ff);
+
+static void help_completer(struct i9e_completion_info *ci,
+ struct i9e_completion_result *cr)
+{
+ char *opts[] = {LSG_PLAY_CMD_HELP_OPTS, NULL};
+
+ if (ci->word[0] == '-') {
+ i9e_complete_option(opts, ci, cr);
+ return;
+ }
+ cr->matches = i9e_complete_commands(ci->word, pp_completers);
+}
+
+static struct i9e_completer pp_completers[] = {
+#define LSG_PLAY_CMD_CMD(_name) {.name = #_name, \
+ .completer = _name ## _completer}
+ LSG_PLAY_CMD_SUBCOMMANDS
+#undef LSG_PLAY_CMD_CMD
+ {.name = NULL}
+};
+
+static void attach_stdout(const char *name)
+{
+ if (pt->btrn)
+ return;
+ pt->btrn = btr_new_node(&(struct btr_node_description)
+ EMBRACE(.name = name));
+ i9e_attach_to_stdout(pt->btrn);
+}
+
+static void detach_stdout(void)
+{
+ btr_remove_node(&pt->btrn);
+}
+
+static int com_quit(__a_unused struct lls_parse_result *lpr)
+{
+ pt->rq = CRT_TERM_RQ;
+ return 0;
+}
+EXPORT_PLAY_CMD_HANDLER(quit);
+
+static int com_help(struct lls_parse_result *lpr)
+{
+ int i;
+ char *buf;
+ size_t sz;
+ unsigned n;
+ const struct lls_opt_result *r =
+ lls_opt_result(LSG_PLAY_CMD_HELP_OPT_LONG, lpr);
+ bool long_help = lls_opt_given(r);
+
+ if (!pt->background) {
+ FOR_EACH_MAPPED_KEY(i) {
+ bool internal = is_internal_key(i);
+ int idx = get_key_map_idx(i);
+ char *seq = get_key_map_seq_safe(i);
+ char *kmc = get_key_map_cmd(i);
+ sz = xasprintf(&buf, "%s key #%d: %s -> %s\n",
+ internal? "internal" : "user-defined",
+ idx, seq, kmc);
+ btr_add_output(buf, sz, pt->btrn);
+ free(seq);
+ free(kmc);
+ }
+ return 0;
+ }
+ lsu_com_help(long_help, lpr, play_cmd_suite, NULL, &buf, &n);
+ btr_add_output(buf, n, pt->btrn);
+ return 0;
+}
+EXPORT_PLAY_CMD_HANDLER(help);
+
+static int com_info(__a_unused struct lls_parse_result *lpr)
+{
+ char *buf;
+ size_t sz;
+ static char dflt[] = "[no information available]";
+
+ sz = xasprintf(&buf, "playlist_pos: %u\npath: %s\n",
+ pt->current_file, get_playlist_file(pt->current_file));
+ btr_add_output(buf, sz, pt->btrn);
+ buf = pt->afhi_txt? pt->afhi_txt : dflt;
+ btr_add_output_dont_free(buf, strlen(buf), pt->btrn);
+ return 0;
+}
+EXPORT_PLAY_CMD_HANDLER(info);
+
+static void list_file(int num)
+{
+ char *buf;
+ size_t sz;
+
+ sz = xasprintf(&buf, "%s %4d %s\n", num == pt->current_file?
+ "*" : " ", num, get_playlist_file(num));
+ btr_add_output(buf, sz, pt->btrn);
+}
+
+static int com_tasks(__a_unused struct lls_parse_result *lpr)
+{
+ static char state;
+ char *buf;
+ size_t sz;
+
+ buf = get_task_list(&sched);
+ btr_add_output(buf, strlen(buf), pt->btrn);
+ state = get_playback_state();
+ sz = xasprintf(&buf, "state: %c\n", state);
+ btr_add_output(buf, sz, pt->btrn);
+ return 0;
+}
+EXPORT_PLAY_CMD_HANDLER(tasks);
+
+static int com_ls(__a_unused struct lls_parse_result *lpr)
+{
+ int i;
+ unsigned num_inputs = lls_num_inputs(play_lpr);
+
+ for (i = 0; i < num_inputs; i++)
+ list_file(i);
+ return 0;
+}
+EXPORT_PLAY_CMD_HANDLER(ls);
+
+static int com_play(struct lls_parse_result *lpr)
+{
+ int32_t x;
+ int ret;
+ char state, *errctx;
+
+ ret = lls(lls_check_arg_count(lpr, 0, 1, &errctx));
+ if (ret < 0) {
+ if (errctx)
+ PARA_ERROR_LOG("%s\n", errctx);
+ free(errctx);
+ return ret;
+ }
+ state = get_playback_state();
+ if (lls_num_inputs(lpr) == 0) {
+ if (state == 'P')
+ return 0;
+ pt->next_file = pt->current_file;
+ pt->rq = CRT_REPOS;
+ pt->playing = true;
+ return 0;
+ }
+ ret = para_atoi32(lls_input(0, lpr), &x);
+ if (ret < 0)
+ return ret;
+ if (x < 0 || x >= lls_num_inputs(play_lpr))
+ return -ERRNO_TO_PARA_ERROR(EINVAL);
+ kill_stream();
+ pt->next_file = x;
+ pt->rq = CRT_FILE_CHANGE;
+ return 0;
+}
+EXPORT_PLAY_CMD_HANDLER(play);
+
+static int com_pause(__a_unused struct lls_parse_result *lpr)
+{
+ char state;
+ long unsigned seconds, ss;
+
+ state = get_playback_state();
+ pt->playing = false;
+ if (state != 'P')
+ return 0;
+ seconds = get_play_time();
+ pt->playing = false;
+ ss = 0;
+ if (pt->seconds > 0)
+ ss = seconds * pt->num_chunks / pt->seconds + 1;
+ ss = PARA_MAX(ss, 0UL);
+ ss = PARA_MIN(ss, pt->num_chunks);
+ pt->start_chunk = ss;
+ kill_stream();
+ return 0;
+}
+EXPORT_PLAY_CMD_HANDLER(pause);
+
+static int com_prev(__a_unused struct lls_parse_result *lpr)
+{
+ int ret;
+
+ ret = previous_valid_file();
+ if (ret < 0)
+ return ret;
+ kill_stream();
+ pt->next_file = ret;
+ pt->rq = CRT_FILE_CHANGE;
+ pt->start_chunk = 0;
+ return 0;
+}
+EXPORT_PLAY_CMD_HANDLER(prev);
+
+static int com_next(__a_unused struct lls_parse_result *lpr)
+{
+ int ret;
+
+ ret = next_valid_file();
+ if (ret < 0)
+ return ret;
+ kill_stream();
+ pt->next_file = ret;
+ pt->rq = CRT_FILE_CHANGE;
+ pt->start_chunk = 0;
+ return 0;
+}
+EXPORT_PLAY_CMD_HANDLER(next);
+
+static int com_fg(__a_unused struct lls_parse_result *lpr)
+{
+ pt->background = false;
+ return 0;
+}
+EXPORT_PLAY_CMD_HANDLER(fg);
+
+static int com_bg(__a_unused struct lls_parse_result *lpr)
+{
+ pt->background = true;
+ return 0;
+}
+EXPORT_PLAY_CMD_HANDLER(bg);
+
+static int com_jmp(struct lls_parse_result *lpr)
+{
+ int32_t percent;
+ int ret;
+ char *errctx;
+
+ ret = lls(lls_check_arg_count(lpr, 1, 1, &errctx));
+ if (ret < 0) {
+ if (errctx)
+ PARA_ERROR_LOG("%s\n", errctx);
+ free(errctx);
+ return ret;
+ }
+ ret = para_atoi32(lls_input(0, lpr), &percent);
+ if (ret < 0)
+ return ret;
+ if (percent < 0 || percent > 100)
+ return -ERRNO_TO_PARA_ERROR(EINVAL);
+ if (percent == 100)
+ return com_next(NULL);
+ if (pt->playing && !pt->fn.btrn)
+ return 0;
+ pt->start_chunk = percent * pt->num_chunks / 100;
+ if (!pt->playing)
+ return 0;
+ pt->rq = CRT_REPOS;
+ kill_stream();
+ return 0;
+}
+EXPORT_PLAY_CMD_HANDLER(jmp);
+
+static int com_ff(struct lls_parse_result *lpr)
+{
+ int32_t seconds;
+ char *errctx;
+ int ret;
+
+ ret = lls(lls_check_arg_count(lpr, 1, 1, &errctx));
+ if (ret < 0) {
+ if (errctx)
+ PARA_ERROR_LOG("%s\n", errctx);
+ free(errctx);
+ return ret;
+ }
+ ret = para_atoi32(lls_input(0, lpr), &seconds);
+ if (ret < 0)
+ return ret;
+ if (pt->playing && !pt->fn.btrn)
+ return 0;
+ seconds += get_play_time();
+ seconds = PARA_MIN(seconds, (typeof(seconds))pt->seconds - 4);
+ seconds = PARA_MAX(seconds, 0);
+ pt->start_chunk = pt->num_chunks * seconds / pt->seconds;
+ pt->start_chunk = PARA_MIN(pt->start_chunk, pt->num_chunks - 1);
+ pt->start_chunk = PARA_MAX(pt->start_chunk, 0UL);
+ if (!pt->playing)
+ return 0;
+ pt->rq = CRT_REPOS;
+ kill_stream();
+ return 0;
+}
+EXPORT_PLAY_CMD_HANDLER(ff);
+
+static int run_command(char *line)
+{
+ int ret, argc;
+ char **argv = NULL;
+ char *errctx = NULL;
+ const struct play_command_info *pci;
+ struct lls_parse_result *lpr;
+ const struct lls_command *cmd;
+
+ attach_stdout(__FUNCTION__);
+ ret = create_argv(line, " ", &argv);
+ if (ret < 0)
+ goto out;
+ if (ret == 0)
+ goto out;
+ argc = ret;
+ ret = lls(lls_lookup_subcmd(argv[0], play_cmd_suite, &errctx));
+ if (ret < 0)
+ goto out;
+ cmd = lls_cmd(ret, play_cmd_suite);
+ ret = lls(lls_parse(argc, argv, cmd, &lpr, &errctx));
+ if (ret < 0)
+ goto out;
+ pci = lls_user_data(cmd);
+ ret = pci->handler(lpr);
+ lls_free_parse_result(lpr, cmd);
+out:
+ if (errctx)
+ PARA_ERROR_LOG("%s\n", errctx);
+ free(errctx);
+ free_argv(argv);
+ return ret;
+}
+
+static int play_i9e_line_handler(char *line)
+{
+ return run_command(line);
+}
+
+static int play_i9e_key_handler(int key)
+{
+ int idx = get_key_map_idx(key);
+ char *seq = get_key_map_seq(key);
+ char *cmd = get_key_map_cmd(key);
+ bool internal = is_internal_key(key);
+
+ PARA_NOTICE_LOG("pressed %d: %s key #%d (%s -> %s)\n",
+ key, internal? "internal" : "user-defined",
+ idx, seq, cmd);
+ run_command(cmd);
+ free(seq);
+ free(cmd);
+ pt->next_update = *now;
+ return 0;
+}
+
+static struct i9e_client_info ici = {
+ .fds = {0, 1, 2},
+ .prompt = "para_play> ",
+ .line_handler = play_i9e_line_handler,
+ .key_handler = play_i9e_key_handler,
+ .completers = pp_completers,
+};
+
+static void sigint_handler(int sig)
+{
+ pt->background = true;
+ i9e_signal_dispatch(sig);
+}
+
+/*
+ * We start with para_log() set to the standard log function which writes to
+ * stderr. Once the i9e subsystem has been initialized, we switch to the i9e
+ * log facility.
+ */
+static void session_open(void)
+{
+ int ret;
+ char *history_file;
+ struct sigaction act;
+
+ PARA_NOTICE_LOG("\n%s\n", version_text("play"));
+ if (OPT_GIVEN(HISTORY_FILE))
+ history_file = para_strdup(OPT_STRING_VAL(HISTORY_FILE));
+ else {
+ char *home = para_homedir();
+ history_file = make_message("%s/.paraslash/play.history",
+ home);
+ free(home);
+ }
+ ici.history_file = history_file;
+ ici.loglevel = loglevel;
+
+ act.sa_handler = sigint_handler;
+ sigemptyset(&act.sa_mask);
+ act.sa_flags = 0;
+ sigaction(SIGINT, &act, NULL);
+ act.sa_handler = i9e_signal_dispatch;
+ sigemptyset(&act.sa_mask);
+ act.sa_flags = 0;
+ sigaction(SIGWINCH, &act, NULL);
+ sched.select_function = i9e_select;
+
+ ici.bound_keyseqs = get_mapped_keyseqs();
+ pt->btrn = ici.producer = btr_new_node(&(struct btr_node_description)
+ EMBRACE(.name = __FUNCTION__));
+ ret = i9e_open(&ici, &sched);
+ if (ret < 0)
+ goto out;
+ para_log = i9e_log;