2011-09-14 00:30:16 +04:00
|
|
|
/**
|
2012-10-09 07:02:04 +04:00
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
2011-09-14 00:30:16 +04:00
|
|
|
* X11 Peer
|
|
|
|
*
|
|
|
|
* Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2012-08-15 01:20:53 +04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2011-09-14 00:30:16 +04:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2012-01-08 06:36:12 +04:00
|
|
|
#include <unistd.h>
|
2012-01-10 07:08:25 +04:00
|
|
|
#include <sys/ipc.h>
|
|
|
|
#include <sys/shm.h>
|
2011-10-27 21:41:39 +04:00
|
|
|
#include <X11/Xlib.h>
|
|
|
|
#include <X11/Xutil.h>
|
|
|
|
#include <sys/select.h>
|
2012-08-15 01:20:53 +04:00
|
|
|
|
2012-11-22 05:21:08 +04:00
|
|
|
#include <winpr/crt.h>
|
2013-03-22 23:52:43 +04:00
|
|
|
#include <winpr/file.h>
|
|
|
|
#include <winpr/path.h>
|
2012-12-14 09:58:48 +04:00
|
|
|
#include <winpr/synch.h>
|
2013-04-24 02:17:01 +04:00
|
|
|
#include <winpr/thread.h>
|
2012-11-22 05:21:08 +04:00
|
|
|
|
2012-09-18 01:38:39 +04:00
|
|
|
#include <freerdp/freerdp.h>
|
2012-01-08 06:36:12 +04:00
|
|
|
#include <freerdp/codec/color.h>
|
2013-03-22 23:52:43 +04:00
|
|
|
#include <freerdp/locale/keyboard.h>
|
2011-09-14 00:30:16 +04:00
|
|
|
|
2012-01-31 06:35:15 +04:00
|
|
|
#include "xf_input.h"
|
2011-09-14 03:03:06 +04:00
|
|
|
#include "xf_encode.h"
|
|
|
|
|
2013-04-24 02:17:01 +04:00
|
|
|
#include "makecert.h"
|
|
|
|
|
2011-09-14 00:30:16 +04:00
|
|
|
#include "xf_peer.h"
|
|
|
|
|
2012-01-03 07:33:35 +04:00
|
|
|
#ifdef WITH_XDAMAGE
|
|
|
|
|
|
|
|
void xf_xdamage_init(xfInfo* xfi)
|
|
|
|
{
|
2012-01-10 07:08:25 +04:00
|
|
|
Bool pixmaps;
|
2012-01-03 07:33:35 +04:00
|
|
|
int damage_event;
|
|
|
|
int damage_error;
|
|
|
|
int major, minor;
|
|
|
|
XGCValues values;
|
|
|
|
|
2012-12-13 03:02:15 +04:00
|
|
|
if (xfi->use_xshm)
|
2012-01-10 07:08:25 +04:00
|
|
|
{
|
2012-12-13 03:02:15 +04:00
|
|
|
if (XShmQueryExtension(xfi->display) != False)
|
|
|
|
{
|
|
|
|
XShmQueryVersion(xfi->display, &major, &minor, &pixmaps);
|
2012-01-10 07:08:25 +04:00
|
|
|
|
2012-12-13 03:02:15 +04:00
|
|
|
if (pixmaps != True)
|
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "XShmQueryVersion failed\n");
|
2012-12-13 03:02:15 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2012-01-10 07:08:25 +04:00
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "XShmQueryExtension failed\n");
|
2012-01-10 07:08:25 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-03 07:33:35 +04:00
|
|
|
if (XDamageQueryExtension(xfi->display, &damage_event, &damage_error) == 0)
|
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "XDamageQueryExtension failed\n");
|
2012-01-03 07:33:35 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
XDamageQueryVersion(xfi->display, &major, &minor);
|
|
|
|
|
|
|
|
if (XDamageQueryVersion(xfi->display, &major, &minor) == 0)
|
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "XDamageQueryVersion failed\n");
|
2012-01-03 07:33:35 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (major < 1)
|
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "XDamageQueryVersion failed: major:%d minor:%d\n", major, minor);
|
2012-01-03 07:33:35 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
xfi->xdamage_notify_event = damage_event + XDamageNotify;
|
2012-01-10 07:08:25 +04:00
|
|
|
xfi->xdamage = XDamageCreate(xfi->display, xfi->root_window, XDamageReportDeltaRectangles);
|
2012-01-03 07:33:35 +04:00
|
|
|
|
|
|
|
if (xfi->xdamage == None)
|
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "XDamageCreate failed\n");
|
2012-01-03 07:33:35 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef WITH_XFIXES
|
|
|
|
xfi->xdamage_region = XFixesCreateRegion(xfi->display, NULL, 0);
|
|
|
|
|
|
|
|
if (xfi->xdamage_region == None)
|
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "XFixesCreateRegion failed\n");
|
2012-01-03 07:33:35 +04:00
|
|
|
XDamageDestroy(xfi->display, xfi->xdamage);
|
|
|
|
xfi->xdamage = None;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
values.subwindow_mode = IncludeInferiors;
|
2012-01-10 07:08:25 +04:00
|
|
|
xfi->xdamage_gc = XCreateGC(xfi->display, xfi->root_window, GCSubwindowMode, &values);
|
2012-02-05 03:18:46 +04:00
|
|
|
XSetFunction(xfi->display, xfi->xdamage_gc, GXcopy);
|
2012-01-03 07:33:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2012-01-10 07:08:25 +04:00
|
|
|
void xf_xshm_init(xfInfo* xfi)
|
|
|
|
{
|
|
|
|
xfi->fb_shm_info.shmid = -1;
|
|
|
|
xfi->fb_shm_info.shmaddr = (char*) -1;
|
|
|
|
|
|
|
|
xfi->fb_image = XShmCreateImage(xfi->display, xfi->visual, xfi->depth,
|
|
|
|
ZPixmap, NULL, &(xfi->fb_shm_info), xfi->width, xfi->height);
|
|
|
|
|
2013-02-17 22:21:52 +04:00
|
|
|
if (!xfi->fb_image)
|
2012-01-10 07:08:25 +04:00
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "XShmCreateImage failed\n");
|
2012-01-10 07:08:25 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
xfi->fb_shm_info.shmid = shmget(IPC_PRIVATE,
|
|
|
|
xfi->fb_image->bytes_per_line * xfi->fb_image->height, IPC_CREAT | 0600);
|
|
|
|
|
|
|
|
if (xfi->fb_shm_info.shmid == -1)
|
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "shmget failed\n");
|
2012-01-10 07:08:25 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
xfi->fb_shm_info.readOnly = False;
|
|
|
|
xfi->fb_shm_info.shmaddr = shmat(xfi->fb_shm_info.shmid, 0, 0);
|
|
|
|
xfi->fb_image->data = xfi->fb_shm_info.shmaddr;
|
|
|
|
|
|
|
|
if (xfi->fb_shm_info.shmaddr == ((char*) -1))
|
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "shmat failed\n");
|
2012-01-10 07:08:25 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
XShmAttach(xfi->display, &(xfi->fb_shm_info));
|
|
|
|
XSync(xfi->display, False);
|
|
|
|
|
|
|
|
shmctl(xfi->fb_shm_info.shmid, IPC_RMID, 0);
|
|
|
|
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "display: %p root_window: %p width: %d height: %d depth: %d\n",
|
2013-01-20 00:16:28 +04:00
|
|
|
xfi->display, (void*) xfi->root_window, xfi->fb_image->width, xfi->fb_image->height, xfi->fb_image->depth);
|
2012-12-13 03:02:15 +04:00
|
|
|
|
2012-01-10 07:08:25 +04:00
|
|
|
xfi->fb_pixmap = XShmCreatePixmap(xfi->display,
|
|
|
|
xfi->root_window, xfi->fb_image->data, &(xfi->fb_shm_info),
|
|
|
|
xfi->fb_image->width, xfi->fb_image->height, xfi->fb_image->depth);
|
|
|
|
}
|
|
|
|
|
2011-10-18 11:10:12 +04:00
|
|
|
xfInfo* xf_info_init()
|
2011-09-14 00:30:16 +04:00
|
|
|
{
|
2011-10-18 11:10:12 +04:00
|
|
|
int i;
|
|
|
|
xfInfo* xfi;
|
|
|
|
int pf_count;
|
|
|
|
int vi_count;
|
|
|
|
XVisualInfo* vi;
|
|
|
|
XVisualInfo* vis;
|
|
|
|
XVisualInfo template;
|
|
|
|
XPixmapFormatValues* pf;
|
|
|
|
XPixmapFormatValues* pfs;
|
|
|
|
|
2012-11-22 05:21:08 +04:00
|
|
|
xfi = (xfInfo*) malloc(sizeof(xfInfo));
|
|
|
|
ZeroMemory(xfi, sizeof(xfInfo));
|
2011-10-18 11:10:12 +04:00
|
|
|
|
2012-12-13 03:02:15 +04:00
|
|
|
/**
|
|
|
|
* Recent X11 servers drop support for shared pixmaps
|
|
|
|
* To see if your X11 server supports shared pixmaps, use:
|
|
|
|
* xdpyinfo -ext MIT-SHM | grep "shared pixmaps"
|
|
|
|
*/
|
|
|
|
xfi->use_xshm = FALSE;
|
|
|
|
|
2013-04-24 02:17:01 +04:00
|
|
|
setenv("DISPLAY", ":0", 1); /* Set DISPLAY variable if not already set */
|
|
|
|
|
2013-02-07 06:57:49 +04:00
|
|
|
if (!XInitThreads())
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "warning: XInitThreads() failure\n");
|
2011-10-18 11:10:12 +04:00
|
|
|
|
2013-02-07 06:57:49 +04:00
|
|
|
xfi->display = XOpenDisplay(NULL);
|
2012-01-08 06:36:12 +04:00
|
|
|
|
2013-02-17 22:21:52 +04:00
|
|
|
if (!xfi->display)
|
2011-10-20 16:25:25 +04:00
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "failed to open display: %s\n", XDisplayName(NULL));
|
2011-10-20 16:25:25 +04:00
|
|
|
exit(1);
|
|
|
|
}
|
2011-10-18 11:10:12 +04:00
|
|
|
|
2012-02-05 03:18:46 +04:00
|
|
|
xfi->xfds = ConnectionNumber(xfi->display);
|
2011-10-18 11:10:12 +04:00
|
|
|
xfi->number = DefaultScreen(xfi->display);
|
|
|
|
xfi->screen = ScreenOfDisplay(xfi->display, xfi->number);
|
|
|
|
xfi->depth = DefaultDepthOfScreen(xfi->screen);
|
|
|
|
xfi->width = WidthOfScreen(xfi->screen);
|
|
|
|
xfi->height = HeightOfScreen(xfi->screen);
|
2012-01-10 07:08:25 +04:00
|
|
|
xfi->root_window = DefaultRootWindow(xfi->display);
|
2011-10-18 11:10:12 +04:00
|
|
|
|
|
|
|
pfs = XListPixmapFormats(xfi->display, &pf_count);
|
|
|
|
|
2013-02-17 22:21:52 +04:00
|
|
|
if (!pfs)
|
2011-10-18 11:10:12 +04:00
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "XListPixmapFormats failed\n");
|
2011-10-18 11:10:12 +04:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < pf_count; i++)
|
|
|
|
{
|
|
|
|
pf = pfs + i;
|
|
|
|
|
|
|
|
if (pf->depth == xfi->depth)
|
|
|
|
{
|
|
|
|
xfi->bpp = pf->bits_per_pixel;
|
|
|
|
xfi->scanline_pad = pf->scanline_pad;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
XFree(pfs);
|
|
|
|
|
2013-02-17 20:03:35 +04:00
|
|
|
ZeroMemory(&template, sizeof(template));
|
2011-10-18 11:10:12 +04:00
|
|
|
template.class = TrueColor;
|
|
|
|
template.screen = xfi->number;
|
|
|
|
|
|
|
|
vis = XGetVisualInfo(xfi->display, VisualClassMask | VisualScreenMask, &template, &vi_count);
|
2011-09-14 00:30:16 +04:00
|
|
|
|
2011-10-18 11:10:12 +04:00
|
|
|
if (vis == NULL)
|
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "XGetVisualInfo failed\n");
|
2011-10-18 11:10:12 +04:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < vi_count; i++)
|
|
|
|
{
|
|
|
|
vi = vis + i;
|
|
|
|
|
|
|
|
if (vi->depth == xfi->depth)
|
|
|
|
{
|
|
|
|
xfi->visual = vi->visual;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
XFree(vis);
|
2011-09-14 00:30:16 +04:00
|
|
|
|
2012-02-02 07:05:01 +04:00
|
|
|
xfi->clrconv = freerdp_clrconv_new(CLRCONV_ALPHA | CLRCONV_INVERT);
|
2011-09-14 00:30:16 +04:00
|
|
|
|
2012-01-10 07:08:25 +04:00
|
|
|
XSelectInput(xfi->display, xfi->root_window, SubstructureNotifyMask);
|
2012-01-03 07:33:35 +04:00
|
|
|
|
2011-12-05 18:20:59 +04:00
|
|
|
#ifdef WITH_XDAMAGE
|
2012-01-03 07:33:35 +04:00
|
|
|
xf_xdamage_init(xfi);
|
2012-01-10 07:08:25 +04:00
|
|
|
#endif
|
|
|
|
|
2012-12-13 03:02:15 +04:00
|
|
|
if (xfi->use_xshm)
|
|
|
|
xf_xshm_init(xfi);
|
2012-01-10 07:08:25 +04:00
|
|
|
|
2012-02-01 06:05:47 +04:00
|
|
|
xfi->bytesPerPixel = 4;
|
2011-11-08 02:20:00 +04:00
|
|
|
|
2012-02-19 07:04:28 +04:00
|
|
|
freerdp_keyboard_init(0);
|
2012-01-09 05:32:50 +04:00
|
|
|
|
2011-10-18 11:10:12 +04:00
|
|
|
return xfi;
|
|
|
|
}
|
2011-09-14 00:30:16 +04:00
|
|
|
|
2011-10-18 11:10:12 +04:00
|
|
|
void xf_peer_context_new(freerdp_peer* client, xfPeerContext* context)
|
2011-09-14 00:30:16 +04:00
|
|
|
{
|
2011-10-18 11:10:12 +04:00
|
|
|
context->info = xf_info_init();
|
2011-10-29 18:01:50 +04:00
|
|
|
context->rfx_context = rfx_context_new();
|
2011-10-18 11:10:12 +04:00
|
|
|
context->rfx_context->mode = RLGR3;
|
2011-11-08 02:20:00 +04:00
|
|
|
context->rfx_context->width = context->info->width;
|
|
|
|
context->rfx_context->height = context->info->height;
|
2012-01-10 07:08:25 +04:00
|
|
|
|
2012-03-13 15:02:19 +04:00
|
|
|
rfx_context_set_pixel_format(context->rfx_context, RDP_PIXEL_FORMAT_B8G8R8A8);
|
2011-10-18 11:10:12 +04:00
|
|
|
|
|
|
|
context->s = stream_new(65536);
|
|
|
|
}
|
2011-09-14 00:30:16 +04:00
|
|
|
|
2011-10-18 11:10:12 +04:00
|
|
|
void xf_peer_context_free(freerdp_peer* client, xfPeerContext* context)
|
|
|
|
{
|
|
|
|
if (context)
|
2011-09-14 00:30:16 +04:00
|
|
|
{
|
2011-10-18 11:10:12 +04:00
|
|
|
stream_free(context->s);
|
|
|
|
rfx_context_free(context->rfx_context);
|
2011-09-14 00:30:16 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-18 11:10:12 +04:00
|
|
|
void xf_peer_init(freerdp_peer* client)
|
|
|
|
{
|
2012-02-02 07:05:01 +04:00
|
|
|
xfInfo* xfi;
|
2012-01-08 06:36:12 +04:00
|
|
|
xfPeerContext* xfp;
|
|
|
|
|
2011-10-30 09:43:04 +04:00
|
|
|
client->context_size = sizeof(xfPeerContext);
|
2011-10-18 11:10:12 +04:00
|
|
|
client->ContextNew = (psPeerContextNew) xf_peer_context_new;
|
|
|
|
client->ContextFree = (psPeerContextFree) xf_peer_context_free;
|
|
|
|
freerdp_peer_context_new(client);
|
2012-01-08 06:36:12 +04:00
|
|
|
|
|
|
|
xfp = (xfPeerContext*) client->context;
|
|
|
|
|
2013-02-17 21:23:25 +04:00
|
|
|
xfp->fps = 16;
|
2012-01-08 06:36:12 +04:00
|
|
|
xfp->thread = 0;
|
|
|
|
xfp->activations = 0;
|
2013-02-17 20:59:29 +04:00
|
|
|
|
|
|
|
xfp->queue = MessageQueue_New();
|
2012-02-02 07:05:01 +04:00
|
|
|
|
|
|
|
xfi = xfp->info;
|
|
|
|
xfp->hdc = gdi_CreateDC(xfi->clrconv, xfi->bpp);
|
2012-01-08 06:36:12 +04:00
|
|
|
|
2012-01-08 06:57:42 +04:00
|
|
|
pthread_mutex_init(&(xfp->mutex), NULL);
|
2011-10-18 11:10:12 +04:00
|
|
|
}
|
|
|
|
|
2013-03-21 23:19:33 +04:00
|
|
|
wStream* xf_peer_stream_init(xfPeerContext* context)
|
2011-09-14 00:30:16 +04:00
|
|
|
{
|
2011-10-18 11:10:12 +04:00
|
|
|
stream_clear(context->s);
|
|
|
|
stream_set_pos(context->s, 0);
|
|
|
|
return context->s;
|
2011-09-14 00:30:16 +04:00
|
|
|
}
|
|
|
|
|
2012-01-08 06:36:12 +04:00
|
|
|
void xf_peer_live_rfx(freerdp_peer* client)
|
|
|
|
{
|
|
|
|
xfPeerContext* xfp = (xfPeerContext*) client->context;
|
2012-12-13 03:02:15 +04:00
|
|
|
pthread_create(&(xfp->thread), 0, xf_monitor_updates, (void*) client);
|
2011-09-14 03:03:06 +04:00
|
|
|
}
|
|
|
|
|
2012-01-08 06:36:12 +04:00
|
|
|
void xf_peer_rfx_update(freerdp_peer* client, int x, int y, int width, int height)
|
|
|
|
{
|
2013-03-21 23:19:33 +04:00
|
|
|
wStream* s;
|
2012-10-09 11:01:37 +04:00
|
|
|
BYTE* data;
|
2012-01-08 06:36:12 +04:00
|
|
|
xfInfo* xfi;
|
|
|
|
RFX_RECT rect;
|
|
|
|
XImage* image;
|
|
|
|
rdpUpdate* update;
|
|
|
|
xfPeerContext* xfp;
|
|
|
|
SURFACE_BITS_COMMAND* cmd;
|
|
|
|
|
|
|
|
update = client->update;
|
|
|
|
xfp = (xfPeerContext*) client->context;
|
|
|
|
cmd = &update->surface_bits_command;
|
|
|
|
xfi = xfp->info;
|
|
|
|
|
|
|
|
if (width * height <= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
s = xf_peer_stream_init(xfp);
|
|
|
|
|
2012-01-10 07:08:25 +04:00
|
|
|
if (xfi->use_xshm)
|
|
|
|
{
|
2012-02-15 11:48:37 +04:00
|
|
|
/**
|
|
|
|
* Passing an offset source rectangle to rfx_compose_message()
|
|
|
|
* leads to protocol errors, so offset the data pointer instead.
|
|
|
|
*/
|
2013-02-17 20:03:35 +04:00
|
|
|
|
2012-02-15 11:48:37 +04:00
|
|
|
rect.x = 0;
|
|
|
|
rect.y = 0;
|
2012-01-10 07:08:25 +04:00
|
|
|
rect.width = width;
|
|
|
|
rect.height = height;
|
|
|
|
|
2012-02-01 06:05:47 +04:00
|
|
|
image = xf_snapshot(xfp, x, y, width, height);
|
|
|
|
|
2012-10-09 11:01:37 +04:00
|
|
|
data = (BYTE*) image->data;
|
2012-02-15 11:48:37 +04:00
|
|
|
data = &data[(y * image->bytes_per_line) + (x * image->bits_per_pixel / 8)];
|
2012-02-05 03:18:46 +04:00
|
|
|
|
|
|
|
rfx_compose_message(xfp->rfx_context, s, &rect, 1, data,
|
|
|
|
width, height, image->bytes_per_line);
|
2012-01-10 07:08:25 +04:00
|
|
|
|
|
|
|
cmd->destLeft = x;
|
|
|
|
cmd->destTop = y;
|
|
|
|
cmd->destRight = x + width;
|
|
|
|
cmd->destBottom = y + height;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rect.x = 0;
|
|
|
|
rect.y = 0;
|
|
|
|
rect.width = width;
|
|
|
|
rect.height = height;
|
2012-01-08 06:36:12 +04:00
|
|
|
|
2012-02-01 06:05:47 +04:00
|
|
|
image = xf_snapshot(xfp, x, y, width, height);
|
2012-01-08 06:36:12 +04:00
|
|
|
|
2012-12-13 03:02:15 +04:00
|
|
|
data = (BYTE*) image->data;
|
|
|
|
|
|
|
|
rfx_compose_message(xfp->rfx_context, s, &rect, 1, data,
|
|
|
|
width, height, image->bytes_per_line);
|
2012-01-10 07:08:25 +04:00
|
|
|
|
|
|
|
cmd->destLeft = x;
|
|
|
|
cmd->destTop = y;
|
|
|
|
cmd->destRight = x + width;
|
|
|
|
cmd->destBottom = y + height;
|
2013-02-18 21:01:30 +04:00
|
|
|
|
|
|
|
XDestroyImage(image);
|
2012-02-01 06:05:47 +04:00
|
|
|
}
|
2012-01-31 23:09:55 +04:00
|
|
|
|
2012-01-08 06:36:12 +04:00
|
|
|
cmd->bpp = 32;
|
2012-11-08 00:13:14 +04:00
|
|
|
cmd->codecID = client->settings->RemoteFxCodecId;
|
2012-01-08 06:36:12 +04:00
|
|
|
cmd->width = width;
|
|
|
|
cmd->height = height;
|
|
|
|
cmd->bitmapDataLength = stream_get_length(s);
|
|
|
|
cmd->bitmapData = stream_get_head(s);
|
|
|
|
|
|
|
|
update->SurfaceBits(update->context, cmd);
|
|
|
|
}
|
|
|
|
|
2012-10-09 10:38:39 +04:00
|
|
|
BOOL xf_peer_get_fds(freerdp_peer* client, void** rfds, int* rcount)
|
2012-01-08 06:36:12 +04:00
|
|
|
{
|
2013-02-17 20:59:29 +04:00
|
|
|
int fds;
|
|
|
|
HANDLE event;
|
2012-01-08 06:36:12 +04:00
|
|
|
xfPeerContext* xfp = (xfPeerContext*) client->context;
|
|
|
|
|
2013-02-17 20:59:29 +04:00
|
|
|
event = MessageQueue_Event(xfp->queue);
|
|
|
|
fds = GetEventFileDescriptor(event);
|
|
|
|
rfds[*rcount] = (void*) (long) fds;
|
2012-01-08 06:36:12 +04:00
|
|
|
(*rcount)++;
|
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2012-01-08 06:36:12 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 10:38:39 +04:00
|
|
|
BOOL xf_peer_check_fds(freerdp_peer* client)
|
2012-01-08 06:36:12 +04:00
|
|
|
{
|
|
|
|
xfInfo* xfi;
|
2013-04-24 04:01:07 +04:00
|
|
|
HGDI_RGN region;
|
2012-01-08 06:36:12 +04:00
|
|
|
xfPeerContext* xfp;
|
|
|
|
|
|
|
|
xfp = (xfPeerContext*) client->context;
|
|
|
|
xfi = xfp->info;
|
|
|
|
|
2013-04-24 04:01:07 +04:00
|
|
|
if (!xfp->activated)
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2012-01-08 06:36:12 +04:00
|
|
|
|
2013-04-24 04:01:07 +04:00
|
|
|
region = xfp->hdc->hwnd->invalid;
|
2013-02-17 20:59:29 +04:00
|
|
|
|
2013-04-24 04:01:07 +04:00
|
|
|
pthread_mutex_lock(&(xfp->mutex));
|
2013-02-17 20:59:29 +04:00
|
|
|
|
2013-04-24 04:01:07 +04:00
|
|
|
if ((region->w * region->h) > 0)
|
|
|
|
xf_peer_rfx_update(client, region->x, region->y, region->w, region->h);
|
2013-02-17 20:59:29 +04:00
|
|
|
|
2013-04-24 04:01:07 +04:00
|
|
|
region->null = 1;
|
2013-02-17 20:59:29 +04:00
|
|
|
|
2013-04-24 04:01:07 +04:00
|
|
|
pthread_mutex_unlock(&(xfp->mutex));
|
2012-01-08 06:36:12 +04:00
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2012-01-08 06:36:12 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 10:38:39 +04:00
|
|
|
BOOL xf_peer_capabilities(freerdp_peer* client)
|
2011-12-15 00:08:09 +04:00
|
|
|
{
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-12-15 00:08:09 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 10:38:39 +04:00
|
|
|
BOOL xf_peer_post_connect(freerdp_peer* client)
|
2011-09-14 00:30:16 +04:00
|
|
|
{
|
2011-11-08 02:20:00 +04:00
|
|
|
xfInfo* xfi;
|
|
|
|
xfPeerContext* xfp;
|
|
|
|
|
|
|
|
xfp = (xfPeerContext*) client->context;
|
|
|
|
xfi = (xfInfo*) xfp->info;
|
|
|
|
|
2011-09-14 00:30:16 +04:00
|
|
|
/**
|
|
|
|
* This callback is called when the entire connection sequence is done, i.e. we've received the
|
|
|
|
* Font List PDU from the client and sent out the Font Map PDU.
|
|
|
|
* The server may start sending graphics output and receiving keyboard/mouse input after this
|
|
|
|
* callback returns.
|
|
|
|
*/
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "Client %s is activated", client->hostname);
|
2012-11-08 03:23:25 +04:00
|
|
|
if (client->settings->AutoLogonEnabled)
|
2011-09-14 00:30:16 +04:00
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, " and wants to login automatically as %s\\%s",
|
2012-11-08 00:13:14 +04:00
|
|
|
client->settings->Domain ? client->settings->Domain : "",
|
|
|
|
client->settings->Username);
|
2011-09-14 00:30:16 +04:00
|
|
|
|
|
|
|
/* A real server may perform OS login here if NLA is not executed previously. */
|
|
|
|
}
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "\n");
|
2011-09-14 00:30:16 +04:00
|
|
|
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "Client requested desktop: %dx%dx%d\n",
|
2012-11-07 19:33:06 +04:00
|
|
|
client->settings->DesktopWidth, client->settings->DesktopHeight, client->settings->ColorDepth);
|
2011-09-14 00:30:16 +04:00
|
|
|
|
2012-11-08 00:13:14 +04:00
|
|
|
if (!client->settings->RemoteFxCodec)
|
2012-03-05 03:01:31 +04:00
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "Client does not support RemoteFX\n");
|
2012-12-13 03:02:15 +04:00
|
|
|
return FALSE;
|
2012-03-05 03:01:31 +04:00
|
|
|
}
|
|
|
|
|
2012-11-07 19:33:06 +04:00
|
|
|
/* A real server should tag the peer as activated here and start sending updates in main loop. */
|
2011-09-14 00:30:16 +04:00
|
|
|
|
2012-11-07 19:33:06 +04:00
|
|
|
client->settings->DesktopWidth = xfi->width;
|
|
|
|
client->settings->DesktopHeight = xfi->height;
|
2012-01-03 08:13:54 +04:00
|
|
|
|
2011-11-22 04:41:49 +04:00
|
|
|
client->update->DesktopResize(client->update->context);
|
2011-11-08 02:20:00 +04:00
|
|
|
|
2012-12-13 03:02:15 +04:00
|
|
|
/* Return FALSE here would stop the execution of the peer main loop. */
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-09-14 00:30:16 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 10:38:39 +04:00
|
|
|
BOOL xf_peer_activate(freerdp_peer* client)
|
2011-09-14 00:30:16 +04:00
|
|
|
{
|
2011-10-18 11:10:12 +04:00
|
|
|
xfPeerContext* xfp = (xfPeerContext*) client->context;
|
2011-09-14 00:30:16 +04:00
|
|
|
|
2011-10-18 11:10:12 +04:00
|
|
|
rfx_context_reset(xfp->rfx_context);
|
2012-10-09 10:31:28 +04:00
|
|
|
xfp->activated = TRUE;
|
2011-09-14 00:30:16 +04:00
|
|
|
|
2013-02-17 22:21:52 +04:00
|
|
|
xf_peer_live_rfx(client);
|
2011-09-14 00:30:16 +04:00
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-09-14 00:30:16 +04:00
|
|
|
}
|
|
|
|
|
2013-04-24 02:17:01 +04:00
|
|
|
const char* makecert_argv[4] =
|
|
|
|
{
|
|
|
|
"makecert",
|
|
|
|
"-rdp",
|
|
|
|
"-live",
|
|
|
|
"-silent"
|
|
|
|
};
|
|
|
|
|
|
|
|
int makecert_argc = (sizeof(makecert_argv) / sizeof(char*));
|
|
|
|
|
|
|
|
int xf_generate_certificate(rdpSettings* settings)
|
|
|
|
{
|
|
|
|
char* server_file_path;
|
|
|
|
MAKECERT_CONTEXT* context;
|
|
|
|
|
|
|
|
server_file_path = GetCombinedPath(settings->ConfigPath, "server");
|
|
|
|
|
|
|
|
if (!PathFileExistsA(server_file_path))
|
|
|
|
CreateDirectoryA(server_file_path, 0);
|
|
|
|
|
|
|
|
settings->CertificateFile = GetCombinedPath(server_file_path, "server.crt");
|
|
|
|
settings->PrivateKeyFile = GetCombinedPath(server_file_path, "server.key");
|
|
|
|
|
|
|
|
if ((!PathFileExistsA(settings->CertificateFile)) ||
|
|
|
|
(!PathFileExistsA(settings->PrivateKeyFile)))
|
|
|
|
{
|
|
|
|
context = makecert_context_new();
|
|
|
|
|
|
|
|
makecert_context_process(context, makecert_argc, (char**) makecert_argv);
|
|
|
|
|
|
|
|
makecert_context_set_output_file_name(context, "server");
|
|
|
|
|
|
|
|
if (!PathFileExistsA(settings->CertificateFile))
|
|
|
|
makecert_context_output_certificate_file(context, server_file_path);
|
|
|
|
|
|
|
|
if (!PathFileExistsA(settings->PrivateKeyFile))
|
|
|
|
makecert_context_output_private_key_file(context, server_file_path);
|
|
|
|
|
|
|
|
makecert_context_free(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(server_file_path);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void* xf_peer_main_loop(void* arg)
|
2011-09-14 00:30:16 +04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int fds;
|
|
|
|
int max_fds;
|
|
|
|
int rcount;
|
|
|
|
void* rfds[32];
|
|
|
|
fd_set rfds_set;
|
2012-01-31 05:35:33 +04:00
|
|
|
rdpSettings* settings;
|
2011-09-14 00:30:16 +04:00
|
|
|
freerdp_peer* client = (freerdp_peer*) arg;
|
2012-02-11 03:09:00 +04:00
|
|
|
xfPeerContext* xfp;
|
2013-04-24 04:01:07 +04:00
|
|
|
struct timeval timeout;
|
2011-09-14 00:30:16 +04:00
|
|
|
|
2013-02-17 20:59:29 +04:00
|
|
|
ZeroMemory(rfds, sizeof(rfds));
|
2013-04-24 04:01:07 +04:00
|
|
|
ZeroMemory(&timeout, sizeof(struct timeval));
|
2011-09-14 00:30:16 +04:00
|
|
|
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "We've got a client %s\n", client->hostname);
|
2011-10-18 11:10:12 +04:00
|
|
|
|
|
|
|
xf_peer_init(client);
|
2012-02-11 03:09:00 +04:00
|
|
|
xfp = (xfPeerContext*) client->context;
|
2012-01-31 05:35:33 +04:00
|
|
|
settings = client->settings;
|
2011-09-14 00:30:16 +04:00
|
|
|
|
2013-04-24 02:17:01 +04:00
|
|
|
xf_generate_certificate(settings);
|
2012-01-31 05:35:33 +04:00
|
|
|
|
2012-11-08 00:13:14 +04:00
|
|
|
settings->RemoteFxCodec = TRUE;
|
2013-01-28 02:17:04 +04:00
|
|
|
settings->ColorDepth = 32;
|
2011-09-14 00:30:16 +04:00
|
|
|
|
2011-12-15 00:08:09 +04:00
|
|
|
client->Capabilities = xf_peer_capabilities;
|
2011-09-14 00:30:16 +04:00
|
|
|
client->PostConnect = xf_peer_post_connect;
|
|
|
|
client->Activate = xf_peer_activate;
|
|
|
|
|
2012-01-31 06:35:15 +04:00
|
|
|
xf_input_register_callbacks(client->input);
|
2011-09-14 00:30:16 +04:00
|
|
|
|
|
|
|
client->Initialize(client);
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
rcount = 0;
|
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
if (client->GetFileDescriptor(client, rfds, &rcount) != TRUE)
|
2011-09-14 00:30:16 +04:00
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "Failed to get FreeRDP file descriptor\n");
|
2011-09-14 00:30:16 +04:00
|
|
|
break;
|
|
|
|
}
|
2013-04-24 04:01:07 +04:00
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
if (xf_peer_get_fds(client, rfds, &rcount) != TRUE)
|
2012-01-08 06:36:12 +04:00
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "Failed to get xfreerdp file descriptor\n");
|
2012-01-08 06:36:12 +04:00
|
|
|
break;
|
|
|
|
}
|
2011-09-14 00:30:16 +04:00
|
|
|
|
|
|
|
max_fds = 0;
|
|
|
|
FD_ZERO(&rfds_set);
|
|
|
|
|
|
|
|
for (i = 0; i < rcount; i++)
|
|
|
|
{
|
|
|
|
fds = (int)(long)(rfds[i]);
|
|
|
|
|
|
|
|
if (fds > max_fds)
|
|
|
|
max_fds = fds;
|
|
|
|
|
|
|
|
FD_SET(fds, &rfds_set);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (max_fds == 0)
|
|
|
|
break;
|
|
|
|
|
2013-04-24 04:01:07 +04:00
|
|
|
timeout.tv_sec = 0;
|
|
|
|
timeout.tv_usec = 100;
|
|
|
|
|
|
|
|
if (select(max_fds + 1, &rfds_set, NULL, NULL, &timeout) == -1)
|
2011-09-14 00:30:16 +04:00
|
|
|
{
|
|
|
|
/* these are not really errors */
|
|
|
|
if (!((errno == EAGAIN) ||
|
|
|
|
(errno == EWOULDBLOCK) ||
|
|
|
|
(errno == EINPROGRESS) ||
|
|
|
|
(errno == EINTR))) /* signal occurred */
|
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "select failed\n");
|
2011-09-14 00:30:16 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
if (client->CheckFileDescriptor(client) != TRUE)
|
2012-01-08 06:36:12 +04:00
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "Failed to check freerdp file descriptor\n");
|
2011-09-14 00:30:16 +04:00
|
|
|
break;
|
2012-01-08 06:36:12 +04:00
|
|
|
}
|
2013-04-24 04:01:07 +04:00
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
if ((xf_peer_check_fds(client)) != TRUE)
|
2012-01-08 06:36:12 +04:00
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "Failed to check xfreerdp file descriptor\n");
|
2012-01-08 06:36:12 +04:00
|
|
|
break;
|
|
|
|
}
|
2011-09-14 00:30:16 +04:00
|
|
|
}
|
|
|
|
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "Client %s disconnected.\n", client->hostname);
|
2011-09-14 00:30:16 +04:00
|
|
|
|
|
|
|
client->Disconnect(client);
|
2012-02-11 03:09:00 +04:00
|
|
|
|
|
|
|
pthread_cancel(xfp->thread);
|
|
|
|
|
|
|
|
pthread_join(xfp->thread, NULL);
|
|
|
|
|
2011-10-18 11:10:12 +04:00
|
|
|
freerdp_peer_context_free(client);
|
2011-09-14 00:30:16 +04:00
|
|
|
freerdp_peer_free(client);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void xf_peer_accepted(freerdp_listener* instance, freerdp_peer* client)
|
|
|
|
{
|
2013-04-24 02:17:01 +04:00
|
|
|
HANDLE thread;
|
2011-09-14 03:03:06 +04:00
|
|
|
|
2013-04-24 02:17:01 +04:00
|
|
|
thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) xf_peer_main_loop, client, 0, NULL);
|
2011-09-14 00:30:16 +04:00
|
|
|
}
|