summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Henningsson <david.henningsson@canonical.com>2010-11-30 04:59:27 +0100
committerColin Guthrie <cguthrie@mandriva.org>2010-12-05 16:01:46 +0000
commit205cad6eed59c3ab110d03620de42a5e85d7a651 (patch)
tree23c3268a15660c253e3c6fd8ed01f53517d946d1
parente9deb97d94ca64b563adfef02dc45fb3ec6be2de (diff)
jack: Add module-jackdbus-detect
-rw-r--r--src/Makefile.am12
-rw-r--r--src/modules/jack/module-jackdbus-detect.c304
2 files changed, 316 insertions, 0 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
index c96c05f5..ec301dad 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1125,6 +1125,12 @@ if HAVE_JACK
modlibexec_LTLIBRARIES += \
module-jack-sink.la \
module-jack-source.la
+
+if HAVE_DBUS
+modlibexec_LTLIBRARIES += \
+ module-jackdbus-detect.la
+endif
+
endif
if HAVE_GCONF
@@ -1229,6 +1235,7 @@ SYMDEF_FILES = \
modules/module-detect-symdef.h \
modules/rtp/module-rtp-send-symdef.h \
modules/rtp/module-rtp-recv-symdef.h \
+ modules/jack/module-jackdbus-detect-symdef.h \
modules/jack/module-jack-sink-symdef.h \
modules/jack/module-jack-source-symdef.h \
modules/module-volume-restore-symdef.h \
@@ -1608,6 +1615,11 @@ module_rtp_recv_la_CFLAGS = $(AM_CFLAGS)
# JACK
+module_jackdbus_detect_la_SOURCES = modules/jack/module-jackdbus-detect.c
+module_jackdbus_detect_la_LDFLAGS = $(MODULE_LDFLAGS)
+module_jackdbus_detect_la_LIBADD = $(AM_LIBADD) $(DBUS_LIBS) libpulsecore-@PA_MAJORMINORMICRO@.la $(JACK_LIBS) libpulsecommon-@PA_MAJORMINORMICRO@.la libpulse.la
+module_jackdbus_detect_la_CFLAGS = $(AM_CFLAGS) $(DBUS_CFLAGS) $(JACK_CFLAGS)
+
module_jack_sink_la_SOURCES = modules/jack/module-jack-sink.c
module_jack_sink_la_LDFLAGS = $(MODULE_LDFLAGS)
module_jack_sink_la_LIBADD = $(AM_LIBADD) libpulsecore-@PA_MAJORMINORMICRO@.la $(JACK_LIBS) libpulsecommon-@PA_MAJORMINORMICRO@.la libpulse.la
diff --git a/src/modules/jack/module-jackdbus-detect.c b/src/modules/jack/module-jackdbus-detect.c
new file mode 100644
index 00000000..f635b233
--- /dev/null
+++ b/src/modules/jack/module-jackdbus-detect.c
@@ -0,0 +1,304 @@
+/***
+ This file is part of PulseAudio.
+
+ Written by David Henningsson <david.henningsson@canonical.com>
+ Copyright 2010 Canonical Ltd.
+
+ Some code taken from other parts of PulseAudio, these are
+ Copyright 2006-2009 Lennart Poettering
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License,
+ or (at your option) any later version.
+
+ PulseAudio 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 Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <pulsecore/log.h>
+#include <pulsecore/modargs.h>
+#include <pulsecore/core-util.h>
+#include <pulsecore/dbus-shared.h>
+
+#include "module-jackdbus-detect-symdef.h"
+
+PA_MODULE_AUTHOR("David Henningsson");
+PA_MODULE_DESCRIPTION("Adds JACK sink/source ports when JACK is started");
+PA_MODULE_LOAD_ONCE(TRUE);
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_USAGE("connect=<connect ports?>");
+
+#define JACK_SERVICE_NAME "org.jackaudio.service"
+#define JACK_INTERFACE_NAME "org.jackaudio.JackControl"
+#define JACK_INTERFACE_PATH "/org/jackaudio/Controller"
+
+#define SERVICE_FILTER \
+ "type='signal'," \
+ "sender='" DBUS_SERVICE_DBUS "'," \
+ "interface='" DBUS_INTERFACE_DBUS "'," \
+ "member='NameOwnerChanged'," \
+ "arg0='" JACK_SERVICE_NAME "'"
+
+#define RUNNING_FILTER(_a) \
+ "type='signal'," \
+ "sender='" JACK_SERVICE_NAME "'," \
+ "interface='" JACK_INTERFACE_NAME "'," \
+ "member='" _a "'"
+
+static const char* const valid_modargs[] = {
+ "connect",
+ NULL
+};
+
+#define JACK_SS_SINK 0
+#define JACK_SS_SOURCE 1
+#define JACK_SS_COUNT 2
+
+static const char* const modnames[JACK_SS_COUNT] = {
+ "module-jack-sink",
+ "module-jack-source"
+};
+
+
+struct userdata {
+ pa_module *module;
+ pa_core *core;
+ pa_dbus_connection *connection;
+ pa_bool_t filter_added, match_added;
+ pa_bool_t is_service_started;
+ pa_bool_t autoconnect_ports;
+ /* Using index here protects us from module unloading without us knowing */
+ int jack_module_index[JACK_SS_COUNT];
+};
+
+
+static void ensure_ports_stopped(struct userdata* u)
+{
+ int i;
+ pa_assert(u);
+
+ for (i = 0; i < JACK_SS_COUNT; i++)
+ if (u->jack_module_index[i]) {
+ pa_module_unload_request_by_index(u->core, u->jack_module_index[i], TRUE);
+ u->jack_module_index[i] = 0;
+ pa_log_info("Stopped %s.", modnames[i]);
+ }
+}
+
+static void ensure_ports_started(struct userdata* u)
+{
+ int i;
+ pa_assert(u);
+
+ for (i = 0; i < JACK_SS_COUNT; i++)
+ if (!u->jack_module_index[i]) {
+ char* args;
+ pa_module* m;
+ args = pa_sprintf_malloc("connect=%s", pa_yes_no(u->autoconnect_ports));
+ m = pa_module_load(u->core, modnames[i], args);
+ pa_xfree(args);
+
+ if (m) {
+ pa_log_info("Successfully started %s.", modnames[i]);
+ u->jack_module_index[i] = m->index;
+ }
+ else
+ pa_log_info("Failed to start %s.", modnames[i]);
+ }
+}
+
+
+static pa_bool_t check_service_started(struct userdata* u)
+{
+ DBusError error;
+ DBusMessage *m = NULL, *reply = NULL;
+ pa_bool_t new_status = FALSE;
+ dbus_bool_t call_result;
+ pa_assert(u);
+
+ dbus_error_init(&error);
+
+ /* Just a safety check; it isn't such a big deal if the name disappears just after the call. */
+ if (!dbus_bus_name_has_owner(pa_dbus_connection_get(u->connection),
+ JACK_SERVICE_NAME, &error)) {
+ pa_log_debug("jackdbus isn't running.");
+ goto finish;
+ }
+
+ if (!(m = dbus_message_new_method_call(JACK_SERVICE_NAME, JACK_INTERFACE_PATH, JACK_INTERFACE_NAME, "IsStarted"))) {
+ pa_log("Failed to allocate IsStarted() method call.");
+ goto finish;
+ }
+
+ if (!(reply = dbus_connection_send_with_reply_and_block(pa_dbus_connection_get(u->connection), m, -1, &error))) {
+ pa_log("IsStarted() call failed: %s: %s", error.name, error.message);
+ goto finish;
+ }
+
+ if (!dbus_message_get_args(reply, &error, DBUS_TYPE_BOOLEAN, &call_result, DBUS_TYPE_INVALID)) {
+ pa_log("IsStarted() call return failed: %s: %s", error.name, error.message);
+ goto finish;
+ }
+
+ new_status = call_result;
+
+finish:
+ if (m)
+ dbus_message_unref(m);
+ if (reply)
+ dbus_message_unref(reply);
+
+ dbus_error_free(&error);
+ if (new_status)
+ ensure_ports_started(u);
+ else
+ ensure_ports_stopped(u);
+ u->is_service_started = new_status;
+ return new_status;
+}
+
+static DBusHandlerResult dbus_filter_handler(DBusConnection *c, DBusMessage *s, void *userdata)
+{
+ struct userdata *u = NULL;
+ DBusError error;
+
+ pa_assert(userdata);
+ u = ((pa_module*) userdata)->userdata;
+ pa_assert(u);
+
+ dbus_error_init(&error);
+
+ if (dbus_message_is_signal(s, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) {
+ const char *name, *old, *new;
+ if (!dbus_message_get_args(
+ s,
+ &error,
+ DBUS_TYPE_STRING, &name,
+ DBUS_TYPE_STRING, &old,
+ DBUS_TYPE_STRING, &new,
+ DBUS_TYPE_INVALID))
+ goto finish;
+ if (strcmp(name, JACK_SERVICE_NAME))
+ goto finish;
+
+ ensure_ports_stopped(u);
+ check_service_started(u);
+ }
+
+ else if (dbus_message_is_signal(s, JACK_INTERFACE_NAME, "ServerStarted")) {
+ ensure_ports_stopped(u);
+ check_service_started(u);
+ }
+
+ else if (dbus_message_is_signal(s, JACK_INTERFACE_NAME, "ServerStopped")) {
+ ensure_ports_stopped(u);
+ }
+
+finish:
+ dbus_error_free(&error);
+ return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+}
+
+int pa__init(pa_module *m)
+{
+ DBusError error;
+ pa_dbus_connection *connection = NULL;
+ struct userdata *u = NULL;
+ pa_modargs *ma;
+
+ pa_assert(m);
+
+ dbus_error_init(&error);
+
+ if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+ pa_log("Failed to parse module arguments");
+ goto fail;
+ }
+
+ m->userdata = u = pa_xnew0(struct userdata, 1);
+ u->core = m->core;
+ u->module = m;
+ u->autoconnect_ports = TRUE;
+
+ if (pa_modargs_get_value_boolean(ma, "connect", &u->autoconnect_ports) < 0) {
+ pa_log("Failed to parse connect= argument.");
+ goto fail;
+ }
+
+ if (!(connection = pa_dbus_bus_get(m->core, DBUS_BUS_SESSION, &error)) || dbus_error_is_set(&error)) {
+
+ if (connection)
+ pa_dbus_connection_unref(connection);
+
+ pa_log_error("Unable to contact D-Bus session bus: %s: %s", error.name, error.message);
+ goto fail;
+ }
+ u->connection = connection;
+
+ if (!dbus_connection_add_filter(pa_dbus_connection_get(connection), dbus_filter_handler, m, NULL)) {
+ pa_log_error("Unable to add D-Bus filter");
+ goto fail;
+ }
+ u->filter_added = 1;
+
+ if (pa_dbus_add_matches(
+ pa_dbus_connection_get(connection), &error, SERVICE_FILTER,
+ RUNNING_FILTER("ServerStarted"), RUNNING_FILTER("ServerStopped"), NULL) < 0) {
+ pa_log_error("Unable to subscribe to signals: %s: %s", error.name, error.message);
+ goto fail;
+ }
+ u->match_added = 1;
+
+ check_service_started(u);
+
+ return 0;
+
+fail:
+ if (ma)
+ pa_modargs_free(ma);
+
+ dbus_error_free(&error);
+ pa__done(m);
+
+ return -1;
+}
+
+void pa__done(pa_module *m)
+{
+ struct userdata *u;
+
+ pa_assert(m);
+
+ if (!(u = m->userdata))
+ return;
+
+ ensure_ports_stopped(u);
+
+ if (u->match_added) {
+ pa_dbus_remove_matches(
+ pa_dbus_connection_get(u->connection), SERVICE_FILTER,
+ RUNNING_FILTER("ServerStarted"), RUNNING_FILTER("ServerStopped"), NULL);
+ }
+
+ if (u->filter_added) {
+ dbus_connection_remove_filter(pa_dbus_connection_get(u->connection), dbus_filter_handler, m);
+ }
+
+ if (u->connection) {
+ pa_dbus_connection_unref(u->connection);
+ }
+
+ pa_xfree(u);
+}