+ result->matches = i9e_complete_commands(ci->word, pp_completers);
+}
+
+I9E_DUMMY_COMPLETER(SUPERCOMMAND_UNAVAILABLE);
+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(struct play_task *pt, 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(struct play_task *pt)
+{
+ btr_remove_node(&pt->btrn);
+}
+
+static int com_quit(struct play_task *pt,
+ __a_unused struct lls_parse_result *lpr)
+{
+ pt->rq = CRT_TERM_RQ;
+ return 0;
+}
+EXPORT_PLAY_CMD_HANDLER(quit);
+
+static int com_help(struct play_task *pt, struct lls_parse_result *lpr)
+{
+ int i, ret;
+ char *buf, *errctx;
+ size_t sz;
+ const struct lls_command *cmd;
+
+ 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;
+ }
+ if (lls_num_inputs(lpr) == 0) {
+ if (pt->background) {
+ for (i = 1; (cmd = lls_cmd(i, play_cmd_suite)); i++) {
+ sz = xasprintf(&buf, "%s\t%s\n",
+ lls_command_name(cmd), lls_purpose(cmd));
+ btr_add_output(buf, sz, pt->btrn);
+ }
+ return 0;
+ }
+ 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;
+ }
+ ret = lls(lls_lookup_subcmd(lls_input(0, lpr), play_cmd_suite,
+ &errctx));
+ if (ret < 0) {
+ if (errctx)
+ PARA_ERROR_LOG("%s\n", errctx);
+ free(errctx);
+ return ret;
+ }
+ cmd = lls_cmd(ret, play_cmd_suite);
+ buf = lls_long_help(cmd);
+ assert(buf);
+ btr_add_output(buf, strlen(buf), pt->btrn);
+ return 0;
+}
+EXPORT_PLAY_CMD_HANDLER(help);
+
+static int com_info(struct play_task *pt,
+ __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(struct play_task *pt, 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(struct play_task *pt,
+ __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(pt);
+ 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(struct play_task *pt,
+ __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(pt, i);
+ return 0;
+}
+EXPORT_PLAY_CMD_HANDLER(ls);
+
+static int com_play(struct play_task *pt, 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(pt);
+ 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);
+ pt->next_file = x;
+ pt->rq = CRT_FILE_CHANGE;
+ return 0;
+}
+EXPORT_PLAY_CMD_HANDLER(play);
+
+static int com_pause(struct play_task *pt,
+ __a_unused struct lls_parse_result *lpr)
+{
+ char state;
+ long unsigned seconds, ss;
+
+ state = get_playback_state(pt);
+ pt->playing = false;
+ if (state != 'P')
+ return 0;
+ seconds = get_play_time(pt);
+ 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(pt);
+ return 0;
+}
+EXPORT_PLAY_CMD_HANDLER(pause);
+
+static int com_prev(struct play_task *pt,
+ __a_unused struct lls_parse_result *lpr)
+{
+ int ret;
+
+ ret = previous_valid_file(pt);
+ if (ret < 0)
+ return ret;
+ kill_stream(pt);
+ pt->next_file = ret;
+ pt->rq = CRT_FILE_CHANGE;
+ pt->start_chunk = 0;
+ return 0;
+}
+EXPORT_PLAY_CMD_HANDLER(prev);
+
+static int com_next(struct play_task *pt,
+ __a_unused struct lls_parse_result *lpr)
+{
+ int ret;
+
+ ret = next_valid_file(pt);
+ if (ret < 0)
+ return ret;
+ kill_stream(pt);
+ pt->next_file = ret;
+ pt->rq = CRT_FILE_CHANGE;
+ pt->start_chunk = 0;
+ return 0;
+}
+EXPORT_PLAY_CMD_HANDLER(next);
+
+static int com_fg(struct play_task *pt,
+ __a_unused struct lls_parse_result *lpr)
+{
+ pt->background = false;
+ return 0;
+}
+EXPORT_PLAY_CMD_HANDLER(fg);
+
+static int com_bg(struct play_task *pt,
+ __a_unused struct lls_parse_result *lpr)
+{
+ pt->background = true;
+ return 0;
+}
+EXPORT_PLAY_CMD_HANDLER(bg);
+
+static int com_jmp(struct play_task *pt, 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(pt, 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(pt);
+ return 0;
+}
+EXPORT_PLAY_CMD_HANDLER(jmp);