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