2014-07-11 00:32:46 +04:00
|
|
|
/**
|
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
|
|
|
*
|
|
|
|
* Copyright 2011-2014 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/ipc.h>
|
|
|
|
#include <sys/shm.h>
|
|
|
|
#include <X11/Xlib.h>
|
|
|
|
#include <X11/Xutil.h>
|
|
|
|
#include <sys/select.h>
|
|
|
|
|
|
|
|
#include <winpr/crt.h>
|
|
|
|
#include <winpr/file.h>
|
|
|
|
#include <winpr/path.h>
|
|
|
|
#include <winpr/synch.h>
|
|
|
|
#include <winpr/thread.h>
|
|
|
|
|
|
|
|
#include <freerdp/freerdp.h>
|
|
|
|
#include <freerdp/codec/color.h>
|
|
|
|
#include <freerdp/locale/keyboard.h>
|
|
|
|
|
|
|
|
#include <winpr/tools/makecert.h>
|
|
|
|
|
|
|
|
#include "x11_shadow.h"
|
|
|
|
|
|
|
|
#ifdef WITH_XDAMAGE
|
|
|
|
|
2014-07-12 02:00:33 +04:00
|
|
|
void x11_shadow_xdamage_init(x11ShadowServer* server)
|
2014-07-11 00:32:46 +04:00
|
|
|
{
|
|
|
|
int damage_event;
|
|
|
|
int damage_error;
|
|
|
|
int major, minor;
|
|
|
|
XGCValues values;
|
|
|
|
|
2014-07-12 02:00:33 +04:00
|
|
|
if (XDamageQueryExtension(server->display, &damage_event, &damage_error) == 0)
|
2014-07-11 00:32:46 +04:00
|
|
|
{
|
|
|
|
fprintf(stderr, "XDamageQueryExtension failed\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-12 02:00:33 +04:00
|
|
|
XDamageQueryVersion(server->display, &major, &minor);
|
2014-07-11 00:32:46 +04:00
|
|
|
|
2014-07-12 02:00:33 +04:00
|
|
|
if (XDamageQueryVersion(server->display, &major, &minor) == 0)
|
2014-07-11 00:32:46 +04:00
|
|
|
{
|
|
|
|
fprintf(stderr, "XDamageQueryVersion failed\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (major < 1)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "XDamageQueryVersion failed: major:%d minor:%d\n", major, minor);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-12 02:00:33 +04:00
|
|
|
server->xdamage_notify_event = damage_event + XDamageNotify;
|
|
|
|
server->xdamage = XDamageCreate(server->display, server->root_window, XDamageReportDeltaRectangles);
|
2014-07-11 00:32:46 +04:00
|
|
|
|
2014-07-12 02:00:33 +04:00
|
|
|
if (server->xdamage == None)
|
2014-07-11 00:32:46 +04:00
|
|
|
{
|
|
|
|
fprintf(stderr, "XDamageCreate failed\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef WITH_XFIXES
|
2014-07-12 02:00:33 +04:00
|
|
|
server->xdamage_region = XFixesCreateRegion(server->display, NULL, 0);
|
2014-07-11 00:32:46 +04:00
|
|
|
|
2014-07-12 02:00:33 +04:00
|
|
|
if (server->xdamage_region == None)
|
2014-07-11 00:32:46 +04:00
|
|
|
{
|
|
|
|
fprintf(stderr, "XFixesCreateRegion failed\n");
|
2014-07-12 02:00:33 +04:00
|
|
|
XDamageDestroy(server->display, server->xdamage);
|
|
|
|
server->xdamage = None;
|
2014-07-11 00:32:46 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
values.subwindow_mode = IncludeInferiors;
|
2014-07-12 02:00:33 +04:00
|
|
|
server->xdamage_gc = XCreateGC(server->display, server->root_window, GCSubwindowMode, &values);
|
|
|
|
XSetFunction(server->display, server->xdamage_gc, GXcopy);
|
2014-07-11 00:32:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2014-07-12 02:00:33 +04:00
|
|
|
int x11_shadow_xshm_init(x11ShadowServer* server)
|
2014-07-11 00:32:46 +04:00
|
|
|
{
|
|
|
|
Bool pixmaps;
|
|
|
|
int major, minor;
|
|
|
|
|
2014-07-12 02:00:33 +04:00
|
|
|
if (XShmQueryExtension(server->display) != False)
|
2014-07-11 00:32:46 +04:00
|
|
|
{
|
2014-07-12 02:00:33 +04:00
|
|
|
XShmQueryVersion(server->display, &major, &minor, &pixmaps);
|
2014-07-11 00:32:46 +04:00
|
|
|
|
|
|
|
if (pixmaps != True)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "XShmQueryVersion failed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr, "XShmQueryExtension failed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-07-12 02:00:33 +04:00
|
|
|
server->fb_shm_info.shmid = -1;
|
|
|
|
server->fb_shm_info.shmaddr = (char*) -1;
|
2014-07-11 00:32:46 +04:00
|
|
|
|
2014-07-12 02:00:33 +04:00
|
|
|
server->fb_image = XShmCreateImage(server->display, server->visual, server->depth,
|
|
|
|
ZPixmap, NULL, &(server->fb_shm_info), server->width, server->height);
|
2014-07-11 00:32:46 +04:00
|
|
|
|
2014-07-12 02:00:33 +04:00
|
|
|
if (!server->fb_image)
|
2014-07-11 00:32:46 +04:00
|
|
|
{
|
|
|
|
fprintf(stderr, "XShmCreateImage failed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-07-12 02:00:33 +04:00
|
|
|
server->fb_shm_info.shmid = shmget(IPC_PRIVATE,
|
|
|
|
server->fb_image->bytes_per_line * server->fb_image->height, IPC_CREAT | 0600);
|
2014-07-11 00:32:46 +04:00
|
|
|
|
2014-07-12 02:00:33 +04:00
|
|
|
if (server->fb_shm_info.shmid == -1)
|
2014-07-11 00:32:46 +04:00
|
|
|
{
|
|
|
|
fprintf(stderr, "shmget failed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-07-12 02:00:33 +04:00
|
|
|
server->fb_shm_info.readOnly = False;
|
|
|
|
server->fb_shm_info.shmaddr = shmat(server->fb_shm_info.shmid, 0, 0);
|
|
|
|
server->fb_image->data = server->fb_shm_info.shmaddr;
|
2014-07-11 00:32:46 +04:00
|
|
|
|
2014-07-12 02:00:33 +04:00
|
|
|
if (server->fb_shm_info.shmaddr == ((char*) -1))
|
2014-07-11 00:32:46 +04:00
|
|
|
{
|
|
|
|
fprintf(stderr, "shmat failed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-07-12 02:00:33 +04:00
|
|
|
XShmAttach(server->display, &(server->fb_shm_info));
|
|
|
|
XSync(server->display, False);
|
2014-07-11 00:32:46 +04:00
|
|
|
|
2014-07-12 02:00:33 +04:00
|
|
|
shmctl(server->fb_shm_info.shmid, IPC_RMID, 0);
|
2014-07-11 00:32:46 +04:00
|
|
|
|
|
|
|
fprintf(stderr, "display: %p root_window: %p width: %d height: %d depth: %d\n",
|
2014-07-12 02:00:33 +04:00
|
|
|
server->display, (void*) server->root_window, server->fb_image->width, server->fb_image->height, server->fb_image->depth);
|
2014-07-11 00:32:46 +04:00
|
|
|
|
2014-07-12 02:00:33 +04:00
|
|
|
server->fb_pixmap = XShmCreatePixmap(server->display,
|
|
|
|
server->root_window, server->fb_image->data, &(server->fb_shm_info),
|
|
|
|
server->fb_image->width, server->fb_image->height, server->fb_image->depth);
|
2014-07-11 00:32:46 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-07-12 02:00:33 +04:00
|
|
|
void x11_shadow_peer_context_new(freerdp_peer* client, xfPeerContext* context)
|
2014-07-11 00:32:46 +04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int pf_count;
|
|
|
|
int vi_count;
|
|
|
|
XVisualInfo* vi;
|
|
|
|
XVisualInfo* vis;
|
|
|
|
XVisualInfo template;
|
|
|
|
XPixmapFormatValues* pf;
|
|
|
|
XPixmapFormatValues* pfs;
|
2014-07-12 02:00:33 +04:00
|
|
|
x11ShadowServer* server;
|
2014-07-11 00:32:46 +04:00
|
|
|
|
2014-07-12 02:00:33 +04:00
|
|
|
server = (x11ShadowServer*) client->context;
|
|
|
|
context->server = server;
|
2014-07-11 00:32:46 +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"
|
|
|
|
*/
|
2014-07-12 02:00:33 +04:00
|
|
|
server->use_xshm = TRUE;
|
2014-07-11 00:32:46 +04:00
|
|
|
|
|
|
|
setenv("DISPLAY", ":0", 1); /* Set DISPLAY variable if not already set */
|
|
|
|
|
|
|
|
if (!XInitThreads())
|
|
|
|
fprintf(stderr, "warning: XInitThreads() failure\n");
|
|
|
|
|
2014-07-12 02:00:33 +04:00
|
|
|
server->display = XOpenDisplay(NULL);
|
2014-07-11 00:32:46 +04:00
|
|
|
|
2014-07-12 02:00:33 +04:00
|
|
|
if (!server->display)
|
2014-07-11 00:32:46 +04:00
|
|
|
{
|
|
|
|
fprintf(stderr, "failed to open display: %s\n", XDisplayName(NULL));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2014-07-12 02:00:33 +04:00
|
|
|
server->xfds = ConnectionNumber(server->display);
|
|
|
|
server->number = DefaultScreen(server->display);
|
|
|
|
server->screen = ScreenOfDisplay(server->display, server->number);
|
|
|
|
server->depth = DefaultDepthOfScreen(server->screen);
|
|
|
|
server->width = WidthOfScreen(server->screen);
|
|
|
|
server->height = HeightOfScreen(server->screen);
|
|
|
|
server->root_window = DefaultRootWindow(server->display);
|
2014-07-11 00:32:46 +04:00
|
|
|
|
2014-07-12 02:00:33 +04:00
|
|
|
pfs = XListPixmapFormats(server->display, &pf_count);
|
2014-07-11 00:32:46 +04:00
|
|
|
|
|
|
|
if (!pfs)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "XListPixmapFormats failed\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < pf_count; i++)
|
|
|
|
{
|
|
|
|
pf = pfs + i;
|
|
|
|
|
2014-07-12 02:00:33 +04:00
|
|
|
if (pf->depth == server->depth)
|
2014-07-11 00:32:46 +04:00
|
|
|
{
|
2014-07-12 02:00:33 +04:00
|
|
|
server->bpp = pf->bits_per_pixel;
|
|
|
|
server->scanline_pad = pf->scanline_pad;
|
2014-07-11 00:32:46 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
XFree(pfs);
|
|
|
|
|
|
|
|
ZeroMemory(&template, sizeof(template));
|
|
|
|
template.class = TrueColor;
|
2014-07-12 02:00:33 +04:00
|
|
|
template.screen = server->number;
|
2014-07-11 00:32:46 +04:00
|
|
|
|
2014-07-12 02:00:33 +04:00
|
|
|
vis = XGetVisualInfo(server->display, VisualClassMask | VisualScreenMask, &template, &vi_count);
|
2014-07-11 00:32:46 +04:00
|
|
|
|
|
|
|
if (!vis)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "XGetVisualInfo failed\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < vi_count; i++)
|
|
|
|
{
|
|
|
|
vi = vis + i;
|
|
|
|
|
2014-07-12 02:00:33 +04:00
|
|
|
if (vi->depth == server->depth)
|
2014-07-11 00:32:46 +04:00
|
|
|
{
|
2014-07-12 02:00:33 +04:00
|
|
|
server->visual = vi->visual;
|
2014-07-11 00:32:46 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
XFree(vis);
|
|
|
|
|
2014-07-12 02:00:33 +04:00
|
|
|
server->clrconv = freerdp_clrconv_new(CLRCONV_ALPHA | CLRCONV_INVERT);
|
2014-07-11 00:32:46 +04:00
|
|
|
|
2014-07-12 02:00:33 +04:00
|
|
|
XSelectInput(server->display, server->root_window, SubstructureNotifyMask);
|
2014-07-11 00:32:46 +04:00
|
|
|
|
2014-07-12 02:00:33 +04:00
|
|
|
if (server->use_xshm)
|
2014-07-11 00:32:46 +04:00
|
|
|
{
|
2014-07-12 02:00:33 +04:00
|
|
|
if (x11_shadow_xshm_init(server) < 0)
|
|
|
|
server->use_xshm = FALSE;
|
2014-07-11 00:32:46 +04:00
|
|
|
}
|
|
|
|
|
2014-07-12 02:00:33 +04:00
|
|
|
if (server->use_xshm)
|
2014-07-11 00:32:46 +04:00
|
|
|
printf("Using X Shared Memory Extension (XShm)\n");
|
|
|
|
|
|
|
|
#ifdef WITH_XDAMAGE
|
2014-07-12 02:00:33 +04:00
|
|
|
x11_shadow_xdamage_init(server);
|
2014-07-11 00:32:46 +04:00
|
|
|
#endif
|
|
|
|
|
2014-07-12 02:00:33 +04:00
|
|
|
x11_shadow_cursor_init(server);
|
2014-07-11 00:32:46 +04:00
|
|
|
|
2014-07-12 02:00:33 +04:00
|
|
|
server->bytesPerPixel = 4;
|
|
|
|
server->activePeerCount = 0;
|
2014-07-11 00:32:46 +04:00
|
|
|
|
|
|
|
freerdp_keyboard_init(0);
|
|
|
|
|
|
|
|
context->rfx_context = rfx_context_new(TRUE);
|
|
|
|
context->rfx_context->mode = RLGR3;
|
2014-07-12 02:00:33 +04:00
|
|
|
context->rfx_context->width = server->width;
|
|
|
|
context->rfx_context->height = server->height;
|
2014-07-11 00:32:46 +04:00
|
|
|
|
|
|
|
rfx_context_set_pixel_format(context->rfx_context, RDP_PIXEL_FORMAT_B8G8R8A8);
|
|
|
|
|
|
|
|
context->s = Stream_New(NULL, 65536);
|
|
|
|
Stream_Clear(context->s);
|
|
|
|
|
|
|
|
context->updateReadyEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
|
|
|
|
context->updateSentEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
|
|
|
|
}
|
|
|
|
|
2014-07-11 01:20:41 +04:00
|
|
|
void x11_shadow_peer_context_free(freerdp_peer* client, xfPeerContext* context)
|
2014-07-11 00:32:46 +04:00
|
|
|
{
|
2014-07-12 02:00:33 +04:00
|
|
|
x11ShadowServer* server;
|
|
|
|
|
2014-07-11 00:32:46 +04:00
|
|
|
if (context)
|
|
|
|
{
|
2014-07-12 02:00:33 +04:00
|
|
|
server = context->server;
|
|
|
|
|
|
|
|
if (server->display)
|
|
|
|
XCloseDisplay(server->display);
|
|
|
|
|
|
|
|
freerdp_clrconv_free(server->clrconv);
|
2014-07-11 00:32:46 +04:00
|
|
|
|
|
|
|
CloseHandle(context->updateReadyEvent);
|
|
|
|
CloseHandle(context->updateSentEvent);
|
|
|
|
|
|
|
|
Stream_Free(context->s, TRUE);
|
|
|
|
rfx_context_free(context->rfx_context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-11 01:20:41 +04:00
|
|
|
void x11_shadow_peer_init(freerdp_peer* client)
|
2014-07-11 00:32:46 +04:00
|
|
|
{
|
|
|
|
xfPeerContext* xfp;
|
|
|
|
|
|
|
|
client->ContextSize = sizeof(xfPeerContext);
|
2014-07-11 01:20:41 +04:00
|
|
|
client->ContextNew = (psPeerContextNew) x11_shadow_peer_context_new;
|
|
|
|
client->ContextFree = (psPeerContextFree) x11_shadow_peer_context_free;
|
2014-07-11 00:32:46 +04:00
|
|
|
freerdp_peer_context_new(client);
|
|
|
|
|
|
|
|
xfp = (xfPeerContext*) client->context;
|
|
|
|
|
|
|
|
xfp->fps = 16;
|
|
|
|
xfp->mutex = CreateMutex(NULL, FALSE, NULL);
|
|
|
|
}
|
|
|
|
|
2014-07-11 01:20:41 +04:00
|
|
|
void x11_shadow_peer_send_update(freerdp_peer* client)
|
2014-07-11 00:32:46 +04:00
|
|
|
{
|
|
|
|
rdpUpdate* update;
|
|
|
|
SURFACE_BITS_COMMAND* cmd;
|
|
|
|
|
|
|
|
update = client->update;
|
|
|
|
cmd = &update->surface_bits_command;
|
|
|
|
|
|
|
|
if (cmd->bitmapDataLength)
|
|
|
|
update->SurfaceBits(update->context, cmd);
|
|
|
|
}
|
|
|
|
|
2014-07-11 01:20:41 +04:00
|
|
|
BOOL x11_shadow_peer_get_fds(freerdp_peer* client, void** rfds, int* rcount)
|
2014-07-11 00:32:46 +04:00
|
|
|
{
|
|
|
|
int fds;
|
|
|
|
HANDLE event;
|
|
|
|
xfPeerContext* xfp = (xfPeerContext*) client->context;
|
|
|
|
|
|
|
|
event = xfp->updateReadyEvent;
|
|
|
|
fds = GetEventFileDescriptor(event);
|
|
|
|
rfds[*rcount] = (void*) (long) fds;
|
|
|
|
(*rcount)++;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2014-07-11 01:20:41 +04:00
|
|
|
BOOL x11_shadow_peer_check_fds(freerdp_peer* client)
|
2014-07-11 00:32:46 +04:00
|
|
|
{
|
|
|
|
xfPeerContext* xfp;
|
|
|
|
|
|
|
|
xfp = (xfPeerContext*) client->context;
|
|
|
|
|
|
|
|
if (WaitForSingleObject(xfp->updateReadyEvent, 0) == WAIT_OBJECT_0)
|
|
|
|
{
|
|
|
|
if (!xfp->activated)
|
|
|
|
return TRUE;
|
|
|
|
|
2014-07-11 01:20:41 +04:00
|
|
|
x11_shadow_peer_send_update(client);
|
2014-07-11 00:32:46 +04:00
|
|
|
|
|
|
|
ResetEvent(xfp->updateReadyEvent);
|
|
|
|
SetEvent(xfp->updateSentEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2014-07-11 01:20:41 +04:00
|
|
|
BOOL x11_shadow_peer_capabilities(freerdp_peer* client)
|
2014-07-11 00:32:46 +04:00
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2014-07-11 01:20:41 +04:00
|
|
|
BOOL x11_shadow_peer_post_connect(freerdp_peer* client)
|
2014-07-11 00:32:46 +04:00
|
|
|
{
|
|
|
|
xfPeerContext* xfp;
|
2014-07-12 02:00:33 +04:00
|
|
|
x11ShadowServer* server;
|
2014-07-11 00:32:46 +04:00
|
|
|
|
|
|
|
xfp = (xfPeerContext*) client->context;
|
2014-07-12 02:00:33 +04:00
|
|
|
server = xfp->server;
|
2014-07-11 00:32:46 +04:00
|
|
|
|
|
|
|
fprintf(stderr, "Client %s is activated", client->hostname);
|
|
|
|
if (client->settings->AutoLogonEnabled)
|
|
|
|
{
|
|
|
|
fprintf(stderr, " and wants to login automatically as %s\\%s",
|
|
|
|
client->settings->Domain ? client->settings->Domain : "",
|
|
|
|
client->settings->Username);
|
|
|
|
}
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
|
|
|
|
fprintf(stderr, "Client requested desktop: %dx%dx%d\n",
|
|
|
|
client->settings->DesktopWidth, client->settings->DesktopHeight, client->settings->ColorDepth);
|
|
|
|
|
|
|
|
if (!client->settings->RemoteFxCodec)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Client does not support RemoteFX\n");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* A real server should tag the peer as activated here and start sending updates in main loop. */
|
|
|
|
|
2014-07-12 02:00:33 +04:00
|
|
|
client->settings->DesktopWidth = server->width;
|
|
|
|
client->settings->DesktopHeight = server->height;
|
2014-07-11 00:32:46 +04:00
|
|
|
|
|
|
|
client->update->DesktopResize(client->update->context);
|
|
|
|
|
|
|
|
/* Return FALSE here would stop the execution of the peer main loop. */
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2014-07-11 01:20:41 +04:00
|
|
|
BOOL x11_shadow_peer_activate(freerdp_peer* client)
|
2014-07-11 00:32:46 +04:00
|
|
|
{
|
|
|
|
xfPeerContext* xfp = (xfPeerContext*) client->context;
|
2014-07-12 02:00:33 +04:00
|
|
|
x11ShadowServer* server = xfp->server;
|
2014-07-11 00:32:46 +04:00
|
|
|
|
|
|
|
rfx_context_reset(xfp->rfx_context);
|
|
|
|
xfp->activated = TRUE;
|
|
|
|
|
2014-07-12 02:00:33 +04:00
|
|
|
server->activePeerCount++;
|
2014-07-11 00:32:46 +04:00
|
|
|
|
|
|
|
xfp->monitorThread = CreateThread(NULL, 0,
|
2014-07-11 01:20:41 +04:00
|
|
|
(LPTHREAD_START_ROUTINE) x11_shadow_update_thread, (void*) client, 0, NULL);
|
2014-07-11 00:32:46 +04:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* makecert_argv[4] =
|
|
|
|
{
|
|
|
|
"makecert",
|
|
|
|
"-rdp",
|
|
|
|
"-live",
|
|
|
|
"-silent"
|
|
|
|
};
|
|
|
|
|
|
|
|
int makecert_argc = (sizeof(makecert_argv) / sizeof(char*));
|
|
|
|
|
2014-07-11 01:20:41 +04:00
|
|
|
int x11_shadow_generate_certificate(rdpSettings* settings)
|
2014-07-11 00:32:46 +04:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-07-11 01:20:41 +04:00
|
|
|
static void* x11_shadow_peer_main_loop(void* arg)
|
2014-07-11 00:32:46 +04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int fds;
|
|
|
|
int max_fds;
|
|
|
|
int rcount;
|
|
|
|
void* rfds[32];
|
|
|
|
fd_set rfds_set;
|
|
|
|
rdpSettings* settings;
|
|
|
|
xfPeerContext* xfp;
|
|
|
|
struct timeval timeout;
|
|
|
|
freerdp_peer* client = (freerdp_peer*) arg;
|
|
|
|
|
|
|
|
ZeroMemory(rfds, sizeof(rfds));
|
|
|
|
ZeroMemory(&timeout, sizeof(struct timeval));
|
|
|
|
|
|
|
|
fprintf(stderr, "We've got a client %s\n", client->hostname);
|
|
|
|
|
2014-07-11 01:20:41 +04:00
|
|
|
x11_shadow_peer_init(client);
|
2014-07-12 02:00:33 +04:00
|
|
|
|
2014-07-11 00:32:46 +04:00
|
|
|
xfp = (xfPeerContext*) client->context;
|
|
|
|
settings = client->settings;
|
|
|
|
|
2014-07-11 01:20:41 +04:00
|
|
|
x11_shadow_generate_certificate(settings);
|
2014-07-11 00:32:46 +04:00
|
|
|
|
|
|
|
settings->RemoteFxCodec = TRUE;
|
|
|
|
settings->ColorDepth = 32;
|
|
|
|
|
|
|
|
settings->NlaSecurity = FALSE;
|
|
|
|
settings->TlsSecurity = TRUE;
|
|
|
|
settings->RdpSecurity = FALSE;
|
|
|
|
|
2014-07-11 01:20:41 +04:00
|
|
|
client->Capabilities = x11_shadow_peer_capabilities;
|
|
|
|
client->PostConnect = x11_shadow_peer_post_connect;
|
|
|
|
client->Activate = x11_shadow_peer_activate;
|
2014-07-11 00:32:46 +04:00
|
|
|
|
2014-07-11 01:20:41 +04:00
|
|
|
x11_shadow_input_register_callbacks(client->input);
|
2014-07-11 00:32:46 +04:00
|
|
|
|
|
|
|
client->Initialize(client);
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
rcount = 0;
|
|
|
|
|
|
|
|
if (client->GetFileDescriptor(client, rfds, &rcount) != TRUE)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Failed to get FreeRDP file descriptor\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-07-11 01:20:41 +04:00
|
|
|
if (x11_shadow_peer_get_fds(client, rfds, &rcount) != TRUE)
|
2014-07-11 00:32:46 +04:00
|
|
|
{
|
|
|
|
fprintf(stderr, "Failed to get xfreerdp file descriptor\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
timeout.tv_sec = 0;
|
|
|
|
timeout.tv_usec = 100;
|
|
|
|
|
|
|
|
if (select(max_fds + 1, &rfds_set, NULL, NULL, &timeout) == -1)
|
|
|
|
{
|
|
|
|
/* these are not really errors */
|
|
|
|
if (!((errno == EAGAIN) ||
|
|
|
|
(errno == EWOULDBLOCK) ||
|
|
|
|
(errno == EINPROGRESS) ||
|
|
|
|
(errno == EINTR))) /* signal occurred */
|
|
|
|
{
|
|
|
|
fprintf(stderr, "select failed\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (client->CheckFileDescriptor(client) != TRUE)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Failed to check freerdp file descriptor\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-07-11 01:20:41 +04:00
|
|
|
if ((x11_shadow_peer_check_fds(client)) != TRUE)
|
2014-07-11 00:32:46 +04:00
|
|
|
{
|
|
|
|
fprintf(stderr, "Failed to check xfreerdp file descriptor\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "Client %s disconnected.\n", client->hostname);
|
|
|
|
|
|
|
|
client->Disconnect(client);
|
|
|
|
|
|
|
|
freerdp_peer_context_free(client);
|
|
|
|
freerdp_peer_free(client);
|
|
|
|
|
|
|
|
ExitThread(0);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-07-11 01:20:41 +04:00
|
|
|
void x11_shadow_peer_accepted(freerdp_listener* instance, freerdp_peer* client)
|
2014-07-11 00:32:46 +04:00
|
|
|
{
|
|
|
|
HANDLE thread;
|
|
|
|
|
2014-07-12 02:00:33 +04:00
|
|
|
thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)
|
|
|
|
x11_shadow_peer_main_loop, client, 0, NULL);
|
2014-07-11 00:32:46 +04:00
|
|
|
}
|