Commit 26ebfc04 authored by Max Kellermann's avatar Max Kellermann

EventPipe: rename to GlobalEvents

parent 8782f6d2
......@@ -243,7 +243,7 @@ src_mpd_SOURCES = \
src/io_thread.c src/io_thread.h \
src/Main.cxx src/Main.hxx \
src/Win32Main.cxx \
src/EventPipe.cxx src/EventPipe.hxx \
src/GlobalEvents.cxx src/GlobalEvents.hxx \
src/daemon.c \
src/AudioCompress/compress.c \
src/MusicBuffer.cxx src/MusicBuffer.hxx \
......
......@@ -18,144 +18,136 @@
*/
#include "config.h"
#include "EventPipe.hxx"
#include "GlobalEvents.hxx"
#include "thread/Mutex.hxx"
#include "fd_util.h"
#include "mpd_error.h"
#include <stdbool.h>
#include <assert.h>
#include <glib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
#ifdef WIN32
/* for _O_BINARY */
#include <fcntl.h>
#endif
#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "event_pipe"
static int event_pipe[2];
static GIOChannel *event_channel;
static guint event_pipe_source_id;
static Mutex event_pipe_mutex;
static bool pipe_events[PIPE_EVENT_MAX];
static event_pipe_callback_t event_pipe_callbacks[PIPE_EVENT_MAX];
#define G_LOG_DOMAIN "global_events"
namespace GlobalEvents {
static int fds[2];
static GIOChannel *channel;
static guint source_id;
static Mutex mutex;
static bool flags[MAX];
static Handler handlers[MAX];
}
/**
* Invoke the callback for a certain event.
*/
static void
event_pipe_invoke(enum pipe_event event)
InvokeGlobalEvent(GlobalEvents::Event event)
{
assert((unsigned)event < PIPE_EVENT_MAX);
assert(event_pipe_callbacks[event] != NULL);
assert((unsigned)event < GlobalEvents::MAX);
assert(GlobalEvents::handlers[event] != NULL);
event_pipe_callbacks[event]();
GlobalEvents::handlers[event]();
}
static gboolean
main_notify_event(G_GNUC_UNUSED GIOChannel *source,
G_GNUC_UNUSED GIOCondition condition,
G_GNUC_UNUSED gpointer data)
GlobalEventCallback(G_GNUC_UNUSED GIOChannel *source,
G_GNUC_UNUSED GIOCondition condition,
G_GNUC_UNUSED gpointer data)
{
char buffer[256];
gsize bytes_read;
GError *error = NULL;
GIOStatus status = g_io_channel_read_chars(event_channel,
GIOStatus status = g_io_channel_read_chars(GlobalEvents::channel,
buffer, sizeof(buffer),
&bytes_read, &error);
if (status == G_IO_STATUS_ERROR)
MPD_ERROR("error reading from pipe: %s", error->message);
bool events[PIPE_EVENT_MAX];
event_pipe_mutex.lock();
memcpy(events, pipe_events, sizeof(events));
memset(pipe_events, 0, sizeof(pipe_events));
event_pipe_mutex.unlock();
bool events[GlobalEvents::MAX];
GlobalEvents::mutex.lock();
memcpy(events, GlobalEvents::flags, sizeof(events));
memset(GlobalEvents::flags, 0,
sizeof(GlobalEvents::flags));
GlobalEvents::mutex.unlock();
for (unsigned i = 0; i < PIPE_EVENT_MAX; ++i)
for (unsigned i = 0; i < GlobalEvents::MAX; ++i)
if (events[i])
/* invoke the event handler */
event_pipe_invoke(pipe_event(i));
InvokeGlobalEvent(GlobalEvents::Event(i));
return true;
}
void event_pipe_init(void)
void
GlobalEvents::Initialize()
{
GIOChannel *channel;
int ret;
ret = pipe_cloexec_nonblock(event_pipe);
if (ret < 0)
if (pipe_cloexec_nonblock(fds) < 0)
MPD_ERROR("Couldn't open pipe: %s", strerror(errno));
#ifndef G_OS_WIN32
channel = g_io_channel_unix_new(event_pipe[0]);
channel = g_io_channel_unix_new(fds[0]);
#else
channel = g_io_channel_win32_new_fd(event_pipe[0]);
channel = g_io_channel_win32_new_fd(fds[0]);
#endif
g_io_channel_set_encoding(channel, NULL, NULL);
g_io_channel_set_buffered(channel, false);
event_pipe_source_id = g_io_add_watch(channel, G_IO_IN,
main_notify_event, NULL);
event_channel = channel;
source_id = g_io_add_watch(channel, G_IO_IN,
GlobalEventCallback, NULL);
}
void event_pipe_deinit(void)
void
GlobalEvents::Deinitialize()
{
g_source_remove(event_pipe_source_id);
g_io_channel_unref(event_channel);
g_source_remove(source_id);
g_io_channel_unref(channel);
#ifndef WIN32
/* By some strange reason this call hangs on Win32 */
close(event_pipe[0]);
close(fds[0]);
#endif
close(event_pipe[1]);
close(fds[1]);
}
void
event_pipe_register(enum pipe_event event, event_pipe_callback_t callback)
GlobalEvents::Register(Event event, Handler callback)
{
assert((unsigned)event < PIPE_EVENT_MAX);
assert(event_pipe_callbacks[event] == NULL);
assert((unsigned)event < MAX);
assert(handlers[event] == NULL);
event_pipe_callbacks[event] = callback;
handlers[event] = callback;
}
void event_pipe_emit(enum pipe_event event)
void
GlobalEvents::Emit(Event event)
{
ssize_t w;
assert((unsigned)event < MAX);
assert((unsigned)event < PIPE_EVENT_MAX);
event_pipe_mutex.lock();
if (pipe_events[event]) {
mutex.lock();
if (flags[event]) {
/* already set: don't write */
event_pipe_mutex.unlock();
mutex.unlock();
return;
}
pipe_events[event] = true;
event_pipe_mutex.unlock();
flags[event] = true;
mutex.unlock();
w = write(event_pipe[1], "", 1);
ssize_t w = write(fds[1], "", 1);
if (w < 0 && errno != EAGAIN && errno != EINTR)
MPD_ERROR("error writing to pipe: %s", strerror(errno));
}
void event_pipe_emit_fast(enum pipe_event event)
void
GlobalEvents::FastEmit(Event event)
{
assert((unsigned)event < PIPE_EVENT_MAX);
assert((unsigned)event < MAX);
pipe_events[event] = true;
flags[event] = true;
G_GNUC_UNUSED ssize_t nbytes = write(event_pipe[1], "", 1);
G_GNUC_UNUSED ssize_t nbytes = write(fds[1], "", 1);
}
......@@ -17,53 +17,54 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef MPD_EVENT_PIPE_HXX
#define MPD_EVENT_PIPE_HXX
#ifndef MPD_GLOBAL_EVENTS_HXX
#define MPD_GLOBAL_EVENTS_HXX
enum pipe_event {
/** database update was finished */
PIPE_EVENT_UPDATE,
namespace GlobalEvents {
enum Event {
/** database update was finished */
UPDATE,
/** during database update, a song was deleted */
PIPE_EVENT_DELETE,
/** during database update, a song was deleted */
DELETE,
/** an idle event was emitted */
PIPE_EVENT_IDLE,
/** an idle event was emitted */
IDLE,
/** must call playlist_sync() */
PIPE_EVENT_PLAYLIST,
/** must call playlist_sync() */
PLAYLIST,
/** the current song's tag has changed */
PIPE_EVENT_TAG,
/** the current song's tag has changed */
TAG,
/** SIGHUP received: reload configuration, roll log file */
PIPE_EVENT_RELOAD,
/** SIGHUP received: reload configuration, roll log file */
RELOAD,
/** a hardware mixer plugin has detected a change */
PIPE_EVENT_MIXER,
/** a hardware mixer plugin has detected a change */
MIXER,
/** shutdown requested */
PIPE_EVENT_SHUTDOWN,
/** shutdown requested */
SHUTDOWN,
PIPE_EVENT_MAX
};
MAX
};
typedef void (*event_pipe_callback_t)(void);
typedef void (*Handler)();
void event_pipe_init(void);
void Initialize();
void event_pipe_deinit(void);
void Deinitialize();
void
event_pipe_register(enum pipe_event event, event_pipe_callback_t callback);
void Register(Event event, Handler handler);
void event_pipe_emit(enum pipe_event event);
void Emit(Event event);
/**
* Similar to event_pipe_emit(), but aimed for use in signal handlers:
* it doesn't lock the mutex, and doesn't log on error. That makes it
* potentially lossy, but for its intended use, that does not matter.
*/
void event_pipe_emit_fast(enum pipe_event event);
/**
* Similar to event_pipe_emit(), but aimed for use in signal handlers:
* it doesn't lock the mutex, and doesn't log on error. That makes it
* potentially lossy, but for its intended use, that does not matter.
*/
void FastEmit(Event event);
}
#endif /* MAIN_NOTIFY_H */
......@@ -24,7 +24,7 @@
#include "config.h"
#include "Idle.hxx"
#include "EventPipe.hxx"
#include "GlobalEvents.hxx"
#include <assert.h>
#include <glib.h>
......@@ -71,7 +71,7 @@ idle_add(unsigned flags)
idle_flags |= flags;
g_mutex_unlock(idle_mutex);
event_pipe_emit(PIPE_EVENT_IDLE);
GlobalEvents::Emit(GlobalEvents::IDLE);
}
unsigned
......
......@@ -43,7 +43,7 @@
#include "Idle.hxx"
#include "SignalHandlers.hxx"
#include "Log.hxx"
#include "EventPipe.hxx"
#include "GlobalEvents.hxx"
extern "C" {
#include "daemon.h"
......@@ -316,7 +316,7 @@ initialize_decoder_and_player(void)
}
/**
* event_pipe callback function for PIPE_EVENT_IDLE
* Handler for GlobalEvents::IDLE.
*/
static void
idle_event_emitted(void)
......@@ -329,7 +329,7 @@ idle_event_emitted(void)
}
/**
* event_pipe callback function for PIPE_EVENT_SHUTDOWN
* Handler for GlobalEvents::SHUTDOWN.
*/
static void
shutdown_event_emitted(void)
......@@ -406,9 +406,9 @@ int mpd_main(int argc, char *argv[])
main_loop = g_main_loop_new(NULL, FALSE);
main_cond = g_cond_new();
event_pipe_init();
event_pipe_register(PIPE_EVENT_IDLE, idle_event_emitted);
event_pipe_register(PIPE_EVENT_SHUTDOWN, shutdown_event_emitted);
GlobalEvents::Initialize();
GlobalEvents::Register(GlobalEvents::IDLE, idle_event_emitted);
GlobalEvents::Register(GlobalEvents::SHUTDOWN, shutdown_event_emitted);
path_global_init();
......@@ -537,7 +537,7 @@ int mpd_main(int argc, char *argv[])
#endif
g_cond_free(main_cond);
event_pipe_deinit();
GlobalEvents::Deinitialize();
playlist_list_global_finish();
input_stream_global_finish();
......
......@@ -52,7 +52,7 @@ win32_main(int argc, char *argv[]);
* When running as a service reports to service control manager
* that our service is started.
* When running as a console application enables console handler that will
* trigger PIPE_EVENT_SHUTDOWN when user closes console window
* trigger GlobalEvents::SHUTDOWN when user closes console window
* or presses Ctrl+C.
* This function should be called just before entering main loop.
*/
......
......@@ -32,7 +32,7 @@
#include "OutputAll.hxx"
#include "tag.h"
#include "Idle.hxx"
#include "EventPipe.hxx"
#include "GlobalEvents.hxx"
#include <cmath>
......@@ -285,7 +285,7 @@ player_wait_for_decoder(struct player *player)
player_unlock(pc);
/* call syncPlaylistWithQueue() in the main thread */
event_pipe_emit(PIPE_EVENT_PLAYLIST);
GlobalEvents::Emit(GlobalEvents::PLAYLIST);
return true;
}
......@@ -665,7 +665,7 @@ update_song_tag(struct song *song, const struct tag *new_tag)
/* the main thread will update the playlist version when he
receives this event */
event_pipe_emit(PIPE_EVENT_TAG);
GlobalEvents::Emit(GlobalEvents::TAG);
/* notify all clients that the tag of the current song has
changed */
......@@ -909,7 +909,7 @@ static void do_play(struct player_control *pc, struct decoder_control *dc)
player_dc_stop(&player);
player_command_finished(pc);
music_pipe_free(player.pipe);
event_pipe_emit(PIPE_EVENT_PLAYLIST);
GlobalEvents::Emit(GlobalEvents::PLAYLIST);
player_lock(pc);
return;
}
......@@ -1086,7 +1086,7 @@ static void do_play(struct player_control *pc, struct decoder_control *dc)
player_unlock(pc);
event_pipe_emit(PIPE_EVENT_PLAYLIST);
GlobalEvents::Emit(GlobalEvents::PLAYLIST);
player_lock(pc);
}
......
......@@ -27,7 +27,7 @@
#include "Playlist.hxx"
#include "Main.hxx"
#include "Partition.hxx"
#include "EventPipe.hxx"
#include "GlobalEvents.hxx"
static void
playlist_tag_event(void)
......@@ -44,6 +44,6 @@ playlist_event(void)
void
playlist_global_init()
{
event_pipe_register(PIPE_EVENT_TAG, playlist_tag_event);
event_pipe_register(PIPE_EVENT_PLAYLIST, playlist_event);
GlobalEvents::Register(GlobalEvents::TAG, playlist_tag_event);
GlobalEvents::Register(GlobalEvents::PLAYLIST, playlist_event);
}
......@@ -24,7 +24,7 @@
#include "Log.hxx"
#include "Main.hxx"
#include "EventPipe.hxx"
#include "GlobalEvents.hxx"
#include "mpd_error.h"
#include <glib.h>
......@@ -40,7 +40,7 @@ static void exit_signal_handler(G_GNUC_UNUSED int signum)
static void reload_signal_handler(G_GNUC_UNUSED int signum)
{
event_pipe_emit_fast(PIPE_EVENT_RELOAD);
GlobalEvents::FastEmit(GlobalEvents::RELOAD);
}
static void
......@@ -73,7 +73,7 @@ void initSigHandlers(void)
x_sigaction(SIGINT, &sa);
x_sigaction(SIGTERM, &sa);
event_pipe_register(PIPE_EVENT_RELOAD, handle_reload_event);
GlobalEvents::Register(GlobalEvents::RELOAD, handle_reload_event);
sa.sa_handler = reload_signal_handler;
x_sigaction(SIGHUP, &sa);
#endif
......
......@@ -25,7 +25,7 @@
#include "Mapper.hxx"
#include "DatabaseSimple.hxx"
#include "Idle.hxx"
#include "EventPipe.hxx"
#include "GlobalEvents.hxx"
extern "C" {
#include "stats.h"
......@@ -92,7 +92,7 @@ static void * update_task(void *_path)
g_free(_path);
progress = UPDATE_PROGRESS_DONE;
event_pipe_emit(PIPE_EVENT_UPDATE);
GlobalEvents::Emit(GlobalEvents::UPDATE);
return NULL;
}
......@@ -173,7 +173,7 @@ static void update_finished_event(void)
void update_global_init(void)
{
event_pipe_register(PIPE_EVENT_UPDATE, update_finished_event);
GlobalEvents::Register(GlobalEvents::UPDATE, update_finished_event);
update_remove_global_init();
update_walk_global_init();
......
......@@ -21,7 +21,7 @@
#include "UpdateRemove.hxx"
#include "Playlist.hxx"
#include "Partition.hxx"
#include "EventPipe.hxx"
#include "GlobalEvents.hxx"
#include "song.h"
#include "Main.hxx"
......@@ -76,7 +76,7 @@ update_remove_global_init(void)
remove_mutex = g_mutex_new();
remove_cond = g_cond_new();
event_pipe_register(PIPE_EVENT_DELETE, song_remove_event);
GlobalEvents::Register(GlobalEvents::DELETE, song_remove_event);
}
void
......@@ -93,7 +93,7 @@ update_remove_song(const struct song *song)
removed_song = song;
event_pipe_emit(PIPE_EVENT_DELETE);
GlobalEvents::Emit(GlobalEvents::DELETE);
g_mutex_lock(remove_mutex);
......
......@@ -21,7 +21,7 @@
#include "Volume.hxx"
#include "MixerAll.hxx"
#include "Idle.hxx"
#include "EventPipe.hxx"
#include "GlobalEvents.hxx"
#include <glib.h>
......@@ -41,7 +41,7 @@ static int last_hardware_volume = -1;
static GTimer *hardware_volume_timer;
/**
* Handler for #PIPE_EVENT_MIXER.
* Handler for #GlobalEvents::MIXER.
*/
static void
mixer_event_callback(void)
......@@ -62,7 +62,7 @@ void volume_init(void)
{
hardware_volume_timer = g_timer_new();
event_pipe_register(PIPE_EVENT_MIXER, mixer_event_callback);
GlobalEvents::Register(GlobalEvents::MIXER, mixer_event_callback);
}
int volume_level_get(void)
......
......@@ -23,7 +23,7 @@
#ifdef WIN32
#include "mpd_error.h"
#include "EventPipe.hxx"
#include "GlobalEvents.hxx"
#include <glib.h>
......@@ -68,7 +68,7 @@ service_dispatcher(G_GNUC_UNUSED DWORD control, G_GNUC_UNUSED DWORD event_type,
switch (control) {
case SERVICE_CONTROL_SHUTDOWN:
case SERVICE_CONTROL_STOP:
event_pipe_emit(PIPE_EVENT_SHUTDOWN);
GlobalEvents::Emit(GlobalEvents::SHUTDOWN);
return NO_ERROR;
default:
return NO_ERROR;
......@@ -104,7 +104,7 @@ console_handler(DWORD event)
case CTRL_C_EVENT:
case CTRL_CLOSE_EVENT:
if (!ignore_console_events)
event_pipe_emit(PIPE_EVENT_SHUTDOWN);
GlobalEvents::Emit(GlobalEvents::SHUTDOWN);
return TRUE;
default:
return FALSE;
......
......@@ -20,7 +20,7 @@
#include "config.h"
#include "mixer_api.h"
#include "output_api.h"
#include "EventPipe.hxx"
#include "GlobalEvents.hxx"
#include <glib.h>
#include <alsa/asoundlib.h>
......@@ -211,7 +211,7 @@ static int
alsa_mixer_elem_callback(G_GNUC_UNUSED snd_mixer_elem_t *elem, unsigned mask)
{
if (mask & SND_CTL_EVENT_MASK_VALUE)
event_pipe_emit(PIPE_EVENT_MIXER);
GlobalEvents::Emit(GlobalEvents::MIXER);
return 0;
}
......
......@@ -22,7 +22,7 @@
#include "mixer_api.h"
#include "output/pulse_output_plugin.h"
#include "conf.h"
#include "EventPipe.hxx"
#include "GlobalEvents.hxx"
#include <glib.h>
......@@ -66,7 +66,7 @@ pulse_mixer_offline(struct pulse_mixer *pm)
pm->online = false;
event_pipe_emit(PIPE_EVENT_MIXER);
GlobalEvents::Emit(GlobalEvents::MIXER);
}
/**
......@@ -90,7 +90,7 @@ pulse_mixer_volume_cb(G_GNUC_UNUSED pa_context *context, const pa_sink_input_inf
pm->online = true;
pm->volume = i->volume;
event_pipe_emit(PIPE_EVENT_MIXER);
GlobalEvents::Emit(GlobalEvents::MIXER);
}
static void
......
......@@ -26,7 +26,7 @@ extern "C" {
}
#include "pcm_volume.h"
#include "EventPipe.hxx"
#include "GlobalEvents.hxx"
#include <glib.h>
......@@ -88,7 +88,7 @@ roar_output_set_volume(G_GNUC_UNUSED struct roar *roar,
#endif
void
event_pipe_emit(G_GNUC_UNUSED enum pipe_event event)
GlobalEvents::Emit(gcc_unused Event event)
{
}
......
......@@ -21,7 +21,7 @@
#include "OutputControl.hxx"
#include "conf.h"
#include "Idle.hxx"
#include "EventPipe.hxx"
#include "GlobalEvents.hxx"
extern "C" {
#include "output_plugin.h"
......@@ -43,7 +43,7 @@ extern "C" {
#include <stdlib.h>
void
event_pipe_emit(G_GNUC_UNUSED enum pipe_event event)
GlobalEvents::Emit(gcc_unused Event event)
{
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment