+ 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;
+ return;
+out:
+ free(history_file);
+ if (ret >= 0)
+ return;
+ PARA_EMERG_LOG("fatal: %s\n", para_strerror(-ret));
+ exit(EXIT_FAILURE);
+}
+
+static void session_update_time_string(char *str, unsigned len)
+{
+ if (pt->background)
+ return;
+ if (pt->btrn) {
+ if (btr_get_output_queue_size(pt->btrn) > 0)
+ return;
+ if (btr_get_input_queue_size(pt->btrn) > 0)
+ return;