#include #include #include "zero-pinyin-service.h" struct _AppData { GApplication *app; GDBusNodeInfo *introspection_data; guint owner_id; }; typedef struct _AppData AppData; static AppData appdata; static const gchar introspection_xml[] = "" " " " " " " " " " " " " " " " " ""; static void handle_method_get_candidates (GDBusMethodInvocation *invocation, AppData *appdata, GVariant *parameters) { gchar *preedit_str = NULL; GVariant *result = NULL; GVariantBuilder *candidates_builder = NULL; GVariantBuilder *matched_lengths_builder = NULL; /* GVariant *candidates = NULL; */ /* GVariant *matched_preedit_str_lengths = NULL; */ g_variant_get (parameters, "(s)", &preedit_str); g_return_if_fail (preedit_str != NULL); candidates_builder = g_variant_builder_new (G_VARIANT_TYPE ("as")); matched_lengths_builder = g_variant_builder_new (G_VARIANT_TYPE ("au")); /* test data */ get_candidates_test (preedit_str, candidates_builder, matched_lengths_builder); result = g_variant_new ("(asau)", candidates_builder, matched_lengths_builder); g_assert_nonnull (result); /* result is a GVarient tuple of two dbus arrays */ g_dbus_method_invocation_return_value (invocation, result); g_variant_builder_unref (candidates_builder); g_variant_builder_unref (matched_lengths_builder); g_free (preedit_str); } 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) { AppData *appdata; gchar *errmsg = NULL; appdata = (AppData*) user_data; g_message ("got method call: %s", method_name); if (g_strcmp0 (method_name, "GetCandidates") == 0) { handle_method_get_candidates (invocation, appdata, parameters); } else if (g_strcmp0 (method_name, "Quit") == 0) { g_application_quit (appdata->app); } else { g_warning ("method not found: %s", method_name); errmsg = g_strdup_printf ("Method not found: %s", method_name); g_dbus_method_invocation_return_dbus_error ( invocation, "org.gtk.GDBus.Failed", errmsg); g_free (errmsg); } } static GVariant * handle_get_property (GDBusConnection *connection, const gchar *sender, const gchar *object_path, const gchar *interface_name, const gchar *property_name, GError **error, gpointer user_data) { return NULL; } static gboolean handle_set_property (GDBusConnection *connection, const gchar *sender, const gchar *object_path, const gchar *interface_name, const gchar *property_name, GVariant *value, GError **error, gpointer user_data) { return TRUE; } static const GDBusInterfaceVTable interface_vtable = { handle_method_call, handle_get_property, handle_set_property }; static void on_bus_acquired (GDBusConnection *connection, const gchar *name, gpointer user_data) { AppData *appdata = NULL; GError *err = NULL; guint registration_id = 0; static const gchar* object_path = "/com/emacsos/zero/ZeroPinyinService"; g_message ("on_bus_acquired() name=%s", name); appdata = (AppData*) user_data; registration_id = g_dbus_connection_register_object ( connection, object_path, appdata->introspection_data->interfaces[0], &interface_vtable, appdata, /* user_data */ NULL, /* user_data_free_func */ &err); /* GError** */ if (err != NULL) { g_warning ("object register failed: %s. exiting now", err->message); g_error_free (err); g_application_quit (appdata->app); return; } if (registration_id > 0) { g_message ("object registered at %s", object_path); } g_assert_cmpint (registration_id, >, 0); } static void on_name_acquired (GDBusConnection *connection, const gchar *name, gpointer user_data) { g_message ("on_name_acquired() name=%s", name); } static void on_name_lost (GDBusConnection *connection, const gchar *name, gpointer user_data) { g_message ("on_name_lost() name=%s", name); } static void config_dbus_service (AppData *appdata) { appdata->introspection_data = g_dbus_node_info_new_for_xml ( introspection_xml, NULL); g_assert (appdata->introspection_data != NULL); appdata->owner_id = g_bus_own_name (G_BUS_TYPE_SESSION, "com.emacsos.zero.ZeroPinyinService", G_BUS_NAME_OWNER_FLAGS_NONE, on_bus_acquired, on_name_acquired, on_name_lost, appdata, NULL); g_assert_cmpint (appdata->owner_id, >, 0); } /** * handle SIGTERM gracefully. */ static gboolean on_sigterm_received (gpointer user_data) { AppData *appdata = (AppData*) user_data; g_application_quit (appdata->app); return G_SOURCE_REMOVE; } static void activate (GApplication *app, AppData *appdata) { /* no-op */ g_message ("zero-pinyin-service activate()"); } static void startup (GApplication *app, AppData *appdata) { g_message ("zero-pinyin-service startup()"); appdata->introspection_data = NULL; appdata->owner_id = 0; config_dbus_service(appdata); g_application_hold (app); } static void shutdown (GApplication *app, AppData *appdata) { g_message ("zero-pinyin-service shutdown()"); if (appdata->owner_id > 0) { g_bus_unown_name (appdata->owner_id); appdata->owner_id = 0; } if (appdata->introspection_data != NULL) { g_dbus_node_info_unref (appdata->introspection_data); appdata->introspection_data = NULL; } } /** * allow graceful shutdown by Ctrl-C and SIGTERM. */ static void setup_sigint_sigterm_handler (AppData *appdata) { GSource *source = NULL; source = g_unix_signal_source_new (SIGTERM); g_source_set_callback (source, on_sigterm_received, appdata, NULL); g_source_attach (source, NULL); g_source_unref (source); source = g_unix_signal_source_new (SIGINT); g_source_set_callback (source, on_sigterm_received, appdata, NULL); g_source_attach (source, NULL); g_source_unref (source); } /** * provide zero-pinyin-service dbus service. * it's a console app based on glib and gio. */ int main (int argc, char *argv[]) { GApplication *app = NULL; int status = 0; app = g_application_new ("com.emacsos.zero.App.ZeroPinyinService", G_APPLICATION_FLAGS_NONE); g_assert_nonnull (app); appdata.app = app; g_signal_connect (app, "activate", G_CALLBACK (activate), &appdata); g_signal_connect (app, "startup", G_CALLBACK (startup), &appdata); g_signal_connect (app, "shutdown", G_CALLBACK (shutdown), &appdata); setup_sigint_sigterm_handler (&appdata); status = g_application_run (G_APPLICATION (app), argc, argv); g_object_unref (app); return status; }