Skip to content

Commit

Permalink
chore(linux): Split startup process
Browse files Browse the repository at this point in the history
This change improves the startup method so that it will also work
if ibus hasn't started yet. Also fixes two memory leaks.
  • Loading branch information
ermshiperete committed Sep 15, 2023
1 parent 71c6bfb commit 7c7d467
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 22 deletions.
16 changes: 10 additions & 6 deletions linux/ibus-keyman/src/keymanutil.c
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,17 @@ ibus_keyman_list_engines (void)
return engines;
}

void
add_engine(gpointer data, gpointer user_data) {
IBusEngineDesc *desc = IBUS_ENGINE_DESC(data);
IBusComponent *component = IBUS_COMPONENT(user_data);
ibus_component_add_engine(component, desc);
}

IBusComponent *
ibus_keyman_get_component (void)
{
GList *engines, *p;
GList *engines;
IBusComponent *component;

component = ibus_component_new ("org.freedesktop.IBus.Keyman",
Expand All @@ -306,12 +313,9 @@ ibus_keyman_get_component (void)
"ibus-keyman");

engines = ibus_keyman_list_engines ();

for (p = engines; p != NULL; p = p->next) {
ibus_component_add_engine (component, (IBusEngineDesc *) p->data);
}

g_list_foreach(engines, add_engine, component);
g_list_free (engines);

return component;
}

Expand Down
57 changes: 41 additions & 16 deletions linux/ibus-keyman/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,29 +59,33 @@ ibus_disconnected_cb(IBusBus *unused_bus, gpointer unused_data) {
}

static void
start_component(void) {
GList *engines, *p;
IBusComponent *component;
add_single_keyboard(gpointer data, gpointer user_data) {
IBusEngineDesc *engine = IBUS_ENGINE_DESC(data);
#if IBUS_CHECK_VERSION(1, 3, 99)
const gchar *engine_name = ibus_engine_desc_get_name(engine);
#else
const gchar *engine_name = engine->name;
#endif /* !IBUS_CHECK_VERSION(1,3,99) */
ibus_factory_add_engine(factory, engine_name, IBUS_TYPE_KEYMAN_ENGINE);
}

ibus_init();
static void
add_keyboards(IBusBus *bus, gpointer user_data) {
GList *engines;
IBusComponent *component;

bus = ibus_bus_new();
g_signal_connect(bus, "disconnected", G_CALLBACK(ibus_disconnected_cb), NULL);
g_message("Adding keyboards to ibus");

component = ibus_keyman_get_component();

factory = ibus_factory_new(ibus_bus_get_connection(bus));
GDBusConnection *connection = ibus_bus_get_connection(bus);
factory = ibus_factory_new(connection);

g_signal_connect(bus, "disconnected", G_CALLBACK(ibus_disconnected_cb), NULL);

engines = ibus_component_get_engines(component);
for (p = engines; p != NULL; p = p->next) {
IBusEngineDesc *engine = (IBusEngineDesc *)p->data;
#if IBUS_CHECK_VERSION(1, 3, 99)
const gchar *engine_name = ibus_engine_desc_get_name(engine);
#else
const gchar *engine_name = engine->name;
#endif /* !IBUS_CHECK_VERSION(1,3,99) */
ibus_factory_add_engine(factory, engine_name, IBUS_TYPE_KEYMAN_ENGINE);
}
g_list_foreach(engines, add_single_keyboard, NULL);
g_list_free(engines);

if (ibus) {
ibus_bus_request_name(bus, "org.freedesktop.IBus.Keyman", 0);
Expand All @@ -91,6 +95,22 @@ start_component(void) {

g_object_unref(component);
km_service_get_default(NULL); // initialise dbus service
}

static void
start_component(void) {
g_message("Starting ibus-engine-keyman");

ibus_init();

bus = ibus_bus_new();

if (ibus_bus_is_connected(bus)) {
add_keyboards(bus, NULL);
} else {
g_message("Waiting for ibus-daemon to start up...");
g_signal_connect(bus, "connected", G_CALLBACK(add_keyboards), NULL);
}

ibus_main();
}
Expand Down Expand Up @@ -126,14 +146,19 @@ main(gint argc, gchar **argv) {

if (!g_option_context_parse(context, &argc, &argv, &error)) {
g_print("Option parsing failed: %s\n", error->message);
g_option_context_free(context);
exit(-1);
}

if (xml) {
print_engines_xml();
g_option_context_free(context);
exit(0);
}

start_component();

g_option_context_free(context);
g_message("Exiting ibus-engine-keyman");
return 0;
}

0 comments on commit 7c7d467

Please sign in to comment.