From c45a6561532447525a8eb28cacca6068d77a713c Mon Sep 17 00:00:00 2001 From: unisgn Date: Fri, 24 Sep 2021 23:10:38 +0800 Subject: [PATCH 1/2] ibus's dbus name owner changed detection fcitx5 works fine with kitty, except that when you restart fcitx5, kitty stop working with fcitx5, because the dbus_connection_get_is_connected(ibus->conn) in #check_connection is always true when you use fcitx5. this patch add a dbus name owner changed detection to detect the online status of ibus, in case the ibus backend uses the session bus, like fcitx5. --- glfw/ibus_glfw.c | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/glfw/ibus_glfw.c b/glfw/ibus_glfw.c index ff90beb94..715889068 100644 --- a/glfw/ibus_glfw.c +++ b/glfw/ibus_glfw.c @@ -147,6 +147,40 @@ message_handler(DBusConnection *conn UNUSED, DBusMessage *msg, void *user_data) return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; } +static DBusHandlerResult +ibus_on_owner_change(DBusConnection* conn UNUSED, DBusMessage* msg, void* user_data) { + if (dbus_message_is_signal(msg, "org.freedesktop.DBus", "NameOwnerChanged")) { + const char* name; + const char* old_owner; + const char* new_owner; + + if (!dbus_message_get_args(msg, NULL, + DBUS_TYPE_STRING, &name, + DBUS_TYPE_STRING, &old_owner, + DBUS_TYPE_STRING, &new_owner, + DBUS_TYPE_INVALID + )) { + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + } + + if (strcmp(name, "org.freedesktop.IBus") != 0) { + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + } + + _GLFWIBUSData* ibus = (_GLFWIBUSData*) user_data; + if (strcmp(new_owner, "") == 0) { + ibus->ok = false; + } else { + ibus->ok = true; + } + + return DBUS_HANDLER_RESULT_HANDLED; + + } + + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; +} + static const char* get_ibus_address_file_name(void) { const char *addr; @@ -243,6 +277,8 @@ input_context_created(DBusMessage *msg, const char* errmsg, void *data) { free((void*)ibus->input_ctx_path); ibus->input_ctx_path = _glfw_strdup(path); if (!ibus->input_ctx_path) return; + dbus_bus_add_match(ibus->conn, "type='signal',interface='org.freedesktop.DBus', member='NameOwnerChanged'", NULL); + dbus_connection_add_filter(ibus->conn, ibus_on_owner_change, ibus, free); dbus_bus_add_match(ibus->conn, "type='signal',interface='org.freedesktop.IBus.InputContext'", NULL); DBusObjectPathVTable ibus_vtable = {.message_function = message_handler}; dbus_connection_try_register_object_path(ibus->conn, ibus->input_ctx_path, &ibus_vtable, ibus, NULL); @@ -306,7 +342,7 @@ glfw_ibus_terminate(_GLFWIBUSData *ibus) { static bool check_connection(_GLFWIBUSData *ibus) { if (!ibus->inited) return false; - if (ibus->conn && dbus_connection_get_is_connected(ibus->conn)) { + if (ibus->conn && dbus_connection_get_is_connected(ibus->conn) && ibus->ok) { return ibus->ok; } struct stat s; From d078cb264a9fc72068baf7df5f682b363e71a87c Mon Sep 17 00:00:00 2001 From: unisgn Date: Sat, 25 Sep 2021 00:10:53 +0800 Subject: [PATCH 2/2] Update ibus_glfw.c --- glfw/ibus_glfw.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/glfw/ibus_glfw.c b/glfw/ibus_glfw.c index 715889068..fdfde8433 100644 --- a/glfw/ibus_glfw.c +++ b/glfw/ibus_glfw.c @@ -168,11 +168,7 @@ ibus_on_owner_change(DBusConnection* conn UNUSED, DBusMessage* msg, void* user_d } _GLFWIBUSData* ibus = (_GLFWIBUSData*) user_data; - if (strcmp(new_owner, "") == 0) { - ibus->ok = false; - } else { - ibus->ok = true; - } + ibus->ok = strcmp(new_owner, "") != 0; return DBUS_HANDLER_RESULT_HANDLED;