1 |
/* |
2 |
rdesktop: A Remote Desktop Protocol client. |
3 |
User interface services - X keyboard mapping |
4 |
Copyright (C) Matthew Chapman 1999-2001 |
5 |
|
6 |
This program is free software; you can redistribute it and/or modify |
7 |
it under the terms of the GNU General Public License as published by |
8 |
the Free Software Foundation; either version 2 of the License, or |
9 |
(at your option) any later version. |
10 |
|
11 |
This program is distributed in the hope that it will be useful, |
12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
GNU General Public License for more details. |
15 |
|
16 |
You should have received a copy of the GNU General Public License |
17 |
along with this program; if not, write to the Free Software |
18 |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
19 |
*/ |
20 |
|
21 |
#include <X11/Xlib.h> |
22 |
#include <X11/keysym.h> |
23 |
#include <stdio.h> |
24 |
#include <stdlib.h> |
25 |
#include <string.h> |
26 |
#include <ctype.h> |
27 |
#include <limits.h> |
28 |
#include <assert.h> |
29 |
#include "rdesktop.h" |
30 |
#include "scancodes.h" |
31 |
|
32 |
#define KEYMAP_SIZE 4096 |
33 |
#define KEYMAP_MASK (KEYMAP_SIZE - 1) |
34 |
#define KEYMAP_MAX_LINE_LENGTH 80 |
35 |
|
36 |
extern Display *display; |
37 |
extern char keymapname[16]; |
38 |
extern int keylayout; |
39 |
extern BOOL enable_compose; |
40 |
|
41 |
static key_translation keymap[KEYMAP_SIZE]; |
42 |
static int min_keycode; |
43 |
static uint16 remote_modifier_state = 0; |
44 |
|
45 |
static void update_modifier_state(uint16 modifiers, BOOL pressed); |
46 |
|
47 |
static void |
48 |
add_to_keymap(char *keyname, uint8 scancode, uint16 modifiers, char *mapname) |
49 |
{ |
50 |
KeySym keysym; |
51 |
|
52 |
keysym = XStringToKeysym(keyname); |
53 |
if (keysym == NoSymbol) |
54 |
{ |
55 |
error("Bad keysym %s in keymap %s\n", keyname, mapname); |
56 |
return; |
57 |
} |
58 |
|
59 |
DEBUG_KBD(("Adding translation, keysym=0x%x, scancode=0x%x, " |
60 |
"modifiers=0x%x\n", (unsigned int) keysym, scancode, modifiers)); |
61 |
|
62 |
keymap[keysym & KEYMAP_MASK].scancode = scancode; |
63 |
keymap[keysym & KEYMAP_MASK].modifiers = modifiers; |
64 |
|
65 |
return; |
66 |
} |
67 |
|
68 |
|
69 |
static BOOL |
70 |
xkeymap_read(char *mapname) |
71 |
{ |
72 |
FILE *fp; |
73 |
char line[KEYMAP_MAX_LINE_LENGTH], path[PATH_MAX]; |
74 |
unsigned int line_num = 0; |
75 |
unsigned int line_length = 0; |
76 |
char *keyname, *p; |
77 |
char *line_rest; |
78 |
uint8 scancode; |
79 |
uint16 modifiers; |
80 |
|
81 |
|
82 |
strcpy(path, KEYMAP_PATH); |
83 |
strncat(path, mapname, sizeof(path) - sizeof(KEYMAP_PATH)); |
84 |
|
85 |
fp = fopen(path, "r"); |
86 |
if (fp == NULL) |
87 |
{ |
88 |
error("Failed to open keymap %s\n", path); |
89 |
return False; |
90 |
} |
91 |
|
92 |
/* FIXME: More tolerant on white space */ |
93 |
while (fgets(line, sizeof(line), fp) != NULL) |
94 |
{ |
95 |
line_num++; |
96 |
|
97 |
/* Replace the \n with \0 */ |
98 |
p = strchr(line, '\n'); |
99 |
if (p != NULL) |
100 |
*p = 0; |
101 |
|
102 |
line_length = strlen(line); |
103 |
|
104 |
/* Completely empty line */ |
105 |
if (strspn(line, " \t\n\r\f\v") == line_length) |
106 |
{ |
107 |
continue; |
108 |
} |
109 |
|
110 |
/* Include */ |
111 |
if (strncmp(line, "include ", 8) == 0) |
112 |
{ |
113 |
if (!xkeymap_read(line + 8)) |
114 |
return False; |
115 |
continue; |
116 |
} |
117 |
|
118 |
/* map */ |
119 |
if (strncmp(line, "map ", 4) == 0) |
120 |
{ |
121 |
keylayout = strtol(line + 4, NULL, 16); |
122 |
DEBUG_KBD(("Keylayout 0x%x\n", keylayout)); |
123 |
continue; |
124 |
} |
125 |
|
126 |
/* compose */ |
127 |
if (strncmp(line, "enable_compose", 15) == 0) |
128 |
{ |
129 |
DEBUG_KBD(("Enabling compose handling\n")); |
130 |
enable_compose = True; |
131 |
continue; |
132 |
} |
133 |
|
134 |
/* Comment */ |
135 |
if (line[0] == '#') |
136 |
{ |
137 |
continue; |
138 |
} |
139 |
|
140 |
/* Normal line */ |
141 |
keyname = line; |
142 |
p = strchr(line, ' '); |
143 |
if (p == NULL) |
144 |
{ |
145 |
error("Bad line %d in keymap %s\n", line_num, mapname); |
146 |
continue; |
147 |
} |
148 |
else |
149 |
{ |
150 |
*p = 0; |
151 |
} |
152 |
|
153 |
/* scancode */ |
154 |
p++; |
155 |
scancode = strtol(p, &line_rest, 16); |
156 |
|
157 |
/* flags */ |
158 |
/* FIXME: Should allow case-insensitive flag names. |
159 |
Fix by using lex+yacc... */ |
160 |
modifiers = 0; |
161 |
if (strstr(line_rest, "altgr")) |
162 |
{ |
163 |
MASK_ADD_BITS(modifiers, MapAltGrMask); |
164 |
} |
165 |
|
166 |
if (strstr(line_rest, "shift")) |
167 |
{ |
168 |
MASK_ADD_BITS(modifiers, MapLeftShiftMask); |
169 |
} |
170 |
|
171 |
if (strstr(line_rest, "numlock")) |
172 |
{ |
173 |
MASK_ADD_BITS(modifiers, MapNumLockMask); |
174 |
} |
175 |
|
176 |
if (strstr(line_rest, "localstate")) |
177 |
{ |
178 |
MASK_ADD_BITS(modifiers, MapLocalStateMask); |
179 |
} |
180 |
|
181 |
if (strstr(line_rest, "inhibit")) |
182 |
{ |
183 |
MASK_ADD_BITS(modifiers, MapInhibitMask); |
184 |
} |
185 |
|
186 |
add_to_keymap(keyname, scancode, modifiers, mapname); |
187 |
|
188 |
if (strstr(line_rest, "addupper")) |
189 |
{ |
190 |
/* Automatically add uppercase key, with same modifiers |
191 |
plus shift */ |
192 |
for (p = keyname; *p; p++) |
193 |
*p = toupper(*p); |
194 |
MASK_ADD_BITS(modifiers, MapLeftShiftMask); |
195 |
add_to_keymap(keyname, scancode, modifiers, mapname); |
196 |
} |
197 |
} |
198 |
|
199 |
fclose(fp); |
200 |
return True; |
201 |
} |
202 |
|
203 |
|
204 |
/* Before connecting and creating UI */ |
205 |
void |
206 |
xkeymap_init(void) |
207 |
{ |
208 |
unsigned int max_keycode; |
209 |
int i; |
210 |
|
211 |
if (strcmp(keymapname, "none")) |
212 |
xkeymap_read(keymapname); |
213 |
|
214 |
XDisplayKeycodes(display, &min_keycode, (int *) &max_keycode); |
215 |
} |
216 |
|
217 |
/* Handles, for example, multi-scancode keypresses (which is not |
218 |
possible via keymap-files) */ |
219 |
BOOL |
220 |
handle_special_keys(KeySym keysym, uint32 ev_time, BOOL pressed) |
221 |
{ |
222 |
switch (keysym) |
223 |
{ |
224 |
case XK_Break: /* toggle full screen */ |
225 |
if (pressed && (get_key_state(XK_Alt_L) || get_key_state(XK_Alt_R))) |
226 |
{ |
227 |
xwin_toggle_fullscreen(); |
228 |
return True; |
229 |
} |
230 |
break; |
231 |
|
232 |
case XK_Meta_L: /* Windows keys */ |
233 |
case XK_Super_L: |
234 |
case XK_Hyper_L: |
235 |
case XK_Meta_R: |
236 |
case XK_Super_R: |
237 |
case XK_Hyper_R: |
238 |
if (pressed) |
239 |
{ |
240 |
rdp_send_scancode(ev_time, RDP_KEYPRESS, SCANCODE_CHAR_LCTRL); |
241 |
rdp_send_scancode(ev_time, RDP_KEYPRESS, SCANCODE_CHAR_ESC); |
242 |
} |
243 |
else |
244 |
{ |
245 |
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_ESC); |
246 |
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LCTRL); |
247 |
} |
248 |
return True; |
249 |
break; |
250 |
} |
251 |
return False; |
252 |
} |
253 |
|
254 |
|
255 |
key_translation |
256 |
xkeymap_translate_key(KeySym keysym, unsigned int keycode, unsigned int state) |
257 |
{ |
258 |
key_translation tr = { 0, 0 }; |
259 |
|
260 |
tr = keymap[keysym & KEYMAP_MASK]; |
261 |
|
262 |
if (tr.modifiers & MapInhibitMask) |
263 |
{ |
264 |
DEBUG_KBD(("Inhibiting key\n")); |
265 |
tr.scancode = 0; |
266 |
return tr; |
267 |
} |
268 |
|
269 |
if (tr.modifiers & MapLocalStateMask) |
270 |
{ |
271 |
/* The modifiers to send for this key should be obtained |
272 |
from the local state. Currently, only shift is implemented. */ |
273 |
if (state & ShiftMask) |
274 |
{ |
275 |
tr.modifiers = MapLeftShiftMask; |
276 |
} |
277 |
} |
278 |
|
279 |
if (tr.scancode != 0) |
280 |
{ |
281 |
DEBUG_KBD |
282 |
(("Found key translation, scancode=0x%x, modifiers=0x%x\n", |
283 |
tr.scancode, tr.modifiers)); |
284 |
return tr; |
285 |
} |
286 |
|
287 |
fprintf(stderr, "No translation for (keysym 0x%lx, %s)\n", keysym, get_ksname(keysym)); |
288 |
|
289 |
/* not in keymap, try to interpret the raw scancode */ |
290 |
if ((keycode >= min_keycode) && (keycode <= 0x60)) |
291 |
{ |
292 |
tr.scancode = keycode - min_keycode; |
293 |
|
294 |
/* The modifiers to send for this key should be |
295 |
obtained from the local state. Currently, only |
296 |
shift is implemented. */ |
297 |
if (state & ShiftMask) |
298 |
{ |
299 |
tr.modifiers = MapLeftShiftMask; |
300 |
} |
301 |
|
302 |
fprintf(stderr, "Sending guessed scancode 0x%x\n", tr.scancode); |
303 |
} |
304 |
else |
305 |
{ |
306 |
fprintf(stderr, "No good guess for keycode 0x%x found\n", keycode); |
307 |
} |
308 |
|
309 |
return tr; |
310 |
} |
311 |
|
312 |
uint16 |
313 |
xkeymap_translate_button(unsigned int button) |
314 |
{ |
315 |
switch (button) |
316 |
{ |
317 |
case Button1: /* left */ |
318 |
return MOUSE_FLAG_BUTTON1; |
319 |
case Button2: /* middle */ |
320 |
return MOUSE_FLAG_BUTTON3; |
321 |
case Button3: /* right */ |
322 |
return MOUSE_FLAG_BUTTON2; |
323 |
case Button4: /* wheel up */ |
324 |
return MOUSE_FLAG_BUTTON4; |
325 |
case Button5: /* wheel down */ |
326 |
return MOUSE_FLAG_BUTTON5; |
327 |
} |
328 |
|
329 |
return 0; |
330 |
} |
331 |
|
332 |
char * |
333 |
get_ksname(KeySym keysym) |
334 |
{ |
335 |
char *ksname = NULL; |
336 |
|
337 |
if (keysym == NoSymbol) |
338 |
ksname = "NoSymbol"; |
339 |
else if (!(ksname = XKeysymToString(keysym))) |
340 |
ksname = "(no name)"; |
341 |
|
342 |
return ksname; |
343 |
} |
344 |
|
345 |
|
346 |
void |
347 |
ensure_remote_modifiers(uint32 ev_time, key_translation tr) |
348 |
{ |
349 |
/* If this key is a modifier, do nothing */ |
350 |
switch (tr.scancode) |
351 |
{ |
352 |
case SCANCODE_CHAR_LSHIFT: |
353 |
case SCANCODE_CHAR_RSHIFT: |
354 |
case SCANCODE_CHAR_LCTRL: |
355 |
case SCANCODE_CHAR_RCTRL: |
356 |
case SCANCODE_CHAR_LALT: |
357 |
case SCANCODE_CHAR_RALT: |
358 |
case SCANCODE_CHAR_LWIN: |
359 |
case SCANCODE_CHAR_RWIN: |
360 |
case SCANCODE_CHAR_NUMLOCK: |
361 |
return; |
362 |
default: |
363 |
break; |
364 |
} |
365 |
|
366 |
/* Shift. Left shift and right shift are treated as equal; either is fine. */ |
367 |
if (MASK_HAS_BITS(tr.modifiers, MapShiftMask) |
368 |
!= MASK_HAS_BITS(remote_modifier_state, MapShiftMask)) |
369 |
{ |
370 |
/* The remote modifier state is not correct */ |
371 |
if (MASK_HAS_BITS(tr.modifiers, MapLeftShiftMask)) |
372 |
{ |
373 |
/* Needs left shift. Send down. */ |
374 |
rdp_send_scancode(ev_time, RDP_KEYPRESS, SCANCODE_CHAR_LSHIFT); |
375 |
} |
376 |
else if (MASK_HAS_BITS(tr.modifiers, MapRightShiftMask)) |
377 |
{ |
378 |
/* Needs right shift. Send down. */ |
379 |
rdp_send_scancode(ev_time, RDP_KEYPRESS, SCANCODE_CHAR_RSHIFT); |
380 |
} |
381 |
else |
382 |
{ |
383 |
/* Should not use this modifier. Send up for shift currently pressed. */ |
384 |
if (MASK_HAS_BITS(remote_modifier_state, MapLeftShiftMask)) |
385 |
/* Left shift is down */ |
386 |
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LSHIFT); |
387 |
else |
388 |
{ |
389 |
assert(MASK_HAS_BITS(remote_modifier_state, MapRightShiftMask)); |
390 |
/* Right shift is down */ |
391 |
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RSHIFT); |
392 |
} |
393 |
|
394 |
} |
395 |
} |
396 |
|
397 |
/* AltGr */ |
398 |
if (MASK_HAS_BITS(tr.modifiers, MapAltGrMask) |
399 |
!= MASK_HAS_BITS(remote_modifier_state, MapAltGrMask)) |
400 |
{ |
401 |
/* The remote modifier state is not correct */ |
402 |
if (MASK_HAS_BITS(tr.modifiers, MapAltGrMask)) |
403 |
{ |
404 |
/* Needs this modifier. Send down. */ |
405 |
rdp_send_scancode(ev_time, RDP_KEYPRESS, SCANCODE_CHAR_RALT); |
406 |
} |
407 |
else |
408 |
{ |
409 |
/* Should not use this modifier. Send up. */ |
410 |
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RALT); |
411 |
} |
412 |
} |
413 |
|
414 |
/* NumLock */ |
415 |
if (MASK_HAS_BITS(tr.modifiers, MapNumLockMask) |
416 |
!= MASK_HAS_BITS(remote_modifier_state, MapNumLockMask)) |
417 |
{ |
418 |
/* The remote modifier state is not correct */ |
419 |
uint16 new_remote_state = 0; |
420 |
|
421 |
if (MASK_HAS_BITS(tr.modifiers, MapNumLockMask)) |
422 |
{ |
423 |
DEBUG_KBD(("Remote NumLock state is incorrect, activating NumLock.\n")); |
424 |
new_remote_state |= KBD_FLAG_NUMLOCK; |
425 |
} |
426 |
else |
427 |
{ |
428 |
DEBUG_KBD(("Remote NumLock state is incorrect, deactivating NumLock.\n")); |
429 |
} |
430 |
|
431 |
rdp_send_input(0, RDP_INPUT_SYNCHRONIZE, 0, new_remote_state, 0); |
432 |
update_modifier_state(SCANCODE_CHAR_NUMLOCK, True); |
433 |
} |
434 |
} |
435 |
|
436 |
|
437 |
void |
438 |
reset_modifier_keys() |
439 |
{ |
440 |
/* reset keys */ |
441 |
uint32 ev_time; |
442 |
ev_time = time(NULL); |
443 |
|
444 |
if (MASK_HAS_BITS(remote_modifier_state, MapLeftShiftMask) && !get_key_state(XK_Shift_L)) |
445 |
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LSHIFT); |
446 |
|
447 |
if (MASK_HAS_BITS(remote_modifier_state, MapRightShiftMask) && !get_key_state(XK_Shift_R)) |
448 |
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RSHIFT); |
449 |
|
450 |
if (MASK_HAS_BITS(remote_modifier_state, MapLeftCtrlMask) && !get_key_state(XK_Control_L)) |
451 |
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LCTRL); |
452 |
|
453 |
if (MASK_HAS_BITS(remote_modifier_state, MapRightCtrlMask) && !get_key_state(XK_Control_R)) |
454 |
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RCTRL); |
455 |
|
456 |
if (MASK_HAS_BITS(remote_modifier_state, MapLeftAltMask) && !get_key_state(XK_Alt_L)) |
457 |
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LALT); |
458 |
|
459 |
if (MASK_HAS_BITS(remote_modifier_state, MapRightAltMask) && |
460 |
!get_key_state(XK_Alt_R) && !get_key_state(XK_Mode_switch)) |
461 |
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RALT); |
462 |
} |
463 |
|
464 |
|
465 |
static void |
466 |
update_modifier_state(uint16 modifiers, BOOL pressed) |
467 |
{ |
468 |
#ifdef WITH_DEBUG_KBD |
469 |
uint16 old_modifier_state; |
470 |
|
471 |
old_modifier_state = remote_modifier_state; |
472 |
#endif |
473 |
|
474 |
switch (modifiers) |
475 |
{ |
476 |
case SCANCODE_CHAR_LSHIFT: |
477 |
MASK_CHANGE_BIT(remote_modifier_state, MapLeftShiftMask, pressed); |
478 |
break; |
479 |
case SCANCODE_CHAR_RSHIFT: |
480 |
MASK_CHANGE_BIT(remote_modifier_state, MapRightShiftMask, pressed); |
481 |
break; |
482 |
case SCANCODE_CHAR_LCTRL: |
483 |
MASK_CHANGE_BIT(remote_modifier_state, MapLeftCtrlMask, pressed); |
484 |
break; |
485 |
case SCANCODE_CHAR_RCTRL: |
486 |
MASK_CHANGE_BIT(remote_modifier_state, MapRightCtrlMask, pressed); |
487 |
break; |
488 |
case SCANCODE_CHAR_LALT: |
489 |
MASK_CHANGE_BIT(remote_modifier_state, MapLeftAltMask, pressed); |
490 |
break; |
491 |
case SCANCODE_CHAR_RALT: |
492 |
MASK_CHANGE_BIT(remote_modifier_state, MapRightAltMask, pressed); |
493 |
break; |
494 |
case SCANCODE_CHAR_LWIN: |
495 |
MASK_CHANGE_BIT(remote_modifier_state, MapLeftWinMask, pressed); |
496 |
break; |
497 |
case SCANCODE_CHAR_RWIN: |
498 |
MASK_CHANGE_BIT(remote_modifier_state, MapRightWinMask, pressed); |
499 |
break; |
500 |
case SCANCODE_CHAR_NUMLOCK: |
501 |
/* KeyReleases for NumLocks are sent immediately. Toggle the |
502 |
modifier state only on Keypress */ |
503 |
if (pressed) |
504 |
{ |
505 |
BOOL newNumLockState; |
506 |
newNumLockState = |
507 |
(MASK_HAS_BITS |
508 |
(remote_modifier_state, MapNumLockMask) == False); |
509 |
MASK_CHANGE_BIT(remote_modifier_state, |
510 |
MapNumLockMask, newNumLockState); |
511 |
} |
512 |
break; |
513 |
} |
514 |
|
515 |
#ifdef WITH_DEBUG_KBD |
516 |
if (old_modifier_state != remote_modifier_state) |
517 |
{ |
518 |
DEBUG_KBD(("Before updating modifier_state:0x%x, pressed=0x%x\n", |
519 |
old_modifier_state, pressed)); |
520 |
DEBUG_KBD(("After updating modifier_state:0x%x\n", remote_modifier_state)); |
521 |
} |
522 |
#endif |
523 |
|
524 |
} |
525 |
|
526 |
/* Send keyboard input */ |
527 |
void |
528 |
rdp_send_scancode(uint32 time, uint16 flags, uint16 scancode) |
529 |
{ |
530 |
update_modifier_state(scancode, !(flags & RDP_KEYRELEASE)); |
531 |
|
532 |
if (scancode & SCANCODE_EXTENDED) |
533 |
{ |
534 |
DEBUG_KBD(("Sending extended scancode=0x%x, flags=0x%x\n", |
535 |
scancode & ~SCANCODE_EXTENDED, flags)); |
536 |
rdp_send_input(time, RDP_INPUT_SCANCODE, flags | KBD_FLAG_EXT, |
537 |
scancode & ~SCANCODE_EXTENDED, 0); |
538 |
} |
539 |
else |
540 |
{ |
541 |
DEBUG_KBD(("Sending scancode=0x%x, flags=0x%x\n", scancode, flags)); |
542 |
rdp_send_input(time, RDP_INPUT_SCANCODE, flags, scancode, 0); |
543 |
} |
544 |
} |