imwayland.c 24.8 KB
Newer Older
1
2
/* GTK - The GIMP Toolkit
 * Copyright (C) 2017 Red Hat, Inc.
3
 * Copyright (C) 2018 Purism SPC
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <string.h>
#include <wayland-client-protocol.h>

#include <gtk/gtk.h>
#include "gtk/gtkintl.h"
#include "gtk/gtkimmodule.h"

#include "gdk/wayland/gdkwayland.h"
29
#include "text-input-unstable-v3-client-protocol.h"
30
31
32
33
34
35
36
37
38

typedef struct _GtkIMContextWaylandGlobal GtkIMContextWaylandGlobal;
typedef struct _GtkIMContextWayland GtkIMContextWayland;
typedef struct _GtkIMContextWaylandClass GtkIMContextWaylandClass;

struct _GtkIMContextWaylandGlobal
{
  struct wl_display *display;
  struct wl_registry *registry;
39
  uint32_t text_input_manager_wl_id;
40
41
  struct zwp_text_input_manager_v3 *text_input_manager;
  struct zwp_text_input_v3 *text_input;
42
43

  GtkIMContext *current;
44

45
46
47
48
  /* The input-method.enter event may happen before or after GTK focus-in,
   * so the context may not exist at the time. Same for leave and focus-out. */
  gboolean focused;

49
  guint serial;
50
51
52
53
54
55
56
};

struct _GtkIMContextWaylandClass
{
  GtkIMContextSimpleClass parent_class;
};

57
58
59
60
61
62
63
64
65
66
67
struct preedit {
  gchar *text;
  gint cursor_begin;
  gint cursor_end;
};

struct surrounding_delete {
  guint before_length;
  guint after_length;
};

68
69
70
71
72
73
74
75
76
77
78
79
80
struct _GtkIMContextWayland
{
  GtkIMContextSimple parent_instance;
  GdkWindow *window;
  GtkWidget *widget;

  GtkGesture *gesture;
  gdouble press_x;
  gdouble press_y;

  struct {
    gchar *text;
    gint cursor_idx;
81
    gint anchor_idx;
82
83
  } surrounding;

84
85
86
87
88
89
90
91
  enum zwp_text_input_v3_change_cause surrounding_change;

  struct surrounding_delete pending_surrounding_delete;

  struct preedit current_preedit;
  struct preedit pending_preedit;

  gchar *pending_commit;
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122

  cairo_rectangle_int_t cursor_rect;
  guint use_preedit : 1;
};

GType type_wayland = 0;
static GObjectClass *parent_class;
static GtkIMContextWaylandGlobal *global = NULL;

static const GtkIMContextInfo imwayland_info =
{
  "wayland",      /* ID */
  NC_("input method menu", "Wayland"),      /* Human readable name */
  GETTEXT_PACKAGE, /* Translation domain */
  GTK_LOCALEDIR,   /* Dir for bindtextdomain (not strictly needed for "gtk+") */
  "",              /* Languages for which this module is the default */
};

static const GtkIMContextInfo *info_list[] =
{
  &imwayland_info,
};

#define GTK_IM_CONTEXT_WAYLAND(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), type_wayland, GtkIMContextWayland))

#ifndef INCLUDE_IM_wayland
#define MODULE_ENTRY(type,function) G_MODULE_EXPORT type im_module_ ## function
#else
#define MODULE_ENTRY(type, function) type _gtk_immodule_wayland_ ## function
#endif

123
124
125
126
127
128
129

static void
enable (GtkIMContextWayland *context_wayland);

static void
disable (GtkIMContextWayland *context_wayland);

130
static void
131
notify_external_change (GtkIMContextWayland *context)
132
{
133
  gboolean result;
134

135
136
137
  if (!global->current)
    return;

138
139
  context->surrounding_change = ZWP_TEXT_INPUT_V3_CHANGE_CAUSE_OTHER;
  g_signal_emit_by_name (global->current, "retrieve-surrounding", &result);
140
141
142
143
}

static void
text_input_enter (void                     *data,
144
                  struct zwp_text_input_v3 *text_input,
145
                  struct wl_surface        *surface)
146
147
148
149
150
151
{
  global->focused = TRUE;

  if (global->current)
    enable (GTK_IM_CONTEXT_WAYLAND (global->current));
}
152
153
154

static void
text_input_leave (void                     *data,
155
                  struct zwp_text_input_v3 *text_input,
156
                  struct wl_surface        *surface)
157
158
159
160
161
162
{
  global->focused = FALSE;

  if (global->current)
    disable (GTK_IM_CONTEXT_WAYLAND (global->current));
}
163
164
165

static void
text_input_preedit (void                     *data,
166
                    struct zwp_text_input_v3 *text_input,
167
                    const char               *text,
168
169
                    gint                      cursor_begin,
                    gint                      cursor_end)
170
171
{
  GtkIMContextWayland *context;
172
  GtkIMContextWaylandGlobal *global = data;
173
174
175
176
177

  if (!global->current)
    return;

  context = GTK_IM_CONTEXT_WAYLAND (global->current);
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
      
  g_free (context->pending_preedit.text);
  context->pending_preedit.text = g_strdup (text);
  context->pending_preedit.cursor_begin = cursor_begin;
  context->pending_preedit.cursor_end = cursor_end;
}

static void
text_input_preedit_apply (GtkIMContextWaylandGlobal *global)
{
  GtkIMContextWayland *context;
  gboolean state_change;
  struct preedit defaults = {0};

  if (!global->current)
193
194
    return;

195
196
197
198
  context = GTK_IM_CONTEXT_WAYLAND (global->current);

  state_change = ((context->pending_preedit.text == NULL)
                 != (context->current_preedit.text == NULL));
199

200
  if (state_change && !context->current_preedit.text)
201
202
    g_signal_emit_by_name (context, "preedit-start");

203
204
205
  g_free (context->current_preedit.text);
  context->current_preedit = context->pending_preedit;
  context->pending_preedit = defaults;
206
207
208

  g_signal_emit_by_name (context, "preedit-changed");

209
  if (state_change && !context->current_preedit.text)
210
211
212
213
214
    g_signal_emit_by_name (context, "preedit-end");
}

static void
text_input_commit (void                     *data,
215
                   struct zwp_text_input_v3 *text_input,
216
217
218
                   const char               *text)
{
  GtkIMContextWaylandGlobal *global = data;
219
220
221
222
223
224
225
226
227
228
  GtkIMContextWayland *context;

  if (!global->current)
      return;
  
  context = GTK_IM_CONTEXT_WAYLAND (global->current);

  g_free (context->pending_commit);
  context->pending_commit = g_strdup (text);
}
229

230
231
232
233
234
235
236
237
238
static void
text_input_commit_apply (GtkIMContextWaylandGlobal *global, gboolean valid)
{
  GtkIMContextWayland *context;
  context = GTK_IM_CONTEXT_WAYLAND (global->current);
  if (context->pending_commit && valid)
    g_signal_emit_by_name (global->current, "commit", context->pending_commit);
  g_free (context->pending_commit);
  context->pending_commit = NULL;
239
240
241
242
}

static void
text_input_delete_surrounding_text (void                     *data,
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
                                    struct zwp_text_input_v3 *text_input,
                                    uint32_t                  before_length,
                                    uint32_t                  after_length)
{
  GtkIMContextWaylandGlobal *global = data;
  GtkIMContextWayland *context;

  if (!global->current)
      return;
  
  context = GTK_IM_CONTEXT_WAYLAND (global->current);

  context->pending_surrounding_delete.before_length = before_length;
  context->pending_surrounding_delete.after_length = after_length;
}

static void
text_input_delete_surrounding_text_apply (GtkIMContextWaylandGlobal *global,
  gboolean valid)
{
  GtkIMContextWayland *context;
264
  gboolean retval;
265
266
  gint len;
  struct surrounding_delete defaults = {0};
267

268
  context = GTK_IM_CONTEXT_WAYLAND (global->current);
269

270
271
272
273
274
  len = context->pending_surrounding_delete.after_length
      + context->pending_surrounding_delete.before_length;
  if (len > 0 && valid)
    g_signal_emit_by_name (global->current, "delete-surrounding",
                           -context->pending_surrounding_delete.before_length,
275
                           len, &retval);
276
277
278
279
280
281
282
  context->pending_surrounding_delete = defaults;
}

static void
text_input_done (void                     *data,
                 struct zwp_text_input_v3 *text_input,
                 uint32_t                  serial)
283
284
{
  GtkIMContextWaylandGlobal *global = data;
285
286
287
288
289
  gboolean result;
  gboolean valid;
  
  if (!global->current)
    return;
290

291
  valid = serial == global->serial;
292
293
294
295
  text_input_delete_surrounding_text_apply(global, valid);
  text_input_commit_apply(global, valid);
  g_signal_emit_by_name (global->current, "retrieve-surrounding", &result);
  text_input_preedit_apply(global);
296
297
}

298
static const struct zwp_text_input_v3_listener text_input_listener = {
299
300
301
302
  text_input_enter,
  text_input_leave,
  text_input_preedit,
  text_input_commit,
303
304
  text_input_delete_surrounding_text,
  text_input_done,
305
306
307
308
309
310
311
312
313
314
315
316
};

static void
registry_handle_global (void               *data,
                        struct wl_registry *registry,
                        uint32_t            id,
                        const char         *interface,
                        uint32_t            version)
{
  GtkIMContextWaylandGlobal *global = data;
  GdkSeat *seat = gdk_display_get_default_seat (gdk_display_get_default ());

317
  if (strcmp (interface, "zwp_text_input_manager_v3") == 0)
318
    {
319
      global->text_input_manager_wl_id = id;
320
      global->text_input_manager =
321
        wl_registry_bind (global->registry, global->text_input_manager_wl_id,
322
                          &zwp_text_input_manager_v3_interface, 1);
323
      global->text_input =
324
325
        zwp_text_input_manager_v3_get_text_input (global->text_input_manager,
                                          gdk_wayland_seat_get_wl_seat (seat));
326
      global->serial = 0;
327
328
      zwp_text_input_v3_add_listener (global->text_input,
                                      &text_input_listener, global);
329
330
331
332
333
334
335
336
337
338
    }
}

static void
registry_handle_global_remove (void               *data,
                               struct wl_registry *registry,
                               uint32_t            id)
{
  GtkIMContextWaylandGlobal *global = data;

339
340
  if (id != global->text_input_manager_wl_id)
    return;
341

342
343
  g_clear_pointer(&global->text_input, zwp_text_input_v3_destroy);
  g_clear_pointer(&global->text_input_manager, zwp_text_input_manager_v3_destroy);
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
}

static const struct wl_registry_listener registry_listener = {
    registry_handle_global,
    registry_handle_global_remove
};

static void
gtk_im_context_wayland_global_init (GdkDisplay *display)
{
  g_return_if_fail (global == NULL);

  global = g_new0 (GtkIMContextWaylandGlobal, 1);
  global->display = gdk_wayland_display_get_wl_display (display);
  global->registry = wl_display_get_registry (global->display);

  wl_registry_add_listener (global->registry, &registry_listener, global);
}

static void
notify_surrounding_text (GtkIMContextWayland *context)
{
  if (!global || !global->text_input)
    return;
  if (global->current != GTK_IM_CONTEXT (context))
    return;
  if (!context->surrounding.text)
    return;

373
374
375
376
377
378
  zwp_text_input_v3_set_surrounding_text (global->text_input,
                                          context->surrounding.text,
                                          context->surrounding.cursor_idx,
                                          context->surrounding.anchor_idx);
  zwp_text_input_v3_set_text_change_cause (global->text_input,
                                           context->surrounding_change);
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
}

static void
notify_cursor_location (GtkIMContextWayland *context)
{
  cairo_rectangle_int_t rect;

  if (!global || !global->text_input)
    return;
  if (global->current != GTK_IM_CONTEXT (context))
    return;
  if (!context->window)
    return;

  rect = context->cursor_rect;
  gdk_window_get_root_coords (context->window, rect.x, rect.y,
                              &rect.x, &rect.y);

397
398
399
  zwp_text_input_v3_set_cursor_rectangle (global->text_input,
                                          rect.x, rect.y,
                                          rect.width, rect.height);
400
401
402
403
404
405
406
407
408
}

static uint32_t
translate_hints (GtkInputHints   input_hints,
                 GtkInputPurpose purpose)
{
  uint32_t hints = 0;

  if (input_hints & GTK_INPUT_HINT_SPELLCHECK)
409
    hints |= ZWP_TEXT_INPUT_V3_CONTENT_HINT_SPELLCHECK;
410
  if (input_hints & GTK_INPUT_HINT_WORD_COMPLETION)
411
    hints |= ZWP_TEXT_INPUT_V3_CONTENT_HINT_COMPLETION;
412
  if (input_hints & GTK_INPUT_HINT_LOWERCASE)
413
    hints |= ZWP_TEXT_INPUT_V3_CONTENT_HINT_LOWERCASE;
414
  if (input_hints & GTK_INPUT_HINT_UPPERCASE_CHARS)
415
    hints |= ZWP_TEXT_INPUT_V3_CONTENT_HINT_UPPERCASE;
416
  if (input_hints & GTK_INPUT_HINT_UPPERCASE_WORDS)
417
    hints |= ZWP_TEXT_INPUT_V3_CONTENT_HINT_TITLECASE;
418
  if (input_hints & GTK_INPUT_HINT_UPPERCASE_SENTENCES)
419
    hints |= ZWP_TEXT_INPUT_V3_CONTENT_HINT_AUTO_CAPITALIZATION;
420
421
422
423

  if (purpose == GTK_INPUT_PURPOSE_PIN ||
      purpose == GTK_INPUT_PURPOSE_PASSWORD)
    {
424
425
      hints |= (ZWP_TEXT_INPUT_V3_CONTENT_HINT_HIDDEN_TEXT |
                ZWP_TEXT_INPUT_V3_CONTENT_HINT_SENSITIVE_DATA);
426
427
428
429
430
431
432
433
434
435
436
    }

  return hints;
}

static uint32_t
translate_purpose (GtkInputPurpose purpose)
{
  switch (purpose)
    {
    case GTK_INPUT_PURPOSE_FREE_FORM:
437
      return ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_NORMAL;
438
    case GTK_INPUT_PURPOSE_ALPHA:
439
      return ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_ALPHA;
440
    case GTK_INPUT_PURPOSE_DIGITS:
441
      return ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_DIGITS;
442
    case GTK_INPUT_PURPOSE_NUMBER:
443
      return ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_NUMBER;
444
    case GTK_INPUT_PURPOSE_PHONE:
445
      return ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_PHONE;
446
    case GTK_INPUT_PURPOSE_URL:
447
      return ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_URL;
448
    case GTK_INPUT_PURPOSE_EMAIL:
449
      return ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_EMAIL;
450
    case GTK_INPUT_PURPOSE_NAME:
451
      return ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_NAME;
452
    case GTK_INPUT_PURPOSE_PASSWORD:
453
      return ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_PASSWORD;
454
    case GTK_INPUT_PURPOSE_PIN:
455
      return ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_PIN;
456
457
    }

458
  return ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_NORMAL;
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
}

static void
notify_content_type (GtkIMContextWayland *context)
{
  GtkInputHints hints;
  GtkInputPurpose purpose;

  if (global->current != GTK_IM_CONTEXT (context))
    return;

  g_object_get (context,
                "input-hints", &hints,
                "input-purpose", &purpose,
                NULL);

475
476
477
  zwp_text_input_v3_set_content_type (global->text_input,
                                      translate_hints (hints, purpose),
                                      translate_purpose (purpose));
478
479
480
481
482
483
484
}

static void
commit_state (GtkIMContextWayland *context)
{
  if (global->current != GTK_IM_CONTEXT (context))
    return;
485
  global->serial++;
486
487
  zwp_text_input_v3_commit (global->text_input);
  context->surrounding_change = ZWP_TEXT_INPUT_V3_CHANGE_CAUSE_INPUT_METHOD;
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
}

static void
gtk_im_context_wayland_finalize (GObject *object)
{
  GtkIMContextWayland *context = GTK_IM_CONTEXT_WAYLAND (object);

  g_clear_object (&context->window);
  g_clear_object (&context->gesture);

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

static void
pressed_cb (GtkGestureMultiPress *gesture,
            gint                  n_press,
            gdouble               x,
            gdouble               y,
            GtkIMContextWayland  *context)
{
  if (n_press == 1)
    {
      context->press_x = x;
      context->press_y = y;
    }
}

static void
released_cb (GtkGestureMultiPress *gesture,
             gint                  n_press,
             gdouble               x,
             gdouble               y,
             GtkIMContextWayland  *context)
{
  GtkInputHints hints;
523
  gboolean result;
524

525
526
527
  if (!global->current)
    return;

528
529
  g_object_get (context, "input-hints", &hints, NULL);

530
531
  if (global->focused &&
      n_press == 1 &&
532
533
534
535
536
537
      (hints & GTK_INPUT_HINT_INHIBIT_OSK) == 0 &&
      !gtk_drag_check_threshold (context->widget,
                                 context->press_x,
                                 context->press_y,
                                 x, y))
    {
538
      zwp_text_input_v3_enable (global->text_input);
539
540
      g_signal_emit_by_name (global->current, "retrieve-surrounding", &result);
      commit_state (context);
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
    }
}

static void
gtk_im_context_wayland_set_client_window (GtkIMContext *context,
                                          GdkWindow    *window)
{
  GtkIMContextWayland *context_wayland = GTK_IM_CONTEXT_WAYLAND (context);
  GtkWidget *widget = NULL;

  if (window == context_wayland->window)
    return;

  if (window)
    gdk_window_get_user_data (window, (gpointer*) &widget);

  if (context_wayland->widget && context_wayland->widget != widget)
    g_clear_object (&context_wayland->gesture);

  g_set_object (&context_wayland->window, window);

  if (context_wayland->widget != widget)
    {
      context_wayland->widget = widget;

      if (widget)
        {
          GtkGesture *gesture;

          gesture = gtk_gesture_multi_press_new (widget);
          gtk_event_controller_set_propagation_phase (GTK_EVENT_CONTROLLER (gesture),
                                                      GTK_PHASE_CAPTURE);
          g_signal_connect (gesture, "pressed",
                            G_CALLBACK (pressed_cb), context);
          g_signal_connect (gesture, "released",
                            G_CALLBACK (released_cb), context);
          context_wayland->gesture = gesture;
        }
    }
}

static void
gtk_im_context_wayland_get_preedit_string (GtkIMContext   *context,
                                           gchar         **str,
                                           PangoAttrList **attrs,
                                           gint           *cursor_pos)
{
  GtkIMContextWayland *context_wayland = GTK_IM_CONTEXT_WAYLAND (context);
  gchar *preedit_str;

591
592
593
  if (attrs)
    *attrs = NULL;

594
595
596
  GTK_IM_CONTEXT_CLASS (parent_class)->get_preedit_string (context,
                                                           str, attrs,
                                                           cursor_pos);
597
598

  /* If the parent implementation returns a len>0 string, go with it */
599
600
601
602
603
604
605
  if (str && *str)
    {
      if (**str)
        return;

      g_free (*str);
    }
606
607

  preedit_str =
608
    context_wayland->current_preedit.text ? context_wayland->current_preedit.text : "";
609
610
611
612

  if (str)
    *str = g_strdup (preedit_str);
  if (cursor_pos)
613
    *cursor_pos = context_wayland->current_preedit.cursor_begin;
614
615
616

  if (attrs)
    {
617
618
      if (!*attrs)
        *attrs = pango_attr_list_new ();
619
620
      pango_attr_list_insert (*attrs,
                              pango_attr_underline_new (PANGO_UNDERLINE_SINGLE));
621
622
623
624
625
626
627
628
629
      if (context_wayland->current_preedit.cursor_begin
          != context_wayland->current_preedit.cursor_end)
        {
          /* FIXME: Oh noes, how to highlight while taking into account user preferences? */
          PangoAttribute *cursor = pango_attr_weight_new (PANGO_WEIGHT_BOLD);
          cursor->start_index = context_wayland->current_preedit.cursor_begin;
          cursor->end_index = context_wayland->current_preedit.cursor_end;
          pango_attr_list_insert (*attrs, cursor);
        }
630
631
632
633
634
635
636
637
638
639
640
641
    }
}

static gboolean
gtk_im_context_wayland_filter_keypress (GtkIMContext *context,
                                        GdkEventKey  *key)
{
  /* This is done by the compositor */
  return GTK_IM_CONTEXT_CLASS (parent_class)->filter_keypress (context, key);
}

static void
642
enable (GtkIMContextWayland *context_wayland)
643
{
644
  gboolean result;
645
  zwp_text_input_v3_enable (global->text_input);
646
  g_signal_emit_by_name (global->current, "retrieve-surrounding", &result);
647
648
649
650
651
652
  notify_content_type (context_wayland);
  notify_cursor_location (context_wayland);
  commit_state (context_wayland);
}

static void
653
disable (GtkIMContextWayland *context_wayland)
654
{
655
656
  zwp_text_input_v3_disable (global->text_input);
  commit_state (context_wayland);
657
658

  /* after disable, incoming state changes won't take effect anyway */
659
660
661
662
663
  if (context_wayland->current_preedit.text)
    {
      text_input_preedit (global, global->text_input, NULL, 0, 0);
      text_input_preedit_apply (global);
    }
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
}

static void
gtk_im_context_wayland_focus_in (GtkIMContext *context)
{
  GtkIMContextWayland *context_wayland = GTK_IM_CONTEXT_WAYLAND (context);

  if (global->current == context)
    return;
  if (!global->text_input)
    return;

  global->current = context;

  if (global->focused)
    enable (context_wayland);
}

static void
gtk_im_context_wayland_focus_out (GtkIMContext *context)
{
  GtkIMContextWayland *context_wayland = GTK_IM_CONTEXT_WAYLAND (context);

  if (global->current != context)
    return;

  if (global->focused)
    disable (context_wayland);
692

693
694
695
696
697
698
  global->current = NULL;
}

static void
gtk_im_context_wayland_reset (GtkIMContext *context)
{
699
  notify_external_change (GTK_IM_CONTEXT_WAYLAND (context));
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738

  GTK_IM_CONTEXT_CLASS (parent_class)->reset (context);
}

static void
gtk_im_context_wayland_set_cursor_location (GtkIMContext *context,
                                            GdkRectangle *rect)
{
  GtkIMContextWayland *context_wayland;

  context_wayland = GTK_IM_CONTEXT_WAYLAND (context);

  context_wayland->cursor_rect = *rect;
  notify_cursor_location (context_wayland);
  commit_state (context_wayland);
}

static void
gtk_im_context_wayland_set_use_preedit (GtkIMContext *context,
                                        gboolean      use_preedit)
{
  GtkIMContextWayland *context_wayland = GTK_IM_CONTEXT_WAYLAND (context);

  context_wayland->use_preedit = !!use_preedit;
}

static void
gtk_im_context_wayland_set_surrounding (GtkIMContext *context,
                                        const gchar  *text,
                                        gint          len,
                                        gint          cursor_index)
{
  GtkIMContextWayland *context_wayland;

  context_wayland = GTK_IM_CONTEXT_WAYLAND (context);

  g_free (context_wayland->surrounding.text);
  context_wayland->surrounding.text = g_strdup (text);
  context_wayland->surrounding.cursor_idx = cursor_index;
739
740
  /* Anchor is not exposed via the set_surrounding interface, emulating. */
  context_wayland->surrounding.anchor_idx = cursor_index;
741
742

  notify_surrounding_text (context_wayland);
743
744
745
746
747
  /* State changes coming from reset don't have any other opportunity to get
   * committed. */
  if (context_wayland->surrounding_change !=
      ZWP_TEXT_INPUT_V3_CHANGE_CAUSE_INPUT_METHOD)
    commit_state (context_wayland);
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
}

static gboolean
gtk_im_context_wayland_get_surrounding (GtkIMContext  *context,
                                        gchar        **text,
                                        gint          *cursor_index)
{
  GtkIMContextWayland *context_wayland;

  context_wayland = GTK_IM_CONTEXT_WAYLAND (context);

  if (!context_wayland->surrounding.text)
    return FALSE;

  *text = context_wayland->surrounding.text;
  *cursor_index = context_wayland->surrounding.cursor_idx;
  return TRUE;
}

static void
gtk_im_context_wayland_class_init (GtkIMContextWaylandClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GtkIMContextClass *im_context_class = GTK_IM_CONTEXT_CLASS (klass);

  object_class->finalize = gtk_im_context_wayland_finalize;

  im_context_class->set_client_window = gtk_im_context_wayland_set_client_window;
  im_context_class->get_preedit_string = gtk_im_context_wayland_get_preedit_string;
  im_context_class->filter_keypress = gtk_im_context_wayland_filter_keypress;
  im_context_class->focus_in = gtk_im_context_wayland_focus_in;
  im_context_class->focus_out = gtk_im_context_wayland_focus_out;
  im_context_class->reset = gtk_im_context_wayland_reset;
  im_context_class->set_cursor_location = gtk_im_context_wayland_set_cursor_location;
  im_context_class->set_use_preedit = gtk_im_context_wayland_set_use_preedit;
  im_context_class->set_surrounding = gtk_im_context_wayland_set_surrounding;
  im_context_class->get_surrounding = gtk_im_context_wayland_get_surrounding;

  parent_class = g_type_class_peek_parent (klass);
}

static void
on_content_type_changed (GtkIMContextWayland *context)
{
  notify_content_type (context);
  commit_state (context);
}

static void
gtk_im_context_wayland_init (GtkIMContextWayland *context)
{
  context->use_preedit = TRUE;
  g_signal_connect_swapped (context, "notify::input-purpose",
                            G_CALLBACK (on_content_type_changed), context);
  g_signal_connect_swapped (context, "notify::input-hints",
                            G_CALLBACK (on_content_type_changed), context);
}

static void
gtk_im_context_wayland_register_type (GTypeModule *module)
{
  const GTypeInfo object_info = {
    sizeof (GtkIMContextWaylandClass),
    NULL, NULL,
    (GClassInitFunc) gtk_im_context_wayland_class_init,
    NULL, NULL,
    sizeof (GtkIMContextWayland),
    0,
    (GInstanceInitFunc) gtk_im_context_wayland_init,
  };

  type_wayland = g_type_module_register_type (module,
                                              GTK_TYPE_IM_CONTEXT_SIMPLE,
                                              "GtkIMContextWayland",
                                              &object_info, 0);
}

MODULE_ENTRY (void, init) (GTypeModule * module)
{
  gtk_im_context_wayland_register_type (module);
  gtk_im_context_wayland_global_init (gdk_display_get_default ());
}

MODULE_ENTRY (void, exit) (void)
{
}

MODULE_ENTRY (void, list) (const GtkIMContextInfo *** contexts, int *n_contexts)
{
  *contexts = info_list;
  *n_contexts = G_N_ELEMENTS (info_list);
}

MODULE_ENTRY (GtkIMContext *, create) (const gchar * context_id)
{
  if (strcmp (context_id, "wayland") == 0)
    return g_object_new (type_wayland, NULL);
  else
    return NULL;
}