diff --git a/src/mainloop-glib.c b/src/mainloop-glib.c new file mode 100644 index 000000000..136ac81c8 --- /dev/null +++ b/src/mainloop-glib.c @@ -0,0 +1,112 @@ +/* PipeWire + * Copyright (C) 2018 Wim Taymans + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include + +#include +#include + +#include +#include + +#include "internal.h" + +struct source { + GSource base; + struct pw_loop *loop; +}; + +struct pa_glib_mainloop { + GMainContext *context; + pa_mainloop *loop; + struct source *source; + guint id; +}; + +static gboolean source_prepare (GSource *base, int *timeout) +{ + *timeout = -1; + return FALSE; +} + +static gboolean source_dispatch (GSource *source, GSourceFunc callback, gpointer user_data) +{ + struct source *s = (struct source *) source; + int result; + + pw_loop_enter (s->loop); + result = pw_loop_iterate (s->loop, 0); + pw_loop_leave (s->loop); + + if (result < 0) + g_warning ("pipewire_loop_iterate failed: %s", spa_strerror (result)); + + return TRUE; +} + +static GSourceFuncs source_funcs = +{ + source_prepare, + NULL, + source_dispatch, + NULL +}; + +pa_glib_mainloop *pa_glib_mainloop_new(GMainContext *c) +{ + + pa_glib_mainloop *loop; + + loop = calloc(1, sizeof(pa_glib_mainloop)); + if (loop == NULL) + goto error; + + loop->context = c; + loop->loop = pa_mainloop_new(); + if (loop->loop == NULL) + goto error_free; + + loop->source = (struct source *) g_source_new(&source_funcs, sizeof(struct source)); + loop->source->loop = loop->loop->loop; + + g_source_add_unix_fd (&loop->source->base, + pw_loop_get_fd(loop->source->loop), + G_IO_IN | G_IO_ERR); + + loop->id = g_source_attach (&loop->source->base, loop->context); + + return loop; + + error_free: + free(loop); + error: + return NULL; + +} + +void pa_glib_mainloop_free(pa_glib_mainloop* g) +{ + pa_mainloop_free(g->loop); + free(g); +} + +pa_mainloop_api* pa_glib_mainloop_get_api(pa_glib_mainloop *g) +{ + return pa_mainloop_get_api(g->loop); +} diff --git a/src/meson.build b/src/meson.build index 862c8670f..88ae3a5e2 100644 --- a/src/meson.build +++ b/src/meson.build @@ -31,6 +31,10 @@ pipewire_pulseaudio_sources = [ 'pipewire-pulseaudio.c', ] +pipewire_mainloop_glib_sources = [ + 'mainloop-glib.c', +] + pipewire_pulseaudio_c_args = [ '-DHAVE_CONFIG_H', '-DPIC', @@ -48,3 +52,13 @@ pipewire_pulseaudio = shared_library('pulse', dependencies : [pipewire_dep, pulseaudio_dep, mathlib], install : false, ) + +pipewire_pulseaudio = shared_library('pulse-mainloop-glib', + pipewire_mainloop_glib_sources, + soversion : '0', + c_args : pipewire_pulseaudio_c_args, + link_args : vflag, + include_directories : [configinc], + dependencies : [pipewire_dep, pulseaudio_dep, mathlib, glib_dep], + install : false, +)