VirtualBox

source: vbox/trunk/src/VBox/Frontends/Common/VBoxKeyboard/keyboard.c@ 99825

Last change on this file since 99825 was 99825, checked in by vboxsync, 19 months ago

FE/Qt: bugref:10407. Adding an getter to UICommon to return display server type.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 25.4 KB
Line 
1/* $Id: keyboard.c 99825 2023-05-17 12:35:01Z vboxsync $ */
2/** @file
3 * VBox/Frontends/Common - X11 keyboard handler library.
4 */
5
6/* This code is originally from the Wine project. */
7
8/*
9 * X11 keyboard driver
10 *
11 * Copyright 1993 Bob Amstadt
12 * Copyright 1996 Albrecht Kleine
13 * Copyright 1997 David Faure
14 * Copyright 1998 Morten Welinder
15 * Copyright 1998 Ulrich Weigand
16 * Copyright 1999 Ove K�ven
17 *
18 * This library is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU Lesser General Public
20 * License as published by the Free Software Foundation; either
21 * version 2.1 of the License, or (at your option) any later version.
22 *
23 * This library is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 * Lesser General Public License for more details.
27 *
28 * You should have received a copy of the GNU Lesser General Public
29 * License along with this library; if not, write to the Free Software
30 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
31 */
32
33/*
34 * Oracle LGPL Disclaimer: For the avoidance of doubt, except that if any license choice
35 * other than GPL or LGPL is available it will apply instead, Oracle elects to use only
36 * the Lesser General Public License version 2.1 (LGPLv2) at this time for any software where
37 * a choice of LGPL license versions is made available with the language indicating
38 * that LGPLv2 or any later version may be used, or where a choice of which version
39 * of the LGPL is applied is otherwise unspecified.
40 */
41
42#include <X11/Xatom.h>
43#include <X11/keysym.h>
44#include <X11/XKBlib.h>
45#include <X11/Xlib.h>
46#include <X11/Xresource.h>
47#include <X11/Xutil.h>
48
49#include <ctype.h>
50#include <stdarg.h>
51#include <string.h>
52#include <stdlib.h>
53#include <stdio.h>
54
55#include <VBox/VBoxKeyboard.h>
56
57/* VBoxKeyboard uses the deprecated XKeycodeToKeysym(3) API, but uses it safely.
58 */
59#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
60
61#define KEYC2SCAN_SIZE 256
62
63/**
64 * Array containing the current mapping of keycodes to scan codes, detected
65 * using the keyboard layout algorithm in X11DRV_InitKeyboardByLayout.
66 */
67static unsigned keyc2scan[KEYC2SCAN_SIZE];
68/** Whether to output basic debugging information to standard output */
69static int log_kb_1 = 0;
70/** Whether to output verbose debugging information to standard output */
71static int log_kb_2 = 0;
72
73/** Output basic debugging information if wished */
74#define LOG_KB_1(a) \
75do { \
76 if (log_kb_1) { \
77 printf a; \
78 } \
79} while (0)
80
81/** Output verbose debugging information if wished */
82#define LOG_KB_2(a) \
83do { \
84 if (log_kb_2) { \
85 printf a; \
86 } \
87} while (0)
88
89/** Keyboard layout tables for guessing the current keyboard layout. */
90#include "keyboard-tables.h"
91
92/** Tables of keycode to scan code mappings for well-known keyboard types. */
93#include "keyboard-types.h"
94
95/**
96 * Translate a keycode in a key event to a scan code. If the keycode maps
97 * to a key symbol which is in the same place on all PC keyboards, look it
98 * up by symbol in one of our hard-coded translation tables. It it maps to
99 * a symbol which can be in a different place on different PC keyboards, look
100 * it up by keycode using either the lookup table which we constructed
101 * earlier, or using a hard-coded table if we know what type of keyboard is
102 * in use.
103 *
104 * @returns the scan code number, with 0x100 added for extended scan codes
105 * @param code the X11 key code to be looked up
106 */
107
108unsigned X11DRV_KeyEvent(Display *display, KeyCode code)
109{
110 unsigned scan;
111 KeySym keysym = XKeycodeToKeysym(display, code, 0);
112 scan = 0;
113 if (keyc2scan[code] == 0 && keysym != 0)
114 {
115 if ((keysym >> 8) == 0xFF) /* non-character key */
116 scan = nonchar_key_scan[keysym & 0xff];
117 else if ((keysym >> 8) == 0x1008FF) /* XFree86 vendor keys */
118 scan = xfree86_vendor_key_scan[keysym & 0xff];
119 else if ((keysym >> 8) == 0x1005FF) /* Sun keys */
120 scan = sun_key_scan[keysym & 0xff];
121 else if (keysym == 0x20) /* Spacebar */
122 scan = 0x39;
123 else if (keysym == 0xFE03) /* ISO level3 shift, aka AltGr */
124 scan = 0x138;
125 else if (keysym == 0xFE11) /* ISO level5 shift, R-Ctrl on */
126 scan = 0x11d; /* Canadian multilingual layout */
127 }
128 if (keyc2scan[code])
129 scan = keyc2scan[code];
130
131 return scan;
132}
133
134/**
135 * Called from X11DRV_InitKeyboardByLayout
136 * See the comments for that function for a description what this function
137 * does.
138 *
139 * @returns an index into the table of keyboard layouts, or 0 if absolutely
140 * nothing fits
141 * @param display pointer to the X11 display handle
142 * @param min_keycode the lowest value in use as a keycode on this server
143 * @param max_keycode the highest value in use as a keycode on this server
144 */
145static int
146X11DRV_KEYBOARD_DetectLayout (Display *display, unsigned min_keycode,
147 unsigned max_keycode)
148{
149 /** Counter variable for iterating through the keyboard layout tables. */
150 unsigned current;
151 /** The best candidate so far for the layout. */
152 unsigned kbd_layout = 0;
153 /** The number of matching keys in the current best candidate layout. */
154 unsigned max_score = 0;
155 /** The number of changes of scan-code direction in the current
156 best candidate. */
157 unsigned max_seq = 0;
158 /** Table for the current keycode to keysym mapping. */
159 char ckey[256][2];
160 /** Counter variable representing a keycode */
161 unsigned keyc;
162
163 /* Fill in our keycode to keysym mapping table. */
164 memset( ckey, 0, sizeof(ckey) );
165 for (keyc = min_keycode; keyc <= max_keycode; keyc++) {
166 /* get data for keycodes from X server */
167 KeySym keysym = XKeycodeToKeysym (display, keyc, 0);
168 /* We leave keycodes which will definitely not be in the lookup tables
169 marked with 0 so that we know that we know not to look them up when
170 we scan the tables. */
171 if ( (0xFF != (keysym >> 8)) /* Non-character key */
172 && (0x1008FF != (keysym >> 8)) /* XFree86 vendor keys */
173 && (0x1005FF != (keysym >> 8)) /* Sun keys */
174 && (0x20 != keysym) /* Spacebar */
175 && (0xFE03 != keysym) /* ISO level3 shift, aka AltGr */
176 ) {
177 ckey[keyc][0] = keysym & 0xFF;
178 ckey[keyc][1] = XKeycodeToKeysym(display, keyc, 1) & 0xFF;
179 }
180 }
181
182 /* Now scan the lookup tables, looking for one that is as close as
183 possible to our current keycode to keysym mapping. */
184 for (current = 0; main_key_tab[current].comment; current++) {
185 /** How many keys have matched so far in this layout? */
186 unsigned match = 0;
187 /** How many keys have not changed the direction? */
188 unsigned seq = 0;
189 /** Pointer to the layout we are currently comparing against. */
190 const char (*lkey)[MAIN_LEN][2] = main_key_tab[current].key;
191 /** For detecting dvorak layouts - in which direction do the server's
192 keycodes seem to be running? We count the number of times that
193 this direction changes as an additional hint as to how likely this
194 layout is to be the right one. */
195 int direction = 1;
196 /** The keycode of the last key that we matched. This is used to
197 determine the direction that the keycodes are running in. */
198 int pkey = -1;
199 LOG_KB_2(("Attempting to match against \"%s\"\n", main_key_tab[current].comment));
200 for (keyc = min_keycode; keyc <= max_keycode; keyc++) {
201 if (0 != ckey[keyc][0]) {
202 /** The candidate key in the current layout for this keycode. */
203 int key;
204 /** Does this key match? */
205 int ok = 0;
206 /* search for a match in layout table */
207 for (key = 0; (key < MAIN_LEN) && (0 == ok); key++) {
208 if ( ((*lkey)[key][0] == ckey[keyc][0])
209 && ((*lkey)[key][1] == ckey[keyc][1])
210 ) {
211 ok = 1;
212 }
213 }
214 /* count the matches and mismatches */
215 if (0 != ok) {
216 match++;
217 /* How well in sequence are the keys? For dvorak layouts. */
218 if (key > pkey) {
219 if (1 == direction) {
220 ++seq;
221 } else {
222 direction = -1;
223 }
224 }
225 if (key < pkey) {
226 if (1 != direction) {
227 ++seq;
228 } else {
229 direction = 1;
230 }
231 }
232 pkey = key;
233 } else {
234#ifdef DEBUG
235 /* print spaces instead of \0's */
236 char str[3] = " ";
237 if ((ckey[keyc][0] > 32) && (ckey[keyc][0] < 127)) {
238 str[0] = ckey[keyc][0];
239 }
240 if ((ckey[keyc][0] > 32) && (ckey[keyc][0] < 127)) {
241 str[0] = ckey[keyc][0];
242 }
243 LOG_KB_2(("Mismatch for keycode %u, keysym \"%s\" (0x%.2hx 0x%.2hx)\n",
244 keyc, str, ckey[keyc][0], ckey[keyc][1]));
245#endif /* DEBUG defined */
246 }
247 }
248 }
249 LOG_KB_2(("Matches=%u, seq=%u\n", match, seq));
250 if ( (match > max_score)
251 || ((match == max_score) && (seq > max_seq))
252 ) {
253 /* best match so far */
254 kbd_layout = current;
255 max_score = match;
256 max_seq = seq;
257 }
258 }
259 /* we're done, report results if necessary */
260 LOG_KB_1(("Detected layout is \"%s\", matches=%u, seq=%u\n",
261 main_key_tab[kbd_layout].comment, max_score, max_seq));
262 return kbd_layout;
263}
264
265/**
266 * Initialise the X11 keyboard driver by building up a table to convert X11
267 * keycodes to scan codes using a heuristic based on comparing the current
268 * keyboard map to known international keyboard layouts.
269 * The basic idea is to examine each key in the current layout to see which
270 * characters it produces in its normal and its "shifted" state, and to look
271 * for known keyboard layouts which it could belong to. We then guess the
272 * current layout based on the number of matches we find.
273 * One difficulty with this approach is so-called Dvorak layouts, which are
274 * identical to non-Dvorak layouts, but with the keys in a different order.
275 * To deal with this, we compare the different candidate layouts to see in
276 * which one the X11 keycodes would be most sequential and hope that they
277 * really are arranged more or less sequentially.
278 *
279 * The actual detection of the current layout is done in the sub-function
280 * X11DRV_KEYBOARD_DetectLayout. Once we have determined the layout, since we
281 * know which PC scan code corresponds to each key in the layout, we can use
282 * this information to associate the scan code with an X11 keycode, which is
283 * what the rest of this function does.
284 *
285 * @warning not re-entrant
286 * @returns 1 if the layout found was optimal, 0 if it was not. This is
287 * for diagnostic purposes
288 * @param display a pointer to the X11 display
289 */
290static unsigned
291X11DRV_InitKeyboardByLayout(Display *display)
292{
293 KeySym keysym;
294 unsigned scan;
295 int keyc, keyn;
296 const char (*lkey)[MAIN_LEN][2];
297 int min_keycode, max_keycode;
298 int kbd_layout;
299 unsigned matches = 0, entries = 0;
300
301 /* Should we log to standard output? */
302 if (NULL != getenv("LOG_KB_PRIMARY")) {
303 log_kb_1 = 1;
304 }
305 if (NULL != getenv("LOG_KB_SECONDARY")) {
306 log_kb_1 = 1;
307 log_kb_2 = 1;
308 }
309 XDisplayKeycodes(display, &min_keycode, &max_keycode);
310
311 /* according to the space this function is guaranteed to never return
312 * values for min_keycode < 8 and values for max_keycode > 255 */
313 if (min_keycode < 0)
314 min_keycode = 0;
315 if (max_keycode > 255)
316 max_keycode = 255;
317
318 /* Detect the keyboard layout */
319 kbd_layout = X11DRV_KEYBOARD_DetectLayout(display, min_keycode,
320 max_keycode);
321 lkey = main_key_tab[kbd_layout].key;
322
323 /* Now build a conversion array :
324 * keycode -> scancode + extended */
325
326 for (keyc = min_keycode; keyc <= max_keycode; keyc++)
327 {
328 keysym = XKeycodeToKeysym(display, keyc, 0);
329 scan = 0;
330 if (keysym) /* otherwise, keycode not used */
331 {
332 /* Skip over keysyms which we look up on the fly */
333 if ( (0xFF != (keysym >> 8)) /* Non-character key */
334 && (0x1008FF != (keysym >> 8)) /* XFree86 vendor keys */
335 && (0x1005FF != (keysym >> 8)) /* Sun keys */
336 && (0x20 != keysym) /* Spacebar */
337 && (0xFE03 != keysym) /* ISO level3 shift, aka AltGr */
338 ) {
339 unsigned found = 0;
340
341 /* we seem to need to search the layout-dependent scancodes */
342 char unshifted = keysym & 0xFF;
343 char shifted = XKeycodeToKeysym(display, keyc, 1) & 0xFF;
344 /* find a key which matches */
345 for (keyn = 0; (0 == found) && (keyn<MAIN_LEN); keyn++) {
346 if ( ((*lkey)[keyn][0] == unshifted)
347 && ((*lkey)[keyn][1] == shifted)
348 ) {
349 found = 1;
350 }
351 }
352 if (0 != found) {
353 /* got it */
354 scan = main_key_scan[keyn - 1];
355 /* We keep track of the number of keys that we found a
356 * match for to see if the layout is optimal or not.
357 * We ignore the 102nd key though (key number 48), since
358 * not all keyboards have it. */
359 if (keyn != 48)
360 ++matches;
361 }
362 if (0 == scan) {
363 /* print spaces instead of \0's */
364 char str[3] = " ";
365 if ((unshifted > 32) && (unshifted < 127)) {
366 str[0] = unshifted;
367 }
368 if ((shifted > 32) && (shifted < 127)) {
369 str[1] = shifted;
370 }
371 LOG_KB_1(("No match found for keycode %d, keysym \"%s\" (0x%x 0x%x)\n",
372 keyc, str, unshifted, shifted));
373 } else if ((keyc > 8) && (keyc < 97) && (keyc - scan != 8)) {
374 /* print spaces instead of \0's */
375 char str[3] = " ";
376 if ((unshifted > 32) && (unshifted < 127)) {
377 str[0] = unshifted;
378 }
379 if ((shifted > 32) && (shifted < 127)) {
380 str[1] = shifted;
381 }
382 LOG_KB_1(("Warning - keycode %d, keysym \"%s\" (0x%x 0x%x) was matched to scancode %u\n",
383 keyc, str, unshifted, shifted, scan));
384 }
385 }
386 }
387 keyc2scan[keyc] = scan;
388 printf("%d %u = %u\n", keyc, keyc2scan[keyc], scan);
389 } /* for */
390 /* Did we find a match for all keys in the layout? Count them first.
391 * Note that we skip the 102nd key, so that owners of 101 key keyboards
392 * don't get bogus messages about bad matches. */
393 for (entries = 0, keyn = 0; keyn < MAIN_LEN; ++keyn) {
394 if ( (0 != (*lkey)[keyn][0])
395 && (0 != (*lkey)[keyn][1])
396 && (keyn != 47) /* don't count the 102nd key */
397 ) {
398 ++entries;
399 }
400 }
401 LOG_KB_1(("Finished mapping keyboard, matches=%u, entries=%u (excluding 102nd key)\n", matches, entries));
402 if (matches != entries)
403 {
404 return 0;
405 }
406 return 1;
407}
408
409static int checkHostKeycode(unsigned hostCode, unsigned targetCode)
410{
411 if (!targetCode)
412 return 0;
413 if (hostCode && hostCode != targetCode)
414 return 0;
415 return 1;
416}
417
418static int compKBMaps(const keyboard_type *pHost, const keyboard_type *pTarget)
419{
420 if ( !pHost->lctrl && !pHost->capslock && !pHost->lshift && !pHost->tab
421 && !pHost->esc && !pHost->enter && !pHost->up && !pHost->down
422 && !pHost->left && !pHost->right && !pHost->f1 && !pHost->f2
423 && !pHost->f3 && !pHost->f4 && !pHost->f5 && !pHost->f6 && !pHost->f7
424 && !pHost->f8)
425 return 0;
426 /* This test is for the people who like to swap control and caps lock */
427 if ( ( !checkHostKeycode(pHost->lctrl, pTarget->lctrl)
428 || !checkHostKeycode(pHost->capslock, pTarget->capslock))
429 && ( !checkHostKeycode(pHost->lctrl, pTarget->capslock)
430 || !checkHostKeycode(pHost->capslock, pTarget->lctrl)))
431 return 0;
432 if ( !checkHostKeycode(pHost->lshift, pTarget->lshift)
433 || !checkHostKeycode(pHost->tab, pTarget->tab)
434 || !checkHostKeycode(pHost->esc, pTarget->esc)
435 || !checkHostKeycode(pHost->enter, pTarget->enter)
436 || !checkHostKeycode(pHost->up, pTarget->up)
437 || !checkHostKeycode(pHost->down, pTarget->down)
438 || !checkHostKeycode(pHost->left, pTarget->left)
439 || !checkHostKeycode(pHost->right, pTarget->right)
440 || !checkHostKeycode(pHost->f1, pTarget->f1)
441 || !checkHostKeycode(pHost->f2, pTarget->f2)
442 || !checkHostKeycode(pHost->f3, pTarget->f3)
443 || !checkHostKeycode(pHost->f4, pTarget->f4)
444 || !checkHostKeycode(pHost->f5, pTarget->f5)
445 || !checkHostKeycode(pHost->f6, pTarget->f6)
446 || !checkHostKeycode(pHost->f7, pTarget->f7)
447 || !checkHostKeycode(pHost->f8, pTarget->f8))
448 return 0;
449 return 1;
450}
451
452static int findHostKBInList(const keyboard_type *pHost,
453 const keyboard_type *pList, int cList)
454{
455 int i = 0;
456 for (; i < cList; ++i)
457 if (compKBMaps(pHost, &pList[i]))
458 return i;
459 return -1;
460}
461
462#ifdef DEBUG
463static void testFindHostKB(void)
464{
465 keyboard_type hostBasic =
466 { NULL, 1 /* lctrl */, 2, 3, 4, 5, 6, 7 /* up */, 8, 9, 10, 11 /* F1 */,
467 12, 13, 14, 15, 16, 17, 18 };
468 keyboard_type hostSwapCtrlCaps =
469 { NULL, 3 /* lctrl */, 2, 1, 4, 5, 6, 7 /* up */, 8, 9, 10, 11 /* F1 */,
470 12, 13, 14, 15, 16, 17, 18 };
471 keyboard_type hostEmpty =
472 { NULL, 0 /* lctrl */, 0, 0, 0, 0, 0, 0 /* up */, 0, 0, 0, 0 /* F1 */,
473 0, 0, 0, 0, 0, 0, 0 };
474 keyboard_type hostNearlyEmpty =
475 { NULL, 1 /* lctrl */, 0, 0, 0, 0, 0, 0 /* up */, 0, 0, 0, 0 /* F1 */,
476 0, 0, 0, 0, 0, 0, 18 };
477 keyboard_type hostNearlyRight =
478 { NULL, 20 /* lctrl */, 2, 3, 4, 5, 6, 7 /* up */, 8, 9, 10, 11 /* F1 */,
479 12, 13, 14, 15, 16, 17, 18 };
480 keyboard_type targetList[] = {
481 { NULL, 18 /* lctrl */, 17, 16, 15, 14, 13, 12 /* up */, 11, 10, 9,
482 8 /* F1 */, 7, 6, 5, 4, 3, 2, 1 },
483 { NULL, 1 /* lctrl */, 2, 3, 4, 5, 6, 7 /* up */, 8, 9, 10,
484 11 /* F1 */, 12, 13, 14, 15, 16, 17, 18 }
485 };
486
487 /* As we don't have assertions here, just printf. This should *really*
488 * never happen. */
489 if ( hostBasic.f8 != 18 || hostSwapCtrlCaps.f8 != 18
490 || hostNearlyEmpty.f8 != 18 || hostNearlyRight.f8 != 18
491 || targetList[0].f8 != 1 || targetList[1].f8 != 18)
492 printf("ERROR: testFindHostKB: bad structures\n");
493 if (findHostKBInList(&hostBasic, targetList, 2) != 1)
494 printf("ERROR: findHostKBInList failed to find a target in a list\n");
495 if (findHostKBInList(&hostSwapCtrlCaps, targetList, 2) != 1)
496 printf("ERROR: findHostKBInList failed on a ctrl-caps swapped map\n");
497 if (findHostKBInList(&hostEmpty, targetList, 2) != -1)
498 printf("ERROR: findHostKBInList accepted an empty host map\n");
499 if (findHostKBInList(&hostNearlyEmpty, targetList, 2) != 1)
500 printf("ERROR: findHostKBInList failed on a partly empty host map\n");
501 if (findHostKBInList(&hostNearlyRight, targetList, 2) != -1)
502 printf("ERROR: findHostKBInList failed to fail a wrong host map\n");
503}
504#endif
505
506static unsigned
507X11DRV_InitKeyboardByType(Display *display)
508{
509 keyboard_type hostKB;
510 int cMap;
511
512 hostKB.lctrl = XKeysymToKeycode(display, XK_Control_L);
513 hostKB.capslock = XKeysymToKeycode(display, XK_Caps_Lock);
514 hostKB.lshift = XKeysymToKeycode(display, XK_Shift_L);
515 hostKB.tab = XKeysymToKeycode(display, XK_Tab);
516 hostKB.esc = XKeysymToKeycode(display, XK_Escape);
517 hostKB.enter = XKeysymToKeycode(display, XK_Return);
518 hostKB.up = XKeysymToKeycode(display, XK_Up);
519 hostKB.down = XKeysymToKeycode(display, XK_Down);
520 hostKB.left = XKeysymToKeycode(display, XK_Left);
521 hostKB.right = XKeysymToKeycode(display, XK_Right);
522 hostKB.f1 = XKeysymToKeycode(display, XK_F1);
523 hostKB.f2 = XKeysymToKeycode(display, XK_F2);
524 hostKB.f3 = XKeysymToKeycode(display, XK_F3);
525 hostKB.f4 = XKeysymToKeycode(display, XK_F4);
526 hostKB.f5 = XKeysymToKeycode(display, XK_F5);
527 hostKB.f6 = XKeysymToKeycode(display, XK_F6);
528 hostKB.f7 = XKeysymToKeycode(display, XK_F7);
529 hostKB.f8 = XKeysymToKeycode(display, XK_F8);
530
531#ifdef DEBUG
532 testFindHostKB();
533#endif
534 cMap = findHostKBInList(&hostKB, main_keyboard_type_list,
535 sizeof(main_keyboard_type_list)
536 / sizeof(main_keyboard_type_list[0]));
537#ifdef DEBUG
538 /* Assertion */
539 if (sizeof(keyc2scan) != sizeof(main_keyboard_type_scans[cMap]))
540 {
541 printf("ERROR: keyc2scan array size doesn't match main_keyboard_type_scans[]!\n");
542 return 0;
543 }
544#endif
545 if (cMap >= 0)
546 {
547 memcpy(keyc2scan, main_keyboard_type_scans[cMap], sizeof(keyc2scan));
548 return 1;
549 }
550 return 0;
551}
552
553/**
554 * Checks for the XKB extension, and if it is found initialises the X11 keycode
555 * to XT scan code mapping by looking at the XKB names for each keycode. As it
556 * turns out that XKB can return an empty list we make sure that the list holds
557 * enough data to be useful to us.
558 */
559static unsigned
560X11DRV_InitKeyboardByXkb(Display *pDisplay)
561{
562 int major = XkbMajorVersion, minor = XkbMinorVersion;
563 XkbDescPtr pKBDesc;
564 unsigned cFound = 0;
565
566 if (!XkbLibraryVersion(&major, &minor))
567 return 0;
568 if (!XkbQueryExtension(pDisplay, NULL, NULL, &major, &minor, NULL))
569 return 0;
570 pKBDesc = XkbGetKeyboard(pDisplay, XkbAllComponentsMask, XkbUseCoreKbd);
571 if (!pKBDesc)
572 return 0;
573 if (XkbGetNames(pDisplay, XkbKeyNamesMask, pKBDesc) != Success)
574 return 0;
575 {
576 unsigned i, j;
577
578 memset(keyc2scan, 0, sizeof(keyc2scan));
579 for (i = pKBDesc->min_key_code; i < pKBDesc->max_key_code; ++i)
580 for (j = 0; j < sizeof(xkbMap) / sizeof(xkbMap[0]); ++j)
581 if (!memcmp(xkbMap[j].cszName,
582 &pKBDesc->names->keys->name[i * XKB_NAME_SIZE],
583 XKB_NAME_SIZE))
584 {
585 keyc2scan[i] = xkbMap[j].uScan;
586 ++cFound;
587 break;
588 }
589 }
590 XkbFreeNames(pKBDesc, XkbKeyNamesMask, True);
591 XkbFreeKeyboard(pKBDesc, XkbAllComponentsMask, True);
592 return cFound >= 45 ? 1 : 0;
593}
594
595/**
596 * Initialise the X11 keyboard driver by finding which X11 keycodes correspond
597 * to which PC scan codes. If the keyboard being used is not a PC keyboard,
598 * the X11 keycodes will be mapped to the scan codes which the equivalent keys
599 * on a PC keyboard would use.
600 *
601 * We use two algorithms to try to determine the mapping. See the comments
602 * attached to the two algorithm functions (X11DRV_InitKeyboardByLayout and
603 * X11DRV_InitKeyboardByType) for descriptions of the algorithms used. Both
604 * functions tell us on return whether they think that they have correctly
605 * determined the mapping. If both functions claim to have determined the
606 * mapping correctly, we prefer the second (ByType). However, if neither does
607 * then we prefer the first (ByLayout), as it produces a fuzzy result which is
608 * still likely to be partially correct.
609 *
610 * @warning not re-entrant
611 * @returns 1 if the layout found was optimal, 0 if it was not. This is
612 * for diagnostic purposes
613 * @param display a pointer to the X11 display
614 * @param byLayoutOK diagnostic - set to one if detection by layout
615 * succeeded, and to 0 otherwise
616 * @param byTypeOK diagnostic - set to one if detection by type
617 * succeeded, and to 0 otherwise
618 * @param byXkbOK diagnostic - set to one if detection using XKB
619 * succeeded, and to 0 otherwise
620 * @param remapScancode array of tuples that remap the keycode (first
621 * part) to a scancode (second part)
622 * @note Xkb takes precedence over byType takes precedence over byLayout,
623 * for anyone who wants to log information about which method is in
624 * use. byLayout is the fallback, as it is likely to be partly usable
625 * even if it doesn't initialise correctly.
626 */
627unsigned X11DRV_InitKeyboard(Display *display, unsigned *byLayoutOK,
628 unsigned *byTypeOK, unsigned *byXkbOK,
629 int (*remapScancodes)[2])
630{
631 unsigned byLayout, byType, byXkb;
632
633 byLayout = X11DRV_InitKeyboardByLayout(display);
634 if (byLayoutOK)
635 *byLayoutOK = byLayout;
636
637 byType = X11DRV_InitKeyboardByType(display);
638 if (byTypeOK)
639 *byTypeOK = byType;
640
641 byXkb = X11DRV_InitKeyboardByXkb(display);
642 if (byXkbOK)
643 *byXkbOK = byXkb;
644
645 /* Fall back to the one which did work. */
646 if (!byXkb)
647 {
648 if (byType)
649 X11DRV_InitKeyboardByType(display);
650 else
651 X11DRV_InitKeyboardByLayout(display);
652 }
653
654 /* Remap keycodes after initialization. Remapping stops after an
655 identity mapping is seen */
656 if (remapScancodes != NULL)
657 for (; (*remapScancodes)[0] != (*remapScancodes)[1]; remapScancodes++)
658 keyc2scan[(*remapScancodes)[0]] = (*remapScancodes)[1];
659
660 return (byLayout || byType || byXkb) ? 1 : 0;
661}
662
663/**
664 * Returns the keycode to scancode array
665 */
666unsigned *X11DRV_getKeyc2scan(void)
667{
668 return keyc2scan;
669}
670
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette