~ubuntu-branches/ubuntu/vivid/freerdp/vivid

« back to all changes in this revision

Viewing changes to client/X11/xf_client.c

  • Committer: Package Import Robot
  • Author(s): Iain Lane
  • Date: 2014-11-11 12:20:50 UTC
  • mfrom: (1.1.9) (9.1.17 sid)
  • Revision ID: package-import@ubuntu.com-20141111122050-wyr8hrnwco9fcmum
Tags: 1.1.0~git20140921.1.440916e+dfsg1-2ubuntu1
* Merge with Debian unstable, remaining changes
  - Disable ffmpeg support
* Disable gstreamer support, this relies on gstreamer 0.10 and we don't want
  to add any more deps on that.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * FreeRDP: A Remote Desktop Protocol Implementation
 
3
 * X11 Client Interface
 
4
 *
 
5
 * Copyright 2013 Marc-Andre Moreau <marcandre.moreau@gmail.com>
 
6
 *
 
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
 
10
 *
 
11
 *     http://www.apache.org/licenses/LICENSE-2.0
 
12
 *
 
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.
 
18
 */
 
19
 
 
20
#ifdef HAVE_CONFIG_H
 
21
#include "config.h"
 
22
#endif
 
23
 
 
24
#include <X11/Xlib.h>
 
25
#include <X11/Xutil.h>
 
26
 
 
27
#ifdef WITH_XCURSOR
 
28
#include <X11/Xcursor/Xcursor.h>
 
29
#endif
 
30
 
 
31
#ifdef WITH_XINERAMA
 
32
#include <X11/extensions/Xinerama.h>
 
33
#endif
 
34
 
 
35
#ifdef WITH_XI
 
36
#include <X11/extensions/XInput2.h>
 
37
#endif
 
38
 
 
39
#ifdef WITH_XRENDER
 
40
#include <X11/extensions/Xrender.h>
 
41
#endif
 
42
 
 
43
#include <errno.h>
 
44
#include <stdio.h>
 
45
#include <stdlib.h>
 
46
#include <locale.h>
 
47
#include <unistd.h>
 
48
#include <string.h>
 
49
#include <termios.h>
 
50
#include <pthread.h>
 
51
#include <sys/wait.h>
 
52
#include <sys/types.h>
 
53
#include <sys/select.h>
 
54
 
 
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>
 
61
 
 
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>
 
67
 
 
68
#include <freerdp/rail.h>
 
69
 
 
70
#include <freerdp/client/file.h>
 
71
#include <freerdp/client/cmdline.h>
 
72
 
 
73
#include <winpr/crt.h>
 
74
#include <winpr/synch.h>
 
75
#include <winpr/file.h>
 
76
 
 
77
#include "xf_gdi.h"
 
78
#include "xf_rail.h"
 
79
#include "xf_tsmf.h"
 
80
#include "xf_event.h"
 
81
#include "xf_input.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"
 
87
 
 
88
#include "xfreerdp.h"
 
89
 
 
90
static long xv_port = 0;
 
91
static const size_t password_size = 512;
 
92
 
 
93
void xf_draw_screen_scaled(xfContext* xfc)
 
94
{
 
95
#ifdef WITH_XRENDER
 
96
        XTransform transform;
 
97
        Picture windowPicture;
 
98
        Picture primaryPicture;
 
99
        XRenderPictureAttributes pa;
 
100
        XRenderPictFormat* picFormat;
 
101
 
 
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);
 
106
 
 
107
        transform.matrix[0][0] = XDoubleToFixed(1);
 
108
        transform.matrix[0][1] = XDoubleToFixed(0);
 
109
        transform.matrix[0][2] = XDoubleToFixed(0);
 
110
 
 
111
        transform.matrix[1][0] = XDoubleToFixed(0);
 
112
        transform.matrix[1][1] = XDoubleToFixed(1);
 
113
        transform.matrix[1][2] = XDoubleToFixed(0);
 
114
 
 
115
        transform.matrix[2][0] = XDoubleToFixed(0);
 
116
        transform.matrix[2][1] = XDoubleToFixed(0);
 
117
        transform.matrix[2][2] = XDoubleToFixed(xfc->scale);
 
118
 
 
119
        XRenderSetPictureTransform(xfc->display, primaryPicture, &transform);
 
120
        XRenderComposite(xfc->display, PictOpSrc, primaryPicture, 0, windowPicture, 0, 0, 0, 0, 0, 0, xfc->currentWidth, xfc->currentHeight);
 
121
#endif
 
122
}
 
123
 
 
124
void xf_sw_begin_paint(rdpContext* context)
 
125
{
 
126
        rdpGdi* gdi = context->gdi;
 
127
        gdi->primary->hdc->hwnd->invalid->null = 1;
 
128
        gdi->primary->hdc->hwnd->ninvalid = 0;
 
129
}
 
130
 
 
131
void xf_sw_end_paint(rdpContext* context)
 
132
{
 
133
        rdpGdi* gdi;
 
134
        INT32 x, y;
 
135
        UINT32 w, h;
 
136
        xfContext* xfc = (xfContext*) context;
 
137
 
 
138
        gdi = context->gdi;
 
139
 
 
140
        if (!xfc->remote_app)
 
141
        {
 
142
                if (!xfc->complex_regions)
 
143
                {
 
144
                        if (gdi->primary->hdc->hwnd->invalid->null)
 
145
                                return;
 
146
 
 
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;
 
151
 
 
152
                        xf_lock_x11(xfc, FALSE);
 
153
 
 
154
                        XPutImage(xfc->display, xfc->primary, xfc->gc, xfc->image, x, y, x, y, w, h);
 
155
 
 
156
                        if (xfc->scale != 1.0)
 
157
                        {
 
158
                                xf_draw_screen_scaled(xfc);
 
159
                        }
 
160
                        else
 
161
                        {
 
162
                                XCopyArea(xfc->display, xfc->primary, xfc->window->handle, xfc->gc, x, y, w, h, x, y);
 
163
                        }
 
164
 
 
165
                        xf_unlock_x11(xfc, FALSE);
 
166
                }
 
167
                else
 
168
                {
 
169
                        int i;
 
170
                        int ninvalid;
 
171
                        HGDI_RGN cinvalid;
 
172
 
 
173
                        if (gdi->primary->hdc->hwnd->ninvalid < 1)
 
174
                                return;
 
175
 
 
176
                        ninvalid = gdi->primary->hdc->hwnd->ninvalid;
 
177
                        cinvalid = gdi->primary->hdc->hwnd->cinvalid;
 
178
 
 
179
                        xf_lock_x11(xfc, FALSE);
 
180
 
 
181
                        for (i = 0; i < ninvalid; i++)
 
182
                        {
 
183
                                x = cinvalid[i].x;
 
184
                                y = cinvalid[i].y;
 
185
                                w = cinvalid[i].w;
 
186
                                h = cinvalid[i].h;
 
187
 
 
188
                                XPutImage(xfc->display, xfc->primary, xfc->gc, xfc->image, x, y, x, y, w, h);
 
189
 
 
190
                                if (xfc->scale != 1.0)
 
191
                                {
 
192
                                        xf_draw_screen_scaled(xfc);
 
193
                                }
 
194
                                else
 
195
                                {
 
196
                                        XCopyArea(xfc->display, xfc->primary, xfc->window->handle, xfc->gc, x, y, w, h, x, y);
 
197
                                }
 
198
                        }
 
199
 
 
200
                        XFlush(xfc->display);
 
201
 
 
202
                        xf_unlock_x11(xfc, FALSE);
 
203
                }
 
204
        }
 
205
        else
 
206
        {
 
207
                if (gdi->primary->hdc->hwnd->invalid->null)
 
208
                        return;
 
209
 
 
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;
 
214
 
 
215
                xf_lock_x11(xfc, FALSE);
 
216
 
 
217
                xf_rail_paint(xfc, context->rail, x, y, x + w - 1, y + h - 1);
 
218
 
 
219
                xf_unlock_x11(xfc, FALSE);
 
220
        }
 
221
}
 
222
 
 
223
void xf_sw_desktop_resize(rdpContext* context)
 
224
{
 
225
        rdpSettings* settings;
 
226
        xfContext* xfc = (xfContext*) context;
 
227
 
 
228
        settings = xfc->instance->settings;
 
229
 
 
230
        xf_lock_x11(xfc, TRUE);
 
231
 
 
232
        if (!xfc->fullscreen)
 
233
        {
 
234
                rdpGdi* gdi = context->gdi;
 
235
                gdi_resize(gdi, xfc->width, xfc->height);
 
236
 
 
237
                if (xfc->image)
 
238
                {
 
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);
 
243
                }
 
244
        }
 
245
 
 
246
        xf_unlock_x11(xfc, TRUE);
 
247
}
 
248
 
 
249
void xf_hw_begin_paint(rdpContext* context)
 
250
{
 
251
        xfContext* xfc = (xfContext*) context;
 
252
 
 
253
        xfc->hdc->hwnd->invalid->null = 1;
 
254
        xfc->hdc->hwnd->ninvalid = 0;
 
255
}
 
256
 
 
257
void xf_hw_end_paint(rdpContext* context)
 
258
{
 
259
        INT32 x, y;
 
260
        UINT32 w, h;
 
261
        xfContext* xfc = (xfContext*) context;
 
262
 
 
263
        if (!xfc->remote_app)
 
264
        {
 
265
                if (!xfc->complex_regions)
 
266
                {
 
267
                        if (xfc->hdc->hwnd->invalid->null)
 
268
                                return;
 
269
 
 
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;
 
274
 
 
275
                        xf_lock_x11(xfc, FALSE);
 
276
 
 
277
                        if (xfc->scale != 1.0)
 
278
                        {
 
279
                                xf_draw_screen_scaled(xfc);
 
280
                        }
 
281
                        else
 
282
                        {
 
283
                                XCopyArea(xfc->display, xfc->primary, xfc->drawable, xfc->gc, x, y, w, h, x, y);
 
284
                        }
 
285
 
 
286
                        xf_unlock_x11(xfc, FALSE);
 
287
                }
 
288
                else
 
289
                {
 
290
                        int i;
 
291
                        int ninvalid;
 
292
                        HGDI_RGN cinvalid;
 
293
 
 
294
                        if (xfc->hdc->hwnd->ninvalid < 1)
 
295
                                return;
 
296
 
 
297
                        ninvalid = xfc->hdc->hwnd->ninvalid;
 
298
                        cinvalid = xfc->hdc->hwnd->cinvalid;
 
299
 
 
300
                        xf_lock_x11(xfc, FALSE);
 
301
 
 
302
                        for (i = 0; i < ninvalid; i++)
 
303
                        {
 
304
                                x = cinvalid[i].x;
 
305
                                y = cinvalid[i].y;
 
306
                                w = cinvalid[i].w;
 
307
                                h = cinvalid[i].h;
 
308
 
 
309
                                if (xfc->scale != 1.0)
 
310
                                {
 
311
                                        xf_draw_screen_scaled(xfc);
 
312
                                }
 
313
                                else
 
314
                                {
 
315
                                        XCopyArea(xfc->display, xfc->primary, xfc->drawable, xfc->gc, x, y, w, h, x, y);
 
316
                                }
 
317
                        }
 
318
 
 
319
                        XFlush(xfc->display);
 
320
 
 
321
                        xf_unlock_x11(xfc, FALSE);
 
322
                }
 
323
        }
 
324
        else
 
325
        {
 
326
                if (xfc->hdc->hwnd->invalid->null)
 
327
                        return;
 
328
 
 
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;
 
333
 
 
334
                xf_lock_x11(xfc, FALSE);
 
335
 
 
336
                xf_rail_paint(xfc, context->rail, x, y, x + w - 1, y + h - 1);
 
337
 
 
338
                xf_unlock_x11(xfc, FALSE);
 
339
        }
 
340
}
 
341
 
 
342
void xf_hw_desktop_resize(rdpContext* context)
 
343
{
 
344
        BOOL same;
 
345
        rdpSettings* settings;
 
346
        xfContext* xfc = (xfContext*) context;
 
347
 
 
348
        settings = xfc->instance->settings;
 
349
 
 
350
        xf_lock_x11(xfc, TRUE);
 
351
 
 
352
        if (!xfc->fullscreen)
 
353
        {
 
354
                xfc->width = settings->DesktopWidth;
 
355
                xfc->height = settings->DesktopHeight;
 
356
 
 
357
                if (xfc->window)
 
358
                        xf_ResizeDesktopWindow(xfc, xfc->window, settings->DesktopWidth, settings->DesktopHeight);
 
359
 
 
360
                if (xfc->primary)
 
361
                {
 
362
                        same = (xfc->primary == xfc->drawing) ? TRUE : FALSE;
 
363
 
 
364
                        XFreePixmap(xfc->display, xfc->primary);
 
365
 
 
366
                        xfc->primary = XCreatePixmap(xfc->display, xfc->drawable,
 
367
                                        xfc->width, xfc->height, xfc->depth);
 
368
 
 
369
                        if (same)
 
370
                                xfc->drawing = xfc->primary;
 
371
                }
 
372
        }
 
373
        else
 
374
        {
 
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);
 
379
        }
 
380
 
 
381
        xf_unlock_x11(xfc, TRUE);
 
382
}
 
383
 
 
384
BOOL xf_get_fds(freerdp* instance, void** rfds, int* rcount, void** wfds, int* wcount)
 
385
{
 
386
        xfContext* xfc = (xfContext*) instance->context;
 
387
 
 
388
        rfds[*rcount] = (void*)(long)(xfc->xfds);
 
389
        (*rcount)++;
 
390
 
 
391
        return TRUE;
 
392
}
 
393
 
 
394
BOOL xf_process_x_events(freerdp* instance)
 
395
{
 
396
        BOOL status;
 
397
        XEvent xevent;
 
398
        int pending_status;
 
399
        xfContext* xfc = (xfContext*) instance->context;
 
400
 
 
401
        status = TRUE;
 
402
        pending_status = TRUE;
 
403
 
 
404
        while (pending_status)
 
405
        {
 
406
                xf_lock_x11(xfc, FALSE);
 
407
 
 
408
                pending_status = XPending(xfc->display);
 
409
 
 
410
                xf_unlock_x11(xfc, FALSE);
 
411
 
 
412
                if (pending_status)
 
413
                {
 
414
                        ZeroMemory(&xevent, sizeof(xevent));
 
415
 
 
416
                        XNextEvent(xfc->display, &xevent);
 
417
                        status = xf_event_process(instance, &xevent);
 
418
 
 
419
                        if (!status)
 
420
                                return status;
 
421
                }
 
422
        }
 
423
 
 
424
        return status;
 
425
}
 
426
 
 
427
void xf_create_window(xfContext* xfc)
 
428
{
 
429
        XEvent xevent;
 
430
        int width, height;
 
431
        char* windowTitle;
 
432
 
 
433
        ZeroMemory(&xevent, sizeof(xevent));
 
434
 
 
435
        width = xfc->width;
 
436
        height = xfc->height;
 
437
 
 
438
        if (!xfc->remote_app)
 
439
        {
 
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;
 
447
 
 
448
                if (xfc->instance->settings->WindowTitle)
 
449
                {
 
450
                        windowTitle = _strdup(xfc->instance->settings->WindowTitle);
 
451
                }
 
452
                else if (xfc->instance->settings->ServerPort == 3389)
 
453
                {
 
454
                        windowTitle = malloc(1 + sizeof("FreeRDP: ") + strlen(xfc->instance->settings->ServerHostname));
 
455
                        sprintf(windowTitle, "FreeRDP: %s", xfc->instance->settings->ServerHostname);
 
456
                }
 
457
                else
 
458
                {
 
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);
 
461
                }
 
462
 
 
463
                xfc->window = xf_CreateDesktopWindow(xfc, windowTitle, width, height, xfc->settings->Decorations);
 
464
                free(windowTitle);
 
465
 
 
466
                if (xfc->fullscreen)
 
467
                        xf_SetWindowFullscreen(xfc, xfc->window, xfc->fullscreen);
 
468
 
 
469
                xfc->unobscured = (xevent.xvisibility.state == VisibilityUnobscured);
 
470
 
 
471
                XSetWMProtocols(xfc->display, xfc->window->handle, &(xfc->WM_DELETE_WINDOW), 1);
 
472
                xfc->drawable = xfc->window->handle;
 
473
        }
 
474
        else
 
475
        {
 
476
                xfc->drawable = DefaultRootWindow(xfc->display);
 
477
        }
 
478
}
 
479
 
 
480
void xf_toggle_fullscreen(xfContext* xfc)
 
481
{
 
482
        Pixmap contents = 0;
 
483
        WindowStateChangeEventArgs e;
 
484
 
 
485
        xf_lock_x11(xfc, TRUE);
 
486
 
 
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);
 
489
 
 
490
        XDestroyWindow(xfc->display, xfc->window->handle);
 
491
        xfc->fullscreen = (xfc->fullscreen) ? FALSE : TRUE;
 
492
        xf_create_window(xfc);
 
493
 
 
494
        XCopyArea(xfc->display, contents, xfc->primary, xfc->gc, 0, 0, xfc->width, xfc->height, 0, 0);
 
495
        XFreePixmap(xfc->display, contents);
 
496
 
 
497
        xf_unlock_x11(xfc, TRUE);
 
498
 
 
499
        EventArgsInit(&e, "xfreerdp");
 
500
        e.state = xfc->fullscreen ? FREERDP_WINDOW_STATE_FULLSCREEN : 0;
 
501
        PubSub_OnWindowStateChange(((rdpContext*) xfc)->pubSub, xfc, &e);
 
502
}
 
503
 
 
504
void xf_lock_x11(xfContext* xfc, BOOL display)
 
505
{
 
506
        if (!xfc->UseXThreads)
 
507
        {
 
508
                WaitForSingleObject(xfc->mutex, INFINITE);
 
509
        }
 
510
        else
 
511
        {
 
512
                if (display)
 
513
                        XLockDisplay(xfc->display);
 
514
        }
 
515
}
 
516
 
 
517
void xf_unlock_x11(xfContext* xfc, BOOL display)
 
518
{
 
519
        if (!xfc->UseXThreads)
 
520
        {
 
521
                ReleaseMutex(xfc->mutex);
 
522
        }
 
523
        else
 
524
        {
 
525
                if (display)
 
526
                        XUnlockDisplay(xfc->display);
 
527
        }
 
528
}
 
529
 
 
530
BOOL xf_get_pixmap_info(xfContext* xfc)
 
531
{
 
532
        int i;
 
533
        int vi_count;
 
534
        int pf_count;
 
535
        XVisualInfo* vi;
 
536
        XVisualInfo* vis;
 
537
        XVisualInfo template;
 
538
        XPixmapFormatValues* pf;
 
539
        XPixmapFormatValues* pfs;
 
540
        XWindowAttributes window_attributes;
 
541
 
 
542
        pfs = XListPixmapFormats(xfc->display, &pf_count);
 
543
 
 
544
        if (pfs == NULL)
 
545
        {
 
546
                fprintf(stderr, "xf_get_pixmap_info: XListPixmapFormats failed\n");
 
547
                return 1;
 
548
        }
 
549
 
 
550
        for (i = 0; i < pf_count; i++)
 
551
        {
 
552
                pf = pfs + i;
 
553
 
 
554
                if (pf->depth == xfc->depth)
 
555
                {
 
556
                        xfc->bpp = pf->bits_per_pixel;
 
557
                        xfc->scanline_pad = pf->scanline_pad;
 
558
                        break;
 
559
                }
 
560
        }
 
561
        XFree(pfs);
 
562
 
 
563
        ZeroMemory(&template, sizeof(template));
 
564
        template.class = TrueColor;
 
565
        template.screen = xfc->screen_number;
 
566
 
 
567
        if (XGetWindowAttributes(xfc->display, RootWindowOfScreen(xfc->screen), &window_attributes) == 0)
 
568
        {
 
569
                fprintf(stderr, "xf_get_pixmap_info: XGetWindowAttributes failed\n");
 
570
                return FALSE;
 
571
        }
 
572
 
 
573
        vis = XGetVisualInfo(xfc->display, VisualClassMask | VisualScreenMask, &template, &vi_count);
 
574
 
 
575
        if (vis == NULL)
 
576
        {
 
577
                fprintf(stderr, "xf_get_pixmap_info: XGetVisualInfo failed\n");
 
578
                return FALSE;
 
579
        }
 
580
 
 
581
        vi = NULL;
 
582
        for (i = 0; i < vi_count; i++)
 
583
        {
 
584
                vi = vis + i;
 
585
 
 
586
                if (vi->visual == window_attributes.visual)
 
587
                {
 
588
                        xfc->visual = vi->visual;
 
589
                        break;
 
590
                }
 
591
        }
 
592
 
 
593
        if (vi)
 
594
        {
 
595
                /*
 
596
                 * Detect if the server visual has an inverted colormap
 
597
                 * (BGR vs RGB, or red being the least significant byte)
 
598
                 */
 
599
 
 
600
                if (vi->red_mask & 0xFF)
 
601
                {
 
602
                        xfc->clrconv->invert = TRUE;
 
603
                }
 
604
        }
 
605
 
 
606
        XFree(vis);
 
607
 
 
608
        if ((xfc->visual == NULL) || (xfc->scanline_pad == 0))
 
609
        {
 
610
                return FALSE;
 
611
        }
 
612
 
 
613
        return TRUE;
 
614
}
 
615
 
 
616
static int (*_def_error_handler)(Display*, XErrorEvent*);
 
617
 
 
618
int xf_error_handler(Display* d, XErrorEvent* ev)
 
619
{
 
620
        char buf[256];
 
621
        int do_abort = TRUE;
 
622
 
 
623
        XGetErrorText(d, ev->error_code, buf, sizeof(buf));
 
624
        fprintf(stderr, "%s", buf);
 
625
 
 
626
        if (do_abort)
 
627
                abort();
 
628
 
 
629
        _def_error_handler(d, ev);
 
630
 
 
631
        return FALSE;
 
632
}
 
633
 
 
634
int _xf_error_handler(Display* d, XErrorEvent* ev)
 
635
{
 
636
        /*
 
637
         * ungrab the keyboard, in case a debugger is running in
 
638
         * another window. This make xf_error_handler() a potential
 
639
         * debugger breakpoint.
 
640
         */
 
641
        XUngrabKeyboard(d, CurrentTime);
 
642
        return xf_error_handler(d, ev);
 
643
}
 
644
 
 
645
/**
 
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.
 
648
 *
 
649
 * @param instance - pointer to the rdp_freerdp structure that contains the connection's parameters, and will
 
650
 * be filled with the appropriate informations.
 
651
 *
 
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.
 
654
 */
 
655
BOOL xf_pre_connect(freerdp* instance)
 
656
{
 
657
        rdpChannels* channels;
 
658
        rdpSettings* settings;
 
659
        xfContext* xfc = (xfContext*) instance->context;
 
660
 
 
661
        xfc->mutex = CreateMutex(NULL, FALSE, NULL);
 
662
 
 
663
        xfc->settings = instance->settings;
 
664
        xfc->instance = instance;
 
665
 
 
666
        settings = instance->settings;
 
667
        channels = instance->context->channels;
 
668
 
 
669
        PubSub_SubscribeChannelConnected(instance->context->pubSub,
 
670
                        (pChannelConnectedEventHandler) xf_OnChannelConnectedEventHandler);
 
671
 
 
672
        PubSub_SubscribeChannelDisconnected(instance->context->pubSub,
 
673
                        (pChannelDisconnectedEventHandler) xf_OnChannelDisconnectedEventHandler);
 
674
 
 
675
        freerdp_client_load_addins(channels, instance->settings);
 
676
 
 
677
        freerdp_channels_pre_connect(channels, instance);
 
678
 
 
679
        if (settings->AuthenticationOnly)
 
680
        {
 
681
                /* Check --authonly has a username and password. */
 
682
                if (settings->Username == NULL)
 
683
                {
 
684
                        fprintf(stderr, "--authonly, but no -u username. Please provide one.\n");
 
685
                        exit(1);
 
686
                }
 
687
                if (settings->Password == NULL)
 
688
                {
 
689
                        fprintf(stderr, "--authonly, but no -p password. Please provide one.\n");
 
690
                        exit(1);
 
691
                }
 
692
                fprintf(stderr, "%s:%d: Authentication only. Don't connect to X.\n", __FILE__, __LINE__);
 
693
                /* Avoid XWindows initialization and configuration below. */
 
694
                return TRUE;
 
695
        }
 
696
 
 
697
        xfc->UseXThreads = TRUE;
 
698
 
 
699
        if (xfc->UseXThreads)
 
700
        {
 
701
                if (!XInitThreads())
 
702
                {
 
703
                        fprintf(stderr, "warning: XInitThreads() failure\n");
 
704
                        xfc->UseXThreads = FALSE;
 
705
                }
 
706
        }
 
707
 
 
708
        xfc->display = XOpenDisplay(NULL);
 
709
 
 
710
        if (!xfc->display)
 
711
        {
 
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");
 
714
                return FALSE;
 
715
        }
 
716
 
 
717
        if (xfc->debug)
 
718
        {
 
719
                fprintf(stderr, "Enabling X11 debug mode.\n");
 
720
                XSynchronize(xfc->display, TRUE);
 
721
                _def_error_handler = XSetErrorHandler(_xf_error_handler);
 
722
        }
 
723
 
 
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);
 
731
 
 
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);
 
741
 
 
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);
 
745
 
 
746
        xf_kbd_init(xfc);
 
747
 
 
748
        xfc->clrconv = freerdp_clrconv_new(CLRCONV_ALPHA);
 
749
 
 
750
        instance->context->cache = cache_new(instance->settings);
 
751
 
 
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);
 
757
 
 
758
        xfc->complex_regions = TRUE;
 
759
        xfc->fullscreen = settings->Fullscreen;
 
760
        xfc->grab_keyboard = settings->GrabKeyboard;
 
761
        xfc->fullscreen_toggle = settings->ToggleFullscreen;
 
762
 
 
763
        xf_detect_monitors(xfc, settings);
 
764
 
 
765
        return TRUE;
 
766
}
 
767
 
 
768
/**
 
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.
 
772
 */
 
773
BOOL xf_post_connect(freerdp* instance)
 
774
{
 
775
        XGCValues gcv;
 
776
        rdpCache* cache;
 
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;
 
783
 
 
784
        cache = instance->context->cache;
 
785
        channels = instance->context->channels;
 
786
        settings = instance->settings;
 
787
 
 
788
        if (!xf_get_pixmap_info(xfc))
 
789
                return FALSE;
 
790
 
 
791
        xf_register_graphics(instance->context->graphics);
 
792
 
 
793
        if (xfc->settings->SoftwareGdi)
 
794
        {
 
795
                rdpGdi* gdi;
 
796
                UINT32 flags;
 
797
 
 
798
                flags = CLRCONV_ALPHA;
 
799
 
 
800
                if (xfc->bpp > 16)
 
801
                        flags |= CLRBUF_32BPP;
 
802
                else
 
803
                        flags |= CLRBUF_16BPP;
 
804
 
 
805
                gdi_init(instance, flags, NULL);
 
806
                gdi = instance->context->gdi;
 
807
                xfc->primary_buffer = gdi->primary_buffer;
 
808
 
 
809
                rfx_context = gdi->rfx_context;
 
810
        }
 
811
        else
 
812
        {
 
813
                xfc->srcBpp = instance->settings->ColorDepth;
 
814
                xf_gdi_register_update_callbacks(instance->update);
 
815
 
 
816
                xfc->hdc = gdi_CreateDC(xfc->clrconv, xfc->bpp);
 
817
 
 
818
                if (instance->settings->RemoteFxCodec)
 
819
                {
 
820
                        rfx_context = (void*) rfx_context_new();
 
821
                        xfc->rfx_context = rfx_context;
 
822
                }
 
823
 
 
824
                if (instance->settings->NSCodec)
 
825
                {
 
826
                        nsc_context = (void*) nsc_context_new();
 
827
                        xfc->nsc_context = nsc_context;
 
828
                }
 
829
        }
 
830
 
 
831
        xfc->originalWidth = settings->DesktopWidth;
 
832
        xfc->originalHeight = settings->DesktopHeight;
 
833
        xfc->currentWidth = xfc->originalWidth;
 
834
        xfc->currentHeight = xfc->originalWidth;
 
835
        xfc->scale = 1.0;
 
836
 
 
837
        xfc->width = settings->DesktopWidth;
 
838
        xfc->height = settings->DesktopHeight;
 
839
 
 
840
        if (settings->RemoteApplicationMode)
 
841
                xfc->remote_app = TRUE;
 
842
 
 
843
        xf_create_window(xfc);
 
844
 
 
845
        ZeroMemory(&gcv, sizeof(gcv));
 
846
        xfc->modifier_map = XGetModifierMapping(xfc->display);
 
847
 
 
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;
 
851
 
 
852
        xfc->bitmap_mono = XCreatePixmap(xfc->display, xfc->drawable, 8, 8, 1);
 
853
        xfc->gc_mono = XCreateGC(xfc->display, xfc->bitmap_mono, GCGraphicsExposures, &gcv);
 
854
 
 
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);
 
860
 
 
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);
 
863
 
 
864
        xfc->bmp_codec_none = (BYTE*) malloc(64 * 64 * 4);
 
865
 
 
866
        if (xfc->settings->SoftwareGdi)
 
867
        {
 
868
                instance->update->BeginPaint = xf_sw_begin_paint;
 
869
                instance->update->EndPaint = xf_sw_end_paint;
 
870
                instance->update->DesktopResize = xf_sw_desktop_resize;
 
871
        }
 
872
        else
 
873
        {
 
874
                instance->update->BeginPaint = xf_hw_begin_paint;
 
875
                instance->update->EndPaint = xf_hw_end_paint;
 
876
                instance->update->DesktopResize = xf_hw_desktop_resize;
 
877
        }
 
878
 
 
879
        pointer_cache_register_callbacks(instance->update);
 
880
 
 
881
        if (!xfc->settings->SoftwareGdi)
 
882
        {
 
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);
 
888
        }
 
889
 
 
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);
 
893
 
 
894
        freerdp_channels_post_connect(channels, instance);
 
895
 
 
896
        xf_tsmf_init(xfc, xv_port);
 
897
 
 
898
        xf_cliprdr_init(xfc, channels);
 
899
 
 
900
        EventArgsInit(&e, "xfreerdp");
 
901
        e.width = settings->DesktopWidth;
 
902
        e.height = settings->DesktopHeight;
 
903
        PubSub_OnResizeWindow(((rdpContext*) xfc)->pubSub, xfc, &e);
 
904
 
 
905
        return TRUE;
 
906
}
 
907
 
 
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.
 
919
 */
 
920
BOOL xf_authenticate(freerdp* instance, char** username, char** password, char** domain)
 
921
{
 
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));
 
925
 
 
926
        if (freerdp_passphrase_read("Password: ", *password, password_size, instance->settings->CredentialsFromStdin) == NULL)
 
927
                return FALSE;
 
928
 
 
929
        return TRUE;
 
930
}
 
931
 
 
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
 
937
 *  @param subject
 
938
 *  @param issuer
 
939
 *  @param fingerprint
 
940
 *  @return TRUE if the certificate is trusted. FALSE otherwise.
 
941
 */
 
942
BOOL xf_verify_certificate(freerdp* instance, char* subject, char* issuer, char* fingerprint)
 
943
{
 
944
        char answer;
 
945
 
 
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");
 
953
 
 
954
        while (1)
 
955
        {
 
956
                printf("Do you trust the above certificate? (Y/N) ");
 
957
                answer = fgetc(stdin);
 
958
 
 
959
                if (feof(stdin))
 
960
                {
 
961
                        printf("\nError: Could not read answer from stdin.");
 
962
                        if (instance->settings->CredentialsFromStdin)
 
963
                                printf(" - Run without parameter \"--from-stdin\" to set trust.");
 
964
                        printf("\n");
 
965
                        return FALSE;
 
966
                }
 
967
 
 
968
                if (answer == 'y' || answer == 'Y')
 
969
                {
 
970
                        return TRUE;
 
971
                }
 
972
                else if (answer == 'n' || answer == 'N')
 
973
                {
 
974
                        break;
 
975
                }
 
976
                printf("\n");
 
977
        }
 
978
 
 
979
        return FALSE;
 
980
}
 
981
 
 
982
int xf_logon_error_info(freerdp* instance, UINT32 data, UINT32 type)
 
983
{
 
984
        xfContext* xfc = (xfContext*) instance->context;
 
985
 
 
986
        xf_rail_disable_remoteapp_mode(xfc);
 
987
 
 
988
        return 1;
 
989
}
 
990
 
 
991
int xf_receive_channel_data(freerdp* instance, int channelId, BYTE* data, int size, int flags, int total_size)
 
992
{
 
993
        return freerdp_channels_data(instance, channelId, data, size, flags, total_size);
 
994
}
 
995
 
 
996
void xf_process_channel_event(rdpChannels* channels, freerdp* instance)
 
997
{
 
998
        xfContext* xfc;
 
999
        wMessage* event;
 
1000
 
 
1001
        xfc = (xfContext*) instance->context;
 
1002
 
 
1003
        event = freerdp_channels_pop_event(channels);
 
1004
 
 
1005
        if (event)
 
1006
        {
 
1007
                switch (GetMessageClass(event->id))
 
1008
                {
 
1009
                        case RailChannel_Class:
 
1010
                                xf_process_rail_event(xfc, channels, event);
 
1011
                                break;
 
1012
 
 
1013
                        case TsmfChannel_Class:
 
1014
                                xf_process_tsmf_event(xfc, event);
 
1015
                                break;
 
1016
 
 
1017
                        case CliprdrChannel_Class:
 
1018
                                xf_process_cliprdr_event(xfc, event);
 
1019
                                break;
 
1020
 
 
1021
                        case RdpeiChannel_Class:
 
1022
                                xf_process_rdpei_event(xfc, event);
 
1023
                                break;
 
1024
 
 
1025
                        default:
 
1026
                                break;
 
1027
                }
 
1028
 
 
1029
                freerdp_event_free(event);
 
1030
        }
 
1031
}
 
1032
 
 
1033
void xf_window_free(xfContext* xfc)
 
1034
{
 
1035
        rdpContext* context = (rdpContext*) xfc;
 
1036
 
 
1037
        if (xfc->modifier_map)
 
1038
        {
 
1039
                XFreeModifiermap(xfc->modifier_map);
 
1040
                xfc->modifier_map = NULL;
 
1041
        }
 
1042
 
 
1043
        if (xfc->gc)
 
1044
        {
 
1045
                XFreeGC(xfc->display, xfc->gc);
 
1046
                xfc->gc = 0;
 
1047
        }
 
1048
 
 
1049
        if (xfc->gc_mono)
 
1050
        {
 
1051
                XFreeGC(xfc->display, xfc->gc_mono);
 
1052
                xfc->gc_mono = 0;
 
1053
        }
 
1054
 
 
1055
        if (xfc->window)
 
1056
        {
 
1057
                xf_DestroyWindow(xfc, xfc->window);
 
1058
                xfc->window = NULL;
 
1059
        }
 
1060
 
 
1061
        if (xfc->primary)
 
1062
        {
 
1063
                XFreePixmap(xfc->display, xfc->primary);
 
1064
                xfc->primary = 0;
 
1065
        }
 
1066
 
 
1067
        if (xfc->bitmap_mono)
 
1068
        {
 
1069
                XFreePixmap(xfc->display, xfc->bitmap_mono);
 
1070
                xfc->bitmap_mono = 0;
 
1071
        }
 
1072
 
 
1073
        if (xfc->image)
 
1074
        {
 
1075
                xfc->image->data = NULL;
 
1076
                XDestroyImage(xfc->image);
 
1077
                xfc->image = NULL;
 
1078
        }
 
1079
 
 
1080
        if (context->cache)
 
1081
        {
 
1082
                cache_free(context->cache);
 
1083
                context->cache = NULL;
 
1084
        }
 
1085
 
 
1086
        if (context->rail)
 
1087
        {
 
1088
                rail_free(context->rail);
 
1089
                context->rail = NULL;
 
1090
        }
 
1091
 
 
1092
        if (xfc->rfx_context)
 
1093
        {
 
1094
                rfx_context_free(xfc->rfx_context);
 
1095
                xfc->rfx_context = NULL;
 
1096
        }
 
1097
 
 
1098
        if (xfc->nsc_context)
 
1099
        {
 
1100
                nsc_context_free(xfc->nsc_context);
 
1101
                xfc->nsc_context = NULL;
 
1102
        }
 
1103
 
 
1104
        if (xfc->clrconv)
 
1105
        {
 
1106
                freerdp_clrconv_free(xfc->clrconv);
 
1107
                xfc->clrconv = NULL;
 
1108
        }
 
1109
 
 
1110
        if (xfc->hdc)
 
1111
                gdi_DeleteDC(xfc->hdc);
 
1112
 
 
1113
        if (xfc->xv_context)
 
1114
        {
 
1115
                xf_tsmf_uninit(xfc);
 
1116
                xfc->xv_context = NULL;
 
1117
        }
 
1118
 
 
1119
        if (xfc->clipboard_context)
 
1120
        {
 
1121
                xf_cliprdr_uninit(xfc);
 
1122
                xfc->clipboard_context = NULL;
 
1123
        }
 
1124
}
 
1125
 
 
1126
void* xf_update_thread(void* arg)
 
1127
{
 
1128
        int status;
 
1129
        wMessage message;
 
1130
        wMessageQueue* queue;
 
1131
        freerdp* instance = (freerdp*) arg;
 
1132
 
 
1133
        status = 1;
 
1134
        queue = freerdp_get_message_queue(instance, FREERDP_UPDATE_MESSAGE_QUEUE);
 
1135
 
 
1136
        while (MessageQueue_Wait(queue))
 
1137
        {
 
1138
                while (MessageQueue_Peek(queue, &message, TRUE))
 
1139
                {
 
1140
                        status = freerdp_message_queue_process_message(instance, FREERDP_UPDATE_MESSAGE_QUEUE, &message);
 
1141
 
 
1142
                        if (!status)
 
1143
                                break;
 
1144
                }
 
1145
 
 
1146
                if (!status)
 
1147
                        break;
 
1148
        }
 
1149
 
 
1150
        return NULL;
 
1151
}
 
1152
 
 
1153
void* xf_input_thread(void* arg)
 
1154
{
 
1155
        xfContext* xfc;
 
1156
        HANDLE event;
 
1157
        XEvent xevent;
 
1158
        wMessageQueue* queue;
 
1159
        int pending_status = 1;
 
1160
        int process_status = 1;
 
1161
        freerdp* instance = (freerdp*) arg;
 
1162
 
 
1163
        xfc = (xfContext*) instance->context;
 
1164
 
 
1165
        event = CreateFileDescriptorEvent(NULL, FALSE, FALSE, xfc->xfds);
 
1166
 
 
1167
        while (WaitForSingleObject(event, INFINITE) == WAIT_OBJECT_0)
 
1168
        {
 
1169
                do
 
1170
                {
 
1171
                        xf_lock_x11(xfc, FALSE);
 
1172
 
 
1173
                        pending_status = XPending(xfc->display);
 
1174
 
 
1175
                        xf_unlock_x11(xfc, FALSE);
 
1176
 
 
1177
                        if (pending_status)
 
1178
                        {
 
1179
                                xf_lock_x11(xfc, FALSE);
 
1180
 
 
1181
                                ZeroMemory(&xevent, sizeof(xevent));
 
1182
                                XNextEvent(xfc->display, &xevent);
 
1183
                                process_status = xf_event_process(instance, &xevent);
 
1184
 
 
1185
                                xf_unlock_x11(xfc, FALSE);
 
1186
 
 
1187
                                if (!process_status)
 
1188
                                        break;
 
1189
                        }
 
1190
                }
 
1191
                while (pending_status);
 
1192
 
 
1193
                if (!process_status)
 
1194
                        break;
 
1195
        }
 
1196
 
 
1197
        queue = freerdp_get_message_queue(instance, FREERDP_INPUT_MESSAGE_QUEUE);
 
1198
        MessageQueue_PostQuit(queue, 0);
 
1199
 
 
1200
        return NULL;
 
1201
}
 
1202
 
 
1203
void* xf_channels_thread(void* arg)
 
1204
{
 
1205
        int status;
 
1206
        xfContext* xfc;
 
1207
        HANDLE event;
 
1208
        rdpChannels* channels;
 
1209
        freerdp* instance = (freerdp*) arg;
 
1210
 
 
1211
        xfc = (xfContext*) instance->context;
 
1212
 
 
1213
        channels = instance->context->channels;
 
1214
        event = freerdp_channels_get_event_handle(instance);
 
1215
 
 
1216
        while (WaitForSingleObject(event, INFINITE) == WAIT_OBJECT_0)
 
1217
        {
 
1218
                status = freerdp_channels_process_pending_messages(instance);
 
1219
                xf_process_channel_event(channels, instance);
 
1220
        }
 
1221
 
 
1222
        return NULL;
 
1223
}
 
1224
 
 
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)
 
1231
 */
 
1232
void* xf_thread(void* param)
 
1233
{
 
1234
        int i;
 
1235
        int fds;
 
1236
        xfContext* xfc;
 
1237
        int max_fds;
 
1238
        int rcount;
 
1239
        int wcount;
 
1240
        BOOL status;
 
1241
        int exit_code;
 
1242
        void* rfds[32];
 
1243
        void* wfds[32];
 
1244
        fd_set rfds_set;
 
1245
        fd_set wfds_set;
 
1246
        freerdp* instance;
 
1247
        int fd_input_event;
 
1248
        HANDLE input_event;
 
1249
        int select_status;
 
1250
        BOOL async_update;
 
1251
        BOOL async_input;
 
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;
 
1260
 
 
1261
        exit_code = 0;
 
1262
        input_event = NULL;
 
1263
 
 
1264
        instance = (freerdp*) param;
 
1265
 
 
1266
        ZeroMemory(rfds, sizeof(rfds));
 
1267
        ZeroMemory(wfds, sizeof(wfds));
 
1268
        ZeroMemory(&timeout, sizeof(struct timeval));
 
1269
 
 
1270
        status = freerdp_connect(instance);
 
1271
 
 
1272
        xfc = (xfContext*) instance->context;
 
1273
 
 
1274
        /* Connection succeeded. --authonly ? */
 
1275
        if (instance->settings->AuthenticationOnly)
 
1276
        {
 
1277
                freerdp_disconnect(instance);
 
1278
                fprintf(stderr, "%s:%d: Authentication only, exit status %d\n", __FILE__, __LINE__, !status);
 
1279
                exit(!status);
 
1280
        }
 
1281
 
 
1282
        if (!status)
 
1283
        {
 
1284
                exit_code = XF_EXIT_CONN_FAILED;
 
1285
                ExitThread(exit_code);
 
1286
        }
 
1287
 
 
1288
        channels = instance->context->channels;
 
1289
        settings = instance->context->settings;
 
1290
 
 
1291
        async_update = settings->AsyncUpdate;
 
1292
        async_input = settings->AsyncInput;
 
1293
        async_channels = settings->AsyncChannels;
 
1294
        async_transport = settings->AsyncTransport;
 
1295
 
 
1296
        if (async_update)
 
1297
        {
 
1298
                update_thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) xf_update_thread, instance, 0, NULL);
 
1299
        }
 
1300
 
 
1301
        if (async_input)
 
1302
        {
 
1303
                input_thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) xf_input_thread, instance, 0, NULL);
 
1304
        }
 
1305
 
 
1306
        if (async_channels)
 
1307
        {
 
1308
                channels_thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) xf_channels_thread, instance, 0, NULL);
 
1309
        }
 
1310
 
 
1311
        while (!xfc->disconnect && !freerdp_shall_disconnect(instance))
 
1312
        {
 
1313
                rcount = 0;
 
1314
                wcount = 0;
 
1315
 
 
1316
                if (freerdp_focus_required(instance))
 
1317
                {
 
1318
                        xf_kbd_focus_in(xfc);
 
1319
                }
 
1320
 
 
1321
                if (!async_transport)
 
1322
                {
 
1323
                        if (freerdp_get_fds(instance, rfds, &rcount, wfds, &wcount) != TRUE)
 
1324
                        {
 
1325
                                fprintf(stderr, "Failed to get FreeRDP file descriptor\n");
 
1326
                                exit_code = XF_EXIT_CONN_FAILED;
 
1327
                                break;
 
1328
                        }
 
1329
                }
 
1330
 
 
1331
                if (!async_channels)
 
1332
                {
 
1333
                        if (freerdp_channels_get_fds(channels, instance, rfds, &rcount, wfds, &wcount) != TRUE)
 
1334
                        {
 
1335
                                fprintf(stderr, "Failed to get channel manager file descriptor\n");
 
1336
                                exit_code = XF_EXIT_CONN_FAILED;
 
1337
                                break;
 
1338
                        }
 
1339
                }
 
1340
 
 
1341
                if (!async_input)
 
1342
                {
 
1343
                        if (xf_get_fds(instance, rfds, &rcount, wfds, &wcount) != TRUE)
 
1344
                        {
 
1345
                                fprintf(stderr, "Failed to get xfreerdp file descriptor\n");
 
1346
                                exit_code = XF_EXIT_CONN_FAILED;
 
1347
                                break;
 
1348
                        }
 
1349
                }
 
1350
                else
 
1351
                {
 
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;
 
1355
                }
 
1356
 
 
1357
                max_fds = 0;
 
1358
                FD_ZERO(&rfds_set);
 
1359
                FD_ZERO(&wfds_set);
 
1360
 
 
1361
                for (i = 0; i < rcount; i++)
 
1362
                {
 
1363
                        fds = (int)(long)(rfds[i]);
 
1364
 
 
1365
                        if (fds > max_fds)
 
1366
                                max_fds = fds;
 
1367
 
 
1368
                        FD_SET(fds, &rfds_set);
 
1369
                }
 
1370
 
 
1371
                if (max_fds == 0)
 
1372
                        break;
 
1373
 
 
1374
                timeout.tv_sec = 1;
 
1375
                timeout.tv_usec = 0;
 
1376
 
 
1377
                select_status = select(max_fds + 1, &rfds_set, NULL, NULL, &timeout);
 
1378
 
 
1379
                if (select_status == 0)
 
1380
                {
 
1381
                        continue; /* select timeout */
 
1382
                }
 
1383
                else if (select_status == -1)
 
1384
                {
 
1385
                        /* these are not really errors */
 
1386
                        if (!((errno == EAGAIN) || (errno == EWOULDBLOCK) ||
 
1387
                                (errno == EINPROGRESS) || (errno == EINTR))) /* signal occurred */
 
1388
                        {
 
1389
                                fprintf(stderr, "xfreerdp_run: select failed\n");
 
1390
                                break;
 
1391
                        }
 
1392
                }
 
1393
 
 
1394
                if (!async_transport)
 
1395
                {
 
1396
                        if (freerdp_check_fds(instance) != TRUE)
 
1397
                        {
 
1398
                                fprintf(stderr, "Failed to check FreeRDP file descriptor\n");
 
1399
                                break;
 
1400
                        }
 
1401
                }
 
1402
 
 
1403
                if (!async_channels)
 
1404
                {
 
1405
                        if (freerdp_channels_check_fds(channels, instance) != TRUE)
 
1406
                        {
 
1407
                                fprintf(stderr, "Failed to check channel manager file descriptor\n");
 
1408
                                break;
 
1409
                        }
 
1410
 
 
1411
                        xf_process_channel_event(channels, instance);
 
1412
                }
 
1413
 
 
1414
                if (!async_input)
 
1415
                {
 
1416
                        if (xf_process_x_events(instance) != TRUE)
 
1417
                        {
 
1418
                                fprintf(stderr, "Closed from X11\n");
 
1419
                                break;
 
1420
                        }
 
1421
                }
 
1422
                else
 
1423
                {
 
1424
                        if (WaitForSingleObject(input_event, 0) == WAIT_OBJECT_0)
 
1425
                        {
 
1426
                                if (!freerdp_message_queue_process_pending_messages(instance, FREERDP_INPUT_MESSAGE_QUEUE))
 
1427
                                {
 
1428
                                        fprintf(stderr, "User Disconnect\n");
 
1429
                                        xfc->disconnect = TRUE;
 
1430
                                        break;
 
1431
                                }
 
1432
                        }
 
1433
                }
 
1434
        }
 
1435
 
 
1436
        if (async_update)
 
1437
        {
 
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);
 
1442
        }
 
1443
 
 
1444
        FILE* fin = fopen("/tmp/tsmf.tid", "rt");
 
1445
 
 
1446
        if (fin)
 
1447
        {
 
1448
                FILE* fin1;
 
1449
                int thid = 0;
 
1450
                int timeout;
 
1451
 
 
1452
                fscanf(fin, "%d", &thid);
 
1453
                fclose(fin);
 
1454
 
 
1455
                pthread_kill((pthread_t) (size_t) thid, SIGUSR1);
 
1456
 
 
1457
                fin1 = fopen("/tmp/tsmf.tid", "rt");
 
1458
                timeout = 5;
 
1459
 
 
1460
                while (fin1)
 
1461
                {
 
1462
                        fclose(fin1);
 
1463
                        sleep(1);
 
1464
                        timeout--;
 
1465
 
 
1466
                        if (timeout <= 0)
 
1467
                        {
 
1468
                                unlink("/tmp/tsmf.tid");
 
1469
                                pthread_kill((pthread_t) (size_t) thid, SIGKILL);
 
1470
                                break;
 
1471
                        }
 
1472
 
 
1473
                        fin1 = fopen("/tmp/tsmf.tid", "rt");
 
1474
                }
 
1475
        }
 
1476
 
 
1477
        if (!exit_code)
 
1478
                exit_code = freerdp_error_info(instance);
 
1479
 
 
1480
        freerdp_channels_close(channels, instance);
 
1481
        freerdp_channels_free(channels);
 
1482
        freerdp_disconnect(instance);
 
1483
        gdi_free(instance);
 
1484
 
 
1485
        ExitThread(exit_code);
 
1486
}
 
1487
 
 
1488
DWORD xf_exit_code_from_disconnect_reason(DWORD reason)
 
1489
{
 
1490
        if (reason == 0 || (reason >= XF_EXIT_PARSE_ARGUMENTS && reason <= XF_EXIT_CONN_FAILED))
 
1491
                 return reason;
 
1492
 
 
1493
        /* License error set */
 
1494
        else if (reason >= 0x100 && reason <= 0x10A)
 
1495
                 reason -= 0x100 + XF_EXIT_LICENSE_INTERNAL;
 
1496
 
 
1497
        /* RDP protocol error set */
 
1498
        else if (reason >= 0x10c9 && reason <= 0x1193)
 
1499
                 reason = XF_EXIT_RDP;
 
1500
 
 
1501
        /* There's no need to test protocol-independent codes: they match */
 
1502
        else if (!(reason <= 0xB))
 
1503
                 reason = XF_EXIT_UNKNOWN;
 
1504
 
 
1505
        return reason;
 
1506
}
 
1507
 
 
1508
void xf_TerminateEventHandler(rdpContext* context, TerminateEventArgs* e)
 
1509
{
 
1510
        wMessageQueue* queue;
 
1511
        xfContext* xfc = (xfContext*) context;
 
1512
 
 
1513
        if (context->settings->AsyncInput)
 
1514
        {
 
1515
                queue = freerdp_get_message_queue(context->instance, FREERDP_INPUT_MESSAGE_QUEUE);
 
1516
 
 
1517
                if (queue)
 
1518
                        MessageQueue_PostQuit(queue, 0);
 
1519
        }
 
1520
        else
 
1521
        {
 
1522
                xfc->disconnect = TRUE;
 
1523
        }
 
1524
}
 
1525
 
 
1526
void xf_ParamChangeEventHandler(rdpContext* context, ParamChangeEventArgs* e)
 
1527
{
 
1528
        switch (e->id)
 
1529
        {
 
1530
                case FreeRDP_ScalingFactor:
 
1531
                        break;
 
1532
 
 
1533
                default:
 
1534
                        break;
 
1535
        }
 
1536
}
 
1537
 
 
1538
/**
 
1539
 * Client Interface
 
1540
 */
 
1541
 
 
1542
void xfreerdp_client_global_init()
 
1543
{
 
1544
        setlocale(LC_ALL, "");
 
1545
        freerdp_handle_signals();
 
1546
        freerdp_channels_global_init();
 
1547
}
 
1548
 
 
1549
void xfreerdp_client_global_uninit()
 
1550
{
 
1551
        freerdp_channels_global_uninit();
 
1552
}
 
1553
 
 
1554
int xfreerdp_client_start(rdpContext* context)
 
1555
{
 
1556
        xfContext* xfc = (xfContext*) context;
 
1557
 
 
1558
        rdpSettings* settings = context->settings;
 
1559
 
 
1560
        if (!settings->ServerHostname)
 
1561
        {
 
1562
                fprintf(stderr, "error: server hostname was not specified with /v:<server>[:port]\n");
 
1563
                return -1;
 
1564
        }
 
1565
 
 
1566
        xfc->thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) xf_thread, context->instance, 0, NULL);
 
1567
 
 
1568
        return 0;
 
1569
}
 
1570
 
 
1571
int xfreerdp_client_stop(rdpContext* context)
 
1572
{
 
1573
        xfContext* xfc = (xfContext*) context;
 
1574
 
 
1575
        if (context->settings->AsyncInput)
 
1576
        {
 
1577
                wMessageQueue* queue;
 
1578
                queue = freerdp_get_message_queue(context->instance, FREERDP_INPUT_MESSAGE_QUEUE);
 
1579
 
 
1580
                if (queue)
 
1581
                        MessageQueue_PostQuit(queue, 0);
 
1582
        }
 
1583
        else
 
1584
        {
 
1585
                xfc->disconnect = TRUE;
 
1586
        }
 
1587
 
 
1588
        return 0;
 
1589
}
 
1590
 
 
1591
double freerdp_client_get_scale(rdpContext* context)
 
1592
{
 
1593
        xfContext* xfc = (xfContext*) context;
 
1594
        return xfc->scale;
 
1595
}
 
1596
 
 
1597
void freerdp_client_reset_scale(rdpContext* context)
 
1598
{
 
1599
        ResizeWindowEventArgs e;
 
1600
        xfContext* xfc = (xfContext*) context;
 
1601
 
 
1602
        xfc->scale = 1.0;
 
1603
        XResizeWindow(xfc->display, xfc->window->handle, xfc->originalWidth * xfc->scale, xfc->originalHeight * xfc->scale);
 
1604
 
 
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);
 
1609
 
 
1610
        xf_draw_screen_scaled(xfc);
 
1611
}
 
1612
 
 
1613
int xfreerdp_client_new(freerdp* instance, rdpContext* context)
 
1614
{
 
1615
        xfContext* xfc;
 
1616
        rdpSettings* settings;
 
1617
 
 
1618
        xfc = (xfContext*) instance->context;
 
1619
 
 
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;
 
1626
 
 
1627
        context->channels = freerdp_channels_new();
 
1628
 
 
1629
        settings = instance->settings;
 
1630
        xfc->settings = instance->context->settings;
 
1631
 
 
1632
        settings->OsMajorType = OSMAJORTYPE_UNIX;
 
1633
        settings->OsMinorType = OSMINORTYPE_NATIVE_XSERVER;
 
1634
 
 
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;
 
1659
 
 
1660
        PubSub_SubscribeTerminate(context->pubSub, (pTerminateEventHandler) xf_TerminateEventHandler);
 
1661
        PubSub_SubscribeParamChange(context->pubSub, (pParamChangeEventHandler) xf_ParamChangeEventHandler);
 
1662
 
 
1663
        return 0;
 
1664
}
 
1665
 
 
1666
void xfreerdp_client_free(freerdp* instance, rdpContext* context)
 
1667
{
 
1668
        xfContext* xfc = (xfContext*) context;
 
1669
 
 
1670
        if (context)
 
1671
        {
 
1672
                xf_window_free(xfc);
 
1673
 
 
1674
                if (xfc->bmp_codec_none)
 
1675
                        free(xfc->bmp_codec_none);
 
1676
 
 
1677
                if (xfc->display)
 
1678
                        XCloseDisplay(xfc->display);
 
1679
        }
 
1680
}
 
1681
 
 
1682
int RdpClientEntry(RDP_CLIENT_ENTRY_POINTS* pEntryPoints)
 
1683
{
 
1684
        pEntryPoints->Version = 1;
 
1685
        pEntryPoints->Size = sizeof(RDP_CLIENT_ENTRY_POINTS_V1);
 
1686
 
 
1687
        pEntryPoints->GlobalInit = xfreerdp_client_global_init;
 
1688
        pEntryPoints->GlobalUninit = xfreerdp_client_global_uninit;
 
1689
 
 
1690
        pEntryPoints->ContextSize = sizeof(xfContext);
 
1691
        pEntryPoints->ClientNew = xfreerdp_client_new;
 
1692
        pEntryPoints->ClientFree = xfreerdp_client_free;
 
1693
 
 
1694
        pEntryPoints->ClientStart = xfreerdp_client_start;
 
1695
        pEntryPoints->ClientStop = xfreerdp_client_stop;
 
1696
 
 
1697
        return 0;
 
1698
}