shadow: reduce X11 code

This commit is contained in:
Marc-André Moreau 2014-07-11 18:30:56 -04:00
parent 63f94ef150
commit 7caf48bcf4
4 changed files with 57 additions and 136 deletions

View File

@ -169,7 +169,7 @@ int x11_shadow_xshm_init(x11ShadowServer* server)
return 0;
}
void x11_shadow_peer_context_new(freerdp_peer* client, xfPeerContext* context)
void x11_shadow_peer_context_new(freerdp_peer* client, x11ShadowClient* context)
{
int i;
int pf_count;
@ -257,8 +257,6 @@ void x11_shadow_peer_context_new(freerdp_peer* client, xfPeerContext* context)
}
XFree(vis);
server->clrconv = freerdp_clrconv_new(CLRCONV_ALPHA | CLRCONV_INVERT);
XSelectInput(server->display, server->root_window, SubstructureNotifyMask);
if (server->use_xshm)
@ -290,12 +288,9 @@ void x11_shadow_peer_context_new(freerdp_peer* client, xfPeerContext* context)
context->s = Stream_New(NULL, 65536);
Stream_Clear(context->s);
context->updateReadyEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
context->updateSentEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
}
void x11_shadow_peer_context_free(freerdp_peer* client, xfPeerContext* context)
void x11_shadow_peer_context_free(freerdp_peer* client, x11ShadowClient* context)
{
x11ShadowServer* server;
@ -306,11 +301,6 @@ void x11_shadow_peer_context_free(freerdp_peer* client, xfPeerContext* context)
if (server->display)
XCloseDisplay(server->display);
freerdp_clrconv_free(server->clrconv);
CloseHandle(context->updateReadyEvent);
CloseHandle(context->updateSentEvent);
Stream_Free(context->s, TRUE);
rfx_context_free(context->rfx_context);
}
@ -318,63 +308,10 @@ void x11_shadow_peer_context_free(freerdp_peer* client, xfPeerContext* context)
void x11_shadow_peer_init(freerdp_peer* client)
{
xfPeerContext* xfp;
client->ContextSize = sizeof(xfPeerContext);
client->ContextSize = sizeof(x11ShadowClient);
client->ContextNew = (psPeerContextNew) x11_shadow_peer_context_new;
client->ContextFree = (psPeerContextFree) x11_shadow_peer_context_free;
freerdp_peer_context_new(client);
xfp = (xfPeerContext*) client->context;
xfp->fps = 16;
xfp->mutex = CreateMutex(NULL, FALSE, NULL);
}
void x11_shadow_peer_send_update(freerdp_peer* client)
{
rdpUpdate* update;
SURFACE_BITS_COMMAND* cmd;
update = client->update;
cmd = &update->surface_bits_command;
if (cmd->bitmapDataLength)
update->SurfaceBits(update->context, cmd);
}
BOOL x11_shadow_peer_get_fds(freerdp_peer* client, void** rfds, int* rcount)
{
int fds;
HANDLE event;
xfPeerContext* xfp = (xfPeerContext*) client->context;
event = xfp->updateReadyEvent;
fds = GetEventFileDescriptor(event);
rfds[*rcount] = (void*) (long) fds;
(*rcount)++;
return TRUE;
}
BOOL x11_shadow_peer_check_fds(freerdp_peer* client)
{
xfPeerContext* xfp;
xfp = (xfPeerContext*) client->context;
if (WaitForSingleObject(xfp->updateReadyEvent, 0) == WAIT_OBJECT_0)
{
if (!xfp->activated)
return TRUE;
x11_shadow_peer_send_update(client);
ResetEvent(xfp->updateReadyEvent);
SetEvent(xfp->updateSentEvent);
}
return TRUE;
}
BOOL x11_shadow_peer_capabilities(freerdp_peer* client)
@ -384,11 +321,11 @@ BOOL x11_shadow_peer_capabilities(freerdp_peer* client)
BOOL x11_shadow_peer_post_connect(freerdp_peer* client)
{
xfPeerContext* xfp;
x11ShadowClient* context;
x11ShadowServer* server;
xfp = (xfPeerContext*) client->context;
server = xfp->server;
context = (x11ShadowClient*) client->context;
server = context->server;
fprintf(stderr, "Client %s is activated", client->hostname);
if (client->settings->AutoLogonEnabled)
@ -408,28 +345,25 @@ BOOL x11_shadow_peer_post_connect(freerdp_peer* client)
return FALSE;
}
/* A real server should tag the peer as activated here and start sending updates in main loop. */
client->settings->DesktopWidth = server->width;
client->settings->DesktopHeight = server->height;
client->update->DesktopResize(client->update->context);
/* Return FALSE here would stop the execution of the peer main loop. */
return TRUE;
}
BOOL x11_shadow_peer_activate(freerdp_peer* client)
{
xfPeerContext* xfp = (xfPeerContext*) client->context;
x11ShadowServer* server = xfp->server;
x11ShadowClient* context = (x11ShadowClient*) client->context;
x11ShadowServer* server = context->server;
rfx_context_reset(xfp->rfx_context);
xfp->activated = TRUE;
rfx_context_reset(context->rfx_context);
context->activated = TRUE;
server->activePeerCount++;
xfp->monitorThread = CreateThread(NULL, 0,
context->monitorThread = CreateThread(NULL, 0,
(LPTHREAD_START_ROUTINE) x11_shadow_update_thread, (void*) client, 0, NULL);
return TRUE;
@ -481,7 +415,7 @@ int x11_shadow_generate_certificate(rdpSettings* settings)
return 0;
}
static void* x11_shadow_peer_main_loop(void* arg)
static void* x11_shadow_client_thread(void* arg)
{
int i;
int fds;
@ -490,7 +424,7 @@ static void* x11_shadow_peer_main_loop(void* arg)
void* rfds[32];
fd_set rfds_set;
rdpSettings* settings;
xfPeerContext* xfp;
x11ShadowClient* xfp;
struct timeval timeout;
freerdp_peer* client = (freerdp_peer*) arg;
@ -501,7 +435,7 @@ static void* x11_shadow_peer_main_loop(void* arg)
x11_shadow_peer_init(client);
xfp = (xfPeerContext*) client->context;
xfp = (x11ShadowClient*) client->context;
settings = client->settings;
x11_shadow_generate_certificate(settings);
@ -531,12 +465,6 @@ static void* x11_shadow_peer_main_loop(void* arg)
break;
}
if (x11_shadow_peer_get_fds(client, rfds, &rcount) != TRUE)
{
fprintf(stderr, "Failed to get xfreerdp file descriptor\n");
break;
}
max_fds = 0;
FD_ZERO(&rfds_set);
@ -571,14 +499,8 @@ static void* x11_shadow_peer_main_loop(void* arg)
if (client->CheckFileDescriptor(client) != TRUE)
{
fprintf(stderr, "Failed to check freerdp file descriptor\n");
break;
}
if ((x11_shadow_peer_check_fds(client)) != TRUE)
{
fprintf(stderr, "Failed to check xfreerdp file descriptor\n");
break;
//fprintf(stderr, "Failed to check freerdp file descriptor\n");
//break;
}
}
@ -599,5 +521,5 @@ void x11_shadow_peer_accepted(freerdp_listener* instance, freerdp_peer* client)
HANDLE thread;
thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)
x11_shadow_peer_main_loop, client, 0, NULL);
x11_shadow_client_thread, client, 0, NULL);
}

View File

@ -61,8 +61,8 @@ void x11_shadow_input_keyboard_event(rdpInput* input, UINT16 flags, UINT16 code)
DWORD vkcode;
DWORD keycode;
BOOL extended = FALSE;
xfPeerContext* xfp = (xfPeerContext*) input->context;
x11ShadowServer* server = xfp->server;
x11ShadowClient* context = (x11ShadowClient*) input->context;
x11ShadowServer* server = context->server;
if (flags & KBD_FLAGS_EXTENDED)
extended = TRUE;
@ -97,8 +97,8 @@ void x11_shadow_input_mouse_event(rdpInput* input, UINT16 flags, UINT16 x, UINT1
#ifdef WITH_XTEST
int button = 0;
BOOL down = FALSE;
xfPeerContext* xfp = (xfPeerContext*) input->context;
x11ShadowServer* server = xfp->server;
x11ShadowClient* context = (x11ShadowClient*) input->context;
x11ShadowServer* server = context->server;
XTestGrabControl(server->display, True);
@ -142,8 +142,8 @@ void x11_shadow_input_extended_mouse_event(rdpInput* input, UINT16 flags, UINT16
#ifdef WITH_XTEST
int button = 0;
BOOL down = FALSE;
xfPeerContext* xfp = (xfPeerContext*) input->context;
x11ShadowServer* server = xfp->server;
x11ShadowClient* context = (x11ShadowClient*) input->context;
x11ShadowServer* server = context->server;
XTestGrabControl(server->display, True);
XTestFakeMotionEvent(server->display, 0, x, y, CurrentTime);

View File

@ -83,10 +83,9 @@ struct x11_shadow_server
Display* display;
int scanline_pad;
int bytesPerPixel;
HCLRCONV clrconv;
BOOL use_xshm;
int activePeerCount;
BOOL use_xshm;
XImage* fb_image;
Pixmap fb_pixmap;
Window root_window;
@ -117,25 +116,15 @@ struct x11_shadow_server
#include <freerdp/listener.h>
#include <freerdp/utils/stopwatch.h>
typedef struct x11_shadow_peer_context xfPeerContext;
typedef struct x11_shadow_client x11ShadowClient;
#define PeerEvent_Base 0
#define PeerEvent_Class (PeerEvent_Base + 1)
#define PeerEvent_EncodeRegion 1
struct x11_shadow_peer_context
struct x11_shadow_client
{
rdpContext _p;
int fps;
wStream* s;
HANDLE mutex;
BOOL activated;
HANDLE monitorThread;
HANDLE updateReadyEvent;
HANDLE updateSentEvent;
RFX_CONTEXT* rfx_context;
x11ShadowServer* server;
};

View File

@ -29,10 +29,10 @@
#include "x11_shadow.h"
XImage* x11_shadow_snapshot(xfPeerContext* xfp, int x, int y, int width, int height)
XImage* x11_shadow_snapshot(x11ShadowClient* context, int x, int y, int width, int height)
{
XImage* image;
x11ShadowServer* server = xfp->server;
x11ShadowServer* server = context->server;
if (server->use_xshm)
{
@ -47,10 +47,10 @@ XImage* x11_shadow_snapshot(xfPeerContext* xfp, int x, int y, int width, int hei
return image;
}
void x11_shadow_xdamage_subtract_region(xfPeerContext* xfp, int x, int y, int width, int height)
void x11_shadow_xdamage_subtract_region(x11ShadowClient* context, int x, int y, int width, int height)
{
XRectangle region;
x11ShadowServer* server = xfp->server;
x11ShadowServer* server = context->server;
region.x = x;
region.y = y;
@ -70,12 +70,12 @@ int x11_shadow_update_encode(freerdp_peer* client, int x, int y, int width, int
RFX_RECT rect;
XImage* image;
rdpUpdate* update;
xfPeerContext* xfp;
x11ShadowClient* context;
x11ShadowServer* server;
SURFACE_BITS_COMMAND* cmd;
xfp = (xfPeerContext*) client->context;
server = xfp->server;
context = (x11ShadowClient*) client->context;
server = context->server;
update = client->update;
cmd = &update->surface_bits_command;
@ -86,7 +86,7 @@ int x11_shadow_update_encode(freerdp_peer* client, int x, int y, int width, int
return -1;
}
s = xfp->s;
s = context->s;
Stream_Clear(s);
Stream_SetPosition(s, 0);
@ -102,12 +102,12 @@ int x11_shadow_update_encode(freerdp_peer* client, int x, int y, int width, int
rect.width = width;
rect.height = height;
image = x11_shadow_snapshot(xfp, x, y, width, height);
image = x11_shadow_snapshot(context, x, y, width, height);
data = (BYTE*) image->data;
data = &data[(y * image->bytes_per_line) + (x * image->bits_per_pixel / 8)];
rfx_compose_message(xfp->rfx_context, s, &rect, 1, data,
rfx_compose_message(context->rfx_context, s, &rect, 1, data,
width, height, image->bytes_per_line);
cmd->destLeft = x;
@ -122,11 +122,11 @@ int x11_shadow_update_encode(freerdp_peer* client, int x, int y, int width, int
rect.width = width;
rect.height = height;
image = x11_shadow_snapshot(xfp, x, y, width, height);
image = x11_shadow_snapshot(context, x, y, width, height);
data = (BYTE*) image->data;
rfx_compose_message(xfp->rfx_context, s, &rect, 1, data,
rfx_compose_message(context->rfx_context, s, &rect, 1, data,
width, height, image->bytes_per_line);
cmd->destLeft = x;
@ -147,23 +147,35 @@ int x11_shadow_update_encode(freerdp_peer* client, int x, int y, int width, int
return 0;
}
void x11_shadow_client_send_update(freerdp_peer* client)
{
rdpUpdate* update;
SURFACE_BITS_COMMAND* cmd;
update = client->update;
cmd = &update->surface_bits_command;
if (cmd->bitmapDataLength)
update->SurfaceBits(update->context, cmd);
}
void* x11_shadow_update_thread(void* param)
{
HANDLE event;
XEvent xevent;
DWORD beg, end;
DWORD diff, rate;
xfPeerContext* xfp;
x11ShadowClient* context;
x11ShadowServer* server;
freerdp_peer* client;
int x, y, width, height;
XDamageNotifyEvent* notify;
client = (freerdp_peer*) param;
xfp = (xfPeerContext*) client->context;
server = xfp->server;
context = (x11ShadowClient*) client->context;
server = context->server;
rate = 1000 / xfp->fps;
rate = 1000 / 10;
event = CreateFileDescriptorEvent(NULL, FALSE, FALSE, server->xfds);
@ -187,12 +199,10 @@ void* x11_shadow_update_thread(void* param)
if (x11_shadow_update_encode(client, x, y, width, height) >= 0)
{
x11_shadow_xdamage_subtract_region(xfp, x, y, width, height);
x11_shadow_xdamage_subtract_region(context, x, y, width, height);
SetEvent(xfp->updateReadyEvent);
WaitForSingleObject(xfp->updateSentEvent, INFINITE);
ResetEvent(xfp->updateSentEvent);
if (context->activated)
x11_shadow_client_send_update(client);
}
}
#ifdef WITH_XFIXES