fix(linux): some refactorings and improvements

This commit is contained in:
Eberhard Beilharz 2026-04-07 14:46:27 +02:00
parent 1714e00901
commit 8e9e957cc0
No known key found for this signature in database
GPG key ID: E9140597606020D3

View file

@ -253,47 +253,62 @@ client_supports_surrounding_text(IBusEngine *engine) {
// surrounding text by emitting the retrieve-surrounding signal. As part
// of that signal handler, the client is expected to call
// gtk_im_context_set_surrounding_with_selection which ends calling
// set_context_if_needed at a time when IBus is still in the middle of
// determining whether the client supports surrounding text.
// ibus_keyman_engine_set_surrounding_text at a time when IBus is still
// in the middle of determining whether the client supports surrounding text.
g_assert(engine != NULL);
if (!engine->enabled) {
g_warning("%s: engine is not enabled, so return value is likley incorrect.", __FUNCTION__);
}
return (engine->client_capabilities & IBUS_CAP_SURROUNDING_TEXT) != 0;
}
static void
set_context_if_needed(IBusEngine *engine) {
IBusKeymanEngine *keyman = (IBusKeymanEngine *)engine;
set_context_impl(
IBusEngine *engine,
IBusText *text,
guint cursor_pos,
guint anchor_pos
) {
IBusKeymanEngine* keyman = (IBusKeymanEngine*)engine;
g_autofree gchar* application_context_utf8 = NULL;
guint context_start, context_end;
context_end = anchor_pos < cursor_pos ? anchor_pos : cursor_pos;
context_start = context_end > MAXCONTEXT_ITEMS ? context_end - MAXCONTEXT_ITEMS : 0;
application_context_utf8 = g_utf8_substring(ibus_text_get_text(text), context_start, context_end);
g_message(
"%s: new application context: |%s| (len:%u) cursor:%d anchor:%d", __FUNCTION__, application_context_utf8,
context_end - context_start, cursor_pos, anchor_pos);
km_core_cu* application_context_utf16 = g_utf8_to_utf16(application_context_utf8, -1, NULL, NULL, NULL);
km_core_context_status result;
result = km_core_state_context_set_if_needed(keyman->state, application_context_utf16);
g_free(application_context_utf16);
g_message(
"%s: context %s", __FUNCTION__,
result == KM_CORE_CONTEXT_STATUS_UNCHANGED ? "unchanged"
: result == KM_CORE_CONTEXT_STATUS_UPDATED ? "updated"
: result == KM_CORE_CONTEXT_STATUS_CLEARED ? "cleared"
: result == KM_CORE_CONTEXT_STATUS_ERROR ? "error"
: "invalid argument");
}
static void
set_context_if_needed(IBusEngine *engine) {
if (!client_supports_surrounding_text(engine)) {
g_message("%s: not a compliant client app", __FUNCTION__);
return;
}
IBusText *text;
g_autofree gchar *application_context_utf8 = NULL;
guint cursor_pos, anchor_pos, context_start, context_end;
guint cursor_pos, anchor_pos;
g_autofree gchar *debug_context = NULL;
g_message("%s: current core context : %s", __FUNCTION__, debug_context = get_context_debug(engine));
ibus_engine_get_surrounding_text(engine, &text, &cursor_pos, &anchor_pos);
context_end = anchor_pos < cursor_pos ? anchor_pos : cursor_pos;
context_start = context_end > MAXCONTEXT_ITEMS ? context_end - MAXCONTEXT_ITEMS : 0;
application_context_utf8 = g_utf8_substring(ibus_text_get_text(text), context_start, context_end);
g_message("%s: new application context: |%s| (len:%u) cursor:%d anchor:%d", __FUNCTION__,
application_context_utf8, context_end - context_start, cursor_pos, anchor_pos);
km_core_cu *application_context_utf16 = g_utf8_to_utf16(application_context_utf8, -1, NULL, NULL, NULL);
km_core_context_status result;
result = km_core_state_context_set_if_needed(keyman->state, application_context_utf16);
g_free(application_context_utf16);
g_message("%s: context %s", __FUNCTION__,
result == KM_CORE_CONTEXT_STATUS_UNCHANGED ? "unchanged"
: result == KM_CORE_CONTEXT_STATUS_UPDATED ? "updated"
: result == KM_CORE_CONTEXT_STATUS_CLEARED ? "cleared"
: result == KM_CORE_CONTEXT_STATUS_ERROR ? "error"
: "invalid argument");
set_context_impl(engine, text, cursor_pos, anchor_pos);
}
static void
@ -567,8 +582,6 @@ ibus_keyman_engine_constructor(
ping_keyman_system_service();
set_context_if_needed(engine);
return (GObject *) keyman;
}
@ -1070,8 +1083,12 @@ ibus_keyman_engine_set_surrounding_text(
guint cursor_pos,
guint anchor_pos
){
parent_class->set_surrounding_text(engine, text, cursor_pos, anchor_pos);
set_context_if_needed(engine);
g_message(
"%s: text=%s (len: %u), cursor_pos=%d, anchor_pos=%d", __FUNCTION__,
ibus_text_get_text(text), ibus_text_get_length(text),cursor_pos, anchor_pos);
parent_class->set_surrounding_text(engine, text, cursor_pos, anchor_pos);
set_context_impl(engine, text, cursor_pos, anchor_pos);
}
// static void ibus_keyman_engine_set_cursor_location (IBusEngine *engine,
@ -1100,7 +1117,11 @@ ibus_keyman_engine_focus_in (IBusEngine *engine)
g_message("%s", __FUNCTION__);
ibus_engine_register_properties (engine, keyman->prop_list);
set_context_if_needed(engine);
if (engine->enabled) {
// While the engine is not enabled ibus might not yet know if
// the client app supports surrounding text.
set_context_if_needed(engine);
}
parent_class->focus_in (engine);
}
@ -1161,7 +1182,8 @@ ibus_keyman_engine_enable (IBusEngine *engine)
km_service_set_ldmlfile (service, keyman->ldmlfile);
km_service_set_name (service, keyman->kb_name);
}
parent_class->enable (engine);
engine->enabled = TRUE;
parent_class->enable(engine);
}
/**
@ -1186,6 +1208,8 @@ ibus_keyman_engine_disable (IBusEngine *engine)
km_service_set_name (service, "None");
// g_clear_object(&service);
engine->enabled = FALSE;
parent_class->disable (engine);
}