22#include <freerdp/config.h>
31#include <X11/extensions/Xfixes.h>
35#include <winpr/assert.h>
36#include <winpr/image.h>
37#include <winpr/stream.h>
38#include <winpr/clipboard.h>
39#include <winpr/path.h>
41#include <freerdp/utils/signal.h>
42#include <freerdp/log.h>
43#include <freerdp/client/cliprdr.h>
44#include <freerdp/channels/channels.h>
45#include <freerdp/channels/cliprdr.h>
47#include <freerdp/client/client_cliprdr_file.h>
49#include "xf_cliprdr.h"
53#define TAG CLIENT_TAG("x11.cliprdr")
55#define MAX_CLIPBOARD_FORMATS 255
57#define DEBUG_CLIPRDR(...) WLog_DBG(TAG, __VA_ARGS__)
62 UINT32 formatToRequest;
77 UINT32 formatToRequest;
84 rdpChannels* channels;
85 CliprdrClientContext* context;
93 Atom timestamp_property_atom;
94 Time selection_ownership_timestamp;
96 Atom raw_transfer_atom;
97 Atom raw_format_list_atom;
99 UINT32 numClientFormats;
100 xfCliprdrFormat clientFormats[20];
102 UINT32 numServerFormats;
108 UINT32 requestedFormatId;
110 wHashTable* cachedData;
111 wHashTable* cachedRawData;
113 BOOL data_raw_format;
115 RequestedFormat* requestedFormat;
117 XSelectionEvent* respond;
126 size_t incr_data_length;
130 int xfixes_event_base;
131 int xfixes_error_base;
132 BOOL xfixes_supported;
136 UINT32 lastSentNumFormats;
137 CliprdrFileContext* file;
141static const char mime_text_plain[] =
"text/plain";
142static const char mime_uri_list[] =
"text/uri-list";
143static const char mime_html[] =
"text/html";
144static const char* mime_bitmap[] = {
"image/bmp",
"image/x-bmp",
"image/x-MS-bmp",
145 "image/x-win-bitmap" };
146static const char mime_webp[] =
"image/webp";
147static const char mime_png[] =
"image/png";
148static const char mime_jpeg[] =
"image/jpeg";
149static const char mime_tiff[] =
"image/tiff";
150static const char* mime_images[] = { mime_webp, mime_png, mime_jpeg, mime_tiff };
152static const char mime_gnome_copied_files[] =
"x-special/gnome-copied-files";
153static const char mime_mate_copied_files[] =
"x-special/mate-copied-files";
155static const char type_FileGroupDescriptorW[] =
"FileGroupDescriptorW";
156static const char type_HtmlFormat[] =
"HTML Format";
158static void xf_cliprdr_clear_cached_data(xfClipboard* clipboard);
159static UINT xf_cliprdr_send_client_format_list(xfClipboard* clipboard, BOOL force);
160static void xf_cliprdr_set_selection_owner(xfContext* xfc, xfClipboard* clipboard, Time timestamp);
162static void requested_format_free(RequestedFormat** ppRequestedFormat)
164 if (!ppRequestedFormat)
166 if (!(*ppRequestedFormat))
169 free((*ppRequestedFormat)->formatName);
170 free(*ppRequestedFormat);
171 *ppRequestedFormat = NULL;
174static BOOL requested_format_replace(RequestedFormat** ppRequestedFormat, UINT32 remoteFormatId,
175 UINT32 localFormatId,
const char* formatName)
177 if (!ppRequestedFormat)
180 requested_format_free(ppRequestedFormat);
181 RequestedFormat* requested = calloc(1,
sizeof(RequestedFormat));
184 requested->localFormat = localFormatId;
185 requested->formatToRequest = remoteFormatId;
188 requested->formatName = _strdup(formatName);
189 if (!requested->formatName)
196 *ppRequestedFormat = requested;
200static void xf_cached_data_free(
void* ptr)
202 xfCachedData* cached_data = ptr;
206 free(cached_data->data);
210static xfCachedData* xf_cached_data_new(BYTE* data,
size_t data_length)
212 if (data_length > UINT32_MAX)
215 xfCachedData* cached_data = calloc(1,
sizeof(xfCachedData));
219 cached_data->data = data;
220 cached_data->data_length = (UINT32)data_length;
225static xfCachedData* xf_cached_data_new_copy(
const BYTE* data,
size_t data_length)
230 copy = calloc(data_length + 1,
sizeof(BYTE));
233 memcpy(copy, data, data_length);
236 xfCachedData* cache = xf_cached_data_new(copy, data_length);
242static void xf_clipboard_free_server_formats(xfClipboard* clipboard)
244 WINPR_ASSERT(clipboard);
245 if (clipboard->serverFormats)
247 for (
size_t i = 0; i < clipboard->numServerFormats; i++)
250 free(format->formatName);
253 free(clipboard->serverFormats);
254 clipboard->serverFormats = NULL;
258static BOOL xf_cliprdr_update_owner(xfClipboard* clipboard)
260 WINPR_ASSERT(clipboard);
262 xfContext* xfc = clipboard->xfc;
265 if (!clipboard->sync)
268 Window owner = LogDynAndXGetSelectionOwner(xfc->log, xfc->display, clipboard->clipboard_atom);
269 if (clipboard->owner == owner)
272 clipboard->owner = owner;
276static void xf_cliprdr_check_owner(xfClipboard* clipboard)
278 if (xf_cliprdr_update_owner(clipboard))
279 xf_cliprdr_send_client_format_list(clipboard, FALSE);
282static BOOL xf_cliprdr_is_self_owned(xfClipboard* clipboard)
284 xfContext* xfc = NULL;
286 WINPR_ASSERT(clipboard);
288 xfc = clipboard->xfc;
290 return LogDynAndXGetSelectionOwner(xfc->log, xfc->display, clipboard->clipboard_atom) ==
294static void xf_cliprdr_set_raw_transfer_enabled(xfClipboard* clipboard, BOOL enabled)
296 UINT32 data = WINPR_ASSERTING_INT_CAST(uint32_t, enabled);
297 xfContext* xfc = NULL;
299 WINPR_ASSERT(clipboard);
301 xfc = clipboard->xfc;
303 LogDynAndXChangeProperty(xfc->log, xfc->display, xfc->drawable, clipboard->raw_transfer_atom,
304 XA_INTEGER, 32, PropModeReplace, (
const BYTE*)&data, 1);
307static BOOL xf_cliprdr_is_raw_transfer_available(xfClipboard* clipboard)
312 unsigned long length = 0;
313 unsigned long bytes_left = 0;
315 UINT32 is_enabled = 0;
317 xfContext* xfc = NULL;
319 WINPR_ASSERT(clipboard);
321 xfc = clipboard->xfc;
324 owner = LogDynAndXGetSelectionOwner(xfc->log, xfc->display, clipboard->clipboard_atom);
328 result = LogDynAndXGetWindowProperty(xfc->log, xfc->display, owner,
329 clipboard->raw_transfer_atom, 0, 4, 0, XA_INTEGER,
330 &type, &format, &length, &bytes_left, (BYTE**)&data);
339 if ((owner == None) || (owner == xfc->drawable))
342 if (result != Success)
345 return is_enabled ? TRUE : FALSE;
348static BOOL xf_cliprdr_formats_equal(
const CLIPRDR_FORMAT* server,
const xfCliprdrFormat* client)
350 WINPR_ASSERT(server);
351 WINPR_ASSERT(client);
353 if (server->formatName && client->formatName)
356 return (0 == strncmp(server->formatName, client->formatName, strlen(server->formatName)));
359 if (!server->formatName && !client->formatName)
361 return (server->formatId == client->formatToRequest);
367static const xfCliprdrFormat* xf_cliprdr_get_client_format_by_id(xfClipboard* clipboard,
370 WINPR_ASSERT(clipboard);
372 const BOOL formatIsHtml = formatId == ClipboardGetFormatId(clipboard->system, type_HtmlFormat);
373 const BOOL fetchImage = clipboard->isImageContent && formatIsHtml;
374 for (
size_t index = 0; index < clipboard->numClientFormats; index++)
376 const xfCliprdrFormat* format = &(clipboard->clientFormats[index]);
378 if (fetchImage && format->isImage)
381 if (format->formatToRequest == formatId)
388static const xfCliprdrFormat* xf_cliprdr_get_client_format_by_atom(xfClipboard* clipboard,
391 WINPR_ASSERT(clipboard);
393 for (UINT32 i = 0; i < clipboard->numClientFormats; i++)
395 const xfCliprdrFormat* format = &(clipboard->clientFormats[i]);
397 if (format->atom == atom)
404static const CLIPRDR_FORMAT* xf_cliprdr_get_server_format_by_atom(xfClipboard* clipboard, Atom atom)
406 WINPR_ASSERT(clipboard);
408 for (
size_t i = 0; i < clipboard->numClientFormats; i++)
410 const xfCliprdrFormat* client_format = &(clipboard->clientFormats[i]);
412 if (client_format->atom == atom)
414 for (
size_t j = 0; j < clipboard->numServerFormats; j++)
416 const CLIPRDR_FORMAT* server_format = &(clipboard->serverFormats[j]);
418 if (xf_cliprdr_formats_equal(server_format, client_format))
419 return server_format;
432static UINT xf_cliprdr_send_data_request(xfClipboard* clipboard, UINT32 formatId,
433 WINPR_ATTR_UNUSED
const xfCliprdrFormat* cformat)
436 request.requestedFormatId = formatId;
438 DEBUG_CLIPRDR(
"requesting format 0x%08" PRIx32
" [%s] {local 0x%08" PRIx32
"} [%s]", formatId,
439 ClipboardGetFormatIdString(formatId), cformat->localFormat, cformat->formatName);
441 WINPR_ASSERT(clipboard);
442 WINPR_ASSERT(clipboard->context);
443 WINPR_ASSERT(clipboard->context->ClientFormatDataRequest);
444 return clipboard->context->ClientFormatDataRequest(clipboard->context, &request);
452static UINT xf_cliprdr_send_data_response(xfClipboard* clipboard,
const xfCliprdrFormat* format,
453 const BYTE* data,
size_t size)
457 WINPR_ASSERT(clipboard);
460 if (clipboard->requestedFormatId == UINT32_MAX)
461 return CHANNEL_RC_OK;
466 DEBUG_CLIPRDR(
"send CB_RESPONSE_FAIL response {format 0x%08" PRIx32
467 " [%s] {local 0x%08" PRIx32
"} [%s]",
468 format->formatToRequest,
469 ClipboardGetFormatIdString(format->formatToRequest), format->localFormat,
472 DEBUG_CLIPRDR(
"send CB_RESPONSE_FAIL response");
476 WINPR_ASSERT(format);
477 DEBUG_CLIPRDR(
"send response format 0x%08" PRIx32
" [%s] {local 0x%08" PRIx32
" [%s]} [%s]",
478 format->formatToRequest, ClipboardGetFormatIdString(format->formatToRequest),
480 ClipboardGetFormatName(clipboard->system, format->localFormat),
484 clipboard->requestedFormatId = UINT32_MAX;
486 response.common.msgFlags = (data) ? CB_RESPONSE_OK : CB_RESPONSE_FAIL;
488 WINPR_ASSERT(size <= UINT32_MAX);
489 response.common.dataLen = (UINT32)size;
490 response.requestedFormatData = data;
492 WINPR_ASSERT(clipboard->context);
493 WINPR_ASSERT(clipboard->context->ClientFormatDataResponse);
494 return clipboard->context->ClientFormatDataResponse(clipboard->context, &response);
497static wStream* xf_cliprdr_serialize_server_format_list(xfClipboard* clipboard)
499 UINT32 formatCount = 0;
502 WINPR_ASSERT(clipboard);
505 if (!(s = Stream_New(NULL, 128)))
507 WLog_ERR(TAG,
"failed to allocate serialized format list");
512 formatCount = (clipboard->numServerFormats > 0) ? clipboard->numServerFormats - 1 : 0;
513 Stream_Write_UINT32(s, formatCount);
515 for (UINT32 i = 0; i < formatCount; i++)
518 size_t name_length = format->formatName ? strlen(format->formatName) : 0;
520 DEBUG_CLIPRDR(
"server announced 0x%08" PRIx32
" [%s][%s]", format->formatId,
521 ClipboardGetFormatIdString(format->formatId), format->formatName);
522 if (!Stream_EnsureRemainingCapacity(s,
sizeof(UINT32) + name_length + 1))
524 WLog_ERR(TAG,
"failed to expand serialized format list");
528 Stream_Write_UINT32(s, format->formatId);
530 if (format->formatName)
531 Stream_Write(s, format->formatName, name_length);
533 Stream_Write_UINT8(s,
'\0');
536 Stream_SealLength(s);
539 Stream_Free(s, TRUE);
543static CLIPRDR_FORMAT* xf_cliprdr_parse_server_format_list(BYTE* data,
size_t length,
549 WINPR_ASSERT(data || (length == 0));
550 WINPR_ASSERT(numFormats);
552 if (!(s = Stream_New(data, length)))
554 WLog_ERR(TAG,
"failed to allocate stream for parsing serialized format list");
558 if (!Stream_CheckAndLogRequiredLength(TAG, s,
sizeof(UINT32)))
561 Stream_Read_UINT32(s, *numFormats);
563 if (*numFormats > MAX_CLIPBOARD_FORMATS)
565 WLog_ERR(TAG,
"unexpectedly large number of formats: %" PRIu32
"", *numFormats);
571 WLog_ERR(TAG,
"failed to allocate format list");
575 for (UINT32 i = 0; i < *numFormats; i++)
577 const char* formatName = NULL;
578 size_t formatNameLength = 0;
580 if (!Stream_CheckAndLogRequiredLength(TAG, s,
sizeof(UINT32)))
583 Stream_Read_UINT32(s, formats[i].formatId);
584 formatName = (
const char*)Stream_Pointer(s);
585 formatNameLength = strnlen(formatName, Stream_GetRemainingLength(s));
587 if (formatNameLength == Stream_GetRemainingLength(s))
589 WLog_ERR(TAG,
"missing terminating null byte, %" PRIuz
" bytes left to read",
594 formats[i].formatName = strndup(formatName, formatNameLength);
595 Stream_Seek(s, formatNameLength + 1);
598 Stream_Free(s, FALSE);
601 Stream_Free(s, FALSE);
607static void xf_cliprdr_free_formats(
CLIPRDR_FORMAT* formats, UINT32 numFormats)
609 WINPR_ASSERT(formats || (numFormats == 0));
611 for (UINT32 i = 0; i < numFormats; i++)
613 free(formats[i].formatName);
619static CLIPRDR_FORMAT* xf_cliprdr_get_raw_server_formats(xfClipboard* clipboard, UINT32* numFormats)
623 unsigned long length = 0;
624 unsigned long remaining = 0;
627 xfContext* xfc = NULL;
629 WINPR_ASSERT(clipboard);
630 WINPR_ASSERT(numFormats);
632 xfc = clipboard->xfc;
637 Window owner = LogDynAndXGetSelectionOwner(xfc->log, xfc->display, clipboard->clipboard_atom);
638 LogDynAndXGetWindowProperty(xfc->log, xfc->display, owner, clipboard->raw_format_list_atom, 0,
639 4096, False, clipboard->raw_format_list_atom, &type, &format,
640 &length, &remaining, &data);
642 if (data && length > 0 && format == 8 && type == clipboard->raw_format_list_atom)
644 formats = xf_cliprdr_parse_server_format_list(data, length, numFormats);
649 "failed to retrieve raw format list: data=%p, length=%lu, format=%d, type=%lu "
651 (
void*)data, length, format, (
unsigned long)type,
652 (
unsigned long)clipboard->raw_format_list_atom);
661static BOOL xf_cliprdr_should_add_format(
const CLIPRDR_FORMAT* formats,
size_t count,
662 const xfCliprdrFormat* xformat)
664 WINPR_ASSERT(formats);
669 for (
size_t x = 0; x < count; x++)
672 if (format->formatId == xformat->formatToRequest)
678static CLIPRDR_FORMAT* xf_cliprdr_get_formats_from_targets(xfClipboard* clipboard,
683 int format_property = 0;
684 unsigned long proplength = 0;
685 unsigned long bytes_left = 0;
688 WINPR_ASSERT(clipboard);
689 WINPR_ASSERT(numFormats);
691 xfContext* xfc = clipboard->xfc;
695 LogDynAndXGetWindowProperty(xfc->log, xfc->display, xfc->drawable, clipboard->property_atom, 0,
696 200, 0, XA_ATOM, &atom, &format_property, &proplength, &bytes_left,
701 unsigned long length = proplength + 1;
704 WLog_ERR(TAG,
"XGetWindowProperty set length = %lu but data is NULL", length);
710 WLog_ERR(TAG,
"failed to allocate %lu CLIPRDR_FORMAT structs", length);
716 BOOL isImage = FALSE;
717 BOOL hasHtml = FALSE;
718 const uint32_t htmlFormatId = ClipboardRegisterFormat(clipboard->system, type_HtmlFormat);
719 for (
unsigned long i = 0; i < proplength; i++)
721 Atom tatom = ((Atom*)data)[i];
722 const xfCliprdrFormat* format = xf_cliprdr_get_client_format_by_atom(clipboard, tatom);
724 if (xf_cliprdr_should_add_format(formats, *numFormats, format))
727 cformat->formatId = format->formatToRequest;
732 if (cformat->formatId == htmlFormatId)
737 if (cformat->formatId == CF_TIFF)
739 else if (cformat->formatId == CF_DIB)
741 else if (cformat->formatId == CF_DIBV5)
744 if (format->formatName)
746 cformat->formatName = _strdup(format->formatName);
747 WINPR_ASSERT(cformat->formatName);
750 cformat->formatName = NULL;
756 clipboard->isImageContent = isImage;
757 if (isImage && !hasHtml)
760 cformat->formatId = htmlFormatId;
761 cformat->formatName = _strdup(type_HtmlFormat);
774static CLIPRDR_FORMAT* xf_cliprdr_get_client_formats(xfClipboard* clipboard, UINT32* numFormats)
778 WINPR_ASSERT(clipboard);
779 WINPR_ASSERT(numFormats);
783 if (xf_cliprdr_is_raw_transfer_available(clipboard))
785 formats = xf_cliprdr_get_raw_server_formats(clipboard, numFormats);
788 if (*numFormats == 0)
790 xf_cliprdr_free_formats(formats, *numFormats);
791 formats = xf_cliprdr_get_formats_from_targets(clipboard, numFormats);
797static void xf_cliprdr_provide_server_format_list(xfClipboard* clipboard)
800 xfContext* xfc = NULL;
802 WINPR_ASSERT(clipboard);
804 xfc = clipboard->xfc;
807 formats = xf_cliprdr_serialize_server_format_list(clipboard);
811 const size_t len = Stream_Length(formats);
812 WINPR_ASSERT(len <= INT32_MAX);
813 LogDynAndXChangeProperty(xfc->log, xfc->display, xfc->drawable,
814 clipboard->raw_format_list_atom, clipboard->raw_format_list_atom,
815 8, PropModeReplace, Stream_Buffer(formats), (
int)len);
819 LogDynAndXDeleteProperty(xfc->log, xfc->display, xfc->drawable,
820 clipboard->raw_format_list_atom);
823 Stream_Free(formats, TRUE);
831 if (a->formatId != b->formatId)
833 if (!a->formatName && !b->formatName)
835 if (!a->formatName || !b->formatName)
837 return strcmp(a->formatName, b->formatName) == 0;
840static BOOL xf_clipboard_changed(xfClipboard* clipboard,
const CLIPRDR_FORMAT* formats,
843 WINPR_ASSERT(clipboard);
844 WINPR_ASSERT(formats || (numFormats == 0));
846 if (clipboard->lastSentNumFormats != numFormats)
849 for (UINT32 x = 0; x < numFormats; x++)
852 BOOL contained = FALSE;
853 for (UINT32 y = 0; y < numFormats; y++)
855 if (xf_clipboard_format_equal(cur, &formats[y]))
868static void xf_clipboard_formats_free(xfClipboard* clipboard)
870 WINPR_ASSERT(clipboard);
872 xf_cliprdr_free_formats(clipboard->lastSentFormats, clipboard->lastSentNumFormats);
873 clipboard->lastSentFormats = NULL;
874 clipboard->lastSentNumFormats = 0;
877static BOOL xf_clipboard_copy_formats(xfClipboard* clipboard,
const CLIPRDR_FORMAT* formats,
880 WINPR_ASSERT(clipboard);
881 WINPR_ASSERT(formats || (numFormats == 0));
883 xf_clipboard_formats_free(clipboard);
885 clipboard->lastSentFormats = calloc(numFormats,
sizeof(
CLIPRDR_FORMAT));
886 if (!clipboard->lastSentFormats)
888 clipboard->lastSentNumFormats = numFormats;
889 for (UINT32 x = 0; x < numFormats; x++)
895 lcur->formatName = _strdup(cur->formatName);
900static UINT xf_cliprdr_send_format_list(xfClipboard* clipboard,
const CLIPRDR_FORMAT* formats,
901 UINT32 numFormats, BOOL force)
907 } cnv = { .cpv = formats };
909 .numFormats = numFormats,
911 .common.msgType = CB_FORMAT_LIST };
914 WINPR_ASSERT(clipboard);
915 WINPR_ASSERT(formats || (numFormats == 0));
917 if (!force && !xf_clipboard_changed(clipboard, formats, numFormats))
918 return CHANNEL_RC_OK;
920#if defined(WITH_DEBUG_CLIPRDR)
921 for (UINT32 x = 0; x < numFormats; x++)
924 DEBUG_CLIPRDR(
"announcing format 0x%08" PRIx32
" [%s] [%s]", format->formatId,
925 ClipboardGetFormatIdString(format->formatId), format->formatName);
929 xf_clipboard_copy_formats(clipboard, formats, numFormats);
931 xf_cliprdr_send_data_response(clipboard, NULL, NULL, 0);
933 xf_cliprdr_clear_cached_data(clipboard);
935 ret = cliprdr_file_context_notify_new_client_format_list(clipboard->file);
939 WINPR_ASSERT(clipboard->context);
940 WINPR_ASSERT(clipboard->context->ClientFormatList);
941 return clipboard->context->ClientFormatList(clipboard->context, &formatList);
944static void xf_cliprdr_get_requested_targets(xfClipboard* clipboard)
946 UINT32 numFormats = 0;
947 CLIPRDR_FORMAT* formats = xf_cliprdr_get_client_formats(clipboard, &numFormats);
948 xf_cliprdr_send_format_list(clipboard, formats, numFormats, FALSE);
949 xf_cliprdr_free_formats(formats, numFormats);
952static void xf_cliprdr_process_requested_data(xfClipboard* clipboard, BOOL hasData,
953 const BYTE* data,
size_t size)
958 INT64 srcFormatId = -1;
959 BYTE* pDstData = NULL;
960 const xfCliprdrFormat* format = NULL;
962 WINPR_ASSERT(clipboard);
964 if (clipboard->incr_starts && hasData)
969 clipboard->incr_data_length = 0;
971 format = xf_cliprdr_get_client_format_by_id(clipboard, clipboard->requestedFormatId);
973 if (!hasData || !data || !format)
975 xf_cliprdr_send_data_response(clipboard, format, NULL, 0);
979 switch (format->formatToRequest)
982 srcFormatId = CF_RAW;
988 srcFormatId = format->localFormat;
992 srcFormatId = format->localFormat;
998 xf_cliprdr_send_data_response(clipboard, format, NULL, 0);
1002 ClipboardLock(clipboard->system);
1003 SrcSize = (UINT32)size;
1004 bSuccess = ClipboardSetData(clipboard->system, (UINT32)srcFormatId, data, SrcSize);
1010 (BYTE*)ClipboardGetData(clipboard->system, clipboard->requestedFormatId, &DstSize);
1012 ClipboardUnlock(clipboard->system);
1016 xf_cliprdr_send_data_response(clipboard, format, NULL, 0);
1027 ClipboardLock(clipboard->system);
1028 if (format->formatToRequest &&
1029 (format->formatToRequest ==
1030 ClipboardGetFormatId(clipboard->system, type_FileGroupDescriptorW)))
1032 UINT error = NO_ERROR;
1038 const UINT32 flags = cliprdr_file_context_remote_get_flags(clipboard->file);
1039 error = cliprdr_serialize_file_list_ex(flags, file_array, file_count, &pDstData, &DstSize);
1042 WLog_ERR(TAG,
"failed to serialize CLIPRDR_FILELIST: 0x%08X", error);
1045 UINT32 formatId = ClipboardGetFormatId(clipboard->system, mime_uri_list);
1046 UINT32 url_size = 0;
1048 char* url = ClipboardGetData(clipboard->system, formatId, &url_size);
1049 cliprdr_file_context_update_client_data(clipboard->file, url, url_size);
1055 ClipboardUnlock(clipboard->system);
1057 xf_cliprdr_send_data_response(clipboard, format, pDstData, DstSize);
1061static BOOL xf_restore_input_flags(xfClipboard* clipboard)
1063 WINPR_ASSERT(clipboard);
1065 xfContext* xfc = clipboard->xfc;
1068 if (clipboard->event_mask != 0)
1070 XSelectInput(xfc->display, xfc->drawable, clipboard->event_mask);
1071 clipboard->event_mask = 0;
1076static BOOL append(xfClipboard* clipboard,
const void* sdata,
size_t length)
1078 WINPR_ASSERT(clipboard);
1080 const size_t size = length + clipboard->incr_data_length + 2;
1081 BYTE* data = realloc(clipboard->incr_data, size);
1084 clipboard->incr_data = data;
1085 memcpy(&data[clipboard->incr_data_length], sdata, length);
1086 clipboard->incr_data_length += length;
1087 clipboard->incr_data[clipboard->incr_data_length + 0] =
'\0';
1088 clipboard->incr_data[clipboard->incr_data_length + 1] =
'\0';
1092static BOOL xf_cliprdr_stop_incr(xfClipboard* clipboard)
1094 clipboard->incr_starts = FALSE;
1095 clipboard->incr_data_length = 0;
1096 return xf_restore_input_flags(clipboard);
1099static BOOL xf_cliprdr_get_requested_data(xfClipboard* clipboard, Atom target)
1101 WINPR_ASSERT(clipboard);
1103 xfContext* xfc = clipboard->xfc;
1106 const xfCliprdrFormat* format =
1107 xf_cliprdr_get_client_format_by_id(clipboard, clipboard->requestedFormatId);
1109 if (!format || (format->atom != target))
1111 xf_cliprdr_send_data_response(clipboard, format, NULL, 0);
1116 BOOL has_data = FALSE;
1117 int format_property = 0;
1118 unsigned long length = 0;
1119 unsigned long total_bytes = 0;
1120 BYTE* property_data = NULL;
1121 const int rc = LogDynAndXGetWindowProperty(
1122 xfc->log, xfc->display, xfc->drawable, clipboard->property_atom, 0, 0, False, target, &type,
1123 &format_property, &length, &total_bytes, &property_data);
1126 xf_cliprdr_send_data_response(clipboard, format, NULL, 0);
1133 if ((total_bytes <= 0) && !clipboard->incr_starts)
1135 xf_cliprdr_stop_incr(clipboard);
1138 else if (type == clipboard->incr_atom)
1140 xf_cliprdr_stop_incr(clipboard);
1141 clipboard->incr_starts = TRUE;
1146 BYTE* incremental_data = NULL;
1147 unsigned long incremental_len = 0;
1150 len = clipboard->incr_data_length;
1151 if (total_bytes <= 0)
1153 xf_cliprdr_stop_incr(clipboard);
1157 else if (LogDynAndXGetWindowProperty(
1158 xfc->log, xfc->display, xfc->drawable, clipboard->property_atom, 0,
1159 WINPR_ASSERTING_INT_CAST(int32_t, total_bytes), False, target, &type,
1160 &format_property, &incremental_len, &length, &incremental_data) == Success)
1162 has_data = append(clipboard, incremental_data, incremental_len);
1163 len = clipboard->incr_data_length;
1166 if (incremental_data)
1167 XFree(incremental_data);
1170 LogDynAndXDeleteProperty(xfc->log, xfc->display, xfc->drawable, clipboard->property_atom);
1171 xf_cliprdr_process_requested_data(clipboard, has_data, clipboard->incr_data, len);
1176static void xf_cliprdr_append_target(xfClipboard* clipboard, Atom target)
1178 WINPR_ASSERT(clipboard);
1180 if (clipboard->numTargets >= ARRAYSIZE(clipboard->targets))
1183 for (
size_t i = 0; i < clipboard->numTargets; i++)
1185 if (clipboard->targets[i] == target)
1189 clipboard->targets[clipboard->numTargets++] = target;
1192static void xf_cliprdr_provide_targets(xfClipboard* clipboard,
const XSelectionEvent* respond)
1194 xfContext* xfc = NULL;
1196 WINPR_ASSERT(clipboard);
1198 xfc = clipboard->xfc;
1201 if (respond->property != None)
1203 WINPR_ASSERT(clipboard->numTargets <= INT32_MAX);
1204 LogDynAndXChangeProperty(xfc->log, xfc->display, respond->requestor, respond->property,
1205 XA_ATOM, 32, PropModeReplace, (
const BYTE*)clipboard->targets,
1206 (
int)clipboard->numTargets);
1210static void xf_cliprdr_provide_timestamp(xfClipboard* clipboard,
const XSelectionEvent* respond)
1212 xfContext* xfc = NULL;
1214 WINPR_ASSERT(clipboard);
1216 xfc = clipboard->xfc;
1219 if (respond->property != None)
1221 LogDynAndXChangeProperty(xfc->log, xfc->display, respond->requestor, respond->property,
1222 XA_INTEGER, 32, PropModeReplace,
1223 (
const BYTE*)&clipboard->selection_ownership_timestamp, 1);
1227#define xf_cliprdr_provide_data(clipboard, respond, data, size) \
1228 xf_cliprdr_provide_data_((clipboard), (respond), (data), (size), __FILE__, __func__, __LINE__)
1229static void xf_cliprdr_provide_data_(xfClipboard* clipboard,
const XSelectionEvent* respond,
1230 const BYTE* data, UINT32 size,
const char* file,
1231 const char* fkt,
size_t line)
1233 WINPR_ASSERT(clipboard);
1235 xfContext* xfc = clipboard->xfc;
1238 if (respond->property != None)
1240 LogDynAndXChangeProperty_ex(xfc->log, file, fkt, line, xfc->display, respond->requestor,
1241 respond->property, respond->target, 8, PropModeReplace, data,
1242 WINPR_ASSERTING_INT_CAST(int32_t, size));
1246static void log_selection_event(xfContext* xfc,
const XEvent* event)
1248 const DWORD level = WLOG_TRACE;
1249 static wLog* _log_cached_ptr = NULL;
1250 if (!_log_cached_ptr)
1251 _log_cached_ptr = WLog_Get(TAG);
1252 if (WLog_IsLevelActive(_log_cached_ptr, level))
1255 switch (event->type)
1257 case SelectionClear:
1259 const XSelectionClearEvent* xevent = &
event->xselectionclear;
1261 Safe_XGetAtomName(_log_cached_ptr, xfc->display, xevent->selection);
1262 WLog_Print(_log_cached_ptr, level,
"got event %s [selection %s]",
1263 x11_event_string(event->type), selection);
1267 case SelectionNotify:
1269 const XSelectionEvent* xevent = &
event->xselection;
1271 Safe_XGetAtomName(_log_cached_ptr, xfc->display, xevent->selection);
1272 char* target = Safe_XGetAtomName(_log_cached_ptr, xfc->display, xevent->target);
1273 char*
property = Safe_XGetAtomName(_log_cached_ptr, xfc->display, xevent->property);
1274 WLog_Print(_log_cached_ptr, level,
1275 "got event %s [selection %s, target %s, property %s]",
1276 x11_event_string(event->type), selection, target, property);
1282 case SelectionRequest:
1284 const XSelectionRequestEvent* xevent = &
event->xselectionrequest;
1286 Safe_XGetAtomName(_log_cached_ptr, xfc->display, xevent->selection);
1287 char* target = Safe_XGetAtomName(_log_cached_ptr, xfc->display, xevent->target);
1288 char*
property = Safe_XGetAtomName(_log_cached_ptr, xfc->display, xevent->property);
1289 WLog_Print(_log_cached_ptr, level,
1290 "got event %s [selection %s, target %s, property %s]",
1291 x11_event_string(event->type), selection, target, property);
1297 case PropertyNotify:
1299 const XPropertyEvent* xevent = &
event->xproperty;
1300 char* atom = Safe_XGetAtomName(_log_cached_ptr, xfc->display, xevent->atom);
1301 WLog_Print(_log_cached_ptr, level,
"got event %s [atom %s]",
1302 x11_event_string(event->type), atom);
1312static BOOL xf_cliprdr_process_selection_notify(xfClipboard* clipboard,
1313 const XSelectionEvent* xevent)
1315 WINPR_ASSERT(clipboard);
1316 WINPR_ASSERT(xevent);
1318 if (xevent->target == clipboard->targets[1])
1320 if (xevent->property == None)
1322 xf_cliprdr_send_client_format_list(clipboard, FALSE);
1326 xf_cliprdr_get_requested_targets(clipboard);
1333 return xf_cliprdr_get_requested_data(clipboard, xevent->target);
1337void xf_cliprdr_clear_cached_data(xfClipboard* clipboard)
1339 WINPR_ASSERT(clipboard);
1341 ClipboardLock(clipboard->system);
1342 ClipboardEmpty(clipboard->system);
1344 HashTable_Clear(clipboard->cachedData);
1345 HashTable_Clear(clipboard->cachedRawData);
1347 cliprdr_file_context_clear(clipboard->file);
1349 xf_cliprdr_stop_incr(clipboard);
1350 ClipboardUnlock(clipboard->system);
1353static void* format_to_cache_slot(UINT32 format)
1360 cnv.uptr = 0x100000000ULL + format;
1364static UINT32 get_dst_format_id_for_local_request(xfClipboard* clipboard,
1365 const xfCliprdrFormat* format)
1367 UINT32 dstFormatId = 0;
1369 WINPR_ASSERT(format);
1371 if (!format->formatName)
1372 return format->localFormat;
1374 ClipboardLock(clipboard->system);
1375 if (strcmp(format->formatName, type_HtmlFormat) == 0)
1376 dstFormatId = ClipboardGetFormatId(clipboard->system, mime_html);
1377 ClipboardUnlock(clipboard->system);
1379 if (strcmp(format->formatName, type_FileGroupDescriptorW) == 0)
1380 dstFormatId = format->localFormat;
1385static void get_src_format_info_for_local_request(xfClipboard* clipboard,
1386 const xfCliprdrFormat* format,
1387 UINT32* srcFormatId, BOOL* nullTerminated)
1390 *nullTerminated = FALSE;
1392 if (format->formatName)
1394 ClipboardLock(clipboard->system);
1395 if (strcmp(format->formatName, type_HtmlFormat) == 0)
1397 *srcFormatId = ClipboardGetFormatId(clipboard->system, type_HtmlFormat);
1398 *nullTerminated = TRUE;
1400 else if (strcmp(format->formatName, type_FileGroupDescriptorW) == 0)
1402 *srcFormatId = ClipboardGetFormatId(clipboard->system, type_FileGroupDescriptorW);
1403 *nullTerminated = TRUE;
1405 ClipboardUnlock(clipboard->system);
1409 *srcFormatId = format->formatToRequest;
1410 switch (format->formatToRequest)
1414 case CF_UNICODETEXT:
1415 *nullTerminated = TRUE;
1418 *srcFormatId = CF_DIB;
1421 *srcFormatId = CF_TIFF;
1429static xfCachedData* convert_data_from_existing_raw_data(xfClipboard* clipboard,
1430 xfCachedData* cached_raw_data,
1431 UINT32 srcFormatId, BOOL nullTerminated,
1434 UINT32 dst_size = 0;
1436 WINPR_ASSERT(clipboard);
1437 WINPR_ASSERT(cached_raw_data);
1438 WINPR_ASSERT(cached_raw_data->data);
1440 ClipboardLock(clipboard->system);
1441 BOOL success = ClipboardSetData(clipboard->system, srcFormatId, cached_raw_data->data,
1442 cached_raw_data->data_length);
1445 WLog_WARN(TAG,
"Failed to set clipboard data (formatId: %u, data: %p, data_length: %u)",
1446 srcFormatId, WINPR_CXX_COMPAT_CAST(
const void*, cached_raw_data->data),
1447 cached_raw_data->data_length);
1448 ClipboardUnlock(clipboard->system);
1452 BYTE* dst_data = ClipboardGetData(clipboard->system, dstFormatId, &dst_size);
1455 WLog_WARN(TAG,
"Failed to get converted clipboard data");
1456 ClipboardUnlock(clipboard->system);
1459 ClipboardUnlock(clipboard->system);
1463 BYTE* nullTerminator = memchr(dst_data,
'\0', dst_size);
1466 const intptr_t diff = nullTerminator - dst_data;
1467 WINPR_ASSERT(diff >= 0);
1468 WINPR_ASSERT(diff <= UINT32_MAX);
1469 dst_size = (UINT32)diff;
1473 xfCachedData* cached_data = xf_cached_data_new(dst_data, dst_size);
1476 WLog_WARN(TAG,
"Failed to allocate cache entry");
1481 if (!HashTable_Insert(clipboard->cachedData, format_to_cache_slot(dstFormatId), cached_data))
1483 WLog_WARN(TAG,
"Failed to cache clipboard data");
1484 xf_cached_data_free(cached_data);
1491static BOOL xf_cliprdr_process_selection_request(xfClipboard* clipboard,
1492 const XSelectionRequestEvent* xevent)
1496 UINT32 formatId = 0;
1497 XSelectionEvent* respond = NULL;
1499 BOOL delayRespond = 0;
1500 BOOL rawTransfer = 0;
1501 unsigned long length = 0;
1502 unsigned long bytes_left = 0;
1503 xfContext* xfc = NULL;
1505 WINPR_ASSERT(clipboard);
1506 WINPR_ASSERT(xevent);
1508 xfc = clipboard->xfc;
1511 if (xevent->owner != xfc->drawable)
1514 delayRespond = FALSE;
1516 if (!(respond = (XSelectionEvent*)calloc(1,
sizeof(XSelectionEvent))))
1518 WLog_ERR(TAG,
"failed to allocate XEvent data");
1522 respond->property = None;
1523 respond->type = SelectionNotify;
1524 respond->display = xevent->display;
1525 respond->requestor = xevent->requestor;
1526 respond->selection = xevent->selection;
1527 respond->target = xevent->target;
1528 respond->time = xevent->time;
1530 if (xevent->target == clipboard->targets[0])
1533 respond->property = xevent->property;
1534 xf_cliprdr_provide_timestamp(clipboard, respond);
1536 else if (xevent->target == clipboard->targets[1])
1539 respond->property = xevent->property;
1540 xf_cliprdr_provide_targets(clipboard, respond);
1545 xf_cliprdr_get_server_format_by_atom(clipboard, xevent->target);
1546 const xfCliprdrFormat* cformat =
1547 xf_cliprdr_get_client_format_by_atom(clipboard, xevent->target);
1549 if (format && (xevent->requestor != xfc->drawable))
1551 formatId = format->formatId;
1552 rawTransfer = FALSE;
1553 xfCachedData* cached_data = NULL;
1555 if (formatId == CF_RAW)
1557 if (LogDynAndXGetWindowProperty(
1558 xfc->log, xfc->display, xevent->requestor, clipboard->property_atom, 0, 4,
1559 0, XA_INTEGER, &type, &fmt, &length, &bytes_left, &data) != Success)
1566 CopyMemory(&formatId, data, 4);
1571 const UINT32 dstFormatId = get_dst_format_id_for_local_request(clipboard, cformat);
1572 DEBUG_CLIPRDR(
"formatId: 0x%08" PRIx32
", dstFormatId: 0x%08" PRIx32
"", formatId,
1576 cached_data = HashTable_GetItemValue(clipboard->cachedData,
1577 format_to_cache_slot(dstFormatId));
1579 cached_data = HashTable_GetItemValue(clipboard->cachedRawData,
1580 format_to_cache_slot(formatId));
1582 DEBUG_CLIPRDR(
"hasCachedData: %u, rawTransfer: %d", cached_data ? 1u : 0u, rawTransfer);
1584 if (!cached_data && !rawTransfer)
1586 UINT32 srcFormatId = 0;
1587 BOOL nullTerminated = FALSE;
1588 xfCachedData* cached_raw_data = NULL;
1590 get_src_format_info_for_local_request(clipboard, cformat, &srcFormatId,
1593 HashTable_GetItemValue(clipboard->cachedRawData, (
void*)(UINT_PTR)srcFormatId);
1595 DEBUG_CLIPRDR(
"hasCachedRawData: %u, rawDataLength: %u", cached_raw_data ? 1u : 0u,
1596 cached_raw_data ? cached_raw_data->data_length : 0);
1598 if (cached_raw_data && cached_raw_data->data_length != 0)
1599 cached_data = convert_data_from_existing_raw_data(
1600 clipboard, cached_raw_data, srcFormatId, nullTerminated, dstFormatId);
1603 DEBUG_CLIPRDR(
"hasCachedData: %u", cached_data ? 1u : 0u);
1608 respond->property = xevent->property;
1611 xf_cliprdr_provide_data(clipboard, respond, cached_data->data,
1612 cached_data->data_length);
1614 else if (clipboard->respond)
1620 WINPR_ASSERT(cformat);
1626 respond->property = xevent->property;
1627 clipboard->respond = respond;
1628 requested_format_replace(&clipboard->requestedFormat, formatId, dstFormatId,
1629 cformat->formatName);
1630 clipboard->data_raw_format = rawTransfer;
1631 delayRespond = TRUE;
1632 xf_cliprdr_send_data_request(clipboard, formatId, cformat);
1642 XSelectionEvent* sev;
1646 LogDynAndXSendEvent(xfc->log, xfc->display, xevent->requestor, 0, 0, conv.ev);
1647 LogDynAndXFlush(xfc->log, xfc->display);
1654static BOOL xf_cliprdr_process_selection_clear(xfClipboard* clipboard,
1655 const XSelectionClearEvent* xevent)
1657 xfContext* xfc = NULL;
1659 WINPR_ASSERT(clipboard);
1660 WINPR_ASSERT(xevent);
1662 xfc = clipboard->xfc;
1665 WINPR_UNUSED(xevent);
1667 if (xf_cliprdr_is_self_owned(clipboard))
1670 LogDynAndXDeleteProperty(xfc->log, xfc->display, clipboard->root_window,
1671 clipboard->property_atom);
1675static BOOL xf_cliprdr_process_property_notify(xfClipboard* clipboard,
const XPropertyEvent* xevent)
1677 const xfCliprdrFormat* format = NULL;
1678 xfContext* xfc = NULL;
1683 xfc = clipboard->xfc;
1685 WINPR_ASSERT(xevent);
1687 if (xevent->atom == clipboard->timestamp_property_atom)
1693 xf_cliprdr_set_selection_owner(xfc, clipboard, xevent->time);
1697 if (xevent->atom != clipboard->property_atom)
1700 if (xevent->window == clipboard->root_window)
1702 xf_cliprdr_send_client_format_list(clipboard, FALSE);
1704 else if ((xevent->window == xfc->drawable) && (xevent->state == PropertyNewValue) &&
1705 clipboard->incr_starts)
1707 format = xf_cliprdr_get_client_format_by_id(clipboard, clipboard->requestedFormatId);
1710 xf_cliprdr_get_requested_data(clipboard, format->atom);
1716void xf_cliprdr_handle_xevent(xfContext* xfc,
const XEvent* event)
1718 xfClipboard* clipboard = NULL;
1723 clipboard = xfc->clipboard;
1730 if (clipboard->xfixes_supported &&
1731 event->type == XFixesSelectionNotify + clipboard->xfixes_event_base)
1733 const XFixesSelectionNotifyEvent* se = (
const XFixesSelectionNotifyEvent*)event;
1735 if (se->subtype == XFixesSetSelectionOwnerNotify)
1737 if (se->selection != clipboard->clipboard_atom)
1740 if (LogDynAndXGetSelectionOwner(xfc->log, xfc->display, se->selection) == xfc->drawable)
1743 clipboard->owner = None;
1744 xf_cliprdr_check_owner(clipboard);
1752 switch (event->type)
1754 case SelectionNotify:
1755 log_selection_event(xfc, event);
1756 xf_cliprdr_process_selection_notify(clipboard, &event->xselection);
1759 case SelectionRequest:
1760 log_selection_event(xfc, event);
1761 xf_cliprdr_process_selection_request(clipboard, &event->xselectionrequest);
1764 case SelectionClear:
1765 log_selection_event(xfc, event);
1766 xf_cliprdr_process_selection_clear(clipboard, &event->xselectionclear);
1769 case PropertyNotify:
1770 log_selection_event(xfc, event);
1771 xf_cliprdr_process_property_notify(clipboard, &event->xproperty);
1775 if (!clipboard->xfixes_supported)
1777 xf_cliprdr_check_owner(clipboard);
1791static UINT xf_cliprdr_send_client_capabilities(xfClipboard* clipboard)
1796 WINPR_ASSERT(clipboard);
1798 capabilities.cCapabilitiesSets = 1;
1800 generalCapabilitySet.capabilitySetType = CB_CAPSTYPE_GENERAL;
1801 generalCapabilitySet.capabilitySetLength = 12;
1802 generalCapabilitySet.version = CB_CAPS_VERSION_2;
1803 generalCapabilitySet.generalFlags = CB_USE_LONG_FORMAT_NAMES;
1805 WINPR_ASSERT(clipboard);
1806 generalCapabilitySet.generalFlags |= cliprdr_file_context_current_flags(clipboard->file);
1808 WINPR_ASSERT(clipboard->context);
1809 WINPR_ASSERT(clipboard->context->ClientCapabilities);
1810 return clipboard->context->ClientCapabilities(clipboard->context, &capabilities);
1818static UINT xf_cliprdr_send_client_format_list(xfClipboard* clipboard, BOOL force)
1820 WINPR_ASSERT(clipboard);
1822 xfContext* xfc = clipboard->xfc;
1825 UINT32 numFormats = 0;
1826 CLIPRDR_FORMAT* formats = xf_cliprdr_get_client_formats(clipboard, &numFormats);
1828 const UINT ret = xf_cliprdr_send_format_list(clipboard, formats, numFormats, force);
1830 if (clipboard->owner && clipboard->owner != xfc->drawable)
1833 LogDynAndXConvertSelection(xfc->log, xfc->display, clipboard->clipboard_atom,
1834 clipboard->targets[1], clipboard->property_atom, xfc->drawable,
1838 xf_cliprdr_free_formats(formats, numFormats);
1848static UINT xf_cliprdr_send_client_format_list_response(xfClipboard* clipboard, BOOL status)
1852 formatListResponse.common.msgType = CB_FORMAT_LIST_RESPONSE;
1853 formatListResponse.common.msgFlags = status ? CB_RESPONSE_OK : CB_RESPONSE_FAIL;
1854 formatListResponse.common.dataLen = 0;
1856 WINPR_ASSERT(clipboard);
1857 WINPR_ASSERT(clipboard->context);
1858 WINPR_ASSERT(clipboard->context->ClientFormatListResponse);
1859 return clipboard->context->ClientFormatListResponse(clipboard->context, &formatListResponse);
1867static UINT xf_cliprdr_monitor_ready(CliprdrClientContext* context,
1871 xfClipboard* clipboard = NULL;
1873 WINPR_ASSERT(context);
1874 WINPR_ASSERT(monitorReady);
1876 clipboard = cliprdr_file_context_get_context(context->custom);
1877 WINPR_ASSERT(clipboard);
1879 WINPR_UNUSED(monitorReady);
1881 if ((ret = xf_cliprdr_send_client_capabilities(clipboard)) != CHANNEL_RC_OK)
1884 xf_clipboard_formats_free(clipboard);
1886 if ((ret = xf_cliprdr_send_client_format_list(clipboard, TRUE)) != CHANNEL_RC_OK)
1889 clipboard->sync = TRUE;
1890 return CHANNEL_RC_OK;
1898static UINT xf_cliprdr_server_capabilities(CliprdrClientContext* context,
1902 const BYTE* capsPtr = NULL;
1903 xfClipboard* clipboard = NULL;
1905 WINPR_ASSERT(context);
1906 WINPR_ASSERT(capabilities);
1908 clipboard = cliprdr_file_context_get_context(context->custom);
1909 WINPR_ASSERT(clipboard);
1911 capsPtr = (
const BYTE*)capabilities->capabilitySets;
1912 WINPR_ASSERT(capsPtr);
1914 cliprdr_file_context_remote_set_flags(clipboard->file, 0);
1916 for (UINT32 i = 0; i < capabilities->cCapabilitiesSets; i++)
1920 if (caps->capabilitySetType == CB_CAPSTYPE_GENERAL)
1924 cliprdr_file_context_remote_set_flags(clipboard->file, generalCaps->generalFlags);
1927 capsPtr += caps->capabilitySetLength;
1930 return CHANNEL_RC_OK;
1933static void xf_cliprdr_prepare_to_set_selection_owner(xfContext* xfc, xfClipboard* clipboard)
1936 WINPR_ASSERT(clipboard);
1953 Atom value = clipboard->timestamp_property_atom;
1955 LogDynAndXChangeProperty(xfc->log, xfc->display, xfc->drawable,
1956 clipboard->timestamp_property_atom, XA_ATOM, 32, PropModeReplace,
1957 (
const BYTE*)&value, 1);
1958 LogDynAndXFlush(xfc->log, xfc->display);
1961static void xf_cliprdr_set_selection_owner(xfContext* xfc, xfClipboard* clipboard, Time timestamp)
1964 WINPR_ASSERT(clipboard);
1970 clipboard->selection_ownership_timestamp = timestamp;
1971 LogDynAndXSetSelectionOwner(xfc->log, xfc->display, clipboard->clipboard_atom, xfc->drawable,
1973 LogDynAndXFlush(xfc->log, xfc->display);
1981static UINT xf_cliprdr_server_format_list(CliprdrClientContext* context,
1984 xfContext* xfc = NULL;
1986 xfClipboard* clipboard = NULL;
1988 WINPR_ASSERT(context);
1989 WINPR_ASSERT(formatList);
1991 clipboard = cliprdr_file_context_get_context(context->custom);
1992 WINPR_ASSERT(clipboard);
1994 xfc = clipboard->xfc;
2000 free(clipboard->respond);
2001 clipboard->respond = NULL;
2003 xf_clipboard_formats_free(clipboard);
2004 xf_cliprdr_clear_cached_data(clipboard);
2005 requested_format_free(&clipboard->requestedFormat);
2007 xf_clipboard_free_server_formats(clipboard);
2009 clipboard->numServerFormats = formatList->numFormats + 1;
2011 if (!(clipboard->serverFormats =
2014 WLog_ERR(TAG,
"failed to allocate %" PRIu32
" CLIPRDR_FORMAT structs",
2015 clipboard->numServerFormats);
2016 ret = CHANNEL_RC_NO_MEMORY;
2020 for (
size_t i = 0; i < formatList->numFormats; i++)
2025 srvFormat->formatId = format->formatId;
2027 if (format->formatName)
2029 srvFormat->formatName = _strdup(format->formatName);
2031 if (!srvFormat->formatName)
2033 for (UINT32 k = 0; k < i; k++)
2034 free(clipboard->serverFormats[k].formatName);
2036 clipboard->numServerFormats = 0;
2037 free(clipboard->serverFormats);
2038 clipboard->serverFormats = NULL;
2039 ret = CHANNEL_RC_NO_MEMORY;
2045 ClipboardLock(clipboard->system);
2046 ret = cliprdr_file_context_notify_new_server_format_list(clipboard->file);
2047 ClipboardUnlock(clipboard->system);
2053 CLIPRDR_FORMAT* format = &clipboard->serverFormats[formatList->numFormats];
2054 format->formatId = CF_RAW;
2055 format->formatName = NULL;
2057 xf_cliprdr_provide_server_format_list(clipboard);
2058 clipboard->numTargets = 2;
2060 for (
size_t i = 0; i < formatList->numFormats; i++)
2064 for (
size_t j = 0; j < clipboard->numClientFormats; j++)
2066 const xfCliprdrFormat* clientFormat = &clipboard->clientFormats[j];
2067 if (xf_cliprdr_formats_equal(format, clientFormat))
2069 if ((clientFormat->formatName != NULL) &&
2070 (strcmp(type_FileGroupDescriptorW, clientFormat->formatName) == 0))
2072 if (!cliprdr_file_context_has_local_support(clipboard->file))
2075 xf_cliprdr_append_target(clipboard, clientFormat->atom);
2080 ret = xf_cliprdr_send_client_format_list_response(clipboard, TRUE);
2081 if (xfc->remote_app)
2082 xf_cliprdr_set_selection_owner(xfc, clipboard, CurrentTime);
2084 xf_cliprdr_prepare_to_set_selection_owner(xfc, clipboard);
2097static UINT xf_cliprdr_server_format_list_response(
2098 WINPR_ATTR_UNUSED CliprdrClientContext* context,
2101 WINPR_ASSERT(context);
2102 WINPR_ASSERT(formatListResponse);
2104 return CHANNEL_RC_OK;
2113xf_cliprdr_server_format_data_request(CliprdrClientContext* context,
2116 const xfCliprdrFormat* format = NULL;
2118 WINPR_ASSERT(context);
2119 WINPR_ASSERT(formatDataRequest);
2121 xfClipboard* clipboard = cliprdr_file_context_get_context(context->custom);
2122 WINPR_ASSERT(clipboard);
2124 xfContext* xfc = clipboard->xfc;
2127 const uint32_t formatId = formatDataRequest->requestedFormatId;
2129 const BOOL rawTransfer = xf_cliprdr_is_raw_transfer_available(clipboard);
2133 format = xf_cliprdr_get_client_format_by_id(clipboard, CF_RAW);
2134 LogDynAndXChangeProperty(xfc->log, xfc->display, xfc->drawable, clipboard->property_atom,
2135 XA_INTEGER, 32, PropModeReplace, (
const BYTE*)&formatId, 1);
2138 format = xf_cliprdr_get_client_format_by_id(clipboard, formatId);
2140 clipboard->requestedFormatId = rawTransfer ? CF_RAW : formatId;
2142 return xf_cliprdr_send_data_response(clipboard, format, NULL, 0);
2144 DEBUG_CLIPRDR(
"requested format 0x%08" PRIx32
" [%s] {local 0x%08" PRIx32
" [%s]} [%s]",
2145 format->formatToRequest, ClipboardGetFormatIdString(format->formatToRequest),
2146 format->localFormat,
2147 ClipboardGetFormatName(clipboard->system, format->localFormat),
2148 format->formatName);
2149 LogDynAndXConvertSelection(xfc->log, xfc->display, clipboard->clipboard_atom, format->atom,
2150 clipboard->property_atom, xfc->drawable, CurrentTime);
2151 LogDynAndXFlush(xfc->log, xfc->display);
2153 return CHANNEL_RC_OK;
2162xf_cliprdr_server_format_data_response(CliprdrClientContext* context,
2166 BYTE* pDstData = NULL;
2169 UINT32 srcFormatId = 0;
2170 UINT32 dstFormatId = 0;
2171 BOOL nullTerminated = FALSE;
2172 xfCachedData* cached_data = NULL;
2174 WINPR_ASSERT(context);
2175 WINPR_ASSERT(formatDataResponse);
2177 xfClipboard* clipboard = cliprdr_file_context_get_context(context->custom);
2178 WINPR_ASSERT(clipboard);
2180 xfContext* xfc = clipboard->xfc;
2183 const UINT32 size = formatDataResponse->common.dataLen;
2184 const BYTE* data = formatDataResponse->requestedFormatData;
2186 if (formatDataResponse->common.msgFlags == CB_RESPONSE_FAIL)
2188 WLog_WARN(TAG,
"Format Data Response PDU msgFlags is CB_RESPONSE_FAIL");
2189 free(clipboard->respond);
2190 clipboard->respond = NULL;
2191 return CHANNEL_RC_OK;
2194 if (!clipboard->respond)
2195 return CHANNEL_RC_OK;
2197 const RequestedFormat* format = clipboard->requestedFormat;
2198 if (clipboard->data_raw_format)
2200 srcFormatId = CF_RAW;
2201 dstFormatId = CF_RAW;
2204 return ERROR_INTERNAL_ERROR;
2205 else if (format->formatName)
2207 dstFormatId = format->localFormat;
2209 ClipboardLock(clipboard->system);
2210 if (strcmp(format->formatName, type_HtmlFormat) == 0)
2212 srcFormatId = ClipboardGetFormatId(clipboard->system, type_HtmlFormat);
2213 dstFormatId = ClipboardGetFormatId(clipboard->system, mime_html);
2214 nullTerminated = TRUE;
2217 if (strcmp(format->formatName, type_FileGroupDescriptorW) == 0)
2219 if (!cliprdr_file_context_update_server_data(clipboard->file, clipboard->system, data,
2221 WLog_WARN(TAG,
"failed to update file descriptors");
2223 srcFormatId = ClipboardGetFormatId(clipboard->system, type_FileGroupDescriptorW);
2224 const xfCliprdrFormat* dstTargetFormat =
2225 xf_cliprdr_get_client_format_by_atom(clipboard, clipboard->respond->target);
2226 if (!dstTargetFormat)
2228 dstFormatId = ClipboardGetFormatId(clipboard->system, mime_uri_list);
2232 dstFormatId = dstTargetFormat->localFormat;
2235 nullTerminated = TRUE;
2237 ClipboardUnlock(clipboard->system);
2241 srcFormatId = format->formatToRequest;
2242 dstFormatId = format->localFormat;
2243 switch (format->formatToRequest)
2246 nullTerminated = TRUE;
2250 nullTerminated = TRUE;
2253 case CF_UNICODETEXT:
2254 nullTerminated = TRUE;
2258 srcFormatId = CF_DIB;
2262 srcFormatId = CF_TIFF;
2270 DEBUG_CLIPRDR(
"requested format 0x%08" PRIx32
" [%s] {local 0x%08" PRIx32
" [%s]} [%s]",
2271 format->formatToRequest, ClipboardGetFormatIdString(format->formatToRequest),
2272 format->localFormat,
2273 ClipboardGetFormatName(clipboard->system, format->localFormat),
2274 format->formatName);
2277 DEBUG_CLIPRDR(
"srcFormatId: 0x%08" PRIx32
", dstFormatId: 0x%08" PRIx32
"", srcFormatId,
2280 ClipboardLock(clipboard->system);
2281 bSuccess = ClipboardSetData(clipboard->system, srcFormatId, data, SrcSize);
2283 BOOL willQuit = FALSE;
2288 WLog_DBG(TAG,
"skipping, empty data detected!");
2289 free(clipboard->respond);
2290 clipboard->respond = NULL;
2295 pDstData = (BYTE*)ClipboardGetData(clipboard->system, dstFormatId, &DstSize);
2299 WLog_WARN(TAG,
"failed to get clipboard data in format %s [source format %s]",
2300 ClipboardGetFormatName(clipboard->system, dstFormatId),
2301 ClipboardGetFormatName(clipboard->system, srcFormatId));
2304 if (nullTerminated && pDstData)
2306 BYTE* nullTerminator = memchr(pDstData,
'\0', DstSize);
2309 const intptr_t diff = nullTerminator - pDstData;
2310 WINPR_ASSERT(diff >= 0);
2311 WINPR_ASSERT(diff <= UINT32_MAX);
2312 DstSize = (UINT32)diff;
2317 ClipboardUnlock(clipboard->system);
2319 return CHANNEL_RC_OK;
2325 cached_data = xf_cached_data_new(pDstData, DstSize);
2328 WLog_WARN(TAG,
"Failed to allocate cache entry");
2330 return CHANNEL_RC_OK;
2332 if (!HashTable_Insert(clipboard->cachedData, format_to_cache_slot(dstFormatId),
2335 WLog_WARN(TAG,
"Failed to cache clipboard data");
2336 xf_cached_data_free(cached_data);
2337 return CHANNEL_RC_OK;
2347 xfCachedData* cached_raw_data = xf_cached_data_new_copy(data, size);
2348 if (!cached_raw_data)
2349 WLog_WARN(TAG,
"Failed to allocate cache entry");
2352 if (!HashTable_Insert(clipboard->cachedRawData, (
void*)(UINT_PTR)srcFormatId,
2355 WLog_WARN(TAG,
"Failed to cache clipboard data");
2356 xf_cached_data_free(cached_raw_data);
2363 xf_cliprdr_provide_data(clipboard, clipboard->respond, pDstData, DstSize);
2368 XSelectionEvent* sev;
2371 conv.sev = clipboard->respond;
2373 LogDynAndXSendEvent(xfc->log, xfc->display, clipboard->respond->requestor, 0, 0, conv.ev);
2374 LogDynAndXFlush(xfc->log, xfc->display);
2376 free(clipboard->respond);
2377 clipboard->respond = NULL;
2378 return CHANNEL_RC_OK;
2381static BOOL xf_cliprdr_is_valid_unix_filename(LPCWSTR filename)
2386 if (filename[0] == L
'\0')
2390 for (
const WCHAR* c = filename; *c; ++c)
2399xfClipboard* xf_clipboard_new(xfContext* xfc, BOOL relieveFilenameRestriction)
2402 rdpChannels* channels = NULL;
2403 xfClipboard* clipboard = NULL;
2404 const char* selectionAtom = NULL;
2405 xfCliprdrFormat* clientFormat = NULL;
2409 WINPR_ASSERT(xfc->common.context.settings);
2411 if (!(clipboard = (xfClipboard*)calloc(1,
sizeof(xfClipboard))))
2413 WLog_ERR(TAG,
"failed to allocate xfClipboard data");
2417 clipboard->file = cliprdr_file_context_new(clipboard);
2418 if (!clipboard->file)
2421 xfc->clipboard = clipboard;
2422 clipboard->xfc = xfc;
2423 channels = xfc->common.context.channels;
2424 clipboard->channels = channels;
2425 clipboard->system = ClipboardCreate();
2426 clipboard->requestedFormatId = UINT32_MAX;
2427 clipboard->root_window = DefaultRootWindow(xfc->display);
2432 selectionAtom =
"CLIPBOARD";
2434 clipboard->clipboard_atom = Logging_XInternAtom(xfc->log, xfc->display, selectionAtom, FALSE);
2436 if (clipboard->clipboard_atom == None)
2438 WLog_ERR(TAG,
"unable to get %s atom", selectionAtom);
2442 clipboard->timestamp_property_atom =
2443 Logging_XInternAtom(xfc->log, xfc->display,
"_FREERDP_TIMESTAMP_PROPERTY", FALSE);
2444 clipboard->property_atom =
2445 Logging_XInternAtom(xfc->log, xfc->display,
"_FREERDP_CLIPRDR", FALSE);
2446 clipboard->raw_transfer_atom =
2447 Logging_XInternAtom(xfc->log, xfc->display,
"_FREERDP_CLIPRDR_RAW", FALSE);
2448 clipboard->raw_format_list_atom =
2449 Logging_XInternAtom(xfc->log, xfc->display,
"_FREERDP_CLIPRDR_FORMATS", FALSE);
2450 xf_cliprdr_set_raw_transfer_enabled(clipboard, TRUE);
2451 XSelectInput(xfc->display, clipboard->root_window, PropertyChangeMask);
2454 if (XFixesQueryExtension(xfc->display, &clipboard->xfixes_event_base,
2455 &clipboard->xfixes_error_base))
2460 if (XFixesQueryVersion(xfc->display, &xfmajor, &xfminor))
2462 XFixesSelectSelectionInput(xfc->display, clipboard->root_window,
2463 clipboard->clipboard_atom,
2464 XFixesSetSelectionOwnerNotifyMask);
2465 clipboard->xfixes_supported = TRUE;
2469 WLog_ERR(TAG,
"Error querying X Fixes extension version");
2474 WLog_ERR(TAG,
"Error loading X Fixes extension");
2480 "Warning: Using clipboard redirection without XFIXES extension is strongly discouraged!");
2482 clientFormat = &clipboard->clientFormats[n++];
2483 clientFormat->atom = Logging_XInternAtom(xfc->log, xfc->display,
"_FREERDP_RAW", False);
2484 clientFormat->localFormat = clientFormat->formatToRequest = CF_RAW;
2486 clientFormat = &clipboard->clientFormats[n++];
2487 clientFormat->atom = Logging_XInternAtom(xfc->log, xfc->display,
"UTF8_STRING", False);
2488 clientFormat->formatToRequest = CF_UNICODETEXT;
2489 clientFormat->localFormat = ClipboardGetFormatId(xfc->clipboard->system, mime_text_plain);
2491 clientFormat = &clipboard->clientFormats[n++];
2492 clientFormat->atom = XA_STRING;
2493 clientFormat->formatToRequest = CF_TEXT;
2494 clientFormat->localFormat = ClipboardGetFormatId(xfc->clipboard->system, mime_text_plain);
2496 clientFormat = &clipboard->clientFormats[n++];
2497 clientFormat->atom = Logging_XInternAtom(xfc->log, xfc->display, mime_tiff, False);
2498 clientFormat->formatToRequest = clientFormat->localFormat = CF_TIFF;
2500 for (
size_t x = 0; x < ARRAYSIZE(mime_bitmap); x++)
2502 const char* mime_bmp = mime_bitmap[x];
2503 const DWORD format = ClipboardGetFormatId(xfc->clipboard->system, mime_bmp);
2506 WLog_DBG(TAG,
"skipping local bitmap format %s [NOT SUPPORTED]", mime_bmp);
2510 WLog_DBG(TAG,
"register local bitmap format %s [0x%08" PRIx32
"]", mime_bmp, format);
2511 clientFormat = &clipboard->clientFormats[n++];
2512 clientFormat->localFormat = format;
2513 clientFormat->atom = Logging_XInternAtom(xfc->log, xfc->display, mime_bmp, False);
2514 clientFormat->formatToRequest = CF_DIB;
2515 clientFormat->isImage = TRUE;
2518 for (
size_t x = 0; x < ARRAYSIZE(mime_images); x++)
2520 const char* mime_bmp = mime_images[x];
2521 const DWORD format = ClipboardGetFormatId(xfc->clipboard->system, mime_bmp);
2524 WLog_DBG(TAG,
"skipping local bitmap format %s [NOT SUPPORTED]", mime_bmp);
2528 WLog_DBG(TAG,
"register local bitmap format %s [0x%08" PRIx32
"]", mime_bmp, format);
2529 clientFormat = &clipboard->clientFormats[n++];
2530 clientFormat->localFormat = format;
2531 clientFormat->atom = Logging_XInternAtom(xfc->log, xfc->display, mime_bmp, False);
2532 clientFormat->formatToRequest = CF_DIB;
2533 clientFormat->isImage = TRUE;
2536 clientFormat = &clipboard->clientFormats[n++];
2537 clientFormat->atom = Logging_XInternAtom(xfc->log, xfc->display, mime_html, False);
2538 clientFormat->formatToRequest = ClipboardGetFormatId(xfc->clipboard->system, type_HtmlFormat);
2539 clientFormat->localFormat = ClipboardGetFormatId(xfc->clipboard->system, mime_html);
2540 clientFormat->formatName = _strdup(type_HtmlFormat);
2542 if (!clientFormat->formatName)
2545 clientFormat = &clipboard->clientFormats[n++];
2554 const UINT32 fgid = ClipboardGetFormatId(clipboard->system, type_FileGroupDescriptorW);
2556 const UINT32 uid = ClipboardGetFormatId(clipboard->system, mime_uri_list);
2559 cliprdr_file_context_set_locally_available(clipboard->file, TRUE);
2560 clientFormat->atom =
2561 Logging_XInternAtom(xfc->log, xfc->display, mime_uri_list, False);
2562 clientFormat->localFormat = uid;
2563 clientFormat->formatToRequest = fgid;
2564 clientFormat->formatName = _strdup(type_FileGroupDescriptorW);
2566 if (!clientFormat->formatName)
2569 clientFormat = &clipboard->clientFormats[n++];
2574 const UINT32 gid = ClipboardGetFormatId(clipboard->system, mime_gnome_copied_files);
2577 cliprdr_file_context_set_locally_available(clipboard->file, TRUE);
2578 clientFormat->atom =
2579 Logging_XInternAtom(xfc->log, xfc->display, mime_gnome_copied_files, False);
2580 clientFormat->localFormat = gid;
2581 clientFormat->formatToRequest = fgid;
2582 clientFormat->formatName = _strdup(type_FileGroupDescriptorW);
2584 if (!clientFormat->formatName)
2587 clientFormat = &clipboard->clientFormats[n++];
2592 const UINT32 mid = ClipboardGetFormatId(clipboard->system, mime_mate_copied_files);
2595 cliprdr_file_context_set_locally_available(clipboard->file, TRUE);
2596 clientFormat->atom =
2597 Logging_XInternAtom(xfc->log, xfc->display, mime_mate_copied_files, False);
2598 clientFormat->localFormat = mid;
2599 clientFormat->formatToRequest = fgid;
2600 clientFormat->formatName = _strdup(type_FileGroupDescriptorW);
2602 if (!clientFormat->formatName)
2608 clipboard->numClientFormats = WINPR_ASSERTING_INT_CAST(uint32_t, n);
2609 clipboard->targets[0] = Logging_XInternAtom(xfc->log, xfc->display,
"TIMESTAMP", FALSE);
2610 clipboard->targets[1] = Logging_XInternAtom(xfc->log, xfc->display,
"TARGETS", FALSE);
2611 clipboard->numTargets = 2;
2612 clipboard->incr_atom = Logging_XInternAtom(xfc->log, xfc->display,
"INCR", FALSE);
2614 if (relieveFilenameRestriction)
2616 WLog_DBG(TAG,
"Relieving CLIPRDR filename restriction");
2617 ClipboardGetDelegate(clipboard->system)->IsFileNameComponentValid =
2618 xf_cliprdr_is_valid_unix_filename;
2621 clipboard->cachedData = HashTable_New(TRUE);
2622 if (!clipboard->cachedData)
2625 obj = HashTable_ValueObject(clipboard->cachedData);
2626 obj->fnObjectFree = xf_cached_data_free;
2628 clipboard->cachedRawData = HashTable_New(TRUE);
2629 if (!clipboard->cachedRawData)
2632 obj = HashTable_ValueObject(clipboard->cachedRawData);
2633 obj->fnObjectFree = xf_cached_data_free;
2638 WINPR_PRAGMA_DIAG_PUSH
2639 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
2640 xf_clipboard_free(clipboard);
2641 WINPR_PRAGMA_DIAG_POP
2645void xf_clipboard_free(xfClipboard* clipboard)
2650 xf_clipboard_free_server_formats(clipboard);
2652 if (clipboard->numClientFormats)
2654 for (UINT32 i = 0; i < clipboard->numClientFormats; i++)
2656 xfCliprdrFormat* format = &clipboard->clientFormats[i];
2657 free(format->formatName);
2661 cliprdr_file_context_free(clipboard->file);
2663 ClipboardDestroy(clipboard->system);
2664 xf_clipboard_formats_free(clipboard);
2665 HashTable_Free(clipboard->cachedRawData);
2666 HashTable_Free(clipboard->cachedData);
2667 requested_format_free(&clipboard->requestedFormat);
2668 free(clipboard->respond);
2669 free(clipboard->incr_data);
2673void xf_cliprdr_init(xfContext* xfc, CliprdrClientContext* cliprdr)
2676 WINPR_ASSERT(cliprdr);
2678 xfc->cliprdr = cliprdr;
2679 xfc->clipboard->context = cliprdr;
2681 cliprdr->MonitorReady = xf_cliprdr_monitor_ready;
2682 cliprdr->ServerCapabilities = xf_cliprdr_server_capabilities;
2683 cliprdr->ServerFormatList = xf_cliprdr_server_format_list;
2684 cliprdr->ServerFormatListResponse = xf_cliprdr_server_format_list_response;
2685 cliprdr->ServerFormatDataRequest = xf_cliprdr_server_format_data_request;
2686 cliprdr->ServerFormatDataResponse = xf_cliprdr_server_format_data_response;
2688 cliprdr_file_context_init(xfc->clipboard->file, cliprdr);
2691void xf_cliprdr_uninit(xfContext* xfc, CliprdrClientContext* cliprdr)
2694 WINPR_ASSERT(cliprdr);
2696 xfc->cliprdr = NULL;
2700 ClipboardLock(xfc->clipboard->system);
2701 cliprdr_file_context_uninit(xfc->clipboard->file, cliprdr);
2702 ClipboardUnlock(xfc->clipboard->system);
2703 xfc->clipboard->context = NULL;
FREERDP_API const char * freerdp_settings_get_string(const rdpSettings *settings, FreeRDP_Settings_Keys_String id)
Returns a immutable string settings value.
This struct contains function pointer to initialize/free objects.