Debian Codegraph @

subprojects/gtk-frdp/src/frdp-session.c

raw symbols SCIP
1 /* frdp-session.c
2 *
3 * Copyright (C) 2018 Felipe Borges <[email protected]>
4 * Copyright (C) 2019 Armin Novak <[email protected]>
5 * Copyright (C) 2023 Marek Kasik <[email protected]>
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <errno.h>
22 #include <freerdp/input.h>
23 #include <freerdp/locale/keyboard.h>
24 #include <freerdp/freerdp.h>
25 #include <freerdp/gdi/gdi.h>
26 #include <freerdp/gdi/video.h>
27 #include <freerdp/gdi/gfx.h>
28 #include <freerdp/client/channels.h>
29 #include <freerdp/client/cmdline.h>
30 #include <freerdp/client/channels.h>
31 #include <freerdp/client/rdpei.h>
32 #include <freerdp/client/tsmf.h>
33 #include <freerdp/client/rail.h>
34 #include <freerdp/client/cliprdr.h>
35 #include <freerdp/client/rdpgfx.h>
36 #include <freerdp/client/encomsp.h>
37 #include <freerdp/client/disp.h>
38 #include <freerdp/client/geometry.h>
39 #include <freerdp/client/video.h>
40 #include <freerdp/channels/disp.h>
41 #include <gdk/gdk.h>
42 #include <gio/gio.h>
43 #include <gtk/gtk.h>
44 #include <math.h>
45
46 #include "frdp-session.h"
47 #include "frdp-context.h"
48 #include "frdp-channel-display-control.h"
49 #include "frdp-channel-clipboard.h"
50
51 #define SELECT_TIMEOUT 20
52 #define FRDP_CONNECTION_THREAD_MAX_ERRORS 10
53
54 #ifdef HAVE_FREERDP3
55 #define CONST_QUALIFIER const
56 #else
57 #define CONST_QUALIFIER
58 #endif
59
60 struct frdp_pointer
61 {
62 rdpPointer pointer;
63 cairo_surface_t *data;
64 };
65 typedef struct frdp_pointer frdpPointer;
66
67 struct _FrdpSessionPrivate
68 {
69 freerdp *freerdp_session;
70
71 GtkWidget *display;
72 cairo_surface_t *surface;
73 cairo_format_t cairo_format;
74 gboolean scaling;
75 double scale;
76 double offset_x;
77 double offset_y;
78
79 guint update_id;
80
81 gboolean is_connected;
82
83 gchar *hostname;
84 gchar *username;
85 gchar *password;
86 gchar *domain;
87 guint port;
88
89 gboolean show_cursor;
90 gboolean cursor_null;
91 frdpPointer *cursor;
92
93 /* Channels */
94 FrdpChannelDisplayControl *display_control_channel;
95 FrdpChannelClipboard *clipboard_channel;
96 gboolean monitor_layout_supported;
97
98 GQueue *area_draw_queue; /* elem: GdkRectangle */
99 GMutex area_draw_mutex;
100 };
101
102 G_DEFINE_TYPE_WITH_PRIVATE (FrdpSession, frdp_session, G_TYPE_OBJECT)
103
104 enum
105 {
106 PROP_0 = 0,
107 PROP_HOSTNAME,
108 PROP_PORT,
109 PROP_USERNAME,
110 PROP_PASSWORD,
111 PROP_DISPLAY,
112 PROP_SCALING,
113 PROP_MONITOR_LAYOUT_SUPPORTED,
114 PROP_DOMAIN
115 };
116
117 enum
118 {
119 RDP_ERROR,
120 RDP_CONNECTED,
121 RDP_DISCONNECTED,
122 RDP_AUTH_FAILURE,
123 RDP_CHANNEL_CONNECTED,
124 LAST_SIGNAL
125 };
126
127 static guint signals[LAST_SIGNAL];
128
129 static void queue_draw_area (FrdpSession *self,
130 gint x,
131 gint y,
132 gint width,
133 gint height);
134
135 static void
136 frdp_session_update_mouse_pointer (FrdpSession *self)
137 {
138 FrdpSessionPrivate *priv = self->priv;
139 GdkCursor *cursor;
140 GdkDisplay *display;
141 GdkWindow *window;
142
143 window = gtk_widget_get_window (priv->display);
144 if (window == NULL)
145 return;
146
147 display = gtk_widget_get_display(priv->display);
148 if (priv->show_cursor && priv->cursor_null) {
149 cairo_surface_t *surface;
150 cairo_t *cairo;
151
152 /* Create a 1x1 image with transparent color */
153 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 1, 1);
154 cairo = cairo_create (surface);
155 cairo_set_source_rgba (cairo, 0.0, 0.0, 0.0, 0.0);
156 cairo_set_line_width(cairo, 1);
157 cairo_rectangle(cairo, 0, 0, 1, 1);
158 cairo_fill (cairo);
159
160 cursor = gdk_cursor_new_from_surface (display, surface, 0, 0);
161 cairo_surface_destroy (surface);
162 cairo_destroy (cairo);
163 } else if (!priv->show_cursor || !priv->cursor) {
164 /* No cursor set or none to show */
165 cursor = gdk_cursor_new_from_name (display, "default");
166 } else {
167 rdpPointer *pointer = &priv->cursor->pointer;
168 int window_scale_factor = gdk_window_get_scale_factor (window);
169 double x = priv->cursor->pointer.xPos * priv->scale;
170 double y = priv->cursor->pointer.yPos * priv->scale;
171 double w = ceil (pointer->width * priv->scale) * window_scale_factor;
172 double h = ceil (pointer->height * priv->scale) * window_scale_factor;
173 cairo_surface_t *surface;
174 cairo_t *cairo;
175
176 if (!self->priv->scaling) {
177 self->priv->scale = 1.0;
178 }
179
180 /* Scale the source image according to current settings. */
181 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, w, h);
182 cairo = cairo_create (surface);
183
184 cairo_scale (cairo,
185 self->priv->scale * window_scale_factor,
186 self->priv->scale * window_scale_factor);
187 cairo_set_source_surface (cairo, priv->cursor->data, 0, 0);
188 cairo_paint (cairo);
189
190 cairo_fill (cairo);
191 /* Set device scale to prevent GDK from scaling again based on HiDPI */
192 cairo_surface_set_device_scale (surface,
193 window_scale_factor,
194 window_scale_factor);
195 cursor = gdk_cursor_new_from_surface (display, surface, x, y);
196 cairo_surface_destroy (surface);
197 cairo_destroy (cairo);
198 }
199
200 gdk_window_set_cursor (window, cursor);
201 }
202
203 static BOOL
204 frdp_pointer_new (rdpContext *context,
205 rdpPointer *pointer)
206 {
207 frdpContext *fcontext = (frdpContext *) context;
208 frdpPointer *fpointer = (frdpPointer *) pointer;
209 int stride;
210 unsigned char *data;
211 cairo_surface_t *surface;
212
213 if (fcontext == NULL || fpointer == NULL)
214 return FALSE;
215
216 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
217 pointer->width,
218 pointer->height);
219 if (surface == NULL)
220 return FALSE;
221
222 { /* FreeRDP BUG https://github.com/FreeRDP/FreeRDP/issues/5061
223 * the function freerdp_image_copy_from_pointer_data
224 * does not initialize the buffer which results in broken alpha data. */
225 cairo_t *cairo = cairo_create (surface);
226
227 cairo_set_source_rgba (cairo, 0.0, 0.0, 0.0, 1.0);
228 cairo_fill (cairo);
229 cairo_paint (cairo);
230 cairo_destroy (cairo);
231 }
232
233 data = cairo_image_surface_get_data (surface);
234 if (data == NULL)
235 goto fail;
236
237 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32, pointer->width);
238 if (!freerdp_image_copy_from_pointer_data (data, PIXEL_FORMAT_BGRA32,
239 stride, 0, 0, pointer->width,
240 pointer->height,
241 pointer->xorMaskData,
242 pointer->lengthXorMask,
243 pointer->andMaskData,
244 pointer->lengthAndMask,
245 pointer->xorBpp,
246 &context->gdi->palette))
247 goto fail;
248
249 fpointer->data = surface;
250
251 return TRUE;
252
253 fail:
254 if (surface != NULL)
255 cairo_surface_destroy (surface);
256
257 return FALSE;
258 }
259
260 static void
261 frdp_pointer_free (rdpContext *context,
262 rdpPointer *pointer)
263 {
264 frdpPointer *fpointer = (frdpPointer *) pointer;
265
266 if (fpointer != NULL && fpointer->data != NULL) {
267 cairo_surface_destroy (fpointer->data);
268 fpointer->data = NULL;
269 }
270 }
271
272 static BOOL
273 frdp_pointer_set (rdpContext *context,
274 rdpPointer *pointer)
275 {
276 frdpContext *fcontext = (frdpContext *) context;
277 frdpPointer *fpointer = (frdpPointer *) pointer;
278 FrdpSessionPrivate *priv = fcontext->self->priv;
279
280 priv->cursor = fpointer;
281 priv->cursor_null = FALSE;
282
283 frdp_session_update_mouse_pointer (fcontext->self);
284
285 return TRUE;
286 }
287
288 static BOOL
289 frdp_pointer_set_null (rdpContext *context)
290 {
291 frdpContext *fcontext = (frdpContext *) context;
292 FrdpSessionPrivate *priv = fcontext->self->priv;
293
294 priv->cursor = NULL;
295 priv->cursor_null = TRUE;
296
297 frdp_session_update_mouse_pointer (fcontext->self);
298
299 return TRUE;
300 }
301
302 static BOOL
303 frdp_pointer_set_default (rdpContext *context)
304 {
305 frdpContext *fcontext = (frdpContext *) context;
306 FrdpSessionPrivate *priv = fcontext->self->priv;
307
308 priv->cursor = NULL;
309 priv->cursor_null = FALSE;
310
311 frdp_session_update_mouse_pointer (fcontext->self);
312
313 return TRUE;
314 }
315
316 static BOOL
317 frdp_pointer_set_position (rdpContext *context,
318 UINT32 x,
319 UINT32 y)
320 {
321 return TRUE;
322 }
323
324 static void
325 frdp_register_pointer (rdpGraphics *graphics)
326 {
327 rdpPointer pointer = { 0 };
328
329 pointer.size = sizeof (frdpPointer);
330 pointer.New = frdp_pointer_new;
331 pointer.Free = frdp_pointer_free;
332 pointer.Set = frdp_pointer_set;
333 pointer.SetNull = frdp_pointer_set_null;
334 pointer.SetDefault = frdp_pointer_set_default;
335 pointer.SetPosition = frdp_pointer_set_position;
336 graphics_register_pointer (graphics, &pointer);
337 }
338
339 static guint32
340 frdp_session_get_best_color_depth (FrdpSession *self)
341 {
342 GdkScreen *display;
343 GdkVisual *visual;
344
345 display = gdk_screen_get_default ();
346 visual = gdk_screen_get_rgba_visual (display);
347
348 return gdk_visual_get_depth (visual);
349 }
350
351 static void
352 create_cairo_surface (FrdpSession *self)
353 {
354 FrdpSessionPrivate *priv = self->priv;
355 rdpGdi *gdi;
356 gint stride;
357
358 if (priv->surface != NULL) {
359 cairo_surface_mark_dirty (priv->surface);
360 cairo_surface_destroy (priv->surface);
361 self->priv->surface = NULL;
362 }
363
364 gdi = priv->freerdp_session->context->gdi;
365
366 if (priv->scaling)
367 gtk_widget_set_size_request (priv->display, -1, -1);
368 else
369 gtk_widget_set_size_request (priv->display,
370 gdi->width,
371 gdi->height);
372 stride = cairo_format_stride_for_width (priv->cairo_format, gdi->width);
373 self->priv->surface =
374 cairo_image_surface_create_for_data ((unsigned char*) gdi->primary_buffer,
375 priv->cairo_format,
376 gdi->width,
377 gdi->height,
378 stride);
379 cairo_surface_flush (priv->surface);
380 }
381
382 static gboolean
383 frdp_desktop_resize (rdpContext *context)
384 {
385 FrdpSession *self = ((frdpContext *) context)->self;
386 rdpGdi *gdi = context->gdi;
387
388 if (gdi_resize (gdi,
389 freerdp_settings_get_uint32 (context->settings, FreeRDP_DesktopWidth),
390 freerdp_settings_get_uint32 (context->settings, FreeRDP_DesktopHeight))) {
391 create_cairo_surface (self);
392 return TRUE;
393 } else {
394 return FALSE;
395 }
396 }
397
398 static void
399 frdp_session_configure_event (GtkWidget *widget,
400 GdkEvent *event,
401 gpointer user_data)
402 {
403 FrdpSession *self = (FrdpSession*) user_data;
404 FrdpSessionPrivate *priv = self->priv;
405 GtkWidget *scrolled;
406 rdpSettings *settings;
407 rdpGdi *gdi;
408 double width, height, widget_ratio, server_ratio;
409 gboolean allow_resize;
410 guint32 desktop_width, desktop_height;
411
412 if (priv->freerdp_session == NULL)
413 return;
414
415 gdi = priv->freerdp_session->context->gdi;
416 if (priv->surface == NULL) {
417 create_cairo_surface (self);
418 }
419
420 scrolled = gtk_widget_get_ancestor (widget, GTK_TYPE_SCROLLED_WINDOW);
421 width = (double)gtk_widget_get_allocated_width (scrolled);
422 height = (double)gtk_widget_get_allocated_height (scrolled);
423
424 if (priv->freerdp_session->context->settings == NULL)
425 return;
426
427 settings = priv->freerdp_session->context->settings;
428 desktop_width = freerdp_settings_get_uint32 (settings, FreeRDP_DesktopWidth);
429 desktop_height = freerdp_settings_get_uint32 (settings, FreeRDP_DesktopHeight);
430
431 g_object_get (G_OBJECT (widget), "allow-resize", &allow_resize, NULL);
432
433 if (allow_resize) {
434 guint32 request_width = width;
435 guint32 request_height = height;
436
437 /* Round width to even number */
438 if (request_width % 2 == 1)
439 request_width--;
440
441 /* Only request resize if the rounded dimensions differ from current desktop size.
442 * This prevents infinite resize loops when the window has odd dimensions. */
443 if ((desktop_width != request_width || desktop_height != request_height) &&
444 priv->display_control_channel != NULL) {
445 frdp_channel_display_control_resize_display (priv->display_control_channel,
446 request_width,
447 request_height);
448 }
449 } else {
450 if (priv->scaling) {
451 widget_ratio = height > 0 ? width / height : 1.0;
452 server_ratio = desktop_height > 0 ? (double) desktop_width / desktop_height : 1.0;
453
454 if (widget_ratio > server_ratio)
455 self->priv->scale = height / desktop_height;
456 else
457 self->priv->scale = width / desktop_width;
458
459 self->priv->offset_x = (width - desktop_width * self->priv->scale) / 2.0;
460 self->priv->offset_y = (height - desktop_height * self->priv->scale) / 2.0;
461 } else {
462 gtk_widget_set_size_request (priv->display, gdi->width, gdi->height);
463 }
464 }
465 }
466
467 static void
468 frdp_session_resize_supported_changed (FrdpDisplay *display,
469 GParamSpec *pspec,
470 gpointer user_data)
471 {
472 FrdpSession *self = user_data;
473 FrdpSessionPrivate *priv = frdp_session_get_instance_private (self);
474 GtkWidget *scrolled;
475 gboolean resize_supported, allow_resize;
476 gint width, height;
477
478 g_object_get (G_OBJECT (display),
479 "resize-supported", &resize_supported,
480 "allow-resize", &allow_resize,
481 NULL);
482
483 if (resize_supported && allow_resize)
484 {
485 scrolled = gtk_widget_get_ancestor (GTK_WIDGET (display), GTK_TYPE_SCROLLED_WINDOW);
486 width = gtk_widget_get_allocated_width (scrolled);
487 height = gtk_widget_get_allocated_height (scrolled);
488
489 frdp_channel_display_control_resize_display (priv->display_control_channel,
490 width,
491 height);
492 }
493 }
494
495 static void
496 frdp_session_set_scaling (FrdpSession *self,
497 gboolean scaling)
498 {
499 self->priv->scaling = scaling;
500 }
501
502 static gboolean
503 frdp_session_draw (GtkWidget *widget,
504 cairo_t *cr,
505 gpointer user_data)
506 {
507 FrdpSession *self = (FrdpSession*) user_data;
508
509 // Nothing to draw if disconnected
510 if (!self->priv->is_connected)
511 return FALSE;
512
513 if (self->priv->surface == NULL ||
514 (self->priv->freerdp_session->context->gdi->width != cairo_image_surface_get_width (self->priv->surface) ||
515 self->priv->freerdp_session->context->gdi->height != cairo_image_surface_get_height (self->priv->surface))) {
516 create_cairo_surface (self);
517 }
518
519 if (self->priv->scaling) {
520 cairo_translate (cr, self->priv->offset_x, self->priv->offset_y);
521 cairo_scale (cr, self->priv->scale, self->priv->scale);
522 }
523
524 cairo_set_source_surface (cr, self->priv->surface, 0, 0);
525 cairo_paint (cr);
526
527 frdp_display_set_scaling (FRDP_DISPLAY (self->priv->display), self->priv->scaling);
528
529 return TRUE;
530 }
531
532 /*
533 * Return 1 to accept and store a certificate, 2 to accept
534 * a certificate only for this session, 0 otherwise.
535 */
536 static guint
537 frdp_certificate_verify_ex (freerdp *freerdp_session,
538 const gchar *host,
539 guint16 port,
540 const gchar *common_name,
541 const gchar *subject,
542 const gchar *issuer,
543 const gchar *fingerprint,
544 guint32 flags)
545 {
546 FrdpSession *self = ((frdpContext *) freerdp_session->context)->self;
547
548 return frdp_display_certificate_verify_ex (FRDP_DISPLAY (self->priv->display),
549 host,
550 port,
551 common_name,
552 subject,
553 issuer,
554 fingerprint,
555 flags);
556 }
557
558 static guint
559 frdp_changed_certificate_verify_ex (freerdp *freerdp_session,
560 const gchar *host,
561 guint16 port,
562 const gchar *common_name,
563 const gchar *subject,
564 const gchar *issuer,
565 const gchar *fingerprint,
566 const gchar *old_subject,
567 const gchar *old_issuer,
568 const gchar *old_fingerprint,
569 guint32 flags)
570 {
571 FrdpSession *self = ((frdpContext *) freerdp_session->context)->self;
572
573 return frdp_display_certificate_change_verify_ex (FRDP_DISPLAY (self->priv->display),
574 host,
575 port,
576 common_name,
577 subject,
578 issuer,
579 fingerprint,
580 old_subject,
581 old_issuer,
582 old_fingerprint,
583 flags);
584 }
585
586 static gboolean
587 frdp_authenticate_ex (freerdp *freerdp_session,
588 gchar **username,
589 gchar **password,
590 gchar **domain,
591 rdp_auth_reason reason)
592 {
593 FrdpSession *self = ((frdpContext *) freerdp_session->context)->self;
594
595 return frdp_display_authenticate (FRDP_DISPLAY (self->priv->display),
596 username,
597 password,
598 domain);
599 }
600
601 static void
602 caps_set (FrdpChannelDisplayControl *channel,
603 gpointer user_data)
604 {
605 FrdpSession *session = user_data;
606
607 g_object_set (G_OBJECT (session), "monitor-layout-supported", TRUE, NULL);
608 }
609
610 static void
611 frdp_on_channel_connected_event_handler (void *context,
612 CONST_QUALIFIER ChannelConnectedEventArgs *e)
613 {
614 frdpContext *ctx = (frdpContext *) context;
615 FrdpSession *session = ctx->self;
616 FrdpSessionPrivate *priv = frdp_session_get_instance_private (session);
617
618 if (strcmp (e->name, RDPEI_DVC_CHANNEL_NAME) == 0) {
619 // TODO Touch input redirection
620 } else if (strcmp (e->name, DISP_DVC_CHANNEL_NAME) == 0) {
621 g_clear_object (&priv->display_control_channel);
622
623 priv->display_control_channel = g_object_new (FRDP_TYPE_CHANNEL_DISPLAY_CONTROL,
624 "session", session,
625 "display-client-context", (DispClientContext *) e->pInterface,
626 NULL);
627 g_signal_connect (priv->display_control_channel, "caps-set", G_CALLBACK (caps_set), session);
628 } else if (strcmp (e->name, TSMF_DVC_CHANNEL_NAME) == 0) {
629 // TODO Old windows 7 multimedia redirection
630 } else if (strcmp (e->name, RDPGFX_DVC_CHANNEL_NAME) == 0) {
631 gdi_graphics_pipeline_init (ctx->context.gdi, (RdpgfxClientContext *) e->pInterface);
632 } else if (strcmp (e->name, RAIL_SVC_CHANNEL_NAME) == 0) {
633 // TODO Remote application
634 } else if (strcmp (e->name, CLIPRDR_SVC_CHANNEL_NAME) == 0) {
635 g_clear_object (&priv->clipboard_channel);
636
637 priv->clipboard_channel = g_object_new (FRDP_TYPE_CHANNEL_CLIPBOARD,
638 "session", session,
639 "cliprdr-client-context", (CliprdrClientContext *) e->pInterface,
640 NULL);
641 } else if (strcmp (e->name, ENCOMSP_SVC_CHANNEL_NAME) == 0) {
642 // TODO Multiparty channel
643 } else if (strcmp (e->name, GEOMETRY_DVC_CHANNEL_NAME) == 0) {
644 gdi_video_geometry_init (ctx->context.gdi, (GeometryClientContext *) e->pInterface);
645 } else if (strcmp (e->name, VIDEO_CONTROL_DVC_CHANNEL_NAME) == 0) {
646 gdi_video_control_init (ctx->context.gdi, (VideoClientContext *) e->pInterface);
647 } else if (strcmp (e->name, VIDEO_DATA_DVC_CHANNEL_NAME) == 0) {
648 gdi_video_data_init (ctx->context.gdi, (VideoClientContext *) e->pInterface);
649 }
650 }
651
652 static void
653 frdp_on_channel_disconnected_event_handler (void *context,
654 CONST_QUALIFIER ChannelDisconnectedEventArgs *e)
655 {
656 frdpContext *ctx = (frdpContext *) context;
657 FrdpSession *session = ctx->self;
658 FrdpSessionPrivate *priv = frdp_session_get_instance_private (session);
659
660 if (strcmp (e->name, RDPEI_DVC_CHANNEL_NAME) == 0) {
661 // TODO Touch input redirection
662 } else if (strcmp (e->name, DISP_DVC_CHANNEL_NAME) == 0) {
663 g_clear_object (&priv->display_control_channel);
664 } else if (strcmp (e->name, TSMF_DVC_CHANNEL_NAME) == 0) {
665 // TODO Old windows 7 multimedia redirection
666 } else if (strcmp (e->name, RDPGFX_DVC_CHANNEL_NAME) == 0) {
667 gdi_graphics_pipeline_uninit (ctx->context.gdi, (RdpgfxClientContext *) e->pInterface);
668 } else if (strcmp (e->name, RAIL_SVC_CHANNEL_NAME) == 0) {
669 // TODO Remote application
670 } else if (strcmp (e->name, CLIPRDR_SVC_CHANNEL_NAME) == 0) {
671 g_clear_object (&priv->clipboard_channel);
672 } else if (strcmp (e->name, ENCOMSP_SVC_CHANNEL_NAME) == 0) {
673 // TODO Multiparty channel
674 } else if (strcmp (e->name, GEOMETRY_DVC_CHANNEL_NAME) == 0) {
675 gdi_video_geometry_uninit (ctx->context.gdi, (GeometryClientContext *) e->pInterface);
676 } else if (strcmp (e->name, VIDEO_CONTROL_DVC_CHANNEL_NAME) == 0) {
677 gdi_video_control_uninit (ctx->context.gdi, (VideoClientContext *) e->pInterface);
678 } else if (strcmp (e->name, VIDEO_DATA_DVC_CHANNEL_NAME) == 0) {
679 gdi_video_data_uninit (ctx->context.gdi, (VideoClientContext *) e->pInterface);
680 }
681 }
682
683 static gboolean
684 frdp_load_channels (freerdp *instance)
685 {
686 if (!freerdp_client_load_addins (instance->context->channels, instance->context->settings))
687 return FALSE;
688
689 return TRUE;
690 }
691
692 static gboolean
693 frdp_pre_connect (freerdp *freerdp_session)
694 {
695 rdpSettings *settings = freerdp_session->context->settings;
696 rdpContext *context = freerdp_session->context;
697 BYTE *OrderSupport = freerdp_settings_get_pointer_writable(settings, FreeRDP_OrderSupport);
698
699 if (OrderSupport != NULL) {
700 OrderSupport[NEG_DSTBLT_INDEX] = TRUE;
701 OrderSupport[NEG_PATBLT_INDEX] = TRUE;
702 OrderSupport[NEG_SCRBLT_INDEX] = TRUE;
703 OrderSupport[NEG_OPAQUE_RECT_INDEX] = TRUE;
704 OrderSupport[NEG_DRAWNINEGRID_INDEX] = FALSE;
705 OrderSupport[NEG_MULTIDSTBLT_INDEX] = FALSE;
706 OrderSupport[NEG_MULTIPATBLT_INDEX] = FALSE;
707 OrderSupport[NEG_MULTISCRBLT_INDEX] = FALSE;
708 OrderSupport[NEG_MULTIOPAQUERECT_INDEX] = TRUE;
709 OrderSupport[NEG_MULTI_DRAWNINEGRID_INDEX] = FALSE;
710 OrderSupport[NEG_LINETO_INDEX] = TRUE;
711 OrderSupport[NEG_POLYLINE_INDEX] = TRUE;
712 OrderSupport[NEG_MEMBLT_INDEX] = TRUE;
713 OrderSupport[NEG_MEM3BLT_INDEX] = FALSE;
714 OrderSupport[NEG_MEMBLT_V2_INDEX] = TRUE;
715 OrderSupport[NEG_MEM3BLT_V2_INDEX] = FALSE;
716 OrderSupport[NEG_SAVEBITMAP_INDEX] = FALSE;
717 OrderSupport[NEG_GLYPH_INDEX_INDEX] = TRUE;
718 OrderSupport[NEG_FAST_INDEX_INDEX] = TRUE;
719 OrderSupport[NEG_FAST_GLYPH_INDEX] = FALSE;
720 OrderSupport[NEG_POLYGON_SC_INDEX] = FALSE;
721 OrderSupport[NEG_POLYGON_CB_INDEX] = FALSE;
722 OrderSupport[NEG_ELLIPSE_SC_INDEX] = FALSE;
723 OrderSupport[NEG_ELLIPSE_CB_INDEX] = FALSE;
724 }
725
726 PubSub_SubscribeChannelConnected (context->pubSub,
727 frdp_on_channel_connected_event_handler);
728 PubSub_SubscribeChannelDisconnected (context->pubSub,
729 frdp_on_channel_disconnected_event_handler);
730
731 #ifndef HAVE_FREERDP3
732 return frdp_load_channels (freerdp_session);
733 #else
734 return TRUE;
735 #endif
736 }
737
738 static gboolean
739 frdp_begin_paint (rdpContext *context)
740 {
741 rdpGdi *gdi = context->gdi;
742
743 gdi->primary->hdc->hwnd->invalid->null = 1;
744 gdi->primary->hdc->hwnd->ninvalid = 0;
745
746 return TRUE;
747 }
748
749 static void
750 queue_draw_area (FrdpSession *self,
751 gint x,
752 gint y,
753 gint width,
754 gint height)
755 {
756 FrdpSessionPrivate *priv = self->priv;
757 GdkRectangle *rectangle;
758
759 rectangle = g_new (GdkRectangle, 1);
760 rectangle->x = x;
761 rectangle->y = y;
762 rectangle->width = width;
763 rectangle->height = height;
764
765 g_mutex_lock (&priv->area_draw_mutex);
766
767 if (priv->area_draw_queue != NULL)
768 g_queue_push_tail (priv->area_draw_queue, rectangle);
769
770 g_mutex_unlock (&priv->area_draw_mutex);
771 }
772
773 static gboolean
774 frdp_end_paint (rdpContext *context)
775 {
776 FrdpSessionPrivate *priv;
777 FrdpSession *self = ((frdpContext *) context)->self;
778 rdpGdi *gdi = context->gdi;
779 gint x, y, w, h;
780 gint pos_x, pos_y;
781
782 if (gdi->primary->hdc->hwnd->invalid->null)
783 return TRUE;
784
785 x = gdi->primary->hdc->hwnd->invalid->x;
786 y = gdi->primary->hdc->hwnd->invalid->y;
787 w = gdi->primary->hdc->hwnd->invalid->w;
788 h = gdi->primary->hdc->hwnd->invalid->h;
789
790 priv = self->priv;
791
792 if (priv->scaling) {
793 pos_x = self->priv->offset_x + x * priv->scale;
794 pos_y = self->priv->offset_y + y * priv->scale;
795 queue_draw_area (self,
796 floor (pos_x),
797 floor (pos_y),
798 ceil (pos_x + w * priv->scale) - floor (pos_x),
799 ceil (pos_y + h * priv->scale) - floor (pos_y));
800 } else {
801 queue_draw_area (self, x, y, w, h);
802 }
803
804 return TRUE;
805 }
806
807 static gboolean
808 frdp_post_connect (freerdp *freerdp_session)
809 {
810 rdpSettings *settings;
811 rdpContext *context;
812 FrdpSession *self = ((frdpContext *) freerdp_session->context)->self;
813 guint32 color_format;
814 ResizeWindowEventArgs e;
815
816 context = freerdp_session->context;
817 settings = context->settings;
818 switch (frdp_session_get_best_color_depth (self)) {
819 case 32:
820 color_format = PIXEL_FORMAT_BGRX32;
821 self->priv->cairo_format = CAIRO_FORMAT_ARGB32;
822 break;
823 case 24:
824 color_format = PIXEL_FORMAT_BGRX32;
825 self->priv->cairo_format = CAIRO_FORMAT_RGB24;
826 break;
827 case 16:
828 case 15:
829 color_format = PIXEL_FORMAT_BGR16;
830 self->priv->cairo_format = CAIRO_FORMAT_RGB16_565;
831 break;
832 default:
833 color_format = PIXEL_FORMAT_BGRX32;
834 self->priv->cairo_format = CAIRO_FORMAT_RGB16_565;
835 break;
836 }
837
838 gdi_init (freerdp_session, color_format);
839
840 frdp_register_pointer (freerdp_session->context->graphics);
841 freerdp_session->context->update->BeginPaint = frdp_begin_paint;
842 freerdp_session->context->update->EndPaint = frdp_end_paint;
843 freerdp_session->context->update->DesktopResize = frdp_desktop_resize;
844
845 EventArgsInit(&e, "frdp");
846 e.width = freerdp_settings_get_uint32 (settings, FreeRDP_DesktopWidth);
847 e.height = freerdp_settings_get_uint32 (settings, FreeRDP_DesktopHeight);
848 PubSub_OnResizeWindow(context->pubSub, freerdp_session->context, &e);
849
850 create_cairo_surface (self);
851
852 return TRUE;
853 }
854
855 static void
856 frdp_post_disconnect (freerdp *instance)
857 {
858 FrdpSession *self;
859 rdpContext *context;
860
861 if (!instance || !instance->context)
862 return;
863
864 self = ((frdpContext *) instance->context)->self;
865
866 g_signal_handlers_disconnect_by_func (self->priv->display, G_CALLBACK (frdp_session_draw), self);
867 g_signal_handlers_disconnect_by_func (self->priv->display, G_CALLBACK (frdp_session_configure_event), self);
868 g_signal_handlers_disconnect_by_func (self->priv->display, G_CALLBACK (frdp_session_resize_supported_changed), self);
869
870 context = instance->context;
871 PubSub_UnsubscribeChannelConnected (context->pubSub,
872 frdp_on_channel_connected_event_handler);
873 PubSub_UnsubscribeChannelDisconnected (context->pubSub,
874 frdp_on_channel_disconnected_event_handler);
875
876 frdp_pointer_set_null (context);
877
878 gdi_free (instance);
879 }
880
881 static gboolean
882 idle_close (gpointer user_data)
883 {
884 FrdpSession *self = (FrdpSession*) user_data;
885
886 self->priv->is_connected = FALSE;
887
888 if (self->priv->update_id > 0) {
889 g_source_remove (self->priv->update_id);
890 self->priv->update_id = 0;
891 }
892
893 g_mutex_lock (&self->priv->area_draw_mutex);
894 g_queue_clear_full (self->priv->area_draw_queue, g_free);
895 g_mutex_unlock (&self->priv->area_draw_mutex);
896 g_mutex_clear (&self->priv->area_draw_mutex);
897
898 if (self->priv->freerdp_session != NULL) {
899 freerdp_disconnect (self->priv->freerdp_session);
900 g_clear_pointer (&self->priv->freerdp_session, freerdp_free);
901 }
902
903 g_signal_emit (self, signals[RDP_DISCONNECTED], 0);
904 g_debug ("RDP client disconnected");
905
906 return FALSE;
907 }
908
909 static gboolean
910 update (gpointer user_data)
911 {
912 DWORD status;
913 HANDLE handles[64];
914 DWORD usedHandles;
915 FrdpSessionPrivate *priv;
916 FrdpSession *self = (FrdpSession*) user_data;
917 GdkRectangle *rectangle;
918
919 priv = self->priv;
920
921 if (priv->display != NULL && !gtk_widget_get_mapped (priv->display))
922 return TRUE;
923
924 g_mutex_lock (&priv->area_draw_mutex);
925
926 while (priv->area_draw_queue != NULL && !g_queue_is_empty (priv->area_draw_queue)) {
927 rectangle = g_queue_pop_head (priv->area_draw_queue);
928 gtk_widget_queue_draw_area (priv->display, rectangle->x, rectangle->y, rectangle->width, rectangle->height);
929 g_free (rectangle);
930 }
931
932 g_mutex_unlock (&priv->area_draw_mutex);
933
934 if (freerdp_shall_disconnect_context (priv->freerdp_session->context)) {
935 priv->update_id = 0;
936 g_idle_add ((GSourceFunc) idle_close, self);
937
938 return FALSE;
939 }
940
941 usedHandles = freerdp_get_event_handles (priv->freerdp_session->context,
942 handles, ARRAYSIZE(handles));
943 if (usedHandles == 0) {
944 g_warning ("Failed to get FreeRDP event handle");
945 priv->update_id = 0;
946 return FALSE;
947 }
948
949 status = WaitForMultipleObjects (usedHandles, handles, FALSE, SELECT_TIMEOUT);
950 if (status == WAIT_TIMEOUT)
951 return TRUE;
952 if (status == WAIT_FAILED) {
953 priv->update_id = 0;
954 return FALSE;
955 }
956
957 if (!freerdp_check_event_handles (priv->freerdp_session->context)) {
958 if (freerdp_get_last_error(priv->freerdp_session->context) == FREERDP_ERROR_SUCCESS) {
959 g_warning ("Failed to check FreeRDP file descriptor");
960 }
961
962 return TRUE;
963 }
964
965 return TRUE;
966 }
967
968 /*
969 * The local layout string is taken from org.gnome.desktop.input-sources.mru-sources
970 * after adding it among available sources and setting it active.
971 * Most of the national layout has been assigned according to default keyboard
972 * for given nation so these can be inaccurate.
973 * Non-standard layouts has to be checked directly by comparing them
974 * (e.g. against layouts on https://kbdlayout.info/).
975 */
976 static const struct {
977 const char *local_layout;
978 const guint freerdp_layout;
979 } keyboard_layouts[] = {
980 { "ara", KBD_ARABIC_101 },
981 { "bg", KBD_BULGARIAN },
982 { "cz", KBD_CZECH },
983 { "da", KBD_DANISH },
984 { "de", KBD_GERMAN },
985 { "gr", KBD_GREEK },
986 { "us", KBD_US },
987 { "es", KBD_SPANISH },
988 { "fi", KBD_FINNISH },
989 { "fr", KBD_FRENCH },
990 { "il", KBD_HEBREW },
991 { "hu", KBD_HUNGARIAN },
992 { "is", KBD_ICELANDIC },
993 { "it", KBD_ITALIAN },
994 { "jp", KBD_JAPANESE },
995 { "kr", KBD_KOREAN },
996 { "nl", KBD_DUTCH },
997 { "no", KBD_NORWEGIAN },
998 { "pl+dvp", KBD_POLISH_PROGRAMMERS },
999 { "pt", KBD_PORTUGUESE_BRAZILIAN_ABNT },
1000 { "ro", KBD_ROMANIAN },
1001 { "ru", KBD_RUSSIAN },
1002 { "hr", KBD_CROATIAN },
1003 { "sk", KBD_SLOVAK },
1004 { "al", KBD_ALBANIAN },
1005 { "se", KBD_SWEDISH },
1006 { "tr", KBD_TURKISH_Q },
1007 { "pk+urd-nla", KBD_URDU },
1008 { "ua", KBD_UKRAINIAN },
1009 { "by", KBD_BELARUSIAN },
1010 { "si", KBD_SLOVENIAN },
1011 { "ee", KBD_ESTONIAN },
1012 { "lv", KBD_LATVIAN },
1013 { "lt+ibm", KBD_LITHUANIAN_IBM },
1014 { "vn", KBD_VIETNAMESE },
1015 { "am+eastern", KBD_ARMENIAN_EASTERN },
1016 { "mk", KBD_FYRO_MACEDONIAN },
1017 { "ge", KBD_GEORGIAN },
1018 { "fo", KBD_FAEROESE },
1019 { "no+smi", KBD_NORWEGIAN_WITH_SAMI },
1020 { "kz", KBD_KAZAKH },
1021 { "gb+extd", KBD_UNITED_KINGDOM_EXTENDED },
1022 { "sy+syc", KBD_SYRIAC },
1023 { "af+ps-olpc", KBD_PASHTO },
1024 { "m17n:dv:phonetic", KBD_DIVEHI_PHONETIC },
1025 { "nz+mao", KBD_MAORI },
1026 { "ch", KBD_SWISS_GERMAN },
1027 { "gb", KBD_UNITED_KINGDOM },
1028 { "latam", KBD_LATIN_AMERICAN },
1029 { "be", KBD_BELGIAN_FRENCH },
1030 { "pt", KBD_PORTUGUESE },
1031 { "ca+fr-legacy", KBD_CANADIAN_FRENCH_LEGACY },
1032 { "ca", KBD_CANADIAN_FRENCH },
1033 { "ba", KBD_BOSNIAN },
1034 { "cz+qwerty", KBD_CZECH_QWERTY },
1035 { "de+T3", KBD_GERMAN_IBM },
1036 { "us+dvorak", KBD_UNITED_STATES_DVORAK },
1037 { "it+ibm", KBD_ITALIAN_142 },
1038 { "pl+qwertz", KBD_POLISH_214 },
1039 { "pt", KBD_PORTUGUESE_BRAZILIAN_ABNT2 },
1040 { "sk+qwerty", KBD_SLOVAK_QWERTY },
1041 { "th+pat", KBD_THAI_PATTACHOTE },
1042 { "tr+f", KBD_TURKISH_F },
1043 { "lt", KBD_LITHUANIAN },
1044 { "am+western", KBD_ARMENIAN_WESTERN },
1045 { "be+iso-alternate", KBD_BELGIAN_COMMA },
1046 { "gb+gla", KBD_GAELIC },
1047 { "us+intl", KBD_UNITED_STATES_INTERNATIONAL },
1048 { "us+dvorak-l", KBD_UNITED_STATES_DVORAK_FOR_LEFT_HAND },
1049 { "us+dvorak-r", KBD_UNITED_STATES_DVORAK_FOR_RIGHT_HAND },
1050 { "us+dvp", KBD_UNITED_STATES_DVORAK_PROGRAMMER },
1051 { "gr+polytonic", KBD_GREEK_POLYTONIC },
1052 { "fr+bepo", KBD_FRENCH_BEPO },
1053 { "de+neo", KBD_GERMAN_NEO },
1054
1055 /* These need to be determined yet. */
1056
1057 { "", KBD_CHINESE_TRADITIONAL_US },
1058 { "", KBD_THAI_KEDMANEE },
1059 { "", KBD_FARSI },
1060 { "", KBD_AZERI_LATIN },
1061 { "", KBD_DEVANAGARI_INSCRIPT },
1062 { "", KBD_MALTESE_47_KEY },
1063 { "", KBD_KYRGYZ_CYRILLIC },
1064 { "", KBD_TATAR },
1065 { "", KBD_BENGALI },
1066 { "", KBD_PUNJABI },
1067 { "", KBD_GUJARATI },
1068 { "", KBD_TAMIL },
1069 { "", KBD_TELUGU },
1070 { "", KBD_KANNADA },
1071 { "", KBD_MALAYALAM },
1072 { "", KBD_MARATHI },
1073 { "", KBD_MONGOLIAN_CYRILLIC },
1074 { "", KBD_NEPALI },
1075 { "", KBD_LUXEMBOURGISH },
1076 { "", KBD_CHINESE_SIMPLIFIED_US },
1077 { "", KBD_BELGIAN_PERIOD },
1078 { "", KBD_SERBIAN_LATIN },
1079 { "", KBD_AZERI_CYRILLIC },
1080 { "", KBD_SWEDISH_WITH_SAMI },
1081 { "", KBD_UZBEK_CYRILLIC },
1082 { "", KBD_INUKTITUT_LATIN },
1083 { "", KBD_SERBIAN_CYRILLIC },
1084 { "", KBD_SWISS_FRENCH },
1085 { "", KBD_IRISH },
1086 { "", KBD_BOSNIAN_CYRILLIC },
1087 { "", KBD_ARABIC_102 },
1088 { "", KBD_BULGARIAN_LATIN },
1089 { "", KBD_GREEK_220 },
1090 { "", KBD_SPANISH_VARIATION },
1091 { "", KBD_HUNGARIAN_101_KEY },
1092 { "", KBD_RUSSIAN_TYPEWRITER },
1093 { "", KBD_LATVIAN_QWERTY },
1094 { "", KBD_HINDI_TRADITIONAL },
1095 { "", KBD_MALTESE_48_KEY },
1096 { "", KBD_SAMI_EXTENDED_NORWAY },
1097 { "", KBD_BENGALI_INSCRIPT },
1098 { "", KBD_SYRIAC_PHONETIC },
1099 { "", KBD_DIVEHI_TYPEWRITER },
1100 { "", KBD_FINNISH_WITH_SAMI },
1101 { "", KBD_CANADIAN_MULTILINGUAL_STANDARD },
1102 { "", KBD_ARABIC_102_AZERTY },
1103 { "", KBD_CZECH_PROGRAMMERS },
1104 { "", KBD_GREEK_319 },
1105 { "", KBD_THAI_KEDMANEE_NON_SHIFTLOCK },
1106 { "", KBD_SAMI_EXTENDED_FINLAND_SWEDEN },
1107 { "", KBD_GREEK_220_LATIN },
1108 { "", KBD_THAI_PATTACHOTE_NON_SHIFTLOCK },
1109 { "", KBD_GREEK_319_LATIN },
1110 { "", KBD_GREEK_LATIN },
1111 { "", KBD_US_ENGLISH_TABLE_FOR_IBM_ARABIC_238_L }
1112
1113 /*
1114 These are available in FreeRDP 3:
1115 { "", KBD_ROMANIAN_STANDARD },
1116 { "", KBD_GEORGIAN_QUERTY },
1117 { "", KBD_KHMER },
1118 { "", KBD_RUSSIAN_PHONETIC },
1119 { "", KBD_BANGLA },
1120 { "", KBD_BULGARIAN_PHONETIC },
1121 { "", KBD_PERSIAN },
1122 */
1123 };
1124
1125 static void
1126 frdp_session_set_current_keyboard_layout (FrdpSession *self) {
1127 GSettingsSchemaSource *source;
1128 FrdpSessionPrivate *priv = self->priv;
1129 GSettingsSchema *schema;
1130 rdpSettings *settings;
1131 const gchar *source_type = NULL, *layout = NULL;
1132 GSettings *gsettings;
1133 GVariant *sources;
1134 gboolean keyboard_layout_set = FALSE;
1135 guint i;
1136
1137 settings = priv->freerdp_session->context->settings;
1138
1139 source = g_settings_schema_source_get_default ();
1140 if (source != NULL) {
1141 schema = g_settings_schema_source_lookup (source, "org.gnome.desktop.input-sources", TRUE);
1142 if (schema != NULL) {
1143 gsettings = g_settings_new (g_settings_schema_get_id (schema));
1144 sources = g_settings_get_value (gsettings, "mru-sources");
1145
1146 if (g_variant_n_children (sources) >= 1)
1147 g_variant_get_child (sources, 0, "(&s&s)", &source_type, &layout);
1148
1149 if (layout != NULL) {
1150 for (i = 0; i < G_N_ELEMENTS (keyboard_layouts); i++) {
1151 if (g_strcmp0 (layout, keyboard_layouts[i].local_layout) == 0) {
1152 #ifdef HAVE_FREERDP_3_11_0
1153 freerdp_settings_set_uint32 (settings, FreeRDP_KeyboardLayout, keyboard_layouts[i].freerdp_layout);
1154 #else
1155 settings->KeyboardLayout = freerdp_keyboard_init (keyboard_layouts[i].freerdp_layout);
1156 #endif
1157 keyboard_layout_set = TRUE;
1158 break;
1159 }
1160 }
1161 }
1162
1163 g_variant_unref (sources);
1164 g_object_unref (gsettings);
1165 g_settings_schema_unref (schema);
1166 }
1167 }
1168
1169 #ifndef HAVE_FREERDP_3_11_0
1170 if (!keyboard_layout_set)
1171 settings->KeyboardLayout = freerdp_keyboard_init (0);
1172 #endif
1173 }
1174
1175 static gboolean
1176 frdp_session_init_freerdp (FrdpSession *self)
1177 {
1178 CONST_QUALIFIER gchar *collections[] = { "disp" };
1179 FrdpSessionPrivate *priv = self->priv;
1180 rdpSettings *settings;
1181 gchar *build_options;
1182 int count = 1;
1183
1184 /* Setup FreeRDP session */
1185 priv->freerdp_session = freerdp_new ();
1186
1187 if (priv->freerdp_session == NULL)
1188 return FALSE;
1189
1190 priv->freerdp_session->PreConnect = frdp_pre_connect;
1191 priv->freerdp_session->PostConnect = frdp_post_connect;
1192 priv->freerdp_session->PostDisconnect = frdp_post_disconnect;
1193 priv->freerdp_session->AuthenticateEx = frdp_authenticate_ex;
1194 priv->freerdp_session->VerifyCertificateEx = frdp_certificate_verify_ex;
1195 priv->freerdp_session->VerifyChangedCertificateEx = frdp_changed_certificate_verify_ex;
1196 #ifdef HAVE_FREERDP3
1197 priv->freerdp_session->LoadChannels = frdp_load_channels;
1198 #endif
1199
1200 priv->freerdp_session->ContextSize = sizeof (frdpContext);
1201
1202 if (!freerdp_context_new (priv->freerdp_session))
1203 return FALSE;
1204
1205 ((frdpContext *) priv->freerdp_session->context)->self = self;
1206
1207 settings = priv->freerdp_session->context->settings;
1208
1209 freerdp_settings_set_string (settings, FreeRDP_ServerHostname, priv->hostname);
1210 freerdp_settings_set_uint32 (settings, FreeRDP_ServerPort, priv->port);
1211 freerdp_settings_set_string (settings, FreeRDP_Username, priv->username);
1212 freerdp_settings_set_string (settings, FreeRDP_Password, priv->password);
1213 freerdp_settings_set_string (settings, FreeRDP_Domain, priv->domain);
1214
1215 freerdp_settings_set_bool (settings, FreeRDP_AllowFontSmoothing, TRUE);
1216 freerdp_settings_set_bool (settings, FreeRDP_AllowUnanouncedOrdersFromServer, TRUE);
1217
1218 /* Security settings */
1219 freerdp_settings_set_bool (settings, FreeRDP_RdpSecurity, TRUE);
1220 freerdp_settings_set_bool (settings, FreeRDP_TlsSecurity, TRUE);
1221 freerdp_settings_set_bool (settings, FreeRDP_NlaSecurity, TRUE);
1222 freerdp_settings_set_uint32 (settings, FreeRDP_EncryptionMethods, ENCRYPTION_METHOD_40BIT | ENCRYPTION_METHOD_128BIT | ENCRYPTION_METHOD_FIPS);
1223 freerdp_settings_set_uint32 (settings, FreeRDP_EncryptionLevel, ENCRYPTION_LEVEL_CLIENT_COMPATIBLE);
1224 freerdp_settings_set_bool (settings, FreeRDP_UseRdpSecurityLayer, FALSE);
1225
1226 freerdp_settings_set_bool (settings, FreeRDP_NegotiateSecurityLayer, TRUE);
1227
1228 freerdp_settings_set_bool (settings, FreeRDP_DesktopResize, TRUE);
1229 freerdp_settings_set_bool (settings, FreeRDP_DynamicResolutionUpdate, TRUE);
1230 freerdp_settings_set_bool (settings, FreeRDP_SupportDisplayControl, TRUE);
1231 freerdp_settings_set_bool (settings, FreeRDP_RemoteFxCodec, TRUE);
1232 freerdp_settings_set_uint32 (settings, FreeRDP_ColorDepth, 32);
1233 freerdp_settings_set_bool (settings, FreeRDP_RedirectClipboard, TRUE);
1234 freerdp_settings_set_bool (settings, FreeRDP_SupportGraphicsPipeline, TRUE);
1235
1236 freerdp_client_add_dynamic_channel (settings, count, collections);
1237
1238 build_options = g_ascii_strup (freerdp_get_build_config (), -1);
1239 if (g_strrstr (build_options, "WITH_GFX_H264=ON") != NULL) {
1240 freerdp_settings_set_bool (settings, FreeRDP_GfxH264, TRUE);
1241 freerdp_settings_set_bool (settings, FreeRDP_GfxAVC444, TRUE);
1242 } else {
1243 freerdp_settings_set_bool (settings, FreeRDP_GfxH264, FALSE);
1244 freerdp_settings_set_bool (settings, FreeRDP_GfxAVC444, FALSE);
1245 }
1246 g_free (build_options);
1247
1248 frdp_session_set_current_keyboard_layout (self);
1249
1250 freerdp_register_addin_provider(freerdp_channels_load_static_addin_entry, 0);
1251
1252 return TRUE;
1253 }
1254
1255 static void
1256 frdp_session_connect_thread (GTask *task,
1257 gpointer source_object,
1258 gpointer task_data,
1259 GCancellable *cancellable)
1260 {
1261 FrdpSession *self = (FrdpSession*) source_object;
1262 guint32 error_code;
1263
1264 if (!frdp_session_init_freerdp (self)) {
1265 if (self->priv->freerdp_session != NULL &&
1266 self->priv->freerdp_session->context != NULL) {
1267 error_code = freerdp_get_last_error (self->priv->freerdp_session->context);
1268 g_signal_emit (self,
1269 signals[RDP_ERROR], 0,
1270 freerdp_get_last_error_string (error_code));
1271 g_warning ("Failed to initialize RDP with error '%s'",
1272 freerdp_get_last_error_string (error_code));
1273 } else {
1274 g_signal_emit (self,
1275 signals[RDP_ERROR], 0,
1276 "Failed to initialize RDP!");
1277 }
1278
1279 g_idle_add ((GSourceFunc) idle_close, self);
1280 g_task_return_boolean (task, FALSE);
1281
1282 return;
1283 }
1284
1285 self->priv->is_connected = freerdp_connect (self->priv->freerdp_session);
1286 if (!self->priv->is_connected) {
1287 error_code = freerdp_get_last_error (self->priv->freerdp_session->context);
1288 switch (error_code) {
1289 case FREERDP_ERROR_AUTHENTICATION_FAILED:
1290 case FREERDP_ERROR_CONNECT_FAILED:
1291 case FREERDP_ERROR_SERVER_DENIED_CONNECTION:
1292 case FREERDP_ERROR_CONNECT_NO_OR_MISSING_CREDENTIALS:
1293 case FREERDP_ERROR_CONNECT_LOGON_FAILURE:
1294 case STATUS_LOGON_FAILURE:
1295 case STATUS_PASSWORD_EXPIRED:
1296 case FREERDP_ERROR_CONNECT_ACCOUNT_EXPIRED:
1297 case FREERDP_ERROR_CONNECT_TRANSPORT_FAILED:
1298 case ERRCONNECT_CONNECT_TRANSPORT_FAILED:
1299 case FREERDP_ERROR_TLS_CONNECT_FAILED:
1300 case FREERDP_ERROR_DNS_NAME_NOT_FOUND:
1301 g_signal_emit (self,
1302 signals[RDP_AUTH_FAILURE], 0,
1303 freerdp_get_last_error_string (error_code));
1304
1305 g_warning ("Failed to connect RDP host with error '%s'",
1306 freerdp_get_last_error_string (error_code));
1307 break;
1308
1309 default:
1310 g_signal_emit (self,
1311 signals[RDP_ERROR], 0,
1312 freerdp_get_last_error_string (error_code));
1313
1314 g_warning ("Unexpected RDP error: '%s'",
1315 freerdp_get_last_error_string (error_code));
1316 break;
1317 }
1318
1319 g_idle_add ((GSourceFunc) idle_close, self);
1320 g_task_return_boolean (task, FALSE);
1321
1322 return;
1323 }
1324
1325 gtk_widget_realize (self->priv->display);
1326 create_cairo_surface (self);
1327 g_signal_connect (self->priv->display, "draw",
1328 G_CALLBACK (frdp_session_draw), self);
1329 g_signal_connect (self->priv->display, "configure-event",
1330 G_CALLBACK (frdp_session_configure_event), self);
1331 g_signal_connect (self->priv->display, "notify::resize-supported",
1332 G_CALLBACK (frdp_session_resize_supported_changed), self);
1333
1334 self->priv->update_id = g_idle_add ((GSourceFunc) update, self);
1335
1336 g_task_return_boolean (task, TRUE);
1337 }
1338
1339 static void
1340 frdp_session_get_property (GObject *object,
1341 guint property_id,
1342 GValue *value,
1343 GParamSpec *pspec)
1344 {
1345 FrdpSession *self = (FrdpSession*) object;
1346
1347 switch (property_id)
1348 {
1349 case PROP_HOSTNAME:
1350 g_value_set_string (value, self->priv->hostname);
1351 break;
1352 case PROP_PORT:
1353 g_value_set_uint (value, self->priv->port);
1354 break;
1355 case PROP_USERNAME:
1356 g_value_set_string (value, self->priv->username);
1357 break;
1358 case PROP_PASSWORD:
1359 g_value_set_string (value, self->priv->password);
1360 break;
1361 case PROP_DOMAIN:
1362 g_value_set_string (value, self->priv->domain);
1363 break;
1364 case PROP_DISPLAY:
1365 g_value_set_object (value, self->priv->display);
1366 break;
1367 case PROP_SCALING:
1368 g_value_set_boolean (value, self->priv->scaling);
1369 break;
1370 case PROP_MONITOR_LAYOUT_SUPPORTED:
1371 g_value_set_boolean (value, self->priv->monitor_layout_supported);
1372 break;
1373 default:
1374 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1375 break;
1376 }
1377 }
1378
1379 static void
1380 frdp_session_set_property (GObject *object,
1381 guint property_id,
1382 const GValue *value,
1383 GParamSpec *pspec)
1384 {
1385 FrdpSession *self = (FrdpSession*) object;
1386
1387 switch (property_id)
1388 {
1389 case PROP_HOSTNAME:
1390 g_free (self->priv->hostname);
1391 self->priv->hostname = g_value_dup_string (value);
1392 break;
1393 case PROP_PORT:
1394 self->priv->port = g_value_get_uint (value);
1395 break;
1396 case PROP_USERNAME:
1397 g_free (self->priv->username);
1398 self->priv->username = g_value_dup_string (value);
1399 break;
1400 case PROP_PASSWORD:
1401 g_free (self->priv->password);
1402 self->priv->password = g_value_dup_string (value);
1403 break;
1404 case PROP_DOMAIN:
1405 g_free (self->priv->domain);
1406 self->priv->domain = g_value_dup_string (value);
1407 break;
1408 case PROP_DISPLAY:
1409 self->priv->display = g_value_get_object (value);
1410 break;
1411 case PROP_SCALING:
1412 frdp_session_set_scaling (self, g_value_get_boolean (value));
1413 break;
1414 case PROP_MONITOR_LAYOUT_SUPPORTED:
1415 self->priv->monitor_layout_supported = g_value_get_boolean (value);
1416 g_object_notify (G_OBJECT (self), "monitor-layout-supported");
1417 break;
1418 default:
1419 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1420 break;
1421 }
1422 }
1423
1424 static void
1425 frdp_session_finalize (GObject *object)
1426 {
1427 FrdpSession *self = (FrdpSession*) object;
1428
1429 idle_close (self);
1430
1431 G_OBJECT_CLASS (frdp_session_parent_class)->finalize (object);
1432 }
1433
1434 static void
1435 frdp_session_class_init (FrdpSessionClass *klass)
1436 {
1437 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1438
1439 gobject_class->finalize = frdp_session_finalize;
1440 gobject_class->get_property = frdp_session_get_property;
1441 gobject_class->set_property = frdp_session_set_property;
1442
1443 g_object_class_install_property (gobject_class,
1444 PROP_HOSTNAME,
1445 g_param_spec_string ("hostname",
1446 "hostname",
1447 "hostname",
1448 NULL,
1449 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE));
1450
1451 g_object_class_install_property (gobject_class,
1452 PROP_PORT,
1453 g_param_spec_uint ("port",
1454 "port",
1455 "port",
1456 0, G_MAXUINT16, 3389,
1457 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE));
1458
1459 g_object_class_install_property (gobject_class,
1460 PROP_USERNAME,
1461 g_param_spec_string ("username",
1462 "username",
1463 "username",
1464 NULL,
1465 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE));
1466
1467 g_object_class_install_property (gobject_class,
1468 PROP_PASSWORD,
1469 g_param_spec_string ("password",
1470 "password",
1471 "password",
1472 NULL,
1473 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE));
1474
1475 g_object_class_install_property (gobject_class,
1476 PROP_DOMAIN,
1477 g_param_spec_string ("domain",
1478 "domain",
1479 "domain",
1480 NULL,
1481 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE));
1482
1483 g_object_class_install_property (gobject_class,
1484 PROP_DISPLAY,
1485 g_param_spec_object ("display",
1486 "display",
1487 "display",
1488 GTK_TYPE_WIDGET,
1489 G_PARAM_READWRITE));
1490
1491 g_object_class_install_property (gobject_class,
1492 PROP_SCALING,
1493 g_param_spec_boolean ("scaling",
1494 "scaling",
1495 "scaling",
1496 TRUE,
1497 G_PARAM_READWRITE));
1498
1499 g_object_class_install_property (gobject_class,
1500 PROP_MONITOR_LAYOUT_SUPPORTED,
1501 g_param_spec_boolean ("monitor-layout-supported",
1502 "monitor-layout-supported",
1503 "monitor-layout-supported",
1504 FALSE,
1505 G_PARAM_READWRITE));
1506
1507 signals[RDP_ERROR] = g_signal_new ("rdp-error",
1508 FRDP_TYPE_SESSION,
1509 G_SIGNAL_RUN_FIRST,
1510 0, NULL, NULL, NULL,
1511 G_TYPE_NONE, 1,
1512 G_TYPE_STRING);
1513
1514 signals[RDP_CONNECTED] = g_signal_new ("rdp-connected",
1515 FRDP_TYPE_SESSION,
1516 G_SIGNAL_RUN_FIRST,
1517 0, NULL, NULL, NULL,
1518 G_TYPE_NONE, 0);
1519
1520 signals[RDP_DISCONNECTED] = g_signal_new ("rdp-disconnected",
1521 FRDP_TYPE_SESSION,
1522 G_SIGNAL_RUN_FIRST,
1523 0, NULL, NULL, NULL,
1524 G_TYPE_NONE, 0);
1525
1526 signals[RDP_AUTH_FAILURE] = g_signal_new ("rdp-auth-failure",
1527 FRDP_TYPE_SESSION,
1528 G_SIGNAL_RUN_FIRST,
1529 0, NULL, NULL, NULL,
1530 G_TYPE_NONE, 1,
1531 G_TYPE_STRING);
1532 }
1533
1534 static void
1535 frdp_session_init (FrdpSession *self)
1536 {
1537 self->priv = frdp_session_get_instance_private (self);
1538
1539 g_mutex_init (&self->priv->area_draw_mutex);
1540 self->priv->area_draw_queue = g_queue_new ();
1541
1542 self->priv->is_connected = FALSE;
1543 }
1544
1545 FrdpSession*
1546 frdp_session_new (FrdpDisplay *display)
1547 {
1548 gtk_widget_show (GTK_WIDGET (display));
1549
1550 return g_object_new (FRDP_TYPE_SESSION,
1551 "display", display,
1552 NULL);
1553 }
1554
1555 void
1556 frdp_session_connect (FrdpSession *self,
1557 const gchar *hostname,
1558 guint port,
1559 GCancellable *cancellable,
1560 GAsyncReadyCallback callback,
1561 gpointer user_data)
1562 {
1563 GTask *task;
1564
1565 self->priv->hostname = g_strdup (hostname);
1566 self->priv->port = port;
1567
1568 task = g_task_new (self, cancellable, callback, user_data);
1569 /* Turn off the asynchronous connection via GTask thread as the FreeRDP
1570 * process then runs in the new thread which makes it hard to cooperate
1571 * with the original thread running UI leading to random race conditions.
1572 * Turn it on again if there will be an async support for connection
1573 * added to FreeRDP.
1574 * The disadvantage is that the application freezes during connection
1575 * for some time.
1576
1577 g_task_run_in_thread (task, frdp_session_connect_thread);
1578
1579 */
1580
1581 frdp_session_connect_thread (task, self, user_data, cancellable);
1582
1583 g_object_unref (task);
1584 }
1585
1586 gboolean
1587 frdp_session_connect_finish (FrdpSession *self,
1588 GAsyncResult *result,
1589 GError **error)
1590 {
1591 g_return_val_if_fail (g_task_is_valid (result, self), FALSE);
1592
1593 return g_task_propagate_boolean (G_TASK (result), error);
1594 }
1595
1596 gboolean
1597 frdp_session_is_open (FrdpSession *self)
1598 {
1599 return self->priv->is_connected;
1600 }
1601
1602 void
1603 frdp_session_close (FrdpSession *self)
1604 {
1605 idle_close (self);
1606
1607 g_debug ("Closing RDP session");
1608 }
1609
1610 void
1611 frdp_session_mouse_event (FrdpSession *self,
1612 FrdpMouseEvent event,
1613 guint16 x,
1614 guint16 y)
1615 {
1616 FrdpSessionPrivate *priv = self->priv;
1617 rdpInput *input;
1618 guint16 flags = 0;
1619 guint16 xflags = 0;
1620
1621 g_return_if_fail (priv->freerdp_session != NULL);
1622
1623 if (event & FRDP_MOUSE_EVENT_MOVE)
1624 flags |= PTR_FLAGS_MOVE;
1625 if (event & FRDP_MOUSE_EVENT_DOWN)
1626 flags |= PTR_FLAGS_DOWN;
1627 if (event & FRDP_MOUSE_EVENT_WHEEL) {
1628 flags |= PTR_FLAGS_WHEEL;
1629 if (event & FRDP_MOUSE_EVENT_WHEEL_NEGATIVE)
1630 flags |= PTR_FLAGS_WHEEL_NEGATIVE | 0x0088;
1631 else
1632 flags |= 0x0078;
1633 }
1634 if (event & FRDP_MOUSE_EVENT_HWHEEL) {
1635 flags |= PTR_FLAGS_HWHEEL;
1636 if (event & FRDP_MOUSE_EVENT_WHEEL_NEGATIVE)
1637 flags |= PTR_FLAGS_WHEEL_NEGATIVE | 0x0088;
1638 else
1639 flags |= 0x0078;
1640 }
1641
1642 if (event & FRDP_MOUSE_EVENT_BUTTON1)
1643 flags |= PTR_FLAGS_BUTTON1;
1644 if (event & FRDP_MOUSE_EVENT_BUTTON2)
1645 flags |= PTR_FLAGS_BUTTON2;
1646 if (event & FRDP_MOUSE_EVENT_BUTTON3)
1647 flags |= PTR_FLAGS_BUTTON3;
1648 if (event & FRDP_MOUSE_EVENT_BUTTON4)
1649 xflags |= PTR_XFLAGS_BUTTON1;
1650 if (event & FRDP_MOUSE_EVENT_BUTTON5)
1651 xflags |= PTR_XFLAGS_BUTTON2;
1652
1653 input = priv->freerdp_session->context->input;
1654
1655 if (priv->scaling) {
1656 x = (x - priv->offset_x) / priv->scale;
1657 y = (y - priv->offset_y) / priv->scale;
1658 }
1659
1660 x = x < 0.0 ? 0.0 : x;
1661 y = y < 0.0 ? 0.0 : y;
1662 if (xflags != 0) {
1663 if (event & FRDP_MOUSE_EVENT_DOWN)
1664 xflags |= PTR_XFLAGS_DOWN;
1665 freerdp_input_send_extended_mouse_event(input, xflags, x, y);
1666 } else if (flags != 0) {
1667 freerdp_input_send_mouse_event (input, flags, x, y);
1668 }
1669 }
1670
1671 void
1672 frdp_session_mouse_smooth_scroll_event (FrdpSession *self,
1673 guint16 x,
1674 guint16 y,
1675 double delta_x,
1676 double delta_y)
1677 {
1678 FrdpSessionPrivate *priv = self->priv;
1679 rdpInput *input;
1680 guint16 flags = 0, value;
1681
1682 g_return_if_fail (priv->freerdp_session != NULL);
1683
1684 if (fabs (delta_y) >= fabs (delta_x)) {
1685 flags |= PTR_FLAGS_WHEEL;
1686 value = (guint16) round (fabs (delta_y) * 0x78);
1687 if (value > 0) {
1688 /* Reversing direction here to reflect the behaviour on local side. */
1689 if (delta_y < 0.0) {
1690 if (value > 255)
1691 value = 255;
1692 flags |= value & WheelRotationMask;
1693 } else {
1694 if (value > 256)
1695 value = 256;
1696 flags |= PTR_FLAGS_WHEEL_NEGATIVE;
1697 flags |= (~value + 1) & WheelRotationMask;
1698 }
1699 }
1700 } else {
1701 flags |= PTR_FLAGS_HWHEEL;
1702 value = (guint16) round (fabs (delta_x) * 0x78);
1703 if (value > 0) {
1704 if (delta_x < 0.0) {
1705 if (value > 256)
1706 value = 256;
1707 flags |= PTR_FLAGS_WHEEL_NEGATIVE;
1708 flags |= (~value + 1) & WheelRotationMask;
1709 } else {
1710 if (value > 255)
1711 value = 255;
1712 flags |= value & WheelRotationMask;
1713 }
1714 }
1715 }
1716
1717 input = priv->freerdp_session->context->input;
1718
1719 freerdp_input_send_mouse_event (input, flags, 0, 0);
1720 }
1721
1722 void
1723 frdp_session_mouse_pointer (FrdpSession *self,
1724 gboolean enter)
1725 {
1726 FrdpSessionPrivate *priv = self->priv;
1727
1728 priv->show_cursor = enter;
1729 frdp_session_update_mouse_pointer (self);
1730 }
1731
1732 void
1733 frdp_session_send_key (FrdpSession *self,
1734 GdkEventKey *key)
1735 {
1736 rdpInput *input = self->priv->freerdp_session->context->input;
1737
1738 #ifdef HAVE_FREERDP_3_11_0
1739 guint32 virtual_code, virtual_scan_code;
1740
1741 virtual_code = GetVirtualKeyCodeFromKeycode (key->hardware_keycode, WINPR_KEYCODE_TYPE_XKB);
1742 virtual_scan_code = GetVirtualScanCodeFromVirtualKeyCode (virtual_code, WINPR_KBD_TYPE_IBM_ENHANCED);
1743
1744 if (virtual_scan_code != RDP_SCANCODE_UNKNOWN &&
1745 (frdp_display_is_keyboard_grabbed (FRDP_DISPLAY (self->priv->display)) ||
1746 (virtual_scan_code != RDP_SCANCODE_LWIN && virtual_scan_code != RDP_SCANCODE_RWIN)))
1747 freerdp_input_send_keyboard_event_ex (input, key->type == GDK_KEY_PRESS, FALSE, virtual_scan_code);
1748 #else
1749 DWORD scancode = 0;
1750 guint8 keycode;
1751 guint16 flags;
1752 gboolean extended = FALSE;
1753
1754 scancode =
1755 freerdp_keyboard_get_rdp_scancode_from_x11_keycode (key->hardware_keycode);
1756
1757 keycode = scancode & 0xFF;
1758 extended = scancode & 0x100;
1759
1760 flags = extended ? KBD_FLAGS_EXTENDED : 0;
1761 flags |= key->type == GDK_KEY_PRESS ? KBD_FLAGS_DOWN : KBD_FLAGS_RELEASE;
1762
1763 if (keycode)
1764 input->KeyboardEvent (input, flags, keycode);
1765 #endif
1766 }
1767
1768 GdkPixbuf *
1769 frdp_session_get_pixbuf (FrdpSession *self)
1770 {
1771 guint width, height;
1772
1773 width = gtk_widget_get_allocated_width (self->priv->display);
1774 height = gtk_widget_get_allocated_height (self->priv->display);
1775
1776 return gdk_pixbuf_get_from_surface (self->priv->surface,
1777 0, 0,
1778 width, height);
1779 }