2 * Copyright © 2013 Hardening <rdp.effort@gmail.com>
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
28 #include <linux/input.h>
30 #if HAVE_FREERDP_VERSION_H
31 #include <freerdp/version.h>
33 /* assume it's a early 1.1 version */
34 #define FREERDP_VERSION_MAJOR 1
35 #define FREERDP_VERSION_MINOR 1
38 #include <freerdp/freerdp.h>
39 #include <freerdp/listener.h>
40 #include <freerdp/update.h>
41 #include <freerdp/input.h>
42 #include <freerdp/codec/color.h>
43 #include <freerdp/codec/rfx.h>
44 #include <freerdp/codec/nsc.h>
45 #include <freerdp/locale/keyboard.h>
46 #include <winpr/input.h>
48 #include "compositor.h"
49 #include "pixman-renderer.h"
51 #define MAX_FREERDP_FDS 32
52 #define DEFAULT_AXIS_STEP_DISTANCE wl_fixed_from_int(10)
53 #define RDP_MODE_FREQ 60 * 1000
55 struct rdp_compositor_config {
64 int no_clients_resize;
69 struct rdp_compositor {
70 struct weston_compositor base;
72 freerdp_listener *listener;
73 struct wl_event_source *listener_events[MAX_FREERDP_FDS];
74 struct rdp_output *output;
80 int no_clients_resize;
83 enum peer_item_flags {
84 RDP_PEER_ACTIVATED = (1 << 0),
85 RDP_PEER_OUTPUT_ENABLED = (1 << 1),
88 struct rdp_peers_item {
91 struct weston_seat seat;
97 struct weston_output base;
98 struct wl_event_source *finish_frame_timer;
99 pixman_image_t *shadow_surface;
101 struct wl_list peers;
104 struct rdp_peer_context {
107 struct rdp_compositor *rdpCompositor;
108 struct wl_event_source *events[MAX_FREERDP_FDS];
109 RFX_CONTEXT *rfx_context;
110 wStream *encode_stream;
112 NSC_CONTEXT *nsc_context;
114 struct rdp_peers_item item;
116 typedef struct rdp_peer_context RdpPeerContext;
119 rdp_compositor_config_init(struct rdp_compositor_config *config) {
121 config->height = 480;
122 config->bind_address = NULL;
124 config->rdp_key = NULL;
125 config->server_cert = NULL;
126 config->server_key = NULL;
127 config->env_socket = 0;
128 config->no_clients_resize = 0;
132 rdp_peer_refresh_rfx(pixman_region32_t *damage, pixman_image_t *image, freerdp_peer *peer)
134 int width, height, nrects, i;
135 pixman_box32_t *region, *rects;
138 rdpUpdate *update = peer->update;
139 SURFACE_BITS_COMMAND *cmd = &update->surface_bits_command;
140 RdpPeerContext *context = (RdpPeerContext *)peer->context;
142 Stream_Clear(context->encode_stream);
143 Stream_SetPosition(context->encode_stream, 0);
145 width = (damage->extents.x2 - damage->extents.x1);
146 height = (damage->extents.y2 - damage->extents.y1);
148 cmd->destLeft = damage->extents.x1;
149 cmd->destTop = damage->extents.y1;
150 cmd->destRight = damage->extents.x2;
151 cmd->destBottom = damage->extents.y2;
153 cmd->codecID = peer->settings->RemoteFxCodecId;
155 cmd->height = height;
157 ptr = pixman_image_get_data(image) + damage->extents.x1 +
158 damage->extents.y1 * (pixman_image_get_stride(image) / sizeof(uint32_t));
160 rects = pixman_region32_rectangles(damage, &nrects);
161 context->rfx_rects = realloc(context->rfx_rects, nrects * sizeof *rfxRect);
163 for (i = 0; i < nrects; i++) {
165 rfxRect = &context->rfx_rects[i];
167 rfxRect->x = (region->x1 - damage->extents.x1);
168 rfxRect->y = (region->y1 - damage->extents.y1);
169 rfxRect->width = (region->x2 - region->x1);
170 rfxRect->height = (region->y2 - region->y1);
173 rfx_compose_message(context->rfx_context, context->encode_stream, context->rfx_rects, nrects,
174 (BYTE *)ptr, width, height,
175 pixman_image_get_stride(image)
178 cmd->bitmapDataLength = Stream_GetPosition(context->encode_stream);
179 cmd->bitmapData = Stream_Buffer(context->encode_stream);
181 update->SurfaceBits(update->context, cmd);
186 rdp_peer_refresh_nsc(pixman_region32_t *damage, pixman_image_t *image, freerdp_peer *peer)
190 rdpUpdate *update = peer->update;
191 SURFACE_BITS_COMMAND *cmd = &update->surface_bits_command;
192 RdpPeerContext *context = (RdpPeerContext *)peer->context;
194 Stream_Clear(context->encode_stream);
195 Stream_SetPosition(context->encode_stream, 0);
197 width = (damage->extents.x2 - damage->extents.x1);
198 height = (damage->extents.y2 - damage->extents.y1);
200 cmd->destLeft = damage->extents.x1;
201 cmd->destTop = damage->extents.y1;
202 cmd->destRight = damage->extents.x2;
203 cmd->destBottom = damage->extents.y2;
205 cmd->codecID = peer->settings->NSCodecId;
207 cmd->height = height;
209 ptr = pixman_image_get_data(image) + damage->extents.x1 +
210 damage->extents.y1 * (pixman_image_get_stride(image) / sizeof(uint32_t));
212 nsc_compose_message(context->nsc_context, context->encode_stream, (BYTE *)ptr,
213 cmd->width, cmd->height,
214 pixman_image_get_stride(image));
215 cmd->bitmapDataLength = Stream_GetPosition(context->encode_stream);
216 cmd->bitmapData = Stream_Buffer(context->encode_stream);
217 update->SurfaceBits(update->context, cmd);
221 pixman_image_flipped_subrect(const pixman_box32_t *rect, pixman_image_t *img, BYTE *dest) {
222 int stride = pixman_image_get_stride(img);
224 int toCopy = (rect->x2 - rect->x1) * 4;
225 int height = (rect->y2 - rect->y1);
226 const BYTE *src = (const BYTE *)pixman_image_get_data(img);
227 src += ((rect->y2-1) * stride) + (rect->x1 * 4);
229 for (h = 0; h < height; h++, src -= stride, dest += toCopy)
230 memcpy(dest, src, toCopy);
234 rdp_peer_refresh_raw(pixman_region32_t *region, pixman_image_t *image, freerdp_peer *peer)
236 rdpUpdate *update = peer->update;
237 SURFACE_BITS_COMMAND *cmd = &update->surface_bits_command;
238 SURFACE_FRAME_MARKER *marker = &update->surface_frame_marker;
239 pixman_box32_t *rect, subrect;
241 int heightIncrement, remainingHeight, top;
243 rect = pixman_region32_rectangles(region, &nrects);
248 marker->frameAction = SURFACECMD_FRAMEACTION_BEGIN;
249 update->SurfaceFrameMarker(peer->context, marker);
254 for (i = 0; i < nrects; i++, rect++) {
255 /*weston_log("rect(%d,%d, %d,%d)\n", rect->x1, rect->y1, rect->x2, rect->y2);*/
256 cmd->destLeft = rect->x1;
257 cmd->destRight = rect->x2;
258 cmd->width = rect->x2 - rect->x1;
260 heightIncrement = peer->settings->MultifragMaxRequestSize / (16 + cmd->width * 4);
261 remainingHeight = rect->y2 - rect->y1;
264 subrect.x1 = rect->x1;
265 subrect.x2 = rect->x2;
267 while (remainingHeight) {
268 cmd->height = (remainingHeight > heightIncrement) ? heightIncrement : remainingHeight;
270 cmd->destBottom = top + cmd->height;
271 cmd->bitmapDataLength = cmd->width * cmd->height * 4;
272 cmd->bitmapData = (BYTE *)realloc(cmd->bitmapData, cmd->bitmapDataLength);
275 subrect.y2 = top + cmd->height;
276 pixman_image_flipped_subrect(&subrect, image, cmd->bitmapData);
278 /*weston_log("* sending (%d,%d, %d,%d)\n", subrect.x1, subrect.y1, subrect.x2, subrect.y2); */
279 update->SurfaceBits(peer->context, cmd);
281 remainingHeight -= cmd->height;
286 marker->frameAction = SURFACECMD_FRAMEACTION_END;
287 update->SurfaceFrameMarker(peer->context, marker);
291 rdp_peer_refresh_region(pixman_region32_t *region, freerdp_peer *peer)
293 RdpPeerContext *context = (RdpPeerContext *)peer->context;
294 struct rdp_output *output = context->rdpCompositor->output;
295 rdpSettings *settings = peer->settings;
297 if (settings->RemoteFxCodec)
298 rdp_peer_refresh_rfx(region, output->shadow_surface, peer);
299 else if (settings->NSCodec)
300 rdp_peer_refresh_nsc(region, output->shadow_surface, peer);
302 rdp_peer_refresh_raw(region, output->shadow_surface, peer);
306 rdp_output_start_repaint_loop(struct weston_output *output)
311 gettimeofday(&tv, NULL);
312 msec = tv.tv_sec * 1000 + tv.tv_usec / 1000;
313 weston_output_finish_frame(output, msec);
317 rdp_output_repaint(struct weston_output *output_base, pixman_region32_t *damage)
319 struct rdp_output *output = container_of(output_base, struct rdp_output, base);
320 struct weston_compositor *ec = output->base.compositor;
321 struct rdp_peers_item *outputPeer;
323 pixman_renderer_output_set_buffer(output_base, output->shadow_surface);
324 ec->renderer->repaint_output(&output->base, damage);
326 if (pixman_region32_not_empty(damage)) {
327 wl_list_for_each(outputPeer, &output->peers, link) {
328 if ((outputPeer->flags & RDP_PEER_ACTIVATED) &&
329 (outputPeer->flags & RDP_PEER_OUTPUT_ENABLED))
331 rdp_peer_refresh_region(damage, outputPeer->peer);
336 pixman_region32_subtract(&ec->primary_plane.damage,
337 &ec->primary_plane.damage, damage);
339 wl_event_source_timer_update(output->finish_frame_timer, 16);
344 rdp_output_destroy(struct weston_output *output_base)
346 struct rdp_output *output = (struct rdp_output *)output_base;
348 wl_event_source_remove(output->finish_frame_timer);
353 finish_frame_handler(void *data)
355 rdp_output_start_repaint_loop(data);
360 static struct weston_mode *
361 rdp_insert_new_mode(struct weston_output *output, int width, int height, int rate) {
362 struct weston_mode *ret;
363 ret = zalloc(sizeof *ret);
367 ret->height = height;
369 wl_list_insert(&output->mode_list, &ret->link);
373 static struct weston_mode *
374 ensure_matching_mode(struct weston_output *output, struct weston_mode *target) {
375 struct weston_mode *local;
377 wl_list_for_each(local, &output->mode_list, link) {
378 if ((local->width == target->width) && (local->height == target->height))
382 return rdp_insert_new_mode(output, target->width, target->height, RDP_MODE_FREQ);
386 rdp_switch_mode(struct weston_output *output, struct weston_mode *target_mode) {
387 struct rdp_output *rdpOutput = container_of(output, struct rdp_output, base);
388 struct rdp_peers_item *rdpPeer;
389 rdpSettings *settings;
390 pixman_image_t *new_shadow_buffer;
391 struct weston_mode *local_mode;
393 local_mode = ensure_matching_mode(output, target_mode);
395 weston_log("mode %dx%d not available\n", target_mode->width, target_mode->height);
399 if (local_mode == output->current_mode)
402 output->current_mode->flags &= ~WL_OUTPUT_MODE_CURRENT;
404 output->current_mode = local_mode;
405 output->current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
407 pixman_renderer_output_destroy(output);
408 pixman_renderer_output_create(output);
410 new_shadow_buffer = pixman_image_create_bits(PIXMAN_x8r8g8b8, target_mode->width,
411 target_mode->height, 0, target_mode->width * 4);
412 pixman_image_composite32(PIXMAN_OP_SRC, rdpOutput->shadow_surface, 0, new_shadow_buffer,
413 0, 0, 0, 0, 0, 0, target_mode->width, target_mode->height);
414 pixman_image_unref(rdpOutput->shadow_surface);
415 rdpOutput->shadow_surface = new_shadow_buffer;
417 wl_list_for_each(rdpPeer, &rdpOutput->peers, link) {
418 settings = rdpPeer->peer->settings;
419 if (settings->DesktopWidth == (UINT32)target_mode->width &&
420 settings->DesktopHeight == (UINT32)target_mode->height)
423 if (!settings->DesktopResize) {
424 /* too bad this peer does not support desktop resize */
425 rdpPeer->peer->Close(rdpPeer->peer);
427 settings->DesktopWidth = target_mode->width;
428 settings->DesktopHeight = target_mode->height;
429 rdpPeer->peer->update->DesktopResize(rdpPeer->peer->context);
436 rdp_compositor_create_output(struct rdp_compositor *c, int width, int height)
438 struct rdp_output *output;
439 struct wl_event_loop *loop;
440 struct weston_mode *currentMode;
441 struct weston_mode initMode;
443 output = zalloc(sizeof *output);
447 wl_list_init(&output->peers);
448 wl_list_init(&output->base.mode_list);
450 initMode.flags = WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
451 initMode.width = width;
452 initMode.height = height;
453 initMode.refresh = RDP_MODE_FREQ;
455 currentMode = ensure_matching_mode(&output->base, &initMode);
457 goto out_free_output;
459 output->base.current_mode = output->base.native_mode = currentMode;
460 weston_output_init(&output->base, &c->base, 0, 0, width, height,
461 WL_OUTPUT_TRANSFORM_NORMAL, 1);
463 output->base.make = "weston";
464 output->base.model = "rdp";
465 output->shadow_surface = pixman_image_create_bits(PIXMAN_x8r8g8b8,
469 if (output->shadow_surface == NULL) {
470 weston_log("Failed to create surface for frame buffer.\n");
474 if (pixman_renderer_output_create(&output->base) < 0)
475 goto out_shadow_surface;
477 loop = wl_display_get_event_loop(c->base.wl_display);
478 output->finish_frame_timer = wl_event_loop_add_timer(loop, finish_frame_handler, output);
480 output->base.start_repaint_loop = rdp_output_start_repaint_loop;
481 output->base.repaint = rdp_output_repaint;
482 output->base.destroy = rdp_output_destroy;
483 output->base.assign_planes = NULL;
484 output->base.set_backlight = NULL;
485 output->base.set_dpms = NULL;
486 output->base.switch_mode = rdp_switch_mode;
489 wl_list_insert(c->base.output_list.prev, &output->base.link);
493 pixman_image_unref(output->shadow_surface);
495 weston_output_destroy(&output->base);
502 rdp_restore(struct weston_compositor *ec)
507 rdp_destroy(struct weston_compositor *ec)
509 weston_compositor_shutdown(ec);
515 int rdp_listener_activity(int fd, uint32_t mask, void *data) {
516 freerdp_listener* instance = (freerdp_listener *)data;
518 if (!(mask & WL_EVENT_READABLE))
520 if (!instance->CheckFileDescriptor(instance))
522 weston_log("failed to check FreeRDP file descriptor\n");
529 int rdp_implant_listener(struct rdp_compositor *c, freerdp_listener* instance) {
532 void* rfds[MAX_FREERDP_FDS];
533 struct wl_event_loop *loop;
535 if (!instance->GetFileDescriptor(instance, rfds, &rcount)) {
536 weston_log("Failed to get FreeRDP file descriptor\n");
540 loop = wl_display_get_event_loop(c->base.wl_display);
541 for (i = 0; i < rcount; i++) {
542 fd = (int)(long)(rfds[i]);
543 c->listener_events[i] = wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
544 rdp_listener_activity, instance);
547 for( ; i < MAX_FREERDP_FDS; i++)
548 c->listener_events[i] = 0;
554 rdp_peer_context_new(freerdp_peer* client, RdpPeerContext* context)
556 context->item.peer = client;
557 context->item.flags = RDP_PEER_OUTPUT_ENABLED;
559 #if FREERDP_VERSION_MAJOR == 1 && FREERDP_VERSION_MINOR == 1
560 context->rfx_context = rfx_context_new();
562 context->rfx_context = rfx_context_new(TRUE);
564 context->rfx_context->mode = RLGR3;
565 context->rfx_context->width = client->settings->DesktopWidth;
566 context->rfx_context->height = client->settings->DesktopHeight;
567 rfx_context_set_pixel_format(context->rfx_context, RDP_PIXEL_FORMAT_B8G8R8A8);
569 context->nsc_context = nsc_context_new();
570 nsc_context_set_pixel_format(context->nsc_context, RDP_PIXEL_FORMAT_B8G8R8A8);
572 context->encode_stream = Stream_New(NULL, 65536);
576 rdp_peer_context_free(freerdp_peer* client, RdpPeerContext* context)
582 wl_list_remove(&context->item.link);
583 for(i = 0; i < MAX_FREERDP_FDS; i++) {
584 if (context->events[i])
585 wl_event_source_remove(context->events[i]);
588 if (context->item.flags & RDP_PEER_ACTIVATED) {
589 weston_seat_release_keyboard(&context->item.seat);
590 weston_seat_release_pointer(&context->item.seat);
591 weston_seat_release(&context->item.seat);
593 Stream_Free(context->encode_stream, TRUE);
594 nsc_context_free(context->nsc_context);
595 rfx_context_free(context->rfx_context);
596 free(context->rfx_rects);
601 rdp_client_activity(int fd, uint32_t mask, void *data) {
602 freerdp_peer* client = (freerdp_peer *)data;
604 if (!client->CheckFileDescriptor(client)) {
605 weston_log("unable to checkDescriptor for %p\n", client);
611 freerdp_peer_context_free(client);
612 freerdp_peer_free(client);
617 xf_peer_capabilities(freerdp_peer* client)
622 struct rdp_to_xkb_keyboard_layout {
623 UINT32 rdpLayoutCode;
624 const char *xkbLayout;
625 const char *xkbVariant;
628 /* table reversed from
629 https://github.com/awakecoding/FreeRDP/blob/master/libfreerdp/locale/xkb_layout_ids.c#L811 */
631 struct rdp_to_xkb_keyboard_layout rdp_keyboards[] = {
632 {KBD_ARABIC_101, "ara", 0},
633 {KBD_BULGARIAN, 0, 0},
634 {KBD_CHINESE_TRADITIONAL_US, 0},
635 {KBD_CZECH, "cz", 0},
636 {KBD_CZECH_PROGRAMMERS, "cz", "bksl"},
637 {KBD_CZECH_QWERTY, "cz", "qwerty"},
638 {KBD_DANISH, "dk", 0},
639 {KBD_GERMAN, "de", 0},
640 {KBD_GERMAN_NEO, "de", "neo"},
641 {KBD_GERMAN_IBM, "de", "qwerty"},
642 {KBD_GREEK, "gr", 0},
643 {KBD_GREEK_220, "gr", "simple"},
644 {KBD_GREEK_319, "gr", "extended"},
645 {KBD_GREEK_POLYTONIC, "gr", "polytonic"},
647 {KBD_US_ENGLISH_TABLE_FOR_IBM_ARABIC_238_L, "ara", "buckwalter"},
648 {KBD_SPANISH, "es", 0},
649 {KBD_SPANISH_VARIATION, "es", "nodeadkeys"},
650 {KBD_FINNISH, "fi", 0},
651 {KBD_FRENCH, "fr", 0},
652 {KBD_HEBREW, "il", 0},
653 {KBD_HUNGARIAN, "hu", 0},
654 {KBD_HUNGARIAN_101_KEY, "hu", "standard"},
655 {KBD_ICELANDIC, "is", 0},
656 {KBD_ITALIAN, "it", 0},
657 {KBD_ITALIAN_142, "it", "nodeadkeys"},
658 {KBD_JAPANESE, "jp", 0},
659 {KBD_JAPANESE_INPUT_SYSTEM_MS_IME2002, "jp", "kana"},
660 {KBD_KOREAN, "kr", 0},
661 {KBD_KOREAN_INPUT_SYSTEM_IME_2000, "kr", "kr104"},
662 {KBD_DUTCH, "nl", 0},
663 {KBD_NORWEGIAN, "no", 0},
664 {KBD_POLISH_PROGRAMMERS, "pl", 0},
665 {KBD_POLISH_214, "pl", "qwertz"},
666 // {KBD_PORTUGUESE_BRAZILIAN_ABN0416, 0},
667 {KBD_ROMANIAN, "ro", 0},
668 {KBD_RUSSIAN, "ru", 0},
669 {KBD_RUSSIAN_TYPEWRITER, "ru", "typewriter"},
670 {KBD_CROATIAN, "hr", 0},
671 {KBD_SLOVAK, "sk", 0},
672 {KBD_SLOVAK_QWERTY, "sk", "qwerty"},
673 {KBD_ALBANIAN, 0, 0},
674 {KBD_SWEDISH, "se", 0},
675 {KBD_THAI_KEDMANEE, "th", 0},
676 {KBD_THAI_KEDMANEE_NON_SHIFTLOCK, "th", "tis"},
677 {KBD_TURKISH_Q, "tr", 0},
678 {KBD_TURKISH_F, "tr", "f"},
679 {KBD_URDU, "in", "urd-phonetic3"},
680 {KBD_UKRAINIAN, "ua", 0},
681 {KBD_BELARUSIAN, "by", 0},
682 {KBD_SLOVENIAN, "si", 0},
683 {KBD_ESTONIAN, "ee", 0},
684 {KBD_LATVIAN, "lv", 0},
685 {KBD_LITHUANIAN_IBM, "lt", "ibm"},
686 {KBD_FARSI, "af", 0},
687 {KBD_VIETNAMESE, "vn", 0},
688 {KBD_ARMENIAN_EASTERN, "am", 0},
689 {KBD_AZERI_LATIN, 0, 0},
690 {KBD_FYRO_MACEDONIAN, "mk", 0},
691 {KBD_GEORGIAN, "ge", 0},
692 {KBD_FAEROESE, 0, 0},
693 {KBD_DEVANAGARI_INSCRIPT, 0, 0},
694 {KBD_MALTESE_47_KEY, 0, 0},
695 {KBD_NORWEGIAN_WITH_SAMI, "no", "smi"},
696 {KBD_KAZAKH, "kz", 0},
697 {KBD_KYRGYZ_CYRILLIC, "kg", "phonetic"},
698 {KBD_TATAR, "ru", "tt"},
699 {KBD_BENGALI, "bd", 0},
700 {KBD_BENGALI_INSCRIPT, "bd", "probhat"},
702 {KBD_GUJARATI, "in", "guj"},
703 {KBD_TAMIL, "in", "tam"},
704 {KBD_TELUGU, "in", "tel"},
705 {KBD_KANNADA, "in", "kan"},
706 {KBD_MALAYALAM, "in", "mal"},
707 {KBD_HINDI_TRADITIONAL, "in", 0},
709 {KBD_MONGOLIAN_CYRILLIC, "mn", 0},
710 {KBD_UNITED_KINGDOM_EXTENDED, "gb", "intl"},
711 {KBD_SYRIAC, "syc", 0},
712 {KBD_SYRIAC_PHONETIC, "syc", "syc_phonetic"},
713 {KBD_NEPALI, "np", 0},
714 {KBD_PASHTO, "af", "ps"},
715 {KBD_DIVEHI_PHONETIC, 0, 0},
716 {KBD_LUXEMBOURGISH, 0, 0},
717 {KBD_MAORI, "mao", 0},
718 {KBD_CHINESE_SIMPLIFIED_US, 0, 0},
719 {KBD_SWISS_GERMAN, "ch", "de_nodeadkeys"},
720 {KBD_UNITED_KINGDOM, "gb", 0},
721 {KBD_LATIN_AMERICAN, "latam", 0},
722 {KBD_BELGIAN_FRENCH, "be", 0},
723 {KBD_BELGIAN_PERIOD, "be", "oss_sundeadkeys"},
724 {KBD_PORTUGUESE, "pt", 0},
725 {KBD_SERBIAN_LATIN, "rs", 0},
726 {KBD_AZERI_CYRILLIC, "az", "cyrillic"},
727 {KBD_SWEDISH_WITH_SAMI, "se", "smi"},
728 {KBD_UZBEK_CYRILLIC, "af", "uz"},
729 {KBD_INUKTITUT_LATIN, "ca", "ike"},
730 {KBD_CANADIAN_FRENCH_LEGACY, "ca", "fr-legacy"},
731 {KBD_SERBIAN_CYRILLIC, "rs", 0},
732 {KBD_CANADIAN_FRENCH, "ca", "fr-legacy"},
733 {KBD_SWISS_FRENCH, "ch", "fr"},
734 {KBD_BOSNIAN, "ba", 0},
736 {KBD_BOSNIAN_CYRILLIC, "ba", "us"},
737 {KBD_UNITED_STATES_DVORAK, "us", "dvorak"},
738 {KBD_PORTUGUESE_BRAZILIAN_ABNT2, "br", "nativo"},
739 {KBD_CANADIAN_MULTILINGUAL_STANDARD, "ca", "multix"},
740 {KBD_GAELIC, "ie", "CloGaelach"},
745 /* taken from 2.2.7.1.6 Input Capability Set (TS_INPUT_CAPABILITYSET) */
746 static char *rdp_keyboard_types[] = {
748 "", /* 1: IBM PC/XT or compatible (83-key) keyboard */
749 "", /* 2: Olivetti "ICO" (102-key) keyboard */
750 "", /* 3: IBM PC/AT (84-key) or similar keyboard */
751 "pc102",/* 4: IBM enhanced (101- or 102-key) keyboard */
752 "", /* 5: Nokia 1050 and similar keyboards */
753 "", /* 6: Nokia 9140 and similar keyboards */
754 "" /* 7: Japanese keyboard */
758 xf_peer_post_connect(freerdp_peer* client)
760 RdpPeerContext *peerCtx;
761 struct rdp_compositor *c;
762 struct rdp_output *output;
763 rdpSettings *settings;
764 rdpPointerUpdate *pointer;
765 struct xkb_context *xkbContext;
766 struct xkb_rule_names xkbRuleNames;
767 struct xkb_keymap *keymap;
770 pixman_region32_t damage;
773 peerCtx = (RdpPeerContext *)client->context;
774 c = peerCtx->rdpCompositor;
776 settings = client->settings;
778 if (!settings->SurfaceCommandsEnabled) {
779 weston_log("client doesn't support required SurfaceCommands\n");
783 if (output->base.width != (int)settings->DesktopWidth ||
784 output->base.height != (int)settings->DesktopHeight)
786 if (c->no_clients_resize) {
787 /* RDP peers don't dictate their resolution to weston */
788 if (!settings->DesktopResize) {
789 /* peer does not support desktop resize */
790 weston_log("%s: client doesn't support resizing, closing connection\n", __FUNCTION__);
793 settings->DesktopWidth = output->base.width;
794 settings->DesktopHeight = output->base.height;
795 client->update->DesktopResize(client->context);
798 /* ask weston to adjust size */
799 struct weston_mode new_mode;
800 struct weston_mode *target_mode;
801 new_mode.width = (int)settings->DesktopWidth;
802 new_mode.height = (int)settings->DesktopHeight;
803 target_mode = ensure_matching_mode(&output->base, &new_mode);
805 weston_log("client mode not found\n");
808 weston_output_switch_mode(&output->base, target_mode, 1, WESTON_MODE_SWITCH_SET_NATIVE);
809 output->base.width = new_mode.width;
810 output->base.height = new_mode.height;
814 weston_log("kbd_layout:%x kbd_type:%x kbd_subType:%x kbd_functionKeys:%x\n",
815 settings->KeyboardLayout, settings->KeyboardType, settings->KeyboardSubType,
816 settings->KeyboardFunctionKey);
818 memset(&xkbRuleNames, 0, sizeof(xkbRuleNames));
819 if (settings->KeyboardType <= 7)
820 xkbRuleNames.model = rdp_keyboard_types[settings->KeyboardType];
821 for(i = 0; rdp_keyboards[i].rdpLayoutCode; i++) {
822 if (rdp_keyboards[i].rdpLayoutCode == settings->KeyboardLayout) {
823 xkbRuleNames.layout = rdp_keyboards[i].xkbLayout;
824 xkbRuleNames.variant = rdp_keyboards[i].xkbVariant;
825 weston_log("%s: matching layout=%s variant=%s\n", __FUNCTION__,
826 xkbRuleNames.layout, xkbRuleNames.variant);
832 if (xkbRuleNames.layout) {
833 xkbContext = xkb_context_new(0);
835 weston_log("unable to create a xkb_context\n");
839 keymap = xkb_keymap_new_from_names(xkbContext, &xkbRuleNames, 0);
841 weston_seat_init_keyboard(&peerCtx->item.seat, keymap);
842 weston_seat_init_pointer(&peerCtx->item.seat);
844 peerCtx->item.flags |= RDP_PEER_ACTIVATED;
846 /* disable pointer on the client side */
847 pointer = client->update->pointer;
848 pointer->pointer_system.type = SYSPTR_NULL;
849 pointer->PointerSystem(client->context, &pointer->pointer_system);
851 /* sends a full refresh */
854 box.x2 = output->base.width;
855 box.y2 = output->base.height;
856 pixman_region32_init_with_extents(&damage, &box);
858 rdp_peer_refresh_region(&damage, client);
860 pixman_region32_fini(&damage);
866 xf_peer_activate(freerdp_peer *client)
868 RdpPeerContext *context = (RdpPeerContext *)client->context;
869 rfx_context_reset(context->rfx_context);
874 xf_mouseEvent(rdpInput *input, UINT16 flags, UINT16 x, UINT16 y) {
875 wl_fixed_t wl_x, wl_y, axis;
876 RdpPeerContext *peerContext = (RdpPeerContext *)input->context;
877 struct rdp_output *output;
880 if (flags & PTR_FLAGS_MOVE) {
881 output = peerContext->rdpCompositor->output;
882 if (x < output->base.width && y < output->base.height) {
883 wl_x = wl_fixed_from_int((int)x);
884 wl_y = wl_fixed_from_int((int)y);
885 notify_motion_absolute(&peerContext->item.seat, weston_compositor_get_time(),
890 if (flags & PTR_FLAGS_BUTTON1)
892 else if (flags & PTR_FLAGS_BUTTON2)
894 else if (flags & PTR_FLAGS_BUTTON3)
898 notify_button(&peerContext->item.seat, weston_compositor_get_time(), button,
899 (flags & PTR_FLAGS_DOWN) ? WL_POINTER_BUTTON_STATE_PRESSED : WL_POINTER_BUTTON_STATE_RELEASED
903 if (flags & PTR_FLAGS_WHEEL) {
904 /* DEFAULT_AXIS_STEP_DISTANCE is stolen from compositor-x11.c
905 * The RDP specs says the lower bits of flags contains the "the number of rotation
906 * units the mouse wheel was rotated".
908 * http://blogs.msdn.com/b/oldnewthing/archive/2013/01/23/10387366.aspx explains the 120 value
910 axis = (DEFAULT_AXIS_STEP_DISTANCE * (flags & 0xff)) / 120;
911 if (flags & PTR_FLAGS_WHEEL_NEGATIVE)
914 notify_axis(&peerContext->item.seat, weston_compositor_get_time(),
915 WL_POINTER_AXIS_VERTICAL_SCROLL,
921 xf_extendedMouseEvent(rdpInput *input, UINT16 flags, UINT16 x, UINT16 y) {
922 wl_fixed_t wl_x, wl_y;
923 RdpPeerContext *peerContext = (RdpPeerContext *)input->context;
924 struct rdp_output *output;
926 output = peerContext->rdpCompositor->output;
927 if (x < output->base.width && y < output->base.height) {
928 wl_x = wl_fixed_from_int((int)x);
929 wl_y = wl_fixed_from_int((int)y);
930 notify_motion_absolute(&peerContext->item.seat, weston_compositor_get_time(),
937 xf_input_synchronize_event(rdpInput *input, UINT32 flags)
939 freerdp_peer *client = input->context->peer;
940 RdpPeerContext *peerCtx = (RdpPeerContext *)input->context;
941 struct rdp_output *output = peerCtx->rdpCompositor->output;
943 pixman_region32_t damage;
945 /* sends a full refresh */
948 box.x2 = output->base.width;
949 box.y2 = output->base.height;
950 pixman_region32_init_with_extents(&damage, &box);
952 rdp_peer_refresh_region(&damage, client);
954 pixman_region32_fini(&damage);
959 xf_input_keyboard_event(rdpInput *input, UINT16 flags, UINT16 code)
961 uint32_t scan_code, vk_code, full_code;
962 enum wl_keyboard_key_state keyState;
963 RdpPeerContext *peerContext = (RdpPeerContext *)input->context;
966 if (flags & KBD_FLAGS_DOWN) {
967 keyState = WL_KEYBOARD_KEY_STATE_PRESSED;
969 } else if (flags & KBD_FLAGS_RELEASE) {
970 keyState = WL_KEYBOARD_KEY_STATE_RELEASED;
976 if (flags & KBD_FLAGS_EXTENDED)
977 full_code |= KBD_FLAGS_EXTENDED;
979 vk_code = GetVirtualKeyCodeFromVirtualScanCode(full_code, 4);
980 if(flags & KBD_FLAGS_EXTENDED)
983 scan_code = GetKeycodeFromVirtualKeyCode(vk_code, KEYCODE_TYPE_EVDEV);
985 /*weston_log("code=%x ext=%d vk_code=%x scan_code=%x\n", code, (flags & KBD_FLAGS_EXTENDED) ? 1 : 0,
986 vk_code, scan_code);*/
987 notify_key(&peerContext->item.seat, weston_compositor_get_time(),
988 scan_code - 8, keyState, STATE_UPDATE_AUTOMATIC);
993 xf_input_unicode_keyboard_event(rdpInput *input, UINT16 flags, UINT16 code)
995 weston_log("Client sent a unicode keyboard event (flags:0x%X code:0x%X)\n", flags, code);
1000 xf_suppress_output(rdpContext *context, BYTE allow, RECTANGLE_16 *area) {
1001 RdpPeerContext *peerContext = (RdpPeerContext *)context;
1003 peerContext->item.flags |= RDP_PEER_OUTPUT_ENABLED;
1005 peerContext->item.flags &= (~RDP_PEER_OUTPUT_ENABLED);
1009 rdp_peer_init(freerdp_peer *client, struct rdp_compositor *c)
1012 void *rfds[MAX_FREERDP_FDS];
1014 struct wl_event_loop *loop;
1015 rdpSettings *settings;
1017 RdpPeerContext *peerCtx;
1020 client->ContextSize = sizeof(RdpPeerContext);
1021 client->ContextNew = (psPeerContextNew)rdp_peer_context_new;
1022 client->ContextFree = (psPeerContextFree)rdp_peer_context_free;
1023 freerdp_peer_context_new(client);
1025 peerCtx = (RdpPeerContext *) client->context;
1026 peerCtx->rdpCompositor = c;
1028 settings = client->settings;
1029 settings->RdpKeyFile = c->rdp_key;
1030 if (c->tls_enabled) {
1031 settings->CertificateFile = c->server_cert;
1032 settings->PrivateKeyFile = c->server_key;
1034 settings->TlsSecurity = FALSE;
1037 settings->NlaSecurity = FALSE;
1039 client->Capabilities = xf_peer_capabilities;
1040 client->PostConnect = xf_peer_post_connect;
1041 client->Activate = xf_peer_activate;
1043 client->update->SuppressOutput = xf_suppress_output;
1045 input = client->input;
1046 input->SynchronizeEvent = xf_input_synchronize_event;
1047 input->MouseEvent = xf_mouseEvent;
1048 input->ExtendedMouseEvent = xf_extendedMouseEvent;
1049 input->KeyboardEvent = xf_input_keyboard_event;
1050 input->UnicodeKeyboardEvent = xf_input_unicode_keyboard_event;
1052 if (snprintf(seat_name, 32, "rdp:%d:%s", client->sockfd, client->hostname) >= 32)
1053 seat_name[31] = '\0';
1055 weston_seat_init(&peerCtx->item.seat, &c->base, seat_name);
1057 client->Initialize(client);
1059 if (!client->GetFileDescriptor(client, rfds, &rcount)) {
1060 weston_log("unable to retrieve client fds\n");
1064 loop = wl_display_get_event_loop(c->base.wl_display);
1065 for(i = 0; i < rcount; i++) {
1066 fd = (int)(long)(rfds[i]);
1068 peerCtx->events[i] = wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1069 rdp_client_activity, client);
1071 for ( ; i < MAX_FREERDP_FDS; i++)
1072 peerCtx->events[i] = 0;
1074 wl_list_insert(&c->output->peers, &peerCtx->item.link);
1080 rdp_incoming_peer(freerdp_listener *instance, freerdp_peer *client)
1082 struct rdp_compositor *c = (struct rdp_compositor *)instance->param4;
1083 if (rdp_peer_init(client, c) < 0)
1087 static struct weston_compositor *
1088 rdp_compositor_create(struct wl_display *display,
1089 struct rdp_compositor_config *config,
1090 int *argc, char *argv[], struct weston_config *wconfig)
1092 struct rdp_compositor *c;
1096 c = zalloc(sizeof *c);
1100 if (weston_compositor_init(&c->base, display, argc, argv, wconfig) < 0)
1103 c->base.destroy = rdp_destroy;
1104 c->base.restore = rdp_restore;
1105 c->rdp_key = config->rdp_key ? strdup(config->rdp_key) : NULL;
1106 c->no_clients_resize = config->no_clients_resize;
1108 /* activate TLS only if certificate/key are available */
1109 if (config->server_cert && config->server_key) {
1110 weston_log("TLS support activated\n");
1111 c->server_cert = strdup(config->server_cert);
1112 c->server_key = strdup(config->server_key);
1113 if (!c->server_cert || !c->server_key)
1114 goto err_free_strings;
1118 if (pixman_renderer_init(&c->base) < 0)
1119 goto err_compositor;
1121 if (rdp_compositor_create_output(c, config->width, config->height) < 0)
1122 goto err_compositor;
1124 c->base.capabilities |= WESTON_CAP_ARBITRARY_MODES;
1126 if(!config->env_socket) {
1127 c->listener = freerdp_listener_new();
1128 c->listener->PeerAccepted = rdp_incoming_peer;
1129 c->listener->param4 = c;
1130 if (!c->listener->Open(c->listener, config->bind_address, config->port)) {
1131 weston_log("unable to bind rdp socket\n");
1135 if (rdp_implant_listener(c, c->listener) < 0)
1136 goto err_compositor;
1138 /* get the socket from RDP_FD var */
1139 fd_str = getenv("RDP_FD");
1141 weston_log("RDP_FD env variable not set");
1145 if (!weston_strtoi(fd_str, NULL, 10, &fd))
1147 if (rdp_peer_init(freerdp_peer_new(fd), c))
1154 freerdp_listener_free(c->listener);
1156 weston_output_destroy(&c->output->base);
1158 weston_compositor_shutdown(&c->base);
1163 free(c->server_cert);
1165 free(c->server_key);
1171 WL_EXPORT struct weston_compositor *
1172 backend_init(struct wl_display *display, int *argc, char *argv[],
1173 struct weston_config *wconfig)
1175 struct rdp_compositor_config config;
1176 rdp_compositor_config_init(&config);
1177 int major, minor, revision;
1179 freerdp_get_version(&major, &minor, &revision);
1180 weston_log("using FreeRDP version %d.%d.%d\n", major, minor, revision);
1182 const struct weston_option rdp_options[] = {
1183 { WESTON_OPTION_BOOLEAN, "env-socket", 0, &config.env_socket },
1184 { WESTON_OPTION_INTEGER, "width", 0, &config.width },
1185 { WESTON_OPTION_INTEGER, "height", 0, &config.height },
1186 { WESTON_OPTION_STRING, "address", 0, &config.bind_address },
1187 { WESTON_OPTION_INTEGER, "port", 0, &config.port },
1188 { WESTON_OPTION_BOOLEAN, "no-clients-resize", 0, &config.no_clients_resize },
1189 { WESTON_OPTION_STRING, "rdp4-key", 0, &config.rdp_key },
1190 { WESTON_OPTION_STRING, "rdp-tls-cert", 0, &config.server_cert },
1191 { WESTON_OPTION_STRING, "rdp-tls-key", 0, &config.server_key }
1194 parse_options(rdp_options, ARRAY_LENGTH(rdp_options), argc, argv);
1195 return rdp_compositor_create(display, &config, argc, argv, wconfig);