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-2002 |
|
5 |
|
Copyright (C) Matthew Chapman 1999-2005 |
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 |
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 g_keylayout; |
44 |
extern int win_button_size; |
extern int g_win_button_size; |
45 |
extern BOOL enable_compose; |
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 |
|
|
137 |
/* map */ |
/* map */ |
138 |
if (strncmp(line, "map ", 4) == 0) |
if (strncmp(line, "map ", 4) == 0) |
139 |
{ |
{ |
140 |
keylayout = strtol(line + 4, NULL, 16); |
g_keylayout = strtol(line + 4, NULL, 16); |
141 |
DEBUG_KBD(("Keylayout 0x%x\n", keylayout)); |
DEBUG_KBD(("Keylayout 0x%x\n", g_keylayout)); |
142 |
continue; |
continue; |
143 |
} |
} |
144 |
|
|
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 |
|
|
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 |
323 |
} |
} |
324 |
/* No release sequence */ |
/* No release sequence */ |
325 |
return True; |
return True; |
326 |
|
break; |
327 |
|
|
328 |
case XK_Pause: |
case XK_Pause: |
329 |
/* According to MS Keyboard Scan Code |
/* According to MS Keyboard Scan Code |
350 |
0x1d, 0); |
0x1d, 0); |
351 |
} |
} |
352 |
return True; |
return True; |
353 |
|
break; |
354 |
|
|
355 |
case XK_Meta_L: /* Windows keys */ |
case XK_Meta_L: /* Windows keys */ |
356 |
case XK_Super_L: |
case XK_Super_L: |
357 |
case XK_Hyper_L: |
case XK_Hyper_L: |
358 |
|
send_winkey(ev_time, pressed, True); |
359 |
|
return True; |
360 |
|
break; |
361 |
|
|
362 |
case XK_Meta_R: |
case XK_Meta_R: |
363 |
case XK_Super_R: |
case XK_Super_R: |
364 |
case XK_Hyper_R: |
case XK_Hyper_R: |
365 |
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); |
|
|
} |
|
366 |
return True; |
return True; |
367 |
|
break; |
368 |
|
|
369 |
case XK_space: |
case XK_space: |
370 |
/* Prevent access to the Windows system menu in single app mode */ |
/* Prevent access to the Windows system menu in single app mode */ |
371 |
if (win_button_size |
if (g_win_button_size |
372 |
&& (get_key_state(state, XK_Alt_L) || get_key_state(state, XK_Alt_R))) |
&& (get_key_state(state, XK_Alt_L) || get_key_state(state, XK_Alt_R))) |
373 |
return True; |
return True; |
374 |
|
break; |
375 |
|
case XK_Num_Lock: |
376 |
|
/* FIXME: We might want to do RDP_INPUT_SYNCHRONIZE here, if g_numlock_sync */ |
377 |
|
if (!g_numlock_sync) |
378 |
|
/* Inhibit */ |
379 |
|
return True; |
380 |
|
break; |
381 |
|
|
382 |
} |
} |
383 |
return False; |
return False; |
408 |
} |
} |
409 |
} |
} |
410 |
|
|
411 |
|
#if 0 |
412 |
|
if (((remote_modifier_state & MapLeftCtrlMask) |
413 |
|
|| (remote_modifier_state & MapRightCtrlMask)) && get_key_state(state, XK_Caps_Lock)) |
414 |
|
{ |
415 |
|
DEBUG_KBD(("CapsLock + Ctrl pressed, releasing LeftShift\n")); |
416 |
|
tr.modifiers ^= MapLeftShiftMask; |
417 |
|
} |
418 |
|
#endif |
419 |
|
|
420 |
if (tr.scancode != 0) |
if (tr.scancode != 0) |
421 |
{ |
{ |
422 |
DEBUG_KBD(("Found key translation, scancode=0x%x, modifiers=0x%x\n", |
DEBUG_KBD(("Found key translation, scancode=0x%x, modifiers=0x%x\n", |
428 |
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)); |
429 |
|
|
430 |
/* not in keymap, try to interpret the raw scancode */ |
/* not in keymap, try to interpret the raw scancode */ |
431 |
if (((int)keycode >= min_keycode) && (keycode <= 0x60)) |
if (((int) keycode >= min_keycode) && (keycode <= 0x60)) |
432 |
{ |
{ |
433 |
tr.scancode = keycode - min_keycode; |
tr.scancode = keycode - min_keycode; |
434 |
|
|
483 |
return ksname; |
return ksname; |
484 |
} |
} |
485 |
|
|
486 |
|
static BOOL |
487 |
void |
is_modifier(uint8 scancode) |
|
ensure_remote_modifiers(uint32 ev_time, key_translation tr) |
|
488 |
{ |
{ |
489 |
/* If this key is a modifier, do nothing */ |
switch (scancode) |
|
switch (tr.scancode) |
|
490 |
{ |
{ |
491 |
case SCANCODE_CHAR_LSHIFT: |
case SCANCODE_CHAR_LSHIFT: |
492 |
case SCANCODE_CHAR_RSHIFT: |
case SCANCODE_CHAR_RSHIFT: |
497 |
case SCANCODE_CHAR_LWIN: |
case SCANCODE_CHAR_LWIN: |
498 |
case SCANCODE_CHAR_RWIN: |
case SCANCODE_CHAR_RWIN: |
499 |
case SCANCODE_CHAR_NUMLOCK: |
case SCANCODE_CHAR_NUMLOCK: |
500 |
return; |
return True; |
501 |
default: |
default: |
502 |
break; |
break; |
503 |
} |
} |
504 |
|
return False; |
505 |
|
} |
506 |
|
|
507 |
/* NumLock */ |
void |
508 |
if (MASK_HAS_BITS(tr.modifiers, MapNumLockMask) |
save_remote_modifiers(uint8 scancode) |
509 |
!= MASK_HAS_BITS(remote_modifier_state, MapNumLockMask)) |
{ |
510 |
{ |
if (is_modifier(scancode)) |
511 |
/* The remote modifier state is not correct */ |
return; |
|
uint16 new_remote_state; |
|
512 |
|
|
513 |
if (MASK_HAS_BITS(tr.modifiers, MapNumLockMask)) |
saved_remote_modifier_state = remote_modifier_state; |
514 |
{ |
} |
515 |
DEBUG_KBD(("Remote NumLock state is incorrect, activating NumLock.\n")); |
|
516 |
new_remote_state = KBD_FLAG_NUMLOCK; |
void |
517 |
remote_modifier_state = MapNumLockMask; |
restore_remote_modifiers(uint32 ev_time, uint8 scancode) |
518 |
} |
{ |
519 |
else |
key_translation dummy; |
520 |
|
|
521 |
|
if (is_modifier(scancode)) |
522 |
|
return; |
523 |
|
|
524 |
|
dummy.scancode = 0; |
525 |
|
dummy.modifiers = saved_remote_modifier_state; |
526 |
|
ensure_remote_modifiers(ev_time, dummy); |
527 |
|
} |
528 |
|
|
529 |
|
void |
530 |
|
ensure_remote_modifiers(uint32 ev_time, key_translation tr) |
531 |
|
{ |
532 |
|
/* If this key is a modifier, do nothing */ |
533 |
|
if (is_modifier(tr.scancode)) |
534 |
|
return; |
535 |
|
|
536 |
|
if (!g_numlock_sync) |
537 |
|
{ |
538 |
|
/* NumLock */ |
539 |
|
if (MASK_HAS_BITS(tr.modifiers, MapNumLockMask) |
540 |
|
!= MASK_HAS_BITS(remote_modifier_state, MapNumLockMask)) |
541 |
{ |
{ |
542 |
DEBUG_KBD(("Remote NumLock state is incorrect, deactivating NumLock.\n")); |
/* The remote modifier state is not correct */ |
543 |
new_remote_state = 0; |
uint16 new_remote_state; |
|
remote_modifier_state = 0; |
|
|
} |
|
544 |
|
|
545 |
rdp_send_input(0, RDP_INPUT_SYNCHRONIZE, 0, new_remote_state, 0); |
if (MASK_HAS_BITS(tr.modifiers, MapNumLockMask)) |
546 |
|
{ |
547 |
|
DEBUG_KBD(("Remote NumLock state is incorrect, activating NumLock.\n")); |
548 |
|
new_remote_state = KBD_FLAG_NUMLOCK; |
549 |
|
remote_modifier_state = MapNumLockMask; |
550 |
|
} |
551 |
|
else |
552 |
|
{ |
553 |
|
DEBUG_KBD(("Remote NumLock state is incorrect, deactivating NumLock.\n")); |
554 |
|
new_remote_state = 0; |
555 |
|
remote_modifier_state = 0; |
556 |
|
} |
557 |
|
|
558 |
|
rdp_send_input(0, RDP_INPUT_SYNCHRONIZE, 0, new_remote_state, 0); |
559 |
|
} |
560 |
} |
} |
561 |
|
|
562 |
|
|
563 |
/* 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. */ |
564 |
if (MASK_HAS_BITS(tr.modifiers, MapShiftMask) |
if (MASK_HAS_BITS(tr.modifiers, MapShiftMask) |
565 |
!= MASK_HAS_BITS(remote_modifier_state, MapShiftMask)) |
!= MASK_HAS_BITS(remote_modifier_state, MapShiftMask)) |
608 |
} |
} |
609 |
|
|
610 |
|
|
611 |
|
unsigned int |
612 |
|
read_keyboard_state() |
613 |
|
{ |
614 |
|
#ifdef RDP2VNC |
615 |
|
return 0; |
616 |
|
#else |
617 |
|
unsigned int state; |
618 |
|
Window wdummy; |
619 |
|
int dummy; |
620 |
|
|
621 |
|
XQueryPointer(g_display, g_wnd, &wdummy, &wdummy, &dummy, &dummy, &dummy, &dummy, &state); |
622 |
|
return state; |
623 |
|
#endif |
624 |
|
} |
625 |
|
|
626 |
|
|
627 |
|
uint16 |
628 |
|
ui_get_numlock_state(unsigned int state) |
629 |
|
{ |
630 |
|
uint16 numlock_state = 0; |
631 |
|
|
632 |
|
if (get_key_state(state, XK_Num_Lock)) |
633 |
|
numlock_state = KBD_FLAG_NUMLOCK; |
634 |
|
|
635 |
|
return numlock_state; |
636 |
|
} |
637 |
|
|
638 |
|
|
639 |
void |
void |
640 |
reset_modifier_keys(unsigned int state) |
reset_modifier_keys() |
641 |
{ |
{ |
642 |
|
unsigned int state = read_keyboard_state(); |
643 |
|
|
644 |
/* reset keys */ |
/* reset keys */ |
645 |
uint32 ev_time; |
uint32 ev_time; |
646 |
ev_time = time(NULL); |
ev_time = time(NULL); |
665 |
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LALT); |
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LALT); |
666 |
|
|
667 |
if (MASK_HAS_BITS(remote_modifier_state, MapRightAltMask) && |
if (MASK_HAS_BITS(remote_modifier_state, MapRightAltMask) && |
668 |
!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) |
669 |
|
&& !get_key_state(state, XK_ISO_Level3_Shift)) |
670 |
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RALT); |
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RALT); |
671 |
|
|
672 |
|
reset_winkey(ev_time); |
673 |
|
|
674 |
|
if (g_numlock_sync) |
675 |
|
rdp_send_input(ev_time, RDP_INPUT_SYNCHRONIZE, 0, ui_get_numlock_state(state), 0); |
676 |
} |
} |
677 |
|
|
678 |
|
|
714 |
case SCANCODE_CHAR_NUMLOCK: |
case SCANCODE_CHAR_NUMLOCK: |
715 |
/* KeyReleases for NumLocks are sent immediately. Toggle the |
/* KeyReleases for NumLocks are sent immediately. Toggle the |
716 |
modifier state only on Keypress */ |
modifier state only on Keypress */ |
717 |
if (pressed) |
if (pressed && !g_numlock_sync) |
718 |
{ |
{ |
719 |
BOOL newNumLockState; |
BOOL newNumLockState; |
720 |
newNumLockState = |
newNumLockState = |
723 |
MASK_CHANGE_BIT(remote_modifier_state, |
MASK_CHANGE_BIT(remote_modifier_state, |
724 |
MapNumLockMask, newNumLockState); |
MapNumLockMask, newNumLockState); |
725 |
} |
} |
|
break; |
|
726 |
} |
} |
727 |
|
|
728 |
#ifdef WITH_DEBUG_KBD |
#ifdef WITH_DEBUG_KBD |