1 /* vim:set et sts=4: */
2 
3 #include <ibus.h>
4 #include <locale.h>
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include "kmflutil.h"
8 #include "engine.h"
9 
10 static IBusBus *bus = NULL;
11 static IBusFactory *factory = NULL;
12 
13 /* options */
14 static gboolean xml = FALSE;
15 static gboolean ibus = FALSE;
16 static gboolean verbose = FALSE;
17 
18 static const GOptionEntry entries[] =
19 {
20     { "xml", 'x', 0, G_OPTION_ARG_NONE, &xml, "generate xml for engines", NULL },
21     { "ibus", 'i', 0, G_OPTION_ARG_NONE, &ibus, "component is executed by ibus", NULL },
22     { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "verbose", NULL },
23     { NULL },
24 };
25 
26 
27 static void
ibus_disconnected_cb(IBusBus * bus,gpointer user_data)28 ibus_disconnected_cb (IBusBus  *bus,
29                       gpointer  user_data)
30 {
31     g_debug ("bus disconnected");
32     ibus_quit ();
33 }
34 
35 
36 static void
start_component(void)37 start_component (void)
38 {
39     GList *engines, *p;
40     IBusComponent *component;
41 
42     ibus_init ();
43 
44     bus = ibus_bus_new ();
45     g_signal_connect (bus, "disconnected", G_CALLBACK (ibus_disconnected_cb), NULL);
46 
47     component = ibus_kmfl_get_component ();
48 
49     factory = ibus_factory_new (ibus_bus_get_connection (bus));
50 
51     engines = ibus_component_get_engines (component);
52     for (p = engines; p != NULL; p = p->next) {
53         IBusEngineDesc *engine = (IBusEngineDesc *)p->data;
54         ibus_factory_add_engine (factory, ibus_engine_desc_get_name (engine), IBUS_TYPE_KMFL_ENGINE);
55     }
56 
57     if (ibus) {
58         ibus_bus_request_name (bus, "org.freedesktop.IBus.KMFL", 0);
59     }
60     else {
61         ibus_bus_register_component (bus, component);
62     }
63 
64     g_object_unref (component);
65 
66     ibus_main ();
67 }
68 
69 static void
print_engines_xml(void)70 print_engines_xml (void)
71 {
72     IBusComponent *component;
73     GString *output;
74 
75     ibus_init ();
76 
77     component = ibus_kmfl_get_component ();
78     output = g_string_new ("");
79 
80     ibus_component_output_engines (component, output, 0);
81 
82     fprintf (stdout, "%s", output->str);
83 
84     g_string_free (output, TRUE);
85 
86 }
87 
88 int
main(gint argc,gchar ** argv)89 main (gint argc, gchar **argv)
90 {
91     GError *error = NULL;
92     GOptionContext *context;
93 
94     setlocale (LC_ALL, "");
95 
96     context = g_option_context_new ("- ibus KMFL engine component");
97 
98     g_option_context_add_main_entries (context, entries, "ibus-kmfl");
99 
100     if (!g_option_context_parse (context, &argc, &argv, &error)) {
101         g_print ("Option parsing failed: %s\n", error->message);
102         exit (-1);
103     }
104 
105     if (xml) {
106         print_engines_xml ();
107         exit (0);
108     }
109 
110     start_component ();
111     return 0;
112 }
113