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;
83 XSelectionEvent* expectedResponse;
84 RequestedFormat* requestedFormat;
91 rdpChannels* channels;
92 CliprdrClientContext* context;
100 Atom timestamp_property_atom;
101 Time selection_ownership_timestamp;
103 Atom raw_transfer_atom;
104 Atom raw_format_list_atom;
106 UINT32 numClientFormats;
107 xfCliprdrFormat clientFormats[20];
109 UINT32 numServerFormats;
115 UINT32 requestedFormatId;
117 wHashTable* cachedData;
118 wHashTable* cachedRawData;
120 wArrayList* pending_responses;
121 wArrayList* queued_responses;
130 size_t incr_data_length;
134 int xfixes_event_base;
135 int xfixes_error_base;
136 BOOL xfixes_supported;
140 UINT32 lastSentNumFormats;
141 CliprdrFileContext* file;
145static const char mime_text_plain[] =
"text/plain";
146static const char mime_uri_list[] =
"text/uri-list";
147static const char mime_html[] =
"text/html";
148static const char* mime_bitmap[] = {
"image/bmp",
"image/x-bmp",
"image/x-MS-bmp",
149 "image/x-win-bitmap" };
150static const char mime_webp[] =
"image/webp";
151static const char mime_png[] =
"image/png";
152static const char mime_jpeg[] =
"image/jpeg";
153static const char mime_tiff[] =
"image/tiff";
154static const char* mime_images[] = { mime_webp, mime_png, mime_jpeg, mime_tiff };
156static const char mime_gnome_copied_files[] =
"x-special/gnome-copied-files";
157static const char mime_mate_copied_files[] =
"x-special/mate-copied-files";
159static const char type_FileGroupDescriptorW[] =
"FileGroupDescriptorW";
160static const char type_HtmlFormat[] =
"HTML Format";
162static void xf_cliprdr_clear_cached_data(xfClipboard* clipboard);
163static UINT xf_cliprdr_send_client_format_list(xfClipboard* clipboard, BOOL force);
164static void xf_cliprdr_set_selection_owner(xfContext* xfc, xfClipboard* clipboard, Time timestamp);
166static void requested_format_free(RequestedFormat** ppRequestedFormat)
168 if (!ppRequestedFormat)
170 if (!(*ppRequestedFormat))
173 free((*ppRequestedFormat)->formatName);
174 free(*ppRequestedFormat);
175 *ppRequestedFormat =
nullptr;
178static BOOL requested_format_replace(RequestedFormat** ppRequestedFormat, UINT32 remoteFormatId,
179 UINT32 localFormatId,
const char* formatName)
181 if (!ppRequestedFormat)
184 requested_format_free(ppRequestedFormat);
185 RequestedFormat* requested = calloc(1,
sizeof(RequestedFormat));
188 requested->localFormat = localFormatId;
189 requested->formatToRequest = remoteFormatId;
192 requested->formatName = _strdup(formatName);
193 if (!requested->formatName)
200 *ppRequestedFormat = requested;
204static void selection_response_free(
void* ptr)
206 SelectionResponse* selection_response = (SelectionResponse*)ptr;
207 if (!selection_response)
210 free(selection_response->expectedResponse);
211 requested_format_free(&selection_response->requestedFormat);
212 free(selection_response);
215static void xf_cached_data_free(
void* ptr)
217 xfCachedData* cached_data = ptr;
221 free(cached_data->data);
225static xfCachedData* xf_cached_data_new(BYTE* data,
size_t data_length)
227 if (data_length > UINT32_MAX)
230 xfCachedData* cached_data = calloc(1,
sizeof(xfCachedData));
234 cached_data->data = data;
235 cached_data->data_length = (UINT32)data_length;
240static xfCachedData* xf_cached_data_new_copy(
const BYTE* data,
size_t data_length)
242 BYTE* copy =
nullptr;
245 copy = calloc(data_length + 1,
sizeof(BYTE));
248 memcpy(copy, data, data_length);
251 xfCachedData* cache = xf_cached_data_new(copy, data_length);
257static void xf_clipboard_free_server_formats(xfClipboard* clipboard)
259 WINPR_ASSERT(clipboard);
260 if (clipboard->serverFormats)
262 for (
size_t i = 0; i < clipboard->numServerFormats; i++)
265 free(format->formatName);
268 free(clipboard->serverFormats);
269 clipboard->serverFormats =
nullptr;
273static BOOL xf_cliprdr_update_owner(xfClipboard* clipboard)
275 WINPR_ASSERT(clipboard);
277 xfContext* xfc = clipboard->xfc;
280 if (!clipboard->sync)
283 Window owner = LogDynAndXGetSelectionOwner(xfc->log, xfc->display, clipboard->clipboard_atom);
284 if (clipboard->owner == owner)
287 clipboard->owner = owner;
291static void xf_cliprdr_check_owner(xfClipboard* clipboard)
293 if (xf_cliprdr_update_owner(clipboard))
294 xf_cliprdr_send_client_format_list(clipboard, FALSE);
297static BOOL xf_cliprdr_is_self_owned(xfClipboard* clipboard)
299 xfContext* xfc =
nullptr;
301 WINPR_ASSERT(clipboard);
303 xfc = clipboard->xfc;
305 return LogDynAndXGetSelectionOwner(xfc->log, xfc->display, clipboard->clipboard_atom) ==
309static void xf_cliprdr_set_raw_transfer_enabled(xfClipboard* clipboard, BOOL enabled)
311 UINT32 data = WINPR_ASSERTING_INT_CAST(uint32_t, enabled);
312 xfContext* xfc =
nullptr;
314 WINPR_ASSERT(clipboard);
316 xfc = clipboard->xfc;
318 LogDynAndXChangeProperty(xfc->log, xfc->display, xfc->drawable, clipboard->raw_transfer_atom,
319 XA_INTEGER, 32, PropModeReplace, (
const BYTE*)&data, 1);
322static BOOL xf_cliprdr_is_raw_transfer_available(xfClipboard* clipboard)
327 unsigned long length = 0;
328 unsigned long bytes_left = 0;
329 UINT32* data =
nullptr;
330 UINT32 is_enabled = 0;
332 xfContext* xfc =
nullptr;
334 WINPR_ASSERT(clipboard);
336 xfc = clipboard->xfc;
339 owner = LogDynAndXGetSelectionOwner(xfc->log, xfc->display, clipboard->clipboard_atom);
343 result = LogDynAndXGetWindowProperty(xfc->log, xfc->display, owner,
344 clipboard->raw_transfer_atom, 0, 4, 0, XA_INTEGER,
345 &type, &format, &length, &bytes_left, (BYTE**)&data);
354 if ((owner == None) || (owner == xfc->drawable))
357 if (result != Success)
360 return is_enabled != 0;
363static BOOL xf_cliprdr_formats_equal(
const CLIPRDR_FORMAT* server,
const xfCliprdrFormat* client)
365 WINPR_ASSERT(server);
366 WINPR_ASSERT(client);
368 if (server->formatName && client->formatName)
371 return (0 == strncmp(server->formatName, client->formatName, strlen(server->formatName)));
374 if (!server->formatName && !client->formatName)
376 return (server->formatId == client->formatToRequest);
382static const xfCliprdrFormat* xf_cliprdr_get_client_format_by_id(xfClipboard* clipboard,
385 WINPR_ASSERT(clipboard);
387 const BOOL formatIsHtml = formatId == ClipboardGetFormatId(clipboard->system, type_HtmlFormat);
388 const BOOL fetchImage = clipboard->isImageContent && formatIsHtml;
389 for (
size_t index = 0; index < clipboard->numClientFormats; index++)
391 const xfCliprdrFormat* format = &(clipboard->clientFormats[index]);
393 if (fetchImage && format->isImage)
396 if (format->formatToRequest == formatId)
403static const xfCliprdrFormat* xf_cliprdr_get_client_format_by_atom(xfClipboard* clipboard,
406 WINPR_ASSERT(clipboard);
408 for (UINT32 i = 0; i < clipboard->numClientFormats; i++)
410 const xfCliprdrFormat* format = &(clipboard->clientFormats[i]);
412 if (format->atom == atom)
419static const CLIPRDR_FORMAT* xf_cliprdr_get_server_format_by_atom(xfClipboard* clipboard, Atom atom)
421 WINPR_ASSERT(clipboard);
423 for (
size_t i = 0; i < clipboard->numClientFormats; i++)
425 const xfCliprdrFormat* client_format = &(clipboard->clientFormats[i]);
427 if (client_format->atom == atom)
429 for (
size_t j = 0; j < clipboard->numServerFormats; j++)
431 const CLIPRDR_FORMAT* server_format = &(clipboard->serverFormats[j]);
433 if (xf_cliprdr_formats_equal(server_format, client_format))
434 return server_format;
447static UINT xf_cliprdr_send_data_request(xfClipboard* clipboard, UINT32 formatId,
448 WINPR_ATTR_UNUSED
const xfCliprdrFormat* cformat)
451 request.requestedFormatId = formatId;
453 DEBUG_CLIPRDR(
"requesting format 0x%08" PRIx32
" [%s] {local 0x%08" PRIx32
"} [%s]", formatId,
454 ClipboardGetFormatIdString(formatId), cformat->localFormat, cformat->formatName);
456 WINPR_ASSERT(clipboard);
457 WINPR_ASSERT(clipboard->context);
458 WINPR_ASSERT(clipboard->context->ClientFormatDataRequest);
459 return clipboard->context->ClientFormatDataRequest(clipboard->context, &request);
467static UINT xf_cliprdr_send_data_response(xfClipboard* clipboard,
const xfCliprdrFormat* format,
468 const BYTE* data,
size_t size)
472 WINPR_ASSERT(clipboard);
475 if (clipboard->requestedFormatId == UINT32_MAX)
476 return CHANNEL_RC_OK;
481 DEBUG_CLIPRDR(
"send CB_RESPONSE_FAIL response {format 0x%08" PRIx32
482 " [%s] {local 0x%08" PRIx32
"} [%s]",
483 format->formatToRequest,
484 ClipboardGetFormatIdString(format->formatToRequest), format->localFormat,
487 DEBUG_CLIPRDR(
"send CB_RESPONSE_FAIL response");
491 WINPR_ASSERT(format);
492 DEBUG_CLIPRDR(
"send response format 0x%08" PRIx32
" [%s] {local 0x%08" PRIx32
" [%s]} [%s]",
493 format->formatToRequest, ClipboardGetFormatIdString(format->formatToRequest),
495 ClipboardGetFormatName(clipboard->system, format->localFormat),
499 clipboard->requestedFormatId = UINT32_MAX;
501 response.common.msgFlags = (data) ? CB_RESPONSE_OK : CB_RESPONSE_FAIL;
503 WINPR_ASSERT(size <= UINT32_MAX);
504 response.common.dataLen = (UINT32)size;
505 response.requestedFormatData = data;
507 WINPR_ASSERT(clipboard->context);
508 WINPR_ASSERT(clipboard->context->ClientFormatDataResponse);
509 return clipboard->context->ClientFormatDataResponse(clipboard->context, &response);
512static wStream* xf_cliprdr_serialize_server_format_list(xfClipboard* clipboard)
514 UINT32 formatCount = 0;
517 WINPR_ASSERT(clipboard);
520 if (!(s = Stream_New(
nullptr, 128)))
522 WLog_ERR(TAG,
"failed to allocate serialized format list");
527 formatCount = (clipboard->numServerFormats > 0) ? clipboard->numServerFormats - 1 : 0;
528 Stream_Write_UINT32(s, formatCount);
530 for (UINT32 i = 0; i < formatCount; i++)
533 size_t name_length = format->formatName ? strlen(format->formatName) : 0;
535 DEBUG_CLIPRDR(
"server announced 0x%08" PRIx32
" [%s][%s]", format->formatId,
536 ClipboardGetFormatIdString(format->formatId), format->formatName);
537 if (!Stream_EnsureRemainingCapacity(s,
sizeof(UINT32) + name_length + 1))
539 WLog_ERR(TAG,
"failed to expand serialized format list");
543 Stream_Write_UINT32(s, format->formatId);
545 if (format->formatName)
546 Stream_Write(s, format->formatName, name_length);
548 Stream_Write_UINT8(s,
'\0');
551 Stream_SealLength(s);
554 Stream_Free(s, TRUE);
558static CLIPRDR_FORMAT* xf_cliprdr_parse_server_format_list(BYTE* data,
size_t length,
564 WINPR_ASSERT(data || (length == 0));
565 WINPR_ASSERT(numFormats);
567 if (!(s = Stream_New(data, length)))
569 WLog_ERR(TAG,
"failed to allocate stream for parsing serialized format list");
573 if (!Stream_CheckAndLogRequiredLength(TAG, s,
sizeof(UINT32)))
576 Stream_Read_UINT32(s, *numFormats);
578 if (*numFormats > MAX_CLIPBOARD_FORMATS)
580 WLog_ERR(TAG,
"unexpectedly large number of formats: %" PRIu32
"", *numFormats);
586 WLog_ERR(TAG,
"failed to allocate format list");
590 for (UINT32 i = 0; i < *numFormats; i++)
592 const char* formatName =
nullptr;
593 size_t formatNameLength = 0;
595 if (!Stream_CheckAndLogRequiredLength(TAG, s,
sizeof(UINT32)))
598 Stream_Read_UINT32(s, formats[i].formatId);
599 formatName = (
const char*)Stream_Pointer(s);
600 formatNameLength = strnlen(formatName, Stream_GetRemainingLength(s));
602 if (formatNameLength == Stream_GetRemainingLength(s))
604 WLog_ERR(TAG,
"missing terminating null byte, %" PRIuz
" bytes left to read",
609 formats[i].formatName = strndup(formatName, formatNameLength);
610 Stream_Seek(s, formatNameLength + 1);
613 Stream_Free(s, FALSE);
616 Stream_Free(s, FALSE);
622static void xf_cliprdr_free_formats(
CLIPRDR_FORMAT* formats, UINT32 numFormats)
624 WINPR_ASSERT(formats || (numFormats == 0));
626 for (UINT32 i = 0; i < numFormats; i++)
628 free(formats[i].formatName);
634static CLIPRDR_FORMAT* xf_cliprdr_get_raw_server_formats(xfClipboard* clipboard, UINT32* numFormats)
638 unsigned long length = 0;
639 unsigned long remaining = 0;
640 BYTE* data =
nullptr;
642 xfContext* xfc =
nullptr;
644 WINPR_ASSERT(clipboard);
645 WINPR_ASSERT(numFormats);
647 xfc = clipboard->xfc;
652 Window owner = LogDynAndXGetSelectionOwner(xfc->log, xfc->display, clipboard->clipboard_atom);
653 LogDynAndXGetWindowProperty(xfc->log, xfc->display, owner, clipboard->raw_format_list_atom, 0,
654 4096, False, clipboard->raw_format_list_atom, &type, &format,
655 &length, &remaining, &data);
657 if (data && length > 0 && format == 8 && type == clipboard->raw_format_list_atom)
659 formats = xf_cliprdr_parse_server_format_list(data, length, numFormats);
664 "failed to retrieve raw format list: data=%p, length=%lu, format=%d, type=%lu "
666 (
void*)data, length, format, (
unsigned long)type,
667 (
unsigned long)clipboard->raw_format_list_atom);
676static BOOL xf_cliprdr_should_add_format(
const CLIPRDR_FORMAT* formats,
size_t count,
677 const xfCliprdrFormat* xformat)
679 WINPR_ASSERT(formats);
684 for (
size_t x = 0; x < count; x++)
687 if (format->formatId == xformat->formatToRequest)
693static CLIPRDR_FORMAT* xf_cliprdr_get_formats_from_targets(xfClipboard* clipboard,
697 BYTE* data =
nullptr;
698 int format_property = 0;
699 unsigned long proplength = 0;
700 unsigned long bytes_left = 0;
703 WINPR_ASSERT(clipboard);
704 WINPR_ASSERT(numFormats);
706 xfContext* xfc = clipboard->xfc;
710 LogDynAndXGetWindowProperty(xfc->log, xfc->display, xfc->drawable, clipboard->property_atom, 0,
711 200, 0, XA_ATOM, &atom, &format_property, &proplength, &bytes_left,
716 unsigned long length = proplength + 1;
719 WLog_ERR(TAG,
"XGetWindowProperty set length = %lu but data is nullptr", length);
725 WLog_ERR(TAG,
"failed to allocate %lu CLIPRDR_FORMAT structs", length);
731 BOOL isImage = FALSE;
732 BOOL hasHtml = FALSE;
733 const uint32_t htmlFormatId = ClipboardRegisterFormat(clipboard->system, type_HtmlFormat);
734 for (
unsigned long i = 0; i < proplength; i++)
736 Atom tatom = ((Atom*)data)[i];
737 const xfCliprdrFormat* format = xf_cliprdr_get_client_format_by_atom(clipboard, tatom);
739 if (xf_cliprdr_should_add_format(formats, *numFormats, format))
742 cformat->formatId = format->formatToRequest;
747 if (cformat->formatId == htmlFormatId)
752 if (cformat->formatId == CF_TIFF)
754 else if (cformat->formatId == CF_DIB)
756 else if (cformat->formatId == CF_DIBV5)
759 if (format->formatName)
761 cformat->formatName = _strdup(format->formatName);
762 WINPR_ASSERT(cformat->formatName);
765 cformat->formatName =
nullptr;
771 clipboard->isImageContent = isImage;
772 if (isImage && !hasHtml)
775 cformat->formatId = htmlFormatId;
776 cformat->formatName = _strdup(type_HtmlFormat);
789static CLIPRDR_FORMAT* xf_cliprdr_get_client_formats(xfClipboard* clipboard, UINT32* numFormats)
793 WINPR_ASSERT(clipboard);
794 WINPR_ASSERT(numFormats);
798 if (xf_cliprdr_is_raw_transfer_available(clipboard))
800 formats = xf_cliprdr_get_raw_server_formats(clipboard, numFormats);
803 if (*numFormats == 0)
805 xf_cliprdr_free_formats(formats, *numFormats);
806 formats = xf_cliprdr_get_formats_from_targets(clipboard, numFormats);
812static void xf_cliprdr_provide_server_format_list(xfClipboard* clipboard)
815 xfContext* xfc =
nullptr;
817 WINPR_ASSERT(clipboard);
819 xfc = clipboard->xfc;
822 formats = xf_cliprdr_serialize_server_format_list(clipboard);
826 const size_t len = Stream_Length(formats);
827 WINPR_ASSERT(len <= INT32_MAX);
828 LogDynAndXChangeProperty(xfc->log, xfc->display, xfc->drawable,
829 clipboard->raw_format_list_atom, clipboard->raw_format_list_atom,
830 8, PropModeReplace, Stream_Buffer(formats), (
int)len);
834 LogDynAndXDeleteProperty(xfc->log, xfc->display, xfc->drawable,
835 clipboard->raw_format_list_atom);
838 Stream_Free(formats, TRUE);
846 if (a->formatId != b->formatId)
848 if (!a->formatName && !b->formatName)
850 if (!a->formatName || !b->formatName)
852 return strcmp(a->formatName, b->formatName) == 0;
855static BOOL xf_clipboard_changed(xfClipboard* clipboard,
const CLIPRDR_FORMAT* formats,
858 WINPR_ASSERT(clipboard);
859 WINPR_ASSERT(formats || (numFormats == 0));
861 if (clipboard->lastSentNumFormats != numFormats)
864 for (UINT32 x = 0; x < numFormats; x++)
867 BOOL contained = FALSE;
868 for (UINT32 y = 0; y < numFormats; y++)
870 if (xf_clipboard_format_equal(cur, &formats[y]))
883static void xf_clipboard_formats_free(xfClipboard* clipboard)
885 WINPR_ASSERT(clipboard);
890 xf_lock_x11(clipboard->xfc);
891 xf_cliprdr_free_formats(clipboard->lastSentFormats, clipboard->lastSentNumFormats);
892 clipboard->lastSentFormats =
nullptr;
893 clipboard->lastSentNumFormats = 0;
894 xf_unlock_x11(clipboard->xfc);
897static BOOL xf_clipboard_copy_formats(xfClipboard* clipboard,
const CLIPRDR_FORMAT* formats,
900 WINPR_ASSERT(clipboard);
901 WINPR_ASSERT(formats || (numFormats == 0));
903 xf_clipboard_formats_free(clipboard);
905 clipboard->lastSentFormats = calloc(numFormats,
sizeof(
CLIPRDR_FORMAT));
906 if (!clipboard->lastSentFormats)
908 clipboard->lastSentNumFormats = numFormats;
909 for (UINT32 x = 0; x < numFormats; x++)
915 lcur->formatName = _strdup(cur->formatName);
920static UINT xf_cliprdr_send_format_list(xfClipboard* clipboard,
const CLIPRDR_FORMAT* formats,
921 UINT32 numFormats, BOOL force)
927 } cnv = { .cpv = formats };
929 .numFormats = numFormats,
931 .common.msgType = CB_FORMAT_LIST };
934 WINPR_ASSERT(clipboard);
935 WINPR_ASSERT(formats || (numFormats == 0));
937 if (!force && !xf_clipboard_changed(clipboard, formats, numFormats))
938 return CHANNEL_RC_OK;
940#if defined(WITH_DEBUG_CLIPRDR)
941 for (UINT32 x = 0; x < numFormats; x++)
944 DEBUG_CLIPRDR(
"announcing format 0x%08" PRIx32
" [%s] [%s]", format->formatId,
945 ClipboardGetFormatIdString(format->formatId), format->formatName);
949 xf_clipboard_copy_formats(clipboard, formats, numFormats);
951 xf_cliprdr_send_data_response(clipboard,
nullptr,
nullptr, 0);
953 xf_cliprdr_clear_cached_data(clipboard);
955 ret = cliprdr_file_context_notify_new_client_format_list(clipboard->file);
959 WINPR_ASSERT(clipboard->context);
960 WINPR_ASSERT(clipboard->context->ClientFormatList);
961 return clipboard->context->ClientFormatList(clipboard->context, &formatList);
964static void xf_cliprdr_get_requested_targets(xfClipboard* clipboard)
966 UINT32 numFormats = 0;
967 CLIPRDR_FORMAT* formats = xf_cliprdr_get_client_formats(clipboard, &numFormats);
968 xf_cliprdr_send_format_list(clipboard, formats, numFormats, FALSE);
969 xf_cliprdr_free_formats(formats, numFormats);
972static void xf_cliprdr_process_requested_data(xfClipboard* clipboard, BOOL hasData,
973 const BYTE* data,
size_t size)
978 INT64 srcFormatId = -1;
979 BYTE* pDstData =
nullptr;
980 const xfCliprdrFormat* format =
nullptr;
982 WINPR_ASSERT(clipboard);
984 if (clipboard->incr_starts && hasData)
989 clipboard->incr_data_length = 0;
991 format = xf_cliprdr_get_client_format_by_id(clipboard, clipboard->requestedFormatId);
993 if (!hasData || !data || !format)
995 xf_cliprdr_send_data_response(clipboard, format,
nullptr, 0);
999 switch (format->formatToRequest)
1002 srcFormatId = CF_RAW;
1007 case CF_UNICODETEXT:
1008 srcFormatId = format->localFormat;
1012 srcFormatId = format->localFormat;
1016 if (srcFormatId < 0)
1018 xf_cliprdr_send_data_response(clipboard, format,
nullptr, 0);
1022 ClipboardLock(clipboard->system);
1023 SrcSize = (UINT32)size;
1024 bSuccess = ClipboardSetData(clipboard->system, (UINT32)srcFormatId, data, SrcSize);
1030 (BYTE*)ClipboardGetData(clipboard->system, clipboard->requestedFormatId, &DstSize);
1032 ClipboardUnlock(clipboard->system);
1036 xf_cliprdr_send_data_response(clipboard, format,
nullptr, 0);
1047 ClipboardLock(clipboard->system);
1048 if (format->formatToRequest &&
1049 (format->formatToRequest ==
1050 ClipboardGetFormatId(clipboard->system, type_FileGroupDescriptorW)))
1052 UINT error = NO_ERROR;
1058 const UINT32 flags = cliprdr_file_context_remote_get_flags(clipboard->file);
1059 error = cliprdr_serialize_file_list_ex(flags, file_array, file_count, &pDstData, &DstSize);
1062 WLog_ERR(TAG,
"failed to serialize CLIPRDR_FILELIST: 0x%08X", error);
1065 UINT32 formatId = ClipboardGetFormatId(clipboard->system, mime_uri_list);
1066 UINT32 url_size = 0;
1068 char* url = ClipboardGetData(clipboard->system, formatId, &url_size);
1069 cliprdr_file_context_update_client_data(clipboard->file, url, url_size);
1075 ClipboardUnlock(clipboard->system);
1077 xf_cliprdr_send_data_response(clipboard, format, pDstData, DstSize);
1081static BOOL xf_restore_input_flags(xfClipboard* clipboard)
1083 WINPR_ASSERT(clipboard);
1085 xfContext* xfc = clipboard->xfc;
1088 if (clipboard->event_mask != 0)
1090 XSelectInput(xfc->display, xfc->drawable, clipboard->event_mask);
1091 clipboard->event_mask = 0;
1096static BOOL append(xfClipboard* clipboard,
const void* sdata,
size_t length)
1098 WINPR_ASSERT(clipboard);
1100 const size_t size = length + clipboard->incr_data_length + 2;
1101 BYTE* data = realloc(clipboard->incr_data, size);
1104 clipboard->incr_data = data;
1105 memcpy(&data[clipboard->incr_data_length], sdata, length);
1106 clipboard->incr_data_length += length;
1107 clipboard->incr_data[clipboard->incr_data_length + 0] =
'\0';
1108 clipboard->incr_data[clipboard->incr_data_length + 1] =
'\0';
1112static BOOL xf_cliprdr_stop_incr(xfClipboard* clipboard)
1114 clipboard->incr_starts = FALSE;
1115 clipboard->incr_data_length = 0;
1116 return xf_restore_input_flags(clipboard);
1119static BOOL xf_cliprdr_get_requested_data(xfClipboard* clipboard, Atom target)
1121 WINPR_ASSERT(clipboard);
1123 xfContext* xfc = clipboard->xfc;
1126 const xfCliprdrFormat* format =
1127 xf_cliprdr_get_client_format_by_id(clipboard, clipboard->requestedFormatId);
1129 if (!format || (format->atom != target))
1131 xf_cliprdr_send_data_response(clipboard, format,
nullptr, 0);
1136 BOOL has_data = FALSE;
1137 int format_property = 0;
1138 unsigned long length = 0;
1139 unsigned long total_bytes = 0;
1140 BYTE* property_data =
nullptr;
1141 const int rc = LogDynAndXGetWindowProperty(
1142 xfc->log, xfc->display, xfc->drawable, clipboard->property_atom, 0, 0, False, target, &type,
1143 &format_property, &length, &total_bytes, &property_data);
1145 XFree(property_data);
1148 xf_cliprdr_send_data_response(clipboard, format,
nullptr, 0);
1155 if ((total_bytes <= 0) && !clipboard->incr_starts)
1157 xf_cliprdr_stop_incr(clipboard);
1160 else if (type == clipboard->incr_atom)
1162 xf_cliprdr_stop_incr(clipboard);
1163 clipboard->incr_starts = TRUE;
1168 BYTE* incremental_data =
nullptr;
1169 unsigned long incremental_len = 0;
1172 len = clipboard->incr_data_length;
1173 if (total_bytes <= 0)
1175 xf_cliprdr_stop_incr(clipboard);
1179 else if (LogDynAndXGetWindowProperty(
1180 xfc->log, xfc->display, xfc->drawable, clipboard->property_atom, 0,
1181 WINPR_ASSERTING_INT_CAST(int32_t, total_bytes), False, target, &type,
1182 &format_property, &incremental_len, &length, &incremental_data) == Success)
1184 has_data = append(clipboard, incremental_data, incremental_len);
1185 len = clipboard->incr_data_length;
1188 if (incremental_data)
1189 XFree(incremental_data);
1192 LogDynAndXDeleteProperty(xfc->log, xfc->display, xfc->drawable, clipboard->property_atom);
1193 xf_cliprdr_process_requested_data(clipboard, has_data, clipboard->incr_data, len);
1198static void xf_cliprdr_append_target(xfClipboard* clipboard, Atom target)
1200 WINPR_ASSERT(clipboard);
1202 if (clipboard->numTargets >= ARRAYSIZE(clipboard->targets))
1205 for (
size_t i = 0; i < clipboard->numTargets; i++)
1207 if (clipboard->targets[i] == target)
1211 clipboard->targets[clipboard->numTargets++] = target;
1214static void xf_cliprdr_provide_targets(xfClipboard* clipboard,
const XSelectionEvent* respond)
1216 xfContext* xfc =
nullptr;
1218 WINPR_ASSERT(clipboard);
1220 xfc = clipboard->xfc;
1223 if (respond->property != None)
1225 WINPR_ASSERT(clipboard->numTargets <= INT32_MAX);
1226 LogDynAndXChangeProperty(xfc->log, xfc->display, respond->requestor, respond->property,
1227 XA_ATOM, 32, PropModeReplace, (
const BYTE*)clipboard->targets,
1228 (
int)clipboard->numTargets);
1232static void xf_cliprdr_provide_timestamp(xfClipboard* clipboard,
const XSelectionEvent* respond)
1234 xfContext* xfc =
nullptr;
1236 WINPR_ASSERT(clipboard);
1238 xfc = clipboard->xfc;
1241 if (respond->property != None)
1243 LogDynAndXChangeProperty(xfc->log, xfc->display, respond->requestor, respond->property,
1244 XA_INTEGER, 32, PropModeReplace,
1245 (
const BYTE*)&clipboard->selection_ownership_timestamp, 1);
1249#define xf_cliprdr_provide_data(clipboard, respond, data, size) \
1250 xf_cliprdr_provide_data_((clipboard), (respond), (data), (size), __FILE__, __func__, __LINE__)
1251static void xf_cliprdr_provide_data_(xfClipboard* clipboard,
const XSelectionEvent* respond,
1252 const BYTE* data, UINT32 size,
const char* file,
1253 const char* fkt,
size_t line)
1255 WINPR_ASSERT(clipboard);
1257 xfContext* xfc = clipboard->xfc;
1260 if (respond->property != None)
1262 LogDynAndXChangeProperty_ex(xfc->log, file, fkt, line, xfc->display, respond->requestor,
1263 respond->property, respond->target, 8, PropModeReplace, data,
1264 WINPR_ASSERTING_INT_CAST(int32_t, size));
1268static void log_selection_event(xfContext* xfc,
const XEvent* event)
1270 const DWORD level = WLOG_TRACE;
1271 static wLog* _log_cached_ptr =
nullptr;
1272 if (!_log_cached_ptr)
1273 _log_cached_ptr = WLog_Get(TAG);
1274 if (WLog_IsLevelActive(_log_cached_ptr, level))
1277 switch (event->type)
1279 case SelectionClear:
1281 const XSelectionClearEvent* xevent = &
event->xselectionclear;
1283 Safe_XGetAtomName(_log_cached_ptr, xfc->display, xevent->selection);
1284 WLog_Print(_log_cached_ptr, level,
"got event %s [selection %s]",
1285 x11_event_string(event->type), selection);
1289 case SelectionNotify:
1291 const XSelectionEvent* xevent = &
event->xselection;
1293 Safe_XGetAtomName(_log_cached_ptr, xfc->display, xevent->selection);
1294 char* target = Safe_XGetAtomName(_log_cached_ptr, xfc->display, xevent->target);
1295 char*
property = Safe_XGetAtomName(_log_cached_ptr, xfc->display, xevent->property);
1296 WLog_Print(_log_cached_ptr, level,
1297 "got event %s [selection %s, target %s, property %s]",
1298 x11_event_string(event->type), selection, target, property);
1304 case SelectionRequest:
1306 const XSelectionRequestEvent* xevent = &
event->xselectionrequest;
1308 Safe_XGetAtomName(_log_cached_ptr, xfc->display, xevent->selection);
1309 char* target = Safe_XGetAtomName(_log_cached_ptr, xfc->display, xevent->target);
1310 char*
property = Safe_XGetAtomName(_log_cached_ptr, xfc->display, xevent->property);
1311 WLog_Print(_log_cached_ptr, level,
1312 "got event %s [selection %s, target %s, property %s]",
1313 x11_event_string(event->type), selection, target, property);
1319 case PropertyNotify:
1321 const XPropertyEvent* xevent = &
event->xproperty;
1322 char* atom = Safe_XGetAtomName(_log_cached_ptr, xfc->display, xevent->atom);
1323 WLog_Print(_log_cached_ptr, level,
"got event %s [atom %s]",
1324 x11_event_string(event->type), atom);
1334static BOOL xf_cliprdr_process_selection_notify(xfClipboard* clipboard,
1335 const XSelectionEvent* xevent)
1337 WINPR_ASSERT(clipboard);
1338 WINPR_ASSERT(xevent);
1340 if (xevent->target == clipboard->targets[1])
1342 if (xevent->property == None)
1344 xf_cliprdr_send_client_format_list(clipboard, FALSE);
1348 xf_cliprdr_get_requested_targets(clipboard);
1355 return xf_cliprdr_get_requested_data(clipboard, xevent->target);
1359void xf_cliprdr_clear_cached_data(xfClipboard* clipboard)
1361 WINPR_ASSERT(clipboard);
1363 ClipboardLock(clipboard->system);
1364 ClipboardEmpty(clipboard->system);
1366 HashTable_Clear(clipboard->cachedData);
1367 HashTable_Clear(clipboard->cachedRawData);
1369 cliprdr_file_context_clear(clipboard->file);
1371 xf_cliprdr_stop_incr(clipboard);
1372 ClipboardUnlock(clipboard->system);
1375static void* format_to_cache_slot(UINT32 format)
1382 cnv.uptr = 0x100000000ULL + format;
1386static UINT32 get_dst_format_id_for_local_request(xfClipboard* clipboard,
1387 const xfCliprdrFormat* format)
1389 UINT32 dstFormatId = 0;
1391 WINPR_ASSERT(format);
1393 if (!format->formatName)
1394 return format->localFormat;
1396 ClipboardLock(clipboard->system);
1397 if (strcmp(format->formatName, type_HtmlFormat) == 0)
1398 dstFormatId = ClipboardGetFormatId(clipboard->system, mime_html);
1399 ClipboardUnlock(clipboard->system);
1401 if (strcmp(format->formatName, type_FileGroupDescriptorW) == 0)
1402 dstFormatId = format->localFormat;
1407static void get_src_format_info_for_local_request(xfClipboard* clipboard,
1408 const xfCliprdrFormat* format,
1409 UINT32* srcFormatId, BOOL* nullTerminated)
1412 *nullTerminated = FALSE;
1414 if (format->formatName)
1416 ClipboardLock(clipboard->system);
1417 if (strcmp(format->formatName, type_HtmlFormat) == 0)
1419 *srcFormatId = ClipboardGetFormatId(clipboard->system, type_HtmlFormat);
1420 *nullTerminated = TRUE;
1422 else if (strcmp(format->formatName, type_FileGroupDescriptorW) == 0)
1424 *srcFormatId = ClipboardGetFormatId(clipboard->system, type_FileGroupDescriptorW);
1425 *nullTerminated = TRUE;
1427 ClipboardUnlock(clipboard->system);
1431 *srcFormatId = format->formatToRequest;
1432 switch (format->formatToRequest)
1436 case CF_UNICODETEXT:
1437 *nullTerminated = TRUE;
1440 *srcFormatId = CF_DIB;
1443 *srcFormatId = CF_TIFF;
1451static xfCachedData* convert_data_from_existing_raw_data(xfClipboard* clipboard,
1452 xfCachedData* cached_raw_data,
1453 UINT32 srcFormatId, BOOL nullTerminated,
1456 UINT32 dst_size = 0;
1458 WINPR_ASSERT(clipboard);
1459 WINPR_ASSERT(cached_raw_data);
1460 WINPR_ASSERT(cached_raw_data->data);
1462 ClipboardLock(clipboard->system);
1463 BOOL success = ClipboardSetData(clipboard->system, srcFormatId, cached_raw_data->data,
1464 cached_raw_data->data_length);
1467 WLog_WARN(TAG,
"Failed to set clipboard data (formatId: %u, data: %p, data_length: %u)",
1468 srcFormatId, WINPR_CXX_COMPAT_CAST(
const void*, cached_raw_data->data),
1469 cached_raw_data->data_length);
1470 ClipboardUnlock(clipboard->system);
1474 BYTE* dst_data = ClipboardGetData(clipboard->system, dstFormatId, &dst_size);
1477 WLog_WARN(TAG,
"Failed to get converted clipboard data");
1478 ClipboardUnlock(clipboard->system);
1481 ClipboardUnlock(clipboard->system);
1485 BYTE* nullTerminator = memchr(dst_data,
'\0', dst_size);
1488 const intptr_t diff = nullTerminator - dst_data;
1489 WINPR_ASSERT(diff >= 0);
1490 WINPR_ASSERT(diff <= UINT32_MAX);
1491 dst_size = (UINT32)diff;
1495 xfCachedData* cached_data = xf_cached_data_new(dst_data, dst_size);
1498 WLog_WARN(TAG,
"Failed to allocate cache entry");
1503 if (!HashTable_Insert(clipboard->cachedData, format_to_cache_slot(dstFormatId), cached_data))
1505 WLog_WARN(TAG,
"Failed to cache clipboard data");
1506 xf_cached_data_free(cached_data);
1514static BOOL xf_cliprdr_pending_responses_ArrayList_ForEachFkt(
void* data,
size_t index, va_list ap)
1516 UINT32 formatId = 0;
1517 SelectionResponse* pendingResponse = (SelectionResponse*)data;
1518 UINT32 currentFormatId = va_arg(ap, UINT32);
1519 BOOL* res = va_arg(ap, BOOL*);
1521 WINPR_UNUSED(index);
1525 formatId = pendingResponse->requestedFormat->formatToRequest;
1527 if (formatId != currentFormatId)
1532static void xf_cliprdr_provide_selection(xfClipboard* clipboard, XSelectionEvent* respond)
1534 WINPR_ASSERT(clipboard);
1536 xfContext* xfc = clipboard->xfc;
1542 XSelectionEvent* sev;
1546 LogDynAndXSendEvent(xfc->log, xfc->display, respond->requestor, 0, 0, conv.ev);
1547 LogDynAndXFlush(xfc->log, xfc->display);
1550static BOOL xf_cliprdr_process_selection_request(xfClipboard* clipboard,
1551 const XSelectionRequestEvent* xevent)
1555 UINT32 formatId = 0;
1556 XSelectionEvent* respond =
nullptr;
1557 BYTE* data =
nullptr;
1558 BOOL delayRespond = 0;
1559 BOOL rawTransfer = 0;
1560 unsigned long length = 0;
1561 unsigned long bytes_left = 0;
1562 xfContext* xfc =
nullptr;
1564 WINPR_ASSERT(clipboard);
1565 WINPR_ASSERT(xevent);
1567 xfc = clipboard->xfc;
1570 if (xevent->owner != xfc->drawable)
1573 delayRespond = FALSE;
1575 if (!(respond = (XSelectionEvent*)calloc(1,
sizeof(XSelectionEvent))))
1577 WLog_ERR(TAG,
"failed to allocate XEvent data");
1581 respond->property = None;
1582 respond->type = SelectionNotify;
1583 respond->display = xevent->display;
1584 respond->requestor = xevent->requestor;
1585 respond->selection = xevent->selection;
1586 respond->target = xevent->target;
1587 respond->time = xevent->time;
1589 if (xevent->target == clipboard->targets[0])
1592 respond->property = xevent->property;
1593 xf_cliprdr_provide_timestamp(clipboard, respond);
1595 else if (xevent->target == clipboard->targets[1])
1598 respond->property = xevent->property;
1599 xf_cliprdr_provide_targets(clipboard, respond);
1604 xf_cliprdr_get_server_format_by_atom(clipboard, xevent->target);
1605 const xfCliprdrFormat* cformat =
1606 xf_cliprdr_get_client_format_by_atom(clipboard, xevent->target);
1608 if (format && (xevent->requestor != xfc->drawable))
1610 formatId = format->formatId;
1611 rawTransfer = FALSE;
1612 xfCachedData* cached_data =
nullptr;
1614 if (formatId == CF_RAW)
1616 if (LogDynAndXGetWindowProperty(
1617 xfc->log, xfc->display, xevent->requestor, clipboard->property_atom, 0, 4,
1618 0, XA_INTEGER, &type, &fmt, &length, &bytes_left, &data) != Success)
1625 CopyMemory(&formatId, data, 4);
1630 const UINT32 dstFormatId = get_dst_format_id_for_local_request(clipboard, cformat);
1631 DEBUG_CLIPRDR(
"formatId: 0x%08" PRIx32
", dstFormatId: 0x%08" PRIx32
"", formatId,
1634 wHashTable* table = clipboard->cachedData;
1636 table = clipboard->cachedRawData;
1638 HashTable_Lock(table);
1640 cached_data = HashTable_GetItemValue(table, format_to_cache_slot(dstFormatId));
1642 cached_data = HashTable_GetItemValue(table, format_to_cache_slot(formatId));
1643 HashTable_Unlock(table);
1645 DEBUG_CLIPRDR(
"hasCachedData: %u, rawTransfer: %d", cached_data ? 1u : 0u, rawTransfer);
1647 if (!cached_data && !rawTransfer)
1649 UINT32 srcFormatId = 0;
1650 BOOL nullTerminated = FALSE;
1651 xfCachedData* cached_raw_data =
nullptr;
1653 get_src_format_info_for_local_request(clipboard, cformat, &srcFormatId,
1656 HashTable_Lock(clipboard->cachedRawData);
1658 HashTable_GetItemValue(clipboard->cachedRawData, (
void*)(UINT_PTR)srcFormatId);
1659 HashTable_Unlock(clipboard->cachedRawData);
1661 DEBUG_CLIPRDR(
"hasCachedRawData: %u, rawDataLength: %u", cached_raw_data ? 1u : 0u,
1662 cached_raw_data ? cached_raw_data->data_length : 0);
1664 if (cached_raw_data && cached_raw_data->data_length != 0)
1665 cached_data = convert_data_from_existing_raw_data(
1666 clipboard, cached_raw_data, srcFormatId, nullTerminated, dstFormatId);
1669 DEBUG_CLIPRDR(
"hasCachedData: %u", cached_data ? 1u : 0u);
1674 respond->property = xevent->property;
1677 xf_cliprdr_provide_data(clipboard, respond, cached_data->data,
1678 cached_data->data_length);
1682 SelectionResponse* selection_response =
nullptr;
1683 WINPR_ASSERT(cformat);
1685 if (!(selection_response =
1686 (SelectionResponse*)calloc(1,
sizeof(SelectionResponse))))
1688 respond->property = None;
1691 respond->property = xevent->property;
1693 selection_response->expectedResponse = respond;
1694 requested_format_replace(&selection_response->requestedFormat, formatId,
1695 dstFormatId, cformat->formatName);
1696 selection_response->data_raw_format = rawTransfer;
1698 ArrayList_Lock(clipboard->pending_responses);
1699 ArrayList_Lock(clipboard->queued_responses);
1700 if (ArrayList_Count(clipboard->pending_responses) > 0)
1702 BOOL shouldQueued = FALSE;
1703 BOOL success = FALSE;
1704 success = ArrayList_ForEach(clipboard->pending_responses,
1705 xf_cliprdr_pending_responses_ArrayList_ForEachFkt,
1706 formatId, &shouldQueued);
1707 if (!success || shouldQueued)
1709 if (!ArrayList_Append(clipboard->queued_responses, selection_response))
1711 requested_format_free(&selection_response->requestedFormat);
1712 free(selection_response);
1713 respond->property = None;
1719 if (!ArrayList_Append(clipboard->pending_responses, selection_response))
1721 requested_format_free(&selection_response->requestedFormat);
1722 free(selection_response);
1723 respond->property = None;
1730 if (!ArrayList_Append(clipboard->pending_responses, selection_response))
1732 requested_format_free(&selection_response->requestedFormat);
1733 free(selection_response);
1734 respond->property = None;
1742 xf_cliprdr_send_data_request(clipboard, formatId, cformat);
1744 delayRespond = TRUE;
1746 ArrayList_Unlock(clipboard->queued_responses);
1747 ArrayList_Unlock(clipboard->pending_responses);
1755 xf_cliprdr_provide_selection(clipboard, respond);
1762static BOOL xf_cliprdr_process_selection_clear(xfClipboard* clipboard,
1763 const XSelectionClearEvent* xevent)
1765 xfContext* xfc =
nullptr;
1767 WINPR_ASSERT(clipboard);
1768 WINPR_ASSERT(xevent);
1770 xfc = clipboard->xfc;
1773 WINPR_UNUSED(xevent);
1775 if (xf_cliprdr_is_self_owned(clipboard))
1778 LogDynAndXDeleteProperty(xfc->log, xfc->display, clipboard->root_window,
1779 clipboard->property_atom);
1783static BOOL xf_cliprdr_process_property_notify(xfClipboard* clipboard,
const XPropertyEvent* xevent)
1785 const xfCliprdrFormat* format =
nullptr;
1786 xfContext* xfc =
nullptr;
1791 xfc = clipboard->xfc;
1793 WINPR_ASSERT(xevent);
1795 if (xevent->atom == clipboard->timestamp_property_atom)
1801 xf_cliprdr_set_selection_owner(xfc, clipboard, xevent->time);
1805 if (xevent->atom != clipboard->property_atom)
1808 if (xevent->window == clipboard->root_window)
1810 xf_cliprdr_send_client_format_list(clipboard, FALSE);
1812 else if ((xevent->window == xfc->drawable) && (xevent->state == PropertyNewValue) &&
1813 clipboard->incr_starts)
1815 format = xf_cliprdr_get_client_format_by_id(clipboard, clipboard->requestedFormatId);
1818 xf_cliprdr_get_requested_data(clipboard, format->atom);
1824void xf_cliprdr_handle_xevent(xfContext* xfc,
const XEvent* event)
1826 xfClipboard* clipboard =
nullptr;
1831 clipboard = xfc->clipboard;
1838 if (clipboard->xfixes_supported &&
1839 event->type == XFixesSelectionNotify + clipboard->xfixes_event_base)
1841 const XFixesSelectionNotifyEvent* se = (
const XFixesSelectionNotifyEvent*)event;
1843 if (se->subtype == XFixesSetSelectionOwnerNotify)
1845 if (se->selection != clipboard->clipboard_atom)
1848 if (LogDynAndXGetSelectionOwner(xfc->log, xfc->display, se->selection) == xfc->drawable)
1851 clipboard->owner = None;
1852 xf_cliprdr_check_owner(clipboard);
1860 switch (event->type)
1862 case SelectionNotify:
1863 log_selection_event(xfc, event);
1864 xf_cliprdr_process_selection_notify(clipboard, &event->xselection);
1867 case SelectionRequest:
1868 log_selection_event(xfc, event);
1869 xf_cliprdr_process_selection_request(clipboard, &event->xselectionrequest);
1872 case SelectionClear:
1873 log_selection_event(xfc, event);
1874 xf_cliprdr_process_selection_clear(clipboard, &event->xselectionclear);
1877 case PropertyNotify:
1878 log_selection_event(xfc, event);
1879 xf_cliprdr_process_property_notify(clipboard, &event->xproperty);
1883 if (!clipboard->xfixes_supported)
1885 xf_cliprdr_check_owner(clipboard);
1899static UINT xf_cliprdr_send_client_capabilities(xfClipboard* clipboard)
1904 WINPR_ASSERT(clipboard);
1906 capabilities.cCapabilitiesSets = 1;
1908 generalCapabilitySet.capabilitySetType = CB_CAPSTYPE_GENERAL;
1909 generalCapabilitySet.capabilitySetLength = 12;
1910 generalCapabilitySet.version = CB_CAPS_VERSION_2;
1911 generalCapabilitySet.generalFlags = CB_USE_LONG_FORMAT_NAMES;
1913 WINPR_ASSERT(clipboard);
1914 generalCapabilitySet.generalFlags |= cliprdr_file_context_current_flags(clipboard->file);
1916 WINPR_ASSERT(clipboard->context);
1917 WINPR_ASSERT(clipboard->context->ClientCapabilities);
1918 return clipboard->context->ClientCapabilities(clipboard->context, &capabilities);
1926static UINT xf_cliprdr_send_client_format_list(xfClipboard* clipboard, BOOL force)
1928 WINPR_ASSERT(clipboard);
1930 xfContext* xfc = clipboard->xfc;
1933 UINT32 numFormats = 0;
1934 CLIPRDR_FORMAT* formats = xf_cliprdr_get_client_formats(clipboard, &numFormats);
1936 const UINT ret = xf_cliprdr_send_format_list(clipboard, formats, numFormats, force);
1938 if (clipboard->owner && clipboard->owner != xfc->drawable)
1941 LogDynAndXConvertSelection(xfc->log, xfc->display, clipboard->clipboard_atom,
1942 clipboard->targets[1], clipboard->property_atom, xfc->drawable,
1946 xf_cliprdr_free_formats(formats, numFormats);
1956static UINT xf_cliprdr_send_client_format_list_response(xfClipboard* clipboard, BOOL status)
1960 formatListResponse.common.msgType = CB_FORMAT_LIST_RESPONSE;
1961 formatListResponse.common.msgFlags = status ? CB_RESPONSE_OK : CB_RESPONSE_FAIL;
1962 formatListResponse.common.dataLen = 0;
1964 WINPR_ASSERT(clipboard);
1965 WINPR_ASSERT(clipboard->context);
1966 WINPR_ASSERT(clipboard->context->ClientFormatListResponse);
1967 return clipboard->context->ClientFormatListResponse(clipboard->context, &formatListResponse);
1975static UINT xf_cliprdr_monitor_ready(CliprdrClientContext* context,
1978 WINPR_ASSERT(context);
1979 WINPR_ASSERT(monitorReady);
1981 xfClipboard* clipboard = cliprdr_file_context_get_context(context->custom);
1982 WINPR_ASSERT(clipboard);
1984 WINPR_UNUSED(monitorReady);
1986 const UINT ret = xf_cliprdr_send_client_capabilities(clipboard);
1987 if (ret != CHANNEL_RC_OK)
1990 xf_clipboard_formats_free(clipboard);
1992 const UINT ret2 = xf_cliprdr_send_client_format_list(clipboard, TRUE);
1993 if (ret2 != CHANNEL_RC_OK)
1996 clipboard->sync = TRUE;
1997 return CHANNEL_RC_OK;
2005static UINT xf_cliprdr_server_capabilities(CliprdrClientContext* context,
2008 WINPR_ASSERT(context);
2009 WINPR_ASSERT(capabilities);
2011 xfClipboard* clipboard = cliprdr_file_context_get_context(context->custom);
2012 WINPR_ASSERT(clipboard);
2014 const BYTE* capsPtr = (
const BYTE*)capabilities->capabilitySets;
2015 WINPR_ASSERT(capsPtr);
2017 if (!cliprdr_file_context_remote_set_flags(clipboard->file, 0))
2018 return ERROR_INTERNAL_ERROR;
2020 for (UINT32 i = 0; i < capabilities->cCapabilitiesSets; i++)
2024 if (caps->capabilitySetType == CB_CAPSTYPE_GENERAL)
2029 if (!cliprdr_file_context_remote_set_flags(clipboard->file, generalCaps->generalFlags))
2030 return ERROR_INTERNAL_ERROR;
2033 capsPtr += caps->capabilitySetLength;
2036 return CHANNEL_RC_OK;
2039static void xf_cliprdr_prepare_to_set_selection_owner(xfContext* xfc, xfClipboard* clipboard)
2042 WINPR_ASSERT(clipboard);
2059 Atom value = clipboard->timestamp_property_atom;
2061 LogDynAndXChangeProperty(xfc->log, xfc->display, xfc->drawable,
2062 clipboard->timestamp_property_atom, XA_ATOM, 32, PropModeReplace,
2063 (
const BYTE*)&value, 1);
2064 LogDynAndXFlush(xfc->log, xfc->display);
2067static void xf_cliprdr_set_selection_owner(xfContext* xfc, xfClipboard* clipboard, Time timestamp)
2070 WINPR_ASSERT(clipboard);
2076 clipboard->selection_ownership_timestamp = timestamp;
2077 LogDynAndXSetSelectionOwner(xfc->log, xfc->display, clipboard->clipboard_atom, xfc->drawable,
2079 LogDynAndXFlush(xfc->log, xfc->display);
2087static UINT xf_cliprdr_server_format_list(CliprdrClientContext* context,
2090 xfContext* xfc =
nullptr;
2092 xfClipboard* clipboard =
nullptr;
2094 WINPR_ASSERT(context);
2095 WINPR_ASSERT(formatList);
2097 clipboard = cliprdr_file_context_get_context(context->custom);
2098 WINPR_ASSERT(clipboard);
2100 xfc = clipboard->xfc;
2106 ArrayList_Clear(clipboard->pending_responses);
2107 ArrayList_Clear(clipboard->queued_responses);
2109 xf_clipboard_formats_free(clipboard);
2110 xf_cliprdr_clear_cached_data(clipboard);
2112 xf_clipboard_free_server_formats(clipboard);
2114 clipboard->numServerFormats = formatList->numFormats + 1;
2116 if (!(clipboard->serverFormats =
2119 WLog_ERR(TAG,
"failed to allocate %" PRIu32
" CLIPRDR_FORMAT structs",
2120 clipboard->numServerFormats);
2121 ret = CHANNEL_RC_NO_MEMORY;
2125 for (
size_t i = 0; i < formatList->numFormats; i++)
2130 srvFormat->formatId = format->formatId;
2132 if (format->formatName)
2134 srvFormat->formatName = _strdup(format->formatName);
2136 if (!srvFormat->formatName)
2138 for (UINT32 k = 0; k < i; k++)
2139 free(clipboard->serverFormats[k].formatName);
2141 clipboard->numServerFormats = 0;
2142 free(clipboard->serverFormats);
2143 clipboard->serverFormats =
nullptr;
2144 ret = CHANNEL_RC_NO_MEMORY;
2150 ClipboardLock(clipboard->system);
2151 ret = cliprdr_file_context_notify_new_server_format_list(clipboard->file);
2152 ClipboardUnlock(clipboard->system);
2158 CLIPRDR_FORMAT* format = &clipboard->serverFormats[formatList->numFormats];
2159 format->formatId = CF_RAW;
2160 format->formatName =
nullptr;
2162 xf_cliprdr_provide_server_format_list(clipboard);
2163 clipboard->numTargets = 2;
2165 for (
size_t i = 0; i < formatList->numFormats; i++)
2169 for (
size_t j = 0; j < clipboard->numClientFormats; j++)
2171 const xfCliprdrFormat* clientFormat = &clipboard->clientFormats[j];
2172 if (xf_cliprdr_formats_equal(format, clientFormat))
2174 if ((clientFormat->formatName !=
nullptr) &&
2175 (strcmp(type_FileGroupDescriptorW, clientFormat->formatName) == 0))
2177 if (!cliprdr_file_context_has_local_support(clipboard->file))
2180 xf_cliprdr_append_target(clipboard, clientFormat->atom);
2185 ret = xf_cliprdr_send_client_format_list_response(clipboard, TRUE);
2186 if (xfc->remote_app)
2187 xf_cliprdr_set_selection_owner(xfc, clipboard, CurrentTime);
2189 xf_cliprdr_prepare_to_set_selection_owner(xfc, clipboard);
2202static UINT xf_cliprdr_server_format_list_response(
2203 WINPR_ATTR_UNUSED CliprdrClientContext* context,
2206 WINPR_ASSERT(context);
2207 WINPR_ASSERT(formatListResponse);
2209 return CHANNEL_RC_OK;
2218xf_cliprdr_server_format_data_request(CliprdrClientContext* context,
2221 const xfCliprdrFormat* format =
nullptr;
2223 WINPR_ASSERT(context);
2224 WINPR_ASSERT(formatDataRequest);
2226 xfClipboard* clipboard = cliprdr_file_context_get_context(context->custom);
2227 WINPR_ASSERT(clipboard);
2229 xfContext* xfc = clipboard->xfc;
2232 const uint32_t formatId = formatDataRequest->requestedFormatId;
2234 const BOOL rawTransfer = xf_cliprdr_is_raw_transfer_available(clipboard);
2238 format = xf_cliprdr_get_client_format_by_id(clipboard, CF_RAW);
2239 LogDynAndXChangeProperty(xfc->log, xfc->display, xfc->drawable, clipboard->property_atom,
2240 XA_INTEGER, 32, PropModeReplace, (
const BYTE*)&formatId, 1);
2243 format = xf_cliprdr_get_client_format_by_id(clipboard, formatId);
2245 clipboard->requestedFormatId = rawTransfer ? CF_RAW : formatId;
2247 return xf_cliprdr_send_data_response(clipboard, format,
nullptr, 0);
2249 DEBUG_CLIPRDR(
"requested format 0x%08" PRIx32
" [%s] {local 0x%08" PRIx32
" [%s]} [%s]",
2250 format->formatToRequest, ClipboardGetFormatIdString(format->formatToRequest),
2251 format->localFormat,
2252 ClipboardGetFormatName(clipboard->system, format->localFormat),
2253 format->formatName);
2254 LogDynAndXConvertSelection(xfc->log, xfc->display, clipboard->clipboard_atom, format->atom,
2255 clipboard->property_atom, xfc->drawable, CurrentTime);
2256 LogDynAndXFlush(xfc->log, xfc->display);
2258 return CHANNEL_RC_OK;
2267xf_cliprdr_server_format_data_response(CliprdrClientContext* context,
2270 BOOL bSuccess = FALSE;
2271 BOOL bRawCached = FALSE;
2273 WINPR_ASSERT(context);
2274 WINPR_ASSERT(formatDataResponse);
2276 xfClipboard* clipboard = cliprdr_file_context_get_context(context->custom);
2277 WINPR_ASSERT(clipboard);
2279 xfContext* xfc = clipboard->xfc;
2282 const UINT32 size = formatDataResponse->common.dataLen;
2283 const BYTE* data = formatDataResponse->requestedFormatData;
2287 ArrayList_Lock(clipboard->pending_responses);
2288 ArrayList_Lock(clipboard->queued_responses);
2289 if (formatDataResponse->common.msgFlags == CB_RESPONSE_FAIL)
2291 WLog_WARN(TAG,
"Format Data Response PDU msgFlags is CB_RESPONSE_FAIL");
2292 while (ArrayList_Count(clipboard->pending_responses) > 0)
2294 SelectionResponse* pending = ArrayList_GetItem(clipboard->pending_responses, 0);
2296 pending->expectedResponse->property = None;
2297 xf_cliprdr_provide_selection(clipboard, pending->expectedResponse);
2299 ArrayList_Remove(clipboard->pending_responses, pending);
2301 WINPR_ASSERT(ArrayList_Count(clipboard->pending_responses) == 0);
2304 while (ArrayList_Count(clipboard->pending_responses) > 0)
2306 BYTE* pDstData =
nullptr;
2309 UINT32 srcFormatId = 0;
2310 UINT32 dstFormatId = 0;
2311 BOOL nullTerminated = FALSE;
2312 xfCachedData* cached_data =
nullptr;
2313 xfCachedData* hit_cached_data =
nullptr;
2315 SelectionResponse* pending = ArrayList_GetItem(clipboard->pending_responses, 0);
2316 const RequestedFormat* format = pending->requestedFormat;
2317 if (pending->data_raw_format)
2319 srcFormatId = CF_RAW;
2320 dstFormatId = CF_RAW;
2324 pending->expectedResponse->property = None;
2327 else if (format->formatName)
2329 dstFormatId = format->localFormat;
2331 ClipboardLock(clipboard->system);
2332 if (strcmp(format->formatName, type_HtmlFormat) == 0)
2334 srcFormatId = ClipboardGetFormatId(clipboard->system, type_HtmlFormat);
2335 dstFormatId = ClipboardGetFormatId(clipboard->system, mime_html);
2336 nullTerminated = TRUE;
2339 if (strcmp(format->formatName, type_FileGroupDescriptorW) == 0)
2341 if (!cliprdr_file_context_update_server_data(clipboard->file, clipboard->system,
2343 WLog_WARN(TAG,
"failed to update file descriptors");
2345 srcFormatId = ClipboardGetFormatId(clipboard->system, type_FileGroupDescriptorW);
2346 const xfCliprdrFormat* dstTargetFormat = xf_cliprdr_get_client_format_by_atom(
2347 clipboard, pending->expectedResponse->target);
2348 if (!dstTargetFormat)
2350 dstFormatId = ClipboardGetFormatId(clipboard->system, mime_uri_list);
2354 dstFormatId = dstTargetFormat->localFormat;
2357 nullTerminated = TRUE;
2359 ClipboardUnlock(clipboard->system);
2363 srcFormatId = format->formatToRequest;
2364 dstFormatId = format->localFormat;
2365 switch (format->formatToRequest)
2368 nullTerminated = TRUE;
2372 nullTerminated = TRUE;
2375 case CF_UNICODETEXT:
2376 nullTerminated = TRUE;
2380 srcFormatId = CF_DIB;
2384 srcFormatId = CF_TIFF;
2392 DEBUG_CLIPRDR(
"requested format 0x%08" PRIx32
" [%s] {local 0x%08" PRIx32
" [%s]} [%s]",
2393 format->formatToRequest, ClipboardGetFormatIdString(format->formatToRequest),
2394 format->localFormat,
2395 ClipboardGetFormatName(clipboard->system, format->localFormat),
2396 format->formatName);
2399 DEBUG_CLIPRDR(
"srcFormatId: 0x%08" PRIx32
", dstFormatId: 0x%08" PRIx32
"", srcFormatId,
2402 if (SrcSize != 0 && !bRawCached)
2410 xfCachedData* cached_raw_data = xf_cached_data_new_copy(data, size);
2411 if (!cached_raw_data)
2412 WLog_WARN(TAG,
"Failed to allocate cache entry");
2416 HashTable_Insert(clipboard->cachedRawData,
2417 (
void*)(UINT_PTR)srcFormatId, cached_raw_data)))
2419 WLog_WARN(TAG,
"Failed to cache clipboard data");
2420 xf_cached_data_free(cached_raw_data);
2429 WLog_DBG(TAG,
"skipping, empty data detected!");
2435 ClipboardLock(clipboard->system);
2436 bSuccess = ClipboardSetData(clipboard->system, srcFormatId, data, SrcSize);
2437 ClipboardUnlock(clipboard->system);
2442 WLog_DBG(TAG,
"skipping, ClipboardSetData failed!");
2446 wHashTable* table = clipboard->cachedData;
2447 if (pending->data_raw_format)
2448 table = clipboard->cachedRawData;
2450 HashTable_Lock(table);
2452 if (!pending->data_raw_format)
2453 hit_cached_data = HashTable_GetItemValue(table, format_to_cache_slot(dstFormatId));
2455 hit_cached_data = HashTable_GetItemValue(table, format_to_cache_slot(srcFormatId));
2457 HashTable_Unlock(table);
2459 DEBUG_CLIPRDR(
"hasCachedData: %u, pending->data_raw_format: %d", hit_cached_data ? 1u : 0u,
2460 pending->data_raw_format);
2462 ClipboardLock(clipboard->system);
2463 if (hit_cached_data)
2465 pDstData = hit_cached_data->data;
2466 DstSize = hit_cached_data->data_length;
2470 pDstData = (BYTE*)ClipboardGetData(clipboard->system, dstFormatId, &DstSize);
2475 WLog_WARN(TAG,
"failed to get clipboard data in format %s [source format %s]",
2476 ClipboardGetFormatName(clipboard->system, dstFormatId),
2477 ClipboardGetFormatName(clipboard->system, srcFormatId));
2479 ClipboardUnlock(clipboard->system);
2483 pending->expectedResponse->property = None;
2487 if (nullTerminated && pDstData)
2489 BYTE* nullTerminator = memchr(pDstData,
'\0', DstSize);
2492 const intptr_t diff = nullTerminator - pDstData;
2493 WINPR_ASSERT(diff >= 0);
2494 WINPR_ASSERT(diff <= UINT32_MAX);
2495 DstSize = (UINT32)diff;
2501 xf_cliprdr_provide_data(clipboard, pending->expectedResponse, pDstData, DstSize);
2503 if (!hit_cached_data && pDstData)
2505 cached_data = xf_cached_data_new(pDstData, DstSize);
2510 WLog_WARN(TAG,
"Failed to allocate cache entry");
2514 HashTable_Lock(clipboard->cachedData);
2515 if (!HashTable_Insert(clipboard->cachedData, format_to_cache_slot(dstFormatId),
2518 WLog_WARN(TAG,
"Failed to cache clipboard data");
2519 xf_cached_data_free(cached_data);
2522 HashTable_Unlock(clipboard->cachedData);
2527 xf_cliprdr_provide_selection(clipboard, pending->expectedResponse);
2529 ArrayList_Remove(clipboard->pending_responses, pending);
2533 WINPR_ASSERT(ArrayList_Count(clipboard->pending_responses) == 0);
2535 SelectionResponse* next = ArrayList_GetItem(clipboard->queued_responses, 0);
2538 UINT32 nextFormatId = next->requestedFormat->formatToRequest;
2539 const xfCliprdrFormat* cformat =
2540 xf_cliprdr_get_client_format_by_atom(clipboard, next->expectedResponse->target);
2543 while ((next = ArrayList_GetItem(clipboard->queued_responses, index)) !=
nullptr)
2545 if (next->requestedFormat->formatToRequest == nextFormatId)
2548 if (!ArrayList_SetItem(clipboard->queued_responses, index,
nullptr))
2550 ArrayList_RemoveAt(clipboard->queued_responses, index);
2551 if (!ArrayList_Append(clipboard->pending_responses, next))
2553 selection_response_free(next);
2561 xf_cliprdr_send_data_request(clipboard, nextFormatId, cformat);
2564 ArrayList_Unlock(clipboard->queued_responses);
2565 ArrayList_Unlock(clipboard->pending_responses);
2568 return CHANNEL_RC_OK;
2571static BOOL xf_cliprdr_is_valid_unix_filename(LPCWSTR filename)
2576 if (filename[0] == L
'\0')
2580 for (
const WCHAR* c = filename; *c; ++c)
2589xfClipboard* xf_clipboard_new(xfContext* xfc, BOOL relieveFilenameRestriction)
2592 rdpChannels* channels =
nullptr;
2593 xfClipboard* clipboard =
nullptr;
2594 const char* selectionAtom =
nullptr;
2595 xfCliprdrFormat* clientFormat =
nullptr;
2599 WINPR_ASSERT(xfc->common.context.settings);
2601 if (!(clipboard = (xfClipboard*)calloc(1,
sizeof(xfClipboard))))
2603 WLog_ERR(TAG,
"failed to allocate xfClipboard data");
2607 clipboard->file = cliprdr_file_context_new(clipboard);
2608 if (!clipboard->file)
2611 xfc->clipboard = clipboard;
2612 clipboard->xfc = xfc;
2613 channels = xfc->common.context.channels;
2614 clipboard->channels = channels;
2615 clipboard->system = ClipboardCreate();
2616 clipboard->requestedFormatId = UINT32_MAX;
2617 clipboard->root_window = DefaultRootWindow(xfc->display);
2622 selectionAtom =
"CLIPBOARD";
2624 clipboard->clipboard_atom = Logging_XInternAtom(xfc->log, xfc->display, selectionAtom, FALSE);
2626 if (clipboard->clipboard_atom == None)
2628 WLog_ERR(TAG,
"unable to get %s atom", selectionAtom);
2632 clipboard->timestamp_property_atom =
2633 Logging_XInternAtom(xfc->log, xfc->display,
"_FREERDP_TIMESTAMP_PROPERTY", FALSE);
2634 clipboard->property_atom =
2635 Logging_XInternAtom(xfc->log, xfc->display,
"_FREERDP_CLIPRDR", FALSE);
2636 clipboard->raw_transfer_atom =
2637 Logging_XInternAtom(xfc->log, xfc->display,
"_FREERDP_CLIPRDR_RAW", FALSE);
2638 clipboard->raw_format_list_atom =
2639 Logging_XInternAtom(xfc->log, xfc->display,
"_FREERDP_CLIPRDR_FORMATS", FALSE);
2640 xf_cliprdr_set_raw_transfer_enabled(clipboard, TRUE);
2641 XSelectInput(xfc->display, clipboard->root_window, PropertyChangeMask);
2644 if (XFixesQueryExtension(xfc->display, &clipboard->xfixes_event_base,
2645 &clipboard->xfixes_error_base))
2650 if (XFixesQueryVersion(xfc->display, &xfmajor, &xfminor))
2652 XFixesSelectSelectionInput(xfc->display, clipboard->root_window,
2653 clipboard->clipboard_atom,
2654 XFixesSetSelectionOwnerNotifyMask);
2655 clipboard->xfixes_supported = TRUE;
2659 WLog_ERR(TAG,
"Error querying X Fixes extension version");
2664 WLog_ERR(TAG,
"Error loading X Fixes extension");
2670 "Warning: Using clipboard redirection without XFIXES extension is strongly discouraged!");
2672 clientFormat = &clipboard->clientFormats[n++];
2673 clientFormat->atom = Logging_XInternAtom(xfc->log, xfc->display,
"_FREERDP_RAW", False);
2674 clientFormat->localFormat = clientFormat->formatToRequest = CF_RAW;
2676 clientFormat = &clipboard->clientFormats[n++];
2677 clientFormat->atom = Logging_XInternAtom(xfc->log, xfc->display,
"UTF8_STRING", False);
2678 clientFormat->formatToRequest = CF_UNICODETEXT;
2679 clientFormat->localFormat = ClipboardGetFormatId(xfc->clipboard->system, mime_text_plain);
2681 clientFormat = &clipboard->clientFormats[n++];
2682 clientFormat->atom = XA_STRING;
2683 clientFormat->formatToRequest = CF_TEXT;
2684 clientFormat->localFormat = ClipboardGetFormatId(xfc->clipboard->system, mime_text_plain);
2686 clientFormat = &clipboard->clientFormats[n++];
2687 clientFormat->atom = Logging_XInternAtom(xfc->log, xfc->display, mime_tiff, False);
2688 clientFormat->formatToRequest = clientFormat->localFormat = CF_TIFF;
2690 for (
size_t x = 0; x < ARRAYSIZE(mime_bitmap); x++)
2692 const char* mime_bmp = mime_bitmap[x];
2693 const DWORD format = ClipboardGetFormatId(xfc->clipboard->system, mime_bmp);
2696 WLog_DBG(TAG,
"skipping local bitmap format %s [NOT SUPPORTED]", mime_bmp);
2700 WLog_DBG(TAG,
"register local bitmap format %s [0x%08" PRIx32
"]", mime_bmp, format);
2701 clientFormat = &clipboard->clientFormats[n++];
2702 clientFormat->localFormat = format;
2703 clientFormat->atom = Logging_XInternAtom(xfc->log, xfc->display, mime_bmp, False);
2704 clientFormat->formatToRequest = CF_DIB;
2705 clientFormat->isImage = TRUE;
2708 for (
size_t x = 0; x < ARRAYSIZE(mime_images); x++)
2710 const char* mime_bmp = mime_images[x];
2711 const DWORD format = ClipboardGetFormatId(xfc->clipboard->system, mime_bmp);
2714 WLog_DBG(TAG,
"skipping local bitmap format %s [NOT SUPPORTED]", mime_bmp);
2718 WLog_DBG(TAG,
"register local bitmap format %s [0x%08" PRIx32
"]", mime_bmp, format);
2719 clientFormat = &clipboard->clientFormats[n++];
2720 clientFormat->localFormat = format;
2721 clientFormat->atom = Logging_XInternAtom(xfc->log, xfc->display, mime_bmp, False);
2722 clientFormat->formatToRequest = CF_DIB;
2723 clientFormat->isImage = TRUE;
2726 clientFormat = &clipboard->clientFormats[n++];
2727 clientFormat->atom = Logging_XInternAtom(xfc->log, xfc->display, mime_html, False);
2728 clientFormat->formatToRequest = ClipboardGetFormatId(xfc->clipboard->system, type_HtmlFormat);
2729 clientFormat->localFormat = ClipboardGetFormatId(xfc->clipboard->system, mime_html);
2730 clientFormat->formatName = _strdup(type_HtmlFormat);
2732 if (!clientFormat->formatName)
2735 clientFormat = &clipboard->clientFormats[n++];
2744 const UINT32 fgid = ClipboardGetFormatId(clipboard->system, type_FileGroupDescriptorW);
2746 const UINT32 uid = ClipboardGetFormatId(clipboard->system, mime_uri_list);
2749 if (!cliprdr_file_context_set_locally_available(clipboard->file, TRUE))
2751 clientFormat->atom =
2752 Logging_XInternAtom(xfc->log, xfc->display, mime_uri_list, False);
2753 clientFormat->localFormat = uid;
2754 clientFormat->formatToRequest = fgid;
2755 clientFormat->formatName = _strdup(type_FileGroupDescriptorW);
2757 if (!clientFormat->formatName)
2760 clientFormat = &clipboard->clientFormats[n++];
2765 const UINT32 gid = ClipboardGetFormatId(clipboard->system, mime_gnome_copied_files);
2768 if (!cliprdr_file_context_set_locally_available(clipboard->file, TRUE))
2770 clientFormat->atom =
2771 Logging_XInternAtom(xfc->log, xfc->display, mime_gnome_copied_files, False);
2772 clientFormat->localFormat = gid;
2773 clientFormat->formatToRequest = fgid;
2774 clientFormat->formatName = _strdup(type_FileGroupDescriptorW);
2776 if (!clientFormat->formatName)
2779 clientFormat = &clipboard->clientFormats[n++];
2784 const UINT32 mid = ClipboardGetFormatId(clipboard->system, mime_mate_copied_files);
2787 if (!cliprdr_file_context_set_locally_available(clipboard->file, TRUE))
2789 clientFormat->atom =
2790 Logging_XInternAtom(xfc->log, xfc->display, mime_mate_copied_files, False);
2791 clientFormat->localFormat = mid;
2792 clientFormat->formatToRequest = fgid;
2793 clientFormat->formatName = _strdup(type_FileGroupDescriptorW);
2795 if (!clientFormat->formatName)
2801 clipboard->numClientFormats = WINPR_ASSERTING_INT_CAST(uint32_t, n);
2802 clipboard->targets[0] = Logging_XInternAtom(xfc->log, xfc->display,
"TIMESTAMP", FALSE);
2803 clipboard->targets[1] = Logging_XInternAtom(xfc->log, xfc->display,
"TARGETS", FALSE);
2804 clipboard->numTargets = 2;
2805 clipboard->incr_atom = Logging_XInternAtom(xfc->log, xfc->display,
"INCR", FALSE);
2807 if (relieveFilenameRestriction)
2809 WLog_DBG(TAG,
"Relieving CLIPRDR filename restriction");
2810 ClipboardGetDelegate(clipboard->system)->IsFileNameComponentValid =
2811 xf_cliprdr_is_valid_unix_filename;
2814 clipboard->cachedData = HashTable_New(TRUE);
2815 if (!clipboard->cachedData)
2818 obj = HashTable_ValueObject(clipboard->cachedData);
2821 clipboard->cachedRawData = HashTable_New(TRUE);
2822 if (!clipboard->cachedRawData)
2825 obj = HashTable_ValueObject(clipboard->cachedRawData);
2828 clipboard->pending_responses = ArrayList_New(TRUE);
2829 if (!clipboard->pending_responses)
2831 obj = ArrayList_Object(clipboard->pending_responses);
2834 clipboard->queued_responses = ArrayList_New(TRUE);
2835 if (!clipboard->queued_responses)
2837 obj = ArrayList_Object(clipboard->queued_responses);
2843 WINPR_PRAGMA_DIAG_PUSH
2844 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
2845 xf_clipboard_free(clipboard);
2846 WINPR_PRAGMA_DIAG_POP
2850void xf_clipboard_free(xfClipboard* clipboard)
2855 xf_clipboard_free_server_formats(clipboard);
2857 if (clipboard->numClientFormats)
2859 for (UINT32 i = 0; i < clipboard->numClientFormats; i++)
2861 xfCliprdrFormat* format = &clipboard->clientFormats[i];
2862 free(format->formatName);
2866 cliprdr_file_context_free(clipboard->file);
2868 ClipboardDestroy(clipboard->system);
2869 xf_clipboard_formats_free(clipboard);
2870 HashTable_Free(clipboard->cachedRawData);
2871 HashTable_Free(clipboard->cachedData);
2872 ArrayList_Free(clipboard->pending_responses);
2873 ArrayList_Free(clipboard->queued_responses);
2874 free(clipboard->incr_data);
2878void xf_cliprdr_init(xfContext* xfc, CliprdrClientContext* cliprdr)
2881 WINPR_ASSERT(cliprdr);
2883 xfc->cliprdr = cliprdr;
2884 xfc->clipboard->context = cliprdr;
2886 cliprdr->MonitorReady = xf_cliprdr_monitor_ready;
2887 cliprdr->ServerCapabilities = xf_cliprdr_server_capabilities;
2888 cliprdr->ServerFormatList = xf_cliprdr_server_format_list;
2889 cliprdr->ServerFormatListResponse = xf_cliprdr_server_format_list_response;
2890 cliprdr->ServerFormatDataRequest = xf_cliprdr_server_format_data_request;
2891 cliprdr->ServerFormatDataResponse = xf_cliprdr_server_format_data_response;
2893 cliprdr_file_context_init(xfc->clipboard->file, cliprdr);
2896void xf_cliprdr_uninit(xfContext* xfc, CliprdrClientContext* cliprdr)
2900 xfc->cliprdr =
nullptr;
2904 ClipboardLock(xfc->clipboard->system);
2905 cliprdr_file_context_uninit(xfc->clipboard->file, cliprdr);
2906 ClipboardUnlock(xfc->clipboard->system);
2907 xfc->clipboard->context =
nullptr;
WINPR_ATTR_NODISCARD 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.
OBJECT_FREE_FN fnObjectFree