VirtualBox

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

Last change on this file since 67954 was 62063, checked in by vboxsync, 8 years ago

FE/Common/VBoxKeyboard: Fix printf signed/unsigned mismatch.

  • 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 62063 2016-07-06 15:12:35Z 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 } /* for */
389 /* Did we find a match for all keys in the layout? Count them first.
390 * Note that we skip the 102nd key, so that owners of 101 key keyboards
391 * don't get bogus messages about bad matches. */
392 for (entries = 0, keyn = 0; keyn < MAIN_LEN; ++keyn) {
393 if ( (0 != (*lkey)[keyn][0])
394 && (0 != (*lkey)[keyn][1])
395 && (keyn != 47) /* don't count the 102nd key */
396 ) {
397 ++entries;
398 }
399 }
400 LOG_KB_1(("Finished mapping keyboard, matches=%u, entries=%u (excluding 102nd key)\n", matches, entries));
401 if (matches != entries)
402 {
403 return 0;
404 }
405 return 1;
406}
407
408static int checkHostKeycode(unsigned hostCode, unsigned targetCode)
409{
410 if (!targetCode)
411 return 0;
412 if (hostCode && hostCode != targetCode)
413 return 0;
414 return 1;
415}
416
417static int compKBMaps(const keyboard_type *pHost, const keyboard_type *pTarget)
418{
419 if ( !pHost->lctrl && !pHost->capslock && !pHost->lshift && !pHost->tab
420 && !pHost->esc && !pHost->enter && !pHost->up && !pHost->down
421 && !pHost->left && !pHost->right && !pHost->f1 && !pHost->f2
422 && !pHost->f3 && !pHost->f4 && !pHost->f5 && !pHost->f6 && !pHost->f7
423 && !pHost->f8)
424 return 0;
425 /* This test is for the people who like to swap control and caps lock */
426 if ( ( !checkHostKeycode(pHost->lctrl, pTarget->lctrl)
427 || !checkHostKeycode(pHost->capslock, pTarget->capslock))
428 && ( !checkHostKeycode(pHost->lctrl, pTarget->capslock)
429 || !checkHostKeycode(pHost->capslock, pTarget->lctrl)))
430 return 0;
431 if ( !checkHostKeycode(pHost->lshift, pTarget->lshift)
432 || !checkHostKeycode(pHost->tab, pTarget->tab)
433 || !checkHostKeycode(pHost->esc, pTarget->esc)
434 || !checkHostKeycode(pHost->enter, pTarget->enter)
435 || !checkHostKeycode(pHost->up, pTarget->up)
436 || !checkHostKeycode(pHost->down, pTarget->down)
437 || !checkHostKeycode(pHost->left, pTarget->left)
438 || !checkHostKeycode(pHost->right, pTarget->right)
439 || !checkHostKeycode(pHost->f1, pTarget->f1)
440 || !checkHostKeycode(pHost->f2, pTarget->f2)
441 || !checkHostKeycode(pHost->f3, pTarget->f3)
442 || !checkHostKeycode(pHost->f4, pTarget->f4)
443 || !checkHostKeycode(pHost->f5, pTarget->f5)
444 || !checkHostKeycode(pHost->f6, pTarget->f6)
445 || !checkHostKeycode(pHost->f7, pTarget->f7)
446 || !checkHostKeycode(pHost->f8, pTarget->f8))
447 return 0;
448 return 1;
449}
450
451static int findHostKBInList(const keyboard_type *pHost,
452 const keyboard_type *pList, int cList)
453{
454 int i = 0;
455 for (; i < cList; ++i)
456 if (compKBMaps(pHost, &pList[i]))
457 return i;
458 return -1;
459}
460
461#ifdef DEBUG
462static void testFindHostKB(void)
463{
464 keyboard_type hostBasic =
465 { NULL, 1 /* lctrl */, 2, 3, 4, 5, 6, 7 /* up */, 8, 9, 10, 11 /* F1 */,
466 12, 13, 14, 15, 16, 17, 18 };
467 keyboard_type hostSwapCtrlCaps =
468 { NULL, 3 /* lctrl */, 2, 1, 4, 5, 6, 7 /* up */, 8, 9, 10, 11 /* F1 */,
469 12, 13, 14, 15, 16, 17, 18 };
470 keyboard_type hostEmpty =
471 { NULL, 0 /* lctrl */, 0, 0, 0, 0, 0, 0 /* up */, 0, 0, 0, 0 /* F1 */,
472 0, 0, 0, 0, 0, 0, 0 };
473 keyboard_type hostNearlyEmpty =
474 { NULL, 1 /* lctrl */, 0, 0, 0, 0, 0, 0 /* up */, 0, 0, 0, 0 /* F1 */,
475 0, 0, 0, 0, 0, 0, 18 };
476 keyboard_type hostNearlyRight =
477 { NULL, 20 /* lctrl */, 2, 3, 4, 5, 6, 7 /* up */, 8, 9, 10, 11 /* F1 */,
478 12, 13, 14, 15, 16, 17, 18 };
479 keyboard_type targetList[] = {
480 { NULL, 18 /* lctrl */, 17, 16, 15, 14, 13, 12 /* up */, 11, 10, 9,
481 8 /* F1 */, 7, 6, 5, 4, 3, 2, 1 },
482 { NULL, 1 /* lctrl */, 2, 3, 4, 5, 6, 7 /* up */, 8, 9, 10,
483 11 /* F1 */, 12, 13, 14, 15, 16, 17, 18 }
484 };
485
486 /* As we don't have assertions here, just printf. This should *really*
487 * never happen. */
488 if ( hostBasic.f8 != 18 || hostSwapCtrlCaps.f8 != 18
489 || hostNearlyEmpty.f8 != 18 || hostNearlyRight.f8 != 18
490 || targetList[0].f8 != 1 || targetList[1].f8 != 18)
491 printf("ERROR: testFindHostKB: bad structures\n");
492 if (findHostKBInList(&hostBasic, targetList, 2) != 1)
493 printf("ERROR: findHostKBInList failed to find a target in a list\n");
494 if (findHostKBInList(&hostSwapCtrlCaps, targetList, 2) != 1)
495 printf("ERROR: findHostKBInList failed on a ctrl-caps swapped map\n");
496 if (findHostKBInList(&hostEmpty, targetList, 2) != -1)
497 printf("ERROR: findHostKBInList accepted an empty host map\n");
498 if (findHostKBInList(&hostNearlyEmpty, targetList, 2) != 1)
499 printf("ERROR: findHostKBInList failed on a partly empty host map\n");
500 if (findHostKBInList(&hostNearlyRight, targetList, 2) != -1)
501 printf("ERROR: findHostKBInList failed to fail a wrong host map\n");
502}
503#endif
504
505static unsigned
506X11DRV_InitKeyboardByType(Display *display)
507{
508 keyboard_type hostKB;
509 int cMap;
510
511 hostKB.lctrl = XKeysymToKeycode(display, XK_Control_L);
512 hostKB.capslock = XKeysymToKeycode(display, XK_Caps_Lock);
513 hostKB.lshift = XKeysymToKeycode(display, XK_Shift_L);
514 hostKB.tab = XKeysymToKeycode(display, XK_Tab);
515 hostKB.esc = XKeysymToKeycode(display, XK_Escape);
516 hostKB.enter = XKeysymToKeycode(display, XK_Return);
517 hostKB.up = XKeysymToKeycode(display, XK_Up);
518 hostKB.down = XKeysymToKeycode(display, XK_Down);
519 hostKB.left = XKeysymToKeycode(display, XK_Left);
520 hostKB.right = XKeysymToKeycode(display, XK_Right);
521 hostKB.f1 = XKeysymToKeycode(display, XK_F1);
522 hostKB.f2 = XKeysymToKeycode(display, XK_F2);
523 hostKB.f3 = XKeysymToKeycode(display, XK_F3);
524 hostKB.f4 = XKeysymToKeycode(display, XK_F4);
525 hostKB.f5 = XKeysymToKeycode(display, XK_F5);
526 hostKB.f6 = XKeysymToKeycode(display, XK_F6);
527 hostKB.f7 = XKeysymToKeycode(display, XK_F7);
528 hostKB.f8 = XKeysymToKeycode(display, XK_F8);
529
530#ifdef DEBUG
531 testFindHostKB();
532#endif
533 cMap = findHostKBInList(&hostKB, main_keyboard_type_list,
534 sizeof(main_keyboard_type_list)
535 / sizeof(main_keyboard_type_list[0]));
536#ifdef DEBUG
537 /* Assertion */
538 if (sizeof(keyc2scan) != sizeof(main_keyboard_type_scans[cMap]))
539 {
540 printf("ERROR: keyc2scan array size doesn't match main_keyboard_type_scans[]!\n");
541 return 0;
542 }
543#endif
544 if (cMap >= 0)
545 {
546 memcpy(keyc2scan, main_keyboard_type_scans[cMap], sizeof(keyc2scan));
547 return 1;
548 }
549 return 0;
550}
551
552/**
553 * Checks for the XKB extension, and if it is found initialises the X11 keycode
554 * to XT scan code mapping by looking at the XKB names for each keycode. As it
555 * turns out that XKB can return an empty list we make sure that the list holds
556 * enough data to be useful to us.
557 */
558static unsigned
559X11DRV_InitKeyboardByXkb(Display *pDisplay)
560{
561 int major = XkbMajorVersion, minor = XkbMinorVersion;
562 XkbDescPtr pKBDesc;
563 unsigned cFound = 0;
564
565 if (!XkbLibraryVersion(&major, &minor))
566 return 0;
567 if (!XkbQueryExtension(pDisplay, NULL, NULL, &major, &minor, NULL))
568 return 0;
569 pKBDesc = XkbGetKeyboard(pDisplay, XkbAllComponentsMask, XkbUseCoreKbd);
570 if (!pKBDesc)
571 return 0;
572 if (XkbGetNames(pDisplay, XkbKeyNamesMask, pKBDesc) != Success)
573 return 0;
574 {
575 unsigned i, j;
576
577 memset(keyc2scan, 0, sizeof(keyc2scan));
578 for (i = pKBDesc->min_key_code; i < pKBDesc->max_key_code; ++i)
579 for (j = 0; j < sizeof(xkbMap) / sizeof(xkbMap[0]); ++j)
580 if (!memcmp(xkbMap[j].cszName,
581 &pKBDesc->names->keys->name[i * XKB_NAME_SIZE],
582 XKB_NAME_SIZE))
583 {
584 keyc2scan[i] = xkbMap[j].uScan;
585 ++cFound;
586 break;
587 }
588 }
589 XkbFreeNames(pKBDesc, XkbKeyNamesMask, True);
590 XkbFreeKeyboard(pKBDesc, XkbAllComponentsMask, True);
591 return cFound >= 45 ? 1 : 0;
592}
593
594/**
595 * Initialise the X11 keyboard driver by finding which X11 keycodes correspond
596 * to which PC scan codes. If the keyboard being used is not a PC keyboard,
597 * the X11 keycodes will be mapped to the scan codes which the equivalent keys
598 * on a PC keyboard would use.
599 *
600 * We use two algorithms to try to determine the mapping. See the comments
601 * attached to the two algorithm functions (X11DRV_InitKeyboardByLayout and
602 * X11DRV_InitKeyboardByType) for descriptions of the algorithms used. Both
603 * functions tell us on return whether they think that they have correctly
604 * determined the mapping. If both functions claim to have determined the
605 * mapping correctly, we prefer the second (ByType). However, if neither does
606 * then we prefer the first (ByLayout), as it produces a fuzzy result which is
607 * still likely to be partially correct.
608 *
609 * @warning not re-entrant
610 * @returns 1 if the layout found was optimal, 0 if it was not. This is
611 * for diagnostic purposes
612 * @param display a pointer to the X11 display
613 * @param byLayoutOK diagnostic - set to one if detection by layout
614 * succeeded, and to 0 otherwise
615 * @param byTypeOK diagnostic - set to one if detection by type
616 * succeeded, and to 0 otherwise
617 * @param byXkbOK diagnostic - set to one if detection using XKB
618 * succeeded, and to 0 otherwise
619 * @param remapScancode array of tuples that remap the keycode (first
620 * part) to a scancode (second part)
621 * @note Xkb takes precedence over byType takes precedence over byLayout,
622 * for anyone who wants to log information about which method is in
623 * use. byLayout is the fallback, as it is likely to be partly usable
624 * even if it doesn't initialise correctly.
625 */
626unsigned X11DRV_InitKeyboard(Display *display, unsigned *byLayoutOK,
627 unsigned *byTypeOK, unsigned *byXkbOK,
628 int (*remapScancodes)[2])
629{
630 unsigned byLayout, byType, byXkb;
631
632 byLayout = X11DRV_InitKeyboardByLayout(display);
633 if (byLayoutOK)
634 *byLayoutOK = byLayout;
635
636 byType = X11DRV_InitKeyboardByType(display);
637 if (byTypeOK)
638 *byTypeOK = byType;
639
640 byXkb = X11DRV_InitKeyboardByXkb(display);
641 if (byXkbOK)
642 *byXkbOK = byXkb;
643
644 /* Fall back to the one which did work. */
645 if (!byXkb)
646 {
647 if (byType)
648 X11DRV_InitKeyboardByType(display);
649 else
650 X11DRV_InitKeyboardByLayout(display);
651 }
652
653 /* Remap keycodes after initialization. Remapping stops after an
654 identity mapping is seen */
655 if (remapScancodes != NULL)
656 for (; (*remapScancodes)[0] != (*remapScancodes)[1]; remapScancodes++)
657 keyc2scan[(*remapScancodes)[0]] = (*remapScancodes)[1];
658
659 return (byLayout || byType || byXkb) ? 1 : 0;
660}
661
662/**
663 * Returns the keycode to scancode array
664 */
665unsigned *X11DRV_getKeyc2scan(void)
666{
667 return keyc2scan;
668}
669
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