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 |
12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
GNU General Public License for more details. |
GNU General Public License for more details. |
15 |
|
|
16 |
You should have received a copy of the GNU General Public License |
You should have received a copy of the GNU General Public License |
17 |
along with this program; if not, write to the Free Software |
along with this program; if not, write to the Free Software |
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 *g_display; |
39 |
extern char keymapname[16]; |
extern char keymapname[16]; |
40 |
extern int keylayout; |
extern int keylayout; |
41 |
extern BOOL enable_compose; |
extern int g_win_button_size; |
42 |
|
extern BOOL g_enable_compose; |
43 |
|
extern BOOL g_use_rdp5; |
44 |
|
|
45 |
static BOOL keymap_loaded; |
static BOOL keymap_loaded; |
46 |
static key_translation keymap[KEYMAP_SIZE]; |
static key_translation keymap[KEYMAP_SIZE]; |
47 |
static int min_keycode; |
static int min_keycode; |
48 |
static uint16 remote_modifier_state = 0; |
static uint16 remote_modifier_state = 0; |
49 |
|
static uint16 saved_remote_modifier_state = 0; |
50 |
|
|
51 |
static void update_modifier_state(uint8 scancode, BOOL pressed); |
static void update_modifier_state(uint8 scancode, BOOL pressed); |
52 |
|
|
58 |
keysym = XStringToKeysym(keyname); |
keysym = XStringToKeysym(keyname); |
59 |
if (keysym == NoSymbol) |
if (keysym == NoSymbol) |
60 |
{ |
{ |
61 |
error("Bad keysym %s in keymap %s\n", keyname, mapname); |
DEBUG_KBD(("Bad keysym \"%s\" in keymap %s (ignoring)\n", keyname, mapname)); |
62 |
return; |
return; |
63 |
} |
} |
64 |
|
|
76 |
xkeymap_read(char *mapname) |
xkeymap_read(char *mapname) |
77 |
{ |
{ |
78 |
FILE *fp; |
FILE *fp; |
79 |
char line[KEYMAP_MAX_LINE_LENGTH], path[PATH_MAX]; |
char line[KEYMAP_MAX_LINE_LENGTH]; |
80 |
|
char path[PATH_MAX], inplace_path[PATH_MAX]; |
81 |
unsigned int line_num = 0; |
unsigned int line_num = 0; |
82 |
unsigned int line_length = 0; |
unsigned int line_length = 0; |
83 |
char *keyname, *p; |
char *keyname, *p; |
92 |
fp = fopen(path, "r"); |
fp = fopen(path, "r"); |
93 |
if (fp == NULL) |
if (fp == NULL) |
94 |
{ |
{ |
95 |
error("Failed to open keymap %s\n", path); |
/* in case we are running from the source tree */ |
96 |
return False; |
strcpy(inplace_path, "keymaps/"); |
97 |
|
strncat(inplace_path, mapname, sizeof(inplace_path) - sizeof("keymaps/")); |
98 |
|
|
99 |
|
fp = fopen(inplace_path, "r"); |
100 |
|
if (fp == NULL) |
101 |
|
{ |
102 |
|
error("Failed to open keymap %s\n", path); |
103 |
|
return False; |
104 |
|
} |
105 |
} |
} |
106 |
|
|
107 |
/* FIXME: More tolerant on white space */ |
/* FIXME: More tolerant on white space */ |
142 |
if (strncmp(line, "enable_compose", 15) == 0) |
if (strncmp(line, "enable_compose", 15) == 0) |
143 |
{ |
{ |
144 |
DEBUG_KBD(("Enabling compose handling\n")); |
DEBUG_KBD(("Enabling compose handling\n")); |
145 |
enable_compose = True; |
g_enable_compose = True; |
146 |
continue; |
continue; |
147 |
} |
} |
148 |
|
|
205 |
/* Automatically add uppercase key, with same modifiers |
/* Automatically add uppercase key, with same modifiers |
206 |
plus shift */ |
plus shift */ |
207 |
for (p = keyname; *p; p++) |
for (p = keyname; *p; p++) |
208 |
*p = toupper(*p); |
*p = toupper((int) *p); |
209 |
MASK_ADD_BITS(modifiers, MapLeftShiftMask); |
MASK_ADD_BITS(modifiers, MapLeftShiftMask); |
210 |
add_to_keymap(keyname, scancode, modifiers, mapname); |
add_to_keymap(keyname, scancode, modifiers, mapname); |
211 |
} |
} |
221 |
xkeymap_init(void) |
xkeymap_init(void) |
222 |
{ |
{ |
223 |
unsigned int max_keycode; |
unsigned int max_keycode; |
224 |
|
char *mapname_ptr; |
225 |
|
|
226 |
|
/* Make keymapname lowercase */ |
227 |
|
mapname_ptr = keymapname; |
228 |
|
while (*mapname_ptr) |
229 |
|
{ |
230 |
|
*mapname_ptr = tolower((int) *mapname_ptr); |
231 |
|
mapname_ptr++; |
232 |
|
} |
233 |
|
|
234 |
if (strcmp(keymapname, "none")) |
if (strcmp(keymapname, "none")) |
235 |
{ |
{ |
237 |
keymap_loaded = True; |
keymap_loaded = True; |
238 |
} |
} |
239 |
|
|
240 |
XDisplayKeycodes(display, &min_keycode, (int *) &max_keycode); |
XDisplayKeycodes(g_display, &min_keycode, (int *) &max_keycode); |
241 |
|
} |
242 |
|
|
243 |
|
static void |
244 |
|
send_winkey(uint32 ev_time, BOOL pressed, BOOL leftkey) |
245 |
|
{ |
246 |
|
uint8 winkey; |
247 |
|
|
248 |
|
if (leftkey) |
249 |
|
winkey = SCANCODE_CHAR_LWIN; |
250 |
|
else |
251 |
|
winkey = SCANCODE_CHAR_RWIN; |
252 |
|
|
253 |
|
if (pressed) |
254 |
|
{ |
255 |
|
if (g_use_rdp5) |
256 |
|
{ |
257 |
|
rdp_send_scancode(ev_time, RDP_KEYPRESS, winkey); |
258 |
|
} |
259 |
|
else |
260 |
|
{ |
261 |
|
/* RDP4 doesn't support winkey. Fake with Ctrl-Esc */ |
262 |
|
rdp_send_scancode(ev_time, RDP_KEYPRESS, SCANCODE_CHAR_LCTRL); |
263 |
|
rdp_send_scancode(ev_time, RDP_KEYPRESS, SCANCODE_CHAR_ESC); |
264 |
|
} |
265 |
|
} |
266 |
|
else |
267 |
|
{ |
268 |
|
/* key released */ |
269 |
|
if (g_use_rdp5) |
270 |
|
{ |
271 |
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, winkey); |
272 |
|
} |
273 |
|
else |
274 |
|
{ |
275 |
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_ESC); |
276 |
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LCTRL); |
277 |
|
} |
278 |
|
} |
279 |
} |
} |
280 |
|
|
281 |
/* Handles, for example, multi-scancode keypresses (which is not |
/* Handles, for example, multi-scancode keypresses (which is not |
285 |
{ |
{ |
286 |
switch (keysym) |
switch (keysym) |
287 |
{ |
{ |
288 |
case XK_Break: |
case XK_Return: |
289 |
if (get_key_state(state, XK_Alt_L) || get_key_state(state, XK_Alt_R)) |
if ((get_key_state(state, XK_Alt_L) || get_key_state(state, XK_Alt_R)) |
290 |
|
&& (get_key_state(state, XK_Control_L) |
291 |
|
|| get_key_state(state, XK_Control_R))) |
292 |
{ |
{ |
293 |
/* toggle full screen */ |
/* Ctrl-Alt-Enter: toggle full screen */ |
294 |
if (pressed) |
if (pressed) |
295 |
xwin_toggle_fullscreen(); |
xwin_toggle_fullscreen(); |
296 |
|
return True; |
297 |
} |
} |
298 |
else |
break; |
299 |
|
|
300 |
|
case XK_Break: |
301 |
|
/* Send Break sequence E0 46 E0 C6 */ |
302 |
|
if (pressed) |
303 |
{ |
{ |
304 |
/* Send Break sequence E0 46 E0 C6 */ |
rdp_send_scancode(ev_time, RDP_KEYPRESS, |
305 |
if (pressed) |
(SCANCODE_EXTENDED | 0x46)); |
306 |
{ |
rdp_send_scancode(ev_time, RDP_KEYPRESS, |
307 |
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 */ |
|
308 |
} |
} |
309 |
|
/* No release sequence */ |
310 |
return True; |
return True; |
|
break; |
|
311 |
|
|
312 |
case XK_Pause: |
case XK_Pause: |
313 |
/* According to MS Keyboard Scan Code |
/* According to MS Keyboard Scan Code |
330 |
else |
else |
331 |
{ |
{ |
332 |
/* Release Left Ctrl */ |
/* Release Left Ctrl */ |
333 |
rdp_send_input(ev_time, RDP_INPUT_SCANCODE, RDP_KEYRELEASE, 0x1d, |
rdp_send_input(ev_time, RDP_INPUT_SCANCODE, RDP_KEYRELEASE, |
334 |
0); |
0x1d, 0); |
335 |
} |
} |
|
|
|
336 |
return True; |
return True; |
|
break; |
|
337 |
|
|
338 |
case XK_Meta_L: /* Windows keys */ |
case XK_Meta_L: /* Windows keys */ |
339 |
case XK_Super_L: |
case XK_Super_L: |
340 |
case XK_Hyper_L: |
case XK_Hyper_L: |
341 |
|
send_winkey(ev_time, pressed, True); |
342 |
|
return True; |
343 |
|
|
344 |
case XK_Meta_R: |
case XK_Meta_R: |
345 |
case XK_Super_R: |
case XK_Super_R: |
346 |
case XK_Hyper_R: |
case XK_Hyper_R: |
347 |
if (pressed) |
send_winkey(ev_time, pressed, False); |
|
{ |
|
|
rdp_send_scancode(ev_time, RDP_KEYPRESS, SCANCODE_CHAR_LCTRL); |
|
|
rdp_send_scancode(ev_time, RDP_KEYPRESS, SCANCODE_CHAR_ESC); |
|
|
} |
|
|
else |
|
|
{ |
|
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_ESC); |
|
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LCTRL); |
|
|
} |
|
348 |
return True; |
return True; |
349 |
break; |
|
350 |
|
case XK_space: |
351 |
|
/* Prevent access to the Windows system menu in single app mode */ |
352 |
|
if (g_win_button_size |
353 |
|
&& (get_key_state(state, XK_Alt_L) || get_key_state(state, XK_Alt_R))) |
354 |
|
return True; |
355 |
|
|
356 |
} |
} |
357 |
return False; |
return False; |
358 |
} |
} |
390 |
} |
} |
391 |
|
|
392 |
if (keymap_loaded) |
if (keymap_loaded) |
393 |
error("No translation for (keysym 0x%lx, %s)\n", keysym, get_ksname(keysym)); |
warning("No translation for (keysym 0x%lx, %s)\n", keysym, get_ksname(keysym)); |
394 |
|
|
395 |
/* not in keymap, try to interpret the raw scancode */ |
/* not in keymap, try to interpret the raw scancode */ |
396 |
if ((keycode >= min_keycode) && (keycode <= 0x60)) |
if (((int) keycode >= min_keycode) && (keycode <= 0x60)) |
397 |
{ |
{ |
398 |
tr.scancode = keycode - min_keycode; |
tr.scancode = keycode - min_keycode; |
399 |
|
|
448 |
return ksname; |
return ksname; |
449 |
} |
} |
450 |
|
|
451 |
|
void |
452 |
|
save_remote_modifiers() |
453 |
|
{ |
454 |
|
saved_remote_modifier_state = remote_modifier_state; |
455 |
|
} |
456 |
|
|
457 |
|
void |
458 |
|
restore_remote_modifiers(uint32 ev_time) |
459 |
|
{ |
460 |
|
key_translation dummy; |
461 |
|
|
462 |
|
dummy.scancode = 0; |
463 |
|
dummy.modifiers = saved_remote_modifier_state; |
464 |
|
ensure_remote_modifiers(ev_time, dummy); |
465 |
|
} |
466 |
|
|
467 |
void |
void |
468 |
ensure_remote_modifiers(uint32 ev_time, key_translation tr) |
ensure_remote_modifiers(uint32 ev_time, key_translation tr) |
484 |
break; |
break; |
485 |
} |
} |
486 |
|
|
487 |
|
/* NumLock */ |
488 |
|
if (MASK_HAS_BITS(tr.modifiers, MapNumLockMask) |
489 |
|
!= MASK_HAS_BITS(remote_modifier_state, MapNumLockMask)) |
490 |
|
{ |
491 |
|
/* The remote modifier state is not correct */ |
492 |
|
uint16 new_remote_state; |
493 |
|
|
494 |
|
if (MASK_HAS_BITS(tr.modifiers, MapNumLockMask)) |
495 |
|
{ |
496 |
|
DEBUG_KBD(("Remote NumLock state is incorrect, activating NumLock.\n")); |
497 |
|
new_remote_state = KBD_FLAG_NUMLOCK; |
498 |
|
remote_modifier_state = MapNumLockMask; |
499 |
|
} |
500 |
|
else |
501 |
|
{ |
502 |
|
DEBUG_KBD(("Remote NumLock state is incorrect, deactivating NumLock.\n")); |
503 |
|
new_remote_state = 0; |
504 |
|
remote_modifier_state = 0; |
505 |
|
} |
506 |
|
|
507 |
|
rdp_send_input(0, RDP_INPUT_SYNCHRONIZE, 0, new_remote_state, 0); |
508 |
|
} |
509 |
|
|
510 |
/* 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. */ |
511 |
if (MASK_HAS_BITS(tr.modifiers, MapShiftMask) |
if (MASK_HAS_BITS(tr.modifiers, MapShiftMask) |
512 |
!= MASK_HAS_BITS(remote_modifier_state, MapShiftMask)) |
!= MASK_HAS_BITS(remote_modifier_state, MapShiftMask)) |
551 |
} |
} |
552 |
} |
} |
553 |
|
|
|
/* 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")); |
|
|
} |
|
554 |
|
|
|
rdp_send_input(0, RDP_INPUT_SYNCHRONIZE, 0, new_remote_state, 0); |
|
|
update_modifier_state(SCANCODE_CHAR_NUMLOCK, True); |
|
|
} |
|
555 |
} |
} |
556 |
|
|
557 |
|
|
562 |
uint32 ev_time; |
uint32 ev_time; |
563 |
ev_time = time(NULL); |
ev_time = time(NULL); |
564 |
|
|
565 |
if (MASK_HAS_BITS(remote_modifier_state, MapLeftShiftMask) && !get_key_state(state, XK_Shift_L)) |
if (MASK_HAS_BITS(remote_modifier_state, MapLeftShiftMask) |
566 |
|
&& !get_key_state(state, XK_Shift_L)) |
567 |
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LSHIFT); |
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LSHIFT); |
568 |
|
|
569 |
if (MASK_HAS_BITS(remote_modifier_state, MapRightShiftMask) && !get_key_state(state, XK_Shift_R)) |
if (MASK_HAS_BITS(remote_modifier_state, MapRightShiftMask) |
570 |
|
&& !get_key_state(state, XK_Shift_R)) |
571 |
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RSHIFT); |
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RSHIFT); |
572 |
|
|
573 |
if (MASK_HAS_BITS(remote_modifier_state, MapLeftCtrlMask) && !get_key_state(state, XK_Control_L)) |
if (MASK_HAS_BITS(remote_modifier_state, MapLeftCtrlMask) |
574 |
|
&& !get_key_state(state, XK_Control_L)) |
575 |
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LCTRL); |
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LCTRL); |
576 |
|
|
577 |
if (MASK_HAS_BITS(remote_modifier_state, MapRightCtrlMask) && !get_key_state(state, XK_Control_R)) |
if (MASK_HAS_BITS(remote_modifier_state, MapRightCtrlMask) |
578 |
|
&& !get_key_state(state, XK_Control_R)) |
579 |
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RCTRL); |
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RCTRL); |
580 |
|
|
581 |
if (MASK_HAS_BITS(remote_modifier_state, MapLeftAltMask) && !get_key_state(state, XK_Alt_L)) |
if (MASK_HAS_BITS(remote_modifier_state, MapLeftAltMask) && !get_key_state(state, XK_Alt_L)) |