FreeRDP
Loading...
Searching...
No Matches
xf_cliprdr.c
1
22#include <freerdp/config.h>
23
24#include <stdlib.h>
25#include <errno.h>
26
27#include <X11/Xlib.h>
28#include <X11/Xatom.h>
29
30#ifdef WITH_XFIXES
31#include <X11/extensions/Xfixes.h>
32#endif
33
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>
40
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>
46
47#include <freerdp/client/client_cliprdr_file.h>
48
49#include "xf_cliprdr.h"
50#include "xf_event.h"
51#include "xf_utils.h"
52
53#define TAG CLIENT_TAG("x11.cliprdr")
54
55#define MAX_CLIPBOARD_FORMATS 255
56
57#define DEBUG_CLIPRDR(...) WLog_DBG(TAG, __VA_ARGS__)
58
59typedef struct
60{
61 Atom atom;
62 UINT32 formatToRequest;
63 UINT32 localFormat;
64 char* formatName;
65 BOOL isImage;
66} xfCliprdrFormat;
67
68typedef struct
69{
70 BYTE* data;
71 UINT32 data_length;
72} xfCachedData;
73
74typedef struct
75{
76 UINT32 localFormat;
77 UINT32 formatToRequest;
78 char* formatName;
79} RequestedFormat;
80
81struct xf_clipboard
82{
83 xfContext* xfc;
84 rdpChannels* channels;
85 CliprdrClientContext* context;
86
87 wClipboard* system;
88
89 Window root_window;
90 Atom clipboard_atom;
91 Atom property_atom;
92
93 Atom timestamp_property_atom;
94 Time selection_ownership_timestamp;
95
96 Atom raw_transfer_atom;
97 Atom raw_format_list_atom;
98
99 UINT32 numClientFormats;
100 xfCliprdrFormat clientFormats[20];
101
102 UINT32 numServerFormats;
103 CLIPRDR_FORMAT* serverFormats;
104
105 size_t numTargets;
106 Atom targets[20];
107
108 UINT32 requestedFormatId;
109
110 wHashTable* cachedData;
111 wHashTable* cachedRawData;
112
113 BOOL data_raw_format;
114
115 RequestedFormat* requestedFormat;
116
117 XSelectionEvent* respond;
118
119 Window owner;
120 BOOL sync;
121
122 /* INCR mechanism */
123 Atom incr_atom;
124 BOOL incr_starts;
125 BYTE* incr_data;
126 size_t incr_data_length;
127 long event_mask;
128
129 /* XFixes extension */
130 int xfixes_event_base;
131 int xfixes_error_base;
132 BOOL xfixes_supported;
133
134 /* last sent data */
135 CLIPRDR_FORMAT* lastSentFormats;
136 UINT32 lastSentNumFormats;
137 CliprdrFileContext* file;
138 BOOL isImageContent;
139};
140
141static const char mime_text_plain[] = "text/plain";
142static const char mime_uri_list[] = "text/uri-list";
143static const char mime_html[] = "text/html";
144static const char* mime_bitmap[] = { "image/bmp", "image/x-bmp", "image/x-MS-bmp",
145 "image/x-win-bitmap" };
146static const char mime_webp[] = "image/webp";
147static const char mime_png[] = "image/png";
148static const char mime_jpeg[] = "image/jpeg";
149static const char mime_tiff[] = "image/tiff";
150static const char* mime_images[] = { mime_webp, mime_png, mime_jpeg, mime_tiff };
151
152static const char mime_gnome_copied_files[] = "x-special/gnome-copied-files";
153static const char mime_mate_copied_files[] = "x-special/mate-copied-files";
154
155static const char type_FileGroupDescriptorW[] = "FileGroupDescriptorW";
156static const char type_HtmlFormat[] = "HTML Format";
157
158static void xf_cliprdr_clear_cached_data(xfClipboard* clipboard);
159static UINT xf_cliprdr_send_client_format_list(xfClipboard* clipboard, BOOL force);
160static void xf_cliprdr_set_selection_owner(xfContext* xfc, xfClipboard* clipboard, Time timestamp);
161
162static void requested_format_free(RequestedFormat** ppRequestedFormat)
163{
164 if (!ppRequestedFormat)
165 return;
166 if (!(*ppRequestedFormat))
167 return;
168
169 free((*ppRequestedFormat)->formatName);
170 free(*ppRequestedFormat);
171 *ppRequestedFormat = nullptr;
172}
173
174static BOOL requested_format_replace(RequestedFormat** ppRequestedFormat, UINT32 remoteFormatId,
175 UINT32 localFormatId, const char* formatName)
176{
177 if (!ppRequestedFormat)
178 return FALSE;
179
180 requested_format_free(ppRequestedFormat);
181 RequestedFormat* requested = calloc(1, sizeof(RequestedFormat));
182 if (!requested)
183 return FALSE;
184 requested->localFormat = localFormatId;
185 requested->formatToRequest = remoteFormatId;
186 if (formatName)
187 {
188 requested->formatName = _strdup(formatName);
189 if (!requested->formatName)
190 {
191 free(requested);
192 return FALSE;
193 }
194 }
195
196 *ppRequestedFormat = requested;
197 return TRUE;
198}
199
200static void xf_cached_data_free(void* ptr)
201{
202 xfCachedData* cached_data = ptr;
203 if (!cached_data)
204 return;
205
206 free(cached_data->data);
207 free(cached_data);
208}
209
210static xfCachedData* xf_cached_data_new(BYTE* data, size_t data_length)
211{
212 if (data_length > UINT32_MAX)
213 return nullptr;
214
215 xfCachedData* cached_data = calloc(1, sizeof(xfCachedData));
216 if (!cached_data)
217 return nullptr;
218
219 cached_data->data = data;
220 cached_data->data_length = (UINT32)data_length;
221
222 return cached_data;
223}
224
225static xfCachedData* xf_cached_data_new_copy(const BYTE* data, size_t data_length)
226{
227 BYTE* copy = nullptr;
228 if (data_length > 0)
229 {
230 copy = calloc(data_length + 1, sizeof(BYTE));
231 if (!copy)
232 return nullptr;
233 memcpy(copy, data, data_length);
234 }
235
236 xfCachedData* cache = xf_cached_data_new(copy, data_length);
237 if (!cache)
238 free(copy);
239 return cache;
240}
241
242static void xf_clipboard_free_server_formats(xfClipboard* clipboard)
243{
244 WINPR_ASSERT(clipboard);
245 if (clipboard->serverFormats)
246 {
247 for (size_t i = 0; i < clipboard->numServerFormats; i++)
248 {
249 CLIPRDR_FORMAT* format = &clipboard->serverFormats[i];
250 free(format->formatName);
251 }
252
253 free(clipboard->serverFormats);
254 clipboard->serverFormats = nullptr;
255 }
256}
257
258static BOOL xf_cliprdr_update_owner(xfClipboard* clipboard)
259{
260 WINPR_ASSERT(clipboard);
261
262 xfContext* xfc = clipboard->xfc;
263 WINPR_ASSERT(xfc);
264
265 if (!clipboard->sync)
266 return FALSE;
267
268 Window owner = LogDynAndXGetSelectionOwner(xfc->log, xfc->display, clipboard->clipboard_atom);
269 if (clipboard->owner == owner)
270 return FALSE;
271
272 clipboard->owner = owner;
273 return TRUE;
274}
275
276static void xf_cliprdr_check_owner(xfClipboard* clipboard)
277{
278 if (xf_cliprdr_update_owner(clipboard))
279 xf_cliprdr_send_client_format_list(clipboard, FALSE);
280}
281
282static BOOL xf_cliprdr_is_self_owned(xfClipboard* clipboard)
283{
284 xfContext* xfc = nullptr;
285
286 WINPR_ASSERT(clipboard);
287
288 xfc = clipboard->xfc;
289 WINPR_ASSERT(xfc);
290 return LogDynAndXGetSelectionOwner(xfc->log, xfc->display, clipboard->clipboard_atom) ==
291 xfc->drawable;
292}
293
294static void xf_cliprdr_set_raw_transfer_enabled(xfClipboard* clipboard, BOOL enabled)
295{
296 UINT32 data = WINPR_ASSERTING_INT_CAST(uint32_t, enabled);
297 xfContext* xfc = nullptr;
298
299 WINPR_ASSERT(clipboard);
300
301 xfc = clipboard->xfc;
302 WINPR_ASSERT(xfc);
303 LogDynAndXChangeProperty(xfc->log, xfc->display, xfc->drawable, clipboard->raw_transfer_atom,
304 XA_INTEGER, 32, PropModeReplace, (const BYTE*)&data, 1);
305}
306
307static BOOL xf_cliprdr_is_raw_transfer_available(xfClipboard* clipboard)
308{
309 Atom type = 0;
310 int format = 0;
311 int result = 0;
312 unsigned long length = 0;
313 unsigned long bytes_left = 0;
314 UINT32* data = nullptr;
315 UINT32 is_enabled = 0;
316 Window owner = None;
317 xfContext* xfc = nullptr;
318
319 WINPR_ASSERT(clipboard);
320
321 xfc = clipboard->xfc;
322 WINPR_ASSERT(xfc);
323
324 owner = LogDynAndXGetSelectionOwner(xfc->log, xfc->display, clipboard->clipboard_atom);
325
326 if (owner != None)
327 {
328 result = LogDynAndXGetWindowProperty(xfc->log, xfc->display, owner,
329 clipboard->raw_transfer_atom, 0, 4, 0, XA_INTEGER,
330 &type, &format, &length, &bytes_left, (BYTE**)&data);
331 }
332
333 if (data)
334 {
335 is_enabled = *data;
336 XFree(data);
337 }
338
339 if ((owner == None) || (owner == xfc->drawable))
340 return FALSE;
341
342 if (result != Success)
343 return FALSE;
344
345 return is_enabled != 0;
346}
347
348static BOOL xf_cliprdr_formats_equal(const CLIPRDR_FORMAT* server, const xfCliprdrFormat* client)
349{
350 WINPR_ASSERT(server);
351 WINPR_ASSERT(client);
352
353 if (server->formatName && client->formatName)
354 {
355 /* The server may be using short format names while we store them in full form. */
356 return (0 == strncmp(server->formatName, client->formatName, strlen(server->formatName)));
357 }
358
359 if (!server->formatName && !client->formatName)
360 {
361 return (server->formatId == client->formatToRequest);
362 }
363
364 return FALSE;
365}
366
367static const xfCliprdrFormat* xf_cliprdr_get_client_format_by_id(xfClipboard* clipboard,
368 UINT32 formatId)
369{
370 WINPR_ASSERT(clipboard);
371
372 const BOOL formatIsHtml = formatId == ClipboardGetFormatId(clipboard->system, type_HtmlFormat);
373 const BOOL fetchImage = clipboard->isImageContent && formatIsHtml;
374 for (size_t index = 0; index < clipboard->numClientFormats; index++)
375 {
376 const xfCliprdrFormat* format = &(clipboard->clientFormats[index]);
377
378 if (fetchImage && format->isImage)
379 return format;
380
381 if (format->formatToRequest == formatId)
382 return format;
383 }
384
385 return nullptr;
386}
387
388static const xfCliprdrFormat* xf_cliprdr_get_client_format_by_atom(xfClipboard* clipboard,
389 Atom atom)
390{
391 WINPR_ASSERT(clipboard);
392
393 for (UINT32 i = 0; i < clipboard->numClientFormats; i++)
394 {
395 const xfCliprdrFormat* format = &(clipboard->clientFormats[i]);
396
397 if (format->atom == atom)
398 return format;
399 }
400
401 return nullptr;
402}
403
404static const CLIPRDR_FORMAT* xf_cliprdr_get_server_format_by_atom(xfClipboard* clipboard, Atom atom)
405{
406 WINPR_ASSERT(clipboard);
407
408 for (size_t i = 0; i < clipboard->numClientFormats; i++)
409 {
410 const xfCliprdrFormat* client_format = &(clipboard->clientFormats[i]);
411
412 if (client_format->atom == atom)
413 {
414 for (size_t j = 0; j < clipboard->numServerFormats; j++)
415 {
416 const CLIPRDR_FORMAT* server_format = &(clipboard->serverFormats[j]);
417
418 if (xf_cliprdr_formats_equal(server_format, client_format))
419 return server_format;
420 }
421 }
422 }
423
424 return nullptr;
425}
426
432static UINT xf_cliprdr_send_data_request(xfClipboard* clipboard, UINT32 formatId,
433 WINPR_ATTR_UNUSED const xfCliprdrFormat* cformat)
434{
435 CLIPRDR_FORMAT_DATA_REQUEST request = WINPR_C_ARRAY_INIT;
436 request.requestedFormatId = formatId;
437
438 DEBUG_CLIPRDR("requesting format 0x%08" PRIx32 " [%s] {local 0x%08" PRIx32 "} [%s]", formatId,
439 ClipboardGetFormatIdString(formatId), cformat->localFormat, cformat->formatName);
440
441 WINPR_ASSERT(clipboard);
442 WINPR_ASSERT(clipboard->context);
443 WINPR_ASSERT(clipboard->context->ClientFormatDataRequest);
444 return clipboard->context->ClientFormatDataRequest(clipboard->context, &request);
445}
446
452static UINT xf_cliprdr_send_data_response(xfClipboard* clipboard, const xfCliprdrFormat* format,
453 const BYTE* data, size_t size)
454{
455 CLIPRDR_FORMAT_DATA_RESPONSE response = WINPR_C_ARRAY_INIT;
456
457 WINPR_ASSERT(clipboard);
458
459 /* No request currently pending, do not send a response. */
460 if (clipboard->requestedFormatId == UINT32_MAX)
461 return CHANNEL_RC_OK;
462
463 if (size == 0)
464 {
465 if (format)
466 DEBUG_CLIPRDR("send CB_RESPONSE_FAIL response {format 0x%08" PRIx32
467 " [%s] {local 0x%08" PRIx32 "} [%s]",
468 format->formatToRequest,
469 ClipboardGetFormatIdString(format->formatToRequest), format->localFormat,
470 format->formatName);
471 else
472 DEBUG_CLIPRDR("send CB_RESPONSE_FAIL response");
473 }
474 else
475 {
476 WINPR_ASSERT(format);
477 DEBUG_CLIPRDR("send response format 0x%08" PRIx32 " [%s] {local 0x%08" PRIx32 " [%s]} [%s]",
478 format->formatToRequest, ClipboardGetFormatIdString(format->formatToRequest),
479 format->localFormat,
480 ClipboardGetFormatName(clipboard->system, format->localFormat),
481 format->formatName);
482 }
483 /* Request handled, reset to invalid */
484 clipboard->requestedFormatId = UINT32_MAX;
485
486 response.common.msgFlags = (data) ? CB_RESPONSE_OK : CB_RESPONSE_FAIL;
487
488 WINPR_ASSERT(size <= UINT32_MAX);
489 response.common.dataLen = (UINT32)size;
490 response.requestedFormatData = data;
491
492 WINPR_ASSERT(clipboard->context);
493 WINPR_ASSERT(clipboard->context->ClientFormatDataResponse);
494 return clipboard->context->ClientFormatDataResponse(clipboard->context, &response);
495}
496
497static wStream* xf_cliprdr_serialize_server_format_list(xfClipboard* clipboard)
498{
499 UINT32 formatCount = 0;
500 wStream* s = nullptr;
501
502 WINPR_ASSERT(clipboard);
503
504 /* Typical MS Word format list is about 80 bytes long. */
505 if (!(s = Stream_New(nullptr, 128)))
506 {
507 WLog_ERR(TAG, "failed to allocate serialized format list");
508 goto error;
509 }
510
511 /* If present, the last format is always synthetic CF_RAW. Do not include it. */
512 formatCount = (clipboard->numServerFormats > 0) ? clipboard->numServerFormats - 1 : 0;
513 Stream_Write_UINT32(s, formatCount);
514
515 for (UINT32 i = 0; i < formatCount; i++)
516 {
517 CLIPRDR_FORMAT* format = &clipboard->serverFormats[i];
518 size_t name_length = format->formatName ? strlen(format->formatName) : 0;
519
520 DEBUG_CLIPRDR("server announced 0x%08" PRIx32 " [%s][%s]", format->formatId,
521 ClipboardGetFormatIdString(format->formatId), format->formatName);
522 if (!Stream_EnsureRemainingCapacity(s, sizeof(UINT32) + name_length + 1))
523 {
524 WLog_ERR(TAG, "failed to expand serialized format list");
525 goto error;
526 }
527
528 Stream_Write_UINT32(s, format->formatId);
529
530 if (format->formatName)
531 Stream_Write(s, format->formatName, name_length);
532
533 Stream_Write_UINT8(s, '\0');
534 }
535
536 Stream_SealLength(s);
537 return s;
538error:
539 Stream_Free(s, TRUE);
540 return nullptr;
541}
542
543static CLIPRDR_FORMAT* xf_cliprdr_parse_server_format_list(BYTE* data, size_t length,
544 UINT32* numFormats)
545{
546 wStream* s = nullptr;
547 CLIPRDR_FORMAT* formats = nullptr;
548
549 WINPR_ASSERT(data || (length == 0));
550 WINPR_ASSERT(numFormats);
551
552 if (!(s = Stream_New(data, length)))
553 {
554 WLog_ERR(TAG, "failed to allocate stream for parsing serialized format list");
555 goto error;
556 }
557
558 if (!Stream_CheckAndLogRequiredLength(TAG, s, sizeof(UINT32)))
559 goto error;
560
561 Stream_Read_UINT32(s, *numFormats);
562
563 if (*numFormats > MAX_CLIPBOARD_FORMATS)
564 {
565 WLog_ERR(TAG, "unexpectedly large number of formats: %" PRIu32 "", *numFormats);
566 goto error;
567 }
568
569 if (!(formats = (CLIPRDR_FORMAT*)calloc(*numFormats, sizeof(CLIPRDR_FORMAT))))
570 {
571 WLog_ERR(TAG, "failed to allocate format list");
572 goto error;
573 }
574
575 for (UINT32 i = 0; i < *numFormats; i++)
576 {
577 const char* formatName = nullptr;
578 size_t formatNameLength = 0;
579
580 if (!Stream_CheckAndLogRequiredLength(TAG, s, sizeof(UINT32)))
581 goto error;
582
583 Stream_Read_UINT32(s, formats[i].formatId);
584 formatName = (const char*)Stream_Pointer(s);
585 formatNameLength = strnlen(formatName, Stream_GetRemainingLength(s));
586
587 if (formatNameLength == Stream_GetRemainingLength(s))
588 {
589 WLog_ERR(TAG, "missing terminating null byte, %" PRIuz " bytes left to read",
590 formatNameLength);
591 goto error;
592 }
593
594 formats[i].formatName = strndup(formatName, formatNameLength);
595 Stream_Seek(s, formatNameLength + 1);
596 }
597
598 Stream_Free(s, FALSE);
599 return formats;
600error:
601 Stream_Free(s, FALSE);
602 free(formats);
603 *numFormats = 0;
604 return nullptr;
605}
606
607static void xf_cliprdr_free_formats(CLIPRDR_FORMAT* formats, UINT32 numFormats)
608{
609 WINPR_ASSERT(formats || (numFormats == 0));
610
611 for (UINT32 i = 0; i < numFormats; i++)
612 {
613 free(formats[i].formatName);
614 }
615
616 free(formats);
617}
618
619static CLIPRDR_FORMAT* xf_cliprdr_get_raw_server_formats(xfClipboard* clipboard, UINT32* numFormats)
620{
621 Atom type = None;
622 int format = 0;
623 unsigned long length = 0;
624 unsigned long remaining = 0;
625 BYTE* data = nullptr;
626 CLIPRDR_FORMAT* formats = nullptr;
627 xfContext* xfc = nullptr;
628
629 WINPR_ASSERT(clipboard);
630 WINPR_ASSERT(numFormats);
631
632 xfc = clipboard->xfc;
633 WINPR_ASSERT(xfc);
634
635 *numFormats = 0;
636
637 Window owner = LogDynAndXGetSelectionOwner(xfc->log, xfc->display, clipboard->clipboard_atom);
638 LogDynAndXGetWindowProperty(xfc->log, xfc->display, owner, clipboard->raw_format_list_atom, 0,
639 4096, False, clipboard->raw_format_list_atom, &type, &format,
640 &length, &remaining, &data);
641
642 if (data && length > 0 && format == 8 && type == clipboard->raw_format_list_atom)
643 {
644 formats = xf_cliprdr_parse_server_format_list(data, length, numFormats);
645 }
646 else
647 {
648 WLog_ERR(TAG,
649 "failed to retrieve raw format list: data=%p, length=%lu, format=%d, type=%lu "
650 "(expected=%lu)",
651 (void*)data, length, format, (unsigned long)type,
652 (unsigned long)clipboard->raw_format_list_atom);
653 }
654
655 if (data)
656 XFree(data);
657
658 return formats;
659}
660
661static BOOL xf_cliprdr_should_add_format(const CLIPRDR_FORMAT* formats, size_t count,
662 const xfCliprdrFormat* xformat)
663{
664 WINPR_ASSERT(formats);
665
666 if (!xformat)
667 return FALSE;
668
669 for (size_t x = 0; x < count; x++)
670 {
671 const CLIPRDR_FORMAT* format = &formats[x];
672 if (format->formatId == xformat->formatToRequest)
673 return FALSE;
674 }
675 return TRUE;
676}
677
678static CLIPRDR_FORMAT* xf_cliprdr_get_formats_from_targets(xfClipboard* clipboard,
679 UINT32* numFormats)
680{
681 Atom atom = None;
682 BYTE* data = nullptr;
683 int format_property = 0;
684 unsigned long proplength = 0;
685 unsigned long bytes_left = 0;
686 CLIPRDR_FORMAT* formats = nullptr;
687
688 WINPR_ASSERT(clipboard);
689 WINPR_ASSERT(numFormats);
690
691 xfContext* xfc = clipboard->xfc;
692 WINPR_ASSERT(xfc);
693
694 *numFormats = 0;
695 LogDynAndXGetWindowProperty(xfc->log, xfc->display, xfc->drawable, clipboard->property_atom, 0,
696 200, 0, XA_ATOM, &atom, &format_property, &proplength, &bytes_left,
697 &data);
698
699 if (proplength > 0)
700 {
701 unsigned long length = proplength + 1;
702 if (!data)
703 {
704 WLog_ERR(TAG, "XGetWindowProperty set length = %lu but data is nullptr", length);
705 goto out;
706 }
707
708 if (!(formats = (CLIPRDR_FORMAT*)calloc(length, sizeof(CLIPRDR_FORMAT))))
709 {
710 WLog_ERR(TAG, "failed to allocate %lu CLIPRDR_FORMAT structs", length);
711 goto out;
712 }
713 }
714
715 {
716 BOOL isImage = FALSE;
717 BOOL hasHtml = FALSE;
718 const uint32_t htmlFormatId = ClipboardRegisterFormat(clipboard->system, type_HtmlFormat);
719 for (unsigned long i = 0; i < proplength; i++)
720 {
721 Atom tatom = ((Atom*)data)[i];
722 const xfCliprdrFormat* format = xf_cliprdr_get_client_format_by_atom(clipboard, tatom);
723
724 if (xf_cliprdr_should_add_format(formats, *numFormats, format))
725 {
726 CLIPRDR_FORMAT* cformat = &formats[*numFormats];
727 cformat->formatId = format->formatToRequest;
728
729 /* We do not want to double register a format, so check if HTML was already
730 * registered.
731 */
732 if (cformat->formatId == htmlFormatId)
733 hasHtml = TRUE;
734
735 /* These are standard image types that will always be registered regardless of
736 * actual image format. */
737 if (cformat->formatId == CF_TIFF)
738 isImage = TRUE;
739 else if (cformat->formatId == CF_DIB)
740 isImage = TRUE;
741 else if (cformat->formatId == CF_DIBV5)
742 isImage = TRUE;
743
744 if (format->formatName)
745 {
746 cformat->formatName = _strdup(format->formatName);
747 WINPR_ASSERT(cformat->formatName);
748 }
749 else
750 cformat->formatName = nullptr;
751
752 *numFormats += 1;
753 }
754 }
755
756 clipboard->isImageContent = isImage;
757 if (isImage && !hasHtml)
758 {
759 CLIPRDR_FORMAT* cformat = &formats[*numFormats];
760 cformat->formatId = htmlFormatId;
761 cformat->formatName = _strdup(type_HtmlFormat);
762
763 *numFormats += 1;
764 }
765 }
766out:
767
768 if (data)
769 XFree(data);
770
771 return formats;
772}
773
774static CLIPRDR_FORMAT* xf_cliprdr_get_client_formats(xfClipboard* clipboard, UINT32* numFormats)
775{
776 CLIPRDR_FORMAT* formats = nullptr;
777
778 WINPR_ASSERT(clipboard);
779 WINPR_ASSERT(numFormats);
780
781 *numFormats = 0;
782
783 if (xf_cliprdr_is_raw_transfer_available(clipboard))
784 {
785 formats = xf_cliprdr_get_raw_server_formats(clipboard, numFormats);
786 }
787
788 if (*numFormats == 0)
789 {
790 xf_cliprdr_free_formats(formats, *numFormats);
791 formats = xf_cliprdr_get_formats_from_targets(clipboard, numFormats);
792 }
793
794 return formats;
795}
796
797static void xf_cliprdr_provide_server_format_list(xfClipboard* clipboard)
798{
799 wStream* formats = nullptr;
800 xfContext* xfc = nullptr;
801
802 WINPR_ASSERT(clipboard);
803
804 xfc = clipboard->xfc;
805 WINPR_ASSERT(xfc);
806
807 formats = xf_cliprdr_serialize_server_format_list(clipboard);
808
809 if (formats)
810 {
811 const size_t len = Stream_Length(formats);
812 WINPR_ASSERT(len <= INT32_MAX);
813 LogDynAndXChangeProperty(xfc->log, xfc->display, xfc->drawable,
814 clipboard->raw_format_list_atom, clipboard->raw_format_list_atom,
815 8, PropModeReplace, Stream_Buffer(formats), (int)len);
816 }
817 else
818 {
819 LogDynAndXDeleteProperty(xfc->log, xfc->display, xfc->drawable,
820 clipboard->raw_format_list_atom);
821 }
822
823 Stream_Free(formats, TRUE);
824}
825
826static BOOL xf_clipboard_format_equal(const CLIPRDR_FORMAT* a, const CLIPRDR_FORMAT* b)
827{
828 WINPR_ASSERT(a);
829 WINPR_ASSERT(b);
830
831 if (a->formatId != b->formatId)
832 return FALSE;
833 if (!a->formatName && !b->formatName)
834 return TRUE;
835 if (!a->formatName || !b->formatName)
836 return FALSE;
837 return strcmp(a->formatName, b->formatName) == 0;
838}
839
840static BOOL xf_clipboard_changed(xfClipboard* clipboard, const CLIPRDR_FORMAT* formats,
841 UINT32 numFormats)
842{
843 WINPR_ASSERT(clipboard);
844 WINPR_ASSERT(formats || (numFormats == 0));
845
846 if (clipboard->lastSentNumFormats != numFormats)
847 return TRUE;
848
849 for (UINT32 x = 0; x < numFormats; x++)
850 {
851 const CLIPRDR_FORMAT* cur = &clipboard->lastSentFormats[x];
852 BOOL contained = FALSE;
853 for (UINT32 y = 0; y < numFormats; y++)
854 {
855 if (xf_clipboard_format_equal(cur, &formats[y]))
856 {
857 contained = TRUE;
858 break;
859 }
860 }
861 if (!contained)
862 return TRUE;
863 }
864
865 return FALSE;
866}
867
868static void xf_clipboard_formats_free(xfClipboard* clipboard)
869{
870 WINPR_ASSERT(clipboard);
871
872 /* Synchronize RDP/X11 thread with channel thread.
873 * Reset the pointer and count inside the lock so that
874 * xf_clipboard_changed cannot observe a freed-but-non-NULL pointer. */
875 xf_lock_x11(clipboard->xfc);
876 xf_cliprdr_free_formats(clipboard->lastSentFormats, clipboard->lastSentNumFormats);
877 clipboard->lastSentFormats = nullptr;
878 clipboard->lastSentNumFormats = 0;
879 xf_unlock_x11(clipboard->xfc);
880}
881
882static BOOL xf_clipboard_copy_formats(xfClipboard* clipboard, const CLIPRDR_FORMAT* formats,
883 UINT32 numFormats)
884{
885 WINPR_ASSERT(clipboard);
886 WINPR_ASSERT(formats || (numFormats == 0));
887
888 xf_clipboard_formats_free(clipboard);
889 if (numFormats > 0)
890 clipboard->lastSentFormats = calloc(numFormats, sizeof(CLIPRDR_FORMAT));
891 if (!clipboard->lastSentFormats)
892 return FALSE;
893 clipboard->lastSentNumFormats = numFormats;
894 for (UINT32 x = 0; x < numFormats; x++)
895 {
896 CLIPRDR_FORMAT* lcur = &clipboard->lastSentFormats[x];
897 const CLIPRDR_FORMAT* cur = &formats[x];
898 *lcur = *cur;
899 if (cur->formatName)
900 lcur->formatName = _strdup(cur->formatName);
901 }
902 return FALSE;
903}
904
905static UINT xf_cliprdr_send_format_list(xfClipboard* clipboard, const CLIPRDR_FORMAT* formats,
906 UINT32 numFormats, BOOL force)
907{
908 union
909 {
910 const CLIPRDR_FORMAT* cpv;
911 CLIPRDR_FORMAT* pv;
912 } cnv = { .cpv = formats };
913 const CLIPRDR_FORMAT_LIST formatList = { .common.msgFlags = 0,
914 .numFormats = numFormats,
915 .formats = cnv.pv,
916 .common.msgType = CB_FORMAT_LIST };
917 UINT ret = 0;
918
919 WINPR_ASSERT(clipboard);
920 WINPR_ASSERT(formats || (numFormats == 0));
921
922 if (!force && !xf_clipboard_changed(clipboard, formats, numFormats))
923 return CHANNEL_RC_OK;
924
925#if defined(WITH_DEBUG_CLIPRDR)
926 for (UINT32 x = 0; x < numFormats; x++)
927 {
928 const CLIPRDR_FORMAT* format = &formats[x];
929 DEBUG_CLIPRDR("announcing format 0x%08" PRIx32 " [%s] [%s]", format->formatId,
930 ClipboardGetFormatIdString(format->formatId), format->formatName);
931 }
932#endif
933
934 xf_clipboard_copy_formats(clipboard, formats, numFormats);
935 /* Ensure all pending requests are answered. */
936 xf_cliprdr_send_data_response(clipboard, nullptr, nullptr, 0);
937
938 xf_cliprdr_clear_cached_data(clipboard);
939
940 ret = cliprdr_file_context_notify_new_client_format_list(clipboard->file);
941 if (ret)
942 return ret;
943
944 WINPR_ASSERT(clipboard->context);
945 WINPR_ASSERT(clipboard->context->ClientFormatList);
946 return clipboard->context->ClientFormatList(clipboard->context, &formatList);
947}
948
949static void xf_cliprdr_get_requested_targets(xfClipboard* clipboard)
950{
951 UINT32 numFormats = 0;
952 CLIPRDR_FORMAT* formats = xf_cliprdr_get_client_formats(clipboard, &numFormats);
953 xf_cliprdr_send_format_list(clipboard, formats, numFormats, FALSE);
954 xf_cliprdr_free_formats(formats, numFormats);
955}
956
957static void xf_cliprdr_process_requested_data(xfClipboard* clipboard, BOOL hasData,
958 const BYTE* data, size_t size)
959{
960 BOOL bSuccess = 0;
961 UINT32 SrcSize = 0;
962 UINT32 DstSize = 0;
963 INT64 srcFormatId = -1;
964 BYTE* pDstData = nullptr;
965 const xfCliprdrFormat* format = nullptr;
966
967 WINPR_ASSERT(clipboard);
968
969 if (clipboard->incr_starts && hasData)
970 return;
971
972 /* Reset incr_data_length, as we've reached the end of a possible incremental update.
973 * this ensures on next event that the buffer is not reused. */
974 clipboard->incr_data_length = 0;
975
976 format = xf_cliprdr_get_client_format_by_id(clipboard, clipboard->requestedFormatId);
977
978 if (!hasData || !data || !format)
979 {
980 xf_cliprdr_send_data_response(clipboard, format, nullptr, 0);
981 return;
982 }
983
984 switch (format->formatToRequest)
985 {
986 case CF_RAW:
987 srcFormatId = CF_RAW;
988 break;
989
990 case CF_TEXT:
991 case CF_OEMTEXT:
992 case CF_UNICODETEXT:
993 srcFormatId = format->localFormat;
994 break;
995
996 default:
997 srcFormatId = format->localFormat;
998 break;
999 }
1000
1001 if (srcFormatId < 0)
1002 {
1003 xf_cliprdr_send_data_response(clipboard, format, nullptr, 0);
1004 return;
1005 }
1006
1007 ClipboardLock(clipboard->system);
1008 SrcSize = (UINT32)size;
1009 bSuccess = ClipboardSetData(clipboard->system, (UINT32)srcFormatId, data, SrcSize);
1010
1011 if (bSuccess)
1012 {
1013 DstSize = 0;
1014 pDstData =
1015 (BYTE*)ClipboardGetData(clipboard->system, clipboard->requestedFormatId, &DstSize);
1016 }
1017 ClipboardUnlock(clipboard->system);
1018
1019 if (!pDstData)
1020 {
1021 xf_cliprdr_send_data_response(clipboard, format, nullptr, 0);
1022 return;
1023 }
1024
1025 /*
1026 * File lists require a bit of postprocessing to convert them from WinPR's FILDESCRIPTOR
1027 * format to CLIPRDR_FILELIST expected by the server.
1028 *
1029 * We check for "FileGroupDescriptorW" format being registered (i.e., nonzero) in order
1030 * to not process CF_RAW as a file list in case WinPR does not support file transfers.
1031 */
1032 ClipboardLock(clipboard->system);
1033 if (format->formatToRequest &&
1034 (format->formatToRequest ==
1035 ClipboardGetFormatId(clipboard->system, type_FileGroupDescriptorW)))
1036 {
1037 UINT error = NO_ERROR;
1038 FILEDESCRIPTORW* file_array = (FILEDESCRIPTORW*)pDstData;
1039 UINT32 file_count = DstSize / sizeof(FILEDESCRIPTORW);
1040 pDstData = nullptr;
1041 DstSize = 0;
1042
1043 const UINT32 flags = cliprdr_file_context_remote_get_flags(clipboard->file);
1044 error = cliprdr_serialize_file_list_ex(flags, file_array, file_count, &pDstData, &DstSize);
1045
1046 if (error)
1047 WLog_ERR(TAG, "failed to serialize CLIPRDR_FILELIST: 0x%08X", error);
1048 else
1049 {
1050 UINT32 formatId = ClipboardGetFormatId(clipboard->system, mime_uri_list);
1051 UINT32 url_size = 0;
1052
1053 char* url = ClipboardGetData(clipboard->system, formatId, &url_size);
1054 cliprdr_file_context_update_client_data(clipboard->file, url, url_size);
1055 free(url);
1056 }
1057
1058 free(file_array);
1059 }
1060 ClipboardUnlock(clipboard->system);
1061
1062 xf_cliprdr_send_data_response(clipboard, format, pDstData, DstSize);
1063 free(pDstData);
1064}
1065
1066static BOOL xf_restore_input_flags(xfClipboard* clipboard)
1067{
1068 WINPR_ASSERT(clipboard);
1069
1070 xfContext* xfc = clipboard->xfc;
1071 WINPR_ASSERT(xfc);
1072
1073 if (clipboard->event_mask != 0)
1074 {
1075 XSelectInput(xfc->display, xfc->drawable, clipboard->event_mask);
1076 clipboard->event_mask = 0;
1077 }
1078 return TRUE;
1079}
1080
1081static BOOL append(xfClipboard* clipboard, const void* sdata, size_t length)
1082{
1083 WINPR_ASSERT(clipboard);
1084
1085 const size_t size = length + clipboard->incr_data_length + 2;
1086 BYTE* data = realloc(clipboard->incr_data, size);
1087 if (!data)
1088 return FALSE;
1089 clipboard->incr_data = data;
1090 memcpy(&data[clipboard->incr_data_length], sdata, length);
1091 clipboard->incr_data_length += length;
1092 clipboard->incr_data[clipboard->incr_data_length + 0] = '\0';
1093 clipboard->incr_data[clipboard->incr_data_length + 1] = '\0';
1094 return TRUE;
1095}
1096
1097static BOOL xf_cliprdr_stop_incr(xfClipboard* clipboard)
1098{
1099 clipboard->incr_starts = FALSE;
1100 clipboard->incr_data_length = 0;
1101 return xf_restore_input_flags(clipboard);
1102}
1103
1104static BOOL xf_cliprdr_get_requested_data(xfClipboard* clipboard, Atom target)
1105{
1106 WINPR_ASSERT(clipboard);
1107
1108 xfContext* xfc = clipboard->xfc;
1109 WINPR_ASSERT(xfc);
1110
1111 const xfCliprdrFormat* format =
1112 xf_cliprdr_get_client_format_by_id(clipboard, clipboard->requestedFormatId);
1113
1114 if (!format || (format->atom != target))
1115 {
1116 xf_cliprdr_send_data_response(clipboard, format, nullptr, 0);
1117 return FALSE;
1118 }
1119
1120 Atom type = 0;
1121 BOOL has_data = FALSE;
1122 int format_property = 0;
1123 unsigned long length = 0;
1124 unsigned long total_bytes = 0;
1125 BYTE* property_data = nullptr;
1126 const int rc = LogDynAndXGetWindowProperty(
1127 xfc->log, xfc->display, xfc->drawable, clipboard->property_atom, 0, 0, False, target, &type,
1128 &format_property, &length, &total_bytes, &property_data);
1129 if (property_data)
1130 XFree(property_data);
1131 if (rc != Success)
1132 {
1133 xf_cliprdr_send_data_response(clipboard, format, nullptr, 0);
1134 return FALSE;
1135 }
1136
1137 size_t len = 0;
1138
1139 /* No data, empty return */
1140 if ((total_bytes <= 0) && !clipboard->incr_starts)
1141 {
1142 xf_cliprdr_stop_incr(clipboard);
1143 }
1144 /* We have to read incremental updates */
1145 else if (type == clipboard->incr_atom)
1146 {
1147 xf_cliprdr_stop_incr(clipboard);
1148 clipboard->incr_starts = TRUE;
1149 has_data = TRUE; /* data will follow in PropertyNotify event */
1150 }
1151 else
1152 {
1153 BYTE* incremental_data = nullptr;
1154 unsigned long incremental_len = 0;
1155
1156 /* Incremental updates completed, pass data */
1157 len = clipboard->incr_data_length;
1158 if (total_bytes <= 0)
1159 {
1160 xf_cliprdr_stop_incr(clipboard);
1161 has_data = TRUE;
1162 }
1163 /* Read incremental data batch */
1164 else if (LogDynAndXGetWindowProperty(
1165 xfc->log, xfc->display, xfc->drawable, clipboard->property_atom, 0,
1166 WINPR_ASSERTING_INT_CAST(int32_t, total_bytes), False, target, &type,
1167 &format_property, &incremental_len, &length, &incremental_data) == Success)
1168 {
1169 has_data = append(clipboard, incremental_data, incremental_len);
1170 len = clipboard->incr_data_length;
1171 }
1172
1173 if (incremental_data)
1174 XFree(incremental_data);
1175 }
1176
1177 LogDynAndXDeleteProperty(xfc->log, xfc->display, xfc->drawable, clipboard->property_atom);
1178 xf_cliprdr_process_requested_data(clipboard, has_data, clipboard->incr_data, len);
1179
1180 return TRUE;
1181}
1182
1183static void xf_cliprdr_append_target(xfClipboard* clipboard, Atom target)
1184{
1185 WINPR_ASSERT(clipboard);
1186
1187 if (clipboard->numTargets >= ARRAYSIZE(clipboard->targets))
1188 return;
1189
1190 for (size_t i = 0; i < clipboard->numTargets; i++)
1191 {
1192 if (clipboard->targets[i] == target)
1193 return;
1194 }
1195
1196 clipboard->targets[clipboard->numTargets++] = target;
1197}
1198
1199static void xf_cliprdr_provide_targets(xfClipboard* clipboard, const XSelectionEvent* respond)
1200{
1201 xfContext* xfc = nullptr;
1202
1203 WINPR_ASSERT(clipboard);
1204
1205 xfc = clipboard->xfc;
1206 WINPR_ASSERT(xfc);
1207
1208 if (respond->property != None)
1209 {
1210 WINPR_ASSERT(clipboard->numTargets <= INT32_MAX);
1211 LogDynAndXChangeProperty(xfc->log, xfc->display, respond->requestor, respond->property,
1212 XA_ATOM, 32, PropModeReplace, (const BYTE*)clipboard->targets,
1213 (int)clipboard->numTargets);
1214 }
1215}
1216
1217static void xf_cliprdr_provide_timestamp(xfClipboard* clipboard, const XSelectionEvent* respond)
1218{
1219 xfContext* xfc = nullptr;
1220
1221 WINPR_ASSERT(clipboard);
1222
1223 xfc = clipboard->xfc;
1224 WINPR_ASSERT(xfc);
1225
1226 if (respond->property != None)
1227 {
1228 LogDynAndXChangeProperty(xfc->log, xfc->display, respond->requestor, respond->property,
1229 XA_INTEGER, 32, PropModeReplace,
1230 (const BYTE*)&clipboard->selection_ownership_timestamp, 1);
1231 }
1232}
1233
1234#define xf_cliprdr_provide_data(clipboard, respond, data, size) \
1235 xf_cliprdr_provide_data_((clipboard), (respond), (data), (size), __FILE__, __func__, __LINE__)
1236static void xf_cliprdr_provide_data_(xfClipboard* clipboard, const XSelectionEvent* respond,
1237 const BYTE* data, UINT32 size, const char* file,
1238 const char* fkt, size_t line)
1239{
1240 WINPR_ASSERT(clipboard);
1241
1242 xfContext* xfc = clipboard->xfc;
1243 WINPR_ASSERT(xfc);
1244
1245 if (respond->property != None)
1246 {
1247 LogDynAndXChangeProperty_ex(xfc->log, file, fkt, line, xfc->display, respond->requestor,
1248 respond->property, respond->target, 8, PropModeReplace, data,
1249 WINPR_ASSERTING_INT_CAST(int32_t, size));
1250 }
1251}
1252
1253static void log_selection_event(xfContext* xfc, const XEvent* event)
1254{
1255 const DWORD level = WLOG_TRACE;
1256 static wLog* _log_cached_ptr = nullptr;
1257 if (!_log_cached_ptr)
1258 _log_cached_ptr = WLog_Get(TAG);
1259 if (WLog_IsLevelActive(_log_cached_ptr, level))
1260 {
1261
1262 switch (event->type)
1263 {
1264 case SelectionClear:
1265 {
1266 const XSelectionClearEvent* xevent = &event->xselectionclear;
1267 char* selection =
1268 Safe_XGetAtomName(_log_cached_ptr, xfc->display, xevent->selection);
1269 WLog_Print(_log_cached_ptr, level, "got event %s [selection %s]",
1270 x11_event_string(event->type), selection);
1271 XFree(selection);
1272 }
1273 break;
1274 case SelectionNotify:
1275 {
1276 const XSelectionEvent* xevent = &event->xselection;
1277 char* selection =
1278 Safe_XGetAtomName(_log_cached_ptr, xfc->display, xevent->selection);
1279 char* target = Safe_XGetAtomName(_log_cached_ptr, xfc->display, xevent->target);
1280 char* property = Safe_XGetAtomName(_log_cached_ptr, xfc->display, xevent->property);
1281 WLog_Print(_log_cached_ptr, level,
1282 "got event %s [selection %s, target %s, property %s]",
1283 x11_event_string(event->type), selection, target, property);
1284 XFree(selection);
1285 XFree(target);
1286 XFree(property);
1287 }
1288 break;
1289 case SelectionRequest:
1290 {
1291 const XSelectionRequestEvent* xevent = &event->xselectionrequest;
1292 char* selection =
1293 Safe_XGetAtomName(_log_cached_ptr, xfc->display, xevent->selection);
1294 char* target = Safe_XGetAtomName(_log_cached_ptr, xfc->display, xevent->target);
1295 char* property = Safe_XGetAtomName(_log_cached_ptr, xfc->display, xevent->property);
1296 WLog_Print(_log_cached_ptr, level,
1297 "got event %s [selection %s, target %s, property %s]",
1298 x11_event_string(event->type), selection, target, property);
1299 XFree(selection);
1300 XFree(target);
1301 XFree(property);
1302 }
1303 break;
1304 case PropertyNotify:
1305 {
1306 const XPropertyEvent* xevent = &event->xproperty;
1307 char* atom = Safe_XGetAtomName(_log_cached_ptr, xfc->display, xevent->atom);
1308 WLog_Print(_log_cached_ptr, level, "got event %s [atom %s]",
1309 x11_event_string(event->type), atom);
1310 XFree(atom);
1311 }
1312 break;
1313 default:
1314 break;
1315 }
1316 }
1317}
1318
1319static BOOL xf_cliprdr_process_selection_notify(xfClipboard* clipboard,
1320 const XSelectionEvent* xevent)
1321{
1322 WINPR_ASSERT(clipboard);
1323 WINPR_ASSERT(xevent);
1324
1325 if (xevent->target == clipboard->targets[1])
1326 {
1327 if (xevent->property == None)
1328 {
1329 xf_cliprdr_send_client_format_list(clipboard, FALSE);
1330 }
1331 else
1332 {
1333 xf_cliprdr_get_requested_targets(clipboard);
1334 }
1335
1336 return TRUE;
1337 }
1338 else
1339 {
1340 return xf_cliprdr_get_requested_data(clipboard, xevent->target);
1341 }
1342}
1343
1344void xf_cliprdr_clear_cached_data(xfClipboard* clipboard)
1345{
1346 WINPR_ASSERT(clipboard);
1347
1348 ClipboardLock(clipboard->system);
1349 ClipboardEmpty(clipboard->system);
1350
1351 HashTable_Clear(clipboard->cachedData);
1352 HashTable_Clear(clipboard->cachedRawData);
1353
1354 cliprdr_file_context_clear(clipboard->file);
1355
1356 xf_cliprdr_stop_incr(clipboard);
1357 ClipboardUnlock(clipboard->system);
1358}
1359
1360static void* format_to_cache_slot(UINT32 format)
1361{
1362 union
1363 {
1364 uintptr_t uptr;
1365 void* vptr;
1366 } cnv;
1367 cnv.uptr = 0x100000000ULL + format;
1368 return cnv.vptr;
1369}
1370
1371static UINT32 get_dst_format_id_for_local_request(xfClipboard* clipboard,
1372 const xfCliprdrFormat* format)
1373{
1374 UINT32 dstFormatId = 0;
1375
1376 WINPR_ASSERT(format);
1377
1378 if (!format->formatName)
1379 return format->localFormat;
1380
1381 ClipboardLock(clipboard->system);
1382 if (strcmp(format->formatName, type_HtmlFormat) == 0)
1383 dstFormatId = ClipboardGetFormatId(clipboard->system, mime_html);
1384 ClipboardUnlock(clipboard->system);
1385
1386 if (strcmp(format->formatName, type_FileGroupDescriptorW) == 0)
1387 dstFormatId = format->localFormat;
1388
1389 return dstFormatId;
1390}
1391
1392static void get_src_format_info_for_local_request(xfClipboard* clipboard,
1393 const xfCliprdrFormat* format,
1394 UINT32* srcFormatId, BOOL* nullTerminated)
1395{
1396 *srcFormatId = 0;
1397 *nullTerminated = FALSE;
1398
1399 if (format->formatName)
1400 {
1401 ClipboardLock(clipboard->system);
1402 if (strcmp(format->formatName, type_HtmlFormat) == 0)
1403 {
1404 *srcFormatId = ClipboardGetFormatId(clipboard->system, type_HtmlFormat);
1405 *nullTerminated = TRUE;
1406 }
1407 else if (strcmp(format->formatName, type_FileGroupDescriptorW) == 0)
1408 {
1409 *srcFormatId = ClipboardGetFormatId(clipboard->system, type_FileGroupDescriptorW);
1410 *nullTerminated = TRUE;
1411 }
1412 ClipboardUnlock(clipboard->system);
1413 }
1414 else
1415 {
1416 *srcFormatId = format->formatToRequest;
1417 switch (format->formatToRequest)
1418 {
1419 case CF_TEXT:
1420 case CF_OEMTEXT:
1421 case CF_UNICODETEXT:
1422 *nullTerminated = TRUE;
1423 break;
1424 case CF_DIB:
1425 *srcFormatId = CF_DIB;
1426 break;
1427 case CF_TIFF:
1428 *srcFormatId = CF_TIFF;
1429 break;
1430 default:
1431 break;
1432 }
1433 }
1434}
1435
1436static xfCachedData* convert_data_from_existing_raw_data(xfClipboard* clipboard,
1437 xfCachedData* cached_raw_data,
1438 UINT32 srcFormatId, BOOL nullTerminated,
1439 UINT32 dstFormatId)
1440{
1441 UINT32 dst_size = 0;
1442
1443 WINPR_ASSERT(clipboard);
1444 WINPR_ASSERT(cached_raw_data);
1445 WINPR_ASSERT(cached_raw_data->data);
1446
1447 ClipboardLock(clipboard->system);
1448 BOOL success = ClipboardSetData(clipboard->system, srcFormatId, cached_raw_data->data,
1449 cached_raw_data->data_length);
1450 if (!success)
1451 {
1452 WLog_WARN(TAG, "Failed to set clipboard data (formatId: %u, data: %p, data_length: %u)",
1453 srcFormatId, WINPR_CXX_COMPAT_CAST(const void*, cached_raw_data->data),
1454 cached_raw_data->data_length);
1455 ClipboardUnlock(clipboard->system);
1456 return nullptr;
1457 }
1458
1459 BYTE* dst_data = ClipboardGetData(clipboard->system, dstFormatId, &dst_size);
1460 if (!dst_data)
1461 {
1462 WLog_WARN(TAG, "Failed to get converted clipboard data");
1463 ClipboardUnlock(clipboard->system);
1464 return nullptr;
1465 }
1466 ClipboardUnlock(clipboard->system);
1467
1468 if (nullTerminated)
1469 {
1470 BYTE* nullTerminator = memchr(dst_data, '\0', dst_size);
1471 if (nullTerminator)
1472 {
1473 const intptr_t diff = nullTerminator - dst_data;
1474 WINPR_ASSERT(diff >= 0);
1475 WINPR_ASSERT(diff <= UINT32_MAX);
1476 dst_size = (UINT32)diff;
1477 }
1478 }
1479
1480 xfCachedData* cached_data = xf_cached_data_new(dst_data, dst_size);
1481 if (!cached_data)
1482 {
1483 WLog_WARN(TAG, "Failed to allocate cache entry");
1484 free(dst_data);
1485 return nullptr;
1486 }
1487
1488 if (!HashTable_Insert(clipboard->cachedData, format_to_cache_slot(dstFormatId), cached_data))
1489 {
1490 WLog_WARN(TAG, "Failed to cache clipboard data");
1491 xf_cached_data_free(cached_data);
1492 return nullptr;
1493 }
1494
1495 return cached_data;
1496}
1497
1498static BOOL xf_cliprdr_process_selection_request(xfClipboard* clipboard,
1499 const XSelectionRequestEvent* xevent)
1500{
1501 int fmt = 0;
1502 Atom type = 0;
1503 UINT32 formatId = 0;
1504 XSelectionEvent* respond = nullptr;
1505 BYTE* data = nullptr;
1506 BOOL delayRespond = 0;
1507 BOOL rawTransfer = 0;
1508 unsigned long length = 0;
1509 unsigned long bytes_left = 0;
1510 xfContext* xfc = nullptr;
1511
1512 WINPR_ASSERT(clipboard);
1513 WINPR_ASSERT(xevent);
1514
1515 xfc = clipboard->xfc;
1516 WINPR_ASSERT(xfc);
1517
1518 if (xevent->owner != xfc->drawable)
1519 return FALSE;
1520
1521 delayRespond = FALSE;
1522
1523 if (!(respond = (XSelectionEvent*)calloc(1, sizeof(XSelectionEvent))))
1524 {
1525 WLog_ERR(TAG, "failed to allocate XEvent data");
1526 return FALSE;
1527 }
1528
1529 respond->property = None;
1530 respond->type = SelectionNotify;
1531 respond->display = xevent->display;
1532 respond->requestor = xevent->requestor;
1533 respond->selection = xevent->selection;
1534 respond->target = xevent->target;
1535 respond->time = xevent->time;
1536
1537 if (xevent->target == clipboard->targets[0]) /* TIMESTAMP */
1538 {
1539 /* Someone else requests the selection's timestamp */
1540 respond->property = xevent->property;
1541 xf_cliprdr_provide_timestamp(clipboard, respond);
1542 }
1543 else if (xevent->target == clipboard->targets[1]) /* TARGETS */
1544 {
1545 /* Someone else requests our available formats */
1546 respond->property = xevent->property;
1547 xf_cliprdr_provide_targets(clipboard, respond);
1548 }
1549 else
1550 {
1551 const CLIPRDR_FORMAT* format =
1552 xf_cliprdr_get_server_format_by_atom(clipboard, xevent->target);
1553 const xfCliprdrFormat* cformat =
1554 xf_cliprdr_get_client_format_by_atom(clipboard, xevent->target);
1555
1556 if (format && (xevent->requestor != xfc->drawable))
1557 {
1558 formatId = format->formatId;
1559 rawTransfer = FALSE;
1560 xfCachedData* cached_data = nullptr;
1561
1562 if (formatId == CF_RAW)
1563 {
1564 if (LogDynAndXGetWindowProperty(
1565 xfc->log, xfc->display, xevent->requestor, clipboard->property_atom, 0, 4,
1566 0, XA_INTEGER, &type, &fmt, &length, &bytes_left, &data) != Success)
1567 {
1568 }
1569
1570 if (data)
1571 {
1572 rawTransfer = TRUE;
1573 CopyMemory(&formatId, data, 4);
1574 XFree(data);
1575 }
1576 }
1577
1578 const UINT32 dstFormatId = get_dst_format_id_for_local_request(clipboard, cformat);
1579 DEBUG_CLIPRDR("formatId: 0x%08" PRIx32 ", dstFormatId: 0x%08" PRIx32 "", formatId,
1580 dstFormatId);
1581
1582 wHashTable* table = clipboard->cachedData;
1583 if (rawTransfer)
1584 table = clipboard->cachedRawData;
1585
1586 HashTable_Lock(table);
1587
1588 if (!rawTransfer)
1589 cached_data = HashTable_GetItemValue(table, format_to_cache_slot(dstFormatId));
1590 else
1591 cached_data = HashTable_GetItemValue(table, format_to_cache_slot(formatId));
1592
1593 DEBUG_CLIPRDR("hasCachedData: %u, rawTransfer: %d", cached_data ? 1u : 0u, rawTransfer);
1594
1595 if (!cached_data && !rawTransfer)
1596 {
1597 UINT32 srcFormatId = 0;
1598 BOOL nullTerminated = FALSE;
1599 xfCachedData* cached_raw_data = nullptr;
1600
1601 get_src_format_info_for_local_request(clipboard, cformat, &srcFormatId,
1602 &nullTerminated);
1603 cached_raw_data =
1604 HashTable_GetItemValue(clipboard->cachedRawData, (void*)(UINT_PTR)srcFormatId);
1605
1606 DEBUG_CLIPRDR("hasCachedRawData: %u, rawDataLength: %u", cached_raw_data ? 1u : 0u,
1607 cached_raw_data ? cached_raw_data->data_length : 0);
1608
1609 if (cached_raw_data && cached_raw_data->data_length != 0)
1610 cached_data = convert_data_from_existing_raw_data(
1611 clipboard, cached_raw_data, srcFormatId, nullTerminated, dstFormatId);
1612 }
1613
1614 DEBUG_CLIPRDR("hasCachedData: %u", cached_data ? 1u : 0u);
1615
1616 if (cached_data)
1617 {
1618 /* Cached clipboard data available. Send it now */
1619 respond->property = xevent->property;
1620
1621 // NOLINTNEXTLINE(clang-analyzer-unix.Malloc)
1622 xf_cliprdr_provide_data(clipboard, respond, cached_data->data,
1623 cached_data->data_length);
1624 }
1625 else if (clipboard->respond)
1626 {
1627 /* duplicate request */
1628 }
1629 else
1630 {
1631 WINPR_ASSERT(cformat);
1632
1637 respond->property = xevent->property;
1638 clipboard->respond = respond;
1639 requested_format_replace(&clipboard->requestedFormat, formatId, dstFormatId,
1640 cformat->formatName);
1641 clipboard->data_raw_format = rawTransfer;
1642 delayRespond = TRUE;
1643 xf_cliprdr_send_data_request(clipboard, formatId, cformat);
1644 }
1645 HashTable_Unlock(table);
1646 }
1647 }
1648
1649 if (!delayRespond)
1650 {
1651 union
1652 {
1653 XEvent* ev;
1654 XSelectionEvent* sev;
1655 } conv;
1656
1657 conv.sev = respond;
1658 LogDynAndXSendEvent(xfc->log, xfc->display, xevent->requestor, 0, 0, conv.ev);
1659 LogDynAndXFlush(xfc->log, xfc->display);
1660 free(respond);
1661 }
1662
1663 return TRUE;
1664}
1665
1666static BOOL xf_cliprdr_process_selection_clear(xfClipboard* clipboard,
1667 const XSelectionClearEvent* xevent)
1668{
1669 xfContext* xfc = nullptr;
1670
1671 WINPR_ASSERT(clipboard);
1672 WINPR_ASSERT(xevent);
1673
1674 xfc = clipboard->xfc;
1675 WINPR_ASSERT(xfc);
1676
1677 WINPR_UNUSED(xevent);
1678
1679 if (xf_cliprdr_is_self_owned(clipboard))
1680 return FALSE;
1681
1682 LogDynAndXDeleteProperty(xfc->log, xfc->display, clipboard->root_window,
1683 clipboard->property_atom);
1684 return TRUE;
1685}
1686
1687static BOOL xf_cliprdr_process_property_notify(xfClipboard* clipboard, const XPropertyEvent* xevent)
1688{
1689 const xfCliprdrFormat* format = nullptr;
1690 xfContext* xfc = nullptr;
1691
1692 if (!clipboard)
1693 return TRUE;
1694
1695 xfc = clipboard->xfc;
1696 WINPR_ASSERT(xfc);
1697 WINPR_ASSERT(xevent);
1698
1699 if (xevent->atom == clipboard->timestamp_property_atom)
1700 {
1701 /* This is the response to the property change we did
1702 * in xf_cliprdr_prepare_to_set_selection_owner. Now
1703 * we can set ourselves as the selection owner. (See
1704 * comments in those functions below.) */
1705 xf_cliprdr_set_selection_owner(xfc, clipboard, xevent->time);
1706 return TRUE;
1707 }
1708
1709 if (xevent->atom != clipboard->property_atom)
1710 return FALSE; /* Not cliprdr-related */
1711
1712 if (xevent->window == clipboard->root_window)
1713 {
1714 xf_cliprdr_send_client_format_list(clipboard, FALSE);
1715 }
1716 else if ((xevent->window == xfc->drawable) && (xevent->state == PropertyNewValue) &&
1717 clipboard->incr_starts)
1718 {
1719 format = xf_cliprdr_get_client_format_by_id(clipboard, clipboard->requestedFormatId);
1720
1721 if (format)
1722 xf_cliprdr_get_requested_data(clipboard, format->atom);
1723 }
1724
1725 return TRUE;
1726}
1727
1728void xf_cliprdr_handle_xevent(xfContext* xfc, const XEvent* event)
1729{
1730 xfClipboard* clipboard = nullptr;
1731
1732 if (!xfc || !event)
1733 return;
1734
1735 clipboard = xfc->clipboard;
1736
1737 if (!clipboard)
1738 return;
1739
1740#ifdef WITH_XFIXES
1741
1742 if (clipboard->xfixes_supported &&
1743 event->type == XFixesSelectionNotify + clipboard->xfixes_event_base)
1744 {
1745 const XFixesSelectionNotifyEvent* se = (const XFixesSelectionNotifyEvent*)event;
1746
1747 if (se->subtype == XFixesSetSelectionOwnerNotify)
1748 {
1749 if (se->selection != clipboard->clipboard_atom)
1750 return;
1751
1752 if (LogDynAndXGetSelectionOwner(xfc->log, xfc->display, se->selection) == xfc->drawable)
1753 return;
1754
1755 clipboard->owner = None;
1756 xf_cliprdr_check_owner(clipboard);
1757 }
1758
1759 return;
1760 }
1761
1762#endif
1763
1764 switch (event->type)
1765 {
1766 case SelectionNotify:
1767 log_selection_event(xfc, event);
1768 xf_cliprdr_process_selection_notify(clipboard, &event->xselection);
1769 break;
1770
1771 case SelectionRequest:
1772 log_selection_event(xfc, event);
1773 xf_cliprdr_process_selection_request(clipboard, &event->xselectionrequest);
1774 break;
1775
1776 case SelectionClear:
1777 log_selection_event(xfc, event);
1778 xf_cliprdr_process_selection_clear(clipboard, &event->xselectionclear);
1779 break;
1780
1781 case PropertyNotify:
1782 log_selection_event(xfc, event);
1783 xf_cliprdr_process_property_notify(clipboard, &event->xproperty);
1784 break;
1785
1786 case FocusIn:
1787 if (!clipboard->xfixes_supported)
1788 {
1789 xf_cliprdr_check_owner(clipboard);
1790 }
1791
1792 break;
1793 default:
1794 break;
1795 }
1796}
1797
1803static UINT xf_cliprdr_send_client_capabilities(xfClipboard* clipboard)
1804{
1805 CLIPRDR_CAPABILITIES capabilities = WINPR_C_ARRAY_INIT;
1806 CLIPRDR_GENERAL_CAPABILITY_SET generalCapabilitySet = WINPR_C_ARRAY_INIT;
1807
1808 WINPR_ASSERT(clipboard);
1809
1810 capabilities.cCapabilitiesSets = 1;
1811 capabilities.capabilitySets = (CLIPRDR_CAPABILITY_SET*)&(generalCapabilitySet);
1812 generalCapabilitySet.capabilitySetType = CB_CAPSTYPE_GENERAL;
1813 generalCapabilitySet.capabilitySetLength = 12;
1814 generalCapabilitySet.version = CB_CAPS_VERSION_2;
1815 generalCapabilitySet.generalFlags = CB_USE_LONG_FORMAT_NAMES;
1816
1817 WINPR_ASSERT(clipboard);
1818 generalCapabilitySet.generalFlags |= cliprdr_file_context_current_flags(clipboard->file);
1819
1820 WINPR_ASSERT(clipboard->context);
1821 WINPR_ASSERT(clipboard->context->ClientCapabilities);
1822 return clipboard->context->ClientCapabilities(clipboard->context, &capabilities);
1823}
1824
1830static UINT xf_cliprdr_send_client_format_list(xfClipboard* clipboard, BOOL force)
1831{
1832 WINPR_ASSERT(clipboard);
1833
1834 xfContext* xfc = clipboard->xfc;
1835 WINPR_ASSERT(xfc);
1836
1837 UINT32 numFormats = 0;
1838 CLIPRDR_FORMAT* formats = xf_cliprdr_get_client_formats(clipboard, &numFormats);
1839
1840 const UINT ret = xf_cliprdr_send_format_list(clipboard, formats, numFormats, force);
1841
1842 if (clipboard->owner && clipboard->owner != xfc->drawable)
1843 {
1844 /* Request the owner for TARGETS, and wait for SelectionNotify event */
1845 LogDynAndXConvertSelection(xfc->log, xfc->display, clipboard->clipboard_atom,
1846 clipboard->targets[1], clipboard->property_atom, xfc->drawable,
1847 CurrentTime);
1848 }
1849
1850 xf_cliprdr_free_formats(formats, numFormats);
1851
1852 return ret;
1853}
1854
1860static UINT xf_cliprdr_send_client_format_list_response(xfClipboard* clipboard, BOOL status)
1861{
1862 CLIPRDR_FORMAT_LIST_RESPONSE formatListResponse = WINPR_C_ARRAY_INIT;
1863
1864 formatListResponse.common.msgType = CB_FORMAT_LIST_RESPONSE;
1865 formatListResponse.common.msgFlags = status ? CB_RESPONSE_OK : CB_RESPONSE_FAIL;
1866 formatListResponse.common.dataLen = 0;
1867
1868 WINPR_ASSERT(clipboard);
1869 WINPR_ASSERT(clipboard->context);
1870 WINPR_ASSERT(clipboard->context->ClientFormatListResponse);
1871 return clipboard->context->ClientFormatListResponse(clipboard->context, &formatListResponse);
1872}
1873
1879static UINT xf_cliprdr_monitor_ready(CliprdrClientContext* context,
1880 const CLIPRDR_MONITOR_READY* monitorReady)
1881{
1882 WINPR_ASSERT(context);
1883 WINPR_ASSERT(monitorReady);
1884
1885 xfClipboard* clipboard = cliprdr_file_context_get_context(context->custom);
1886 WINPR_ASSERT(clipboard);
1887
1888 WINPR_UNUSED(monitorReady);
1889
1890 const UINT ret = xf_cliprdr_send_client_capabilities(clipboard);
1891 if (ret != CHANNEL_RC_OK)
1892 return ret;
1893
1894 xf_clipboard_formats_free(clipboard);
1895
1896 const UINT ret2 = xf_cliprdr_send_client_format_list(clipboard, TRUE);
1897 if (ret2 != CHANNEL_RC_OK)
1898 return ret2;
1899
1900 clipboard->sync = TRUE;
1901 return CHANNEL_RC_OK;
1902}
1903
1909static UINT xf_cliprdr_server_capabilities(CliprdrClientContext* context,
1910 const CLIPRDR_CAPABILITIES* capabilities)
1911{
1912 WINPR_ASSERT(context);
1913 WINPR_ASSERT(capabilities);
1914
1915 xfClipboard* clipboard = cliprdr_file_context_get_context(context->custom);
1916 WINPR_ASSERT(clipboard);
1917
1918 const BYTE* capsPtr = (const BYTE*)capabilities->capabilitySets;
1919 WINPR_ASSERT(capsPtr);
1920
1921 if (!cliprdr_file_context_remote_set_flags(clipboard->file, 0))
1922 return ERROR_INTERNAL_ERROR;
1923
1924 for (UINT32 i = 0; i < capabilities->cCapabilitiesSets; i++)
1925 {
1926 const CLIPRDR_CAPABILITY_SET* caps = (const CLIPRDR_CAPABILITY_SET*)capsPtr;
1927
1928 if (caps->capabilitySetType == CB_CAPSTYPE_GENERAL)
1929 {
1930 const CLIPRDR_GENERAL_CAPABILITY_SET* generalCaps =
1931 (const CLIPRDR_GENERAL_CAPABILITY_SET*)caps;
1932
1933 if (!cliprdr_file_context_remote_set_flags(clipboard->file, generalCaps->generalFlags))
1934 return ERROR_INTERNAL_ERROR;
1935 }
1936
1937 capsPtr += caps->capabilitySetLength;
1938 }
1939
1940 return CHANNEL_RC_OK;
1941}
1942
1943static void xf_cliprdr_prepare_to_set_selection_owner(xfContext* xfc, xfClipboard* clipboard)
1944{
1945 WINPR_ASSERT(xfc);
1946 WINPR_ASSERT(clipboard);
1947 /*
1948 * When you're writing to the selection in response to a
1949 * normal X event like a mouse click or keyboard action, you
1950 * get the selection timestamp by copying the time field out
1951 * of that X event. Here, we're doing it on our own
1952 * initiative, so we have to _request_ the X server time.
1953 *
1954 * There isn't a GetServerTime request in the X protocol, so I
1955 * work around it by setting a property on our own window, and
1956 * waiting for a PropertyNotify event to come back telling me
1957 * it's been done - which will have a timestamp we can use.
1958 */
1959
1960 /* We have to set the property to some value, but it doesn't
1961 * matter what. Set it to its own name, which we have here
1962 * anyway! */
1963 Atom value = clipboard->timestamp_property_atom;
1964
1965 LogDynAndXChangeProperty(xfc->log, xfc->display, xfc->drawable,
1966 clipboard->timestamp_property_atom, XA_ATOM, 32, PropModeReplace,
1967 (const BYTE*)&value, 1);
1968 LogDynAndXFlush(xfc->log, xfc->display);
1969}
1970
1971static void xf_cliprdr_set_selection_owner(xfContext* xfc, xfClipboard* clipboard, Time timestamp)
1972{
1973 WINPR_ASSERT(xfc);
1974 WINPR_ASSERT(clipboard);
1975 /*
1976 * Actually set ourselves up as the selection owner, now that
1977 * we have a timestamp to use.
1978 */
1979
1980 clipboard->selection_ownership_timestamp = timestamp;
1981 LogDynAndXSetSelectionOwner(xfc->log, xfc->display, clipboard->clipboard_atom, xfc->drawable,
1982 timestamp);
1983 LogDynAndXFlush(xfc->log, xfc->display);
1984}
1985
1991static UINT xf_cliprdr_server_format_list(CliprdrClientContext* context,
1992 const CLIPRDR_FORMAT_LIST* formatList)
1993{
1994 xfContext* xfc = nullptr;
1995 UINT ret = 0;
1996 xfClipboard* clipboard = nullptr;
1997
1998 WINPR_ASSERT(context);
1999 WINPR_ASSERT(formatList);
2000
2001 clipboard = cliprdr_file_context_get_context(context->custom);
2002 WINPR_ASSERT(clipboard);
2003
2004 xfc = clipboard->xfc;
2005 WINPR_ASSERT(xfc);
2006
2007 xf_lock_x11(xfc);
2008
2009 /* Clear the active SelectionRequest, as it is now invalid */
2010 free(clipboard->respond);
2011 clipboard->respond = nullptr;
2012
2013 xf_clipboard_formats_free(clipboard);
2014 xf_cliprdr_clear_cached_data(clipboard);
2015 requested_format_free(&clipboard->requestedFormat);
2016
2017 xf_clipboard_free_server_formats(clipboard);
2018
2019 clipboard->numServerFormats = formatList->numFormats + 1; /* +1 for CF_RAW */
2020
2021 if (!(clipboard->serverFormats =
2022 (CLIPRDR_FORMAT*)calloc(clipboard->numServerFormats, sizeof(CLIPRDR_FORMAT))))
2023 {
2024 WLog_ERR(TAG, "failed to allocate %" PRIu32 " CLIPRDR_FORMAT structs",
2025 clipboard->numServerFormats);
2026 ret = CHANNEL_RC_NO_MEMORY;
2027 goto out;
2028 }
2029
2030 for (size_t i = 0; i < formatList->numFormats; i++)
2031 {
2032 const CLIPRDR_FORMAT* format = &formatList->formats[i];
2033 CLIPRDR_FORMAT* srvFormat = &clipboard->serverFormats[i];
2034
2035 srvFormat->formatId = format->formatId;
2036
2037 if (format->formatName)
2038 {
2039 srvFormat->formatName = _strdup(format->formatName);
2040
2041 if (!srvFormat->formatName)
2042 {
2043 for (UINT32 k = 0; k < i; k++)
2044 free(clipboard->serverFormats[k].formatName);
2045
2046 clipboard->numServerFormats = 0;
2047 free(clipboard->serverFormats);
2048 clipboard->serverFormats = nullptr;
2049 ret = CHANNEL_RC_NO_MEMORY;
2050 goto out;
2051 }
2052 }
2053 }
2054
2055 ClipboardLock(clipboard->system);
2056 ret = cliprdr_file_context_notify_new_server_format_list(clipboard->file);
2057 ClipboardUnlock(clipboard->system);
2058 if (ret)
2059 goto out;
2060
2061 /* CF_RAW is always implicitly supported by the server */
2062 {
2063 CLIPRDR_FORMAT* format = &clipboard->serverFormats[formatList->numFormats];
2064 format->formatId = CF_RAW;
2065 format->formatName = nullptr;
2066 }
2067 xf_cliprdr_provide_server_format_list(clipboard);
2068 clipboard->numTargets = 2;
2069
2070 for (size_t i = 0; i < formatList->numFormats; i++)
2071 {
2072 const CLIPRDR_FORMAT* format = &formatList->formats[i];
2073
2074 for (size_t j = 0; j < clipboard->numClientFormats; j++)
2075 {
2076 const xfCliprdrFormat* clientFormat = &clipboard->clientFormats[j];
2077 if (xf_cliprdr_formats_equal(format, clientFormat))
2078 {
2079 if ((clientFormat->formatName != nullptr) &&
2080 (strcmp(type_FileGroupDescriptorW, clientFormat->formatName) == 0))
2081 {
2082 if (!cliprdr_file_context_has_local_support(clipboard->file))
2083 continue;
2084 }
2085 xf_cliprdr_append_target(clipboard, clientFormat->atom);
2086 }
2087 }
2088 }
2089
2090 ret = xf_cliprdr_send_client_format_list_response(clipboard, TRUE);
2091 if (xfc->remote_app)
2092 xf_cliprdr_set_selection_owner(xfc, clipboard, CurrentTime);
2093 else
2094 xf_cliprdr_prepare_to_set_selection_owner(xfc, clipboard);
2095
2096out:
2097 xf_unlock_x11(xfc);
2098
2099 return ret;
2100}
2101
2107static UINT xf_cliprdr_server_format_list_response(
2108 WINPR_ATTR_UNUSED CliprdrClientContext* context,
2109 WINPR_ATTR_UNUSED const CLIPRDR_FORMAT_LIST_RESPONSE* formatListResponse)
2110{
2111 WINPR_ASSERT(context);
2112 WINPR_ASSERT(formatListResponse);
2113 // xfClipboard* clipboard = (xfClipboard*) context->custom;
2114 return CHANNEL_RC_OK;
2115}
2116
2122static UINT
2123xf_cliprdr_server_format_data_request(CliprdrClientContext* context,
2124 const CLIPRDR_FORMAT_DATA_REQUEST* formatDataRequest)
2125{
2126 const xfCliprdrFormat* format = nullptr;
2127
2128 WINPR_ASSERT(context);
2129 WINPR_ASSERT(formatDataRequest);
2130
2131 xfClipboard* clipboard = cliprdr_file_context_get_context(context->custom);
2132 WINPR_ASSERT(clipboard);
2133
2134 xfContext* xfc = clipboard->xfc;
2135 WINPR_ASSERT(xfc);
2136
2137 const uint32_t formatId = formatDataRequest->requestedFormatId;
2138
2139 const BOOL rawTransfer = xf_cliprdr_is_raw_transfer_available(clipboard);
2140
2141 if (rawTransfer)
2142 {
2143 format = xf_cliprdr_get_client_format_by_id(clipboard, CF_RAW);
2144 LogDynAndXChangeProperty(xfc->log, xfc->display, xfc->drawable, clipboard->property_atom,
2145 XA_INTEGER, 32, PropModeReplace, (const BYTE*)&formatId, 1);
2146 }
2147 else
2148 format = xf_cliprdr_get_client_format_by_id(clipboard, formatId);
2149
2150 clipboard->requestedFormatId = rawTransfer ? CF_RAW : formatId;
2151 if (!format)
2152 return xf_cliprdr_send_data_response(clipboard, format, nullptr, 0);
2153
2154 DEBUG_CLIPRDR("requested format 0x%08" PRIx32 " [%s] {local 0x%08" PRIx32 " [%s]} [%s]",
2155 format->formatToRequest, ClipboardGetFormatIdString(format->formatToRequest),
2156 format->localFormat,
2157 ClipboardGetFormatName(clipboard->system, format->localFormat),
2158 format->formatName);
2159 LogDynAndXConvertSelection(xfc->log, xfc->display, clipboard->clipboard_atom, format->atom,
2160 clipboard->property_atom, xfc->drawable, CurrentTime);
2161 LogDynAndXFlush(xfc->log, xfc->display);
2162 /* After this point, we expect a SelectionNotify event from the clipboard owner. */
2163 return CHANNEL_RC_OK;
2164}
2165
2171static UINT
2172xf_cliprdr_server_format_data_response(CliprdrClientContext* context,
2173 const CLIPRDR_FORMAT_DATA_RESPONSE* formatDataResponse)
2174{
2175 BOOL bSuccess = 0;
2176 BYTE* pDstData = nullptr;
2177 UINT32 DstSize = 0;
2178 UINT32 SrcSize = 0;
2179 UINT32 srcFormatId = 0;
2180 UINT32 dstFormatId = 0;
2181 BOOL nullTerminated = FALSE;
2182 xfCachedData* cached_data = nullptr;
2183
2184 WINPR_ASSERT(context);
2185 WINPR_ASSERT(formatDataResponse);
2186
2187 xfClipboard* clipboard = cliprdr_file_context_get_context(context->custom);
2188 WINPR_ASSERT(clipboard);
2189
2190 xfContext* xfc = clipboard->xfc;
2191 WINPR_ASSERT(xfc);
2192
2193 const UINT32 size = formatDataResponse->common.dataLen;
2194 const BYTE* data = formatDataResponse->requestedFormatData;
2195
2196 if (formatDataResponse->common.msgFlags == CB_RESPONSE_FAIL)
2197 {
2198 WLog_WARN(TAG, "Format Data Response PDU msgFlags is CB_RESPONSE_FAIL");
2199 free(clipboard->respond);
2200 clipboard->respond = nullptr;
2201 return CHANNEL_RC_OK;
2202 }
2203
2204 if (!clipboard->respond)
2205 return CHANNEL_RC_OK;
2206
2207 const RequestedFormat* format = clipboard->requestedFormat;
2208 if (clipboard->data_raw_format)
2209 {
2210 srcFormatId = CF_RAW;
2211 dstFormatId = CF_RAW;
2212 }
2213 else if (!format)
2214 return ERROR_INTERNAL_ERROR;
2215 else if (format->formatName)
2216 {
2217 dstFormatId = format->localFormat;
2218
2219 ClipboardLock(clipboard->system);
2220 if (strcmp(format->formatName, type_HtmlFormat) == 0)
2221 {
2222 srcFormatId = ClipboardGetFormatId(clipboard->system, type_HtmlFormat);
2223 dstFormatId = ClipboardGetFormatId(clipboard->system, mime_html);
2224 nullTerminated = TRUE;
2225 }
2226
2227 if (strcmp(format->formatName, type_FileGroupDescriptorW) == 0)
2228 {
2229 if (!cliprdr_file_context_update_server_data(clipboard->file, clipboard->system, data,
2230 size))
2231 WLog_WARN(TAG, "failed to update file descriptors");
2232
2233 srcFormatId = ClipboardGetFormatId(clipboard->system, type_FileGroupDescriptorW);
2234 const xfCliprdrFormat* dstTargetFormat =
2235 xf_cliprdr_get_client_format_by_atom(clipboard, clipboard->respond->target);
2236 if (!dstTargetFormat)
2237 {
2238 dstFormatId = ClipboardGetFormatId(clipboard->system, mime_uri_list);
2239 }
2240 else
2241 {
2242 dstFormatId = dstTargetFormat->localFormat;
2243 }
2244
2245 nullTerminated = TRUE;
2246 }
2247 ClipboardUnlock(clipboard->system);
2248 }
2249 else
2250 {
2251 srcFormatId = format->formatToRequest;
2252 dstFormatId = format->localFormat;
2253 switch (format->formatToRequest)
2254 {
2255 case CF_TEXT:
2256 nullTerminated = TRUE;
2257 break;
2258
2259 case CF_OEMTEXT:
2260 nullTerminated = TRUE;
2261 break;
2262
2263 case CF_UNICODETEXT:
2264 nullTerminated = TRUE;
2265 break;
2266
2267 case CF_DIB:
2268 srcFormatId = CF_DIB;
2269 break;
2270
2271 case CF_TIFF:
2272 srcFormatId = CF_TIFF;
2273 break;
2274
2275 default:
2276 break;
2277 }
2278 }
2279
2280 DEBUG_CLIPRDR("requested format 0x%08" PRIx32 " [%s] {local 0x%08" PRIx32 " [%s]} [%s]",
2281 format->formatToRequest, ClipboardGetFormatIdString(format->formatToRequest),
2282 format->localFormat,
2283 ClipboardGetFormatName(clipboard->system, format->localFormat),
2284 format->formatName);
2285 SrcSize = size;
2286
2287 DEBUG_CLIPRDR("srcFormatId: 0x%08" PRIx32 ", dstFormatId: 0x%08" PRIx32 "", srcFormatId,
2288 dstFormatId);
2289
2290 ClipboardLock(clipboard->system);
2291 bSuccess = ClipboardSetData(clipboard->system, srcFormatId, data, SrcSize);
2292
2293 BOOL willQuit = FALSE;
2294 if (bSuccess)
2295 {
2296 if (SrcSize == 0)
2297 {
2298 WLog_DBG(TAG, "skipping, empty data detected!");
2299 free(clipboard->respond);
2300 clipboard->respond = nullptr;
2301 willQuit = TRUE;
2302 }
2303 else
2304 {
2305 pDstData = (BYTE*)ClipboardGetData(clipboard->system, dstFormatId, &DstSize);
2306
2307 if (!pDstData)
2308 {
2309 WLog_WARN(TAG, "failed to get clipboard data in format %s [source format %s]",
2310 ClipboardGetFormatName(clipboard->system, dstFormatId),
2311 ClipboardGetFormatName(clipboard->system, srcFormatId));
2312 }
2313
2314 if (nullTerminated && pDstData)
2315 {
2316 BYTE* nullTerminator = memchr(pDstData, '\0', DstSize);
2317 if (nullTerminator)
2318 {
2319 const intptr_t diff = nullTerminator - pDstData;
2320 WINPR_ASSERT(diff >= 0);
2321 WINPR_ASSERT(diff <= UINT32_MAX);
2322 DstSize = (UINT32)diff;
2323 }
2324 }
2325 }
2326 }
2327 ClipboardUnlock(clipboard->system);
2328 if (willQuit)
2329 return CHANNEL_RC_OK;
2330
2331 /* Cache converted and original data to avoid doing a possibly costly
2332 * conversion again on subsequent requests */
2333 if (pDstData)
2334 {
2335 cached_data = xf_cached_data_new(pDstData, DstSize);
2336 if (!cached_data)
2337 {
2338 WLog_WARN(TAG, "Failed to allocate cache entry");
2339 free(pDstData);
2340 return CHANNEL_RC_OK;
2341 }
2342 if (!HashTable_Insert(clipboard->cachedData, format_to_cache_slot(dstFormatId),
2343 cached_data))
2344 {
2345 WLog_WARN(TAG, "Failed to cache clipboard data");
2346 xf_cached_data_free(cached_data);
2347 return CHANNEL_RC_OK;
2348 }
2349 }
2350
2351 /* We have to copy the original data again, as pSrcData is now owned
2352 * by clipboard->system. Memory allocation failure is not fatal here
2353 * as this is only a cached value. */
2354 {
2355 // clipboard->cachedData owns cached_data
2356 // NOLINTNEXTLINE(clang-analyzer-unix.Malloc
2357 xfCachedData* cached_raw_data = xf_cached_data_new_copy(data, size);
2358 if (!cached_raw_data)
2359 WLog_WARN(TAG, "Failed to allocate cache entry");
2360 else
2361 {
2362 if (!HashTable_Insert(clipboard->cachedRawData, (void*)(UINT_PTR)srcFormatId,
2363 cached_raw_data))
2364 {
2365 WLog_WARN(TAG, "Failed to cache clipboard data");
2366 xf_cached_data_free(cached_raw_data);
2367 }
2368 }
2369 }
2370
2371 // clipboard->cachedRawData owns cached_raw_data
2372 // NOLINTNEXTLINE(clang-analyzer-unix.Malloc)
2373 xf_cliprdr_provide_data(clipboard, clipboard->respond, pDstData, DstSize);
2374 {
2375 union
2376 {
2377 XEvent* ev;
2378 XSelectionEvent* sev;
2379 } conv;
2380
2381 conv.sev = clipboard->respond;
2382
2383 LogDynAndXSendEvent(xfc->log, xfc->display, clipboard->respond->requestor, 0, 0, conv.ev);
2384 LogDynAndXFlush(xfc->log, xfc->display);
2385 }
2386 free(clipboard->respond);
2387 clipboard->respond = nullptr;
2388 return CHANNEL_RC_OK;
2389}
2390
2391static BOOL xf_cliprdr_is_valid_unix_filename(LPCWSTR filename)
2392{
2393 if (!filename)
2394 return FALSE;
2395
2396 if (filename[0] == L'\0')
2397 return FALSE;
2398
2399 /* Reserved characters */
2400 for (const WCHAR* c = filename; *c; ++c)
2401 {
2402 if (*c == L'/')
2403 return FALSE;
2404 }
2405
2406 return TRUE;
2407}
2408
2409xfClipboard* xf_clipboard_new(xfContext* xfc, BOOL relieveFilenameRestriction)
2410{
2411 int n = 0;
2412 rdpChannels* channels = nullptr;
2413 xfClipboard* clipboard = nullptr;
2414 const char* selectionAtom = nullptr;
2415 xfCliprdrFormat* clientFormat = nullptr;
2416 wObject* obj = nullptr;
2417
2418 WINPR_ASSERT(xfc);
2419 WINPR_ASSERT(xfc->common.context.settings);
2420
2421 if (!(clipboard = (xfClipboard*)calloc(1, sizeof(xfClipboard))))
2422 {
2423 WLog_ERR(TAG, "failed to allocate xfClipboard data");
2424 return nullptr;
2425 }
2426
2427 clipboard->file = cliprdr_file_context_new(clipboard);
2428 if (!clipboard->file)
2429 goto fail;
2430
2431 xfc->clipboard = clipboard;
2432 clipboard->xfc = xfc;
2433 channels = xfc->common.context.channels;
2434 clipboard->channels = channels;
2435 clipboard->system = ClipboardCreate();
2436 clipboard->requestedFormatId = UINT32_MAX;
2437 clipboard->root_window = DefaultRootWindow(xfc->display);
2438
2439 selectionAtom =
2440 freerdp_settings_get_string(xfc->common.context.settings, FreeRDP_ClipboardUseSelection);
2441 if (!selectionAtom)
2442 selectionAtom = "CLIPBOARD";
2443
2444 clipboard->clipboard_atom = Logging_XInternAtom(xfc->log, xfc->display, selectionAtom, FALSE);
2445
2446 if (clipboard->clipboard_atom == None)
2447 {
2448 WLog_ERR(TAG, "unable to get %s atom", selectionAtom);
2449 goto fail;
2450 }
2451
2452 clipboard->timestamp_property_atom =
2453 Logging_XInternAtom(xfc->log, xfc->display, "_FREERDP_TIMESTAMP_PROPERTY", FALSE);
2454 clipboard->property_atom =
2455 Logging_XInternAtom(xfc->log, xfc->display, "_FREERDP_CLIPRDR", FALSE);
2456 clipboard->raw_transfer_atom =
2457 Logging_XInternAtom(xfc->log, xfc->display, "_FREERDP_CLIPRDR_RAW", FALSE);
2458 clipboard->raw_format_list_atom =
2459 Logging_XInternAtom(xfc->log, xfc->display, "_FREERDP_CLIPRDR_FORMATS", FALSE);
2460 xf_cliprdr_set_raw_transfer_enabled(clipboard, TRUE);
2461 XSelectInput(xfc->display, clipboard->root_window, PropertyChangeMask);
2462#ifdef WITH_XFIXES
2463
2464 if (XFixesQueryExtension(xfc->display, &clipboard->xfixes_event_base,
2465 &clipboard->xfixes_error_base))
2466 {
2467 int xfmajor = 0;
2468 int xfminor = 0;
2469
2470 if (XFixesQueryVersion(xfc->display, &xfmajor, &xfminor))
2471 {
2472 XFixesSelectSelectionInput(xfc->display, clipboard->root_window,
2473 clipboard->clipboard_atom,
2474 XFixesSetSelectionOwnerNotifyMask);
2475 clipboard->xfixes_supported = TRUE;
2476 }
2477 else
2478 {
2479 WLog_ERR(TAG, "Error querying X Fixes extension version");
2480 }
2481 }
2482 else
2483 {
2484 WLog_ERR(TAG, "Error loading X Fixes extension");
2485 }
2486
2487#else
2488 WLog_ERR(
2489 TAG,
2490 "Warning: Using clipboard redirection without XFIXES extension is strongly discouraged!");
2491#endif
2492 clientFormat = &clipboard->clientFormats[n++];
2493 clientFormat->atom = Logging_XInternAtom(xfc->log, xfc->display, "_FREERDP_RAW", False);
2494 clientFormat->localFormat = clientFormat->formatToRequest = CF_RAW;
2495
2496 clientFormat = &clipboard->clientFormats[n++];
2497 clientFormat->atom = Logging_XInternAtom(xfc->log, xfc->display, "UTF8_STRING", False);
2498 clientFormat->formatToRequest = CF_UNICODETEXT;
2499 clientFormat->localFormat = ClipboardGetFormatId(xfc->clipboard->system, mime_text_plain);
2500
2501 clientFormat = &clipboard->clientFormats[n++];
2502 clientFormat->atom = XA_STRING;
2503 clientFormat->formatToRequest = CF_TEXT;
2504 clientFormat->localFormat = ClipboardGetFormatId(xfc->clipboard->system, mime_text_plain);
2505
2506 clientFormat = &clipboard->clientFormats[n++];
2507 clientFormat->atom = Logging_XInternAtom(xfc->log, xfc->display, mime_tiff, False);
2508 clientFormat->formatToRequest = clientFormat->localFormat = CF_TIFF;
2509
2510 for (size_t x = 0; x < ARRAYSIZE(mime_bitmap); x++)
2511 {
2512 const char* mime_bmp = mime_bitmap[x];
2513 const DWORD format = ClipboardGetFormatId(xfc->clipboard->system, mime_bmp);
2514 if (format == 0)
2515 {
2516 WLog_DBG(TAG, "skipping local bitmap format %s [NOT SUPPORTED]", mime_bmp);
2517 continue;
2518 }
2519
2520 WLog_DBG(TAG, "register local bitmap format %s [0x%08" PRIx32 "]", mime_bmp, format);
2521 clientFormat = &clipboard->clientFormats[n++];
2522 clientFormat->localFormat = format;
2523 clientFormat->atom = Logging_XInternAtom(xfc->log, xfc->display, mime_bmp, False);
2524 clientFormat->formatToRequest = CF_DIB;
2525 clientFormat->isImage = TRUE;
2526 }
2527
2528 for (size_t x = 0; x < ARRAYSIZE(mime_images); x++)
2529 {
2530 const char* mime_bmp = mime_images[x];
2531 const DWORD format = ClipboardGetFormatId(xfc->clipboard->system, mime_bmp);
2532 if (format == 0)
2533 {
2534 WLog_DBG(TAG, "skipping local bitmap format %s [NOT SUPPORTED]", mime_bmp);
2535 continue;
2536 }
2537
2538 WLog_DBG(TAG, "register local bitmap format %s [0x%08" PRIx32 "]", mime_bmp, format);
2539 clientFormat = &clipboard->clientFormats[n++];
2540 clientFormat->localFormat = format;
2541 clientFormat->atom = Logging_XInternAtom(xfc->log, xfc->display, mime_bmp, False);
2542 clientFormat->formatToRequest = CF_DIB;
2543 clientFormat->isImage = TRUE;
2544 }
2545
2546 clientFormat = &clipboard->clientFormats[n++];
2547 clientFormat->atom = Logging_XInternAtom(xfc->log, xfc->display, mime_html, False);
2548 clientFormat->formatToRequest = ClipboardGetFormatId(xfc->clipboard->system, type_HtmlFormat);
2549 clientFormat->localFormat = ClipboardGetFormatId(xfc->clipboard->system, mime_html);
2550 clientFormat->formatName = _strdup(type_HtmlFormat);
2551
2552 if (!clientFormat->formatName)
2553 goto fail;
2554
2555 clientFormat = &clipboard->clientFormats[n++];
2556
2557 /*
2558 * Existence of registered format IDs for file formats does not guarantee that they are
2559 * in fact supported by wClipboard (as further initialization may have failed after format
2560 * registration). However, they are definitely not supported if there are no registered
2561 * formats. In this case we should not list file formats in TARGETS.
2562 */
2563 {
2564 const UINT32 fgid = ClipboardGetFormatId(clipboard->system, type_FileGroupDescriptorW);
2565 {
2566 const UINT32 uid = ClipboardGetFormatId(clipboard->system, mime_uri_list);
2567 if (uid)
2568 {
2569 if (!cliprdr_file_context_set_locally_available(clipboard->file, TRUE))
2570 goto fail;
2571 clientFormat->atom =
2572 Logging_XInternAtom(xfc->log, xfc->display, mime_uri_list, False);
2573 clientFormat->localFormat = uid;
2574 clientFormat->formatToRequest = fgid;
2575 clientFormat->formatName = _strdup(type_FileGroupDescriptorW);
2576
2577 if (!clientFormat->formatName)
2578 goto fail;
2579
2580 clientFormat = &clipboard->clientFormats[n++];
2581 }
2582 }
2583
2584 {
2585 const UINT32 gid = ClipboardGetFormatId(clipboard->system, mime_gnome_copied_files);
2586 if (gid != 0)
2587 {
2588 if (!cliprdr_file_context_set_locally_available(clipboard->file, TRUE))
2589 goto fail;
2590 clientFormat->atom =
2591 Logging_XInternAtom(xfc->log, xfc->display, mime_gnome_copied_files, False);
2592 clientFormat->localFormat = gid;
2593 clientFormat->formatToRequest = fgid;
2594 clientFormat->formatName = _strdup(type_FileGroupDescriptorW);
2595
2596 if (!clientFormat->formatName)
2597 goto fail;
2598
2599 clientFormat = &clipboard->clientFormats[n++];
2600 }
2601 }
2602
2603 {
2604 const UINT32 mid = ClipboardGetFormatId(clipboard->system, mime_mate_copied_files);
2605 if (mid != 0)
2606 {
2607 if (!cliprdr_file_context_set_locally_available(clipboard->file, TRUE))
2608 goto fail;
2609 clientFormat->atom =
2610 Logging_XInternAtom(xfc->log, xfc->display, mime_mate_copied_files, False);
2611 clientFormat->localFormat = mid;
2612 clientFormat->formatToRequest = fgid;
2613 clientFormat->formatName = _strdup(type_FileGroupDescriptorW);
2614
2615 if (!clientFormat->formatName)
2616 goto fail;
2617 }
2618 }
2619 }
2620
2621 clipboard->numClientFormats = WINPR_ASSERTING_INT_CAST(uint32_t, n);
2622 clipboard->targets[0] = Logging_XInternAtom(xfc->log, xfc->display, "TIMESTAMP", FALSE);
2623 clipboard->targets[1] = Logging_XInternAtom(xfc->log, xfc->display, "TARGETS", FALSE);
2624 clipboard->numTargets = 2;
2625 clipboard->incr_atom = Logging_XInternAtom(xfc->log, xfc->display, "INCR", FALSE);
2626
2627 if (relieveFilenameRestriction)
2628 {
2629 WLog_DBG(TAG, "Relieving CLIPRDR filename restriction");
2630 ClipboardGetDelegate(clipboard->system)->IsFileNameComponentValid =
2631 xf_cliprdr_is_valid_unix_filename;
2632 }
2633
2634 clipboard->cachedData = HashTable_New(TRUE);
2635 if (!clipboard->cachedData)
2636 goto fail;
2637
2638 obj = HashTable_ValueObject(clipboard->cachedData);
2639 obj->fnObjectFree = xf_cached_data_free;
2640
2641 clipboard->cachedRawData = HashTable_New(TRUE);
2642 if (!clipboard->cachedRawData)
2643 goto fail;
2644
2645 obj = HashTable_ValueObject(clipboard->cachedRawData);
2646 obj->fnObjectFree = xf_cached_data_free;
2647
2648 return clipboard;
2649
2650fail:
2651 WINPR_PRAGMA_DIAG_PUSH
2652 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
2653 xf_clipboard_free(clipboard);
2654 WINPR_PRAGMA_DIAG_POP
2655 return nullptr;
2656}
2657
2658void xf_clipboard_free(xfClipboard* clipboard)
2659{
2660 if (!clipboard)
2661 return;
2662
2663 xf_clipboard_free_server_formats(clipboard);
2664
2665 if (clipboard->numClientFormats)
2666 {
2667 for (UINT32 i = 0; i < clipboard->numClientFormats; i++)
2668 {
2669 xfCliprdrFormat* format = &clipboard->clientFormats[i];
2670 free(format->formatName);
2671 }
2672 }
2673
2674 cliprdr_file_context_free(clipboard->file);
2675
2676 ClipboardDestroy(clipboard->system);
2677 xf_clipboard_formats_free(clipboard);
2678 HashTable_Free(clipboard->cachedRawData);
2679 HashTable_Free(clipboard->cachedData);
2680 requested_format_free(&clipboard->requestedFormat);
2681 free(clipboard->respond);
2682 free(clipboard->incr_data);
2683 free(clipboard);
2684}
2685
2686void xf_cliprdr_init(xfContext* xfc, CliprdrClientContext* cliprdr)
2687{
2688 WINPR_ASSERT(xfc);
2689 WINPR_ASSERT(cliprdr);
2690
2691 xfc->cliprdr = cliprdr;
2692 xfc->clipboard->context = cliprdr;
2693
2694 cliprdr->MonitorReady = xf_cliprdr_monitor_ready;
2695 cliprdr->ServerCapabilities = xf_cliprdr_server_capabilities;
2696 cliprdr->ServerFormatList = xf_cliprdr_server_format_list;
2697 cliprdr->ServerFormatListResponse = xf_cliprdr_server_format_list_response;
2698 cliprdr->ServerFormatDataRequest = xf_cliprdr_server_format_data_request;
2699 cliprdr->ServerFormatDataResponse = xf_cliprdr_server_format_data_response;
2700
2701 cliprdr_file_context_init(xfc->clipboard->file, cliprdr);
2702}
2703
2704void xf_cliprdr_uninit(xfContext* xfc, CliprdrClientContext* cliprdr)
2705{
2706 WINPR_ASSERT(xfc);
2707
2708 xfc->cliprdr = nullptr;
2709
2710 if (xfc->clipboard)
2711 {
2712 ClipboardLock(xfc->clipboard->system);
2713 cliprdr_file_context_uninit(xfc->clipboard->file, cliprdr);
2714 ClipboardUnlock(xfc->clipboard->system);
2715 xfc->clipboard->context = nullptr;
2716 }
2717}
WINPR_ATTR_NODISCARD FREERDP_API const char * freerdp_settings_get_string(const rdpSettings *settings, FreeRDP_Settings_Keys_String id)
Returns a immutable string settings value.
This struct contains function pointer to initialize/free objects.
Definition collections.h:52
OBJECT_FREE_FN fnObjectFree
Definition collections.h:59