gf-keyboard-manager.c 16.6 KB
Newer Older
1
2
3
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
 * Copyright (C) 2015 Alberts Muktupāvels
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <glib.h>
#include <gdk/gdkx.h>
#include <libgnome-desktop/gnome-languages.h>
#include <X11/XKBlib.h>
#include <X11/extensions/XKBrules.h>

#include "gf-keyboard-manager.h"

#define DEFAULT_XKB_RULES_FILE "evdev"
#define DEFAULT_XKB_MODEL "pc105+inet"

#define DEFAULT_LOCALE "en_US"
#define DEFAULT_LAYOUT "us"
#define DEFAULT_VARIANT ""

/* The XKB protocol doesn't allow for more than 4 layouts in a keymap. */
#define MAX_LAYOUTS_PER_GROUP 4

typedef struct _LayoutInfo LayoutInfo;
struct _LayoutInfo
{
  gchar       *id;
  gchar       *layout;
  gchar       *variant;

  LayoutInfo **group;
  guint        index;
};

struct _GfKeyboardManager
{
  GObject       parent;

  Display      *xdisplay;

  gint          xkb_event_base;
  gint          xkb_error_base;

  GnomeXkbInfo *xkb_info;

  LayoutInfo   *locale;

  GHashTable   *layout_infos;
  LayoutInfo   *current;

  gchar        *layouts;
  gchar        *variants;
  gchar        *options;
68
69

  guint         locked_group;
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
};

G_DEFINE_TYPE (GfKeyboardManager, gf_keyboard_manager, G_TYPE_OBJECT)

static LayoutInfo *
layout_info_new (const gchar *id,
                 const gchar *layout,
                 const gchar *variant)
{
  LayoutInfo *layout_info;

  layout_info = g_new0 (LayoutInfo, 1);

  layout_info->id = g_strdup (id);
  layout_info->layout = g_strdup (layout);
  layout_info->variant = g_strdup (variant);

  layout_info->group = NULL;

  return layout_info;
}

static void
layout_info_free (gpointer data)
{
  LayoutInfo *layout_info;

  layout_info = (LayoutInfo *) data;

  g_free (layout_info->id);
  g_free (layout_info->layout);
  g_free (layout_info->variant);

  if (layout_info->index == 0)
    g_free (layout_info->group);

  g_free (layout_info);
}

static void
upload_xkb_description (Display              *xdisplay,
                        const gchar          *rules_file_path,
                        XkbComponentNamesRec *component_names,
                        XkbRF_VarDefsRec     *var_defs)
{
  guint want_mask;
  guint need_mask;
  XkbDescRec *xkb_desc;
  gchar *rules_file;

  want_mask = XkbGBN_AllComponentsMask;
  need_mask = XkbGBN_AllComponentsMask & (~XkbGBN_GeometryMask);
  xkb_desc = XkbGetKeyboardByName (xdisplay, XkbUseCoreKbd, component_names,
                                   want_mask, need_mask, True);

  if (xkb_desc == NULL)
    {
      g_warning ("Couldn't upload new XKB keyboard description");
      return;
    }

  XkbFreeKeyboard (xkb_desc, 0, True);

  rules_file = g_path_get_basename (rules_file_path);

  if (!XkbRF_SetNamesProp (xdisplay, rules_file, var_defs))
    g_warning ("Couldn't update the XKB root window property");

  g_free (rules_file);
}

static void
get_xkbrf_var_defs (Display           *xdisplay,
                    const char        *layouts,
                    const char        *variants,
                    const char        *options,
                    gchar            **rules,
                    XkbRF_VarDefsRec **var_defs)
{
  gchar *tmp;
  XkbRF_VarDefsRec *defs;

  defs = g_new0 (XkbRF_VarDefsRec, 1);

  tmp = NULL;
  if (!XkbRF_GetNamesProp (xdisplay, &tmp, defs) || !tmp)
    {
      tmp = g_strdup (DEFAULT_XKB_RULES_FILE);

      defs->model = g_strdup (DEFAULT_XKB_MODEL);
      defs->layout = NULL;
      defs->variant = NULL;
      defs->options = NULL;
    }

  g_free (defs->layout);
  defs->layout = g_strdup (layouts);

  g_free (defs->variant);
  defs->variant = g_strdup (variants);

  g_free (defs->options);
  defs->options = g_strdup (options);

  if (tmp[0] == '/')
    *rules = g_strdup (tmp);
  else
    *rules = g_build_filename (XKB_BASE, "rules", tmp, NULL);
  g_free (tmp);

  *var_defs = defs;
}

static void
xkbrf_var_defs_free (XkbRF_VarDefsRec *var_defs)
{
  g_free (var_defs->model);
  g_free (var_defs->layout);
  g_free (var_defs->variant);
  g_free (var_defs->options);

  g_free (var_defs);
}

static void
xkb_component_names_free (XkbComponentNamesRec *xkb_comp_names)
{
  g_free (xkb_comp_names->keymap);
  g_free (xkb_comp_names->keycodes);
  g_free (xkb_comp_names->types);
  g_free (xkb_comp_names->compat);
  g_free (xkb_comp_names->symbols);
  g_free (xkb_comp_names->geometry);

  g_free (xkb_comp_names);
}

static void
set_keymap (GfKeyboardManager *manager)
{
  gchar *rules_file_path;
  XkbRF_VarDefsRec *xkb_var_defs;
  XkbRF_RulesRec *xkb_rules;

214
  if (manager->xkb_event_base == -1)
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
    return;

  if (!manager->layouts || !manager->variants || !manager->options)
    return;

  xkb_var_defs = NULL;
  get_xkbrf_var_defs (manager->xdisplay,
                      manager->layouts, manager->variants, manager->options,
                      &rules_file_path, &xkb_var_defs);

  xkb_rules = XkbRF_Load (rules_file_path, NULL, True, True);

  if (xkb_rules != NULL)
    {
      XkbComponentNamesRec *xkb_comp_names;

      xkb_comp_names = g_new0 (XkbComponentNamesRec, 1);
      XkbRF_GetComponents (xkb_rules, xkb_var_defs, xkb_comp_names);

      upload_xkb_description (manager->xdisplay, rules_file_path,
                              xkb_comp_names, xkb_var_defs);

      xkb_component_names_free (xkb_comp_names);
      XkbRF_Free (xkb_rules, True);
    }
  else
    g_warning ("Couldn't load XKB rules");

  g_free (rules_file_path);
  xkbrf_var_defs_free (xkb_var_defs);
}

static gchar *
get_layouts_string (GfKeyboardManager  *manager,
                    LayoutInfo        **group)
{
  gchar *layouts;
  gint i;
  gchar *tmp;

  if (group[0] == NULL)
    return g_strdup ("");

  layouts = g_strdup (group[0]->layout);
  for (i = 1; i < (MAX_LAYOUTS_PER_GROUP - 1); i++)
    {
      if (group[i] == NULL)
        continue;

      tmp = g_strdup_printf ("%s,%s", layouts, group[i]->layout);
      g_free (layouts);

      layouts = tmp;
    }

  if (manager->locale != NULL)
    {
      tmp = g_strdup_printf ("%s,%s", layouts, manager->locale->layout);
      g_free (layouts);

      layouts = tmp;
    }

  return layouts;
}

static gchar *
get_variants_string (GfKeyboardManager  *manager,
                     LayoutInfo        **group)
{
  gchar *variants;
  gint i;
  gchar *tmp;

  if (group[0] == NULL)
    return g_strdup ("");

  variants = g_strdup (group[0]->variant);
  for (i = 1; i < (MAX_LAYOUTS_PER_GROUP - 1); i++)
    {
      if (group[i] == NULL)
        continue;

      tmp = g_strdup_printf ("%s,%s", variants, group[i]->variant);
      g_free (variants);

      variants = tmp;
    }

  if (manager->locale != NULL)
    {
      tmp = g_strdup_printf ("%s,%s", variants, manager->locale->variant);
      g_free (variants);

      variants = tmp;
    }

  return variants;
}

static void
apply_layout_group (GfKeyboardManager  *manager,
                    LayoutInfo        **group)
{
  g_free (manager->layouts);
  manager->layouts = get_layouts_string (manager, group);

  g_free (manager->variants);
  manager->variants = get_variants_string (manager, group);

  set_keymap (manager);
}

static void
apply_layout_index (GfKeyboardManager *manager,
                    guint              index)
{
332
  if (manager->xkb_event_base == -1)
333
334
    return;

335
  manager->locked_group = index;
336
337
338
339
  XkbLockGroup (manager->xdisplay, XkbUseCoreKbd, index);
}

static void
340
341
342
device_added_cb (GdkSeat   *seat,
                 GdkDevice *device,
                 gpointer   user_data)
343
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
373
374
375
376
377
378
379
380
{
  GfKeyboardManager *manager;

  manager = GF_KEYBOARD_MANAGER (user_data);

  if (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD)
    return;

  set_keymap (manager);
}

static void
get_locale_layout_info (GfKeyboardManager *manager)
{
  const gchar *locale;
  const gchar *type;
  const gchar *id;
  const gchar *layout;
  const gchar *variant;

  locale = g_get_language_names ()[0];
  if (g_strrstr (locale, "_") == NULL)
    locale = DEFAULT_LOCALE;

  if (!gnome_get_input_source_from_locale (locale, &type, &id))
    gnome_get_input_source_from_locale (DEFAULT_LOCALE, &type, &id);

  if (gnome_xkb_info_get_layout_info (manager->xkb_info, id, NULL, NULL,
                                      &layout, &variant))
    {
      manager->locale = layout_info_new (id, layout, variant);
    }
  else
    {
      manager->locale = layout_info_new (id, DEFAULT_LAYOUT, DEFAULT_VARIANT);
    }
}

381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
static GdkFilterReturn
filter_func (GdkXEvent *xevent,
             GdkEvent  *event,
             gpointer   user_data)
{
  GfKeyboardManager *manager;
  XEvent *ev;
  XkbEvent *xkb_ev;

  manager = GF_KEYBOARD_MANAGER (user_data);
  ev = (XEvent *) xevent;

  if (ev->type != manager->xkb_event_base)
    return GDK_FILTER_CONTINUE;

  xkb_ev = (XkbEvent *) ev;

  if (xkb_ev->any.xkb_type == XkbStateNotify &&
      xkb_ev->state.changed & XkbGroupLockMask)
    {
      if ((gint) manager->locked_group != xkb_ev->state.locked_group)
        apply_layout_index (manager, manager->locked_group);
    }

  return GDK_FILTER_CONTINUE;
}

408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
static void
gf_keyboard_manager_dispose (GObject *object)
{
  GfKeyboardManager *manager;

  manager = GF_KEYBOARD_MANAGER (object);

  g_clear_object (&manager->xkb_info);

  G_OBJECT_CLASS (gf_keyboard_manager_parent_class)->dispose (object);
}

static void
gf_keyboard_manager_finalize (GObject *object)
{
  GfKeyboardManager *manager;

  manager = GF_KEYBOARD_MANAGER (object);

427
428
  gdk_window_remove_filter (NULL, filter_func, manager);

429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
  if (manager->locale != 0)
    {
      layout_info_free (manager->locale);
      manager->locale = NULL;
    }

  if (manager->layout_infos != NULL)
    {
      g_hash_table_destroy (manager->layout_infos);
      manager->layout_infos = NULL;
    }

  g_free (manager->layouts);
  g_free (manager->variants);
  g_free (manager->options);

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

static void
gf_keyboard_manager_class_init (GfKeyboardManagerClass *manager_class)
{
  GObjectClass *object_class;

  object_class = G_OBJECT_CLASS (manager_class);

  object_class->dispose = gf_keyboard_manager_dispose;
  object_class->finalize = gf_keyboard_manager_finalize;
}

static void
gf_keyboard_manager_init (GfKeyboardManager *manager)
{
  GdkDisplay *display;
463
  GdkSeat *seat;
464
465
466
467
468
  gint xkb_opcode;
  gint xkb_major;
  gint xkb_minor;

  display = gdk_display_get_default ();
469
  seat = gdk_display_get_default_seat (display);
470
471
472
473
474

  manager->xdisplay = gdk_x11_display_get_xdisplay (display);
  manager->xkb_info = gnome_xkb_info_new ();
  manager->options = g_strdup ("");

475
476
  xkb_major = XkbMajorVersion;
  xkb_minor = XkbMinorVersion;
477
478
479
480
481

  if (!XkbQueryExtension (manager->xdisplay, &xkb_opcode,
                          &manager->xkb_event_base, &manager->xkb_error_base,
                          &xkb_major, &xkb_minor))
    {
482
483
      manager->xkb_event_base = -1;

484
485
      g_warning ("X server doesn't have the XKB extension, version %d.%d or "
                 "newer", XkbMajorVersion, XkbMinorVersion);
486
487
    }

488
  g_signal_connect_object (seat, "device-added",
489
490
491
492
                           G_CALLBACK (device_added_cb), manager,
                           G_CONNECT_AFTER);

  get_locale_layout_info (manager);
493
494

  gdk_window_add_filter (NULL, filter_func, manager);
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
}

/**
 * gf_keyboard_manager_new:
 *
 * Creates a new #GfKeyboardManager.
 *
 * Returns: (transfer full): a newly created #GfKeyboardManager.
 */
GfKeyboardManager *
gf_keyboard_manager_new (void)
{
  return g_object_new (GF_TYPE_KEYBOARD_MANAGER, NULL);
}

/**
 * gf_keyboard_manager_get_xkb_info:
 * @manager: a #GfKeyboardManager
 *
 * Returns: (transfer full):
 */
GnomeXkbInfo *
gf_keyboard_manager_get_xkb_info (GfKeyboardManager *manager)
{
  return g_object_ref (manager->xkb_info);
}

/**
 * gf_keyboard_manager_set_xkb_options:
 * @manager: a #GfKeyboardManager
 * @options: a %NULL-terminated array of xkb options
 */
void
gf_keyboard_manager_set_xkb_options (GfKeyboardManager  *manager,
                                     gchar             **options)
{
  g_free (manager->options);
  manager->options = g_strjoinv (",", options);
}

/**
 * gf_keyboard_manager_set_user_layouts:
 * @manager: a #GfKeyboardManager
 * @ids: a %NULL-terminated array of input source ids
 */
void
gf_keyboard_manager_set_user_layouts (GfKeyboardManager  *manager,
                                      gchar             **ids)
{
  gint i;
545
  gint j;
546
547
548
549
550
551
552
553
554
555
556
557
  const gchar *layout;
  const gchar *variant;
  LayoutInfo **group;

  manager->current = NULL;

  if (manager->layout_infos != NULL)
    g_hash_table_destroy (manager->layout_infos);

  manager->layout_infos = g_hash_table_new_full (g_str_hash, g_str_equal,
                                                 g_free, layout_info_free);

558
  for (i = 0, j = 0; ids[i] != NULL; i++)
559
560
561
562
563
    {
      if (gnome_xkb_info_get_layout_info (manager->xkb_info, ids[i],
                                          NULL, NULL, &layout, &variant))
        {
          LayoutInfo *info;
564
          guint index;
565

566
567
          if (g_hash_table_contains (manager->layout_infos, ids[i]))
            continue;
568

569
          info = layout_info_new (ids[i], layout, variant);
570

571
572
573
574
575
576
577
578
          /*
           * We need to leave one slot on each group free so that we
           * can add a layout containing the symbols for the language
           * used in UI strings to ensure that toolkits can handle
           * mnemonics like Alt+Ф even if the user is actually typing
           * in a different layout.
           */
          index = j % (MAX_LAYOUTS_PER_GROUP - 1);
579

580
581
          if (index == 0)
            group = g_new0 (LayoutInfo *, (MAX_LAYOUTS_PER_GROUP - 1));
582

583
          group[index] = info;
584

585
586
          info->group = group;
          info->index = index;
587

588
          g_hash_table_insert (manager->layout_infos, g_strdup (ids[i]), info);
589

590
591
          ++j;
        }
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
    }
}

/**
 * gf_keyboard_manager_apply:
 * @manager: a #GfKeyboardManager
 * @id: the xkb_layout + xkb_variant or xkb_layout if a XKB variant isn't
 *     needed
 */
void
gf_keyboard_manager_apply (GfKeyboardManager *manager,
                           const gchar       *id)
{
  LayoutInfo *info;

  info = (LayoutInfo *) g_hash_table_lookup (manager->layout_infos, id);

  if (info == NULL)
    return;

  if (manager->current != NULL && manager->current->group == info->group)
    {
      if (manager->current->index != info->index)
        apply_layout_index (manager, info->index);
    }
  else
    {
      apply_layout_group (manager, info->group);
      apply_layout_index (manager, info->index);
    }

  manager->current = info;
}

/**
 * gf_keyboard_manager_reapply:
 * @manager: a #GfKeyboardManager
 */
void
gf_keyboard_manager_reapply (GfKeyboardManager *manager)
{
  if (manager->current == NULL)
    return;

  apply_layout_group (manager, manager->current->group);
  apply_layout_index (manager, manager->current->index);
}

/**
 * gf_keyboard_manager_grab:
 * @manager: a #GfKeyboardManager
 * @timestamp: the timestamp of the user interaction (typically a button or
 *     key press event) which triggered this call
645
646
 *
 * Returns: %TRUE if grab was successful, %FALSE otherwise
647
 */
648
gboolean
649
650
651
652
653
654
gf_keyboard_manager_grab (GfKeyboardManager *manager,
                          guint32            timestamp)
{
  GdkDisplay *display;
  GdkScreen *screen;
  GdkWindow *root;
655
656
657
  Window xroot;
  gint status;
  gint error;
658
659
660
661

  display = gdk_display_get_default ();
  screen = gdk_display_get_default_screen (display);
  root = gdk_screen_get_root_window (screen);
662
  xroot = gdk_x11_window_get_xid (root);
663

664
  gdk_x11_display_error_trap_push (display);
665
666
667
  status = XGrabKeyboard (manager->xdisplay, xroot, False,
                          GrabModeAsync, GrabModeSync,
                          timestamp);
668
  error = gdk_x11_display_error_trap_pop (display);
669

670
671
  if (error != 0)
    return FALSE;
672

673
674
  if (status != 0)
    return FALSE;
675

676
  return TRUE;
677
678
679
680
681
682
683
684
685
686
687
688
}

/**
 * gf_keyboard_manager_ungrab:
 * @manager: a #GfKeyboardManager
 * @timestamp: the timestamp of the user interaction (typically a button or
 *     key press event) which triggered this call
 */
void
gf_keyboard_manager_ungrab (GfKeyboardManager *manager,
                            guint32            timestamp)
{
689
  XUngrabKeyboard (manager->xdisplay, timestamp);
690
}
691
692
693
694
695
696
697
698
699
700

/**
 * gf_keyboard_manager_get_default_layout:
 * @manager: a #GfKeyboardManager
 */
const gchar *
gf_keyboard_manager_get_default_layout (GfKeyboardManager *manager)
{
  return DEFAULT_LAYOUT;
}