From 4428cd68c28f32c92159b41fab6973053f51b036 Mon Sep 17 00:00:00 2001 From: Kubat <mael.martin31@gmail.com> Date: Fri, 17 Apr 2020 13:55:43 +0200 Subject: [PATCH] WIP: Command seekid, but no database function --- inc/lektor/commands.h | 1 + inc/lektor/database.h | 1 + src/commands.c | 73 +++++++++++++++++++++++++++++++++++++------ src/database/queue.c | 5 +++ 4 files changed, 71 insertions(+), 9 deletions(-) diff --git a/inc/lektor/commands.h b/inc/lektor/commands.h index 1cc95b66..8741f6ef 100644 --- a/inc/lektor/commands.h +++ b/inc/lektor/commands.h @@ -39,6 +39,7 @@ bool command_crop(sqlite3 *db, enum mpd_idle_flag *watch_mask_ptr); bool command_move(sqlite3 *db, char *args[LKT_MESSAGE_ARGS_MAX], enum mpd_idle_flag *watch_mask_ptr); bool command_shuffle(sqlite3 *db, enum mpd_idle_flag *watch_mask_ptr); bool command_queue_list(struct lkt_state *srv, size_t c, char *args[LKT_MESSAGE_ARGS_MAX]); +bool command_playid(sqlite3 *db, struct lkt_win *win, char *args[LKT_MESSAGE_ARGS_MAX], enum mpd_idle_flag *watch_mask_ptr); /* The playlists */ bool command_plt_create(sqlite3 *db, char *args[LKT_MESSAGE_ARGS_MAX], enum mpd_idle_flag *watch_mask_ptr); diff --git a/inc/lektor/database.h b/inc/lektor/database.h index 74891622..50247177 100644 --- a/inc/lektor/database.h +++ b/inc/lektor/database.h @@ -67,6 +67,7 @@ bool database_queue_clear(sqlite3 *db); bool database_queue_crop(sqlite3 *db); bool database_queue_move(sqlite3 *db, int from, int to); bool database_queue_shuffle(sqlite3 *db); +bool database_queue_seekid(sqlite3 *db, int id, int *out_pos); /* Control the playing state of the queue. */ bool database_queue_toggle_pause(sqlite3 *db); diff --git a/src/commands.c b/src/commands.c index 3c127f38..1604dbbe 100644 --- a/src/commands.c +++ b/src/commands.c @@ -182,12 +182,36 @@ command_previous(sqlite3 *db, struct lkt_win *win, enum mpd_idle_flag *watch_mas return res; } +bool +__play_that_file(sqlite3 *db, struct lkt_win *win, int pos) +{ + char filepath[PATH_MAX]; + + if (!database_queue_play(db, (int) pos)) { + fprintf(stderr, " ! __play_that_file: command failed because of db interactions\n"); + goto error; + } + + if (!database_queue_get_current_file(db, filepath)) { + fprintf(stderr, " ! __play_that_file: command failed because of get current\n"); + goto error; + } + + if (!win->load_file(win, filepath)) { + fprintf(stderr, " ! __play_that_file: command failed because of get current\n"); + goto error; + } + +error: + return false; +} + bool command_play(sqlite3 *db, struct lkt_win *win, char *args[LKT_MESSAGE_ARGS_MAX], enum mpd_idle_flag *watch_mask_ptr) { *watch_mask_ptr |= MPD_IDLE_PLAYER; - char filepath[PATH_MAX], *endptr; + char *endptr; long pos; /* Argument handle. */ @@ -219,26 +243,57 @@ command_play(sqlite3 *db, struct lkt_win *win, char *args[LKT_MESSAGE_ARGS_MAX], goto error; } - if (!database_queue_play(db, (int) pos)) { - fprintf(stderr, " ! command_play: command failed because of db interactions\n"); + if (!__play_that_file(db, win, pos)) goto error; - } - if (!database_queue_get_current_file(db, filepath)) { - fprintf(stderr, " ! command_play: command failed because of get current\n"); + return true; +error: + return false; +} + +bool +command_playid(sqlite3 *db, struct lkt_win *win, char *args[LKT_MESSAGE_ARGS_MAX], enum mpd_idle_flag *watch_mask_ptr) +{ + *watch_mask_ptr |= MPD_IDLE_PLAYER; + char *endptr; + int pos; + long id; + + /* Argument handle. */ + + if (args[0] == NULL) + goto error; + + id = strtol(args[0], &endptr, 10); + + if ((errno == ERANGE && (id == LONG_MAX || id == LONG_MIN)) + || (errno != 0 && id == 0)) { + fprintf(stderr, " ! command_playid: strtol failed: %s\n", strerror(errno)); goto error; } - if (!win->load_file(win, filepath)) { - fprintf(stderr, " ! command_play: command failed because of get current\n"); + if (endptr == args[0]) { + fprintf(stderr, " . command_playid: to digit found in string '%s'\n", args[1]); goto error; } - return true; + /* Do the work. */ + + if (!win->window && !win->new(win)) + goto error; + + if (!database_queue_seekid(db, (int) id, &pos)) + goto error; + + if (!__play_that_file(db, win, pos)) + goto error; + error: + fprintf(stderr, " ! command_playid: Failed\n"); return false; } + bool command_stop(sqlite3 *db, struct lkt_win *win, enum mpd_idle_flag *watch_mask_ptr) { diff --git a/src/database/queue.c b/src/database/queue.c index 2b54a4d4..506adff2 100644 --- a/src/database/queue.c +++ b/src/database/queue.c @@ -721,3 +721,8 @@ error: sqlite3_finalize(stmt); return ret; } + +bool +database_queue_seekid(sqlite3 *db, int id, int *out_pos) +{ +} -- GitLab