From bf2b9c968d5a1aa2f9ffd52693a8148c87abc830 Mon Sep 17 00:00:00 2001 From: Daiki Ueno Date: Mon, 21 Feb 2011 15:11:26 +0900 Subject: [PATCH] Move name owning code from server.c to server-main.c. --- eekboard/eekboard-keyboard.c | 4 +- src/client-main.c | 2 +- src/server-main.c | 19 ++++- src/server.c | 159 ++++++++++++++++++----------------- src/server.h | 6 +- src/system-client.c | 2 +- 6 files changed, 105 insertions(+), 87 deletions(-) diff --git a/eekboard/eekboard-keyboard.c b/eekboard/eekboard-keyboard.c index bdb94b24..f4036ed9 100644 --- a/eekboard/eekboard-keyboard.c +++ b/eekboard/eekboard-keyboard.c @@ -182,10 +182,10 @@ eekboard_keyboard_new (const gchar *path, cancellable, error, "g-connection", connection, - "g-name", "com.redhat.eekboard.Keyboard", + "g-name", "com.redhat.Eekboard.Keyboard", "g-flags", G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START | G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES, - "g-interface-name", "com.redhat.eekboard.Keyboard", + "g-interface-name", "com.redhat.Eekboard.Keyboard", "g-object-path", path, NULL); if (initable != NULL) diff --git a/src/client-main.c b/src/client-main.c index 3009f8b5..897eab32 100644 --- a/src/client-main.c +++ b/src/client-main.c @@ -86,7 +86,7 @@ main (int argc, char **argv) } error = NULL; - keyboard = eekboard_keyboard_new ("/com/redhat/eekboard/Keyboard", + keyboard = eekboard_keyboard_new ("/com/redhat/Eekboard/Keyboard", connection, NULL, &error); diff --git a/src/server-main.c b/src/server-main.c index e30bfa88..19ed7dce 100644 --- a/src/server-main.c +++ b/src/server-main.c @@ -37,6 +37,7 @@ main (int argc, char **argv) GDBusConnection *connection; GError *error; GMainLoop *loop; + guint owner_id; #if HAVE_CLUTTER_GTK if (gtk_clutter_init (&argc, &argv) != CLUTTER_INIT_SUCCESS) { @@ -65,17 +66,29 @@ main (int argc, char **argv) exit (1); } - server = eekboard_server_new (connection); + server = eekboard_server_new ("/com/redhat/Eekboard/Keyboard", connection); - if (!eekboard_server_start (server)) { + if (server == NULL) { g_printerr ("Can't start server\n"); exit (1); } + owner_id = g_bus_own_name_on_connection (connection, + "com.redhat.Eekboard.Keyboard", + G_BUS_NAME_OWNER_FLAGS_NONE, + NULL, + NULL, + NULL, + NULL); + if (owner_id == 0) { + g_printerr ("Can't own the name\n"); + exit (1); + } + loop = g_main_loop_new (NULL, FALSE); g_main_loop_run (loop); - eekboard_server_stop (server); + g_bus_unown_name (owner_id); g_object_unref (server); g_object_unref (connection); g_main_loop_unref (loop); diff --git a/src/server.c b/src/server.c index 7ffed6d0..29768ba6 100644 --- a/src/server.c +++ b/src/server.c @@ -37,13 +37,14 @@ enum { PROP_0, + PROP_OBJECT_PATH, PROP_CONNECTION, PROP_LAST }; static const gchar introspection_xml[] = "" - " " + " " " " " " " " @@ -75,8 +76,9 @@ typedef struct _EekboardServerClass EekboardServerClass; struct _EekboardServer { GObject parent; GDBusConnection *connection; - guint owner_id; GDBusNodeInfo *introspection_data; + guint registration_id; + char *object_path; GtkWidget *window; GtkWidget *widget; @@ -92,6 +94,22 @@ struct _EekboardServerClass { G_DEFINE_TYPE (EekboardServer, eekboard_server, G_TYPE_OBJECT); +static void handle_method_call (GDBusConnection *connection, + const gchar *sender, + const gchar *object_path, + const gchar *interface_name, + const gchar *method_name, + GVariant *parameters, + GDBusMethodInvocation *invocation, + gpointer user_data); + +static const GDBusInterfaceVTable interface_vtable = +{ + handle_method_call, + NULL, + NULL +}; + #if HAVE_CLUTTER_GTK static void on_allocation_changed (ClutterActor *stage, @@ -127,9 +145,9 @@ on_notify_visible (GObject *object, GParamSpec *spec, gpointer user_data) error = NULL; g_dbus_connection_emit_signal (server->connection, - "com.redhat.eekboard.Keyboard", - "/com/redhat/eekboard/Keyboard", - "com.redhat.eekboard.Keyboard", + "com.redhat.Eekboard.Keyboard", + "/com/redhat/Eekboard/Keyboard", + "com.redhat.Eekboard.Keyboard", "VisibilityChanged", g_variant_new ("(b)", visible), &error); @@ -207,6 +225,11 @@ eekboard_server_set_property (GObject *object, GDBusConnection *connection; switch (prop_id) { + case PROP_OBJECT_PATH: + if (server->object_path) + g_free (server->object_path); + server->object_path = g_strdup (g_value_get_string (value)); + break; case PROP_CONNECTION: connection = g_value_get_object (value); if (server->connection) @@ -225,22 +248,63 @@ static void eekboard_server_dispose (GObject *object) { EekboardServer *server = EEKBOARD_SERVER(object); + if (server->connection) { + if (server->registration_id > 0) { + g_dbus_connection_unregister_object (server->connection, + server->registration_id); + server->registration_id = 0; + } + g_object_unref (server->connection); server->connection = NULL; } + + if (server->introspection_data) { + g_dbus_node_info_unref (server->introspection_data); + server->introspection_data = NULL; + } + G_OBJECT_CLASS (eekboard_server_parent_class)->dispose (object); } +static void +eekboard_server_constructed (GObject *object) +{ + EekboardServer *server = EEKBOARD_SERVER (object); + if (server->connection && server->object_path) { + GError *error = NULL; + + server->registration_id = g_dbus_connection_register_object + (server->connection, + server->object_path, + server->introspection_data->interfaces[0], + &interface_vtable, + server, + NULL, + &error); + } +} + static void eekboard_server_class_init (EekboardServerClass *klass) { GObjectClass *gobject_class = G_OBJECT_CLASS (klass); GParamSpec *pspec; + gobject_class->constructed = eekboard_server_constructed; gobject_class->set_property = eekboard_server_set_property; gobject_class->dispose = eekboard_server_dispose; + pspec = g_param_spec_string ("object-path", + "Object-path", + "Object-path", + NULL, + G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE); + g_object_class_install_property (gobject_class, + PROP_OBJECT_PATH, + pspec); + pspec = g_param_spec_object ("connection", "Connection", "Connection", @@ -260,7 +324,8 @@ eekboard_server_init (EekboardServer *server) server->introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, &error); g_assert (server->introspection_data != NULL); - server->owner_id = 0; + server->registration_id = 0; + server->object_path = NULL; server->keyboard = NULL; server->widget = NULL; server->window = NULL; @@ -278,9 +343,9 @@ on_key_pressed (EekKeyboard *keyboard, error = NULL; g_dbus_connection_emit_signal (server->connection, - "com.redhat.eekboard.Keyboard", - "/com/redhat/eekboard/Keyboard", - "com.redhat.eekboard.Keyboard", + "com.redhat.Eekboard.Keyboard", + "/com/redhat/Eekboard/Keyboard", + "com.redhat.Eekboard.Keyboard", "KeyPressed", g_variant_new ("(u)", eek_key_get_keycode (key)), @@ -298,9 +363,9 @@ on_key_released (EekKeyboard *keyboard, error = NULL; g_dbus_connection_emit_signal (server->connection, - "com.redhat.eekboard.Keyboard", - "/com/redhat/eekboard/Keyboard", - "com.redhat.eekboard.Keyboard", + "com.redhat.Eekboard.Keyboard", + "/com/redhat/Eekboard/Keyboard", + "com.redhat.Eekboard.Keyboard", "KeyReleased", g_variant_new ("(u)", eek_key_get_keycode (key)), @@ -469,70 +534,12 @@ handle_method_call (GDBusConnection *connection, g_return_if_reached (); } -static const GDBusInterfaceVTable interface_vtable = -{ - handle_method_call, - NULL, - NULL -}; - -static void -on_name_acquired (GDBusConnection *connection, - const gchar *name, - gpointer user_data) -{ - // g_debug ("name acquired %s", name); -} - -static void -on_name_lost (GDBusConnection *connection, - const gchar *name, - gpointer user_data) -{ - // g_debug ("name lost %s", name); -} - EekboardServer * -eekboard_server_new (GDBusConnection *connection) +eekboard_server_new (const gchar *object_path, + GDBusConnection *connection) { - return g_object_new (EEKBOARD_TYPE_SERVER, "connection", connection, NULL); -} - -gboolean -eekboard_server_start (EekboardServer *server) -{ - guint registration_id; - GError *error; - - error = NULL; - registration_id = g_dbus_connection_register_object - (server->connection, - "/com/redhat/eekboard/Keyboard", - server->introspection_data->interfaces[0], - &interface_vtable, - server, - NULL, - &error); - if (error) - g_printerr ("%s\n", error->message); - g_assert (registration_id > 0); - - server->owner_id = - g_bus_own_name_on_connection (server->connection, - "com.redhat.eekboard.Keyboard", - G_BUS_NAME_OWNER_FLAGS_NONE, - on_name_acquired, - on_name_lost, - NULL, - NULL); - return server->owner_id > 0; -} - -void -eekboard_server_stop (EekboardServer *server) -{ - if (server->owner_id > 0) - g_bus_unown_name (server->owner_id); - if (server->introspection_data) - g_dbus_node_info_unref (server->introspection_data); + return g_object_new (EEKBOARD_TYPE_SERVER, + "object-path", object_path, + "connection", connection, + NULL); } diff --git a/src/server.h b/src/server.h index 45ba6b6f..cbf5b9aa 100644 --- a/src/server.h +++ b/src/server.h @@ -31,10 +31,8 @@ G_BEGIN_DECLS typedef struct _EekboardServer EekboardServer; -EekboardServer *eekboard_server_new (GDBusConnection *connection); - -gboolean eekboard_server_start (EekboardServer *server); -void eekboard_server_stop (EekboardServer *server); +EekboardServer *eekboard_server_new (const gchar *object_path, + GDBusConnection *connection); G_END_DECLS #endif /* EEKBOARD_SERVER_H */ diff --git a/src/system-client.c b/src/system-client.c index ddfbb17b..c086b842 100644 --- a/src/system-client.c +++ b/src/system-client.c @@ -117,7 +117,7 @@ eekboard_system_client_set_property (GObject *object, case PROP_CONNECTION: connection = g_value_get_object (value); error = NULL; - client->keyboard = eekboard_keyboard_new ("/com/redhat/eekboard/Keyboard", + client->keyboard = eekboard_keyboard_new ("/com/redhat/Eekboard/Keyboard", connection, NULL, &error);