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#ifdef WITH_DEBUG_CLIPRDR
58#define DEBUG_CLIPRDR(...) WLog_DBG(TAG, __VA_ARGS__)
60#define DEBUG_CLIPRDR(...) \
69 UINT32 formatToRequest;
84 UINT32 formatToRequest;
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 BOOL data_raw_format;
122 RequestedFormat* requestedFormat;
124 XSelectionEvent* respond;
133 size_t incr_data_length;
137 int xfixes_event_base;
138 int xfixes_error_base;
139 BOOL xfixes_supported;
143 UINT32 lastSentNumFormats;
144 CliprdrFileContext* file;
148static const char mime_text_plain[] =
"text/plain";
149static const char mime_uri_list[] =
"text/uri-list";
150static const char mime_html[] =
"text/html";
151static const char* mime_bitmap[] = {
"image/bmp",
"image/x-bmp",
"image/x-MS-bmp",
152 "image/x-win-bitmap" };
153static const char mime_webp[] =
"image/webp";
154static const char mime_png[] =
"image/png";
155static const char mime_jpeg[] =
"image/jpeg";
156static const char mime_tiff[] =
"image/tiff";
157static const char* mime_images[] = { mime_webp, mime_png, mime_jpeg, mime_tiff };
159static const char mime_gnome_copied_files[] =
"x-special/gnome-copied-files";
160static const char mime_mate_copied_files[] =
"x-special/mate-copied-files";
162static const char type_FileGroupDescriptorW[] =
"FileGroupDescriptorW";
163static const char type_HtmlFormat[] =
"HTML Format";
165static void xf_cliprdr_clear_cached_data(xfClipboard* clipboard);
166static UINT xf_cliprdr_send_client_format_list(xfClipboard* clipboard, BOOL force);
167static void xf_cliprdr_set_selection_owner(xfContext* xfc, xfClipboard* clipboard, Time timestamp);
169static void requested_format_free(RequestedFormat** ppRequestedFormat)
171 if (!ppRequestedFormat)
173 if (!(*ppRequestedFormat))
176 free((*ppRequestedFormat)->formatName);
177 free(*ppRequestedFormat);
178 *ppRequestedFormat = NULL;
181static BOOL requested_format_replace(RequestedFormat** ppRequestedFormat, UINT32 formatId,
182 const char* formatName)
184 if (!ppRequestedFormat)
187 requested_format_free(ppRequestedFormat);
188 RequestedFormat* requested = calloc(1,
sizeof(RequestedFormat));
191 requested->localFormat = formatId;
192 requested->formatToRequest = formatId;
195 requested->formatName = _strdup(formatName);
196 if (!requested->formatName)
203 *ppRequestedFormat = requested;
207static void xf_cached_data_free(
void* ptr)
209 xfCachedData* cached_data = ptr;
213 free(cached_data->data);
217static xfCachedData* xf_cached_data_new(BYTE* data,
size_t data_length)
219 if (data_length > UINT32_MAX)
222 xfCachedData* cached_data = calloc(1,
sizeof(xfCachedData));
226 cached_data->data = data;
227 cached_data->data_length = (UINT32)data_length;
232static xfCachedData* xf_cached_data_new_copy(
const BYTE* data,
size_t data_length)
237 copy = calloc(data_length + 1,
sizeof(BYTE));
240 memcpy(copy, data, data_length);
243 xfCachedData* cache = xf_cached_data_new(copy, data_length);
249static void xf_clipboard_free_server_formats(xfClipboard* clipboard)
251 WINPR_ASSERT(clipboard);
252 if (clipboard->serverFormats)
254 for (
size_t i = 0; i < clipboard->numServerFormats; i++)
257 free(format->formatName);
260 free(clipboard->serverFormats);
261 clipboard->serverFormats = NULL;
265static BOOL xf_cliprdr_update_owner(xfClipboard* clipboard)
267 WINPR_ASSERT(clipboard);
269 xfContext* xfc = clipboard->xfc;
272 if (!clipboard->sync)
275 Window owner = LogDynAndXGetSelectionOwner(xfc->log, xfc->display, clipboard->clipboard_atom);
276 if (clipboard->owner == owner)
279 clipboard->owner = owner;
283static void xf_cliprdr_check_owner(xfClipboard* clipboard)
285 if (xf_cliprdr_update_owner(clipboard))
286 xf_cliprdr_send_client_format_list(clipboard, FALSE);
289static BOOL xf_cliprdr_is_self_owned(xfClipboard* clipboard)
291 xfContext* xfc = NULL;
293 WINPR_ASSERT(clipboard);
295 xfc = clipboard->xfc;
297 return LogDynAndXGetSelectionOwner(xfc->log, xfc->display, clipboard->clipboard_atom) ==
301static void xf_cliprdr_set_raw_transfer_enabled(xfClipboard* clipboard, BOOL enabled)
303 UINT32 data = WINPR_ASSERTING_INT_CAST(uint32_t, enabled);
304 xfContext* xfc = NULL;
306 WINPR_ASSERT(clipboard);
308 xfc = clipboard->xfc;
310 LogDynAndXChangeProperty(xfc->log, xfc->display, xfc->drawable, clipboard->raw_transfer_atom,
311 XA_INTEGER, 32, PropModeReplace, (
const BYTE*)&data, 1);
314static BOOL xf_cliprdr_is_raw_transfer_available(xfClipboard* clipboard)
319 unsigned long length = 0;
320 unsigned long bytes_left = 0;
322 UINT32 is_enabled = 0;
324 xfContext* xfc = NULL;
326 WINPR_ASSERT(clipboard);
328 xfc = clipboard->xfc;
331 owner = LogDynAndXGetSelectionOwner(xfc->log, xfc->display, clipboard->clipboard_atom);
335 result = LogDynAndXGetWindowProperty(xfc->log, xfc->display, owner,
336 clipboard->raw_transfer_atom, 0, 4, 0, XA_INTEGER,
337 &type, &format, &length, &bytes_left, (BYTE**)&data);
346 if ((owner == None) || (owner == xfc->drawable))
349 if (result != Success)
352 return is_enabled ? TRUE : FALSE;
355static BOOL xf_cliprdr_formats_equal(
const CLIPRDR_FORMAT* server,
const xfCliprdrFormat* client)
357 WINPR_ASSERT(server);
358 WINPR_ASSERT(client);
360 if (server->formatName && client->formatName)
363 return (0 == strncmp(server->formatName, client->formatName, strlen(server->formatName)));
366 if (!server->formatName && !client->formatName)
368 return (server->formatId == client->formatToRequest);
374static const xfCliprdrFormat* xf_cliprdr_get_client_format_by_id(xfClipboard* clipboard,
377 WINPR_ASSERT(clipboard);
379 const BOOL formatIsHtml = formatId == ClipboardGetFormatId(clipboard->system, type_HtmlFormat);
380 const BOOL fetchImage = clipboard->isImageContent && formatIsHtml;
381 for (
size_t index = 0; index < clipboard->numClientFormats; index++)
383 const xfCliprdrFormat* format = &(clipboard->clientFormats[index]);
385 if (fetchImage && format->isImage)
388 if (format->formatToRequest == formatId)
395static const xfCliprdrFormat* xf_cliprdr_get_client_format_by_atom(xfClipboard* clipboard,
398 WINPR_ASSERT(clipboard);
400 for (UINT32 i = 0; i < clipboard->numClientFormats; i++)
402 const xfCliprdrFormat* format = &(clipboard->clientFormats[i]);
404 if (format->atom == atom)
411static const CLIPRDR_FORMAT* xf_cliprdr_get_server_format_by_atom(xfClipboard* clipboard, Atom atom)
413 WINPR_ASSERT(clipboard);
415 for (
size_t i = 0; i < clipboard->numClientFormats; i++)
417 const xfCliprdrFormat* client_format = &(clipboard->clientFormats[i]);
419 if (client_format->atom == atom)
421 for (
size_t j = 0; j < clipboard->numServerFormats; j++)
423 const CLIPRDR_FORMAT* server_format = &(clipboard->serverFormats[j]);
425 if (xf_cliprdr_formats_equal(server_format, client_format))
426 return server_format;
439static UINT xf_cliprdr_send_data_request(xfClipboard* clipboard, UINT32 formatId,
440 WINPR_ATTR_UNUSED
const xfCliprdrFormat* cformat)
443 request.requestedFormatId = formatId;
445 DEBUG_CLIPRDR(
"requesting format 0x%08" PRIx32
" [%s] {local 0x%08" PRIx32
"} [%s]", formatId,
446 ClipboardGetFormatIdString(formatId), cformat->localFormat, cformat->formatName);
448 WINPR_ASSERT(clipboard);
449 WINPR_ASSERT(clipboard->context);
450 WINPR_ASSERT(clipboard->context->ClientFormatDataRequest);
451 return clipboard->context->ClientFormatDataRequest(clipboard->context, &request);
459static UINT xf_cliprdr_send_data_response(xfClipboard* clipboard,
const xfCliprdrFormat* format,
460 const BYTE* data,
size_t size)
464 WINPR_ASSERT(clipboard);
467 if (clipboard->requestedFormatId == UINT32_MAX)
468 return CHANNEL_RC_OK;
473 DEBUG_CLIPRDR(
"send CB_RESPONSE_FAIL response {format 0x%08" PRIx32
474 " [%s] {local 0x%08" PRIx32
"} [%s]",
475 format->formatToRequest,
476 ClipboardGetFormatIdString(format->formatToRequest), format->localFormat,
479 DEBUG_CLIPRDR(
"send CB_RESPONSE_FAIL response");
483 WINPR_ASSERT(format);
484 DEBUG_CLIPRDR(
"send response format 0x%08" PRIx32
" [%s] {local 0x%08" PRIx32
"} [%s]",
485 format->formatToRequest, ClipboardGetFormatIdString(format->formatToRequest),
486 format->localFormat, format->formatName);
489 clipboard->requestedFormatId = UINT32_MAX;
491 response.common.msgFlags = (data) ? CB_RESPONSE_OK : CB_RESPONSE_FAIL;
493 WINPR_ASSERT(size <= UINT32_MAX);
494 response.common.dataLen = (UINT32)size;
495 response.requestedFormatData = data;
497 WINPR_ASSERT(clipboard->context);
498 WINPR_ASSERT(clipboard->context->ClientFormatDataResponse);
499 return clipboard->context->ClientFormatDataResponse(clipboard->context, &response);
502static wStream* xf_cliprdr_serialize_server_format_list(xfClipboard* clipboard)
504 UINT32 formatCount = 0;
507 WINPR_ASSERT(clipboard);
510 if (!(s = Stream_New(NULL, 128)))
512 WLog_ERR(TAG,
"failed to allocate serialized format list");
517 formatCount = (clipboard->numServerFormats > 0) ? clipboard->numServerFormats - 1 : 0;
518 Stream_Write_UINT32(s, formatCount);
520 for (UINT32 i = 0; i < formatCount; i++)
523 size_t name_length = format->formatName ? strlen(format->formatName) : 0;
525 DEBUG_CLIPRDR(
"server announced 0x%08" PRIx32
" [%s][%s]", format->formatId,
526 ClipboardGetFormatIdString(format->formatId), format->formatName);
527 if (!Stream_EnsureRemainingCapacity(s,
sizeof(UINT32) + name_length + 1))
529 WLog_ERR(TAG,
"failed to expand serialized format list");
533 Stream_Write_UINT32(s, format->formatId);
535 if (format->formatName)
536 Stream_Write(s, format->formatName, name_length);
538 Stream_Write_UINT8(s,
'\0');
541 Stream_SealLength(s);
544 Stream_Free(s, TRUE);
548static CLIPRDR_FORMAT* xf_cliprdr_parse_server_format_list(BYTE* data,
size_t length,
554 WINPR_ASSERT(data || (length == 0));
555 WINPR_ASSERT(numFormats);
557 if (!(s = Stream_New(data, length)))
559 WLog_ERR(TAG,
"failed to allocate stream for parsing serialized format list");
563 if (!Stream_CheckAndLogRequiredLength(TAG, s,
sizeof(UINT32)))
566 Stream_Read_UINT32(s, *numFormats);
568 if (*numFormats > MAX_CLIPBOARD_FORMATS)
570 WLog_ERR(TAG,
"unexpectedly large number of formats: %" PRIu32
"", *numFormats);
576 WLog_ERR(TAG,
"failed to allocate format list");
580 for (UINT32 i = 0; i < *numFormats; i++)
582 const char* formatName = NULL;
583 size_t formatNameLength = 0;
585 if (!Stream_CheckAndLogRequiredLength(TAG, s,
sizeof(UINT32)))
588 Stream_Read_UINT32(s, formats[i].formatId);
589 formatName = (
const char*)Stream_Pointer(s);
590 formatNameLength = strnlen(formatName, Stream_GetRemainingLength(s));
592 if (formatNameLength == Stream_GetRemainingLength(s))
594 WLog_ERR(TAG,
"missing terminating null byte, %" PRIuz
" bytes left to read",
599 formats[i].formatName = strndup(formatName, formatNameLength);
600 Stream_Seek(s, formatNameLength + 1);
603 Stream_Free(s, FALSE);
606 Stream_Free(s, FALSE);
612static void xf_cliprdr_free_formats(
CLIPRDR_FORMAT* formats, UINT32 numFormats)
614 WINPR_ASSERT(formats || (numFormats == 0));
616 for (UINT32 i = 0; i < numFormats; i++)
618 free(formats[i].formatName);
624static CLIPRDR_FORMAT* xf_cliprdr_get_raw_server_formats(xfClipboard* clipboard, UINT32* numFormats)
628 unsigned long length = 0;
629 unsigned long remaining = 0;
632 xfContext* xfc = NULL;
634 WINPR_ASSERT(clipboard);
635 WINPR_ASSERT(numFormats);
637 xfc = clipboard->xfc;
642 Window owner = LogDynAndXGetSelectionOwner(xfc->log, xfc->display, clipboard->clipboard_atom);
643 LogDynAndXGetWindowProperty(xfc->log, xfc->display, owner, clipboard->raw_format_list_atom, 0,
644 4096, False, clipboard->raw_format_list_atom, &type, &format,
645 &length, &remaining, &data);
647 if (data && length > 0 && format == 8 && type == clipboard->raw_format_list_atom)
649 formats = xf_cliprdr_parse_server_format_list(data, length, numFormats);
654 "failed to retrieve raw format list: data=%p, length=%lu, format=%d, type=%lu "
656 (
void*)data, length, format, (
unsigned long)type,
657 (
unsigned long)clipboard->raw_format_list_atom);
666static BOOL xf_cliprdr_should_add_format(
const CLIPRDR_FORMAT* formats,
size_t count,
667 const xfCliprdrFormat* xformat)
669 WINPR_ASSERT(formats);
674 for (
size_t x = 0; x < count; x++)
677 if (format->formatId == xformat->formatToRequest)
683static CLIPRDR_FORMAT* xf_cliprdr_get_formats_from_targets(xfClipboard* clipboard,
688 int format_property = 0;
689 unsigned long proplength = 0;
690 unsigned long bytes_left = 0;
693 WINPR_ASSERT(clipboard);
694 WINPR_ASSERT(numFormats);
696 xfContext* xfc = clipboard->xfc;
700 LogDynAndXGetWindowProperty(xfc->log, xfc->display, xfc->drawable, clipboard->property_atom, 0,
701 200, 0, XA_ATOM, &atom, &format_property, &proplength, &bytes_left,
706 unsigned long length = proplength + 1;
709 WLog_ERR(TAG,
"XGetWindowProperty set length = %lu but data is NULL", length);
715 WLog_ERR(TAG,
"failed to allocate %lu CLIPRDR_FORMAT structs", length);
720 BOOL isImage = FALSE;
721 BOOL hasHtml = FALSE;
722 const uint32_t htmlFormatId = ClipboardRegisterFormat(clipboard->system, type_HtmlFormat);
723 for (
unsigned long i = 0; i < proplength; i++)
725 Atom tatom = ((Atom*)data)[i];
726 const xfCliprdrFormat* format = xf_cliprdr_get_client_format_by_atom(clipboard, tatom);
728 if (xf_cliprdr_should_add_format(formats, *numFormats, format))
731 cformat->formatId = format->formatToRequest;
735 if (cformat->formatId == htmlFormatId)
740 if (cformat->formatId == CF_TIFF)
742 else if (cformat->formatId == CF_DIB)
744 else if (cformat->formatId == CF_DIBV5)
747 if (format->formatName)
749 cformat->formatName = _strdup(format->formatName);
750 WINPR_ASSERT(cformat->formatName);
753 cformat->formatName = NULL;
759 clipboard->isImageContent = isImage;
760 if (isImage && !hasHtml)
763 cformat->formatId = htmlFormatId;
764 cformat->formatName = _strdup(type_HtmlFormat);
776static CLIPRDR_FORMAT* xf_cliprdr_get_client_formats(xfClipboard* clipboard, UINT32* numFormats)
780 WINPR_ASSERT(clipboard);
781 WINPR_ASSERT(numFormats);
785 if (xf_cliprdr_is_raw_transfer_available(clipboard))
787 formats = xf_cliprdr_get_raw_server_formats(clipboard, numFormats);
790 if (*numFormats == 0)
792 xf_cliprdr_free_formats(formats, *numFormats);
793 formats = xf_cliprdr_get_formats_from_targets(clipboard, numFormats);
799static void xf_cliprdr_provide_server_format_list(xfClipboard* clipboard)
802 xfContext* xfc = NULL;
804 WINPR_ASSERT(clipboard);
806 xfc = clipboard->xfc;
809 formats = xf_cliprdr_serialize_server_format_list(clipboard);
813 const size_t len = Stream_Length(formats);
814 WINPR_ASSERT(len <= INT32_MAX);
815 LogDynAndXChangeProperty(xfc->log, xfc->display, xfc->drawable,
816 clipboard->raw_format_list_atom, clipboard->raw_format_list_atom,
817 8, PropModeReplace, Stream_Buffer(formats), (
int)len);
821 LogDynAndXDeleteProperty(xfc->log, xfc->display, xfc->drawable,
822 clipboard->raw_format_list_atom);
825 Stream_Free(formats, TRUE);
833 if (a->formatId != b->formatId)
835 if (!a->formatName && !b->formatName)
837 if (!a->formatName || !b->formatName)
839 return strcmp(a->formatName, b->formatName) == 0;
842static BOOL xf_clipboard_changed(xfClipboard* clipboard,
const CLIPRDR_FORMAT* formats,
845 WINPR_ASSERT(clipboard);
846 WINPR_ASSERT(formats || (numFormats == 0));
848 if (clipboard->lastSentNumFormats != numFormats)
851 for (UINT32 x = 0; x < numFormats; x++)
854 BOOL contained = FALSE;
855 for (UINT32 y = 0; y < numFormats; y++)
857 if (xf_clipboard_format_equal(cur, &formats[y]))
870static void xf_clipboard_formats_free(xfClipboard* clipboard)
872 WINPR_ASSERT(clipboard);
874 xf_cliprdr_free_formats(clipboard->lastSentFormats, clipboard->lastSentNumFormats);
875 clipboard->lastSentFormats = NULL;
876 clipboard->lastSentNumFormats = 0;
879static BOOL xf_clipboard_copy_formats(xfClipboard* clipboard,
const CLIPRDR_FORMAT* formats,
882 WINPR_ASSERT(clipboard);
883 WINPR_ASSERT(formats || (numFormats == 0));
885 xf_clipboard_formats_free(clipboard);
887 clipboard->lastSentFormats = calloc(numFormats,
sizeof(
CLIPRDR_FORMAT));
888 if (!clipboard->lastSentFormats)
890 clipboard->lastSentNumFormats = numFormats;
891 for (UINT32 x = 0; x < numFormats; x++)
897 lcur->formatName = _strdup(cur->formatName);
902static UINT xf_cliprdr_send_format_list(xfClipboard* clipboard,
const CLIPRDR_FORMAT* formats,
903 UINT32 numFormats, BOOL force)
909 } cnv = { .cpv = formats };
911 .numFormats = numFormats,
913 .common.msgType = CB_FORMAT_LIST };
916 WINPR_ASSERT(clipboard);
917 WINPR_ASSERT(formats || (numFormats == 0));
919 if (!force && !xf_clipboard_changed(clipboard, formats, numFormats))
920 return CHANNEL_RC_OK;
922#if defined(WITH_DEBUG_CLIPRDR)
923 for (UINT32 x = 0; x < numFormats; x++)
926 DEBUG_CLIPRDR(
"announcing format 0x%08" PRIx32
" [%s] [%s]", format->formatId,
927 ClipboardGetFormatIdString(format->formatId), format->formatName);
931 xf_clipboard_copy_formats(clipboard, formats, numFormats);
933 xf_cliprdr_send_data_response(clipboard, NULL, NULL, 0);
935 xf_cliprdr_clear_cached_data(clipboard);
937 ret = cliprdr_file_context_notify_new_client_format_list(clipboard->file);
941 WINPR_ASSERT(clipboard->context);
942 WINPR_ASSERT(clipboard->context->ClientFormatList);
943 return clipboard->context->ClientFormatList(clipboard->context, &formatList);
946static void xf_cliprdr_get_requested_targets(xfClipboard* clipboard)
948 UINT32 numFormats = 0;
949 CLIPRDR_FORMAT* formats = xf_cliprdr_get_client_formats(clipboard, &numFormats);
950 xf_cliprdr_send_format_list(clipboard, formats, numFormats, FALSE);
951 xf_cliprdr_free_formats(formats, numFormats);
954static void xf_cliprdr_process_requested_data(xfClipboard* clipboard, BOOL hasData,
955 const BYTE* data,
size_t size)
960 INT64 srcFormatId = -1;
961 BYTE* pDstData = NULL;
962 const xfCliprdrFormat* format = NULL;
964 WINPR_ASSERT(clipboard);
966 if (clipboard->incr_starts && hasData)
971 clipboard->incr_data_length = 0;
973 format = xf_cliprdr_get_client_format_by_id(clipboard, clipboard->requestedFormatId);
975 if (!hasData || !data || !format)
977 xf_cliprdr_send_data_response(clipboard, format, NULL, 0);
981 switch (format->formatToRequest)
984 srcFormatId = CF_RAW;
990 srcFormatId = format->localFormat;
994 srcFormatId = format->localFormat;
1000 xf_cliprdr_send_data_response(clipboard, format, NULL, 0);
1004 ClipboardLock(clipboard->system);
1005 SrcSize = (UINT32)size;
1006 bSuccess = ClipboardSetData(clipboard->system, (UINT32)srcFormatId, data, SrcSize);
1012 (BYTE*)ClipboardGetData(clipboard->system, clipboard->requestedFormatId, &DstSize);
1014 ClipboardUnlock(clipboard->system);
1018 xf_cliprdr_send_data_response(clipboard, format, NULL, 0);
1029 ClipboardLock(clipboard->system);
1030 if (format->formatToRequest &&
1031 (format->formatToRequest ==
1032 ClipboardGetFormatId(clipboard->system, type_FileGroupDescriptorW)))
1034 UINT error = NO_ERROR;
1040 const UINT32 flags = cliprdr_file_context_remote_get_flags(clipboard->file);
1041 error = cliprdr_serialize_file_list_ex(flags, file_array, file_count, &pDstData, &DstSize);
1044 WLog_ERR(TAG,
"failed to serialize CLIPRDR_FILELIST: 0x%08X", error);
1047 UINT32 formatId = ClipboardGetFormatId(clipboard->system, mime_uri_list);
1048 UINT32 url_size = 0;
1050 char* url = ClipboardGetData(clipboard->system, formatId, &url_size);
1051 cliprdr_file_context_update_client_data(clipboard->file, url, url_size);
1057 ClipboardUnlock(clipboard->system);
1059 xf_cliprdr_send_data_response(clipboard, format, pDstData, DstSize);
1063static BOOL xf_restore_input_flags(xfClipboard* clipboard)
1065 WINPR_ASSERT(clipboard);
1067 xfContext* xfc = clipboard->xfc;
1070 if (clipboard->event_mask != 0)
1072 XSelectInput(xfc->display, xfc->drawable, clipboard->event_mask);
1073 clipboard->event_mask = 0;
1078static BOOL append(xfClipboard* clipboard,
const void* sdata,
size_t length)
1080 WINPR_ASSERT(clipboard);
1082 const size_t size = length + clipboard->incr_data_length + 2;
1083 BYTE* data = realloc(clipboard->incr_data, size);
1086 clipboard->incr_data = data;
1087 memcpy(&data[clipboard->incr_data_length], sdata, length);
1088 clipboard->incr_data_length += length;
1089 clipboard->incr_data[clipboard->incr_data_length + 0] =
'\0';
1090 clipboard->incr_data[clipboard->incr_data_length + 1] =
'\0';
1094static BOOL xf_cliprdr_stop_incr(xfClipboard* clipboard)
1096 clipboard->incr_starts = FALSE;
1097 clipboard->incr_data_length = 0;
1098 return xf_restore_input_flags(clipboard);
1101static BOOL xf_cliprdr_get_requested_data(xfClipboard* clipboard, Atom target)
1103 WINPR_ASSERT(clipboard);
1105 xfContext* xfc = clipboard->xfc;
1108 const xfCliprdrFormat* format =
1109 xf_cliprdr_get_client_format_by_id(clipboard, clipboard->requestedFormatId);
1111 if (!format || (format->atom != target))
1113 xf_cliprdr_send_data_response(clipboard, format, NULL, 0);
1118 BOOL has_data = FALSE;
1119 int format_property = 0;
1120 unsigned long length = 0;
1121 unsigned long total_bytes = 0;
1122 BYTE* property_data = NULL;
1123 const int rc = LogDynAndXGetWindowProperty(
1124 xfc->log, xfc->display, xfc->drawable, clipboard->property_atom, 0, 0, False, target, &type,
1125 &format_property, &length, &total_bytes, &property_data);
1128 xf_cliprdr_send_data_response(clipboard, format, NULL, 0);
1135 if ((total_bytes <= 0) && !clipboard->incr_starts)
1137 xf_cliprdr_stop_incr(clipboard);
1140 else if (type == clipboard->incr_atom)
1142 xf_cliprdr_stop_incr(clipboard);
1143 clipboard->incr_starts = TRUE;
1148 BYTE* incremental_data = NULL;
1149 unsigned long incremental_len = 0;
1152 len = clipboard->incr_data_length;
1153 if (total_bytes <= 0)
1155 xf_cliprdr_stop_incr(clipboard);
1159 else if (LogDynAndXGetWindowProperty(
1160 xfc->log, xfc->display, xfc->drawable, clipboard->property_atom, 0,
1161 WINPR_ASSERTING_INT_CAST(int32_t, total_bytes), False, target, &type,
1162 &format_property, &incremental_len, &length, &incremental_data) == Success)
1164 has_data = append(clipboard, incremental_data, incremental_len);
1165 len = clipboard->incr_data_length;
1168 if (incremental_data)
1169 XFree(incremental_data);
1172 LogDynAndXDeleteProperty(xfc->log, xfc->display, xfc->drawable, clipboard->property_atom);
1173 xf_cliprdr_process_requested_data(clipboard, has_data, clipboard->incr_data, len);
1178static void xf_cliprdr_append_target(xfClipboard* clipboard, Atom target)
1180 WINPR_ASSERT(clipboard);
1182 if (clipboard->numTargets >= ARRAYSIZE(clipboard->targets))
1185 for (
size_t i = 0; i < clipboard->numTargets; i++)
1187 if (clipboard->targets[i] == target)
1191 clipboard->targets[clipboard->numTargets++] = target;
1194static void xf_cliprdr_provide_targets(xfClipboard* clipboard,
const XSelectionEvent* respond)
1196 xfContext* xfc = NULL;
1198 WINPR_ASSERT(clipboard);
1200 xfc = clipboard->xfc;
1203 if (respond->property != None)
1205 WINPR_ASSERT(clipboard->numTargets <= INT32_MAX);
1206 LogDynAndXChangeProperty(xfc->log, xfc->display, respond->requestor, respond->property,
1207 XA_ATOM, 32, PropModeReplace, (
const BYTE*)clipboard->targets,
1208 (
int)clipboard->numTargets);
1212static void xf_cliprdr_provide_timestamp(xfClipboard* clipboard,
const XSelectionEvent* respond)
1214 xfContext* xfc = NULL;
1216 WINPR_ASSERT(clipboard);
1218 xfc = clipboard->xfc;
1221 if (respond->property != None)
1223 LogDynAndXChangeProperty(xfc->log, xfc->display, respond->requestor, respond->property,
1224 XA_INTEGER, 32, PropModeReplace,
1225 (
const BYTE*)&clipboard->selection_ownership_timestamp, 1);
1229static void xf_cliprdr_provide_data(xfClipboard* clipboard,
const XSelectionEvent* respond,
1230 const BYTE* data, UINT32 size)
1232 xfContext* xfc = NULL;
1234 WINPR_ASSERT(clipboard);
1236 xfc = clipboard->xfc;
1239 if (respond->property != None)
1241 LogDynAndXChangeProperty(xfc->log, xfc->display, respond->requestor, respond->property,
1242 respond->target, 8, PropModeReplace, data,
1243 WINPR_ASSERTING_INT_CAST(int32_t, size));
1247static void log_selection_event(xfContext* xfc,
const XEvent* event)
1249 const DWORD level = WLOG_TRACE;
1250 static wLog* _log_cached_ptr = NULL;
1251 if (!_log_cached_ptr)
1252 _log_cached_ptr = WLog_Get(TAG);
1253 if (WLog_IsLevelActive(_log_cached_ptr, level))
1256 switch (event->type)
1258 case SelectionClear:
1260 const XSelectionClearEvent* xevent = &
event->xselectionclear;
1262 Safe_XGetAtomName(_log_cached_ptr, xfc->display, xevent->selection);
1263 WLog_Print(_log_cached_ptr, level,
"got event %s [selection %s]",
1264 x11_event_string(event->type), selection);
1268 case SelectionNotify:
1270 const XSelectionEvent* xevent = &
event->xselection;
1272 Safe_XGetAtomName(_log_cached_ptr, xfc->display, xevent->selection);
1273 char* target = Safe_XGetAtomName(_log_cached_ptr, xfc->display, xevent->target);
1274 char*
property = Safe_XGetAtomName(_log_cached_ptr, xfc->display, xevent->property);
1275 WLog_Print(_log_cached_ptr, level,
1276 "got event %s [selection %s, target %s, property %s]",
1277 x11_event_string(event->type), selection, target, property);
1283 case SelectionRequest:
1285 const XSelectionRequestEvent* xevent = &
event->xselectionrequest;
1287 Safe_XGetAtomName(_log_cached_ptr, xfc->display, xevent->selection);
1288 char* target = Safe_XGetAtomName(_log_cached_ptr, xfc->display, xevent->target);
1289 char*
property = Safe_XGetAtomName(_log_cached_ptr, xfc->display, xevent->property);
1290 WLog_Print(_log_cached_ptr, level,
1291 "got event %s [selection %s, target %s, property %s]",
1292 x11_event_string(event->type), selection, target, property);
1298 case PropertyNotify:
1300 const XPropertyEvent* xevent = &
event->xproperty;
1301 char* atom = Safe_XGetAtomName(_log_cached_ptr, xfc->display, xevent->atom);
1302 WLog_Print(_log_cached_ptr, level,
"got event %s [atom %s]",
1303 x11_event_string(event->type), atom);
1313static BOOL xf_cliprdr_process_selection_notify(xfClipboard* clipboard,
1314 const XSelectionEvent* xevent)
1316 WINPR_ASSERT(clipboard);
1317 WINPR_ASSERT(xevent);
1319 if (xevent->target == clipboard->targets[1])
1321 if (xevent->property == None)
1323 xf_cliprdr_send_client_format_list(clipboard, FALSE);
1327 xf_cliprdr_get_requested_targets(clipboard);
1334 return xf_cliprdr_get_requested_data(clipboard, xevent->target);
1338void xf_cliprdr_clear_cached_data(xfClipboard* clipboard)
1340 WINPR_ASSERT(clipboard);
1342 ClipboardLock(clipboard->system);
1343 ClipboardEmpty(clipboard->system);
1345 HashTable_Clear(clipboard->cachedData);
1346 HashTable_Clear(clipboard->cachedRawData);
1348 cliprdr_file_context_clear(clipboard->file);
1350 xf_cliprdr_stop_incr(clipboard);
1351 ClipboardUnlock(clipboard->system);
1354static void* format_to_cache_slot(UINT32 format)
1361 cnv.uptr = 0x100000000ULL + format;
1365static UINT32 get_dst_format_id_for_local_request(xfClipboard* clipboard,
1366 const xfCliprdrFormat* format)
1368 UINT32 dstFormatId = 0;
1370 WINPR_ASSERT(format);
1372 if (!format->formatName)
1373 return format->localFormat;
1375 ClipboardLock(clipboard->system);
1376 if (strcmp(format->formatName, type_HtmlFormat) == 0)
1377 dstFormatId = ClipboardGetFormatId(clipboard->system, mime_html);
1378 ClipboardUnlock(clipboard->system);
1380 if (strcmp(format->formatName, type_FileGroupDescriptorW) == 0)
1381 dstFormatId = format->localFormat;
1386static void get_src_format_info_for_local_request(xfClipboard* clipboard,
1387 const xfCliprdrFormat* format,
1388 UINT32* srcFormatId, BOOL* nullTerminated)
1391 *nullTerminated = FALSE;
1393 if (format->formatName)
1395 ClipboardLock(clipboard->system);
1396 if (strcmp(format->formatName, type_HtmlFormat) == 0)
1398 *srcFormatId = ClipboardGetFormatId(clipboard->system, type_HtmlFormat);
1399 *nullTerminated = TRUE;
1401 else if (strcmp(format->formatName, type_FileGroupDescriptorW) == 0)
1403 *srcFormatId = ClipboardGetFormatId(clipboard->system, type_FileGroupDescriptorW);
1404 *nullTerminated = TRUE;
1406 ClipboardUnlock(clipboard->system);
1410 *srcFormatId = format->formatToRequest;
1411 switch (format->formatToRequest)
1415 case CF_UNICODETEXT:
1416 *nullTerminated = TRUE;
1419 *srcFormatId = CF_DIB;
1422 *srcFormatId = CF_TIFF;
1430static xfCachedData* convert_data_from_existing_raw_data(xfClipboard* clipboard,
1431 xfCachedData* cached_raw_data,
1432 UINT32 srcFormatId, BOOL nullTerminated,
1435 xfCachedData* cached_data = NULL;
1437 BYTE* dst_data = NULL;
1438 UINT32 dst_size = 0;
1440 WINPR_ASSERT(clipboard);
1441 WINPR_ASSERT(cached_raw_data);
1442 WINPR_ASSERT(cached_raw_data->data);
1444 ClipboardLock(clipboard->system);
1445 success = ClipboardSetData(clipboard->system, srcFormatId, cached_raw_data->data,
1446 cached_raw_data->data_length);
1449 WLog_WARN(TAG,
"Failed to set clipboard data (formatId: %u, data: %p, data_length: %u)",
1450 srcFormatId, cached_raw_data->data, cached_raw_data->data_length);
1451 ClipboardUnlock(clipboard->system);
1455 dst_data = ClipboardGetData(clipboard->system, dstFormatId, &dst_size);
1458 WLog_WARN(TAG,
"Failed to get converted clipboard data");
1459 ClipboardUnlock(clipboard->system);
1462 ClipboardUnlock(clipboard->system);
1466 BYTE* nullTerminator = memchr(dst_data,
'\0', dst_size);
1469 const intptr_t diff = nullTerminator - dst_data;
1470 WINPR_ASSERT(diff >= 0);
1471 WINPR_ASSERT(diff <= UINT32_MAX);
1472 dst_size = (UINT32)diff;
1476 cached_data = xf_cached_data_new(dst_data, dst_size);
1479 WLog_WARN(TAG,
"Failed to allocate cache entry");
1484 if (!HashTable_Insert(clipboard->cachedData, format_to_cache_slot(dstFormatId), cached_data))
1486 WLog_WARN(TAG,
"Failed to cache clipboard data");
1487 xf_cached_data_free(cached_data);
1494static BOOL xf_cliprdr_process_selection_request(xfClipboard* clipboard,
1495 const XSelectionRequestEvent* xevent)
1499 UINT32 formatId = 0;
1500 XSelectionEvent* respond = NULL;
1502 BOOL delayRespond = 0;
1503 BOOL rawTransfer = 0;
1504 unsigned long length = 0;
1505 unsigned long bytes_left = 0;
1506 xfContext* xfc = NULL;
1508 WINPR_ASSERT(clipboard);
1509 WINPR_ASSERT(xevent);
1511 xfc = clipboard->xfc;
1514 if (xevent->owner != xfc->drawable)
1517 delayRespond = FALSE;
1519 if (!(respond = (XSelectionEvent*)calloc(1,
sizeof(XSelectionEvent))))
1521 WLog_ERR(TAG,
"failed to allocate XEvent data");
1525 respond->property = None;
1526 respond->type = SelectionNotify;
1527 respond->display = xevent->display;
1528 respond->requestor = xevent->requestor;
1529 respond->selection = xevent->selection;
1530 respond->target = xevent->target;
1531 respond->time = xevent->time;
1533 if (xevent->target == clipboard->targets[0])
1536 respond->property = xevent->property;
1537 xf_cliprdr_provide_timestamp(clipboard, respond);
1539 else if (xevent->target == clipboard->targets[1])
1542 respond->property = xevent->property;
1543 xf_cliprdr_provide_targets(clipboard, respond);
1548 xf_cliprdr_get_server_format_by_atom(clipboard, xevent->target);
1549 const xfCliprdrFormat* cformat =
1550 xf_cliprdr_get_client_format_by_atom(clipboard, xevent->target);
1552 if (format && (xevent->requestor != xfc->drawable))
1554 formatId = format->formatId;
1555 rawTransfer = FALSE;
1556 xfCachedData* cached_data = NULL;
1557 UINT32 dstFormatId = 0;
1559 if (formatId == CF_RAW)
1561 if (LogDynAndXGetWindowProperty(
1562 xfc->log, xfc->display, xevent->requestor, clipboard->property_atom, 0, 4,
1563 0, XA_INTEGER, &type, &fmt, &length, &bytes_left, &data) != Success)
1570 CopyMemory(&formatId, data, 4);
1575 dstFormatId = get_dst_format_id_for_local_request(clipboard, cformat);
1576 DEBUG_CLIPRDR(
"formatId: %u, dstFormatId: %u", formatId, dstFormatId);
1579 cached_data = HashTable_GetItemValue(clipboard->cachedData,
1580 format_to_cache_slot(dstFormatId));
1582 cached_data = HashTable_GetItemValue(clipboard->cachedRawData,
1583 format_to_cache_slot(formatId));
1585 DEBUG_CLIPRDR(
"hasCachedData: %u, rawTransfer: %u", cached_data ? 1 : 0, rawTransfer);
1587 if (!cached_data && !rawTransfer)
1589 UINT32 srcFormatId = 0;
1590 BOOL nullTerminated = FALSE;
1591 xfCachedData* cached_raw_data = NULL;
1593 get_src_format_info_for_local_request(clipboard, cformat, &srcFormatId,
1596 HashTable_GetItemValue(clipboard->cachedRawData, (
void*)(UINT_PTR)srcFormatId);
1598 DEBUG_CLIPRDR(
"hasCachedRawData: %u, rawDataLength: %u", cached_raw_data ? 1 : 0,
1599 cached_raw_data ? cached_raw_data->data_length : 0);
1601 if (cached_raw_data && cached_raw_data->data_length != 0)
1602 cached_data = convert_data_from_existing_raw_data(
1603 clipboard, cached_raw_data, srcFormatId, nullTerminated, dstFormatId);
1606 DEBUG_CLIPRDR(
"hasCachedData: %u", cached_data ? 1 : 0);
1611 respond->property = xevent->property;
1614 xf_cliprdr_provide_data(clipboard, respond, cached_data->data,
1615 cached_data->data_length);
1617 else if (clipboard->respond)
1623 WINPR_ASSERT(cformat);
1629 respond->property = xevent->property;
1630 clipboard->respond = respond;
1631 requested_format_replace(&clipboard->requestedFormat, formatId,
1632 cformat->formatName);
1633 clipboard->data_raw_format = rawTransfer;
1634 delayRespond = TRUE;
1635 xf_cliprdr_send_data_request(clipboard, formatId, cformat);
1645 XSelectionEvent* sev;
1649 LogDynAndXSendEvent(xfc->log, xfc->display, xevent->requestor, 0, 0, conv.ev);
1650 LogDynAndXFlush(xfc->log, xfc->display);
1657static BOOL xf_cliprdr_process_selection_clear(xfClipboard* clipboard,
1658 const XSelectionClearEvent* xevent)
1660 xfContext* xfc = NULL;
1662 WINPR_ASSERT(clipboard);
1663 WINPR_ASSERT(xevent);
1665 xfc = clipboard->xfc;
1668 WINPR_UNUSED(xevent);
1670 if (xf_cliprdr_is_self_owned(clipboard))
1673 LogDynAndXDeleteProperty(xfc->log, xfc->display, clipboard->root_window,
1674 clipboard->property_atom);
1678static BOOL xf_cliprdr_process_property_notify(xfClipboard* clipboard,
const XPropertyEvent* xevent)
1680 const xfCliprdrFormat* format = NULL;
1681 xfContext* xfc = NULL;
1686 xfc = clipboard->xfc;
1688 WINPR_ASSERT(xevent);
1690 if (xevent->atom == clipboard->timestamp_property_atom)
1696 xf_cliprdr_set_selection_owner(xfc, clipboard, xevent->time);
1700 if (xevent->atom != clipboard->property_atom)
1703 if (xevent->window == clipboard->root_window)
1705 xf_cliprdr_send_client_format_list(clipboard, FALSE);
1707 else if ((xevent->window == xfc->drawable) && (xevent->state == PropertyNewValue) &&
1708 clipboard->incr_starts)
1710 format = xf_cliprdr_get_client_format_by_id(clipboard, clipboard->requestedFormatId);
1713 xf_cliprdr_get_requested_data(clipboard, format->atom);
1719void xf_cliprdr_handle_xevent(xfContext* xfc,
const XEvent* event)
1721 xfClipboard* clipboard = NULL;
1726 clipboard = xfc->clipboard;
1733 if (clipboard->xfixes_supported &&
1734 event->type == XFixesSelectionNotify + clipboard->xfixes_event_base)
1736 const XFixesSelectionNotifyEvent* se = (
const XFixesSelectionNotifyEvent*)event;
1738 if (se->subtype == XFixesSetSelectionOwnerNotify)
1740 if (se->selection != clipboard->clipboard_atom)
1743 if (LogDynAndXGetSelectionOwner(xfc->log, xfc->display, se->selection) == xfc->drawable)
1746 clipboard->owner = None;
1747 xf_cliprdr_check_owner(clipboard);
1755 switch (event->type)
1757 case SelectionNotify:
1758 log_selection_event(xfc, event);
1759 xf_cliprdr_process_selection_notify(clipboard, &event->xselection);
1762 case SelectionRequest:
1763 log_selection_event(xfc, event);
1764 xf_cliprdr_process_selection_request(clipboard, &event->xselectionrequest);
1767 case SelectionClear:
1768 log_selection_event(xfc, event);
1769 xf_cliprdr_process_selection_clear(clipboard, &event->xselectionclear);
1772 case PropertyNotify:
1773 log_selection_event(xfc, event);
1774 xf_cliprdr_process_property_notify(clipboard, &event->xproperty);
1778 if (!clipboard->xfixes_supported)
1780 xf_cliprdr_check_owner(clipboard);
1794static UINT xf_cliprdr_send_client_capabilities(xfClipboard* clipboard)
1799 WINPR_ASSERT(clipboard);
1801 capabilities.cCapabilitiesSets = 1;
1803 generalCapabilitySet.capabilitySetType = CB_CAPSTYPE_GENERAL;
1804 generalCapabilitySet.capabilitySetLength = 12;
1805 generalCapabilitySet.version = CB_CAPS_VERSION_2;
1806 generalCapabilitySet.generalFlags = CB_USE_LONG_FORMAT_NAMES;
1808 WINPR_ASSERT(clipboard);
1809 generalCapabilitySet.generalFlags |= cliprdr_file_context_current_flags(clipboard->file);
1811 WINPR_ASSERT(clipboard->context);
1812 WINPR_ASSERT(clipboard->context->ClientCapabilities);
1813 return clipboard->context->ClientCapabilities(clipboard->context, &capabilities);
1821static UINT xf_cliprdr_send_client_format_list(xfClipboard* clipboard, BOOL force)
1823 WINPR_ASSERT(clipboard);
1825 xfContext* xfc = clipboard->xfc;
1828 UINT32 numFormats = 0;
1829 CLIPRDR_FORMAT* formats = xf_cliprdr_get_client_formats(clipboard, &numFormats);
1831 const UINT ret = xf_cliprdr_send_format_list(clipboard, formats, numFormats, force);
1833 if (clipboard->owner && clipboard->owner != xfc->drawable)
1836 LogDynAndXConvertSelection(xfc->log, xfc->display, clipboard->clipboard_atom,
1837 clipboard->targets[1], clipboard->property_atom, xfc->drawable,
1841 xf_cliprdr_free_formats(formats, numFormats);
1851static UINT xf_cliprdr_send_client_format_list_response(xfClipboard* clipboard, BOOL status)
1855 formatListResponse.common.msgType = CB_FORMAT_LIST_RESPONSE;
1856 formatListResponse.common.msgFlags = status ? CB_RESPONSE_OK : CB_RESPONSE_FAIL;
1857 formatListResponse.common.dataLen = 0;
1859 WINPR_ASSERT(clipboard);
1860 WINPR_ASSERT(clipboard->context);
1861 WINPR_ASSERT(clipboard->context->ClientFormatListResponse);
1862 return clipboard->context->ClientFormatListResponse(clipboard->context, &formatListResponse);
1870static UINT xf_cliprdr_monitor_ready(CliprdrClientContext* context,
1874 xfClipboard* clipboard = NULL;
1876 WINPR_ASSERT(context);
1877 WINPR_ASSERT(monitorReady);
1879 clipboard = cliprdr_file_context_get_context(context->custom);
1880 WINPR_ASSERT(clipboard);
1882 WINPR_UNUSED(monitorReady);
1884 if ((ret = xf_cliprdr_send_client_capabilities(clipboard)) != CHANNEL_RC_OK)
1887 xf_clipboard_formats_free(clipboard);
1889 if ((ret = xf_cliprdr_send_client_format_list(clipboard, TRUE)) != CHANNEL_RC_OK)
1892 clipboard->sync = TRUE;
1893 return CHANNEL_RC_OK;
1901static UINT xf_cliprdr_server_capabilities(CliprdrClientContext* context,
1905 const BYTE* capsPtr = NULL;
1906 xfClipboard* clipboard = NULL;
1908 WINPR_ASSERT(context);
1909 WINPR_ASSERT(capabilities);
1911 clipboard = cliprdr_file_context_get_context(context->custom);
1912 WINPR_ASSERT(clipboard);
1914 capsPtr = (
const BYTE*)capabilities->capabilitySets;
1915 WINPR_ASSERT(capsPtr);
1917 cliprdr_file_context_remote_set_flags(clipboard->file, 0);
1919 for (UINT32 i = 0; i < capabilities->cCapabilitiesSets; i++)
1923 if (caps->capabilitySetType == CB_CAPSTYPE_GENERAL)
1927 cliprdr_file_context_remote_set_flags(clipboard->file, generalCaps->generalFlags);
1930 capsPtr += caps->capabilitySetLength;
1933 return CHANNEL_RC_OK;
1936static void xf_cliprdr_prepare_to_set_selection_owner(xfContext* xfc, xfClipboard* clipboard)
1939 WINPR_ASSERT(clipboard);
1956 Atom value = clipboard->timestamp_property_atom;
1958 LogDynAndXChangeProperty(xfc->log, xfc->display, xfc->drawable,
1959 clipboard->timestamp_property_atom, XA_ATOM, 32, PropModeReplace,
1960 (
const BYTE*)&value, 1);
1961 LogDynAndXFlush(xfc->log, xfc->display);
1964static void xf_cliprdr_set_selection_owner(xfContext* xfc, xfClipboard* clipboard, Time timestamp)
1967 WINPR_ASSERT(clipboard);
1973 clipboard->selection_ownership_timestamp = timestamp;
1974 LogDynAndXSetSelectionOwner(xfc->log, xfc->display, clipboard->clipboard_atom, xfc->drawable,
1976 LogDynAndXFlush(xfc->log, xfc->display);
1984static UINT xf_cliprdr_server_format_list(CliprdrClientContext* context,
1987 xfContext* xfc = NULL;
1989 xfClipboard* clipboard = NULL;
1991 WINPR_ASSERT(context);
1992 WINPR_ASSERT(formatList);
1994 clipboard = cliprdr_file_context_get_context(context->custom);
1995 WINPR_ASSERT(clipboard);
1997 xfc = clipboard->xfc;
2003 free(clipboard->respond);
2004 clipboard->respond = NULL;
2006 xf_clipboard_formats_free(clipboard);
2007 xf_cliprdr_clear_cached_data(clipboard);
2008 requested_format_free(&clipboard->requestedFormat);
2010 xf_clipboard_free_server_formats(clipboard);
2012 clipboard->numServerFormats = formatList->numFormats + 1;
2014 if (!(clipboard->serverFormats =
2017 WLog_ERR(TAG,
"failed to allocate %d CLIPRDR_FORMAT structs", clipboard->numServerFormats);
2018 ret = CHANNEL_RC_NO_MEMORY;
2022 for (
size_t i = 0; i < formatList->numFormats; i++)
2027 srvFormat->formatId = format->formatId;
2029 if (format->formatName)
2031 srvFormat->formatName = _strdup(format->formatName);
2033 if (!srvFormat->formatName)
2035 for (UINT32 k = 0; k < i; k++)
2036 free(clipboard->serverFormats[k].formatName);
2038 clipboard->numServerFormats = 0;
2039 free(clipboard->serverFormats);
2040 clipboard->serverFormats = NULL;
2041 ret = CHANNEL_RC_NO_MEMORY;
2047 ClipboardLock(clipboard->system);
2048 ret = cliprdr_file_context_notify_new_server_format_list(clipboard->file);
2049 ClipboardUnlock(clipboard->system);
2055 CLIPRDR_FORMAT* format = &clipboard->serverFormats[formatList->numFormats];
2056 format->formatId = CF_RAW;
2057 format->formatName = NULL;
2059 xf_cliprdr_provide_server_format_list(clipboard);
2060 clipboard->numTargets = 2;
2062 for (
size_t i = 0; i < formatList->numFormats; i++)
2066 for (
size_t j = 0; j < clipboard->numClientFormats; j++)
2068 const xfCliprdrFormat* clientFormat = &clipboard->clientFormats[j];
2069 if (xf_cliprdr_formats_equal(format, clientFormat))
2071 if ((clientFormat->formatName != NULL) &&
2072 (strcmp(type_FileGroupDescriptorW, clientFormat->formatName) == 0))
2074 if (!cliprdr_file_context_has_local_support(clipboard->file))
2077 xf_cliprdr_append_target(clipboard, clientFormat->atom);
2082 ret = xf_cliprdr_send_client_format_list_response(clipboard, TRUE);
2083 if (xfc->remote_app)
2084 xf_cliprdr_set_selection_owner(xfc, clipboard, CurrentTime);
2086 xf_cliprdr_prepare_to_set_selection_owner(xfc, clipboard);
2099static UINT xf_cliprdr_server_format_list_response(
2100 WINPR_ATTR_UNUSED CliprdrClientContext* context,
2103 WINPR_ASSERT(context);
2104 WINPR_ASSERT(formatListResponse);
2106 return CHANNEL_RC_OK;
2115xf_cliprdr_server_format_data_request(CliprdrClientContext* context,
2118 const xfCliprdrFormat* format = NULL;
2120 WINPR_ASSERT(context);
2121 WINPR_ASSERT(formatDataRequest);
2123 xfClipboard* clipboard = cliprdr_file_context_get_context(context->custom);
2124 WINPR_ASSERT(clipboard);
2126 xfContext* xfc = clipboard->xfc;
2129 const uint32_t formatId = formatDataRequest->requestedFormatId;
2131 const BOOL rawTransfer = xf_cliprdr_is_raw_transfer_available(clipboard);
2135 format = xf_cliprdr_get_client_format_by_id(clipboard, CF_RAW);
2136 LogDynAndXChangeProperty(xfc->log, xfc->display, xfc->drawable, clipboard->property_atom,
2137 XA_INTEGER, 32, PropModeReplace, (
const BYTE*)&formatId, 1);
2140 format = xf_cliprdr_get_client_format_by_id(clipboard, formatId);
2142 clipboard->requestedFormatId = rawTransfer ? CF_RAW : formatId;
2144 return xf_cliprdr_send_data_response(clipboard, format, NULL, 0);
2146 DEBUG_CLIPRDR(
"requested format 0x%08" PRIx32
" [%s] {local 0x%08" PRIx32
"} [%s]",
2147 format->formatToRequest, ClipboardGetFormatIdString(format->formatToRequest),
2148 format->localFormat, format->formatName);
2149 LogDynAndXConvertSelection(xfc->log, xfc->display, clipboard->clipboard_atom, format->atom,
2150 clipboard->property_atom, xfc->drawable, CurrentTime);
2151 LogDynAndXFlush(xfc->log, xfc->display);
2153 return CHANNEL_RC_OK;
2162xf_cliprdr_server_format_data_response(CliprdrClientContext* context,
2166 BYTE* pDstData = NULL;
2169 UINT32 srcFormatId = 0;
2170 UINT32 dstFormatId = 0;
2171 BOOL nullTerminated = FALSE;
2173 const BYTE* data = NULL;
2174 xfContext* xfc = NULL;
2175 xfClipboard* clipboard = NULL;
2176 xfCachedData* cached_data = NULL;
2178 WINPR_ASSERT(context);
2179 WINPR_ASSERT(formatDataResponse);
2181 clipboard = cliprdr_file_context_get_context(context->custom);
2182 WINPR_ASSERT(clipboard);
2184 xfc = clipboard->xfc;
2187 size = formatDataResponse->common.dataLen;
2188 data = formatDataResponse->requestedFormatData;
2190 if (formatDataResponse->common.msgFlags == CB_RESPONSE_FAIL)
2192 WLog_WARN(TAG,
"Format Data Response PDU msgFlags is CB_RESPONSE_FAIL");
2193 free(clipboard->respond);
2194 clipboard->respond = NULL;
2195 return CHANNEL_RC_OK;
2198 if (!clipboard->respond)
2199 return CHANNEL_RC_OK;
2206 const RequestedFormat* format = clipboard->requestedFormat;
2207 if (clipboard->data_raw_format)
2209 srcFormatId = CF_RAW;
2210 dstFormatId = CF_RAW;
2213 return ERROR_INTERNAL_ERROR;
2214 else if (format->formatName)
2216 ClipboardLock(clipboard->system);
2217 if (strcmp(format->formatName, type_HtmlFormat) == 0)
2219 srcFormatId = ClipboardGetFormatId(clipboard->system, type_HtmlFormat);
2220 dstFormatId = ClipboardGetFormatId(clipboard->system, mime_html);
2221 nullTerminated = TRUE;
2224 if (strcmp(format->formatName, type_FileGroupDescriptorW) == 0)
2226 if (!cliprdr_file_context_update_server_data(clipboard->file, clipboard->system, data,
2228 WLog_WARN(TAG,
"failed to update file descriptors");
2230 srcFormatId = ClipboardGetFormatId(clipboard->system, type_FileGroupDescriptorW);
2231 const xfCliprdrFormat* dstTargetFormat =
2232 xf_cliprdr_get_client_format_by_atom(clipboard, clipboard->respond->target);
2233 if (!dstTargetFormat)
2235 dstFormatId = ClipboardGetFormatId(clipboard->system, mime_uri_list);
2239 dstFormatId = dstTargetFormat->localFormat;
2242 nullTerminated = TRUE;
2244 ClipboardUnlock(clipboard->system);
2248 srcFormatId = format->formatToRequest;
2249 dstFormatId = format->localFormat;
2250 switch (format->formatToRequest)
2253 nullTerminated = TRUE;
2257 nullTerminated = TRUE;
2260 case CF_UNICODETEXT:
2261 nullTerminated = TRUE;
2265 srcFormatId = CF_DIB;
2269 srcFormatId = CF_TIFF;
2277 DEBUG_CLIPRDR(
"requested format 0x%08" PRIx32
" [%s] {local 0x%08" PRIx32
"} [%s]",
2278 format->formatToRequest, ClipboardGetFormatIdString(format->formatToRequest),
2279 format->localFormat, format->formatName);
2282 DEBUG_CLIPRDR(
"srcFormatId: %u, dstFormatId: %u", srcFormatId, dstFormatId);
2284 ClipboardLock(clipboard->system);
2285 bSuccess = ClipboardSetData(clipboard->system, srcFormatId, data, SrcSize);
2287 BOOL willQuit = FALSE;
2292 WLog_DBG(TAG,
"skipping, empty data detected!");
2293 free(clipboard->respond);
2294 clipboard->respond = NULL;
2299 pDstData = (BYTE*)ClipboardGetData(clipboard->system, dstFormatId, &DstSize);
2303 WLog_WARN(TAG,
"failed to get clipboard data in format %s [source format %s]",
2304 ClipboardGetFormatName(clipboard->system, dstFormatId),
2305 ClipboardGetFormatName(clipboard->system, srcFormatId));
2308 if (nullTerminated && pDstData)
2310 BYTE* nullTerminator = memchr(pDstData,
'\0', DstSize);
2313 const intptr_t diff = nullTerminator - pDstData;
2314 WINPR_ASSERT(diff >= 0);
2315 WINPR_ASSERT(diff <= UINT32_MAX);
2316 DstSize = (UINT32)diff;
2321 ClipboardUnlock(clipboard->system);
2323 return CHANNEL_RC_OK;
2329 cached_data = xf_cached_data_new(pDstData, DstSize);
2332 WLog_WARN(TAG,
"Failed to allocate cache entry");
2334 return CHANNEL_RC_OK;
2336 if (!HashTable_Insert(clipboard->cachedData, format_to_cache_slot(dstFormatId),
2339 WLog_WARN(TAG,
"Failed to cache clipboard data");
2340 xf_cached_data_free(cached_data);
2341 return CHANNEL_RC_OK;
2351 xfCachedData* cached_raw_data = xf_cached_data_new_copy(data, size);
2352 if (!cached_raw_data)
2353 WLog_WARN(TAG,
"Failed to allocate cache entry");
2356 if (!HashTable_Insert(clipboard->cachedRawData, (
void*)(UINT_PTR)srcFormatId,
2359 WLog_WARN(TAG,
"Failed to cache clipboard data");
2360 xf_cached_data_free(cached_raw_data);
2367 xf_cliprdr_provide_data(clipboard, clipboard->respond, pDstData, DstSize);
2372 XSelectionEvent* sev;
2375 conv.sev = clipboard->respond;
2377 LogDynAndXSendEvent(xfc->log, xfc->display, clipboard->respond->requestor, 0, 0, conv.ev);
2378 LogDynAndXFlush(xfc->log, xfc->display);
2380 free(clipboard->respond);
2381 clipboard->respond = NULL;
2382 return CHANNEL_RC_OK;
2385static BOOL xf_cliprdr_is_valid_unix_filename(LPCWSTR filename)
2390 if (filename[0] == L
'\0')
2394 for (
const WCHAR* c = filename; *c; ++c)
2403xfClipboard* xf_clipboard_new(xfContext* xfc, BOOL relieveFilenameRestriction)
2406 rdpChannels* channels = NULL;
2407 xfClipboard* clipboard = NULL;
2408 const char* selectionAtom = NULL;
2409 xfCliprdrFormat* clientFormat = NULL;
2413 WINPR_ASSERT(xfc->common.context.settings);
2415 if (!(clipboard = (xfClipboard*)calloc(1,
sizeof(xfClipboard))))
2417 WLog_ERR(TAG,
"failed to allocate xfClipboard data");
2421 clipboard->file = cliprdr_file_context_new(clipboard);
2422 if (!clipboard->file)
2425 xfc->clipboard = clipboard;
2426 clipboard->xfc = xfc;
2427 channels = xfc->common.context.channels;
2428 clipboard->channels = channels;
2429 clipboard->system = ClipboardCreate();
2430 clipboard->requestedFormatId = UINT32_MAX;
2431 clipboard->root_window = DefaultRootWindow(xfc->display);
2436 selectionAtom =
"CLIPBOARD";
2438 clipboard->clipboard_atom = Logging_XInternAtom(xfc->log, xfc->display, selectionAtom, FALSE);
2440 if (clipboard->clipboard_atom == None)
2442 WLog_ERR(TAG,
"unable to get %s atom", selectionAtom);
2446 clipboard->timestamp_property_atom =
2447 Logging_XInternAtom(xfc->log, xfc->display,
"_FREERDP_TIMESTAMP_PROPERTY", FALSE);
2448 clipboard->property_atom =
2449 Logging_XInternAtom(xfc->log, xfc->display,
"_FREERDP_CLIPRDR", FALSE);
2450 clipboard->raw_transfer_atom =
2451 Logging_XInternAtom(xfc->log, xfc->display,
"_FREERDP_CLIPRDR_RAW", FALSE);
2452 clipboard->raw_format_list_atom =
2453 Logging_XInternAtom(xfc->log, xfc->display,
"_FREERDP_CLIPRDR_FORMATS", FALSE);
2454 xf_cliprdr_set_raw_transfer_enabled(clipboard, TRUE);
2455 XSelectInput(xfc->display, clipboard->root_window, PropertyChangeMask);
2458 if (XFixesQueryExtension(xfc->display, &clipboard->xfixes_event_base,
2459 &clipboard->xfixes_error_base))
2464 if (XFixesQueryVersion(xfc->display, &xfmajor, &xfminor))
2466 XFixesSelectSelectionInput(xfc->display, clipboard->root_window,
2467 clipboard->clipboard_atom,
2468 XFixesSetSelectionOwnerNotifyMask);
2469 clipboard->xfixes_supported = TRUE;
2473 WLog_ERR(TAG,
"Error querying X Fixes extension version");
2478 WLog_ERR(TAG,
"Error loading X Fixes extension");
2484 "Warning: Using clipboard redirection without XFIXES extension is strongly discouraged!");
2486 clientFormat = &clipboard->clientFormats[n++];
2487 clientFormat->atom = Logging_XInternAtom(xfc->log, xfc->display,
"_FREERDP_RAW", False);
2488 clientFormat->localFormat = clientFormat->formatToRequest = CF_RAW;
2490 clientFormat = &clipboard->clientFormats[n++];
2491 clientFormat->atom = Logging_XInternAtom(xfc->log, xfc->display,
"UTF8_STRING", False);
2492 clientFormat->formatToRequest = CF_UNICODETEXT;
2493 clientFormat->localFormat = ClipboardGetFormatId(xfc->clipboard->system, mime_text_plain);
2495 clientFormat = &clipboard->clientFormats[n++];
2496 clientFormat->atom = XA_STRING;
2497 clientFormat->formatToRequest = CF_TEXT;
2498 clientFormat->localFormat = ClipboardGetFormatId(xfc->clipboard->system, mime_text_plain);
2500 clientFormat = &clipboard->clientFormats[n++];
2501 clientFormat->atom = Logging_XInternAtom(xfc->log, xfc->display, mime_tiff, False);
2502 clientFormat->formatToRequest = clientFormat->localFormat = CF_TIFF;
2504 for (
size_t x = 0; x < ARRAYSIZE(mime_bitmap); x++)
2506 const char* mime_bmp = mime_bitmap[x];
2507 const DWORD format = ClipboardGetFormatId(xfc->clipboard->system, mime_bmp);
2510 WLog_DBG(TAG,
"skipping local bitmap format %s [NOT SUPPORTED]", mime_bmp);
2514 WLog_DBG(TAG,
"register local bitmap format %s [0x%08" PRIx32
"]", mime_bmp, format);
2515 clientFormat = &clipboard->clientFormats[n++];
2516 clientFormat->localFormat = format;
2517 clientFormat->atom = Logging_XInternAtom(xfc->log, xfc->display, mime_bmp, False);
2518 clientFormat->formatToRequest = CF_DIB;
2519 clientFormat->isImage = TRUE;
2522 for (
size_t x = 0; x < ARRAYSIZE(mime_images); x++)
2524 const char* mime_bmp = mime_images[x];
2525 const DWORD format = ClipboardGetFormatId(xfc->clipboard->system, mime_bmp);
2528 WLog_DBG(TAG,
"skipping local bitmap format %s [NOT SUPPORTED]", mime_bmp);
2532 WLog_DBG(TAG,
"register local bitmap format %s [0x%08" PRIx32
"]", mime_bmp, format);
2533 clientFormat = &clipboard->clientFormats[n++];
2534 clientFormat->localFormat = format;
2535 clientFormat->atom = Logging_XInternAtom(xfc->log, xfc->display, mime_bmp, False);
2536 clientFormat->formatToRequest = CF_DIB;
2537 clientFormat->isImage = TRUE;
2540 clientFormat = &clipboard->clientFormats[n++];
2541 clientFormat->atom = Logging_XInternAtom(xfc->log, xfc->display, mime_html, False);
2542 clientFormat->formatToRequest = ClipboardGetFormatId(xfc->clipboard->system, type_HtmlFormat);
2543 clientFormat->localFormat = ClipboardGetFormatId(xfc->clipboard->system, mime_html);
2544 clientFormat->formatName = _strdup(type_HtmlFormat);
2546 if (!clientFormat->formatName)
2549 clientFormat = &clipboard->clientFormats[n++];
2557 const UINT32 fgid = ClipboardGetFormatId(clipboard->system, type_FileGroupDescriptorW);
2558 const UINT32 uid = ClipboardGetFormatId(clipboard->system, mime_uri_list);
2561 cliprdr_file_context_set_locally_available(clipboard->file, TRUE);
2562 clientFormat->atom = Logging_XInternAtom(xfc->log, xfc->display, mime_uri_list, False);
2563 clientFormat->localFormat = uid;
2564 clientFormat->formatToRequest = fgid;
2565 clientFormat->formatName = _strdup(type_FileGroupDescriptorW);
2567 if (!clientFormat->formatName)
2570 clientFormat = &clipboard->clientFormats[n++];
2573 const UINT32 gid = ClipboardGetFormatId(clipboard->system, mime_gnome_copied_files);
2576 cliprdr_file_context_set_locally_available(clipboard->file, TRUE);
2577 clientFormat->atom =
2578 Logging_XInternAtom(xfc->log, xfc->display, mime_gnome_copied_files, False);
2579 clientFormat->localFormat = gid;
2580 clientFormat->formatToRequest = fgid;
2581 clientFormat->formatName = _strdup(type_FileGroupDescriptorW);
2583 if (!clientFormat->formatName)
2586 clientFormat = &clipboard->clientFormats[n++];
2589 const UINT32 mid = ClipboardGetFormatId(clipboard->system, mime_mate_copied_files);
2592 cliprdr_file_context_set_locally_available(clipboard->file, TRUE);
2593 clientFormat->atom =
2594 Logging_XInternAtom(xfc->log, xfc->display, mime_mate_copied_files, False);
2595 clientFormat->localFormat = mid;
2596 clientFormat->formatToRequest = fgid;
2597 clientFormat->formatName = _strdup(type_FileGroupDescriptorW);
2599 if (!clientFormat->formatName)
2603 clipboard->numClientFormats = WINPR_ASSERTING_INT_CAST(uint32_t, n);
2604 clipboard->targets[0] = Logging_XInternAtom(xfc->log, xfc->display,
"TIMESTAMP", FALSE);
2605 clipboard->targets[1] = Logging_XInternAtom(xfc->log, xfc->display,
"TARGETS", FALSE);
2606 clipboard->numTargets = 2;
2607 clipboard->incr_atom = Logging_XInternAtom(xfc->log, xfc->display,
"INCR", FALSE);
2609 if (relieveFilenameRestriction)
2611 WLog_DBG(TAG,
"Relieving CLIPRDR filename restriction");
2612 ClipboardGetDelegate(clipboard->system)->IsFileNameComponentValid =
2613 xf_cliprdr_is_valid_unix_filename;
2616 clipboard->cachedData = HashTable_New(TRUE);
2617 if (!clipboard->cachedData)
2620 obj = HashTable_ValueObject(clipboard->cachedData);
2621 obj->fnObjectFree = xf_cached_data_free;
2623 clipboard->cachedRawData = HashTable_New(TRUE);
2624 if (!clipboard->cachedRawData)
2627 obj = HashTable_ValueObject(clipboard->cachedRawData);
2628 obj->fnObjectFree = xf_cached_data_free;
2633 WINPR_PRAGMA_DIAG_PUSH
2634 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
2635 xf_clipboard_free(clipboard);
2636 WINPR_PRAGMA_DIAG_POP
2640void xf_clipboard_free(xfClipboard* clipboard)
2645 xf_clipboard_free_server_formats(clipboard);
2647 if (clipboard->numClientFormats)
2649 for (UINT32 i = 0; i < clipboard->numClientFormats; i++)
2651 xfCliprdrFormat* format = &clipboard->clientFormats[i];
2652 free(format->formatName);
2656 cliprdr_file_context_free(clipboard->file);
2658 ClipboardDestroy(clipboard->system);
2659 xf_clipboard_formats_free(clipboard);
2660 HashTable_Free(clipboard->cachedRawData);
2661 HashTable_Free(clipboard->cachedData);
2662 requested_format_free(&clipboard->requestedFormat);
2663 free(clipboard->respond);
2664 free(clipboard->incr_data);
2668void xf_cliprdr_init(xfContext* xfc, CliprdrClientContext* cliprdr)
2671 WINPR_ASSERT(cliprdr);
2673 xfc->cliprdr = cliprdr;
2674 xfc->clipboard->context = cliprdr;
2676 cliprdr->MonitorReady = xf_cliprdr_monitor_ready;
2677 cliprdr->ServerCapabilities = xf_cliprdr_server_capabilities;
2678 cliprdr->ServerFormatList = xf_cliprdr_server_format_list;
2679 cliprdr->ServerFormatListResponse = xf_cliprdr_server_format_list_response;
2680 cliprdr->ServerFormatDataRequest = xf_cliprdr_server_format_data_request;
2681 cliprdr->ServerFormatDataResponse = xf_cliprdr_server_format_data_response;
2683 cliprdr_file_context_init(xfc->clipboard->file, cliprdr);
2686void xf_cliprdr_uninit(xfContext* xfc, CliprdrClientContext* cliprdr)
2689 WINPR_ASSERT(cliprdr);
2691 xfc->cliprdr = NULL;
2695 ClipboardLock(xfc->clipboard->system);
2696 cliprdr_file_context_uninit(xfc->clipboard->file, cliprdr);
2697 ClipboardUnlock(xfc->clipboard->system);
2698 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.