VirtualBox

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

最後變更 在這個檔案從37080是 36335,由 vboxsync 提交於 14 年 前

FE/Common/VBoxKeyboard: some release logging

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

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