[PATCH 2/2] Add a FreeRds backend, take 2

Hardening rdp.effort at gmail.com
Sun Dec 8 14:38:46 PST 2013


FreeRDS is a FreeRDP based RDP server, the server handles incoming connections and
talks RDP with the peers. FreeRds cooperates with an "out-service": the out-service creates
the content to display, and FreeRds will take care of encoding the content in the
appropriate format (bitmapUpdate, remoteFx or NsCodec).
To communicate, they use a unix socket for passing commands, and a shared buffer for
the screen content. A vblank signal sent by FreeRds via the command channel allows
to share the framebuffer nicely.
This patch adds a backend to create a FreeRDS compositor and have weston being an out-service
for FreeRds.

This new version handles SuppressOutput messages sent by the client, so that the compositor does
not send frames when the client doesn't want some.
---
 configure.ac             |  10 +
 src/compositor-freerds.c | 894 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 904 insertions(+)
 create mode 100644 src/compositor-freerds.c

diff --git a/configure.ac b/configure.ac
index 8baef88..0c6111e 100644
--- a/configure.ac
+++ b/configure.ac
@@ -206,6 +206,15 @@ if test x$enable_rdp_compositor = xyes; then
   PKG_CHECK_MODULES(RDP_COMPOSITOR, [freerdp >= 1.1.0])
 fi
 
+AC_ARG_ENABLE([freerds-compositor], [  --enable-freerds-compositor],,
+              enable_freerds_compositor=no)
+AM_CONDITIONAL([ENABLE_FREERDS_COMPOSITOR],
+               [test x$enable_freerds_compositor = xyes])
+if test x$enable_freerds_compositor = xyes; then
+  AC_DEFINE([BUILD_FREERDS_COMPOSITOR], [1], [Build the FreeRDS compositor])
+  PKG_CHECK_MODULES(FREERDS_COMPOSITOR, [freerds-backend])
+fi
+
 AC_ARG_WITH(cairo,
 	    AS_HELP_STRING([--with-cairo=@<:@image|gl|glesv2@:>@]
 			   [Which Cairo renderer to use for the clients]),
@@ -526,6 +535,7 @@ AC_MSG_RESULT([
 	RPI Compositor			${enable_rpi_compositor}
 	FBDEV Compositor		${enable_fbdev_compositor}
 	RDP Compositor			${enable_rdp_compositor}
+	FreeRDS Compositor		${enable_freerds_compositor}
 
 	Raspberry Pi BCM headers	${have_bcm_host}
 
diff --git a/src/compositor-freerds.c b/src/compositor-freerds.c
new file mode 100644
index 0000000..8ddc58c
--- /dev/null
+++ b/src/compositor-freerds.c
@@ -0,0 +1,894 @@
+/**
+ * Copyright © 2013 Hardening <contact at hardening-consulting.com>
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that the above copyright notice appear in all copies and that both that
+ * copyright notice and this permission notice appear in supporting
+ * documentation, and that the name of the copyright holders not be used in
+ * advertising or publicity pertaining to distribution of the software
+ * without specific, written prior permission.  The copyright holders make
+ * no representations about the suitability of this software for any
+ * purpose.  It is provided "as is" without express or implied warranty.
+ *
+ * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
+ * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
+ * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
+ * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
+ * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include "config.h"
+
+#include <stdlib.h>
+#include <stdbool.h>
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <sys/shm.h>
+#include <sys/mman.h>
+#include <linux/input.h>
+
+#include <freerds/backend.h>
+
+#include <freerdp/freerdp.h>
+#include <freerdp/update.h>
+#include <freerdp/input.h>
+#include <freerdp/locale/keyboard.h>
+
+#include <winpr/input.h>
+#include <winpr/stream.h>
+
+#include "compositor.h"
+#include "pixman-renderer.h"
+
+
+#define DEFAULT_AXIS_STEP_DISTANCE wl_fixed_from_int(10)
+
+struct freerds_compositor_config {
+	int width;
+	int height;
+	char *freerds_named_pipe;
+};
+
+struct freerds_compositor;
+struct freerds_output;
+
+/** @brief state of the module protocol automata */
+enum StreamState {
+	STREAM_WAITING_COMMON_HEADER,
+	STREAM_WAITING_DATA
+};
+
+struct freerds_compositor {
+	struct weston_compositor base;
+
+	struct freerds_output *output;
+	struct weston_seat seat;
+
+	int listening_fd;
+	int	client_fd;
+	enum StreamState streamState;
+	wStream *in_stream;
+	wStream *out_stream;
+	int expected_bytes;
+	UINT32 keyboard_layout;
+	UINT32 keyboard_type;
+
+	RDS_MSG_COMMON header_common;
+	RDS_MSG_MOUSE_EVENT rds_mouse_event;
+	RDS_MSG_SCANCODE_KEYBOARD_EVENT rds_scancode;
+	RDS_MSG_VIRTUAL_KEYBOARD_EVENT rds_virtual_keyboard;
+
+	RDS_MSG_SHARED_FRAMEBUFFER rds_shared_fb;
+	RDS_MSG_BEGIN_UPDATE rds_begin_update;
+	RDS_MSG_END_UPDATE rds_end_update;
+	RDS_MSG_PAINT_RECT rds_paint_rect;
+	RDS_MSG_SET_SYSTEM_POINTER rds_set_system_pointer;
+	RDS_MSG_REFRESH_RECT rds_refresh_rect;
+};
+
+struct freerds_output {
+	struct weston_output base;
+	struct freerds_compositor *compositor;
+	struct wl_event_source *finish_frame_timer;
+
+	int shmid;
+	void *buffer;
+	pixman_image_t *shadow_surface;
+	pixman_region32_t damagedRegion;
+	bool waitingVBlank;
+	bool outputActive;
+};
+
+static void
+freerds_compositor_config_init(struct freerds_compositor_config *config) {
+	config->width = 640;
+	config->height = 480;
+	config->freerds_named_pipe = 0;
+}
+
+static int
+freerds_send_stream(struct freerds_compositor *c, wStream *s) {
+	int toWrite, written;
+	const char *ptr;
+
+	toWrite = Stream_Length(s);
+	ptr = (const char *)Stream_Buffer(s);
+	while (toWrite) {
+		written = write(c->client_fd, ptr, toWrite);
+		if (written <= 0)
+			return -1;
+		toWrite -= written;
+		ptr += written;
+	}
+	return 0;
+}
+
+#define ROUND_DOWN(V, B) ((V) - ((V) % (B)))
+#define ROUND_UP(V, B) ((V + (B - ((V) % B))))
+
+static void
+freerds_update_framebuffer(struct freerds_compositor *c, pixman_box32_t *rect) {
+	struct freerds_output *output = c->output;
+	unsigned char *src = (unsigned char *)pixman_image_get_data(output->shadow_surface) +
+			(pixman_image_get_stride(output->shadow_surface) * rect->y1) +
+			(rect->x1 * 4);
+	unsigned char *dst = (unsigned char *)output->buffer +
+			(c->rds_shared_fb.scanline * rect->y1) +
+			(rect->x1 * 4);
+	int widthBytes = (rect->x2 - rect->x1) * 4;
+	int y;
+
+	for (y = rect->y1; y < rect->y2; y++) {
+		memcpy(dst, src, widthBytes);
+		src += pixman_image_get_stride(output->shadow_surface);
+		dst += c->rds_shared_fb.scanline;
+	}
+}
+
+static void
+freerds_refresh_region(struct freerds_compositor *c, pixman_region32_t *region)
+{
+	int nrects, i;
+	pixman_box32_t *rect;
+	RDS_MSG_PAINT_RECT *paint = &c->rds_paint_rect;
+
+	Stream_SetPosition(c->out_stream, 0);
+	freerds_server_message_write(c->out_stream, (RDS_MSG_COMMON *)&c->rds_begin_update);
+
+	rect = pixman_region32_rectangles(region, &nrects);
+	for (i = 0; i < nrects; i++, rect++) {
+		//weston_log("refresh_rect(%d,%d,%d,%d)\n", rect->x1, rect->y1, rect->x2, rect->y2);
+		freerds_update_framebuffer(c, rect);
+
+		paint->nXSrc = paint->nLeftRect = ROUND_DOWN(rect->x1, 16);
+		paint->nYSrc = paint->nTopRect = ROUND_DOWN(rect->y1, 16);
+		paint->nWidth = ROUND_UP(rect->x2, 16) - paint->nLeftRect;
+		paint->nHeight = ROUND_UP(rect->y2, 16) - paint->nTopRect;
+		freerds_server_message_write(c->out_stream, (RDS_MSG_COMMON *)&c->rds_paint_rect);
+	}
+
+	freerds_server_message_write(c->out_stream, (RDS_MSG_COMMON *)&c->rds_end_update);
+
+	Stream_SealLength(c->out_stream);
+	freerds_send_stream(c, c->out_stream);
+
+	pixman_region32_clear(region);
+
+	c->output->waitingVBlank = true;
+}
+
+
+static void
+freerds_output_start_repaint_loop(struct weston_output *output)
+{
+	uint32_t msec;
+	struct timeval tv;
+
+	gettimeofday(&tv, NULL);
+	msec = tv.tv_sec * 1000 + tv.tv_usec / 1000;
+	weston_output_finish_frame(output, msec);
+}
+
+static int
+freerds_output_repaint(struct weston_output *output_base, pixman_region32_t *damage)
+{
+	struct freerds_output *output = container_of(output_base, struct freerds_output, base);
+	struct weston_compositor *ec = output->base.compositor;
+
+	pixman_region32_union(&output->damagedRegion, &output->damagedRegion, damage);
+
+	pixman_renderer_output_set_buffer(output_base, output->shadow_surface);
+	ec->renderer->repaint_output(&output->base, damage);
+
+	if (!output->waitingVBlank && output->outputActive)
+		freerds_refresh_region(output->compositor, &output->damagedRegion);
+
+	pixman_region32_subtract(&ec->primary_plane.damage, &ec->primary_plane.damage, damage);
+
+	wl_event_source_timer_update(output->finish_frame_timer, 16);
+	return 0;
+}
+
+static void
+freerds_output_destroy(struct weston_output *output_base)
+{
+	struct freerds_output *output = (struct freerds_output *)output_base;
+
+	wl_event_source_remove(output->finish_frame_timer);
+	free(output);
+}
+
+static int
+finish_frame_handler(void *data)
+{
+	freerds_output_start_repaint_loop(data);
+
+	return 1;
+}
+
+
+static int
+freerds_compositor_create_output(struct freerds_compositor *c, int width, int height)
+{
+	struct freerds_output *output;
+	struct wl_event_loop *loop;
+	struct weston_mode *currentMode, *next;
+	RDS_MSG_SHARED_FRAMEBUFFER *shared_fb;
+	RDS_MSG_PAINT_RECT *rect;
+	RDS_MSG_SET_SYSTEM_POINTER *system_pointer;
+
+	output = zalloc(sizeof *output);
+	if (output == NULL)
+		return -1;
+
+	wl_list_init(&output->base.mode_list);
+
+	currentMode = malloc(sizeof *currentMode);
+	if(!currentMode)
+		goto out_free_output;
+	currentMode->flags = WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
+	currentMode->width = width;
+	currentMode->height = height;
+	currentMode->refresh = 60;
+	wl_list_insert(&output->base.mode_list, &currentMode->link);
+
+	output->base.current_mode = output->base.native_mode = currentMode;
+	weston_output_init(&output->base, &c->base, 0, 0, width, height,
+			   WL_OUTPUT_TRANSFORM_NORMAL, 1);
+
+	output->base.make = "weston";
+	output->base.model = "freerds";
+
+	output->shmid = shmget(IPC_PRIVATE, currentMode->width * currentMode->height * 4,
+			IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR);
+	if (output->shmid < 0) {
+		weston_log("unable to create a SHM, error=%s\n", strerror(errno));
+		goto out_output;
+	}
+
+	output->buffer = shmat(output->shmid, 0, 0);
+	if (!output->buffer) {
+		weston_log("unable to attach to SHM, error=%s\n", strerror(errno));
+		goto out_shmid;
+	}
+
+	shared_fb = &c->rds_shared_fb;
+	shared_fb->type = RDS_SERVER_SHARED_FRAMEBUFFER;
+	shared_fb->width = width;
+	shared_fb->height = height;
+	shared_fb->bitsPerPixel = 32;
+	shared_fb->bytesPerPixel = 4;
+	shared_fb->attach = 1;
+	shared_fb->segmentId = output->shmid;
+	shared_fb->scanline = width * 4;
+
+	rect = &c->rds_paint_rect;
+	rect->type = RDS_SERVER_PAINT_RECT;
+	rect->fbSegmentId = output->shmid;
+	rect->bitmapData = NULL;
+	rect->bitmapDataLength = 0;
+	rect->framebuffer = 0;
+
+	system_pointer = &c->rds_set_system_pointer;
+	system_pointer->type = RDS_SERVER_SET_SYSTEM_POINTER;
+	system_pointer->ptrType = SYSPTR_NULL;
+
+	c->rds_begin_update.type = RDS_SERVER_BEGIN_UPDATE;
+	c->rds_end_update.type = RDS_SERVER_END_UPDATE;
+
+	output->waitingVBlank = true;
+	output->outputActive = true;
+
+	pixman_region32_init(&output->damagedRegion);
+	output->shadow_surface = pixman_image_create_bits(PIXMAN_a8r8g8b8,
+			width, height,
+		    NULL,
+		    width * 4);
+	if (output->shadow_surface == NULL) {
+		weston_log("Failed to create surface for frame buffer.\n");
+		goto out_shmid;
+	}
+
+	if (pixman_renderer_output_create(&output->base) < 0)
+		goto out_shadow_surface;
+
+	weston_output_move(&output->base, 0, 0);
+
+	loop = wl_display_get_event_loop(c->base.wl_display);
+	output->finish_frame_timer = wl_event_loop_add_timer(loop, finish_frame_handler, output);
+
+	output->base.start_repaint_loop = freerds_output_start_repaint_loop;
+	output->base.repaint = freerds_output_repaint;
+	output->base.destroy = freerds_output_destroy;
+	output->base.assign_planes = NULL;
+	output->base.set_backlight = NULL;
+	output->base.set_dpms = NULL;
+	output->compositor = c;
+	// TODO: output->base.switch_mode = freerds_switch_mode;
+	c->output = output;
+
+	wl_list_insert(c->base.output_list.prev, &output->base.link);
+	return 0;
+
+out_shadow_surface:
+	pixman_image_unref(output->shadow_surface);
+out_shmid:
+	shm_unlink(output->buffer);
+out_output:
+	weston_output_destroy(&output->base);
+
+	wl_list_for_each_safe(currentMode, next, &output->base.mode_list, link)
+		free(currentMode);
+out_free_output:
+	free(output);
+	return -1;
+}
+
+static void
+freerds_restore(struct weston_compositor *ec)
+{
+}
+
+static void
+freerds_destroy(struct weston_compositor *ec)
+{
+	ec->renderer->destroy(ec);
+	weston_compositor_shutdown(ec);
+
+	free(ec);
+}
+
+
+static void
+freerds_mouse_event(struct freerds_compositor *c, DWORD x, DWORD y, DWORD flags) {
+	wl_fixed_t wl_x, wl_y, axis;
+	uint32_t button = 0;
+
+	//weston_log("mouse event: x=%ld y=%ld flags=0x%x\n", x, y, flags);
+	if (flags & PTR_FLAGS_MOVE) {
+		if((int)x < c->output->base.width && (int)y < c->output->base.height) {
+			wl_x = wl_fixed_from_int((int)x);
+			wl_y = wl_fixed_from_int((int)y);
+			notify_motion_absolute(&c->seat, weston_compositor_get_time(), wl_x, wl_y);
+		}
+	}
+
+	if (flags & PTR_FLAGS_BUTTON1)
+		button = BTN_LEFT;
+	else if (flags & PTR_FLAGS_BUTTON2)
+		button = BTN_RIGHT;
+	else if (flags & PTR_FLAGS_BUTTON3)
+		button = BTN_MIDDLE;
+
+	if(button) {
+		notify_button(&c->seat, weston_compositor_get_time(), button,
+			(flags & PTR_FLAGS_DOWN) ? WL_POINTER_BUTTON_STATE_PRESSED : WL_POINTER_BUTTON_STATE_RELEASED
+		);
+	}
+
+	if (flags & PTR_FLAGS_WHEEL) {
+		/* DEFAULT_AXIS_STEP_DISTANCE is stolen from compositor-x11.c
+		 * The RDP specs says the lower bits of flags contains the "the number of rotation
+		 * units the mouse wheel was rotated".
+		 *
+		 * http://blogs.msdn.com/b/oldnewthing/archive/2013/01/23/10387366.aspx explains the 120 value
+		 */
+		axis = (DEFAULT_AXIS_STEP_DISTANCE * (flags & 0xff)) / 120;
+		if (flags & PTR_FLAGS_WHEEL_NEGATIVE)
+			axis = -axis;
+
+		notify_axis(&c->seat, weston_compositor_get_time(),
+					    WL_POINTER_AXIS_VERTICAL_SCROLL,
+					    axis);
+	}
+}
+
+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
+freerds_scancode_keyboard_event(struct freerds_compositor *c, UINT32 flags, UINT32 code,
+		UINT32 keyboardType)
+{
+	uint32_t vk_code, full_code, key_code;
+	enum wl_keyboard_key_state keyState;
+	int notify = 0;
+
+	//weston_log("code=%d flags=0x%x keyb=%d\n", code, flags, keyboardType);
+	if (flags & KBD_FLAGS_DOWN) {
+		keyState = WL_KEYBOARD_KEY_STATE_PRESSED;
+		notify = 1;
+	} else if (flags & KBD_FLAGS_RELEASE) {
+		keyState = WL_KEYBOARD_KEY_STATE_RELEASED;
+		notify = 1;
+	}
+
+	if(notify) {
+		full_code = code;
+		if(flags & KBD_FLAGS_EXTENDED)
+			full_code |= KBD_FLAGS_EXTENDED;
+
+		vk_code = GetVirtualKeyCodeFromVirtualScanCode(full_code, keyboardType);
+		if(vk_code > 0xff) {
+			weston_log("invalid vk_code %x", vk_code);
+			return;
+		}
+		key_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(&c->seat, weston_compositor_get_time(), key_code, keyState,
+				STATE_UPDATE_AUTOMATIC);
+	}
+}
+
+struct rdp_to_xkb_keyboard_layout {
+	UINT32 rdpLayoutCode;
+	char *xkbLayout;
+};
+
+
+/* table reversed from
+ 	 https://github.com/awakecoding/FreeRDP/blob/master/libfreerdp/locale/xkb_layout_ids.c#L811 */
+static
+struct rdp_to_xkb_keyboard_layout rdp_keyboards[] = {
+		{KBD_ARABIC_101, "ara"},
+		{KBD_BULGARIAN, "bg"},
+		{KBD_CHINESE_TRADITIONAL_US, 0},
+		{KBD_CZECH, "cz"},
+		{KBD_DANISH, "dk"},
+		{KBD_GERMAN, "de"},
+		{KBD_GREEK, "gr"},
+		{KBD_US, "us"},
+		{KBD_SPANISH, "es"},
+		{KBD_FINNISH, "fi"},
+		{KBD_FRENCH, "fr"},
+		{KBD_HEBREW, "il"},
+		{KBD_HUNGARIAN, "hu"},
+		{KBD_ICELANDIC, "is"},
+		{KBD_ITALIAN, "it"},
+		{KBD_JAPANESE, "jp"},
+		{KBD_KOREAN, "kr"},
+		{KBD_DUTCH, "nl"},
+		{KBD_NORWEGIAN, "no"},
+		{KBD_POLISH_PROGRAMMERS, 0},
+//		{KBD_PORTUGUESE_BRAZILIAN_ABN0416, 0},
+		{KBD_ROMANIAN, 0},
+		{KBD_RUSSIAN, "ru"},
+		{KBD_CROATIAN, 0},
+		{KBD_SLOVAK, 0},
+		{KBD_ALBANIAN, 0},
+		{KBD_SWEDISH, 0},
+		{KBD_THAI_KEDMANEE, 0},
+		{KBD_TURKISH_Q, 0},
+		{KBD_URDU, 0},
+		{KBD_UKRAINIAN, 0},
+		{KBD_BELARUSIAN, 0},
+		{KBD_SLOVENIAN, 0},
+		{KBD_ESTONIAN, "ee"},
+		{KBD_LATVIAN, 0},
+		{KBD_LITHUANIAN_IBM, 0},
+		{KBD_FARSI, 0},
+		{KBD_VIETNAMESE, 0},
+		{KBD_ARMENIAN_EASTERN, 0},
+		{KBD_AZERI_LATIN, 0},
+		{KBD_FYRO_MACEDONIAN, 0},
+		{KBD_GEORGIAN, 0},
+		{KBD_FAEROESE, 0},
+		{KBD_DEVANAGARI_INSCRIPT, 0},
+		{KBD_MALTESE_47_KEY, 0},
+		{KBD_NORWEGIAN_WITH_SAMI, 0},
+		{KBD_KAZAKH, 0},
+		{KBD_KYRGYZ_CYRILLIC, 0},
+		{KBD_TATAR, 0},
+		{KBD_BENGALI, 0},
+		{KBD_PUNJABI, 0},
+		{KBD_GUJARATI, 0},
+		{KBD_TAMIL, 0},
+		{KBD_TELUGU, 0},
+		{KBD_KANNADA, 0},
+		{KBD_MALAYALAM, 0},
+		{KBD_MARATHI, 0},
+		{KBD_MONGOLIAN_CYRILLIC, 0},
+		{KBD_UNITED_KINGDOM_EXTENDED, 0},
+		{KBD_SYRIAC, 0},
+		{KBD_NEPALI, 0},
+		{KBD_PASHTO, 0},
+		{KBD_DIVEHI_PHONETIC, 0},
+		{KBD_LUXEMBOURGISH, 0},
+		{KBD_MAORI, 0},
+		{KBD_CHINESE_SIMPLIFIED_US, 0},
+		{KBD_SWISS_GERMAN, 0},
+		{KBD_UNITED_KINGDOM, 0},
+		{KBD_LATIN_AMERICAN, 0},
+		{KBD_BELGIAN_FRENCH, 0},
+		{KBD_BELGIAN_PERIOD, 0},
+		{KBD_PORTUGUESE, 0},
+		{KBD_SERBIAN_LATIN, 0},
+		{KBD_AZERI_CYRILLIC, 0},
+		{KBD_SWEDISH_WITH_SAMI, 0},
+		{KBD_UZBEK_CYRILLIC, 0},
+		{KBD_INUKTITUT_LATIN, 0},
+		{KBD_CANADIAN_FRENCH_LEGACY, "fr-legacy"},
+		{KBD_SERBIAN_CYRILLIC, 0},
+		{KBD_CANADIAN_FRENCH, 0},
+		{KBD_SWISS_FRENCH, "ch"},
+		{KBD_BOSNIAN, "unicode"},
+		{KBD_IRISH, 0},
+		{KBD_BOSNIAN_CYRILLIC, 0},
+
+		{0x00000000, 0},
+};
+
+/* 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 */
+	"pc105",/* 4: IBM enhanced (101- or 102-key) keyboard */
+	"", /* 5: Nokia 1050 and similar keyboards */
+	"",	/* 6: Nokia 9140 and similar keyboards */
+	""	/* 7: Japanese keyboard */
+};
+
+static struct xkb_keymap *
+freerds_retrieve_keymap(UINT32 rdpKbLayout, UINT32 rdpKbType) {
+	struct xkb_context *xkbContext;
+	struct xkb_rule_names xkbRuleNames;
+	struct xkb_keymap *keymap;
+	int i;
+
+	memset(&xkbRuleNames, 0, sizeof(xkbRuleNames));
+	if(rdpKbType <= 7 && rdpKbType > 0)
+		xkbRuleNames.model = rdp_keyboard_types[rdpKbType];
+	else
+		xkbRuleNames.model = "pc105";
+
+	for(i = 0; rdp_keyboards[i].rdpLayoutCode; i++) {
+		if(rdp_keyboards[i].rdpLayoutCode == rdpKbLayout) {
+			xkbRuleNames.layout = rdp_keyboards[i].xkbLayout;
+			break;
+		}
+	}
+
+	keymap = NULL;
+	if(xkbRuleNames.layout) {
+		xkbContext = xkb_context_new(0);
+		if(!xkbContext) {
+			weston_log("unable to create a xkb_context\n");
+			return NULL;
+		}
+
+		weston_log("looking for keymap %s\n", xkbRuleNames.layout);
+		keymap = xkb_keymap_new_from_names(xkbContext, &xkbRuleNames, 0);
+	}
+	return keymap;
+}
+
+static void
+freerds_configure_keyboard(struct freerds_compositor *c, UINT32 layout, UINT32 keyboard_type) {
+	if (c->keyboard_layout == layout && c->keyboard_type == keyboard_type)
+		return;
+
+	weston_seat_init_keyboard(&c->seat,
+		freerds_retrieve_keymap(layout, keyboard_type)
+	);
+
+	c->keyboard_layout = layout;
+	c->keyboard_type = keyboard_type;
+}
+
+static int
+freerds_send_shared_framebuffer(struct freerds_compositor *c) {
+	Stream_SetPosition(c->out_stream, 0);
+	freerds_server_message_write(c->out_stream, (RDS_MSG_COMMON *)&c->rds_shared_fb);
+	Stream_SealLength(c->out_stream);
+
+	return freerds_send_stream(c, c->out_stream);
+}
+
+static int
+freerds_send_disable_pointer(struct freerds_compositor *c) {
+	Stream_SetPosition(c->out_stream, 0);
+	freerds_server_message_write(c->out_stream, (RDS_MSG_COMMON *)&c->rds_set_system_pointer);
+	Stream_SealLength(c->out_stream);
+	return freerds_send_stream(c, c->out_stream);
+}
+
+
+static int
+freerds_treat_message(struct freerds_compositor *c, wStream *s) {
+	RDS_MSG_CAPABILITIES capabilities;
+	RDS_MSG_SUPPRESS_OUTPUT suppress_output;
+	RDS_MSG_SCANCODE_KEYBOARD_EVENT *scancode;
+	RECTANGLE_16 *rect16;
+	unsigned int i;
+
+	//weston_log("message type %d\n", c->header_common.type);
+	switch (c->header_common.type) {
+	case RDS_CLIENT_CAPABILITIES:
+		if (freerds_read_capabilities(s, &capabilities) < 0) {
+			weston_log("invalid capabilities message\n");
+			return -1;
+		}
+		weston_seat_init(&c->seat, &c->base, "freerds");
+		weston_seat_init_pointer(&c->seat);
+
+		freerds_configure_keyboard(c, capabilities.KeyboardLayout, capabilities.KeyboardSubType);
+
+		if (freerds_send_shared_framebuffer(c) < 0)
+			weston_log("unable to send shared framebuffer, errno=%d\n", errno);
+
+		if (freerds_send_disable_pointer(c) < 0)
+			weston_log("unable to disable client-side pointer, errno=%d\n", errno);
+		break;
+
+	case RDS_CLIENT_MOUSE_EVENT:
+		if (freerds_read_mouse_event(s, &c->rds_mouse_event) < 0) {
+			weston_log("invalid mouse event message\n");
+			return -1;
+		}
+		freerds_mouse_event(c, c->rds_mouse_event.x, c->rds_mouse_event.y, c->rds_mouse_event.flags);
+		break;
+
+	case RDS_CLIENT_SCANCODE_KEYBOARD_EVENT:
+		scancode = &c->rds_scancode;
+		if (freerds_read_scancode_keyboard_event(s, scancode) < 0) {
+			weston_log("invalid scancode keyboard event message\n");
+			return -1;
+		}
+		freerds_scancode_keyboard_event(c, scancode->flags, scancode->code, scancode->keyboardType);
+		break;
+
+	case RDS_CLIENT_VBLANK_EVENT:
+		c->output->waitingVBlank = false;
+		if (c->output->outputActive && pixman_region32_not_empty(&c->output->damagedRegion))
+			freerds_refresh_region(c, &c->output->damagedRegion);
+		break;
+
+	case RDS_CLIENT_REFRESH_RECT:
+		if (freerds_read_refresh_rect(s, &c->rds_refresh_rect) < 0) {
+			weston_log("invalid refresh_rect message\n");
+			return -1;
+		}
+
+		rect16 = c->rds_refresh_rect.areasToRefresh;
+		for (i = 0; i < c->rds_refresh_rect.numberOfAreas; i++, rect16++) {
+			pixman_region32_union_rect(&c->output->damagedRegion, &c->output->damagedRegion,
+					rect16->left, rect16->top,
+					(rect16->right - rect16->left),
+					(rect16->bottom - rect16->top));
+		}
+
+		if (!c->output->waitingVBlank && c->output->outputActive)
+			freerds_refresh_region(c, &c->output->damagedRegion);
+		break;
+
+	case RDS_CLIENT_SUPPRESS_OUTPUT:
+		if (freerds_read_suppress_output(s, &suppress_output) < 0) {
+			weston_log("invalid suppress_output message\n");
+			return -1;
+		}
+
+		c->output->outputActive = suppress_output.activeOutput;
+		// TODO: freeze the compositor when output is suppressed ?
+		break;
+	case RDS_CLIENT_SYNCHRONIZE_KEYBOARD_EVENT:
+	case RDS_CLIENT_VIRTUAL_KEYBOARD_EVENT:
+	case RDS_CLIENT_UNICODE_KEYBOARD_EVENT:
+	case RDS_CLIENT_EXTENDED_MOUSE_EVENT:
+	default:
+		weston_log("not handled yet, %d\n", c->header_common.type);
+		break;
+	}
+	return 0;
+}
+
+
+#define FREERDS_COMMON_LENGTH 10
+static int
+freerds_client_activity(int fd, uint32_t mask, void *data) {
+	struct freerds_compositor *c = (struct freerds_compositor *)data;
+	int ret;
+
+	ret = read(c->client_fd, Stream_Pointer(c->in_stream), c->expected_bytes);
+	if (ret <= 0) {
+		weston_log("connection closed, releasing seat\n");
+		weston_seat_release(&c->seat);
+		return -1;
+	}
+
+	Stream_Seek(c->in_stream, ret);
+	c->expected_bytes -= ret;
+
+	if (c->expected_bytes)
+		return 0;
+
+	Stream_SealLength(c->in_stream);
+	Stream_SetPosition(c->in_stream, 0);
+
+	switch (c->streamState) {
+	case STREAM_WAITING_COMMON_HEADER:
+		//weston_log("reading %d for common header\n", ret);
+		freerds_read_common_header(c->in_stream, &c->header_common);
+		if (c->header_common.length > FREERDS_COMMON_LENGTH) {
+			c->streamState = STREAM_WAITING_DATA;
+			c->expected_bytes = c->header_common.length - FREERDS_COMMON_LENGTH;
+			Stream_EnsureCapacity(c->in_stream, c->header_common.length);
+			Stream_SetPosition(c->in_stream, FREERDS_COMMON_LENGTH);
+		} else {
+			freerds_treat_message(c, c->in_stream);
+
+			Stream_SetPosition(c->in_stream, 0);
+			c->expected_bytes = FREERDS_COMMON_LENGTH;
+		}
+		break;
+	case STREAM_WAITING_DATA:
+		Stream_SetPosition(c->in_stream, FREERDS_COMMON_LENGTH);
+		freerds_treat_message(c, c->in_stream);
+
+		Stream_SetPosition(c->in_stream, 0);
+		c->streamState = STREAM_WAITING_COMMON_HEADER;
+		c->expected_bytes = FREERDS_COMMON_LENGTH;
+		break;
+	default:
+		weston_log("unknown module protocol state\n");
+		return -1;
+	}
+	return 0;
+}
+
+static int
+freerds_named_pipe_activity(int fd, uint32_t mask, void *data) {
+	struct freerds_compositor *c = (struct freerds_compositor *)data;
+	struct wl_event_loop *loop;
+
+	if (c->client_fd != -1) {
+		weston_log("dropping existing client");
+		close(c->client_fd);
+		weston_seat_release(&c->seat);
+	}
+
+	c->client_fd = accept(c->listening_fd, 0, 0);
+	if (c->client_fd >= 0) {
+		loop = wl_display_get_event_loop(c->base.wl_display);
+		wl_event_loop_add_fd(loop, c->client_fd, WL_EVENT_READABLE, freerds_client_activity, c);
+	}
+	return 0;
+}
+
+
+static struct weston_compositor *
+freerds_compositor_create(struct wl_display *display,
+		struct freerds_compositor_config *config,
+		int *argc, char *argv[], struct weston_config *wconfig)
+{
+	struct freerds_compositor *c;
+	struct wl_event_loop *loop;
+	struct sockaddr_un remote;
+	int len;
+
+	c = zalloc(sizeof *c);
+	if (c == NULL)
+		return NULL;
+
+	if (weston_compositor_init(&c->base, display, argc, argv, wconfig) < 0)
+		goto err_free;
+
+	c->base.destroy = freerds_destroy;
+	c->base.restore = freerds_restore;
+	c->client_fd = -1;
+
+	if (pixman_renderer_init(&c->base) < 0)
+		goto err_compositor;
+
+	if (freerds_compositor_create_output(c, config->width, config->height) < 0)
+		goto err_compositor;
+
+	if (!config->freerds_named_pipe) {
+		weston_log("no socket path given");
+		goto err_output;
+	}
+
+	c->listening_fd = socket(AF_UNIX, SOCK_STREAM, 0);
+	if (c->listening_fd < 0) {
+		weston_log("unable to create the listening socket\n");
+		goto err_output;
+	}
+
+	memset(&remote, 0, sizeof(remote));
+	remote.sun_family = AF_UNIX;
+	strcpy(remote.sun_path, config->freerds_named_pipe);
+	len = strlen(remote.sun_path) + sizeof(remote.sun_family);
+	if (bind(c->listening_fd, (struct sockaddr *)&remote, len) < 0) {
+		weston_log("unable to bind the named pipe, errno=%d path=%s\n",
+				errno, config->freerds_named_pipe);
+		goto err_socket;
+	}
+
+	if (!listen(c->listening_fd, 1) < 0) {
+		weston_log("unable to listen on the named pipe, errno=%d path=%s\n",
+				errno, config->freerds_named_pipe);
+		goto err_socket;
+	}
+
+	c->in_stream = Stream_New(NULL, FREERDS_COMMON_LENGTH);
+	c->out_stream = Stream_New(NULL, 65536);
+	c->expected_bytes = FREERDS_COMMON_LENGTH;
+	c->streamState = STREAM_WAITING_COMMON_HEADER;
+	loop = wl_display_get_event_loop(c->base.wl_display);
+	wl_event_loop_add_fd(loop, c->listening_fd, WL_EVENT_READABLE, freerds_named_pipe_activity, c);
+
+	return &c->base;
+
+err_socket:
+	close(c->listening_fd);
+err_output:
+	weston_output_destroy(&c->output->base);
+err_compositor:
+	weston_compositor_shutdown(&c->base);
+err_free:
+	free(c);
+	return NULL;
+}
+
+WL_EXPORT struct weston_compositor *
+backend_init(struct wl_display *display, int *argc, char *argv[],
+	     struct weston_config *wconfig)
+{
+	struct freerds_compositor_config config;
+	freerds_compositor_config_init(&config);
+	int major, minor, revision;
+
+	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 freerds_options[] = {
+		{ WESTON_OPTION_STRING, "freerds-pipe", 0, &config.freerds_named_pipe },
+		{ WESTON_OPTION_UNSIGNED_INTEGER, "width", 0, &config.width },
+		{ WESTON_OPTION_UNSIGNED_INTEGER, "height", 0, &config.height },
+	};
+
+	parse_options(freerds_options, ARRAY_LENGTH(freerds_options), argc, argv);
+	return freerds_compositor_create(display, &config, argc, argv, wconfig);
+}
-- 
1.8.1.2



More information about the wayland-devel mailing list