VirtualBox

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

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

FE/Common/VBoxKeyboard: try to use XKB to determine the keyboard layout as well, and by default

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

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