From 68eb5dd2486611d08d1d14e02ff1624ec27997c7 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 6 Aug 2004 23:07:48 +0000 Subject: add mainloop test utility fix glib mainloop support git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@106 fefdeb5f-60dc-0310-8127-8f9354f1896f --- polyp/Makefile.am | 16 ++++-- polyp/glib-mainloop.c | 147 +++++++++++++++++++++++++------------------------- polyp/glib-mainloop.h | 11 +++- polyp/glib-test.c | 92 +++++++++++++++++++++++++++++++ polyp/mainloop-api.h | 8 +++ polyp/polyplib-def.h | 8 +++ polyp/polyplib.h | 8 +++ polyp/sample.h | 8 +++ 8 files changed, 221 insertions(+), 77 deletions(-) create mode 100644 polyp/glib-test.c diff --git a/polyp/Makefile.am b/polyp/Makefile.am index c0be8ce0..1be04f41 100644 --- a/polyp/Makefile.am +++ b/polyp/Makefile.am @@ -21,18 +21,21 @@ AM_CFLAGS=-ansi -D_GNU_SOURCE -DDLSEARCHDIR=\"$(pkglibdir)\" -I$(srcdir)/.. AM_LDADD=-L. AM_LIBADD=-L. +polypincludedir=$(includedir)/polyp + EXTRA_DIST = polypaudio.pa depmod.py bin_PROGRAMS = polypaudio pacat pactl -noinst_PROGRAMS = pacat-simple parec-simple +noinst_PROGRAMS = pacat-simple parec-simple glib-test -pkginclude_HEADERS=polyplib.h \ +polypinclude_HEADERS=polyplib.h \ polyplib-def.h \ polyplib-simple.h \ polyplib-error.h \ mainloop-api.h \ mainloop.h \ mainloop-signal.h \ - sample.h + sample.h \ + glib-mainloop.h pkglib_LTLIBRARIES=libiochannel.la \ libsocket-server.la \ @@ -312,9 +315,13 @@ libpolyp_mainloop_glib_la_SOURCES = glib-mainloop.h glib-mainloop.c libpolyp_mainloop_glib_la_CFLAGS = $(AM_CFLAGS) $(GLIB20_CFLAGS) libpolyp_mainloop_glib_la_LIBADD = $(AM_LIBADD) libpolyp-mainloop.la $(GLIB20_LIBS) +glib_test_SOURCES = glib-test.c +glib_test_CFLAGS = $(AM_CFLAGS) $(GLIB20_CFLAGS) +glib_test_LDADD = $(AM_LDADD) $(GLIB20_LIBS) libpolyp.la libpolyp-mainloop-glib.la + if BUILD_LIBPOLYPCORE -pkginclude_HEADERS+=cli-command.h\ +polypinclude_HEADERS+=cli-command.h\ client.h \ core.h \ dynarray.h \ @@ -380,3 +387,4 @@ libpolypcore_la_SOURCES = idxset.c idxset.h \ dynarray.c dynarray.h endif + diff --git a/polyp/glib-mainloop.c b/polyp/glib-mainloop.c index 978cad07..3b9feaad 100644 --- a/polyp/glib-mainloop.c +++ b/polyp/glib-mainloop.c @@ -5,11 +5,12 @@ #include "xmalloc.h" struct pa_io_event { - GSource source; - int dead; struct pa_glib_mainloop *mainloop; + int dead; + GIOChannel *io_channel; + GSource *source; + GIOCondition io_condition; int fd; - GPollFD pollfd; 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); @@ -38,7 +39,7 @@ struct pa_defer_event { }; struct pa_glib_mainloop { - GMainLoop *glib_mainloop; + GMainContext *glib_main_context; struct pa_mainloop_api api; GSource *cleanup_source; struct pa_io_event *io_events, *dead_io_events; @@ -48,55 +49,16 @@ struct pa_glib_mainloop { static void schedule_free_dead_events(struct pa_glib_mainloop *g); -static gboolean glib_source_prepare(GSource *source, gint *timeout) { - return FALSE; -} - -static gboolean glib_source_check(GSource *source) { - struct pa_io_event *e = (struct pa_io_event*) source; - assert(e); - return !!e->pollfd.revents; -} - -static gboolean glib_source_dispatch(GSource *source, GSourceFunc callback, gpointer user_data) { - struct pa_io_event *e = (struct pa_io_event*) source; - assert(e); - - if (e->pollfd.revents) { - int f = - (e->pollfd.revents ? G_IO_IN : PA_IO_EVENT_INPUT) | - (e->pollfd.revents ? G_IO_OUT : PA_IO_EVENT_OUTPUT) | - (e->pollfd.revents ? G_IO_HUP : PA_IO_EVENT_HANGUP) | - (e->pollfd.revents ? G_IO_ERR : PA_IO_EVENT_ERROR); - e->pollfd.revents = 0; - - assert(e->callback); - 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); 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; - GSourceFuncs io_source_funcs = { - prepare: glib_source_prepare, - check: glib_source_check, - dispatch: glib_source_dispatch, - finalize: NULL, - closure_callback: NULL, - closure_marshal : NULL, - }; - assert(m && m->userdata && fd >= 0 && callback); g = m->userdata; - e = (struct pa_io_event*) g_source_new(&io_source_funcs, sizeof(struct pa_io_event)); - assert(e); + e = pa_xmalloc(sizeof(struct pa_io_event)); e->mainloop = m->userdata; e->dead = 0; e->fd = fd; @@ -104,10 +66,10 @@ static struct pa_io_event* glib_io_new(struct pa_mainloop_api*m, int fd, enum pa e->userdata = userdata; e->destroy_callback = NULL; - e->pollfd.fd = fd; - e->pollfd.events = e->pollfd.revents = 0; - - g_source_attach(&e->source, g_main_loop_get_context(g->glib_mainloop)); + e->io_channel = g_io_channel_unix_new(e->fd); + assert(e->io_channel); + e->source = NULL; + e->io_condition = 0; glib_io_enable(e, f); @@ -119,23 +81,52 @@ static struct pa_io_event* glib_io_new(struct pa_mainloop_api*m, int fd, enum pa 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) { - int o; + GIOCondition c; assert(e && !e->dead); - o = e->pollfd.events; - e->pollfd.events = (f & PA_IO_EVENT_INPUT ? G_IO_IN : 0) | (f & PA_IO_EVENT_OUTPUT ? G_IO_OUT : 0) | G_IO_HUP | G_IO_ERR; - - if (!o && e->pollfd.events) - g_source_add_poll(&e->source, &e->pollfd); - else if (o && !e->pollfd.events) - g_source_remove_poll(&e->source, &e->pollfd); + 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) { + g_source_destroy(e->source); + g_source_unref(e->source); + } + + e->source = g_io_create_watch(e->io_channel, c | G_IO_ERR | G_IO_HUP); + assert(e->source); + + g_source_set_callback(e->source, (GSourceFunc) io_cb, e, NULL); + g_source_attach(e->source, e->mainloop->glib_main_context); + g_source_set_priority(e->source, G_PRIORITY_DEFAULT); + + e->io_condition = c; } static void glib_io_free(struct pa_io_event*e) { assert(e && !e->dead); - g_source_destroy(&e->source); + if (e->source) { + g_source_destroy(e->source); + g_source_unref(e->source); + e->source = NULL; + } if (e->prev) e->prev->next = e->next; @@ -235,7 +226,8 @@ static void glib_time_restart(struct pa_time_event*e, const struct timeval *tv) e->source = g_timeout_source_new(msec_diff(tv, &now)); assert(e->source); g_source_set_callback(e->source, time_cb, e, NULL); - g_source_attach(e->source, g_main_loop_get_context(e->mainloop->glib_mainloop)); + g_source_set_priority(e->source, G_PRIORITY_HIGH); + g_source_attach(e->source, e->mainloop->glib_main_context); } else e->source = NULL; } @@ -319,7 +311,7 @@ static void glib_defer_enable(struct pa_defer_event *e, int b) { e->source = g_idle_source_new(); assert(e->source); g_source_set_callback(e->source, idle_cb, e, NULL); - g_source_attach(e->source, g_main_loop_get_context(e->mainloop->glib_mainloop)); + g_source_attach(e->source, e->mainloop->glib_main_context); g_source_set_priority(e->source, G_PRIORITY_HIGH_IDLE); } } @@ -362,8 +354,8 @@ static void glib_quit(struct pa_mainloop_api*a, int retval) { struct pa_glib_mainloop *g; assert(a && a->userdata); g = a->userdata; - - g_main_loop_quit(g->glib_mainloop); + + /* NOOP */ } static const struct pa_mainloop_api vtable = { @@ -387,12 +379,16 @@ static const struct pa_mainloop_api vtable = { quit : glib_quit, }; -struct pa_glib_mainloop *pa_glib_mainloop_new(GMainLoop *ml) { +struct pa_glib_mainloop *pa_glib_mainloop_new(GMainContext *c) { struct pa_glib_mainloop *g; - assert(ml); g = pa_xmalloc(sizeof(struct pa_glib_mainloop)); - g->glib_mainloop = g_main_loop_ref(ml); + if (c) { + g->glib_main_context = c; + g_main_context_ref(c); + } else + g->glib_main_context = g_main_context_default(); + g->api = vtable; g->api.userdata = g; @@ -409,16 +405,18 @@ static void free_io_events(struct pa_io_event *e) { struct pa_io_event *r = e; e = r->next; - if (r->pollfd.events) - g_source_remove_poll(&r->source, &r->pollfd); + if (r->source) { + g_source_destroy(r->source); + g_source_unref(r->source); + } - if (!r->dead) - g_source_destroy(&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); - g_source_unref(&r->source); + pa_xfree(r); } } @@ -466,7 +464,12 @@ void pa_glib_mainloop_free(struct pa_glib_mainloop* g) { free_time_events(g->time_events); free_time_events(g->dead_time_events); - g_main_loop_unref(g->glib_mainloop); + if (g->cleanup_source) { + g_source_destroy(g->cleanup_source); + g_source_unref(g->cleanup_source); + } + + g_main_context_unref(g->glib_main_context); pa_xfree(g); } @@ -491,7 +494,7 @@ static gboolean free_dead_events(gpointer p) { } static void schedule_free_dead_events(struct pa_glib_mainloop *g) { - assert(g && g->glib_mainloop); + assert(g && g->glib_main_context); if (g->cleanup_source) return; @@ -499,5 +502,5 @@ static void schedule_free_dead_events(struct pa_glib_mainloop *g) { g->cleanup_source = g_idle_source_new(); assert(g->cleanup_source); g_source_set_callback(g->cleanup_source, free_dead_events, g, NULL); - g_source_attach(g->cleanup_source, g_main_loop_get_context(g->glib_mainloop)); + g_source_attach(g->cleanup_source, g->glib_main_context); } diff --git a/polyp/glib-mainloop.h b/polyp/glib-mainloop.h index 50fe8b9c..7655108a 100644 --- a/polyp/glib-mainloop.h +++ b/polyp/glib-mainloop.h @@ -5,10 +5,19 @@ #include "mainloop-api.h" +#ifdef __cplusplus +extern "C" { +#endif + struct pa_glib_mainloop; -struct pa_glib_mainloop *pa_glib_mainloop_new(GMainLoop *ml); +struct pa_glib_mainloop *pa_glib_mainloop_new(GMainContext *c); void pa_glib_mainloop_free(struct pa_glib_mainloop* g); struct pa_mainloop_api* pa_glib_mainloop_get_api(struct pa_glib_mainloop *g); + +#ifdef __cplusplus +} +#endif + #endif diff --git a/polyp/glib-test.c b/polyp/glib-test.c new file mode 100644 index 00000000..99196c4a --- /dev/null +++ b/polyp/glib-test.c @@ -0,0 +1,92 @@ +#include +#include +#include +#include + +/*#define GLIB_MAIN_LOOP*/ + +#ifdef GLIB_MAIN_LOOP +#include +#include "glib-mainloop.h" +static GMainLoop* glib_main_loop = NULL; +#else +#include "mainloop.h" +#endif + +static void iocb(struct pa_mainloop_api*a, struct pa_io_event *e, int fd, enum pa_io_event_flags f, void *userdata) { + unsigned char c; + read(fd, &c, sizeof(c)); + fprintf(stderr, "IO EVENT: %c\n", c < 32 ? '.' : c); +} + +static void dcb(struct pa_mainloop_api*a, struct pa_defer_event *e, void *userdata) { + fprintf(stderr, "DEFER EVENT\n"); + a->defer_enable(e, 0); +} + +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 + g_main_loop_quit(glib_main_loop); +#else + a->quit(a, 0); +#endif +} + +int main(int argc, char *argv[]) { + struct pa_mainloop_api *a; + struct pa_io_event *ioe; + struct pa_defer_event *de; + struct pa_time_event *te; + struct timeval tv; + +#ifdef GLIB_MAIN_LOOP + struct pa_glib_mainloop *g; + glib_main_loop = g_main_loop_new(NULL, FALSE); + assert(glib_main_loop); + + g = pa_glib_mainloop_new(NULL); + assert(g); + + a = pa_glib_mainloop_get_api(g); + assert(a); +#else + struct pa_mainloop *m; + + m = pa_mainloop_new(); + assert(m); + + a = pa_mainloop_get_api(m); + assert(a); +#endif + + ioe = a->io_new(a, 0, PA_IO_EVENT_INPUT, iocb, NULL); + assert(ioe); + + de = a->defer_new(a, dcb, NULL); + assert(de); + + gettimeofday(&tv, NULL); + tv.tv_sec += 10; + te = a->time_new(a, &tv, tcb, NULL); + +#ifdef GLIB_MAIN_LOOP + g_main_loop_run(glib_main_loop); +#else + pa_mainloop_run(m, NULL); +#endif + + a->time_free(te); + a->defer_free(de); + a->io_free(ioe); + +#ifdef GLIB_MAIN_LOOP + pa_glib_mainloop_free(g); + g_main_loop_unref(glib_main_loop); +#else + pa_mainloop_free(m); +#endif + + return 0; +} diff --git a/polyp/mainloop-api.h b/polyp/mainloop-api.h index 4c8e379b..1b9e2783 100644 --- a/polyp/mainloop-api.h +++ b/polyp/mainloop-api.h @@ -25,6 +25,10 @@ #include #include +#ifdef __cplusplus +extern "C" { +#endif + enum pa_io_event_flags { PA_IO_EVENT_NULL = 0, PA_IO_EVENT_INPUT = 1, @@ -64,4 +68,8 @@ struct pa_mainloop_api { void pa_mainloop_api_once(struct pa_mainloop_api*m, void (*callback)(struct pa_mainloop_api*m, void *userdata), void *userdata); +#ifdef __cplusplus +} +#endif + #endif diff --git a/polyp/polyplib-def.h b/polyp/polyplib-def.h index ec2d528b..e5c786f2 100644 --- a/polyp/polyplib-def.h +++ b/polyp/polyplib-def.h @@ -24,6 +24,10 @@ #include +#ifdef __cplusplus +extern "C" { +#endif + enum pa_stream_direction { PA_STREAM_PLAYBACK, PA_STREAM_RECORD, @@ -38,4 +42,8 @@ struct pa_buffer_attr { uint32_t fragsize; }; +#ifdef __cplusplus +} +#endif + #endif diff --git a/polyp/polyplib.h b/polyp/polyplib.h index 08e6a5a5..9741fc93 100644 --- a/polyp/polyplib.h +++ b/polyp/polyplib.h @@ -28,6 +28,10 @@ #include "polyplib-def.h" #include "mainloop-api.h" +#ifdef __cplusplus +extern "C" { +#endif + struct pa_context; struct pa_context *pa_context_new(struct pa_mainloop_api *mainloop, const char *name); @@ -127,4 +131,8 @@ void pa_context_get_source_info_by_name(struct pa_context *c, const char *name, void pa_context_get_source_info_by_id(struct pa_context *c, uint32_t id, void (*cb)(struct pa_context *c, const struct pa_source_info *i, void *userdata), void *userdata); void pa_context_get_source_info_list(struct pa_context *c, void (*cb)(struct pa_context *c, const struct pa_source_info *i, void *userdata), void *userdata); +#ifdef __cplusplus +} +#endif + #endif diff --git a/polyp/sample.h b/polyp/sample.h index 01a4efcf..25027879 100644 --- a/polyp/sample.h +++ b/polyp/sample.h @@ -25,6 +25,10 @@ #include #include +#ifdef __cplusplus +extern "C" { +#endif + enum pa_sample_format { PA_SAMPLE_U8, PA_SAMPLE_ALAW, @@ -60,4 +64,8 @@ int pa_sample_spec_equal(const struct pa_sample_spec*a, const struct pa_sample_s #define PA_SAMPLE_SNPRINT_MAX_LENGTH 32 void pa_sample_snprint(char *s, size_t l, const struct pa_sample_spec *spec); +#ifdef __cplusplus +} +#endif + #endif -- cgit