2 * FreeRDP: A Remote Desktop Protocol Client
5 * Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
21 #include <X11/Xutil.h>
24 #include <X11/extensions/Xinerama.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>
45 #include "xf_monitor.h"
46 #include "xf_keyboard.h"
51 static int g_thread_count = 0;
58 void xf_begin_paint(rdpUpdate* update)
61 gdi = GET_GDI(update);
62 gdi->primary->hdc->hwnd->invalid->null = 1;
63 gdi->primary->hdc->hwnd->ninvalid = 0;
66 void xf_end_paint(rdpUpdate* update)
73 gdi = GET_GDI(update);
74 xfi = GET_XFI(update);
76 if (xfi->remote_app != True)
78 if (xfi->complex_regions != True)
80 if (gdi->primary->hdc->hwnd->invalid->null)
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;
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);
97 if (gdi->primary->hdc->hwnd->ninvalid < 1)
100 ninvalid = gdi->primary->hdc->hwnd->ninvalid;
101 cinvalid = gdi->primary->hdc->hwnd->cinvalid;
103 for (i = 0; i < ninvalid; i++)
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);
114 XFlush(xfi->display);
119 if (gdi->primary->hdc->hwnd->invalid->null)
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;
127 xf_rail_paint(xfi, update->rail, x, y, x + w - 1, y + h - 1);
131 void xf_desktop_resize(rdpUpdate* update)
136 rdpSettings* settings;
138 xfi = GET_XFI(update);
139 gdi = GET_GDI(update);
140 settings = xfi->instance->settings;
142 if (!xfi->fullscreen)
144 xfi->width = settings->width;
145 xfi->height = settings->height;
148 xf_ResizeDesktopWindow(xfi, xfi->window, settings->width, settings->height);
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);
157 xfi->drawing = xfi->primary;
161 gdi_resize(gdi, xfi->width, xfi->height);
163 if (gdi && xfi->image)
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);
173 boolean xf_get_fds(freerdp* instance, void** rfds, int* rcount, void** wfds, int* wcount)
175 xfInfo* xfi = GET_XFI(instance);
177 rfds[*rcount] = (void*)(long)(xfi->xfds);
183 boolean xf_check_fds(freerdp* instance, fd_set* set)
186 xfInfo* xfi = GET_XFI(instance);
188 while (XPending(xfi->display))
190 memset(&xevent, 0, sizeof(xevent));
191 XNextEvent(xfi->display, &xevent);
193 if (xf_event_process(instance, &xevent) != True)
200 void xf_toggle_fullscreen(xfInfo* xfi)
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);
207 xfi->fullscreen = (xfi->fullscreen) ? False : True;
209 XCopyArea(xfi->display, contents, xfi->primary, xfi->gc, 0, 0, xfi->width, xfi->height, 0, 0);
210 XFreePixmap(xfi->display, contents);
213 boolean xf_get_pixmap_info(xfInfo* xfi)
220 XVisualInfo template;
221 XPixmapFormatValues* pf;
222 XPixmapFormatValues* pfs;
224 pfs = XListPixmapFormats(xfi->display, &pf_count);
228 printf("xf_get_pixmap_info: XListPixmapFormats failed\n");
232 for (i = 0; i < pf_count; i++)
236 if (pf->depth == xfi->depth)
238 xfi->bpp = pf->bits_per_pixel;
239 xfi->scanline_pad = pf->scanline_pad;
245 memset(&template, 0, sizeof(template));
246 template.class = TrueColor;
247 template.screen = xfi->screen_number;
249 vis = XGetVisualInfo(xfi->display, VisualClassMask | VisualScreenMask, &template, &vi_count);
253 printf("xf_get_pixmap_info: XGetVisualInfo failed\n");
257 for (i = 0; i < vi_count; i++)
261 if (vi->depth == xfi->depth)
263 xfi->visual = vi->visual;
269 if ((xfi->visual == NULL) || (xfi->scanline_pad == 0))
277 boolean xf_pre_connect(freerdp* instance)
280 rdpSettings* settings;
282 xfi = (xfInfo*) xzalloc(sizeof(xfInfo));
283 SET_XFI(instance, xfi);
285 xfi->instance = instance;
287 settings = instance->settings;
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;
314 freerdp_chanman_pre_connect(GET_CHANMAN(instance), instance);
316 xfi->display = XOpenDisplay(NULL);
318 if (xfi->display == NULL)
320 printf("xf_pre_connect: failed to open display: %s\n", XDisplayName(NULL));
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);
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);
338 xfi->_NET_WM_MOVERESIZE = XInternAtom(xfi->display, "_NET_WM_MOVERESIZE", False);
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;
348 xfi->cache = cache_new(instance->settings);
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);
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;
362 xf_detect_monitors(xfi, settings);
367 boolean xf_post_connect(freerdp* instance)
376 xfi = GET_XFI(instance);
377 SET_XFI(instance->update, xfi);
379 if (xf_get_pixmap_info(xfi) != True)
382 gdi_init(instance, CLRCONV_ALPHA | CLRBUF_32BPP);
383 gdi = GET_GDI(instance->update);
385 if (instance->settings->sw_gdi != True)
387 xfi->srcBpp = instance->settings->color_depth;
388 xf_gdi_register_update_callbacks(instance->update);
390 if (instance->settings->rfx_codec)
391 xfi->rfx_context = (void*) rfx_context_new();
395 xfi->decoration = False;
397 xfi->width = xfi->fullscreen ? WidthOfScreen(xfi->screen) : instance->settings->width;
398 xfi->height = xfi->fullscreen ? HeightOfScreen(xfi->screen) : instance->settings->height;
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;
406 if (xfi->remote_app != True)
408 xfi->window = xf_CreateDesktopWindow(xfi, "xfreerdp", xfi->width, xfi->height);
410 xf_SetWindowDecorations(xfi, xfi->window, xfi->decoration);
413 xf_SetWindowFullscreen(xfi, xfi->window, xfi->fullscreen);
415 /* wait for VisibilityNotify */
418 XMaskEvent(xfi->display, VisibilityChangeMask, &xevent);
420 while (xevent.type != VisibilityNotify);
422 xfi->unobscured = (xevent.xvisibility.state == VisibilityUnobscured);
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);
429 memset(&gcv, 0, sizeof(gcv));
430 xfi->modifier_map = XGetModifierMapping(xfi->display);
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;
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);
439 XSetForeground(xfi->display, xfi->gc, BlackPixelOfScreen(xfi->screen));
440 XFillRectangle(xfi->display, xfi->primary, xfi->gc, 0, 0, xfi->width, xfi->height);
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);
445 instance->update->BeginPaint = xf_begin_paint;
446 instance->update->EndPaint = xf_end_paint;
447 instance->update->DesktopResize = xf_desktop_resize;
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);
454 freerdp_chanman_post_connect(GET_CHANMAN(instance), instance);
459 int xf_process_ui_args(rdpSettings* settings, const char* opt, const char* val, void* user_data)
461 if (strcmp("--kbd-list", opt) == 0)
464 rdpKeyboardLayout* layouts;
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);
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);
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);
490 int xf_process_plugin_args(rdpSettings* settings, const char* name, RDP_PLUGIN_DATA* plugin_data, void* user_data)
492 rdpChanMan* chanman = (rdpChanMan*) user_data;
494 printf("loading plugin %s\n", name);
495 freerdp_chanman_load_plugin(chanman, settings, name, plugin_data);
500 int xf_receive_channel_data(freerdp* instance, int channelId, uint8* data, int size, int flags, int total_size)
502 return freerdp_chanman_data(instance, channelId, data, size, flags, total_size);
505 void xf_process_cb_sync_event(rdpChanMan* chanman, freerdp* instance)
508 RDP_CB_FORMAT_LIST_EVENT* format_list_event;
510 event = freerdp_event_new(RDP_EVENT_CLASS_CLIPRDR, RDP_EVENT_TYPE_CB_FORMAT_LIST, NULL, NULL);
512 format_list_event = (RDP_CB_FORMAT_LIST_EVENT*) event;
513 format_list_event->num_formats = 0;
515 freerdp_chanman_send_event(chanman, event);
518 void xf_process_channel_event(rdpChanMan* chanman, freerdp* instance)
523 xfi = GET_XFI(instance);
525 event = freerdp_chanman_pop_event(chanman);
529 switch (event->event_class)
531 case RDP_EVENT_CLASS_RAIL:
532 xf_process_rail_event(xfi, chanman, event);
539 switch (event->event_type)
541 case RDP_EVENT_TYPE_CB_SYNC:
542 xf_process_cb_sync_event(chanman, instance);
547 freerdp_event_free(event);
551 void xf_window_free(xfInfo* xfi)
553 XFreeModifiermap(xfi->modifier_map);
554 xfi->modifier_map = 0;
556 XFreeGC(xfi->display, xfi->gc);
559 xf_DestroyWindow(xfi, xfi->window);
564 XFreePixmap(xfi->display, xfi->primary);
570 xfi->image->data = NULL;
571 XDestroyImage(xfi->image);
577 cache_free(xfi->cache);
582 rail_free(xfi->rail);
585 void xf_free(xfInfo* xfi)
588 XCloseDisplay(xfi->display);
592 int xfreerdp_run(freerdp* instance)
606 memset(rfds, 0, sizeof(rfds));
607 memset(wfds, 0, sizeof(wfds));
609 if (!instance->Connect(instance))
612 xfi = GET_XFI(instance);
613 chanman = GET_CHANMAN(instance);
620 if (instance->GetFileDescriptor(instance, rfds, &rcount, wfds, &wcount) != True)
622 printf("Failed to get FreeRDP file descriptor\n");
625 if (freerdp_chanman_get_fds(chanman, instance, rfds, &rcount, wfds, &wcount) != True)
627 printf("Failed to get channel manager file descriptor\n");
630 if (xf_get_fds(instance, rfds, &rcount, wfds, &wcount) != True)
632 printf("Failed to get xfreerdp file descriptor\n");
640 for (i = 0; i < rcount; i++)
642 fds = (int)(long)(rfds[i]);
647 FD_SET(fds, &rfds_set);
653 if (select(max_fds + 1, &rfds_set, &wfds_set, NULL, NULL) == -1)
655 /* these are not really errors */
656 if (!((errno == EAGAIN) ||
657 (errno == EWOULDBLOCK) ||
658 (errno == EINPROGRESS) ||
659 (errno == EINTR))) /* signal occurred */
661 printf("xfreerdp_run: select failed\n");
666 if (instance->CheckFileDescriptor(instance) != True)
668 printf("Failed to check FreeRDP file descriptor\n");
671 if (xf_check_fds(instance, &rfds_set) != True)
673 printf("Failed to check xfreerdp file descriptor\n");
676 if (freerdp_chanman_check_fds(chanman, instance) != True)
678 printf("Failed to check channel manager file descriptor\n");
681 xf_process_channel_event(chanman, instance);
684 freerdp_chanman_close(chanman, instance);
685 freerdp_chanman_free(chanman);
686 instance->Disconnect(instance);
688 freerdp_free(instance);
694 void* thread_func(void* param)
696 struct thread_data* data;
697 data = (struct thread_data*) param;
699 xfreerdp_run(data->instance);
703 pthread_detach(pthread_self());
707 if (g_thread_count < 1)
708 freerdp_sem_signal(g_sem);
713 int main(int argc, char* argv[])
717 struct thread_data* data;
720 setlocale(LC_ALL, "");
722 freerdp_chanman_global_init();
724 g_sem = freerdp_sem_new(1);
726 instance = freerdp_new();
727 instance->PreConnect = xf_pre_connect;
728 instance->PostConnect = xf_post_connect;
729 instance->ReceiveChannelData = xf_receive_channel_data;
731 chanman = freerdp_chanman_new();
732 SET_CHANMAN(instance, chanman);
734 instance->settings->sw_gdi = True;
736 if (freerdp_parse_args(instance->settings, argc, argv,
737 xf_process_plugin_args, chanman, xf_process_ui_args, NULL) < 0)
740 data = (struct thread_data*) xzalloc(sizeof(struct thread_data));
741 data->instance = instance;
744 pthread_create(&thread, 0, thread_func, data);
746 while (g_thread_count > 0)
748 freerdp_sem_wait(g_sem);
751 freerdp_chanman_global_uninit();