From 3e4b43492507a360959e3a1b3c5cba2e03e6d05d Mon Sep 17 00:00:00 2001 From: Mads Kiilerich Date: Thu, 29 Mar 2012 01:12:48 +0200 Subject: [PATCH] clients: use rdp scancode definitions when sending key events This also introduces a slightly more high-level convenience function for sending key events. The existing function where an RDP protocol flag field has to be encoded by the caller is very lowlevel ... and a bad fit for fastpath input. That could use a refactoring. --- client/DirectFB/df_event.c | 10 +---- client/Windows/wf_event.c | 60 ++++++++++++----------------- client/X11/xf_keyboard.c | 26 +++++-------- include/freerdp/input.h | 7 ++++ include/freerdp/keyboard_scancode.h | 6 ++- 5 files changed, 47 insertions(+), 62 deletions(-) diff --git a/client/DirectFB/df_event.c b/client/DirectFB/df_event.c index 3b1048684..d6814d084 100644 --- a/client/DirectFB/df_event.c +++ b/client/DirectFB/df_event.c @@ -190,10 +190,7 @@ void df_send_mouse_wheel_event(rdpInput* input, sint16 axisrel, uint16 x, uint16 void df_send_keyboard_event(rdpInput* input, boolean down, uint8 keycode, uint8 function) { - uint16 flags; uint8 vkcode; - uint8 scancode; - boolean extended; RDP_SCANCODE rdp_scancode; if (keycode) @@ -204,13 +201,8 @@ void df_send_keyboard_event(rdpInput* input, boolean down, uint8 keycode, uint8 return; rdp_scancode = freerdp_keyboard_get_rdp_scancode_from_virtual_key_code(vkcode); - scancode = rdp_scancode_code(rdp_scancode); - extended = rdp_scancode_extended(rdp_scancode); - flags = (extended) ? KBD_FLAGS_EXTENDED : 0; - flags |= (down) ? KBD_FLAGS_DOWN : KBD_FLAGS_RELEASE; - - input->KeyboardEvent(input, flags, scancode); + freerdp_input_send_keyboard_event_2(input, down, rdp_scancode); } boolean df_event_process(freerdp* instance, DFBEvent* event) diff --git a/client/Windows/wf_event.c b/client/Windows/wf_event.c index 51dcd71cf..22bbebe45 100644 --- a/client/Windows/wf_event.c +++ b/client/Windows/wf_event.c @@ -33,11 +33,9 @@ extern HCURSOR g_default_cursor; LRESULT CALLBACK wf_ll_kbd_proc(int nCode, WPARAM wParam, LPARAM lParam) { - DWORD flags; wfInfo* wfi; - uint8 scanCode; + RDP_SCANCODE rdp_scancode; rdpInput* input; - uint16 kbdFlags; PKBDLLHOOKSTRUCT p; DEBUG_KBD("Low-level keyboard hook, hWnd %X nCode %X wParam %X", g_focus_hWnd, nCode, wParam); @@ -52,13 +50,11 @@ LRESULT CALLBACK wf_ll_kbd_proc(int nCode, WPARAM wParam, LPARAM lParam) case WM_SYSKEYUP: wfi = (wfInfo*) GetWindowLongPtr(g_focus_hWnd, GWLP_USERDATA); p = (PKBDLLHOOKSTRUCT) lParam; - scanCode = (uint8) p->scanCode; input = wfi->instance->input; - flags = p->flags; - kbdFlags = 0; + rdp_scancode = mk_rdp_scancode((uint8) p->scanCode, p->flags & LLKHF_EXTENDED); DEBUG_KBD("keydown %d scanCode %04X flags %02X vkCode %02X", - (wParam == WM_KEYDOWN), scanCode, flags, p->vkCode); + (wParam == WM_KEYDOWN), (uint8) p->scanCode, p->flags, p->vkCode); if (wfi->fs_toggle && ((p->vkCode == VK_RETURN) || (p->vkCode == VK_CANCEL)) && @@ -70,44 +66,38 @@ LRESULT CALLBACK wf_ll_kbd_proc(int nCode, WPARAM wParam, LPARAM lParam) return 1; } - if (scanCode == 0x45) /* NumLock-ish */ + if (rdp_scancode == RDP_SCANCODE_NUMLOCK_EXTENDED) { - if (flags & LLKHF_EXTENDED) + /* Windows sends NumLock as extended - rdp doesn't */ + DEBUG_KBD("hack: NumLock (x45) should not be extended"); + rdp_scancode = RDP_SCANCODE_NUMLOCK; + } + else if (rdp_scancode == RDP_SCANCODE_NUMLOCK) + { + /* Windows sends Pause as if it was a RDP NumLock (handled above). + * It must however be sent as a one-shot Ctrl+NumLock */ + if (wParam == WM_KEYDOWN) { - /* Windows sends NumLock as extended - rdp doesn't */ - DEBUG_KBD("hack: NumLock (x45) should not be extended"); - flags &= ~LLKHF_EXTENDED; + DEBUG_KBD("Pause, sent as Ctrl+NumLock"); + freerdp_input_send_keyboard_event_2(input, true, RDP_SCANCODE_LCONTROL); + freerdp_input_send_keyboard_event_2(input, true, RDP_SCANCODE_NUMLOCK); + freerdp_input_send_keyboard_event_2(input, false, RDP_SCANCODE_LCONTROL); + freerdp_input_send_keyboard_event_2(input, false, RDP_SCANCODE_NUMLOCK); } else { - /* Windows sends Pause as if it was a RDP NumLock (handled above). - * It must however be sent as a one-shot Ctrl+NumLock */ - if (wParam == WM_KEYDOWN) - { - DEBUG_KBD("Pause, sent as Ctrl+NumLock"); - input->KeyboardEvent(input, KBD_FLAGS_DOWN, 0x1D); /* Ctrl down */ - input->KeyboardEvent(input, KBD_FLAGS_DOWN, 0x45); /* NumLock down */ - input->KeyboardEvent(input, KBD_FLAGS_RELEASE, 0x1D); /* Ctrl up */ - input->KeyboardEvent(input, KBD_FLAGS_RELEASE, 0x45); /* NumLock up */ - } - else - { - DEBUG_KBD("Pause up"); - } - - return 1; + DEBUG_KBD("Pause up"); } - } - if ((scanCode == 0x36) && (flags & LLKHF_EXTENDED)) + return 1; + } + else if (rdp_scancode == RDP_SCANCODE_RSHIFT_EXTENDED) { - DEBUG_KBD("hack: right shift (x36) should not be extended"); - flags &= ~LLKHF_EXTENDED; + DEBUG_KBD("right shift (x36) should not be extended"); + rdp_scancode = RDP_SCANCODE_RSHIFT; } - kbdFlags |= (flags & LLKHF_UP) ? KBD_FLAGS_RELEASE : KBD_FLAGS_DOWN; - kbdFlags |= (flags & LLKHF_EXTENDED) ? KBD_FLAGS_EXTENDED : 0; - input->KeyboardEvent(input, kbdFlags, scanCode); + freerdp_input_send_keyboard_event_2(input, !(flags & LLKHF_UP), rdp_scancode); if (p->vkCode == VK_CAPITAL) DEBUG_KBD("caps lock is processed on client side too to toggle caps lock indicator"); diff --git a/client/X11/xf_keyboard.c b/client/X11/xf_keyboard.c index 3ebf3aff7..2a0e149f2 100644 --- a/client/X11/xf_keyboard.c +++ b/client/X11/xf_keyboard.c @@ -58,41 +58,33 @@ boolean xf_kbd_key_pressed(xfInfo* xfi, KeySym keysym) void xf_kbd_send_key(xfInfo* xfi, boolean down, uint8 keycode) { - uint16 flags; RDP_SCANCODE rdp_scancode; - uint8 scancode; - boolean extended; rdpInput* input; input = xfi->instance->input; rdp_scancode = freerdp_keyboard_get_rdp_scancode_from_x11_keycode(keycode); - scancode = rdp_scancode_code(rdp_scancode); - extended = rdp_scancode_extended(rdp_scancode); - if (scancode == 0) + if (rdp_scancode == RDP_SCANCODE_UNKNOWN) { - /* unknown key */ + printf("Unknown key with X keycode 0x%02x\n", keycode); } - else if ((scancode == 0x46) && extended && + else if (rdp_scancode == RDP_SCANCODE_PAUSE && !xf_kbd_key_pressed(xfi, XK_Control_L) && !xf_kbd_key_pressed(xfi, XK_Control_R)) { /* Pause without Ctrl has to be sent as Ctrl + NumLock. */ if (down) { - input->KeyboardEvent(input, KBD_FLAGS_DOWN, 0x1D); /* Ctrl down */ - input->KeyboardEvent(input, KBD_FLAGS_DOWN, 0x45); /* NumLock down */ - input->KeyboardEvent(input, KBD_FLAGS_RELEASE, 0x1D); /* Ctrl up */ - input->KeyboardEvent(input, KBD_FLAGS_RELEASE, 0x45); /* NumLock up */ + freerdp_input_send_keyboard_event_2(input, true, RDP_SCANCODE_LCONTROL); + freerdp_input_send_keyboard_event_2(input, true, RDP_SCANCODE_NUMLOCK); + freerdp_input_send_keyboard_event_2(input, false, RDP_SCANCODE_LCONTROL); + freerdp_input_send_keyboard_event_2(input, false, RDP_SCANCODE_NUMLOCK); } } else { - flags = (extended) ? KBD_FLAGS_EXTENDED : 0; - flags |= (down) ? KBD_FLAGS_DOWN : KBD_FLAGS_RELEASE; + freerdp_input_send_keyboard_event_2(input, down, rdp_scancode); - input->KeyboardEvent(input, flags, scancode); - - if ((scancode == 0x3A) && (down == false)) /* caps lock was released */ + if ((rdp_scancode == RDP_SCANCODE_CAPITAL) && (down == false)) { uint32 syncFlags; syncFlags = xf_kbd_get_toggle_keys_state(xfi); diff --git a/include/freerdp/input.h b/include/freerdp/input.h index bc5ac63cc..0d8f0bf5a 100644 --- a/include/freerdp/input.h +++ b/include/freerdp/input.h @@ -24,6 +24,7 @@ typedef struct rdp_input rdpInput; #include #include +#include /* keyboard Flags */ #define KBD_FLAGS_EXTENDED 0x0100 @@ -79,4 +80,10 @@ FREERDP_API void freerdp_input_send_unicode_keyboard_event(rdpInput* input, uint FREERDP_API void freerdp_input_send_mouse_event(rdpInput* input, uint16 flags, uint16 x, uint16 y); FREERDP_API void freerdp_input_send_extended_mouse_event(rdpInput* input, uint16 flags, uint16 x, uint16 y); +#define freerdp_input_send_keyboard_event_2(input, down, rdp_scancode) \ + freerdp_input_send_keyboard_event(input, \ + (rdp_scancode_extended(rdp_scancode) ? KBD_FLAGS_EXTENDED : 0) | \ + ((down) ? KBD_FLAGS_DOWN : KBD_FLAGS_RELEASE), \ + rdp_scancode_code(rdp_scancode)) + #endif /* __INPUT_API_H */ diff --git a/include/freerdp/keyboard_scancode.h b/include/freerdp/keyboard_scancode.h index 50ea3761e..ee4a8b2fe 100644 --- a/include/freerdp/keyboard_scancode.h +++ b/include/freerdp/keyboard_scancode.h @@ -157,7 +157,7 @@ typedef uint32 RDP_SCANCODE; /* Our own representation of a RDP protocol scancod #define RDP_SCANCODE_F22 mk_rdp_scancode(0x6D, false) /* VK_F22 */ #define RDP_SCANCODE_F23 mk_rdp_scancode(0x6E, false) /* VK_F23 */ #define RDP_SCANCODE_F24 mk_rdp_scancode(0x6F, false) /* VK_F24 */ -#define RDP_SCANCODE_NUMLOCK mk_rdp_scancode(0x45, false) /* VK_NUMLOCK */ +#define RDP_SCANCODE_NUMLOCK mk_rdp_scancode(0x45, false) /* VK_NUMLOCK */ /* Note: when this seems to appear in PKBDLLHOOKSTRUCT it means Pause which must be sent as Ctrl + NumLock */ #define RDP_SCANCODE_SCROLL mk_rdp_scancode(0x46, false) /* VK_SCROLL */ #define RDP_SCANCODE_LSHIFT mk_rdp_scancode(0x2A, false) /* VK_LSHIFT */ #define RDP_SCANCODE_RSHIFT mk_rdp_scancode(0x36, false) /* VK_RSHIFT */ @@ -210,4 +210,8 @@ typedef uint32 RDP_SCANCODE; /* Our own representation of a RDP protocol scancod /* #define RDP_SCANCODE_PA1 VK_PA1 */ /* #define RDP_SCANCODE_OEM_CLEAR VK_OEM_CLEAR */ +/* _not_ valid scancode, but this is what a windows PKBDLLHOOKSTRUCT for NumLock contains */ +#define RDP_SCANCODE_NUMLOCK_EXTENDED mk_rdp_scancode(0x45, true) /* should be RDP_SCANCODE_NUMLOCK */ +#define RDP_SCANCODE_RSHIFT_EXTENDED mk_rdp_scancode(0x36, true) /* should be RDP_SCANCODE_RSHIFT */ + #endif /* __FREERDP_LOCALE_KEYBOARD_RDP_SCANCODE_H */