diff --git a/inc/lektor/commands.h b/inc/lektor/commands.h index e5b45e4e2d9d576fd0b19dfd3e3555f64d84e092..8cc92516d0af1337e610056db5ff4f0ea42052ef 100644 --- a/inc/lektor/commands.h +++ b/inc/lektor/commands.h @@ -44,6 +44,7 @@ bool command_move (struct lkt_state *srv, char *args[LKT_MESSAGE_ARGS_MAX]); bool command_shuffle(struct lkt_state *srv, char *args[LKT_MESSAGE_ARGS_MAX]); bool command_dump (struct lkt_state *srv, char *args[LKT_MESSAGE_ARGS_MAX]); bool command_swap (struct lkt_state *srv, char *args[LKT_MESSAGE_ARGS_MAX]); +bool command_swapid (struct lkt_state *srv, char *args[LKT_MESSAGE_ARGS_MAX]); bool command_flat (struct lkt_state *srv, char *args[LKT_MESSAGE_ARGS_MAX]); bool command_queue_list (struct lkt_state *srv, size_t c, char *args[LKT_MESSAGE_ARGS_MAX]); diff --git a/inc/lektor/database.h b/inc/lektor/database.h index 68b93cc5d84b098ecd0e07b0b157e9dba47d005e..be8bcf264a877a375975bf471eb23258fa28f93d 100644 --- a/inc/lektor/database.h +++ b/inc/lektor/database.h @@ -87,7 +87,7 @@ struct lkt_callback { /* List the content of the queue */ bool database_queue_list(volatile sqlite3 *db, size_t from, size_t to, struct lkt_callback *callback); -bool database_queue_probe_id(volatile sqlite3 *db, int id); +int database_queue_probe_id(volatile sqlite3 *db, int id); /* Search the database */ struct lkt_search { diff --git a/man/lkt.template b/man/lkt.template index 07ff2e42353a84e0ac9ba3a10ab25c3f8daf7c0f..7c89fdd665d397666f747c185578b8a84ae09a65 100644 --- a/man/lkt.template +++ b/man/lkt.template @@ -38,8 +38,7 @@ specified index. For lektord to be paused, don't do anything is lektord is already paused. .TP \fBunpause\fP -Unpause lektord, don't do anything if lektord is already playing or if it is -stopped. In that case use the \fBplay\fP command. +Unpause lektord, starts at the begening of the queue if lektord was stopped. .TP \fBnext\fP Play next kara in the queue. diff --git a/src/base/commands.c b/src/base/commands.c index f10afa7edb84782a57f003c0b4f63f366c6e03a1..745485c79ee7034c264f38d7a06e8628a4f1307c 100644 --- a/src/base/commands.c +++ b/src/base/commands.c @@ -293,9 +293,10 @@ __play_that_file(struct lkt_state *win, int pos) { char filepath[PATH_MAX]; RETURN_UNLESS(pos, "Invalid argument", false); - RETURN_UNLESS(database_queue_play(win->db, (int) pos), "DB error", false); + RETURN_UNLESS(database_queue_play(win->db, pos), "DB error", false); RETURN_UNLESS(database_queue_get_current_file(win->db, filepath), "Can't get current kara", false); RETURN_IF(MOD_CALL(win->window_mod, "load", filepath), "Can't load file", false); + LOG_DEBUG("COMMAND", "Playing queue at index %d, file %s", pos, filepath); return true; } @@ -513,26 +514,14 @@ command_move(struct lkt_state UNUSED *srv, char *args[LKT_MESSAGE_ARGS_MAX]) return false; } -bool -command_swap(struct lkt_state *srv, char *args[LKT_MESSAGE_ARGS_MAX]) +static bool +__swap_positions(struct lkt_state *srv, int from, int to) { - long from, to; - char *endptr = NULL, err = 0; - - RETURN_UNLESS(args && args[0] && args[1], "Invalid argument", false); - - /* First argument: from */ - STRTOL(from, args[0], endptr, err); - RETURN_IF(err, "STRTOL failed", false); - - /* Second argument: to */ - STRTOL(to, args[1], endptr, err); - RETURN_IF(err, "STRTOL failed", false); + RETURN_IF(to == from, "Can't swap with two identic positions", false); struct lkt_queue_state queue = { .current = -1 }; database_queue_state(srv->db, &queue); - int new_current = (((long) queue.current) == to) ? from : (((long) queue.current) == from) ? to : -1; @@ -552,6 +541,34 @@ command_swap(struct lkt_state *srv, char *args[LKT_MESSAGE_ARGS_MAX]) return sta; } +bool +command_swapid(struct lkt_state *srv, char *args[LKT_MESSAGE_ARGS_MAX]) +{ + RETURN_UNLESS(args && args[0] && args[1], "Invalid argument", false); + int from = database_queue_probe_id(srv->db, strtol(args[0], NULL, 0)); + int to = database_queue_probe_id(srv->db, strtol(args[1], NULL, 0)); + return __swap_positions(srv, from, to); +} + +bool +command_swap(struct lkt_state *srv, char *args[LKT_MESSAGE_ARGS_MAX]) +{ + long from, to; + char *endptr = NULL, err = 0; + + RETURN_UNLESS(args && args[0] && args[1], "Invalid argument", false); + + /* First argument: from */ + STRTOL(from, args[0], endptr, err); + RETURN_IF(err, "STRTOL failed", false); + + /* Second argument: to */ + STRTOL(to, args[1], endptr, err); + RETURN_IF(err, "STRTOL failed", false); + + return __swap_positions(srv, from, to); +} + bool command_help(struct lkt_state *srv, size_t c, char UNUSED *args[LKT_MESSAGE_ARGS_MAX]) { diff --git a/src/database/queue.c b/src/database/queue.c index dd0a4151b106c2627c2a3ebcfa978b223acab4e5..c6882ac0bda0be6104ae2bda7f6c6fa7abf20cd5 100644 --- a/src/database/queue.c +++ b/src/database/queue.c @@ -802,19 +802,19 @@ error: return false; } -bool +int database_queue_probe_id(volatile sqlite3 *db, int id) { - bool sta = false; + int position = 0; /* The queue begins with the position 1 */ sqlite3_stmt *stmt = NULL; - static const char *SQL = "SELECT * FROM queue WHERE kara_id = ?;"; + static const char *SQL = "SELECT position FROM queue WHERE kara_id = ?;"; SQLITE_PREPARE(db, stmt, SQL, error); SQLITE_BIND_INT(db, stmt, 1, id, error); SQLITE_STEP_ROW(db, stmt, error); - sta = true; + position = sqlite3_column_int(stmt, 0); error: sqlite3_finalize(stmt); - return sta; + return position; } bool diff --git a/src/net/listen.c b/src/net/listen.c index 33114bff45342bb5c6a73081fe4bd60ac0718e15..bdde871306aec88b4849214025bed1527c24aa30 100644 --- a/src/net/listen.c +++ b/src/net/listen.c @@ -207,6 +207,8 @@ handle_simple_command(struct lkt_state *srv, size_t c, struct lkt_command cmd) err = ! command_shuffle(srv, NULL); else if (STR_MATCH(cmd.name, "swap")) err = ! command_swap(srv, cmd.args); + else if (STR_MATCH(cmd.name, "swapid")) + err = ! command_swapid(srv, cmd.args); else if (STR_MATCH(cmd.name, "__flat")) err = ! command_flat(srv, cmd.args);