7c2879ae41cbd6b81800deedde03785ec145b606
[platform/upstream/freerdp.git] / client / X11 / xfreerdp.c
1 /**
2  * FreeRDP: A Remote Desktop Protocol Client
3  * X11 Client
4  *
5  * Copyright 2011 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 #include <X11/Xlib.h>
21 #include <X11/Xutil.h>
22
23 #ifdef WITH_XINERAMA
24 #include <X11/extensions/Xinerama.h>
25 #endif
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <errno.h>
30 #include <pthread.h>
31 #include <locale.h>
32 #include <sys/select.h>
33 #include <freerdp/rfx/rfx.h>
34 #include <freerdp/utils/args.h>
35 #include <freerdp/utils/memory.h>
36 #include <freerdp/utils/semaphore.h>
37 #include <freerdp/utils/event.h>
38 #include <freerdp/constants.h>
39 #include <freerdp/plugins/cliprdr.h>
40 #include <freerdp/rail.h>
41
42 #include "xf_gdi.h"
43 #include "xf_rail.h"
44 #include "xf_event.h"
45 #include "xf_monitor.h"
46 #include "xf_keyboard.h"
47
48 #include "xfreerdp.h"
49
50 freerdp_sem g_sem;
51 static int g_thread_count = 0;
52
53 struct thread_data
54 {
55         freerdp* instance;
56 };
57
58 void xf_begin_paint(rdpUpdate* update)
59 {
60         GDI* gdi;
61         gdi = GET_GDI(update);
62         gdi->primary->hdc->hwnd->invalid->null = 1;
63         gdi->primary->hdc->hwnd->ninvalid = 0;
64 }
65
66 void xf_end_paint(rdpUpdate* update)
67 {
68         GDI* gdi;
69         xfInfo* xfi;
70         sint32 x, y;
71         uint32 w, h;
72
73         gdi = GET_GDI(update);
74         xfi = GET_XFI(update);
75
76         if (xfi->remote_app != True)
77         {
78                 if (xfi->complex_regions != True)
79                 {
80                         if (gdi->primary->hdc->hwnd->invalid->null)
81                                 return;
82
83                         x = gdi->primary->hdc->hwnd->invalid->x;
84                         y = gdi->primary->hdc->hwnd->invalid->y;
85                         w = gdi->primary->hdc->hwnd->invalid->w;
86                         h = gdi->primary->hdc->hwnd->invalid->h;
87
88                         XPutImage(xfi->display, xfi->primary, xfi->gc, xfi->image, x, y, x, y, w, h);
89                         XCopyArea(xfi->display, xfi->primary, xfi->window->handle, xfi->gc, x, y, w, h, x, y);
90                 }
91                 else
92                 {
93                         int i;
94                         int ninvalid;
95                         HGDI_RGN cinvalid;
96
97                         if (gdi->primary->hdc->hwnd->ninvalid < 1)
98                                 return;
99
100                         ninvalid = gdi->primary->hdc->hwnd->ninvalid;
101                         cinvalid = gdi->primary->hdc->hwnd->cinvalid;
102
103                         for (i = 0; i < ninvalid; i++)
104                         {
105                                 x = cinvalid[i].x;
106                                 y = cinvalid[i].y;
107                                 w = cinvalid[i].w;
108                                 h = cinvalid[i].h;
109
110                                 XPutImage(xfi->display, xfi->primary, xfi->gc, xfi->image, x, y, x, y, w, h);
111                                 XCopyArea(xfi->display, xfi->primary, xfi->window->handle, xfi->gc, x, y, w, h, x, y);
112                         }
113
114                         XFlush(xfi->display);
115                 }
116         }
117         else
118         {
119                 if (gdi->primary->hdc->hwnd->invalid->null)
120                         return;
121
122                 x = gdi->primary->hdc->hwnd->invalid->x;
123                 y = gdi->primary->hdc->hwnd->invalid->y;
124                 w = gdi->primary->hdc->hwnd->invalid->w;
125                 h = gdi->primary->hdc->hwnd->invalid->h;
126
127                 xf_rail_paint(xfi, update->rail, x, y, x + w - 1, y + h - 1);
128         }
129 }
130
131 void xf_desktop_resize(rdpUpdate* update)
132 {
133         GDI* gdi;
134         xfInfo* xfi;
135         boolean same;
136         rdpSettings* settings;
137
138         xfi = GET_XFI(update);
139         gdi = GET_GDI(update);
140         settings = xfi->instance->settings;
141
142         if (!xfi->fullscreen)
143         {
144                 xfi->width = settings->width;
145                 xfi->height = settings->height;
146
147                 if (xfi->window)
148                         xf_ResizeDesktopWindow(xfi, xfi->window, settings->width, settings->height);
149
150                 if (xfi->primary)
151                 {
152                         same = (xfi->primary == xfi->drawing ? True : False);
153                         XFreePixmap(xfi->display, xfi->primary);
154                         xfi->primary = XCreatePixmap(xfi->display, DefaultRootWindow(xfi->display),
155                                 xfi->width, xfi->height, xfi->depth);
156                         if (same)
157                                 xfi->drawing = xfi->primary;
158                 }
159
160                 if (gdi)
161                         gdi_resize(gdi, xfi->width, xfi->height);
162
163                 if (gdi && xfi->image)
164                 {
165                         xfi->image->data = NULL;
166                         XDestroyImage(xfi->image);
167                         xfi->image = XCreateImage(xfi->display, xfi->visual, xfi->depth, ZPixmap, 0,
168                                         (char*) gdi->primary_buffer, gdi->width, gdi->height, xfi->scanline_pad, 0);
169                 }
170         }
171 }
172
173 boolean xf_get_fds(freerdp* instance, void** rfds, int* rcount, void** wfds, int* wcount)
174 {
175         xfInfo* xfi = GET_XFI(instance);
176
177         rfds[*rcount] = (void*)(long)(xfi->xfds);
178         (*rcount)++;
179
180         return True;
181 }
182
183 boolean xf_check_fds(freerdp* instance, fd_set* set)
184 {
185         XEvent xevent;
186         xfInfo* xfi = GET_XFI(instance);
187
188         while (XPending(xfi->display))
189         {
190                 memset(&xevent, 0, sizeof(xevent));
191                 XNextEvent(xfi->display, &xevent);
192
193                 if (xf_event_process(instance, &xevent) != True)
194                         return False;
195         }
196
197         return True;
198 }
199
200 void xf_toggle_fullscreen(xfInfo* xfi)
201 {
202         Pixmap contents = 0;
203
204         contents = XCreatePixmap(xfi->display, xfi->window->handle, xfi->width, xfi->height, xfi->depth);
205         XCopyArea(xfi->display, xfi->primary, contents, xfi->gc, 0, 0, xfi->width, xfi->height, 0, 0);
206
207         xfi->fullscreen = (xfi->fullscreen) ? False : True;
208
209         XCopyArea(xfi->display, contents, xfi->primary, xfi->gc, 0, 0, xfi->width, xfi->height, 0, 0);
210         XFreePixmap(xfi->display, contents);
211 }
212
213 boolean xf_get_pixmap_info(xfInfo* xfi)
214 {
215         int i;
216         int vi_count;
217         int pf_count;
218         XVisualInfo* vi;
219         XVisualInfo* vis;
220         XVisualInfo template;
221         XPixmapFormatValues* pf;
222         XPixmapFormatValues* pfs;
223
224         pfs = XListPixmapFormats(xfi->display, &pf_count);
225
226         if (pfs == NULL)
227         {
228                 printf("xf_get_pixmap_info: XListPixmapFormats failed\n");
229                 return 1;
230         }
231
232         for (i = 0; i < pf_count; i++)
233         {
234                 pf = pfs + i;
235
236                 if (pf->depth == xfi->depth)
237                 {
238                         xfi->bpp = pf->bits_per_pixel;
239                         xfi->scanline_pad = pf->scanline_pad;
240                         break;
241                 }
242         }
243         XFree(pfs);
244
245         memset(&template, 0, sizeof(template));
246         template.class = TrueColor;
247         template.screen = xfi->screen_number;
248
249         vis = XGetVisualInfo(xfi->display, VisualClassMask | VisualScreenMask, &template, &vi_count);
250
251         if (vis == NULL)
252         {
253                 printf("xf_get_pixmap_info: XGetVisualInfo failed\n");
254                 return False;
255         }
256
257         for (i = 0; i < vi_count; i++)
258         {
259                 vi = vis + i;
260
261                 if (vi->depth == xfi->depth)
262                 {
263                         xfi->visual = vi->visual;
264                         break;
265                 }
266         }
267         XFree(vis);
268
269         if ((xfi->visual == NULL) || (xfi->scanline_pad == 0))
270         {
271                 return False;
272         }
273
274         return True;
275 }
276
277 boolean xf_pre_connect(freerdp* instance)
278 {
279         xfInfo* xfi;
280         rdpSettings* settings;
281
282         xfi = (xfInfo*) xzalloc(sizeof(xfInfo));
283         SET_XFI(instance, xfi);
284
285         xfi->instance = instance;
286
287         settings = instance->settings;
288
289         settings->order_support[NEG_DSTBLT_INDEX] = True;
290         settings->order_support[NEG_PATBLT_INDEX] = True;
291         settings->order_support[NEG_SCRBLT_INDEX] = True;
292         settings->order_support[NEG_OPAQUE_RECT_INDEX] = True;
293         settings->order_support[NEG_DRAWNINEGRID_INDEX] = False;
294         settings->order_support[NEG_MULTIDSTBLT_INDEX] = False;
295         settings->order_support[NEG_MULTIPATBLT_INDEX] = False;
296         settings->order_support[NEG_MULTISCRBLT_INDEX] = False;
297         settings->order_support[NEG_MULTIOPAQUERECT_INDEX] = True;
298         settings->order_support[NEG_MULTI_DRAWNINEGRID_INDEX] = False;
299         settings->order_support[NEG_LINETO_INDEX] = True;
300         settings->order_support[NEG_POLYLINE_INDEX] = True;
301         settings->order_support[NEG_MEMBLT_INDEX] = False;
302         settings->order_support[NEG_MEM3BLT_INDEX] = False;
303         settings->order_support[NEG_MEMBLT_V2_INDEX] = False;
304         settings->order_support[NEG_MEM3BLT_V2_INDEX] = False;
305         settings->order_support[NEG_SAVEBITMAP_INDEX] = True;
306         settings->order_support[NEG_GLYPH_INDEX_INDEX] = True;
307         settings->order_support[NEG_FAST_INDEX_INDEX] = True;
308         settings->order_support[NEG_FAST_GLYPH_INDEX] = True;
309         settings->order_support[NEG_POLYGON_SC_INDEX] = False;
310         settings->order_support[NEG_POLYGON_CB_INDEX] = False;
311         settings->order_support[NEG_ELLIPSE_SC_INDEX] = False;
312         settings->order_support[NEG_ELLIPSE_CB_INDEX] = False;
313
314         freerdp_chanman_pre_connect(GET_CHANMAN(instance), instance);
315
316         xfi->display = XOpenDisplay(NULL);
317
318         if (xfi->display == NULL)
319         {
320                 printf("xf_pre_connect: failed to open display: %s\n", XDisplayName(NULL));
321                 return False;
322         }
323
324         xfi->_NET_WM_ICON = XInternAtom(xfi->display, "_NET_WM_ICON", False);
325         xfi->_MOTIF_WM_HINTS = XInternAtom(xfi->display, "_MOTIF_WM_HINTS", False);
326         xfi->_NET_CURRENT_DESKTOP = XInternAtom(xfi->display, "_NET_CURRENT_DESKTOP", False);
327         xfi->_NET_WORKAREA = XInternAtom(xfi->display, "_NET_WORKAREA", False);
328         xfi->_NET_WM_STATE = XInternAtom(xfi->display, "_NET_WM_STATE", False);
329         xfi->_NET_WM_STATE_FULLSCREEN = XInternAtom(xfi->display, "_NET_WM_STATE_FULLSCREEN", False);
330         xfi->_NET_WM_WINDOW_TYPE = XInternAtom(xfi->display, "_NET_WM_WINDOW_TYPE", False);
331
332         xfi->_NET_WM_WINDOW_TYPE_NORMAL = XInternAtom(xfi->display, "_NET_WM_WINDOW_TYPE_NORMAL", False);
333         xfi->_NET_WM_WINDOW_TYPE_DIALOG = XInternAtom(xfi->display, "_NET_WM_WINDOW_TYPE_DIALOG", False);
334         xfi->_NET_WM_WINDOW_TYPE_UTILITY = XInternAtom(xfi->display, "_NET_WM_WINDOW_TYPE_UTILITY", False);
335         xfi->_NET_WM_STATE_SKIP_TASKBAR = XInternAtom(xfi->display, "_NET_WM_STATE_SKIP_TASKBAR", False);
336         xfi->_NET_WM_STATE_SKIP_PAGER = XInternAtom(xfi->display, "_NET_WM_STATE_SKIP_PAGER", False);
337
338         xfi->_NET_WM_MOVERESIZE = XInternAtom(xfi->display, "_NET_WM_MOVERESIZE", False);
339
340         xf_kbd_init(xfi);
341
342         xfi->clrconv = xnew(CLRCONV);
343         xfi->clrconv->palette = NULL;
344         xfi->clrconv->alpha = 1;
345         xfi->clrconv->invert = 0;
346         xfi->clrconv->rgb555 = 0;
347
348         xfi->cache = cache_new(instance->settings);
349
350         xfi->xfds = ConnectionNumber(xfi->display);
351         xfi->screen_number = DefaultScreen(xfi->display);
352         xfi->screen = ScreenOfDisplay(xfi->display, xfi->screen_number);
353         xfi->depth = DefaultDepthOfScreen(xfi->screen);
354         xfi->big_endian = (ImageByteOrder(xfi->display) == MSBFirst);
355
356         xfi->mouse_motion = settings->mouse_motion;
357         xfi->complex_regions = True;
358         xfi->decoration = settings->decorations;
359         xfi->remote_app = settings->remote_app;
360         xfi->fullscreen = settings->fullscreen;
361
362         xf_detect_monitors(xfi, settings);
363
364         return True;
365 }
366
367 boolean xf_post_connect(freerdp* instance)
368 {
369         GDI* gdi;
370         xfInfo* xfi;
371         XEvent xevent;
372         XGCValues gcv;
373         Atom kill_atom;
374         Atom protocol_atom;
375
376         xfi = GET_XFI(instance);
377         SET_XFI(instance->update, xfi);
378
379         if (xf_get_pixmap_info(xfi) != True)
380                 return False;
381
382         gdi_init(instance, CLRCONV_ALPHA | CLRBUF_32BPP);
383         gdi = GET_GDI(instance->update);
384
385         if (instance->settings->sw_gdi != True)
386         {
387                 xfi->srcBpp = instance->settings->color_depth;
388                 xf_gdi_register_update_callbacks(instance->update);
389
390                 if (instance->settings->rfx_codec)
391                         xfi->rfx_context = (void*) rfx_context_new();
392         }
393
394         if (xfi->fullscreen)
395                 xfi->decoration = False;
396
397         xfi->width = xfi->fullscreen ? WidthOfScreen(xfi->screen) : instance->settings->width;
398         xfi->height = xfi->fullscreen ? HeightOfScreen(xfi->screen) : instance->settings->height;
399
400         xfi->attribs.background_pixel = BlackPixelOfScreen(xfi->screen);
401         xfi->attribs.border_pixel = WhitePixelOfScreen(xfi->screen);
402         xfi->attribs.backing_store = xfi->primary ? NotUseful : Always;
403         xfi->attribs.override_redirect = xfi->fullscreen;
404         xfi->attribs.colormap = xfi->colormap;
405
406         if (xfi->remote_app != True)
407         {
408                 xfi->window = xf_CreateDesktopWindow(xfi, "xfreerdp", xfi->width, xfi->height);
409
410                 xf_SetWindowDecorations(xfi, xfi->window, xfi->decoration);
411
412                 if (xfi->fullscreen)
413                         xf_SetWindowFullscreen(xfi, xfi->window, xfi->fullscreen);
414
415                 /* wait for VisibilityNotify */
416                 do
417                 {
418                         XMaskEvent(xfi->display, VisibilityChangeMask, &xevent);
419                 }
420                 while (xevent.type != VisibilityNotify);
421
422                 xfi->unobscured = (xevent.xvisibility.state == VisibilityUnobscured);
423
424                 protocol_atom = XInternAtom(xfi->display, "WM_PROTOCOLS", True);
425                 kill_atom = XInternAtom(xfi->display, "WM_DELETE_WINDOW", True);
426                 XSetWMProtocols(xfi->display, xfi->window->handle, &kill_atom, 1);
427         }
428
429         memset(&gcv, 0, sizeof(gcv));
430         xfi->modifier_map = XGetModifierMapping(xfi->display);
431
432         xfi->gc = XCreateGC(xfi->display, DefaultRootWindow(xfi->display), GCGraphicsExposures, &gcv);
433         xfi->primary = XCreatePixmap(xfi->display, DefaultRootWindow(xfi->display), xfi->width, xfi->height, xfi->depth);
434         xfi->drawing = xfi->primary;
435
436         xfi->bitmap_mono = XCreatePixmap(xfi->display, xfi->window->handle, 8, 8, 1);
437         xfi->gc_mono = XCreateGC(xfi->display, xfi->bitmap_mono, GCGraphicsExposures, &gcv);
438
439         XSetForeground(xfi->display, xfi->gc, BlackPixelOfScreen(xfi->screen));
440         XFillRectangle(xfi->display, xfi->primary, xfi->gc, 0, 0, xfi->width, xfi->height);
441
442         xfi->image = XCreateImage(xfi->display, xfi->visual, xfi->depth, ZPixmap, 0,
443                         (char*) gdi->primary_buffer, gdi->width, gdi->height, xfi->scanline_pad, 0);
444
445         instance->update->BeginPaint = xf_begin_paint;
446         instance->update->EndPaint = xf_end_paint;
447         instance->update->DesktopResize = xf_desktop_resize;
448
449         xfi->rail = rail_new(instance->settings);
450         instance->update->rail = (void*) xfi->rail;
451         rail_register_update_callbacks(xfi->rail, instance->update);
452         xf_rail_register_callbacks(xfi, xfi->rail);
453
454         freerdp_chanman_post_connect(GET_CHANMAN(instance), instance);
455
456         return True;
457 }
458
459 int xf_process_ui_args(rdpSettings* settings, const char* opt, const char* val, void* user_data)
460 {
461         if (strcmp("--kbd-list", opt) == 0)
462         {
463                 int i;
464                 rdpKeyboardLayout* layouts;
465
466                 layouts = freerdp_kbd_get_layouts(RDP_KEYBOARD_LAYOUT_TYPE_STANDARD);
467                 printf("\nKeyboard Layouts\n");
468                 for (i = 0; layouts[i].code; i++)
469                         printf("0x%08X\t%s\n", layouts[i].code, layouts[i].name);
470                 free(layouts);
471
472                 layouts = freerdp_kbd_get_layouts(RDP_KEYBOARD_LAYOUT_TYPE_VARIANT);
473                 printf("\nKeyboard Layout Variants\n");
474                 for (i = 0; layouts[i].code; i++)
475                         printf("0x%08X\t%s\n", layouts[i].code, layouts[i].name);
476                 free(layouts);
477
478                 layouts = freerdp_kbd_get_layouts(RDP_KEYBOARD_LAYOUT_TYPE_IME);
479                 printf("\nKeyboard Input Method Editors (IMEs)\n");
480                 for (i = 0; layouts[i].code; i++)
481                         printf("0x%08X\t%s\n", layouts[i].code, layouts[i].name);
482                 free(layouts);
483
484                 exit(0);
485         }
486
487         return 1;
488 }
489
490 int xf_process_plugin_args(rdpSettings* settings, const char* name, RDP_PLUGIN_DATA* plugin_data, void* user_data)
491 {
492         rdpChanMan* chanman = (rdpChanMan*) user_data;
493
494         printf("loading plugin %s\n", name);
495         freerdp_chanman_load_plugin(chanman, settings, name, plugin_data);
496
497         return 1;
498 }
499
500 int xf_receive_channel_data(freerdp* instance, int channelId, uint8* data, int size, int flags, int total_size)
501 {
502         return freerdp_chanman_data(instance, channelId, data, size, flags, total_size);
503 }
504
505 void xf_process_cb_sync_event(rdpChanMan* chanman, freerdp* instance)
506 {
507         RDP_EVENT* event;
508         RDP_CB_FORMAT_LIST_EVENT* format_list_event;
509
510         event = freerdp_event_new(RDP_EVENT_CLASS_CLIPRDR, RDP_EVENT_TYPE_CB_FORMAT_LIST, NULL, NULL);
511
512         format_list_event = (RDP_CB_FORMAT_LIST_EVENT*) event;
513         format_list_event->num_formats = 0;
514
515         freerdp_chanman_send_event(chanman, event);
516 }
517
518 void xf_process_channel_event(rdpChanMan* chanman, freerdp* instance)
519 {
520         xfInfo* xfi;
521         RDP_EVENT* event;
522
523         xfi = GET_XFI(instance);
524
525         event = freerdp_chanman_pop_event(chanman);
526
527         if (event)
528         {
529                 switch (event->event_class)
530                 {
531                         case RDP_EVENT_CLASS_RAIL:
532                                 xf_process_rail_event(xfi, chanman, event);
533                                 break;
534
535                         default:
536                                 break;
537                 }
538
539                 switch (event->event_type)
540                 {
541                         case RDP_EVENT_TYPE_CB_SYNC:
542                                 xf_process_cb_sync_event(chanman, instance);
543                                 break;
544                         default:
545                                 break;
546                 }
547                 freerdp_event_free(event);
548         }
549 }
550
551 void xf_window_free(xfInfo* xfi)
552 {
553         XFreeModifiermap(xfi->modifier_map);
554         xfi->modifier_map = 0;
555
556         XFreeGC(xfi->display, xfi->gc);
557         xfi->gc = 0;
558
559         xf_DestroyWindow(xfi, xfi->window);
560         xfi->window = NULL;
561
562         if (xfi->primary)
563         {
564                 XFreePixmap(xfi->display, xfi->primary);
565                 xfi->primary = 0;
566         }
567
568         if (xfi->image)
569         {
570                 xfi->image->data = NULL;
571                 XDestroyImage(xfi->image);
572                 xfi->image = NULL;
573         }
574
575         if (xfi->cache)
576         {
577                 cache_free(xfi->cache);
578                 xfi->cache = NULL;
579         }
580
581         xfree(xfi->clrconv);
582         rail_free(xfi->rail);
583 }
584
585 void xf_free(xfInfo* xfi)
586 {
587         xf_window_free(xfi);
588         XCloseDisplay(xfi->display);
589         xfree(xfi);
590 }
591
592 int xfreerdp_run(freerdp* instance)
593 {
594         int i;
595         int fds;
596         xfInfo* xfi;
597         int max_fds;
598         int rcount;
599         int wcount;
600         void* rfds[32];
601         void* wfds[32];
602         fd_set rfds_set;
603         fd_set wfds_set;
604         rdpChanMan* chanman;
605
606         memset(rfds, 0, sizeof(rfds));
607         memset(wfds, 0, sizeof(wfds));
608
609         if (!instance->Connect(instance))
610                 return 0;
611
612         xfi = GET_XFI(instance);
613         chanman = GET_CHANMAN(instance);
614
615         while (1)
616         {
617                 rcount = 0;
618                 wcount = 0;
619
620                 if (instance->GetFileDescriptor(instance, rfds, &rcount, wfds, &wcount) != True)
621                 {
622                         printf("Failed to get FreeRDP file descriptor\n");
623                         break;
624                 }
625                 if (freerdp_chanman_get_fds(chanman, instance, rfds, &rcount, wfds, &wcount) != True)
626                 {
627                         printf("Failed to get channel manager file descriptor\n");
628                         break;
629                 }
630                 if (xf_get_fds(instance, rfds, &rcount, wfds, &wcount) != True)
631                 {
632                         printf("Failed to get xfreerdp file descriptor\n");
633                         break;
634                 }
635
636                 max_fds = 0;
637                 FD_ZERO(&rfds_set);
638                 FD_ZERO(&wfds_set);
639
640                 for (i = 0; i < rcount; i++)
641                 {
642                         fds = (int)(long)(rfds[i]);
643
644                         if (fds > max_fds)
645                                 max_fds = fds;
646
647                         FD_SET(fds, &rfds_set);
648                 }
649
650                 if (max_fds == 0)
651                         break;
652
653                 if (select(max_fds + 1, &rfds_set, &wfds_set, NULL, NULL) == -1)
654                 {
655                         /* these are not really errors */
656                         if (!((errno == EAGAIN) ||
657                                 (errno == EWOULDBLOCK) ||
658                                 (errno == EINPROGRESS) ||
659                                 (errno == EINTR))) /* signal occurred */
660                         {
661                                 printf("xfreerdp_run: select failed\n");
662                                 break;
663                         }
664                 }
665
666                 if (instance->CheckFileDescriptor(instance) != True)
667                 {
668                         printf("Failed to check FreeRDP file descriptor\n");
669                         break;
670                 }
671                 if (xf_check_fds(instance, &rfds_set) != True)
672                 {
673                         printf("Failed to check xfreerdp file descriptor\n");
674                         break;
675                 }
676                 if (freerdp_chanman_check_fds(chanman, instance) != True)
677                 {
678                         printf("Failed to check channel manager file descriptor\n");
679                         break;
680                 }
681                 xf_process_channel_event(chanman, instance);
682         }
683
684         freerdp_chanman_close(chanman, instance);
685         freerdp_chanman_free(chanman);
686         instance->Disconnect(instance);
687         gdi_free(instance);
688         freerdp_free(instance);
689         xf_free(xfi);
690
691         return 0;
692 }
693
694 void* thread_func(void* param)
695 {
696         struct thread_data* data;
697         data = (struct thread_data*) param;
698
699         xfreerdp_run(data->instance);
700
701         xfree(data);
702
703         pthread_detach(pthread_self());
704
705         g_thread_count--;
706
707         if (g_thread_count < 1)
708                 freerdp_sem_signal(g_sem);
709
710         return NULL;
711 }
712
713 int main(int argc, char* argv[])
714 {
715         pthread_t thread;
716         freerdp* instance;
717         struct thread_data* data;
718         rdpChanMan* chanman;
719
720         setlocale(LC_ALL, "");
721
722         freerdp_chanman_global_init();
723
724         g_sem = freerdp_sem_new(1);
725
726         instance = freerdp_new();
727         instance->PreConnect = xf_pre_connect;
728         instance->PostConnect = xf_post_connect;
729         instance->ReceiveChannelData = xf_receive_channel_data;
730
731         chanman = freerdp_chanman_new();
732         SET_CHANMAN(instance, chanman);
733
734         instance->settings->sw_gdi = True;
735
736         if (freerdp_parse_args(instance->settings, argc, argv,
737                         xf_process_plugin_args, chanman, xf_process_ui_args, NULL) < 0)
738                 return 1;
739
740         data = (struct thread_data*) xzalloc(sizeof(struct thread_data));
741         data->instance = instance;
742
743         g_thread_count++;
744         pthread_create(&thread, 0, thread_func, data);
745
746         while (g_thread_count > 0)
747         {
748                 freerdp_sem_wait(g_sem);
749         }
750
751         freerdp_chanman_global_uninit();
752
753         return 0;
754 }