[PATCH] Correctly handle extended RDP keycodes

Hardening rdp.effort at gmail.com
Sat Apr 6 14:39:26 PDT 2013


The keycodes received by the FreeRDP server aren't evdev keycodes.
This patch adds the correct convertion to evdev keycodes. After the
patch all keys that are marked as extended in RDP packets become
functionnal (that's the case for the windows key).
Please note that this patch rely on some corrections that have been
pushed on the FreeRDP github tonight.
---
 src/compositor-rdp.c |   54 +++++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 42 insertions(+), 12 deletions(-)

diff --git a/src/compositor-rdp.c b/src/compositor-rdp.c
index 2caaa57..e50ed5b 100644
--- a/src/compositor-rdp.c
+++ b/src/compositor-rdp.c
@@ -36,6 +36,7 @@
 #include <freerdp/codec/color.h>
 #include <freerdp/codec/rfx.h>
 #include <freerdp/codec/nsc.h>
+#include <winpr/input.h>
 
 #include "compositor.h"
 #include "pixman-renderer.h"
@@ -603,9 +604,10 @@ xf_peer_capabilities(freerdp_peer* client)
 	return TRUE;
 }
 
+
 struct rdp_to_xkb_keyboard_layout {
-	UINT32	rdpLayoutCode;
-	char 	*xkbLayout;
+	UINT32 rdpLayoutCode;
+	char *xkbLayout;
 };
 
 /* picked from http://technet.microsoft.com/en-us/library/cc766503(WS.10).aspx */
@@ -621,14 +623,14 @@ static struct rdp_to_xkb_keyboard_layout rdp_keyboards[] = {
 
 /* taken from 2.2.7.1.6 Input Capability Set (TS_INPUT_CAPABILITYSET) */
 static char *rdp_keyboard_types[] = {
-	"",		/* 0: unused */
-	"", 	/* 1: IBM PC/XT or compatible (83-key) keyboard */
-	"", 	/* 2: Olivetti "ICO" (102-key) keyboard */
-	"", 	/* 3: IBM PC/AT (84-key) or similar keyboard */
+	"",	/* 0: unused */
+	"", /* 1: IBM PC/XT or compatible (83-key) keyboard */
+	"", /* 2: Olivetti "ICO" (102-key) keyboard */
+	"", /* 3: IBM PC/AT (84-key) or similar keyboard */
 	"pc102",/* 4: IBM enhanced (101- or 102-key) keyboard */
-	"", 	/* 5: Nokia 1050 and similar keyboards */
-	"",		/* 6: Nokia 9140 and similar keyboards */
-	""		/* 7: Japanese keyboard */
+	"", /* 5: Nokia 1050 and similar keyboards */
+	"",	/* 6: Nokia 9140 and similar keyboards */
+	""	/* 7: Japanese keyboard */
 };
 
 static BOOL
@@ -643,7 +645,6 @@ xf_peer_post_connect(freerdp_peer* client)
 	struct xkb_keymap *keymap;
 	int i;
 
-
 	peerCtx = (RdpPeerContext *)client->context;
 	c = peerCtx->rdpCompositor;
 	output = c->output;
@@ -778,9 +779,22 @@ xf_input_synchronize_event(rdpInput *input, UINT32 flags)
 	pixman_region32_fini(&damage);
 }
 
+extern DWORD KEYCODE_TO_VKCODE_EVDEV[];
+static uint32_t vk_to_keycode[256];
+static void
+init_vk_translator(void)
+{
+	int i;
+
+	memset(vk_to_keycode, 0, sizeof(vk_to_keycode));
+	for(i = 0; i < 256; i++)
+		vk_to_keycode[KEYCODE_TO_VKCODE_EVDEV[i] & 0xff] = i-8;
+}
+
 static void
 xf_input_keyboard_event(rdpInput *input, UINT16 flags, UINT16 code)
 {
+	uint32_t scan_code, vk_code, full_code;
 	enum wl_keyboard_key_state keyState;
 	RdpPeerContext *peerContext = (RdpPeerContext *)input->context;
 	int notify = 0;
@@ -793,9 +807,24 @@ xf_input_keyboard_event(rdpInput *input, UINT16 flags, UINT16 code)
 		notify = 1;
 	}
 
-	if(notify)
+	if(notify) {
+		full_code = code;
+		if(flags & KBD_FLAGS_EXTENDED)
+			full_code |= KBD_FLAGS_EXTENDED;
+
+		vk_code = GetVirtualKeyCodeFromVirtualScanCode(full_code, 4);
+		if(vk_code > 0xff) {
+			weston_log("invalid vk_code %x", vk_code);
+			return;
+		}
+		scan_code = vk_to_keycode[vk_code];
+
+
+		/*weston_log("code=%x ext=%d vk_code=%x scan_code=%x\n", code, (flags & KBD_FLAGS_EXTENDED) ? 1 : 0,
+				vk_code, scan_code);*/
 		notify_key(&peerContext->item.seat, weston_compositor_get_time(),
-					code, keyState,	STATE_UPDATE_AUTOMATIC);
+					scan_code, keyState, STATE_UPDATE_AUTOMATIC);
+	}
 }
 
 static void
@@ -986,6 +1015,7 @@ backend_init(struct wl_display *display, int *argc, char *argv[],
 
 	freerdp_get_version(&major, &minor, &revision);
 	weston_log("using FreeRDP version %d.%d.%d\n", major, minor, revision);
+	init_vk_translator();
 
 	const struct weston_option rdp_options[] = {
 		{ WESTON_OPTION_BOOLEAN, "env-socket", 0, &config.env_socket },
-- 
1.7.10.4



More information about the wayland-devel mailing list