From b565b6950bbd689be453983e3940235003ffcc45 Mon Sep 17 00:00:00 2001 From: Benjamin Franzke Date: Thu, 20 Oct 2011 08:48:36 +0200 Subject: g_unix_signal: Remove g_ prefix to not collide with glib There is a g_unix_signal_new function in glib as well now, but that is only allowed to be used for SIGINT, SIGHUP and SIGTERM, so we have to stay with our own. --- src/Makefile.am | 4 +- src/g_unix_signal.c | 136 ---------------------------------------------------- src/g_unix_signal.h | 10 ---- src/interface.c | 4 +- src/unix_signal.c | 136 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/unix_signal.h | 14 ++++++ 6 files changed, 154 insertions(+), 150 deletions(-) delete mode 100644 src/g_unix_signal.c delete mode 100644 src/g_unix_signal.h create mode 100644 src/unix_signal.c create mode 100644 src/unix_signal.h diff --git a/src/Makefile.am b/src/Makefile.am index 690ed8d..6079b35 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,8 +1,8 @@ bin_PROGRAMS = pa-sink-ctl -pa_sink_ctl_SOURCES = interface.c pa-sink-ctl.c sink.c sink_input.c g_unix_signal.c +pa_sink_ctl_SOURCES = interface.c pa-sink-ctl.c sink.c sink_input.c unix_signal.c AM_CFLAGS = $(GCC_CFLAGS) AM_CPPFLAGS = $(PULSE_CFLAGS) $(PULSE_MAINLOOP_CFLAGS) $(GLIB_CFLAGS) pa_sink_ctl_LDADD = $(GLIB_LIBS) $(PULSE_LIBS) $(PULSE_MAINLOOP_LIBS) $(CURSES_LIBS) -noinst_HEADERS = interface.h pa-sink-ctl.h sink.h sink_input.h g_unix_signal.h +noinst_HEADERS = interface.h pa-sink-ctl.h sink.h sink_input.h unix_signal.h diff --git a/src/g_unix_signal.c b/src/g_unix_signal.c deleted file mode 100644 index e91df17..0000000 --- a/src/g_unix_signal.c +++ /dev/null @@ -1,136 +0,0 @@ -#define _POSIX_SOURCE -#include -#include -#include "g_unix_signal.h" - -static GPtrArray *signal_data = NULL; - -typedef struct _GUnixSignalData { - guint source_id; - GMainContext *context; - gboolean triggered; - gint signum; -} GUnixSignalData; - -typedef struct _GUnixSignalSource { - GSource source; - GUnixSignalData *data; -} GUnixSignalSource; - -static inline GUnixSignalData * -g_unix_signal_data(guint index) -{ - return (GUnixSignalData *) g_ptr_array_index(signal_data, index); -} - -static void -handler(gint signum) -{ - g_assert(signal_data != NULL); - for (guint i = 0; i < signal_data->len; ++i) - if (g_unix_signal_data(i)->signum == signum) - g_unix_signal_data(i)->triggered = TRUE; - sigaction(signum, &(struct sigaction){handler}, NULL); -} - -static gboolean -check(GSource *source) -{ - GUnixSignalSource *signal_source = (GUnixSignalSource *) source; - - return signal_source->data->triggered; -} - -static gboolean -prepare(GSource *source, gint *timeout_) -{ - GUnixSignalSource *signal_source = (GUnixSignalSource*) source; - - if (signal_source->data->context == NULL) { - g_main_context_ref(signal_source->data->context = g_source_get_context(source)); - signal_source->data->source_id = g_source_get_id(source); - } - - *timeout_ = -1; - - return signal_source->data->triggered; -} - -static gboolean -dispatch(GSource *source, GSourceFunc callback, gpointer user_data) -{ - GUnixSignalSource *signal_source = (GUnixSignalSource *) source; - - signal_source->data->triggered = FALSE; - - return callback(user_data) ? TRUE : FALSE; -} - -static void -finalize(GSource *source) -{ - GUnixSignalSource *signal_source = (GUnixSignalSource*) source; - - sigaction(signal_source->data->signum, &(struct sigaction){NULL}, NULL); - g_main_context_unref(signal_source->data->context); - g_ptr_array_remove_fast(signal_data, signal_source->data); - if (signal_data->len == 0) - signal_data = (GPtrArray*) g_ptr_array_free(signal_data, TRUE); - g_free(signal_source->data); - -} -static GSourceFuncs SourceFuncs = -{ - .prepare = prepare, - .check = check, - .dispatch = dispatch, - .finalize = finalize, - .closure_callback = NULL, .closure_marshal = NULL -}; - -static void -g_unix_signal_source_init(GSource *source, gint signum) -{ - GUnixSignalSource *signal_source = (GUnixSignalSource *) source; - - signal_source->data = g_new(GUnixSignalData, 1); - signal_source->data->triggered = FALSE; - signal_source->data->signum = signum; - signal_source->data->context = NULL; - - if (signal_data == NULL) - signal_data = g_ptr_array_new(); - g_ptr_array_add(signal_data, signal_source->data); -} - -GSource * -g_unix_signal_source_new(gint signum) -{ - GSource *source = g_source_new(&SourceFuncs, sizeof(GUnixSignalSource)); - - g_unix_signal_source_init(source, signum); - sigaction(signum, &(struct sigaction){handler}, NULL); - - return source; -} - -guint -g_unix_signal_add_full(gint priority, gint signum, GSourceFunc function, gpointer data, GDestroyNotify notify) -{ - g_return_val_if_fail(function != NULL, 0); - GSource *source = g_unix_signal_source_new(signum); - guint id; - - if (priority != G_PRIORITY_DEFAULT) - g_source_set_priority (source, priority); - g_source_set_callback(source, function, data, notify); - id = g_source_attach(source, NULL); - g_source_unref(source); - - return id; -} - -guint g_unix_signal_add(gint signum, GSourceFunc function, gpointer data) -{ - return g_unix_signal_add_full(G_PRIORITY_DEFAULT, signum, function, data, NULL); -} diff --git a/src/g_unix_signal.h b/src/g_unix_signal.h deleted file mode 100644 index f9d6f8b..0000000 --- a/src/g_unix_signal.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef G_UNIX_SIGNAL_H -#define G_UNIX_SIGNAL_H - -#include - -GSource *g_unix_signal_source_new(gint signum); -guint g_unix_signal_add(gint signum, GSourceFunc function, gpointer data); -guint g_unix_signal_add_full(gint priority, gint signum, GSourceFunc function, gpointer data, GDestroyNotify notify); - -#endif /* G_UNIX_SIGNAL_H */ diff --git a/src/interface.c b/src/interface.c index 0513687..83bd3b0 100644 --- a/src/interface.c +++ b/src/interface.c @@ -10,7 +10,7 @@ #include "sink.h" #include "pa-sink-ctl.h" -#include "g_unix_signal.h" +#include "unix_signal.h" #define H_MSG_BOX 3 @@ -379,7 +379,7 @@ interface_init(void) interface_resize(NULL); /* register event handler for resize and input */ - resize_source_id = g_unix_signal_add(SIGWINCH, interface_resize, NULL); + resize_source_id = unix_signal_add(SIGWINCH, interface_resize, NULL); input_channel = g_io_channel_unix_new(STDIN_FILENO); if (!input_channel) exit(EXIT_FAILURE); diff --git a/src/unix_signal.c b/src/unix_signal.c new file mode 100644 index 0000000..7577051 --- /dev/null +++ b/src/unix_signal.c @@ -0,0 +1,136 @@ +#define _POSIX_SOURCE +#include +#include +#include "unix_signal.h" + +static GPtrArray *signal_data = NULL; + +typedef struct _UnixSignalData { + guint source_id; + GMainContext *context; + gboolean triggered; + gint signum; +} UnixSignalData; + +typedef struct _UnixSignalSource { + GSource source; + UnixSignalData *data; +} UnixSignalSource; + +static inline UnixSignalData * +unix_signal_data(guint index) +{ + return (UnixSignalData *) g_ptr_array_index(signal_data, index); +} + +static void +handler(gint signum) +{ + g_assert(signal_data != NULL); + for (guint i = 0; i < signal_data->len; ++i) + if (unix_signal_data(i)->signum == signum) + unix_signal_data(i)->triggered = TRUE; + sigaction(signum, &(struct sigaction){handler}, NULL); +} + +static gboolean +check(GSource *source) +{ + UnixSignalSource *signal_source = (UnixSignalSource *) source; + + return signal_source->data->triggered; +} + +static gboolean +prepare(GSource *source, gint *timeout_) +{ + UnixSignalSource *signal_source = (UnixSignalSource*) source; + + if (signal_source->data->context == NULL) { + g_main_context_ref(signal_source->data->context = g_source_get_context(source)); + signal_source->data->source_id = g_source_get_id(source); + } + + *timeout_ = -1; + + return signal_source->data->triggered; +} + +static gboolean +dispatch(GSource *source, GSourceFunc callback, gpointer user_data) +{ + UnixSignalSource *signal_source = (UnixSignalSource *) source; + + signal_source->data->triggered = FALSE; + + return callback(user_data) ? TRUE : FALSE; +} + +static void +finalize(GSource *source) +{ + UnixSignalSource *signal_source = (UnixSignalSource*) source; + + sigaction(signal_source->data->signum, &(struct sigaction){NULL}, NULL); + g_main_context_unref(signal_source->data->context); + g_ptr_array_remove_fast(signal_data, signal_source->data); + if (signal_data->len == 0) + signal_data = (GPtrArray*) g_ptr_array_free(signal_data, TRUE); + g_free(signal_source->data); + +} +static GSourceFuncs SourceFuncs = +{ + .prepare = prepare, + .check = check, + .dispatch = dispatch, + .finalize = finalize, + .closure_callback = NULL, .closure_marshal = NULL +}; + +static void +unix_signal_source_init(GSource *source, gint signum) +{ + UnixSignalSource *signal_source = (UnixSignalSource *) source; + + signal_source->data = g_new(UnixSignalData, 1); + signal_source->data->triggered = FALSE; + signal_source->data->signum = signum; + signal_source->data->context = NULL; + + if (signal_data == NULL) + signal_data = g_ptr_array_new(); + g_ptr_array_add(signal_data, signal_source->data); +} + +GSource * +unix_signal_source_new(gint signum) +{ + GSource *source = g_source_new(&SourceFuncs, sizeof(UnixSignalSource)); + + unix_signal_source_init(source, signum); + sigaction(signum, &(struct sigaction){handler}, NULL); + + return source; +} + +guint +unix_signal_add_full(gint priority, gint signum, GSourceFunc function, gpointer data, GDestroyNotify notify) +{ + g_return_val_if_fail(function != NULL, 0); + GSource *source = unix_signal_source_new(signum); + guint id; + + if (priority != G_PRIORITY_DEFAULT) + g_source_set_priority (source, priority); + g_source_set_callback(source, function, data, notify); + id = g_source_attach(source, NULL); + g_source_unref(source); + + return id; +} + +guint unix_signal_add(gint signum, GSourceFunc function, gpointer data) +{ + return unix_signal_add_full(G_PRIORITY_DEFAULT, signum, function, data, NULL); +} diff --git a/src/unix_signal.h b/src/unix_signal.h new file mode 100644 index 0000000..2e1aab6 --- /dev/null +++ b/src/unix_signal.h @@ -0,0 +1,14 @@ +#ifndef UNIX_SIGNAL_H +#define UNIX_SIGNAL_H + +#include + +GSource * +unix_signal_source_new(gint signum); +guint +unix_signal_add(gint signum, GSourceFunc function, gpointer data); +guint +unix_signal_add_full(gint priority, gint signum, GSourceFunc function, + gpointer data, GDestroyNotify notify); + +#endif /* UNIX_SIGNAL_H */ -- cgit