2006-08-06 16:16:50 +00:00
|
|
|
/***
|
|
|
|
|
This file is part of PulseAudio.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-02-13 15:35:19 +00:00
|
|
|
Copyright 2006 Lennart Poettering
|
|
|
|
|
Copyright 2006 Shams E. King
|
|
|
|
|
|
2006-08-06 16:16:50 +00:00
|
|
|
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 of the License,
|
|
|
|
|
or (at your option) any later version.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-08-06 16:16:50 +00:00
|
|
|
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.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-08-06 16:16:50 +00:00
|
|
|
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 <pulse/xmalloc.h>
|
2006-08-07 20:29:31 +00:00
|
|
|
#include <pulse/timeval.h>
|
2007-10-28 19:13:50 +00:00
|
|
|
#include <pulsecore/log.h>
|
2008-08-01 02:10:54 +03:00
|
|
|
#include <pulsecore/shared.h>
|
2006-08-06 16:16:50 +00:00
|
|
|
|
|
|
|
|
#include "dbus-util.h"
|
|
|
|
|
|
|
|
|
|
struct pa_dbus_connection {
|
2007-10-28 19:13:50 +00:00
|
|
|
PA_REFCNT_DECLARE;
|
|
|
|
|
|
2006-08-06 16:16:50 +00:00
|
|
|
pa_core *core;
|
|
|
|
|
DBusConnection *connection;
|
|
|
|
|
const char *property_name;
|
|
|
|
|
pa_defer_event* dispatch_event;
|
|
|
|
|
};
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
static void dispatch_cb(pa_mainloop_api *ea, pa_defer_event *ev, void *userdata) {
|
|
|
|
|
DBusConnection *conn = userdata;
|
|
|
|
|
|
2006-08-06 16:16:50 +00:00
|
|
|
if (dbus_connection_dispatch(conn) == DBUS_DISPATCH_COMPLETE) {
|
|
|
|
|
/* no more data to process, disable the deferred */
|
|
|
|
|
ea->defer_enable(ev, 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* DBusDispatchStatusFunction callback for the pa mainloop */
|
2007-10-28 19:13:50 +00:00
|
|
|
static void dispatch_status(DBusConnection *conn, DBusDispatchStatus status, void *userdata) {
|
|
|
|
|
pa_dbus_connection *c = userdata;
|
|
|
|
|
|
|
|
|
|
pa_assert(c);
|
|
|
|
|
|
2006-08-06 16:16:50 +00:00
|
|
|
switch(status) {
|
2007-10-28 19:13:50 +00:00
|
|
|
|
2006-08-06 16:16:50 +00:00
|
|
|
case DBUS_DISPATCH_COMPLETE:
|
|
|
|
|
c->core->mainloop->defer_enable(c->dispatch_event, 0);
|
|
|
|
|
break;
|
2007-10-28 19:13:50 +00:00
|
|
|
|
2006-08-06 16:16:50 +00:00
|
|
|
case DBUS_DISPATCH_DATA_REMAINS:
|
|
|
|
|
case DBUS_DISPATCH_NEED_MEMORY:
|
|
|
|
|
default:
|
|
|
|
|
c->core->mainloop->defer_enable(c->dispatch_event, 1);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
static pa_io_event_flags_t get_watch_flags(DBusWatch *watch) {
|
|
|
|
|
unsigned int flags;
|
|
|
|
|
pa_io_event_flags_t events = 0;
|
|
|
|
|
|
|
|
|
|
pa_assert(watch);
|
|
|
|
|
|
|
|
|
|
flags = dbus_watch_get_flags(watch);
|
2006-08-06 16:16:50 +00:00
|
|
|
|
|
|
|
|
/* no watch flags for disabled watches */
|
|
|
|
|
if (!dbus_watch_get_enabled(watch))
|
|
|
|
|
return PA_IO_EVENT_NULL;
|
|
|
|
|
|
|
|
|
|
if (flags & DBUS_WATCH_READABLE)
|
|
|
|
|
events |= PA_IO_EVENT_INPUT;
|
|
|
|
|
if (flags & DBUS_WATCH_WRITABLE)
|
|
|
|
|
events |= PA_IO_EVENT_OUTPUT;
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
return events | PA_IO_EVENT_HANGUP | PA_IO_EVENT_ERROR;
|
2006-08-06 16:16:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* pa_io_event_cb_t IO event handler */
|
2008-08-09 16:20:29 +02:00
|
|
|
static void handle_io_event(pa_mainloop_api *ea, pa_io_event *e, int fd, pa_io_event_flags_t events, void *userdata) {
|
2006-08-06 16:16:50 +00:00
|
|
|
unsigned int flags = 0;
|
2007-10-28 19:13:50 +00:00
|
|
|
DBusWatch *watch = userdata;
|
2006-08-06 16:16:50 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
#if HAVE_DBUS_WATCH_GET_UNIX_FD
|
|
|
|
|
pa_assert(fd == dbus_watch_get_unix_fd(watch));
|
|
|
|
|
#else
|
|
|
|
|
pa_assert(fd == dbus_watch_get_fd(watch));
|
|
|
|
|
#endif
|
2006-08-06 16:16:50 +00:00
|
|
|
|
|
|
|
|
if (!dbus_watch_get_enabled(watch)) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_log_warn("Asked to handle disabled watch: %p %i", (void*) watch, fd);
|
2006-08-06 16:16:50 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (events & PA_IO_EVENT_INPUT)
|
|
|
|
|
flags |= DBUS_WATCH_READABLE;
|
|
|
|
|
if (events & PA_IO_EVENT_OUTPUT)
|
|
|
|
|
flags |= DBUS_WATCH_WRITABLE;
|
|
|
|
|
if (events & PA_IO_EVENT_HANGUP)
|
|
|
|
|
flags |= DBUS_WATCH_HANGUP;
|
|
|
|
|
if (events & PA_IO_EVENT_ERROR)
|
|
|
|
|
flags |= DBUS_WATCH_ERROR;
|
|
|
|
|
|
|
|
|
|
dbus_watch_handle(watch, flags);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* pa_time_event_cb_t timer event handler */
|
2007-10-28 19:13:50 +00:00
|
|
|
static void handle_time_event(pa_mainloop_api *ea, pa_time_event* e, const struct timeval *tv, void *userdata) {
|
|
|
|
|
DBusTimeout *timeout = userdata;
|
2006-08-06 16:16:50 +00:00
|
|
|
|
|
|
|
|
if (dbus_timeout_get_enabled(timeout)) {
|
|
|
|
|
struct timeval next = *tv;
|
|
|
|
|
dbus_timeout_handle(timeout);
|
|
|
|
|
|
|
|
|
|
/* restart it for the next scheduled time */
|
2008-08-19 22:39:54 +02:00
|
|
|
pa_timeval_add(&next, (pa_usec_t) dbus_timeout_get_interval(timeout) * 1000);
|
2006-08-06 16:16:50 +00:00
|
|
|
ea->time_restart(e, &next);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* DBusAddWatchFunction callback for pa mainloop */
|
2007-10-28 19:13:50 +00:00
|
|
|
static dbus_bool_t add_watch(DBusWatch *watch, void *data) {
|
|
|
|
|
pa_core *c = PA_CORE(data);
|
2006-08-06 16:16:50 +00:00
|
|
|
pa_io_event *ev;
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(watch);
|
|
|
|
|
pa_assert(c);
|
2006-08-06 16:16:50 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
ev = c->mainloop->io_new(
|
|
|
|
|
c->mainloop,
|
|
|
|
|
#if HAVE_DBUS_WATCH_GET_UNIX_FD
|
|
|
|
|
dbus_watch_get_unix_fd(watch),
|
|
|
|
|
#else
|
|
|
|
|
dbus_watch_get_fd(watch),
|
|
|
|
|
#endif
|
|
|
|
|
get_watch_flags(watch), handle_io_event, watch);
|
|
|
|
|
|
|
|
|
|
dbus_watch_set_data(watch, ev, NULL);
|
2006-08-06 16:16:50 +00:00
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* DBusRemoveWatchFunction callback for pa mainloop */
|
2007-10-28 19:13:50 +00:00
|
|
|
static void remove_watch(DBusWatch *watch, void *data) {
|
|
|
|
|
pa_core *c = PA_CORE(data);
|
|
|
|
|
pa_io_event *ev;
|
2006-08-06 16:16:50 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(watch);
|
|
|
|
|
pa_assert(c);
|
|
|
|
|
|
|
|
|
|
if ((ev = dbus_watch_get_data(watch)))
|
2006-08-06 16:16:50 +00:00
|
|
|
c->mainloop->io_free(ev);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* DBusWatchToggledFunction callback for pa mainloop */
|
2007-10-28 19:13:50 +00:00
|
|
|
static void toggle_watch(DBusWatch *watch, void *data) {
|
|
|
|
|
pa_core *c = PA_CORE(data);
|
|
|
|
|
pa_io_event *ev;
|
|
|
|
|
|
|
|
|
|
pa_assert(watch);
|
|
|
|
|
pa_core_assert_ref(c);
|
|
|
|
|
|
|
|
|
|
pa_assert_se(ev = dbus_watch_get_data(watch));
|
2006-08-06 16:16:50 +00:00
|
|
|
|
|
|
|
|
/* get_watch_flags() checks if the watch is enabled */
|
|
|
|
|
c->mainloop->io_enable(ev, get_watch_flags(watch));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* DBusAddTimeoutFunction callback for pa mainloop */
|
2007-10-28 19:13:50 +00:00
|
|
|
static dbus_bool_t add_timeout(DBusTimeout *timeout, void *data) {
|
|
|
|
|
pa_core *c = PA_CORE(data);
|
2006-08-06 16:16:50 +00:00
|
|
|
pa_time_event *ev;
|
2007-10-28 19:13:50 +00:00
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
|
|
pa_assert(timeout);
|
|
|
|
|
pa_assert(c);
|
2006-08-06 16:16:50 +00:00
|
|
|
|
|
|
|
|
if (!dbus_timeout_get_enabled(timeout))
|
|
|
|
|
return FALSE;
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_gettimeofday(&tv);
|
2008-08-19 22:39:54 +02:00
|
|
|
pa_timeval_add(&tv, (pa_usec_t) dbus_timeout_get_interval(timeout) * 1000);
|
2006-08-06 16:16:50 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
ev = c->mainloop->time_new(c->mainloop, &tv, handle_time_event, timeout);
|
2006-08-06 16:16:50 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
dbus_timeout_set_data(timeout, ev, NULL);
|
2006-08-06 16:16:50 +00:00
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* DBusRemoveTimeoutFunction callback for pa mainloop */
|
2007-10-28 19:13:50 +00:00
|
|
|
static void remove_timeout(DBusTimeout *timeout, void *data) {
|
|
|
|
|
pa_core *c = PA_CORE(data);
|
|
|
|
|
pa_time_event *ev;
|
|
|
|
|
|
|
|
|
|
pa_assert(timeout);
|
|
|
|
|
pa_assert(c);
|
2006-08-06 16:16:50 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
if ((ev = dbus_timeout_get_data(timeout)))
|
2006-08-06 16:16:50 +00:00
|
|
|
c->mainloop->time_free(ev);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* DBusTimeoutToggledFunction callback for pa mainloop */
|
2007-10-28 19:13:50 +00:00
|
|
|
static void toggle_timeout(DBusTimeout *timeout, void *data) {
|
|
|
|
|
pa_core *c = PA_CORE(data);
|
|
|
|
|
pa_time_event *ev;
|
|
|
|
|
|
|
|
|
|
pa_assert(timeout);
|
|
|
|
|
pa_assert(c);
|
|
|
|
|
|
|
|
|
|
pa_assert_se(ev = dbus_timeout_get_data(timeout));
|
2006-08-06 16:16:50 +00:00
|
|
|
|
|
|
|
|
if (dbus_timeout_get_enabled(timeout)) {
|
2007-10-28 19:13:50 +00:00
|
|
|
struct timeval tv;
|
|
|
|
|
|
2006-08-07 20:29:31 +00:00
|
|
|
pa_gettimeofday(&tv);
|
2008-08-19 22:39:54 +02:00
|
|
|
pa_timeval_add(&tv, (pa_usec_t) dbus_timeout_get_interval(timeout) * 1000);
|
2007-10-28 19:13:50 +00:00
|
|
|
|
2006-08-06 16:16:50 +00:00
|
|
|
c->mainloop->time_restart(ev, &tv);
|
2007-10-28 19:13:50 +00:00
|
|
|
} else
|
2006-08-07 20:29:31 +00:00
|
|
|
c->mainloop->time_restart(ev, NULL);
|
2006-08-06 16:16:50 +00:00
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
static void wakeup_main(void *userdata) {
|
|
|
|
|
pa_dbus_connection *c = userdata;
|
2006-08-06 16:16:50 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(c);
|
2006-08-06 16:16:50 +00:00
|
|
|
|
|
|
|
|
/* this will wakeup the mainloop and dispatch events, although
|
|
|
|
|
* it may not be the cleanest way of accomplishing it */
|
|
|
|
|
c->core->mainloop->defer_enable(c->dispatch_event, 1);
|
|
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
static pa_dbus_connection* pa_dbus_connection_new(pa_core* c, DBusConnection *conn, const char* name) {
|
|
|
|
|
pa_dbus_connection *pconn;
|
2006-08-06 16:16:50 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pconn = pa_xnew(pa_dbus_connection, 1);
|
|
|
|
|
PA_REFCNT_INIT(pconn);
|
2006-08-06 16:16:50 +00:00
|
|
|
pconn->core = c;
|
|
|
|
|
pconn->property_name = name;
|
|
|
|
|
pconn->connection = conn;
|
2007-10-28 19:13:50 +00:00
|
|
|
pconn->dispatch_event = c->mainloop->defer_new(c->mainloop, dispatch_cb, conn);
|
2006-08-06 16:16:50 +00:00
|
|
|
|
2008-08-01 01:56:09 +03:00
|
|
|
pa_shared_set(c, name, pconn);
|
2006-08-06 16:16:50 +00:00
|
|
|
|
|
|
|
|
return pconn;
|
|
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
DBusConnection* pa_dbus_connection_get(pa_dbus_connection *c){
|
|
|
|
|
pa_assert(c);
|
|
|
|
|
pa_assert(PA_REFCNT_VALUE(c) > 0);
|
|
|
|
|
pa_assert(c->connection);
|
|
|
|
|
|
2006-08-06 16:16:50 +00:00
|
|
|
return c->connection;
|
|
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
void pa_dbus_connection_unref(pa_dbus_connection *c) {
|
|
|
|
|
pa_assert(c);
|
|
|
|
|
pa_assert(PA_REFCNT_VALUE(c) > 0);
|
2006-08-06 16:16:50 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
if (PA_REFCNT_DEC(c) > 0)
|
2006-08-06 16:16:50 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (dbus_connection_get_is_connected(c->connection)) {
|
|
|
|
|
dbus_connection_close(c->connection);
|
2007-10-28 19:13:50 +00:00
|
|
|
/* must process remaining messages, bit of a kludge to handle
|
|
|
|
|
* both unload and shutdown */
|
|
|
|
|
while (dbus_connection_read_write_dispatch(c->connection, -1));
|
2006-08-06 16:16:50 +00:00
|
|
|
}
|
2007-10-28 19:13:50 +00:00
|
|
|
|
|
|
|
|
/* already disconnected, just free */
|
2008-08-01 01:56:09 +03:00
|
|
|
pa_shared_remove(c->core, c->property_name);
|
2007-10-28 19:13:50 +00:00
|
|
|
c->core->mainloop->defer_free(c->dispatch_event);
|
|
|
|
|
dbus_connection_unref(c->connection);
|
|
|
|
|
pa_xfree(c);
|
2006-08-06 16:16:50 +00:00
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_dbus_connection* pa_dbus_connection_ref(pa_dbus_connection *c) {
|
|
|
|
|
pa_assert(c);
|
|
|
|
|
pa_assert(PA_REFCNT_VALUE(c) > 0);
|
2006-08-06 16:16:50 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
PA_REFCNT_INC(c);
|
2006-08-06 16:16:50 +00:00
|
|
|
|
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_dbus_connection* pa_dbus_bus_get(pa_core *c, DBusBusType type, DBusError *error) {
|
|
|
|
|
|
|
|
|
|
static const char *const prop_name[] = {
|
|
|
|
|
[DBUS_BUS_SESSION] = "dbus-connection-session",
|
|
|
|
|
[DBUS_BUS_SYSTEM] = "dbus-connection-system",
|
|
|
|
|
[DBUS_BUS_STARTER] = "dbus-connection-starter"
|
|
|
|
|
};
|
2006-08-06 16:16:50 +00:00
|
|
|
DBusConnection *conn;
|
|
|
|
|
pa_dbus_connection *pconn;
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(type == DBUS_BUS_SYSTEM || type == DBUS_BUS_SESSION || type == DBUS_BUS_STARTER);
|
2006-08-06 16:16:50 +00:00
|
|
|
|
2008-08-01 01:56:09 +03:00
|
|
|
if ((pconn = pa_shared_get(c, prop_name[type])))
|
2006-08-06 16:16:50 +00:00
|
|
|
return pa_dbus_connection_ref(pconn);
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
if (!(conn = dbus_bus_get_private(type, error)))
|
2006-08-06 16:16:50 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pconn = pa_dbus_connection_new(c, conn, prop_name[type]);
|
2006-08-06 16:16:50 +00:00
|
|
|
|
|
|
|
|
dbus_connection_set_exit_on_disconnect(conn, FALSE);
|
2007-10-28 19:13:50 +00:00
|
|
|
dbus_connection_set_dispatch_status_function(conn, dispatch_status, pconn, NULL);
|
|
|
|
|
dbus_connection_set_watch_functions(conn, add_watch, remove_watch, toggle_watch, c, NULL);
|
|
|
|
|
dbus_connection_set_timeout_functions(conn, add_timeout, remove_timeout, toggle_timeout, c, NULL);
|
2006-08-06 16:16:50 +00:00
|
|
|
dbus_connection_set_wakeup_main_function(conn, wakeup_main, pconn, NULL);
|
|
|
|
|
|
|
|
|
|
return pconn;
|
|
|
|
|
}
|