FreeRDP/client/X11/xf_cliprdr.c

1166 lines
28 KiB
C
Raw Normal View History

2011-09-23 07:37:17 +04:00
/**
2012-10-09 07:02:04 +04:00
* FreeRDP: A Remote Desktop Protocol Implementation
2011-09-23 07:37:17 +04:00
* X11 Clipboard Redirection
*
* Copyright 2010-2011 Vic Lee
*
* 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
2011-09-23 07:37:17 +04:00
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#ifdef WITH_XFIXES
#include <X11/extensions/Xfixes.h>
#endif
#include <winpr/crt.h>
#include <winpr/image.h>
#include <winpr/stream.h>
#include <winpr/clipboard.h>
2014-09-12 19:13:01 +04:00
#include <freerdp/log.h>
#include <freerdp/client/cliprdr.h>
#include <freerdp/channels/channels.h>
2011-09-23 07:37:17 +04:00
#include "xf_cliprdr.h"
2014-09-12 19:13:01 +04:00
#define TAG CLIENT_TAG("x11")
2014-10-15 23:49:57 +04:00
struct xf_cliprdr_format
2011-09-23 07:37:17 +04:00
{
2014-10-15 23:49:57 +04:00
Atom atom;
UINT32 formatId;
char* formatName;
2011-09-23 07:37:17 +04:00
};
2014-10-15 23:49:57 +04:00
typedef struct xf_cliprdr_format xfCliprdrFormat;
2011-09-23 07:37:17 +04:00
2014-10-15 06:24:07 +04:00
struct xf_clipboard
2011-09-23 07:37:17 +04:00
{
2014-10-15 06:24:07 +04:00
xfContext* xfc;
rdpChannels* channels;
CliprdrClientContext* context;
wClipboard* system;
2011-09-23 07:37:17 +04:00
Window root_window;
Atom clipboard_atom;
Atom property_atom;
Atom identity_atom;
2014-10-15 23:49:57 +04:00
int numClientFormats;
xfCliprdrFormat clientFormats[20];
int numServerFormats;
CLIPRDR_FORMAT* serverFormats;
2011-09-23 07:37:17 +04:00
2014-10-15 23:49:57 +04:00
int numTargets;
2011-09-23 07:37:17 +04:00
Atom targets[20];
2014-10-15 23:49:57 +04:00
int requestedFormatId;
BYTE* data;
2012-10-09 11:26:39 +04:00
UINT32 data_format;
UINT32 data_alt_format;
2011-09-23 07:37:17 +04:00
int data_length;
XEvent* respond;
Window owner;
BOOL sync;
2011-09-23 07:37:17 +04:00
/* INCR mechanism */
Atom incr_atom;
BOOL incr_starts;
BYTE* incr_data;
2011-09-23 07:37:17 +04:00
int incr_data_length;
/* XFixes extension */
int xfixes_event_base;
int xfixes_error_base;
BOOL xfixes_supported;
2011-09-23 07:37:17 +04:00
};
2014-10-16 06:48:18 +04:00
int xf_cliprdr_send_client_format_list(xfClipboard* clipboard);
static void xf_cliprdr_check_owner(xfClipboard* clipboard)
{
Window owner;
xfContext* xfc = clipboard->xfc;
if (clipboard->sync)
{
owner = XGetSelectionOwner(xfc->display, clipboard->clipboard_atom);
if (clipboard->owner != owner)
{
clipboard->owner = owner;
xf_cliprdr_send_client_format_list(clipboard);
}
}
}
2014-10-15 06:24:07 +04:00
static BOOL xf_cliprdr_is_self_owned(xfClipboard* clipboard)
2011-09-23 07:37:17 +04:00
{
Atom type;
2012-10-09 11:26:39 +04:00
UINT32 id = 0;
UINT32* pid = NULL;
2011-09-23 07:37:17 +04:00
int format, result = 0;
unsigned long length;
unsigned long bytes_left;
2014-10-15 06:24:07 +04:00
xfContext* xfc = clipboard->xfc;
2011-09-23 07:37:17 +04:00
2014-10-15 06:24:07 +04:00
clipboard->owner = XGetSelectionOwner(xfc->display, clipboard->clipboard_atom);
2014-10-15 06:24:07 +04:00
if (clipboard->owner != None)
2011-09-23 07:37:17 +04:00
{
2014-10-15 06:24:07 +04:00
result = XGetWindowProperty(xfc->display, clipboard->owner,
clipboard->identity_atom, 0, 4, 0, XA_INTEGER,
&type, &format, &length, &bytes_left, (BYTE**) &pid);
2011-09-23 07:37:17 +04:00
}
2011-09-23 07:37:17 +04:00
if (pid)
{
id = *pid;
XFree(pid);
}
2014-10-15 06:24:07 +04:00
if ((clipboard->owner == None) || (clipboard->owner == xfc->drawable))
return FALSE;
2011-09-23 07:37:17 +04:00
if (result != Success)
return FALSE;
return (id ? TRUE : FALSE);
2011-09-23 07:37:17 +04:00
}
2014-10-15 23:49:57 +04:00
static xfCliprdrFormat* xf_cliprdr_get_format_by_id(xfClipboard* clipboard, UINT32 formatId)
2011-09-23 07:37:17 +04:00
{
2014-10-15 23:49:57 +04:00
UINT32 index;
xfCliprdrFormat* format;
2011-09-23 07:37:17 +04:00
2014-10-15 23:49:57 +04:00
for (index = 0; index < clipboard->numClientFormats; index++)
2011-09-23 07:37:17 +04:00
{
2014-10-15 23:49:57 +04:00
format = &(clipboard->clientFormats[index]);
if (format->formatId == formatId)
return format;
2011-09-23 07:37:17 +04:00
}
2014-10-15 23:49:57 +04:00
return NULL;
2011-09-23 07:37:17 +04:00
}
2014-10-15 23:49:57 +04:00
static xfCliprdrFormat* xf_cliprdr_get_format_by_atom(xfClipboard* clipboard, Atom atom)
2011-09-23 07:37:17 +04:00
{
2014-10-15 23:49:57 +04:00
int i, j;
xfCliprdrFormat* format;
2011-09-23 07:37:17 +04:00
2014-10-15 23:49:57 +04:00
for (i = 0; i < clipboard->numClientFormats; i++)
2011-09-23 07:37:17 +04:00
{
2014-10-15 23:49:57 +04:00
format = &(clipboard->clientFormats[i]);
if (format->atom != atom)
2011-09-23 07:37:17 +04:00
continue;
if (format->formatId == 0)
2014-10-15 23:49:57 +04:00
return format;
2011-09-23 07:37:17 +04:00
2014-10-15 23:49:57 +04:00
for (j = 0; j < clipboard->numServerFormats; j++)
2011-09-23 07:37:17 +04:00
{
2014-10-15 23:49:57 +04:00
if (clipboard->serverFormats[j].formatId == format->formatId)
return format;
2011-09-23 07:37:17 +04:00
}
}
2014-10-15 23:49:57 +04:00
return NULL;
2011-09-23 07:37:17 +04:00
}
static void xf_cliprdr_send_data_request(xfClipboard* clipboard, UINT32 formatId)
{
CLIPRDR_FORMAT_DATA_REQUEST request;
ZeroMemory(&request, sizeof(CLIPRDR_FORMAT_DATA_REQUEST));
request.requestedFormatId = formatId;
clipboard->context->ClientFormatDataRequest(clipboard->context, &request);
}
static void xf_cliprdr_send_data_response(xfClipboard* clipboard, BYTE* data, int size)
{
CLIPRDR_FORMAT_DATA_RESPONSE response;
ZeroMemory(&response, sizeof(CLIPRDR_FORMAT_DATA_RESPONSE));
response.msgFlags = CB_RESPONSE_OK;
response.dataLen = size;
response.requestedFormatData = data;
clipboard->context->ClientFormatDataResponse(clipboard->context, &response);
}
static void xf_cliprdr_get_requested_targets(xfClipboard* clipboard)
{
int i;
Atom atom;
BYTE* data = NULL;
int format_property;
unsigned long length;
unsigned long bytes_left;
UINT32 numFormats = 0;
CLIPRDR_FORMAT_LIST formatList;
xfCliprdrFormat* format = NULL;
CLIPRDR_FORMAT* formats = NULL;
xfContext* xfc = clipboard->xfc;
if (!clipboard->numServerFormats)
return; /* server format list was not yet received */
XGetWindowProperty(xfc->display, xfc->drawable, clipboard->property_atom,
0, 200, 0, XA_ATOM, &atom, &format_property, &length, &bytes_left, &data);
if (length > 0)
formats = (CLIPRDR_FORMAT*) calloc(length, sizeof(CLIPRDR_FORMAT));
for (i = 0; i < length; i++)
{
atom = ((Atom*) data)[i];
format = xf_cliprdr_get_format_by_atom(clipboard, atom);
if (format)
{
formats[numFormats].formatId = format->formatId;
formats[numFormats].formatName = format->formatName;
numFormats++;
}
}
XFree(data);
ZeroMemory(&formatList, sizeof(CLIPRDR_FORMAT_LIST));
formatList.msgFlags = CB_RESPONSE_OK;
formatList.numFormats = numFormats;
formatList.formats = formats;
clipboard->context->ClientFormatList(clipboard->context, &formatList);
free(formats);
}
static void xf_cliprdr_process_requested_data(xfClipboard* clipboard, BOOL hasData, BYTE* data, int size)
2011-09-23 07:37:17 +04:00
{
BOOL bSuccess;
UINT32 SrcSize;
UINT32 DstSize;
UINT32 formatId;
UINT32 altFormatId;
BYTE* pSrcData = NULL;
BYTE* pDstData = NULL;
2014-10-15 23:49:57 +04:00
xfCliprdrFormat* format;
2011-09-23 07:37:17 +04:00
if (clipboard->incr_starts && hasData)
2011-09-23 07:37:17 +04:00
return;
2014-10-15 23:49:57 +04:00
format = xf_cliprdr_get_format_by_id(clipboard, clipboard->requestedFormatId);
if (!hasData || !data || !format)
2011-09-23 07:37:17 +04:00
{
2014-10-15 23:49:57 +04:00
xf_cliprdr_send_data_response(clipboard, NULL, 0);
2011-09-23 07:37:17 +04:00
return;
}
formatId = 0;
altFormatId = 0;
2014-10-15 23:49:57 +04:00
switch (format->formatId)
2011-09-23 07:37:17 +04:00
{
case CF_TEXT:
case CF_OEMTEXT:
case CF_UNICODETEXT:
size = strlen((char*) data) + 1;
formatId = ClipboardGetFormatId(clipboard->system, "UTF8_STRING");
2011-09-23 07:37:17 +04:00
break;
case CF_DIB:
formatId = ClipboardGetFormatId(clipboard->system, "image/bmp");
2011-09-23 07:37:17 +04:00
break;
case CB_FORMAT_HTML:
size = strlen((char*) data) + 1;
formatId = ClipboardGetFormatId(clipboard->system, "text/html");
2011-09-23 07:37:17 +04:00
break;
}
SrcSize = (UINT32) size;
pSrcData = (BYTE*) malloc(SrcSize);
if (!pSrcData)
return;
CopyMemory(pSrcData, data, SrcSize);
bSuccess = ClipboardSetData(clipboard->system, formatId, (void*) pSrcData, SrcSize);
2014-11-17 02:26:33 +03:00
if (!bSuccess)
free(pSrcData);
altFormatId = clipboard->requestedFormatId;
2011-09-23 07:37:17 +04:00
if (bSuccess && altFormatId)
{
DstSize = 0;
pDstData = (BYTE*) ClipboardGetData(clipboard->system, altFormatId, &DstSize);
}
if (!pDstData)
{
xf_cliprdr_send_data_response(clipboard, NULL, 0);
return;
}
xf_cliprdr_send_data_response(clipboard, pDstData, (int) DstSize);
free(pDstData);
2011-09-23 07:37:17 +04:00
}
2014-10-15 06:24:07 +04:00
static BOOL xf_cliprdr_get_requested_data(xfClipboard* clipboard, Atom target)
2011-09-23 07:37:17 +04:00
{
Atom type;
BYTE* data = NULL;
BOOL has_data = FALSE;
2014-10-15 23:49:57 +04:00
int format_property;
unsigned long dummy;
unsigned long length;
unsigned long bytes_left;
xfCliprdrFormat* format;
2014-10-15 06:24:07 +04:00
xfContext* xfc = clipboard->xfc;
2011-09-23 07:37:17 +04:00
2014-10-15 23:49:57 +04:00
format = xf_cliprdr_get_format_by_id(clipboard, clipboard->requestedFormatId);
if (!format || (format->atom != target))
2011-09-23 07:37:17 +04:00
{
2014-10-15 23:49:57 +04:00
xf_cliprdr_send_data_response(clipboard, NULL, 0);
return FALSE;
2011-09-23 07:37:17 +04:00
}
XGetWindowProperty(xfc->display, xfc->drawable,
2014-10-15 06:24:07 +04:00
clipboard->property_atom, 0, 0, 0, target,
2014-10-15 23:49:57 +04:00
&type, &format_property, &length, &bytes_left, &data);
2011-09-23 07:37:17 +04:00
if (data)
{
XFree(data);
data = NULL;
}
2014-10-15 06:24:07 +04:00
if (bytes_left <= 0 && !clipboard->incr_starts)
2011-09-23 07:37:17 +04:00
{
2014-10-15 23:49:57 +04:00
2011-09-23 07:37:17 +04:00
}
2014-10-15 06:24:07 +04:00
else if (type == clipboard->incr_atom)
2011-09-23 07:37:17 +04:00
{
2014-10-15 06:24:07 +04:00
clipboard->incr_starts = TRUE;
2014-10-15 06:24:07 +04:00
if (clipboard->incr_data)
2011-09-23 07:37:17 +04:00
{
2014-10-15 06:24:07 +04:00
free(clipboard->incr_data);
clipboard->incr_data = NULL;
2011-09-23 07:37:17 +04:00
}
2014-10-15 06:24:07 +04:00
clipboard->incr_data_length = 0;
has_data = TRUE; /* data will be followed in PropertyNotify event */
2011-09-23 07:37:17 +04:00
}
else
{
if (bytes_left <= 0)
{
/* INCR finish */
2014-10-15 06:24:07 +04:00
data = clipboard->incr_data;
clipboard->incr_data = NULL;
bytes_left = clipboard->incr_data_length;
clipboard->incr_data_length = 0;
clipboard->incr_starts = 0;
has_data = TRUE;
2011-09-23 07:37:17 +04:00
}
else if (XGetWindowProperty(xfc->display, xfc->drawable,
2014-10-15 06:24:07 +04:00
clipboard->property_atom, 0, bytes_left, 0, target,
2014-10-15 23:49:57 +04:00
&type, &format_property, &length, &dummy, &data) == Success)
2011-09-23 07:37:17 +04:00
{
2014-10-15 06:24:07 +04:00
if (clipboard->incr_starts)
2011-09-23 07:37:17 +04:00
{
2014-10-15 23:49:57 +04:00
bytes_left = length * format_property / 8;
2014-10-15 06:24:07 +04:00
clipboard->incr_data = (BYTE*) realloc(clipboard->incr_data, clipboard->incr_data_length + bytes_left);
CopyMemory(clipboard->incr_data + clipboard->incr_data_length, data, bytes_left);
clipboard->incr_data_length += bytes_left;
2011-09-23 07:37:17 +04:00
XFree(data);
data = NULL;
}
has_data = TRUE;
2011-09-23 07:37:17 +04:00
}
else
{
2014-10-15 23:49:57 +04:00
2011-09-23 07:37:17 +04:00
}
}
2014-10-15 23:49:57 +04:00
2014-10-15 06:24:07 +04:00
XDeleteProperty(xfc->display, xfc->drawable, clipboard->property_atom);
2011-09-23 07:37:17 +04:00
2014-10-15 06:24:07 +04:00
xf_cliprdr_process_requested_data(clipboard, has_data, data, (int) bytes_left);
2011-09-23 07:37:17 +04:00
if (data)
XFree(data);
return TRUE;
2011-09-23 07:37:17 +04:00
}
2014-10-15 06:24:07 +04:00
static void xf_cliprdr_append_target(xfClipboard* clipboard, Atom target)
2011-09-23 07:37:17 +04:00
{
int i;
2014-10-15 23:49:57 +04:00
if (clipboard->numTargets >= ARRAYSIZE(clipboard->targets))
2011-09-23 07:37:17 +04:00
return;
2014-10-15 23:49:57 +04:00
for (i = 0; i < clipboard->numTargets; i++)
2011-09-23 07:37:17 +04:00
{
2014-10-15 06:24:07 +04:00
if (clipboard->targets[i] == target)
2011-09-23 07:37:17 +04:00
return;
}
2014-10-15 23:49:57 +04:00
clipboard->targets[clipboard->numTargets++] = target;
2011-09-23 07:37:17 +04:00
}
2014-10-15 06:24:07 +04:00
static void xf_cliprdr_provide_targets(xfClipboard* clipboard, XEvent* respond)
2011-09-23 07:37:17 +04:00
{
2014-10-15 06:24:07 +04:00
xfContext* xfc = clipboard->xfc;
2011-09-23 07:37:17 +04:00
if (respond->xselection.property != None)
{
2014-10-15 23:49:57 +04:00
XChangeProperty(xfc->display, respond->xselection.requestor,
respond->xselection.property, XA_ATOM, 32, PropModeReplace,
(BYTE*) clipboard->targets, clipboard->numTargets);
2011-09-23 07:37:17 +04:00
}
}
static void xf_cliprdr_provide_data(xfClipboard* clipboard, XEvent* respond, BYTE* data, UINT32 size)
2011-09-23 07:37:17 +04:00
{
2014-10-15 06:24:07 +04:00
xfContext* xfc = clipboard->xfc;
2011-09-23 07:37:17 +04:00
if (respond->xselection.property != None)
{
2014-10-15 23:49:57 +04:00
XChangeProperty(xfc->display, respond->xselection.requestor,
respond->xselection.property, respond->xselection.target,
8, PropModeReplace, data, size);
2011-09-23 07:37:17 +04:00
}
}
2014-10-15 06:24:07 +04:00
static BOOL xf_cliprdr_process_selection_notify(xfClipboard* clipboard, XEvent* xevent)
2011-09-23 07:37:17 +04:00
{
2014-10-15 06:24:07 +04:00
if (xevent->xselection.target == clipboard->targets[1])
2011-09-23 07:37:17 +04:00
{
if (xevent->xselection.property == None)
{
2014-10-16 23:05:06 +04:00
xf_cliprdr_send_client_format_list(clipboard);
2011-09-23 07:37:17 +04:00
}
else
{
2014-10-15 06:24:07 +04:00
xf_cliprdr_get_requested_targets(clipboard);
2011-09-23 07:37:17 +04:00
}
return TRUE;
2011-09-23 07:37:17 +04:00
}
else
{
2014-10-15 06:24:07 +04:00
return xf_cliprdr_get_requested_data(clipboard, xevent->xselection.target);
2011-09-23 07:37:17 +04:00
}
}
2014-10-15 06:24:07 +04:00
static BOOL xf_cliprdr_process_selection_request(xfClipboard* clipboard, XEvent* xevent)
2011-09-23 07:37:17 +04:00
{
int fmt;
Atom type;
2014-10-15 23:49:57 +04:00
UINT32 formatId;
2011-09-23 07:37:17 +04:00
XEvent* respond;
2014-10-15 23:49:57 +04:00
UINT32 altFormatId;
BYTE* data = NULL;
BOOL delayRespond;
2014-10-15 23:49:57 +04:00
unsigned long length;
unsigned long bytes_left;
xfCliprdrFormat* format;
2014-10-15 06:24:07 +04:00
xfContext* xfc = clipboard->xfc;
2011-09-23 07:37:17 +04:00
if (xevent->xselectionrequest.owner != xfc->drawable)
return FALSE;
2011-09-23 07:37:17 +04:00
delayRespond = FALSE;
2014-10-15 06:24:07 +04:00
respond = (XEvent*) calloc(1, sizeof(XEvent));
2011-09-23 07:37:17 +04:00
respond->xselection.property = None;
respond->xselection.type = SelectionNotify;
respond->xselection.display = xevent->xselectionrequest.display;
respond->xselection.requestor = xevent->xselectionrequest.requestor;
respond->xselection.selection = xevent->xselectionrequest.selection;
respond->xselection.target = xevent->xselectionrequest.target;
respond->xselection.time = xevent->xselectionrequest.time;
2014-10-15 06:24:07 +04:00
if (xevent->xselectionrequest.target == clipboard->targets[0]) /* TIMESTAMP */
2011-09-23 07:37:17 +04:00
{
/* TODO */
}
2014-10-15 06:24:07 +04:00
else if (xevent->xselectionrequest.target == clipboard->targets[1]) /* TARGETS */
2011-09-23 07:37:17 +04:00
{
/* Someone else requests our available formats */
respond->xselection.property = xevent->xselectionrequest.property;
2014-10-15 06:24:07 +04:00
xf_cliprdr_provide_targets(clipboard, respond);
2011-09-23 07:37:17 +04:00
}
else
{
2014-10-15 23:49:57 +04:00
format = xf_cliprdr_get_format_by_atom(clipboard, xevent->xselectionrequest.target);
2014-10-15 23:49:57 +04:00
if (format && (xevent->xselectionrequest.requestor != xfc->drawable))
2011-09-23 07:37:17 +04:00
{
2014-10-15 23:49:57 +04:00
formatId = format->formatId;
altFormatId = formatId;
if (formatId == 0)
2011-09-23 07:37:17 +04:00
{
if (XGetWindowProperty(xfc->display, xevent->xselectionrequest.requestor,
2014-10-15 06:24:07 +04:00
clipboard->property_atom, 0, 4, 0, XA_INTEGER,
&type, &fmt, &length, &bytes_left, &data) != Success)
2011-09-23 07:37:17 +04:00
{
2014-10-16 06:48:18 +04:00
2011-09-23 07:37:17 +04:00
}
2014-10-16 06:48:18 +04:00
2011-09-23 07:37:17 +04:00
if (data)
{
2014-10-15 23:49:57 +04:00
CopyMemory(&altFormatId, data, 4);
2011-09-23 07:37:17 +04:00
XFree(data);
}
}
2014-10-15 23:49:57 +04:00
if ((clipboard->data != 0) && (formatId == clipboard->data_format) && (altFormatId == clipboard->data_alt_format))
2011-09-23 07:37:17 +04:00
{
/* Cached clipboard data available. Send it now */
respond->xselection.property = xevent->xselectionrequest.property;
xf_cliprdr_provide_data(clipboard, respond, clipboard->data, clipboard->data_length);
2011-09-23 07:37:17 +04:00
}
2014-10-15 06:24:07 +04:00
else if (clipboard->respond)
2011-09-23 07:37:17 +04:00
{
2014-10-16 06:48:18 +04:00
/* duplicate request */
2011-09-23 07:37:17 +04:00
}
else
{
/**
* Send clipboard data request to the server.
* Response will be postponed after receiving the data
*/
2014-10-15 06:24:07 +04:00
if (clipboard->data)
2011-09-23 07:37:17 +04:00
{
2014-10-15 06:24:07 +04:00
free(clipboard->data);
clipboard->data = NULL;
2011-09-23 07:37:17 +04:00
}
2011-09-23 07:37:17 +04:00
respond->xselection.property = xevent->xselectionrequest.property;
2014-10-15 06:24:07 +04:00
clipboard->respond = respond;
2014-10-15 23:49:57 +04:00
clipboard->data_format = formatId;
clipboard->data_alt_format = altFormatId;
delayRespond = TRUE;
2011-09-23 07:37:17 +04:00
2014-10-15 23:49:57 +04:00
xf_cliprdr_send_data_request(clipboard, altFormatId);
2011-09-23 07:37:17 +04:00
}
}
}
if (!delayRespond)
2011-09-23 07:37:17 +04:00
{
XSendEvent(xfc->display, xevent->xselectionrequest.requestor, 0, 0, respond);
XFlush(xfc->display);
free(respond);
2011-09-23 07:37:17 +04:00
}
return TRUE;
2011-09-23 07:37:17 +04:00
}
2014-10-15 06:24:07 +04:00
static BOOL xf_cliprdr_process_selection_clear(xfClipboard* clipboard, XEvent* xevent)
2011-09-23 07:37:17 +04:00
{
2014-10-15 06:24:07 +04:00
xfContext* xfc = clipboard->xfc;
2011-09-23 07:37:17 +04:00
2014-10-15 06:24:07 +04:00
if (xf_cliprdr_is_self_owned(clipboard))
return FALSE;
2011-09-23 07:37:17 +04:00
2014-10-15 06:24:07 +04:00
XDeleteProperty(xfc->display, clipboard->root_window, clipboard->property_atom);
2011-09-23 07:37:17 +04:00
return TRUE;
2011-09-23 07:37:17 +04:00
}
2014-10-15 06:24:07 +04:00
static BOOL xf_cliprdr_process_property_notify(xfClipboard* clipboard, XEvent* xevent)
2011-09-23 07:37:17 +04:00
{
2014-10-15 23:49:57 +04:00
xfCliprdrFormat* format;
2014-10-15 06:24:07 +04:00
xfContext* xfc = clipboard->xfc;
2011-09-23 07:37:17 +04:00
2014-10-15 06:24:07 +04:00
if (!clipboard)
return TRUE;
2014-10-15 06:24:07 +04:00
if (xevent->xproperty.atom != clipboard->property_atom)
return FALSE; /* Not cliprdr-related */
2011-09-23 07:37:17 +04:00
2014-10-15 06:24:07 +04:00
if (xevent->xproperty.window == clipboard->root_window)
2011-09-23 07:37:17 +04:00
{
2014-10-16 06:48:18 +04:00
xf_cliprdr_send_client_format_list(clipboard);
2011-09-23 07:37:17 +04:00
}
2014-10-15 23:49:57 +04:00
else if ((xevent->xproperty.window == xfc->drawable) &&
(xevent->xproperty.state == PropertyNewValue) && clipboard->incr_starts)
2011-09-23 07:37:17 +04:00
{
2014-10-15 23:49:57 +04:00
format = xf_cliprdr_get_format_by_id(clipboard, clipboard->requestedFormatId);
if (format)
xf_cliprdr_get_requested_data(clipboard, format->atom);
2011-09-23 07:37:17 +04:00
}
return TRUE;
2011-09-23 07:37:17 +04:00
}
void xf_cliprdr_handle_xevent(xfContext* xfc, XEvent* event)
{
2014-10-15 06:24:07 +04:00
xfClipboard* clipboard;
if (!xfc || !event)
return;
2014-10-15 23:49:57 +04:00
clipboard = xfc->clipboard;
if (!clipboard)
return;
#ifdef WITH_XFIXES
2014-10-15 06:24:07 +04:00
if (clipboard->xfixes_supported && event->type == XFixesSelectionNotify + clipboard->xfixes_event_base)
{
XFixesSelectionNotifyEvent* se = (XFixesSelectionNotifyEvent*) event;
2014-10-15 06:24:07 +04:00
if (se->subtype == XFixesSetSelectionOwnerNotify)
{
2014-10-15 06:24:07 +04:00
if (se->selection != clipboard->clipboard_atom)
return;
if (XGetSelectionOwner(xfc->display, se->selection) == xfc->drawable)
return;
2014-10-15 06:24:07 +04:00
clipboard->owner = None;
2014-10-15 23:49:57 +04:00
xf_cliprdr_check_owner(clipboard);
}
2014-10-15 06:24:07 +04:00
return;
}
#endif
2014-10-15 06:24:07 +04:00
switch (event->type)
{
case SelectionNotify:
2014-10-15 23:49:57 +04:00
xf_cliprdr_process_selection_notify(clipboard, event);
break;
2014-10-15 06:24:07 +04:00
case SelectionRequest:
2014-10-15 23:49:57 +04:00
xf_cliprdr_process_selection_request(clipboard, event);
break;
2014-10-15 06:24:07 +04:00
case SelectionClear:
2014-10-15 23:49:57 +04:00
xf_cliprdr_process_selection_clear(clipboard, event);
break;
2014-10-15 06:24:07 +04:00
case PropertyNotify:
2014-10-15 23:49:57 +04:00
xf_cliprdr_process_property_notify(clipboard, event);
break;
2014-10-15 06:24:07 +04:00
case FocusIn:
2014-10-15 06:24:07 +04:00
if (!clipboard->xfixes_supported)
{
2014-10-15 23:49:57 +04:00
xf_cliprdr_check_owner(clipboard);
}
break;
}
}
2014-10-15 06:24:07 +04:00
int xf_cliprdr_send_client_capabilities(xfClipboard* clipboard)
2014-10-15 06:24:07 +04:00
{
CLIPRDR_CAPABILITIES capabilities;
CLIPRDR_GENERAL_CAPABILITY_SET generalCapabilitySet;
capabilities.cCapabilitiesSets = 1;
capabilities.capabilitySets = (CLIPRDR_CAPABILITY_SET*) &(generalCapabilitySet);
generalCapabilitySet.capabilitySetType = CB_CAPSTYPE_GENERAL;
generalCapabilitySet.capabilitySetLength = 12;
generalCapabilitySet.version = CB_CAPS_VERSION_2;
generalCapabilitySet.generalFlags = CB_USE_LONG_FORMAT_NAMES;
clipboard->context->ClientCapabilities(clipboard->context, &capabilities);
return 1;
}
int xf_cliprdr_send_client_format_list(xfClipboard* clipboard)
{
2014-10-16 23:05:06 +04:00
UINT32 i, numFormats;
CLIPRDR_FORMAT* formats;
2014-10-16 06:48:18 +04:00
CLIPRDR_FORMAT_LIST formatList;
xfContext* xfc = clipboard->xfc;
ZeroMemory(&formatList, sizeof(CLIPRDR_FORMAT_LIST));
2014-10-16 23:05:06 +04:00
numFormats = clipboard->numClientFormats;
formats = (CLIPRDR_FORMAT*) calloc(numFormats, sizeof(CLIPRDR_FORMAT));
2014-10-16 06:48:18 +04:00
2014-10-16 23:05:06 +04:00
for (i = 0; i < numFormats; i++)
{
formats[i].formatId = clipboard->clientFormats[i].formatId;
formats[i].formatName = clipboard->clientFormats[i].formatName;
2014-10-16 06:48:18 +04:00
}
2014-10-16 23:05:06 +04:00
formatList.msgFlags = CB_RESPONSE_OK;
formatList.numFormats = numFormats;
2014-10-16 23:05:06 +04:00
formatList.formats = formats;
2014-10-16 06:48:18 +04:00
2014-10-16 23:05:06 +04:00
clipboard->context->ClientFormatList(clipboard->context, &formatList);
2014-10-16 06:48:18 +04:00
2014-10-16 23:05:06 +04:00
free(formats);
2014-10-16 06:48:18 +04:00
if (clipboard->owner && clipboard->owner != xfc->drawable)
2014-10-16 06:48:18 +04:00
{
/* Request the owner for TARGETS, and wait for SelectionNotify event */
XConvertSelection(xfc->display, clipboard->clipboard_atom,
clipboard->targets[1], clipboard->property_atom, xfc->drawable, CurrentTime);
}
return 1;
}
int xf_cliprdr_send_client_format_list_response(xfClipboard* clipboard, BOOL status)
{
CLIPRDR_FORMAT_LIST_RESPONSE formatListResponse;
formatListResponse.msgType = CB_FORMAT_LIST_RESPONSE;
formatListResponse.msgFlags = status ? CB_RESPONSE_OK : CB_RESPONSE_FAIL;
formatListResponse.dataLen = 0;
clipboard->context->ClientFormatListResponse(clipboard->context, &formatListResponse);
return 1;
}
int xf_cliprdr_send_client_format_data_request(xfClipboard* clipboard, UINT32 formatId)
{
CLIPRDR_FORMAT_DATA_REQUEST formatDataRequest;
formatDataRequest.msgType = CB_FORMAT_DATA_REQUEST;
formatDataRequest.msgFlags = CB_RESPONSE_OK;
formatDataRequest.requestedFormatId = formatId;
clipboard->requestedFormatId = formatId;
clipboard->context->ClientFormatDataRequest(clipboard->context, &formatDataRequest);
return 1;
}
static int xf_cliprdr_monitor_ready(CliprdrClientContext* context, CLIPRDR_MONITOR_READY* monitorReady)
{
xfClipboard* clipboard = (xfClipboard*) context->custom;
xf_cliprdr_send_client_capabilities(clipboard);
xf_cliprdr_send_client_format_list(clipboard);
2014-10-15 23:49:57 +04:00
clipboard->sync = TRUE;
return 1;
}
2014-10-15 06:24:07 +04:00
static int xf_cliprdr_server_capabilities(CliprdrClientContext* context, CLIPRDR_CAPABILITIES* capabilities)
{
//xfClipboard* clipboard = (xfClipboard*) context->custom;
return 1;
2014-10-15 06:24:07 +04:00
}
static int xf_cliprdr_server_format_list(CliprdrClientContext* context, CLIPRDR_FORMAT_LIST* formatList)
{
2014-10-15 23:49:57 +04:00
int i, j;
CLIPRDR_FORMAT* format;
xfClipboard* clipboard = (xfClipboard*) context->custom;
xfContext* xfc = clipboard->xfc;
2014-10-15 06:24:07 +04:00
2014-10-15 23:49:57 +04:00
if (clipboard->data)
{
free(clipboard->data);
clipboard->data = NULL;
}
if (clipboard->serverFormats)
{
2014-10-16 06:48:18 +04:00
for (i = 0; i < clipboard->numServerFormats; i++)
free(clipboard->serverFormats[i].formatName);
2014-10-15 23:49:57 +04:00
free(clipboard->serverFormats);
clipboard->serverFormats = NULL;
2014-10-16 23:05:06 +04:00
clipboard->numServerFormats = 0;
2014-10-15 23:49:57 +04:00
}
clipboard->numServerFormats = formatList->numFormats;
2014-10-15 23:49:57 +04:00
clipboard->serverFormats = (CLIPRDR_FORMAT*) calloc(clipboard->numServerFormats, sizeof(CLIPRDR_FORMAT));
if (!clipboard->serverFormats)
return -1;
for (i = 0; i < formatList->numFormats; i++)
2014-10-15 23:49:57 +04:00
{
format = &formatList->formats[i];
clipboard->serverFormats[i].formatId = format->formatId;
clipboard->serverFormats[i].formatName = _strdup(format->formatName);
}
clipboard->numTargets = 2;
for (i = 0; i < formatList->numFormats; i++)
2014-10-15 23:49:57 +04:00
{
format = &formatList->formats[i];
for (j = 0; j < clipboard->numClientFormats; j++)
{
if (format->formatId == clipboard->clientFormats[j].formatId)
{
xf_cliprdr_append_target(clipboard, clipboard->clientFormats[j].atom);
}
}
}
xf_cliprdr_send_client_format_list_response(clipboard, TRUE);
2014-10-16 06:48:18 +04:00
XSetSelectionOwner(xfc->display, clipboard->clipboard_atom, xfc->drawable, CurrentTime);
2014-10-16 06:48:18 +04:00
XFlush(xfc->display);
return 1;
2014-10-15 06:24:07 +04:00
}
static int xf_cliprdr_server_format_list_response(CliprdrClientContext* context, CLIPRDR_FORMAT_LIST_RESPONSE* formatListResponse)
{
//xfClipboard* clipboard = (xfClipboard*) context->custom;
return 1;
2014-10-15 06:24:07 +04:00
}
static int xf_cliprdr_server_format_data_request(CliprdrClientContext* context, CLIPRDR_FORMAT_DATA_REQUEST* formatDataRequest)
{
xfCliprdrFormat* format = NULL;
UINT32 formatId = formatDataRequest->requestedFormatId;
xfClipboard* clipboard = (xfClipboard*) context->custom;
xfContext* xfc = clipboard->xfc;
if (xf_cliprdr_is_self_owned(clipboard))
{
format = xf_cliprdr_get_format_by_id(clipboard, 0);
XChangeProperty(xfc->display, xfc->drawable, clipboard->property_atom,
XA_INTEGER, 32, PropModeReplace, (BYTE*) &formatId, 1);
}
else
{
format = xf_cliprdr_get_format_by_id(clipboard, formatId);
}
if (!format)
{
xf_cliprdr_send_data_response(clipboard, NULL, 0);
return 1;
}
clipboard->requestedFormatId = formatId;
XConvertSelection(xfc->display, clipboard->clipboard_atom,
format->atom, clipboard->property_atom, xfc->drawable, CurrentTime);
XFlush(xfc->display);
/* After this point, we expect a SelectionNotify event from the clipboard owner. */
return 1;
2014-10-15 06:24:07 +04:00
}
static int xf_cliprdr_server_format_data_response(CliprdrClientContext* context, CLIPRDR_FORMAT_DATA_RESPONSE* formatDataResponse)
{
BOOL bSuccess;
BYTE* pSrcData;
2014-10-16 23:05:06 +04:00
BYTE* pDstData;
UINT32 DstSize;
UINT32 SrcSize;
UINT32 formatId;
UINT32 altFormatId;
xfCliprdrFormat* format;
BOOL nullTerminated = FALSE;
UINT32 size = formatDataResponse->dataLen;
BYTE* data = formatDataResponse->requestedFormatData;
xfClipboard* clipboard = (xfClipboard*) context->custom;
xfContext* xfc = clipboard->xfc;
if (!clipboard->respond)
return 1;
format = xf_cliprdr_get_format_by_id(clipboard, clipboard->requestedFormatId);
if (clipboard->data)
{
free(clipboard->data);
clipboard->data = NULL;
}
pDstData = NULL;
2014-12-07 02:29:28 +03:00
DstSize = 0;
2014-10-16 23:05:06 +04:00
formatId = 0;
altFormatId = 0;
switch (clipboard->data_format)
{
case CF_TEXT:
formatId = CF_TEXT;
altFormatId = ClipboardGetFormatId(clipboard->system, "UTF8_STRING");
nullTerminated = TRUE;
break;
case CF_OEMTEXT:
formatId = CF_OEMTEXT;
altFormatId = ClipboardGetFormatId(clipboard->system, "UTF8_STRING");
nullTerminated = TRUE;
break;
case CF_UNICODETEXT:
formatId = CF_UNICODETEXT;
altFormatId = ClipboardGetFormatId(clipboard->system, "UTF8_STRING");
nullTerminated = TRUE;
break;
case CF_DIB:
formatId = CF_DIB;
altFormatId = ClipboardGetFormatId(clipboard->system, "image/bmp");
break;
case CB_FORMAT_HTML:
formatId = ClipboardGetFormatId(clipboard->system, "HTML Format");
altFormatId = ClipboardGetFormatId(clipboard->system, "text/html");
nullTerminated = TRUE;
break;
}
SrcSize = (UINT32) size;
pSrcData = (BYTE*) malloc(SrcSize);
if (!pSrcData)
return -1;
CopyMemory(pSrcData, data, SrcSize);
bSuccess = ClipboardSetData(clipboard->system, formatId, (void*) pSrcData, SrcSize);
2014-11-17 02:26:33 +03:00
if (!bSuccess)
free (pSrcData);
2014-10-16 23:05:06 +04:00
if (bSuccess && altFormatId)
{
DstSize = 0;
pDstData = (BYTE*) ClipboardGetData(clipboard->system, altFormatId, &DstSize);
if ((DstSize > 1) && nullTerminated)
DstSize--;
}
clipboard->data = pDstData;
clipboard->data_length = DstSize;
xf_cliprdr_provide_data(clipboard, clipboard->respond, pDstData, DstSize);
XSendEvent(xfc->display, clipboard->respond->xselection.requestor, 0, 0, clipboard->respond);
XFlush(xfc->display);
free(clipboard->respond);
clipboard->respond = NULL;
return 1;
2014-10-15 06:24:07 +04:00
}
xfClipboard* xf_clipboard_new(xfContext* xfc)
{
int n;
UINT32 id;
rdpChannels* channels;
xfClipboard* clipboard;
clipboard = (xfClipboard*) calloc(1, sizeof(xfClipboard));
xfc->clipboard = clipboard;
clipboard->xfc = xfc;
channels = ((rdpContext*) xfc)->channels;
clipboard->channels = channels;
clipboard->system = ClipboardCreate();
2014-10-15 23:49:57 +04:00
clipboard->requestedFormatId = -1;
2014-10-15 06:24:07 +04:00
clipboard->root_window = DefaultRootWindow(xfc->display);
clipboard->clipboard_atom = XInternAtom(xfc->display, "CLIPBOARD", FALSE);
if (clipboard->clipboard_atom == None)
{
WLog_ERR(TAG, "unable to get CLIPBOARD atom");
2014-10-16 06:56:25 +04:00
free(clipboard);
return NULL;
2014-10-15 06:24:07 +04:00
}
id = 1;
clipboard->property_atom = XInternAtom(xfc->display, "_FREERDP_CLIPRDR", FALSE);
clipboard->identity_atom = XInternAtom(xfc->display, "_FREERDP_CLIPRDR_ID", FALSE);
XChangeProperty(xfc->display, xfc->drawable, clipboard->identity_atom,
XA_INTEGER, 32, PropModeReplace, (BYTE*) &id, 1);
XSelectInput(xfc->display, clipboard->root_window, PropertyChangeMask);
#ifdef WITH_XFIXES
if (XFixesQueryExtension(xfc->display, &clipboard->xfixes_event_base, &clipboard->xfixes_error_base))
{
int xfmajor, xfminor;
2014-10-16 06:48:18 +04:00
2014-10-15 06:24:07 +04:00
if (XFixesQueryVersion(xfc->display, &xfmajor, &xfminor))
{
XFixesSelectSelectionInput(xfc->display, clipboard->root_window,
clipboard->clipboard_atom, XFixesSetSelectionOwnerNotifyMask);
clipboard->xfixes_supported = TRUE;
}
else
{
WLog_ERR(TAG, "Error querying X Fixes extension version");
}
}
else
{
WLog_ERR(TAG, "Error loading X Fixes extension");
}
#else
WLog_ERR(TAG, "Warning: Using clipboard redirection without XFIXES extension is strongly discouraged!");
#endif
n = 0;
clipboard->clientFormats[n].atom = XInternAtom(xfc->display, "_FREERDP_RAW", False);
clipboard->clientFormats[n].formatId = 0;
2014-10-15 23:49:57 +04:00
n++;
clipboard->clientFormats[n].atom = XInternAtom(xfc->display, "UTF8_STRING", False);
clipboard->clientFormats[n].formatId = CF_UNICODETEXT;
2014-10-15 06:24:07 +04:00
n++;
2014-10-15 23:49:57 +04:00
clipboard->clientFormats[n].atom = XA_STRING;
clipboard->clientFormats[n].formatId = CF_TEXT;
2014-10-15 06:24:07 +04:00
n++;
clipboard->clientFormats[n].atom = XInternAtom(xfc->display, "image/png", False);
2014-10-15 23:49:57 +04:00
clipboard->clientFormats[n].formatId = CB_FORMAT_PNG;
2014-10-15 06:24:07 +04:00
n++;
clipboard->clientFormats[n].atom = XInternAtom(xfc->display, "image/jpeg", False);
2014-10-15 23:49:57 +04:00
clipboard->clientFormats[n].formatId = CB_FORMAT_JPEG;
2014-10-15 06:24:07 +04:00
n++;
clipboard->clientFormats[n].atom = XInternAtom(xfc->display, "image/gif", False);
2014-10-15 23:49:57 +04:00
clipboard->clientFormats[n].formatId = CB_FORMAT_GIF;
2014-10-15 06:24:07 +04:00
n++;
clipboard->clientFormats[n].atom = XInternAtom(xfc->display, "image/bmp", False);
clipboard->clientFormats[n].formatId = CF_DIB;
2014-10-15 06:24:07 +04:00
n++;
clipboard->clientFormats[n].atom = XInternAtom(xfc->display, "text/html", False);
2014-10-15 23:49:57 +04:00
clipboard->clientFormats[n].formatId = CB_FORMAT_HTML;
clipboard->clientFormats[n].formatName = _strdup("HTML Format");
2014-10-15 06:24:07 +04:00
n++;
2014-10-15 23:49:57 +04:00
clipboard->numClientFormats = n;
2014-10-15 06:24:07 +04:00
clipboard->targets[0] = XInternAtom(xfc->display, "TIMESTAMP", FALSE);
clipboard->targets[1] = XInternAtom(xfc->display, "TARGETS", FALSE);
2014-10-15 23:49:57 +04:00
clipboard->numTargets = 2;
2014-10-15 06:24:07 +04:00
clipboard->incr_atom = XInternAtom(xfc->display, "INCR", FALSE);
return clipboard;
}
void xf_clipboard_free(xfClipboard* clipboard)
{
2014-10-16 06:56:25 +04:00
int i;
2014-10-15 06:24:07 +04:00
if (!clipboard)
return;
2014-10-16 06:56:25 +04:00
if (clipboard->serverFormats)
{
for (i = 0; i < clipboard->numServerFormats; i++)
free(clipboard->serverFormats[i].formatName);
free(clipboard->serverFormats);
clipboard->serverFormats = NULL;
}
2014-12-03 22:17:27 +03:00
if (clipboard->numClientFormats)
{
for (i = 0; i < clipboard->numClientFormats; i++)
free(clipboard->clientFormats[i].formatName);
}
ClipboardDestroy(clipboard->system);
2014-10-15 06:24:07 +04:00
free(clipboard->data);
free(clipboard->respond);
free(clipboard->incr_data);
free(clipboard);
}
void xf_cliprdr_init(xfContext* xfc, CliprdrClientContext* cliprdr)
{
xfc->cliprdr = cliprdr;
xfc->clipboard->context = cliprdr;
2014-10-15 06:24:07 +04:00
cliprdr->custom = (void*) xfc->clipboard;
cliprdr->MonitorReady = xf_cliprdr_monitor_ready;
cliprdr->ServerCapabilities = xf_cliprdr_server_capabilities;
cliprdr->ServerFormatList = xf_cliprdr_server_format_list;
cliprdr->ServerFormatListResponse = xf_cliprdr_server_format_list_response;
cliprdr->ServerFormatDataRequest = xf_cliprdr_server_format_data_request;
cliprdr->ServerFormatDataResponse = xf_cliprdr_server_format_data_response;
}
void xf_cliprdr_uninit(xfContext* xfc, CliprdrClientContext* cliprdr)
{
xfc->cliprdr = NULL;
cliprdr->custom = NULL;
if (xfc->clipboard)
xfc->clipboard->context = NULL;
2014-10-15 06:24:07 +04:00
}