From 5ca6026787d287ba604ab8393a1d3d86424f5398 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 10 Mar 2016 22:44:34 +0100 Subject: [PATCH] Partition: use CallbackMaskMonitor, replacing class GlobalEvents::Monitor --- Makefile.am | 3 +-- src/GlobalEvents.cxx | 58 ------------------------------------------ src/GlobalEvents.hxx | 24 ----------------- src/Instance.cxx | 16 ------------ src/Instance.hxx | 11 -------- src/Main.cxx | 2 -- src/Partition.cxx | 25 +++++++++++++++--- src/Partition.hxx | 8 +++++- src/PlaylistGlobal.cxx | 48 ---------------------------------- src/PlaylistGlobal.hxx | 26 ------------------- 10 files changed, 30 insertions(+), 191 deletions(-) delete mode 100644 src/GlobalEvents.cxx delete mode 100644 src/PlaylistGlobal.cxx delete mode 100644 src/PlaylistGlobal.hxx diff --git a/Makefile.am b/Makefile.am index 7d270a713..befdc043e 100644 --- a/Makefile.am +++ b/Makefile.am @@ -132,7 +132,7 @@ libmpd_a_SOURCES = \ src/IOThread.cxx src/IOThread.hxx \ src/Instance.cxx src/Instance.hxx \ src/win32/Win32Main.cxx \ - src/GlobalEvents.cxx src/GlobalEvents.hxx \ + src/GlobalEvents.hxx \ src/MixRampInfo.hxx \ src/MusicBuffer.cxx src/MusicBuffer.hxx \ src/MusicPipe.cxx src/MusicPipe.hxx \ @@ -145,7 +145,6 @@ libmpd_a_SOURCES = \ src/player/Control.cxx src/player/Control.hxx \ src/player/Listener.hxx \ src/PlaylistError.cxx src/PlaylistError.hxx \ - src/PlaylistGlobal.cxx src/PlaylistGlobal.hxx \ src/PlaylistPrint.cxx src/PlaylistPrint.hxx \ src/PlaylistSave.cxx src/PlaylistSave.hxx \ src/playlist/PlaylistStream.cxx src/playlist/PlaylistStream.hxx \ diff --git a/src/GlobalEvents.cxx b/src/GlobalEvents.cxx deleted file mode 100644 index da389b940..000000000 --- a/src/GlobalEvents.cxx +++ /dev/null @@ -1,58 +0,0 @@ -/* - * 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. - */ - -#include "config.h" -#include "GlobalEvents.hxx" - -#include - -inline void -GlobalEvents::Monitor::Invoke(Event event) -{ - assert((unsigned)event < GlobalEvents::MAX); - assert(handlers[event] != nullptr); - - handlers[event](); -} - -void -GlobalEvents::Monitor::HandleMask(unsigned f) -{ - for (unsigned i = 0; i < MAX; ++i) - if (f & (1u << i)) - /* invoke the event handler */ - Invoke(Event(i)); -} - -void -GlobalEvents::Monitor::Register(Event event, Handler callback) -{ - assert((unsigned)event < MAX); - - handlers[event] = callback; -} - -void -GlobalEvents::Monitor::Emit(Event event) -{ - assert((unsigned)event < MAX); - - const unsigned mask = 1u << unsigned(event); - OrMask(mask); -} diff --git a/src/GlobalEvents.hxx b/src/GlobalEvents.hxx index abd6949f6..c3c30d507 100644 --- a/src/GlobalEvents.hxx +++ b/src/GlobalEvents.hxx @@ -20,8 +20,6 @@ #ifndef MPD_GLOBAL_EVENTS_HXX #define MPD_GLOBAL_EVENTS_HXX -#include "event/MaskMonitor.hxx" - namespace GlobalEvents { enum Event { /** must call playlist_sync() */ @@ -32,28 +30,6 @@ namespace GlobalEvents { MAX }; - - typedef void (*Handler)(); - - class Monitor final : MaskMonitor { - Handler handlers[MAX]; - - public: - explicit Monitor(EventLoop &_loop):MaskMonitor(_loop) {} - - void Register(Event event, Handler handler); - - void Emit(Event event); - - private: - /** - * Invoke the callback for a certain event. - */ - void Invoke(Event event); - - protected: - void HandleMask(unsigned mask) override; - }; } #endif /* MAIN_NOTIFY_H */ diff --git a/src/Instance.cxx b/src/Instance.cxx index 6bc857b10..3559ca528 100644 --- a/src/Instance.cxx +++ b/src/Instance.cxx @@ -45,22 +45,6 @@ Instance::GetDatabase(Error &error) return database; } -#endif - -void -Instance::TagModified() -{ - partition->TagModified(); -} - -void -Instance::SyncWithPlayer() -{ - partition->SyncWithPlayer(); -} - -#ifdef ENABLE_DATABASE - void Instance::OnDatabaseModified() { diff --git a/src/Instance.hxx b/src/Instance.hxx index f411a0e89..ed4dacd4d 100644 --- a/src/Instance.hxx +++ b/src/Instance.hxx @@ -112,17 +112,6 @@ struct Instance final Database *GetDatabase(Error &error); #endif - /** - * A tag in the play queue has been modified by the player - * thread. Propagate the change to all subsystems. - */ - void TagModified(); - - /** - * Synchronize the player with the play queue. - */ - void SyncWithPlayer(); - private: #ifdef ENABLE_DATABASE virtual void OnDatabaseModified() override; diff --git a/src/Main.cxx b/src/Main.cxx index 7bbcd59d7..f6146faf1 100644 --- a/src/Main.cxx +++ b/src/Main.cxx @@ -22,7 +22,6 @@ #include "Instance.hxx" #include "CommandLine.hxx" #include "PlaylistFile.hxx" -#include "PlaylistGlobal.hxx" #include "MusicChunk.hxx" #include "StateFile.hxx" #include "player/Thread.hxx" @@ -523,7 +522,6 @@ try { } initPermissions(); - playlist_global_init(); spl_global_init(); #ifdef ENABLE_ARCHIVE archive_plugin_init_all(); diff --git a/src/Partition.cxx b/src/Partition.cxx index 573fdc718..4a55e41de 100644 --- a/src/Partition.cxx +++ b/src/Partition.cxx @@ -30,13 +30,22 @@ Partition::Partition(Instance &_instance, unsigned buffer_chunks, unsigned buffered_before_play) :instance(_instance), - global_events(instance.event_loop), + global_events(instance.event_loop, *this, &Partition::OnGlobalEvent), playlist(max_length, *this), outputs(*this), pc(*this, outputs, buffer_chunks, buffered_before_play) { } +void +Partition::EmitGlobalEvent(GlobalEvents::Event event) +{ + assert((unsigned)event < GlobalEvents::MAX); + + const unsigned mask = 1u << unsigned(event); + global_events.OrMask(mask); +} + void Partition::EmitIdle(unsigned mask) { @@ -97,13 +106,13 @@ Partition::OnQueueSongStarted() void Partition::OnPlayerSync() { - global_events.Emit(GlobalEvents::PLAYLIST); + EmitGlobalEvent(GlobalEvents::PLAYLIST); } void Partition::OnPlayerTagModified() { - global_events.Emit(GlobalEvents::TAG); + EmitGlobalEvent(GlobalEvents::TAG); } void @@ -114,3 +123,13 @@ Partition::OnMixerVolumeChanged(gcc_unused Mixer &mixer, gcc_unused int volume) /* notify clients */ EmitIdle(IDLE_MIXER); } + +void +Partition::OnGlobalEvent(unsigned mask) +{ + if ((mask & (1u << unsigned(GlobalEvents::TAG))) != 0) + TagModified(); + + if ((mask & (1u << unsigned(GlobalEvents::PLAYLIST))) != 0) + SyncWithPlayer(); +} diff --git a/src/Partition.hxx b/src/Partition.hxx index ca1e6a9d2..785a2e52d 100644 --- a/src/Partition.hxx +++ b/src/Partition.hxx @@ -20,6 +20,7 @@ #ifndef MPD_PARTITION_HXX #define MPD_PARTITION_HXX +#include "event/MaskMonitor.hxx" #include "GlobalEvents.hxx" #include "queue/Playlist.hxx" #include "queue/Listener.hxx" @@ -41,7 +42,7 @@ class SongLoader; struct Partition final : QueueListener, PlayerListener, MixerListener { Instance &instance; - GlobalEvents::Monitor global_events; + CallbackMaskMonitor global_events; struct playlist playlist; @@ -54,6 +55,8 @@ struct Partition final : QueueListener, PlayerListener, MixerListener { unsigned buffer_chunks, unsigned buffered_before_play); + void EmitGlobalEvent(GlobalEvents::Event event); + void EmitIdle(unsigned mask); void ClearQueue() { @@ -215,6 +218,9 @@ private: /* virtual methods from class MixerListener */ virtual void OnMixerVolumeChanged(Mixer &mixer, int volume) override; + + /* callback for #global_events */ + void OnGlobalEvent(unsigned mask); }; #endif diff --git a/src/PlaylistGlobal.cxx b/src/PlaylistGlobal.cxx deleted file mode 100644 index f88d96482..000000000 --- a/src/PlaylistGlobal.cxx +++ /dev/null @@ -1,48 +0,0 @@ -/* - * 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. - */ - -/* - * The manager of the global "struct playlist" instance (g_playlist). - * - */ - -#include "config.h" -#include "PlaylistGlobal.hxx" -#include "Main.hxx" -#include "Instance.hxx" -#include "Partition.hxx" - -static void -playlist_tag_event(void) -{ - instance->TagModified(); -} - -static void -playlist_event(void) -{ - instance->SyncWithPlayer(); -} - -void -playlist_global_init() -{ - instance->partition->global_events.Register(GlobalEvents::TAG, playlist_tag_event); - instance->partition->global_events.Register(GlobalEvents::PLAYLIST, playlist_event); -} diff --git a/src/PlaylistGlobal.hxx b/src/PlaylistGlobal.hxx deleted file mode 100644 index 3d52d0414..000000000 --- a/src/PlaylistGlobal.hxx +++ /dev/null @@ -1,26 +0,0 @@ -/* - * 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_PLAYLIST_GLOBAL_HXX -#define MPD_PLAYLIST_GLOBAL_HXX - -void -playlist_global_init(); - -#endif