2
* FreeRDP: A Remote Desktop Protocol Implementation
5
* Copyright 2013 Marc-Andre Moreau <marcandre.moreau@gmail.com>
7
* Licensed under the Apache License, Version 2.0 (the "License");
8
* you may not use this file except in compliance with the License.
9
* You may obtain a copy of the License at
11
* http://www.apache.org/licenses/LICENSE-2.0
13
* Unless required by applicable law or agreed to in writing, software
14
* distributed under the License is distributed on an "AS IS" BASIS,
15
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
* See the License for the specific language governing permissions and
17
* limitations under the License.
25
#include <X11/Xutil.h>
28
#include <X11/Xcursor/Xcursor.h>
32
#include <X11/extensions/Xinerama.h>
36
#include <X11/extensions/XInput2.h>
40
#include <X11/extensions/Xrender.h>
52
#include <sys/types.h>
53
#include <sys/select.h>
55
#include <freerdp/freerdp.h>
56
#include <freerdp/constants.h>
57
#include <freerdp/codec/nsc.h>
58
#include <freerdp/codec/rfx.h>
59
#include <freerdp/codec/color.h>
60
#include <freerdp/codec/bitmap.h>
62
#include <freerdp/utils/event.h>
63
#include <freerdp/utils/signal.h>
64
#include <freerdp/utils/passphrase.h>
65
#include <freerdp/client/cliprdr.h>
66
#include <freerdp/client/channels.h>
68
#include <freerdp/rail.h>
70
#include <freerdp/client/file.h>
71
#include <freerdp/client/cmdline.h>
73
#include <winpr/crt.h>
74
#include <winpr/synch.h>
75
#include <winpr/file.h>
82
#include "xf_cliprdr.h"
83
#include "xf_monitor.h"
84
#include "xf_graphics.h"
85
#include "xf_keyboard.h"
86
#include "xf_channels.h"
90
static long xv_port = 0;
91
static const size_t password_size = 512;
93
void xf_draw_screen_scaled(xfContext* xfc)
97
Picture windowPicture;
98
Picture primaryPicture;
99
XRenderPictureAttributes pa;
100
XRenderPictFormat* picFormat;
102
picFormat = XRenderFindStandardFormat(xfc->display, PictStandardRGB24);
103
pa.subwindow_mode = IncludeInferiors;
104
primaryPicture = XRenderCreatePicture(xfc->display, xfc->primary, picFormat, CPSubwindowMode, &pa);
105
windowPicture = XRenderCreatePicture(xfc->display, xfc->window->handle, picFormat, CPSubwindowMode, &pa);
107
transform.matrix[0][0] = XDoubleToFixed(1);
108
transform.matrix[0][1] = XDoubleToFixed(0);
109
transform.matrix[0][2] = XDoubleToFixed(0);
111
transform.matrix[1][0] = XDoubleToFixed(0);
112
transform.matrix[1][1] = XDoubleToFixed(1);
113
transform.matrix[1][2] = XDoubleToFixed(0);
115
transform.matrix[2][0] = XDoubleToFixed(0);
116
transform.matrix[2][1] = XDoubleToFixed(0);
117
transform.matrix[2][2] = XDoubleToFixed(xfc->scale);
119
XRenderSetPictureTransform(xfc->display, primaryPicture, &transform);
120
XRenderComposite(xfc->display, PictOpSrc, primaryPicture, 0, windowPicture, 0, 0, 0, 0, 0, 0, xfc->currentWidth, xfc->currentHeight);
124
void xf_sw_begin_paint(rdpContext* context)
126
rdpGdi* gdi = context->gdi;
127
gdi->primary->hdc->hwnd->invalid->null = 1;
128
gdi->primary->hdc->hwnd->ninvalid = 0;
131
void xf_sw_end_paint(rdpContext* context)
136
xfContext* xfc = (xfContext*) context;
140
if (!xfc->remote_app)
142
if (!xfc->complex_regions)
144
if (gdi->primary->hdc->hwnd->invalid->null)
147
x = gdi->primary->hdc->hwnd->invalid->x;
148
y = gdi->primary->hdc->hwnd->invalid->y;
149
w = gdi->primary->hdc->hwnd->invalid->w;
150
h = gdi->primary->hdc->hwnd->invalid->h;
152
xf_lock_x11(xfc, FALSE);
154
XPutImage(xfc->display, xfc->primary, xfc->gc, xfc->image, x, y, x, y, w, h);
156
if (xfc->scale != 1.0)
158
xf_draw_screen_scaled(xfc);
162
XCopyArea(xfc->display, xfc->primary, xfc->window->handle, xfc->gc, x, y, w, h, x, y);
165
xf_unlock_x11(xfc, FALSE);
173
if (gdi->primary->hdc->hwnd->ninvalid < 1)
176
ninvalid = gdi->primary->hdc->hwnd->ninvalid;
177
cinvalid = gdi->primary->hdc->hwnd->cinvalid;
179
xf_lock_x11(xfc, FALSE);
181
for (i = 0; i < ninvalid; i++)
188
XPutImage(xfc->display, xfc->primary, xfc->gc, xfc->image, x, y, x, y, w, h);
190
if (xfc->scale != 1.0)
192
xf_draw_screen_scaled(xfc);
196
XCopyArea(xfc->display, xfc->primary, xfc->window->handle, xfc->gc, x, y, w, h, x, y);
200
XFlush(xfc->display);
202
xf_unlock_x11(xfc, FALSE);
207
if (gdi->primary->hdc->hwnd->invalid->null)
210
x = gdi->primary->hdc->hwnd->invalid->x;
211
y = gdi->primary->hdc->hwnd->invalid->y;
212
w = gdi->primary->hdc->hwnd->invalid->w;
213
h = gdi->primary->hdc->hwnd->invalid->h;
215
xf_lock_x11(xfc, FALSE);
217
xf_rail_paint(xfc, context->rail, x, y, x + w - 1, y + h - 1);
219
xf_unlock_x11(xfc, FALSE);
223
void xf_sw_desktop_resize(rdpContext* context)
225
rdpSettings* settings;
226
xfContext* xfc = (xfContext*) context;
228
settings = xfc->instance->settings;
230
xf_lock_x11(xfc, TRUE);
232
if (!xfc->fullscreen)
234
rdpGdi* gdi = context->gdi;
235
gdi_resize(gdi, xfc->width, xfc->height);
239
xfc->image->data = NULL;
240
XDestroyImage(xfc->image);
241
xfc->image = XCreateImage(xfc->display, xfc->visual, xfc->depth, ZPixmap, 0,
242
(char*) gdi->primary_buffer, gdi->width, gdi->height, xfc->scanline_pad, 0);
246
xf_unlock_x11(xfc, TRUE);
249
void xf_hw_begin_paint(rdpContext* context)
251
xfContext* xfc = (xfContext*) context;
253
xfc->hdc->hwnd->invalid->null = 1;
254
xfc->hdc->hwnd->ninvalid = 0;
257
void xf_hw_end_paint(rdpContext* context)
261
xfContext* xfc = (xfContext*) context;
263
if (!xfc->remote_app)
265
if (!xfc->complex_regions)
267
if (xfc->hdc->hwnd->invalid->null)
270
x = xfc->hdc->hwnd->invalid->x;
271
y = xfc->hdc->hwnd->invalid->y;
272
w = xfc->hdc->hwnd->invalid->w;
273
h = xfc->hdc->hwnd->invalid->h;
275
xf_lock_x11(xfc, FALSE);
277
if (xfc->scale != 1.0)
279
xf_draw_screen_scaled(xfc);
283
XCopyArea(xfc->display, xfc->primary, xfc->drawable, xfc->gc, x, y, w, h, x, y);
286
xf_unlock_x11(xfc, FALSE);
294
if (xfc->hdc->hwnd->ninvalid < 1)
297
ninvalid = xfc->hdc->hwnd->ninvalid;
298
cinvalid = xfc->hdc->hwnd->cinvalid;
300
xf_lock_x11(xfc, FALSE);
302
for (i = 0; i < ninvalid; i++)
309
if (xfc->scale != 1.0)
311
xf_draw_screen_scaled(xfc);
315
XCopyArea(xfc->display, xfc->primary, xfc->drawable, xfc->gc, x, y, w, h, x, y);
319
XFlush(xfc->display);
321
xf_unlock_x11(xfc, FALSE);
326
if (xfc->hdc->hwnd->invalid->null)
329
x = xfc->hdc->hwnd->invalid->x;
330
y = xfc->hdc->hwnd->invalid->y;
331
w = xfc->hdc->hwnd->invalid->w;
332
h = xfc->hdc->hwnd->invalid->h;
334
xf_lock_x11(xfc, FALSE);
336
xf_rail_paint(xfc, context->rail, x, y, x + w - 1, y + h - 1);
338
xf_unlock_x11(xfc, FALSE);
342
void xf_hw_desktop_resize(rdpContext* context)
345
rdpSettings* settings;
346
xfContext* xfc = (xfContext*) context;
348
settings = xfc->instance->settings;
350
xf_lock_x11(xfc, TRUE);
352
if (!xfc->fullscreen)
354
xfc->width = settings->DesktopWidth;
355
xfc->height = settings->DesktopHeight;
358
xf_ResizeDesktopWindow(xfc, xfc->window, settings->DesktopWidth, settings->DesktopHeight);
362
same = (xfc->primary == xfc->drawing) ? TRUE : FALSE;
364
XFreePixmap(xfc->display, xfc->primary);
366
xfc->primary = XCreatePixmap(xfc->display, xfc->drawable,
367
xfc->width, xfc->height, xfc->depth);
370
xfc->drawing = xfc->primary;
375
XSetFunction(xfc->display, xfc->gc, GXcopy);
376
XSetFillStyle(xfc->display, xfc->gc, FillSolid);
377
XSetForeground(xfc->display, xfc->gc, 0);
378
XFillRectangle(xfc->display, xfc->drawable, xfc->gc, 0, 0, xfc->width, xfc->height);
381
xf_unlock_x11(xfc, TRUE);
384
BOOL xf_get_fds(freerdp* instance, void** rfds, int* rcount, void** wfds, int* wcount)
386
xfContext* xfc = (xfContext*) instance->context;
388
rfds[*rcount] = (void*)(long)(xfc->xfds);
394
BOOL xf_process_x_events(freerdp* instance)
399
xfContext* xfc = (xfContext*) instance->context;
402
pending_status = TRUE;
404
while (pending_status)
406
xf_lock_x11(xfc, FALSE);
408
pending_status = XPending(xfc->display);
410
xf_unlock_x11(xfc, FALSE);
414
ZeroMemory(&xevent, sizeof(xevent));
416
XNextEvent(xfc->display, &xevent);
417
status = xf_event_process(instance, &xevent);
427
void xf_create_window(xfContext* xfc)
433
ZeroMemory(&xevent, sizeof(xevent));
436
height = xfc->height;
438
if (!xfc->remote_app)
440
xfc->attribs.background_pixel = BlackPixelOfScreen(xfc->screen);
441
xfc->attribs.border_pixel = WhitePixelOfScreen(xfc->screen);
442
xfc->attribs.backing_store = xfc->primary ? NotUseful : Always;
443
xfc->attribs.override_redirect = xfc->grab_keyboard ? xfc->fullscreen : False;
444
xfc->attribs.colormap = xfc->colormap;
445
xfc->attribs.bit_gravity = NorthWestGravity;
446
xfc->attribs.win_gravity = NorthWestGravity;
448
if (xfc->instance->settings->WindowTitle)
450
windowTitle = _strdup(xfc->instance->settings->WindowTitle);
452
else if (xfc->instance->settings->ServerPort == 3389)
454
windowTitle = malloc(1 + sizeof("FreeRDP: ") + strlen(xfc->instance->settings->ServerHostname));
455
sprintf(windowTitle, "FreeRDP: %s", xfc->instance->settings->ServerHostname);
459
windowTitle = malloc(1 + sizeof("FreeRDP: ") + strlen(xfc->instance->settings->ServerHostname) + sizeof(":00000"));
460
sprintf(windowTitle, "FreeRDP: %s:%i", xfc->instance->settings->ServerHostname, xfc->instance->settings->ServerPort);
463
xfc->window = xf_CreateDesktopWindow(xfc, windowTitle, width, height, xfc->settings->Decorations);
467
xf_SetWindowFullscreen(xfc, xfc->window, xfc->fullscreen);
469
xfc->unobscured = (xevent.xvisibility.state == VisibilityUnobscured);
471
XSetWMProtocols(xfc->display, xfc->window->handle, &(xfc->WM_DELETE_WINDOW), 1);
472
xfc->drawable = xfc->window->handle;
476
xfc->drawable = DefaultRootWindow(xfc->display);
480
void xf_toggle_fullscreen(xfContext* xfc)
483
WindowStateChangeEventArgs e;
485
xf_lock_x11(xfc, TRUE);
487
contents = XCreatePixmap(xfc->display, xfc->window->handle, xfc->width, xfc->height, xfc->depth);
488
XCopyArea(xfc->display, xfc->primary, contents, xfc->gc, 0, 0, xfc->width, xfc->height, 0, 0);
490
XDestroyWindow(xfc->display, xfc->window->handle);
491
xfc->fullscreen = (xfc->fullscreen) ? FALSE : TRUE;
492
xf_create_window(xfc);
494
XCopyArea(xfc->display, contents, xfc->primary, xfc->gc, 0, 0, xfc->width, xfc->height, 0, 0);
495
XFreePixmap(xfc->display, contents);
497
xf_unlock_x11(xfc, TRUE);
499
EventArgsInit(&e, "xfreerdp");
500
e.state = xfc->fullscreen ? FREERDP_WINDOW_STATE_FULLSCREEN : 0;
501
PubSub_OnWindowStateChange(((rdpContext*) xfc)->pubSub, xfc, &e);
504
void xf_lock_x11(xfContext* xfc, BOOL display)
506
if (!xfc->UseXThreads)
508
WaitForSingleObject(xfc->mutex, INFINITE);
513
XLockDisplay(xfc->display);
517
void xf_unlock_x11(xfContext* xfc, BOOL display)
519
if (!xfc->UseXThreads)
521
ReleaseMutex(xfc->mutex);
526
XUnlockDisplay(xfc->display);
530
BOOL xf_get_pixmap_info(xfContext* xfc)
537
XVisualInfo template;
538
XPixmapFormatValues* pf;
539
XPixmapFormatValues* pfs;
540
XWindowAttributes window_attributes;
542
pfs = XListPixmapFormats(xfc->display, &pf_count);
546
fprintf(stderr, "xf_get_pixmap_info: XListPixmapFormats failed\n");
550
for (i = 0; i < pf_count; i++)
554
if (pf->depth == xfc->depth)
556
xfc->bpp = pf->bits_per_pixel;
557
xfc->scanline_pad = pf->scanline_pad;
563
ZeroMemory(&template, sizeof(template));
564
template.class = TrueColor;
565
template.screen = xfc->screen_number;
567
if (XGetWindowAttributes(xfc->display, RootWindowOfScreen(xfc->screen), &window_attributes) == 0)
569
fprintf(stderr, "xf_get_pixmap_info: XGetWindowAttributes failed\n");
573
vis = XGetVisualInfo(xfc->display, VisualClassMask | VisualScreenMask, &template, &vi_count);
577
fprintf(stderr, "xf_get_pixmap_info: XGetVisualInfo failed\n");
582
for (i = 0; i < vi_count; i++)
586
if (vi->visual == window_attributes.visual)
588
xfc->visual = vi->visual;
596
* Detect if the server visual has an inverted colormap
597
* (BGR vs RGB, or red being the least significant byte)
600
if (vi->red_mask & 0xFF)
602
xfc->clrconv->invert = TRUE;
608
if ((xfc->visual == NULL) || (xfc->scanline_pad == 0))
616
static int (*_def_error_handler)(Display*, XErrorEvent*);
618
int xf_error_handler(Display* d, XErrorEvent* ev)
623
XGetErrorText(d, ev->error_code, buf, sizeof(buf));
624
fprintf(stderr, "%s", buf);
629
_def_error_handler(d, ev);
634
int _xf_error_handler(Display* d, XErrorEvent* ev)
637
* ungrab the keyboard, in case a debugger is running in
638
* another window. This make xf_error_handler() a potential
639
* debugger breakpoint.
641
XUngrabKeyboard(d, CurrentTime);
642
return xf_error_handler(d, ev);
646
* Callback given to freerdp_connect() to process the pre-connect operations.
647
* It will fill the rdp_freerdp structure (instance) with the appropriate options to use for the connection.
649
* @param instance - pointer to the rdp_freerdp structure that contains the connection's parameters, and will
650
* be filled with the appropriate informations.
652
* @return TRUE if successful. FALSE otherwise.
653
* Can exit with error code XF_EXIT_PARSE_ARGUMENTS if there is an error in the parameters.
655
BOOL xf_pre_connect(freerdp* instance)
657
rdpChannels* channels;
658
rdpSettings* settings;
659
xfContext* xfc = (xfContext*) instance->context;
661
xfc->mutex = CreateMutex(NULL, FALSE, NULL);
663
xfc->settings = instance->settings;
664
xfc->instance = instance;
666
settings = instance->settings;
667
channels = instance->context->channels;
669
PubSub_SubscribeChannelConnected(instance->context->pubSub,
670
(pChannelConnectedEventHandler) xf_OnChannelConnectedEventHandler);
672
PubSub_SubscribeChannelDisconnected(instance->context->pubSub,
673
(pChannelDisconnectedEventHandler) xf_OnChannelDisconnectedEventHandler);
675
freerdp_client_load_addins(channels, instance->settings);
677
freerdp_channels_pre_connect(channels, instance);
679
if (settings->AuthenticationOnly)
681
/* Check --authonly has a username and password. */
682
if (settings->Username == NULL)
684
fprintf(stderr, "--authonly, but no -u username. Please provide one.\n");
687
if (settings->Password == NULL)
689
fprintf(stderr, "--authonly, but no -p password. Please provide one.\n");
692
fprintf(stderr, "%s:%d: Authentication only. Don't connect to X.\n", __FILE__, __LINE__);
693
/* Avoid XWindows initialization and configuration below. */
697
xfc->UseXThreads = TRUE;
699
if (xfc->UseXThreads)
703
fprintf(stderr, "warning: XInitThreads() failure\n");
704
xfc->UseXThreads = FALSE;
708
xfc->display = XOpenDisplay(NULL);
712
fprintf(stderr, "xf_pre_connect: failed to open display: %s\n", XDisplayName(NULL));
713
fprintf(stderr, "Please check that the $DISPLAY environment variable is properly set.\n");
719
fprintf(stderr, "Enabling X11 debug mode.\n");
720
XSynchronize(xfc->display, TRUE);
721
_def_error_handler = XSetErrorHandler(_xf_error_handler);
724
xfc->_NET_WM_ICON = XInternAtom(xfc->display, "_NET_WM_ICON", False);
725
xfc->_MOTIF_WM_HINTS = XInternAtom(xfc->display, "_MOTIF_WM_HINTS", False);
726
xfc->_NET_CURRENT_DESKTOP = XInternAtom(xfc->display, "_NET_CURRENT_DESKTOP", False);
727
xfc->_NET_WORKAREA = XInternAtom(xfc->display, "_NET_WORKAREA", False);
728
xfc->_NET_WM_STATE = XInternAtom(xfc->display, "_NET_WM_STATE", False);
729
xfc->_NET_WM_STATE_FULLSCREEN = XInternAtom(xfc->display, "_NET_WM_STATE_FULLSCREEN", False);
730
xfc->_NET_WM_WINDOW_TYPE = XInternAtom(xfc->display, "_NET_WM_WINDOW_TYPE", False);
732
xfc->_NET_WM_WINDOW_TYPE_NORMAL = XInternAtom(xfc->display, "_NET_WM_WINDOW_TYPE_NORMAL", False);
733
xfc->_NET_WM_WINDOW_TYPE_DIALOG = XInternAtom(xfc->display, "_NET_WM_WINDOW_TYPE_DIALOG", False);
734
xfc->_NET_WM_WINDOW_TYPE_POPUP = XInternAtom(xfc->display, "_NET_WM_WINDOW_TYPE_POPUP", False);
735
xfc->_NET_WM_WINDOW_TYPE_UTILITY = XInternAtom(xfc->display, "_NET_WM_WINDOW_TYPE_UTILITY", False);
736
xfc->_NET_WM_WINDOW_TYPE_DROPDOWN_MENU = XInternAtom(xfc->display, "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", False);
737
xfc->_NET_WM_STATE_SKIP_TASKBAR = XInternAtom(xfc->display, "_NET_WM_STATE_SKIP_TASKBAR", False);
738
xfc->_NET_WM_STATE_SKIP_PAGER = XInternAtom(xfc->display, "_NET_WM_STATE_SKIP_PAGER", False);
739
xfc->_NET_WM_MOVERESIZE = XInternAtom(xfc->display, "_NET_WM_MOVERESIZE", False);
740
xfc->_NET_MOVERESIZE_WINDOW = XInternAtom(xfc->display, "_NET_MOVERESIZE_WINDOW", False);
742
xfc->WM_PROTOCOLS = XInternAtom(xfc->display, "WM_PROTOCOLS", False);
743
xfc->WM_DELETE_WINDOW = XInternAtom(xfc->display, "WM_DELETE_WINDOW", False);
744
xfc->WM_STATE = XInternAtom(xfc->display, "WM_STATE", False);
748
xfc->clrconv = freerdp_clrconv_new(CLRCONV_ALPHA);
750
instance->context->cache = cache_new(instance->settings);
752
xfc->xfds = ConnectionNumber(xfc->display);
753
xfc->screen_number = DefaultScreen(xfc->display);
754
xfc->screen = ScreenOfDisplay(xfc->display, xfc->screen_number);
755
xfc->depth = DefaultDepthOfScreen(xfc->screen);
756
xfc->big_endian = (ImageByteOrder(xfc->display) == MSBFirst);
758
xfc->complex_regions = TRUE;
759
xfc->fullscreen = settings->Fullscreen;
760
xfc->grab_keyboard = settings->GrabKeyboard;
761
xfc->fullscreen_toggle = settings->ToggleFullscreen;
763
xf_detect_monitors(xfc, settings);
769
* Callback given to freerdp_connect() to perform post-connection operations.
770
* It will be called only if the connection was initialized properly, and will continue the initialization based on the
771
* newly created connection.
773
BOOL xf_post_connect(freerdp* instance)
777
rdpChannels* channels;
778
rdpSettings* settings;
779
ResizeWindowEventArgs e;
780
RFX_CONTEXT* rfx_context = NULL;
781
NSC_CONTEXT* nsc_context = NULL;
782
xfContext* xfc = (xfContext*) instance->context;
784
cache = instance->context->cache;
785
channels = instance->context->channels;
786
settings = instance->settings;
788
if (!xf_get_pixmap_info(xfc))
791
xf_register_graphics(instance->context->graphics);
793
if (xfc->settings->SoftwareGdi)
798
flags = CLRCONV_ALPHA;
801
flags |= CLRBUF_32BPP;
803
flags |= CLRBUF_16BPP;
805
gdi_init(instance, flags, NULL);
806
gdi = instance->context->gdi;
807
xfc->primary_buffer = gdi->primary_buffer;
809
rfx_context = gdi->rfx_context;
813
xfc->srcBpp = instance->settings->ColorDepth;
814
xf_gdi_register_update_callbacks(instance->update);
816
xfc->hdc = gdi_CreateDC(xfc->clrconv, xfc->bpp);
818
if (instance->settings->RemoteFxCodec)
820
rfx_context = (void*) rfx_context_new();
821
xfc->rfx_context = rfx_context;
824
if (instance->settings->NSCodec)
826
nsc_context = (void*) nsc_context_new();
827
xfc->nsc_context = nsc_context;
831
xfc->originalWidth = settings->DesktopWidth;
832
xfc->originalHeight = settings->DesktopHeight;
833
xfc->currentWidth = xfc->originalWidth;
834
xfc->currentHeight = xfc->originalWidth;
837
xfc->width = settings->DesktopWidth;
838
xfc->height = settings->DesktopHeight;
840
if (settings->RemoteApplicationMode)
841
xfc->remote_app = TRUE;
843
xf_create_window(xfc);
845
ZeroMemory(&gcv, sizeof(gcv));
846
xfc->modifier_map = XGetModifierMapping(xfc->display);
848
xfc->gc = XCreateGC(xfc->display, xfc->drawable, GCGraphicsExposures, &gcv);
849
xfc->primary = XCreatePixmap(xfc->display, xfc->drawable, xfc->width, xfc->height, xfc->depth);
850
xfc->drawing = xfc->primary;
852
xfc->bitmap_mono = XCreatePixmap(xfc->display, xfc->drawable, 8, 8, 1);
853
xfc->gc_mono = XCreateGC(xfc->display, xfc->bitmap_mono, GCGraphicsExposures, &gcv);
855
XSetFunction(xfc->display, xfc->gc, GXcopy);
856
XSetFillStyle(xfc->display, xfc->gc, FillSolid);
857
XSetForeground(xfc->display, xfc->gc, BlackPixelOfScreen(xfc->screen));
858
XFillRectangle(xfc->display, xfc->primary, xfc->gc, 0, 0, xfc->width, xfc->height);
859
XFlush(xfc->display);
861
xfc->image = XCreateImage(xfc->display, xfc->visual, xfc->depth, ZPixmap, 0,
862
(char*) xfc->primary_buffer, xfc->width, xfc->height, xfc->scanline_pad, 0);
864
xfc->bmp_codec_none = (BYTE*) malloc(64 * 64 * 4);
866
if (xfc->settings->SoftwareGdi)
868
instance->update->BeginPaint = xf_sw_begin_paint;
869
instance->update->EndPaint = xf_sw_end_paint;
870
instance->update->DesktopResize = xf_sw_desktop_resize;
874
instance->update->BeginPaint = xf_hw_begin_paint;
875
instance->update->EndPaint = xf_hw_end_paint;
876
instance->update->DesktopResize = xf_hw_desktop_resize;
879
pointer_cache_register_callbacks(instance->update);
881
if (!xfc->settings->SoftwareGdi)
883
glyph_cache_register_callbacks(instance->update);
884
brush_cache_register_callbacks(instance->update);
885
bitmap_cache_register_callbacks(instance->update);
886
offscreen_cache_register_callbacks(instance->update);
887
palette_cache_register_callbacks(instance->update);
890
instance->context->rail = rail_new(instance->settings);
891
rail_register_update_callbacks(instance->context->rail, instance->update);
892
xf_rail_register_callbacks(xfc, instance->context->rail);
894
freerdp_channels_post_connect(channels, instance);
896
xf_tsmf_init(xfc, xv_port);
898
xf_cliprdr_init(xfc, channels);
900
EventArgsInit(&e, "xfreerdp");
901
e.width = settings->DesktopWidth;
902
e.height = settings->DesktopHeight;
903
PubSub_OnResizeWindow(((rdpContext*) xfc)->pubSub, xfc, &e);
908
/** Callback set in the rdp_freerdp structure, and used to get the user's password,
909
* if required to establish the connection.
910
* This function is actually called in credssp_ntlmssp_client_init()
911
* @see rdp_server_accept_nego() and rdp_check_fds()
912
* @param instance - pointer to the rdp_freerdp structure that contains the connection settings
913
* @param username - unused
914
* @param password - on return: pointer to a character string that will be filled by the password entered by the user.
915
* Note that this character string will be allocated inside the function, and needs to be deallocated by the caller
916
* using free(), even in case this function fails.
917
* @param domain - unused
918
* @return TRUE if a password was successfully entered. See freerdp_passphrase_read() for more details.
920
BOOL xf_authenticate(freerdp* instance, char** username, char** password, char** domain)
922
// FIXME: seems this callback may be called when 'username' is not known.
923
// But it doesn't do anything to fix it...
924
*password = malloc(password_size * sizeof(char));
926
if (freerdp_passphrase_read("Password: ", *password, password_size, instance->settings->CredentialsFromStdin) == NULL)
932
/** Callback set in the rdp_freerdp structure, and used to make a certificate validation
933
* when the connection requires it.
934
* This function will actually be called by tls_verify_certificate().
935
* @see rdp_client_connect() and tls_connect()
936
* @param instance - pointer to the rdp_freerdp structure that contains the connection settings
940
* @return TRUE if the certificate is trusted. FALSE otherwise.
942
BOOL xf_verify_certificate(freerdp* instance, char* subject, char* issuer, char* fingerprint)
946
printf("Certificate details:\n");
947
printf("\tSubject: %s\n", subject);
948
printf("\tIssuer: %s\n", issuer);
949
printf("\tThumbprint: %s\n", fingerprint);
950
printf("The above X.509 certificate could not be verified, possibly because you do not have "
951
"the CA certificate in your certificate store, or the certificate has expired. "
952
"Please look at the documentation on how to create local certificate store for a private CA.\n");
956
printf("Do you trust the above certificate? (Y/N) ");
957
answer = fgetc(stdin);
961
printf("\nError: Could not read answer from stdin.");
962
if (instance->settings->CredentialsFromStdin)
963
printf(" - Run without parameter \"--from-stdin\" to set trust.");
968
if (answer == 'y' || answer == 'Y')
972
else if (answer == 'n' || answer == 'N')
982
int xf_logon_error_info(freerdp* instance, UINT32 data, UINT32 type)
984
xfContext* xfc = (xfContext*) instance->context;
986
xf_rail_disable_remoteapp_mode(xfc);
991
int xf_receive_channel_data(freerdp* instance, int channelId, BYTE* data, int size, int flags, int total_size)
993
return freerdp_channels_data(instance, channelId, data, size, flags, total_size);
996
void xf_process_channel_event(rdpChannels* channels, freerdp* instance)
1001
xfc = (xfContext*) instance->context;
1003
event = freerdp_channels_pop_event(channels);
1007
switch (GetMessageClass(event->id))
1009
case RailChannel_Class:
1010
xf_process_rail_event(xfc, channels, event);
1013
case TsmfChannel_Class:
1014
xf_process_tsmf_event(xfc, event);
1017
case CliprdrChannel_Class:
1018
xf_process_cliprdr_event(xfc, event);
1021
case RdpeiChannel_Class:
1022
xf_process_rdpei_event(xfc, event);
1029
freerdp_event_free(event);
1033
void xf_window_free(xfContext* xfc)
1035
rdpContext* context = (rdpContext*) xfc;
1037
if (xfc->modifier_map)
1039
XFreeModifiermap(xfc->modifier_map);
1040
xfc->modifier_map = NULL;
1045
XFreeGC(xfc->display, xfc->gc);
1051
XFreeGC(xfc->display, xfc->gc_mono);
1057
xf_DestroyWindow(xfc, xfc->window);
1063
XFreePixmap(xfc->display, xfc->primary);
1067
if (xfc->bitmap_mono)
1069
XFreePixmap(xfc->display, xfc->bitmap_mono);
1070
xfc->bitmap_mono = 0;
1075
xfc->image->data = NULL;
1076
XDestroyImage(xfc->image);
1082
cache_free(context->cache);
1083
context->cache = NULL;
1088
rail_free(context->rail);
1089
context->rail = NULL;
1092
if (xfc->rfx_context)
1094
rfx_context_free(xfc->rfx_context);
1095
xfc->rfx_context = NULL;
1098
if (xfc->nsc_context)
1100
nsc_context_free(xfc->nsc_context);
1101
xfc->nsc_context = NULL;
1106
freerdp_clrconv_free(xfc->clrconv);
1107
xfc->clrconv = NULL;
1111
gdi_DeleteDC(xfc->hdc);
1113
if (xfc->xv_context)
1115
xf_tsmf_uninit(xfc);
1116
xfc->xv_context = NULL;
1119
if (xfc->clipboard_context)
1121
xf_cliprdr_uninit(xfc);
1122
xfc->clipboard_context = NULL;
1126
void* xf_update_thread(void* arg)
1130
wMessageQueue* queue;
1131
freerdp* instance = (freerdp*) arg;
1134
queue = freerdp_get_message_queue(instance, FREERDP_UPDATE_MESSAGE_QUEUE);
1136
while (MessageQueue_Wait(queue))
1138
while (MessageQueue_Peek(queue, &message, TRUE))
1140
status = freerdp_message_queue_process_message(instance, FREERDP_UPDATE_MESSAGE_QUEUE, &message);
1153
void* xf_input_thread(void* arg)
1158
wMessageQueue* queue;
1159
int pending_status = 1;
1160
int process_status = 1;
1161
freerdp* instance = (freerdp*) arg;
1163
xfc = (xfContext*) instance->context;
1165
event = CreateFileDescriptorEvent(NULL, FALSE, FALSE, xfc->xfds);
1167
while (WaitForSingleObject(event, INFINITE) == WAIT_OBJECT_0)
1171
xf_lock_x11(xfc, FALSE);
1173
pending_status = XPending(xfc->display);
1175
xf_unlock_x11(xfc, FALSE);
1179
xf_lock_x11(xfc, FALSE);
1181
ZeroMemory(&xevent, sizeof(xevent));
1182
XNextEvent(xfc->display, &xevent);
1183
process_status = xf_event_process(instance, &xevent);
1185
xf_unlock_x11(xfc, FALSE);
1187
if (!process_status)
1191
while (pending_status);
1193
if (!process_status)
1197
queue = freerdp_get_message_queue(instance, FREERDP_INPUT_MESSAGE_QUEUE);
1198
MessageQueue_PostQuit(queue, 0);
1203
void* xf_channels_thread(void* arg)
1208
rdpChannels* channels;
1209
freerdp* instance = (freerdp*) arg;
1211
xfc = (xfContext*) instance->context;
1213
channels = instance->context->channels;
1214
event = freerdp_channels_get_event_handle(instance);
1216
while (WaitForSingleObject(event, INFINITE) == WAIT_OBJECT_0)
1218
status = freerdp_channels_process_pending_messages(instance);
1219
xf_process_channel_event(channels, instance);
1225
/** Main loop for the rdp connection.
1226
* It will be run from the thread's entry point (thread_func()).
1227
* It initiates the connection, and will continue to run until the session ends,
1228
* processing events as they are received.
1229
* @param instance - pointer to the rdp_freerdp structure that contains the session's settings
1230
* @return A code from the enum XF_EXIT_CODE (0 if successful)
1232
void* xf_thread(void* param)
1252
BOOL async_channels;
1253
BOOL async_transport;
1254
HANDLE update_thread;
1255
HANDLE input_thread;
1256
HANDLE channels_thread;
1257
rdpChannels* channels;
1258
rdpSettings* settings;
1259
struct timeval timeout;
1264
instance = (freerdp*) param;
1266
ZeroMemory(rfds, sizeof(rfds));
1267
ZeroMemory(wfds, sizeof(wfds));
1268
ZeroMemory(&timeout, sizeof(struct timeval));
1270
status = freerdp_connect(instance);
1272
xfc = (xfContext*) instance->context;
1274
/* Connection succeeded. --authonly ? */
1275
if (instance->settings->AuthenticationOnly)
1277
freerdp_disconnect(instance);
1278
fprintf(stderr, "%s:%d: Authentication only, exit status %d\n", __FILE__, __LINE__, !status);
1284
exit_code = XF_EXIT_CONN_FAILED;
1285
ExitThread(exit_code);
1288
channels = instance->context->channels;
1289
settings = instance->context->settings;
1291
async_update = settings->AsyncUpdate;
1292
async_input = settings->AsyncInput;
1293
async_channels = settings->AsyncChannels;
1294
async_transport = settings->AsyncTransport;
1298
update_thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) xf_update_thread, instance, 0, NULL);
1303
input_thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) xf_input_thread, instance, 0, NULL);
1308
channels_thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) xf_channels_thread, instance, 0, NULL);
1311
while (!xfc->disconnect && !freerdp_shall_disconnect(instance))
1316
if (freerdp_focus_required(instance))
1318
xf_kbd_focus_in(xfc);
1321
if (!async_transport)
1323
if (freerdp_get_fds(instance, rfds, &rcount, wfds, &wcount) != TRUE)
1325
fprintf(stderr, "Failed to get FreeRDP file descriptor\n");
1326
exit_code = XF_EXIT_CONN_FAILED;
1331
if (!async_channels)
1333
if (freerdp_channels_get_fds(channels, instance, rfds, &rcount, wfds, &wcount) != TRUE)
1335
fprintf(stderr, "Failed to get channel manager file descriptor\n");
1336
exit_code = XF_EXIT_CONN_FAILED;
1343
if (xf_get_fds(instance, rfds, &rcount, wfds, &wcount) != TRUE)
1345
fprintf(stderr, "Failed to get xfreerdp file descriptor\n");
1346
exit_code = XF_EXIT_CONN_FAILED;
1352
input_event = freerdp_get_message_queue_event_handle(instance, FREERDP_INPUT_MESSAGE_QUEUE);
1353
fd_input_event = GetEventFileDescriptor(input_event);
1354
rfds[rcount++] = (void*) (long) fd_input_event;
1361
for (i = 0; i < rcount; i++)
1363
fds = (int)(long)(rfds[i]);
1368
FD_SET(fds, &rfds_set);
1375
timeout.tv_usec = 0;
1377
select_status = select(max_fds + 1, &rfds_set, NULL, NULL, &timeout);
1379
if (select_status == 0)
1381
continue; /* select timeout */
1383
else if (select_status == -1)
1385
/* these are not really errors */
1386
if (!((errno == EAGAIN) || (errno == EWOULDBLOCK) ||
1387
(errno == EINPROGRESS) || (errno == EINTR))) /* signal occurred */
1389
fprintf(stderr, "xfreerdp_run: select failed\n");
1394
if (!async_transport)
1396
if (freerdp_check_fds(instance) != TRUE)
1398
fprintf(stderr, "Failed to check FreeRDP file descriptor\n");
1403
if (!async_channels)
1405
if (freerdp_channels_check_fds(channels, instance) != TRUE)
1407
fprintf(stderr, "Failed to check channel manager file descriptor\n");
1411
xf_process_channel_event(channels, instance);
1416
if (xf_process_x_events(instance) != TRUE)
1418
fprintf(stderr, "Closed from X11\n");
1424
if (WaitForSingleObject(input_event, 0) == WAIT_OBJECT_0)
1426
if (!freerdp_message_queue_process_pending_messages(instance, FREERDP_INPUT_MESSAGE_QUEUE))
1428
fprintf(stderr, "User Disconnect\n");
1429
xfc->disconnect = TRUE;
1438
wMessageQueue* update_queue = freerdp_get_message_queue(instance, FREERDP_UPDATE_MESSAGE_QUEUE);
1439
MessageQueue_PostQuit(update_queue, 0);
1440
WaitForSingleObject(update_thread, INFINITE);
1441
CloseHandle(update_thread);
1444
FILE* fin = fopen("/tmp/tsmf.tid", "rt");
1452
fscanf(fin, "%d", &thid);
1455
pthread_kill((pthread_t) (size_t) thid, SIGUSR1);
1457
fin1 = fopen("/tmp/tsmf.tid", "rt");
1468
unlink("/tmp/tsmf.tid");
1469
pthread_kill((pthread_t) (size_t) thid, SIGKILL);
1473
fin1 = fopen("/tmp/tsmf.tid", "rt");
1478
exit_code = freerdp_error_info(instance);
1480
freerdp_channels_close(channels, instance);
1481
freerdp_channels_free(channels);
1482
freerdp_disconnect(instance);
1485
ExitThread(exit_code);
1488
DWORD xf_exit_code_from_disconnect_reason(DWORD reason)
1490
if (reason == 0 || (reason >= XF_EXIT_PARSE_ARGUMENTS && reason <= XF_EXIT_CONN_FAILED))
1493
/* License error set */
1494
else if (reason >= 0x100 && reason <= 0x10A)
1495
reason -= 0x100 + XF_EXIT_LICENSE_INTERNAL;
1497
/* RDP protocol error set */
1498
else if (reason >= 0x10c9 && reason <= 0x1193)
1499
reason = XF_EXIT_RDP;
1501
/* There's no need to test protocol-independent codes: they match */
1502
else if (!(reason <= 0xB))
1503
reason = XF_EXIT_UNKNOWN;
1508
void xf_TerminateEventHandler(rdpContext* context, TerminateEventArgs* e)
1510
wMessageQueue* queue;
1511
xfContext* xfc = (xfContext*) context;
1513
if (context->settings->AsyncInput)
1515
queue = freerdp_get_message_queue(context->instance, FREERDP_INPUT_MESSAGE_QUEUE);
1518
MessageQueue_PostQuit(queue, 0);
1522
xfc->disconnect = TRUE;
1526
void xf_ParamChangeEventHandler(rdpContext* context, ParamChangeEventArgs* e)
1530
case FreeRDP_ScalingFactor:
1542
void xfreerdp_client_global_init()
1544
setlocale(LC_ALL, "");
1545
freerdp_handle_signals();
1546
freerdp_channels_global_init();
1549
void xfreerdp_client_global_uninit()
1551
freerdp_channels_global_uninit();
1554
int xfreerdp_client_start(rdpContext* context)
1556
xfContext* xfc = (xfContext*) context;
1558
rdpSettings* settings = context->settings;
1560
if (!settings->ServerHostname)
1562
fprintf(stderr, "error: server hostname was not specified with /v:<server>[:port]\n");
1566
xfc->thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) xf_thread, context->instance, 0, NULL);
1571
int xfreerdp_client_stop(rdpContext* context)
1573
xfContext* xfc = (xfContext*) context;
1575
if (context->settings->AsyncInput)
1577
wMessageQueue* queue;
1578
queue = freerdp_get_message_queue(context->instance, FREERDP_INPUT_MESSAGE_QUEUE);
1581
MessageQueue_PostQuit(queue, 0);
1585
xfc->disconnect = TRUE;
1591
double freerdp_client_get_scale(rdpContext* context)
1593
xfContext* xfc = (xfContext*) context;
1597
void freerdp_client_reset_scale(rdpContext* context)
1599
ResizeWindowEventArgs e;
1600
xfContext* xfc = (xfContext*) context;
1603
XResizeWindow(xfc->display, xfc->window->handle, xfc->originalWidth * xfc->scale, xfc->originalHeight * xfc->scale);
1605
EventArgsInit(&e, "xfreerdp");
1606
e.width = (int) xfc->originalWidth * xfc->scale;
1607
e.height = (int) xfc->originalHeight * xfc->scale;
1608
PubSub_OnResizeWindow(((rdpContext*) xfc)->pubSub, xfc, &e);
1610
xf_draw_screen_scaled(xfc);
1613
int xfreerdp_client_new(freerdp* instance, rdpContext* context)
1616
rdpSettings* settings;
1618
xfc = (xfContext*) instance->context;
1620
instance->PreConnect = xf_pre_connect;
1621
instance->PostConnect = xf_post_connect;
1622
instance->Authenticate = xf_authenticate;
1623
instance->VerifyCertificate = xf_verify_certificate;
1624
instance->LogonErrorInfo = xf_logon_error_info;
1625
instance->ReceiveChannelData = xf_receive_channel_data;
1627
context->channels = freerdp_channels_new();
1629
settings = instance->settings;
1630
xfc->settings = instance->context->settings;
1632
settings->OsMajorType = OSMAJORTYPE_UNIX;
1633
settings->OsMinorType = OSMINORTYPE_NATIVE_XSERVER;
1635
settings->OrderSupport[NEG_DSTBLT_INDEX] = TRUE;
1636
settings->OrderSupport[NEG_PATBLT_INDEX] = TRUE;
1637
settings->OrderSupport[NEG_SCRBLT_INDEX] = TRUE;
1638
settings->OrderSupport[NEG_OPAQUE_RECT_INDEX] = TRUE;
1639
settings->OrderSupport[NEG_DRAWNINEGRID_INDEX] = FALSE;
1640
settings->OrderSupport[NEG_MULTIDSTBLT_INDEX] = FALSE;
1641
settings->OrderSupport[NEG_MULTIPATBLT_INDEX] = FALSE;
1642
settings->OrderSupport[NEG_MULTISCRBLT_INDEX] = FALSE;
1643
settings->OrderSupport[NEG_MULTIOPAQUERECT_INDEX] = TRUE;
1644
settings->OrderSupport[NEG_MULTI_DRAWNINEGRID_INDEX] = FALSE;
1645
settings->OrderSupport[NEG_LINETO_INDEX] = TRUE;
1646
settings->OrderSupport[NEG_POLYLINE_INDEX] = TRUE;
1647
settings->OrderSupport[NEG_MEMBLT_INDEX] = settings->BitmapCacheEnabled;
1648
settings->OrderSupport[NEG_MEM3BLT_INDEX] = (settings->SoftwareGdi) ? TRUE : FALSE;
1649
settings->OrderSupport[NEG_MEMBLT_V2_INDEX] = settings->BitmapCacheEnabled;
1650
settings->OrderSupport[NEG_MEM3BLT_V2_INDEX] = FALSE;
1651
settings->OrderSupport[NEG_SAVEBITMAP_INDEX] = FALSE;
1652
settings->OrderSupport[NEG_GLYPH_INDEX_INDEX] = TRUE;
1653
settings->OrderSupport[NEG_FAST_INDEX_INDEX] = TRUE;
1654
settings->OrderSupport[NEG_FAST_GLYPH_INDEX] = TRUE;
1655
settings->OrderSupport[NEG_POLYGON_SC_INDEX] = (settings->SoftwareGdi) ? FALSE : TRUE;
1656
settings->OrderSupport[NEG_POLYGON_CB_INDEX] = (settings->SoftwareGdi) ? FALSE : TRUE;
1657
settings->OrderSupport[NEG_ELLIPSE_SC_INDEX] = FALSE;
1658
settings->OrderSupport[NEG_ELLIPSE_CB_INDEX] = FALSE;
1660
PubSub_SubscribeTerminate(context->pubSub, (pTerminateEventHandler) xf_TerminateEventHandler);
1661
PubSub_SubscribeParamChange(context->pubSub, (pParamChangeEventHandler) xf_ParamChangeEventHandler);
1666
void xfreerdp_client_free(freerdp* instance, rdpContext* context)
1668
xfContext* xfc = (xfContext*) context;
1672
xf_window_free(xfc);
1674
if (xfc->bmp_codec_none)
1675
free(xfc->bmp_codec_none);
1678
XCloseDisplay(xfc->display);
1682
int RdpClientEntry(RDP_CLIENT_ENTRY_POINTS* pEntryPoints)
1684
pEntryPoints->Version = 1;
1685
pEntryPoints->Size = sizeof(RDP_CLIENT_ENTRY_POINTS_V1);
1687
pEntryPoints->GlobalInit = xfreerdp_client_global_init;
1688
pEntryPoints->GlobalUninit = xfreerdp_client_global_uninit;
1690
pEntryPoints->ContextSize = sizeof(xfContext);
1691
pEntryPoints->ClientNew = xfreerdp_client_new;
1692
pEntryPoints->ClientFree = xfreerdp_client_free;
1694
pEntryPoints->ClientStart = xfreerdp_client_start;
1695
pEntryPoints->ClientStop = xfreerdp_client_stop;