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 |
|
|
5 |
Copyright (C) Matthew Chapman 1999-2002 |
Copyright (C) Matthew Chapman 1999-2002 |
6 |
|
Copyright (C) Peter Astrand <peter@cendio.se> 2003 |
7 |
|
|
8 |
This program is free software; you can redistribute it and/or modify |
This program is free software; you can redistribute it and/or modify |
9 |
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 |
14 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 |
GNU General Public License for more details. |
GNU General Public License for more details. |
17 |
|
|
18 |
You should have received a copy of the GNU General Public License |
You should have received a copy of the GNU General Public License |
19 |
along with this program; if not, write to the Free Software |
along with this program; if not, write to the Free Software |
20 |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
21 |
*/ |
*/ |
22 |
|
|
23 |
|
#ifdef RDP2VNC |
24 |
|
#include "vnc/x11stubs.h" |
25 |
|
#else |
26 |
#include <X11/Xlib.h> |
#include <X11/Xlib.h> |
27 |
#define XK_MISCELLANY |
#include <X11/keysym.h> |
28 |
#include <X11/keysymdef.h> |
#endif |
29 |
|
|
30 |
#include <ctype.h> |
#include <ctype.h> |
31 |
#include <limits.h> |
#include <limits.h> |
32 |
#include <time.h> |
#include <time.h> |
37 |
#define KEYMAP_MASK 0xffff |
#define KEYMAP_MASK 0xffff |
38 |
#define KEYMAP_MAX_LINE_LENGTH 80 |
#define KEYMAP_MAX_LINE_LENGTH 80 |
39 |
|
|
40 |
extern Display *display; |
extern Display *g_display; |
41 |
|
extern Window g_wnd; |
42 |
extern char keymapname[16]; |
extern char keymapname[16]; |
43 |
extern int keylayout; |
extern int keylayout; |
44 |
extern BOOL enable_compose; |
extern int g_win_button_size; |
45 |
|
extern BOOL g_enable_compose; |
46 |
|
extern BOOL g_use_rdp5; |
47 |
|
extern BOOL g_numlock_sync; |
48 |
|
|
49 |
static BOOL keymap_loaded; |
static BOOL keymap_loaded; |
50 |
static key_translation keymap[KEYMAP_SIZE]; |
static key_translation keymap[KEYMAP_SIZE]; |
51 |
static int min_keycode; |
static int min_keycode; |
52 |
static uint16 remote_modifier_state = 0; |
static uint16 remote_modifier_state = 0; |
53 |
|
static uint16 saved_remote_modifier_state = 0; |
54 |
|
|
55 |
static void update_modifier_state(uint8 scancode, BOOL pressed); |
static void update_modifier_state(uint8 scancode, BOOL pressed); |
56 |
|
|
62 |
keysym = XStringToKeysym(keyname); |
keysym = XStringToKeysym(keyname); |
63 |
if (keysym == NoSymbol) |
if (keysym == NoSymbol) |
64 |
{ |
{ |
65 |
warning("Bad keysym %s in keymap %s\n", keyname, mapname); |
DEBUG_KBD(("Bad keysym \"%s\" in keymap %s (ignoring)\n", keyname, mapname)); |
66 |
return; |
return; |
67 |
} |
} |
68 |
|
|
146 |
if (strncmp(line, "enable_compose", 15) == 0) |
if (strncmp(line, "enable_compose", 15) == 0) |
147 |
{ |
{ |
148 |
DEBUG_KBD(("Enabling compose handling\n")); |
DEBUG_KBD(("Enabling compose handling\n")); |
149 |
enable_compose = True; |
g_enable_compose = True; |
150 |
continue; |
continue; |
151 |
} |
} |
152 |
|
|
209 |
/* Automatically add uppercase key, with same modifiers |
/* Automatically add uppercase key, with same modifiers |
210 |
plus shift */ |
plus shift */ |
211 |
for (p = keyname; *p; p++) |
for (p = keyname; *p; p++) |
212 |
*p = toupper((int)*p); |
*p = toupper((int) *p); |
213 |
MASK_ADD_BITS(modifiers, MapLeftShiftMask); |
MASK_ADD_BITS(modifiers, MapLeftShiftMask); |
214 |
add_to_keymap(keyname, scancode, modifiers, mapname); |
add_to_keymap(keyname, scancode, modifiers, mapname); |
215 |
} |
} |
231 |
mapname_ptr = keymapname; |
mapname_ptr = keymapname; |
232 |
while (*mapname_ptr) |
while (*mapname_ptr) |
233 |
{ |
{ |
234 |
*mapname_ptr = tolower((int)*mapname_ptr); |
*mapname_ptr = tolower((int) *mapname_ptr); |
235 |
mapname_ptr++; |
mapname_ptr++; |
236 |
} |
} |
237 |
|
|
241 |
keymap_loaded = True; |
keymap_loaded = True; |
242 |
} |
} |
243 |
|
|
244 |
XDisplayKeycodes(display, &min_keycode, (int *) &max_keycode); |
XDisplayKeycodes(g_display, &min_keycode, (int *) &max_keycode); |
245 |
|
} |
246 |
|
|
247 |
|
static void |
248 |
|
send_winkey(uint32 ev_time, BOOL pressed, BOOL leftkey) |
249 |
|
{ |
250 |
|
uint8 winkey; |
251 |
|
|
252 |
|
if (leftkey) |
253 |
|
winkey = SCANCODE_CHAR_LWIN; |
254 |
|
else |
255 |
|
winkey = SCANCODE_CHAR_RWIN; |
256 |
|
|
257 |
|
if (pressed) |
258 |
|
{ |
259 |
|
if (g_use_rdp5) |
260 |
|
{ |
261 |
|
rdp_send_scancode(ev_time, RDP_KEYPRESS, winkey); |
262 |
|
} |
263 |
|
else |
264 |
|
{ |
265 |
|
/* RDP4 doesn't support winkey. Fake with Ctrl-Esc */ |
266 |
|
rdp_send_scancode(ev_time, RDP_KEYPRESS, SCANCODE_CHAR_LCTRL); |
267 |
|
rdp_send_scancode(ev_time, RDP_KEYPRESS, SCANCODE_CHAR_ESC); |
268 |
|
} |
269 |
|
} |
270 |
|
else |
271 |
|
{ |
272 |
|
/* key released */ |
273 |
|
if (g_use_rdp5) |
274 |
|
{ |
275 |
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, winkey); |
276 |
|
} |
277 |
|
else |
278 |
|
{ |
279 |
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_ESC); |
280 |
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LCTRL); |
281 |
|
} |
282 |
|
} |
283 |
|
} |
284 |
|
|
285 |
|
static void |
286 |
|
reset_winkey(uint32 ev_time) |
287 |
|
{ |
288 |
|
if (g_use_rdp5) |
289 |
|
{ |
290 |
|
/* For some reason, it seems to suffice to release |
291 |
|
*either* the left or right winkey. */ |
292 |
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LWIN); |
293 |
|
} |
294 |
} |
} |
295 |
|
|
296 |
/* Handles, for example, multi-scancode keypresses (which is not |
/* Handles, for example, multi-scancode keypresses (which is not |
353 |
case XK_Meta_L: /* Windows keys */ |
case XK_Meta_L: /* Windows keys */ |
354 |
case XK_Super_L: |
case XK_Super_L: |
355 |
case XK_Hyper_L: |
case XK_Hyper_L: |
356 |
|
send_winkey(ev_time, pressed, True); |
357 |
|
return True; |
358 |
|
|
359 |
case XK_Meta_R: |
case XK_Meta_R: |
360 |
case XK_Super_R: |
case XK_Super_R: |
361 |
case XK_Hyper_R: |
case XK_Hyper_R: |
362 |
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); |
|
|
} |
|
363 |
return True; |
return True; |
364 |
|
|
365 |
|
case XK_space: |
366 |
|
/* Prevent access to the Windows system menu in single app mode */ |
367 |
|
if (g_win_button_size |
368 |
|
&& (get_key_state(state, XK_Alt_L) || get_key_state(state, XK_Alt_R))) |
369 |
|
return True; |
370 |
|
case XK_Num_Lock: |
371 |
|
/* FIXME: We might want to do RDP_INPUT_SYNCHRONIZE here, if g_numlock_sync */ |
372 |
|
if (!g_numlock_sync) |
373 |
|
/* Inhibit */ |
374 |
|
return True; |
375 |
|
|
376 |
|
|
377 |
} |
} |
378 |
return False; |
return False; |
379 |
} |
} |
414 |
warning("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)); |
415 |
|
|
416 |
/* not in keymap, try to interpret the raw scancode */ |
/* not in keymap, try to interpret the raw scancode */ |
417 |
if ((keycode >= min_keycode) && (keycode <= 0x60)) |
if (((int) keycode >= min_keycode) && (keycode <= 0x60)) |
418 |
{ |
{ |
419 |
tr.scancode = keycode - min_keycode; |
tr.scancode = keycode - min_keycode; |
420 |
|
|
469 |
return ksname; |
return ksname; |
470 |
} |
} |
471 |
|
|
472 |
|
static BOOL |
473 |
void |
is_modifier(uint8 scancode) |
|
ensure_remote_modifiers(uint32 ev_time, key_translation tr) |
|
474 |
{ |
{ |
475 |
/* If this key is a modifier, do nothing */ |
switch (scancode) |
|
switch (tr.scancode) |
|
476 |
{ |
{ |
477 |
case SCANCODE_CHAR_LSHIFT: |
case SCANCODE_CHAR_LSHIFT: |
478 |
case SCANCODE_CHAR_RSHIFT: |
case SCANCODE_CHAR_RSHIFT: |
483 |
case SCANCODE_CHAR_LWIN: |
case SCANCODE_CHAR_LWIN: |
484 |
case SCANCODE_CHAR_RWIN: |
case SCANCODE_CHAR_RWIN: |
485 |
case SCANCODE_CHAR_NUMLOCK: |
case SCANCODE_CHAR_NUMLOCK: |
486 |
return; |
return True; |
487 |
default: |
default: |
488 |
break; |
break; |
489 |
} |
} |
490 |
|
return False; |
491 |
|
} |
492 |
|
|
493 |
/* NumLock */ |
void |
494 |
if (MASK_HAS_BITS(tr.modifiers, MapNumLockMask) |
save_remote_modifiers(uint8 scancode) |
495 |
!= MASK_HAS_BITS(remote_modifier_state, MapNumLockMask)) |
{ |
496 |
{ |
if (is_modifier(scancode)) |
497 |
/* The remote modifier state is not correct */ |
return; |
|
uint16 new_remote_state; |
|
498 |
|
|
499 |
if (MASK_HAS_BITS(tr.modifiers, MapNumLockMask)) |
saved_remote_modifier_state = remote_modifier_state; |
500 |
{ |
} |
501 |
DEBUG_KBD(("Remote NumLock state is incorrect, activating NumLock.\n")); |
|
502 |
new_remote_state = KBD_FLAG_NUMLOCK; |
void |
503 |
remote_modifier_state = MapNumLockMask; |
restore_remote_modifiers(uint32 ev_time, uint8 scancode) |
504 |
} |
{ |
505 |
else |
key_translation dummy; |
506 |
|
|
507 |
|
if (is_modifier(scancode)) |
508 |
|
return; |
509 |
|
|
510 |
|
dummy.scancode = 0; |
511 |
|
dummy.modifiers = saved_remote_modifier_state; |
512 |
|
ensure_remote_modifiers(ev_time, dummy); |
513 |
|
} |
514 |
|
|
515 |
|
void |
516 |
|
ensure_remote_modifiers(uint32 ev_time, key_translation tr) |
517 |
|
{ |
518 |
|
/* If this key is a modifier, do nothing */ |
519 |
|
if (is_modifier(tr.scancode)) |
520 |
|
return; |
521 |
|
|
522 |
|
if (!g_numlock_sync) |
523 |
|
{ |
524 |
|
/* NumLock */ |
525 |
|
if (MASK_HAS_BITS(tr.modifiers, MapNumLockMask) |
526 |
|
!= MASK_HAS_BITS(remote_modifier_state, MapNumLockMask)) |
527 |
{ |
{ |
528 |
DEBUG_KBD(("Remote NumLock state is incorrect, deactivating NumLock.\n")); |
/* The remote modifier state is not correct */ |
529 |
new_remote_state = 0; |
uint16 new_remote_state; |
530 |
remote_modifier_state = 0; |
|
531 |
} |
if (MASK_HAS_BITS(tr.modifiers, MapNumLockMask)) |
532 |
|
{ |
533 |
|
DEBUG_KBD(("Remote NumLock state is incorrect, activating NumLock.\n")); |
534 |
|
new_remote_state = KBD_FLAG_NUMLOCK; |
535 |
|
remote_modifier_state = MapNumLockMask; |
536 |
|
} |
537 |
|
else |
538 |
|
{ |
539 |
|
DEBUG_KBD(("Remote NumLock state is incorrect, deactivating NumLock.\n")); |
540 |
|
new_remote_state = 0; |
541 |
|
remote_modifier_state = 0; |
542 |
|
} |
543 |
|
|
544 |
rdp_send_input(0, RDP_INPUT_SYNCHRONIZE, 0, new_remote_state, 0); |
rdp_send_input(0, RDP_INPUT_SYNCHRONIZE, 0, new_remote_state, 0); |
545 |
|
} |
546 |
} |
} |
547 |
|
|
548 |
|
|
549 |
/* 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. */ |
550 |
if (MASK_HAS_BITS(tr.modifiers, MapShiftMask) |
if (MASK_HAS_BITS(tr.modifiers, MapShiftMask) |
551 |
!= MASK_HAS_BITS(remote_modifier_state, MapShiftMask)) |
!= MASK_HAS_BITS(remote_modifier_state, MapShiftMask)) |
594 |
} |
} |
595 |
|
|
596 |
|
|
597 |
|
unsigned int |
598 |
|
read_keyboard_state() |
599 |
|
{ |
600 |
|
unsigned int state; |
601 |
|
Window wdummy; |
602 |
|
int dummy; |
603 |
|
|
604 |
|
XQueryPointer(g_display, g_wnd, &wdummy, &wdummy, &dummy, &dummy, &dummy, &dummy, &state); |
605 |
|
return state; |
606 |
|
} |
607 |
|
|
608 |
|
|
609 |
|
uint16 |
610 |
|
ui_get_numlock_state(unsigned int state) |
611 |
|
{ |
612 |
|
uint16 numlock_state = 0; |
613 |
|
|
614 |
|
if (get_key_state(state, XK_Num_Lock)) |
615 |
|
numlock_state = KBD_FLAG_NUMLOCK; |
616 |
|
|
617 |
|
return numlock_state; |
618 |
|
} |
619 |
|
|
620 |
|
|
621 |
void |
void |
622 |
reset_modifier_keys(unsigned int state) |
reset_modifier_keys() |
623 |
{ |
{ |
624 |
|
unsigned int state = read_keyboard_state(); |
625 |
|
|
626 |
/* reset keys */ |
/* reset keys */ |
627 |
uint32 ev_time; |
uint32 ev_time; |
628 |
ev_time = time(NULL); |
ev_time = time(NULL); |
649 |
if (MASK_HAS_BITS(remote_modifier_state, MapRightAltMask) && |
if (MASK_HAS_BITS(remote_modifier_state, MapRightAltMask) && |
650 |
!get_key_state(state, XK_Alt_R) && !get_key_state(state, XK_Mode_switch)) |
!get_key_state(state, XK_Alt_R) && !get_key_state(state, XK_Mode_switch)) |
651 |
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RALT); |
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RALT); |
652 |
|
|
653 |
|
reset_winkey(ev_time); |
654 |
|
|
655 |
|
if (g_numlock_sync) |
656 |
|
rdp_send_input(ev_time, RDP_INPUT_SYNCHRONIZE, 0, ui_get_numlock_state(state), 0); |
657 |
} |
} |
658 |
|
|
659 |
|
|
695 |
case SCANCODE_CHAR_NUMLOCK: |
case SCANCODE_CHAR_NUMLOCK: |
696 |
/* KeyReleases for NumLocks are sent immediately. Toggle the |
/* KeyReleases for NumLocks are sent immediately. Toggle the |
697 |
modifier state only on Keypress */ |
modifier state only on Keypress */ |
698 |
if (pressed) |
if (pressed && !g_numlock_sync) |
699 |
{ |
{ |
700 |
BOOL newNumLockState; |
BOOL newNumLockState; |
701 |
newNumLockState = |
newNumLockState = |
704 |
MASK_CHANGE_BIT(remote_modifier_state, |
MASK_CHANGE_BIT(remote_modifier_state, |
705 |
MapNumLockMask, newNumLockState); |
MapNumLockMask, newNumLockState); |
706 |
} |
} |
|
break; |
|
707 |
} |
} |
708 |
|
|
709 |
#ifdef WITH_DEBUG_KBD |
#ifdef WITH_DEBUG_KBD |