PlayerListener: new interface to replace GlobalEvents access

This commit is contained in:
Max Kellermann 2014-02-21 08:55:52 +01:00
parent 860339c132
commit 30a82076ba
8 changed files with 76 additions and 14 deletions

View File

@ -137,6 +137,7 @@ libmpd_a_SOURCES = \
src/Permission.cxx src/Permission.hxx \ src/Permission.cxx src/Permission.hxx \
src/PlayerThread.cxx src/PlayerThread.hxx \ src/PlayerThread.cxx src/PlayerThread.hxx \
src/PlayerControl.cxx src/PlayerControl.hxx \ src/PlayerControl.cxx src/PlayerControl.hxx \
src/PlayerListener.hxx \
src/Playlist.cxx src/Playlist.hxx \ src/Playlist.cxx src/Playlist.hxx \
src/PlaylistError.cxx src/PlaylistError.hxx \ src/PlaylistError.cxx src/PlaylistError.hxx \
src/PlaylistGlobal.cxx src/PlaylistGlobal.hxx \ src/PlaylistGlobal.cxx src/PlaylistGlobal.hxx \

View File

@ -23,6 +23,7 @@
#include "output/MultipleOutputs.hxx" #include "output/MultipleOutputs.hxx"
#include "mixer/Volume.hxx" #include "mixer/Volume.hxx"
#include "Idle.hxx" #include "Idle.hxx"
#include "GlobalEvents.hxx"
#ifdef ENABLE_DATABASE #ifdef ENABLE_DATABASE
@ -50,6 +51,18 @@ Partition::SyncWithPlayer()
playlist.SyncWithPlayer(pc); playlist.SyncWithPlayer(pc);
} }
void
Partition::OnPlayerSync()
{
GlobalEvents::Emit(GlobalEvents::PLAYLIST);
}
void
Partition::OnPlayerTagModified()
{
GlobalEvents::Emit(GlobalEvents::TAG);
}
void void
Partition::OnMixerVolumeChanged(gcc_unused Mixer &mixer, gcc_unused int volume) Partition::OnMixerVolumeChanged(gcc_unused Mixer &mixer, gcc_unused int volume)
{ {

View File

@ -24,6 +24,7 @@
#include "output/MultipleOutputs.hxx" #include "output/MultipleOutputs.hxx"
#include "mixer/Listener.hxx" #include "mixer/Listener.hxx"
#include "PlayerControl.hxx" #include "PlayerControl.hxx"
#include "PlayerListener.hxx"
struct Instance; struct Instance;
class MultipleOutputs; class MultipleOutputs;
@ -33,7 +34,7 @@ class SongLoader;
* A partition of the Music Player Daemon. It is a separate unit with * A partition of the Music Player Daemon. It is a separate unit with
* a playlist, a player, outputs etc. * a playlist, a player, outputs etc.
*/ */
struct Partition final : private MixerListener { struct Partition final : private PlayerListener, private MixerListener {
Instance &instance; Instance &instance;
struct playlist playlist; struct playlist playlist;
@ -48,7 +49,7 @@ struct Partition final : private MixerListener {
unsigned buffered_before_play) unsigned buffered_before_play)
:instance(_instance), playlist(max_length), :instance(_instance), playlist(max_length),
outputs(*this), outputs(*this),
pc(outputs, buffer_chunks, buffered_before_play) {} pc(*this, outputs, buffer_chunks, buffered_before_play) {}
void ClearQueue() { void ClearQueue() {
playlist.Clear(pc); playlist.Clear(pc);
@ -192,6 +193,10 @@ struct Partition final : private MixerListener {
void SyncWithPlayer(); void SyncWithPlayer();
private: private:
/* virtual methods from class PlayerListener */
virtual void OnPlayerSync() override;
virtual void OnPlayerTagModified() override;
/* virtual methods from class MixerListener */ /* virtual methods from class MixerListener */
virtual void OnMixerVolumeChanged(Mixer &mixer, int volume) override; virtual void OnMixerVolumeChanged(Mixer &mixer, int volume) override;
}; };

View File

@ -26,10 +26,11 @@
#include <assert.h> #include <assert.h>
PlayerControl::PlayerControl(MultipleOutputs &_outputs, PlayerControl::PlayerControl(PlayerListener &_listener,
MultipleOutputs &_outputs,
unsigned _buffer_chunks, unsigned _buffer_chunks,
unsigned _buffered_before_play) unsigned _buffered_before_play)
:outputs(_outputs), :listener(_listener), outputs(_outputs),
buffer_chunks(_buffer_chunks), buffer_chunks(_buffer_chunks),
buffered_before_play(_buffered_before_play), buffered_before_play(_buffered_before_play),
command(PlayerCommand::NONE), command(PlayerCommand::NONE),

View File

@ -29,6 +29,7 @@
#include <stdint.h> #include <stdint.h>
class PlayerListener;
class MultipleOutputs; class MultipleOutputs;
class DetachedSong; class DetachedSong;
@ -92,6 +93,8 @@ struct player_status {
}; };
struct PlayerControl { struct PlayerControl {
PlayerListener &listener;
MultipleOutputs &outputs; MultipleOutputs &outputs;
unsigned buffer_chunks; unsigned buffer_chunks;
@ -137,8 +140,8 @@ struct PlayerControl {
* A copy of the current #DetachedSong after its tags have * A copy of the current #DetachedSong after its tags have
* been updated by the decoder (for example, a radio stream * been updated by the decoder (for example, a radio stream
* that has sent a new tag after switching to the next song). * that has sent a new tag after switching to the next song).
* This shall be used by the GlobalEvents::TAG handler to * This shall be used by PlayerListener::OnPlayerTagModified()
* update the current #DetachedSong in the queue. * to update the current #DetachedSong in the queue.
* *
* Protected by #mutex. Set by the PlayerThread and consumed * Protected by #mutex. Set by the PlayerThread and consumed
* by the main thread. * by the main thread.
@ -173,7 +176,8 @@ struct PlayerControl {
*/ */
bool border_pause; bool border_pause;
PlayerControl(MultipleOutputs &_outputs, PlayerControl(PlayerListener &_listener,
MultipleOutputs &_outputs,
unsigned buffer_chunks, unsigned buffer_chunks,
unsigned buffered_before_play); unsigned buffered_before_play);
~PlayerControl(); ~PlayerControl();

36
src/PlayerListener.hxx Normal file
View File

@ -0,0 +1,36 @@
/*
* Copyright (C) 2003-2014 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_PLAYER_LISTENER_HXX
#define MPD_PLAYER_LISTENER_HXX
class PlayerListener {
public:
/**
* Must call playlist_sync().
*/
virtual void OnPlayerSync() = 0;
/**
* The current song's tag has changed.
*/
virtual void OnPlayerTagModified() = 0;
};
#endif

View File

@ -19,6 +19,7 @@
#include "config.h" #include "config.h"
#include "PlayerThread.hxx" #include "PlayerThread.hxx"
#include "PlayerListener.hxx"
#include "decoder/DecoderThread.hxx" #include "decoder/DecoderThread.hxx"
#include "decoder/DecoderControl.hxx" #include "decoder/DecoderControl.hxx"
#include "MusicPipe.hxx" #include "MusicPipe.hxx"
@ -31,7 +32,6 @@
#include "output/MultipleOutputs.hxx" #include "output/MultipleOutputs.hxx"
#include "tag/Tag.hxx" #include "tag/Tag.hxx"
#include "Idle.hxx" #include "Idle.hxx"
#include "GlobalEvents.hxx"
#include "util/Domain.hxx" #include "util/Domain.hxx"
#include "thread/Name.hxx" #include "thread/Name.hxx"
#include "Log.hxx" #include "Log.hxx"
@ -359,7 +359,7 @@ Player::WaitForDecoder()
pc.Unlock(); pc.Unlock();
/* call syncPlaylistWithQueue() in the main thread */ /* call syncPlaylistWithQueue() in the main thread */
GlobalEvents::Emit(GlobalEvents::PLAYLIST); pc.listener.OnPlayerSync();
return true; return true;
} }
@ -696,7 +696,7 @@ update_song_tag(PlayerControl &pc, DetachedSong &song, const Tag &new_tag)
/* the main thread will update the playlist version when he /* the main thread will update the playlist version when he
receives this event */ receives this event */
GlobalEvents::Emit(GlobalEvents::TAG); pc.listener.OnPlayerTagModified();
/* notify all clients that the tag of the current song has /* notify all clients that the tag of the current song has
changed */ changed */
@ -1124,7 +1124,7 @@ player_task(void *arg)
pc.Unlock(); pc.Unlock();
do_play(pc, dc, buffer); do_play(pc, dc, buffer);
GlobalEvents::Emit(GlobalEvents::PLAYLIST); pc.listener.OnPlayerSync();
pc.Lock(); pc.Lock();
break; break;

View File

@ -53,10 +53,11 @@ filter_plugin_by_name(gcc_unused const char *name)
return NULL; return NULL;
} }
PlayerControl::PlayerControl(gcc_unused MultipleOutputs &_outputs, PlayerControl::PlayerControl(PlayerListener &_listener,
MultipleOutputs &_outputs,
gcc_unused unsigned _buffer_chunks, gcc_unused unsigned _buffer_chunks,
gcc_unused unsigned _buffered_before_play) gcc_unused unsigned _buffered_before_play)
:outputs(_outputs) {} :listener(_listener), outputs(_outputs) {}
PlayerControl::~PlayerControl() {} PlayerControl::~PlayerControl() {}
static AudioOutput * static AudioOutput *
@ -69,7 +70,8 @@ load_audio_output(EventLoop &event_loop, const char *name)
return nullptr; return nullptr;
} }
static struct PlayerControl dummy_player_control(*(MultipleOutputs *)nullptr, static struct PlayerControl dummy_player_control(*(PlayerListener *)nullptr,
*(MultipleOutputs *)nullptr,
32, 4); 32, 4);
Error error; Error error;