1 |
/* |
/* |
2 |
rdesktop: A Remote Desktop Protocol client. |
rdesktop: A Remote Desktop Protocol client. |
3 |
User interface services - X keyboard mapping |
User interface services - X keyboard mapping |
4 |
Copyright (C) Matthew Chapman 1999-2001 |
Copyright (C) Matthew Chapman 1999-2002 |
5 |
|
|
6 |
This program is free software; you can redistribute it and/or modify |
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 |
it under the terms of the GNU General Public License as published by |
18 |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
19 |
*/ |
*/ |
20 |
|
|
21 |
|
#ifdef RDP2VNC |
22 |
|
#include "vnc/x11stubs.h" |
23 |
|
#else |
24 |
#include <X11/Xlib.h> |
#include <X11/Xlib.h> |
25 |
#define XK_MISCELLANY |
#include <X11/keysym.h> |
26 |
#include <X11/keysymdef.h> |
#endif |
27 |
|
|
28 |
#include <ctype.h> |
#include <ctype.h> |
29 |
#include <limits.h> |
#include <limits.h> |
30 |
#include <time.h> |
#include <time.h> |
31 |
#include "rdesktop.h" |
#include "rdesktop.h" |
32 |
#include "scancodes.h" |
#include "scancodes.h" |
33 |
|
|
34 |
#define KEYMAP_SIZE 4096 |
#define KEYMAP_SIZE 0xffff+1 |
35 |
#define KEYMAP_MASK (KEYMAP_SIZE - 1) |
#define KEYMAP_MASK 0xffff |
36 |
#define KEYMAP_MAX_LINE_LENGTH 80 |
#define KEYMAP_MAX_LINE_LENGTH 80 |
37 |
|
|
38 |
extern Display *display; |
extern Display *display; |
39 |
extern char keymapname[16]; |
extern char keymapname[16]; |
40 |
extern int keylayout; |
extern int keylayout; |
41 |
|
extern int win_button_size; |
42 |
extern BOOL enable_compose; |
extern BOOL enable_compose; |
43 |
|
|
44 |
|
static BOOL keymap_loaded; |
45 |
static key_translation keymap[KEYMAP_SIZE]; |
static key_translation keymap[KEYMAP_SIZE]; |
46 |
static int min_keycode; |
static int min_keycode; |
47 |
static uint16 remote_modifier_state = 0; |
static uint16 remote_modifier_state = 0; |
48 |
|
|
49 |
static void update_modifier_state(uint16 modifiers, BOOL pressed); |
static void update_modifier_state(uint8 scancode, BOOL pressed); |
50 |
|
|
51 |
static void |
static void |
52 |
add_to_keymap(char *keyname, uint8 scancode, uint16 modifiers, char *mapname) |
add_to_keymap(char *keyname, uint8 scancode, uint16 modifiers, char *mapname) |
56 |
keysym = XStringToKeysym(keyname); |
keysym = XStringToKeysym(keyname); |
57 |
if (keysym == NoSymbol) |
if (keysym == NoSymbol) |
58 |
{ |
{ |
59 |
error("Bad keysym %s in keymap %s\n", keyname, mapname); |
DEBUG_KBD(("Bad keysym \"%s\" in keymap %s (ignoring)\n", keyname, mapname)); |
60 |
return; |
return; |
61 |
} |
} |
62 |
|
|
74 |
xkeymap_read(char *mapname) |
xkeymap_read(char *mapname) |
75 |
{ |
{ |
76 |
FILE *fp; |
FILE *fp; |
77 |
char line[KEYMAP_MAX_LINE_LENGTH], path[PATH_MAX]; |
char line[KEYMAP_MAX_LINE_LENGTH]; |
78 |
|
char path[PATH_MAX], inplace_path[PATH_MAX]; |
79 |
unsigned int line_num = 0; |
unsigned int line_num = 0; |
80 |
unsigned int line_length = 0; |
unsigned int line_length = 0; |
81 |
char *keyname, *p; |
char *keyname, *p; |
90 |
fp = fopen(path, "r"); |
fp = fopen(path, "r"); |
91 |
if (fp == NULL) |
if (fp == NULL) |
92 |
{ |
{ |
93 |
error("Failed to open keymap %s\n", path); |
/* in case we are running from the source tree */ |
94 |
return False; |
strcpy(inplace_path, "keymaps/"); |
95 |
|
strncat(inplace_path, mapname, sizeof(inplace_path) - sizeof("keymaps/")); |
96 |
|
|
97 |
|
fp = fopen(inplace_path, "r"); |
98 |
|
if (fp == NULL) |
99 |
|
{ |
100 |
|
error("Failed to open keymap %s\n", path); |
101 |
|
return False; |
102 |
|
} |
103 |
} |
} |
104 |
|
|
105 |
/* FIXME: More tolerant on white space */ |
/* FIXME: More tolerant on white space */ |
203 |
/* Automatically add uppercase key, with same modifiers |
/* Automatically add uppercase key, with same modifiers |
204 |
plus shift */ |
plus shift */ |
205 |
for (p = keyname; *p; p++) |
for (p = keyname; *p; p++) |
206 |
*p = toupper(*p); |
*p = toupper((int) *p); |
207 |
MASK_ADD_BITS(modifiers, MapLeftShiftMask); |
MASK_ADD_BITS(modifiers, MapLeftShiftMask); |
208 |
add_to_keymap(keyname, scancode, modifiers, mapname); |
add_to_keymap(keyname, scancode, modifiers, mapname); |
209 |
} |
} |
219 |
xkeymap_init(void) |
xkeymap_init(void) |
220 |
{ |
{ |
221 |
unsigned int max_keycode; |
unsigned int max_keycode; |
222 |
|
char *mapname_ptr; |
223 |
|
|
224 |
|
/* Make keymapname lowercase */ |
225 |
|
mapname_ptr = keymapname; |
226 |
|
while (*mapname_ptr) |
227 |
|
{ |
228 |
|
*mapname_ptr = tolower((int) *mapname_ptr); |
229 |
|
mapname_ptr++; |
230 |
|
} |
231 |
|
|
232 |
if (strcmp(keymapname, "none")) |
if (strcmp(keymapname, "none")) |
233 |
xkeymap_read(keymapname); |
{ |
234 |
|
if (xkeymap_read(keymapname)) |
235 |
|
keymap_loaded = True; |
236 |
|
} |
237 |
|
|
238 |
XDisplayKeycodes(display, &min_keycode, (int *) &max_keycode); |
XDisplayKeycodes(display, &min_keycode, (int *) &max_keycode); |
239 |
} |
} |
241 |
/* Handles, for example, multi-scancode keypresses (which is not |
/* Handles, for example, multi-scancode keypresses (which is not |
242 |
possible via keymap-files) */ |
possible via keymap-files) */ |
243 |
BOOL |
BOOL |
244 |
handle_special_keys(uint32 keysym, uint32 ev_time, BOOL pressed) |
handle_special_keys(uint32 keysym, unsigned int state, uint32 ev_time, BOOL pressed) |
245 |
{ |
{ |
246 |
switch (keysym) |
switch (keysym) |
247 |
{ |
{ |
248 |
case XK_Break: |
case XK_Return: |
249 |
if (get_key_state(XK_Alt_L) || get_key_state(XK_Alt_R)) |
if ((get_key_state(state, XK_Alt_L) || get_key_state(state, XK_Alt_R)) |
250 |
|
&& (get_key_state(state, XK_Control_L) |
251 |
|
|| get_key_state(state, XK_Control_R))) |
252 |
{ |
{ |
253 |
/* toggle full screen */ |
/* Ctrl-Alt-Enter: toggle full screen */ |
254 |
if (pressed) |
if (pressed) |
255 |
xwin_toggle_fullscreen(); |
xwin_toggle_fullscreen(); |
256 |
|
return True; |
257 |
} |
} |
258 |
else |
break; |
259 |
|
|
260 |
|
case XK_Break: |
261 |
|
/* Send Break sequence E0 46 E0 C6 */ |
262 |
|
if (pressed) |
263 |
{ |
{ |
264 |
/* Send Break sequence E0 46 E0 C6 */ |
rdp_send_scancode(ev_time, RDP_KEYPRESS, |
265 |
if (pressed) |
(SCANCODE_EXTENDED | 0x46)); |
266 |
{ |
rdp_send_scancode(ev_time, RDP_KEYPRESS, |
267 |
rdp_send_scancode(ev_time, RDP_KEYPRESS, |
(SCANCODE_EXTENDED | 0xc6)); |
|
(SCANCODE_EXTENDED | 0x46)); |
|
|
rdp_send_scancode(ev_time, RDP_KEYPRESS, |
|
|
(SCANCODE_EXTENDED | 0xc6)); |
|
|
} |
|
|
/* No break sequence */ |
|
268 |
} |
} |
269 |
|
/* No release sequence */ |
270 |
return True; |
return True; |
|
break; |
|
271 |
|
|
272 |
case XK_Pause: |
case XK_Pause: |
273 |
/* According to MS Keyboard Scan Code |
/* According to MS Keyboard Scan Code |
289 |
} |
} |
290 |
else |
else |
291 |
{ |
{ |
292 |
// Release Left Ctrl |
/* Release Left Ctrl */ |
293 |
rdp_send_input(ev_time, RDP_INPUT_SCANCODE, RDP_KEYRELEASE, 0x1d, |
rdp_send_input(ev_time, RDP_INPUT_SCANCODE, RDP_KEYRELEASE, |
294 |
0); |
0x1d, 0); |
295 |
} |
} |
|
|
|
296 |
return True; |
return True; |
|
break; |
|
297 |
|
|
298 |
case XK_Meta_L: /* Windows keys */ |
case XK_Meta_L: /* Windows keys */ |
299 |
case XK_Super_L: |
case XK_Super_L: |
312 |
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LCTRL); |
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LCTRL); |
313 |
} |
} |
314 |
return True; |
return True; |
315 |
break; |
|
316 |
|
case XK_space: |
317 |
|
/* Prevent access to the Windows system menu in single app mode */ |
318 |
|
if (win_button_size |
319 |
|
&& (get_key_state(state, XK_Alt_L) || get_key_state(state, XK_Alt_R))) |
320 |
|
return True; |
321 |
|
|
322 |
} |
} |
323 |
return False; |
return False; |
324 |
} |
} |
355 |
return tr; |
return tr; |
356 |
} |
} |
357 |
|
|
358 |
DEBUG_KBD(("No translation for (keysym 0x%lx, %s)\n", keysym, get_ksname(keysym))); |
if (keymap_loaded) |
359 |
|
warning("No translation for (keysym 0x%lx, %s)\n", keysym, get_ksname(keysym)); |
360 |
|
|
361 |
/* not in keymap, try to interpret the raw scancode */ |
/* not in keymap, try to interpret the raw scancode */ |
362 |
if ((keycode >= min_keycode) && (keycode <= 0x60)) |
if ((keycode >= min_keycode) && (keycode <= 0x60)) |
435 |
break; |
break; |
436 |
} |
} |
437 |
|
|
438 |
|
/* NumLock */ |
439 |
|
if (MASK_HAS_BITS(tr.modifiers, MapNumLockMask) |
440 |
|
!= MASK_HAS_BITS(remote_modifier_state, MapNumLockMask)) |
441 |
|
{ |
442 |
|
/* The remote modifier state is not correct */ |
443 |
|
uint16 new_remote_state; |
444 |
|
|
445 |
|
if (MASK_HAS_BITS(tr.modifiers, MapNumLockMask)) |
446 |
|
{ |
447 |
|
DEBUG_KBD(("Remote NumLock state is incorrect, activating NumLock.\n")); |
448 |
|
new_remote_state = KBD_FLAG_NUMLOCK; |
449 |
|
remote_modifier_state = MapNumLockMask; |
450 |
|
} |
451 |
|
else |
452 |
|
{ |
453 |
|
DEBUG_KBD(("Remote NumLock state is incorrect, deactivating NumLock.\n")); |
454 |
|
new_remote_state = 0; |
455 |
|
remote_modifier_state = 0; |
456 |
|
} |
457 |
|
|
458 |
|
rdp_send_input(0, RDP_INPUT_SYNCHRONIZE, 0, new_remote_state, 0); |
459 |
|
} |
460 |
|
|
461 |
/* Shift. Left shift and right shift are treated as equal; either is fine. */ |
/* Shift. Left shift and right shift are treated as equal; either is fine. */ |
462 |
if (MASK_HAS_BITS(tr.modifiers, MapShiftMask) |
if (MASK_HAS_BITS(tr.modifiers, MapShiftMask) |
463 |
!= MASK_HAS_BITS(remote_modifier_state, MapShiftMask)) |
!= MASK_HAS_BITS(remote_modifier_state, MapShiftMask)) |
502 |
} |
} |
503 |
} |
} |
504 |
|
|
|
/* NumLock */ |
|
|
if (MASK_HAS_BITS(tr.modifiers, MapNumLockMask) |
|
|
!= MASK_HAS_BITS(remote_modifier_state, MapNumLockMask)) |
|
|
{ |
|
|
/* The remote modifier state is not correct */ |
|
|
uint16 new_remote_state = 0; |
|
|
|
|
|
if (MASK_HAS_BITS(tr.modifiers, MapNumLockMask)) |
|
|
{ |
|
|
DEBUG_KBD(("Remote NumLock state is incorrect, activating NumLock.\n")); |
|
|
new_remote_state |= KBD_FLAG_NUMLOCK; |
|
|
} |
|
|
else |
|
|
{ |
|
|
DEBUG_KBD(("Remote NumLock state is incorrect, deactivating NumLock.\n")); |
|
|
} |
|
505 |
|
|
|
rdp_send_input(0, RDP_INPUT_SYNCHRONIZE, 0, new_remote_state, 0); |
|
|
update_modifier_state(SCANCODE_CHAR_NUMLOCK, True); |
|
|
} |
|
506 |
} |
} |
507 |
|
|
508 |
|
|
509 |
void |
void |
510 |
reset_modifier_keys(void) |
reset_modifier_keys(unsigned int state) |
511 |
{ |
{ |
512 |
/* reset keys */ |
/* reset keys */ |
513 |
uint32 ev_time; |
uint32 ev_time; |
514 |
ev_time = time(NULL); |
ev_time = time(NULL); |
515 |
|
|
516 |
if (MASK_HAS_BITS(remote_modifier_state, MapLeftShiftMask) && !get_key_state(XK_Shift_L)) |
if (MASK_HAS_BITS(remote_modifier_state, MapLeftShiftMask) |
517 |
|
&& !get_key_state(state, XK_Shift_L)) |
518 |
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LSHIFT); |
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LSHIFT); |
519 |
|
|
520 |
if (MASK_HAS_BITS(remote_modifier_state, MapRightShiftMask) && !get_key_state(XK_Shift_R)) |
if (MASK_HAS_BITS(remote_modifier_state, MapRightShiftMask) |
521 |
|
&& !get_key_state(state, XK_Shift_R)) |
522 |
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RSHIFT); |
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RSHIFT); |
523 |
|
|
524 |
if (MASK_HAS_BITS(remote_modifier_state, MapLeftCtrlMask) && !get_key_state(XK_Control_L)) |
if (MASK_HAS_BITS(remote_modifier_state, MapLeftCtrlMask) |
525 |
|
&& !get_key_state(state, XK_Control_L)) |
526 |
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LCTRL); |
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LCTRL); |
527 |
|
|
528 |
if (MASK_HAS_BITS(remote_modifier_state, MapRightCtrlMask) && !get_key_state(XK_Control_R)) |
if (MASK_HAS_BITS(remote_modifier_state, MapRightCtrlMask) |
529 |
|
&& !get_key_state(state, XK_Control_R)) |
530 |
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RCTRL); |
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RCTRL); |
531 |
|
|
532 |
if (MASK_HAS_BITS(remote_modifier_state, MapLeftAltMask) && !get_key_state(XK_Alt_L)) |
if (MASK_HAS_BITS(remote_modifier_state, MapLeftAltMask) && !get_key_state(state, XK_Alt_L)) |
533 |
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LALT); |
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LALT); |
534 |
|
|
535 |
if (MASK_HAS_BITS(remote_modifier_state, MapRightAltMask) && |
if (MASK_HAS_BITS(remote_modifier_state, MapRightAltMask) && |
536 |
!get_key_state(XK_Alt_R) && !get_key_state(XK_Mode_switch)) |
!get_key_state(state, XK_Alt_R) && !get_key_state(state, XK_Mode_switch)) |
537 |
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RALT); |
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RALT); |
538 |
} |
} |
539 |
|
|
540 |
|
|
541 |
static void |
static void |
542 |
update_modifier_state(uint16 modifiers, BOOL pressed) |
update_modifier_state(uint8 scancode, BOOL pressed) |
543 |
{ |
{ |
544 |
#ifdef WITH_DEBUG_KBD |
#ifdef WITH_DEBUG_KBD |
545 |
uint16 old_modifier_state; |
uint16 old_modifier_state; |
547 |
old_modifier_state = remote_modifier_state; |
old_modifier_state = remote_modifier_state; |
548 |
#endif |
#endif |
549 |
|
|
550 |
switch (modifiers) |
switch (scancode) |
551 |
{ |
{ |
552 |
case SCANCODE_CHAR_LSHIFT: |
case SCANCODE_CHAR_LSHIFT: |
553 |
MASK_CHANGE_BIT(remote_modifier_state, MapLeftShiftMask, pressed); |
MASK_CHANGE_BIT(remote_modifier_state, MapLeftShiftMask, pressed); |
601 |
|
|
602 |
/* Send keyboard input */ |
/* Send keyboard input */ |
603 |
void |
void |
604 |
rdp_send_scancode(uint32 time, uint16 flags, uint16 scancode) |
rdp_send_scancode(uint32 time, uint16 flags, uint8 scancode) |
605 |
{ |
{ |
606 |
update_modifier_state(scancode, !(flags & RDP_KEYRELEASE)); |
update_modifier_state(scancode, !(flags & RDP_KEYRELEASE)); |
607 |
|
|