diff --git a/inc/lektor/commands.h b/inc/lektor/commands.h index 1cc95b662f09d14105422e3284922576e58fdca0..8741f6ef44dcd04945f5171c501c46c1dce48709 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 748916227b2fe9597a9e86c5fd5be187302ca837..50247177d2f3bdd9f10369a0a471b6beef431d6b 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 3c127f38508d4e297400ac8c814872678a36a7b4..1604dbbed4b539fb6133327265473ecd43a209f9 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 2b54a4d4e238300db2422b1b5fd42de49b87a11e..506adff264e926a82d67cbde470d410fd10aa834 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) +{ +}