2009-02-04 20:31:22 +01:00
|
|
|
/*
|
2013-01-04 20:50:00 +01:00
|
|
|
* Copyright (C) 2003-2013 The Music Player Daemon Project
|
2009-02-04 20:31:22 +01:00
|
|
|
* http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2009-02-04 20:31:22 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Functions for controlling playback on the playlist level.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2013-01-04 20:50:00 +01:00
|
|
|
#include "PlaylistInternal.hxx"
|
2012-08-09 22:19:39 +02:00
|
|
|
#include "song.h"
|
2013-01-04 20:50:00 +01:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include "player_control.h"
|
|
|
|
}
|
2009-02-04 20:31:22 +01:00
|
|
|
|
2009-02-04 22:15:31 +01:00
|
|
|
#include <glib.h>
|
|
|
|
|
|
|
|
#undef G_LOG_DOMAIN
|
|
|
|
#define G_LOG_DOMAIN "playlist"
|
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
void
|
|
|
|
playlist_stop(struct playlist *playlist, struct player_control *pc)
|
2009-02-04 20:31:22 +01:00
|
|
|
{
|
|
|
|
if (!playlist->playing)
|
|
|
|
return;
|
|
|
|
|
|
|
|
assert(playlist->current >= 0);
|
|
|
|
|
2009-02-04 22:15:31 +01:00
|
|
|
g_debug("stop");
|
2009-11-03 21:08:48 +01:00
|
|
|
pc_stop(pc);
|
2009-02-04 20:31:22 +01:00
|
|
|
playlist->queued = -1;
|
|
|
|
playlist->playing = false;
|
|
|
|
|
|
|
|
if (playlist->queue.random) {
|
|
|
|
/* shuffle the playlist, so the next playback will
|
|
|
|
result in a new random order */
|
|
|
|
|
|
|
|
unsigned current_position =
|
|
|
|
queue_order_to_position(&playlist->queue,
|
|
|
|
playlist->current);
|
|
|
|
|
|
|
|
queue_shuffle_order(&playlist->queue);
|
|
|
|
|
|
|
|
/* make sure that "current" stays valid, and the next
|
|
|
|
"play" command plays the same song again */
|
|
|
|
playlist->current =
|
|
|
|
queue_position_to_order(&playlist->queue,
|
|
|
|
current_position);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
enum playlist_result
|
|
|
|
playlist_play(struct playlist *playlist, struct player_control *pc,
|
|
|
|
int song)
|
2009-02-04 20:31:22 +01:00
|
|
|
{
|
|
|
|
unsigned i = song;
|
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
pc_clear_error(pc);
|
2009-02-04 20:31:22 +01:00
|
|
|
|
|
|
|
if (song == -1) {
|
|
|
|
/* play any song ("current" song, or the first song */
|
|
|
|
|
|
|
|
if (queue_is_empty(&playlist->queue))
|
|
|
|
return PLAYLIST_RESULT_SUCCESS;
|
|
|
|
|
|
|
|
if (playlist->playing) {
|
|
|
|
/* already playing: unpause playback, just in
|
|
|
|
case it was paused, and return */
|
2009-11-03 21:08:48 +01:00
|
|
|
pc_set_pause(pc, false);
|
2009-02-04 20:31:22 +01:00
|
|
|
return PLAYLIST_RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* select a song: "current" song, or the first one */
|
|
|
|
i = playlist->current >= 0
|
|
|
|
? playlist->current
|
|
|
|
: 0;
|
|
|
|
} else if (!queue_valid_position(&playlist->queue, song))
|
|
|
|
return PLAYLIST_RESULT_BAD_RANGE;
|
|
|
|
|
|
|
|
if (playlist->queue.random) {
|
|
|
|
if (song >= 0)
|
|
|
|
/* "i" is currently the song position (which
|
|
|
|
would be equal to the order number in
|
|
|
|
no-random mode); convert it to a order
|
|
|
|
number, because random mode is enabled */
|
|
|
|
i = queue_position_to_order(&playlist->queue, song);
|
|
|
|
|
|
|
|
if (!playlist->playing)
|
|
|
|
playlist->current = 0;
|
|
|
|
|
|
|
|
/* swap the new song with the previous "current" one,
|
|
|
|
so playback continues as planned */
|
|
|
|
queue_swap_order(&playlist->queue,
|
|
|
|
i, playlist->current);
|
|
|
|
i = playlist->current;
|
|
|
|
}
|
|
|
|
|
|
|
|
playlist->stop_on_error = false;
|
|
|
|
playlist->error_count = 0;
|
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
playlist_play_order(playlist, pc, i);
|
2009-02-04 20:31:22 +01:00
|
|
|
return PLAYLIST_RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum playlist_result
|
2009-11-03 21:08:48 +01:00
|
|
|
playlist_play_id(struct playlist *playlist, struct player_control *pc,
|
|
|
|
int id)
|
2009-02-04 20:31:22 +01:00
|
|
|
{
|
|
|
|
int song;
|
|
|
|
|
|
|
|
if (id == -1) {
|
2009-11-03 21:08:48 +01:00
|
|
|
return playlist_play(playlist, pc, id);
|
2009-02-04 20:31:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
song = queue_id_to_position(&playlist->queue, id);
|
|
|
|
if (song < 0)
|
|
|
|
return PLAYLIST_RESULT_NO_SUCH_SONG;
|
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
return playlist_play(playlist, pc, song);
|
2009-02-04 20:31:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-11-03 21:08:48 +01:00
|
|
|
playlist_next(struct playlist *playlist, struct player_control *pc)
|
2009-02-04 20:31:22 +01:00
|
|
|
{
|
|
|
|
int next_order;
|
2009-03-30 00:01:02 +02:00
|
|
|
int current;
|
2009-02-04 20:31:22 +01:00
|
|
|
|
|
|
|
if (!playlist->playing)
|
|
|
|
return;
|
|
|
|
|
|
|
|
assert(!queue_is_empty(&playlist->queue));
|
|
|
|
assert(queue_valid_order(&playlist->queue, playlist->current));
|
|
|
|
|
2009-03-30 00:01:02 +02:00
|
|
|
current = playlist->current;
|
2009-02-04 20:31:22 +01:00
|
|
|
playlist->stop_on_error = false;
|
|
|
|
|
|
|
|
/* determine the next song from the queue's order list */
|
|
|
|
|
|
|
|
next_order = queue_next_order(&playlist->queue, playlist->current);
|
|
|
|
if (next_order < 0) {
|
|
|
|
/* no song after this one: stop playback */
|
2009-11-03 21:08:48 +01:00
|
|
|
playlist_stop(playlist, pc);
|
2009-05-28 23:23:32 +02:00
|
|
|
|
|
|
|
/* reset "current song" */
|
|
|
|
playlist->current = -1;
|
2009-02-04 20:31:22 +01:00
|
|
|
}
|
2009-03-30 00:01:02 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (next_order == 0 && playlist->queue.random) {
|
|
|
|
/* The queue told us that the next song is the first
|
|
|
|
song. This means we are in repeat mode. Shuffle
|
|
|
|
the queue order, so this time, the user hears the
|
|
|
|
songs in a different than before */
|
|
|
|
assert(playlist->queue.repeat);
|
|
|
|
|
|
|
|
queue_shuffle_order(&playlist->queue);
|
|
|
|
|
|
|
|
/* note that playlist->current and playlist->queued are
|
2009-07-29 00:07:01 +02:00
|
|
|
now invalid, but playlist_play_order() will
|
2009-03-30 00:01:02 +02:00
|
|
|
discard them anyway */
|
|
|
|
}
|
2009-02-04 20:31:22 +01:00
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
playlist_play_order(playlist, pc, next_order);
|
2009-02-04 20:31:22 +01:00
|
|
|
}
|
|
|
|
|
2009-03-30 00:01:02 +02:00
|
|
|
/* Consume mode removes each played songs. */
|
|
|
|
if(playlist->queue.consume)
|
2009-11-03 21:08:48 +01:00
|
|
|
playlist_delete(playlist, pc,
|
|
|
|
queue_order_to_position(&playlist->queue,
|
|
|
|
current));
|
2009-02-04 20:31:22 +01:00
|
|
|
}
|
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
void
|
|
|
|
playlist_previous(struct playlist *playlist, struct player_control *pc)
|
2009-02-04 20:31:22 +01:00
|
|
|
{
|
|
|
|
if (!playlist->playing)
|
|
|
|
return;
|
|
|
|
|
2009-10-08 20:33:50 +02:00
|
|
|
assert(queue_length(&playlist->queue) > 0);
|
2009-02-04 20:31:22 +01:00
|
|
|
|
2009-10-08 20:33:50 +02:00
|
|
|
if (playlist->current > 0) {
|
|
|
|
/* play the preceding song */
|
2009-11-03 21:08:48 +01:00
|
|
|
playlist_play_order(playlist, pc,
|
2009-10-08 20:33:50 +02:00
|
|
|
playlist->current - 1);
|
|
|
|
} else if (playlist->queue.repeat) {
|
|
|
|
/* play the last song in "repeat" mode */
|
2009-11-03 21:08:48 +01:00
|
|
|
playlist_play_order(playlist, pc,
|
2009-10-08 20:33:50 +02:00
|
|
|
queue_length(&playlist->queue) - 1);
|
2009-02-04 20:31:22 +01:00
|
|
|
} else {
|
2009-10-08 20:33:50 +02:00
|
|
|
/* re-start playing the current song if it's
|
|
|
|
the first one */
|
2009-11-03 21:08:48 +01:00
|
|
|
playlist_play_order(playlist, pc, playlist->current);
|
2009-02-04 20:31:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enum playlist_result
|
2009-11-03 21:08:48 +01:00
|
|
|
playlist_seek_song(struct playlist *playlist, struct player_control *pc,
|
|
|
|
unsigned song, float seek_time)
|
2009-02-04 20:31:22 +01:00
|
|
|
{
|
|
|
|
const struct song *queued;
|
|
|
|
unsigned i;
|
2009-05-06 18:35:22 +02:00
|
|
|
bool success;
|
2009-02-04 20:31:22 +01:00
|
|
|
|
|
|
|
if (!queue_valid_position(&playlist->queue, song))
|
|
|
|
return PLAYLIST_RESULT_BAD_RANGE;
|
|
|
|
|
|
|
|
queued = playlist_get_queued_song(playlist);
|
|
|
|
|
|
|
|
if (playlist->queue.random)
|
|
|
|
i = queue_position_to_order(&playlist->queue, song);
|
|
|
|
else
|
|
|
|
i = song;
|
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
pc_clear_error(pc);
|
2009-02-04 20:31:22 +01:00
|
|
|
playlist->stop_on_error = true;
|
|
|
|
playlist->error_count = 0;
|
|
|
|
|
|
|
|
if (!playlist->playing || (unsigned)playlist->current != i) {
|
2011-07-20 11:33:51 +02:00
|
|
|
/* seeking is not within the current song - prepare
|
|
|
|
song change */
|
|
|
|
|
|
|
|
playlist->playing = true;
|
|
|
|
playlist->current = i;
|
2009-02-04 20:31:22 +01:00
|
|
|
|
|
|
|
queued = NULL;
|
|
|
|
}
|
|
|
|
|
2012-08-09 22:19:39 +02:00
|
|
|
struct song *the_song =
|
|
|
|
song_dup_detached(queue_get_order(&playlist->queue, i));
|
|
|
|
success = pc_seek(pc, the_song, seek_time);
|
2009-05-06 18:35:22 +02:00
|
|
|
if (!success) {
|
2009-11-03 21:08:48 +01:00
|
|
|
playlist_update_queued_song(playlist, pc, queued);
|
2009-02-10 00:17:46 +01:00
|
|
|
|
2009-02-04 20:31:22 +01:00
|
|
|
return PLAYLIST_RESULT_NOT_PLAYING;
|
2009-02-10 00:17:46 +01:00
|
|
|
}
|
|
|
|
|
2009-03-06 00:41:59 +01:00
|
|
|
playlist->queued = -1;
|
2009-11-03 21:08:48 +01:00
|
|
|
playlist_update_queued_song(playlist, pc, NULL);
|
2009-02-04 20:31:22 +01:00
|
|
|
|
|
|
|
return PLAYLIST_RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum playlist_result
|
2009-11-03 21:08:48 +01:00
|
|
|
playlist_seek_song_id(struct playlist *playlist, struct player_control *pc,
|
|
|
|
unsigned id, float seek_time)
|
2009-02-04 20:31:22 +01:00
|
|
|
{
|
|
|
|
int song = queue_id_to_position(&playlist->queue, id);
|
|
|
|
if (song < 0)
|
|
|
|
return PLAYLIST_RESULT_NO_SUCH_SONG;
|
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
return playlist_seek_song(playlist, pc, song, seek_time);
|
2009-02-04 20:31:22 +01:00
|
|
|
}
|
2011-12-24 11:20:02 +01:00
|
|
|
|
|
|
|
enum playlist_result
|
|
|
|
playlist_seek_current(struct playlist *playlist, struct player_control *pc,
|
|
|
|
float seek_time, bool relative)
|
|
|
|
{
|
|
|
|
if (!playlist->playing)
|
|
|
|
return PLAYLIST_RESULT_NOT_PLAYING;
|
|
|
|
|
|
|
|
if (relative) {
|
|
|
|
struct player_status status;
|
|
|
|
pc_get_status(pc, &status);
|
|
|
|
|
|
|
|
if (status.state != PLAYER_STATE_PLAY &&
|
|
|
|
status.state != PLAYER_STATE_PAUSE)
|
|
|
|
return PLAYLIST_RESULT_NOT_PLAYING;
|
|
|
|
|
|
|
|
seek_time += (int)status.elapsed_time;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (seek_time < 0)
|
|
|
|
seek_time = 0;
|
|
|
|
|
|
|
|
return playlist_seek_song(playlist, pc, playlist->current, seek_time);
|
|
|
|
}
|