diff options
-rw-r--r-- | src/ZYEngine.h | 6 | ||||
-rw-r--r-- | src/ZYMain.cc | 141 |
2 files changed, 144 insertions, 3 deletions
diff --git a/src/ZYEngine.h b/src/ZYEngine.h index 43d6d3c..4d24e43 100644 --- a/src/ZYEngine.h +++ b/src/ZYEngine.h @@ -30,10 +30,10 @@ namespace ZY { -#define IBUS_TYPE_PINYIN_ENGINE \ - (ZY::ibus_pinyin_engine_get_type ()) +#define IBUS_TYPE_ZHUYIN_ENGINE \ + (ZY::ibus_zhuyin_engine_get_type ()) -GType ibus_pinyin_engine_get_type (void); +GType ibus_zhuyin_engine_get_type (void); class Engine { public: diff --git a/src/ZYMain.cc b/src/ZYMain.cc index 571e6b8..ef9956b 100644 --- a/src/ZYMain.cc +++ b/src/ZYMain.cc @@ -24,9 +24,150 @@ # include "config.h" #endif #include <ibus.h> +#include <stdlib.h> +#include <locale.h> +#include "ZYEngine.h" +#include "ZYPointer.h" +#include "ZYBus.h" +#include "ZYConfig.h" +#include "ZYZConfig.h" +#include "ZYLibZhuyin.h" + + +using namespace ZY; + +#define N_(text) text + +static Pointer<IBusFactory> factory; + +/* options */ +static gboolean ibus = FALSE; +static gboolean verbose = FALSE; + +static void +show_version_and_quit (void) +{ + g_print ("%s - Version %s\n", g_get_application_name (), VERSION); + exit (EXIT_SUCCESS); +} + +static const GOptionEntry entries[] = +{ + { "version", 'V', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, + (gpointer) show_version_and_quit, "Show the application's version.", NULL }, + { "ibus", 'i', 0, G_OPTION_ARG_NONE, &ibus, "component is executed by ibus", NULL }, + { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "verbose", NULL }, + { NULL }, +}; + + +static void +ibus_disconnected_cb (IBusBus *bus, + gpointer user_data) +{ + g_debug ("bus disconnected"); + ibus_quit (); +} + + +static void +start_component (void) +{ + Pointer<IBusComponent> component; + + ibus_init (); + Bus bus; + + if (!bus.isConnected ()) { + g_warning ("Can not connect to ibus!"); + exit (0); + } + + if (!ibus_bus_get_config (bus)) { + g_warning ("IBus config component is not ready!"); + exit (0); + } + + LibZhuyinBackEnd::init (); + + ZhuyinConfig::init (bus); + + g_signal_connect ((IBusBus *)bus, "disconnected", G_CALLBACK (ibus_disconnected_cb), NULL); + + component = ibus_component_new ("org.freedesktop.IBus.Libzhuyin", + N_("Libzhuyin input method"), + VERSION, + "GPL", + "Peng Wu <alexepico@gmail.com>", + "https://github.com/libzhuyin/ibus-libzhuyin", + "", + "ibus-libzhuyin"); + + ibus_component_add_engine (component, + ibus_engine_desc_new ("libzhuyin-debug", + N_("New Zhuyin (debug)"), + N_("New Zhuyin input method (debug)"), + "zh_TW", + "GPL", + "Peng Wu <alexepico@gmail.com>\n" + "Peng Huang <shawn.p.huang@gmail.com>\n" + "BYVoid <byvoid1@gmail.com>", + PKGDATADIR "/icons/ibus-zhuyin.svg", + "us")); + + factory = ibus_factory_new (ibus_bus_get_connection (bus)); + + if (ibus) { + ibus_factory_add_engine (factory, "libzhuyin", IBUS_TYPE_ZHUYIN_ENGINE); + + ibus_bus_request_name (bus, "org.freedesktop.IBus.Libzhuyin", 0); + } + else { + ibus_factory_add_engine (factory, "libzhuyin-debug", IBUS_TYPE_ZHUYIN_ENGINE); + ibus_bus_register_component (bus, component); + } + + ibus_main (); +} + +#include <signal.h> + +static void +sigterm_cb (int sig) +{ + LibZhuyinBackEnd::finalize (); + + ::exit (EXIT_FAILURE); +} + +static void +atexit_cb (void) +{ + LibZhuyinBackEnd::finalize (); +} + int main (gint argc, gchar **argv) { + GError *error = NULL; + GOptionContext *context; + + setlocale (LC_ALL, ""); + + context = g_option_context_new ("- ibus zhuyin engine component"); + + g_option_context_add_main_entries (context, entries, "ibus-libzhuyin"); + + if (!g_option_context_parse (context, &argc, &argv, &error)) { + g_print ("Option parsing failed: %s\n", error->message); + exit (-1); + } + + ::signal (SIGTERM, sigterm_cb); + ::signal (SIGINT, sigterm_cb); + g_atexit (atexit_cb); + + start_component (); return 0; } |