summaryrefslogtreecommitdiffstats
path: root/dbus/dbus-bus.c
diff options
context:
space:
mode:
authorAnders Carlsson <andersca@codefactory.se>2003-03-28 14:23:10 +0000
committerAnders Carlsson <andersca@codefactory.se>2003-03-28 14:23:10 +0000
commitd361874ef6a94a61fa3e0534d8352392edf9bbb9 (patch)
tree9696b9418e3b373dc3c5a2520e94cdfb03ac9601 /dbus/dbus-bus.c
parentbf99381351b802fb3348a24037898222aae631e2 (diff)
2003-03-28 Anders Carlsson <andersca@codefactory.se>
* dbus/dbus-bus.c: (bus_data_free), (dbus_bus_get): * dbus/dbus-bus.h: Add dbus_bus_get. * dbus/dbus-memory.c: Fix a doc comment.
Diffstat (limited to 'dbus/dbus-bus.c')
-rw-r--r--dbus/dbus-bus.c95
1 files changed, 95 insertions, 0 deletions
diff --git a/dbus/dbus-bus.c b/dbus/dbus-bus.c
index 1b63d608..c3978db2 100644
--- a/dbus/dbus-bus.c
+++ b/dbus/dbus-bus.c
@@ -51,6 +51,7 @@ typedef struct
{
char *base_service; /**< Base service name of this connection */
+ DBusConnection **connection; /**< Pointer to bus_connections entry */
} BusData;
/** The slot we have reserved to store BusData
@@ -65,6 +66,7 @@ static int bus_data_slot_refcount = 0;
*/
_DBUS_DEFINE_GLOBAL_LOCK (bus);
+
static dbus_bool_t
data_slot_ref (void)
{
@@ -114,6 +116,9 @@ bus_data_free (void *data)
{
BusData *bd = data;
+ if (bd->connection)
+ *bd->connection = NULL;
+
dbus_free (bd->base_service);
dbus_free (bd);
@@ -163,6 +168,96 @@ ensure_bus_data (DBusConnection *connection)
* @{
*/
+/** Number of bus types */
+#define BUS_TYPES 2
+
+static DBusConnection *bus_connections[BUS_TYPES];
+
+/**
+ * Connects to a bus daemon and registers the client with it.
+ * If a connection to the bus already exists, then that connection is returned.
+ *
+ * @todo alex thinks we should nullify the connection when we get a disconnect-message.
+ *
+ * @param type bus type
+ * @param error address where an error can be returned.
+ * @returns a DBusConnection
+ */
+DBusConnection *
+dbus_bus_get (DBusBusType type,
+ DBusError *error)
+{
+ const char *name, *value;
+ DBusConnection *connection;
+ BusData *bd;
+
+ if (type <= 0 || type >= BUS_TYPES)
+ {
+ _dbus_assert_not_reached ("Invalid bus type specified.");
+
+ return NULL;
+ }
+
+ _DBUS_LOCK (bus);
+
+ if (bus_connections[type] != NULL)
+ {
+ connection = bus_connections[type];
+ dbus_connection_ref (connection);
+
+ _DBUS_UNLOCK (bus);
+ return connection;
+ }
+
+ switch (type)
+ {
+ case DBUS_BUS_SESSION:
+ name = "DBUS_SESSION_BUS_ADDRESS";
+ break;
+ case DBUS_BUS_SYSTEM:
+ name = "DBUS_SYSTEM_BUS_ADDRESS";
+ break;
+ }
+
+ value = _dbus_getenv (name);
+
+ if (!value)
+ {
+ dbus_set_error (error, DBUS_ERROR_FAILED,
+ "Could not get bus daemon address.");
+ _DBUS_UNLOCK (bus);
+
+ return NULL;
+ }
+
+ connection = dbus_connection_open (value, error);
+
+ if (!connection)
+ {
+ _DBUS_UNLOCK (bus);
+ return NULL;
+ }
+
+ if (!dbus_bus_register (connection, error))
+ {
+ dbus_connection_disconnect (connection);
+ dbus_connection_unref (connection);
+
+ _DBUS_UNLOCK (bus);
+ return NULL;
+ }
+
+ bus_connections[type] = connection;
+ bd = ensure_bus_data (connection);
+ _dbus_assert (bd != NULL);
+
+ bd->connection = &bus_connections[type];
+
+ _DBUS_UNLOCK (bus);
+ return connection;
+}
+
+
/**
* Registers a connection with the bus. This must be the first
* thing an application does when connecting to the message bus.