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;
138 CliprdrFileContext* file;
142static const char mime_text_plain[] =
"text/plain";
143static const char mime_uri_list[] =
"text/uri-list";
144static const char mime_html[] =
"text/html";
145static const char* mime_bitmap[] = {
"image/bmp",
"image/x-bmp",
"image/x-MS-bmp",
146 "image/x-win-bitmap" };
147static const char mime_webp[] =
"image/webp";
148static const char mime_png[] =
"image/png";
149static const char mime_jpeg[] =
"image/jpeg";
150static const char mime_tiff[] =
"image/tiff";
151static const char* mime_images[] = { mime_webp, mime_png, mime_jpeg, mime_tiff };
153static const char mime_gnome_copied_files[] =
"x-special/gnome-copied-files";
154static const char mime_mate_copied_files[] =
"x-special/mate-copied-files";
156static const char type_FileGroupDescriptorW[] =
"FileGroupDescriptorW";
157static const char type_HtmlFormat[] =
"HTML Format";
159static void xf_cliprdr_clear_cached_data(xfClipboard* clipboard);
160static UINT xf_cliprdr_send_client_format_list(xfClipboard* clipboard);
161static void xf_cliprdr_set_selection_owner(xfContext* xfc, xfClipboard* clipboard, Time timestamp);
163static void requested_format_free(RequestedFormat** ppRequestedFormat)
165 if (!ppRequestedFormat)
167 if (!(*ppRequestedFormat))
170 free((*ppRequestedFormat)->formatName);
171 free(*ppRequestedFormat);
172 *ppRequestedFormat =
nullptr;
175static BOOL requested_format_replace(RequestedFormat** ppRequestedFormat, UINT32 remoteFormatId,
176 UINT32 localFormatId,
const char* formatName)
178 if (!ppRequestedFormat)
181 requested_format_free(ppRequestedFormat);
182 RequestedFormat* requested = calloc(1,
sizeof(RequestedFormat));
185 requested->localFormat = localFormatId;
186 requested->formatToRequest = remoteFormatId;
189 requested->formatName = _strdup(formatName);
190 if (!requested->formatName)
197 *ppRequestedFormat = requested;
201static void selection_response_free(
void* ptr)
203 SelectionResponse* selection_response = (SelectionResponse*)ptr;
204 if (!selection_response)
207 free(selection_response->expectedResponse);
208 requested_format_free(&selection_response->requestedFormat);
209 free(selection_response);
212static void xf_cached_data_free(
void* ptr)
214 xfCachedData* cached_data = ptr;
218 free(cached_data->data);
222static xfCachedData* xf_cached_data_new(BYTE* data,
size_t data_length)
224 if (data_length > UINT32_MAX)
227 xfCachedData* cached_data = calloc(1,
sizeof(xfCachedData));
231 cached_data->data = data;
232 cached_data->data_length = (UINT32)data_length;
237static xfCachedData* xf_cached_data_new_copy(
const BYTE* data,
size_t data_length)
239 BYTE* copy =
nullptr;
242 copy = calloc(data_length + 1,
sizeof(BYTE));
245 memcpy(copy, data, data_length);
248 xfCachedData* cache = xf_cached_data_new(copy, data_length);
254static void xf_clipboard_free_server_formats(xfClipboard* clipboard)
256 WINPR_ASSERT(clipboard);
257 if (clipboard->serverFormats)
259 for (
size_t i = 0; i < clipboard->numServerFormats; i++)
262 free(format->formatName);
265 free(clipboard->serverFormats);
266 clipboard->serverFormats =
nullptr;
270static BOOL xf_cliprdr_update_owner(xfClipboard* clipboard)
272 WINPR_ASSERT(clipboard);
274 xfContext* xfc = clipboard->xfc;
277 if (!clipboard->sync)
280 Window owner = LogDynAndXGetSelectionOwner(xfc->log, xfc->display, clipboard->clipboard_atom);
281 if (clipboard->owner == owner)
284 clipboard->owner = owner;
288static void xf_cliprdr_check_owner(xfClipboard* clipboard)
290 if (xf_cliprdr_update_owner(clipboard))
291 xf_cliprdr_send_client_format_list(clipboard);
294static BOOL xf_cliprdr_is_self_owned(xfClipboard* clipboard)
296 xfContext* xfc =
nullptr;
298 WINPR_ASSERT(clipboard);
300 xfc = clipboard->xfc;
302 return LogDynAndXGetSelectionOwner(xfc->log, xfc->display, clipboard->clipboard_atom) ==
306static void xf_cliprdr_set_raw_transfer_enabled(xfClipboard* clipboard, BOOL enabled)
308 UINT32 data = WINPR_ASSERTING_INT_CAST(uint32_t, enabled);
309 xfContext* xfc =
nullptr;
311 WINPR_ASSERT(clipboard);
313 xfc = clipboard->xfc;
315 LogDynAndXChangeProperty(xfc->log, xfc->display, xfc->drawable, clipboard->raw_transfer_atom,
316 XA_INTEGER, 32, PropModeReplace, (
const BYTE*)&data, 1);
319static BOOL xf_cliprdr_is_raw_transfer_available(xfClipboard* clipboard)
324 unsigned long length = 0;
325 unsigned long bytes_left = 0;
326 UINT32* data =
nullptr;
327 UINT32 is_enabled = 0;
329 xfContext* xfc =
nullptr;
331 WINPR_ASSERT(clipboard);
333 xfc = clipboard->xfc;
336 owner = LogDynAndXGetSelectionOwner(xfc->log, xfc->display, clipboard->clipboard_atom);
340 result = LogDynAndXGetWindowProperty(xfc->log, xfc->display, owner,
341 clipboard->raw_transfer_atom, 0, 4, 0, XA_INTEGER,
342 &type, &format, &length, &bytes_left, (BYTE**)&data);
351 if ((owner == None) || (owner == xfc->drawable))
354 if (result != Success)
357 return is_enabled != 0;
360static BOOL xf_cliprdr_formats_equal(
const CLIPRDR_FORMAT* server,
const xfCliprdrFormat* client)
362 WINPR_ASSERT(server);
363 WINPR_ASSERT(client);
365 if (server->formatName && client->formatName)
368 return (0 == strncmp(server->formatName, client->formatName, strlen(server->formatName)));
371 if (!server->formatName && !client->formatName)
373 return (server->formatId == client->formatToRequest);
379static const xfCliprdrFormat* xf_cliprdr_get_client_format_by_id(xfClipboard* clipboard,
382 WINPR_ASSERT(clipboard);
384 const BOOL formatIsHtml = formatId == ClipboardGetFormatId(clipboard->system, type_HtmlFormat);
385 const BOOL fetchImage = clipboard->isImageContent && formatIsHtml;
386 for (
size_t index = 0; index < clipboard->numClientFormats; index++)
388 const xfCliprdrFormat* format = &(clipboard->clientFormats[index]);
390 if (fetchImage && format->isImage)
393 if (format->formatToRequest == formatId)
400static const xfCliprdrFormat* xf_cliprdr_get_client_format_by_atom(xfClipboard* clipboard,
403 WINPR_ASSERT(clipboard);
405 for (UINT32 i = 0; i < clipboard->numClientFormats; i++)
407 const xfCliprdrFormat* format = &(clipboard->clientFormats[i]);
409 if (format->atom == atom)
416static const CLIPRDR_FORMAT* xf_cliprdr_get_server_format_by_atom(xfClipboard* clipboard, Atom atom)
418 WINPR_ASSERT(clipboard);
420 for (
size_t i = 0; i < clipboard->numClientFormats; i++)
422 const xfCliprdrFormat* client_format = &(clipboard->clientFormats[i]);
424 if (client_format->atom == atom)
426 for (
size_t j = 0; j < clipboard->numServerFormats; j++)
428 const CLIPRDR_FORMAT* server_format = &(clipboard->serverFormats[j]);
430 if (xf_cliprdr_formats_equal(server_format, client_format))
431 return server_format;
444static UINT xf_cliprdr_send_data_request(xfClipboard* clipboard, UINT32 formatId,
445 WINPR_ATTR_UNUSED
const xfCliprdrFormat* cformat)
448 request.requestedFormatId = formatId;
450 DEBUG_CLIPRDR(
"requesting format 0x%08" PRIx32
" [%s] {local 0x%08" PRIx32
"} [%s]", formatId,
451 ClipboardGetFormatIdString(formatId), cformat->localFormat, cformat->formatName);
453 WINPR_ASSERT(clipboard);
454 WINPR_ASSERT(clipboard->context);
455 WINPR_ASSERT(clipboard->context->ClientFormatDataRequest);
456 return clipboard->context->ClientFormatDataRequest(clipboard->context, &request);
464static UINT xf_cliprdr_send_data_response(xfClipboard* clipboard,
const xfCliprdrFormat* format,
465 const BYTE* data,
size_t size)
469 WINPR_ASSERT(clipboard);
472 if (clipboard->requestedFormatId == UINT32_MAX)
473 return CHANNEL_RC_OK;
478 DEBUG_CLIPRDR(
"send CB_RESPONSE_FAIL response {format 0x%08" PRIx32
479 " [%s] {local 0x%08" PRIx32
"} [%s]",
480 format->formatToRequest,
481 ClipboardGetFormatIdString(format->formatToRequest), format->localFormat,
484 DEBUG_CLIPRDR(
"send CB_RESPONSE_FAIL response");
488 WINPR_ASSERT(format);
489 DEBUG_CLIPRDR(
"send response format 0x%08" PRIx32
" [%s] {local 0x%08" PRIx32
" [%s]} [%s]",
490 format->formatToRequest, ClipboardGetFormatIdString(format->formatToRequest),
492 ClipboardGetFormatName(clipboard->system, format->localFormat),
496 clipboard->requestedFormatId = UINT32_MAX;
498 response.common.msgFlags = (data) ? CB_RESPONSE_OK : CB_RESPONSE_FAIL;
500 WINPR_ASSERT(size <= UINT32_MAX);
501 response.common.dataLen = (UINT32)size;
502 response.requestedFormatData = data;
504 WINPR_ASSERT(clipboard->context);
505 WINPR_ASSERT(clipboard->context->ClientFormatDataResponse);
506 return clipboard->context->ClientFormatDataResponse(clipboard->context, &response);
509static wStream* xf_cliprdr_serialize_server_format_list(xfClipboard* clipboard)
511 UINT32 formatCount = 0;
514 WINPR_ASSERT(clipboard);
517 if (!(s = Stream_New(
nullptr, 128)))
519 WLog_ERR(TAG,
"failed to allocate serialized format list");
524 formatCount = (clipboard->numServerFormats > 0) ? clipboard->numServerFormats - 1 : 0;
525 Stream_Write_UINT32(s, formatCount);
527 for (UINT32 i = 0; i < formatCount; i++)
530 size_t name_length = format->formatName ? strlen(format->formatName) : 0;
532 DEBUG_CLIPRDR(
"server announced 0x%08" PRIx32
" [%s][%s]", format->formatId,
533 ClipboardGetFormatIdString(format->formatId), format->formatName);
534 if (!Stream_EnsureRemainingCapacity(s,
sizeof(UINT32) + name_length + 1))
536 WLog_ERR(TAG,
"failed to expand serialized format list");
540 Stream_Write_UINT32(s, format->formatId);
542 if (format->formatName)
543 Stream_Write(s, format->formatName, name_length);
545 Stream_Write_UINT8(s,
'\0');
548 Stream_SealLength(s);
551 Stream_Free(s, TRUE);
555static CLIPRDR_FORMAT* xf_cliprdr_parse_server_format_list(BYTE* data,
size_t length,
561 WINPR_ASSERT(data || (length == 0));
562 WINPR_ASSERT(numFormats);
564 if (!(s = Stream_New(data, length)))
566 WLog_ERR(TAG,
"failed to allocate stream for parsing serialized format list");
570 if (!Stream_CheckAndLogRequiredLength(TAG, s,
sizeof(UINT32)))
573 Stream_Read_UINT32(s, *numFormats);
575 if (*numFormats > MAX_CLIPBOARD_FORMATS)
577 WLog_ERR(TAG,
"unexpectedly large number of formats: %" PRIu32
"", *numFormats);
583 WLog_ERR(TAG,
"failed to allocate format list");
587 for (UINT32 i = 0; i < *numFormats; i++)
589 const char* formatName =
nullptr;
590 size_t formatNameLength = 0;
592 if (!Stream_CheckAndLogRequiredLength(TAG, s,
sizeof(UINT32)))
595 Stream_Read_UINT32(s, formats[i].formatId);
596 formatName = (
const char*)Stream_Pointer(s);
597 formatNameLength = strnlen(formatName, Stream_GetRemainingLength(s));
599 if (formatNameLength == Stream_GetRemainingLength(s))
601 WLog_ERR(TAG,
"missing terminating null byte, %" PRIuz
" bytes left to read",
606 formats[i].formatName = strndup(formatName, formatNameLength);
607 Stream_Seek(s, formatNameLength + 1);
610 Stream_Free(s, FALSE);
613 Stream_Free(s, FALSE);
619static void xf_cliprdr_free_formats(
CLIPRDR_FORMAT* formats, UINT32 numFormats)
621 WINPR_ASSERT(formats || (numFormats == 0));
623 for (UINT32 i = 0; i < numFormats; i++)
625 free(formats[i].formatName);
631static CLIPRDR_FORMAT* xf_cliprdr_get_raw_server_formats(xfClipboard* clipboard, UINT32* numFormats)
635 unsigned long length = 0;
636 unsigned long remaining = 0;
637 BYTE* data =
nullptr;
639 xfContext* xfc =
nullptr;
641 WINPR_ASSERT(clipboard);
642 WINPR_ASSERT(numFormats);
644 xfc = clipboard->xfc;
649 Window owner = LogDynAndXGetSelectionOwner(xfc->log, xfc->display, clipboard->clipboard_atom);
650 LogDynAndXGetWindowProperty(xfc->log, xfc->display, owner, clipboard->raw_format_list_atom, 0,
651 4096, False, clipboard->raw_format_list_atom, &type, &format,
652 &length, &remaining, &data);
654 if (data && length > 0 && format == 8 && type == clipboard->raw_format_list_atom)
656 formats = xf_cliprdr_parse_server_format_list(data, length, numFormats);
661 "failed to retrieve raw format list: data=%p, length=%lu, format=%d, type=%lu "
663 (
void*)data, length, format, (
unsigned long)type,
664 (
unsigned long)clipboard->raw_format_list_atom);
673static BOOL xf_cliprdr_should_add_format(
const CLIPRDR_FORMAT* formats,
size_t count,
674 const xfCliprdrFormat* xformat)
676 WINPR_ASSERT(formats);
681 for (
size_t x = 0; x < count; x++)
684 if (format->formatId == xformat->formatToRequest)
690static CLIPRDR_FORMAT* xf_cliprdr_get_formats_from_targets(xfClipboard* clipboard,
694 BYTE* data =
nullptr;
695 int format_property = 0;
696 unsigned long proplength = 0;
697 unsigned long bytes_left = 0;
700 WINPR_ASSERT(clipboard);
701 WINPR_ASSERT(numFormats);
703 xfContext* xfc = clipboard->xfc;
707 LogDynAndXGetWindowProperty(xfc->log, xfc->display, xfc->drawable, clipboard->property_atom, 0,
708 200, 0, XA_ATOM, &atom, &format_property, &proplength, &bytes_left,
713 unsigned long length = proplength + 1;
716 WLog_ERR(TAG,
"XGetWindowProperty set length = %lu but data is nullptr", length);
722 WLog_ERR(TAG,
"failed to allocate %lu CLIPRDR_FORMAT structs", length);
728 BOOL isImage = FALSE;
729 BOOL hasHtml = FALSE;
730 const uint32_t htmlFormatId = ClipboardRegisterFormat(clipboard->system, type_HtmlFormat);
731 for (
unsigned long i = 0; i < proplength; i++)
733 Atom tatom = ((Atom*)data)[i];
734 const xfCliprdrFormat* format = xf_cliprdr_get_client_format_by_atom(clipboard, tatom);
736 if (xf_cliprdr_should_add_format(formats, *numFormats, format))
739 cformat->formatId = format->formatToRequest;
744 if (cformat->formatId == htmlFormatId)
749 if (cformat->formatId == CF_TIFF)
751 else if (cformat->formatId == CF_DIB)
753 else if (cformat->formatId == CF_DIBV5)
756 if (format->formatName)
758 cformat->formatName = _strdup(format->formatName);
759 WINPR_ASSERT(cformat->formatName);
762 cformat->formatName =
nullptr;
768 clipboard->isImageContent = isImage;
769 if (isImage && !hasHtml)
772 cformat->formatId = htmlFormatId;
773 cformat->formatName = _strdup(type_HtmlFormat);
786static CLIPRDR_FORMAT* xf_cliprdr_get_client_formats(xfClipboard* clipboard, UINT32* numFormats)
790 WINPR_ASSERT(clipboard);
791 WINPR_ASSERT(numFormats);
795 if (xf_cliprdr_is_raw_transfer_available(clipboard))
797 formats = xf_cliprdr_get_raw_server_formats(clipboard, numFormats);
800 if (*numFormats == 0)
802 xf_cliprdr_free_formats(formats, *numFormats);
803 formats = xf_cliprdr_get_formats_from_targets(clipboard, numFormats);
809static void xf_cliprdr_provide_server_format_list(xfClipboard* clipboard)
812 xfContext* xfc =
nullptr;
814 WINPR_ASSERT(clipboard);
816 xfc = clipboard->xfc;
819 formats = xf_cliprdr_serialize_server_format_list(clipboard);
823 const size_t len = Stream_Length(formats);
824 WINPR_ASSERT(len <= INT32_MAX);
825 LogDynAndXChangeProperty(xfc->log, xfc->display, xfc->drawable,
826 clipboard->raw_format_list_atom, clipboard->raw_format_list_atom,
827 8, PropModeReplace, Stream_Buffer(formats), (
int)len);
831 LogDynAndXDeleteProperty(xfc->log, xfc->display, xfc->drawable,
832 clipboard->raw_format_list_atom);
835 Stream_Free(formats, TRUE);
838static UINT xf_cliprdr_send_format_list(xfClipboard* clipboard,
const CLIPRDR_FORMAT* formats,
845 } cnv = { .cpv = formats };
847 .numFormats = numFormats,
849 .common.msgType = CB_FORMAT_LIST };
852 WINPR_ASSERT(clipboard);
853 WINPR_ASSERT(formats || (numFormats == 0));
855#if defined(WITH_DEBUG_CLIPRDR)
856 for (UINT32 x = 0; x < numFormats; x++)
859 DEBUG_CLIPRDR(
"announcing format 0x%08" PRIx32
" [%s] [%s]", format->formatId,
860 ClipboardGetFormatIdString(format->formatId), format->formatName);
865 xf_cliprdr_send_data_response(clipboard,
nullptr,
nullptr, 0);
867 xf_cliprdr_clear_cached_data(clipboard);
869 ret = cliprdr_file_context_notify_new_client_format_list(clipboard->file);
873 WINPR_ASSERT(clipboard->context);
874 WINPR_ASSERT(clipboard->context->ClientFormatList);
875 return clipboard->context->ClientFormatList(clipboard->context, &formatList);
878static void xf_cliprdr_get_requested_targets(xfClipboard* clipboard)
880 UINT32 numFormats = 0;
881 CLIPRDR_FORMAT* formats = xf_cliprdr_get_client_formats(clipboard, &numFormats);
882 xf_cliprdr_send_format_list(clipboard, formats, numFormats);
883 xf_cliprdr_free_formats(formats, numFormats);
886static void xf_cliprdr_process_requested_data(xfClipboard* clipboard, BOOL hasData,
887 const BYTE* data,
size_t size)
892 INT64 srcFormatId = -1;
893 BYTE* pDstData =
nullptr;
894 const xfCliprdrFormat* format =
nullptr;
896 WINPR_ASSERT(clipboard);
898 if (clipboard->incr_starts && hasData)
903 clipboard->incr_data_length = 0;
905 format = xf_cliprdr_get_client_format_by_id(clipboard, clipboard->requestedFormatId);
907 if (!hasData || !data || !format)
909 xf_cliprdr_send_data_response(clipboard, format,
nullptr, 0);
913 switch (format->formatToRequest)
916 srcFormatId = CF_RAW;
922 srcFormatId = format->localFormat;
926 srcFormatId = format->localFormat;
932 xf_cliprdr_send_data_response(clipboard, format,
nullptr, 0);
936 ClipboardLock(clipboard->system);
937 SrcSize = (UINT32)size;
938 bSuccess = ClipboardSetData(clipboard->system, (UINT32)srcFormatId, data, SrcSize);
944 (BYTE*)ClipboardGetData(clipboard->system, clipboard->requestedFormatId, &DstSize);
946 ClipboardUnlock(clipboard->system);
950 xf_cliprdr_send_data_response(clipboard, format,
nullptr, 0);
961 ClipboardLock(clipboard->system);
962 if (format->formatToRequest &&
963 (format->formatToRequest ==
964 ClipboardGetFormatId(clipboard->system, type_FileGroupDescriptorW)))
966 UINT error = NO_ERROR;
972 const UINT32 flags = cliprdr_file_context_remote_get_flags(clipboard->file);
973 error = cliprdr_serialize_file_list_ex(flags, file_array, file_count, &pDstData, &DstSize);
976 WLog_ERR(TAG,
"failed to serialize CLIPRDR_FILELIST: 0x%08X", error);
979 UINT32 formatId = ClipboardGetFormatId(clipboard->system, mime_uri_list);
982 char* url = ClipboardGetData(clipboard->system, formatId, &url_size);
983 cliprdr_file_context_update_client_data(clipboard->file, url, url_size);
989 ClipboardUnlock(clipboard->system);
991 xf_cliprdr_send_data_response(clipboard, format, pDstData, DstSize);
995static BOOL xf_restore_input_flags(xfClipboard* clipboard)
997 WINPR_ASSERT(clipboard);
999 xfContext* xfc = clipboard->xfc;
1002 if (clipboard->event_mask != 0)
1004 XSelectInput(xfc->display, xfc->drawable, clipboard->event_mask);
1005 clipboard->event_mask = 0;
1010static BOOL append(xfClipboard* clipboard,
const void* sdata,
size_t length)
1012 WINPR_ASSERT(clipboard);
1014 const size_t size = length + clipboard->incr_data_length + 2;
1015 BYTE* data = realloc(clipboard->incr_data, size);
1018 clipboard->incr_data = data;
1019 memcpy(&data[clipboard->incr_data_length], sdata, length);
1020 clipboard->incr_data_length += length;
1021 clipboard->incr_data[clipboard->incr_data_length + 0] =
'\0';
1022 clipboard->incr_data[clipboard->incr_data_length + 1] =
'\0';
1026static BOOL xf_cliprdr_stop_incr(xfClipboard* clipboard)
1028 clipboard->incr_starts = FALSE;
1029 clipboard->incr_data_length = 0;
1030 return xf_restore_input_flags(clipboard);
1033static BOOL xf_cliprdr_get_requested_data(xfClipboard* clipboard, Atom target)
1035 WINPR_ASSERT(clipboard);
1037 xfContext* xfc = clipboard->xfc;
1040 const xfCliprdrFormat* format =
1041 xf_cliprdr_get_client_format_by_id(clipboard, clipboard->requestedFormatId);
1043 if (!format || (format->atom != target))
1045 xf_cliprdr_send_data_response(clipboard, format,
nullptr, 0);
1050 BOOL has_data = FALSE;
1051 int format_property = 0;
1052 unsigned long length = 0;
1053 unsigned long total_bytes = 0;
1054 BYTE* property_data =
nullptr;
1055 const int rc = LogDynAndXGetWindowProperty(
1056 xfc->log, xfc->display, xfc->drawable, clipboard->property_atom, 0, 0, False, target, &type,
1057 &format_property, &length, &total_bytes, &property_data);
1059 XFree(property_data);
1062 xf_cliprdr_send_data_response(clipboard, format,
nullptr, 0);
1069 if ((total_bytes <= 0) && !clipboard->incr_starts)
1071 xf_cliprdr_stop_incr(clipboard);
1074 else if (type == clipboard->incr_atom)
1076 xf_cliprdr_stop_incr(clipboard);
1077 clipboard->incr_starts = TRUE;
1082 BYTE* incremental_data =
nullptr;
1083 unsigned long incremental_len = 0;
1086 len = clipboard->incr_data_length;
1087 if (total_bytes <= 0)
1089 xf_cliprdr_stop_incr(clipboard);
1093 else if (LogDynAndXGetWindowProperty(
1094 xfc->log, xfc->display, xfc->drawable, clipboard->property_atom, 0,
1095 WINPR_ASSERTING_INT_CAST(int32_t, total_bytes), False, target, &type,
1096 &format_property, &incremental_len, &length, &incremental_data) == Success)
1098 has_data = append(clipboard, incremental_data, incremental_len);
1099 len = clipboard->incr_data_length;
1102 if (incremental_data)
1103 XFree(incremental_data);
1106 LogDynAndXDeleteProperty(xfc->log, xfc->display, xfc->drawable, clipboard->property_atom);
1107 xf_cliprdr_process_requested_data(clipboard, has_data, clipboard->incr_data, len);
1112static void xf_cliprdr_append_target(xfClipboard* clipboard, Atom target)
1114 WINPR_ASSERT(clipboard);
1116 if (clipboard->numTargets >= ARRAYSIZE(clipboard->targets))
1119 for (
size_t i = 0; i < clipboard->numTargets; i++)
1121 if (clipboard->targets[i] == target)
1125 clipboard->targets[clipboard->numTargets++] = target;
1128static void xf_cliprdr_provide_targets(xfClipboard* clipboard,
const XSelectionEvent* respond)
1130 xfContext* xfc =
nullptr;
1132 WINPR_ASSERT(clipboard);
1134 xfc = clipboard->xfc;
1137 if (respond->property != None)
1139 WINPR_ASSERT(clipboard->numTargets <= INT32_MAX);
1140 LogDynAndXChangeProperty(xfc->log, xfc->display, respond->requestor, respond->property,
1141 XA_ATOM, 32, PropModeReplace, (
const BYTE*)clipboard->targets,
1142 (
int)clipboard->numTargets);
1146static void xf_cliprdr_provide_timestamp(xfClipboard* clipboard,
const XSelectionEvent* respond)
1148 xfContext* xfc =
nullptr;
1150 WINPR_ASSERT(clipboard);
1152 xfc = clipboard->xfc;
1155 if (respond->property != None)
1157 LogDynAndXChangeProperty(xfc->log, xfc->display, respond->requestor, respond->property,
1158 XA_INTEGER, 32, PropModeReplace,
1159 (
const BYTE*)&clipboard->selection_ownership_timestamp, 1);
1163#define xf_cliprdr_provide_data(clipboard, respond, data, size) \
1164 xf_cliprdr_provide_data_((clipboard), (respond), (data), (size), __FILE__, __func__, __LINE__)
1165static void xf_cliprdr_provide_data_(xfClipboard* clipboard,
const XSelectionEvent* respond,
1166 const BYTE* data, UINT32 size,
const char* file,
1167 const char* fkt,
size_t line)
1169 WINPR_ASSERT(clipboard);
1171 xfContext* xfc = clipboard->xfc;
1174 if (respond->property != None)
1176 LogDynAndXChangeProperty_ex(xfc->log, file, fkt, line, xfc->display, respond->requestor,
1177 respond->property, respond->target, 8, PropModeReplace, data,
1178 WINPR_ASSERTING_INT_CAST(int32_t, size));
1182static void log_selection_event(xfContext* xfc,
const XEvent* event)
1184 const DWORD level = WLOG_TRACE;
1185 static wLog* _log_cached_ptr =
nullptr;
1186 if (!_log_cached_ptr)
1187 _log_cached_ptr = WLog_Get(TAG);
1188 if (WLog_IsLevelActive(_log_cached_ptr, level))
1191 switch (event->type)
1193 case SelectionClear:
1195 const XSelectionClearEvent* xevent = &
event->xselectionclear;
1197 Safe_XGetAtomName(_log_cached_ptr, xfc->display, xevent->selection);
1198 WLog_Print(_log_cached_ptr, level,
"got event %s [selection %s]",
1199 x11_event_string(event->type), selection);
1203 case SelectionNotify:
1205 const XSelectionEvent* xevent = &
event->xselection;
1207 Safe_XGetAtomName(_log_cached_ptr, xfc->display, xevent->selection);
1208 char* target = Safe_XGetAtomName(_log_cached_ptr, xfc->display, xevent->target);
1209 char*
property = Safe_XGetAtomName(_log_cached_ptr, xfc->display, xevent->property);
1210 WLog_Print(_log_cached_ptr, level,
1211 "got event %s [selection %s, target %s, property %s]",
1212 x11_event_string(event->type), selection, target, property);
1218 case SelectionRequest:
1220 const XSelectionRequestEvent* xevent = &
event->xselectionrequest;
1222 Safe_XGetAtomName(_log_cached_ptr, xfc->display, xevent->selection);
1223 char* target = Safe_XGetAtomName(_log_cached_ptr, xfc->display, xevent->target);
1224 char*
property = Safe_XGetAtomName(_log_cached_ptr, xfc->display, xevent->property);
1225 WLog_Print(_log_cached_ptr, level,
1226 "got event %s [selection %s, target %s, property %s]",
1227 x11_event_string(event->type), selection, target, property);
1233 case PropertyNotify:
1235 const XPropertyEvent* xevent = &
event->xproperty;
1236 char* atom = Safe_XGetAtomName(_log_cached_ptr, xfc->display, xevent->atom);
1237 WLog_Print(_log_cached_ptr, level,
"got event %s [atom %s]",
1238 x11_event_string(event->type), atom);
1248static BOOL xf_cliprdr_process_selection_notify(xfClipboard* clipboard,
1249 const XSelectionEvent* xevent)
1251 WINPR_ASSERT(clipboard);
1252 WINPR_ASSERT(xevent);
1254 if (xevent->target == clipboard->targets[1])
1256 if (xevent->property == None)
1258 xf_cliprdr_send_client_format_list(clipboard);
1262 xf_cliprdr_get_requested_targets(clipboard);
1269 return xf_cliprdr_get_requested_data(clipboard, xevent->target);
1273void xf_cliprdr_clear_cached_data(xfClipboard* clipboard)
1275 WINPR_ASSERT(clipboard);
1277 ClipboardLock(clipboard->system);
1278 ClipboardEmpty(clipboard->system);
1280 HashTable_Clear(clipboard->cachedData);
1281 HashTable_Clear(clipboard->cachedRawData);
1283 cliprdr_file_context_clear(clipboard->file);
1285 xf_cliprdr_stop_incr(clipboard);
1286 ClipboardUnlock(clipboard->system);
1289static void* format_to_cache_slot(UINT32 format)
1296 cnv.uptr = 0x100000000ULL + format;
1300static UINT32 get_dst_format_id_for_local_request(xfClipboard* clipboard,
1301 const xfCliprdrFormat* format)
1303 UINT32 dstFormatId = 0;
1305 WINPR_ASSERT(format);
1307 if (!format->formatName)
1308 return format->localFormat;
1310 ClipboardLock(clipboard->system);
1311 if (strcmp(format->formatName, type_HtmlFormat) == 0)
1312 dstFormatId = ClipboardGetFormatId(clipboard->system, mime_html);
1313 ClipboardUnlock(clipboard->system);
1315 if (strcmp(format->formatName, type_FileGroupDescriptorW) == 0)
1316 dstFormatId = format->localFormat;
1321static void get_src_format_info_for_local_request(xfClipboard* clipboard,
1322 const xfCliprdrFormat* format,
1323 UINT32* srcFormatId, BOOL* nullTerminated)
1326 *nullTerminated = FALSE;
1328 if (format->formatName)
1330 ClipboardLock(clipboard->system);
1331 if (strcmp(format->formatName, type_HtmlFormat) == 0)
1333 *srcFormatId = ClipboardGetFormatId(clipboard->system, type_HtmlFormat);
1334 *nullTerminated = TRUE;
1336 else if (strcmp(format->formatName, type_FileGroupDescriptorW) == 0)
1338 *srcFormatId = ClipboardGetFormatId(clipboard->system, type_FileGroupDescriptorW);
1339 *nullTerminated = TRUE;
1341 ClipboardUnlock(clipboard->system);
1345 *srcFormatId = format->formatToRequest;
1346 switch (format->formatToRequest)
1350 case CF_UNICODETEXT:
1351 *nullTerminated = TRUE;
1354 *srcFormatId = CF_DIB;
1357 *srcFormatId = CF_TIFF;
1365static xfCachedData* convert_data_from_existing_raw_data(xfClipboard* clipboard,
1366 xfCachedData* cached_raw_data,
1367 UINT32 srcFormatId, BOOL nullTerminated,
1370 UINT32 dst_size = 0;
1372 WINPR_ASSERT(clipboard);
1373 WINPR_ASSERT(cached_raw_data);
1374 WINPR_ASSERT(cached_raw_data->data);
1376 ClipboardLock(clipboard->system);
1377 BOOL success = ClipboardSetData(clipboard->system, srcFormatId, cached_raw_data->data,
1378 cached_raw_data->data_length);
1381 WLog_WARN(TAG,
"Failed to set clipboard data (formatId: %u, data: %p, data_length: %u)",
1382 srcFormatId, WINPR_CXX_COMPAT_CAST(
const void*, cached_raw_data->data),
1383 cached_raw_data->data_length);
1384 ClipboardUnlock(clipboard->system);
1388 BYTE* dst_data = ClipboardGetData(clipboard->system, dstFormatId, &dst_size);
1391 WLog_WARN(TAG,
"Failed to get converted clipboard data");
1392 ClipboardUnlock(clipboard->system);
1395 ClipboardUnlock(clipboard->system);
1399 BYTE* nullTerminator = memchr(dst_data,
'\0', dst_size);
1402 const intptr_t diff = nullTerminator - dst_data;
1403 WINPR_ASSERT(diff >= 0);
1404 WINPR_ASSERT(diff <= UINT32_MAX);
1405 dst_size = (UINT32)diff;
1409 xfCachedData* cached_data = xf_cached_data_new(dst_data, dst_size);
1412 WLog_WARN(TAG,
"Failed to allocate cache entry");
1417 if (!HashTable_Insert(clipboard->cachedData, format_to_cache_slot(dstFormatId), cached_data))
1419 WLog_WARN(TAG,
"Failed to cache clipboard data");
1420 xf_cached_data_free(cached_data);
1428static BOOL xf_cliprdr_pending_responses_ArrayList_ForEachFkt(
void* data,
size_t index, va_list ap)
1430 UINT32 formatId = 0;
1431 SelectionResponse* pendingResponse = (SelectionResponse*)data;
1432 UINT32 currentFormatId = va_arg(ap, UINT32);
1433 BOOL* res = va_arg(ap, BOOL*);
1435 WINPR_UNUSED(index);
1439 formatId = pendingResponse->requestedFormat->formatToRequest;
1441 if (formatId != currentFormatId)
1446static void xf_cliprdr_provide_selection(xfClipboard* clipboard, XSelectionEvent* respond)
1448 WINPR_ASSERT(clipboard);
1450 xfContext* xfc = clipboard->xfc;
1456 XSelectionEvent* sev;
1460 LogDynAndXSendEvent(xfc->log, xfc->display, respond->requestor, 0, 0, conv.ev);
1461 LogDynAndXFlush(xfc->log, xfc->display);
1464static BOOL xf_cliprdr_process_selection_request(xfClipboard* clipboard,
1465 const XSelectionRequestEvent* xevent)
1469 UINT32 formatId = 0;
1470 XSelectionEvent* respond =
nullptr;
1471 BYTE* data =
nullptr;
1472 BOOL delayRespond = 0;
1473 BOOL rawTransfer = 0;
1474 unsigned long length = 0;
1475 unsigned long bytes_left = 0;
1476 xfContext* xfc =
nullptr;
1478 WINPR_ASSERT(clipboard);
1479 WINPR_ASSERT(xevent);
1481 xfc = clipboard->xfc;
1484 if (xevent->owner != xfc->drawable)
1487 delayRespond = FALSE;
1489 if (!(respond = (XSelectionEvent*)calloc(1,
sizeof(XSelectionEvent))))
1491 WLog_ERR(TAG,
"failed to allocate XEvent data");
1495 respond->property = None;
1496 respond->type = SelectionNotify;
1497 respond->display = xevent->display;
1498 respond->requestor = xevent->requestor;
1499 respond->selection = xevent->selection;
1500 respond->target = xevent->target;
1501 respond->time = xevent->time;
1503 if (xevent->target == clipboard->targets[0])
1506 respond->property = xevent->property;
1507 xf_cliprdr_provide_timestamp(clipboard, respond);
1509 else if (xevent->target == clipboard->targets[1])
1512 respond->property = xevent->property;
1513 xf_cliprdr_provide_targets(clipboard, respond);
1518 xf_cliprdr_get_server_format_by_atom(clipboard, xevent->target);
1519 const xfCliprdrFormat* cformat =
1520 xf_cliprdr_get_client_format_by_atom(clipboard, xevent->target);
1522 if (format && (xevent->requestor != xfc->drawable))
1524 formatId = format->formatId;
1525 rawTransfer = FALSE;
1526 xfCachedData* cached_data =
nullptr;
1528 if (formatId == CF_RAW)
1530 if (LogDynAndXGetWindowProperty(
1531 xfc->log, xfc->display, xevent->requestor, clipboard->property_atom, 0, 4,
1532 0, XA_INTEGER, &type, &fmt, &length, &bytes_left, &data) != Success)
1539 CopyMemory(&formatId, data, 4);
1544 const UINT32 dstFormatId = get_dst_format_id_for_local_request(clipboard, cformat);
1545 DEBUG_CLIPRDR(
"formatId: 0x%08" PRIx32
", dstFormatId: 0x%08" PRIx32
"", formatId,
1548 wHashTable* table = clipboard->cachedData;
1550 table = clipboard->cachedRawData;
1552 HashTable_Lock(table);
1554 cached_data = HashTable_GetItemValue(table, format_to_cache_slot(dstFormatId));
1556 cached_data = HashTable_GetItemValue(table, format_to_cache_slot(formatId));
1557 HashTable_Unlock(table);
1559 DEBUG_CLIPRDR(
"hasCachedData: %u, rawTransfer: %d", cached_data ? 1u : 0u, rawTransfer);
1561 if (!cached_data && !rawTransfer)
1563 UINT32 srcFormatId = 0;
1564 BOOL nullTerminated = FALSE;
1565 xfCachedData* cached_raw_data =
nullptr;
1567 get_src_format_info_for_local_request(clipboard, cformat, &srcFormatId,
1570 HashTable_Lock(clipboard->cachedRawData);
1572 HashTable_GetItemValue(clipboard->cachedRawData, (
void*)(UINT_PTR)srcFormatId);
1573 HashTable_Unlock(clipboard->cachedRawData);
1575 DEBUG_CLIPRDR(
"hasCachedRawData: %u, rawDataLength: %u", cached_raw_data ? 1u : 0u,
1576 cached_raw_data ? cached_raw_data->data_length : 0);
1578 if (cached_raw_data && cached_raw_data->data_length != 0)
1579 cached_data = convert_data_from_existing_raw_data(
1580 clipboard, cached_raw_data, srcFormatId, nullTerminated, dstFormatId);
1583 DEBUG_CLIPRDR(
"hasCachedData: %u", cached_data ? 1u : 0u);
1588 respond->property = xevent->property;
1591 xf_cliprdr_provide_data(clipboard, respond, cached_data->data,
1592 cached_data->data_length);
1596 SelectionResponse* selection_response =
nullptr;
1597 WINPR_ASSERT(cformat);
1599 if (!(selection_response =
1600 (SelectionResponse*)calloc(1,
sizeof(SelectionResponse))))
1602 respond->property = None;
1605 respond->property = xevent->property;
1607 selection_response->expectedResponse = respond;
1608 requested_format_replace(&selection_response->requestedFormat, formatId,
1609 dstFormatId, cformat->formatName);
1610 selection_response->data_raw_format = rawTransfer;
1612 ArrayList_Lock(clipboard->pending_responses);
1613 ArrayList_Lock(clipboard->queued_responses);
1614 if (ArrayList_Count(clipboard->pending_responses) > 0)
1616 BOOL shouldQueued = FALSE;
1617 BOOL success = FALSE;
1618 success = ArrayList_ForEach(clipboard->pending_responses,
1619 xf_cliprdr_pending_responses_ArrayList_ForEachFkt,
1620 formatId, &shouldQueued);
1621 if (!success || shouldQueued)
1623 if (!ArrayList_Append(clipboard->queued_responses, selection_response))
1625 requested_format_free(&selection_response->requestedFormat);
1626 free(selection_response);
1627 respond->property = None;
1633 if (!ArrayList_Append(clipboard->pending_responses, selection_response))
1635 requested_format_free(&selection_response->requestedFormat);
1636 free(selection_response);
1637 respond->property = None;
1644 if (!ArrayList_Append(clipboard->pending_responses, selection_response))
1646 requested_format_free(&selection_response->requestedFormat);
1647 free(selection_response);
1648 respond->property = None;
1656 xf_cliprdr_send_data_request(clipboard, formatId, cformat);
1658 delayRespond = TRUE;
1660 ArrayList_Unlock(clipboard->queued_responses);
1661 ArrayList_Unlock(clipboard->pending_responses);
1669 xf_cliprdr_provide_selection(clipboard, respond);
1676static BOOL xf_cliprdr_process_selection_clear(xfClipboard* clipboard,
1677 const XSelectionClearEvent* xevent)
1679 xfContext* xfc =
nullptr;
1681 WINPR_ASSERT(clipboard);
1682 WINPR_ASSERT(xevent);
1684 xfc = clipboard->xfc;
1687 WINPR_UNUSED(xevent);
1689 if (xf_cliprdr_is_self_owned(clipboard))
1692 LogDynAndXDeleteProperty(xfc->log, xfc->display, clipboard->root_window,
1693 clipboard->property_atom);
1697static BOOL xf_cliprdr_process_property_notify(xfClipboard* clipboard,
const XPropertyEvent* xevent)
1699 const xfCliprdrFormat* format =
nullptr;
1700 xfContext* xfc =
nullptr;
1705 xfc = clipboard->xfc;
1707 WINPR_ASSERT(xevent);
1709 if (xevent->atom == clipboard->timestamp_property_atom)
1715 xf_cliprdr_set_selection_owner(xfc, clipboard, xevent->time);
1719 if (xevent->atom != clipboard->property_atom)
1722 if (xevent->window == clipboard->root_window)
1724 xf_cliprdr_send_client_format_list(clipboard);
1726 else if ((xevent->window == xfc->drawable) && (xevent->state == PropertyNewValue) &&
1727 clipboard->incr_starts)
1729 format = xf_cliprdr_get_client_format_by_id(clipboard, clipboard->requestedFormatId);
1732 xf_cliprdr_get_requested_data(clipboard, format->atom);
1738void xf_cliprdr_handle_xevent(xfContext* xfc,
const XEvent* event)
1740 xfClipboard* clipboard =
nullptr;
1745 clipboard = xfc->clipboard;
1752 if (clipboard->xfixes_supported &&
1753 event->type == XFixesSelectionNotify + clipboard->xfixes_event_base)
1755 const XFixesSelectionNotifyEvent* se = (
const XFixesSelectionNotifyEvent*)event;
1757 if (se->subtype == XFixesSetSelectionOwnerNotify)
1759 if (se->selection != clipboard->clipboard_atom)
1762 if (LogDynAndXGetSelectionOwner(xfc->log, xfc->display, se->selection) == xfc->drawable)
1765 clipboard->owner = None;
1766 xf_cliprdr_check_owner(clipboard);
1774 switch (event->type)
1776 case SelectionNotify:
1777 log_selection_event(xfc, event);
1778 xf_cliprdr_process_selection_notify(clipboard, &event->xselection);
1781 case SelectionRequest:
1782 log_selection_event(xfc, event);
1783 xf_cliprdr_process_selection_request(clipboard, &event->xselectionrequest);
1786 case SelectionClear:
1787 log_selection_event(xfc, event);
1788 xf_cliprdr_process_selection_clear(clipboard, &event->xselectionclear);
1791 case PropertyNotify:
1792 log_selection_event(xfc, event);
1793 xf_cliprdr_process_property_notify(clipboard, &event->xproperty);
1797 if (!clipboard->xfixes_supported)
1799 xf_cliprdr_check_owner(clipboard);
1813static UINT xf_cliprdr_send_client_capabilities(xfClipboard* clipboard)
1818 WINPR_ASSERT(clipboard);
1820 capabilities.cCapabilitiesSets = 1;
1822 generalCapabilitySet.capabilitySetType = CB_CAPSTYPE_GENERAL;
1823 generalCapabilitySet.capabilitySetLength = 12;
1824 generalCapabilitySet.version = CB_CAPS_VERSION_2;
1825 generalCapabilitySet.generalFlags = CB_USE_LONG_FORMAT_NAMES;
1827 WINPR_ASSERT(clipboard);
1828 generalCapabilitySet.generalFlags |= cliprdr_file_context_current_flags(clipboard->file);
1830 WINPR_ASSERT(clipboard->context);
1831 WINPR_ASSERT(clipboard->context->ClientCapabilities);
1832 return clipboard->context->ClientCapabilities(clipboard->context, &capabilities);
1840static UINT xf_cliprdr_send_client_format_list(xfClipboard* clipboard)
1842 WINPR_ASSERT(clipboard);
1844 xfContext* xfc = clipboard->xfc;
1847 UINT32 numFormats = 0;
1848 CLIPRDR_FORMAT* formats = xf_cliprdr_get_client_formats(clipboard, &numFormats);
1850 const UINT ret = xf_cliprdr_send_format_list(clipboard, formats, numFormats);
1852 if (clipboard->owner && clipboard->owner != xfc->drawable)
1855 LogDynAndXConvertSelection(xfc->log, xfc->display, clipboard->clipboard_atom,
1856 clipboard->targets[1], clipboard->property_atom, xfc->drawable,
1860 xf_cliprdr_free_formats(formats, numFormats);
1870static UINT xf_cliprdr_send_client_format_list_response(xfClipboard* clipboard, BOOL status)
1874 formatListResponse.common.msgType = CB_FORMAT_LIST_RESPONSE;
1875 formatListResponse.common.msgFlags = status ? CB_RESPONSE_OK : CB_RESPONSE_FAIL;
1876 formatListResponse.common.dataLen = 0;
1878 WINPR_ASSERT(clipboard);
1879 WINPR_ASSERT(clipboard->context);
1880 WINPR_ASSERT(clipboard->context->ClientFormatListResponse);
1881 return clipboard->context->ClientFormatListResponse(clipboard->context, &formatListResponse);
1889static UINT xf_cliprdr_monitor_ready(CliprdrClientContext* context,
1892 WINPR_ASSERT(context);
1893 WINPR_ASSERT(monitorReady);
1895 xfClipboard* clipboard = cliprdr_file_context_get_context(context->custom);
1896 WINPR_ASSERT(clipboard);
1898 WINPR_UNUSED(monitorReady);
1900 const UINT ret = xf_cliprdr_send_client_capabilities(clipboard);
1901 if (ret != CHANNEL_RC_OK)
1904 const UINT ret2 = xf_cliprdr_send_client_format_list(clipboard);
1905 if (ret2 != CHANNEL_RC_OK)
1908 clipboard->sync = TRUE;
1909 return CHANNEL_RC_OK;
1917static UINT xf_cliprdr_server_capabilities(CliprdrClientContext* context,
1920 WINPR_ASSERT(context);
1921 WINPR_ASSERT(capabilities);
1923 xfClipboard* clipboard = cliprdr_file_context_get_context(context->custom);
1924 WINPR_ASSERT(clipboard);
1926 const BYTE* capsPtr = (
const BYTE*)capabilities->capabilitySets;
1927 WINPR_ASSERT(capsPtr);
1929 if (!cliprdr_file_context_remote_set_flags(clipboard->file, 0))
1930 return ERROR_INTERNAL_ERROR;
1932 for (UINT32 i = 0; i < capabilities->cCapabilitiesSets; i++)
1936 if (caps->capabilitySetType == CB_CAPSTYPE_GENERAL)
1941 if (!cliprdr_file_context_remote_set_flags(clipboard->file, generalCaps->generalFlags))
1942 return ERROR_INTERNAL_ERROR;
1945 capsPtr += caps->capabilitySetLength;
1948 return CHANNEL_RC_OK;
1951static void xf_cliprdr_prepare_to_set_selection_owner(xfContext* xfc, xfClipboard* clipboard)
1954 WINPR_ASSERT(clipboard);
1971 Atom value = clipboard->timestamp_property_atom;
1973 LogDynAndXChangeProperty(xfc->log, xfc->display, xfc->drawable,
1974 clipboard->timestamp_property_atom, XA_ATOM, 32, PropModeReplace,
1975 (
const BYTE*)&value, 1);
1976 LogDynAndXFlush(xfc->log, xfc->display);
1979static void xf_cliprdr_set_selection_owner(xfContext* xfc, xfClipboard* clipboard, Time timestamp)
1982 WINPR_ASSERT(clipboard);
1988 clipboard->selection_ownership_timestamp = timestamp;
1989 LogDynAndXSetSelectionOwner(xfc->log, xfc->display, clipboard->clipboard_atom, xfc->drawable,
1991 LogDynAndXFlush(xfc->log, xfc->display);
1999static UINT xf_cliprdr_server_format_list(CliprdrClientContext* context,
2002 xfContext* xfc =
nullptr;
2004 xfClipboard* clipboard =
nullptr;
2006 WINPR_ASSERT(context);
2007 WINPR_ASSERT(formatList);
2009 clipboard = cliprdr_file_context_get_context(context->custom);
2010 WINPR_ASSERT(clipboard);
2012 xfc = clipboard->xfc;
2018 ArrayList_Clear(clipboard->pending_responses);
2019 ArrayList_Clear(clipboard->queued_responses);
2021 xf_cliprdr_clear_cached_data(clipboard);
2023 xf_clipboard_free_server_formats(clipboard);
2025 clipboard->numServerFormats = formatList->numFormats + 1;
2027 if (!(clipboard->serverFormats =
2030 WLog_ERR(TAG,
"failed to allocate %" PRIu32
" CLIPRDR_FORMAT structs",
2031 clipboard->numServerFormats);
2032 ret = CHANNEL_RC_NO_MEMORY;
2036 for (
size_t i = 0; i < formatList->numFormats; i++)
2041 srvFormat->formatId = format->formatId;
2043 if (format->formatName)
2045 srvFormat->formatName = _strdup(format->formatName);
2047 if (!srvFormat->formatName)
2049 for (UINT32 k = 0; k < i; k++)
2050 free(clipboard->serverFormats[k].formatName);
2052 clipboard->numServerFormats = 0;
2053 free(clipboard->serverFormats);
2054 clipboard->serverFormats =
nullptr;
2055 ret = CHANNEL_RC_NO_MEMORY;
2061 ClipboardLock(clipboard->system);
2062 ret = cliprdr_file_context_notify_new_server_format_list(clipboard->file);
2063 ClipboardUnlock(clipboard->system);
2069 CLIPRDR_FORMAT* format = &clipboard->serverFormats[formatList->numFormats];
2070 format->formatId = CF_RAW;
2071 format->formatName =
nullptr;
2073 xf_cliprdr_provide_server_format_list(clipboard);
2074 clipboard->numTargets = 2;
2076 for (
size_t i = 0; i < formatList->numFormats; i++)
2080 for (
size_t j = 0; j < clipboard->numClientFormats; j++)
2082 const xfCliprdrFormat* clientFormat = &clipboard->clientFormats[j];
2083 if (xf_cliprdr_formats_equal(format, clientFormat))
2085 if ((clientFormat->formatName !=
nullptr) &&
2086 (strcmp(type_FileGroupDescriptorW, clientFormat->formatName) == 0))
2088 if (!cliprdr_file_context_has_local_support(clipboard->file))
2091 xf_cliprdr_append_target(clipboard, clientFormat->atom);
2096 ret = xf_cliprdr_send_client_format_list_response(clipboard, TRUE);
2097 if (xfc->remote_app)
2098 xf_cliprdr_set_selection_owner(xfc, clipboard, CurrentTime);
2100 xf_cliprdr_prepare_to_set_selection_owner(xfc, clipboard);
2113static UINT xf_cliprdr_server_format_list_response(
2114 WINPR_ATTR_UNUSED CliprdrClientContext* context,
2117 WINPR_ASSERT(context);
2118 WINPR_ASSERT(formatListResponse);
2120 return CHANNEL_RC_OK;
2129xf_cliprdr_server_format_data_request(CliprdrClientContext* context,
2132 const xfCliprdrFormat* format =
nullptr;
2134 WINPR_ASSERT(context);
2135 WINPR_ASSERT(formatDataRequest);
2137 xfClipboard* clipboard = cliprdr_file_context_get_context(context->custom);
2138 WINPR_ASSERT(clipboard);
2140 xfContext* xfc = clipboard->xfc;
2143 const uint32_t formatId = formatDataRequest->requestedFormatId;
2145 const BOOL rawTransfer = xf_cliprdr_is_raw_transfer_available(clipboard);
2149 format = xf_cliprdr_get_client_format_by_id(clipboard, CF_RAW);
2150 LogDynAndXChangeProperty(xfc->log, xfc->display, xfc->drawable, clipboard->property_atom,
2151 XA_INTEGER, 32, PropModeReplace, (
const BYTE*)&formatId, 1);
2154 format = xf_cliprdr_get_client_format_by_id(clipboard, formatId);
2156 clipboard->requestedFormatId = rawTransfer ? CF_RAW : formatId;
2158 return xf_cliprdr_send_data_response(clipboard, format,
nullptr, 0);
2160 DEBUG_CLIPRDR(
"requested format 0x%08" PRIx32
" [%s] {local 0x%08" PRIx32
" [%s]} [%s]",
2161 format->formatToRequest, ClipboardGetFormatIdString(format->formatToRequest),
2162 format->localFormat,
2163 ClipboardGetFormatName(clipboard->system, format->localFormat),
2164 format->formatName);
2165 LogDynAndXConvertSelection(xfc->log, xfc->display, clipboard->clipboard_atom, format->atom,
2166 clipboard->property_atom, xfc->drawable, CurrentTime);
2167 LogDynAndXFlush(xfc->log, xfc->display);
2169 return CHANNEL_RC_OK;
2178xf_cliprdr_server_format_data_response(CliprdrClientContext* context,
2181 BOOL bSuccess = FALSE;
2182 BOOL bRawCached = FALSE;
2183 BOOL bFileContextUpdated = FALSE;
2185 WINPR_ASSERT(context);
2186 WINPR_ASSERT(formatDataResponse);
2188 xfClipboard* clipboard = cliprdr_file_context_get_context(context->custom);
2189 WINPR_ASSERT(clipboard);
2191 xfContext* xfc = clipboard->xfc;
2194 const UINT32 size = formatDataResponse->common.dataLen;
2195 const BYTE* data = formatDataResponse->requestedFormatData;
2199 ArrayList_Lock(clipboard->pending_responses);
2200 ArrayList_Lock(clipboard->queued_responses);
2201 if (formatDataResponse->common.msgFlags == CB_RESPONSE_FAIL)
2203 WLog_WARN(TAG,
"Format Data Response PDU msgFlags is CB_RESPONSE_FAIL");
2204 while (ArrayList_Count(clipboard->pending_responses) > 0)
2206 SelectionResponse* pending = ArrayList_GetItem(clipboard->pending_responses, 0);
2208 pending->expectedResponse->property = None;
2209 xf_cliprdr_provide_selection(clipboard, pending->expectedResponse);
2211 ArrayList_Remove(clipboard->pending_responses, pending);
2213 WINPR_ASSERT(ArrayList_Count(clipboard->pending_responses) == 0);
2216 while (ArrayList_Count(clipboard->pending_responses) > 0)
2218 BYTE* pDstData =
nullptr;
2221 UINT32 srcFormatId = 0;
2222 UINT32 dstFormatId = 0;
2223 BOOL nullTerminated = FALSE;
2224 xfCachedData* cached_data =
nullptr;
2225 xfCachedData* hit_cached_data =
nullptr;
2227 SelectionResponse* pending = ArrayList_GetItem(clipboard->pending_responses, 0);
2228 const RequestedFormat* format = pending->requestedFormat;
2229 if (pending->data_raw_format)
2231 srcFormatId = CF_RAW;
2232 dstFormatId = CF_RAW;
2236 pending->expectedResponse->property = None;
2239 else if (format->formatName)
2241 dstFormatId = format->localFormat;
2243 ClipboardLock(clipboard->system);
2244 if (strcmp(format->formatName, type_HtmlFormat) == 0)
2246 srcFormatId = ClipboardGetFormatId(clipboard->system, type_HtmlFormat);
2247 dstFormatId = ClipboardGetFormatId(clipboard->system, mime_html);
2248 nullTerminated = TRUE;
2251 if (strcmp(format->formatName, type_FileGroupDescriptorW) == 0)
2253 if (!bFileContextUpdated)
2255 if (!cliprdr_file_context_update_server_data(clipboard->file, clipboard->system,
2257 WLog_WARN(TAG,
"failed to update file descriptors");
2259 bFileContextUpdated = TRUE;
2262 srcFormatId = ClipboardGetFormatId(clipboard->system, type_FileGroupDescriptorW);
2263 const xfCliprdrFormat* dstTargetFormat = xf_cliprdr_get_client_format_by_atom(
2264 clipboard, pending->expectedResponse->target);
2265 if (!dstTargetFormat)
2267 dstFormatId = ClipboardGetFormatId(clipboard->system, mime_uri_list);
2271 dstFormatId = dstTargetFormat->localFormat;
2274 nullTerminated = TRUE;
2276 ClipboardUnlock(clipboard->system);
2280 srcFormatId = format->formatToRequest;
2281 dstFormatId = format->localFormat;
2282 switch (format->formatToRequest)
2285 nullTerminated = TRUE;
2289 nullTerminated = TRUE;
2292 case CF_UNICODETEXT:
2293 nullTerminated = TRUE;
2297 srcFormatId = CF_DIB;
2301 srcFormatId = CF_TIFF;
2309 DEBUG_CLIPRDR(
"requested format 0x%08" PRIx32
" [%s] {local 0x%08" PRIx32
" [%s]} [%s]",
2310 format->formatToRequest, ClipboardGetFormatIdString(format->formatToRequest),
2311 format->localFormat,
2312 ClipboardGetFormatName(clipboard->system, format->localFormat),
2313 format->formatName);
2316 DEBUG_CLIPRDR(
"srcFormatId: 0x%08" PRIx32
", dstFormatId: 0x%08" PRIx32
"", srcFormatId,
2319 if (SrcSize != 0 && !bRawCached)
2327 xfCachedData* cached_raw_data = xf_cached_data_new_copy(data, size);
2328 if (!cached_raw_data)
2329 WLog_WARN(TAG,
"Failed to allocate cache entry");
2333 HashTable_Insert(clipboard->cachedRawData,
2334 (
void*)(UINT_PTR)srcFormatId, cached_raw_data)))
2336 WLog_WARN(TAG,
"Failed to cache clipboard data");
2337 xf_cached_data_free(cached_raw_data);
2346 WLog_DBG(TAG,
"skipping, empty data detected!");
2352 ClipboardLock(clipboard->system);
2353 bSuccess = ClipboardSetData(clipboard->system, srcFormatId, data, SrcSize);
2354 ClipboardUnlock(clipboard->system);
2359 WLog_DBG(TAG,
"skipping, ClipboardSetData failed!");
2363 wHashTable* table = clipboard->cachedData;
2364 if (pending->data_raw_format)
2365 table = clipboard->cachedRawData;
2367 HashTable_Lock(table);
2369 if (!pending->data_raw_format)
2370 hit_cached_data = HashTable_GetItemValue(table, format_to_cache_slot(dstFormatId));
2372 hit_cached_data = HashTable_GetItemValue(table, format_to_cache_slot(srcFormatId));
2374 HashTable_Unlock(table);
2376 DEBUG_CLIPRDR(
"hasCachedData: %u, pending->data_raw_format: %d", hit_cached_data ? 1u : 0u,
2377 pending->data_raw_format);
2379 ClipboardLock(clipboard->system);
2380 if (hit_cached_data)
2382 pDstData = hit_cached_data->data;
2383 DstSize = hit_cached_data->data_length;
2387 pDstData = (BYTE*)ClipboardGetData(clipboard->system, dstFormatId, &DstSize);
2392 WLog_WARN(TAG,
"failed to get clipboard data in format %s [source format %s]",
2393 ClipboardGetFormatName(clipboard->system, dstFormatId),
2394 ClipboardGetFormatName(clipboard->system, srcFormatId));
2396 ClipboardUnlock(clipboard->system);
2400 pending->expectedResponse->property = None;
2404 if (nullTerminated && pDstData)
2406 BYTE* nullTerminator = memchr(pDstData,
'\0', DstSize);
2409 const intptr_t diff = nullTerminator - pDstData;
2410 WINPR_ASSERT(diff >= 0);
2411 WINPR_ASSERT(diff <= UINT32_MAX);
2412 DstSize = (UINT32)diff;
2418 xf_cliprdr_provide_data(clipboard, pending->expectedResponse, pDstData, DstSize);
2420 if (!hit_cached_data && pDstData)
2422 cached_data = xf_cached_data_new(pDstData, DstSize);
2427 WLog_WARN(TAG,
"Failed to allocate cache entry");
2431 HashTable_Lock(clipboard->cachedData);
2432 if (!HashTable_Insert(clipboard->cachedData, format_to_cache_slot(dstFormatId),
2435 WLog_WARN(TAG,
"Failed to cache clipboard data");
2436 xf_cached_data_free(cached_data);
2439 HashTable_Unlock(clipboard->cachedData);
2444 xf_cliprdr_provide_selection(clipboard, pending->expectedResponse);
2446 ArrayList_Remove(clipboard->pending_responses, pending);
2450 WINPR_ASSERT(ArrayList_Count(clipboard->pending_responses) == 0);
2452 SelectionResponse* next = ArrayList_GetItem(clipboard->queued_responses, 0);
2455 UINT32 nextFormatId = next->requestedFormat->formatToRequest;
2456 const xfCliprdrFormat* cformat =
2457 xf_cliprdr_get_client_format_by_atom(clipboard, next->expectedResponse->target);
2460 while ((next = ArrayList_GetItem(clipboard->queued_responses, index)) !=
nullptr)
2462 if (next->requestedFormat->formatToRequest == nextFormatId)
2465 if (!ArrayList_SetItem(clipboard->queued_responses, index,
nullptr))
2467 ArrayList_RemoveAt(clipboard->queued_responses, index);
2468 if (!ArrayList_Append(clipboard->pending_responses, next))
2470 selection_response_free(next);
2478 xf_cliprdr_send_data_request(clipboard, nextFormatId, cformat);
2481 ArrayList_Unlock(clipboard->queued_responses);
2482 ArrayList_Unlock(clipboard->pending_responses);
2485 return CHANNEL_RC_OK;
2488static BOOL xf_cliprdr_is_valid_unix_filename(LPCWSTR filename)
2493 if (filename[0] == L
'\0')
2497 for (
const WCHAR* c = filename; *c; ++c)
2506xfClipboard* xf_clipboard_new(xfContext* xfc, BOOL relieveFilenameRestriction)
2509 rdpChannels* channels =
nullptr;
2510 xfClipboard* clipboard =
nullptr;
2511 const char* selectionAtom =
nullptr;
2512 xfCliprdrFormat* clientFormat =
nullptr;
2516 WINPR_ASSERT(xfc->common.context.settings);
2518 if (!(clipboard = (xfClipboard*)calloc(1,
sizeof(xfClipboard))))
2520 WLog_ERR(TAG,
"failed to allocate xfClipboard data");
2524 clipboard->file = cliprdr_file_context_new(clipboard);
2525 if (!clipboard->file)
2528 xfc->clipboard = clipboard;
2529 clipboard->xfc = xfc;
2530 channels = xfc->common.context.channels;
2531 clipboard->channels = channels;
2532 clipboard->system = ClipboardCreate();
2533 clipboard->requestedFormatId = UINT32_MAX;
2534 clipboard->root_window = DefaultRootWindow(xfc->display);
2539 selectionAtom =
"CLIPBOARD";
2541 clipboard->clipboard_atom = Logging_XInternAtom(xfc->log, xfc->display, selectionAtom, FALSE);
2543 if (clipboard->clipboard_atom == None)
2545 WLog_ERR(TAG,
"unable to get %s atom", selectionAtom);
2549 clipboard->timestamp_property_atom =
2550 Logging_XInternAtom(xfc->log, xfc->display,
"_FREERDP_TIMESTAMP_PROPERTY", FALSE);
2551 clipboard->property_atom =
2552 Logging_XInternAtom(xfc->log, xfc->display,
"_FREERDP_CLIPRDR", FALSE);
2553 clipboard->raw_transfer_atom =
2554 Logging_XInternAtom(xfc->log, xfc->display,
"_FREERDP_CLIPRDR_RAW", FALSE);
2555 clipboard->raw_format_list_atom =
2556 Logging_XInternAtom(xfc->log, xfc->display,
"_FREERDP_CLIPRDR_FORMATS", FALSE);
2557 xf_cliprdr_set_raw_transfer_enabled(clipboard, TRUE);
2558 XSelectInput(xfc->display, clipboard->root_window, PropertyChangeMask);
2561 if (XFixesQueryExtension(xfc->display, &clipboard->xfixes_event_base,
2562 &clipboard->xfixes_error_base))
2567 if (XFixesQueryVersion(xfc->display, &xfmajor, &xfminor))
2569 XFixesSelectSelectionInput(xfc->display, clipboard->root_window,
2570 clipboard->clipboard_atom,
2571 XFixesSetSelectionOwnerNotifyMask);
2572 clipboard->xfixes_supported = TRUE;
2576 WLog_ERR(TAG,
"Error querying X Fixes extension version");
2581 WLog_ERR(TAG,
"Error loading X Fixes extension");
2587 "Warning: Using clipboard redirection without XFIXES extension is strongly discouraged!");
2589 clientFormat = &clipboard->clientFormats[n++];
2590 clientFormat->atom = Logging_XInternAtom(xfc->log, xfc->display,
"_FREERDP_RAW", False);
2591 clientFormat->localFormat = clientFormat->formatToRequest = CF_RAW;
2593 clientFormat = &clipboard->clientFormats[n++];
2594 clientFormat->atom = Logging_XInternAtom(xfc->log, xfc->display,
"UTF8_STRING", False);
2595 clientFormat->formatToRequest = CF_UNICODETEXT;
2596 clientFormat->localFormat = ClipboardGetFormatId(xfc->clipboard->system, mime_text_plain);
2598 clientFormat = &clipboard->clientFormats[n++];
2599 clientFormat->atom = XA_STRING;
2600 clientFormat->formatToRequest = CF_TEXT;
2601 clientFormat->localFormat = ClipboardGetFormatId(xfc->clipboard->system, mime_text_plain);
2603 clientFormat = &clipboard->clientFormats[n++];
2604 clientFormat->atom = Logging_XInternAtom(xfc->log, xfc->display, mime_tiff, False);
2605 clientFormat->formatToRequest = clientFormat->localFormat = CF_TIFF;
2607 for (
size_t x = 0; x < ARRAYSIZE(mime_bitmap); x++)
2609 const char* mime_bmp = mime_bitmap[x];
2610 const DWORD format = ClipboardGetFormatId(xfc->clipboard->system, mime_bmp);
2613 WLog_DBG(TAG,
"skipping local bitmap format %s [NOT SUPPORTED]", mime_bmp);
2617 WLog_DBG(TAG,
"register local bitmap format %s [0x%08" PRIx32
"]", mime_bmp, format);
2618 clientFormat = &clipboard->clientFormats[n++];
2619 clientFormat->localFormat = format;
2620 clientFormat->atom = Logging_XInternAtom(xfc->log, xfc->display, mime_bmp, False);
2621 clientFormat->formatToRequest = CF_DIB;
2622 clientFormat->isImage = TRUE;
2625 for (
size_t x = 0; x < ARRAYSIZE(mime_images); x++)
2627 const char* mime_bmp = mime_images[x];
2628 const DWORD format = ClipboardGetFormatId(xfc->clipboard->system, mime_bmp);
2631 WLog_DBG(TAG,
"skipping local bitmap format %s [NOT SUPPORTED]", mime_bmp);
2635 WLog_DBG(TAG,
"register local bitmap format %s [0x%08" PRIx32
"]", mime_bmp, format);
2636 clientFormat = &clipboard->clientFormats[n++];
2637 clientFormat->localFormat = format;
2638 clientFormat->atom = Logging_XInternAtom(xfc->log, xfc->display, mime_bmp, False);
2639 clientFormat->formatToRequest = CF_DIB;
2640 clientFormat->isImage = TRUE;
2643 clientFormat = &clipboard->clientFormats[n++];
2644 clientFormat->atom = Logging_XInternAtom(xfc->log, xfc->display, mime_html, False);
2645 clientFormat->formatToRequest = ClipboardGetFormatId(xfc->clipboard->system, type_HtmlFormat);
2646 clientFormat->localFormat = ClipboardGetFormatId(xfc->clipboard->system, mime_html);
2647 clientFormat->formatName = _strdup(type_HtmlFormat);
2649 if (!clientFormat->formatName)
2652 clientFormat = &clipboard->clientFormats[n++];
2661 const UINT32 fgid = ClipboardGetFormatId(clipboard->system, type_FileGroupDescriptorW);
2663 const UINT32 uid = ClipboardGetFormatId(clipboard->system, mime_uri_list);
2666 if (!cliprdr_file_context_set_locally_available(clipboard->file, TRUE))
2668 clientFormat->atom =
2669 Logging_XInternAtom(xfc->log, xfc->display, mime_uri_list, False);
2670 clientFormat->localFormat = uid;
2671 clientFormat->formatToRequest = fgid;
2672 clientFormat->formatName = _strdup(type_FileGroupDescriptorW);
2674 if (!clientFormat->formatName)
2677 clientFormat = &clipboard->clientFormats[n++];
2682 const UINT32 gid = ClipboardGetFormatId(clipboard->system, mime_gnome_copied_files);
2685 if (!cliprdr_file_context_set_locally_available(clipboard->file, TRUE))
2687 clientFormat->atom =
2688 Logging_XInternAtom(xfc->log, xfc->display, mime_gnome_copied_files, False);
2689 clientFormat->localFormat = gid;
2690 clientFormat->formatToRequest = fgid;
2691 clientFormat->formatName = _strdup(type_FileGroupDescriptorW);
2693 if (!clientFormat->formatName)
2696 clientFormat = &clipboard->clientFormats[n++];
2701 const UINT32 mid = ClipboardGetFormatId(clipboard->system, mime_mate_copied_files);
2704 if (!cliprdr_file_context_set_locally_available(clipboard->file, TRUE))
2706 clientFormat->atom =
2707 Logging_XInternAtom(xfc->log, xfc->display, mime_mate_copied_files, False);
2708 clientFormat->localFormat = mid;
2709 clientFormat->formatToRequest = fgid;
2710 clientFormat->formatName = _strdup(type_FileGroupDescriptorW);
2712 if (!clientFormat->formatName)
2718 clipboard->numClientFormats = WINPR_ASSERTING_INT_CAST(uint32_t, n);
2719 clipboard->targets[0] = Logging_XInternAtom(xfc->log, xfc->display,
"TIMESTAMP", FALSE);
2720 clipboard->targets[1] = Logging_XInternAtom(xfc->log, xfc->display,
"TARGETS", FALSE);
2721 clipboard->numTargets = 2;
2722 clipboard->incr_atom = Logging_XInternAtom(xfc->log, xfc->display,
"INCR", FALSE);
2724 if (relieveFilenameRestriction)
2726 WLog_DBG(TAG,
"Relieving CLIPRDR filename restriction");
2727 ClipboardGetDelegate(clipboard->system)->IsFileNameComponentValid =
2728 xf_cliprdr_is_valid_unix_filename;
2731 clipboard->cachedData = HashTable_New(TRUE);
2732 if (!clipboard->cachedData)
2735 obj = HashTable_ValueObject(clipboard->cachedData);
2738 clipboard->cachedRawData = HashTable_New(TRUE);
2739 if (!clipboard->cachedRawData)
2742 obj = HashTable_ValueObject(clipboard->cachedRawData);
2745 clipboard->pending_responses = ArrayList_New(TRUE);
2746 if (!clipboard->pending_responses)
2748 obj = ArrayList_Object(clipboard->pending_responses);
2751 clipboard->queued_responses = ArrayList_New(TRUE);
2752 if (!clipboard->queued_responses)
2754 obj = ArrayList_Object(clipboard->queued_responses);
2760 WINPR_PRAGMA_DIAG_PUSH
2761 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
2762 xf_clipboard_free(clipboard);
2763 WINPR_PRAGMA_DIAG_POP
2767void xf_clipboard_free(xfClipboard* clipboard)
2772 xf_clipboard_free_server_formats(clipboard);
2774 if (clipboard->numClientFormats)
2776 for (UINT32 i = 0; i < clipboard->numClientFormats; i++)
2778 xfCliprdrFormat* format = &clipboard->clientFormats[i];
2779 free(format->formatName);
2783 cliprdr_file_context_free(clipboard->file);
2785 ClipboardDestroy(clipboard->system);
2786 HashTable_Free(clipboard->cachedRawData);
2787 HashTable_Free(clipboard->cachedData);
2788 ArrayList_Free(clipboard->pending_responses);
2789 ArrayList_Free(clipboard->queued_responses);
2790 free(clipboard->incr_data);
2794void xf_cliprdr_init(xfContext* xfc, CliprdrClientContext* cliprdr)
2797 WINPR_ASSERT(cliprdr);
2799 xfc->cliprdr = cliprdr;
2800 xfc->clipboard->context = cliprdr;
2802 cliprdr->MonitorReady = xf_cliprdr_monitor_ready;
2803 cliprdr->ServerCapabilities = xf_cliprdr_server_capabilities;
2804 cliprdr->ServerFormatList = xf_cliprdr_server_format_list;
2805 cliprdr->ServerFormatListResponse = xf_cliprdr_server_format_list_response;
2806 cliprdr->ServerFormatDataRequest = xf_cliprdr_server_format_data_request;
2807 cliprdr->ServerFormatDataResponse = xf_cliprdr_server_format_data_response;
2809 cliprdr_file_context_init(xfc->clipboard->file, cliprdr);
2812void xf_cliprdr_uninit(xfContext* xfc, CliprdrClientContext* cliprdr)
2816 xfc->cliprdr =
nullptr;
2820 ClipboardLock(xfc->clipboard->system);
2821 cliprdr_file_context_uninit(xfc->clipboard->file, cliprdr);
2822 ClipboardUnlock(xfc->clipboard->system);
2823 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