Merge pull request #2851 from ilammy/x11-cliprdr/improve-format-support
Improve clipboard format support in X11 client
This commit is contained in:
commit
05aa425cae
@ -44,6 +44,8 @@
|
||||
|
||||
#define TAG CLIENT_TAG("x11")
|
||||
|
||||
#define MAX_CLIPBOARD_FORMATS 255
|
||||
|
||||
struct xf_cliprdr_format
|
||||
{
|
||||
Atom atom;
|
||||
@ -64,6 +66,9 @@ struct xf_clipboard
|
||||
Atom clipboard_atom;
|
||||
Atom property_atom;
|
||||
|
||||
Atom raw_transfer_atom;
|
||||
Atom raw_format_list_atom;
|
||||
|
||||
int numClientFormats;
|
||||
xfCliprdrFormat clientFormats[20];
|
||||
|
||||
@ -76,8 +81,9 @@ struct xf_clipboard
|
||||
int requestedFormatId;
|
||||
|
||||
BYTE* data;
|
||||
UINT32 data_format;
|
||||
UINT32 data_alt_format;
|
||||
BOOL data_raw_format;
|
||||
UINT32 data_format_id;
|
||||
const char* data_format_name;
|
||||
int data_length;
|
||||
XEvent* respond;
|
||||
|
||||
@ -122,9 +128,70 @@ static BOOL xf_cliprdr_is_self_owned(xfClipboard* clipboard)
|
||||
return XGetSelectionOwner(xfc->display, clipboard->clipboard_atom) == xfc->drawable;
|
||||
}
|
||||
|
||||
static xfCliprdrFormat* xf_cliprdr_get_format_by_id(xfClipboard* clipboard, UINT32 formatId)
|
||||
static void xf_cliprdr_set_raw_transfer_enabled(xfClipboard* clipboard, BOOL enabled)
|
||||
{
|
||||
UINT32 index;
|
||||
UINT32 data = enabled;
|
||||
xfContext* xfc = clipboard->xfc;
|
||||
|
||||
XChangeProperty(xfc->display, xfc->drawable, clipboard->raw_transfer_atom,
|
||||
XA_INTEGER, 32, PropModeReplace, (BYTE*) &data, 1);
|
||||
}
|
||||
|
||||
static BOOL xf_cliprdr_is_raw_transfer_available(xfClipboard* clipboard)
|
||||
{
|
||||
Atom type;
|
||||
int format;
|
||||
int result = 0;
|
||||
unsigned long length;
|
||||
unsigned long bytes_left;
|
||||
UINT32* data = NULL;
|
||||
UINT32 is_enabled = 0;
|
||||
Window owner = None;
|
||||
xfContext* xfc = clipboard->xfc;
|
||||
|
||||
owner = XGetSelectionOwner(xfc->display, clipboard->clipboard_atom);
|
||||
|
||||
if (owner != None)
|
||||
{
|
||||
result = XGetWindowProperty(xfc->display, owner,
|
||||
clipboard->raw_transfer_atom, 0, 4, 0, XA_INTEGER,
|
||||
&type, &format, &length, &bytes_left, (BYTE**) &data);
|
||||
}
|
||||
|
||||
if (data)
|
||||
{
|
||||
is_enabled = *data;
|
||||
XFree(data);
|
||||
}
|
||||
|
||||
if ((owner == None) || (owner == xfc->drawable))
|
||||
return FALSE;
|
||||
|
||||
if (result != Success)
|
||||
return FALSE;
|
||||
|
||||
return is_enabled ? TRUE : FALSE;
|
||||
}
|
||||
|
||||
static BOOL xf_cliprdr_formats_equal(const CLIPRDR_FORMAT* server, const xfCliprdrFormat* client)
|
||||
{
|
||||
if (server->formatName && client->formatName)
|
||||
{
|
||||
/* The server may be using short format names while we store them in full form. */
|
||||
return (0 == strncmp(server->formatName, client->formatName, strlen(server->formatName)));
|
||||
}
|
||||
|
||||
if (!server->formatName && !client->formatName)
|
||||
{
|
||||
return (server->formatId == client->formatId);
|
||||
}
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
static xfCliprdrFormat* xf_cliprdr_get_client_format_by_id(xfClipboard* clipboard, UINT32 formatId)
|
||||
{
|
||||
int index;
|
||||
xfCliprdrFormat* format;
|
||||
|
||||
for (index = 0; index < clipboard->numClientFormats; index++)
|
||||
@ -138,25 +205,41 @@ static xfCliprdrFormat* xf_cliprdr_get_format_by_id(xfClipboard* clipboard, UINT
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static xfCliprdrFormat* xf_cliprdr_get_format_by_atom(xfClipboard* clipboard, Atom atom)
|
||||
static xfCliprdrFormat* xf_cliprdr_get_client_format_by_atom(xfClipboard* clipboard, Atom atom)
|
||||
{
|
||||
int i, j;
|
||||
int i;
|
||||
xfCliprdrFormat* format;
|
||||
|
||||
for (i = 0; i < clipboard->numClientFormats; i++)
|
||||
{
|
||||
format = &(clipboard->clientFormats[i]);
|
||||
|
||||
if (format->atom != atom)
|
||||
continue;
|
||||
|
||||
if (format->formatId == 0)
|
||||
if (format->atom == atom)
|
||||
return format;
|
||||
}
|
||||
|
||||
for (j = 0; j < clipboard->numServerFormats; j++)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static CLIPRDR_FORMAT* xf_cliprdr_get_server_format_by_atom(xfClipboard* clipboard, Atom atom)
|
||||
{
|
||||
int i, j;
|
||||
xfCliprdrFormat* client_format;
|
||||
CLIPRDR_FORMAT* server_format;
|
||||
|
||||
for (i = 0; i < clipboard->numClientFormats; i++)
|
||||
{
|
||||
client_format = &(clipboard->clientFormats[i]);
|
||||
|
||||
if (client_format->atom == atom)
|
||||
{
|
||||
if (clipboard->serverFormats[j].formatId == format->formatId)
|
||||
return format;
|
||||
for (j = 0; j < clipboard->numServerFormats; j++)
|
||||
{
|
||||
server_format = &(clipboard->serverFormats[j]);
|
||||
|
||||
if (xf_cliprdr_formats_equal(server_format, client_format))
|
||||
return server_format;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -190,14 +273,170 @@ static UINT xf_cliprdr_send_data_response(xfClipboard* clipboard, BYTE* data, in
|
||||
|
||||
ZeroMemory(&response, sizeof(CLIPRDR_FORMAT_DATA_RESPONSE));
|
||||
|
||||
response.msgFlags = CB_RESPONSE_OK;
|
||||
response.msgFlags = (data) ? CB_RESPONSE_OK : CB_RESPONSE_FAIL;
|
||||
response.dataLen = size;
|
||||
response.requestedFormatData = data;
|
||||
|
||||
return clipboard->context->ClientFormatDataResponse(clipboard->context, &response);
|
||||
}
|
||||
|
||||
static void xf_cliprdr_get_requested_targets(xfClipboard* clipboard)
|
||||
static wStream* xf_cliprdr_serialize_server_format_list(xfClipboard* clipboard)
|
||||
{
|
||||
UINT32 i;
|
||||
UINT32 formatCount;
|
||||
wStream* s = NULL;
|
||||
|
||||
/* Typical MS Word format list is about 80 bytes long. */
|
||||
if (!(s = Stream_New(NULL, 128)))
|
||||
{
|
||||
WLog_ERR(TAG, "failed to allocate serialized format list");
|
||||
goto error;
|
||||
}
|
||||
|
||||
/* If present, the last format is always synthetic CF_RAW. Do not include it. */
|
||||
formatCount = (clipboard->numServerFormats > 0) ? clipboard->numServerFormats - 1 : 0;
|
||||
|
||||
Stream_Write_UINT32(s, formatCount);
|
||||
|
||||
for (i = 0; i < formatCount; i++)
|
||||
{
|
||||
CLIPRDR_FORMAT* format = &clipboard->serverFormats[i];
|
||||
size_t name_length = format->formatName ? strlen(format->formatName) : 0;
|
||||
|
||||
if (!Stream_EnsureRemainingCapacity(s, sizeof(UINT32) + name_length + 1))
|
||||
{
|
||||
WLog_ERR(TAG, "failed to expand serialized format list");
|
||||
goto error;
|
||||
}
|
||||
|
||||
Stream_Write_UINT32(s, format->formatId);
|
||||
Stream_Write(s, format->formatName, name_length);
|
||||
Stream_Write_UINT8(s, '\0');
|
||||
}
|
||||
|
||||
Stream_SealLength(s);
|
||||
|
||||
return s;
|
||||
|
||||
error:
|
||||
Stream_Free(s, TRUE);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static CLIPRDR_FORMAT* xf_cliprdr_parse_server_format_list(BYTE* data, size_t length, UINT32* numFormats)
|
||||
{
|
||||
UINT32 i;
|
||||
wStream* s = NULL;
|
||||
CLIPRDR_FORMAT* formats = NULL;
|
||||
|
||||
if (!(s = Stream_New(data, length)))
|
||||
{
|
||||
WLog_ERR(TAG, "failed to allocate stream for parsing serialized format list");
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (Stream_GetRemainingLength(s) < sizeof(UINT32))
|
||||
{
|
||||
WLog_ERR(TAG, "too short serialized format list");
|
||||
goto error;
|
||||
}
|
||||
|
||||
Stream_Read_UINT32(s, *numFormats);
|
||||
|
||||
if (*numFormats > MAX_CLIPBOARD_FORMATS)
|
||||
{
|
||||
WLog_ERR(TAG, "unexpectedly large number of formats: %u", *numFormats);
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (!(formats = (CLIPRDR_FORMAT*) calloc(*numFormats, sizeof(CLIPRDR_FORMAT))))
|
||||
{
|
||||
WLog_ERR(TAG, "failed to allocate format list");
|
||||
goto error;
|
||||
}
|
||||
|
||||
for (i = 0; i < *numFormats; i++)
|
||||
{
|
||||
const char* formatName = NULL;
|
||||
size_t formatNameLength = 0;
|
||||
|
||||
if (Stream_GetRemainingLength(s) < sizeof(UINT32))
|
||||
{
|
||||
WLog_ERR(TAG, "unexpected end of serialized format list");
|
||||
goto error;
|
||||
}
|
||||
|
||||
Stream_Read_UINT32(s, formats[i].formatId);
|
||||
|
||||
formatName = (const char*) Stream_Pointer(s);
|
||||
formatNameLength = strnlen(formatName, Stream_GetRemainingLength(s));
|
||||
|
||||
if (formatNameLength == Stream_GetRemainingLength(s))
|
||||
{
|
||||
WLog_ERR(TAG, "missing terminating null byte, %zu bytes left to read", formatNameLength);
|
||||
goto error;
|
||||
}
|
||||
|
||||
formats[i].formatName = strndup(formatName, formatNameLength);
|
||||
Stream_Seek(s, formatNameLength + 1);
|
||||
}
|
||||
|
||||
Stream_Free(s, FALSE);
|
||||
|
||||
return formats;
|
||||
|
||||
error:
|
||||
Stream_Free(s, FALSE);
|
||||
free(formats);
|
||||
*numFormats = 0;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void xf_cliprdr_free_formats(CLIPRDR_FORMAT* formats, UINT32 numFormats)
|
||||
{
|
||||
UINT32 i;
|
||||
|
||||
for (i = 0; i < numFormats; i++)
|
||||
{
|
||||
free(formats[i].formatName);
|
||||
}
|
||||
|
||||
free(formats);
|
||||
}
|
||||
|
||||
static CLIPRDR_FORMAT* xf_cliprdr_get_raw_server_formats(xfClipboard* clipboard, UINT32* numFormats)
|
||||
{
|
||||
Atom type = None;
|
||||
int format = 0;
|
||||
unsigned long length = 0;
|
||||
unsigned long remaining;
|
||||
BYTE* data = NULL;
|
||||
CLIPRDR_FORMAT* formats = NULL;
|
||||
xfContext* xfc = clipboard->xfc;
|
||||
|
||||
*numFormats = 0;
|
||||
|
||||
XGetWindowProperty(xfc->display, clipboard->owner, clipboard->raw_format_list_atom,
|
||||
0, 4096, False, clipboard->raw_format_list_atom, &type, &format,
|
||||
&length, &remaining, &data);
|
||||
|
||||
if (data && length > 0 && format == 8 && type == clipboard->raw_format_list_atom)
|
||||
{
|
||||
formats = xf_cliprdr_parse_server_format_list(data, length, numFormats);
|
||||
}
|
||||
else
|
||||
{
|
||||
WLog_ERR(TAG, "failed to retrieve raw format list: data=%p, length=%lu, format=%d, type=%d (expected=%d)",
|
||||
data, length, format, type, clipboard->raw_format_list_atom);
|
||||
}
|
||||
|
||||
if (data)
|
||||
XFree(data);
|
||||
|
||||
return formats;
|
||||
}
|
||||
|
||||
static CLIPRDR_FORMAT* xf_cliprdr_get_formats_from_targets(xfClipboard* clipboard, UINT32* numFormats)
|
||||
{
|
||||
int i;
|
||||
Atom atom;
|
||||
@ -205,14 +444,11 @@ static void xf_cliprdr_get_requested_targets(xfClipboard* clipboard)
|
||||
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 */
|
||||
*numFormats = 0;
|
||||
|
||||
XGetWindowProperty(xfc->display, xfc->drawable, clipboard->property_atom,
|
||||
0, 200, 0, XA_ATOM, &atom, &format_property, &length, &bytes_left, &data);
|
||||
@ -235,16 +471,73 @@ static void xf_cliprdr_get_requested_targets(xfClipboard* clipboard)
|
||||
{
|
||||
atom = ((Atom*) data)[i];
|
||||
|
||||
format = xf_cliprdr_get_format_by_atom(clipboard, atom);
|
||||
format = xf_cliprdr_get_client_format_by_atom(clipboard, atom);
|
||||
|
||||
if (format)
|
||||
{
|
||||
formats[numFormats].formatId = format->formatId;
|
||||
formats[numFormats].formatName = format->formatName;
|
||||
numFormats++;
|
||||
formats[*numFormats].formatId = format->formatId;
|
||||
formats[*numFormats].formatName = _strdup(format->formatName);
|
||||
*numFormats += 1;
|
||||
}
|
||||
}
|
||||
|
||||
out:
|
||||
if (data)
|
||||
XFree(data);
|
||||
|
||||
return formats;
|
||||
}
|
||||
|
||||
static CLIPRDR_FORMAT* xf_cliprdr_get_client_formats(xfClipboard* clipboard, UINT32* numFormats)
|
||||
{
|
||||
CLIPRDR_FORMAT* formats = NULL;
|
||||
|
||||
*numFormats = 0;
|
||||
|
||||
if (xf_cliprdr_is_raw_transfer_available(clipboard))
|
||||
{
|
||||
formats = xf_cliprdr_get_raw_server_formats(clipboard, numFormats);
|
||||
}
|
||||
|
||||
if (*numFormats == 0)
|
||||
{
|
||||
xf_cliprdr_free_formats(formats, *numFormats);
|
||||
|
||||
formats = xf_cliprdr_get_formats_from_targets(clipboard, numFormats);
|
||||
}
|
||||
|
||||
return formats;
|
||||
}
|
||||
|
||||
static void xf_cliprdr_provide_server_format_list(xfClipboard* clipboard)
|
||||
{
|
||||
wStream* formats = NULL;
|
||||
xfContext* xfc = clipboard->xfc;
|
||||
|
||||
formats = xf_cliprdr_serialize_server_format_list(clipboard);
|
||||
|
||||
if (formats)
|
||||
{
|
||||
XChangeProperty(xfc->display, xfc->drawable, clipboard->raw_format_list_atom,
|
||||
clipboard->raw_format_list_atom, 8, PropModeReplace,
|
||||
Stream_Buffer(formats), Stream_Length(formats));
|
||||
}
|
||||
else
|
||||
{
|
||||
XDeleteProperty(xfc->display, xfc->drawable, clipboard->raw_format_list_atom);
|
||||
}
|
||||
|
||||
Stream_Free(formats, TRUE);
|
||||
}
|
||||
|
||||
static void xf_cliprdr_get_requested_targets(xfClipboard* clipboard)
|
||||
{
|
||||
UINT32 numFormats = 0;
|
||||
CLIPRDR_FORMAT* formats = NULL;
|
||||
CLIPRDR_FORMAT_LIST formatList;
|
||||
|
||||
formats = xf_cliprdr_get_client_formats(clipboard, &numFormats);
|
||||
|
||||
ZeroMemory(&formatList, sizeof(CLIPRDR_FORMAT_LIST));
|
||||
|
||||
formatList.msgFlags = CB_RESPONSE_OK;
|
||||
@ -253,10 +546,7 @@ static void xf_cliprdr_get_requested_targets(xfClipboard* clipboard)
|
||||
|
||||
clipboard->context->ClientFormatList(clipboard->context, &formatList);
|
||||
|
||||
out:
|
||||
if (data)
|
||||
XFree(data);
|
||||
free(formats);
|
||||
xf_cliprdr_free_formats(formats, numFormats);
|
||||
}
|
||||
|
||||
static void xf_cliprdr_process_requested_data(xfClipboard* clipboard, BOOL hasData, BYTE* data, int size)
|
||||
@ -264,8 +554,8 @@ static void xf_cliprdr_process_requested_data(xfClipboard* clipboard, BOOL hasDa
|
||||
BOOL bSuccess;
|
||||
UINT32 SrcSize;
|
||||
UINT32 DstSize;
|
||||
UINT32 formatId;
|
||||
UINT32 altFormatId;
|
||||
UINT32 srcFormatId;
|
||||
UINT32 dstFormatId;
|
||||
BYTE* pSrcData = NULL;
|
||||
BYTE* pDstData = NULL;
|
||||
xfCliprdrFormat* format;
|
||||
@ -273,7 +563,7 @@ static void xf_cliprdr_process_requested_data(xfClipboard* clipboard, BOOL hasDa
|
||||
if (clipboard->incr_starts && hasData)
|
||||
return;
|
||||
|
||||
format = xf_cliprdr_get_format_by_id(clipboard, clipboard->requestedFormatId);
|
||||
format = xf_cliprdr_get_client_format_by_id(clipboard, clipboard->requestedFormatId);
|
||||
|
||||
if (!hasData || !data || !format)
|
||||
{
|
||||
@ -281,25 +571,29 @@ static void xf_cliprdr_process_requested_data(xfClipboard* clipboard, BOOL hasDa
|
||||
return;
|
||||
}
|
||||
|
||||
formatId = 0;
|
||||
altFormatId = 0;
|
||||
srcFormatId = 0;
|
||||
dstFormatId = 0;
|
||||
|
||||
switch (format->formatId)
|
||||
{
|
||||
case CF_RAW:
|
||||
srcFormatId = CF_RAW;
|
||||
break;
|
||||
|
||||
case CF_TEXT:
|
||||
case CF_OEMTEXT:
|
||||
case CF_UNICODETEXT:
|
||||
size = strlen((char*) data) + 1;
|
||||
formatId = ClipboardGetFormatId(clipboard->system, "UTF8_STRING");
|
||||
srcFormatId = ClipboardGetFormatId(clipboard->system, "UTF8_STRING");
|
||||
break;
|
||||
|
||||
case CF_DIB:
|
||||
formatId = ClipboardGetFormatId(clipboard->system, "image/bmp");
|
||||
srcFormatId = ClipboardGetFormatId(clipboard->system, "image/bmp");
|
||||
break;
|
||||
|
||||
case CB_FORMAT_HTML:
|
||||
size = strlen((char*) data) + 1;
|
||||
formatId = ClipboardGetFormatId(clipboard->system, "text/html");
|
||||
srcFormatId = ClipboardGetFormatId(clipboard->system, "text/html");
|
||||
break;
|
||||
}
|
||||
|
||||
@ -311,17 +605,24 @@ static void xf_cliprdr_process_requested_data(xfClipboard* clipboard, BOOL hasDa
|
||||
|
||||
CopyMemory(pSrcData, data, SrcSize);
|
||||
|
||||
bSuccess = ClipboardSetData(clipboard->system, formatId, (void*) pSrcData, SrcSize);
|
||||
bSuccess = ClipboardSetData(clipboard->system, srcFormatId, (void*) pSrcData, SrcSize);
|
||||
|
||||
if (!bSuccess)
|
||||
free(pSrcData);
|
||||
|
||||
altFormatId = clipboard->requestedFormatId;
|
||||
if (format->formatName)
|
||||
{
|
||||
dstFormatId = ClipboardGetFormatId(clipboard->system, format->formatName);
|
||||
}
|
||||
else
|
||||
{
|
||||
dstFormatId = format->formatId;
|
||||
}
|
||||
|
||||
if (bSuccess && altFormatId)
|
||||
if (bSuccess)
|
||||
{
|
||||
DstSize = 0;
|
||||
pDstData = (BYTE*) ClipboardGetData(clipboard->system, altFormatId, &DstSize);
|
||||
pDstData = (BYTE*) ClipboardGetData(clipboard->system, dstFormatId, &DstSize);
|
||||
}
|
||||
|
||||
if (!pDstData)
|
||||
@ -346,7 +647,7 @@ static BOOL xf_cliprdr_get_requested_data(xfClipboard* clipboard, Atom target)
|
||||
xfCliprdrFormat* format;
|
||||
xfContext* xfc = clipboard->xfc;
|
||||
|
||||
format = xf_cliprdr_get_format_by_id(clipboard, clipboard->requestedFormatId);
|
||||
format = xf_cliprdr_get_client_format_by_id(clipboard, clipboard->requestedFormatId);
|
||||
|
||||
if (!format || (format->atom != target))
|
||||
{
|
||||
@ -495,13 +796,14 @@ static BOOL xf_cliprdr_process_selection_request(xfClipboard* clipboard, XEvent*
|
||||
int fmt;
|
||||
Atom type;
|
||||
UINT32 formatId;
|
||||
const char* formatName;
|
||||
XEvent* respond;
|
||||
UINT32 altFormatId;
|
||||
BYTE* data = NULL;
|
||||
BOOL delayRespond;
|
||||
BOOL rawTransfer;
|
||||
unsigned long length;
|
||||
unsigned long bytes_left;
|
||||
xfCliprdrFormat* format;
|
||||
CLIPRDR_FORMAT* format;
|
||||
xfContext* xfc = clipboard->xfc;
|
||||
|
||||
if (xevent->xselectionrequest.owner != xfc->drawable)
|
||||
@ -535,14 +837,15 @@ static BOOL xf_cliprdr_process_selection_request(xfClipboard* clipboard, XEvent*
|
||||
}
|
||||
else
|
||||
{
|
||||
format = xf_cliprdr_get_format_by_atom(clipboard, xevent->xselectionrequest.target);
|
||||
format = xf_cliprdr_get_server_format_by_atom(clipboard, xevent->xselectionrequest.target);
|
||||
|
||||
if (format && (xevent->xselectionrequest.requestor != xfc->drawable))
|
||||
{
|
||||
formatId = format->formatId;
|
||||
altFormatId = formatId;
|
||||
formatName = format->formatName;
|
||||
rawTransfer = FALSE;
|
||||
|
||||
if (formatId == 0)
|
||||
if (formatId == CF_RAW)
|
||||
{
|
||||
if (XGetWindowProperty(xfc->display, xevent->xselectionrequest.requestor,
|
||||
clipboard->property_atom, 0, 4, 0, XA_INTEGER,
|
||||
@ -553,12 +856,13 @@ static BOOL xf_cliprdr_process_selection_request(xfClipboard* clipboard, XEvent*
|
||||
|
||||
if (data)
|
||||
{
|
||||
CopyMemory(&altFormatId, data, 4);
|
||||
rawTransfer = TRUE;
|
||||
CopyMemory(&formatId, data, 4);
|
||||
XFree(data);
|
||||
}
|
||||
}
|
||||
|
||||
if ((clipboard->data != 0) && (formatId == clipboard->data_format) && (altFormatId == clipboard->data_alt_format))
|
||||
if ((clipboard->data != 0) && (formatId == clipboard->data_format_id) && (formatName == clipboard->data_format_name))
|
||||
{
|
||||
/* Cached clipboard data available. Send it now */
|
||||
respond->xselection.property = xevent->xselectionrequest.property;
|
||||
@ -582,11 +886,12 @@ static BOOL xf_cliprdr_process_selection_request(xfClipboard* clipboard, XEvent*
|
||||
|
||||
respond->xselection.property = xevent->xselectionrequest.property;
|
||||
clipboard->respond = respond;
|
||||
clipboard->data_format = formatId;
|
||||
clipboard->data_alt_format = altFormatId;
|
||||
clipboard->data_format_id = formatId;
|
||||
clipboard->data_format_name = formatName;
|
||||
clipboard->data_raw_format = rawTransfer;
|
||||
delayRespond = TRUE;
|
||||
|
||||
xf_cliprdr_send_data_request(clipboard, altFormatId);
|
||||
xf_cliprdr_send_data_request(clipboard, formatId);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -631,7 +936,7 @@ static BOOL xf_cliprdr_process_property_notify(xfClipboard* clipboard, XEvent* x
|
||||
else if ((xevent->xproperty.window == xfc->drawable) &&
|
||||
(xevent->xproperty.state == PropertyNewValue) && clipboard->incr_starts)
|
||||
{
|
||||
format = xf_cliprdr_get_format_by_id(clipboard, clipboard->requestedFormatId);
|
||||
format = xf_cliprdr_get_client_format_by_id(clipboard, clipboard->requestedFormatId);
|
||||
|
||||
if (format)
|
||||
xf_cliprdr_get_requested_data(clipboard, format->atom);
|
||||
@ -787,24 +1092,6 @@ UINT xf_cliprdr_send_client_format_list_response(xfClipboard* clipboard, BOOL st
|
||||
return clipboard->context->ClientFormatListResponse(clipboard->context, &formatListResponse);
|
||||
}
|
||||
|
||||
/**
|
||||
* Function description
|
||||
*
|
||||
* @return 0 on success, otherwise a Win32 error code
|
||||
*/
|
||||
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;
|
||||
|
||||
return clipboard->context->ClientFormatDataRequest(clipboard->context, &formatDataRequest);
|
||||
}
|
||||
|
||||
/**
|
||||
* Function description
|
||||
*
|
||||
@ -856,6 +1143,9 @@ static UINT xf_cliprdr_server_format_list(CliprdrClientContext* context, CLIPRDR
|
||||
clipboard->data = NULL;
|
||||
}
|
||||
|
||||
clipboard->data_format_id = -1;
|
||||
clipboard->data_format_name = NULL;
|
||||
|
||||
if (clipboard->serverFormats)
|
||||
{
|
||||
for (i = 0; i < clipboard->numServerFormats; i++)
|
||||
@ -867,14 +1157,11 @@ static UINT xf_cliprdr_server_format_list(CliprdrClientContext* context, CLIPRDR
|
||||
clipboard->numServerFormats = 0;
|
||||
}
|
||||
|
||||
clipboard->numServerFormats = formatList->numFormats;
|
||||
clipboard->numServerFormats = formatList->numFormats + 1; /* +1 for CF_RAW */
|
||||
|
||||
if (clipboard->numServerFormats)
|
||||
{
|
||||
if (!(clipboard->serverFormats = (CLIPRDR_FORMAT*) calloc(clipboard->numServerFormats, sizeof(CLIPRDR_FORMAT)))) {
|
||||
WLog_ERR(TAG, "failed to allocate %d CLIPRDR_FORMAT structs", clipboard->numServerFormats);
|
||||
return CHANNEL_RC_NO_MEMORY;
|
||||
}
|
||||
if (!(clipboard->serverFormats = (CLIPRDR_FORMAT*) calloc(clipboard->numServerFormats, sizeof(CLIPRDR_FORMAT)))) {
|
||||
WLog_ERR(TAG, "failed to allocate %d CLIPRDR_FORMAT structs", clipboard->numServerFormats);
|
||||
return CHANNEL_RC_NO_MEMORY;
|
||||
}
|
||||
|
||||
for (i = 0; i < formatList->numFormats; i++)
|
||||
@ -897,6 +1184,13 @@ static UINT xf_cliprdr_server_format_list(CliprdrClientContext* context, CLIPRDR
|
||||
}
|
||||
}
|
||||
|
||||
/* CF_RAW is always implicitly supported by the server */
|
||||
format = &clipboard->serverFormats[formatList->numFormats];
|
||||
format->formatId = CF_RAW;
|
||||
format->formatName = NULL;
|
||||
|
||||
xf_cliprdr_provide_server_format_list(clipboard);
|
||||
|
||||
clipboard->numTargets = 2;
|
||||
|
||||
for (i = 0; i < formatList->numFormats; i++)
|
||||
@ -905,7 +1199,7 @@ static UINT xf_cliprdr_server_format_list(CliprdrClientContext* context, CLIPRDR
|
||||
|
||||
for (j = 0; j < clipboard->numClientFormats; j++)
|
||||
{
|
||||
if (format->formatId == clipboard->clientFormats[j].formatId)
|
||||
if (xf_cliprdr_formats_equal(format, &clipboard->clientFormats[j]))
|
||||
{
|
||||
xf_cliprdr_append_target(clipboard, clipboard->clientFormats[j].atom);
|
||||
}
|
||||
@ -940,25 +1234,28 @@ static UINT xf_cliprdr_server_format_list_response(CliprdrClientContext* context
|
||||
*/
|
||||
static UINT xf_cliprdr_server_format_data_request(CliprdrClientContext* context, CLIPRDR_FORMAT_DATA_REQUEST* formatDataRequest)
|
||||
{
|
||||
BOOL rawTransfer;
|
||||
xfCliprdrFormat* format = NULL;
|
||||
UINT32 formatId = formatDataRequest->requestedFormatId;
|
||||
xfClipboard* clipboard = (xfClipboard*) context->custom;
|
||||
xfContext* xfc = clipboard->xfc;
|
||||
|
||||
if (xf_cliprdr_is_self_owned(clipboard))
|
||||
rawTransfer = xf_cliprdr_is_raw_transfer_available(clipboard);
|
||||
|
||||
if (rawTransfer)
|
||||
{
|
||||
format = xf_cliprdr_get_format_by_id(clipboard, 0);
|
||||
format = xf_cliprdr_get_client_format_by_id(clipboard, CF_RAW);
|
||||
|
||||
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);
|
||||
format = xf_cliprdr_get_client_format_by_id(clipboard, formatId);
|
||||
|
||||
if (!format)
|
||||
return xf_cliprdr_send_data_response(clipboard, NULL, 0);
|
||||
|
||||
clipboard->requestedFormatId = formatId;
|
||||
clipboard->requestedFormatId = rawTransfer ? CF_RAW : formatId;
|
||||
|
||||
XConvertSelection(xfc->display, clipboard->clipboard_atom,
|
||||
format->atom, clipboard->property_atom, xfc->drawable, CurrentTime);
|
||||
@ -982,9 +1279,8 @@ static UINT xf_cliprdr_server_format_data_response(CliprdrClientContext* context
|
||||
BYTE* pDstData;
|
||||
UINT32 DstSize;
|
||||
UINT32 SrcSize;
|
||||
UINT32 formatId;
|
||||
UINT32 altFormatId;
|
||||
xfCliprdrFormat* format;
|
||||
UINT32 srcFormatId;
|
||||
UINT32 dstFormatId;
|
||||
BOOL nullTerminated = FALSE;
|
||||
UINT32 size = formatDataResponse->dataLen;
|
||||
BYTE* data = formatDataResponse->requestedFormatData;
|
||||
@ -994,8 +1290,6 @@ static UINT xf_cliprdr_server_format_data_response(CliprdrClientContext* context
|
||||
if (!clipboard->respond)
|
||||
return CHANNEL_RC_OK;
|
||||
|
||||
format = xf_cliprdr_get_format_by_id(clipboard, clipboard->requestedFormatId);
|
||||
|
||||
if (clipboard->data)
|
||||
{
|
||||
free(clipboard->data);
|
||||
@ -1005,39 +1299,50 @@ static UINT xf_cliprdr_server_format_data_response(CliprdrClientContext* context
|
||||
pDstData = NULL;
|
||||
DstSize = 0;
|
||||
|
||||
formatId = 0;
|
||||
altFormatId = 0;
|
||||
srcFormatId = 0;
|
||||
dstFormatId = 0;
|
||||
|
||||
switch (clipboard->data_format)
|
||||
if (clipboard->data_raw_format)
|
||||
{
|
||||
srcFormatId = CF_RAW;
|
||||
dstFormatId = CF_RAW;
|
||||
}
|
||||
else if (clipboard->data_format_name)
|
||||
{
|
||||
if (strcmp(clipboard->data_format_name, "HTML Format") == 0)
|
||||
{
|
||||
srcFormatId = ClipboardGetFormatId(clipboard->system, "HTML Format");
|
||||
dstFormatId = ClipboardGetFormatId(clipboard->system, "text/html");
|
||||
nullTerminated = TRUE;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (clipboard->data_format_id)
|
||||
{
|
||||
case CF_TEXT:
|
||||
formatId = CF_TEXT;
|
||||
altFormatId = ClipboardGetFormatId(clipboard->system, "UTF8_STRING");
|
||||
srcFormatId = CF_TEXT;
|
||||
dstFormatId = ClipboardGetFormatId(clipboard->system, "UTF8_STRING");
|
||||
nullTerminated = TRUE;
|
||||
break;
|
||||
|
||||
case CF_OEMTEXT:
|
||||
formatId = CF_OEMTEXT;
|
||||
altFormatId = ClipboardGetFormatId(clipboard->system, "UTF8_STRING");
|
||||
srcFormatId = CF_OEMTEXT;
|
||||
dstFormatId = ClipboardGetFormatId(clipboard->system, "UTF8_STRING");
|
||||
nullTerminated = TRUE;
|
||||
break;
|
||||
|
||||
case CF_UNICODETEXT:
|
||||
formatId = CF_UNICODETEXT;
|
||||
altFormatId = ClipboardGetFormatId(clipboard->system, "UTF8_STRING");
|
||||
srcFormatId = CF_UNICODETEXT;
|
||||
dstFormatId = 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;
|
||||
srcFormatId = CF_DIB;
|
||||
dstFormatId = ClipboardGetFormatId(clipboard->system, "image/bmp");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
SrcSize = (UINT32) size;
|
||||
@ -1048,18 +1353,21 @@ static UINT xf_cliprdr_server_format_data_response(CliprdrClientContext* context
|
||||
|
||||
CopyMemory(pSrcData, data, SrcSize);
|
||||
|
||||
bSuccess = ClipboardSetData(clipboard->system, formatId, (void*) pSrcData, SrcSize);
|
||||
bSuccess = ClipboardSetData(clipboard->system, srcFormatId, (void*) pSrcData, SrcSize);
|
||||
|
||||
if (!bSuccess)
|
||||
free (pSrcData);
|
||||
|
||||
if (bSuccess && altFormatId)
|
||||
if (bSuccess)
|
||||
{
|
||||
DstSize = 0;
|
||||
pDstData = (BYTE*) ClipboardGetData(clipboard->system, altFormatId, &DstSize);
|
||||
pDstData = (BYTE*) ClipboardGetData(clipboard->system, dstFormatId, &DstSize);
|
||||
|
||||
if ((DstSize > 1) && nullTerminated)
|
||||
DstSize--;
|
||||
if (nullTerminated)
|
||||
{
|
||||
while (DstSize > 0 && pDstData[DstSize - 1] == '\0')
|
||||
DstSize--;
|
||||
}
|
||||
}
|
||||
|
||||
clipboard->data = pDstData;
|
||||
@ -1111,6 +1419,11 @@ xfClipboard* xf_clipboard_new(xfContext* xfc)
|
||||
|
||||
clipboard->property_atom = XInternAtom(xfc->display, "_FREERDP_CLIPRDR", FALSE);
|
||||
|
||||
clipboard->raw_transfer_atom = XInternAtom(xfc->display, "_FREERDP_CLIPRDR_RAW", FALSE);
|
||||
clipboard->raw_format_list_atom = XInternAtom(xfc->display, "_FREERDP_CLIPRDR_FORMATS", FALSE);
|
||||
|
||||
xf_cliprdr_set_raw_transfer_enabled(clipboard, TRUE);
|
||||
|
||||
XSelectInput(xfc->display, clipboard->root_window, PropertyChangeMask);
|
||||
|
||||
#ifdef WITH_XFIXES
|
||||
@ -1140,7 +1453,7 @@ xfClipboard* xf_clipboard_new(xfContext* xfc)
|
||||
n = 0;
|
||||
|
||||
clipboard->clientFormats[n].atom = XInternAtom(xfc->display, "_FREERDP_RAW", False);
|
||||
clipboard->clientFormats[n].formatId = 0;
|
||||
clipboard->clientFormats[n].formatId = CF_RAW;
|
||||
n++;
|
||||
|
||||
clipboard->clientFormats[n].atom = XInternAtom(xfc->display, "UTF8_STRING", False);
|
||||
|
@ -50,6 +50,7 @@
|
||||
#define IDTIMEOUT 32000
|
||||
#define IDASYNC 32001
|
||||
|
||||
#define CF_RAW 0
|
||||
#define CF_TEXT 1
|
||||
#define CF_BITMAP 2
|
||||
#define CF_METAFILEPICT 3
|
||||
|
Loading…
Reference in New Issue
Block a user