added the shufflerange command

This command shuffles a range of songs.
This commit is contained in:
Romain Bignon 2009-02-13 10:38:32 +01:00 committed by Max Kellermann
parent beaf860a17
commit 9fe459f625
4 changed files with 30 additions and 24 deletions

View File

@ -782,11 +782,14 @@ OK
<term> <term>
<cmdsynopsis> <cmdsynopsis>
<command>shuffle</command> <command>shuffle</command>
<arg><replaceable>SONGRANGE</replaceable></arg>
</cmdsynopsis> </cmdsynopsis>
</term> </term>
<listitem> <listitem>
<para> <para>
Shuffles the current playlist. Shuffles the current playlist.
<varname>SONGRANGE</varname> is optional and specifies
a range of songs.
</para> </para>
</listitem> </listitem>
</varlistentry> </varlistentry>

View File

@ -656,7 +656,12 @@ static enum command_return
handle_shuffle(G_GNUC_UNUSED struct client *client, handle_shuffle(G_GNUC_UNUSED struct client *client,
G_GNUC_UNUSED int argc, G_GNUC_UNUSED char *argv[]) G_GNUC_UNUSED int argc, G_GNUC_UNUSED char *argv[])
{ {
shufflePlaylist(&g_playlist); unsigned start = 0, end = queue_length(&g_playlist.queue);
if (argc == 2 && !check_range(client, &start, &end,
argv[1], need_range))
return COMMAND_RETURN_ERROR;
shufflePlaylist(&g_playlist, start, end);
return COMMAND_RETURN_OK; return COMMAND_RETURN_OK;
} }
@ -1566,7 +1571,7 @@ static const struct command commands[] = {
{ "seek", PERMISSION_CONTROL, 2, 2, handle_seek }, { "seek", PERMISSION_CONTROL, 2, 2, handle_seek },
{ "seekid", PERMISSION_CONTROL, 2, 2, handle_seekid }, { "seekid", PERMISSION_CONTROL, 2, 2, handle_seekid },
{ "setvol", PERMISSION_CONTROL, 1, 1, handle_setvol }, { "setvol", PERMISSION_CONTROL, 1, 1, handle_setvol },
{ "shuffle", PERMISSION_CONTROL, 0, 0, handle_shuffle }, { "shuffle", PERMISSION_CONTROL, 0, 1, handle_shuffle },
{ "stats", PERMISSION_READ, 0, 0, handle_stats }, { "stats", PERMISSION_READ, 0, 0, handle_stats },
{ "status", PERMISSION_READ, 0, 0, handle_status }, { "status", PERMISSION_READ, 0, 0, handle_status },
#ifdef ENABLE_SQLITE #ifdef ENABLE_SQLITE

View File

@ -151,7 +151,7 @@ void syncPlayerAndPlaylist(struct playlist *playlist);
void previousSongInPlaylist(struct playlist *playlist); void previousSongInPlaylist(struct playlist *playlist);
void shufflePlaylist(struct playlist *playlist); void shufflePlaylist(struct playlist *playlist, unsigned start, unsigned end);
void void
deleteASongFromPlaylist(struct playlist *playlist, const struct song *song); deleteASongFromPlaylist(struct playlist *playlist, const struct song *song);

View File

@ -353,42 +353,40 @@ moveSongInPlaylistById(struct playlist *playlist, unsigned id1, int to)
return moveSongInPlaylist(playlist, song, to); return moveSongInPlaylist(playlist, song, to);
} }
void shufflePlaylist(struct playlist *playlist) void shufflePlaylist(struct playlist *playlist, unsigned start, unsigned end)
{ {
const struct song *queued; const struct song *queued;
unsigned i;
if (queue_length(&playlist->queue) <= 1) if (end-1 <= start || end > queue_length(&playlist->queue))
return; return;
queued = playlist_get_queued_song(playlist); queued = playlist_get_queued_song(playlist);
if (playlist->playing && playlist->current >= 0) {
unsigned current_position;
current_position = queue_order_to_position(&playlist->queue,
playlist->current);
if (playlist->playing) { if (current_position >= start && current_position < end)
if (playlist->current >= 0) {
/* put current playing song first */ /* put current playing song first */
queue_swap(&playlist->queue, 0, queue_swap(&playlist->queue, start, current_position);
queue_order_to_position(&playlist->queue,
playlist->current));
if (playlist->queue.random) { if (playlist->queue.random) {
playlist->current = playlist->current =
queue_position_to_order(&playlist->queue, 0); queue_position_to_order(&playlist->queue, start);
} else } else
playlist->current = 0; playlist->current = start;
/* start shuffle after the current song */ /* start shuffle after the current song */
i = 1; start++;
}
} else { } else {
/* no playback currently: shuffle everything, and /* no playback currently: reset playlist->current */
reset playlist->current */
i = 0;
playlist->current = -1; playlist->current = -1;
} }
/* shuffle the rest of the list */ queue_shuffle_range(&playlist->queue, start, end);
queue_shuffle_range(&playlist->queue, i,
queue_length(&playlist->queue));
incrPlaylistVersion(playlist); incrPlaylistVersion(playlist);