diff options
author | Marcel Holtmann <marcel@holtmann.org> | 2008-05-08 19:39:12 +0000 |
---|---|---|
committer | Marcel Holtmann <marcel@holtmann.org> | 2008-05-08 19:39:12 +0000 |
commit | aa215d6fa551a3edbd129d4a163210e98357bdd1 (patch) | |
tree | d117772b0b444240eac624427393e9dff4d92703 | |
parent | 33191e6275ab82aa4161cdf75b45597e4d1568ec (diff) |
Emulate libgdbus API for mainloop integration
-rw-r--r-- | cups/main.c | 2 | ||||
-rw-r--r-- | daemon/main.c | 3 | ||||
-rw-r--r-- | gdbus/gdbus.h | 10 | ||||
-rw-r--r-- | gdbus/mainloop.c | 71 | ||||
-rw-r--r-- | hcid/dbus-common.c | 9 |
5 files changed, 49 insertions, 46 deletions
diff --git a/cups/main.c b/cups/main.c index b36b2074..001d6788 100644 --- a/cups/main.c +++ b/cups/main.c @@ -455,7 +455,7 @@ static gboolean list_printers(void) char *adapter, *match; guint len; - conn = dbus_bus_system_setup_with_main_loop("org.bluez", NULL, NULL); + conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, NULL, NULL); if (conn == NULL) return FALSE; diff --git a/daemon/main.c b/daemon/main.c index be731b14..7479d25e 100644 --- a/daemon/main.c +++ b/daemon/main.c @@ -58,8 +58,7 @@ static DBusConnection *system_bus = NULL; static int setup_dbus(void) { - system_bus = dbus_bus_system_setup_with_main_loop("org.bluez", - NULL, NULL); + system_bus = g_dbus_setup_bus(DBUS_BUS_SYSTEM, "org.bluez", NULL); if (!system_bus) return -1; diff --git a/gdbus/gdbus.h b/gdbus/gdbus.h index d749d5e2..a9f2aa51 100644 --- a/gdbus/gdbus.h +++ b/gdbus/gdbus.h @@ -31,10 +31,14 @@ extern "C" { #include <dbus/dbus.h> #include <glib.h> -void setup_dbus_with_main_loop(DBusConnection *conn); +typedef void (* GDBusDisconnectFunction) (void *user_data); -DBusConnection *dbus_bus_system_setup_with_main_loop(const char *name, - void (*disconnect_cb)(void *), void *user_data); +DBusConnection *g_dbus_setup_bus(DBusBusType type, const char *name, + DBusError *error); + +gboolean g_dbus_set_disconnect_function(DBusConnection *connection, + GDBusDisconnectFunction function, + void *user_data, DBusFreeFunction destroy); #ifdef __cplusplus } diff --git a/gdbus/mainloop.c b/gdbus/mainloop.c index e8e7e0af..a9b8237c 100644 --- a/gdbus/mainloop.c +++ b/gdbus/mainloop.c @@ -235,7 +235,7 @@ static void dispatch_status_cb(DBusConnection *conn, } #endif -void setup_dbus_with_main_loop(DBusConnection *conn) +static void setup_dbus_with_main_loop(DBusConnection *conn) { #ifdef HAVE_DBUS_GLIB debug("Using D-Bus GLib connection setup"); @@ -253,66 +253,61 @@ void setup_dbus_with_main_loop(DBusConnection *conn) #endif } -static DBusConnection *init_dbus(const char *name, - void (*disconnect_cb)(void *), void *user_data) +DBusConnection *g_dbus_setup_bus(DBusBusType type, const char *name, + DBusError *error) { - struct disconnect_data *dc_data; DBusConnection *conn; - DBusError err; - - dbus_error_init(&err); - conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err); + conn = dbus_bus_get(type, error); - if (dbus_error_is_set(&err)) { - error("Can't connect to system message bus: %s", err.message); - dbus_error_free(&err); - return NULL; + if (error != NULL) { + if (dbus_error_is_set(error) == TRUE) + return NULL; } - setup_dbus_with_main_loop(conn); - - if (name) { - dbus_error_init(&err); + if (conn == NULL) + return NULL; - if (dbus_bus_request_name(conn, name, 0, &err) != + if (name != NULL) { + if (dbus_bus_request_name(conn, name, + DBUS_NAME_FLAG_DO_NOT_QUEUE, error) != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER ) { - error("Could not become the primary owner of %s", name); dbus_connection_unref(conn); return NULL; } - if (dbus_error_is_set(&err)) { - error("Can't get bus name %s: %s", name, err.message); - dbus_error_free(&err); - dbus_connection_unref(conn); - return NULL; + if (error != NULL) { + if (dbus_error_is_set(error) == TRUE) { + dbus_connection_unref(conn); + return NULL; + } } } - if (!disconnect_cb) - return conn; + setup_dbus_with_main_loop(conn); + + return conn; +} + +gboolean g_dbus_set_disconnect_function(DBusConnection *connection, + GDBusDisconnectFunction function, + void *user_data, DBusFreeFunction destroy) +{ + struct disconnect_data *dc_data; dc_data = g_new(struct disconnect_data, 1); - dc_data->disconnect_cb = disconnect_cb; + dc_data->disconnect_cb = function; dc_data->user_data = user_data; - dbus_connection_set_exit_on_disconnect(conn, FALSE); + dbus_connection_set_exit_on_disconnect(connection, FALSE); - if (!dbus_connection_add_filter(conn, disconnect_filter, - dc_data, g_free)) { + if (dbus_connection_add_filter(connection, disconnect_filter, + dc_data, g_free) == FALSE) { error("Can't add D-Bus disconnect filter"); g_free(dc_data); - dbus_connection_unref(conn); - return NULL; + return FALSE; } - return conn; -} - -DBusConnection *dbus_bus_system_setup_with_main_loop(const char *name, - void (*disconnect_cb)(void *), void *user_data) -{ - return init_dbus(name, disconnect_cb, user_data); + return TRUE; } diff --git a/hcid/dbus-common.c b/hcid/dbus-common.c index 8a8a721a..7aa7b6d1 100644 --- a/hcid/dbus-common.c +++ b/hcid/dbus-common.c @@ -338,11 +338,16 @@ int hcid_dbus_init(void) { DBusConnection *conn; - conn = dbus_bus_system_setup_with_main_loop(BLUEZ_NAME, - disconnect_callback, NULL); + conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, BLUEZ_NAME, NULL); if (!conn) return -1; + if (g_dbus_set_disconnect_function(conn, disconnect_callback, + NULL, NULL) == FALSE) { + dbus_connection_unref(conn); + return -1; + } + if (hcid_dbus_use_experimental()) { debug("Registering experimental manager path"); dbus_connection_create_object_path(conn, "/", NULL, NULL); |