1 | /* $Id: keyboard.c 29654 2010-05-18 21:37:32Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | *
|
---|
4 | * VBox frontends: Qt GUI ("VirtualBox"):
|
---|
5 | * X11 keyboard handler library
|
---|
6 | */
|
---|
7 |
|
---|
8 | /* This code is originally from the Wine project. */
|
---|
9 |
|
---|
10 | /*
|
---|
11 | * X11 keyboard driver
|
---|
12 | *
|
---|
13 | * Copyright 1993 Bob Amstadt
|
---|
14 | * Copyright 1996 Albrecht Kleine
|
---|
15 | * Copyright 1997 David Faure
|
---|
16 | * Copyright 1998 Morten Welinder
|
---|
17 | * Copyright 1998 Ulrich Weigand
|
---|
18 | * Copyright 1999 Ove K�ven
|
---|
19 | *
|
---|
20 | * This library is free software; you can redistribute it and/or
|
---|
21 | * modify it under the terms of the GNU Lesser General Public
|
---|
22 | * License as published by the Free Software Foundation; either
|
---|
23 | * version 2.1 of the License, or (at your option) any later version.
|
---|
24 | *
|
---|
25 | * This library is distributed in the hope that it will be useful,
|
---|
26 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
27 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
28 | * Lesser General Public License for more details.
|
---|
29 | *
|
---|
30 | * You should have received a copy of the GNU Lesser General Public
|
---|
31 | * License along with this library; if not, write to the Free Software
|
---|
32 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
---|
33 | */
|
---|
34 |
|
---|
35 | /*
|
---|
36 | * Sun LGPL Disclaimer: For the avoidance of doubt, except that if any license choice
|
---|
37 | * other than GPL or LGPL is available it will apply instead, Sun elects to use only
|
---|
38 | * the Lesser General Public License version 2.1 (LGPLv2) at this time for any software where
|
---|
39 | * a choice of LGPL license versions is made available with the language indicating
|
---|
40 | * that LGPLv2 or any later version may be used, or where a choice of which version
|
---|
41 | * of the LGPL is applied is otherwise unspecified.
|
---|
42 | */
|
---|
43 |
|
---|
44 | #include <X11/Xatom.h>
|
---|
45 | #include <X11/keysym.h>
|
---|
46 | #include <X11/Xlib.h>
|
---|
47 | #include <X11/Xresource.h>
|
---|
48 | #include <X11/Xutil.h>
|
---|
49 |
|
---|
50 | #include <ctype.h>
|
---|
51 | #include <stdarg.h>
|
---|
52 | #include <string.h>
|
---|
53 | #include <stdlib.h>
|
---|
54 | #include <stdio.h>
|
---|
55 |
|
---|
56 | #include <VBox/VBoxKeyboard.h>
|
---|
57 |
|
---|
58 | #define KEYC2SCAN_SIZE 256
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Array containing the current mapping of keycodes to scan codes, detected
|
---|
62 | * using the keyboard layout algorithm in X11DRV_InitKeyboardByLayout.
|
---|
63 | */
|
---|
64 | static unsigned keyc2scan[KEYC2SCAN_SIZE];
|
---|
65 | /** Whether to output basic debugging information to standard output */
|
---|
66 | static int log_kb_1 = 0;
|
---|
67 | /** Whether to output verbose debugging information to standard output */
|
---|
68 | static int log_kb_2 = 0;
|
---|
69 |
|
---|
70 | /** Output basic debugging information if wished */
|
---|
71 | #define LOG_KB_1(a) \
|
---|
72 | do { \
|
---|
73 | if (log_kb_1) { \
|
---|
74 | printf a; \
|
---|
75 | } \
|
---|
76 | } while (0)
|
---|
77 |
|
---|
78 | /** Output verbose debugging information if wished */
|
---|
79 | #define LOG_KB_2(a) \
|
---|
80 | do { \
|
---|
81 | if (log_kb_2) { \
|
---|
82 | printf a; \
|
---|
83 | } \
|
---|
84 | } while (0)
|
---|
85 |
|
---|
86 | /** Keyboard layout tables for guessing the current keyboard layout. */
|
---|
87 | #include "keyboard-tables.h"
|
---|
88 |
|
---|
89 | /** Tables of keycode to scan code mappings for well-known keyboard types. */
|
---|
90 | #include "keyboard-types.h"
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Translate a keycode in a key event to a scan code. If the keycode maps
|
---|
94 | * to a key symbol which is in the same place on all PC keyboards, look it
|
---|
95 | * up by symbol in one of our hard-coded translation tables. It it maps to
|
---|
96 | * a symbol which can be in a different place on different PC keyboards, look
|
---|
97 | * it up by keycode using either the lookup table which we constructed
|
---|
98 | * earlier, or using a hard-coded table if we know what type of keyboard is
|
---|
99 | * in use.
|
---|
100 | *
|
---|
101 | * @returns the scan code number, with 0x100 added for extended scan codes
|
---|
102 | * @param code the X11 key code to be looked up
|
---|
103 | */
|
---|
104 |
|
---|
105 | unsigned X11DRV_KeyEvent(Display *display, KeyCode code)
|
---|
106 | {
|
---|
107 | unsigned scan;
|
---|
108 | KeySym keysym = XKeycodeToKeysym(display, code, 0);
|
---|
109 | scan = 0;
|
---|
110 | if (keysym != 0) /* otherwise, keycode not used */
|
---|
111 | {
|
---|
112 | if ((keysym >> 8) == 0xFF) /* non-character key */
|
---|
113 | scan = nonchar_key_scan[keysym & 0xff];
|
---|
114 | else if ((keysym >> 8) == 0x1008FF) /* XFree86 vendor keys */
|
---|
115 | scan = xfree86_vendor_key_scan[keysym & 0xff];
|
---|
116 | else if ((keysym >> 8) == 0x1005FF) /* Sun keys */
|
---|
117 | scan = sun_key_scan[keysym & 0xff];
|
---|
118 | else if (keysym == 0x20) /* Spacebar */
|
---|
119 | scan = 0x39;
|
---|
120 | else if (keysym == 0xFE03) /* ISO level3 shift, aka AltGr */
|
---|
121 | scan = 0x138;
|
---|
122 | }
|
---|
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 | */
|
---|
140 | static int
|
---|
141 | X11DRV_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 | */
|
---|
285 | static unsigned
|
---|
286 | X11DRV_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 |
|
---|
403 | static 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 |
|
---|
412 | static 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 |
|
---|
446 | static 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
|
---|
457 | static 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 |
|
---|
500 | static unsigned
|
---|
501 | X11DRV_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 | if (cMap >= 0)
|
---|
532 | {
|
---|
533 | memcpy(keyc2scan, main_keyboard_type_scans[cMap], KEYC2SCAN_SIZE);
|
---|
534 | return 1;
|
---|
535 | }
|
---|
536 | return 0;
|
---|
537 | }
|
---|
538 |
|
---|
539 | /**
|
---|
540 | * Initialise the X11 keyboard driver by finding which X11 keycodes correspond
|
---|
541 | * to which PC scan codes. If the keyboard being used is not a PC keyboard,
|
---|
542 | * the X11 keycodes will be mapped to the scan codes which the equivalent keys
|
---|
543 | * on a PC keyboard would use.
|
---|
544 | *
|
---|
545 | * We use two algorithms to try to determine the mapping. See the comments
|
---|
546 | * attached to the two algorithm functions (X11DRV_InitKeyboardByLayout and
|
---|
547 | * X11DRV_InitKeyboardByType) for descriptions of the algorithms used. Both
|
---|
548 | * functions tell us on return whether they think that they have correctly
|
---|
549 | * determined the mapping. If both functions claim to have determined the
|
---|
550 | * mapping correctly, we prefer the second (ByType). However, if neither does
|
---|
551 | * then we prefer the first (ByLayout), as it produces a fuzzy result which is
|
---|
552 | * still likely to be partially correct.
|
---|
553 | *
|
---|
554 | * @warning not re-entrant
|
---|
555 | * @returns 1 if the layout found was optimal, 0 if it was not. This is
|
---|
556 | * for diagnostic purposes
|
---|
557 | * @param display a pointer to the X11 display
|
---|
558 | * @param byLayoutOK diagnostic - set to one if detection by layout
|
---|
559 | * succeeded, and to 0 otherwise
|
---|
560 | * @param byTypeOK diagnostic - set to one if detection by type
|
---|
561 | * succeeded, and to 0 otherwise
|
---|
562 | * @param remapScancode array of tuples that remap the keycode (first
|
---|
563 | * part) to a scancode (second part)
|
---|
564 | */
|
---|
565 | unsigned X11DRV_InitKeyboard(Display *display, unsigned *byLayoutOK, unsigned *byTypeOK, int (*remapScancodes)[2])
|
---|
566 | {
|
---|
567 | unsigned byLayout;
|
---|
568 | unsigned byType;
|
---|
569 |
|
---|
570 | byLayout = X11DRV_InitKeyboardByLayout(display);
|
---|
571 | if (byLayoutOK)
|
---|
572 | *byLayoutOK = byLayout;
|
---|
573 |
|
---|
574 | byType = X11DRV_InitKeyboardByType(display);
|
---|
575 | if (byTypeOK)
|
---|
576 | *byTypeOK = byType;
|
---|
577 |
|
---|
578 | /* Remap keycodes after initialization. Remapping stops after an
|
---|
579 | identity mapping is seen */
|
---|
580 | if (remapScancodes != NULL)
|
---|
581 | for (; (*remapScancodes)[0] != (*remapScancodes)[1]; remapScancodes++)
|
---|
582 | keyc2scan[(*remapScancodes)[0]] = (*remapScancodes)[1];
|
---|
583 |
|
---|
584 | return (byLayout || byType) ? 1 : 0;
|
---|
585 | }
|
---|
586 |
|
---|
587 | /**
|
---|
588 | * Returns the keycode to scancode array
|
---|
589 | */
|
---|
590 | unsigned *X11DRV_getKeyc2scan(void)
|
---|
591 | {
|
---|
592 | return keyc2scan;
|
---|
593 | }
|
---|
594 |
|
---|