--- sourceforge.net/trunk/rdesktop/xkeymap.c 2003/08/31 19:45:56 452 +++ sourceforge.net/trunk/rdesktop/xkeymap.c 2004/06/01 08:00:58 705 @@ -1,7 +1,9 @@ /* rdesktop: A Remote Desktop Protocol client. User interface services - X keyboard mapping + Copyright (C) Matthew Chapman 1999-2002 + Copyright (C) Peter Astrand 2003 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -36,11 +38,13 @@ #define KEYMAP_MAX_LINE_LENGTH 80 extern Display *g_display; +extern Window g_wnd; extern char keymapname[16]; extern int keylayout; extern int g_win_button_size; extern BOOL g_enable_compose; extern BOOL g_use_rdp5; +extern BOOL g_numlock_sync; static BOOL keymap_loaded; static key_translation keymap[KEYMAP_SIZE]; @@ -278,6 +282,17 @@ } } +static void +reset_winkey(uint32 ev_time) +{ + if (g_use_rdp5) + { + /* For some reason, it seems to suffice to release + *either* the left or right winkey. */ + rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LWIN); + } +} + /* Handles, for example, multi-scancode keypresses (which is not possible via keymap-files) */ BOOL @@ -308,6 +323,7 @@ } /* No release sequence */ return True; + break; case XK_Pause: /* According to MS Keyboard Scan Code @@ -334,24 +350,34 @@ 0x1d, 0); } return True; + break; case XK_Meta_L: /* Windows keys */ case XK_Super_L: case XK_Hyper_L: send_winkey(ev_time, pressed, True); return True; + break; case XK_Meta_R: case XK_Super_R: case XK_Hyper_R: send_winkey(ev_time, pressed, False); return True; + break; case XK_space: /* Prevent access to the Windows system menu in single app mode */ if (g_win_button_size && (get_key_state(state, XK_Alt_L) || get_key_state(state, XK_Alt_R))) return True; + break; + case XK_Num_Lock: + /* FIXME: We might want to do RDP_INPUT_SYNCHRONIZE here, if g_numlock_sync */ + if (!g_numlock_sync) + /* Inhibit */ + return True; + break; } return False; @@ -448,17 +474,44 @@ return ksname; } +static BOOL +is_modifier(uint8 scancode) +{ + switch (scancode) + { + case SCANCODE_CHAR_LSHIFT: + case SCANCODE_CHAR_RSHIFT: + case SCANCODE_CHAR_LCTRL: + case SCANCODE_CHAR_RCTRL: + case SCANCODE_CHAR_LALT: + case SCANCODE_CHAR_RALT: + case SCANCODE_CHAR_LWIN: + case SCANCODE_CHAR_RWIN: + case SCANCODE_CHAR_NUMLOCK: + return True; + default: + break; + } + return False; +} + void -save_remote_modifiers() +save_remote_modifiers(uint8 scancode) { + if (is_modifier(scancode)) + return; + saved_remote_modifier_state = remote_modifier_state; } void -restore_remote_modifiers(uint32 ev_time) +restore_remote_modifiers(uint32 ev_time, uint8 scancode) { key_translation dummy; + if (is_modifier(scancode)) + return; + dummy.scancode = 0; dummy.modifiers = saved_remote_modifier_state; ensure_remote_modifiers(ev_time, dummy); @@ -468,45 +521,36 @@ ensure_remote_modifiers(uint32 ev_time, key_translation tr) { /* If this key is a modifier, do nothing */ - switch (tr.scancode) - { - case SCANCODE_CHAR_LSHIFT: - case SCANCODE_CHAR_RSHIFT: - case SCANCODE_CHAR_LCTRL: - case SCANCODE_CHAR_RCTRL: - case SCANCODE_CHAR_LALT: - case SCANCODE_CHAR_RALT: - case SCANCODE_CHAR_LWIN: - case SCANCODE_CHAR_RWIN: - case SCANCODE_CHAR_NUMLOCK: - return; - default: - break; - } + if (is_modifier(tr.scancode)) + return; - /* NumLock */ - if (MASK_HAS_BITS(tr.modifiers, MapNumLockMask) - != MASK_HAS_BITS(remote_modifier_state, MapNumLockMask)) + if (!g_numlock_sync) { - /* The remote modifier state is not correct */ - uint16 new_remote_state; - - if (MASK_HAS_BITS(tr.modifiers, MapNumLockMask)) - { - DEBUG_KBD(("Remote NumLock state is incorrect, activating NumLock.\n")); - new_remote_state = KBD_FLAG_NUMLOCK; - remote_modifier_state = MapNumLockMask; - } - else + /* NumLock */ + if (MASK_HAS_BITS(tr.modifiers, MapNumLockMask) + != MASK_HAS_BITS(remote_modifier_state, MapNumLockMask)) { - DEBUG_KBD(("Remote NumLock state is incorrect, deactivating NumLock.\n")); - new_remote_state = 0; - remote_modifier_state = 0; - } + /* The remote modifier state is not correct */ + uint16 new_remote_state; - rdp_send_input(0, RDP_INPUT_SYNCHRONIZE, 0, 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; + remote_modifier_state = MapNumLockMask; + } + else + { + DEBUG_KBD(("Remote NumLock state is incorrect, deactivating NumLock.\n")); + new_remote_state = 0; + remote_modifier_state = 0; + } + + rdp_send_input(0, RDP_INPUT_SYNCHRONIZE, 0, new_remote_state, 0); + } } + /* Shift. Left shift and right shift are treated as equal; either is fine. */ if (MASK_HAS_BITS(tr.modifiers, MapShiftMask) != MASK_HAS_BITS(remote_modifier_state, MapShiftMask)) @@ -555,9 +599,39 @@ } +unsigned int +read_keyboard_state() +{ +#ifdef RDP2VNC + return 0; +#else + unsigned int state; + Window wdummy; + int dummy; + + XQueryPointer(g_display, g_wnd, &wdummy, &wdummy, &dummy, &dummy, &dummy, &dummy, &state); + return state; +#endif +} + + +uint16 +ui_get_numlock_state(unsigned int state) +{ + uint16 numlock_state = 0; + + if (get_key_state(state, XK_Num_Lock)) + numlock_state = KBD_FLAG_NUMLOCK; + + return numlock_state; +} + + void -reset_modifier_keys(unsigned int state) +reset_modifier_keys() { + unsigned int state = read_keyboard_state(); + /* reset keys */ uint32 ev_time; ev_time = time(NULL); @@ -582,8 +656,13 @@ rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LALT); if (MASK_HAS_BITS(remote_modifier_state, MapRightAltMask) && - !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) && !get_key_state(state, XK_ISO_Level3_Shift)) rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RALT); + + reset_winkey(ev_time); + + if (g_numlock_sync) + rdp_send_input(ev_time, RDP_INPUT_SYNCHRONIZE, 0, ui_get_numlock_state(state), 0); } @@ -625,7 +704,7 @@ case SCANCODE_CHAR_NUMLOCK: /* KeyReleases for NumLocks are sent immediately. Toggle the modifier state only on Keypress */ - if (pressed) + if (pressed && !g_numlock_sync) { BOOL newNumLockState; newNumLockState = @@ -634,7 +713,6 @@ MASK_CHANGE_BIT(remote_modifier_state, MapNumLockMask, newNumLockState); } - break; } #ifdef WITH_DEBUG_KBD