Debian Codegraph @

clutter/x11/clutter-keymap-x11.c

raw
1 /*
2 * Clutter.
3 *
4 * An OpenGL based 'interactive canvas' library.
5 *
6 * Copyright (C) 2010 Intel Corp.
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 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, see <http://www.gnu.org/licenses/>.
20 *
21 * Author: Emmanuele Bassi <[email protected]>
22 */
23
24 #include "config.h"
25
26 #include "clutter-keymap-x11.h"
27 #include "clutter-backend-x11.h"
28
29 #include "clutter-debug.h"
30 #include "clutter-event-translator.h"
31 #include "clutter-private.h"
32
33 #include <X11/Xatom.h>
34
35 #ifdef HAVE_XKB
36 #include <X11/XKBlib.h>
37 #endif
38
39 typedef struct _ClutterKeymapX11Class ClutterKeymapX11Class;
40 typedef struct _DirectionCacheEntry DirectionCacheEntry;
41
42 struct _DirectionCacheEntry
43 {
44 guint serial;
45 Atom group_atom;
46 PangoDirection direction;
47 };
48
49 struct _ClutterKeymapX11
50 {
51 GObject parent_instance;
52
53 ClutterBackend *backend;
54
55 int min_keycode;
56 int max_keycode;
57
58 ClutterModifierType modmap[8];
59
60 ClutterModifierType num_lock_mask;
61 ClutterModifierType scroll_lock_mask;
62
63 PangoDirection current_direction;
64
65 #ifdef HAVE_XKB
66 XkbDescPtr xkb_desc;
67 int xkb_event_base;
68 guint xkb_map_serial;
69 Atom current_group_atom;
70 guint current_cache_serial;
71 DirectionCacheEntry group_direction_cache[4];
72 #endif
73
74 guint caps_lock_state : 1;
75 guint num_lock_state : 1;
76 guint has_direction : 1;
77 };
78
79 struct _ClutterKeymapX11Class
80 {
81 GObjectClass parent_class;
82 };
83
84 enum
85 {
86 PROP_0,
87
88 PROP_BACKEND,
89
90 PROP_LAST
91 };
92
93 static GParamSpec *obj_props[PROP_LAST] = { NULL, };
94
95 static void clutter_event_translator_iface_init (ClutterEventTranslatorIface *iface);
96
97 #define clutter_keymap_x11_get_type _clutter_keymap_x11_get_type
98
99 G_DEFINE_TYPE_WITH_CODE (ClutterKeymapX11, clutter_keymap_x11, G_TYPE_OBJECT,
100 G_IMPLEMENT_INTERFACE (CLUTTER_TYPE_EVENT_TRANSLATOR,
101 clutter_event_translator_iface_init));
102
103 #ifdef HAVE_XKB
104
105 /* code adapted from gdk/x11/gdkkeys-x11.c - update_modmap */
106 static void
107 update_modmap (Display *display,
108 ClutterKeymapX11 *keymap_x11)
109 {
110 static struct {
111 const gchar *name;
112 Atom atom;
113 ClutterModifierType mask;
114 } vmods[] = {
115 { "Meta", 0, CLUTTER_META_MASK },
116 { "Super", 0, CLUTTER_SUPER_MASK },
117 { "Hyper", 0, CLUTTER_HYPER_MASK },
118 { NULL, 0, 0 }
119 };
120
121 int i, j, k;
122
123 if (vmods[0].atom == 0)
124 for (i = 0; vmods[i].name; i++)
125 vmods[i].atom = XInternAtom (display, vmods[i].name, FALSE);
126
127 for (i = 0; i < 8; i++)
128 keymap_x11->modmap[i] = 1 << i;
129
130 for (i = 0; i < XkbNumVirtualMods; i++)
131 {
132 for (j = 0; vmods[j].atom; j++)
133 {
134 if (keymap_x11->xkb_desc->names->vmods[i] == vmods[j].atom)
135 {
136 for (k = 0; k < 8; k++)
137 {
138 if (keymap_x11->xkb_desc->server->vmods[i] & (1 << k))
139 keymap_x11->modmap[k] |= vmods[j].mask;
140 }
141 }
142 }
143 }
144 }
145
146 static XkbDescPtr
147 get_xkb (ClutterKeymapX11 *keymap_x11)
148 {
149 ClutterBackendX11 *backend_x11 = CLUTTER_BACKEND_X11 (keymap_x11->backend);
150
151 if (keymap_x11->max_keycode == 0)
152 XDisplayKeycodes (backend_x11->xdpy,
153 &keymap_x11->min_keycode,
154 &keymap_x11->max_keycode);
155
156 if (keymap_x11->xkb_desc == NULL)
157 {
158 int flags = XkbKeySymsMask
159 | XkbKeyTypesMask
160 | XkbModifierMapMask
161 | XkbVirtualModsMask;
162
163 keymap_x11->xkb_desc = XkbGetMap (backend_x11->xdpy, flags, XkbUseCoreKbd);
164 if (G_UNLIKELY (keymap_x11->xkb_desc == NULL))
165 {
166 g_error ("Failed to get the keymap from XKB");
167 return NULL;
168 }
169
170 flags = XkbGroupNamesMask | XkbVirtualModNamesMask;
171 XkbGetNames (backend_x11->xdpy, flags, keymap_x11->xkb_desc);
172
173 update_modmap (backend_x11->xdpy, keymap_x11);
174 }
175 else if (keymap_x11->xkb_map_serial != backend_x11->keymap_serial)
176 {
177 int flags = XkbKeySymsMask
178 | XkbKeyTypesMask
179 | XkbModifierMapMask
180 | XkbVirtualModsMask;
181
182 CLUTTER_NOTE (BACKEND, "Updating XKB keymap");
183
184 XkbGetUpdatedMap (backend_x11->xdpy, flags, keymap_x11->xkb_desc);
185
186 flags = XkbGroupNamesMask | XkbVirtualModNamesMask;
187 XkbGetNames (backend_x11->xdpy, flags, keymap_x11->xkb_desc);
188
189 update_modmap (backend_x11->xdpy, keymap_x11);
190
191 keymap_x11->xkb_map_serial = backend_x11->keymap_serial;
192 }
193
194 if (keymap_x11->num_lock_mask == 0)
195 keymap_x11->num_lock_mask = XkbKeysymToModifiers (backend_x11->xdpy,
196 XK_Num_Lock);
197
198 if (keymap_x11->scroll_lock_mask == 0)
199 keymap_x11->scroll_lock_mask = XkbKeysymToModifiers (backend_x11->xdpy,
200 XK_Scroll_Lock);
201
202 return keymap_x11->xkb_desc;
203 }
204 #endif /* HAVE_XKB */
205
206 #ifdef HAVE_XKB
207 static void
208 update_locked_mods (ClutterKeymapX11 *keymap_x11,
209 gint locked_mods)
210 {
211 #if 0
212 gboolean old_caps_lock_state, old_num_lock_state;
213
214 old_caps_lock_state = keymap_x11->caps_lock_state;
215 old_num_lock_state = keymap_x11->num_lock_state;
216 #endif
217
218 keymap_x11->caps_lock_state = (locked_mods & CLUTTER_LOCK_MASK) != 0;
219 keymap_x11->num_lock_state = (locked_mods & keymap_x11->num_lock_mask) != 0;
220
221 CLUTTER_NOTE (BACKEND, "Locks state changed - Num: %s, Caps: %s",
222 keymap_x11->num_lock_state ? "set" : "unset",
223 keymap_x11->caps_lock_state ? "set" : "unset");
224
225 #if 0
226 /* Add signal to ClutterBackend? */
227 if ((keymap_x11->caps_lock_state != old_caps_lock_state) ||
228 (keymap_x11->num_lock_state != old_num_lock_state))
229 g_signal_emit_by_name (keymap_x11->backend, "key-lock-changed");
230 #endif
231 }
232 #endif /* HAVE_XKB */
233
234 #ifdef HAVE_XKB
235 /* the code to retrieve the keymap direction and cache it
236 * is taken from GDK:
237 * gdk/x11/gdkkeys-x11.c
238 */
239 static PangoDirection
240 get_direction (XkbDescPtr xkb,
241 int group)
242 {
243 int rtl_minus_ltr = 0; /* total number of RTL keysyms minus LTR ones */
244 int code;
245
246 for (code = xkb->min_key_code;
247 code <= xkb->max_key_code;
248 code += 1)
249 {
250 int level = 0;
251 KeySym sym = XkbKeySymEntry (xkb, code, level, group);
252 PangoDirection dir = pango_unichar_direction (clutter_keysym_to_unicode (sym));
253
254 switch (dir)
255 {
256 case PANGO_DIRECTION_RTL:
257 rtl_minus_ltr++;
258 break;
259
260 case PANGO_DIRECTION_LTR:
261 rtl_minus_ltr--;
262 break;
263
264 default:
265 break;
266 }
267 }
268
269 if (rtl_minus_ltr > 0)
270 return PANGO_DIRECTION_RTL;
271
272 return PANGO_DIRECTION_LTR;
273 }
274
275 static PangoDirection
276 get_direction_from_cache (ClutterKeymapX11 *keymap_x11,
277 XkbDescPtr xkb,
278 int group)
279 {
280 Atom group_atom = xkb->names->groups[group];
281 gboolean cache_hit = FALSE;
282 DirectionCacheEntry *cache = keymap_x11->group_direction_cache;
283 PangoDirection direction = PANGO_DIRECTION_NEUTRAL;
284 int i;
285
286 if (keymap_x11->has_direction)
287 {
288 /* look up in the cache */
289 for (i = 0; i < G_N_ELEMENTS (keymap_x11->group_direction_cache); i++)
290 {
291 if (cache[i].group_atom == group_atom)
292 {
293 cache_hit = TRUE;
294 cache[i].serial = keymap_x11->current_cache_serial++;
295 direction = cache[i].direction;
296 group_atom = cache[i].group_atom;
297 break;
298 }
299 }
300 }
301 else
302 {
303 /* initialize the cache */
304 for (i = 0; i < G_N_ELEMENTS (keymap_x11->group_direction_cache); i++)
305 {
306 cache[i].group_atom = 0;
307 cache[i].direction = PANGO_DIRECTION_NEUTRAL;
308 cache[i].serial = keymap_x11->current_cache_serial;
309 }
310
311 keymap_x11->current_cache_serial += 1;
312 }
313
314 /* insert the new entry in the cache */
315 if (!cache_hit)
316 {
317 int oldest = 0;
318
319 direction = get_direction (xkb, group);
320
321 /* replace the oldest entry */
322 for (i = 0; i < G_N_ELEMENTS (keymap_x11->group_direction_cache); i++)
323 {
324 if (cache[i].serial < cache[oldest].serial)
325 oldest = i;
326 }
327
328 cache[oldest].group_atom = group_atom;
329 cache[oldest].direction = direction;
330 cache[oldest].serial = keymap_x11->current_cache_serial++;
331 }
332
333 return direction;
334 }
335 #endif /* HAVE_XKB */
336
337 static void
338 update_direction (ClutterKeymapX11 *keymap_x11,
339 int group)
340 {
341 #ifdef HAVE_XKB
342 XkbDescPtr xkb = get_xkb (keymap_x11);
343 Atom group_atom;
344
345 group_atom = xkb->names->groups[group];
346
347 if (!keymap_x11->has_direction || keymap_x11->current_group_atom != group_atom)
348 {
349 keymap_x11->current_direction = get_direction_from_cache (keymap_x11, xkb, group);
350 keymap_x11->current_group_atom = group_atom;
351 keymap_x11->has_direction = TRUE;
352 }
353 #endif /* HAVE_XKB */
354 }
355
356 static void
357 clutter_keymap_x11_constructed (GObject *gobject)
358 {
359 ClutterKeymapX11 *keymap_x11 = CLUTTER_KEYMAP_X11 (gobject);
360 ClutterBackendX11 *backend_x11;
361
362 g_assert (keymap_x11->backend != NULL);
363 backend_x11 = CLUTTER_BACKEND_X11 (keymap_x11->backend);
364
365 #ifdef HAVE_XKB
366 {
367 gint xkb_major = XkbMajorVersion;
368 gint xkb_minor = XkbMinorVersion;
369
370 if (XkbLibraryVersion (&xkb_major, &xkb_minor))
371 {
372 xkb_major = XkbMajorVersion;
373 xkb_minor = XkbMinorVersion;
374
375 if (XkbQueryExtension (backend_x11->xdpy,
376 NULL,
377 &keymap_x11->xkb_event_base,
378 NULL,
379 &xkb_major, &xkb_minor))
380 {
381 Bool detectable_autorepeat_supported;
382
383 backend_x11->use_xkb = TRUE;
384
385 XkbSelectEvents (backend_x11->xdpy,
386 XkbUseCoreKbd,
387 XkbNewKeyboardNotifyMask | XkbMapNotifyMask | XkbStateNotifyMask,
388 XkbNewKeyboardNotifyMask | XkbMapNotifyMask | XkbStateNotifyMask);
389
390 XkbSelectEventDetails (backend_x11->xdpy,
391 XkbUseCoreKbd, XkbStateNotify,
392 XkbAllStateComponentsMask,
393 XkbGroupLockMask | XkbModifierLockMask);
394
395 /* enable XKB autorepeat */
396 XkbSetDetectableAutoRepeat (backend_x11->xdpy,
397 True,
398 &detectable_autorepeat_supported);
399
400 backend_x11->have_xkb_autorepeat = detectable_autorepeat_supported;
401
402 CLUTTER_NOTE (BACKEND, "Detectable autorepeat: %s",
403 backend_x11->have_xkb_autorepeat ? "supported"
404 : "not supported");
405 }
406 }
407 }
408 #endif /* HAVE_XKB */
409 }
410
411 static void
412 clutter_keymap_x11_set_property (GObject *gobject,
413 guint prop_id,
414 const GValue *value,
415 GParamSpec *pspec)
416 {
417 ClutterKeymapX11 *keymap = CLUTTER_KEYMAP_X11 (gobject);
418
419 switch (prop_id)
420 {
421 case PROP_BACKEND:
422 keymap->backend = g_value_get_object (value);
423 break;
424
425 default:
426 G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
427 break;
428 }
429 }
430
431 static void
432 clutter_keymap_x11_finalize (GObject *gobject)
433 {
434 ClutterKeymapX11 *keymap;
435 ClutterEventTranslator *translator;
436
437 keymap = CLUTTER_KEYMAP_X11 (gobject);
438 translator = CLUTTER_EVENT_TRANSLATOR (keymap);
439
440 #ifdef HAVE_XKB
441 _clutter_backend_remove_event_translator (keymap->backend, translator);
442
443 if (keymap->xkb_desc != NULL)
444 XkbFreeKeyboard (keymap->xkb_desc, XkbAllComponentsMask, True);
445 #endif
446
447 G_OBJECT_CLASS (clutter_keymap_x11_parent_class)->finalize (gobject);
448 }
449
450 static void
451 clutter_keymap_x11_class_init (ClutterKeymapX11Class *klass)
452 {
453 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
454
455 obj_props[PROP_BACKEND] =
456 g_param_spec_object ("backend",
457 P_("Backend"),
458 P_("The Clutter backend"),
459 CLUTTER_TYPE_BACKEND,
460 CLUTTER_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY);
461
462 gobject_class->constructed = clutter_keymap_x11_constructed;
463 gobject_class->set_property = clutter_keymap_x11_set_property;
464 gobject_class->finalize = clutter_keymap_x11_finalize;
465 g_object_class_install_properties (gobject_class, PROP_LAST, obj_props);
466 }
467
468 static void
469 clutter_keymap_x11_init (ClutterKeymapX11 *keymap)
470 {
471 keymap->current_direction = PANGO_DIRECTION_NEUTRAL;
472 }
473
474 static ClutterTranslateReturn
475 clutter_keymap_x11_translate_event (ClutterEventTranslator *translator,
476 gpointer native,
477 ClutterEvent *event)
478 {
479 ClutterKeymapX11 *keymap_x11 = CLUTTER_KEYMAP_X11 (translator);
480 ClutterBackendX11 *backend_x11;
481 ClutterTranslateReturn retval;
482 XEvent *xevent;
483
484 backend_x11 = CLUTTER_BACKEND_X11 (keymap_x11->backend);
485 if (!backend_x11->use_xkb)
486 return CLUTTER_TRANSLATE_CONTINUE;
487
488 xevent = native;
489
490 retval = CLUTTER_TRANSLATE_CONTINUE;
491
492 #ifdef HAVE_XKB
493 if (xevent->type == keymap_x11->xkb_event_base)
494 {
495 XkbEvent *xkb_event = (XkbEvent *) xevent;
496
497 switch (xkb_event->any.xkb_type)
498 {
499 case XkbStateNotify:
500 CLUTTER_NOTE (EVENT, "Updating keyboard state");
501 update_direction (keymap_x11, XkbStateGroup (&xkb_event->state));
502 update_locked_mods (keymap_x11, xkb_event->state.locked_mods);
503 retval = CLUTTER_TRANSLATE_REMOVE;
504 break;
505
506 case XkbNewKeyboardNotify:
507 case XkbMapNotify:
508 CLUTTER_NOTE (EVENT, "Updating keyboard mapping");
509 XkbRefreshKeyboardMapping (&xkb_event->map);
510 backend_x11->keymap_serial += 1;
511 retval = CLUTTER_TRANSLATE_REMOVE;
512 break;
513
514 default:
515 break;
516 }
517 }
518 #endif /* HAVE_XKB */
519
520 return retval;
521 }
522
523 static void
524 clutter_event_translator_iface_init (ClutterEventTranslatorIface *iface)
525 {
526 iface->translate_event = clutter_keymap_x11_translate_event;
527 }
528
529 gint
530 _clutter_keymap_x11_get_key_group (ClutterKeymapX11 *keymap,
531 ClutterModifierType state)
532 {
533 #ifdef HAVE_XKB
534 return XkbGroupForCoreState (state);
535 #else
536 return 0;
537 #endif /* HAVE_XKB */
538 }
539
540 gboolean
541 _clutter_keymap_x11_get_num_lock_state (ClutterKeymapX11 *keymap)
542 {
543 g_return_val_if_fail (CLUTTER_IS_KEYMAP_X11 (keymap), FALSE);
544
545 return keymap->num_lock_state;
546 }
547
548 gboolean
549 _clutter_keymap_x11_get_caps_lock_state (ClutterKeymapX11 *keymap)
550 {
551 g_return_val_if_fail (CLUTTER_IS_KEYMAP_X11 (keymap), FALSE);
552
553 return keymap->caps_lock_state;
554 }
555
556 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
557
558 /* XXX - yes, I know that XKeycodeToKeysym() has been deprecated; hopefully,
559 * this code will never get run on any decent system that is also able to
560 * run Clutter. I just don't want to copy the implementation inside GDK for
561 * a fallback path.
562 */
563 static int
564 translate_keysym (ClutterKeymapX11 *keymap,
565 guint hardware_keycode)
566 {
567 ClutterBackendX11 *backend_x11;
568 gint retval;
569
570 backend_x11 = CLUTTER_BACKEND_X11 (keymap->backend);
571
572 retval = XKeycodeToKeysym (backend_x11->xdpy, hardware_keycode, 0);
573
574 return retval;
575 }
576
577 G_GNUC_END_IGNORE_DEPRECATIONS
578
579 gint
580 _clutter_keymap_x11_translate_key_state (ClutterKeymapX11 *keymap,
581 guint hardware_keycode,
582 ClutterModifierType *modifier_state_p,
583 ClutterModifierType *mods_p)
584 {
585 ClutterBackendX11 *backend_x11;
586 ClutterModifierType unconsumed_modifiers = 0;
587 ClutterModifierType modifier_state = *modifier_state_p;
588 gint retval;
589
590 g_return_val_if_fail (CLUTTER_IS_KEYMAP_X11 (keymap), 0);
591
592 backend_x11 = CLUTTER_BACKEND_X11 (keymap->backend);
593
594 #ifdef HAVE_XKB
595 if (backend_x11->use_xkb)
596 {
597 XkbDescRec *xkb = get_xkb (keymap);
598 KeySym tmp_keysym;
599
600 if (XkbTranslateKeyCode (xkb, hardware_keycode, modifier_state,
601 &unconsumed_modifiers,
602 &tmp_keysym))
603 {
604 retval = tmp_keysym;
605 }
606 else
607 retval = 0;
608 }
609 else
610 #endif /* HAVE_XKB */
611 retval = translate_keysym (keymap, hardware_keycode);
612
613 if (mods_p)
614 *mods_p = unconsumed_modifiers;
615
616 *modifier_state_p = modifier_state & ~(keymap->num_lock_mask |
617 keymap->scroll_lock_mask |
618 LockMask);
619
620 return retval;
621 }
622
623 gboolean
624 _clutter_keymap_x11_get_is_modifier (ClutterKeymapX11 *keymap,
625 gint keycode)
626 {
627 g_return_val_if_fail (CLUTTER_IS_KEYMAP_X11 (keymap), FALSE);
628
629 if (keycode < keymap->min_keycode || keycode > keymap->max_keycode)
630 return FALSE;
631
632 #ifdef HAVE_XKB
633 if (CLUTTER_BACKEND_X11 (keymap->backend)->use_xkb)
634 {
635 XkbDescRec *xkb = get_xkb (keymap);
636
637 if (xkb->map->modmap && xkb->map->modmap[keycode] != 0)
638 return TRUE;
639 }
640 #endif /* HAVE_XKB */
641
642 return FALSE;
643 }
644
645 PangoDirection
646 _clutter_keymap_x11_get_direction (ClutterKeymapX11 *keymap)
647 {
648 g_return_val_if_fail (CLUTTER_IS_KEYMAP_X11 (keymap), PANGO_DIRECTION_NEUTRAL);
649
650 #ifdef HAVE_XKB
651 if (CLUTTER_BACKEND_X11 (keymap->backend)->use_xkb)
652 {
653 if (!keymap->has_direction)
654 {
655 Display *xdisplay = CLUTTER_BACKEND_X11 (keymap->backend)->xdpy;
656 XkbStateRec state_rec;
657
658 XkbGetState (xdisplay, XkbUseCoreKbd, &state_rec);
659 update_direction (keymap, XkbStateGroup (&state_rec));
660 }
661
662 return keymap->current_direction;
663 }
664 else
665 #endif
666 return PANGO_DIRECTION_NEUTRAL;
667 }