1 /*
2 * Copyright © 2013 Hardening <rdp.effort@gmail.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial
14 * portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 */
25
26 #include "config.h"
27
28 #include <assert.h>
29 #include <stdint.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <errno.h>
33 #include <linux/input.h>
34
35 #if HAVE_FREERDP_VERSION_H
36 #include <freerdp/version.h>
37 #else
38 /* assume it's a early 1.1 version */
39 #define FREERDP_VERSION_MAJOR 1
40 #define FREERDP_VERSION_MINOR 1
41 #define FREERDP_VERSION_REVISION 0
42 #endif
43
44 #define FREERDP_VERSION_NUMBER ((FREERDP_VERSION_MAJOR * 0x10000) + \
45 (FREERDP_VERSION_MINOR * 0x100) + FREERDP_VERSION_REVISION)
46
47
48 #if FREERDP_VERSION_NUMBER >= 0x10201
49 #define HAVE_SKIP_COMPRESSION
50 #endif
51
52 #if FREERDP_VERSION_NUMBER < 0x10202
53 # define FREERDP_CB_RET_TYPE void
54 # define FREERDP_CB_RETURN(V) return
55 # define NSC_RESET(C, W, H)
56 # define RFX_RESET(C, W, H) do { rfx_context_reset(C); C->width = W; C->height = H; } while(0)
57 #else
58 #if FREERDP_VERSION_MAJOR >= 2
59 # define NSC_RESET(C, W, H) nsc_context_reset(C, W, H)
60 # define RFX_RESET(C, W, H) rfx_context_reset(C, W, H)
61 #else
62 # define NSC_RESET(C, W, H) do { nsc_context_reset(C); C->width = W; C->height = H; } while(0)
63 # define RFX_RESET(C, W, H) do { rfx_context_reset(C); C->width = W; C->height = H; } while(0)
64 #endif
65 #define FREERDP_CB_RET_TYPE BOOL
66 #define FREERDP_CB_RETURN(V) return TRUE
67 #endif
68
69 #ifdef HAVE_SURFACE_BITS_BMP
70 #define SURFACE_BPP(cmd) cmd.bmp.bpp
71 #define SURFACE_CODECID(cmd) cmd.bmp.codecID
72 #define SURFACE_WIDTH(cmd) cmd.bmp.width
73 #define SURFACE_HEIGHT(cmd) cmd.bmp.height
74 #define SURFACE_BITMAP_DATA(cmd) cmd.bmp.bitmapData
75 #define SURFACE_BITMAP_DATA_LEN(cmd) cmd.bmp.bitmapDataLength
76 #else
77 #define SURFACE_BPP(cmd) cmd.bpp
78 #define SURFACE_CODECID(cmd) cmd.codecID
79 #define SURFACE_WIDTH(cmd) cmd.width
80 #define SURFACE_HEIGHT(cmd) cmd.height
81 #define SURFACE_BITMAP_DATA(cmd) cmd.bitmapData
82 #define SURFACE_BITMAP_DATA_LEN(cmd) cmd.bitmapDataLength
83 #endif
84
85 #include <freerdp/freerdp.h>
86 #include <freerdp/listener.h>
87 #include <freerdp/update.h>
88 #include <freerdp/input.h>
89 #include <freerdp/codec/color.h>
90 #include <freerdp/codec/rfx.h>
91 #include <freerdp/codec/nsc.h>
92 #include <freerdp/locale/keyboard.h>
93 #include <winpr/input.h>
94
95 #if FREERDP_VERSION_MAJOR >= 2
96 #include <winpr/ssl.h>
97 #endif
98
99 #include "shared/helpers.h"
100 #include "shared/timespec-util.h"
101 #include <libweston/libweston.h>
102 #include <libweston/backend-rdp.h>
103 #include "pixman-renderer.h"
104
105 #define MAX_FREERDP_FDS 32
106 #define DEFAULT_AXIS_STEP_DISTANCE 10
107 #define RDP_MODE_FREQ 60 * 1000
108
109 #if FREERDP_VERSION_MAJOR >= 2 && defined(PIXEL_FORMAT_BGRA32) && !defined(PIXEL_FORMAT_B8G8R8A8)
110 /* The RDP API is truly wonderful: the pixel format definition changed
111 * from BGRA32 to B8G8R8A8, but some versions ship with a definition of
112 * PIXEL_FORMAT_BGRA32 which doesn't actually build. Try really, really,
113 * hard to find one which does. */
114 # define DEFAULT_PIXEL_FORMAT PIXEL_FORMAT_BGRA32
115 #else
116 # define DEFAULT_PIXEL_FORMAT RDP_PIXEL_FORMAT_B8G8R8A8
117 #endif
118
119 struct rdp_output;
120
121 struct rdp_backend {
122 struct weston_backend base;
123 struct weston_compositor *compositor;
124
125 freerdp_listener *listener;
126 struct wl_event_source *listener_events[MAX_FREERDP_FDS];
127 struct rdp_output *output;
128
129 char *server_cert;
130 char *server_key;
131 char *rdp_key;
132 int tls_enabled;
133 int no_clients_resize;
134 int force_no_compression;
135 };
136
137 enum peer_item_flags {
138 RDP_PEER_ACTIVATED = (1 << 0),
139 RDP_PEER_OUTPUT_ENABLED = (1 << 1),
140 };
141
142 struct rdp_peers_item {
143 int flags;
144 freerdp_peer *peer;
145 struct weston_seat *seat;
146
147 struct wl_list link;
148 };
149
150 struct rdp_head {
151 struct weston_head base;
152 };
153
154 struct rdp_output {
155 struct weston_output base;
156 struct wl_event_source *finish_frame_timer;
157 pixman_image_t *shadow_surface;
158
159 struct wl_list peers;
160 };
161
162 struct rdp_peer_context {
163 rdpContext _p;
164
165 struct rdp_backend *rdpBackend;
166 struct wl_event_source *events[MAX_FREERDP_FDS];
167 RFX_CONTEXT *rfx_context;
168 wStream *encode_stream;
169 RFX_RECT *rfx_rects;
170 NSC_CONTEXT *nsc_context;
171
172 struct rdp_peers_item item;
173 };
174 typedef struct rdp_peer_context RdpPeerContext;
175
176 static inline struct rdp_head *
to_rdp_head(struct weston_head * base)177 to_rdp_head(struct weston_head *base)
178 {
179 return container_of(base, struct rdp_head, base);
180 }
181
182 static inline struct rdp_output *
to_rdp_output(struct weston_output * base)183 to_rdp_output(struct weston_output *base)
184 {
185 return container_of(base, struct rdp_output, base);
186 }
187
188 static inline struct rdp_backend *
to_rdp_backend(struct weston_compositor * base)189 to_rdp_backend(struct weston_compositor *base)
190 {
191 return container_of(base->backend, struct rdp_backend, base);
192 }
193
194 static void
rdp_peer_refresh_rfx(pixman_region32_t * damage,pixman_image_t * image,freerdp_peer * peer)195 rdp_peer_refresh_rfx(pixman_region32_t *damage, pixman_image_t *image, freerdp_peer *peer)
196 {
197 int width, height, nrects, i;
198 pixman_box32_t *region, *rects;
199 uint32_t *ptr;
200 RFX_RECT *rfxRect;
201 rdpUpdate *update = peer->update;
202 SURFACE_BITS_COMMAND cmd;
203 RdpPeerContext *context = (RdpPeerContext *)peer->context;
204
205 Stream_Clear(context->encode_stream);
206 Stream_SetPosition(context->encode_stream, 0);
207
208 width = (damage->extents.x2 - damage->extents.x1);
209 height = (damage->extents.y2 - damage->extents.y1);
210
211 #ifdef HAVE_SKIP_COMPRESSION
212 cmd.skipCompression = TRUE;
213 #else
214 memset(&cmd, 0, sizeof(*cmd));
215 #endif
216 cmd.destLeft = damage->extents.x1;
217 cmd.destTop = damage->extents.y1;
218 cmd.destRight = damage->extents.x2;
219 cmd.destBottom = damage->extents.y2;
220 SURFACE_BPP(cmd) = 32;
221 SURFACE_CODECID(cmd) = peer->settings->RemoteFxCodecId;
222 SURFACE_WIDTH(cmd) = width;
223 SURFACE_HEIGHT(cmd) = height;
224
225 ptr = pixman_image_get_data(image) + damage->extents.x1 +
226 damage->extents.y1 * (pixman_image_get_stride(image) / sizeof(uint32_t));
227
228 rects = pixman_region32_rectangles(damage, &nrects);
229 context->rfx_rects = realloc(context->rfx_rects, nrects * sizeof *rfxRect);
230
231 for (i = 0; i < nrects; i++) {
232 region = &rects[i];
233 rfxRect = &context->rfx_rects[i];
234
235 rfxRect->x = (region->x1 - damage->extents.x1);
236 rfxRect->y = (region->y1 - damage->extents.y1);
237 rfxRect->width = (region->x2 - region->x1);
238 rfxRect->height = (region->y2 - region->y1);
239 }
240
241 rfx_compose_message(context->rfx_context, context->encode_stream, context->rfx_rects, nrects,
242 (BYTE *)ptr, width, height,
243 pixman_image_get_stride(image)
244 );
245
246 SURFACE_BITMAP_DATA_LEN(cmd) = Stream_GetPosition(context->encode_stream);
247 SURFACE_BITMAP_DATA(cmd) = Stream_Buffer(context->encode_stream);
248
249 update->SurfaceBits(update->context, &cmd);
250 }
251
252
253 static void
rdp_peer_refresh_nsc(pixman_region32_t * damage,pixman_image_t * image,freerdp_peer * peer)254 rdp_peer_refresh_nsc(pixman_region32_t *damage, pixman_image_t *image, freerdp_peer *peer)
255 {
256 int width, height;
257 uint32_t *ptr;
258 rdpUpdate *update = peer->update;
259 SURFACE_BITS_COMMAND cmd;
260 RdpPeerContext *context = (RdpPeerContext *)peer->context;
261
262 Stream_Clear(context->encode_stream);
263 Stream_SetPosition(context->encode_stream, 0);
264
265 width = (damage->extents.x2 - damage->extents.x1);
266 height = (damage->extents.y2 - damage->extents.y1);
267
268 #ifdef HAVE_SKIP_COMPRESSION
269 cmd.skipCompression = TRUE;
270 #else
271 memset(cmd, 0, sizeof(*cmd));
272 #endif
273
274 cmd.destLeft = damage->extents.x1;
275 cmd.destTop = damage->extents.y1;
276 cmd.destRight = damage->extents.x2;
277 cmd.destBottom = damage->extents.y2;
278 SURFACE_BPP(cmd) = 32;
279 SURFACE_CODECID(cmd) = peer->settings->NSCodecId;
280 SURFACE_WIDTH(cmd) = width;
281 SURFACE_HEIGHT(cmd) = height;
282
283 ptr = pixman_image_get_data(image) + damage->extents.x1 +
284 damage->extents.y1 * (pixman_image_get_stride(image) / sizeof(uint32_t));
285
286 nsc_compose_message(context->nsc_context, context->encode_stream, (BYTE *)ptr,
287 width, height,
288 pixman_image_get_stride(image));
289
290 SURFACE_BITMAP_DATA_LEN(cmd) = Stream_GetPosition(context->encode_stream);
291 SURFACE_BITMAP_DATA(cmd) = Stream_Buffer(context->encode_stream);
292
293 update->SurfaceBits(update->context, &cmd);
294 }
295
296 static void
pixman_image_flipped_subrect(const pixman_box32_t * rect,pixman_image_t * img,BYTE * dest)297 pixman_image_flipped_subrect(const pixman_box32_t *rect, pixman_image_t *img, BYTE *dest)
298 {
299 int stride = pixman_image_get_stride(img);
300 int h;
301 int toCopy = (rect->x2 - rect->x1) * 4;
302 int height = (rect->y2 - rect->y1);
303 const BYTE *src = (const BYTE *)pixman_image_get_data(img);
304 src += ((rect->y2-1) * stride) + (rect->x1 * 4);
305
306 for (h = 0; h < height; h++, src -= stride, dest += toCopy)
307 memcpy(dest, src, toCopy);
308 }
309
310 static void
rdp_peer_refresh_raw(pixman_region32_t * region,pixman_image_t * image,freerdp_peer * peer)311 rdp_peer_refresh_raw(pixman_region32_t *region, pixman_image_t *image, freerdp_peer *peer)
312 {
313 rdpUpdate *update = peer->update;
314 SURFACE_BITS_COMMAND cmd;
315 SURFACE_FRAME_MARKER marker;
316 pixman_box32_t *rect, subrect;
317 int nrects, i;
318 int heightIncrement, remainingHeight, top;
319
320 rect = pixman_region32_rectangles(region, &nrects);
321 if (!nrects)
322 return;
323
324 marker.frameId++;
325 marker.frameAction = SURFACECMD_FRAMEACTION_BEGIN;
326 update->SurfaceFrameMarker(peer->context, &marker);
327
328 memset(&cmd, 0, sizeof(cmd));
329 SURFACE_BPP(cmd) = 32;
330 SURFACE_CODECID(cmd) = 0;
331
332 for (i = 0; i < nrects; i++, rect++) {
333 /*weston_log("rect(%d,%d, %d,%d)\n", rect->x1, rect->y1, rect->x2, rect->y2);*/
334 cmd.destLeft = rect->x1;
335 cmd.destRight = rect->x2;
336 SURFACE_WIDTH(cmd) = rect->x2 - rect->x1;
337
338 heightIncrement = peer->settings->MultifragMaxRequestSize / (16 + SURFACE_WIDTH(cmd) * 4);
339 remainingHeight = rect->y2 - rect->y1;
340 top = rect->y1;
341
342 subrect.x1 = rect->x1;
343 subrect.x2 = rect->x2;
344
345 while (remainingHeight) {
346 SURFACE_HEIGHT(cmd) = (remainingHeight > heightIncrement) ? heightIncrement : remainingHeight;
347 cmd.destTop = top;
348 cmd.destBottom = top + SURFACE_HEIGHT(cmd);
349 SURFACE_BITMAP_DATA_LEN(cmd) = SURFACE_WIDTH(cmd) * SURFACE_HEIGHT(cmd) * 4;
350 SURFACE_BITMAP_DATA(cmd) = (BYTE *)realloc(SURFACE_BITMAP_DATA(cmd), SURFACE_BITMAP_DATA_LEN(cmd));
351
352 subrect.y1 = top;
353 subrect.y2 = top + SURFACE_HEIGHT(cmd);
354 pixman_image_flipped_subrect(&subrect, image, SURFACE_BITMAP_DATA(cmd));
355
356 /*weston_log("* sending (%d,%d, %d,%d)\n", subrect.x1, subrect.y1, subrect.x2, subrect.y2); */
357 update->SurfaceBits(peer->context, &cmd);
358
359 remainingHeight -= SURFACE_HEIGHT(cmd);
360 top += SURFACE_HEIGHT(cmd);
361 }
362 }
363
364 free(SURFACE_BITMAP_DATA(cmd));
365
366 marker.frameAction = SURFACECMD_FRAMEACTION_END;
367 update->SurfaceFrameMarker(peer->context, &marker);
368 }
369
370 static void
rdp_peer_refresh_region(pixman_region32_t * region,freerdp_peer * peer)371 rdp_peer_refresh_region(pixman_region32_t *region, freerdp_peer *peer)
372 {
373 RdpPeerContext *context = (RdpPeerContext *)peer->context;
374 struct rdp_output *output = context->rdpBackend->output;
375 rdpSettings *settings = peer->settings;
376
377 if (settings->RemoteFxCodec)
378 rdp_peer_refresh_rfx(region, output->shadow_surface, peer);
379 else if (settings->NSCodec)
380 rdp_peer_refresh_nsc(region, output->shadow_surface, peer);
381 else
382 rdp_peer_refresh_raw(region, output->shadow_surface, peer);
383 }
384
385 static int
rdp_output_start_repaint_loop(struct weston_output * output)386 rdp_output_start_repaint_loop(struct weston_output *output)
387 {
388 struct timespec ts;
389
390 weston_compositor_read_presentation_clock(output->compositor, &ts);
391 weston_output_finish_frame(output, &ts, WP_PRESENTATION_FEEDBACK_INVALID);
392
393 return 0;
394 }
395
396 static int
rdp_output_repaint(struct weston_output * output_base,pixman_region32_t * damage,void * repaint_data)397 rdp_output_repaint(struct weston_output *output_base, pixman_region32_t *damage,
398 void *repaint_data)
399 {
400 struct rdp_output *output = container_of(output_base, struct rdp_output, base);
401 struct weston_compositor *ec = output->base.compositor;
402 struct rdp_peers_item *outputPeer;
403
404 pixman_renderer_output_set_buffer(output_base, output->shadow_surface);
405 ec->renderer->repaint_output(&output->base, damage);
406
407 if (pixman_region32_not_empty(damage)) {
408 wl_list_for_each(outputPeer, &output->peers, link) {
409 if ((outputPeer->flags & RDP_PEER_ACTIVATED) &&
410 (outputPeer->flags & RDP_PEER_OUTPUT_ENABLED))
411 {
412 rdp_peer_refresh_region(damage, outputPeer->peer);
413 }
414 }
415 }
416
417 pixman_region32_subtract(&ec->primary_plane.damage,
418 &ec->primary_plane.damage, damage);
419
420 wl_event_source_timer_update(output->finish_frame_timer, 16);
421 return 0;
422 }
423
424 static int
finish_frame_handler(void * data)425 finish_frame_handler(void *data)
426 {
427 struct rdp_output *output = data;
428 struct timespec ts;
429
430 weston_compositor_read_presentation_clock(output->base.compositor, &ts);
431 weston_output_finish_frame(&output->base, &ts, 0);
432
433 return 1;
434 }
435
436 static struct weston_mode *
rdp_insert_new_mode(struct weston_output * output,int width,int height,int rate)437 rdp_insert_new_mode(struct weston_output *output, int width, int height, int rate)
438 {
439 struct weston_mode *ret;
440 ret = zalloc(sizeof *ret);
441 if (!ret)
442 return NULL;
443 ret->width = width;
444 ret->height = height;
445 ret->refresh = rate;
446 wl_list_insert(&output->mode_list, &ret->link);
447 return ret;
448 }
449
450 static struct weston_mode *
ensure_matching_mode(struct weston_output * output,struct weston_mode * target)451 ensure_matching_mode(struct weston_output *output, struct weston_mode *target)
452 {
453 struct weston_mode *local;
454
455 wl_list_for_each(local, &output->mode_list, link) {
456 if ((local->width == target->width) && (local->height == target->height))
457 return local;
458 }
459
460 return rdp_insert_new_mode(output, target->width, target->height, RDP_MODE_FREQ);
461 }
462
463 static int
rdp_switch_mode(struct weston_output * output,struct weston_mode * target_mode)464 rdp_switch_mode(struct weston_output *output, struct weston_mode *target_mode)
465 {
466 struct rdp_output *rdpOutput = container_of(output, struct rdp_output, base);
467 struct rdp_peers_item *rdpPeer;
468 rdpSettings *settings;
469 pixman_image_t *new_shadow_buffer;
470 struct weston_mode *local_mode;
471
472 local_mode = ensure_matching_mode(output, target_mode);
473 if (!local_mode) {
474 weston_log("mode %dx%d not available\n", target_mode->width, target_mode->height);
475 return -ENOENT;
476 }
477
478 if (local_mode == output->current_mode)
479 return 0;
480
481 output->current_mode->flags &= ~WL_OUTPUT_MODE_CURRENT;
482
483 output->current_mode = local_mode;
484 output->current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
485
486 pixman_renderer_output_destroy(output);
487 pixman_renderer_output_create(output, PIXMAN_RENDERER_OUTPUT_USE_SHADOW);
488
489 new_shadow_buffer = pixman_image_create_bits(PIXMAN_x8r8g8b8, target_mode->width,
490 target_mode->height, 0, target_mode->width * 4);
491 pixman_image_composite32(PIXMAN_OP_SRC, rdpOutput->shadow_surface, 0, new_shadow_buffer,
492 0, 0, 0, 0, 0, 0, target_mode->width, target_mode->height);
493 pixman_image_unref(rdpOutput->shadow_surface);
494 rdpOutput->shadow_surface = new_shadow_buffer;
495
496 wl_list_for_each(rdpPeer, &rdpOutput->peers, link) {
497 settings = rdpPeer->peer->settings;
498 if (settings->DesktopWidth == (UINT32)target_mode->width &&
499 settings->DesktopHeight == (UINT32)target_mode->height)
500 continue;
501
502 if (!settings->DesktopResize) {
503 /* too bad this peer does not support desktop resize */
504 rdpPeer->peer->Close(rdpPeer->peer);
505 } else {
506 settings->DesktopWidth = target_mode->width;
507 settings->DesktopHeight = target_mode->height;
508 rdpPeer->peer->update->DesktopResize(rdpPeer->peer->context);
509 }
510 }
511 return 0;
512 }
513
514 static int
rdp_output_set_size(struct weston_output * base,int width,int height)515 rdp_output_set_size(struct weston_output *base,
516 int width, int height)
517 {
518 struct rdp_output *output = to_rdp_output(base);
519 struct weston_head *head;
520 struct weston_mode *currentMode;
521 struct weston_mode initMode;
522
523 /* We can only be called once. */
524 assert(!output->base.current_mode);
525
526 wl_list_for_each(head, &output->base.head_list, output_link) {
527 weston_head_set_monitor_strings(head, "weston", "rdp", NULL);
528
529 /* XXX: Calculate proper size. */
530 weston_head_set_physical_size(head, width, height);
531 }
532
533 wl_list_init(&output->peers);
534
535 initMode.flags = WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
536 initMode.width = width;
537 initMode.height = height;
538 initMode.refresh = RDP_MODE_FREQ;
539
540 currentMode = ensure_matching_mode(&output->base, &initMode);
541 if (!currentMode)
542 return -1;
543
544 output->base.current_mode = output->base.native_mode = currentMode;
545
546 output->base.start_repaint_loop = rdp_output_start_repaint_loop;
547 output->base.repaint = rdp_output_repaint;
548 output->base.assign_planes = NULL;
549 output->base.set_backlight = NULL;
550 output->base.set_dpms = NULL;
551 output->base.switch_mode = rdp_switch_mode;
552
553 return 0;
554 }
555
556 static int
rdp_output_enable(struct weston_output * base)557 rdp_output_enable(struct weston_output *base)
558 {
559 struct rdp_output *output = to_rdp_output(base);
560 struct rdp_backend *b = to_rdp_backend(base->compositor);
561 struct wl_event_loop *loop;
562
563 output->shadow_surface = pixman_image_create_bits(PIXMAN_x8r8g8b8,
564 output->base.current_mode->width,
565 output->base.current_mode->height,
566 NULL,
567 output->base.current_mode->width * 4);
568 if (output->shadow_surface == NULL) {
569 weston_log("Failed to create surface for frame buffer.\n");
570 return -1;
571 }
572
573 if (pixman_renderer_output_create(&output->base,
574 PIXMAN_RENDERER_OUTPUT_USE_SHADOW) < 0) {
575 pixman_image_unref(output->shadow_surface);
576 return -1;
577 }
578
579 loop = wl_display_get_event_loop(b->compositor->wl_display);
580 output->finish_frame_timer = wl_event_loop_add_timer(loop, finish_frame_handler, output);
581
582 b->output = output;
583
584 return 0;
585 }
586
587 static int
rdp_output_disable(struct weston_output * base)588 rdp_output_disable(struct weston_output *base)
589 {
590 struct rdp_output *output = to_rdp_output(base);
591 struct rdp_backend *b = to_rdp_backend(base->compositor);
592
593 if (!output->base.enabled)
594 return 0;
595
596 pixman_image_unref(output->shadow_surface);
597 pixman_renderer_output_destroy(&output->base);
598
599 wl_event_source_remove(output->finish_frame_timer);
600 b->output = NULL;
601
602 return 0;
603 }
604
605 static void
rdp_output_destroy(struct weston_output * base)606 rdp_output_destroy(struct weston_output *base)
607 {
608 struct rdp_output *output = to_rdp_output(base);
609
610 rdp_output_disable(&output->base);
611 weston_output_release(&output->base);
612
613 free(output);
614 }
615
616 static struct weston_output *
rdp_output_create(struct weston_compositor * compositor,const char * name)617 rdp_output_create(struct weston_compositor *compositor, const char *name)
618 {
619 struct rdp_output *output;
620
621 output = zalloc(sizeof *output);
622 if (output == NULL)
623 return NULL;
624
625 weston_output_init(&output->base, compositor, name);
626
627 output->base.destroy = rdp_output_destroy;
628 output->base.disable = rdp_output_disable;
629 output->base.enable = rdp_output_enable;
630 output->base.attach_head = NULL;
631
632 weston_compositor_add_pending_output(&output->base, compositor);
633
634 return &output->base;
635 }
636
637 static int
rdp_head_create(struct weston_compositor * compositor,const char * name)638 rdp_head_create(struct weston_compositor *compositor, const char *name)
639 {
640 struct rdp_head *head;
641
642 head = zalloc(sizeof *head);
643 if (!head)
644 return -1;
645
646 weston_head_init(&head->base, name);
647 weston_head_set_connection_status(&head->base, true);
648 weston_compositor_add_head(compositor, &head->base);
649
650 return 0;
651 }
652
653 static void
rdp_head_destroy(struct rdp_head * head)654 rdp_head_destroy(struct rdp_head *head)
655 {
656 weston_head_release(&head->base);
657 free(head);
658 }
659
660 static void
rdp_destroy(struct weston_compositor * ec)661 rdp_destroy(struct weston_compositor *ec)
662 {
663 struct rdp_backend *b = to_rdp_backend(ec);
664 struct weston_head *base, *next;
665 int i;
666
667 weston_compositor_shutdown(ec);
668
669 wl_list_for_each_safe(base, next, &ec->head_list, compositor_link)
670 rdp_head_destroy(to_rdp_head(base));
671
672 for (i = 0; i < MAX_FREERDP_FDS; i++)
673 if (b->listener_events[i])
674 wl_event_source_remove(b->listener_events[i]);
675
676 freerdp_listener_free(b->listener);
677
678 free(b->server_cert);
679 free(b->server_key);
680 free(b->rdp_key);
681 free(b);
682 }
683
684 static
rdp_listener_activity(int fd,uint32_t mask,void * data)685 int rdp_listener_activity(int fd, uint32_t mask, void *data)
686 {
687 freerdp_listener* instance = (freerdp_listener *)data;
688
689 if (!(mask & WL_EVENT_READABLE))
690 return 0;
691 if (!instance->CheckFileDescriptor(instance)) {
692 weston_log("failed to check FreeRDP file descriptor\n");
693 return -1;
694 }
695 return 0;
696 }
697
698 static
rdp_implant_listener(struct rdp_backend * b,freerdp_listener * instance)699 int rdp_implant_listener(struct rdp_backend *b, freerdp_listener* instance)
700 {
701 int i, fd;
702 int rcount = 0;
703 void* rfds[MAX_FREERDP_FDS];
704 struct wl_event_loop *loop;
705
706 if (!instance->GetFileDescriptor(instance, rfds, &rcount)) {
707 weston_log("Failed to get FreeRDP file descriptor\n");
708 return -1;
709 }
710
711 loop = wl_display_get_event_loop(b->compositor->wl_display);
712 for (i = 0; i < rcount; i++) {
713 fd = (int)(long)(rfds[i]);
714 b->listener_events[i] = wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
715 rdp_listener_activity, instance);
716 }
717
718 for ( ; i < MAX_FREERDP_FDS; i++)
719 b->listener_events[i] = 0;
720 return 0;
721 }
722
723
724 static FREERDP_CB_RET_TYPE
rdp_peer_context_new(freerdp_peer * client,RdpPeerContext * context)725 rdp_peer_context_new(freerdp_peer* client, RdpPeerContext* context)
726 {
727 context->item.peer = client;
728 context->item.flags = RDP_PEER_OUTPUT_ENABLED;
729
730 #if FREERDP_VERSION_MAJOR == 1 && FREERDP_VERSION_MINOR == 1
731 context->rfx_context = rfx_context_new();
732 #else
733 context->rfx_context = rfx_context_new(TRUE);
734 #endif
735 if (!context->rfx_context) {
736 FREERDP_CB_RETURN(FALSE);
737 }
738
739 context->rfx_context->mode = RLGR3;
740 context->rfx_context->width = client->settings->DesktopWidth;
741 context->rfx_context->height = client->settings->DesktopHeight;
742 rfx_context_set_pixel_format(context->rfx_context, DEFAULT_PIXEL_FORMAT);
743
744 context->nsc_context = nsc_context_new();
745 if (!context->nsc_context)
746 goto out_error_nsc;
747
748 nsc_context_set_pixel_format(context->nsc_context, DEFAULT_PIXEL_FORMAT);
749
750 context->encode_stream = Stream_New(NULL, 65536);
751 if (!context->encode_stream)
752 goto out_error_stream;
753
754 FREERDP_CB_RETURN(TRUE);
755
756 out_error_nsc:
757 rfx_context_free(context->rfx_context);
758 out_error_stream:
759 nsc_context_free(context->nsc_context);
760 FREERDP_CB_RETURN(FALSE);
761 }
762
763 static void
rdp_peer_context_free(freerdp_peer * client,RdpPeerContext * context)764 rdp_peer_context_free(freerdp_peer* client, RdpPeerContext* context)
765 {
766 int i;
767 if (!context)
768 return;
769
770 wl_list_remove(&context->item.link);
771 for (i = 0; i < MAX_FREERDP_FDS; i++) {
772 if (context->events[i])
773 wl_event_source_remove(context->events[i]);
774 }
775
776 if (context->item.flags & RDP_PEER_ACTIVATED) {
777 weston_seat_release_keyboard(context->item.seat);
778 weston_seat_release_pointer(context->item.seat);
779 /* XXX we should weston_seat_release(context->item.seat); here
780 * but it would crash on reconnect */
781 }
782
783 Stream_Free(context->encode_stream, TRUE);
784 nsc_context_free(context->nsc_context);
785 rfx_context_free(context->rfx_context);
786 free(context->rfx_rects);
787 }
788
789
790 static int
rdp_client_activity(int fd,uint32_t mask,void * data)791 rdp_client_activity(int fd, uint32_t mask, void *data)
792 {
793 freerdp_peer* client = (freerdp_peer *)data;
794
795 if (!client->CheckFileDescriptor(client)) {
796 weston_log("unable to checkDescriptor for %p\n", client);
797 goto out_clean;
798 }
799 return 0;
800
801 out_clean:
802 freerdp_peer_context_free(client);
803 freerdp_peer_free(client);
804 return 0;
805 }
806
807 static BOOL
xf_peer_capabilities(freerdp_peer * client)808 xf_peer_capabilities(freerdp_peer* client)
809 {
810 return TRUE;
811 }
812
813 struct rdp_to_xkb_keyboard_layout {
814 UINT32 rdpLayoutCode;
815 const char *xkbLayout;
816 const char *xkbVariant;
817 };
818
819 /* table reversed from
820 https://github.com/awakecoding/FreeRDP/blob/master/libfreerdp/locale/xkb_layout_ids.c#L811 */
821 static
822 struct rdp_to_xkb_keyboard_layout rdp_keyboards[] = {
823 {KBD_ARABIC_101, "ara", 0},
824 {KBD_BULGARIAN, 0, 0},
825 {KBD_CHINESE_TRADITIONAL_US, 0},
826 {KBD_CZECH, "cz", 0},
827 {KBD_CZECH_PROGRAMMERS, "cz", "bksl"},
828 {KBD_CZECH_QWERTY, "cz", "qwerty"},
829 {KBD_DANISH, "dk", 0},
830 {KBD_GERMAN, "de", 0},
831 {KBD_GERMAN_NEO, "de", "neo"},
832 {KBD_GERMAN_IBM, "de", "qwerty"},
833 {KBD_GREEK, "gr", 0},
834 {KBD_GREEK_220, "gr", "simple"},
835 {KBD_GREEK_319, "gr", "extended"},
836 {KBD_GREEK_POLYTONIC, "gr", "polytonic"},
837 {KBD_US, "us", 0},
838 {KBD_US_ENGLISH_TABLE_FOR_IBM_ARABIC_238_L, "ara", "buckwalter"},
839 {KBD_SPANISH, "es", 0},
840 {KBD_SPANISH_VARIATION, "es", "nodeadkeys"},
841 {KBD_FINNISH, "fi", 0},
842 {KBD_FRENCH, "fr", 0},
843 {KBD_HEBREW, "il", 0},
844 {KBD_HUNGARIAN, "hu", 0},
845 {KBD_HUNGARIAN_101_KEY, "hu", "standard"},
846 {KBD_ICELANDIC, "is", 0},
847 {KBD_ITALIAN, "it", 0},
848 {KBD_ITALIAN_142, "it", "nodeadkeys"},
849 {KBD_JAPANESE, "jp", 0},
850 {KBD_JAPANESE_INPUT_SYSTEM_MS_IME2002, "jp", "kana"},
851 {KBD_KOREAN, "kr", 0},
852 {KBD_KOREAN_INPUT_SYSTEM_IME_2000, "kr", "kr104"},
853 {KBD_DUTCH, "nl", 0},
854 {KBD_NORWEGIAN, "no", 0},
855 {KBD_POLISH_PROGRAMMERS, "pl", 0},
856 {KBD_POLISH_214, "pl", "qwertz"},
857 {KBD_ROMANIAN, "ro", 0},
858 {KBD_RUSSIAN, "ru", 0},
859 {KBD_RUSSIAN_TYPEWRITER, "ru", "typewriter"},
860 {KBD_CROATIAN, "hr", 0},
861 {KBD_SLOVAK, "sk", 0},
862 {KBD_SLOVAK_QWERTY, "sk", "qwerty"},
863 {KBD_ALBANIAN, 0, 0},
864 {KBD_SWEDISH, "se", 0},
865 {KBD_THAI_KEDMANEE, "th", 0},
866 {KBD_THAI_KEDMANEE_NON_SHIFTLOCK, "th", "tis"},
867 {KBD_TURKISH_Q, "tr", 0},
868 {KBD_TURKISH_F, "tr", "f"},
869 {KBD_URDU, "in", "urd-phonetic3"},
870 {KBD_UKRAINIAN, "ua", 0},
871 {KBD_BELARUSIAN, "by", 0},
872 {KBD_SLOVENIAN, "si", 0},
873 {KBD_ESTONIAN, "ee", 0},
874 {KBD_LATVIAN, "lv", 0},
875 {KBD_LITHUANIAN_IBM, "lt", "ibm"},
876 {KBD_FARSI, "af", 0},
877 {KBD_VIETNAMESE, "vn", 0},
878 {KBD_ARMENIAN_EASTERN, "am", 0},
879 {KBD_AZERI_LATIN, 0, 0},
880 {KBD_FYRO_MACEDONIAN, "mk", 0},
881 {KBD_GEORGIAN, "ge", 0},
882 {KBD_FAEROESE, 0, 0},
883 {KBD_DEVANAGARI_INSCRIPT, 0, 0},
884 {KBD_MALTESE_47_KEY, 0, 0},
885 {KBD_NORWEGIAN_WITH_SAMI, "no", "smi"},
886 {KBD_KAZAKH, "kz", 0},
887 {KBD_KYRGYZ_CYRILLIC, "kg", "phonetic"},
888 {KBD_TATAR, "ru", "tt"},
889 {KBD_BENGALI, "bd", 0},
890 {KBD_BENGALI_INSCRIPT, "bd", "probhat"},
891 {KBD_PUNJABI, 0, 0},
892 {KBD_GUJARATI, "in", "guj"},
893 {KBD_TAMIL, "in", "tam"},
894 {KBD_TELUGU, "in", "tel"},
895 {KBD_KANNADA, "in", "kan"},
896 {KBD_MALAYALAM, "in", "mal"},
897 {KBD_HINDI_TRADITIONAL, "in", 0},
898 {KBD_MARATHI, 0, 0},
899 {KBD_MONGOLIAN_CYRILLIC, "mn", 0},
900 {KBD_UNITED_KINGDOM_EXTENDED, "gb", "intl"},
901 {KBD_SYRIAC, "syc", 0},
902 {KBD_SYRIAC_PHONETIC, "syc", "syc_phonetic"},
903 {KBD_NEPALI, "np", 0},
904 {KBD_PASHTO, "af", "ps"},
905 {KBD_DIVEHI_PHONETIC, 0, 0},
906 {KBD_LUXEMBOURGISH, 0, 0},
907 {KBD_MAORI, "mao", 0},
908 {KBD_CHINESE_SIMPLIFIED_US, 0, 0},
909 {KBD_SWISS_GERMAN, "ch", "de_nodeadkeys"},
910 {KBD_UNITED_KINGDOM, "gb", 0},
911 {KBD_LATIN_AMERICAN, "latam", 0},
912 {KBD_BELGIAN_FRENCH, "be", 0},
913 {KBD_BELGIAN_PERIOD, "be", "oss_sundeadkeys"},
914 {KBD_PORTUGUESE, "pt", 0},
915 {KBD_SERBIAN_LATIN, "rs", 0},
916 {KBD_AZERI_CYRILLIC, "az", "cyrillic"},
917 {KBD_SWEDISH_WITH_SAMI, "se", "smi"},
918 {KBD_UZBEK_CYRILLIC, "af", "uz"},
919 {KBD_INUKTITUT_LATIN, "ca", "ike"},
920 {KBD_CANADIAN_FRENCH_LEGACY, "ca", "fr-legacy"},
921 {KBD_SERBIAN_CYRILLIC, "rs", 0},
922 {KBD_CANADIAN_FRENCH, "ca", "fr-legacy"},
923 {KBD_SWISS_FRENCH, "ch", "fr"},
924 {KBD_BOSNIAN, "ba", 0},
925 {KBD_IRISH, 0, 0},
926 {KBD_BOSNIAN_CYRILLIC, "ba", "us"},
927 {KBD_UNITED_STATES_DVORAK, "us", "dvorak"},
928 {KBD_PORTUGUESE_BRAZILIAN_ABNT2, "br", "nativo"},
929 {KBD_CANADIAN_MULTILINGUAL_STANDARD, "ca", "multix"},
930 {KBD_GAELIC, "ie", "CloGaelach"},
931
932 {0x00000000, 0, 0},
933 };
934
935 /* taken from 2.2.7.1.6 Input Capability Set (TS_INPUT_CAPABILITYSET) */
936 static char *rdp_keyboard_types[] = {
937 "", /* 0: unused */
938 "", /* 1: IBM PC/XT or compatible (83-key) keyboard */
939 "", /* 2: Olivetti "ICO" (102-key) keyboard */
940 "", /* 3: IBM PC/AT (84-key) or similar keyboard */
941 "pc102",/* 4: IBM enhanced (101- or 102-key) keyboard */
942 "", /* 5: Nokia 1050 and similar keyboards */
943 "", /* 6: Nokia 9140 and similar keyboards */
944 "" /* 7: Japanese keyboard */
945 };
946
947 static BOOL
xf_peer_activate(freerdp_peer * client)948 xf_peer_activate(freerdp_peer* client)
949 {
950 RdpPeerContext *peerCtx;
951 struct rdp_backend *b;
952 struct rdp_output *output;
953 rdpSettings *settings;
954 rdpPointerUpdate *pointer;
955 struct rdp_peers_item *peersItem;
956 struct xkb_context *xkbContext;
957 struct xkb_rule_names xkbRuleNames;
958 struct xkb_keymap *keymap;
959 struct weston_output *weston_output;
960 int i;
961 pixman_box32_t box;
962 pixman_region32_t damage;
963 char seat_name[50];
964 POINTER_SYSTEM_UPDATE pointer_system;
965
966 peerCtx = (RdpPeerContext *)client->context;
967 b = peerCtx->rdpBackend;
968 peersItem = &peerCtx->item;
969 output = b->output;
970 settings = client->settings;
971
972 if (!settings->SurfaceCommandsEnabled) {
973 weston_log("client doesn't support required SurfaceCommands\n");
974 return FALSE;
975 }
976
977 if (b->force_no_compression && settings->CompressionEnabled) {
978 weston_log("Forcing compression off\n");
979 settings->CompressionEnabled = FALSE;
980 }
981
982 if (output->base.width != (int)settings->DesktopWidth ||
983 output->base.height != (int)settings->DesktopHeight)
984 {
985 if (b->no_clients_resize) {
986 /* RDP peers don't dictate their resolution to weston */
987 if (!settings->DesktopResize) {
988 /* peer does not support desktop resize */
989 weston_log("%s: client doesn't support resizing, closing connection\n", __FUNCTION__);
990 return FALSE;
991 } else {
992 settings->DesktopWidth = output->base.width;
993 settings->DesktopHeight = output->base.height;
994 client->update->DesktopResize(client->context);
995 }
996 } else {
997 /* ask weston to adjust size */
998 struct weston_mode new_mode;
999 struct weston_mode *target_mode;
1000 new_mode.width = (int)settings->DesktopWidth;
1001 new_mode.height = (int)settings->DesktopHeight;
1002 target_mode = ensure_matching_mode(&output->base, &new_mode);
1003 if (!target_mode) {
1004 weston_log("client mode not found\n");
1005 return FALSE;
1006 }
1007 weston_output_mode_set_native(&output->base, target_mode, 1);
1008 output->base.width = new_mode.width;
1009 output->base.height = new_mode.height;
1010 }
1011 }
1012
1013 weston_output = &output->base;
1014 RFX_RESET(peerCtx->rfx_context, weston_output->width, weston_output->height);
1015 NSC_RESET(peerCtx->nsc_context, weston_output->width, weston_output->height);
1016
1017 if (peersItem->flags & RDP_PEER_ACTIVATED)
1018 return TRUE;
1019
1020 /* when here it's the first reactivation, we need to setup a little more */
1021 weston_log("kbd_layout:0x%x kbd_type:0x%x kbd_subType:0x%x kbd_functionKeys:0x%x\n",
1022 settings->KeyboardLayout, settings->KeyboardType, settings->KeyboardSubType,
1023 settings->KeyboardFunctionKey);
1024
1025 memset(&xkbRuleNames, 0, sizeof(xkbRuleNames));
1026 if (settings->KeyboardType <= 7)
1027 xkbRuleNames.model = rdp_keyboard_types[settings->KeyboardType];
1028 for (i = 0; rdp_keyboards[i].rdpLayoutCode; i++) {
1029 if (rdp_keyboards[i].rdpLayoutCode == settings->KeyboardLayout) {
1030 xkbRuleNames.layout = rdp_keyboards[i].xkbLayout;
1031 xkbRuleNames.variant = rdp_keyboards[i].xkbVariant;
1032 weston_log("%s: matching layout=%s variant=%s\n", __FUNCTION__,
1033 xkbRuleNames.layout, xkbRuleNames.variant);
1034 break;
1035 }
1036 }
1037
1038 keymap = NULL;
1039 if (xkbRuleNames.layout) {
1040 xkbContext = xkb_context_new(0);
1041 if (!xkbContext) {
1042 weston_log("unable to create a xkb_context\n");
1043 return FALSE;
1044 }
1045
1046 keymap = xkb_keymap_new_from_names(xkbContext, &xkbRuleNames, 0);
1047 }
1048
1049 if (settings->ClientHostname)
1050 snprintf(seat_name, sizeof(seat_name), "RDP %s", settings->ClientHostname);
1051 else
1052 snprintf(seat_name, sizeof(seat_name), "RDP peer @%s", settings->ClientAddress);
1053
1054 peersItem->seat = zalloc(sizeof(*peersItem->seat));
1055 if (!peersItem->seat) {
1056 xkb_keymap_unref(keymap);
1057 weston_log("unable to create a weston_seat\n");
1058 return FALSE;
1059 }
1060
1061 weston_seat_init(peersItem->seat, b->compositor, seat_name);
1062 weston_seat_init_keyboard(peersItem->seat, keymap);
1063 weston_seat_init_pointer(peersItem->seat);
1064
1065 peersItem->flags |= RDP_PEER_ACTIVATED;
1066
1067 /* disable pointer on the client side */
1068 pointer = client->update->pointer;
1069 pointer_system.type = SYSPTR_NULL;
1070 pointer->PointerSystem(client->context, &pointer_system);
1071
1072 /* sends a full refresh */
1073 box.x1 = 0;
1074 box.y1 = 0;
1075 box.x2 = output->base.width;
1076 box.y2 = output->base.height;
1077 pixman_region32_init_with_extents(&damage, &box);
1078
1079 rdp_peer_refresh_region(&damage, client);
1080
1081 pixman_region32_fini(&damage);
1082
1083 return TRUE;
1084 }
1085
1086 static BOOL
xf_peer_post_connect(freerdp_peer * client)1087 xf_peer_post_connect(freerdp_peer *client)
1088 {
1089 return TRUE;
1090 }
1091
1092 static FREERDP_CB_RET_TYPE
xf_mouseEvent(rdpInput * input,UINT16 flags,UINT16 x,UINT16 y)1093 xf_mouseEvent(rdpInput *input, UINT16 flags, UINT16 x, UINT16 y)
1094 {
1095 RdpPeerContext *peerContext = (RdpPeerContext *)input->context;
1096 struct rdp_output *output;
1097 uint32_t button = 0;
1098 bool need_frame = false;
1099 struct timespec time;
1100
1101 if (flags & PTR_FLAGS_MOVE) {
1102 output = peerContext->rdpBackend->output;
1103 if (x < output->base.width && y < output->base.height) {
1104 weston_compositor_get_time(&time);
1105 notify_motion_absolute(peerContext->item.seat, &time,
1106 x, y);
1107 need_frame = true;
1108 }
1109 }
1110
1111 if (flags & PTR_FLAGS_BUTTON1)
1112 button = BTN_LEFT;
1113 else if (flags & PTR_FLAGS_BUTTON2)
1114 button = BTN_RIGHT;
1115 else if (flags & PTR_FLAGS_BUTTON3)
1116 button = BTN_MIDDLE;
1117
1118 if (button) {
1119 weston_compositor_get_time(&time);
1120 notify_button(peerContext->item.seat, &time, button,
1121 (flags & PTR_FLAGS_DOWN) ? WL_POINTER_BUTTON_STATE_PRESSED : WL_POINTER_BUTTON_STATE_RELEASED
1122 );
1123 need_frame = true;
1124 }
1125
1126 if (flags & PTR_FLAGS_WHEEL) {
1127 struct weston_pointer_axis_event weston_event;
1128 double value;
1129
1130 /* DEFAULT_AXIS_STEP_DISTANCE is stolen from compositor-x11.c
1131 * The RDP specs says the lower bits of flags contains the "the number of rotation
1132 * units the mouse wheel was rotated".
1133 *
1134 * https://blogs.msdn.microsoft.com/oldnewthing/20130123-00/?p=5473 explains the 120 value
1135 */
1136 value = -(flags & 0xff) / 120.0;
1137 if (flags & PTR_FLAGS_WHEEL_NEGATIVE)
1138 value = -value;
1139
1140 weston_event.axis = WL_POINTER_AXIS_VERTICAL_SCROLL;
1141 weston_event.value = DEFAULT_AXIS_STEP_DISTANCE * value;
1142 weston_event.discrete = (int)value;
1143 weston_event.has_discrete = true;
1144
1145 weston_compositor_get_time(&time);
1146
1147 notify_axis(peerContext->item.seat, &time, &weston_event);
1148 need_frame = true;
1149 }
1150
1151 if (need_frame)
1152 notify_pointer_frame(peerContext->item.seat);
1153
1154 FREERDP_CB_RETURN(TRUE);
1155 }
1156
1157 static FREERDP_CB_RET_TYPE
xf_extendedMouseEvent(rdpInput * input,UINT16 flags,UINT16 x,UINT16 y)1158 xf_extendedMouseEvent(rdpInput *input, UINT16 flags, UINT16 x, UINT16 y)
1159 {
1160 RdpPeerContext *peerContext = (RdpPeerContext *)input->context;
1161 struct rdp_output *output;
1162 struct timespec time;
1163
1164 output = peerContext->rdpBackend->output;
1165 if (x < output->base.width && y < output->base.height) {
1166 weston_compositor_get_time(&time);
1167 notify_motion_absolute(peerContext->item.seat, &time, x, y);
1168 }
1169
1170 FREERDP_CB_RETURN(TRUE);
1171 }
1172
1173
1174 static FREERDP_CB_RET_TYPE
xf_input_synchronize_event(rdpInput * input,UINT32 flags)1175 xf_input_synchronize_event(rdpInput *input, UINT32 flags)
1176 {
1177 freerdp_peer *client = input->context->peer;
1178 RdpPeerContext *peerCtx = (RdpPeerContext *)input->context;
1179 struct rdp_output *output = peerCtx->rdpBackend->output;
1180 pixman_box32_t box;
1181 pixman_region32_t damage;
1182
1183 /* sends a full refresh */
1184 box.x1 = 0;
1185 box.y1 = 0;
1186 box.x2 = output->base.width;
1187 box.y2 = output->base.height;
1188 pixman_region32_init_with_extents(&damage, &box);
1189
1190 rdp_peer_refresh_region(&damage, client);
1191
1192 pixman_region32_fini(&damage);
1193 FREERDP_CB_RETURN(TRUE);
1194 }
1195
1196
1197 static FREERDP_CB_RET_TYPE
xf_input_keyboard_event(rdpInput * input,UINT16 flags,UINT16 code)1198 xf_input_keyboard_event(rdpInput *input, UINT16 flags, UINT16 code)
1199 {
1200 uint32_t scan_code, vk_code, full_code;
1201 enum wl_keyboard_key_state keyState;
1202 RdpPeerContext *peerContext = (RdpPeerContext *)input->context;
1203 int notify = 0;
1204 struct timespec time;
1205
1206 if (!(peerContext->item.flags & RDP_PEER_ACTIVATED))
1207 FREERDP_CB_RETURN(TRUE);
1208
1209 if (flags & KBD_FLAGS_DOWN) {
1210 keyState = WL_KEYBOARD_KEY_STATE_PRESSED;
1211 notify = 1;
1212 } else if (flags & KBD_FLAGS_RELEASE) {
1213 keyState = WL_KEYBOARD_KEY_STATE_RELEASED;
1214 notify = 1;
1215 }
1216
1217 if (notify) {
1218 full_code = code;
1219 if (flags & KBD_FLAGS_EXTENDED)
1220 full_code |= KBD_FLAGS_EXTENDED;
1221
1222 vk_code = GetVirtualKeyCodeFromVirtualScanCode(full_code, 4);
1223 if (flags & KBD_FLAGS_EXTENDED)
1224 vk_code |= KBDEXT;
1225
1226 scan_code = GetKeycodeFromVirtualKeyCode(vk_code, KEYCODE_TYPE_EVDEV);
1227
1228 /*weston_log("code=%x ext=%d vk_code=%x scan_code=%x\n", code, (flags & KBD_FLAGS_EXTENDED) ? 1 : 0,
1229 vk_code, scan_code);*/
1230 weston_compositor_get_time(&time);
1231 notify_key(peerContext->item.seat, &time,
1232 scan_code - 8, keyState, STATE_UPDATE_AUTOMATIC);
1233 }
1234
1235 FREERDP_CB_RETURN(TRUE);
1236 }
1237
1238 static FREERDP_CB_RET_TYPE
xf_input_unicode_keyboard_event(rdpInput * input,UINT16 flags,UINT16 code)1239 xf_input_unicode_keyboard_event(rdpInput *input, UINT16 flags, UINT16 code)
1240 {
1241 weston_log("Client sent a unicode keyboard event (flags:0x%X code:0x%X)\n", flags, code);
1242 FREERDP_CB_RETURN(TRUE);
1243 }
1244
1245
1246 static FREERDP_CB_RET_TYPE
xf_suppress_output(rdpContext * context,BYTE allow,const RECTANGLE_16 * area)1247 xf_suppress_output(rdpContext *context, BYTE allow, const RECTANGLE_16 *area)
1248 {
1249 RdpPeerContext *peerContext = (RdpPeerContext *)context;
1250
1251 if (allow)
1252 peerContext->item.flags |= RDP_PEER_OUTPUT_ENABLED;
1253 else
1254 peerContext->item.flags &= (~RDP_PEER_OUTPUT_ENABLED);
1255
1256 FREERDP_CB_RETURN(TRUE);
1257 }
1258
1259 static int
rdp_peer_init(freerdp_peer * client,struct rdp_backend * b)1260 rdp_peer_init(freerdp_peer *client, struct rdp_backend *b)
1261 {
1262 int rcount = 0;
1263 void *rfds[MAX_FREERDP_FDS];
1264 int i, fd;
1265 struct wl_event_loop *loop;
1266 rdpSettings *settings;
1267 rdpInput *input;
1268 RdpPeerContext *peerCtx;
1269
1270 client->ContextSize = sizeof(RdpPeerContext);
1271 client->ContextNew = (psPeerContextNew)rdp_peer_context_new;
1272 client->ContextFree = (psPeerContextFree)rdp_peer_context_free;
1273 freerdp_peer_context_new(client);
1274
1275 peerCtx = (RdpPeerContext *) client->context;
1276 peerCtx->rdpBackend = b;
1277
1278 settings = client->settings;
1279 /* configure security settings */
1280 if (b->rdp_key)
1281 settings->RdpKeyFile = strdup(b->rdp_key);
1282 if (b->tls_enabled) {
1283 settings->CertificateFile = strdup(b->server_cert);
1284 settings->PrivateKeyFile = strdup(b->server_key);
1285 } else {
1286 settings->TlsSecurity = FALSE;
1287 }
1288 settings->NlaSecurity = FALSE;
1289
1290 if (!client->Initialize(client)) {
1291 weston_log("peer initialization failed\n");
1292 goto error_initialize;
1293 }
1294
1295 settings->OsMajorType = OSMAJORTYPE_UNIX;
1296 settings->OsMinorType = OSMINORTYPE_PSEUDO_XSERVER;
1297 settings->ColorDepth = 32;
1298 settings->RefreshRect = TRUE;
1299 settings->RemoteFxCodec = TRUE;
1300 settings->NSCodec = TRUE;
1301 settings->FrameMarkerCommandEnabled = TRUE;
1302 settings->SurfaceFrameMarkerEnabled = TRUE;
1303
1304 client->Capabilities = xf_peer_capabilities;
1305 client->PostConnect = xf_peer_post_connect;
1306 client->Activate = xf_peer_activate;
1307
1308 client->update->SuppressOutput = (pSuppressOutput)xf_suppress_output;
1309
1310 input = client->input;
1311 input->SynchronizeEvent = xf_input_synchronize_event;
1312 input->MouseEvent = xf_mouseEvent;
1313 input->ExtendedMouseEvent = xf_extendedMouseEvent;
1314 input->KeyboardEvent = xf_input_keyboard_event;
1315 input->UnicodeKeyboardEvent = xf_input_unicode_keyboard_event;
1316
1317 if (!client->GetFileDescriptor(client, rfds, &rcount)) {
1318 weston_log("unable to retrieve client fds\n");
1319 goto error_initialize;
1320 }
1321
1322 loop = wl_display_get_event_loop(b->compositor->wl_display);
1323 for (i = 0; i < rcount; i++) {
1324 fd = (int)(long)(rfds[i]);
1325
1326 peerCtx->events[i] = wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1327 rdp_client_activity, client);
1328 }
1329 for ( ; i < MAX_FREERDP_FDS; i++)
1330 peerCtx->events[i] = 0;
1331
1332 wl_list_insert(&b->output->peers, &peerCtx->item.link);
1333 return 0;
1334
1335 error_initialize:
1336 client->Close(client);
1337 return -1;
1338 }
1339
1340
1341 static FREERDP_CB_RET_TYPE
rdp_incoming_peer(freerdp_listener * instance,freerdp_peer * client)1342 rdp_incoming_peer(freerdp_listener *instance, freerdp_peer *client)
1343 {
1344 struct rdp_backend *b = (struct rdp_backend *)instance->param4;
1345 if (rdp_peer_init(client, b) < 0) {
1346 weston_log("error when treating incoming peer\n");
1347 FREERDP_CB_RETURN(FALSE);
1348 }
1349
1350 FREERDP_CB_RETURN(TRUE);
1351 }
1352
1353 static const struct weston_rdp_output_api api = {
1354 rdp_output_set_size,
1355 };
1356
1357 static struct rdp_backend *
rdp_backend_create(struct weston_compositor * compositor,struct weston_rdp_backend_config * config)1358 rdp_backend_create(struct weston_compositor *compositor,
1359 struct weston_rdp_backend_config *config)
1360 {
1361 struct rdp_backend *b;
1362 char *fd_str;
1363 char *fd_tail;
1364 int fd, ret;
1365
1366 b = zalloc(sizeof *b);
1367 if (b == NULL)
1368 return NULL;
1369
1370 b->compositor = compositor;
1371 b->base.destroy = rdp_destroy;
1372 b->base.create_output = rdp_output_create;
1373 b->rdp_key = config->rdp_key ? strdup(config->rdp_key) : NULL;
1374 b->no_clients_resize = config->no_clients_resize;
1375 b->force_no_compression = config->force_no_compression;
1376
1377 compositor->backend = &b->base;
1378
1379 /* activate TLS only if certificate/key are available */
1380 if (config->server_cert && config->server_key) {
1381 weston_log("TLS support activated\n");
1382 b->server_cert = strdup(config->server_cert);
1383 b->server_key = strdup(config->server_key);
1384 if (!b->server_cert || !b->server_key)
1385 goto err_free_strings;
1386 b->tls_enabled = 1;
1387 }
1388
1389 if (weston_compositor_set_presentation_clock_software(compositor) < 0)
1390 goto err_compositor;
1391
1392 if (pixman_renderer_init(compositor) < 0)
1393 goto err_compositor;
1394
1395 if (rdp_head_create(compositor, "rdp") < 0)
1396 goto err_compositor;
1397
1398 compositor->capabilities |= WESTON_CAP_ARBITRARY_MODES;
1399
1400 if (!config->env_socket) {
1401 b->listener = freerdp_listener_new();
1402 b->listener->PeerAccepted = rdp_incoming_peer;
1403 b->listener->param4 = b;
1404 if (!b->listener->Open(b->listener, config->bind_address, config->port)) {
1405 weston_log("unable to bind rdp socket\n");
1406 goto err_listener;
1407 }
1408
1409 if (rdp_implant_listener(b, b->listener) < 0)
1410 goto err_compositor;
1411 } else {
1412 /* get the socket from RDP_FD var */
1413 fd_str = getenv("RDP_FD");
1414 if (!fd_str) {
1415 weston_log("RDP_FD env variable not set\n");
1416 goto err_output;
1417 }
1418
1419 fd = strtoul(fd_str, &fd_tail, 10);
1420 if (errno != 0 || fd_tail == fd_str || *fd_tail != '\0'
1421 || rdp_peer_init(freerdp_peer_new(fd), b))
1422 goto err_output;
1423 }
1424
1425 ret = weston_plugin_api_register(compositor, WESTON_RDP_OUTPUT_API_NAME,
1426 &api, sizeof(api));
1427
1428 if (ret < 0) {
1429 weston_log("Failed to register output API.\n");
1430 goto err_output;
1431 }
1432
1433 return b;
1434
1435 err_listener:
1436 freerdp_listener_free(b->listener);
1437 err_output:
1438 weston_output_release(&b->output->base);
1439 err_compositor:
1440 weston_compositor_shutdown(compositor);
1441 err_free_strings:
1442 free(b->rdp_key);
1443 free(b->server_cert);
1444 free(b->server_key);
1445 free(b);
1446 return NULL;
1447 }
1448
1449 static void
config_init_to_defaults(struct weston_rdp_backend_config * config)1450 config_init_to_defaults(struct weston_rdp_backend_config *config)
1451 {
1452 config->bind_address = NULL;
1453 config->port = 3389;
1454 config->rdp_key = NULL;
1455 config->server_cert = NULL;
1456 config->server_key = NULL;
1457 config->env_socket = 0;
1458 config->no_clients_resize = 0;
1459 config->force_no_compression = 0;
1460 }
1461
1462 WL_EXPORT int
weston_backend_init(struct weston_compositor * compositor,struct weston_backend_config * config_base)1463 weston_backend_init(struct weston_compositor *compositor,
1464 struct weston_backend_config *config_base)
1465 {
1466 struct rdp_backend *b;
1467 struct weston_rdp_backend_config config = {{ 0, }};
1468 int major, minor, revision;
1469
1470 #if FREERDP_VERSION_MAJOR >= 2
1471 winpr_InitializeSSL(0);
1472 #endif
1473 freerdp_get_version(&major, &minor, &revision);
1474 weston_log("using FreeRDP version %d.%d.%d\n", major, minor, revision);
1475
1476 if (config_base == NULL ||
1477 config_base->struct_version != WESTON_RDP_BACKEND_CONFIG_VERSION ||
1478 config_base->struct_size > sizeof(struct weston_rdp_backend_config)) {
1479 weston_log("RDP backend config structure is invalid\n");
1480 return -1;
1481 }
1482
1483 config_init_to_defaults(&config);
1484 memcpy(&config, config_base, config_base->struct_size);
1485
1486 if (!config.rdp_key && (!config.server_cert || !config.server_key)) {
1487 weston_log("the RDP compositor requires keys and an optional certificate for RDP or TLS security ("
1488 "--rdp4-key or --rdp-tls-cert/--rdp-tls-key)\n");
1489 return -1;
1490 }
1491
1492 b = rdp_backend_create(compositor, &config);
1493 if (b == NULL)
1494 return -1;
1495 return 0;
1496 }
1497