--- sourceforge.net/trunk/rdesktop/xkeymap.c 2003/09/15 08:03:30 470 +++ sourceforge.net/trunk/rdesktop/xkeymap.c 2005/08/03 08:25:32 957 @@ -2,7 +2,7 @@ rdesktop: A Remote Desktop Protocol client. User interface services - X keyboard mapping - Copyright (C) Matthew Chapman 1999-2002 + Copyright (C) Matthew Chapman 1999-2005 Copyright (C) Peter Astrand 2003 This program is free software; you can redistribute it and/or modify @@ -38,24 +38,41 @@ #define KEYMAP_MAX_LINE_LENGTH 80 extern Display *g_display; +extern Window g_wnd; extern char keymapname[16]; -extern int keylayout; +extern int g_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]; +static key_translation *keymap[KEYMAP_SIZE]; static int min_keycode; static uint16 remote_modifier_state = 0; static uint16 saved_remote_modifier_state = 0; static void update_modifier_state(uint8 scancode, BOOL pressed); +/* Free key_translation structure, including linked list */ +static void +free_key_translation(key_translation * ptr) +{ + key_translation *next; + + while (ptr) + { + next = ptr->next; + xfree(ptr); + ptr = next; + } +} + static void add_to_keymap(char *keyname, uint8 scancode, uint16 modifiers, char *mapname) { KeySym keysym; + key_translation *tr; keysym = XStringToKeysym(keyname); if (keysym == NoSymbol) @@ -67,19 +84,142 @@ DEBUG_KBD(("Adding translation, keysym=0x%x, scancode=0x%x, " "modifiers=0x%x\n", (unsigned int) keysym, scancode, modifiers)); - keymap[keysym & KEYMAP_MASK].scancode = scancode; - keymap[keysym & KEYMAP_MASK].modifiers = modifiers; + tr = (key_translation *) xmalloc(sizeof(key_translation)); + memset(tr, 0, sizeof(key_translation)); + tr->scancode = scancode; + tr->modifiers = modifiers; + free_key_translation(keymap[keysym & KEYMAP_MASK]); + keymap[keysym & KEYMAP_MASK] = tr; return; } +static void +add_sequence(char *rest, char *mapname) +{ + KeySym keysym; + key_translation *tr, **prev_next; + size_t chars; + char keyname[KEYMAP_MAX_LINE_LENGTH]; + + /* Skip over whitespace after the sequence keyword */ + chars = strspn(rest, " \t"); + rest += chars; + + /* Fetch the keysym name */ + chars = strcspn(rest, " \t\0"); + STRNCPY(keyname, rest, chars + 1); + rest += chars; + + keysym = XStringToKeysym(keyname); + if (keysym == NoSymbol) + { + DEBUG_KBD(("Bad keysym \"%s\" in keymap %s (ignoring line)\n", keyname, mapname)); + return; + } + + + DEBUG_KBD(("Adding sequence for keysym (0x%lx, %s) -> ", keysym, keyname)); + + free_key_translation(keymap[keysym & KEYMAP_MASK]); + prev_next = &keymap[keysym & KEYMAP_MASK]; + + while (*rest) + { + /* Skip whitespace */ + chars = strspn(rest, " \t"); + rest += chars; + + /* Fetch the keysym name */ + chars = strcspn(rest, " \t\0"); + STRNCPY(keyname, rest, chars + 1); + rest += chars; + + keysym = XStringToKeysym(keyname); + if (keysym == NoSymbol) + { + DEBUG_KBD(("Bad keysym \"%s\" in keymap %s (ignoring line)\n", keyname, + mapname)); + return; + } + + /* Allocate space for key_translation structure */ + tr = (key_translation *) xmalloc(sizeof(key_translation)); + memset(tr, 0, sizeof(key_translation)); + *prev_next = tr; + prev_next = &tr->next; + tr->seq_keysym = keysym; + + DEBUG_KBD(("0x%x, ", (unsigned int) keysym)); + } + DEBUG_KBD(("\n")); +} + +/* Joins two path components. The result should be freed with + xfree(). */ +static char * +pathjoin(const char *a, const char *b) +{ + char *result; + result = xmalloc(PATH_MAX * 2 + 1); + + if (b[0] == '/') + { + strncpy(result, b, PATH_MAX); + } + else + { + strncpy(result, a, PATH_MAX); + strcat(result, "/"); + strncat(result, b, PATH_MAX); + } + return result; +} + +/* Try to open a keymap with fopen() */ +FILE * +xkeymap_open(const char *filename) +{ + char *path1, *path2; + char *home; + FILE *fp; + + /* Try KEYMAP_PATH */ + path1 = pathjoin(KEYMAP_PATH, filename); + fp = fopen(path1, "r"); + xfree(path1); + if (fp) + return fp; + + /* Try ~/.rdesktop/keymaps */ + home = getenv("HOME"); + if (home) + { + path1 = pathjoin(home, ".rdesktop/keymaps"); + path2 = pathjoin(path1, filename); + xfree(path1); + fp = fopen(path2, "r"); + xfree(path2); + if (fp) + return fp; + } + + /* Try current directory, in case we are running from the source + tree */ + path1 = pathjoin("keymaps", filename); + fp = fopen(path1, "r"); + xfree(path1); + if (fp) + return fp; + + return NULL; +} static BOOL xkeymap_read(char *mapname) { FILE *fp; char line[KEYMAP_MAX_LINE_LENGTH]; - char path[PATH_MAX], inplace_path[PATH_MAX]; unsigned int line_num = 0; unsigned int line_length = 0; char *keyname, *p; @@ -87,23 +227,11 @@ uint8 scancode; uint16 modifiers; - - strcpy(path, KEYMAP_PATH); - strncat(path, mapname, sizeof(path) - sizeof(KEYMAP_PATH)); - - fp = fopen(path, "r"); + fp = xkeymap_open(mapname); if (fp == NULL) { - /* in case we are running from the source tree */ - strcpy(inplace_path, "keymaps/"); - strncat(inplace_path, mapname, sizeof(inplace_path) - sizeof("keymaps/")); - - fp = fopen(inplace_path, "r"); - if (fp == NULL) - { - error("Failed to open keymap %s\n", path); - return False; - } + error("Failed to open keymap %s\n", mapname); + return False; } /* FIXME: More tolerant on white space */ @@ -135,8 +263,8 @@ /* map */ if (strncmp(line, "map ", 4) == 0) { - keylayout = strtol(line + 4, NULL, 16); - DEBUG_KBD(("Keylayout 0x%x\n", keylayout)); + g_keylayout = strtol(line + 4, NULL, 16); + DEBUG_KBD(("Keylayout 0x%x\n", g_keylayout)); continue; } @@ -148,6 +276,13 @@ continue; } + /* sequence */ + if (strncmp(line, "sequence", 8) == 0) + { + add_sequence(line + 8, mapname); + continue; + } + /* Comment */ if (line[0] == '#') { @@ -280,8 +415,18 @@ } } -/* Handles, for example, multi-scancode keypresses (which is not - possible via keymap-files) */ +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); + } +} + +/* Handle special key combinations */ BOOL handle_special_keys(uint32 keysym, unsigned int state, uint32 ev_time, BOOL pressed) { @@ -310,6 +455,7 @@ } /* No release sequence */ return True; + break; case XK_Pause: /* According to MS Keyboard Scan Code @@ -336,24 +482,38 @@ 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: + /* Synchronize on key release */ + if (g_numlock_sync && !pressed) + rdp_send_input(0, RDP_INPUT_SYNCHRONIZE, 0, + ui_get_numlock_state(read_keyboard_state()), 0); + + /* Inhibit */ + return True; + break; } return False; @@ -363,58 +523,116 @@ key_translation xkeymap_translate_key(uint32 keysym, unsigned int keycode, unsigned int state) { - key_translation tr = { 0, 0 }; + key_translation tr = { 0, 0, 0, 0 }; + key_translation *ptr; - tr = keymap[keysym & KEYMAP_MASK]; - - if (tr.modifiers & MapInhibitMask) + ptr = keymap[keysym & KEYMAP_MASK]; + if (ptr) { - DEBUG_KBD(("Inhibiting key\n")); - tr.scancode = 0; - return tr; - } + tr = *ptr; + if (tr.seq_keysym == 0) /* Normal scancode translation */ + { + if (tr.modifiers & MapInhibitMask) + { + DEBUG_KBD(("Inhibiting key\n")); + tr.scancode = 0; + return tr; + } + + if (tr.modifiers & MapLocalStateMask) + { + /* The modifiers to send for this key should be obtained + from the local state. Currently, only shift is implemented. */ + if (state & ShiftMask) + { + tr.modifiers = MapLeftShiftMask; + } + } + + if ((tr.modifiers & MapLeftShiftMask) + && ((remote_modifier_state & MapLeftCtrlMask) + || (remote_modifier_state & MapRightCtrlMask)) + && get_key_state(state, XK_Caps_Lock)) + { + DEBUG_KBD(("CapsLock + Ctrl pressed, releasing LeftShift\n")); + tr.modifiers ^= MapLeftShiftMask; + } - if (tr.modifiers & MapLocalStateMask) + DEBUG_KBD(("Found scancode translation, scancode=0x%x, modifiers=0x%x\n", + tr.scancode, tr.modifiers)); + } + } + else { - /* The modifiers to send for this key should be obtained - from the local state. Currently, only shift is implemented. */ - if (state & ShiftMask) + if (keymap_loaded) + warning("No translation for (keysym 0x%lx, %s)\n", keysym, + get_ksname(keysym)); + + /* not in keymap, try to interpret the raw scancode */ + if (((int) keycode >= min_keycode) && (keycode <= 0x60)) + { + tr.scancode = keycode - min_keycode; + + /* The modifiers to send for this key should be + obtained from the local state. Currently, only + shift is implemented. */ + if (state & ShiftMask) + { + tr.modifiers = MapLeftShiftMask; + } + + DEBUG_KBD(("Sending guessed scancode 0x%x\n", tr.scancode)); + } + else { - tr.modifiers = MapLeftShiftMask; + DEBUG_KBD(("No good guess for keycode 0x%x found\n", keycode)); } } - if (tr.scancode != 0) - { - DEBUG_KBD(("Found key translation, scancode=0x%x, modifiers=0x%x\n", - tr.scancode, tr.modifiers)); - return tr; - } + return tr; +} - if (keymap_loaded) - warning("No translation for (keysym 0x%lx, %s)\n", keysym, get_ksname(keysym)); +void +xkeymap_send_keys(uint32 keysym, unsigned int keycode, unsigned int state, uint32 ev_time, + BOOL pressed) +{ + key_translation tr, *ptr; + tr = xkeymap_translate_key(keysym, keycode, state); - /* not in keymap, try to interpret the raw scancode */ - if (((int) keycode >= min_keycode) && (keycode <= 0x60)) + if (tr.seq_keysym == 0) { - tr.scancode = keycode - min_keycode; + /* Scancode translation */ + if (tr.scancode == 0) + return; - /* The modifiers to send for this key should be - obtained from the local state. Currently, only - shift is implemented. */ - if (state & ShiftMask) + if (pressed) { - tr.modifiers = MapLeftShiftMask; + save_remote_modifiers(tr.scancode); + ensure_remote_modifiers(ev_time, tr); + rdp_send_scancode(ev_time, RDP_KEYPRESS, tr.scancode); + restore_remote_modifiers(ev_time, tr.scancode); } - - DEBUG_KBD(("Sending guessed scancode 0x%x\n", tr.scancode)); + else + { + rdp_send_scancode(ev_time, RDP_KEYRELEASE, tr.scancode); + } + return; } - else + + /* Sequence, only on key down */ + if (pressed) { - DEBUG_KBD(("No good guess for keycode 0x%x found\n", keycode)); + ptr = &tr; + do + { + DEBUG_KBD(("Handling sequence element, keysym=0x%x\n", + (unsigned int) ptr->seq_keysym)); + xkeymap_send_keys(ptr->seq_keysym, keycode, state, ev_time, True); + xkeymap_send_keys(ptr->seq_keysym, keycode, state, ev_time, False); + ptr = ptr->next; + } + while (ptr); } - - return tr; } uint16 @@ -500,29 +718,33 @@ 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; + + 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); + 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)) @@ -571,9 +793,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); @@ -598,8 +850,14 @@ 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); } @@ -641,7 +899,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 = @@ -650,7 +908,6 @@ MASK_CHANGE_BIT(remote_modifier_state, MapNumLockMask, newNumLockState); } - break; } #ifdef WITH_DEBUG_KBD