Debian Codegraph @

client/gtk3/ibusimcontext.c

raw
1 /* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
2 /* vim:set et sts=4: */
3 /* ibus - The Input Bus
4 * Copyright (C) 2008-2013 Peng Huang <[email protected]>
5 * Copyright (C) 2015-2025 Takao Fujiwara <[email protected]>
6 * Copyright (C) 2008-2025 Red Hat, Inc.
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
21 * USA
22 */
23
24 #ifdef HAVE_CONFIG_H
25 # include <config.h>
26 #endif
27
28 #include <string.h>
29 #include <gtk/gtk.h>
30 #include <gdk/gdk.h>
31 #include <gdk/gdkkeysyms.h>
32 #include <ibus.h>
33 #include <ibusattrlistprivate.h>
34 #include "ibusimcontext.h"
35 #include "iconwidget.h"
36
37 #ifdef GDK_WINDOWING_WAYLAND
38 #if GTK_CHECK_VERSION (3, 98, 4)
39 #include <gdk/wayland/gdkwayland.h>
40 #else
41 #include <gdk/gdkwayland.h>
42 #endif
43 #endif
44
45 #ifdef GDK_WINDOWING_X11
46 #if GTK_CHECK_VERSION (3, 98, 4)
47 #include <gdk/x11/gdkx.h>
48 #include <X11/Xlib.h>
49 #endif
50 #endif
51
52 #if !GTK_CHECK_VERSION (2, 91, 0)
53 # define DEPRECATED_GDK_KEYSYMS 1
54 #endif
55
56 #ifdef DEBUG
57 # define IDEBUG g_debug
58 #else
59 # define IDEBUG(a...)
60 #endif
61
62 #define MAX_QUEUED_EVENTS 20
63
64 struct _IBusIMContext {
65 GtkIMContext parent;
66
67 /* instance members */
68 GtkIMContext *slave;
69 #if GTK_CHECK_VERSION (3, 98, 4)
70 GtkWidget *client_window;
71 #else
72 GdkWindow *client_window;
73 #endif
74 GtkWidget *text_view;
75 GtkSettings *settings;
76 IBusThemedRGBA *rgba;
77
78 IBusInputContext *ibuscontext;
79
80 /* preedit status */
81 gchar *preedit_string;
82 PangoAttrList *preedit_attrs;
83 gint preedit_cursor_pos;
84 gboolean preedit_visible;
85 guint preedit_mode;
86
87 GdkRectangle cursor_area;
88 gboolean has_focus;
89
90 guint32 time;
91 gint caps;
92
93 /* cancellable */
94 GCancellable *cancellable;
95 GQueue *events_queue;
96
97 #if GTK_CHECK_VERSION (3, 98, 4)
98 GdkSurface *surface;
99 GdkDevice *device;
100 double x;
101 double y;
102 #else
103 gboolean use_button_press_event;
104 #endif
105 };
106
107 struct _IBusIMContextClass {
108 GtkIMContextClass parent;
109 /* class members */
110 };
111
112 static guint _signal_commit_id = 0;
113 static guint _signal_preedit_changed_id = 0;
114 static guint _signal_preedit_start_id = 0;
115 static guint _signal_preedit_end_id = 0;
116 static guint _signal_delete_surrounding_id = 0;
117 static guint _signal_retrieve_surrounding_id = 0;
118
119 #if GTK_CHECK_VERSION (3, 98, 4)
120 static char _use_sync_mode = 1;
121 #else
122 static const gchar *_no_snooper_apps = NO_SNOOPER_APPS;
123 static gboolean _use_key_snooper = ENABLE_SNOOPER;
124 static guint _key_snooper_id = 0;
125
126 static char _use_sync_mode = 0;
127 #endif
128
129 static const gchar *_discard_password_apps = "";
130 static gboolean _use_discard_password = FALSE;
131
132 static GtkIMContext *_focus_im_context = NULL;
133 static IBusInputContext *_fake_context = NULL;
134 #if !GTK_CHECK_VERSION (3, 98, 4)
135 static GdkWindow *_input_window = NULL;
136 static GtkWidget *_input_widget = NULL;
137 #endif
138
139 /* functions prototype */
140 static void ibus_im_context_class_init (IBusIMContextClass *class);
141 static void ibus_im_context_class_fini (IBusIMContextClass *class);
142 static void ibus_im_context_init (GObject *obj);
143 static void ibus_im_context_notify (GObject *obj,
144 GParamSpec *pspec);
145 static void ibus_im_context_finalize (GObject *obj);
146 static void ibus_im_context_reset (GtkIMContext *context);
147 static gboolean ibus_im_context_filter_keypress
148 (GtkIMContext *context,
149 #if GTK_CHECK_VERSION (3, 98, 4)
150 GdkEvent *key);
151 #else
152 GdkEventKey *key);
153 #endif
154 static void ibus_im_context_focus_in (GtkIMContext *context);
155 static void ibus_im_context_focus_out (GtkIMContext *context);
156 static void ibus_im_context_get_preedit_string
157 (GtkIMContext *context,
158 gchar **str,
159 PangoAttrList **attrs,
160 gint *cursor_pos);
161 #if GTK_CHECK_VERSION (3, 98, 4)
162 static void ibus_im_context_set_client_widget
163 (GtkIMContext *context,
164 GtkWidget *client);
165 #else
166 static void ibus_im_context_set_client_window
167 (GtkIMContext *context,
168 GdkWindow *client);
169 #endif
170 static void ibus_im_context_set_cursor_location
171 (GtkIMContext *context,
172 GdkRectangle *area);
173 static void ibus_im_context_set_use_preedit
174 (GtkIMContext *context,
175 gboolean use_preedit);
176 #if !GTK_CHECK_VERSION (4, 1, 2)
177 static void ibus_im_context_set_surrounding
178 (GtkIMContext *slave,
179 const gchar *text,
180 int len,
181 int cursor_index);
182 #endif
183 static void ibus_im_context_set_surrounding_with_selection
184 (GtkIMContext *slave,
185 const gchar *text,
186 int len,
187 int cursor_index,
188 int anchor_index);
189
190 /* static methods*/
191 static void _ibus_context_update_preedit_text_cb
192 (IBusInputContext *ibuscontext,
193 IBusText *text,
194 gint cursor_pos,
195 gboolean visible,
196 guint mode,
197 IBusIMContext *ibusimcontext);
198 static void _create_input_context (IBusIMContext *context);
199 static gboolean _set_cursor_location_internal
200 (IBusIMContext *context);
201
202 static void _bus_connected_cb (IBusBus *bus,
203 IBusIMContext *context);
204 /* callback functions for slave context */
205 static void _slave_commit_cb (GtkIMContext *slave,
206 gchar *string,
207 IBusIMContext *context);
208 static void _slave_preedit_changed_cb (GtkIMContext *slave,
209 IBusIMContext *context);
210 static void _slave_preedit_start_cb (GtkIMContext *slave,
211 IBusIMContext *context);
212 static void _slave_preedit_end_cb (GtkIMContext *slave,
213 IBusIMContext *context);
214 static gboolean _slave_retrieve_surrounding_cb
215 (GtkIMContext *slave,
216 IBusIMContext *context);
217 static gboolean _slave_delete_surrounding_cb
218 (GtkIMContext *slave,
219 gint offset_from_cursor,
220 guint nchars,
221 IBusIMContext *context);
222 static gboolean _request_surrounding_text (IBusIMContext *context);
223 static void _create_fake_input_context (void);
224 static gboolean _set_content_type (IBusIMContext *context);
225
226
227 static GType _ibus_type_im_context = 0;
228 static GtkIMContextClass *parent_class = NULL;
229
230 static IBusBus *_bus = NULL;
231 static guint _daemon_name_watch_id = 0;
232 static gboolean _daemon_is_running = FALSE;
233
234 void
235 ibus_im_context_register_type (GTypeModule *type_module)
236 {
237 IDEBUG ("%s", __FUNCTION__);
238
239 static const GTypeInfo ibus_im_context_info = {
240 sizeof (IBusIMContextClass),
241 (GBaseInitFunc) NULL,
242 (GBaseFinalizeFunc) NULL,
243 (GClassInitFunc) ibus_im_context_class_init,
244 (GClassFinalizeFunc) ibus_im_context_class_fini,
245 NULL, /* class data */
246 sizeof (IBusIMContext),
247 0,
248 (GInstanceInitFunc) ibus_im_context_init,
249 };
250
251 if (!_ibus_type_im_context) {
252 if (type_module) {
253 _ibus_type_im_context =
254 g_type_module_register_type (type_module,
255 GTK_TYPE_IM_CONTEXT,
256 "IBusIMContext",
257 &ibus_im_context_info,
258 (GTypeFlags)0);
259 }
260 else {
261 _ibus_type_im_context =
262 g_type_register_static (GTK_TYPE_IM_CONTEXT,
263 "IBusIMContext",
264 &ibus_im_context_info,
265 (GTypeFlags)0);
266 }
267 }
268 }
269
270 GType
271 ibus_im_context_get_type (void)
272 {
273 IDEBUG ("%s", __FUNCTION__);
274
275 if (_ibus_type_im_context == 0) {
276 ibus_im_context_register_type (NULL);
277 }
278
279 g_assert (_ibus_type_im_context != 0);
280 return _ibus_type_im_context;
281 }
282
283 IBusIMContext *
284 ibus_im_context_new (void)
285 {
286 IDEBUG ("%s", __FUNCTION__);
287
288 GObject *obj = g_object_new (IBUS_TYPE_IM_CONTEXT, NULL);
289 return IBUS_IM_CONTEXT (obj);
290 }
291
292 #if !GTK_CHECK_VERSION (3, 98, 4)
293 static gboolean
294 _focus_in_cb (GtkWidget *widget,
295 GdkEventFocus *event,
296 gpointer user_data)
297 {
298 if (_focus_im_context == NULL && _fake_context != NULL) {
299 ibus_input_context_focus_in (_fake_context);
300 }
301 return FALSE;
302 }
303
304 static gboolean
305 _focus_out_cb (GtkWidget *widget,
306 GdkEventFocus *event,
307 gpointer user_data)
308 {
309 if (_focus_im_context == NULL && _fake_context != NULL) {
310 ibus_input_context_focus_out (_fake_context);
311 }
312 return FALSE;
313 }
314 #endif /* end of GTK_CHECK_VERSION (3, 98, 4) */
315
316 static gboolean
317 ibus_im_context_commit_event (IBusIMContext *ibusimcontext,
318 #if GTK_CHECK_VERSION (3, 98, 4)
319 GdkEvent *event)
320 #else
321 GdkEventKey *event)
322 #endif
323 {
324 guint keyval = 0;
325 GdkModifierType state = 0;
326 int i;
327 GdkModifierType no_text_input_mask;
328 gunichar ch;
329
330 #if GTK_CHECK_VERSION (3, 98, 4)
331 if (gdk_event_get_event_type (event) == GDK_KEY_RELEASE)
332 return FALSE;
333 keyval = gdk_key_event_get_keyval (event);
334 state = gdk_event_get_modifier_state (event);
335 #else
336 if (event->type == GDK_KEY_RELEASE)
337 return FALSE;
338 keyval = event->keyval;
339 state = event->state;
340 #endif
341
342 /* Ignore modifier key presses */
343 for (i = 0; i < G_N_ELEMENTS (IBUS_COMPOSE_IGNORE_KEYLIST); i++)
344 if (keyval == IBUS_COMPOSE_IGNORE_KEYLIST[i])
345 return FALSE;
346 #if GTK_CHECK_VERSION (3, 98, 4)
347 no_text_input_mask = GDK_MODIFIER_MASK;
348 #elif GTK_CHECK_VERSION (3, 4, 0)
349 no_text_input_mask = gdk_keymap_get_modifier_mask (
350 gdk_keymap_get_for_display (gdk_display_get_default ()),
351 GDK_MODIFIER_INTENT_NO_TEXT_INPUT);
352 #else
353 # ifndef GDK_WINDOWING_QUARTZ
354 # define _IBUS_NO_TEXT_INPUT_MOD_MASK (GDK_MOD1_MASK | GDK_CONTROL_MASK)
355 # else
356 # define _IBUS_NO_TEXT_INPUT_MOD_MASK (GDK_MOD2_MASK | GDK_CONTROL_MASK)
357 # endif
358
359 no_text_input_mask = _IBUS_NO_TEXT_INPUT_MOD_MASK;
360
361 # undef _IBUS_NO_TEXT_INPUT_MOD_MASK
362 #endif
363 if (state & no_text_input_mask ||
364 keyval == GDK_KEY_Return ||
365 keyval == GDK_KEY_ISO_Enter ||
366 keyval == GDK_KEY_KP_Enter) {
367 return FALSE;
368 }
369 /* #2588 If IBus tries to commit a character, it should be forwarded to
370 * the application at once with IBUS_IGNORED_MASK before the actual
371 * commit because any characters can be control characters even if
372 * they are not ASCII characters, e.g. game cursor keys with a
373 * language keyboard layout likes VIM cursor mode "hjkl" keys.
374 */
375 ch = ibus_keyval_to_unicode (keyval);
376 if (ch != 0 && !g_unichar_iscntrl (ch)) {
377 IBusText *text = ibus_text_new_from_unichar (ch);
378 g_signal_emit (ibusimcontext, _signal_commit_id, 0, text->text);
379 g_object_unref (text);
380 _request_surrounding_text (ibusimcontext);
381 #if !GTK_CHECK_VERSION (3, 98, 4)
382 /* Avoid a loop with _ibus_context_forward_key_event_cb() */
383 event->state |= IBUS_HANDLED_MASK;
384 #endif
385 return TRUE;
386 }
387 return FALSE;
388 }
389
390 typedef struct {
391 GdkEvent *event;
392 IBusIMContext *ibusimcontext;
393 } ProcessKeyEventData;
394
395 typedef struct {
396 int count;
397 guint count_cb_id;
398 gboolean retval;
399 } ProcessKeyEventReplyData;
400
401
402 static void
403 _process_key_event_done (GObject *object,
404 GAsyncResult *res,
405 gpointer user_data)
406 {
407 IBusInputContext *context = (IBusInputContext *)object;
408
409 ProcessKeyEventData *data = (ProcessKeyEventData *)user_data;
410 GdkEvent *event = data->event;
411 #if GTK_CHECK_VERSION (3, 98, 4)
412 IBusIMContext *ibusimcontext = data->ibusimcontext;
413 #endif
414 GError *error = NULL;
415 gboolean retval;
416
417 g_slice_free (ProcessKeyEventData, data);
418 retval = ibus_input_context_process_key_event_async_finish (context,
419 res,
420 &error);
421
422 if (error != NULL) {
423 g_warning ("Process Key Event failed: %s.", error->message);
424 g_error_free (error);
425 }
426
427 if (retval == FALSE) {
428 #if GTK_CHECK_VERSION (3, 98, 4)
429 g_return_if_fail (GTK_IS_IM_CONTEXT (ibusimcontext));
430 gtk_im_context_filter_key (
431 GTK_IM_CONTEXT (ibusimcontext),
432 gdk_event_get_event_type (event) == GDK_KEY_PRESS,
433 gdk_event_get_surface (event),
434 gdk_event_get_device (event),
435 gdk_event_get_time (event),
436 gdk_key_event_get_keycode (event),
437 gdk_event_get_modifier_state (event) | IBUS_IGNORED_MASK,
438 0);
439 #else
440 ((GdkEventKey *)event)->state |= IBUS_IGNORED_MASK;
441 gdk_event_put (event);
442 #endif
443 }
444 #if GTK_CHECK_VERSION (3, 98, 4)
445 gdk_event_unref (event);
446 #else
447 gdk_event_free (event);
448 #endif
449 }
450
451
452 static void
453 _process_key_event_reply_done (GObject *object,
454 GAsyncResult *res,
455 gpointer user_data)
456 {
457 IBusInputContext *context = (IBusInputContext *)object;
458 ProcessKeyEventReplyData *data = (ProcessKeyEventReplyData *)user_data;
459 GError *error = NULL;
460 gboolean retval = ibus_input_context_process_key_event_async_finish (
461 context,
462 res,
463 &error);
464 if (error != NULL) {
465 g_warning ("Process Key Event failed: %s.", error->message);
466 g_error_free (error);
467 }
468 g_return_if_fail (data);
469 data->retval = retval;
470 data->count = 0;
471 g_source_remove (data->count_cb_id);
472 }
473
474
475 static gboolean
476 _process_key_event_count_cb (gpointer user_data)
477 {
478 ProcessKeyEventReplyData *data = (ProcessKeyEventReplyData *)user_data;
479 g_return_val_if_fail (data, G_SOURCE_REMOVE);
480 if (!data->count)
481 return G_SOURCE_REMOVE;
482 /* Wait for about 10 secs. */
483 if (data->count++ == 10000) {
484 data->count = 0;
485 return G_SOURCE_REMOVE;
486 }
487 return G_SOURCE_CONTINUE;
488 }
489
490
491 static gboolean
492 _process_key_event_sync (IBusInputContext *context,
493 guint keyval,
494 guint keycode,
495 guint state)
496 {
497 gboolean retval;
498
499 g_assert (IBUS_IS_INPUT_CONTEXT (context));
500 retval = ibus_input_context_process_key_event (context,
501 keyval,
502 keycode - 8,
503 state);
504 ibus_input_context_post_process_key_event (context);
505 return retval;
506 }
507
508
509 static gboolean
510 _process_key_event_async (IBusInputContext *context,
511 guint keyval,
512 guint keycode,
513 guint state,
514 GdkEvent *event,
515 IBusIMContext *ibusimcontext)
516 {
517 ProcessKeyEventData *data = g_slice_new0 (ProcessKeyEventData);
518
519 g_assert (event);
520 if (!data) {
521 g_warning ("Cannot allocate async data");
522 return _process_key_event_sync (context, keyval, keycode, state);
523 }
524 #if GTK_CHECK_VERSION (3, 98, 4)
525 data->event = gdk_event_ref (event);
526 #else
527 data->event = gdk_event_copy (event);
528 #endif
529 data->ibusimcontext = ibusimcontext;
530 ibus_input_context_process_key_event_async (context,
531 keyval,
532 keycode - 8,
533 state,
534 -1,
535 NULL,
536 _process_key_event_done,
537 data);
538
539 return TRUE;
540 }
541
542
543 static gboolean
544 _process_key_event_hybrid_async (IBusInputContext *context,
545 guint keyval,
546 guint keycode,
547 guint state)
548 {
549 GSource *source = g_timeout_source_new (1);
550 ProcessKeyEventReplyData *data = NULL;
551 gboolean retval = FALSE;
552
553 if (source)
554 data = g_slice_new0 (ProcessKeyEventReplyData);
555 if (!data) {
556 g_warning ("Cannot wait for the reply of the process key event.");
557 retval = _process_key_event_sync (context, keyval, keycode, state);
558 if (source)
559 g_source_destroy (source);
560 return retval;
561 }
562 data->count = 1;
563 g_source_attach (source, NULL);
564 g_source_unref (source);
565 data->count_cb_id = g_source_get_id (source);
566 ibus_input_context_process_key_event_async (context,
567 keyval,
568 keycode - 8,
569 state,
570 -1,
571 NULL,
572 _process_key_event_reply_done,
573 data);
574 g_source_set_callback (source, _process_key_event_count_cb, data, NULL);
575 while (data->count)
576 g_main_context_iteration (NULL, TRUE);
577 /* #2498 Checking source->ref_count might cause Nautilus hang up
578 */
579 retval = data->retval;
580 g_slice_free (ProcessKeyEventReplyData, data);
581 return retval;
582 }
583
584
585 static gboolean
586 _process_key_event (IBusInputContext *context,
587 #if GTK_CHECK_VERSION (3, 98, 4)
588 GdkEvent *event,
589 #else
590 GdkEventKey *event,
591 #endif
592 IBusIMContext *ibusimcontext)
593 {
594 guint state;
595 guint keyval = 0;
596 guint16 hardware_keycode = 0;
597 guint keycode = 0;
598 gboolean retval = FALSE;
599
600 #if GTK_CHECK_VERSION (3, 98, 4)
601 GdkModifierType gdkstate = gdk_event_get_modifier_state (event);
602 state = (uint)gdkstate;
603 if (gdk_event_get_event_type (event) == GDK_KEY_RELEASE)
604 state |= IBUS_RELEASE_MASK;
605 keyval = gdk_key_event_get_keyval (event);
606 hardware_keycode = gdk_key_event_get_keycode (event);
607 #else
608 state = event->state;
609 if (event->type == GDK_KEY_RELEASE)
610 state |= IBUS_RELEASE_MASK;
611 keyval = event->keyval;
612 hardware_keycode = event->hardware_keycode;
613 #endif
614 keycode = hardware_keycode;
615
616 switch (_use_sync_mode) {
617 case 1: {
618 retval = _process_key_event_sync (context, keyval, keycode, state);
619 break;
620 }
621 case 2: {
622 retval = _process_key_event_hybrid_async (context,
623 keyval, keycode, state);
624 break;
625 }
626 default: {
627 retval = _process_key_event_async (context,
628 keyval, keycode, state,
629 (GdkEvent *)event,
630 ibusimcontext);
631 break;
632 }
633 }
634
635 /* GTK4 does not provide gtk_key_snooper_install() and also
636 * GtkIMContextClass->filter_keypress() cannot send the updated
637 * GdkEventKey so event->state is not updated here in GTK4.
638 */
639 #if !GTK_CHECK_VERSION (3, 98, 4)
640 if (retval)
641 event->state |= IBUS_HANDLED_MASK;
642 else
643 event->state |= IBUS_IGNORED_MASK;
644 #endif
645
646 return retval;
647 }
648
649
650 /* emit "retrieve-surrounding" glib signal of GtkIMContext, if
651 * context->caps has IBUS_CAP_SURROUNDING_TEXT and the current IBus
652 * engine needs surrounding-text.
653 */
654 static gboolean
655 _request_surrounding_text (IBusIMContext *context)
656 {
657 gboolean return_value = TRUE;
658 if (context &&
659 (context->caps & IBUS_CAP_SURROUNDING_TEXT) != 0 &&
660 context->ibuscontext != NULL &&
661 ibus_input_context_needs_surrounding_text (context->ibuscontext)) {
662 IDEBUG ("requesting surrounding text");
663 g_signal_emit (context, _signal_retrieve_surrounding_id, 0,
664 &return_value);
665 }
666 return return_value;
667 }
668
669 static gboolean
670 _set_content_type (IBusIMContext *context)
671 {
672 #if GTK_CHECK_VERSION (3, 6, 0)
673 if (context->ibuscontext != NULL) {
674 GtkInputPurpose purpose;
675 GtkInputHints hints;
676
677 g_object_get (G_OBJECT (context),
678 "input-purpose", &purpose,
679 "input-hints", &hints,
680 NULL);
681
682 if (_use_discard_password) {
683 if (purpose == GTK_INPUT_PURPOSE_PASSWORD ||
684 purpose == GTK_INPUT_PURPOSE_PIN) {
685 return FALSE;
686 }
687 }
688 ibus_input_context_set_content_type (context->ibuscontext,
689 purpose,
690 hints);
691 }
692 #endif
693 return TRUE;
694 }
695
696
697 static void
698 _settings_notify_gtk_theme_name_cb (GObject *object,
699 GParamSpec *pspec,
700 gpointer user_data)
701 {
702 IBusIMContext *ibusimcontext = (IBusIMContext *)user_data;
703
704 g_return_if_fail (IBUS_IS_IM_CONTEXT (ibusimcontext));
705 if (ibusimcontext->rgba)
706 ibus_themed_rgba_get_colors (ibusimcontext->rgba);
707 if (ibusimcontext->rgba && ibusimcontext->rgba->selected_fg &&
708 ibusimcontext->rgba->selected_bg && ibusimcontext->ibuscontext) {
709 ibus_input_context_set_selected_color (
710 ibusimcontext->ibuscontext,
711 ibusimcontext->rgba->selected_fg,
712 ibusimcontext->rgba->selected_bg);
713 }
714 }
715
716
717 #if !GTK_CHECK_VERSION (3, 98, 4)
718 static gint
719 _key_snooper_cb (GtkWidget *widget,
720 GdkEventKey *event,
721 gpointer user_data)
722 {
723 IDEBUG ("%s", __FUNCTION__);
724 gboolean retval = FALSE;
725
726 IBusIMContext *ibusimcontext = NULL;
727 IBusInputContext *ibuscontext = NULL;
728
729 if (!_use_key_snooper)
730 return FALSE;
731
732 if (_focus_im_context != NULL &&
733 ((IBusIMContext *) _focus_im_context)->has_focus == TRUE) {
734 ibusimcontext = (IBusIMContext *) _focus_im_context;
735 /* has IC with focus */
736 ibuscontext = ibusimcontext->ibuscontext;
737 }
738 else {
739 /* If no IC has focus, and fake IC has been created, then pass key events to fake IC. */
740 ibuscontext = _fake_context;
741 }
742
743 if (ibuscontext == NULL)
744 return FALSE;
745
746 if (G_UNLIKELY (event->state & IBUS_HANDLED_MASK))
747 return TRUE;
748
749 if (G_UNLIKELY (event->state & IBUS_IGNORED_MASK))
750 return FALSE;
751
752 do {
753 if (_fake_context != ibuscontext)
754 break;
755
756 /* window has input focus is not changed */
757 if (_input_window == event->window)
758 break;
759
760 if (_input_window != NULL) {
761 g_object_remove_weak_pointer ((GObject *) _input_window,
762 (gpointer *) &_input_window);
763 }
764 if (event->window != NULL) {
765 g_object_add_weak_pointer ((GObject *) event->window,
766 (gpointer *) &_input_window);
767 }
768 _input_window = event->window;
769
770 /* Trace widget has input focus, and listen focus events of it.
771 * It is workaround for Alt+Shift+Tab shortcut key issue(crosbug.com/8855).
772 * gtk_get_event_widget returns the widget that is associated with the
773 * GdkWindow of the GdkEvent.
774 * */
775 GtkWidget *widget = gtk_get_event_widget ((GdkEvent *)event);
776 /* g_assert (_input_widget != widget). */
777 if (_input_widget == widget)
778 break;
779
780 if (_input_widget != NULL) {
781 g_signal_handlers_disconnect_by_func (_input_widget,
782 (GCallback) _focus_in_cb,
783 NULL);
784 g_signal_handlers_disconnect_by_func (_input_widget,
785 (GCallback) _focus_out_cb,
786 NULL);
787 g_object_remove_weak_pointer ((GObject *) _input_widget,
788 (gpointer *) &_input_widget);
789 }
790
791 if (widget != NULL) {
792 g_signal_connect (widget,
793 "focus-in-event",
794 (GCallback) _focus_in_cb,
795 NULL);
796 g_signal_connect (widget,
797 "focus-out-event",
798 (GCallback) _focus_out_cb,
799 NULL);
800 g_object_add_weak_pointer ((GObject *) widget,
801 (gpointer *) &_input_widget);
802 }
803 _input_widget = widget;
804
805 } while (0);
806
807 if (ibusimcontext != NULL && event->type == GDK_KEY_PRESS) {
808 /* "retrieve-surrounding" signal sometimes calls unref by
809 * gtk_im_multicontext_get_slave() because priv->context_id is not
810 * the latest than global_context_id in GtkIMMulticontext.
811 * Since _focus_im_context is gotten by the focus_in event,
812 * it would be good to call ref here.
813 *
814 * Most release key events would be redundant from
815 * _ibus_context_forward_key_event_cb ().
816 */
817 g_object_ref (ibusimcontext);
818 _request_surrounding_text (ibusimcontext);
819 ibusimcontext->time = event->time;
820 }
821
822 retval = _process_key_event (ibuscontext, event, ibusimcontext);
823
824 if (ibusimcontext != NULL && event->type == GDK_KEY_PRESS) {
825 /* unref ibusimcontext could call ibus_im_context_finalize here
826 * because "retrieve-surrounding" signal could call unref.
827 */
828 g_object_unref (ibusimcontext);
829 }
830
831 return retval;
832 }
833 #endif
834
835 static gboolean
836 _get_boolean_env (const gchar *name,
837 gboolean defval)
838 {
839 const gchar *value = g_getenv (name);
840
841 if (value == NULL)
842 return defval;
843
844 if (g_strcmp0 (value, "") == 0 ||
845 g_strcmp0 (value, "0") == 0 ||
846 g_strcmp0 (value, "false") == 0 ||
847 g_strcmp0 (value, "False") == 0 ||
848 g_strcmp0 (value, "FALSE") == 0) {
849 return FALSE;
850 }
851
852 return TRUE;
853 }
854
855 static char
856 _get_char_env (const gchar *name,
857 char defval)
858 {
859 const gchar *value = g_getenv (name);
860
861 if (value == NULL)
862 return defval;
863
864 if (g_strcmp0 (value, "") == 0 ||
865 g_strcmp0 (value, "0") == 0 ||
866 g_strcmp0 (value, "false") == 0 ||
867 g_strcmp0 (value, "False") == 0 ||
868 g_strcmp0 (value, "FALSE") == 0) {
869 return 0;
870 } else if (!g_strcmp0 (value, "2")) {
871 return 2;
872 }
873
874 return 1;
875 }
876
877 static void
878 daemon_name_appeared (GDBusConnection *connection,
879 const gchar *name,
880 const gchar *owner,
881 gpointer data)
882 {
883 if (!g_strcmp0 (ibus_bus_get_service_name (_bus), IBUS_SERVICE_PORTAL)) {
884 _daemon_is_running = TRUE;
885 return;
886 }
887 /* If ibus-daemon is running and run ssh -X localhost,
888 * daemon_name_appeared() is called but ibus_get_address() == NULL
889 * because the hostname and display number are different between
890 * ibus-daemon and clients. So IBusBus would not be connected and
891 * ibusimcontext->ibuscontext == NULL and ibusimcontext->events_queue
892 * could go beyond MAX_QUEUED_EVENTS . */
893 _daemon_is_running = (ibus_get_address () != NULL);
894 }
895
896 static void
897 daemon_name_vanished (GDBusConnection *connection,
898 const gchar *name,
899 gpointer data)
900 {
901 _daemon_is_running = FALSE;
902 }
903
904 static void
905 ibus_im_context_class_init (IBusIMContextClass *class)
906 {
907 IDEBUG ("%s", __FUNCTION__);
908
909 GtkIMContextClass *im_context_class = GTK_IM_CONTEXT_CLASS (class);
910 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
911
912 parent_class = (GtkIMContextClass *) g_type_class_peek_parent (class);
913
914 im_context_class->reset = ibus_im_context_reset;
915 im_context_class->focus_in = ibus_im_context_focus_in;
916 im_context_class->focus_out = ibus_im_context_focus_out;
917 im_context_class->filter_keypress = ibus_im_context_filter_keypress;
918 im_context_class->get_preedit_string = ibus_im_context_get_preedit_string;
919 #if GTK_CHECK_VERSION (3, 98, 4)
920 im_context_class->set_client_widget = ibus_im_context_set_client_widget;
921 #else
922 im_context_class->set_client_window = ibus_im_context_set_client_window;
923 #endif
924 im_context_class->set_cursor_location = ibus_im_context_set_cursor_location;
925 im_context_class->set_use_preedit = ibus_im_context_set_use_preedit;
926 #if GTK_CHECK_VERSION (4, 1, 2)
927 im_context_class->set_surrounding_with_selection
928 = ibus_im_context_set_surrounding_with_selection;
929 #else
930 im_context_class->set_surrounding = ibus_im_context_set_surrounding;
931 #endif
932 gobject_class->notify = ibus_im_context_notify;
933 gobject_class->finalize = ibus_im_context_finalize;
934
935 _signal_commit_id =
936 g_signal_lookup ("commit", G_TYPE_FROM_CLASS (class));
937 g_assert (_signal_commit_id != 0);
938
939 _signal_preedit_changed_id =
940 g_signal_lookup ("preedit-changed", G_TYPE_FROM_CLASS (class));
941 g_assert (_signal_preedit_changed_id != 0);
942
943 _signal_preedit_start_id =
944 g_signal_lookup ("preedit-start", G_TYPE_FROM_CLASS (class));
945 g_assert (_signal_preedit_start_id != 0);
946
947 _signal_preedit_end_id =
948 g_signal_lookup ("preedit-end", G_TYPE_FROM_CLASS (class));
949 g_assert (_signal_preedit_end_id != 0);
950
951 _signal_delete_surrounding_id =
952 g_signal_lookup ("delete-surrounding", G_TYPE_FROM_CLASS (class));
953 g_assert (_signal_delete_surrounding_id != 0);
954
955 _signal_retrieve_surrounding_id =
956 g_signal_lookup ("retrieve-surrounding", G_TYPE_FROM_CLASS (class));
957 g_assert (_signal_retrieve_surrounding_id != 0);
958
959 #if GTK_CHECK_VERSION (3, 98, 4)
960 /* IBus GtkIMModule, QtIMModlue, ibus-x11, ibus-wayland are called as
961 * IBus clients.
962 * Each GTK application, each QT application, Xorg server, Wayland
963 * comppsitor are called as IBus event owners here.
964 *
965 * The IBus client processes the key events between the IBus event owner
966 * and the IBus daemon and the procedure step is to:
967 *
968 * receive the key event from the IBus event owner and forward the
969 * event to the IBus daemon with the "ProcessKeyEvent" D-Bus method at
970 * first,
971 *
972 * receive the return value from the IBus daemon with the "ProessKeyEvent"
973 * D-Bus method and forward the value to the IBus event owner secondly and
974 * the return value includes if the key event is processed normally or not.
975 *
976 * The procedure behavior can be changed by the "IBUS_ENABLE_SYNC_MODE"
977 * environment variable with the synchronous procedure or asynchronous
978 * one and value is:
979 *
980 * 1: Synchronous process key event:
981 * Wait for the return of the IBus "ProcessKeyEvent" D-Bus method
982 * synchronously and forward the return value to the IBus event owner
983 * synchronously.
984 * 0: Asynchronous process key event:
985 * Return to the IBus event owner as the key event is processed normally
986 * at first as soon as the IBus client receives the event from the
987 * IBus event owner and also forward the event to the IBus daemon with
988 * the "ProcessKeyEvent" D-Bus method and wait for the return value of
989 * the D-Bus method *asynchronously*.
990 * If the return value indicates the key event is disposed by IBus,
991 * the IBus client does not perform anything. Otherwise the IBus client
992 * forwards the key event with the gdk_event_put() in GTK3,
993 * gtk_im_context_filter_key() in GTK4, IMForwardEvent() in XIM API.
994 * 2: Hybrid asynchronous process key event:
995 * Wait for the return of the IBus "ProcessKeyEvent" D-Bus method
996 * *asynchronously* with a GSource loop and forward the return value
997 * to the IBus event owner synchronously. So IBus clients perform
998 * virtually synchronously to cover problems of IBus synchronous APIs.
999 *
1000 * The purpose of the asynchronous process is that each IBus input
1001 * method can process the key events without D-Bus timeout and also
1002 * the IBus synchronous process has a problem that the IBus
1003 * "ProcessKeyEvent" D-Bus method cannot send the commit-text and
1004 * forwar-key-event D-Bus signals until the D-Bus method is finished.
1005 *
1006 * Relative issues: #1713, #2486
1007 */
1008 _use_sync_mode = _get_char_env ("IBUS_ENABLE_SYNC_MODE", 1);
1009 #else
1010 _use_key_snooper = !_get_boolean_env ("IBUS_DISABLE_SNOOPER",
1011 !(ENABLE_SNOOPER));
1012 _use_sync_mode = (char)_get_char_env ("IBUS_ENABLE_SYNC_MODE", 0);
1013 #endif
1014 _use_discard_password = _get_boolean_env ("IBUS_DISCARD_PASSWORD", FALSE);
1015
1016 #define CHECK_APP_IN_CSV_ENV_VARIABLES(retval, \
1017 env_apps, \
1018 fallback_apps, \
1019 value_if_found) \
1020 { \
1021 const gchar * prgname = g_get_prgname (); \
1022 gchar **p; \
1023 gchar ** apps; \
1024 if (g_getenv ((#env_apps))) { \
1025 fallback_apps = g_getenv (#env_apps); \
1026 } \
1027 apps = g_strsplit ((fallback_apps), ",", 0); \
1028 for (p = apps; *p != NULL; p++) { \
1029 if (g_regex_match_simple (*p, prgname, 0, 0)) { \
1030 retval = (value_if_found); \
1031 break; \
1032 } \
1033 } \
1034 g_strfreev (apps); \
1035 }
1036
1037 #if !GTK_CHECK_VERSION (3, 98, 4)
1038 /* env IBUS_DISABLE_SNOOPER does not exist */
1039 if (_use_key_snooper) {
1040 /* disable snooper if app is in _no_snooper_apps */
1041 CHECK_APP_IN_CSV_ENV_VARIABLES (_use_key_snooper,
1042 IBUS_NO_SNOOPER_APPS,
1043 _no_snooper_apps,
1044 FALSE);
1045 }
1046 #endif
1047 if (!_use_discard_password) {
1048 CHECK_APP_IN_CSV_ENV_VARIABLES (_use_discard_password,
1049 IBUS_DISCARD_PASSWORD_APPS,
1050 _discard_password_apps,
1051 TRUE);
1052 }
1053
1054 #undef CHECK_APP_IN_CSV_ENV_VARIABLES
1055
1056 /* init bus object */
1057 if (_bus == NULL) {
1058 _bus = ibus_bus_new_async_client ();
1059
1060 /* init the global fake context */
1061 if (ibus_bus_is_connected (_bus)) {
1062 _create_fake_input_context ();
1063 }
1064
1065 g_signal_connect (_bus, "connected", G_CALLBACK (_bus_connected_cb), NULL);
1066 }
1067
1068
1069 #if !GTK_CHECK_VERSION (3, 98, 4)
1070 /* always install snooper */
1071 if (_key_snooper_id == 0) {
1072 #pragma GCC diagnostic push
1073 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1074 _key_snooper_id = gtk_key_snooper_install (_key_snooper_cb, NULL);
1075 #pragma GCC diagnostic pop
1076 }
1077 #endif
1078
1079 _daemon_name_watch_id = g_bus_watch_name (G_BUS_TYPE_SESSION,
1080 ibus_bus_get_service_name (_bus),
1081 G_BUS_NAME_WATCHER_FLAGS_NONE,
1082 daemon_name_appeared,
1083 daemon_name_vanished,
1084 NULL,
1085 NULL);
1086 }
1087
1088 static void
1089 ibus_im_context_class_fini (IBusIMContextClass *class)
1090 {
1091 #if !GTK_CHECK_VERSION (3, 98, 4)
1092 if (_key_snooper_id != 0) {
1093 IDEBUG ("snooper is terminated.");
1094 #pragma GCC diagnostic push
1095 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1096 gtk_key_snooper_remove (_key_snooper_id);
1097 #pragma GCC diagnostic pop
1098 _key_snooper_id = 0;
1099 }
1100 #endif
1101
1102 g_bus_unwatch_name (_daemon_name_watch_id);
1103 }
1104
1105 static void
1106 ibus_im_context_init (GObject *obj)
1107 {
1108 IDEBUG ("%s", __FUNCTION__);
1109
1110 IBusIMContext *ibusimcontext = IBUS_IM_CONTEXT (obj);
1111
1112 ibusimcontext->client_window = NULL;
1113
1114 // Init preedit status
1115 ibusimcontext->preedit_string = NULL;
1116 ibusimcontext->preedit_attrs = NULL;
1117 ibusimcontext->preedit_cursor_pos = 0;
1118 ibusimcontext->preedit_visible = FALSE;
1119 ibusimcontext->preedit_mode = IBUS_ENGINE_PREEDIT_CLEAR;
1120
1121 // Init cursor area
1122 ibusimcontext->cursor_area.x = -1;
1123 ibusimcontext->cursor_area.y = -1;
1124 ibusimcontext->cursor_area.width = 0;
1125 ibusimcontext->cursor_area.height = 0;
1126
1127 ibusimcontext->ibuscontext = NULL;
1128 ibusimcontext->has_focus = FALSE;
1129 ibusimcontext->time = GDK_CURRENT_TIME;
1130 #ifdef ENABLE_SURROUNDING
1131 ibusimcontext->caps = IBUS_CAP_PREEDIT_TEXT | IBUS_CAP_FOCUS | IBUS_CAP_SURROUNDING_TEXT;
1132 #else
1133 ibusimcontext->caps = IBUS_CAP_PREEDIT_TEXT | IBUS_CAP_FOCUS;
1134 #endif
1135
1136 ibusimcontext->events_queue = g_queue_new ();
1137 ibusimcontext->slave = gtk_im_context_simple_new ();
1138 ibusimcontext->settings = gtk_settings_get_default ();
1139 if (ibusimcontext->settings)
1140 g_object_ref (ibusimcontext->settings);
1141
1142 g_signal_connect (ibusimcontext->slave,
1143 "commit",
1144 G_CALLBACK (_slave_commit_cb),
1145 ibusimcontext);
1146 g_signal_connect (ibusimcontext->slave,
1147 "preedit-start",
1148 G_CALLBACK (_slave_preedit_start_cb),
1149 ibusimcontext);
1150 g_signal_connect (ibusimcontext->slave,
1151 "preedit-end",
1152 G_CALLBACK (_slave_preedit_end_cb),
1153 ibusimcontext);
1154 g_signal_connect (ibusimcontext->slave,
1155 "preedit-changed",
1156 G_CALLBACK (_slave_preedit_changed_cb),
1157 ibusimcontext);
1158 g_signal_connect (ibusimcontext->slave,
1159 "retrieve-surrounding",
1160 G_CALLBACK (_slave_retrieve_surrounding_cb),
1161 ibusimcontext);
1162 g_signal_connect (ibusimcontext->slave,
1163 "delete-surrounding",
1164 G_CALLBACK (_slave_delete_surrounding_cb),
1165 ibusimcontext);
1166 g_signal_connect (ibusimcontext->settings,
1167 "notify::gtk-theme-name",
1168 G_CALLBACK (_settings_notify_gtk_theme_name_cb),
1169 ibusimcontext);
1170
1171 if (ibus_bus_is_connected (_bus)) {
1172 _create_input_context (ibusimcontext);
1173 }
1174
1175 g_signal_connect (_bus, "connected", G_CALLBACK (_bus_connected_cb), obj);
1176 }
1177
1178 static void
1179 ibus_im_context_notify (GObject *obj,
1180 GParamSpec *pspec)
1181 {
1182 IDEBUG ("%s", __FUNCTION__);
1183
1184 if (g_strcmp0 (pspec->name, "input-purpose") == 0 ||
1185 g_strcmp0 (pspec->name, "input-hints") == 0) {
1186 _set_content_type (IBUS_IM_CONTEXT (obj));
1187 }
1188 }
1189
1190 static void
1191 ibus_im_context_finalize (GObject *obj)
1192 {
1193 IDEBUG ("%s", __FUNCTION__);
1194
1195 IBusIMContext *ibusimcontext = IBUS_IM_CONTEXT (obj);
1196
1197 g_signal_handlers_disconnect_by_func (_bus, G_CALLBACK (_bus_connected_cb), obj);
1198
1199 if (ibusimcontext->cancellable != NULL) {
1200 /* Cancel any ongoing create input context request */
1201 g_cancellable_cancel (ibusimcontext->cancellable);
1202 g_object_unref (ibusimcontext->cancellable);
1203 ibusimcontext->cancellable = NULL;
1204 }
1205
1206 if (ibusimcontext->ibuscontext) {
1207 ibus_proxy_destroy ((IBusProxy *)ibusimcontext->ibuscontext);
1208 g_clear_object (&ibusimcontext->ibuscontext);
1209 }
1210
1211 if (ibusimcontext->settings) {
1212 g_signal_handlers_disconnect_by_func (
1213 ibusimcontext->settings,
1214 _settings_notify_gtk_theme_name_cb,
1215 ibusimcontext);
1216 g_clear_object (&ibusimcontext->settings);
1217 }
1218
1219 #if GTK_CHECK_VERSION (3, 98, 4)
1220 ibus_im_context_set_client_widget ((GtkIMContext *)ibusimcontext, NULL);
1221 #else
1222 ibus_im_context_set_client_window ((GtkIMContext *)ibusimcontext, NULL);
1223 #endif
1224
1225 if (ibusimcontext->slave) {
1226 g_object_unref (ibusimcontext->slave);
1227 ibusimcontext->slave = NULL;
1228 }
1229
1230 // release preedit
1231 if (ibusimcontext->preedit_string) {
1232 g_free (ibusimcontext->preedit_string);
1233 }
1234 if (ibusimcontext->preedit_attrs) {
1235 pango_attr_list_unref (ibusimcontext->preedit_attrs);
1236 }
1237
1238 #if GTK_CHECK_VERSION (3, 98, 4)
1239 g_queue_free_full (ibusimcontext->events_queue,
1240 (GDestroyNotify)gdk_event_unref);
1241 #else
1242 g_queue_free_full (ibusimcontext->events_queue,
1243 (GDestroyNotify)gdk_event_free);
1244 #endif
1245
1246 G_OBJECT_CLASS(parent_class)->finalize (obj);
1247 }
1248
1249 static void
1250 ibus_im_context_clear_preedit_text (IBusIMContext *ibusimcontext)
1251 {
1252 gchar *preedit_string = NULL;
1253 IBusText *text;
1254 g_assert (ibusimcontext->ibuscontext);
1255 if (ibusimcontext->preedit_visible &&
1256 ibusimcontext->preedit_mode == IBUS_ENGINE_PREEDIT_COMMIT) {
1257 preedit_string = g_strdup (ibusimcontext->preedit_string);
1258 }
1259
1260 /* Clear the preedit_string but keep the preedit_cursor_pos and
1261 * preedit_visible because a time lag could happen, firefox commit
1262 * the preedit text before the preedit text is cleared and it cause
1263 * a double commits of the Hangul preedit in firefox if the preedit
1264 * would be located on the URL bar and click on anywhere of firefox
1265 * out of the URL bar.
1266 */
1267 if (!(text = ibus_text_new_from_string (""))) {
1268 g_warning ("Cannot allocate IBusText.");
1269 g_free (preedit_string);
1270 return;
1271 }
1272 _ibus_context_update_preedit_text_cb (ibusimcontext->ibuscontext,
1273 text,
1274 ibusimcontext->preedit_cursor_pos,
1275 ibusimcontext->preedit_visible,
1276 IBUS_ENGINE_PREEDIT_CLEAR,
1277 ibusimcontext);
1278 g_object_unref (text);
1279 if (preedit_string) {
1280 g_signal_emit (ibusimcontext, _signal_commit_id, 0, preedit_string);
1281 g_free (preedit_string);
1282 _request_surrounding_text (ibusimcontext);
1283 }
1284 }
1285
1286 static gboolean
1287 ibus_im_context_filter_keypress (GtkIMContext *context,
1288 #if GTK_CHECK_VERSION (3, 98, 4)
1289 GdkEvent *event)
1290 #else
1291 GdkEventKey *event)
1292 #endif
1293 {
1294 IDEBUG ("%s", __FUNCTION__);
1295
1296 IBusIMContext *ibusimcontext = IBUS_IM_CONTEXT (context);
1297
1298 if (!_daemon_is_running)
1299 return gtk_im_context_filter_keypress (ibusimcontext->slave, event);
1300
1301 /* If context does not have focus, ibus will process key event in
1302 * sync mode. It is a workaround for increase search in treeview.
1303 */
1304 if (!ibusimcontext->has_focus)
1305 return gtk_im_context_filter_keypress (ibusimcontext->slave, event);
1306
1307 #if GTK_CHECK_VERSION (3, 98, 4)
1308 {
1309 GdkModifierType state = gdk_event_get_modifier_state (event);
1310 if (state & IBUS_HANDLED_MASK)
1311 return TRUE;
1312 if (state & IBUS_IGNORED_MASK)
1313 return ibus_im_context_commit_event (ibusimcontext, event);
1314 }
1315 #else
1316 if (event->state & IBUS_HANDLED_MASK)
1317 return TRUE;
1318
1319 /* Do not call gtk_im_context_filter_keypress() because
1320 * gtk_im_context_simple_filter_keypress() binds Ctrl-Shift-u
1321 */
1322 if (event->state & IBUS_IGNORED_MASK)
1323 return ibus_im_context_commit_event (ibusimcontext, event);
1324
1325 /* XXX it is a workaround for some applications do not set client
1326 * window. */
1327 if (ibusimcontext->client_window == NULL && event->window != NULL)
1328 gtk_im_context_set_client_window ((GtkIMContext *)ibusimcontext,
1329 event->window);
1330 #endif
1331
1332 _request_surrounding_text (ibusimcontext);
1333
1334 #if GTK_CHECK_VERSION (3, 98, 4)
1335 ibusimcontext->time = gdk_event_get_time (event);
1336 ibusimcontext->surface= gdk_event_get_surface (event);
1337 ibusimcontext->device = gdk_event_get_device (event);
1338 gdk_event_get_position (event, &ibusimcontext->x, &ibusimcontext->y);
1339 #else
1340 ibusimcontext->time = event->time;
1341 #endif
1342
1343 if (ibusimcontext->ibuscontext) {
1344 if (_process_key_event (ibusimcontext->ibuscontext,
1345 event,
1346 ibusimcontext)) {
1347 return TRUE;
1348 } else {
1349 return gtk_im_context_filter_keypress (ibusimcontext->slave,
1350 event);
1351 }
1352 }
1353
1354 /* At this point we _should_ be waiting for the IBus context to be
1355 * created or the connection to IBus to be established. If that's
1356 * the case we queue events to be processed when the IBus context
1357 * is ready. */
1358 g_return_val_if_fail (ibusimcontext->cancellable != NULL ||
1359 ibus_bus_is_connected (_bus) == FALSE,
1360 FALSE);
1361 g_queue_push_tail (ibusimcontext->events_queue,
1362 #if GTK_CHECK_VERSION (3, 98, 4)
1363 gdk_event_ref (event));
1364 #else
1365 gdk_event_copy ((GdkEvent *)event));
1366 #endif
1367
1368 if (g_queue_get_length (ibusimcontext->events_queue) > MAX_QUEUED_EVENTS) {
1369 g_warning ("Events queue growing too big, will start to drop.");
1370 #if GTK_CHECK_VERSION (3, 98, 4)
1371 gdk_event_unref ((GdkEvent *)
1372 g_queue_pop_head (ibusimcontext->events_queue));
1373 #else
1374 gdk_event_free ((GdkEvent *)
1375 g_queue_pop_head (ibusimcontext->events_queue));
1376 #endif
1377 }
1378
1379 return TRUE;
1380 }
1381
1382 static void
1383 ibus_im_context_focus_in (GtkIMContext *context)
1384 {
1385 IBusIMContext *ibusimcontext = (IBusIMContext *) context;
1386 GtkWidget *widget = NULL;
1387
1388 IDEBUG ("%s", __FUNCTION__);
1389
1390 if (ibusimcontext->has_focus)
1391 return;
1392
1393 /* don't set focus on password entry */
1394 #if GTK_CHECK_VERSION (3, 98, 4)
1395 widget = ibusimcontext->client_window;
1396 #else
1397 if (ibusimcontext->client_window != NULL) {
1398 gdk_window_get_user_data (ibusimcontext->client_window,
1399 (gpointer *)&widget);
1400
1401 }
1402 #endif
1403
1404 if (widget && GTK_IS_ENTRY (widget) &&
1405 !gtk_entry_get_visibility (GTK_ENTRY (widget))) {
1406 return;
1407 }
1408 /* Do not call gtk_im_context_focus_out() here.
1409 * google-chrome's notification popup window (Pushbullet)
1410 * takes the focus and the popup window disappears.
1411 * So other applications lose the focus because
1412 * ibusimcontext->has_focus is FALSE if
1413 * gtk_im_context_focus_out() is called here when
1414 * _focus_im_context != context.
1415 */
1416 if (_focus_im_context == NULL) {
1417 /* focus out fake context */
1418 if (_fake_context != NULL) {
1419 ibus_input_context_focus_out (_fake_context);
1420 }
1421 }
1422
1423 ibusimcontext->has_focus = TRUE;
1424 if (ibusimcontext->ibuscontext) {
1425 if (!_set_content_type (ibusimcontext)) {
1426 ibusimcontext->has_focus = FALSE;
1427 return;
1428 }
1429 ibus_input_context_focus_in (ibusimcontext->ibuscontext);
1430 }
1431
1432 gtk_im_context_focus_in (ibusimcontext->slave);
1433
1434 /* set_cursor_location_internal() will get origin from X server,
1435 * it blocks UI. So delay it to idle callback. */
1436 g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
1437 (GSourceFunc) _set_cursor_location_internal,
1438 g_object_ref (ibusimcontext),
1439 (GDestroyNotify) g_object_unref);
1440
1441 /* retrieve the initial surrounding-text (regardless of whether
1442 * the current IBus engine needs surrounding-text) */
1443 _request_surrounding_text (ibusimcontext);
1444
1445 g_object_add_weak_pointer ((GObject *) context,
1446 (gpointer *) &_focus_im_context);
1447 _focus_im_context = context;
1448 }
1449
1450 static void
1451 ibus_im_context_focus_out (GtkIMContext *context)
1452 {
1453 IDEBUG ("%s", __FUNCTION__);
1454 IBusIMContext *ibusimcontext = (IBusIMContext *) context;
1455
1456 if (ibusimcontext->has_focus == FALSE) {
1457 return;
1458 }
1459
1460 /* If _use_discard_password is TRUE or GtkEntry has no visibility,
1461 * _focus_im_context is NULL.
1462 */
1463 if (_focus_im_context) {
1464 g_object_remove_weak_pointer ((GObject *) context,
1465 (gpointer *) &_focus_im_context);
1466 _focus_im_context = NULL;
1467 }
1468
1469 ibusimcontext->has_focus = FALSE;
1470 if (ibusimcontext->ibuscontext) {
1471 ibus_im_context_clear_preedit_text (ibusimcontext);
1472 ibus_input_context_focus_out (ibusimcontext->ibuscontext);
1473 }
1474
1475 gtk_im_context_focus_out (ibusimcontext->slave);
1476
1477 /* focus in the fake ic */
1478 if (_fake_context != NULL) {
1479 ibus_input_context_focus_in (_fake_context);
1480 }
1481 }
1482
1483 static void
1484 ibus_im_context_reset (GtkIMContext *context)
1485 {
1486 IDEBUG ("%s", __FUNCTION__);
1487
1488 IBusIMContext *ibusimcontext = IBUS_IM_CONTEXT (context);
1489
1490 if (ibusimcontext->ibuscontext) {
1491 /* Commented out ibus_im_context_clear_preedit_text().
1492 * Hangul needs to receive the reset callback with button press
1493 * but other IMEs should avoid to receive the reset callback
1494 * by themselves.
1495 * IBus uses button-press-event instead until GTK is fixed.
1496 * https://gitlab.gnome.org/GNOME/gtk/issues/1534
1497 */
1498 if (_use_sync_mode != 0)
1499 ibus_im_context_clear_preedit_text (ibusimcontext);
1500 ibus_input_context_reset (ibusimcontext->ibuscontext);
1501 }
1502 gtk_im_context_reset (ibusimcontext->slave);
1503 }
1504
1505
1506 static void
1507 ibus_im_context_get_preedit_string (GtkIMContext *context,
1508 gchar **str,
1509 PangoAttrList **attrs,
1510 gint *cursor_pos)
1511 {
1512 IDEBUG ("%s", __FUNCTION__);
1513
1514 IBusIMContext *ibusimcontext = IBUS_IM_CONTEXT (context);
1515
1516 if (ibusimcontext->preedit_visible) {
1517 if (str) {
1518 *str = g_strdup (ibusimcontext->preedit_string ? ibusimcontext->preedit_string: "");
1519 }
1520
1521 if (attrs) {
1522 *attrs = ibusimcontext->preedit_attrs ?
1523 pango_attr_list_ref (ibusimcontext->preedit_attrs):
1524 pango_attr_list_new ();
1525 }
1526
1527 if (cursor_pos) {
1528 *cursor_pos = ibusimcontext->preedit_cursor_pos;
1529 }
1530 }
1531 else {
1532 if (str) {
1533 *str = g_strdup ("");
1534 }
1535 if (attrs) {
1536 *attrs = pango_attr_list_new ();
1537 }
1538 if (cursor_pos) {
1539 *cursor_pos = 0;
1540 }
1541 }
1542 IDEBUG ("str=%s", *str);
1543 }
1544
1545
1546 #if !GTK_CHECK_VERSION (3, 98, 4)
1547 /* Use the button-press-event signal until GtkIMContext always emits the reset
1548 * signal.
1549 * https://gitlab.gnome.org/GNOME/gtk/merge_requests/460
1550 */
1551 static gboolean
1552 ibus_im_context_button_press_event_cb (GtkWidget *widget,
1553 GdkEventButton *event,
1554 IBusIMContext *ibusimcontext)
1555 {
1556 if (event->button != 1)
1557 return FALSE;
1558
1559 if (ibusimcontext->ibuscontext) {
1560 ibus_im_context_clear_preedit_text (ibusimcontext);
1561 ibus_input_context_reset (ibusimcontext->ibuscontext);
1562 }
1563 return FALSE;
1564 }
1565
1566 static void
1567 _connect_button_press_event (IBusIMContext *ibusimcontext,
1568 gboolean do_connect)
1569 {
1570 GtkWidget *widget = NULL;
1571
1572 g_assert (ibusimcontext->client_window);
1573 gdk_window_get_user_data (ibusimcontext->client_window,
1574 (gpointer *)&widget);
1575 /* firefox needs GtkWidget instead of GtkWindow */
1576 if (GTK_IS_WIDGET (widget)) {
1577 if (do_connect) {
1578 g_signal_connect (
1579 widget,
1580 "button-press-event",
1581 G_CALLBACK (ibus_im_context_button_press_event_cb),
1582 ibusimcontext);
1583 ibusimcontext->use_button_press_event = TRUE;
1584 } else {
1585 g_signal_handlers_disconnect_by_func (
1586 widget,
1587 G_CALLBACK (ibus_im_context_button_press_event_cb),
1588 ibusimcontext);
1589 ibusimcontext->use_button_press_event = FALSE;
1590 }
1591 }
1592 }
1593 #endif
1594
1595 #if GTK_CHECK_VERSION (3, 98, 4)
1596 static void
1597 ibus_im_context_set_client_widget (GtkIMContext *context,
1598 GtkWidget *client)
1599 #else
1600 static void
1601 ibus_im_context_set_client_window (GtkIMContext *context,
1602 GdkWindow *client)
1603 #endif
1604 {
1605 IBusIMContext *ibusimcontext;
1606
1607 IDEBUG ("%s", __FUNCTION__);
1608
1609 ibusimcontext = IBUS_IM_CONTEXT (context);
1610
1611 if (ibusimcontext->client_window) {
1612 #if !GTK_CHECK_VERSION (3, 98, 4)
1613 if (ibusimcontext->use_button_press_event && _use_sync_mode == 0)
1614 _connect_button_press_event (ibusimcontext, FALSE);
1615 #endif
1616 g_clear_object (&ibusimcontext->client_window);
1617 }
1618 if (ibusimcontext->rgba)
1619 g_clear_object (&ibusimcontext->rgba);
1620 if (ibusimcontext->text_view)
1621 g_clear_object (&ibusimcontext->text_view);
1622
1623 if (client != NULL) {
1624 #if GTK_CHECK_VERSION (2, 91, 0)
1625 GtkStyleContext *style_context;
1626 #else
1627 GtkStyle *style_context;
1628 #endif
1629 ibusimcontext->client_window = g_object_ref (client);
1630 #if !GTK_CHECK_VERSION (3, 98, 4)
1631 if (!ibusimcontext->use_button_press_event && _use_sync_mode == 0)
1632 _connect_button_press_event (ibusimcontext, TRUE);
1633 #endif
1634 ibusimcontext->text_view = g_object_ref_sink (gtk_text_view_new ());
1635 #if GTK_CHECK_VERSION (2, 91, 0)
1636 style_context = gtk_widget_get_style_context (ibusimcontext->text_view);
1637 #else
1638 style_context = gtk_widget_get_style (ibusimcontext->text_view);
1639 #endif
1640 ibusimcontext->rgba = ibus_themed_rgba_new (style_context);
1641 }
1642 #if GTK_CHECK_VERSION (3, 98, 4)
1643 if (ibusimcontext->slave)
1644 gtk_im_context_set_client_widget (ibusimcontext->slave, client);
1645 #else
1646 if (ibusimcontext->slave)
1647 gtk_im_context_set_client_window (ibusimcontext->slave, client);
1648 #endif
1649 }
1650
1651 static void
1652 _set_rect_scale_factor_with_window (GdkRectangle *area,
1653 #if GTK_CHECK_VERSION (3, 98, 4)
1654 GtkWidget *window)
1655 #else
1656 GdkWindow *window)
1657 #endif
1658 {
1659 #if GTK_CHECK_VERSION (3, 10, 0)
1660 int scale_factor;
1661
1662 g_assert (area);
1663 #if GTK_CHECK_VERSION (3, 98, 4)
1664 g_assert (GTK_IS_WIDGET (window));
1665
1666 scale_factor = gtk_widget_get_scale_factor (window);
1667 #else
1668 g_assert (GDK_IS_WINDOW (window));
1669
1670 scale_factor = gdk_window_get_scale_factor (window);
1671 #endif
1672 area->x *= scale_factor;
1673 area->y *= scale_factor;
1674 area->width *= scale_factor;
1675 area->height *= scale_factor;
1676 #endif
1677 }
1678
1679 static gboolean
1680 _set_cursor_location_internal (IBusIMContext *ibusimcontext)
1681 {
1682 GdkRectangle area;
1683 GdkDisplay *display = NULL;
1684 #if GTK_CHECK_VERSION (3, 98, 4)
1685 GtkWidget *root;
1686 GtkNative *native;
1687 graphene_point_t p;
1688 int tx = 0, ty = 0;
1689 double nx = 0., ny = 0.;
1690 #endif
1691
1692 if(ibusimcontext->client_window == NULL ||
1693 ibusimcontext->ibuscontext == NULL) {
1694 return FALSE;
1695 }
1696
1697 area = ibusimcontext->cursor_area;
1698
1699 #if GTK_CHECK_VERSION (3, 98, 4)
1700 root = GTK_WIDGET (gtk_widget_get_root (ibusimcontext->client_window));
1701 /* Translates the given point in client_window coordinates to coordinates
1702 relative to root coordinate system. */
1703 if (!gtk_widget_compute_point (ibusimcontext->client_window,
1704 root,
1705 &GRAPHENE_POINT_INIT (area.x, area.y),
1706 &p)) {
1707 graphene_point_init (&p, area.x, area.y);
1708 }
1709
1710 native = gtk_widget_get_native (ibusimcontext->client_window);
1711 /* Translates from the surface coordinates into the widget coordinates. */
1712 gtk_native_get_surface_transform (native, &nx, &ny);
1713
1714 #ifdef HAVE_XIM
1715 display = gtk_widget_get_display (ibusimcontext->client_window);
1716 if (GDK_IS_X11_DISPLAY (display)) {
1717 GdkSurface *surface = gtk_native_get_surface
1718 (gtk_widget_get_native (ibusimcontext->client_window));
1719 Window child;
1720 int scale_factor = gtk_widget_get_scale_factor
1721 (ibusimcontext->client_window);
1722
1723 XTranslateCoordinates (GDK_DISPLAY_XDISPLAY (display),
1724 GDK_SURFACE_XID (surface),
1725 gdk_x11_display_get_xrootwindow (display),
1726 0, 0, &tx, &ty,
1727 &child);
1728
1729 tx = tx / scale_factor;
1730 ty = ty / scale_factor;
1731 }
1732 #endif
1733
1734 area.x = p.x + nx + tx;
1735 area.y = p.y + ny + ty;
1736 #else
1737 gdk_window_get_root_coords (ibusimcontext->client_window,
1738 area.x, area.y,
1739 &area.x, &area.y);
1740 #endif
1741
1742 _set_rect_scale_factor_with_window (&area, ibusimcontext->client_window);
1743
1744 #ifdef GDK_WINDOWING_WAYLAND
1745 #if !GTK_CHECK_VERSION (3, 98, 4)
1746 display = gdk_window_get_display (ibusimcontext->client_window);
1747 #endif
1748
1749 if (GDK_IS_WAYLAND_DISPLAY (display)) {
1750 ibus_input_context_set_cursor_location_relative (
1751 ibusimcontext->ibuscontext,
1752 area.x,
1753 area.y,
1754 area.width,
1755 area.height);
1756 } else {
1757 #endif
1758 ibus_input_context_set_cursor_location (ibusimcontext->ibuscontext,
1759 area.x,
1760 area.y,
1761 area.width,
1762 area.height);
1763 #ifdef GDK_WINDOWING_WAYLAND
1764 }
1765 #endif
1766
1767 return FALSE;
1768 }
1769
1770 static void
1771 ibus_im_context_set_cursor_location (GtkIMContext *context, GdkRectangle *area)
1772 {
1773 IDEBUG ("%s", __FUNCTION__);
1774
1775 IBusIMContext *ibusimcontext = IBUS_IM_CONTEXT (context);
1776
1777 #if !GTK_CHECK_VERSION (3, 93, 0)
1778 /* The area is the relative coordinates and this has to get the absolute
1779 * ones in _set_cursor_location_internal() since GTK 4.0.
1780 */
1781 if (ibusimcontext->cursor_area.x == area->x &&
1782 ibusimcontext->cursor_area.y == area->y &&
1783 ibusimcontext->cursor_area.width == area->width &&
1784 ibusimcontext->cursor_area.height == area->height) {
1785 return;
1786 }
1787 #endif
1788 ibusimcontext->cursor_area = *area;
1789 _set_cursor_location_internal (ibusimcontext);
1790 gtk_im_context_set_cursor_location (ibusimcontext->slave, area);
1791 }
1792
1793 static void
1794 ibus_im_context_set_use_preedit (GtkIMContext *context, gboolean use_preedit)
1795 {
1796 IDEBUG ("%s", __FUNCTION__);
1797
1798 IBusIMContext *ibusimcontext = IBUS_IM_CONTEXT (context);
1799
1800 if (use_preedit) {
1801 ibusimcontext->caps |= IBUS_CAP_PREEDIT_TEXT;
1802 }
1803 else {
1804 ibusimcontext->caps &= ~IBUS_CAP_PREEDIT_TEXT;
1805 }
1806 if(ibusimcontext->ibuscontext) {
1807 ibus_input_context_set_capabilities (ibusimcontext->ibuscontext,
1808 ibusimcontext->caps);
1809 }
1810 gtk_im_context_set_use_preedit (ibusimcontext->slave, use_preedit);
1811 }
1812
1813 static guint
1814 get_selection_anchor_point (IBusIMContext *ibusimcontext,
1815 guint cursor_pos,
1816 guint surrounding_text_len)
1817 {
1818 GtkWidget *widget;
1819 if (ibusimcontext->client_window == NULL) {
1820 return cursor_pos;
1821 }
1822 #if GTK_CHECK_VERSION (3, 98, 4)
1823 widget = ibusimcontext->client_window;
1824 #else
1825 gdk_window_get_user_data (ibusimcontext->client_window, (gpointer *)&widget);
1826 #endif
1827
1828 if (!GTK_IS_TEXT_VIEW (widget)){
1829 return cursor_pos;
1830 }
1831
1832 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
1833 GtkTextBuffer *buffer = gtk_text_view_get_buffer (text_view);
1834
1835 if (!gtk_text_buffer_get_has_selection (buffer)) {
1836 return cursor_pos;
1837 }
1838
1839 GtkTextIter start_iter, end_iter, cursor_iter;
1840 if (!gtk_text_buffer_get_selection_bounds (buffer, &start_iter, &end_iter)) {
1841 return cursor_pos;
1842 }
1843
1844 gtk_text_buffer_get_iter_at_mark (buffer,
1845 &cursor_iter,
1846 gtk_text_buffer_get_insert (buffer));
1847
1848 guint start_index = gtk_text_iter_get_offset (&start_iter);
1849 guint end_index = gtk_text_iter_get_offset (&end_iter);
1850 guint cursor_index = gtk_text_iter_get_offset (&cursor_iter);
1851
1852 guint anchor;
1853
1854 if (start_index == cursor_index) {
1855 anchor = end_index;
1856 } else if (end_index == cursor_index) {
1857 anchor = start_index;
1858 } else {
1859 return cursor_pos;
1860 }
1861
1862 // Change absolute index to relative position.
1863 guint relative_origin = cursor_index - cursor_pos;
1864
1865 if (anchor < relative_origin) {
1866 return cursor_pos;
1867 }
1868 anchor -= relative_origin;
1869
1870 if (anchor > surrounding_text_len) {
1871 return cursor_pos;
1872 }
1873
1874 return anchor;
1875 }
1876
1877 #if !GTK_CHECK_VERSION (4, 1, 2)
1878 static void
1879 ibus_im_context_set_surrounding (GtkIMContext *context,
1880 const gchar *text,
1881 int len,
1882 int cursor_index)
1883 {
1884 ibus_im_context_set_surrounding_with_selection (context,
1885 text,
1886 len,
1887 cursor_index,
1888 cursor_index);
1889 }
1890 #endif
1891
1892 static void
1893 ibus_im_context_set_surrounding_with_selection (GtkIMContext *context,
1894 const gchar *text,
1895 int len,
1896 int cursor_index,
1897 int anchor_index)
1898 {
1899 g_return_if_fail (context != NULL);
1900 g_return_if_fail (IBUS_IS_IM_CONTEXT (context));
1901 g_return_if_fail (text != NULL);
1902 g_return_if_fail (strlen (text) >= len);
1903 g_return_if_fail (0 <= cursor_index && cursor_index <= len);
1904
1905 IBusIMContext *ibusimcontext = IBUS_IM_CONTEXT (context);
1906
1907 if (ibusimcontext->ibuscontext) {
1908 IBusText *ibustext;
1909 guint cursor_pos;
1910 guint utf8_len;
1911 gchar *p;
1912
1913 p = g_strndup (text, len);
1914 cursor_pos = g_utf8_strlen (p, cursor_index);
1915 utf8_len = g_utf8_strlen(p, len);
1916 ibustext = ibus_text_new_from_string (p);
1917 g_free (p);
1918
1919 gint anchor_pos = get_selection_anchor_point (ibusimcontext,
1920 cursor_pos,
1921 utf8_len);
1922 ibus_input_context_set_surrounding_text (ibusimcontext->ibuscontext,
1923 ibustext,
1924 cursor_pos,
1925 anchor_pos);
1926 }
1927 #if GTK_CHECK_VERSION (4, 1, 2)
1928 gtk_im_context_set_surrounding_with_selection (ibusimcontext->slave,
1929 text,
1930 len,
1931 cursor_index,
1932 anchor_index);
1933 #else
1934 gtk_im_context_set_surrounding (ibusimcontext->slave,
1935 text,
1936 len,
1937 cursor_index);
1938 #endif
1939 }
1940
1941 static void
1942 _bus_connected_cb (IBusBus *bus,
1943 IBusIMContext *ibusimcontext)
1944 {
1945 IDEBUG ("%s", __FUNCTION__);
1946 if (ibusimcontext)
1947 _create_input_context (ibusimcontext);
1948 else
1949 _create_fake_input_context ();
1950 }
1951
1952 static void
1953 _ibus_context_commit_text_cb (IBusInputContext *ibuscontext,
1954 IBusText *text,
1955 IBusIMContext *ibusimcontext)
1956 {
1957 IDEBUG ("%s", __FUNCTION__);
1958
1959 g_signal_emit (ibusimcontext, _signal_commit_id, 0, text->text);
1960
1961 _request_surrounding_text (ibusimcontext);
1962 }
1963
1964 #if !GTK_CHECK_VERSION (3, 98, 4)
1965 static gboolean
1966 _key_is_modifier (guint keyval)
1967 {
1968 /* See gdkkeys-x11.c:_gdk_keymap_key_is_modifier() for how this
1969 * really should be implemented */
1970
1971 switch (keyval) {
1972 #ifdef DEPRECATED_GDK_KEYSYMS
1973 case GDK_Shift_L:
1974 case GDK_Shift_R:
1975 case GDK_Control_L:
1976 case GDK_Control_R:
1977 case GDK_Caps_Lock:
1978 case GDK_Shift_Lock:
1979 case GDK_Meta_L:
1980 case GDK_Meta_R:
1981 case GDK_Alt_L:
1982 case GDK_Alt_R:
1983 case GDK_Super_L:
1984 case GDK_Super_R:
1985 case GDK_Hyper_L:
1986 case GDK_Hyper_R:
1987 case GDK_ISO_Lock:
1988 case GDK_ISO_Level2_Latch:
1989 case GDK_ISO_Level3_Shift:
1990 case GDK_ISO_Level3_Latch:
1991 case GDK_ISO_Level3_Lock:
1992 case GDK_ISO_Level5_Shift:
1993 case GDK_ISO_Level5_Latch:
1994 case GDK_ISO_Level5_Lock:
1995 case GDK_ISO_Group_Shift:
1996 case GDK_ISO_Group_Latch:
1997 case GDK_ISO_Group_Lock:
1998 return TRUE;
1999 #else
2000 case GDK_KEY_Shift_L:
2001 case GDK_KEY_Shift_R:
2002 case GDK_KEY_Control_L:
2003 case GDK_KEY_Control_R:
2004 case GDK_KEY_Caps_Lock:
2005 case GDK_KEY_Shift_Lock:
2006 case GDK_KEY_Meta_L:
2007 case GDK_KEY_Meta_R:
2008 case GDK_KEY_Alt_L:
2009 case GDK_KEY_Alt_R:
2010 case GDK_KEY_Super_L:
2011 case GDK_KEY_Super_R:
2012 case GDK_KEY_Hyper_L:
2013 case GDK_KEY_Hyper_R:
2014 case GDK_KEY_ISO_Lock:
2015 case GDK_KEY_ISO_Level2_Latch:
2016 case GDK_KEY_ISO_Level3_Shift:
2017 case GDK_KEY_ISO_Level3_Latch:
2018 case GDK_KEY_ISO_Level3_Lock:
2019 case GDK_KEY_ISO_Level5_Shift:
2020 case GDK_KEY_ISO_Level5_Latch:
2021 case GDK_KEY_ISO_Level5_Lock:
2022 case GDK_KEY_ISO_Group_Shift:
2023 case GDK_KEY_ISO_Group_Latch:
2024 case GDK_KEY_ISO_Group_Lock:
2025 return TRUE;
2026 #endif
2027 default:
2028 return FALSE;
2029 }
2030 }
2031
2032 /* Copy from gdk */
2033 static GdkEventKey *
2034 _create_gdk_event (IBusIMContext *ibusimcontext,
2035 guint keyval,
2036 guint keycode,
2037 guint state)
2038 {
2039 gunichar c = 0;
2040 gchar buf[8];
2041
2042 GdkEventKey *event = (GdkEventKey *)gdk_event_new ((state & IBUS_RELEASE_MASK) ? GDK_KEY_RELEASE : GDK_KEY_PRESS);
2043
2044 if (ibusimcontext && ibusimcontext->client_window)
2045 event->window = g_object_ref (ibusimcontext->client_window);
2046 else if (_input_window)
2047 event->window = g_object_ref (_input_window);
2048
2049 /* The time is copied the latest value from the previous
2050 * GdkKeyEvent in filter_keypress().
2051 *
2052 * We understand the best way would be to pass the all time value
2053 * to IBus functions process_key_event() and IBus DBus functions
2054 * ProcessKeyEvent() in IM clients and IM engines so that the
2055 * _create_gdk_event() could get the correct time values.
2056 * However it would causes to change many functions and the time value
2057 * would not provide the useful meanings for each IBus engines but just
2058 * pass the original value to ForwardKeyEvent().
2059 * We use the saved value at the moment.
2060 *
2061 * Another idea might be to have the time implementation in X servers
2062 * but some Xorg uses clock_gettime() and others use gettimeofday()
2063 * and the values would be different in each implementation and
2064 * locale/remote X server. So probably that idea would not work. */
2065 if (ibusimcontext) {
2066 event->time = ibusimcontext->time;
2067 } else {
2068 event->time = GDK_CURRENT_TIME;
2069 }
2070
2071 event->send_event = FALSE;
2072 event->state = state;
2073 event->keyval = keyval;
2074 event->string = NULL;
2075 event->length = 0;
2076 event->hardware_keycode = (keycode != 0) ? keycode + 8 : 0;
2077 event->group = 0;
2078 event->is_modifier = _key_is_modifier (keyval);
2079
2080 #ifdef DEPRECATED_GDK_KEYSYMS
2081 if (keyval != GDK_VoidSymbol)
2082 #else
2083 if (keyval != GDK_KEY_VoidSymbol)
2084 #endif
2085 c = gdk_keyval_to_unicode (keyval);
2086
2087 if (c) {
2088 gsize bytes_written;
2089 gint len;
2090
2091 /* Apply the control key - Taken from Xlib
2092 */
2093 if (event->state & GDK_CONTROL_MASK) {
2094 if ((c >= '@' && c < '\177') || c == ' ') c &= 0x1F;
2095 else if (c == '2') {
2096 #if GLIB_CHECK_VERSION (2, 68, 0)
2097 event->string = g_memdup2 ("\0\0", 2);
2098 #else
2099 event->string = g_memdup ("\0\0", 2);
2100 #endif
2101 event->length = 1;
2102 buf[0] = '\0';
2103 goto out;
2104 }
2105 else if (c >= '3' && c <= '7') c -= ('3' - '\033');
2106 else if (c == '8') c = '\177';
2107 else if (c == '/') c = '_' & 0x1F;
2108 }
2109
2110 len = g_unichar_to_utf8 (c, buf);
2111 buf[len] = '\0';
2112
2113 event->string = g_locale_from_utf8 (buf, len,
2114 NULL, &bytes_written,
2115 NULL);
2116 if (event->string)
2117 event->length = bytes_written;
2118 #ifdef DEPRECATED_GDK_KEYSYMS
2119 } else if (keyval == GDK_Escape) {
2120 #else
2121 } else if (keyval == GDK_KEY_Escape) {
2122 #endif
2123 event->length = 1;
2124 event->string = g_strdup ("\033");
2125 }
2126 #ifdef DEPRECATED_GDK_KEYSYMS
2127 else if (keyval == GDK_Return ||
2128 keyval == GDK_KP_Enter) {
2129 #else
2130 else if (keyval == GDK_KEY_Return ||
2131 keyval == GDK_KEY_KP_Enter) {
2132 #endif
2133 event->length = 1;
2134 event->string = g_strdup ("\r");
2135 }
2136
2137 if (!event->string) {
2138 event->length = 0;
2139 event->string = g_strdup ("");
2140 }
2141 out:
2142 return event;
2143 }
2144 #endif
2145
2146 static void
2147 _ibus_context_forward_key_event_cb (IBusInputContext *ibuscontext,
2148 guint keyval,
2149 guint keycode,
2150 guint state,
2151 IBusIMContext *ibusimcontext)
2152 {
2153 IDEBUG ("%s", __FUNCTION__);
2154 #if GTK_CHECK_VERSION (3, 98, 4)
2155 int group = 0;
2156 g_return_if_fail (GTK_IS_IM_CONTEXT (ibusimcontext));
2157 if (keycode != 0) {
2158 keycode += 8; // to GTK keycode
2159 } else if (ibusimcontext->client_window) {
2160 GdkDisplay *display =
2161 gtk_widget_get_display (ibusimcontext->client_window);
2162 GdkKeymapKey *keys = NULL;
2163 gint n_keys = 0;
2164 if (gdk_display_map_keyval (display, keyval, &keys, &n_keys)) {
2165 keycode = keys->keycode;
2166 group = keys->group;
2167 g_free (keys);
2168 } else {
2169 g_warning ("Failed to parse keycode from keyval %x", keyval);
2170 }
2171 }
2172 gtk_im_context_filter_key (
2173 GTK_IM_CONTEXT (ibusimcontext),
2174 (state & IBUS_RELEASE_MASK) ? FALSE : TRUE,
2175 ibusimcontext->surface,
2176 ibusimcontext->device,
2177 ibusimcontext->time,
2178 keycode,
2179 (GdkModifierType)state,
2180 group);
2181 #else
2182 if (keycode == 0 && ibusimcontext->client_window) {
2183 GdkDisplay *display =
2184 gdk_window_get_display (ibusimcontext->client_window);
2185 GdkKeymap *keymap = gdk_keymap_get_for_display (display);
2186 GdkKeymapKey *keys = NULL;
2187 gint n_keys = 0;
2188 if (gdk_keymap_get_entries_for_keyval (keymap, keyval, &keys, &n_keys))
2189 keycode = keys->keycode;
2190 else
2191 g_warning ("Failed to parse keycode from keyval %x", keyval);
2192 /* _create_gdk_event() will add 8 to keycode. */
2193 if (keycode != 0)
2194 keycode -= 8;
2195 }
2196 GdkEventKey *event = _create_gdk_event (ibusimcontext, keyval, keycode, state);
2197 gdk_event_put ((GdkEvent *)event);
2198 gdk_event_free ((GdkEvent *)event);
2199 #endif
2200 }
2201
2202 static void
2203 _ibus_context_delete_surrounding_text_cb (IBusInputContext *ibuscontext,
2204 gint offset_from_cursor,
2205 guint nchars,
2206 IBusIMContext *ibusimcontext)
2207 {
2208 gboolean return_value;
2209 g_signal_emit (ibusimcontext, _signal_delete_surrounding_id, 0, offset_from_cursor, nchars, &return_value);
2210 }
2211
2212 static void
2213 _ibus_context_update_preedit_text_cb (IBusInputContext *ibuscontext,
2214 IBusText *text,
2215 gint cursor_pos,
2216 gboolean visible,
2217 guint mode,
2218 IBusIMContext *ibusimcontext)
2219 {
2220 IDEBUG ("%s", __FUNCTION__);
2221
2222 const gchar *str;
2223 gboolean flag;
2224
2225 if (ibusimcontext->preedit_string) {
2226 g_free (ibusimcontext->preedit_string);
2227 }
2228 if (ibusimcontext->preedit_attrs) {
2229 pango_attr_list_unref (ibusimcontext->preedit_attrs);
2230 ibusimcontext->preedit_attrs = NULL;
2231 }
2232
2233 #if !GTK_CHECK_VERSION (3, 98, 4)
2234 if (!ibusimcontext->use_button_press_event &&
2235 mode == IBUS_ENGINE_PREEDIT_COMMIT &&
2236 _use_sync_mode == 0) {
2237 if (ibusimcontext->client_window) {
2238 _connect_button_press_event (ibusimcontext, TRUE);
2239 }
2240 }
2241 #endif
2242
2243 str = text->text;
2244 ibusimcontext->preedit_string = g_strdup (str);
2245 if (text->attrs) {
2246 guint i;
2247 ibusimcontext->preedit_attrs = pango_attr_list_new ();
2248 for (i = 0; ; i++) {
2249 IBusAttribute *attr = ibus_attr_list_get (text->attrs, i);
2250 if (attr == NULL) {
2251 break;
2252 }
2253
2254 PangoAttribute *pango_attr;
2255 switch (attr->type) {
2256 case IBUS_ATTR_TYPE_UNDERLINE:
2257 pango_attr = pango_attr_underline_new (attr->value);
2258 break;
2259 case IBUS_ATTR_TYPE_FOREGROUND:
2260 pango_attr = pango_attr_foreground_new (
2261 ((attr->value & 0xff0000) >> 8) | 0xff,
2262 ((attr->value & 0x00ff00)) | 0xff,
2263 ((attr->value & 0x0000ff) << 8) | 0xff);
2264 break;
2265 case IBUS_ATTR_TYPE_BACKGROUND:
2266 pango_attr = pango_attr_background_new (
2267 ((attr->value & 0xff0000) >> 8) | 0xff,
2268 ((attr->value & 0x00ff00)) | 0xff,
2269 ((attr->value & 0x0000ff) << 8) | 0xff);
2270 break;
2271 case IBUS_ATTR_TYPE_HINT:
2272 g_warning ("IBUS_ATTR_TYPE_HINT should not happen.");
2273 continue;
2274 default:
2275 continue;
2276 }
2277 pango_attr->start_index = g_utf8_offset_to_pointer (str, attr->start_index) - str;
2278 pango_attr->end_index = g_utf8_offset_to_pointer (str, attr->end_index) - str;
2279 pango_attr_list_insert (ibusimcontext->preedit_attrs, pango_attr);
2280 }
2281 }
2282
2283 ibusimcontext->preedit_cursor_pos = cursor_pos;
2284
2285 flag = ibusimcontext->preedit_visible != visible;
2286 ibusimcontext->preedit_visible = visible;
2287 ibusimcontext->preedit_mode = mode;
2288
2289 if (ibusimcontext->preedit_visible) {
2290 if (flag) {
2291 /* invisible => visible */
2292 g_signal_emit (ibusimcontext, _signal_preedit_start_id, 0);
2293 }
2294 g_signal_emit (ibusimcontext, _signal_preedit_changed_id, 0);
2295 }
2296 else {
2297 if (flag) {
2298 /* visible => invisible */
2299 g_signal_emit (ibusimcontext, _signal_preedit_changed_id, 0);
2300 g_signal_emit (ibusimcontext, _signal_preedit_end_id, 0);
2301 }
2302 else {
2303 /* still invisible */
2304 /* do nothing */
2305 }
2306 }
2307 }
2308
2309 static void
2310 _ibus_context_show_preedit_text_cb (IBusInputContext *ibuscontext,
2311 IBusIMContext *ibusimcontext)
2312 {
2313 IDEBUG ("%s", __FUNCTION__);
2314
2315 if (ibusimcontext->preedit_visible == TRUE)
2316 return;
2317
2318 ibusimcontext->preedit_visible = TRUE;
2319 g_signal_emit (ibusimcontext, _signal_preedit_start_id, 0);
2320 g_signal_emit (ibusimcontext, _signal_preedit_changed_id, 0);
2321
2322 _request_surrounding_text (ibusimcontext);
2323 }
2324
2325 static void
2326 _ibus_context_hide_preedit_text_cb (IBusInputContext *ibuscontext,
2327 IBusIMContext *ibusimcontext)
2328 {
2329 IDEBUG ("%s", __FUNCTION__);
2330
2331 if (ibusimcontext->preedit_visible == FALSE)
2332 return;
2333
2334 ibusimcontext->preedit_visible = FALSE;
2335 g_signal_emit (ibusimcontext, _signal_preedit_changed_id, 0);
2336 g_signal_emit (ibusimcontext, _signal_preedit_end_id, 0);
2337 }
2338
2339 static void
2340 _ibus_warn_no_support_surrounding_text (IBusIMContext *context)
2341 {
2342 /* Engines can disable the surrounding text feature with
2343 * the updated capabilities.
2344 */
2345 if (context->caps & IBUS_CAP_SURROUNDING_TEXT) {
2346 context->caps &= ~IBUS_CAP_SURROUNDING_TEXT;
2347 ibus_input_context_set_capabilities (context->ibuscontext,
2348 context->caps);
2349 }
2350 g_warning ("%s has no capability of surrounding-text feature",
2351 g_get_prgname ());
2352 }
2353
2354 static void
2355 _ibus_context_require_surrounding_text_cb (IBusInputContext *ibuscontext,
2356 IBusIMContext *ibusimcontext)
2357 {
2358 IDEBUG ("%s", __FUNCTION__);
2359 g_assert (ibusimcontext->ibuscontext == ibuscontext);
2360 if (!_request_surrounding_text (ibusimcontext))
2361 _ibus_warn_no_support_surrounding_text (ibusimcontext);
2362 g_signal_handlers_disconnect_by_func (
2363 ibusimcontext->ibuscontext,
2364 G_CALLBACK (_ibus_context_require_surrounding_text_cb),
2365 ibusimcontext);
2366 }
2367
2368 static void
2369 _ibus_context_destroy_cb (IBusInputContext *ibuscontext,
2370 IBusIMContext *ibusimcontext)
2371 {
2372 IDEBUG ("%s", __FUNCTION__);
2373 g_assert (ibusimcontext->ibuscontext == ibuscontext);
2374
2375 g_object_unref (ibusimcontext->ibuscontext);
2376 ibusimcontext->ibuscontext = NULL;
2377
2378 /* clear preedit */
2379 ibusimcontext->preedit_visible = FALSE;
2380 ibusimcontext->preedit_cursor_pos = 0;
2381 g_free (ibusimcontext->preedit_string);
2382 ibusimcontext->preedit_string = NULL;
2383
2384 g_signal_emit (ibusimcontext, _signal_preedit_changed_id, 0);
2385 g_signal_emit (ibusimcontext, _signal_preedit_end_id, 0);
2386 }
2387
2388 static void
2389 _create_input_context_done (IBusBus *bus,
2390 GAsyncResult *res,
2391 IBusIMContext *ibusimcontext)
2392 {
2393 GError *error = NULL;
2394 IBusInputContext *context = ibus_bus_create_input_context_async_finish (
2395 _bus, res, &error);
2396
2397 if (ibusimcontext->cancellable != NULL) {
2398 g_object_unref (ibusimcontext->cancellable);
2399 ibusimcontext->cancellable = NULL;
2400 }
2401
2402 if (context == NULL) {
2403 g_warning ("Create input context failed: %s.", error->message);
2404 g_error_free (error);
2405 } else {
2406 gboolean requested_surrounding_text = FALSE;
2407 ibus_input_context_set_client_commit_preedit (context, TRUE);
2408 if (_use_sync_mode == 1)
2409 ibus_input_context_set_post_process_key_event (context, TRUE);
2410 ibusimcontext->ibuscontext = context;
2411
2412 g_signal_connect (ibusimcontext->ibuscontext,
2413 "commit-text",
2414 G_CALLBACK (_ibus_context_commit_text_cb),
2415 ibusimcontext);
2416 g_signal_connect (ibusimcontext->ibuscontext,
2417 "forward-key-event",
2418 G_CALLBACK (_ibus_context_forward_key_event_cb),
2419 ibusimcontext);
2420 g_signal_connect (ibusimcontext->ibuscontext,
2421 "delete-surrounding-text",
2422 G_CALLBACK (_ibus_context_delete_surrounding_text_cb),
2423 ibusimcontext);
2424 g_signal_connect (ibusimcontext->ibuscontext,
2425 "update-preedit-text-with-mode",
2426 G_CALLBACK (_ibus_context_update_preedit_text_cb),
2427 ibusimcontext);
2428 g_signal_connect (ibusimcontext->ibuscontext,
2429 "show-preedit-text",
2430 G_CALLBACK (_ibus_context_show_preedit_text_cb),
2431 ibusimcontext);
2432 g_signal_connect (ibusimcontext->ibuscontext,
2433 "hide-preedit-text",
2434 G_CALLBACK (_ibus_context_hide_preedit_text_cb),
2435 ibusimcontext);
2436 g_signal_connect (ibusimcontext->ibuscontext, "destroy",
2437 G_CALLBACK (_ibus_context_destroy_cb),
2438 ibusimcontext);
2439
2440 ibus_input_context_set_capabilities (ibusimcontext->ibuscontext,
2441 ibusimcontext->caps);
2442
2443 if (ibusimcontext->rgba && ibusimcontext->rgba->selected_fg &&
2444 ibusimcontext->rgba->selected_bg) {
2445 ibus_input_context_set_selected_color (
2446 ibusimcontext->ibuscontext,
2447 ibusimcontext->rgba->selected_fg,
2448 ibusimcontext->rgba->selected_bg);
2449 }
2450 if (ibusimcontext->has_focus) {
2451 /* The time order is _create_input_context() ->
2452 * ibus_im_context_notify() -> ibus_im_context_focus_in() ->
2453 * _create_input_context_done()
2454 * so _set_content_type() is called at the beginning here
2455 * because ibusimcontext->ibuscontext == NULL before. */
2456 _set_content_type (ibusimcontext);
2457
2458 ibus_input_context_focus_in (ibusimcontext->ibuscontext);
2459 _set_cursor_location_internal (ibusimcontext);
2460 if (ibus_input_context_needs_surrounding_text (
2461 ibusimcontext->ibuscontext)) {
2462 if (!_request_surrounding_text (ibusimcontext))
2463 _ibus_warn_no_support_surrounding_text (ibusimcontext);
2464 requested_surrounding_text = TRUE;
2465 }
2466 }
2467 if (!requested_surrounding_text) {
2468 g_signal_connect (
2469 ibusimcontext->ibuscontext,
2470 "require-surrounding-text",
2471 G_CALLBACK (_ibus_context_require_surrounding_text_cb),
2472 ibusimcontext);
2473 }
2474
2475 if (!g_queue_is_empty (ibusimcontext->events_queue)) {
2476 #if GTK_CHECK_VERSION (3, 98, 4)
2477 GdkEvent *event;
2478 #else
2479 GdkEventKey *event;
2480 #endif
2481 while ((event = g_queue_pop_head (ibusimcontext->events_queue))) {
2482 _process_key_event (context, event, ibusimcontext);
2483 #if GTK_CHECK_VERSION (3, 98, 4)
2484 gdk_event_unref (event);
2485 #else
2486 gdk_event_free ((GdkEvent *)event);
2487 #endif
2488 }
2489 }
2490 }
2491
2492 g_object_unref (ibusimcontext);
2493 }
2494
2495 static void
2496 _create_input_context (IBusIMContext *ibusimcontext)
2497 {
2498 gchar *prgname;
2499 gchar *client_name;
2500 IDEBUG ("%s", __FUNCTION__);
2501
2502 g_assert (ibusimcontext->ibuscontext == NULL);
2503
2504 g_return_if_fail (ibusimcontext->cancellable == NULL);
2505
2506 prgname = g_strdup (g_get_prgname());
2507 ibusimcontext->cancellable = g_cancellable_new ();
2508
2509 if (!prgname)
2510 prgname = g_strdup_printf ("(%d)", getpid ());
2511 client_name = g_strdup_printf ("%s:%s",
2512 #if GTK_CHECK_VERSION (3, 98, 4)
2513 "gtk4-im",
2514 #elif GTK_CHECK_VERSION (2, 91, 0)
2515 "gtk3-im",
2516 #else
2517 "gtk-im",
2518 #endif
2519 prgname);
2520 g_free (prgname);
2521 ibus_bus_create_input_context_async (_bus,
2522 client_name, -1,
2523 ibusimcontext->cancellable,
2524 (GAsyncReadyCallback)_create_input_context_done,
2525 g_object_ref (ibusimcontext));
2526 g_free (client_name);
2527 }
2528
2529 /* Callback functions for slave context */
2530 static void
2531 _slave_commit_cb (GtkIMContext *slave,
2532 gchar *string,
2533 IBusIMContext *ibusimcontext)
2534 {
2535 g_signal_emit (ibusimcontext, _signal_commit_id, 0, string);
2536 }
2537
2538 static void
2539 _slave_preedit_changed_cb (GtkIMContext *slave,
2540 IBusIMContext *ibusimcontext)
2541 {
2542 if (ibusimcontext->ibuscontext) {
2543 return;
2544 }
2545
2546 g_signal_emit (ibusimcontext, _signal_preedit_changed_id, 0);
2547 }
2548
2549 static void
2550 _slave_preedit_start_cb (GtkIMContext *slave,
2551 IBusIMContext *ibusimcontext)
2552 {
2553 if (ibusimcontext->ibuscontext) {
2554 return;
2555 }
2556
2557 g_signal_emit (ibusimcontext, _signal_preedit_start_id, 0);
2558 }
2559
2560 static void
2561 _slave_preedit_end_cb (GtkIMContext *slave,
2562 IBusIMContext *ibusimcontext)
2563 {
2564 if (ibusimcontext->ibuscontext) {
2565 return;
2566 }
2567 g_signal_emit (ibusimcontext, _signal_preedit_end_id, 0);
2568 }
2569
2570 static gboolean
2571 _slave_retrieve_surrounding_cb (GtkIMContext *slave,
2572 IBusIMContext *ibusimcontext)
2573 {
2574 gboolean return_value;
2575
2576 if (ibusimcontext->ibuscontext) {
2577 return FALSE;
2578 }
2579 g_signal_emit (ibusimcontext, _signal_retrieve_surrounding_id, 0,
2580 &return_value);
2581 return return_value;
2582 }
2583
2584 static gboolean
2585 _slave_delete_surrounding_cb (GtkIMContext *slave,
2586 gint offset_from_cursor,
2587 guint nchars,
2588 IBusIMContext *ibusimcontext)
2589 {
2590 gboolean return_value;
2591
2592 if (ibusimcontext->ibuscontext) {
2593 return FALSE;
2594 }
2595 g_signal_emit (ibusimcontext, _signal_delete_surrounding_id, 0, offset_from_cursor, nchars, &return_value);
2596 return return_value;
2597 }
2598
2599 #ifdef OS_CHROMEOS
2600 static void
2601 _ibus_fake_context_destroy_cb (IBusInputContext *ibuscontext,
2602 gpointer user_data)
2603 {
2604 /* The fack IC may be destroyed when the connection is lost.
2605 * Should release it. */
2606 g_assert (ibuscontext == _fake_context);
2607 g_object_unref (_fake_context);
2608 _fake_context = NULL;
2609 }
2610
2611 static GCancellable *_fake_cancellable = NULL;
2612
2613 static void
2614 _create_fake_input_context_done (IBusBus *bus,
2615 GAsyncResult *res,
2616 IBusIMContext *ibusimcontext)
2617 {
2618 GError *error = NULL;
2619 IBusInputContext *context = ibus_bus_create_input_context_async_finish (
2620 _bus, res, &error);
2621
2622 if (_fake_cancellable != NULL) {
2623 g_object_unref (_fake_cancellable);
2624 _fake_cancellable = NULL;
2625 }
2626
2627 if (context == NULL) {
2628 g_warning ("Create fake input context failed: %s.", error->message);
2629 g_error_free (error);
2630 return;
2631 }
2632
2633 _fake_context = context;
2634
2635 g_signal_connect (_fake_context, "forward-key-event",
2636 G_CALLBACK (_ibus_context_forward_key_event_cb),
2637 NULL);
2638 g_signal_connect (_fake_context, "destroy",
2639 G_CALLBACK (_ibus_fake_context_destroy_cb),
2640 NULL);
2641
2642 guint32 caps = IBUS_CAP_PREEDIT_TEXT | IBUS_CAP_FOCUS
2643 | IBUS_CAP_SURROUNDING_TEXT;
2644 ibus_input_context_set_capabilities (_fake_context, caps);
2645
2646 /* focus in/out the fake context */
2647 if (_focus_im_context == NULL)
2648 ibus_input_context_focus_in (_fake_context);
2649 else
2650 ibus_input_context_focus_out (_fake_context);
2651 }
2652
2653 static void
2654 _create_fake_input_context (void)
2655 {
2656 g_return_if_fail (_fake_context == NULL);
2657
2658 /* Global engine is always enabled in Chrome OS,
2659 * so create fake IC, and set focus if no other IC has focus.
2660 */
2661
2662 if (_fake_cancellable != NULL) {
2663 g_cancellable_cancel (_fake_cancellable);
2664 g_object_unref (_fake_cancellable);
2665 _fake_cancellable = NULL;
2666 }
2667
2668 _fake_cancellable = g_cancellable_new ();
2669
2670 ibus_bus_create_input_context_async (_bus,
2671 "fake-gtk-im", -1,
2672 _fake_cancellable,
2673 (GAsyncReadyCallback)_create_fake_input_context_done,
2674 NULL);
2675
2676 }
2677 #else
2678 static void
2679 _create_fake_input_context (void)
2680 {
2681 /* For Linux desktop, do not use fake IC. */
2682 }
2683 #endif