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 "../shared/str-util.h"
50 #include "pixman-renderer.h"
52 #define MAX_FREERDP_FDS 32
53 #define DEFAULT_AXIS_STEP_DISTANCE wl_fixed_from_int(10)
54 #define RDP_MODE_FREQ 60 * 1000
56 struct rdp_compositor_config {
65 int no_clients_resize;
70 struct rdp_compositor {
71 struct weston_compositor base;
73 freerdp_listener *listener;
74 struct wl_event_source *listener_events[MAX_FREERDP_FDS];
75 struct rdp_output *output;
81 int no_clients_resize;
84 enum peer_item_flags {
85 RDP_PEER_ACTIVATED = (1 << 0),
86 RDP_PEER_OUTPUT_ENABLED = (1 << 1),
89 struct rdp_peers_item {
92 struct weston_seat seat;
98 struct weston_output base;
99 struct wl_event_source *finish_frame_timer;
100 pixman_image_t *shadow_surface;
102 struct wl_list peers;
105 struct rdp_peer_context {
108 struct rdp_compositor *rdpCompositor;
109 struct wl_event_source *events[MAX_FREERDP_FDS];
110 RFX_CONTEXT *rfx_context;
111 wStream *encode_stream;
113 NSC_CONTEXT *nsc_context;
115 struct rdp_peers_item item;
117 typedef struct rdp_peer_context RdpPeerContext;
120 rdp_compositor_config_init(struct rdp_compositor_config *config) {
122 config->height = 480;
123 config->bind_address = NULL;
125 config->rdp_key = NULL;
126 config->server_cert = NULL;
127 config->server_key = NULL;
128 config->env_socket = 0;
129 config->no_clients_resize = 0;
133 rdp_peer_refresh_rfx(pixman_region32_t *damage, pixman_image_t *image, freerdp_peer *peer)
135 int width, height, nrects, i;
136 pixman_box32_t *region, *rects;
139 rdpUpdate *update = peer->update;
140 SURFACE_BITS_COMMAND *cmd = &update->surface_bits_command;
141 RdpPeerContext *context = (RdpPeerContext *)peer->context;
143 Stream_Clear(context->encode_stream);
144 Stream_SetPosition(context->encode_stream, 0);
146 width = (damage->extents.x2 - damage->extents.x1);
147 height = (damage->extents.y2 - damage->extents.y1);
149 cmd->destLeft = damage->extents.x1;
150 cmd->destTop = damage->extents.y1;
151 cmd->destRight = damage->extents.x2;
152 cmd->destBottom = damage->extents.y2;
154 cmd->codecID = peer->settings->RemoteFxCodecId;
156 cmd->height = height;
158 ptr = pixman_image_get_data(image) + damage->extents.x1 +
159 damage->extents.y1 * (pixman_image_get_stride(image) / sizeof(uint32_t));
161 rects = pixman_region32_rectangles(damage, &nrects);
162 context->rfx_rects = realloc(context->rfx_rects, nrects * sizeof *rfxRect);
164 for (i = 0; i < nrects; i++) {
166 rfxRect = &context->rfx_rects[i];
168 rfxRect->x = (region->x1 - damage->extents.x1);
169 rfxRect->y = (region->y1 - damage->extents.y1);
170 rfxRect->width = (region->x2 - region->x1);
171 rfxRect->height = (region->y2 - region->y1);
174 rfx_compose_message(context->rfx_context, context->encode_stream, context->rfx_rects, nrects,
175 (BYTE *)ptr, width, height,
176 pixman_image_get_stride(image)
179 cmd->bitmapDataLength = Stream_GetPosition(context->encode_stream);
180 cmd->bitmapData = Stream_Buffer(context->encode_stream);
182 update->SurfaceBits(update->context, cmd);
187 rdp_peer_refresh_nsc(pixman_region32_t *damage, pixman_image_t *image, freerdp_peer *peer)
191 rdpUpdate *update = peer->update;
192 SURFACE_BITS_COMMAND *cmd = &update->surface_bits_command;
193 RdpPeerContext *context = (RdpPeerContext *)peer->context;
195 Stream_Clear(context->encode_stream);
196 Stream_SetPosition(context->encode_stream, 0);
198 width = (damage->extents.x2 - damage->extents.x1);
199 height = (damage->extents.y2 - damage->extents.y1);
201 cmd->destLeft = damage->extents.x1;
202 cmd->destTop = damage->extents.y1;
203 cmd->destRight = damage->extents.x2;
204 cmd->destBottom = damage->extents.y2;
206 cmd->codecID = peer->settings->NSCodecId;
208 cmd->height = height;
210 ptr = pixman_image_get_data(image) + damage->extents.x1 +
211 damage->extents.y1 * (pixman_image_get_stride(image) / sizeof(uint32_t));
213 nsc_compose_message(context->nsc_context, context->encode_stream, (BYTE *)ptr,
214 cmd->width, cmd->height,
215 pixman_image_get_stride(image));
216 cmd->bitmapDataLength = Stream_GetPosition(context->encode_stream);
217 cmd->bitmapData = Stream_Buffer(context->encode_stream);
218 update->SurfaceBits(update->context, cmd);
222 pixman_image_flipped_subrect(const pixman_box32_t *rect, pixman_image_t *img, BYTE *dest) {
223 int stride = pixman_image_get_stride(img);
225 int toCopy = (rect->x2 - rect->x1) * 4;
226 int height = (rect->y2 - rect->y1);
227 const BYTE *src = (const BYTE *)pixman_image_get_data(img);
228 src += ((rect->y2-1) * stride) + (rect->x1 * 4);
230 for (h = 0; h < height; h++, src -= stride, dest += toCopy)
231 memcpy(dest, src, toCopy);
235 rdp_peer_refresh_raw(pixman_region32_t *region, pixman_image_t *image, freerdp_peer *peer)
237 rdpUpdate *update = peer->update;
238 SURFACE_BITS_COMMAND *cmd = &update->surface_bits_command;
239 SURFACE_FRAME_MARKER *marker = &update->surface_frame_marker;
240 pixman_box32_t *rect, subrect;
242 int heightIncrement, remainingHeight, top;
244 rect = pixman_region32_rectangles(region, &nrects);
249 marker->frameAction = SURFACECMD_FRAMEACTION_BEGIN;
250 update->SurfaceFrameMarker(peer->context, marker);
255 for (i = 0; i < nrects; i++, rect++) {
256 /*weston_log("rect(%d,%d, %d,%d)\n", rect->x1, rect->y1, rect->x2, rect->y2);*/
257 cmd->destLeft = rect->x1;
258 cmd->destRight = rect->x2;
259 cmd->width = rect->x2 - rect->x1;
261 heightIncrement = peer->settings->MultifragMaxRequestSize / (16 + cmd->width * 4);
262 remainingHeight = rect->y2 - rect->y1;
265 subrect.x1 = rect->x1;
266 subrect.x2 = rect->x2;
268 while (remainingHeight) {
269 cmd->height = (remainingHeight > heightIncrement) ? heightIncrement : remainingHeight;
271 cmd->destBottom = top + cmd->height;
272 cmd->bitmapDataLength = cmd->width * cmd->height * 4;
273 cmd->bitmapData = (BYTE *)realloc(cmd->bitmapData, cmd->bitmapDataLength);
276 subrect.y2 = top + cmd->height;
277 pixman_image_flipped_subrect(&subrect, image, cmd->bitmapData);
279 /*weston_log("* sending (%d,%d, %d,%d)\n", subrect.x1, subrect.y1, subrect.x2, subrect.y2); */
280 update->SurfaceBits(peer->context, cmd);
282 remainingHeight -= cmd->height;
287 marker->frameAction = SURFACECMD_FRAMEACTION_END;
288 update->SurfaceFrameMarker(peer->context, marker);
292 rdp_peer_refresh_region(pixman_region32_t *region, freerdp_peer *peer)
294 RdpPeerContext *context = (RdpPeerContext *)peer->context;
295 struct rdp_output *output = context->rdpCompositor->output;
296 rdpSettings *settings = peer->settings;
298 if (settings->RemoteFxCodec)
299 rdp_peer_refresh_rfx(region, output->shadow_surface, peer);
300 else if (settings->NSCodec)
301 rdp_peer_refresh_nsc(region, output->shadow_surface, peer);
303 rdp_peer_refresh_raw(region, output->shadow_surface, peer);
307 rdp_output_start_repaint_loop(struct weston_output *output)
312 gettimeofday(&tv, NULL);
313 msec = tv.tv_sec * 1000 + tv.tv_usec / 1000;
314 weston_output_finish_frame(output, msec);
318 rdp_output_repaint(struct weston_output *output_base, pixman_region32_t *damage)
320 struct rdp_output *output = container_of(output_base, struct rdp_output, base);
321 struct weston_compositor *ec = output->base.compositor;
322 struct rdp_peers_item *outputPeer;
324 pixman_renderer_output_set_buffer(output_base, output->shadow_surface);
325 ec->renderer->repaint_output(&output->base, damage);
327 if (pixman_region32_not_empty(damage)) {
328 wl_list_for_each(outputPeer, &output->peers, link) {
329 if ((outputPeer->flags & RDP_PEER_ACTIVATED) &&
330 (outputPeer->flags & RDP_PEER_OUTPUT_ENABLED))
332 rdp_peer_refresh_region(damage, outputPeer->peer);
337 pixman_region32_subtract(&ec->primary_plane.damage,
338 &ec->primary_plane.damage, damage);
340 wl_event_source_timer_update(output->finish_frame_timer, 16);
345 rdp_output_destroy(struct weston_output *output_base)
347 struct rdp_output *output = (struct rdp_output *)output_base;
349 wl_event_source_remove(output->finish_frame_timer);
354 finish_frame_handler(void *data)
356 rdp_output_start_repaint_loop(data);
361 static struct weston_mode *
362 rdp_insert_new_mode(struct weston_output *output, int width, int height, int rate) {
363 struct weston_mode *ret;
364 ret = zalloc(sizeof *ret);
368 ret->height = height;
370 wl_list_insert(&output->mode_list, &ret->link);
374 static struct weston_mode *
375 ensure_matching_mode(struct weston_output *output, struct weston_mode *target) {
376 struct weston_mode *local;
378 wl_list_for_each(local, &output->mode_list, link) {
379 if ((local->width == target->width) && (local->height == target->height))
383 return rdp_insert_new_mode(output, target->width, target->height, RDP_MODE_FREQ);
387 rdp_switch_mode(struct weston_output *output, struct weston_mode *target_mode) {
388 struct rdp_output *rdpOutput = container_of(output, struct rdp_output, base);
389 struct rdp_peers_item *rdpPeer;
390 rdpSettings *settings;
391 pixman_image_t *new_shadow_buffer;
392 struct weston_mode *local_mode;
394 local_mode = ensure_matching_mode(output, target_mode);
396 weston_log("mode %dx%d not available\n", target_mode->width, target_mode->height);
400 if (local_mode == output->current_mode)
403 output->current_mode->flags &= ~WL_OUTPUT_MODE_CURRENT;
405 output->current_mode = local_mode;
406 output->current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
408 pixman_renderer_output_destroy(output);
409 pixman_renderer_output_create(output);
411 new_shadow_buffer = pixman_image_create_bits(PIXMAN_x8r8g8b8, target_mode->width,
412 target_mode->height, 0, target_mode->width * 4);
413 pixman_image_composite32(PIXMAN_OP_SRC, rdpOutput->shadow_surface, 0, new_shadow_buffer,
414 0, 0, 0, 0, 0, 0, target_mode->width, target_mode->height);
415 pixman_image_unref(rdpOutput->shadow_surface);
416 rdpOutput->shadow_surface = new_shadow_buffer;
418 wl_list_for_each(rdpPeer, &rdpOutput->peers, link) {
419 settings = rdpPeer->peer->settings;
420 if (settings->DesktopWidth == (UINT32)target_mode->width &&
421 settings->DesktopHeight == (UINT32)target_mode->height)
424 if (!settings->DesktopResize) {
425 /* too bad this peer does not support desktop resize */
426 rdpPeer->peer->Close(rdpPeer->peer);
428 settings->DesktopWidth = target_mode->width;
429 settings->DesktopHeight = target_mode->height;
430 rdpPeer->peer->update->DesktopResize(rdpPeer->peer->context);
437 rdp_compositor_create_output(struct rdp_compositor *c, int width, int height)
439 struct rdp_output *output;
440 struct wl_event_loop *loop;
441 struct weston_mode *currentMode;
442 struct weston_mode initMode;
444 output = zalloc(sizeof *output);
448 wl_list_init(&output->peers);
449 wl_list_init(&output->base.mode_list);
451 initMode.flags = WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
452 initMode.width = width;
453 initMode.height = height;
454 initMode.refresh = RDP_MODE_FREQ;
456 currentMode = ensure_matching_mode(&output->base, &initMode);
458 goto out_free_output;
460 output->base.current_mode = output->base.native_mode = currentMode;
461 weston_output_init(&output->base, &c->base, 0, 0, width, height,
462 WL_OUTPUT_TRANSFORM_NORMAL, 1);
464 output->base.make = "weston";
465 output->base.model = "rdp";
466 output->shadow_surface = pixman_image_create_bits(PIXMAN_x8r8g8b8,
470 if (output->shadow_surface == NULL) {
471 weston_log("Failed to create surface for frame buffer.\n");
475 if (pixman_renderer_output_create(&output->base) < 0)
476 goto out_shadow_surface;
478 loop = wl_display_get_event_loop(c->base.wl_display);
479 output->finish_frame_timer = wl_event_loop_add_timer(loop, finish_frame_handler, output);
481 output->base.start_repaint_loop = rdp_output_start_repaint_loop;
482 output->base.repaint = rdp_output_repaint;
483 output->base.destroy = rdp_output_destroy;
484 output->base.assign_planes = NULL;
485 output->base.set_backlight = NULL;
486 output->base.set_dpms = NULL;
487 output->base.switch_mode = rdp_switch_mode;
490 wl_list_insert(c->base.output_list.prev, &output->base.link);
494 pixman_image_unref(output->shadow_surface);
496 weston_output_destroy(&output->base);
503 rdp_restore(struct weston_compositor *ec)
508 rdp_destroy(struct weston_compositor *ec)
510 weston_compositor_shutdown(ec);
516 int rdp_listener_activity(int fd, uint32_t mask, void *data) {
517 freerdp_listener* instance = (freerdp_listener *)data;
519 if (!(mask & WL_EVENT_READABLE))
521 if (!instance->CheckFileDescriptor(instance))
523 weston_log("failed to check FreeRDP file descriptor\n");
530 int rdp_implant_listener(struct rdp_compositor *c, freerdp_listener* instance) {
533 void* rfds[MAX_FREERDP_FDS];
534 struct wl_event_loop *loop;
536 if (!instance->GetFileDescriptor(instance, rfds, &rcount)) {
537 weston_log("Failed to get FreeRDP file descriptor\n");
541 loop = wl_display_get_event_loop(c->base.wl_display);
542 for (i = 0; i < rcount; i++) {
543 fd = (int)(long)(rfds[i]);
544 c->listener_events[i] = wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
545 rdp_listener_activity, instance);
548 for( ; i < MAX_FREERDP_FDS; i++)
549 c->listener_events[i] = 0;
555 rdp_peer_context_new(freerdp_peer* client, RdpPeerContext* context)
557 context->item.peer = client;
558 context->item.flags = RDP_PEER_OUTPUT_ENABLED;
560 #if FREERDP_VERSION_MAJOR == 1 && FREERDP_VERSION_MINOR == 1
561 context->rfx_context = rfx_context_new();
563 context->rfx_context = rfx_context_new(TRUE);
565 context->rfx_context->mode = RLGR3;
566 context->rfx_context->width = client->settings->DesktopWidth;
567 context->rfx_context->height = client->settings->DesktopHeight;
568 rfx_context_set_pixel_format(context->rfx_context, RDP_PIXEL_FORMAT_B8G8R8A8);
570 context->nsc_context = nsc_context_new();
571 nsc_context_set_pixel_format(context->nsc_context, RDP_PIXEL_FORMAT_B8G8R8A8);
573 context->encode_stream = Stream_New(NULL, 65536);
577 rdp_peer_context_free(freerdp_peer* client, RdpPeerContext* context)
583 wl_list_remove(&context->item.link);
584 for(i = 0; i < MAX_FREERDP_FDS; i++) {
585 if (context->events[i])
586 wl_event_source_remove(context->events[i]);
589 if (context->item.flags & RDP_PEER_ACTIVATED) {
590 weston_seat_release_keyboard(&context->item.seat);
591 weston_seat_release_pointer(&context->item.seat);
592 weston_seat_release(&context->item.seat);
594 Stream_Free(context->encode_stream, TRUE);
595 nsc_context_free(context->nsc_context);
596 rfx_context_free(context->rfx_context);
597 free(context->rfx_rects);
602 rdp_client_activity(int fd, uint32_t mask, void *data) {
603 freerdp_peer* client = (freerdp_peer *)data;
605 if (!client->CheckFileDescriptor(client)) {
606 weston_log("unable to checkDescriptor for %p\n", client);
612 freerdp_peer_context_free(client);
613 freerdp_peer_free(client);
618 xf_peer_capabilities(freerdp_peer* client)
623 struct rdp_to_xkb_keyboard_layout {
624 UINT32 rdpLayoutCode;
625 const char *xkbLayout;
626 const char *xkbVariant;
629 /* table reversed from
630 https://github.com/awakecoding/FreeRDP/blob/master/libfreerdp/locale/xkb_layout_ids.c#L811 */
632 struct rdp_to_xkb_keyboard_layout rdp_keyboards[] = {
633 {KBD_ARABIC_101, "ara", 0},
634 {KBD_BULGARIAN, 0, 0},
635 {KBD_CHINESE_TRADITIONAL_US, 0},
636 {KBD_CZECH, "cz", 0},
637 {KBD_CZECH_PROGRAMMERS, "cz", "bksl"},
638 {KBD_CZECH_QWERTY, "cz", "qwerty"},
639 {KBD_DANISH, "dk", 0},
640 {KBD_GERMAN, "de", 0},
641 {KBD_GERMAN_NEO, "de", "neo"},
642 {KBD_GERMAN_IBM, "de", "qwerty"},
643 {KBD_GREEK, "gr", 0},
644 {KBD_GREEK_220, "gr", "simple"},
645 {KBD_GREEK_319, "gr", "extended"},
646 {KBD_GREEK_POLYTONIC, "gr", "polytonic"},
648 {KBD_US_ENGLISH_TABLE_FOR_IBM_ARABIC_238_L, "ara", "buckwalter"},
649 {KBD_SPANISH, "es", 0},
650 {KBD_SPANISH_VARIATION, "es", "nodeadkeys"},
651 {KBD_FINNISH, "fi", 0},
652 {KBD_FRENCH, "fr", 0},
653 {KBD_HEBREW, "il", 0},
654 {KBD_HUNGARIAN, "hu", 0},
655 {KBD_HUNGARIAN_101_KEY, "hu", "standard"},
656 {KBD_ICELANDIC, "is", 0},
657 {KBD_ITALIAN, "it", 0},
658 {KBD_ITALIAN_142, "it", "nodeadkeys"},
659 {KBD_JAPANESE, "jp", 0},
660 {KBD_JAPANESE_INPUT_SYSTEM_MS_IME2002, "jp", "kana"},
661 {KBD_KOREAN, "kr", 0},
662 {KBD_KOREAN_INPUT_SYSTEM_IME_2000, "kr", "kr104"},
663 {KBD_DUTCH, "nl", 0},
664 {KBD_NORWEGIAN, "no", 0},
665 {KBD_POLISH_PROGRAMMERS, "pl", 0},
666 {KBD_POLISH_214, "pl", "qwertz"},
667 // {KBD_PORTUGUESE_BRAZILIAN_ABN0416, 0},
668 {KBD_ROMANIAN, "ro", 0},
669 {KBD_RUSSIAN, "ru", 0},
670 {KBD_RUSSIAN_TYPEWRITER, "ru", "typewriter"},
671 {KBD_CROATIAN, "hr", 0},
672 {KBD_SLOVAK, "sk", 0},
673 {KBD_SLOVAK_QWERTY, "sk", "qwerty"},
674 {KBD_ALBANIAN, 0, 0},
675 {KBD_SWEDISH, "se", 0},
676 {KBD_THAI_KEDMANEE, "th", 0},
677 {KBD_THAI_KEDMANEE_NON_SHIFTLOCK, "th", "tis"},
678 {KBD_TURKISH_Q, "tr", 0},
679 {KBD_TURKISH_F, "tr", "f"},
680 {KBD_URDU, "in", "urd-phonetic3"},
681 {KBD_UKRAINIAN, "ua", 0},
682 {KBD_BELARUSIAN, "by", 0},
683 {KBD_SLOVENIAN, "si", 0},
684 {KBD_ESTONIAN, "ee", 0},
685 {KBD_LATVIAN, "lv", 0},
686 {KBD_LITHUANIAN_IBM, "lt", "ibm"},
687 {KBD_FARSI, "af", 0},
688 {KBD_VIETNAMESE, "vn", 0},
689 {KBD_ARMENIAN_EASTERN, "am", 0},
690 {KBD_AZERI_LATIN, 0, 0},
691 {KBD_FYRO_MACEDONIAN, "mk", 0},
692 {KBD_GEORGIAN, "ge", 0},
693 {KBD_FAEROESE, 0, 0},
694 {KBD_DEVANAGARI_INSCRIPT, 0, 0},
695 {KBD_MALTESE_47_KEY, 0, 0},
696 {KBD_NORWEGIAN_WITH_SAMI, "no", "smi"},
697 {KBD_KAZAKH, "kz", 0},
698 {KBD_KYRGYZ_CYRILLIC, "kg", "phonetic"},
699 {KBD_TATAR, "ru", "tt"},
700 {KBD_BENGALI, "bd", 0},
701 {KBD_BENGALI_INSCRIPT, "bd", "probhat"},
703 {KBD_GUJARATI, "in", "guj"},
704 {KBD_TAMIL, "in", "tam"},
705 {KBD_TELUGU, "in", "tel"},
706 {KBD_KANNADA, "in", "kan"},
707 {KBD_MALAYALAM, "in", "mal"},
708 {KBD_HINDI_TRADITIONAL, "in", 0},
710 {KBD_MONGOLIAN_CYRILLIC, "mn", 0},
711 {KBD_UNITED_KINGDOM_EXTENDED, "gb", "intl"},
712 {KBD_SYRIAC, "syc", 0},
713 {KBD_SYRIAC_PHONETIC, "syc", "syc_phonetic"},
714 {KBD_NEPALI, "np", 0},
715 {KBD_PASHTO, "af", "ps"},
716 {KBD_DIVEHI_PHONETIC, 0, 0},
717 {KBD_LUXEMBOURGISH, 0, 0},
718 {KBD_MAORI, "mao", 0},
719 {KBD_CHINESE_SIMPLIFIED_US, 0, 0},
720 {KBD_SWISS_GERMAN, "ch", "de_nodeadkeys"},
721 {KBD_UNITED_KINGDOM, "gb", 0},
722 {KBD_LATIN_AMERICAN, "latam", 0},
723 {KBD_BELGIAN_FRENCH, "be", 0},
724 {KBD_BELGIAN_PERIOD, "be", "oss_sundeadkeys"},
725 {KBD_PORTUGUESE, "pt", 0},
726 {KBD_SERBIAN_LATIN, "rs", 0},
727 {KBD_AZERI_CYRILLIC, "az", "cyrillic"},
728 {KBD_SWEDISH_WITH_SAMI, "se", "smi"},
729 {KBD_UZBEK_CYRILLIC, "af", "uz"},
730 {KBD_INUKTITUT_LATIN, "ca", "ike"},
731 {KBD_CANADIAN_FRENCH_LEGACY, "ca", "fr-legacy"},
732 {KBD_SERBIAN_CYRILLIC, "rs", 0},
733 {KBD_CANADIAN_FRENCH, "ca", "fr-legacy"},
734 {KBD_SWISS_FRENCH, "ch", "fr"},
735 {KBD_BOSNIAN, "ba", 0},
737 {KBD_BOSNIAN_CYRILLIC, "ba", "us"},
738 {KBD_UNITED_STATES_DVORAK, "us", "dvorak"},
739 {KBD_PORTUGUESE_BRAZILIAN_ABNT2, "br", "nativo"},
740 {KBD_CANADIAN_MULTILINGUAL_STANDARD, "ca", "multix"},
741 {KBD_GAELIC, "ie", "CloGaelach"},
746 /* taken from 2.2.7.1.6 Input Capability Set (TS_INPUT_CAPABILITYSET) */
747 static char *rdp_keyboard_types[] = {
749 "", /* 1: IBM PC/XT or compatible (83-key) keyboard */
750 "", /* 2: Olivetti "ICO" (102-key) keyboard */
751 "", /* 3: IBM PC/AT (84-key) or similar keyboard */
752 "pc102",/* 4: IBM enhanced (101- or 102-key) keyboard */
753 "", /* 5: Nokia 1050 and similar keyboards */
754 "", /* 6: Nokia 9140 and similar keyboards */
755 "" /* 7: Japanese keyboard */
759 xf_peer_post_connect(freerdp_peer* client)
761 RdpPeerContext *peerCtx;
762 struct rdp_compositor *c;
763 struct rdp_output *output;
764 rdpSettings *settings;
765 rdpPointerUpdate *pointer;
766 struct xkb_context *xkbContext;
767 struct xkb_rule_names xkbRuleNames;
768 struct xkb_keymap *keymap;
771 pixman_region32_t damage;
774 peerCtx = (RdpPeerContext *)client->context;
775 c = peerCtx->rdpCompositor;
777 settings = client->settings;
779 if (!settings->SurfaceCommandsEnabled) {
780 weston_log("client doesn't support required SurfaceCommands\n");
784 if (output->base.width != (int)settings->DesktopWidth ||
785 output->base.height != (int)settings->DesktopHeight)
787 if (c->no_clients_resize) {
788 /* RDP peers don't dictate their resolution to weston */
789 if (!settings->DesktopResize) {
790 /* peer does not support desktop resize */
791 weston_log("%s: client doesn't support resizing, closing connection\n", __FUNCTION__);
794 settings->DesktopWidth = output->base.width;
795 settings->DesktopHeight = output->base.height;
796 client->update->DesktopResize(client->context);
799 /* ask weston to adjust size */
800 struct weston_mode new_mode;
801 struct weston_mode *target_mode;
802 new_mode.width = (int)settings->DesktopWidth;
803 new_mode.height = (int)settings->DesktopHeight;
804 target_mode = ensure_matching_mode(&output->base, &new_mode);
806 weston_log("client mode not found\n");
809 weston_output_switch_mode(&output->base, target_mode, 1, WESTON_MODE_SWITCH_SET_NATIVE);
810 output->base.width = new_mode.width;
811 output->base.height = new_mode.height;
815 weston_log("kbd_layout:%x kbd_type:%x kbd_subType:%x kbd_functionKeys:%x\n",
816 settings->KeyboardLayout, settings->KeyboardType, settings->KeyboardSubType,
817 settings->KeyboardFunctionKey);
819 memset(&xkbRuleNames, 0, sizeof(xkbRuleNames));
820 if (settings->KeyboardType <= 7)
821 xkbRuleNames.model = rdp_keyboard_types[settings->KeyboardType];
822 for(i = 0; rdp_keyboards[i].rdpLayoutCode; i++) {
823 if (rdp_keyboards[i].rdpLayoutCode == settings->KeyboardLayout) {
824 xkbRuleNames.layout = rdp_keyboards[i].xkbLayout;
825 xkbRuleNames.variant = rdp_keyboards[i].xkbVariant;
826 weston_log("%s: matching layout=%s variant=%s\n", __FUNCTION__,
827 xkbRuleNames.layout, xkbRuleNames.variant);
833 if (xkbRuleNames.layout) {
834 xkbContext = xkb_context_new(0);
836 weston_log("unable to create a xkb_context\n");
840 keymap = xkb_keymap_new_from_names(xkbContext, &xkbRuleNames, 0);
842 weston_seat_init_keyboard(&peerCtx->item.seat, keymap);
843 weston_seat_init_pointer(&peerCtx->item.seat);
845 peerCtx->item.flags |= RDP_PEER_ACTIVATED;
847 /* disable pointer on the client side */
848 pointer = client->update->pointer;
849 pointer->pointer_system.type = SYSPTR_NULL;
850 pointer->PointerSystem(client->context, &pointer->pointer_system);
852 /* sends a full refresh */
855 box.x2 = output->base.width;
856 box.y2 = output->base.height;
857 pixman_region32_init_with_extents(&damage, &box);
859 rdp_peer_refresh_region(&damage, client);
861 pixman_region32_fini(&damage);
867 xf_peer_activate(freerdp_peer *client)
869 RdpPeerContext *context = (RdpPeerContext *)client->context;
870 rfx_context_reset(context->rfx_context);
875 xf_mouseEvent(rdpInput *input, UINT16 flags, UINT16 x, UINT16 y) {
876 wl_fixed_t wl_x, wl_y, axis;
877 RdpPeerContext *peerContext = (RdpPeerContext *)input->context;
878 struct rdp_output *output;
881 if (flags & PTR_FLAGS_MOVE) {
882 output = peerContext->rdpCompositor->output;
883 if (x < output->base.width && y < output->base.height) {
884 wl_x = wl_fixed_from_int((int)x);
885 wl_y = wl_fixed_from_int((int)y);
886 notify_motion_absolute(&peerContext->item.seat, weston_compositor_get_time(),
891 if (flags & PTR_FLAGS_BUTTON1)
893 else if (flags & PTR_FLAGS_BUTTON2)
895 else if (flags & PTR_FLAGS_BUTTON3)
899 notify_button(&peerContext->item.seat, weston_compositor_get_time(), button,
900 (flags & PTR_FLAGS_DOWN) ? WL_POINTER_BUTTON_STATE_PRESSED : WL_POINTER_BUTTON_STATE_RELEASED
904 if (flags & PTR_FLAGS_WHEEL) {
905 /* DEFAULT_AXIS_STEP_DISTANCE is stolen from compositor-x11.c
906 * The RDP specs says the lower bits of flags contains the "the number of rotation
907 * units the mouse wheel was rotated".
909 * http://blogs.msdn.com/b/oldnewthing/archive/2013/01/23/10387366.aspx explains the 120 value
911 axis = (DEFAULT_AXIS_STEP_DISTANCE * (flags & 0xff)) / 120;
912 if (flags & PTR_FLAGS_WHEEL_NEGATIVE)
915 notify_axis(&peerContext->item.seat, weston_compositor_get_time(),
916 WL_POINTER_AXIS_VERTICAL_SCROLL,
922 xf_extendedMouseEvent(rdpInput *input, UINT16 flags, UINT16 x, UINT16 y) {
923 wl_fixed_t wl_x, wl_y;
924 RdpPeerContext *peerContext = (RdpPeerContext *)input->context;
925 struct rdp_output *output;
927 output = peerContext->rdpCompositor->output;
928 if (x < output->base.width && y < output->base.height) {
929 wl_x = wl_fixed_from_int((int)x);
930 wl_y = wl_fixed_from_int((int)y);
931 notify_motion_absolute(&peerContext->item.seat, weston_compositor_get_time(),
938 xf_input_synchronize_event(rdpInput *input, UINT32 flags)
940 freerdp_peer *client = input->context->peer;
941 RdpPeerContext *peerCtx = (RdpPeerContext *)input->context;
942 struct rdp_output *output = peerCtx->rdpCompositor->output;
944 pixman_region32_t damage;
946 /* sends a full refresh */
949 box.x2 = output->base.width;
950 box.y2 = output->base.height;
951 pixman_region32_init_with_extents(&damage, &box);
953 rdp_peer_refresh_region(&damage, client);
955 pixman_region32_fini(&damage);
960 xf_input_keyboard_event(rdpInput *input, UINT16 flags, UINT16 code)
962 uint32_t scan_code, vk_code, full_code;
963 enum wl_keyboard_key_state keyState;
964 RdpPeerContext *peerContext = (RdpPeerContext *)input->context;
967 if (flags & KBD_FLAGS_DOWN) {
968 keyState = WL_KEYBOARD_KEY_STATE_PRESSED;
970 } else if (flags & KBD_FLAGS_RELEASE) {
971 keyState = WL_KEYBOARD_KEY_STATE_RELEASED;
977 if (flags & KBD_FLAGS_EXTENDED)
978 full_code |= KBD_FLAGS_EXTENDED;
980 vk_code = GetVirtualKeyCodeFromVirtualScanCode(full_code, 4);
981 if(flags & KBD_FLAGS_EXTENDED)
984 scan_code = GetKeycodeFromVirtualKeyCode(vk_code, KEYCODE_TYPE_EVDEV);
986 /*weston_log("code=%x ext=%d vk_code=%x scan_code=%x\n", code, (flags & KBD_FLAGS_EXTENDED) ? 1 : 0,
987 vk_code, scan_code);*/
988 notify_key(&peerContext->item.seat, weston_compositor_get_time(),
989 scan_code - 8, keyState, STATE_UPDATE_AUTOMATIC);
994 xf_input_unicode_keyboard_event(rdpInput *input, UINT16 flags, UINT16 code)
996 weston_log("Client sent a unicode keyboard event (flags:0x%X code:0x%X)\n", flags, code);
1001 xf_suppress_output(rdpContext *context, BYTE allow, RECTANGLE_16 *area) {
1002 RdpPeerContext *peerContext = (RdpPeerContext *)context;
1004 peerContext->item.flags |= RDP_PEER_OUTPUT_ENABLED;
1006 peerContext->item.flags &= (~RDP_PEER_OUTPUT_ENABLED);
1010 rdp_peer_init(freerdp_peer *client, struct rdp_compositor *c)
1013 void *rfds[MAX_FREERDP_FDS];
1015 struct wl_event_loop *loop;
1016 rdpSettings *settings;
1018 RdpPeerContext *peerCtx;
1021 client->ContextSize = sizeof(RdpPeerContext);
1022 client->ContextNew = (psPeerContextNew)rdp_peer_context_new;
1023 client->ContextFree = (psPeerContextFree)rdp_peer_context_free;
1024 freerdp_peer_context_new(client);
1026 peerCtx = (RdpPeerContext *) client->context;
1027 peerCtx->rdpCompositor = c;
1029 settings = client->settings;
1030 settings->RdpKeyFile = c->rdp_key;
1031 if (c->tls_enabled) {
1032 settings->CertificateFile = c->server_cert;
1033 settings->PrivateKeyFile = c->server_key;
1035 settings->TlsSecurity = FALSE;
1038 settings->NlaSecurity = FALSE;
1040 client->Capabilities = xf_peer_capabilities;
1041 client->PostConnect = xf_peer_post_connect;
1042 client->Activate = xf_peer_activate;
1044 client->update->SuppressOutput = xf_suppress_output;
1046 input = client->input;
1047 input->SynchronizeEvent = xf_input_synchronize_event;
1048 input->MouseEvent = xf_mouseEvent;
1049 input->ExtendedMouseEvent = xf_extendedMouseEvent;
1050 input->KeyboardEvent = xf_input_keyboard_event;
1051 input->UnicodeKeyboardEvent = xf_input_unicode_keyboard_event;
1053 if (snprintf(seat_name, 32, "rdp:%d:%s", client->sockfd, client->hostname) >= 32)
1054 seat_name[31] = '\0';
1056 weston_seat_init(&peerCtx->item.seat, &c->base, seat_name);
1058 client->Initialize(client);
1060 if (!client->GetFileDescriptor(client, rfds, &rcount)) {
1061 weston_log("unable to retrieve client fds\n");
1065 loop = wl_display_get_event_loop(c->base.wl_display);
1066 for(i = 0; i < rcount; i++) {
1067 fd = (int)(long)(rfds[i]);
1069 peerCtx->events[i] = wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1070 rdp_client_activity, client);
1072 for ( ; i < MAX_FREERDP_FDS; i++)
1073 peerCtx->events[i] = 0;
1075 wl_list_insert(&c->output->peers, &peerCtx->item.link);
1081 rdp_incoming_peer(freerdp_listener *instance, freerdp_peer *client)
1083 struct rdp_compositor *c = (struct rdp_compositor *)instance->param4;
1084 if (rdp_peer_init(client, c) < 0)
1088 static struct weston_compositor *
1089 rdp_compositor_create(struct wl_display *display,
1090 struct rdp_compositor_config *config,
1091 int *argc, char *argv[], struct weston_config *wconfig)
1093 struct rdp_compositor *c;
1097 c = zalloc(sizeof *c);
1101 if (weston_compositor_init(&c->base, display, argc, argv, wconfig) < 0)
1104 c->base.destroy = rdp_destroy;
1105 c->base.restore = rdp_restore;
1106 c->rdp_key = config->rdp_key ? strdup(config->rdp_key) : NULL;
1107 c->no_clients_resize = config->no_clients_resize;
1109 /* activate TLS only if certificate/key are available */
1110 if (config->server_cert && config->server_key) {
1111 weston_log("TLS support activated\n");
1112 c->server_cert = strdup(config->server_cert);
1113 c->server_key = strdup(config->server_key);
1114 if (!c->server_cert || !c->server_key)
1115 goto err_free_strings;
1119 if (pixman_renderer_init(&c->base) < 0)
1120 goto err_compositor;
1122 if (rdp_compositor_create_output(c, config->width, config->height) < 0)
1123 goto err_compositor;
1125 c->base.capabilities |= WESTON_CAP_ARBITRARY_MODES;
1127 if(!config->env_socket) {
1128 c->listener = freerdp_listener_new();
1129 c->listener->PeerAccepted = rdp_incoming_peer;
1130 c->listener->param4 = c;
1131 if (!c->listener->Open(c->listener, config->bind_address, config->port)) {
1132 weston_log("unable to bind rdp socket\n");
1136 if (rdp_implant_listener(c, c->listener) < 0)
1137 goto err_compositor;
1139 /* get the socket from RDP_FD var */
1140 fd_str = getenv("RDP_FD");
1142 weston_log("RDP_FD env variable not set");
1146 if (!weston_strtoi(fd_str, NULL, 10, &fd))
1148 if (rdp_peer_init(freerdp_peer_new(fd), c))
1155 freerdp_listener_free(c->listener);
1157 weston_output_destroy(&c->output->base);
1159 weston_compositor_shutdown(&c->base);
1164 free(c->server_cert);
1166 free(c->server_key);
1172 WL_EXPORT struct weston_compositor *
1173 backend_init(struct wl_display *display, int *argc, char *argv[],
1174 struct weston_config *wconfig)
1176 struct rdp_compositor_config config;
1177 rdp_compositor_config_init(&config);
1178 int major, minor, revision;
1180 freerdp_get_version(&major, &minor, &revision);
1181 weston_log("using FreeRDP version %d.%d.%d\n", major, minor, revision);
1183 const struct weston_option rdp_options[] = {
1184 { WESTON_OPTION_BOOLEAN, "env-socket", 0, &config.env_socket },
1185 { WESTON_OPTION_INTEGER, "width", 0, &config.width },
1186 { WESTON_OPTION_INTEGER, "height", 0, &config.height },
1187 { WESTON_OPTION_STRING, "address", 0, &config.bind_address },
1188 { WESTON_OPTION_INTEGER, "port", 0, &config.port },
1189 { WESTON_OPTION_BOOLEAN, "no-clients-resize", 0, &config.no_clients_resize },
1190 { WESTON_OPTION_STRING, "rdp4-key", 0, &config.rdp_key },
1191 { WESTON_OPTION_STRING, "rdp-tls-cert", 0, &config.server_cert },
1192 { WESTON_OPTION_STRING, "rdp-tls-key", 0, &config.server_key }
1195 parse_options(rdp_options, ARRAY_LENGTH(rdp_options), argc, argv);
1196 return rdp_compositor_create(display, &config, argc, argv, wconfig);