-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathkeybindings.c
More file actions
5125 lines (4392 loc) · 163 KB
/
keybindings.c
File metadata and controls
5125 lines (4392 loc) · 163 KB
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
68
69
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
214
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
332
333
334
335
336
337
338
339
340
341
342
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
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
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
591
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
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
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
692
693
694
695
696
697
698
699
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
739
740
741
742
743
744
745
746
747
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
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/* Mutter Keybindings */
/*
* Copyright (C) 2001 Havoc Pennington
* Copyright (C) 2002 Red Hat Inc.
* Copyright (C) 2003 Rob Adams
* Copyright (C) 2004-2006 Elijah Newren
*
* 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 2 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/>.
*/
/**
* SECTION:keybindings
* @Title: MetaKeybinding
* @Short_Description: Key bindings
*/
#include "config.h"
#include "backends/meta-backend-private.h"
#include "backends/meta-logical-monitor.h"
#include "backends/meta-monitor-manager-private.h"
#include "backends/x11/meta-backend-x11.h"
#include "compositor/compositor-private.h"
#include "core/edge-resistance.h"
#include "core/frame.h"
#include "core/keybindings-private.h"
#include "core/meta-accel-parse.h"
#include "core/meta-workspace-manager-private.h"
#include "core/workspace-private.h"
#include "meta/compositor.h"
#include "meta/meta-x11-errors.h"
#include "meta/prefs.h"
#include "x11/meta-x11-display-private.h"
#include "x11/window-x11.h"
#ifdef HAVE_NATIVE_BACKEND
#include "backends/native/meta-backend-native.h"
#endif
#ifdef __linux__
#include <linux/input.h>
#elif !defined KEY_GRAVE
#define KEY_GRAVE 0x29 /* assume the use of xf86-input-keyboard */
#endif
#define SCHEMA_COMMON_KEYBINDINGS "org.cinnamon.desktop.keybindings.wm"
#define SCHEMA_MUTTER_KEYBINDINGS "org.cinnamon.muffin.keybindings"
#define SCHEMA_MUTTER_WAYLAND_KEYBINDINGS "org.cinnamon.muffin.wayland.keybindings"
#define META_KEY_BINDING_PRIMARY_LAYOUT 0
#define META_KEY_BINDING_SECONDARY_LAYOUT 1
/* Only for special modifier keys */
#define IGNORED_MODIFIERS (CLUTTER_LOCK_MASK | \
CLUTTER_MOD2_MASK | \
CLUTTER_BUTTON1_MASK | \
CLUTTER_BUTTON2_MASK | \
CLUTTER_BUTTON3_MASK | \
CLUTTER_BUTTON4_MASK | \
CLUTTER_BUTTON5_MASK)
static gboolean modifier_key_only_pressed = FALSE;
static gboolean add_builtin_keybinding (MetaDisplay *display,
const char *name,
GSettings *settings,
MetaKeyBindingFlags flags,
MetaKeyBindingAction action,
MetaKeyHandlerFunc handler,
int handler_arg);
static void
resolved_key_combo_reset (MetaResolvedKeyCombo *resolved_combo)
{
g_free (resolved_combo->keycodes);
resolved_combo->len = 0;
resolved_combo->keycodes = NULL;
}
static void
resolved_key_combo_copy (MetaResolvedKeyCombo *from,
MetaResolvedKeyCombo *to)
{
to->len = from->len;
to->keycodes = g_memdup (from->keycodes,
from->len * sizeof (xkb_keycode_t));
}
static gboolean
resolved_key_combo_has_keycode (MetaResolvedKeyCombo *resolved_combo,
int keycode)
{
int i;
for (i = 0; i < resolved_combo->len; i++)
if ((int) resolved_combo->keycodes[i] == keycode)
return TRUE;
return FALSE;
}
static gboolean
resolved_key_combo_intersect (MetaResolvedKeyCombo *a,
MetaResolvedKeyCombo *b)
{
int i;
for (i = 0; i < a->len; i++)
if (resolved_key_combo_has_keycode (b, a->keycodes[i]))
return TRUE;
return FALSE;
}
static void
meta_key_binding_free (MetaKeyBinding *binding)
{
resolved_key_combo_reset (&binding->resolved_combo);
g_slice_free (MetaKeyBinding, binding);
}
static MetaKeyBinding *
meta_key_binding_copy (MetaKeyBinding *binding)
{
MetaKeyBinding *clone = g_slice_dup (MetaKeyBinding, binding);
resolved_key_combo_copy (&binding->resolved_combo,
&clone->resolved_combo);
return clone;
}
G_DEFINE_BOXED_TYPE(MetaKeyBinding,
meta_key_binding,
meta_key_binding_copy,
meta_key_binding_free)
const char *
meta_key_binding_get_name (MetaKeyBinding *binding)
{
return binding->name;
}
MetaVirtualModifier
meta_key_binding_get_modifiers (MetaKeyBinding *binding)
{
return binding->combo.modifiers;
}
gboolean
meta_key_binding_is_reversed (MetaKeyBinding *binding)
{
return (binding->handler->flags & META_KEY_BINDING_IS_REVERSED) != 0;
}
guint
meta_key_binding_get_mask (MetaKeyBinding *binding)
{
return binding->resolved_combo.mask;
}
gboolean
meta_key_binding_is_builtin (MetaKeyBinding *binding)
{
return binding->handler->flags & META_KEY_BINDING_BUILTIN;
}
/* These can't be bound to anything, but they are used to handle
* various other events. TODO: Possibly we should include them as event
* handler functions and have some kind of flag to say they're unbindable.
*/
static gboolean process_mouse_move_resize_grab (MetaDisplay *display,
MetaWindow *window,
ClutterKeyEvent *event);
static gboolean process_keyboard_move_grab (MetaDisplay *display,
MetaWindow *window,
ClutterKeyEvent *event);
static gboolean process_keyboard_resize_grab (MetaDisplay *display,
MetaWindow *window,
ClutterKeyEvent *event);
static GHashTable *key_handlers;
static GHashTable *external_grabs;
#define HANDLER(name) g_hash_table_lookup (key_handlers, (name))
static void
key_handler_free (MetaKeyHandler *handler)
{
g_free (handler->name);
if (handler->user_data_free_func && handler->user_data)
handler->user_data_free_func (handler->user_data);
g_free (handler);
}
typedef struct _MetaKeyGrab MetaKeyGrab;
struct _MetaKeyGrab {
char *name;
guint action;
MetaKeyCombo combo;
gint flags;
};
static void
meta_key_grab_free (MetaKeyGrab *grab)
{
g_free (grab->name);
g_free (grab);
}
static guint32
key_combo_key (MetaResolvedKeyCombo *resolved_combo,
int i)
{
/* On X, keycodes are only 8 bits while libxkbcommon supports 32 bit
keycodes, but since we're using the same XKB keymaps that X uses,
we won't find keycodes bigger than 8 bits in practice. The bits
that mutter cares about in the modifier mask are also all in the
lower 8 bits both on X and clutter key events. This means that we
can use a 32 bit integer to safely concatenate both keycode and
mask and thus making it easy to use them as an index in a
GHashTable. */
guint32 key = resolved_combo->keycodes[i] & 0xffff;
return (key << 16) | (resolved_combo->mask & 0xffff);
}
static void
reload_modmap (MetaKeyBindingManager *keys)
{
struct xkb_keymap *keymap = meta_backend_get_keymap (keys->backend);
struct xkb_state *scratch_state;
xkb_mod_mask_t scroll_lock_mask;
xkb_mod_mask_t dummy_mask;
/* Modifiers to find. */
struct {
const char *name;
xkb_mod_mask_t *mask_p;
xkb_mod_mask_t *virtual_mask_p;
} mods[] = {
{ "ScrollLock", &scroll_lock_mask, &dummy_mask },
{ "Meta", &keys->meta_mask, &keys->virtual_meta_mask },
{ "Hyper", &keys->hyper_mask, &keys->virtual_hyper_mask },
{ "Super", &keys->super_mask, &keys->virtual_super_mask },
};
scratch_state = xkb_state_new (keymap);
gsize i;
for (i = 0; i < G_N_ELEMENTS (mods); i++)
{
xkb_mod_mask_t *mask_p = mods[i].mask_p;
xkb_mod_mask_t *virtual_mask_p = mods[i].virtual_mask_p;
xkb_mod_index_t idx = xkb_keymap_mod_get_index (keymap, mods[i].name);
if (idx != XKB_MOD_INVALID)
{
xkb_mod_mask_t vmodmask = (1 << idx);
xkb_state_update_mask (scratch_state, vmodmask, 0, 0, 0, 0, 0);
*mask_p = xkb_state_serialize_mods (scratch_state, XKB_STATE_MODS_DEPRESSED) & ~vmodmask;
*virtual_mask_p = vmodmask;
}
else
{
*mask_p = 0;
*virtual_mask_p = 0;
}
}
xkb_state_unref (scratch_state);
keys->ignored_modifier_mask = (scroll_lock_mask | Mod2Mask | LockMask);
meta_topic (META_DEBUG_KEYBINDINGS,
"Ignoring modmask 0x%x scroll lock 0x%x hyper 0x%x super 0x%x meta 0x%x\n",
keys->ignored_modifier_mask,
scroll_lock_mask,
keys->hyper_mask,
keys->super_mask,
keys->meta_mask);
}
static gboolean
is_keycode_for_keysym (struct xkb_keymap *keymap,
xkb_layout_index_t layout,
xkb_level_index_t level,
xkb_keycode_t keycode,
xkb_keysym_t keysym)
{
const xkb_keysym_t *syms;
int num_syms, k;
num_syms = xkb_keymap_key_get_syms_by_level (keymap, keycode, layout, level, &syms);
for (k = 0; k < num_syms; k++)
{
if (syms[k] == keysym)
return TRUE;
}
return FALSE;
}
typedef struct
{
GArray *keycodes;
xkb_keysym_t keysym;
xkb_layout_index_t layout;
xkb_level_index_t level;
} FindKeysymData;
static void
get_keycodes_for_keysym_iter (struct xkb_keymap *keymap,
xkb_keycode_t keycode,
void *data)
{
FindKeysymData *search_data = data;
GArray *keycodes = search_data->keycodes;
xkb_keysym_t keysym = search_data->keysym;
xkb_layout_index_t layout = search_data->layout;
xkb_level_index_t level = search_data->level;
if (is_keycode_for_keysym (keymap, layout, level, keycode, keysym))
{
guint i;
gboolean missing = TRUE;
/* duplicate keycode detection */
for (i = 0; i < keycodes->len; i++)
if (g_array_index (keycodes, xkb_keysym_t, i) == keycode)
{
missing = FALSE;
break;
}
if (missing)
g_array_append_val (keycodes, keycode);
}
}
static void
add_keysym_keycodes_from_layout (int keysym,
MetaKeyBindingKeyboardLayout *layout,
GArray *keycodes)
{
xkb_level_index_t layout_level;
for (layout_level = 0;
layout_level < layout->n_levels && keycodes->len == 0;
layout_level++)
{
FindKeysymData search_data = (FindKeysymData) {
.keycodes = keycodes,
.keysym = keysym,
.layout = layout->index,
.level = layout_level
};
xkb_keymap_key_for_each (layout->keymap,
get_keycodes_for_keysym_iter,
&search_data);
}
}
/* Original code from gdk_x11_keymap_get_entries_for_keyval() in
* gdkkeys-x11.c */
static void
get_keycodes_for_keysym (MetaKeyBindingManager *keys,
int keysym,
MetaResolvedKeyCombo *resolved_combo)
{
unsigned int i;
GArray *keycodes;
int keycode;
keycodes = g_array_new (FALSE, FALSE, sizeof (xkb_keysym_t));
/* Special-case: Fake mutter keysym */
if (keysym == META_KEY_ABOVE_TAB)
{
keycode = KEY_GRAVE + 8;
g_array_append_val (keycodes, keycode);
goto out;
}
for (i = 0; i < G_N_ELEMENTS (keys->active_layouts); i++)
{
MetaKeyBindingKeyboardLayout *layout = &keys->active_layouts[i];
if (!layout->keymap)
continue;
add_keysym_keycodes_from_layout (keysym, layout, keycodes);
}
out:
resolved_combo->len = keycodes->len;
resolved_combo->keycodes =
(xkb_keycode_t *) g_array_free (keycodes,
keycodes->len == 0 ? TRUE : FALSE);
}
typedef struct _CalculateLayoutLevelsState
{
struct xkb_keymap *keymap;
xkb_layout_index_t layout_index;
xkb_level_index_t out_n_levels;
} CalculateLayoutLevelState;
static void
calculate_n_layout_levels_iter (struct xkb_keymap *keymap,
xkb_keycode_t keycode,
void *data)
{
CalculateLayoutLevelState *state = data;
xkb_level_index_t n_levels;
n_levels = xkb_keymap_num_levels_for_key (keymap,
keycode,
state->layout_index);
state->out_n_levels = MAX (n_levels, state->out_n_levels);
}
static xkb_level_index_t
calculate_n_layout_levels (struct xkb_keymap *keymap,
xkb_layout_index_t layout_index)
{
CalculateLayoutLevelState state = {
.keymap = keymap,
.layout_index = layout_index,
.out_n_levels = 0
};
xkb_keymap_key_for_each (keymap, calculate_n_layout_levels_iter, &state);
return state.out_n_levels;
}
static void
reload_iso_next_group_combos (MetaKeyBindingManager *keys)
{
const char *iso_next_group_option;
int i;
for (i = 0; i < keys->n_iso_next_group_combos; i++)
resolved_key_combo_reset (&keys->iso_next_group_combo[i]);
keys->n_iso_next_group_combos = 0;
iso_next_group_option = meta_prefs_get_iso_next_group_option ();
if (iso_next_group_option == NULL)
return;
get_keycodes_for_keysym (keys, XKB_KEY_ISO_Next_Group, keys->iso_next_group_combo);
if (keys->iso_next_group_combo[0].len == 0)
return;
keys->n_iso_next_group_combos = 1;
if (g_str_equal (iso_next_group_option, "toggle") ||
g_str_equal (iso_next_group_option, "lalt_toggle") ||
g_str_equal (iso_next_group_option, "lwin_toggle") ||
g_str_equal (iso_next_group_option, "rwin_toggle") ||
g_str_equal (iso_next_group_option, "lshift_toggle") ||
g_str_equal (iso_next_group_option, "rshift_toggle") ||
g_str_equal (iso_next_group_option, "lctrl_toggle") ||
g_str_equal (iso_next_group_option, "rctrl_toggle") ||
g_str_equal (iso_next_group_option, "sclk_toggle") ||
g_str_equal (iso_next_group_option, "menu_toggle") ||
g_str_equal (iso_next_group_option, "caps_toggle"))
{
keys->iso_next_group_combo[0].mask = 0;
}
else if (g_str_equal (iso_next_group_option, "shift_caps_toggle") ||
g_str_equal (iso_next_group_option, "shifts_toggle"))
{
keys->iso_next_group_combo[0].mask = ShiftMask;
}
else if (g_str_equal (iso_next_group_option, "alt_caps_toggle") ||
g_str_equal (iso_next_group_option, "alt_space_toggle"))
{
keys->iso_next_group_combo[0].mask = Mod1Mask;
}
else if (g_str_equal (iso_next_group_option, "ctrl_shift_toggle") ||
g_str_equal (iso_next_group_option, "lctrl_lshift_toggle") ||
g_str_equal (iso_next_group_option, "rctrl_rshift_toggle"))
{
resolved_key_combo_copy (&keys->iso_next_group_combo[0],
&keys->iso_next_group_combo[1]);
keys->iso_next_group_combo[0].mask = ShiftMask;
keys->iso_next_group_combo[1].mask = ControlMask;
keys->n_iso_next_group_combos = 2;
}
else if (g_str_equal (iso_next_group_option, "ctrl_alt_toggle"))
{
resolved_key_combo_copy (&keys->iso_next_group_combo[0],
&keys->iso_next_group_combo[1]);
keys->iso_next_group_combo[0].mask = Mod1Mask;
keys->iso_next_group_combo[1].mask = ControlMask;
keys->n_iso_next_group_combos = 2;
}
else if (g_str_equal (iso_next_group_option, "alt_shift_toggle") ||
g_str_equal (iso_next_group_option, "lalt_lshift_toggle"))
{
resolved_key_combo_copy (&keys->iso_next_group_combo[0],
&keys->iso_next_group_combo[1]);
keys->iso_next_group_combo[0].mask = Mod1Mask;
keys->iso_next_group_combo[1].mask = ShiftMask;
keys->n_iso_next_group_combos = 2;
}
else
{
resolved_key_combo_reset (keys->iso_next_group_combo);
keys->n_iso_next_group_combos = 0;
}
}
static void
devirtualize_modifiers (MetaKeyBindingManager *keys,
MetaVirtualModifier modifiers,
unsigned int *mask)
{
*mask = 0;
if (modifiers & META_VIRTUAL_SHIFT_MASK)
*mask |= ShiftMask;
if (modifiers & META_VIRTUAL_CONTROL_MASK)
*mask |= ControlMask;
if (modifiers & META_VIRTUAL_ALT_MASK)
*mask |= Mod1Mask;
if (modifiers & META_VIRTUAL_META_MASK)
*mask |= keys->meta_mask;
if (modifiers & META_VIRTUAL_HYPER_MASK)
*mask |= keys->hyper_mask;
if (modifiers & META_VIRTUAL_SUPER_MASK)
*mask |= keys->super_mask;
if (modifiers & META_VIRTUAL_MOD2_MASK)
*mask |= Mod2Mask;
if (modifiers & META_VIRTUAL_MOD3_MASK)
*mask |= Mod3Mask;
if (modifiers & META_VIRTUAL_MOD4_MASK)
*mask |= Mod4Mask;
if (modifiers & META_VIRTUAL_MOD5_MASK)
*mask |= Mod5Mask;
}
static void
index_binding (MetaKeyBindingManager *keys,
MetaKeyBinding *binding)
{
int i;
for (i = 0; i < binding->resolved_combo.len; i++)
{
MetaKeyBinding *existing;
guint32 index_key;
index_key = key_combo_key (&binding->resolved_combo, i);
existing = g_hash_table_lookup (keys->key_bindings_index,
GINT_TO_POINTER (index_key));
if (existing != NULL)
{
/* Overwrite already indexed keycodes only for the first
* keycode, i.e. we give those primary keycodes precedence
* over non-first ones. */
if (i > 0)
continue;
meta_warning ("%s - Overwriting existing binding of keysym %x"
" with keysym %x (keycode %x).\n",
binding->name,
binding->combo.keysym,
existing->combo.keysym,
binding->resolved_combo.keycodes[i]);
}
g_hash_table_replace (keys->key_bindings_index,
GINT_TO_POINTER (index_key), binding);
}
}
static void
resolve_key_combo (MetaKeyBindingManager *keys,
MetaKeyCombo *combo,
MetaResolvedKeyCombo *resolved_combo)
{
resolved_key_combo_reset (resolved_combo);
if (combo->keysym != 0)
{
get_keycodes_for_keysym (keys, combo->keysym, resolved_combo);
}
else if (combo->keycode != 0)
{
resolved_combo->keycodes = g_new0 (xkb_keycode_t, 1);
resolved_combo->keycodes[0] = combo->keycode;
resolved_combo->len = 1;
}
devirtualize_modifiers (keys, combo->modifiers, &resolved_combo->mask);
}
static void
binding_reload_combos_foreach (gpointer key,
gpointer value,
gpointer data)
{
MetaKeyBindingManager *keys = data;
MetaKeyBinding *binding = value;
resolve_key_combo (keys, &binding->combo, &binding->resolved_combo);
index_binding (keys, binding);
}
typedef struct _FindLatinKeysymsState
{
MetaKeyBindingKeyboardLayout *layout;
gboolean *required_keysyms_found;
int n_required_keysyms;
} FindLatinKeysymsState;
static void
find_latin_keysym (struct xkb_keymap *keymap,
xkb_keycode_t key,
void *data)
{
FindLatinKeysymsState *state = data;
int n_keysyms, i;
const xkb_keysym_t *keysyms;
n_keysyms = xkb_keymap_key_get_syms_by_level (state->layout->keymap,
key,
state->layout->index,
0,
&keysyms);
for (i = 0; i < n_keysyms; i++)
{
xkb_keysym_t keysym = keysyms[i];
if (keysym >= XKB_KEY_a && keysym <= XKB_KEY_z)
{
unsigned int keysym_index = keysym - XKB_KEY_a;
if (!state->required_keysyms_found[keysym_index])
{
state->required_keysyms_found[keysym_index] = TRUE;
state->n_required_keysyms--;
}
}
}
}
static gboolean
needs_secondary_layout (MetaKeyBindingKeyboardLayout *layout)
{
gboolean required_keysyms_found[] = {
FALSE, /* XKB_KEY_a */
FALSE, /* XKB_KEY_b */
FALSE, /* XKB_KEY_c */
FALSE, /* XKB_KEY_d */
FALSE, /* XKB_KEY_e */
FALSE, /* XKB_KEY_f */
FALSE, /* XKB_KEY_g */
FALSE, /* XKB_KEY_h */
FALSE, /* XKB_KEY_i */
FALSE, /* XKB_KEY_j */
FALSE, /* XKB_KEY_k */
FALSE, /* XKB_KEY_l */
FALSE, /* XKB_KEY_m */
FALSE, /* XKB_KEY_n */
FALSE, /* XKB_KEY_o */
FALSE, /* XKB_KEY_p */
FALSE, /* XKB_KEY_q */
FALSE, /* XKB_KEY_r */
FALSE, /* XKB_KEY_s */
FALSE, /* XKB_KEY_t */
FALSE, /* XKB_KEY_u */
FALSE, /* XKB_KEY_v */
FALSE, /* XKB_KEY_w */
FALSE, /* XKB_KEY_x */
FALSE, /* XKB_KEY_y */
FALSE, /* XKB_KEY_z */
};
FindLatinKeysymsState state = {
.layout = layout,
.required_keysyms_found = required_keysyms_found,
.n_required_keysyms = G_N_ELEMENTS (required_keysyms_found),
};
xkb_keymap_key_for_each (layout->keymap, find_latin_keysym, &state);
return state.n_required_keysyms != 0;
}
static void
clear_active_keyboard_layouts (MetaKeyBindingManager *keys)
{
unsigned int i;
for (i = 0; i < G_N_ELEMENTS (keys->active_layouts); i++)
{
MetaKeyBindingKeyboardLayout *layout = &keys->active_layouts[i];
g_clear_pointer (&layout->keymap, xkb_keymap_unref);
*layout = (MetaKeyBindingKeyboardLayout) { 0 };
}
}
static MetaKeyBindingKeyboardLayout
create_us_layout (void)
{
struct xkb_rule_names names;
struct xkb_keymap *keymap;
struct xkb_context *context;
names.rules = DEFAULT_XKB_RULES_FILE;
names.model = DEFAULT_XKB_MODEL;
names.layout = "us";
names.variant = "";
names.options = "";
context = xkb_context_new (XKB_CONTEXT_NO_FLAGS);
keymap = xkb_keymap_new_from_names (context, &names, XKB_KEYMAP_COMPILE_NO_FLAGS);
xkb_context_unref (context);
return (MetaKeyBindingKeyboardLayout) {
.keymap = keymap,
.n_levels = calculate_n_layout_levels (keymap, 0),
};
}
static void
reload_active_keyboard_layouts (MetaKeyBindingManager *keys)
{
struct xkb_keymap *keymap;
xkb_layout_index_t layout_index;
MetaKeyBindingKeyboardLayout primary_layout;
clear_active_keyboard_layouts (keys);
keymap = meta_backend_get_keymap (keys->backend);
layout_index = meta_backend_get_keymap_layout_group (keys->backend);
primary_layout = (MetaKeyBindingKeyboardLayout) {
.keymap = xkb_keymap_ref (keymap),
.index = layout_index,
.n_levels = calculate_n_layout_levels (keymap, layout_index),
};
keys->active_layouts[META_KEY_BINDING_PRIMARY_LAYOUT] = primary_layout;
if (needs_secondary_layout (&primary_layout))
{
MetaKeyBindingKeyboardLayout us_layout;
us_layout = create_us_layout ();
keys->active_layouts[META_KEY_BINDING_SECONDARY_LAYOUT] = us_layout;
}
}
static void
reload_combos (MetaKeyBindingManager *keys)
{
g_hash_table_remove_all (keys->key_bindings_index);
reload_active_keyboard_layouts (keys);
resolve_key_combo (keys,
&keys->overlay_key_combo,
&keys->overlay_resolved_key_combo);
resolve_key_combo (keys,
&keys->locate_pointer_key_combo,
&keys->locate_pointer_resolved_key_combo);
reload_iso_next_group_combos (keys);
g_hash_table_foreach (keys->key_bindings, binding_reload_combos_foreach, keys);
}
static void
rebuild_binding_table (MetaKeyBindingManager *keys,
GList *prefs,
GList *grabs)
{
MetaKeyBinding *b;
GList *p, *g;
g_hash_table_remove_all (keys->key_bindings);
p = prefs;
while (p)
{
MetaKeyPref *pref = (MetaKeyPref*)p->data;
GSList *tmp = pref->combos;
while (tmp)
{
MetaKeyCombo *combo = tmp->data;
if (combo && (combo->keysym != None || combo->keycode != 0))
{
MetaKeyHandler *handler = HANDLER (pref->name);
b = g_slice_new0 (MetaKeyBinding);
b->name = pref->name;
b->handler = handler;
b->flags = handler->flags;
b->combo = *combo;
g_hash_table_add (keys->key_bindings, b);
}
tmp = tmp->next;
}
p = p->next;
}
g = grabs;
while (g)
{
MetaKeyGrab *grab = (MetaKeyGrab*)g->data;
if (grab->combo.keysym != None || grab->combo.keycode != 0)
{
MetaKeyHandler *handler = HANDLER ("external-grab");
b = g_slice_new0 (MetaKeyBinding);
b->name = grab->name;
b->handler = handler;
b->flags = grab->flags;
b->combo = grab->combo;
g_hash_table_add (keys->key_bindings, b);
}
g = g->next;
}
meta_topic (META_DEBUG_KEYBINDINGS,
" %d bindings in table\n",
g_hash_table_size (keys->key_bindings));
}
static void
rebuild_key_binding_table (MetaKeyBindingManager *keys)
{
GList *prefs, *grabs;
meta_topic (META_DEBUG_KEYBINDINGS,
"Rebuilding key binding table from preferences\n");
prefs = meta_prefs_get_keybindings ();
grabs = g_hash_table_get_values (external_grabs);
rebuild_binding_table (keys, prefs, grabs);
g_list_free (prefs);
g_list_free (grabs);
}
static void
rebuild_special_bindings (MetaKeyBindingManager *keys)
{
MetaKeyCombo combo;
meta_prefs_get_overlay_binding (&combo);
keys->overlay_key_combo = combo;
meta_prefs_get_locate_pointer_binding (&combo);
keys->locate_pointer_key_combo = combo;
}
static void
ungrab_key_bindings (MetaDisplay *display)
{
GSList *windows, *l;
if (display->x11_display)
meta_x11_display_ungrab_keys (display->x11_display);
windows = meta_display_list_windows (display, META_LIST_DEFAULT);
for (l = windows; l; l = l->next)
{
MetaWindow *w = l->data;
meta_window_ungrab_keys (w);
}
g_slist_free (windows);
}
static void
grab_key_bindings (MetaDisplay *display)
{
GSList *windows, *l;
if (display->x11_display)
meta_x11_display_grab_keys (display->x11_display);
windows = meta_display_list_windows (display, META_LIST_DEFAULT);
for (l = windows; l; l = l->next)
{
MetaWindow *w = l->data;
meta_window_grab_keys (w);
}
g_slist_free (windows);
}
static MetaKeyBinding *
get_keybinding (MetaKeyBindingManager *keys,
MetaResolvedKeyCombo *resolved_combo)
{
MetaKeyBinding *binding = NULL;
int i;
for (i = 0; i < resolved_combo->len; i++)
{
guint32 key;
key = key_combo_key (resolved_combo, i);
binding = g_hash_table_lookup (keys->key_bindings_index,
GINT_TO_POINTER (key));
if (binding != NULL)
break;
}
return binding;
}
static guint
next_dynamic_keybinding_action (void)
{
static guint num_dynamic_bindings = 0;
return META_KEYBINDING_ACTION_LAST + (++num_dynamic_bindings);
}
static gboolean
add_keybinding_internal (MetaDisplay *display,
const char *name,
GSettings *settings,
const char **bindings,
MetaKeyBindingFlags flags,
MetaKeyBindingAction action,
MetaKeyHandlerFunc func,
int data,
gpointer user_data,
GDestroyNotify free_data)
{
MetaKeyHandler *handler;
if (!meta_prefs_add_keybinding (name, settings, bindings, action, flags))
return FALSE;
handler = g_new0 (MetaKeyHandler, 1);
handler->name = g_strdup (name);
handler->func = func;
handler->default_func = func;
handler->data = data;
handler->flags = flags;
handler->user_data = user_data;
handler->user_data_free_func = free_data;
g_hash_table_insert (key_handlers, g_strdup (name), handler);
return TRUE;
}
static gboolean
add_builtin_keybinding (MetaDisplay *display,
const char *name,
GSettings *settings,
MetaKeyBindingFlags flags,
MetaKeyBindingAction action,
MetaKeyHandlerFunc handler,
int handler_arg)
{
return add_keybinding_internal (display, name, settings, NULL,
flags | META_KEY_BINDING_BUILTIN,
action, handler, handler_arg, NULL, NULL);