1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006, 2009 Lennart Poettering
5 Copyright 2006 Shams E. King
6
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 2.1 of the License,
10 or (at your option) any later version.
11
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #include <pulse/xmalloc.h>
26
27 #include <pulsecore/shared.h>
28
29 #include "dbus-shared.h"
30
31 struct pa_dbus_connection {
32 PA_REFCNT_DECLARE;
33
34 pa_dbus_wrap_connection *connection;
35 pa_core *core;
36 const char *property_name;
37 };
38
dbus_connection_new(pa_core * c,pa_dbus_wrap_connection * conn,const char * name)39 static pa_dbus_connection* dbus_connection_new(pa_core *c, pa_dbus_wrap_connection *conn, const char *name) {
40 pa_dbus_connection *pconn;
41
42 pconn = pa_xnew(pa_dbus_connection, 1);
43 PA_REFCNT_INIT(pconn);
44 pconn->core = c;
45 pconn->property_name = name;
46 pconn->connection = conn;
47
48 pa_assert_se(pa_shared_set(c, name, pconn) >= 0);
49
50 return pconn;
51 }
52
pa_dbus_bus_get(pa_core * c,DBusBusType type,DBusError * error)53 pa_dbus_connection* pa_dbus_bus_get(pa_core *c, DBusBusType type, DBusError *error) {
54
55 static const char *const prop_name[] = {
56 [DBUS_BUS_SESSION] = "dbus-connection-session",
57 [DBUS_BUS_SYSTEM] = "dbus-connection-system",
58 [DBUS_BUS_STARTER] = "dbus-connection-starter"
59 };
60 pa_dbus_wrap_connection *conn;
61 pa_dbus_connection *pconn;
62
63 pa_assert(type == DBUS_BUS_SYSTEM || type == DBUS_BUS_SESSION || type == DBUS_BUS_STARTER);
64
65 if ((pconn = pa_shared_get(c, prop_name[type])))
66 return pa_dbus_connection_ref(pconn);
67
68 if (!(conn = pa_dbus_wrap_connection_new(c->mainloop, true, type, error)))
69 return NULL;
70
71 return dbus_connection_new(c, conn, prop_name[type]);
72 }
73
pa_dbus_connection_get(pa_dbus_connection * c)74 DBusConnection* pa_dbus_connection_get(pa_dbus_connection *c) {
75 pa_assert(c);
76 pa_assert(PA_REFCNT_VALUE(c) > 0);
77 pa_assert(c->connection);
78
79 return pa_dbus_wrap_connection_get(c->connection);
80 }
81
pa_dbus_connection_unref(pa_dbus_connection * c)82 void pa_dbus_connection_unref(pa_dbus_connection *c) {
83 pa_assert(c);
84 pa_assert(PA_REFCNT_VALUE(c) > 0);
85
86 if (PA_REFCNT_DEC(c) > 0)
87 return;
88
89 pa_dbus_wrap_connection_free(c->connection);
90
91 pa_assert_se(pa_shared_remove(c->core, c->property_name) >= 0);
92 pa_xfree(c);
93 }
94
pa_dbus_connection_ref(pa_dbus_connection * c)95 pa_dbus_connection* pa_dbus_connection_ref(pa_dbus_connection *c) {
96 pa_assert(c);
97 pa_assert(PA_REFCNT_VALUE(c) > 0);
98
99 PA_REFCNT_INC(c);
100
101 return c;
102 }
103