22 #include <freerdp/config.h>
28 #include <X11/Xatom.h>
31 #include <X11/extensions/Xfixes.h>
34 #include <winpr/crt.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 #ifdef WITH_DEBUG_CLIPRDR
58 #define DEBUG_CLIPRDR(...) WLog_DBG(TAG, __VA_ARGS__)
60 #define DEBUG_CLIPRDR(...) \
69 UINT32 formatToRequest;
83 UINT32 formatToRequest;
90 rdpChannels* channels;
91 CliprdrClientContext* context;
99 Atom timestamp_property_atom;
100 Time selection_ownership_timestamp;
102 Atom raw_transfer_atom;
103 Atom raw_format_list_atom;
105 UINT32 numClientFormats;
106 xfCliprdrFormat clientFormats[20];
108 UINT32 numServerFormats;
114 int requestedFormatId;
116 wHashTable* cachedData;
117 wHashTable* cachedRawData;
119 BOOL data_raw_format;
121 RequestedFormat* requestedFormat;
123 XSelectionEvent* respond;
132 int incr_data_length;
136 int xfixes_event_base;
137 int xfixes_error_base;
138 BOOL xfixes_supported;
142 UINT32 lastSentNumFormats;
143 CliprdrFileContext* file;
146 static const char mime_text_plain[] =
"text/plain";
147 static const char mime_uri_list[] =
"text/uri-list";
148 static const char mime_html[] =
"text/html";
149 static const char* mime_bitmap[] = {
"image/bmp",
"image/x-bmp",
"image/x-MS-bmp",
150 "image/x-win-bitmap" };
151 static const char mime_webp[] =
"image/webp";
152 static const char mime_png[] =
"image/png";
153 static const char mime_jpeg[] =
"image/jpeg";
154 static const char mime_tiff[] =
"image/tiff";
155 static const char* mime_images[] = { mime_webp, mime_png, mime_jpeg, mime_tiff };
157 static const char mime_gnome_copied_files[] =
"x-special/gnome-copied-files";
158 static const char mime_mate_copied_files[] =
"x-special/mate-copied-files";
160 static const char type_FileGroupDescriptorW[] =
"FileGroupDescriptorW";
161 static const char type_HtmlFormat[] =
"HTML Format";
163 static void xf_cliprdr_clear_cached_data(xfClipboard* clipboard);
164 static UINT xf_cliprdr_send_client_format_list(xfClipboard* clipboard, BOOL force);
165 static void xf_cliprdr_set_selection_owner(xfContext* xfc, xfClipboard* clipboard, Time timestamp);
167 static void requested_format_free(RequestedFormat** ppRequestedFormat)
169 if (!ppRequestedFormat)
171 if (!(*ppRequestedFormat))
174 free((*ppRequestedFormat)->formatName);
175 free(*ppRequestedFormat);
176 *ppRequestedFormat = NULL;
179 static BOOL requested_format_replace(RequestedFormat** ppRequestedFormat, UINT32 formatId,
180 const char* formatName)
182 if (!ppRequestedFormat)
185 requested_format_free(ppRequestedFormat);
186 RequestedFormat* requested = calloc(1,
sizeof(RequestedFormat));
189 requested->localFormat = formatId;
190 requested->formatToRequest = formatId;
193 requested->formatName = _strdup(formatName);
194 if (!requested->formatName)
201 *ppRequestedFormat = requested;
205 static BOOL requested_format_matches(
const RequestedFormat* pRequestedFormat, UINT32 formatId,
206 const char* formatName)
208 if (!pRequestedFormat)
210 if (pRequestedFormat->formatToRequest != formatId)
212 if (formatName || pRequestedFormat->formatName)
214 if (!formatName || !pRequestedFormat->formatName)
216 if (strcmp(formatName, pRequestedFormat->formatName) != 0)
222 static void xf_cached_data_free(
void* ptr)
224 xfCachedData* cached_data = ptr;
228 free(cached_data->data);
232 static xfCachedData* xf_cached_data_new(BYTE* data,
size_t data_length)
234 if (data_length > UINT32_MAX)
237 xfCachedData* cached_data = calloc(1,
sizeof(xfCachedData));
241 cached_data->data = data;
242 cached_data->data_length = (UINT32)data_length;
247 static xfCachedData* xf_cached_data_new_copy(
const BYTE* data,
size_t data_length)
252 copy = malloc(data_length);
255 memcpy(copy, data, data_length);
258 xfCachedData* cache = xf_cached_data_new(copy, data_length);
264 static void xf_clipboard_free_server_formats(xfClipboard* clipboard)
266 WINPR_ASSERT(clipboard);
267 if (clipboard->serverFormats)
269 for (
size_t i = 0; i < clipboard->numServerFormats; i++)
272 free(format->formatName);
275 free(clipboard->serverFormats);
276 clipboard->serverFormats = NULL;
280 static BOOL xf_cliprdr_update_owner(xfClipboard* clipboard)
282 WINPR_ASSERT(clipboard);
284 xfContext* xfc = clipboard->xfc;
287 if (!clipboard->sync)
290 Window owner = XGetSelectionOwner(xfc->display, clipboard->clipboard_atom);
291 if (clipboard->owner == owner)
294 clipboard->owner = owner;
298 static void xf_cliprdr_check_owner(xfClipboard* clipboard)
300 if (xf_cliprdr_update_owner(clipboard))
301 xf_cliprdr_send_client_format_list(clipboard, FALSE);
304 static BOOL xf_cliprdr_is_self_owned(xfClipboard* clipboard)
306 xfContext* xfc = NULL;
308 WINPR_ASSERT(clipboard);
310 xfc = clipboard->xfc;
312 return XGetSelectionOwner(xfc->display, clipboard->clipboard_atom) == xfc->drawable;
315 static void xf_cliprdr_set_raw_transfer_enabled(xfClipboard* clipboard, BOOL enabled)
317 UINT32 data = enabled;
318 xfContext* xfc = NULL;
320 WINPR_ASSERT(clipboard);
322 xfc = clipboard->xfc;
324 LogTagAndXChangeProperty(TAG, xfc->display, xfc->drawable, clipboard->raw_transfer_atom,
325 XA_INTEGER, 32, PropModeReplace, (BYTE*)&data, 1);
328 static BOOL xf_cliprdr_is_raw_transfer_available(xfClipboard* clipboard)
333 unsigned long length = 0;
334 unsigned long bytes_left = 0;
336 UINT32 is_enabled = 0;
338 xfContext* xfc = NULL;
340 WINPR_ASSERT(clipboard);
342 xfc = clipboard->xfc;
345 owner = XGetSelectionOwner(xfc->display, clipboard->clipboard_atom);
349 result = LogTagAndXGetWindowProperty(TAG, xfc->display, owner, clipboard->raw_transfer_atom,
350 0, 4, 0, XA_INTEGER, &type, &format, &length,
351 &bytes_left, (BYTE**)&data);
360 if ((owner == None) || (owner == xfc->drawable))
363 if (result != Success)
366 return is_enabled ? TRUE : FALSE;
369 static BOOL xf_cliprdr_formats_equal(
const CLIPRDR_FORMAT* server,
const xfCliprdrFormat* client)
371 WINPR_ASSERT(server);
372 WINPR_ASSERT(client);
374 if (server->formatName && client->formatName)
377 return (0 == strncmp(server->formatName, client->formatName, strlen(server->formatName)));
380 if (!server->formatName && !client->formatName)
382 return (server->formatId == client->formatToRequest);
388 static const xfCliprdrFormat* xf_cliprdr_get_client_format_by_id(xfClipboard* clipboard,
391 WINPR_ASSERT(clipboard);
393 for (
size_t index = 0; index < clipboard->numClientFormats; index++)
395 const xfCliprdrFormat* format = &(clipboard->clientFormats[index]);
397 if (format->formatToRequest == formatId)
404 static const xfCliprdrFormat* xf_cliprdr_get_client_format_by_atom(xfClipboard* clipboard,
407 WINPR_ASSERT(clipboard);
409 for (UINT32 i = 0; i < clipboard->numClientFormats; i++)
411 const xfCliprdrFormat* format = &(clipboard->clientFormats[i]);
413 if (format->atom == atom)
420 static const CLIPRDR_FORMAT* xf_cliprdr_get_server_format_by_atom(xfClipboard* clipboard, Atom atom)
422 WINPR_ASSERT(clipboard);
424 for (
size_t i = 0; i < clipboard->numClientFormats; i++)
426 const xfCliprdrFormat* client_format = &(clipboard->clientFormats[i]);
428 if (client_format->atom == atom)
430 for (
size_t j = 0; j < clipboard->numServerFormats; j++)
432 const CLIPRDR_FORMAT* server_format = &(clipboard->serverFormats[j]);
434 if (xf_cliprdr_formats_equal(server_format, client_format))
435 return server_format;
448 static UINT xf_cliprdr_send_data_request(xfClipboard* clipboard, UINT32 formatId,
449 const xfCliprdrFormat* cformat)
452 request.requestedFormatId = formatId;
454 DEBUG_CLIPRDR(
"requesting format 0x%08" PRIx32
" [%s] {local 0x%08" PRIx32
"} [%s]", formatId,
455 ClipboardGetFormatIdString(formatId), cformat->localFormat, cformat->formatName);
457 WINPR_ASSERT(clipboard);
458 WINPR_ASSERT(clipboard->context);
459 WINPR_ASSERT(clipboard->context->ClientFormatDataRequest);
460 return clipboard->context->ClientFormatDataRequest(clipboard->context, &request);
468 static UINT xf_cliprdr_send_data_response(xfClipboard* clipboard,
const xfCliprdrFormat* format,
469 const BYTE* data,
size_t size)
473 WINPR_ASSERT(clipboard);
476 if (clipboard->requestedFormatId < 0)
477 return CHANNEL_RC_OK;
482 DEBUG_CLIPRDR(
"send CB_RESPONSE_FAIL response {format 0x%08" PRIx32
483 " [%s] {local 0x%08" PRIx32
"} [%s]",
484 format->formatToRequest,
485 ClipboardGetFormatIdString(format->formatToRequest), format->localFormat,
488 DEBUG_CLIPRDR(
"send CB_RESPONSE_FAIL response");
492 WINPR_ASSERT(format);
493 DEBUG_CLIPRDR(
"send response format 0x%08" PRIx32
" [%s] {local 0x%08" PRIx32
"} [%s]",
494 format->formatToRequest, ClipboardGetFormatIdString(format->formatToRequest),
495 format->localFormat, format->formatName);
498 clipboard->requestedFormatId = -1;
500 response.common.msgFlags = (data) ? CB_RESPONSE_OK : CB_RESPONSE_FAIL;
502 WINPR_ASSERT(size <= UINT32_MAX);
503 response.common.dataLen = (UINT32)size;
504 response.requestedFormatData = data;
506 WINPR_ASSERT(clipboard->context);
507 WINPR_ASSERT(clipboard->context->ClientFormatDataResponse);
508 return clipboard->context->ClientFormatDataResponse(clipboard->context, &response);
511 static wStream* xf_cliprdr_serialize_server_format_list(xfClipboard* clipboard)
513 UINT32 formatCount = 0;
516 WINPR_ASSERT(clipboard);
519 if (!(s = Stream_New(NULL, 128)))
521 WLog_ERR(TAG,
"failed to allocate serialized format list");
526 formatCount = (clipboard->numServerFormats > 0) ? clipboard->numServerFormats - 1 : 0;
527 Stream_Write_UINT32(s, formatCount);
529 for (UINT32 i = 0; i < formatCount; i++)
532 size_t name_length = format->formatName ? strlen(format->formatName) : 0;
534 DEBUG_CLIPRDR(
"server announced 0x%08" PRIx32
" [%s][%s]", format->formatId,
535 ClipboardGetFormatIdString(format->formatId), format->formatName);
536 if (!Stream_EnsureRemainingCapacity(s,
sizeof(UINT32) + name_length + 1))
538 WLog_ERR(TAG,
"failed to expand serialized format list");
542 Stream_Write_UINT32(s, format->formatId);
544 if (format->formatName)
545 Stream_Write(s, format->formatName, name_length);
547 Stream_Write_UINT8(s,
'\0');
550 Stream_SealLength(s);
553 Stream_Free(s, TRUE);
557 static CLIPRDR_FORMAT* xf_cliprdr_parse_server_format_list(BYTE* data,
size_t length,
563 WINPR_ASSERT(data || (length == 0));
564 WINPR_ASSERT(numFormats);
566 if (!(s = Stream_New(data, length)))
568 WLog_ERR(TAG,
"failed to allocate stream for parsing serialized format list");
572 if (!Stream_CheckAndLogRequiredLength(TAG, s,
sizeof(UINT32)))
575 Stream_Read_UINT32(s, *numFormats);
577 if (*numFormats > MAX_CLIPBOARD_FORMATS)
579 WLog_ERR(TAG,
"unexpectedly large number of formats: %" PRIu32
"", *numFormats);
585 WLog_ERR(TAG,
"failed to allocate format list");
589 for (UINT32 i = 0; i < *numFormats; i++)
591 const char* formatName = NULL;
592 size_t formatNameLength = 0;
594 if (!Stream_CheckAndLogRequiredLength(TAG, s,
sizeof(UINT32)))
597 Stream_Read_UINT32(s, formats[i].formatId);
598 formatName = (
const char*)Stream_Pointer(s);
599 formatNameLength = strnlen(formatName, Stream_GetRemainingLength(s));
601 if (formatNameLength == Stream_GetRemainingLength(s))
603 WLog_ERR(TAG,
"missing terminating null byte, %" PRIuz
" bytes left to read",
608 formats[i].formatName = strndup(formatName, formatNameLength);
609 Stream_Seek(s, formatNameLength + 1);
612 Stream_Free(s, FALSE);
615 Stream_Free(s, FALSE);
621 static void xf_cliprdr_free_formats(
CLIPRDR_FORMAT* formats, UINT32 numFormats)
623 WINPR_ASSERT(formats || (numFormats == 0));
625 for (UINT32 i = 0; i < numFormats; i++)
627 free(formats[i].formatName);
633 static CLIPRDR_FORMAT* xf_cliprdr_get_raw_server_formats(xfClipboard* clipboard, UINT32* numFormats)
637 unsigned long length = 0;
638 unsigned long remaining = 0;
641 xfContext* xfc = NULL;
643 WINPR_ASSERT(clipboard);
644 WINPR_ASSERT(numFormats);
646 xfc = clipboard->xfc;
651 Window owner = XGetSelectionOwner(xfc->display, clipboard->clipboard_atom);
652 LogTagAndXGetWindowProperty(TAG, xfc->display, owner, clipboard->raw_format_list_atom, 0, 4096,
653 False, clipboard->raw_format_list_atom, &type, &format, &length,
656 if (data && length > 0 && format == 8 && type == clipboard->raw_format_list_atom)
658 formats = xf_cliprdr_parse_server_format_list(data, length, numFormats);
663 "failed to retrieve raw format list: data=%p, length=%lu, format=%d, type=%lu "
665 (
void*)data, length, format, (
unsigned long)type,
666 (
unsigned long)clipboard->raw_format_list_atom);
675 static BOOL xf_cliprdr_should_add_format(
const CLIPRDR_FORMAT* formats,
size_t count,
676 const xfCliprdrFormat* xformat)
678 WINPR_ASSERT(formats);
683 for (
size_t x = 0; x < count; x++)
686 if (format->formatId == xformat->formatToRequest)
692 static CLIPRDR_FORMAT* xf_cliprdr_get_formats_from_targets(xfClipboard* clipboard,
697 int format_property = 0;
698 unsigned long length = 0;
699 unsigned long bytes_left = 0;
702 WINPR_ASSERT(clipboard);
703 WINPR_ASSERT(numFormats);
705 xfContext* xfc = clipboard->xfc;
709 LogTagAndXGetWindowProperty(TAG, xfc->display, xfc->drawable, clipboard->property_atom, 0, 200,
710 0, XA_ATOM, &atom, &format_property, &length, &bytes_left, &data);
716 WLog_ERR(TAG,
"XGetWindowProperty set length = %lu but data is NULL", length);
722 WLog_ERR(TAG,
"failed to allocate %lu CLIPRDR_FORMAT structs", length);
727 for (
unsigned long i = 0; i < length; i++)
729 Atom tatom = ((Atom*)data)[i];
730 const xfCliprdrFormat* format = xf_cliprdr_get_client_format_by_atom(clipboard, tatom);
732 if (xf_cliprdr_should_add_format(formats, *numFormats, format))
735 cformat->formatId = format->formatToRequest;
736 if (format->formatName)
738 cformat->formatName = _strdup(format->formatName);
739 WINPR_ASSERT(cformat->formatName);
742 cformat->formatName = NULL;
756 static CLIPRDR_FORMAT* xf_cliprdr_get_client_formats(xfClipboard* clipboard, UINT32* numFormats)
760 WINPR_ASSERT(clipboard);
761 WINPR_ASSERT(numFormats);
765 if (xf_cliprdr_is_raw_transfer_available(clipboard))
767 formats = xf_cliprdr_get_raw_server_formats(clipboard, numFormats);
770 if (*numFormats == 0)
772 xf_cliprdr_free_formats(formats, *numFormats);
773 formats = xf_cliprdr_get_formats_from_targets(clipboard, numFormats);
779 static void xf_cliprdr_provide_server_format_list(xfClipboard* clipboard)
782 xfContext* xfc = NULL;
784 WINPR_ASSERT(clipboard);
786 xfc = clipboard->xfc;
789 formats = xf_cliprdr_serialize_server_format_list(clipboard);
793 const size_t len = Stream_Length(formats);
794 WINPR_ASSERT(len <= INT32_MAX);
795 LogTagAndXChangeProperty(TAG, xfc->display, xfc->drawable, clipboard->raw_format_list_atom,
796 clipboard->raw_format_list_atom, 8, PropModeReplace,
797 Stream_Buffer(formats), (
int)len);
801 LogTagAndXDeleteProperty(TAG, xfc->display, xfc->drawable, clipboard->raw_format_list_atom);
804 Stream_Free(formats, TRUE);
812 if (a->formatId != b->formatId)
814 if (!a->formatName && !b->formatName)
816 if (!a->formatName || !b->formatName)
818 return strcmp(a->formatName, b->formatName) == 0;
821 static BOOL xf_clipboard_changed(xfClipboard* clipboard,
const CLIPRDR_FORMAT* formats,
824 WINPR_ASSERT(clipboard);
825 WINPR_ASSERT(formats || (numFormats == 0));
827 if (clipboard->lastSentNumFormats != numFormats)
830 for (UINT32 x = 0; x < numFormats; x++)
833 BOOL contained = FALSE;
834 for (UINT32 y = 0; y < numFormats; y++)
836 if (xf_clipboard_format_equal(cur, &formats[y]))
849 static void xf_clipboard_formats_free(xfClipboard* clipboard)
851 WINPR_ASSERT(clipboard);
853 xf_cliprdr_free_formats(clipboard->lastSentFormats, clipboard->lastSentNumFormats);
854 clipboard->lastSentFormats = NULL;
855 clipboard->lastSentNumFormats = 0;
858 static BOOL xf_clipboard_copy_formats(xfClipboard* clipboard,
const CLIPRDR_FORMAT* formats,
861 WINPR_ASSERT(clipboard);
862 WINPR_ASSERT(formats || (numFormats == 0));
864 xf_clipboard_formats_free(clipboard);
866 clipboard->lastSentFormats = calloc(numFormats,
sizeof(
CLIPRDR_FORMAT));
867 if (!clipboard->lastSentFormats)
869 clipboard->lastSentNumFormats = numFormats;
870 for (UINT32 x = 0; x < numFormats; x++)
876 lcur->formatName = _strdup(cur->formatName);
881 static UINT xf_cliprdr_send_format_list(xfClipboard* clipboard,
const CLIPRDR_FORMAT* formats,
882 UINT32 numFormats, BOOL force)
888 } cnv = { .cpv = formats };
890 .numFormats = numFormats,
892 .common.msgType = CB_FORMAT_LIST };
895 WINPR_ASSERT(clipboard);
896 WINPR_ASSERT(formats || (numFormats == 0));
898 if (!force && !xf_clipboard_changed(clipboard, formats, numFormats))
899 return CHANNEL_RC_OK;
901 #if defined(WITH_DEBUG_CLIPRDR)
902 for (UINT32 x = 0; x < numFormats; x++)
905 DEBUG_CLIPRDR(
"announcing format 0x%08" PRIx32
" [%s] [%s]", format->formatId,
906 ClipboardGetFormatIdString(format->formatId), format->formatName);
910 xf_clipboard_copy_formats(clipboard, formats, numFormats);
912 xf_cliprdr_send_data_response(clipboard, NULL, NULL, 0);
914 xf_cliprdr_clear_cached_data(clipboard);
916 ret = cliprdr_file_context_notify_new_client_format_list(clipboard->file);
920 WINPR_ASSERT(clipboard->context);
921 WINPR_ASSERT(clipboard->context->ClientFormatList);
922 return clipboard->context->ClientFormatList(clipboard->context, &formatList);
925 static void xf_cliprdr_get_requested_targets(xfClipboard* clipboard)
927 UINT32 numFormats = 0;
928 CLIPRDR_FORMAT* formats = xf_cliprdr_get_client_formats(clipboard, &numFormats);
929 xf_cliprdr_send_format_list(clipboard, formats, numFormats, FALSE);
930 xf_cliprdr_free_formats(formats, numFormats);
933 static void xf_cliprdr_process_requested_data(xfClipboard* clipboard, BOOL hasData,
934 const BYTE* data,
size_t size)
939 INT64 srcFormatId = -1;
940 BYTE* pDstData = NULL;
941 const xfCliprdrFormat* format = NULL;
943 WINPR_ASSERT(clipboard);
945 if (clipboard->incr_starts && hasData)
948 format = xf_cliprdr_get_client_format_by_id(clipboard, clipboard->requestedFormatId);
950 if (!hasData || !data || !format)
952 xf_cliprdr_send_data_response(clipboard, format, NULL, 0);
956 switch (format->formatToRequest)
959 srcFormatId = CF_RAW;
965 size = strlen((
const char*)data) + 1;
966 srcFormatId = format->localFormat;
970 srcFormatId = format->localFormat;
976 xf_cliprdr_send_data_response(clipboard, format, NULL, 0);
980 ClipboardLock(clipboard->system);
981 SrcSize = (UINT32)size;
982 bSuccess = ClipboardSetData(clipboard->system, (UINT32)srcFormatId, data, SrcSize);
987 pDstData = (BYTE*)ClipboardGetData(clipboard->system, format->formatToRequest, &DstSize);
989 ClipboardUnlock(clipboard->system);
993 xf_cliprdr_send_data_response(clipboard, format, NULL, 0);
1004 ClipboardLock(clipboard->system);
1005 if (format->formatToRequest &&
1006 (format->formatToRequest ==
1007 ClipboardGetFormatId(clipboard->system, type_FileGroupDescriptorW)))
1009 UINT error = NO_ERROR;
1015 const UINT32 flags = cliprdr_file_context_remote_get_flags(clipboard->file);
1016 error = cliprdr_serialize_file_list_ex(flags, file_array, file_count, &pDstData, &DstSize);
1019 WLog_ERR(TAG,
"failed to serialize CLIPRDR_FILELIST: 0x%08X", error);
1022 UINT32 formatId = ClipboardGetFormatId(clipboard->system, mime_uri_list);
1023 UINT32 url_size = 0;
1025 char* url = ClipboardGetData(clipboard->system, formatId, &url_size);
1026 cliprdr_file_context_update_client_data(clipboard->file, url, url_size);
1032 ClipboardUnlock(clipboard->system);
1034 xf_cliprdr_send_data_response(clipboard, format, pDstData, DstSize);
1038 static BOOL xf_add_input_flags(xfClipboard* clipboard,
long mask)
1040 WINPR_ASSERT(clipboard);
1042 xfContext* xfc = clipboard->xfc;
1045 XWindowAttributes attr = { 0 };
1046 XGetWindowAttributes(xfc->display, xfc->drawable, &attr);
1047 if ((attr.all_event_masks & mask) == 0)
1048 clipboard->event_mask = attr.all_event_masks;
1050 XSelectInput(xfc->display, xfc->drawable, attr.all_event_masks | mask);
1054 static BOOL xf_restore_input_flags(xfClipboard* clipboard)
1056 WINPR_ASSERT(clipboard);
1058 xfContext* xfc = clipboard->xfc;
1061 if (clipboard->event_mask != 0)
1063 XSelectInput(xfc->display, xfc->drawable, clipboard->event_mask);
1064 clipboard->event_mask = 0;
1069 static BOOL xf_cliprdr_get_requested_data(xfClipboard* clipboard, Atom target)
1073 BOOL has_data = FALSE;
1074 int format_property = 0;
1075 unsigned long dummy = 0;
1076 unsigned long length = 0;
1077 unsigned long bytes_left = 0;
1078 const xfCliprdrFormat* format = NULL;
1079 xfContext* xfc = NULL;
1081 WINPR_ASSERT(clipboard);
1083 xfc = clipboard->xfc;
1086 format = xf_cliprdr_get_client_format_by_id(clipboard, clipboard->requestedFormatId);
1088 if (!format || (format->atom != target))
1090 xf_cliprdr_send_data_response(clipboard, format, NULL, 0);
1094 LogTagAndXGetWindowProperty(TAG, xfc->display, xfc->drawable, clipboard->property_atom, 0, 0, 0,
1095 target, &type, &format_property, &length, &bytes_left, &data);
1103 if (bytes_left <= 0 && !clipboard->incr_starts)
1106 else if (type == clipboard->incr_atom)
1108 clipboard->incr_starts = TRUE;
1110 if (clipboard->incr_data)
1112 free(clipboard->incr_data);
1113 clipboard->incr_data = NULL;
1116 clipboard->incr_data_length = 0;
1118 xf_add_input_flags(clipboard, PropertyChangeMask);
1122 if (bytes_left <= 0)
1125 data = clipboard->incr_data;
1126 clipboard->incr_data = NULL;
1127 bytes_left = clipboard->incr_data_length;
1128 clipboard->incr_data_length = 0;
1129 clipboard->incr_starts = FALSE;
1132 xf_restore_input_flags(clipboard);
1136 else if (LogTagAndXGetWindowProperty(
1137 TAG, xfc->display, xfc->drawable, clipboard->property_atom, 0, bytes_left, 0,
1138 target, &type, &format_property, &length, &dummy, &data) == Success)
1140 if (clipboard->incr_starts)
1142 BYTE* new_data = NULL;
1143 bytes_left = length * format_property / 8;
1145 (BYTE*)realloc(clipboard->incr_data, clipboard->incr_data_length + bytes_left);
1150 clipboard->incr_data = new_data;
1151 CopyMemory(clipboard->incr_data + clipboard->incr_data_length, data,
1153 clipboard->incr_data_length += bytes_left;
1166 LogTagAndXDeleteProperty(TAG, xfc->display, xfc->drawable, clipboard->property_atom);
1167 xf_cliprdr_process_requested_data(clipboard, has_data, data, bytes_left);
1175 static void xf_cliprdr_append_target(xfClipboard* clipboard, Atom target)
1177 WINPR_ASSERT(clipboard);
1179 if (clipboard->numTargets >= ARRAYSIZE(clipboard->targets))
1182 for (
size_t i = 0; i < clipboard->numTargets; i++)
1184 if (clipboard->targets[i] == target)
1188 clipboard->targets[clipboard->numTargets++] = target;
1191 static void xf_cliprdr_provide_targets(xfClipboard* clipboard,
const XSelectionEvent* respond)
1193 xfContext* xfc = NULL;
1195 WINPR_ASSERT(clipboard);
1197 xfc = clipboard->xfc;
1200 if (respond->property != None)
1202 WINPR_ASSERT(clipboard->numTargets <= INT32_MAX);
1203 LogTagAndXChangeProperty(TAG, xfc->display, respond->requestor, respond->property, XA_ATOM,
1204 32, PropModeReplace, (BYTE*)clipboard->targets,
1205 (
int)clipboard->numTargets);
1209 static void xf_cliprdr_provide_timestamp(xfClipboard* clipboard,
const XSelectionEvent* respond)
1211 xfContext* xfc = NULL;
1213 WINPR_ASSERT(clipboard);
1215 xfc = clipboard->xfc;
1218 if (respond->property != None)
1220 LogTagAndXChangeProperty(TAG, xfc->display, respond->requestor, respond->property,
1221 XA_INTEGER, 32, PropModeReplace,
1222 (BYTE*)&clipboard->selection_ownership_timestamp, 1);
1226 static void xf_cliprdr_provide_data(xfClipboard* clipboard,
const XSelectionEvent* respond,
1227 const BYTE* data, UINT32 size)
1229 xfContext* xfc = NULL;
1231 WINPR_ASSERT(clipboard);
1233 xfc = clipboard->xfc;
1236 if (respond->property != None)
1238 LogTagAndXChangeProperty(TAG, xfc->display, respond->requestor, respond->property,
1239 respond->target, 8, PropModeReplace, data, size);
1243 static void log_selection_event(xfContext* xfc,
const XEvent* event)
1245 const DWORD level = WLOG_TRACE;
1246 static wLog* _log_cached_ptr = NULL;
1247 if (!_log_cached_ptr)
1248 _log_cached_ptr = WLog_Get(TAG);
1249 if (WLog_IsLevelActive(_log_cached_ptr, level))
1252 switch (event->type)
1254 case SelectionClear:
1256 const XSelectionClearEvent* xevent = &
event->xselectionclear;
1258 Safe_XGetAtomName(_log_cached_ptr, xfc->display, xevent->selection);
1259 WLog_Print(_log_cached_ptr, level,
"got event %s [selection %s]",
1260 x11_event_string(event->type), selection);
1264 case SelectionNotify:
1266 const XSelectionEvent* xevent = &
event->xselection;
1268 Safe_XGetAtomName(_log_cached_ptr, xfc->display, xevent->selection);
1269 char* target = Safe_XGetAtomName(_log_cached_ptr, xfc->display, xevent->target);
1270 char*
property = Safe_XGetAtomName(_log_cached_ptr, xfc->display, xevent->property);
1271 WLog_Print(_log_cached_ptr, level,
1272 "got event %s [selection %s, target %s, property %s]",
1273 x11_event_string(event->type), selection, target, property);
1279 case SelectionRequest:
1281 const XSelectionRequestEvent* xevent = &
event->xselectionrequest;
1283 Safe_XGetAtomName(_log_cached_ptr, xfc->display, xevent->selection);
1284 char* target = Safe_XGetAtomName(_log_cached_ptr, xfc->display, xevent->target);
1285 char*
property = Safe_XGetAtomName(_log_cached_ptr, xfc->display, xevent->property);
1286 WLog_Print(_log_cached_ptr, level,
1287 "got event %s [selection %s, target %s, property %s]",
1288 x11_event_string(event->type), selection, target, property);
1294 case PropertyNotify:
1296 const XPropertyEvent* xevent = &
event->xproperty;
1297 char* atom = Safe_XGetAtomName(_log_cached_ptr, xfc->display, xevent->atom);
1298 WLog_Print(_log_cached_ptr, level,
"got event %s [atom %s]",
1299 x11_event_string(event->type), atom);
1309 static BOOL xf_cliprdr_process_selection_notify(xfClipboard* clipboard,
1310 const XSelectionEvent* xevent)
1312 WINPR_ASSERT(clipboard);
1313 WINPR_ASSERT(xevent);
1315 if (xevent->target == clipboard->targets[1])
1317 if (xevent->property == None)
1319 xf_cliprdr_send_client_format_list(clipboard, FALSE);
1323 xf_cliprdr_get_requested_targets(clipboard);
1330 return xf_cliprdr_get_requested_data(clipboard, xevent->target);
1334 void xf_cliprdr_clear_cached_data(xfClipboard* clipboard)
1336 WINPR_ASSERT(clipboard);
1338 ClipboardLock(clipboard->system);
1339 ClipboardEmpty(clipboard->system);
1341 HashTable_Clear(clipboard->cachedData);
1342 HashTable_Clear(clipboard->cachedRawData);
1344 cliprdr_file_context_clear(clipboard->file);
1345 ClipboardUnlock(clipboard->system);
1348 static void* format_to_cache_slot(UINT32 format)
1355 cnv.uptr = 0x100000000ULL + format;
1359 static UINT32 get_dst_format_id_for_local_request(xfClipboard* clipboard,
1360 const xfCliprdrFormat* format)
1362 UINT32 dstFormatId = 0;
1364 WINPR_ASSERT(format);
1366 if (!format->formatName)
1367 return format->localFormat;
1369 ClipboardLock(clipboard->system);
1370 if (strcmp(format->formatName, type_HtmlFormat) == 0)
1371 dstFormatId = ClipboardGetFormatId(clipboard->system, mime_html);
1372 ClipboardUnlock(clipboard->system);
1374 if (strcmp(format->formatName, type_FileGroupDescriptorW) == 0)
1375 dstFormatId = format->localFormat;
1380 static void get_src_format_info_for_local_request(xfClipboard* clipboard,
1381 const xfCliprdrFormat* format,
1382 UINT32* srcFormatId, BOOL* nullTerminated)
1385 *nullTerminated = FALSE;
1387 if (format->formatName)
1389 ClipboardLock(clipboard->system);
1390 if (strcmp(format->formatName, type_HtmlFormat) == 0)
1392 *srcFormatId = ClipboardGetFormatId(clipboard->system, type_HtmlFormat);
1393 *nullTerminated = TRUE;
1395 else if (strcmp(format->formatName, type_FileGroupDescriptorW) == 0)
1397 *srcFormatId = ClipboardGetFormatId(clipboard->system, type_FileGroupDescriptorW);
1398 *nullTerminated = TRUE;
1400 ClipboardUnlock(clipboard->system);
1404 *srcFormatId = format->formatToRequest;
1405 switch (format->formatToRequest)
1409 case CF_UNICODETEXT:
1410 *nullTerminated = TRUE;
1413 *srcFormatId = CF_DIB;
1416 *srcFormatId = CF_TIFF;
1424 static xfCachedData* convert_data_from_existing_raw_data(xfClipboard* clipboard,
1425 xfCachedData* cached_raw_data,
1426 UINT32 srcFormatId, BOOL nullTerminated,
1429 xfCachedData* cached_data = NULL;
1431 BYTE* dst_data = NULL;
1432 UINT32 dst_size = 0;
1434 WINPR_ASSERT(clipboard);
1435 WINPR_ASSERT(cached_raw_data);
1436 WINPR_ASSERT(cached_raw_data->data);
1438 ClipboardLock(clipboard->system);
1439 success = ClipboardSetData(clipboard->system, srcFormatId, cached_raw_data->data,
1440 cached_raw_data->data_length);
1443 WLog_WARN(TAG,
"Failed to set clipboard data (formatId: %u, data: %p, data_length: %u)",
1444 srcFormatId, cached_raw_data->data, cached_raw_data->data_length);
1445 ClipboardUnlock(clipboard->system);
1449 dst_data = ClipboardGetData(clipboard->system, dstFormatId, &dst_size);
1452 WLog_WARN(TAG,
"Failed to get converted clipboard data");
1453 ClipboardUnlock(clipboard->system);
1456 ClipboardUnlock(clipboard->system);
1460 BYTE* nullTerminator = memchr(dst_data,
'\0', dst_size);
1463 const intptr_t diff = nullTerminator - dst_data;
1464 WINPR_ASSERT(diff >= 0);
1465 WINPR_ASSERT(diff <= UINT32_MAX);
1466 dst_size = (UINT32)diff;
1470 cached_data = xf_cached_data_new(dst_data, dst_size);
1473 WLog_WARN(TAG,
"Failed to allocate cache entry");
1478 if (!HashTable_Insert(clipboard->cachedData, format_to_cache_slot(dstFormatId), cached_data))
1480 WLog_WARN(TAG,
"Failed to cache clipboard data");
1481 xf_cached_data_free(cached_data);
1488 static BOOL xf_cliprdr_process_selection_request(xfClipboard* clipboard,
1489 const XSelectionRequestEvent* xevent)
1493 UINT32 formatId = 0;
1494 XSelectionEvent* respond = NULL;
1496 BOOL delayRespond = 0;
1497 BOOL rawTransfer = 0;
1498 unsigned long length = 0;
1499 unsigned long bytes_left = 0;
1500 xfContext* xfc = NULL;
1502 WINPR_ASSERT(clipboard);
1503 WINPR_ASSERT(xevent);
1505 xfc = clipboard->xfc;
1508 if (xevent->owner != xfc->drawable)
1511 delayRespond = FALSE;
1513 if (!(respond = (XSelectionEvent*)calloc(1,
sizeof(XSelectionEvent))))
1515 WLog_ERR(TAG,
"failed to allocate XEvent data");
1519 respond->property = None;
1520 respond->type = SelectionNotify;
1521 respond->display = xevent->display;
1522 respond->requestor = xevent->requestor;
1523 respond->selection = xevent->selection;
1524 respond->target = xevent->target;
1525 respond->time = xevent->time;
1527 if (xevent->target == clipboard->targets[0])
1530 respond->property = xevent->property;
1531 xf_cliprdr_provide_timestamp(clipboard, respond);
1533 else if (xevent->target == clipboard->targets[1])
1536 respond->property = xevent->property;
1537 xf_cliprdr_provide_targets(clipboard, respond);
1542 xf_cliprdr_get_server_format_by_atom(clipboard, xevent->target);
1543 const xfCliprdrFormat* cformat =
1544 xf_cliprdr_get_client_format_by_atom(clipboard, xevent->target);
1546 if (format && (xevent->requestor != xfc->drawable))
1548 formatId = format->formatId;
1549 rawTransfer = FALSE;
1550 xfCachedData* cached_data = NULL;
1551 UINT32 dstFormatId = 0;
1553 if (formatId == CF_RAW)
1555 if (LogTagAndXGetWindowProperty(
1556 TAG, xfc->display, xevent->requestor, clipboard->property_atom, 0, 4, 0,
1557 XA_INTEGER, &type, &fmt, &length, &bytes_left, &data) != Success)
1564 CopyMemory(&formatId, data, 4);
1569 dstFormatId = get_dst_format_id_for_local_request(clipboard, cformat);
1570 DEBUG_CLIPRDR(
"formatId: %u, dstFormatId: %u", formatId, dstFormatId);
1573 cached_data = HashTable_GetItemValue(clipboard->cachedData,
1574 format_to_cache_slot(dstFormatId));
1576 cached_data = HashTable_GetItemValue(clipboard->cachedRawData,
1577 format_to_cache_slot(formatId));
1579 DEBUG_CLIPRDR(
"hasCachedData: %u, rawTransfer: %u", cached_data ? 1 : 0, rawTransfer);
1581 if (!cached_data && !rawTransfer)
1583 UINT32 srcFormatId = 0;
1584 BOOL nullTerminated = FALSE;
1585 xfCachedData* cached_raw_data = NULL;
1587 get_src_format_info_for_local_request(clipboard, cformat, &srcFormatId,
1590 HashTable_GetItemValue(clipboard->cachedRawData, (
void*)(UINT_PTR)srcFormatId);
1592 DEBUG_CLIPRDR(
"hasCachedRawData: %u, rawDataLength: %u", cached_raw_data ? 1 : 0,
1593 cached_raw_data ? cached_raw_data->data_length : 0);
1595 if (cached_raw_data && cached_raw_data->data_length != 0)
1596 cached_data = convert_data_from_existing_raw_data(
1597 clipboard, cached_raw_data, srcFormatId, nullTerminated, dstFormatId);
1600 DEBUG_CLIPRDR(
"hasCachedData: %u", cached_data ? 1 : 0);
1605 respond->property = xevent->property;
1608 xf_cliprdr_provide_data(clipboard, respond, cached_data->data,
1609 cached_data->data_length);
1611 else if (clipboard->respond)
1617 WINPR_ASSERT(cformat);
1623 respond->property = xevent->property;
1624 clipboard->respond = respond;
1625 requested_format_replace(&clipboard->requestedFormat, formatId,
1626 cformat->formatName);
1627 clipboard->data_raw_format = rawTransfer;
1628 delayRespond = TRUE;
1629 xf_cliprdr_send_data_request(clipboard, formatId, cformat);
1639 XSelectionEvent* sev;
1643 XSendEvent(xfc->display, xevent->requestor, 0, 0, conv.ev);
1644 XFlush(xfc->display);
1651 static BOOL xf_cliprdr_process_selection_clear(xfClipboard* clipboard,
1652 const XSelectionClearEvent* xevent)
1654 xfContext* xfc = NULL;
1656 WINPR_ASSERT(clipboard);
1657 WINPR_ASSERT(xevent);
1659 xfc = clipboard->xfc;
1662 WINPR_UNUSED(xevent);
1664 if (xf_cliprdr_is_self_owned(clipboard))
1667 LogTagAndXDeleteProperty(TAG, xfc->display, clipboard->root_window, clipboard->property_atom);
1671 static BOOL xf_cliprdr_process_property_notify(xfClipboard* clipboard,
const XPropertyEvent* xevent)
1673 const xfCliprdrFormat* format = NULL;
1674 xfContext* xfc = NULL;
1679 xfc = clipboard->xfc;
1681 WINPR_ASSERT(xevent);
1683 if (xevent->atom == clipboard->timestamp_property_atom)
1689 xf_cliprdr_set_selection_owner(xfc, clipboard, xevent->time);
1693 if (xevent->atom != clipboard->property_atom)
1696 if (xevent->window == clipboard->root_window)
1698 xf_cliprdr_send_client_format_list(clipboard, FALSE);
1700 else if ((xevent->window == xfc->drawable) && (xevent->state == PropertyNewValue) &&
1701 clipboard->incr_starts)
1703 format = xf_cliprdr_get_client_format_by_id(clipboard, clipboard->requestedFormatId);
1706 xf_cliprdr_get_requested_data(clipboard, format->atom);
1712 void xf_cliprdr_handle_xevent(xfContext* xfc,
const XEvent* event)
1714 xfClipboard* clipboard = NULL;
1719 clipboard = xfc->clipboard;
1726 if (clipboard->xfixes_supported &&
1727 event->type == XFixesSelectionNotify + clipboard->xfixes_event_base)
1729 const XFixesSelectionNotifyEvent* se = (
const XFixesSelectionNotifyEvent*)event;
1731 if (se->subtype == XFixesSetSelectionOwnerNotify)
1733 if (se->selection != clipboard->clipboard_atom)
1736 if (XGetSelectionOwner(xfc->display, se->selection) == xfc->drawable)
1739 clipboard->owner = None;
1740 xf_cliprdr_check_owner(clipboard);
1748 switch (event->type)
1750 case SelectionNotify:
1751 log_selection_event(xfc, event);
1752 xf_cliprdr_process_selection_notify(clipboard, &event->xselection);
1755 case SelectionRequest:
1756 log_selection_event(xfc, event);
1757 xf_cliprdr_process_selection_request(clipboard, &event->xselectionrequest);
1760 case SelectionClear:
1761 log_selection_event(xfc, event);
1762 xf_cliprdr_process_selection_clear(clipboard, &event->xselectionclear);
1765 case PropertyNotify:
1766 log_selection_event(xfc, event);
1767 xf_cliprdr_process_property_notify(clipboard, &event->xproperty);
1771 if (!clipboard->xfixes_supported)
1773 xf_cliprdr_check_owner(clipboard);
1787 static UINT xf_cliprdr_send_client_capabilities(xfClipboard* clipboard)
1792 WINPR_ASSERT(clipboard);
1794 capabilities.cCapabilitiesSets = 1;
1796 generalCapabilitySet.capabilitySetType = CB_CAPSTYPE_GENERAL;
1797 generalCapabilitySet.capabilitySetLength = 12;
1798 generalCapabilitySet.version = CB_CAPS_VERSION_2;
1799 generalCapabilitySet.generalFlags = CB_USE_LONG_FORMAT_NAMES;
1801 WINPR_ASSERT(clipboard);
1802 generalCapabilitySet.generalFlags |= cliprdr_file_context_current_flags(clipboard->file);
1804 WINPR_ASSERT(clipboard->context);
1805 WINPR_ASSERT(clipboard->context->ClientCapabilities);
1806 return clipboard->context->ClientCapabilities(clipboard->context, &capabilities);
1814 static UINT xf_cliprdr_send_client_format_list(xfClipboard* clipboard, BOOL force)
1816 WINPR_ASSERT(clipboard);
1818 xfContext* xfc = clipboard->xfc;
1821 UINT32 numFormats = 0;
1822 CLIPRDR_FORMAT* formats = xf_cliprdr_get_client_formats(clipboard, &numFormats);
1824 const UINT ret = xf_cliprdr_send_format_list(clipboard, formats, numFormats, force);
1826 if (clipboard->owner && clipboard->owner != xfc->drawable)
1829 LogTagAndXConvertSelection(TAG, xfc->display, clipboard->clipboard_atom,
1830 clipboard->targets[1], clipboard->property_atom, xfc->drawable,
1834 xf_cliprdr_free_formats(formats, numFormats);
1844 static UINT xf_cliprdr_send_client_format_list_response(xfClipboard* clipboard, BOOL status)
1848 formatListResponse.common.msgType = CB_FORMAT_LIST_RESPONSE;
1849 formatListResponse.common.msgFlags = status ? CB_RESPONSE_OK : CB_RESPONSE_FAIL;
1850 formatListResponse.common.dataLen = 0;
1852 WINPR_ASSERT(clipboard);
1853 WINPR_ASSERT(clipboard->context);
1854 WINPR_ASSERT(clipboard->context->ClientFormatListResponse);
1855 return clipboard->context->ClientFormatListResponse(clipboard->context, &formatListResponse);
1863 static UINT xf_cliprdr_monitor_ready(CliprdrClientContext* context,
1867 xfClipboard* clipboard = NULL;
1869 WINPR_ASSERT(context);
1870 WINPR_ASSERT(monitorReady);
1872 clipboard = cliprdr_file_context_get_context(context->custom);
1873 WINPR_ASSERT(clipboard);
1875 WINPR_UNUSED(monitorReady);
1877 if ((ret = xf_cliprdr_send_client_capabilities(clipboard)) != CHANNEL_RC_OK)
1880 xf_clipboard_formats_free(clipboard);
1882 if ((ret = xf_cliprdr_send_client_format_list(clipboard, TRUE)) != CHANNEL_RC_OK)
1885 clipboard->sync = TRUE;
1886 return CHANNEL_RC_OK;
1894 static UINT xf_cliprdr_server_capabilities(CliprdrClientContext* context,
1898 const BYTE* capsPtr = NULL;
1899 xfClipboard* clipboard = NULL;
1901 WINPR_ASSERT(context);
1902 WINPR_ASSERT(capabilities);
1904 clipboard = cliprdr_file_context_get_context(context->custom);
1905 WINPR_ASSERT(clipboard);
1907 capsPtr = (
const BYTE*)capabilities->capabilitySets;
1908 WINPR_ASSERT(capsPtr);
1910 cliprdr_file_context_remote_set_flags(clipboard->file, 0);
1912 for (UINT32 i = 0; i < capabilities->cCapabilitiesSets; i++)
1916 if (caps->capabilitySetType == CB_CAPSTYPE_GENERAL)
1920 cliprdr_file_context_remote_set_flags(clipboard->file, generalCaps->generalFlags);
1923 capsPtr += caps->capabilitySetLength;
1926 return CHANNEL_RC_OK;
1929 static void xf_cliprdr_prepare_to_set_selection_owner(xfContext* xfc, xfClipboard* clipboard)
1932 WINPR_ASSERT(clipboard);
1949 Atom value = clipboard->timestamp_property_atom;
1951 LogTagAndXChangeProperty(TAG, xfc->display, xfc->drawable, clipboard->timestamp_property_atom,
1952 XA_ATOM, 32, PropModeReplace, (BYTE*)&value, 1);
1953 XFlush(xfc->display);
1956 static void xf_cliprdr_set_selection_owner(xfContext* xfc, xfClipboard* clipboard, Time timestamp)
1959 WINPR_ASSERT(clipboard);
1965 clipboard->selection_ownership_timestamp = timestamp;
1966 XSetSelectionOwner(xfc->display, clipboard->clipboard_atom, xfc->drawable, timestamp);
1967 XFlush(xfc->display);
1975 static UINT xf_cliprdr_server_format_list(CliprdrClientContext* context,
1978 xfContext* xfc = NULL;
1980 xfClipboard* clipboard = NULL;
1982 WINPR_ASSERT(context);
1983 WINPR_ASSERT(formatList);
1985 clipboard = cliprdr_file_context_get_context(context->custom);
1986 WINPR_ASSERT(clipboard);
1988 xfc = clipboard->xfc;
1992 free(clipboard->respond);
1993 clipboard->respond = NULL;
1995 xf_clipboard_formats_free(clipboard);
1996 xf_cliprdr_clear_cached_data(clipboard);
1997 requested_format_free(&clipboard->requestedFormat);
1999 xf_clipboard_free_server_formats(clipboard);
2001 clipboard->numServerFormats = formatList->numFormats + 1;
2003 if (!(clipboard->serverFormats =
2006 WLog_ERR(TAG,
"failed to allocate %d CLIPRDR_FORMAT structs", clipboard->numServerFormats);
2007 return CHANNEL_RC_NO_MEMORY;
2010 for (
size_t i = 0; i < formatList->numFormats; i++)
2015 srvFormat->formatId = format->formatId;
2017 if (format->formatName)
2019 srvFormat->formatName = _strdup(format->formatName);
2021 if (!srvFormat->formatName)
2023 for (UINT32 k = 0; k < i; k++)
2024 free(clipboard->serverFormats[k].formatName);
2026 clipboard->numServerFormats = 0;
2027 free(clipboard->serverFormats);
2028 clipboard->serverFormats = NULL;
2029 return CHANNEL_RC_NO_MEMORY;
2034 ret = cliprdr_file_context_notify_new_server_format_list(clipboard->file);
2040 CLIPRDR_FORMAT* format = &clipboard->serverFormats[formatList->numFormats];
2041 format->formatId = CF_RAW;
2042 format->formatName = NULL;
2044 xf_cliprdr_provide_server_format_list(clipboard);
2045 clipboard->numTargets = 2;
2047 for (
size_t i = 0; i < formatList->numFormats; i++)
2051 for (
size_t j = 0; j < clipboard->numClientFormats; j++)
2053 const xfCliprdrFormat* clientFormat = &clipboard->clientFormats[j];
2054 if (xf_cliprdr_formats_equal(format, clientFormat))
2056 if ((clientFormat->formatName != NULL) &&
2057 (strcmp(type_FileGroupDescriptorW, clientFormat->formatName) == 0))
2059 if (!cliprdr_file_context_has_local_support(clipboard->file))
2062 xf_cliprdr_append_target(clipboard, clientFormat->atom);
2067 ret = xf_cliprdr_send_client_format_list_response(clipboard, TRUE);
2068 if (xfc->remote_app)
2069 xf_cliprdr_set_selection_owner(xfc, clipboard, CurrentTime);
2071 xf_cliprdr_prepare_to_set_selection_owner(xfc, clipboard);
2081 xf_cliprdr_server_format_list_response(CliprdrClientContext* context,
2084 WINPR_ASSERT(context);
2085 WINPR_ASSERT(formatListResponse);
2087 return CHANNEL_RC_OK;
2096 xf_cliprdr_server_format_data_request(CliprdrClientContext* context,
2099 BOOL rawTransfer = 0;
2100 const xfCliprdrFormat* format = NULL;
2101 UINT32 formatId = 0;
2102 xfContext* xfc = NULL;
2103 xfClipboard* clipboard = NULL;
2105 WINPR_ASSERT(context);
2106 WINPR_ASSERT(formatDataRequest);
2108 clipboard = cliprdr_file_context_get_context(context->custom);
2109 WINPR_ASSERT(clipboard);
2111 xfc = clipboard->xfc;
2114 formatId = formatDataRequest->requestedFormatId;
2116 rawTransfer = xf_cliprdr_is_raw_transfer_available(clipboard);
2120 format = xf_cliprdr_get_client_format_by_id(clipboard, CF_RAW);
2121 LogTagAndXChangeProperty(TAG, xfc->display, xfc->drawable, clipboard->property_atom,
2122 XA_INTEGER, 32, PropModeReplace, (BYTE*)&formatId, 1);
2125 format = xf_cliprdr_get_client_format_by_id(clipboard, formatId);
2127 clipboard->requestedFormatId = rawTransfer ? CF_RAW : formatId;
2129 return xf_cliprdr_send_data_response(clipboard, format, NULL, 0);
2131 DEBUG_CLIPRDR(
"requested format 0x%08" PRIx32
" [%s] {local 0x%08" PRIx32
"} [%s]",
2132 format->formatToRequest, ClipboardGetFormatIdString(format->formatToRequest),
2133 format->localFormat, format->formatName);
2134 LogTagAndXConvertSelection(TAG, xfc->display, clipboard->clipboard_atom, format->atom,
2135 clipboard->property_atom, xfc->drawable, CurrentTime);
2136 XFlush(xfc->display);
2138 return CHANNEL_RC_OK;
2147 xf_cliprdr_server_format_data_response(CliprdrClientContext* context,
2151 BYTE* pDstData = NULL;
2154 UINT32 srcFormatId = 0;
2155 UINT32 dstFormatId = 0;
2156 BOOL nullTerminated = FALSE;
2158 const BYTE* data = NULL;
2159 xfContext* xfc = NULL;
2160 xfClipboard* clipboard = NULL;
2161 xfCachedData* cached_data = NULL;
2163 WINPR_ASSERT(context);
2164 WINPR_ASSERT(formatDataResponse);
2166 clipboard = cliprdr_file_context_get_context(context->custom);
2167 WINPR_ASSERT(clipboard);
2169 xfc = clipboard->xfc;
2172 size = formatDataResponse->common.dataLen;
2173 data = formatDataResponse->requestedFormatData;
2175 if (formatDataResponse->common.msgFlags == CB_RESPONSE_FAIL)
2177 WLog_WARN(TAG,
"Format Data Response PDU msgFlags is CB_RESPONSE_FAIL");
2178 free(clipboard->respond);
2179 clipboard->respond = NULL;
2180 return CHANNEL_RC_OK;
2183 if (!clipboard->respond)
2184 return CHANNEL_RC_OK;
2191 const RequestedFormat* format = clipboard->requestedFormat;
2192 if (clipboard->data_raw_format)
2194 srcFormatId = CF_RAW;
2195 dstFormatId = CF_RAW;
2198 return ERROR_INTERNAL_ERROR;
2199 else if (format->formatName)
2201 ClipboardLock(clipboard->system);
2202 if (strcmp(format->formatName, type_HtmlFormat) == 0)
2204 srcFormatId = ClipboardGetFormatId(clipboard->system, type_HtmlFormat);
2205 dstFormatId = ClipboardGetFormatId(clipboard->system, mime_html);
2206 nullTerminated = TRUE;
2209 if (strcmp(format->formatName, type_FileGroupDescriptorW) == 0)
2211 if (!cliprdr_file_context_update_server_data(clipboard->file, clipboard->system, data,
2213 WLog_WARN(TAG,
"failed to update file descriptors");
2215 srcFormatId = ClipboardGetFormatId(clipboard->system, type_FileGroupDescriptorW);
2216 const xfCliprdrFormat* dstTargetFormat =
2217 xf_cliprdr_get_client_format_by_atom(clipboard, clipboard->respond->target);
2218 if (!dstTargetFormat)
2220 dstFormatId = ClipboardGetFormatId(clipboard->system, mime_uri_list);
2224 dstFormatId = dstTargetFormat->localFormat;
2227 nullTerminated = TRUE;
2229 ClipboardUnlock(clipboard->system);
2233 srcFormatId = format->formatToRequest;
2234 dstFormatId = format->localFormat;
2235 switch (format->formatToRequest)
2238 nullTerminated = TRUE;
2242 nullTerminated = TRUE;
2245 case CF_UNICODETEXT:
2246 nullTerminated = TRUE;
2250 srcFormatId = CF_DIB;
2254 srcFormatId = CF_TIFF;
2262 DEBUG_CLIPRDR(
"requested format 0x%08" PRIx32
" [%s] {local 0x%08" PRIx32
"} [%s]",
2263 format->formatToRequest, ClipboardGetFormatIdString(format->formatToRequest),
2264 format->localFormat, format->formatName);
2267 DEBUG_CLIPRDR(
"srcFormatId: %u, dstFormatId: %u", srcFormatId, dstFormatId);
2269 ClipboardLock(clipboard->system);
2270 bSuccess = ClipboardSetData(clipboard->system, srcFormatId, data, SrcSize);
2272 BOOL willQuit = FALSE;
2277 WLog_DBG(TAG,
"skipping, empty data detected!");
2278 free(clipboard->respond);
2279 clipboard->respond = NULL;
2284 pDstData = (BYTE*)ClipboardGetData(clipboard->system, dstFormatId, &DstSize);
2288 WLog_WARN(TAG,
"failed to get clipboard data in format %s [source format %s]",
2289 ClipboardGetFormatName(clipboard->system, dstFormatId),
2290 ClipboardGetFormatName(clipboard->system, srcFormatId));
2293 if (nullTerminated && pDstData)
2295 BYTE* nullTerminator = memchr(pDstData,
'\0', DstSize);
2298 const intptr_t diff = nullTerminator - pDstData;
2299 WINPR_ASSERT(diff >= 0);
2300 WINPR_ASSERT(diff <= UINT32_MAX);
2301 DstSize = (UINT32)diff;
2306 ClipboardUnlock(clipboard->system);
2308 return CHANNEL_RC_OK;
2314 cached_data = xf_cached_data_new(pDstData, DstSize);
2317 WLog_WARN(TAG,
"Failed to allocate cache entry");
2319 return CHANNEL_RC_OK;
2321 if (!HashTable_Insert(clipboard->cachedData, format_to_cache_slot(dstFormatId),
2324 WLog_WARN(TAG,
"Failed to cache clipboard data");
2325 xf_cached_data_free(cached_data);
2326 return CHANNEL_RC_OK;
2336 xfCachedData* cached_raw_data = xf_cached_data_new_copy(data, size);
2337 if (!cached_raw_data)
2338 WLog_WARN(TAG,
"Failed to allocate cache entry");
2341 if (!HashTable_Insert(clipboard->cachedRawData, (
void*)(UINT_PTR)srcFormatId,
2344 WLog_WARN(TAG,
"Failed to cache clipboard data");
2345 xf_cached_data_free(cached_raw_data);
2352 xf_cliprdr_provide_data(clipboard, clipboard->respond, pDstData, DstSize);
2357 XSelectionEvent* sev;
2360 conv.sev = clipboard->respond;
2362 XSendEvent(xfc->display, clipboard->respond->requestor, 0, 0, conv.ev);
2363 XFlush(xfc->display);
2365 free(clipboard->respond);
2366 clipboard->respond = NULL;
2367 return CHANNEL_RC_OK;
2370 static BOOL xf_cliprdr_is_valid_unix_filename(LPCWSTR filename)
2375 if (filename[0] == L
'\0')
2379 for (
const WCHAR* c = filename; *c; ++c)
2388 xfClipboard* xf_clipboard_new(xfContext* xfc, BOOL relieveFilenameRestriction)
2391 rdpChannels* channels = NULL;
2392 xfClipboard* clipboard = NULL;
2393 const char* selectionAtom = NULL;
2394 xfCliprdrFormat* clientFormat = NULL;
2398 WINPR_ASSERT(xfc->common.context.settings);
2400 if (!(clipboard = (xfClipboard*)calloc(1,
sizeof(xfClipboard))))
2402 WLog_ERR(TAG,
"failed to allocate xfClipboard data");
2406 clipboard->file = cliprdr_file_context_new(clipboard);
2407 if (!clipboard->file)
2410 xfc->clipboard = clipboard;
2411 clipboard->xfc = xfc;
2412 channels = xfc->common.context.channels;
2413 clipboard->channels = channels;
2414 clipboard->system = ClipboardCreate();
2415 clipboard->requestedFormatId = -1;
2416 clipboard->root_window = DefaultRootWindow(xfc->display);
2421 selectionAtom =
"CLIPBOARD";
2423 clipboard->clipboard_atom = Logging_XInternAtom(xfc->log, xfc->display, selectionAtom, FALSE);
2425 if (clipboard->clipboard_atom == None)
2427 WLog_ERR(TAG,
"unable to get %s atom", selectionAtom);
2431 clipboard->timestamp_property_atom =
2432 Logging_XInternAtom(xfc->log, xfc->display,
"_FREERDP_TIMESTAMP_PROPERTY", FALSE);
2433 clipboard->property_atom =
2434 Logging_XInternAtom(xfc->log, xfc->display,
"_FREERDP_CLIPRDR", FALSE);
2435 clipboard->raw_transfer_atom =
2436 Logging_XInternAtom(xfc->log, xfc->display,
"_FREERDP_CLIPRDR_RAW", FALSE);
2437 clipboard->raw_format_list_atom =
2438 Logging_XInternAtom(xfc->log, xfc->display,
"_FREERDP_CLIPRDR_FORMATS", FALSE);
2439 xf_cliprdr_set_raw_transfer_enabled(clipboard, TRUE);
2440 XSelectInput(xfc->display, clipboard->root_window, PropertyChangeMask);
2443 if (XFixesQueryExtension(xfc->display, &clipboard->xfixes_event_base,
2444 &clipboard->xfixes_error_base))
2449 if (XFixesQueryVersion(xfc->display, &xfmajor, &xfminor))
2451 XFixesSelectSelectionInput(xfc->display, clipboard->root_window,
2452 clipboard->clipboard_atom,
2453 XFixesSetSelectionOwnerNotifyMask);
2454 clipboard->xfixes_supported = TRUE;
2458 WLog_ERR(TAG,
"Error querying X Fixes extension version");
2463 WLog_ERR(TAG,
"Error loading X Fixes extension");
2469 "Warning: Using clipboard redirection without XFIXES extension is strongly discouraged!");
2471 clientFormat = &clipboard->clientFormats[n++];
2472 clientFormat->atom = Logging_XInternAtom(xfc->log, xfc->display,
"_FREERDP_RAW", False);
2473 clientFormat->localFormat = clientFormat->formatToRequest = CF_RAW;
2475 clientFormat = &clipboard->clientFormats[n++];
2476 clientFormat->atom = Logging_XInternAtom(xfc->log, xfc->display,
"UTF8_STRING", False);
2477 clientFormat->formatToRequest = CF_UNICODETEXT;
2478 clientFormat->localFormat = ClipboardGetFormatId(xfc->clipboard->system, mime_text_plain);
2480 clientFormat = &clipboard->clientFormats[n++];
2481 clientFormat->atom = XA_STRING;
2482 clientFormat->formatToRequest = CF_TEXT;
2483 clientFormat->localFormat = ClipboardGetFormatId(xfc->clipboard->system, mime_text_plain);
2485 clientFormat = &clipboard->clientFormats[n++];
2486 clientFormat->atom = Logging_XInternAtom(xfc->log, xfc->display, mime_tiff, False);
2487 clientFormat->formatToRequest = clientFormat->localFormat = CF_TIFF;
2489 for (
size_t x = 0; x < ARRAYSIZE(mime_bitmap); x++)
2491 const char* mime_bmp = mime_bitmap[x];
2492 const DWORD format = ClipboardGetFormatId(xfc->clipboard->system, mime_bmp);
2495 WLog_DBG(TAG,
"skipping local bitmap format %s [NOT SUPPORTED]", mime_bmp);
2499 WLog_DBG(TAG,
"register local bitmap format %s [0x%08" PRIx32
"]", mime_bmp, format);
2500 clientFormat = &clipboard->clientFormats[n++];
2501 clientFormat->localFormat = format;
2502 clientFormat->atom = Logging_XInternAtom(xfc->log, xfc->display, mime_bmp, False);
2503 clientFormat->formatToRequest = CF_DIB;
2506 for (
size_t x = 0; x < ARRAYSIZE(mime_images); x++)
2508 const char* mime_bmp = mime_images[x];
2509 const DWORD format = ClipboardGetFormatId(xfc->clipboard->system, mime_bmp);
2512 WLog_DBG(TAG,
"skipping local bitmap format %s [NOT SUPPORTED]", mime_bmp);
2516 WLog_DBG(TAG,
"register local bitmap format %s [0x%08" PRIx32
"]", mime_bmp, format);
2517 clientFormat = &clipboard->clientFormats[n++];
2518 clientFormat->localFormat = format;
2519 clientFormat->atom = Logging_XInternAtom(xfc->log, xfc->display, mime_bmp, False);
2520 clientFormat->formatToRequest = CF_DIB;
2523 clientFormat = &clipboard->clientFormats[n++];
2524 clientFormat->atom = Logging_XInternAtom(xfc->log, xfc->display, mime_html, False);
2525 clientFormat->formatToRequest = ClipboardGetFormatId(xfc->clipboard->system, type_HtmlFormat);
2526 clientFormat->localFormat = ClipboardGetFormatId(xfc->clipboard->system, mime_html);
2527 clientFormat->formatName = _strdup(type_HtmlFormat);
2529 if (!clientFormat->formatName)
2532 clientFormat = &clipboard->clientFormats[n++];
2540 const UINT32 fgid = ClipboardGetFormatId(clipboard->system, type_FileGroupDescriptorW);
2541 const UINT32 uid = ClipboardGetFormatId(clipboard->system, mime_uri_list);
2544 cliprdr_file_context_set_locally_available(clipboard->file, TRUE);
2545 clientFormat->atom = Logging_XInternAtom(xfc->log, xfc->display, mime_uri_list, False);
2546 clientFormat->localFormat = uid;
2547 clientFormat->formatToRequest = fgid;
2548 clientFormat->formatName = _strdup(type_FileGroupDescriptorW);
2550 if (!clientFormat->formatName)
2553 clientFormat = &clipboard->clientFormats[n++];
2556 const UINT32 gid = ClipboardGetFormatId(clipboard->system, mime_gnome_copied_files);
2559 cliprdr_file_context_set_locally_available(clipboard->file, TRUE);
2560 clientFormat->atom =
2561 Logging_XInternAtom(xfc->log, xfc->display, mime_gnome_copied_files, False);
2562 clientFormat->localFormat = gid;
2563 clientFormat->formatToRequest = fgid;
2564 clientFormat->formatName = _strdup(type_FileGroupDescriptorW);
2566 if (!clientFormat->formatName)
2569 clientFormat = &clipboard->clientFormats[n++];
2572 const UINT32 mid = ClipboardGetFormatId(clipboard->system, mime_mate_copied_files);
2575 cliprdr_file_context_set_locally_available(clipboard->file, TRUE);
2576 clientFormat->atom =
2577 Logging_XInternAtom(xfc->log, xfc->display, mime_mate_copied_files, False);
2578 clientFormat->localFormat = mid;
2579 clientFormat->formatToRequest = fgid;
2580 clientFormat->formatName = _strdup(type_FileGroupDescriptorW);
2582 if (!clientFormat->formatName)
2586 clipboard->numClientFormats = n;
2587 clipboard->targets[0] = Logging_XInternAtom(xfc->log, xfc->display,
"TIMESTAMP", FALSE);
2588 clipboard->targets[1] = Logging_XInternAtom(xfc->log, xfc->display,
"TARGETS", FALSE);
2589 clipboard->numTargets = 2;
2590 clipboard->incr_atom = Logging_XInternAtom(xfc->log, xfc->display,
"INCR", FALSE);
2592 if (relieveFilenameRestriction)
2594 WLog_DBG(TAG,
"Relieving CLIPRDR filename restriction");
2595 ClipboardGetDelegate(clipboard->system)->IsFileNameComponentValid =
2596 xf_cliprdr_is_valid_unix_filename;
2599 clipboard->cachedData = HashTable_New(TRUE);
2600 if (!clipboard->cachedData)
2603 obj = HashTable_ValueObject(clipboard->cachedData);
2604 obj->fnObjectFree = xf_cached_data_free;
2606 clipboard->cachedRawData = HashTable_New(TRUE);
2607 if (!clipboard->cachedRawData)
2610 obj = HashTable_ValueObject(clipboard->cachedRawData);
2611 obj->fnObjectFree = xf_cached_data_free;
2616 WINPR_PRAGMA_DIAG_PUSH
2617 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
2618 xf_clipboard_free(clipboard);
2619 WINPR_PRAGMA_DIAG_POP
2623 void xf_clipboard_free(xfClipboard* clipboard)
2628 xf_clipboard_free_server_formats(clipboard);
2630 if (clipboard->numClientFormats)
2632 for (UINT32 i = 0; i < clipboard->numClientFormats; i++)
2634 xfCliprdrFormat* format = &clipboard->clientFormats[i];
2635 free(format->formatName);
2639 cliprdr_file_context_free(clipboard->file);
2641 ClipboardDestroy(clipboard->system);
2642 xf_clipboard_formats_free(clipboard);
2643 HashTable_Free(clipboard->cachedRawData);
2644 HashTable_Free(clipboard->cachedData);
2645 requested_format_free(&clipboard->requestedFormat);
2646 free(clipboard->respond);
2647 free(clipboard->incr_data);
2651 void xf_cliprdr_init(xfContext* xfc, CliprdrClientContext* cliprdr)
2654 WINPR_ASSERT(cliprdr);
2656 xfc->cliprdr = cliprdr;
2657 xfc->clipboard->context = cliprdr;
2659 cliprdr->MonitorReady = xf_cliprdr_monitor_ready;
2660 cliprdr->ServerCapabilities = xf_cliprdr_server_capabilities;
2661 cliprdr->ServerFormatList = xf_cliprdr_server_format_list;
2662 cliprdr->ServerFormatListResponse = xf_cliprdr_server_format_list_response;
2663 cliprdr->ServerFormatDataRequest = xf_cliprdr_server_format_data_request;
2664 cliprdr->ServerFormatDataResponse = xf_cliprdr_server_format_data_response;
2666 cliprdr_file_context_init(xfc->clipboard->file, cliprdr);
2669 void xf_cliprdr_uninit(xfContext* xfc, CliprdrClientContext* cliprdr)
2672 WINPR_ASSERT(cliprdr);
2674 xfc->cliprdr = NULL;
2678 cliprdr_file_context_uninit(xfc->clipboard->file, cliprdr);
2679 xfc->clipboard->context = NULL;
FREERDP_API const char * freerdp_settings_get_string(const rdpSettings *settings, FreeRDP_Settings_Keys_String id)
Returns a immutable string settings value.
This struct contains function pointer to initialize/free objects.