From 5e8bb1410e422e144538f1bf6e56dcf47ff0b99d Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 20 Aug 2004 20:20:20 +0000 Subject: add support for glib12 git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@150 fefdeb5f-60dc-0310-8127-8f9354f1896f --- polyp/Makefile.am | 15 +- polyp/glib-mainloop.h | 5 + polyp/glib12-mainloop.c | 497 ++++++++++++++++++++++++++++++++++++++++++++++++ polyp/mainloop-test.c | 35 +++- polyp/polypaudio.pa | 5 +- 5 files changed, 547 insertions(+), 10 deletions(-) create mode 100644 polyp/glib12-mainloop.c (limited to 'polyp') diff --git a/polyp/Makefile.am b/polyp/Makefile.am index f9f6b6ab..8acbc012 100644 --- a/polyp/Makefile.am +++ b/polyp/Makefile.am @@ -17,7 +17,8 @@ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 # USA. -AM_CFLAGS=-D_GNU_SOURCE -DDLSEARCHDIR=\"$(pkglibdir)\" -I$(top_srcdir) $(PTHREAD_CFLAGS) +AM_CFLAGS=-D_GNU_SOURCE -I$(top_srcdir) $(PTHREAD_CFLAGS) +#AM_CFLAGS+= -DDLSEARCHDIR=\"$(pkglibdir)\" AM_LDADD=$(PTHREAD_LIBS) AM_LIBADD=$(PTHREAD_LIBS) @@ -25,7 +26,7 @@ polypincludedir=$(includedir)/polyp EXTRA_DIST = polypaudio.pa depmod.py bin_PROGRAMS = polypaudio pacat pactl -noinst_PROGRAMS = mainloop-test mainloop-test-glib pacat-simple parec-simple +noinst_PROGRAMS = mainloop-test mainloop-test-glib mainloop-test-glib12 pacat-simple parec-simple BUILT_SOURCES=polyplib-version.h @@ -88,6 +89,7 @@ lib_LTLIBRARIES=libpolyp.la \ libpolyp-error.la \ libpolyp-mainloop.la \ libpolyp-mainloop-glib.la \ + libpolyp-mainloop-glib12.la \ libpolyp-simple.la polypaudio_SOURCES = idxset.c idxset.h \ @@ -333,6 +335,11 @@ libpolyp_mainloop_glib_la_CFLAGS = $(AM_CFLAGS) $(GLIB20_CFLAGS) libpolyp_mainloop_glib_la_LIBADD = $(AM_LIBADD) libpolyp-mainloop.la $(GLIB20_LIBS) libpolyp_mainloop_glib_la_LDFLAGS = -version-info 0:0:0 +libpolyp_mainloop_glib12_la_SOURCES = glib-mainloop.h glib12-mainloop.c +libpolyp_mainloop_glib12_la_CFLAGS = $(AM_CFLAGS) $(GLIB12_CFLAGS) +libpolyp_mainloop_glib12_la_LIBADD = $(AM_LIBADD) libpolyp-mainloop.la $(GLIB12_LIBS) +libpolyp_mainloop_glib12_la_LDFLAGS = -version-info 0:0:0 + pacat_SOURCES = pacat.c pacat_LDADD = $(AM_LDADD) libpolyp.la libpolyp-error.la libpolyp-mainloop.la pacat_CFLAGS = $(AM_CFLAGS) @@ -357,6 +364,10 @@ mainloop_test_glib_SOURCES = $(mainloop_test_SOURCES) mainloop_test_glib_CFLAGS = $(mainloop_test_CFLAGS) $(GLIB20_CFLAGS) -DGLIB_MAIN_LOOP mainloop_test_glib_LDADD = $(mainloop_test_LDADD) $(GLIB20_LIBS) libpolyp-mainloop-glib.la +mainloop_test_glib12_SOURCES = $(mainloop_test_SOURCES) +mainloop_test_glib12_CFLAGS = $(mainloop_test_CFLAGS) $(GLIB12_CFLAGS) -DGLIB_MAIN_LOOP +mainloop_test_glib12_LDADD = $(mainloop_test_LDADD) $(GLIB12_LIBS) libpolyp-mainloop-glib12.la + if BUILD_LIBPOLYPCORE polypinclude_HEADERS+=cli-command.h\ diff --git a/polyp/glib-mainloop.h b/polyp/glib-mainloop.h index 1fc7f64a..afc83be2 100644 --- a/polyp/glib-mainloop.h +++ b/polyp/glib-mainloop.h @@ -37,7 +37,12 @@ PA_C_DECL_BEGIN struct pa_glib_mainloop; /** Create a new GLIB main loop object for the specified GLIB main loop context. If c is NULL the default context is used. */ +#if GLIB_MAJOR_VERSION >= 2 struct pa_glib_mainloop *pa_glib_mainloop_new(GMainContext *c); +#else +struct pa_glib_mainloop *pa_glib_mainloop_new(void); +#endif + /** Free the GLIB main loop object */ void pa_glib_mainloop_free(struct pa_glib_mainloop* g); diff --git a/polyp/glib12-mainloop.c b/polyp/glib12-mainloop.c new file mode 100644 index 00000000..c7085cc9 --- /dev/null +++ b/polyp/glib12-mainloop.c @@ -0,0 +1,497 @@ +/* $Id$ */ + +/*** + This file is part of polypaudio. + + polypaudio 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. + + polypaudio 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 polypaudio; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + USA. +***/ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include + +#include "glib-mainloop.h" +#include "idxset.h" +#include "xmalloc.h" + +struct pa_io_event { + struct pa_glib_mainloop *mainloop; + int dead; + GIOChannel *io_channel; + guint source; + GIOCondition io_condition; + int fd; + void (*callback) (struct pa_mainloop_api*m, struct pa_io_event *e, int fd, enum pa_io_event_flags f, void *userdata); + void *userdata; + void (*destroy_callback) (struct pa_mainloop_api *m, struct pa_io_event*e, void *userdata); + struct pa_io_event *next, *prev; +}; + +struct pa_time_event { + struct pa_glib_mainloop *mainloop; + int dead; + guint source; + struct timeval timeval; + void (*callback) (struct pa_mainloop_api*m, struct pa_time_event *e, const struct timeval *tv, void *userdata); + void *userdata; + void (*destroy_callback) (struct pa_mainloop_api *m, struct pa_time_event*e, void *userdata); + struct pa_time_event *next, *prev; +}; + +struct pa_defer_event { + struct pa_glib_mainloop *mainloop; + int dead; + guint source; + void (*callback) (struct pa_mainloop_api*m, struct pa_defer_event *e, void *userdata); + void *userdata; + void (*destroy_callback) (struct pa_mainloop_api *m, struct pa_defer_event*e, void *userdata); + struct pa_defer_event *next, *prev; +}; + +struct pa_glib_mainloop { + struct pa_mainloop_api api; + guint cleanup_source; + struct pa_io_event *io_events, *dead_io_events; + struct pa_time_event *time_events, *dead_time_events; + struct pa_defer_event *defer_events, *dead_defer_events; +}; + +static void schedule_free_dead_events(struct pa_glib_mainloop *g); + +static void glib_io_enable(struct pa_io_event*e, enum pa_io_event_flags f); + +static struct pa_io_event* glib_io_new(struct pa_mainloop_api*m, int fd, enum pa_io_event_flags f, void (*callback) (struct pa_mainloop_api*m, struct pa_io_event*e, int fd, enum pa_io_event_flags f, void *userdata), void *userdata) { + struct pa_io_event *e; + struct pa_glib_mainloop *g; + + assert(m && m->userdata && fd >= 0 && callback); + g = m->userdata; + + e = pa_xmalloc(sizeof(struct pa_io_event)); + e->mainloop = m->userdata; + e->dead = 0; + e->fd = fd; + e->callback = callback; + e->userdata = userdata; + e->destroy_callback = NULL; + + e->io_channel = g_io_channel_unix_new(e->fd); + assert(e->io_channel); + e->source = (guint) -1; + e->io_condition = 0; + + glib_io_enable(e, f); + + e->next = g->io_events; + if (e->next) e->next->prev = e; + g->io_events = e; + e->prev = NULL; + + return e; +} + +static gboolean io_cb(GIOChannel *source, GIOCondition condition, gpointer data) { + struct pa_io_event *e = data; + enum pa_io_event_flags f; + assert(source && e && e->io_channel == source); + + f = (condition & G_IO_IN ? PA_IO_EVENT_INPUT : 0) | + (condition & G_IO_OUT ? PA_IO_EVENT_OUTPUT : 0) | + (condition & G_IO_ERR ? PA_IO_EVENT_ERROR : 0) | + (condition & G_IO_HUP ? PA_IO_EVENT_HANGUP : 0); + + e->callback(&e->mainloop->api, e, e->fd, f, e->userdata); + return TRUE; +} + +static void glib_io_enable(struct pa_io_event*e, enum pa_io_event_flags f) { + GIOCondition c; + assert(e && !e->dead); + + c = (f & PA_IO_EVENT_INPUT ? G_IO_IN : 0) | (f & PA_IO_EVENT_OUTPUT ? G_IO_OUT : 0); + + if (c == e->io_condition) + return; + + if (e->source != (guint) -1) + g_source_remove(e->source); + + e->source = g_io_add_watch_full(e->io_channel, G_PRIORITY_DEFAULT, c | G_IO_ERR | G_IO_HUP, io_cb, e, NULL); + assert(e->source != (guint) -1); + e->io_condition = c; +} + +static void glib_io_free(struct pa_io_event*e) { + assert(e && !e->dead); + + if (e->source != (guint) -1) { + g_source_remove(e->source); + e->source = (guint) -1; + } + + if (e->prev) + e->prev->next = e->next; + else + e->mainloop->io_events = e->next; + + if (e->next) + e->next->prev = e->prev; + + if ((e->next = e->mainloop->dead_io_events)) + e->next->prev = e; + + e->mainloop->dead_io_events = e; + e->prev = NULL; + + e->dead = 1; + schedule_free_dead_events(e->mainloop); +} + +static void glib_io_set_destroy(struct pa_io_event*e, void (*callback)(struct pa_mainloop_api*m, struct pa_io_event *e, void *userdata)) { + assert(e); + e->destroy_callback = callback; +} + +/* Time sources */ + +static void glib_time_restart(struct pa_time_event*e, const struct timeval *tv); + +static struct pa_time_event* glib_time_new(struct pa_mainloop_api*m, const struct timeval *tv, void (*callback) (struct pa_mainloop_api*m, struct pa_time_event*e, const struct timeval *tv, void *userdata), void *userdata) { + struct pa_glib_mainloop *g; + struct pa_time_event *e; + + assert(m && m->userdata && tv && callback); + g = m->userdata; + + e = pa_xmalloc(sizeof(struct pa_time_event)); + e->mainloop = g; + e->dead = 0; + e->callback = callback; + e->userdata = userdata; + e->destroy_callback = NULL; + e->source = (guint) -1; + + glib_time_restart(e, tv); + + e->next = g->time_events; + if (e->next) e->next->prev = e; + g->time_events = e; + e->prev = NULL; + + return e; +} + +static guint msec_diff(const struct timeval *a, const struct timeval *b) { + guint r; + assert(a && b); + + if (a->tv_sec < b->tv_sec) + return 0; + + if (a->tv_sec == b->tv_sec && a->tv_sec <= b->tv_sec) + return 0; + + r = (a->tv_sec-b->tv_sec)*1000; + + if (a->tv_usec >= b->tv_usec) + r += (a->tv_usec - b->tv_usec) / 1000; + else + r -= (b->tv_usec - a->tv_usec) / 1000; + + return r; +} + +static gboolean time_cb(gpointer data) { + struct pa_time_event* e = data; + assert(e && e->mainloop && e->source != (guint) -1); + + g_source_remove(e->source); + e->source = (guint) -1; + + e->callback(&e->mainloop->api, e, &e->timeval, e->userdata); + return FALSE; +} + +static void glib_time_restart(struct pa_time_event*e, const struct timeval *tv) { + struct timeval now; + assert(e && e->mainloop && !e->dead); + + gettimeofday(&now, NULL); + if (e->source != (guint) -1) + g_source_remove(e->source); + + if (tv) { + e->timeval = *tv; + e->source = g_timeout_add_full(G_PRIORITY_DEFAULT, msec_diff(tv, &now), time_cb, e, NULL); + assert(e->source != (guint) -1); + } else + e->source = (guint) -1; + } + +static void glib_time_free(struct pa_time_event *e) { + assert(e && e->mainloop && !e->dead); + + if (e->source != (guint) -1) { + g_source_remove(e->source); + e->source = (guint) -1; + } + + if (e->prev) + e->prev->next = e->next; + else + e->mainloop->time_events = e->next; + + if (e->next) + e->next->prev = e->prev; + + if ((e->next = e->mainloop->dead_time_events)) + e->next->prev = e; + + e->mainloop->dead_time_events = e; + e->prev = NULL; + + e->dead = 1; + schedule_free_dead_events(e->mainloop); +} + +static void glib_time_set_destroy(struct pa_time_event *e, void (*callback)(struct pa_mainloop_api*m, struct pa_time_event*e, void *userdata)) { + assert(e); + e->destroy_callback = callback; +} + +/* Deferred sources */ + +static void glib_defer_enable(struct pa_defer_event *e, int b); + +static struct pa_defer_event* glib_defer_new(struct pa_mainloop_api*m, void (*callback) (struct pa_mainloop_api*m, struct pa_defer_event *e, void *userdata), void *userdata) { + struct pa_defer_event *e; + struct pa_glib_mainloop *g; + + assert(m && m->userdata && callback); + g = m->userdata; + + e = pa_xmalloc(sizeof(struct pa_defer_event)); + e->mainloop = g; + e->dead = 0; + e->callback = callback; + e->userdata = userdata; + e->destroy_callback = NULL; + e->source = (guint) -1; + + glib_defer_enable(e, 1); + + e->next = g->defer_events; + if (e->next) e->next->prev = e; + g->defer_events = e; + e->prev = NULL; + return e; +} + +static gboolean idle_cb(gpointer data) { + struct pa_defer_event* e = data; + assert(e && e->mainloop && e->source != (guint) -1); + + e->callback(&e->mainloop->api, e, e->userdata); + return TRUE; +} + +static void glib_defer_enable(struct pa_defer_event *e, int b) { + assert(e && e->mainloop); + + if (e->source != (guint) -1 && !b) { + g_source_remove(e->source); + e->source = (guint) -1; + } else if (e->source == (guint) -1 && b) { + e->source = g_idle_add_full(G_PRIORITY_HIGH, idle_cb, e, NULL); + assert(e->source != (guint) -1); + } +} + +static void glib_defer_free(struct pa_defer_event *e) { + assert(e && e->mainloop && !e->dead); + + if (e->source != (guint) -1) { + g_source_remove(e->source); + e->source = (guint) -1; + } + + if (e->prev) + e->prev->next = e->next; + else + e->mainloop->defer_events = e->next; + + if (e->next) + e->next->prev = e->prev; + + if ((e->next = e->mainloop->dead_defer_events)) + e->next->prev = e; + + e->mainloop->dead_defer_events = e; + e->prev = NULL; + + e->dead = 1; + schedule_free_dead_events(e->mainloop); +} + +static void glib_defer_set_destroy(struct pa_defer_event *e, void (*callback)(struct pa_mainloop_api *m, struct pa_defer_event *e, void *userdata)) { + assert(e); + e->destroy_callback = callback; +} + +/* quit() */ + +static void glib_quit(struct pa_mainloop_api*a, int retval) { + struct pa_glib_mainloop *g; + assert(a && a->userdata); + g = a->userdata; + + /* NOOP */ +} + +static const struct pa_mainloop_api vtable = { + .userdata = NULL, + + .io_new = glib_io_new, + .io_enable = glib_io_enable, + .io_free = glib_io_free, + .io_set_destroy= glib_io_set_destroy, + + .time_new = glib_time_new, + .time_restart = glib_time_restart, + .time_free = glib_time_free, + .time_set_destroy = glib_time_set_destroy, + + .defer_new = glib_defer_new, + .defer_enable = glib_defer_enable, + .defer_free = glib_defer_free, + .defer_set_destroy = glib_defer_set_destroy, + + .quit = glib_quit, +}; + +struct pa_glib_mainloop *pa_glib_mainloop_new(void) { + struct pa_glib_mainloop *g; + + g = pa_xmalloc(sizeof(struct pa_glib_mainloop)); + + g->api = vtable; + g->api.userdata = g; + + g->io_events = g->dead_io_events = NULL; + g->time_events = g->dead_time_events = NULL; + g->defer_events = g->dead_defer_events = NULL; + + g->cleanup_source = (guint) -1; + return g; +} + +static void free_io_events(struct pa_io_event *e) { + while (e) { + struct pa_io_event *r = e; + e = r->next; + + if (r->source != (guint) -1) + g_source_remove(r->source); + + if (r->io_channel) + g_io_channel_unref(r->io_channel); + + if (r->destroy_callback) + r->destroy_callback(&r->mainloop->api, r, r->userdata); + + pa_xfree(r); + } +} + +static void free_time_events(struct pa_time_event *e) { + while (e) { + struct pa_time_event *r = e; + e = r->next; + + if (r->source != (guint) -1) + g_source_remove(r->source); + + if (r->destroy_callback) + r->destroy_callback(&r->mainloop->api, r, r->userdata); + + pa_xfree(r); + } +} + +static void free_defer_events(struct pa_defer_event *e) { + while (e) { + struct pa_defer_event *r = e; + e = r->next; + + if (r->source != (guint) -1) + g_source_remove(r->source); + + if (r->destroy_callback) + r->destroy_callback(&r->mainloop->api, r, r->userdata); + + pa_xfree(r); + } +} + +void pa_glib_mainloop_free(struct pa_glib_mainloop* g) { + assert(g); + + free_io_events(g->io_events); + free_io_events(g->dead_io_events); + free_defer_events(g->defer_events); + free_defer_events(g->dead_defer_events); + free_time_events(g->time_events); + free_time_events(g->dead_time_events); + + if (g->cleanup_source != (guint) -1) + g_source_remove(g->cleanup_source); + + pa_xfree(g); +} + +struct pa_mainloop_api* pa_glib_mainloop_get_api(struct pa_glib_mainloop *g) { + assert(g); + return &g->api; +} + +static gboolean free_dead_events(gpointer p) { + struct pa_glib_mainloop *g = p; + assert(g); + + free_io_events(g->dead_io_events); + free_defer_events(g->dead_defer_events); + free_time_events(g->dead_time_events); + + g->dead_io_events = NULL; + g->dead_defer_events = NULL; + g->dead_time_events = NULL; + + g_source_remove(g->cleanup_source); + g->cleanup_source = (guint) -1; + + return FALSE; +} + +static void schedule_free_dead_events(struct pa_glib_mainloop *g) { + assert(g); + + if (g->cleanup_source != (guint) -1) + return; + + g->cleanup_source = g_idle_add_full(G_PRIORITY_HIGH, free_dead_events, g, NULL); +} diff --git a/polyp/mainloop-test.c b/polyp/mainloop-test.c index c2517065..92b83c14 100644 --- a/polyp/mainloop-test.c +++ b/polyp/mainloop-test.c @@ -29,12 +29,21 @@ #include #ifdef GLIB_MAIN_LOOP + #include #include "glib-mainloop.h" static GMainLoop* glib_main_loop = NULL; + +#if GLIB_MAJOR_VERSION >= 2 +#define GLIB20 #else +#undef GLIB20 +#endif + + +#else /* GLIB_MAIN_LOOP */ #include "mainloop.h" -#endif +#endif /* GLIB_MAIN_LOOP */ static struct pa_defer_event *de; @@ -53,8 +62,10 @@ static void dcb(struct pa_mainloop_api*a, struct pa_defer_event *e, void *userda static void tcb(struct pa_mainloop_api*a, struct pa_time_event *e, const struct timeval *tv, void *userdata) { fprintf(stderr, "TIME EVENT\n"); -#ifdef GLIB_MAIN_LOOP +#if defined(GLIB_MAIN_LOOP) && defined(GLIB20) g_main_loop_quit(glib_main_loop); +#elif defined(GLIB_MAIN_LOOP) + g_main_quit(glib_main_loop); #else a->quit(a, 0); #endif @@ -68,15 +79,23 @@ int main(int argc, char *argv[]) { #ifdef GLIB_MAIN_LOOP struct pa_glib_mainloop *g; + +#ifdef GLIB20 glib_main_loop = g_main_loop_new(NULL, FALSE); assert(glib_main_loop); g = pa_glib_mainloop_new(NULL); +#else /* GLIB20 */ + glib_main_loop = g_main_new(FALSE); + assert(glib_main_loop); + + g = pa_glib_mainloop_new(); +#endif /* GLIB20 */ assert(g); a = pa_glib_mainloop_get_api(g); assert(a); -#else +#else /* GLIB_MAIN_LOOP */ struct pa_mainloop *m; m = pa_mainloop_new(); @@ -84,7 +103,7 @@ int main(int argc, char *argv[]) { a = pa_mainloop_get_api(m); assert(a); -#endif +#endif /* GLIB_MAIN_LOOP */ ioe = a->io_new(a, 0, PA_IO_EVENT_INPUT, iocb, NULL); assert(ioe); @@ -96,8 +115,10 @@ int main(int argc, char *argv[]) { tv.tv_sec += 10; te = a->time_new(a, &tv, tcb, NULL); -#ifdef GLIB_MAIN_LOOP +#if defined(GLIB_MAIN_LOOP) && defined(GLIB20) g_main_loop_run(glib_main_loop); +#elif defined(GLIB_MAIN_LOOP) + g_main_run(glib_main_loop); #else pa_mainloop_run(m, NULL); #endif @@ -108,7 +129,11 @@ int main(int argc, char *argv[]) { #ifdef GLIB_MAIN_LOOP pa_glib_mainloop_free(g); +#ifdef GLIB20 g_main_loop_unref(glib_main_loop); +#else + g_main_destroy(glib_main_loop); +#endif #else pa_mainloop_free(m); #endif diff --git a/polyp/polypaudio.pa b/polyp/polypaudio.pa index 9f8cf860..c63e1479 100755 --- a/polyp/polypaudio.pa +++ b/polyp/polypaudio.pa @@ -30,8 +30,8 @@ load module-pipe-sink #autoload_sink_add output module-oss device="/dev/dsp" sink_name=output source_name=input #autoload_source_add input module-oss device="/dev/dsp" sink_name=output source_name=input -#autoload_sink_add output module-oss-mmap device="/dev/dsp" sink_name=output source_name=input -#autoload_source_add input module-oss-mmap device="/dev/dsp" sink_name=output source_name=input +autoload_sink_add output module-oss-mmap device="/dev/dsp" sink_name=output source_name=input +autoload_source_add input module-oss-mmap device="/dev/dsp" sink_name=output source_name=input #autoload_sink_add output module-alsa-sink sink_name=output #autoload_source_add input module-alsa-source source_name=input @@ -52,7 +52,6 @@ source_default input # Load something to the sample cache scache_load /usr/share/sounds/KDE_Notify.wav x11-bell -scache_play x11-bell output # Load X11 bell module load module-x11-bell sample=x11-bell sink=output -- cgit