queue/Playlist: add interface QueueListener, replacing calls to idle_add()

This commit is contained in:
Max Kellermann 2016-03-10 20:10:14 +01:00
parent ba43ec5759
commit 9a9b6fa326
8 changed files with 88 additions and 14 deletions

View File

@ -166,6 +166,7 @@ libmpd_a_SOURCES = \
src/queue/PlaylistEdit.cxx \
src/queue/PlaylistTag.cxx \
src/queue/PlaylistState.cxx src/queue/PlaylistState.hxx \
src/queue/Listener.hxx \
src/ReplayGainConfig.cxx src/ReplayGainConfig.hxx \
src/ReplayGainInfo.cxx src/ReplayGainInfo.hxx \
src/DetachedSong.cxx src/DetachedSong.hxx \

View File

@ -29,7 +29,7 @@ Partition::Partition(Instance &_instance,
unsigned max_length,
unsigned buffer_chunks,
unsigned buffered_before_play)
:instance(_instance), playlist(max_length),
:instance(_instance), playlist(max_length, *this),
outputs(*this),
pc(*this, outputs, buffer_chunks, buffered_before_play)
{
@ -74,6 +74,24 @@ Partition::SyncWithPlayer()
playlist.SyncWithPlayer(pc);
}
void
Partition::OnQueueModified()
{
EmitIdle(IDLE_PLAYLIST);
}
void
Partition::OnQueueOptionsChanged()
{
EmitIdle(IDLE_OPTIONS);
}
void
Partition::OnQueueSongStarted()
{
EmitIdle(IDLE_PLAYER);
}
void
Partition::OnPlayerSync()
{

View File

@ -21,6 +21,7 @@
#define MPD_PARTITION_HXX
#include "queue/Playlist.hxx"
#include "queue/Listener.hxx"
#include "output/MultipleOutputs.hxx"
#include "mixer/Listener.hxx"
#include "player/Control.hxx"
@ -36,7 +37,7 @@ class SongLoader;
* A partition of the Music Player Daemon. It is a separate unit with
* a playlist, a player, outputs etc.
*/
struct Partition final : private PlayerListener, private MixerListener {
struct Partition final : QueueListener, PlayerListener, MixerListener {
Instance &instance;
struct playlist playlist;
@ -200,6 +201,11 @@ struct Partition final : private PlayerListener, private MixerListener {
void SyncWithPlayer();
private:
/* virtual methods from class QueueListener */
void OnQueueModified() override;
void OnQueueOptionsChanged() override;
void OnQueueSongStarted() override;
/* virtual methods from class PlayerListener */
virtual void OnPlayerSync() override;
virtual void OnPlayerTagModified() override;

43
src/queue/Listener.hxx Normal file
View File

@ -0,0 +1,43 @@
/*
* Copyright 2003-2016 The Music Player Daemon Project
* 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.
*
* 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.
*/
#ifndef MPD_QUEUE_LISTENER_HXX
#define MPD_QUEUE_LISTENER_HXX
class QueueListener {
public:
/**
* Called after the queue has been modified.
*/
virtual void OnQueueModified() = 0;
/**
* Called after a playback options have been changed.
*/
virtual void OnQueueOptionsChanged() = 0;
/**
* Called after the player has started playing a new song.
* This gets called by playlist::SyncWithPlayer() after it has
* been notified by the player thread.
*/
virtual void OnQueueSongStarted() = 0;
};
#endif

View File

@ -19,10 +19,10 @@
#include "config.h"
#include "Playlist.hxx"
#include "Listener.hxx"
#include "PlaylistError.hxx"
#include "player/Control.hxx"
#include "DetachedSong.hxx"
#include "Idle.hxx"
#include "Log.hxx"
#include <assert.h>
@ -86,7 +86,7 @@ playlist::QueuedSongStarted(PlayerControl &pc)
if (queue.consume)
DeleteOrder(pc, old_current);
idle_add(IDLE_PLAYER);
listener.OnQueueSongStarted();
SongStarted();
}
@ -245,7 +245,7 @@ playlist::SetRepeat(PlayerControl &pc, bool status)
might change when repeat mode is toggled */
UpdateQueuedSong(pc, GetQueuedSong());
idle_add(IDLE_OPTIONS);
listener.OnQueueOptionsChanged();
}
static void
@ -272,7 +272,7 @@ playlist::SetSingle(PlayerControl &pc, bool status)
might change when single mode is toggled */
UpdateQueuedSong(pc, GetQueuedSong());
idle_add(IDLE_OPTIONS);
listener.OnQueueOptionsChanged();
}
void
@ -282,7 +282,7 @@ playlist::SetConsume(bool status)
return;
queue.consume = status;
idle_add(IDLE_OPTIONS);
listener.OnQueueOptionsChanged();
}
void
@ -319,7 +319,7 @@ playlist::SetRandom(PlayerControl &pc, bool status)
UpdateQueuedSong(pc, queued_song);
idle_add(IDLE_OPTIONS);
listener.OnQueueOptionsChanged();
}
int

View File

@ -30,6 +30,7 @@ class Error;
class SongLoader;
class SongTime;
class SignedSongTime;
class QueueListener;
struct playlist {
/**
@ -37,6 +38,8 @@ struct playlist {
*/
Queue queue;
QueueListener &listener;
/**
* This value is true if the player is currently playing (or
* should be playing).
@ -85,8 +88,11 @@ struct playlist {
*/
int queued;
playlist(unsigned max_length)
:queue(max_length), playing(false),
playlist(unsigned max_length,
QueueListener &_listener)
:queue(max_length),
listener(_listener),
playing(false),
bulk_edit(false),
current(-1), queued(-1) {
}
@ -129,7 +135,8 @@ struct playlist {
protected:
/**
* Called by all editing methods after a modification.
* Updates the queue version and emits #IDLE_PLAYLIST.
* Updates the queue version and invokes
* QueueListener::OnQueueModified().
*/
void OnModified();

View File

@ -25,12 +25,12 @@
#include "config.h"
#include "Playlist.hxx"
#include "Listener.hxx"
#include "PlaylistError.hxx"
#include "player/Control.hxx"
#include "util/Error.hxx"
#include "DetachedSong.hxx"
#include "SongLoader.hxx"
#include "Idle.hxx"
#include <memory>
@ -47,7 +47,7 @@ playlist::OnModified()
queue.IncrementVersion();
idle_add(IDLE_PLAYLIST);
listener.OnQueueModified();
}
void

View File

@ -22,7 +22,6 @@
#include "db/Interface.hxx"
#include "db/LightSong.hxx"
#include "DetachedSong.hxx"
#include "Idle.hxx"
#include "util/Error.hxx"
static bool