Debian Codegraph @

src/x/xkeyboard.c

raw
1 /* ______ ___ ___
2 * /\ _ \ /\_ \ /\_ \
3 * \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___
4 * \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\
5 * \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \
6 * \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
7 * \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
8 * /\____/
9 * \_/__/
10 *
11 * X keyboard driver.
12 *
13 * By Elias Pschernig.
14 *
15 * Modified for the new keyboard API by Peter Wang.
16 *
17 * See readme.txt for copyright information.
18 */
19
20 #define ALLEGRO_NO_COMPATIBILITY
21
22 #include <signal.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <signal.h>
26 #include <ctype.h>
27 #include <X11/Xlocale.h>
28 #include <X11/Xos.h>
29 #include <X11/Xlib.h>
30 #include <X11/Xutil.h>
31 #include <X11/Xproto.h>
32 #include <X11/XKBlib.h>
33
34 #include "allegro5/allegro.h"
35 #include "allegro5/internal/aintern.h"
36 #include "allegro5/internal/aintern_events.h"
37 #include "allegro5/internal/aintern_keyboard.h"
38 #include "allegro5/internal/aintern_x.h"
39 #include "allegro5/internal/aintern_xdisplay.h"
40 #include "allegro5/internal/aintern_xkeyboard.h"
41 #include "allegro5/internal/aintern_xsystem.h"
42
43 #ifdef ALLEGRO_RASPBERRYPI
44 #include "allegro5/internal/aintern_raspberrypi.h"
45 #define ALLEGRO_SYSTEM_XGLX ALLEGRO_SYSTEM_RASPBERRYPI
46 #endif
47
48 ALLEGRO_DEBUG_CHANNEL("keyboard")
49
50 /*----------------------------------------------------------------------*/
51 static void handle_key_press(int mycode, int unichar, int filtered,
52 unsigned int modifiers, ALLEGRO_DISPLAY *display);
53 static void handle_key_release(int mycode, unsigned int modifiers, ALLEGRO_DISPLAY *display);
54 static int _key_shifts;
55 /*----------------------------------------------------------------------*/
56
57
58 typedef struct ALLEGRO_KEYBOARD_XWIN
59 {
60 ALLEGRO_KEYBOARD parent;
61 ALLEGRO_KEYBOARD_STATE state;
62 // Quit if Ctrl-Alt-Del is pressed.
63 bool three_finger_flag;
64 } ALLEGRO_KEYBOARD_XWIN;
65
66
67 typedef struct ALLEGRO_KEY_REPEAT_DATA {
68 XKeyEvent *event;
69 bool found;
70 } ALLEGRO_KEY_REPEAT_DATA;
71
72
73 /* the one and only keyboard object */
74 static ALLEGRO_KEYBOARD_XWIN the_keyboard;
75
76 static int last_press_code = -1;
77
78 #ifdef ALLEGRO_XWINDOWS_WITH_XIM
79 static XIM xim = NULL;
80 static XIC xic = NULL;
81 #endif
82 static XModifierKeymap *xmodmap = NULL;
83 static int xkeyboard_installed = 0;
84 static int used[ALLEGRO_KEY_MAX];
85 static int sym_per_key;
86 static int min_keycode, max_keycode;
87 static KeySym *keysyms = NULL;
88 static int main_pid; /* The pid to kill with ctrl-alt-del. */
89 static int pause_key = 0; /* Allegro's special pause key state. */
90 static int keycode_to_scancode[256];
91
92 /* This table can be ammended to provide more reasonable defaults for
93 * mappings other than US/UK. They are used to map X11 KeySyms as found in
94 * X11/keysym.h to Allegro's ALLEGRO_KEY_* codes. This will only work well on US/UK
95 * keyboards since Allegro simply doesn't have ALLEGRO_KEY_* codes for non US/UK
96 * keyboards. So with other mappings, the unmapped keys will be distributed
97 * arbitrarily to the remaining ALLEGRO_KEY_* codes.
98 *
99 * Double mappings should be avoided, else they can lead to different keys
100 * producing the same ALLEGRO_KEY_* code on some mappings.
101 *
102 * In cases where there is no other way to detect a key, since we have no
103 * ASCII applied to it, like ALLEGRO_KEY_LEFT or ALLEGRO_KEY_PAUSE, multiple mappings should
104 * be ok though. This table will never be able to be 100% perfect, so just
105 * try to make it work for as many as possible, using additional hacks in
106 * some cases. There is also still the possibility to override keys with
107 * the [xkeyboard] config section, so users can always re-map keys. (E.g.
108 * to play an Allegro game which hard-coded ALLEGRO_KEY_Y and ALLEGRO_KEY_X for left and
109 * right.)
110 */
111 static struct {
112 KeySym keysym;
113 int allegro_key;
114 }
115 translation_table[] = {
116 {XK_a, ALLEGRO_KEY_A},
117 {XK_b, ALLEGRO_KEY_B},
118 {XK_c, ALLEGRO_KEY_C},
119 {XK_d, ALLEGRO_KEY_D},
120 {XK_e, ALLEGRO_KEY_E},
121 {XK_f, ALLEGRO_KEY_F},
122 {XK_g, ALLEGRO_KEY_G},
123 {XK_h, ALLEGRO_KEY_H},
124 {XK_i, ALLEGRO_KEY_I},
125 {XK_j, ALLEGRO_KEY_J},
126 {XK_k, ALLEGRO_KEY_K},
127 {XK_l, ALLEGRO_KEY_L},
128 {XK_m, ALLEGRO_KEY_M},
129 {XK_n, ALLEGRO_KEY_N},
130 {XK_o, ALLEGRO_KEY_O},
131 {XK_p, ALLEGRO_KEY_P},
132 {XK_q, ALLEGRO_KEY_Q},
133 {XK_r, ALLEGRO_KEY_R},
134 {XK_s, ALLEGRO_KEY_S},
135 {XK_t, ALLEGRO_KEY_T},
136 {XK_u, ALLEGRO_KEY_U},
137 {XK_v, ALLEGRO_KEY_V},
138 {XK_w, ALLEGRO_KEY_W},
139 {XK_x, ALLEGRO_KEY_X},
140 {XK_y, ALLEGRO_KEY_Y},
141 {XK_z, ALLEGRO_KEY_Z},
142 {XK_0, ALLEGRO_KEY_0},
143 {XK_1, ALLEGRO_KEY_1},
144 {XK_2, ALLEGRO_KEY_2},
145 {XK_3, ALLEGRO_KEY_3},
146 {XK_4, ALLEGRO_KEY_4},
147 {XK_5, ALLEGRO_KEY_5},
148 {XK_6, ALLEGRO_KEY_6},
149 {XK_7, ALLEGRO_KEY_7},
150 {XK_8, ALLEGRO_KEY_8},
151 {XK_9, ALLEGRO_KEY_9},
152
153 /* Double mappings for numeric keyboard.
154 * If an X server actually uses both at the same time, Allegro will
155 * detect them as the same. But normally, an X server just reports it as
156 * either of them, and therefore we always get the keys as ALLEGRO_KEY_PAD_*.
157 */
158 {XK_KP_0, ALLEGRO_KEY_PAD_0},
159 {XK_KP_Insert, ALLEGRO_KEY_PAD_0},
160 {XK_KP_1, ALLEGRO_KEY_PAD_1},
161 {XK_KP_End, ALLEGRO_KEY_PAD_1},
162 {XK_KP_2, ALLEGRO_KEY_PAD_2},
163 {XK_KP_Down, ALLEGRO_KEY_PAD_2},
164 {XK_KP_3, ALLEGRO_KEY_PAD_3},
165 {XK_KP_Page_Down, ALLEGRO_KEY_PAD_3},
166 {XK_KP_4, ALLEGRO_KEY_PAD_4},
167 {XK_KP_Left, ALLEGRO_KEY_PAD_4},
168 {XK_KP_5, ALLEGRO_KEY_PAD_5},
169 {XK_KP_Begin, ALLEGRO_KEY_PAD_5},
170 {XK_KP_6, ALLEGRO_KEY_PAD_6},
171 {XK_KP_Right, ALLEGRO_KEY_PAD_6},
172 {XK_KP_7, ALLEGRO_KEY_PAD_7},
173 {XK_KP_Home, ALLEGRO_KEY_PAD_7},
174 {XK_KP_8, ALLEGRO_KEY_PAD_8},
175 {XK_KP_Up, ALLEGRO_KEY_PAD_8},
176 {XK_KP_9, ALLEGRO_KEY_PAD_9},
177 {XK_KP_Page_Up, ALLEGRO_KEY_PAD_9},
178 {XK_KP_Delete, ALLEGRO_KEY_PAD_DELETE},
179 {XK_KP_Decimal, ALLEGRO_KEY_PAD_DELETE},
180
181 /* Double mapping!
182 * Same as above - but normally, the X server just reports one or the
183 * other for the Pause key, and the other is not reported for any key.
184 */
185 {XK_Pause, ALLEGRO_KEY_PAUSE},
186 {XK_Break, ALLEGRO_KEY_PAUSE},
187
188 {XK_F1, ALLEGRO_KEY_F1},
189 {XK_F2, ALLEGRO_KEY_F2},
190 {XK_F3, ALLEGRO_KEY_F3},
191 {XK_F4, ALLEGRO_KEY_F4},
192 {XK_F5, ALLEGRO_KEY_F5},
193 {XK_F6, ALLEGRO_KEY_F6},
194 {XK_F7, ALLEGRO_KEY_F7},
195 {XK_F8, ALLEGRO_KEY_F8},
196 {XK_F9, ALLEGRO_KEY_F9},
197 {XK_F10, ALLEGRO_KEY_F10},
198 {XK_F11, ALLEGRO_KEY_F11},
199 {XK_F12, ALLEGRO_KEY_F12},
200 {XK_Escape, ALLEGRO_KEY_ESCAPE},
201 {XK_grave, ALLEGRO_KEY_TILDE}, /* US left of 1 */
202 {XK_minus, ALLEGRO_KEY_MINUS}, /* US right of 0 */
203 {XK_equal, ALLEGRO_KEY_EQUALS}, /* US 2 right of 0 */
204 {XK_BackSpace, ALLEGRO_KEY_BACKSPACE},
205 {XK_Tab, ALLEGRO_KEY_TAB},
206 {XK_bracketleft, ALLEGRO_KEY_OPENBRACE}, /* US right of P */
207 {XK_bracketright, ALLEGRO_KEY_CLOSEBRACE}, /* US 2 right of P */
208 {XK_Return, ALLEGRO_KEY_ENTER},
209 {XK_semicolon, ALLEGRO_KEY_SEMICOLON}, /* US right of L */
210 {XK_apostrophe, ALLEGRO_KEY_QUOTE}, /* US 2 right of L */
211 {XK_backslash, ALLEGRO_KEY_BACKSLASH}, /* US 3 right of L */
212 {XK_less, ALLEGRO_KEY_BACKSLASH2}, /* US left of Y */
213 {XK_comma, ALLEGRO_KEY_COMMA}, /* US right of M */
214 {XK_period, ALLEGRO_KEY_FULLSTOP}, /* US 2 right of M */
215 {XK_slash, ALLEGRO_KEY_SLASH}, /* US 3 right of M */
216 {XK_space, ALLEGRO_KEY_SPACE},
217 {XK_Insert, ALLEGRO_KEY_INSERT},
218 {XK_Delete, ALLEGRO_KEY_DELETE},
219 {XK_Home, ALLEGRO_KEY_HOME},
220 {XK_End, ALLEGRO_KEY_END},
221 {XK_Page_Up, ALLEGRO_KEY_PGUP},
222 {XK_Page_Down, ALLEGRO_KEY_PGDN},
223 {XK_Left, ALLEGRO_KEY_LEFT},
224 {XK_Right, ALLEGRO_KEY_RIGHT},
225 {XK_Up, ALLEGRO_KEY_UP},
226 {XK_Down, ALLEGRO_KEY_DOWN},
227 {XK_KP_Divide, ALLEGRO_KEY_PAD_SLASH},
228 {XK_KP_Multiply, ALLEGRO_KEY_PAD_ASTERISK},
229 {XK_KP_Subtract, ALLEGRO_KEY_PAD_MINUS},
230 {XK_KP_Add, ALLEGRO_KEY_PAD_PLUS},
231 {XK_KP_Enter, ALLEGRO_KEY_PAD_ENTER},
232 {XK_Print, ALLEGRO_KEY_PRINTSCREEN},
233
234 //{, ALLEGRO_KEY_ABNT_C1},
235 //{, ALLEGRO_KEY_YEN},
236 //{, ALLEGRO_KEY_KANA},
237 //{, ALLEGRO_KEY_CONVERT},
238 //{, ALLEGRO_KEY_NOCONVERT},
239 //{, ALLEGRO_KEY_AT},
240 //{, ALLEGRO_KEY_CIRCUMFLEX},
241 //{, ALLEGRO_KEY_COLON2},
242 //{, ALLEGRO_KEY_KANJI},
243 {XK_KP_Equal, ALLEGRO_KEY_PAD_EQUALS}, /* MacOS X */
244 //{, ALLEGRO_KEY_BACKQUOTE}, /* MacOS X */
245 //{, ALLEGRO_KEY_SEMICOLON}, /* MacOS X */
246 //{, ALLEGRO_KEY_COMMAND}, /* MacOS X */
247
248 {XK_Shift_L, ALLEGRO_KEY_LSHIFT},
249 {XK_Shift_R, ALLEGRO_KEY_RSHIFT},
250 {XK_Control_L, ALLEGRO_KEY_LCTRL},
251 {XK_Control_R, ALLEGRO_KEY_RCTRL},
252 {XK_Alt_L, ALLEGRO_KEY_ALT},
253
254 /* Double mappings. This is a bit of a problem, since you can configure
255 * X11 differently to what report for those keys.
256 */
257 {XK_Alt_R, ALLEGRO_KEY_ALTGR},
258 {XK_ISO_Level3_Shift, ALLEGRO_KEY_ALTGR},
259 {XK_Meta_L, ALLEGRO_KEY_LWIN},
260 {XK_Super_L, ALLEGRO_KEY_LWIN},
261 {XK_Meta_R, ALLEGRO_KEY_RWIN},
262 {XK_Super_R, ALLEGRO_KEY_RWIN},
263
264 {XK_Menu, ALLEGRO_KEY_MENU},
265 {XK_Scroll_Lock, ALLEGRO_KEY_SCROLLLOCK},
266 {XK_Num_Lock, ALLEGRO_KEY_NUMLOCK},
267 {XK_Caps_Lock, ALLEGRO_KEY_CAPSLOCK}
268 };
269
270 /* Table of: Allegro's modifier flag, associated X11 flag, toggle method. */
271 static int modifier_flags[8][3] = {
272 {ALLEGRO_KEYMOD_SHIFT, ShiftMask, 0},
273 {ALLEGRO_KEYMOD_CAPSLOCK, LockMask, 1},
274 {ALLEGRO_KEYMOD_CTRL, ControlMask, 0},
275 {ALLEGRO_KEYMOD_ALT, Mod1Mask, 0},
276 {ALLEGRO_KEYMOD_NUMLOCK, Mod2Mask, 1},
277 {ALLEGRO_KEYMOD_SCROLLLOCK, Mod3Mask, 1},
278 {ALLEGRO_KEYMOD_LWIN | ALLEGRO_KEYMOD_RWIN, Mod4Mask, 0}, /* Should we use only one? */
279 {ALLEGRO_KEYMOD_ALTGR, Mod5Mask, 0} /* AltGr */
280 };
281
282 /* Table of key names. */
283 static char const *key_names[ALLEGRO_KEY_MAX];
284
285
286
287 /* update_shifts
288 * Update Allegro's key_shifts variable, directly from the corresponding
289 * X11 modifiers state.
290 */
291 static void update_shifts(XKeyEvent *event)
292 {
293 int mask = 0;
294 int i;
295
296 for (i = 0; i < 8; i++) {
297 int j;
298 /* This is the state of the modifiers just before the key
299 * press/release.
300 */
301 if (event->state & modifier_flags[i][1])
302 mask |= modifier_flags[i][0];
303
304 /* In case a modifier key itself was pressed, we now need to update
305 * the above state for Allegro, which wants the state after the
306 * press, not before as reported by X.
307 */
308 for (j = 0; j < xmodmap->max_keypermod; j++) {
309 if (event->keycode && event->keycode ==
310 xmodmap->modifiermap[i * xmodmap->max_keypermod + j]) {
311 if (event->type == KeyPress) {
312 /* Modifier key pressed - toggle or set flag. */
313 if (modifier_flags[i][2])
314 mask ^= modifier_flags[i][0];
315 else
316 mask |= modifier_flags[i][0];
317 }
318 else if (event->type == KeyRelease) {
319 /* Modifier key of non-toggle key released - remove flag. */
320 if (!modifier_flags[i][2])
321 mask &= ~modifier_flags[i][0];
322 }
323 }
324 }
325 }
326 _key_shifts = mask;
327 }
328
329
330
331 /* find_unknown_key_assignment
332 * In some cases, X11 doesn't report any KeySym for a key - so the earliest
333 * time we can map it to an Allegro key is when it is first pressed.
334 */
335 static int find_unknown_key_assignment(int i)
336 {
337 int j;
338
339 for (j = 1; j < ALLEGRO_KEY_MAX; j++) {
340 if (!used[j]) {
341 const char *str;
342 keycode_to_scancode[i] = j;
343 str = XKeysymToString(keysyms[sym_per_key * (i - min_keycode)]);
344 if (str)
345 key_names[j] = str;
346 else {
347 key_names[j] = _al_keyboard_common_names[j];
348 }
349 used[j] = 1;
350 break;
351 }
352 }
353
354 if (j == ALLEGRO_KEY_MAX) {
355 ALLEGRO_ERROR("You have more keys reported by X than Allegro's "
356 "maximum of %i keys. Please send a bug report.\n", ALLEGRO_KEY_MAX);
357 keycode_to_scancode[i] = 0;
358 }
359
360 char str[1024];
361 sprintf(str, "Key %i missing:", i);
362 for (j = 0; j < sym_per_key; j++) {
363 char *sym_str = XKeysymToString(keysyms[sym_per_key * (i - min_keycode) + j]);
364 sprintf(str + strlen(str), " %s", sym_str ? sym_str : "NULL");
365 }
366 ALLEGRO_DEBUG("%s assigned to %i.\n", str, keycode_to_scancode[i]);
367
368 return keycode_to_scancode[i];
369 }
370
371
372 /* XCheckIfAny predicate that checks if this event may be a key repeat event */
373 static Bool check_for_repeat(Display *display, XEvent *event, XPointer arg)
374 {
375 ALLEGRO_KEY_REPEAT_DATA *d = (ALLEGRO_KEY_REPEAT_DATA *)arg;
376
377 (void)display;
378
379 if (event->type == KeyPress &&
380 event->xkey.keycode == d->event->keycode &&
381 (event->xkey.time - d->event->time) < 4) {
382 d->found = true;
383 }
384
385 return False;
386 }
387
388
389 /* _al_xwin_keyboard_handler:
390 * Keyboard "interrupt" handler.
391 */
392 void _al_xwin_keyboard_handler(XKeyEvent *event, ALLEGRO_DISPLAY *display)
393 {
394 int keycode;
395
396 if (!xkeyboard_installed)
397 return;
398
399 keycode = keycode_to_scancode[event->keycode];
400 if (keycode == -1)
401 keycode = find_unknown_key_assignment(event->keycode);
402
403 update_shifts(event);
404
405 /* Special case the pause key. */
406 if (keycode == ALLEGRO_KEY_PAUSE) {
407 /* Allegro ignore's releasing of the pause key. */
408 if (event->type == KeyRelease)
409 return;
410 if (pause_key) {
411 event->type = KeyRelease;
412 pause_key = 0;
413 }
414 else {
415 pause_key = 1;
416 }
417 }
418
419 if (event->type == KeyPress) { /* Key pressed. */
420 int len;
421 char buffer[16];
422 int unicode = 0;
423 int filtered = 0;
424
425 #if defined (ALLEGRO_XWINDOWS_WITH_XIM) && defined(X_HAVE_UTF8_STRING)
426 if (xic) {
427 len = Xutf8LookupString(xic, event, buffer, sizeof buffer, NULL, NULL);
428 }
429 else
430 #endif
431 {
432 /* XLookupString is supposed to only use ASCII. */
433 len = XLookupString(event, buffer, sizeof buffer, NULL, NULL);
434 }
435 buffer[len] = '\0';
436 ALLEGRO_USTR_INFO info;
437 const ALLEGRO_USTR *ustr = al_ref_cstr(&info, buffer);
438 unicode = al_ustr_get(ustr, 0);
439 if (unicode < 0)
440 unicode = 0;
441
442 #ifdef ALLEGRO_XWINDOWS_WITH_XIM
443 ALLEGRO_DISPLAY_XGLX *glx = (void *)display;
444 filtered = XFilterEvent((XEvent *)event, glx->window);
445 #endif
446 if (keycode || unicode) {
447 handle_key_press(keycode, unicode, filtered, _key_shifts, display);
448 }
449 }
450 else { /* Key release. */
451 /* HACK:
452 * Detect key repeat by looking forward to see if this release is
453 * followed by a press event, in which case we assume that this release
454 * event was generated in response to that key press event. Events are
455 * simultaneous if they are separated by less than 4 ms (a value that
456 * worked well on one machine where this hack was needed).
457 *
458 * This is unnecessary on systems where XkbSetDetectableAutorepeat works.
459 */
460 if (XPending(event->display) > 0) {
461 ALLEGRO_KEY_REPEAT_DATA d;
462 XEvent dummy;
463 d.event = event;
464 d.found = false;
465 XCheckIfEvent(event->display, &dummy, check_for_repeat, (XPointer)&d);
466 if (d.found) {
467 return;
468 }
469 }
470 handle_key_release(keycode, _key_shifts, display);
471 }
472 }
473
474
475
476 /* _al_xwin_keyboard_switch_handler:
477 * Handle a focus switch event.
478 */
479 void _al_xwin_keyboard_switch_handler(ALLEGRO_DISPLAY *display, bool focus_in)
480 {
481 _al_event_source_lock(&the_keyboard.parent.es);
482
483 if (focus_in) {
484 the_keyboard.state.display = display;
485 #ifdef ALLEGRO_XWINDOWS_WITH_XIM
486 if (xic) {
487 ALLEGRO_DISPLAY_XGLX *display_glx = (void *)display;
488 XSetICValues(xic, XNClientWindow, display_glx->window, NULL);
489 }
490 #endif
491 }
492 else {
493 the_keyboard.state.display = NULL;
494 }
495
496 _al_event_source_unlock(&the_keyboard.parent.es);
497 }
498
499
500
501 /* find_allegro_key
502 * Search the translation table for the Allegro key corresponding to the
503 * given KeySym.
504 */
505 static int find_allegro_key(KeySym sym)
506 {
507 int i;
508 int n = sizeof translation_table / sizeof *translation_table;
509
510 for (i = 0; i < n; i++) {
511 if (translation_table[i].keysym == sym)
512 return translation_table[i].allegro_key;
513 }
514 return 0;
515 }
516
517
518
519 /* scancode_to_name:
520 * Converts the given scancode to a description of the key.
521 */
522 static const char *x_scancode_to_name(int scancode)
523 {
524 ASSERT (scancode >= 0 && scancode < ALLEGRO_KEY_MAX);
525
526 return key_names[scancode];
527 }
528
529
530
531 /* _al_xwin_get_keyboard_mapping:
532 * Generate a mapping from X11 keycodes to Allegro ALLEGRO_KEY_* codes. We have
533 * two goals: Every keypress should be mapped to a distinct Allegro ALLEGRO_KEY_*
534 * code. And we want the ALLEGRO_KEY_* codes to match the pressed
535 * key to some extent. To do the latter, the X11 KeySyms produced by a key
536 * are examined. If a match is found in the table above, the mapping is
537 * added to the mapping table. If no known KeySym is found for a key (or
538 * the same KeySym is found for more keys) - the remaining keys are
539 * distributed arbitrarily to the remaining ALLEGRO_KEY_* codes.
540 *
541 * In a future version, this could be simplified by mapping *all* the X11
542 * KeySyms to ALLEGRO_KEY_* codes.
543 */
544
545 static bool _al_xwin_get_keyboard_mapping(void)
546 {
547 int i;
548 int count;
549 int missing = 0;
550
551 ALLEGRO_SYSTEM_XGLX *system = (void *)al_get_system_driver();
552
553 memset(used, 0, sizeof used);
554 memset(keycode_to_scancode, 0, sizeof keycode_to_scancode);
555
556 XDisplayKeycodes(system->x11display, &min_keycode, &max_keycode);
557 count = 1 + max_keycode - min_keycode;
558
559 if (keysyms) {
560 XFree(keysyms);
561 }
562 keysyms = XGetKeyboardMapping(system->x11display, min_keycode,
563 count, &sym_per_key);
564
565 ALLEGRO_INFO("%i keys, %i symbols per key.\n", count, sym_per_key);
566
567 if (sym_per_key <= 0) {
568 return false;
569 }
570
571 missing = 0;
572
573 for (i = min_keycode; i <= max_keycode; i++) {
574 KeySym sym = keysyms[sym_per_key * (i - min_keycode)];
575 KeySym sym2 = keysyms[sym_per_key * (i - min_keycode) + 1];
576 char *sym_str, *sym2_str;
577 int allegro_key = 0;
578 char str[1024];
579
580 sym_str = XKeysymToString(sym);
581 sym2_str = XKeysymToString(sym2);
582
583 snprintf(str, sizeof str, "key [%i: %s %s]", i,
584 sym_str ? sym_str : "NULL", sym2_str ? sym2_str : "NULL");
585
586 /* Hack for French keyboards, to correctly map ALLEGRO_KEY_0 to ALLEGRO_KEY_9. */
587 if (sym2 >= XK_0 && sym2 <= XK_9) {
588 allegro_key = find_allegro_key(sym2);
589 }
590
591 if (!allegro_key) {
592 if (sym != NoSymbol) {
593 allegro_key = find_allegro_key(sym);
594
595 if (allegro_key == 0) {
596 missing++;
597 ALLEGRO_DEBUG("%s defering.\n", str);
598 }
599 }
600 else {
601 /* No KeySym for this key - ignore it. */
602 keycode_to_scancode[i] = -1;
603 ALLEGRO_DEBUG("%s not assigned.\n", str);
604 }
605 }
606
607 if (allegro_key) {
608 bool is_double = false;
609 if (used[allegro_key]) {
610 is_double = true;
611 }
612 keycode_to_scancode[i] = allegro_key;
613 key_names[allegro_key] =
614 XKeysymToString(keysyms[sym_per_key * (i - min_keycode)]);
615 used[allegro_key] = 1;
616 ALLEGRO_DEBUG("%s%s assigned to %i.\n", str,
617 is_double ? " *double*" : "", allegro_key);
618 }
619 }
620
621 if (missing) {
622 /* The keys still not assigned are just assigned arbitrarily now. */
623 for (i = min_keycode; i <= max_keycode; i++) {
624 if (keycode_to_scancode[i] == 0) {
625 find_unknown_key_assignment(i);
626 }
627 }
628 }
629
630 if (xmodmap)
631 XFreeModifiermap(xmodmap);
632 xmodmap = XGetModifierMapping(system->x11display);
633 for (i = 0; i < 8; i++) {
634 int j;
635 char str[1024];
636 sprintf(str, "Modifier %d:", i + 1);
637 for (j = 0; j < xmodmap->max_keypermod; j++) {
638 KeyCode keycode = xmodmap->modifiermap[i * xmodmap->max_keypermod + j];
639 // XKeycodeToKeysym is deprecated
640 //KeySym sym = XKeycodeToKeysym(system->x11display, keycode, 0);
641 KeySym sym = XkbKeycodeToKeysym(system->x11display, keycode, 0, 0);
642
643 char *sym_str = XKeysymToString(sym);
644 sprintf(str + strlen(str), " %s", sym_str ? sym_str : "NULL");
645 }
646 ALLEGRO_DEBUG("%s\n", str);
647 }
648
649 /* The [xkeymap] section can be useful, e.g. if trying to play a
650 * game which has X and Y hardcoded as ALLEGRO_KEY_X and ALLEGRO_KEY_Y to mean
651 * left/right movement, but on the X11 keyboard X and Y are far apart.
652 * For normal use, a user never should have to touch [xkeymap] anymore
653 * though, and proper written programs will not hardcode such mappings.
654 */
655 ALLEGRO_CONFIG *c = al_get_system_config();
656
657 char const *key;
658 ALLEGRO_CONFIG_ENTRY *it;
659 key = al_get_first_config_entry(c, "xkeymap", &it);
660 while (key) {
661 char const *val;
662 val = al_get_config_value(c, "xkeymap", key);
663 int keycode = strtol(key, NULL, 10);
664 int scancode = strtol(val, NULL, 10);
665 if (keycode > 0 && scancode > 0) {
666 keycode_to_scancode[keycode] = scancode;
667 ALLEGRO_WARN("User override: KeySym %i assigned to %i.\n",
668 keycode, scancode);
669 }
670 key = al_get_next_config_entry(&it);
671 }
672
673 return true;
674 }
675
676
677
678 /* x_set_leds:
679 * Update the keyboard LEDs.
680 */
681 static void x_set_leds(int leds)
682 {
683 XKeyboardControl values;
684 ALLEGRO_SYSTEM_XGLX *system = (void *)al_get_system_driver();
685
686 ASSERT(xkeyboard_installed);
687
688 _al_mutex_lock(&system->lock);
689
690 values.led = 1;
691 values.led_mode = leds & ALLEGRO_KEYMOD_NUMLOCK ? LedModeOn : LedModeOff;
692 XChangeKeyboardControl(system->x11display, KBLed | KBLedMode, &values);
693
694 values.led = 2;
695 values.led_mode = leds & ALLEGRO_KEYMOD_CAPSLOCK ? LedModeOn : LedModeOff;
696 XChangeKeyboardControl(system->x11display, KBLed | KBLedMode, &values);
697
698 values.led = 3;
699 values.led_mode = leds & ALLEGRO_KEYMOD_SCROLLLOCK ? LedModeOn : LedModeOff;
700 XChangeKeyboardControl(system->x11display, KBLed | KBLedMode, &values);
701
702 _al_mutex_unlock(&system->lock);
703 }
704
705
706
707 /* x_keyboard_init
708 * Initialise the X11 keyboard driver.
709 */
710 static int x_keyboard_init(void)
711 {
712 #ifdef ALLEGRO_XWINDOWS_WITH_XIM
713 char *old_locale;
714 XIMStyles *xim_styles;
715 XIMStyle xim_style = 0;
716 char *imvalret;
717 int i;
718 #endif
719 ALLEGRO_SYSTEM_XGLX *s = (void *)al_get_system_driver();
720
721 if (xkeyboard_installed)
722 return 0;
723
724 if (s->x11display == NULL)
725 return 0;
726
727 main_pid = getpid();
728
729 memcpy(key_names, _al_keyboard_common_names, sizeof key_names);
730
731 _al_mutex_lock(&s->lock);
732
733 /* HACK: XkbSetDetectableAutoRepeat is broken in some versions of X.Org */
734 Bool supported;
735 XkbSetDetectableAutoRepeat(s->x11display, True, &supported);
736 if (!supported) {
737 ALLEGRO_WARN("XkbSetDetectableAutoRepeat failed.\n");
738 }
739
740 #ifdef ALLEGRO_XWINDOWS_WITH_XIM
741 ALLEGRO_INFO("Using X Input Method.\n");
742
743 old_locale = setlocale(LC_CTYPE, NULL);
744 ALLEGRO_DEBUG("Old locale: %s\n", old_locale ? old_locale : "(null)");
745 if (old_locale) {
746 /* The return value of setlocale() may be clobbered by the next call
747 * to setlocale() so we must copy it.
748 */
749 old_locale = strdup(old_locale);
750 }
751
752 /* Otherwise we are restricted to ISO-8859-1 characters. */
753 if (setlocale(LC_CTYPE, "") == NULL) {
754 ALLEGRO_WARN("Could not set default locale.\n");
755 }
756
757 /* By default never use an input method as we are not prepared to
758 * handle any of them. This is still enough to get composed keys.
759 */
760 char const *modifiers = XSetLocaleModifiers("@im=none");
761 if (modifiers == NULL) {
762 ALLEGRO_WARN("XSetLocaleModifiers failed.\n");
763 }
764
765 xim = XOpenIM(s->x11display, NULL, NULL, NULL);
766 if (xim == NULL) {
767 ALLEGRO_WARN("XOpenIM failed.\n");
768 }
769
770 if (old_locale) {
771 ALLEGRO_DEBUG("Restoring old locale: %s\n", old_locale);
772 setlocale(LC_CTYPE, old_locale);
773 free(old_locale);
774 }
775
776 if (xim) {
777 imvalret = XGetIMValues(xim, XNQueryInputStyle, &xim_styles, NULL);
778 if (imvalret != NULL || xim_styles == NULL) {
779 ALLEGRO_WARN("Input method doesn't support any styles.\n");
780 }
781
782 if (xim_styles) {
783 xim_style = 0;
784 for (i = 0; i < xim_styles->count_styles; i++) {
785 if (xim_styles->supported_styles[i] ==
786 (XIMPreeditNothing | XIMStatusNothing)) {
787 xim_style = xim_styles->supported_styles[i];
788 break;
789 }
790 }
791
792 if (xim_style == 0) {
793 ALLEGRO_WARN("Input method doesn't support the style we support.\n");
794 }
795 else {
796 ALLEGRO_INFO("Input method style = %ld\n", xim_style);
797 }
798 XFree(xim_styles);
799 }
800 }
801
802 if (xim && xim_style) {
803 xic = XCreateIC(xim,
804 XNInputStyle, xim_style,
805 NULL);
806 if (xic == NULL) {
807 ALLEGRO_WARN("XCreateIC failed.\n");
808 }
809 else {
810 ALLEGRO_INFO("XCreateIC succeeded.\n");
811 }
812
813 /* In case al_install_keyboard() is called when there already is
814 * a display, we set it as client window.
815 */
816 ALLEGRO_DISPLAY *display = al_get_current_display();
817 ALLEGRO_DISPLAY_XGLX *display_glx = (void *)display;
818 if (display_glx && xic)
819 XSetICValues(xic, XNClientWindow, display_glx->window, NULL);
820 }
821 #endif
822
823 if (!_al_xwin_get_keyboard_mapping()) {
824 return 1;
825 }
826
827 _al_mutex_unlock(&s->lock);
828
829 xkeyboard_installed = 1;
830
831 return 0;
832 }
833
834
835
836 /* x_keyboard_exit
837 * Shut down the X11 keyboard driver.
838 */
839 static void x_keyboard_exit(void)
840 {
841 if (!xkeyboard_installed)
842 return;
843 xkeyboard_installed = 0;
844
845 ALLEGRO_SYSTEM_XGLX *s = (void *)al_get_system_driver();
846
847 _al_mutex_lock(&s->lock);
848
849 #ifdef ALLEGRO_XWINDOWS_WITH_XIM
850
851 if (xic) {
852 XDestroyIC(xic);
853 xic = NULL;
854 }
855
856 if (xim) {
857 XCloseIM(xim);
858 xim = NULL;
859 }
860
861 #endif
862
863 if (xmodmap) {
864 XFreeModifiermap(xmodmap);
865 xmodmap = NULL;
866 }
867
868 if (keysyms) {
869 XFree(keysyms);
870 keysyms = NULL;
871 }
872
873 _al_mutex_unlock(&s->lock);
874 }
875
876
877
878 /*----------------------------------------------------------------------
879 *
880 * Supports for new API. For now this code is kept separate from the
881 * above to ease merging changes made in the trunk.
882 *
883 */
884
885
886 /* forward declarations */
887 static bool xkeybd_init_keyboard(void);
888 static void xkeybd_exit_keyboard(void);
889 static ALLEGRO_KEYBOARD *xkeybd_get_keyboard(void);
890 static bool xkeybd_set_keyboard_leds(int leds);
891 static const char *xkeybd_keycode_to_name(int keycode);
892 static void xkeybd_get_keyboard_state(ALLEGRO_KEYBOARD_STATE *ret_state);
893 static void xkeybd_clear_keyboard_state(void);
894
895
896
897 /* the driver vtable */
898 #define KEYDRV_XWIN AL_ID('X','W','I','N')
899
900 static ALLEGRO_KEYBOARD_DRIVER keydrv_xwin =
901 {
902 KEYDRV_XWIN,
903 "",
904 "",
905 "X11 keyboard",
906 xkeybd_init_keyboard,
907 xkeybd_exit_keyboard,
908 xkeybd_get_keyboard,
909 xkeybd_set_keyboard_leds,
910 xkeybd_keycode_to_name,
911 xkeybd_get_keyboard_state,
912 xkeybd_clear_keyboard_state
913 };
914
915
916
917 ALLEGRO_KEYBOARD_DRIVER *_al_xwin_keyboard_driver(void)
918 {
919 return &keydrv_xwin;
920 }
921
922
923
924 /* xkeybd_init_keyboard:
925 * Initialise the driver.
926 */
927 static bool xkeybd_init_keyboard(void)
928 {
929 if (x_keyboard_init() != 0)
930 return false;
931
932 memset(&the_keyboard, 0, sizeof the_keyboard);
933
934 _al_event_source_init(&the_keyboard.parent.es);
935
936 the_keyboard.three_finger_flag = true;
937
938 const char *value = al_get_config_value(al_get_system_config(),
939 "keyboard", "enable_three_finger_exit");
940 if (value) {
941 the_keyboard.three_finger_flag = !strncmp(value, "true", 4);
942 }
943 ALLEGRO_DEBUG("Three finger flag enabled: %s\n",
944 the_keyboard.three_finger_flag ? "true" : "false");
945
946 //_xwin_keydrv_set_leds(_key_shifts);
947
948 /* Get the pid, which we use for the three finger salute */
949 main_pid = getpid();
950
951 return true;
952 }
953
954
955
956 /* xkeybd_exit_keyboard:
957 * Shut down the keyboard driver.
958 */
959 static void xkeybd_exit_keyboard(void)
960 {
961 x_keyboard_exit();
962
963 _al_event_source_free(&the_keyboard.parent.es);
964 }
965
966
967
968 /* xkeybd_get_keyboard:
969 * Returns the address of a ALLEGRO_KEYBOARD structure representing the keyboard.
970 */
971 static ALLEGRO_KEYBOARD *xkeybd_get_keyboard(void)
972 {
973 return (ALLEGRO_KEYBOARD *)&the_keyboard;
974 }
975
976
977
978 /* xkeybd_set_keyboard_leds:
979 * Updates the LED state.
980 */
981 static bool xkeybd_set_keyboard_leds(int leds)
982 {
983 x_set_leds(leds);
984 return true;
985 }
986
987
988
989 /* xkeybd_keycode_to_name:
990 * Converts the given keycode to a description of the key.
991 */
992 static const char *xkeybd_keycode_to_name(int keycode)
993 {
994 return x_scancode_to_name(keycode);
995 }
996
997
998
999 /* xkeybd_get_keyboard_state:
1000 * Copy the current keyboard state into RET_STATE, with any necessary locking.
1001 */
1002 static void xkeybd_get_keyboard_state(ALLEGRO_KEYBOARD_STATE *ret_state)
1003 {
1004 _al_event_source_lock(&the_keyboard.parent.es);
1005 {
1006 *ret_state = the_keyboard.state;
1007 }
1008 _al_event_source_unlock(&the_keyboard.parent.es);
1009 }
1010
1011
1012
1013 /* xkeybd_get_keyboard_state:
1014 * Clear the current keyboard state, with any necessary locking.
1015 */
1016 static void xkeybd_clear_keyboard_state(void)
1017 {
1018 _al_event_source_lock(&the_keyboard.parent.es);
1019 {
1020 last_press_code = -1;
1021 memset(&the_keyboard.state, 0, sizeof(the_keyboard.state));
1022 }
1023 _al_event_source_unlock(&the_keyboard.parent.es);
1024 }
1025
1026
1027
1028 /* handle_key_press: [bgman thread]
1029 * Hook for the X event dispatcher to handle key presses.
1030 * The caller must lock the X-display.
1031 */
1032 static void handle_key_press(int mycode, int unichar, int filtered,
1033 unsigned int modifiers, ALLEGRO_DISPLAY *display)
1034 {
1035 bool is_repeat;
1036
1037 is_repeat = (last_press_code == mycode);
1038 if (mycode > 0)
1039 last_press_code = mycode;
1040
1041 _al_event_source_lock(&the_keyboard.parent.es);
1042 {
1043 /* Update the key_down array. */
1044 _AL_KEYBOARD_STATE_SET_KEY_DOWN(the_keyboard.state, mycode);
1045
1046 /* Generate the events if necessary. */
1047 if (_al_event_source_needs_to_generate_event(&the_keyboard.parent.es)) {
1048 ALLEGRO_EVENT event;
1049
1050 event.keyboard.type = ALLEGRO_EVENT_KEY_DOWN;
1051 event.keyboard.timestamp = al_get_time();
1052 event.keyboard.display = display;
1053 event.keyboard.keycode = last_press_code;
1054 event.keyboard.unichar = 0;
1055 event.keyboard.modifiers = modifiers;
1056 event.keyboard.repeat = false;
1057
1058 /* Don't send KEY_DOWN for non-physical key events. */
1059 if (mycode > 0 && !is_repeat) {
1060 _al_event_source_emit_event(&the_keyboard.parent.es, &event);
1061 }
1062
1063 /* Don't send KEY_CHAR for events filtered by an input method,
1064 * nor modifier keys.
1065 */
1066 if (!filtered && mycode < ALLEGRO_KEY_MODIFIERS) {
1067 event.keyboard.type = ALLEGRO_EVENT_KEY_CHAR;
1068 event.keyboard.unichar = unichar;
1069 event.keyboard.modifiers = modifiers;
1070 event.keyboard.repeat = is_repeat;
1071 _al_event_source_emit_event(&the_keyboard.parent.es, &event);
1072 }
1073 }
1074 }
1075 _al_event_source_unlock(&the_keyboard.parent.es);
1076
1077 // FIXME?
1078 #ifndef ALLEGRO_RASPBERRYPI
1079 /* Toggle mouse grab key. The system driver should not be locked here. */
1080 if (last_press_code && !is_repeat) {
1081 ALLEGRO_SYSTEM_XGLX *system = (void *)al_get_system_driver();
1082 if (system->toggle_mouse_grab_keycode &&
1083 system->toggle_mouse_grab_keycode == mycode &&
1084 (modifiers & system->toggle_mouse_grab_modifiers)
1085 == system->toggle_mouse_grab_modifiers)
1086 {
1087 if (system->mouse_grab_display == display)
1088 al_ungrab_mouse();
1089 else
1090 al_grab_mouse(display);
1091 }
1092 }
1093 #endif
1094
1095 /* Exit by Ctrl-Alt-End. */
1096 if ((the_keyboard.three_finger_flag)
1097 && ((mycode == ALLEGRO_KEY_DELETE) || (mycode == ALLEGRO_KEY_END))
1098 && (modifiers & ALLEGRO_KEYMOD_CTRL)
1099 && (modifiers & (ALLEGRO_KEYMOD_ALT | ALLEGRO_KEYMOD_ALTGR)))
1100 {
1101 ALLEGRO_WARN("Three finger combo detected. SIGTERMing "
1102 "pid %d\n", main_pid);
1103 kill(main_pid, SIGTERM);
1104 }
1105 }
1106
1107
1108
1109 /* handle_key_release: [bgman thread]
1110 * Hook for the X event dispatcher to handle key releases.
1111 * The caller must lock the X-display.
1112 */
1113 static void handle_key_release(int mycode, unsigned int modifiers, ALLEGRO_DISPLAY *display)
1114 {
1115 if (last_press_code == mycode)
1116 last_press_code = -1;
1117
1118 _al_event_source_lock(&the_keyboard.parent.es);
1119 {
1120 /* Update the key_down array. */
1121 _AL_KEYBOARD_STATE_CLEAR_KEY_DOWN(the_keyboard.state, mycode);
1122
1123 /* Generate the release event if necessary. */
1124 if (_al_event_source_needs_to_generate_event(&the_keyboard.parent.es)) {
1125 ALLEGRO_EVENT event;
1126 event.keyboard.type = ALLEGRO_EVENT_KEY_UP;
1127 event.keyboard.timestamp = al_get_time();
1128 event.keyboard.display = display;
1129 event.keyboard.keycode = mycode;
1130 event.keyboard.unichar = 0;
1131 event.keyboard.modifiers = modifiers;
1132 _al_event_source_emit_event(&the_keyboard.parent.es, &event);
1133 }
1134 }
1135 _al_event_source_unlock(&the_keyboard.parent.es);
1136 }
1137
1138
1139 /* vim: set sts=3 sw=3 et: */