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 xf_lock_x11(clipboard->xfc);
874 xf_cliprdr_free_formats(clipboard->lastSentFormats, clipboard->lastSentNumFormats);
875 xf_unlock_x11(clipboard->xfc);
876
877 clipboard->lastSentFormats = nullptr;
878 clipboard->lastSentNumFormats = 0;
879}
880
881static BOOL xf_clipboard_copy_formats(xfClipboard* clipboard, const CLIPRDR_FORMAT* formats,
882 UINT32 numFormats)
883{
884 WINPR_ASSERT(clipboard);
885 WINPR_ASSERT(formats || (numFormats == 0));
886
887 xf_clipboard_formats_free(clipboard);
888 if (numFormats > 0)
889 clipboard->lastSentFormats = calloc(numFormats, sizeof(CLIPRDR_FORMAT));
890 if (!clipboard->lastSentFormats)
891 return FALSE;
892 clipboard->lastSentNumFormats = numFormats;
893 for (UINT32 x = 0; x < numFormats; x++)
894 {
895 CLIPRDR_FORMAT* lcur = &clipboard->lastSentFormats[x];
896 const CLIPRDR_FORMAT* cur = &formats[x];
897 *lcur = *cur;
898 if (cur->formatName)
899 lcur->formatName = _strdup(cur->formatName);
900 }
901 return FALSE;
902}
903
904static UINT xf_cliprdr_send_format_list(xfClipboard* clipboard, const CLIPRDR_FORMAT* formats,
905 UINT32 numFormats, BOOL force)
906{
907 union
908 {
909 const CLIPRDR_FORMAT* cpv;
910 CLIPRDR_FORMAT* pv;
911 } cnv = { .cpv = formats };
912 const CLIPRDR_FORMAT_LIST formatList = { .common.msgFlags = 0,
913 .numFormats = numFormats,
914 .formats = cnv.pv,
915 .common.msgType = CB_FORMAT_LIST };
916 UINT ret = 0;
917
918 WINPR_ASSERT(clipboard);
919 WINPR_ASSERT(formats || (numFormats == 0));
920
921 if (!force && !xf_clipboard_changed(clipboard, formats, numFormats))
922 return CHANNEL_RC_OK;
923
924#if defined(WITH_DEBUG_CLIPRDR)
925 for (UINT32 x = 0; x < numFormats; x++)
926 {
927 const CLIPRDR_FORMAT* format = &formats[x];
928 DEBUG_CLIPRDR("announcing format 0x%08" PRIx32 " [%s] [%s]", format->formatId,
929 ClipboardGetFormatIdString(format->formatId), format->formatName);
930 }
931#endif
932
933 xf_clipboard_copy_formats(clipboard, formats, numFormats);
934 /* Ensure all pending requests are answered. */
935 xf_cliprdr_send_data_response(clipboard, nullptr, nullptr, 0);
936
937 xf_cliprdr_clear_cached_data(clipboard);
938
939 ret = cliprdr_file_context_notify_new_client_format_list(clipboard->file);
940 if (ret)
941 return ret;
942
943 WINPR_ASSERT(clipboard->context);
944 WINPR_ASSERT(clipboard->context->ClientFormatList);
945 return clipboard->context->ClientFormatList(clipboard->context, &formatList);
946}
947
948static void xf_cliprdr_get_requested_targets(xfClipboard* clipboard)
949{
950 UINT32 numFormats = 0;
951 CLIPRDR_FORMAT* formats = xf_cliprdr_get_client_formats(clipboard, &numFormats);
952 xf_cliprdr_send_format_list(clipboard, formats, numFormats, FALSE);
953 xf_cliprdr_free_formats(formats, numFormats);
954}
955
956static void xf_cliprdr_process_requested_data(xfClipboard* clipboard, BOOL hasData,
957 const BYTE* data, size_t size)
958{
959 BOOL bSuccess = 0;
960 UINT32 SrcSize = 0;
961 UINT32 DstSize = 0;
962 INT64 srcFormatId = -1;
963 BYTE* pDstData = nullptr;
964 const xfCliprdrFormat* format = nullptr;
965
966 WINPR_ASSERT(clipboard);
967
968 if (clipboard->incr_starts && hasData)
969 return;
970
971 /* Reset incr_data_length, as we've reached the end of a possible incremental update.
972 * this ensures on next event that the buffer is not reused. */
973 clipboard->incr_data_length = 0;
974
975 format = xf_cliprdr_get_client_format_by_id(clipboard, clipboard->requestedFormatId);
976
977 if (!hasData || !data || !format)
978 {
979 xf_cliprdr_send_data_response(clipboard, format, nullptr, 0);
980 return;
981 }
982
983 switch (format->formatToRequest)
984 {
985 case CF_RAW:
986 srcFormatId = CF_RAW;
987 break;
988
989 case CF_TEXT:
990 case CF_OEMTEXT:
991 case CF_UNICODETEXT:
992 srcFormatId = format->localFormat;
993 break;
994
995 default:
996 srcFormatId = format->localFormat;
997 break;
998 }
999
1000 if (srcFormatId < 0)
1001 {
1002 xf_cliprdr_send_data_response(clipboard, format, nullptr, 0);
1003 return;
1004 }
1005
1006 ClipboardLock(clipboard->system);
1007 SrcSize = (UINT32)size;
1008 bSuccess = ClipboardSetData(clipboard->system, (UINT32)srcFormatId, data, SrcSize);
1009
1010 if (bSuccess)
1011 {
1012 DstSize = 0;
1013 pDstData =
1014 (BYTE*)ClipboardGetData(clipboard->system, clipboard->requestedFormatId, &DstSize);
1015 }
1016 ClipboardUnlock(clipboard->system);
1017
1018 if (!pDstData)
1019 {
1020 xf_cliprdr_send_data_response(clipboard, format, nullptr, 0);
1021 return;
1022 }
1023
1024 /*
1025 * File lists require a bit of postprocessing to convert them from WinPR's FILDESCRIPTOR
1026 * format to CLIPRDR_FILELIST expected by the server.
1027 *
1028 * We check for "FileGroupDescriptorW" format being registered (i.e., nonzero) in order
1029 * to not process CF_RAW as a file list in case WinPR does not support file transfers.
1030 */
1031 ClipboardLock(clipboard->system);
1032 if (format->formatToRequest &&
1033 (format->formatToRequest ==
1034 ClipboardGetFormatId(clipboard->system, type_FileGroupDescriptorW)))
1035 {
1036 UINT error = NO_ERROR;
1037 FILEDESCRIPTORW* file_array = (FILEDESCRIPTORW*)pDstData;
1038 UINT32 file_count = DstSize / sizeof(FILEDESCRIPTORW);
1039 pDstData = nullptr;
1040 DstSize = 0;
1041
1042 const UINT32 flags = cliprdr_file_context_remote_get_flags(clipboard->file);
1043 error = cliprdr_serialize_file_list_ex(flags, file_array, file_count, &pDstData, &DstSize);
1044
1045 if (error)
1046 WLog_ERR(TAG, "failed to serialize CLIPRDR_FILELIST: 0x%08X", error);
1047 else
1048 {
1049 UINT32 formatId = ClipboardGetFormatId(clipboard->system, mime_uri_list);
1050 UINT32 url_size = 0;
1051
1052 char* url = ClipboardGetData(clipboard->system, formatId, &url_size);
1053 cliprdr_file_context_update_client_data(clipboard->file, url, url_size);
1054 free(url);
1055 }
1056
1057 free(file_array);
1058 }
1059 ClipboardUnlock(clipboard->system);
1060
1061 xf_cliprdr_send_data_response(clipboard, format, pDstData, DstSize);
1062 free(pDstData);
1063}
1064
1065static BOOL xf_restore_input_flags(xfClipboard* clipboard)
1066{
1067 WINPR_ASSERT(clipboard);
1068
1069 xfContext* xfc = clipboard->xfc;
1070 WINPR_ASSERT(xfc);
1071
1072 if (clipboard->event_mask != 0)
1073 {
1074 XSelectInput(xfc->display, xfc->drawable, clipboard->event_mask);
1075 clipboard->event_mask = 0;
1076 }
1077 return TRUE;
1078}
1079
1080static BOOL append(xfClipboard* clipboard, const void* sdata, size_t length)
1081{
1082 WINPR_ASSERT(clipboard);
1083
1084 const size_t size = length + clipboard->incr_data_length + 2;
1085 BYTE* data = realloc(clipboard->incr_data, size);
1086 if (!data)
1087 return FALSE;
1088 clipboard->incr_data = data;
1089 memcpy(&data[clipboard->incr_data_length], sdata, length);
1090 clipboard->incr_data_length += length;
1091 clipboard->incr_data[clipboard->incr_data_length + 0] = '\0';
1092 clipboard->incr_data[clipboard->incr_data_length + 1] = '\0';
1093 return TRUE;
1094}
1095
1096static BOOL xf_cliprdr_stop_incr(xfClipboard* clipboard)
1097{
1098 clipboard->incr_starts = FALSE;
1099 clipboard->incr_data_length = 0;
1100 return xf_restore_input_flags(clipboard);
1101}
1102
1103static BOOL xf_cliprdr_get_requested_data(xfClipboard* clipboard, Atom target)
1104{
1105 WINPR_ASSERT(clipboard);
1106
1107 xfContext* xfc = clipboard->xfc;
1108 WINPR_ASSERT(xfc);
1109
1110 const xfCliprdrFormat* format =
1111 xf_cliprdr_get_client_format_by_id(clipboard, clipboard->requestedFormatId);
1112
1113 if (!format || (format->atom != target))
1114 {
1115 xf_cliprdr_send_data_response(clipboard, format, nullptr, 0);
1116 return FALSE;
1117 }
1118
1119 Atom type = 0;
1120 BOOL has_data = FALSE;
1121 int format_property = 0;
1122 unsigned long length = 0;
1123 unsigned long total_bytes = 0;
1124 BYTE* property_data = nullptr;
1125 const int rc = LogDynAndXGetWindowProperty(
1126 xfc->log, xfc->display, xfc->drawable, clipboard->property_atom, 0, 0, False, target, &type,
1127 &format_property, &length, &total_bytes, &property_data);
1128 if (property_data)
1129 XFree(property_data);
1130 if (rc != Success)
1131 {
1132 xf_cliprdr_send_data_response(clipboard, format, nullptr, 0);
1133 return FALSE;
1134 }
1135
1136 size_t len = 0;
1137
1138 /* No data, empty return */
1139 if ((total_bytes <= 0) && !clipboard->incr_starts)
1140 {
1141 xf_cliprdr_stop_incr(clipboard);
1142 }
1143 /* We have to read incremental updates */
1144 else if (type == clipboard->incr_atom)
1145 {
1146 xf_cliprdr_stop_incr(clipboard);
1147 clipboard->incr_starts = TRUE;
1148 has_data = TRUE; /* data will follow in PropertyNotify event */
1149 }
1150 else
1151 {
1152 BYTE* incremental_data = nullptr;
1153 unsigned long incremental_len = 0;
1154
1155 /* Incremental updates completed, pass data */
1156 len = clipboard->incr_data_length;
1157 if (total_bytes <= 0)
1158 {
1159 xf_cliprdr_stop_incr(clipboard);
1160 has_data = TRUE;
1161 }
1162 /* Read incremental data batch */
1163 else if (LogDynAndXGetWindowProperty(
1164 xfc->log, xfc->display, xfc->drawable, clipboard->property_atom, 0,
1165 WINPR_ASSERTING_INT_CAST(int32_t, total_bytes), False, target, &type,
1166 &format_property, &incremental_len, &length, &incremental_data) == Success)
1167 {
1168 has_data = append(clipboard, incremental_data, incremental_len);
1169 len = clipboard->incr_data_length;
1170 }
1171
1172 if (incremental_data)
1173 XFree(incremental_data);
1174 }
1175
1176 LogDynAndXDeleteProperty(xfc->log, xfc->display, xfc->drawable, clipboard->property_atom);
1177 xf_cliprdr_process_requested_data(clipboard, has_data, clipboard->incr_data, len);
1178
1179 return TRUE;
1180}
1181
1182static void xf_cliprdr_append_target(xfClipboard* clipboard, Atom target)
1183{
1184 WINPR_ASSERT(clipboard);
1185
1186 if (clipboard->numTargets >= ARRAYSIZE(clipboard->targets))
1187 return;
1188
1189 for (size_t i = 0; i < clipboard->numTargets; i++)
1190 {
1191 if (clipboard->targets[i] == target)
1192 return;
1193 }
1194
1195 clipboard->targets[clipboard->numTargets++] = target;
1196}
1197
1198static void xf_cliprdr_provide_targets(xfClipboard* clipboard, const XSelectionEvent* respond)
1199{
1200 xfContext* xfc = nullptr;
1201
1202 WINPR_ASSERT(clipboard);
1203
1204 xfc = clipboard->xfc;
1205 WINPR_ASSERT(xfc);
1206
1207 if (respond->property != None)
1208 {
1209 WINPR_ASSERT(clipboard->numTargets <= INT32_MAX);
1210 LogDynAndXChangeProperty(xfc->log, xfc->display, respond->requestor, respond->property,
1211 XA_ATOM, 32, PropModeReplace, (const BYTE*)clipboard->targets,
1212 (int)clipboard->numTargets);
1213 }
1214}
1215
1216static void xf_cliprdr_provide_timestamp(xfClipboard* clipboard, const XSelectionEvent* respond)
1217{
1218 xfContext* xfc = nullptr;
1219
1220 WINPR_ASSERT(clipboard);
1221
1222 xfc = clipboard->xfc;
1223 WINPR_ASSERT(xfc);
1224
1225 if (respond->property != None)
1226 {
1227 LogDynAndXChangeProperty(xfc->log, xfc->display, respond->requestor, respond->property,
1228 XA_INTEGER, 32, PropModeReplace,
1229 (const BYTE*)&clipboard->selection_ownership_timestamp, 1);
1230 }
1231}
1232
1233#define xf_cliprdr_provide_data(clipboard, respond, data, size) \
1234 xf_cliprdr_provide_data_((clipboard), (respond), (data), (size), __FILE__, __func__, __LINE__)
1235static void xf_cliprdr_provide_data_(xfClipboard* clipboard, const XSelectionEvent* respond,
1236 const BYTE* data, UINT32 size, const char* file,
1237 const char* fkt, size_t line)
1238{
1239 WINPR_ASSERT(clipboard);
1240
1241 xfContext* xfc = clipboard->xfc;
1242 WINPR_ASSERT(xfc);
1243
1244 if (respond->property != None)
1245 {
1246 LogDynAndXChangeProperty_ex(xfc->log, file, fkt, line, xfc->display, respond->requestor,
1247 respond->property, respond->target, 8, PropModeReplace, data,
1248 WINPR_ASSERTING_INT_CAST(int32_t, size));
1249 }
1250}
1251
1252static void log_selection_event(xfContext* xfc, const XEvent* event)
1253{
1254 const DWORD level = WLOG_TRACE;
1255 static wLog* _log_cached_ptr = nullptr;
1256 if (!_log_cached_ptr)
1257 _log_cached_ptr = WLog_Get(TAG);
1258 if (WLog_IsLevelActive(_log_cached_ptr, level))
1259 {
1260
1261 switch (event->type)
1262 {
1263 case SelectionClear:
1264 {
1265 const XSelectionClearEvent* xevent = &event->xselectionclear;
1266 char* selection =
1267 Safe_XGetAtomName(_log_cached_ptr, xfc->display, xevent->selection);
1268 WLog_Print(_log_cached_ptr, level, "got event %s [selection %s]",
1269 x11_event_string(event->type), selection);
1270 XFree(selection);
1271 }
1272 break;
1273 case SelectionNotify:
1274 {
1275 const XSelectionEvent* xevent = &event->xselection;
1276 char* selection =
1277 Safe_XGetAtomName(_log_cached_ptr, xfc->display, xevent->selection);
1278 char* target = Safe_XGetAtomName(_log_cached_ptr, xfc->display, xevent->target);
1279 char* property = Safe_XGetAtomName(_log_cached_ptr, xfc->display, xevent->property);
1280 WLog_Print(_log_cached_ptr, level,
1281 "got event %s [selection %s, target %s, property %s]",
1282 x11_event_string(event->type), selection, target, property);
1283 XFree(selection);
1284 XFree(target);
1285 XFree(property);
1286 }
1287 break;
1288 case SelectionRequest:
1289 {
1290 const XSelectionRequestEvent* xevent = &event->xselectionrequest;
1291 char* selection =
1292 Safe_XGetAtomName(_log_cached_ptr, xfc->display, xevent->selection);
1293 char* target = Safe_XGetAtomName(_log_cached_ptr, xfc->display, xevent->target);
1294 char* property = Safe_XGetAtomName(_log_cached_ptr, xfc->display, xevent->property);
1295 WLog_Print(_log_cached_ptr, level,
1296 "got event %s [selection %s, target %s, property %s]",
1297 x11_event_string(event->type), selection, target, property);
1298 XFree(selection);
1299 XFree(target);
1300 XFree(property);
1301 }
1302 break;
1303 case PropertyNotify:
1304 {
1305 const XPropertyEvent* xevent = &event->xproperty;
1306 char* atom = Safe_XGetAtomName(_log_cached_ptr, xfc->display, xevent->atom);
1307 WLog_Print(_log_cached_ptr, level, "got event %s [atom %s]",
1308 x11_event_string(event->type), atom);
1309 XFree(atom);
1310 }
1311 break;
1312 default:
1313 break;
1314 }
1315 }
1316}
1317
1318static BOOL xf_cliprdr_process_selection_notify(xfClipboard* clipboard,
1319 const XSelectionEvent* xevent)
1320{
1321 WINPR_ASSERT(clipboard);
1322 WINPR_ASSERT(xevent);
1323
1324 if (xevent->target == clipboard->targets[1])
1325 {
1326 if (xevent->property == None)
1327 {
1328 xf_cliprdr_send_client_format_list(clipboard, FALSE);
1329 }
1330 else
1331 {
1332 xf_cliprdr_get_requested_targets(clipboard);
1333 }
1334
1335 return TRUE;
1336 }
1337 else
1338 {
1339 return xf_cliprdr_get_requested_data(clipboard, xevent->target);
1340 }
1341}
1342
1343void xf_cliprdr_clear_cached_data(xfClipboard* clipboard)
1344{
1345 WINPR_ASSERT(clipboard);
1346
1347 ClipboardLock(clipboard->system);
1348 ClipboardEmpty(clipboard->system);
1349
1350 HashTable_Clear(clipboard->cachedData);
1351 HashTable_Clear(clipboard->cachedRawData);
1352
1353 cliprdr_file_context_clear(clipboard->file);
1354
1355 xf_cliprdr_stop_incr(clipboard);
1356 ClipboardUnlock(clipboard->system);
1357}
1358
1359static void* format_to_cache_slot(UINT32 format)
1360{
1361 union
1362 {
1363 uintptr_t uptr;
1364 void* vptr;
1365 } cnv;
1366 cnv.uptr = 0x100000000ULL + format;
1367 return cnv.vptr;
1368}
1369
1370static UINT32 get_dst_format_id_for_local_request(xfClipboard* clipboard,
1371 const xfCliprdrFormat* format)
1372{
1373 UINT32 dstFormatId = 0;
1374
1375 WINPR_ASSERT(format);
1376
1377 if (!format->formatName)
1378 return format->localFormat;
1379
1380 ClipboardLock(clipboard->system);
1381 if (strcmp(format->formatName, type_HtmlFormat) == 0)
1382 dstFormatId = ClipboardGetFormatId(clipboard->system, mime_html);
1383 ClipboardUnlock(clipboard->system);
1384
1385 if (strcmp(format->formatName, type_FileGroupDescriptorW) == 0)
1386 dstFormatId = format->localFormat;
1387
1388 return dstFormatId;
1389}
1390
1391static void get_src_format_info_for_local_request(xfClipboard* clipboard,
1392 const xfCliprdrFormat* format,
1393 UINT32* srcFormatId, BOOL* nullTerminated)
1394{
1395 *srcFormatId = 0;
1396 *nullTerminated = FALSE;
1397
1398 if (format->formatName)
1399 {
1400 ClipboardLock(clipboard->system);
1401 if (strcmp(format->formatName, type_HtmlFormat) == 0)
1402 {
1403 *srcFormatId = ClipboardGetFormatId(clipboard->system, type_HtmlFormat);
1404 *nullTerminated = TRUE;
1405 }
1406 else if (strcmp(format->formatName, type_FileGroupDescriptorW) == 0)
1407 {
1408 *srcFormatId = ClipboardGetFormatId(clipboard->system, type_FileGroupDescriptorW);
1409 *nullTerminated = TRUE;
1410 }
1411 ClipboardUnlock(clipboard->system);
1412 }
1413 else
1414 {
1415 *srcFormatId = format->formatToRequest;
1416 switch (format->formatToRequest)
1417 {
1418 case CF_TEXT:
1419 case CF_OEMTEXT:
1420 case CF_UNICODETEXT:
1421 *nullTerminated = TRUE;
1422 break;
1423 case CF_DIB:
1424 *srcFormatId = CF_DIB;
1425 break;
1426 case CF_TIFF:
1427 *srcFormatId = CF_TIFF;
1428 break;
1429 default:
1430 break;
1431 }
1432 }
1433}
1434
1435static xfCachedData* convert_data_from_existing_raw_data(xfClipboard* clipboard,
1436 xfCachedData* cached_raw_data,
1437 UINT32 srcFormatId, BOOL nullTerminated,
1438 UINT32 dstFormatId)
1439{
1440 UINT32 dst_size = 0;
1441
1442 WINPR_ASSERT(clipboard);
1443 WINPR_ASSERT(cached_raw_data);
1444 WINPR_ASSERT(cached_raw_data->data);
1445
1446 ClipboardLock(clipboard->system);
1447 BOOL success = ClipboardSetData(clipboard->system, srcFormatId, cached_raw_data->data,
1448 cached_raw_data->data_length);
1449 if (!success)
1450 {
1451 WLog_WARN(TAG, "Failed to set clipboard data (formatId: %u, data: %p, data_length: %u)",
1452 srcFormatId, WINPR_CXX_COMPAT_CAST(const void*, cached_raw_data->data),
1453 cached_raw_data->data_length);
1454 ClipboardUnlock(clipboard->system);
1455 return nullptr;
1456 }
1457
1458 BYTE* dst_data = ClipboardGetData(clipboard->system, dstFormatId, &dst_size);
1459 if (!dst_data)
1460 {
1461 WLog_WARN(TAG, "Failed to get converted clipboard data");
1462 ClipboardUnlock(clipboard->system);
1463 return nullptr;
1464 }
1465 ClipboardUnlock(clipboard->system);
1466
1467 if (nullTerminated)
1468 {
1469 BYTE* nullTerminator = memchr(dst_data, '\0', dst_size);
1470 if (nullTerminator)
1471 {
1472 const intptr_t diff = nullTerminator - dst_data;
1473 WINPR_ASSERT(diff >= 0);
1474 WINPR_ASSERT(diff <= UINT32_MAX);
1475 dst_size = (UINT32)diff;
1476 }
1477 }
1478
1479 xfCachedData* cached_data = xf_cached_data_new(dst_data, dst_size);
1480 if (!cached_data)
1481 {
1482 WLog_WARN(TAG, "Failed to allocate cache entry");
1483 free(dst_data);
1484 return nullptr;
1485 }
1486
1487 if (!HashTable_Insert(clipboard->cachedData, format_to_cache_slot(dstFormatId), cached_data))
1488 {
1489 WLog_WARN(TAG, "Failed to cache clipboard data");
1490 xf_cached_data_free(cached_data);
1491 return nullptr;
1492 }
1493
1494 return cached_data;
1495}
1496
1497static BOOL xf_cliprdr_process_selection_request(xfClipboard* clipboard,
1498 const XSelectionRequestEvent* xevent)
1499{
1500 int fmt = 0;
1501 Atom type = 0;
1502 UINT32 formatId = 0;
1503 XSelectionEvent* respond = nullptr;
1504 BYTE* data = nullptr;
1505 BOOL delayRespond = 0;
1506 BOOL rawTransfer = 0;
1507 unsigned long length = 0;
1508 unsigned long bytes_left = 0;
1509 xfContext* xfc = nullptr;
1510
1511 WINPR_ASSERT(clipboard);
1512 WINPR_ASSERT(xevent);
1513
1514 xfc = clipboard->xfc;
1515 WINPR_ASSERT(xfc);
1516
1517 if (xevent->owner != xfc->drawable)
1518 return FALSE;
1519
1520 delayRespond = FALSE;
1521
1522 if (!(respond = (XSelectionEvent*)calloc(1, sizeof(XSelectionEvent))))
1523 {
1524 WLog_ERR(TAG, "failed to allocate XEvent data");
1525 return FALSE;
1526 }
1527
1528 respond->property = None;
1529 respond->type = SelectionNotify;
1530 respond->display = xevent->display;
1531 respond->requestor = xevent->requestor;
1532 respond->selection = xevent->selection;
1533 respond->target = xevent->target;
1534 respond->time = xevent->time;
1535
1536 if (xevent->target == clipboard->targets[0]) /* TIMESTAMP */
1537 {
1538 /* Someone else requests the selection's timestamp */
1539 respond->property = xevent->property;
1540 xf_cliprdr_provide_timestamp(clipboard, respond);
1541 }
1542 else if (xevent->target == clipboard->targets[1]) /* TARGETS */
1543 {
1544 /* Someone else requests our available formats */
1545 respond->property = xevent->property;
1546 xf_cliprdr_provide_targets(clipboard, respond);
1547 }
1548 else
1549 {
1550 const CLIPRDR_FORMAT* format =
1551 xf_cliprdr_get_server_format_by_atom(clipboard, xevent->target);
1552 const xfCliprdrFormat* cformat =
1553 xf_cliprdr_get_client_format_by_atom(clipboard, xevent->target);
1554
1555 if (format && (xevent->requestor != xfc->drawable))
1556 {
1557 formatId = format->formatId;
1558 rawTransfer = FALSE;
1559 xfCachedData* cached_data = nullptr;
1560
1561 if (formatId == CF_RAW)
1562 {
1563 if (LogDynAndXGetWindowProperty(
1564 xfc->log, xfc->display, xevent->requestor, clipboard->property_atom, 0, 4,
1565 0, XA_INTEGER, &type, &fmt, &length, &bytes_left, &data) != Success)
1566 {
1567 }
1568
1569 if (data)
1570 {
1571 rawTransfer = TRUE;
1572 CopyMemory(&formatId, data, 4);
1573 XFree(data);
1574 }
1575 }
1576
1577 const UINT32 dstFormatId = get_dst_format_id_for_local_request(clipboard, cformat);
1578 DEBUG_CLIPRDR("formatId: 0x%08" PRIx32 ", dstFormatId: 0x%08" PRIx32 "", formatId,
1579 dstFormatId);
1580
1581 wHashTable* table = clipboard->cachedData;
1582 if (rawTransfer)
1583 table = clipboard->cachedRawData;
1584
1585 HashTable_Lock(table);
1586
1587 if (!rawTransfer)
1588 cached_data = HashTable_GetItemValue(table, format_to_cache_slot(dstFormatId));
1589 else
1590 cached_data = HashTable_GetItemValue(table, format_to_cache_slot(formatId));
1591
1592 DEBUG_CLIPRDR("hasCachedData: %u, rawTransfer: %d", cached_data ? 1u : 0u, rawTransfer);
1593
1594 if (!cached_data && !rawTransfer)
1595 {
1596 UINT32 srcFormatId = 0;
1597 BOOL nullTerminated = FALSE;
1598 xfCachedData* cached_raw_data = nullptr;
1599
1600 get_src_format_info_for_local_request(clipboard, cformat, &srcFormatId,
1601 &nullTerminated);
1602 cached_raw_data =
1603 HashTable_GetItemValue(clipboard->cachedRawData, (void*)(UINT_PTR)srcFormatId);
1604
1605 DEBUG_CLIPRDR("hasCachedRawData: %u, rawDataLength: %u", cached_raw_data ? 1u : 0u,
1606 cached_raw_data ? cached_raw_data->data_length : 0);
1607
1608 if (cached_raw_data && cached_raw_data->data_length != 0)
1609 cached_data = convert_data_from_existing_raw_data(
1610 clipboard, cached_raw_data, srcFormatId, nullTerminated, dstFormatId);
1611 }
1612
1613 DEBUG_CLIPRDR("hasCachedData: %u", cached_data ? 1u : 0u);
1614
1615 if (cached_data)
1616 {
1617 /* Cached clipboard data available. Send it now */
1618 respond->property = xevent->property;
1619
1620 // NOLINTNEXTLINE(clang-analyzer-unix.Malloc)
1621 xf_cliprdr_provide_data(clipboard, respond, cached_data->data,
1622 cached_data->data_length);
1623 }
1624 else if (clipboard->respond)
1625 {
1626 /* duplicate request */
1627 }
1628 else
1629 {
1630 WINPR_ASSERT(cformat);
1631
1636 respond->property = xevent->property;
1637 clipboard->respond = respond;
1638 requested_format_replace(&clipboard->requestedFormat, formatId, dstFormatId,
1639 cformat->formatName);
1640 clipboard->data_raw_format = rawTransfer;
1641 delayRespond = TRUE;
1642 xf_cliprdr_send_data_request(clipboard, formatId, cformat);
1643 }
1644 HashTable_Unlock(table);
1645 }
1646 }
1647
1648 if (!delayRespond)
1649 {
1650 union
1651 {
1652 XEvent* ev;
1653 XSelectionEvent* sev;
1654 } conv;
1655
1656 conv.sev = respond;
1657 LogDynAndXSendEvent(xfc->log, xfc->display, xevent->requestor, 0, 0, conv.ev);
1658 LogDynAndXFlush(xfc->log, xfc->display);
1659 free(respond);
1660 }
1661
1662 return TRUE;
1663}
1664
1665static BOOL xf_cliprdr_process_selection_clear(xfClipboard* clipboard,
1666 const XSelectionClearEvent* xevent)
1667{
1668 xfContext* xfc = nullptr;
1669
1670 WINPR_ASSERT(clipboard);
1671 WINPR_ASSERT(xevent);
1672
1673 xfc = clipboard->xfc;
1674 WINPR_ASSERT(xfc);
1675
1676 WINPR_UNUSED(xevent);
1677
1678 if (xf_cliprdr_is_self_owned(clipboard))
1679 return FALSE;
1680
1681 LogDynAndXDeleteProperty(xfc->log, xfc->display, clipboard->root_window,
1682 clipboard->property_atom);
1683 return TRUE;
1684}
1685
1686static BOOL xf_cliprdr_process_property_notify(xfClipboard* clipboard, const XPropertyEvent* xevent)
1687{
1688 const xfCliprdrFormat* format = nullptr;
1689 xfContext* xfc = nullptr;
1690
1691 if (!clipboard)
1692 return TRUE;
1693
1694 xfc = clipboard->xfc;
1695 WINPR_ASSERT(xfc);
1696 WINPR_ASSERT(xevent);
1697
1698 if (xevent->atom == clipboard->timestamp_property_atom)
1699 {
1700 /* This is the response to the property change we did
1701 * in xf_cliprdr_prepare_to_set_selection_owner. Now
1702 * we can set ourselves as the selection owner. (See
1703 * comments in those functions below.) */
1704 xf_cliprdr_set_selection_owner(xfc, clipboard, xevent->time);
1705 return TRUE;
1706 }
1707
1708 if (xevent->atom != clipboard->property_atom)
1709 return FALSE; /* Not cliprdr-related */
1710
1711 if (xevent->window == clipboard->root_window)
1712 {
1713 xf_cliprdr_send_client_format_list(clipboard, FALSE);
1714 }
1715 else if ((xevent->window == xfc->drawable) && (xevent->state == PropertyNewValue) &&
1716 clipboard->incr_starts)
1717 {
1718 format = xf_cliprdr_get_client_format_by_id(clipboard, clipboard->requestedFormatId);
1719
1720 if (format)
1721 xf_cliprdr_get_requested_data(clipboard, format->atom);
1722 }
1723
1724 return TRUE;
1725}
1726
1727void xf_cliprdr_handle_xevent(xfContext* xfc, const XEvent* event)
1728{
1729 xfClipboard* clipboard = nullptr;
1730
1731 if (!xfc || !event)
1732 return;
1733
1734 clipboard = xfc->clipboard;
1735
1736 if (!clipboard)
1737 return;
1738
1739#ifdef WITH_XFIXES
1740
1741 if (clipboard->xfixes_supported &&
1742 event->type == XFixesSelectionNotify + clipboard->xfixes_event_base)
1743 {
1744 const XFixesSelectionNotifyEvent* se = (const XFixesSelectionNotifyEvent*)event;
1745
1746 if (se->subtype == XFixesSetSelectionOwnerNotify)
1747 {
1748 if (se->selection != clipboard->clipboard_atom)
1749 return;
1750
1751 if (LogDynAndXGetSelectionOwner(xfc->log, xfc->display, se->selection) == xfc->drawable)
1752 return;
1753
1754 clipboard->owner = None;
1755 xf_cliprdr_check_owner(clipboard);
1756 }
1757
1758 return;
1759 }
1760
1761#endif
1762
1763 switch (event->type)
1764 {
1765 case SelectionNotify:
1766 log_selection_event(xfc, event);
1767 xf_cliprdr_process_selection_notify(clipboard, &event->xselection);
1768 break;
1769
1770 case SelectionRequest:
1771 log_selection_event(xfc, event);
1772 xf_cliprdr_process_selection_request(clipboard, &event->xselectionrequest);
1773 break;
1774
1775 case SelectionClear:
1776 log_selection_event(xfc, event);
1777 xf_cliprdr_process_selection_clear(clipboard, &event->xselectionclear);
1778 break;
1779
1780 case PropertyNotify:
1781 log_selection_event(xfc, event);
1782 xf_cliprdr_process_property_notify(clipboard, &event->xproperty);
1783 break;
1784
1785 case FocusIn:
1786 if (!clipboard->xfixes_supported)
1787 {
1788 xf_cliprdr_check_owner(clipboard);
1789 }
1790
1791 break;
1792 default:
1793 break;
1794 }
1795}
1796
1802static UINT xf_cliprdr_send_client_capabilities(xfClipboard* clipboard)
1803{
1804 CLIPRDR_CAPABILITIES capabilities = WINPR_C_ARRAY_INIT;
1805 CLIPRDR_GENERAL_CAPABILITY_SET generalCapabilitySet = WINPR_C_ARRAY_INIT;
1806
1807 WINPR_ASSERT(clipboard);
1808
1809 capabilities.cCapabilitiesSets = 1;
1810 capabilities.capabilitySets = (CLIPRDR_CAPABILITY_SET*)&(generalCapabilitySet);
1811 generalCapabilitySet.capabilitySetType = CB_CAPSTYPE_GENERAL;
1812 generalCapabilitySet.capabilitySetLength = 12;
1813 generalCapabilitySet.version = CB_CAPS_VERSION_2;
1814 generalCapabilitySet.generalFlags = CB_USE_LONG_FORMAT_NAMES;
1815
1816 WINPR_ASSERT(clipboard);
1817 generalCapabilitySet.generalFlags |= cliprdr_file_context_current_flags(clipboard->file);
1818
1819 WINPR_ASSERT(clipboard->context);
1820 WINPR_ASSERT(clipboard->context->ClientCapabilities);
1821 return clipboard->context->ClientCapabilities(clipboard->context, &capabilities);
1822}
1823
1829static UINT xf_cliprdr_send_client_format_list(xfClipboard* clipboard, BOOL force)
1830{
1831 WINPR_ASSERT(clipboard);
1832
1833 xfContext* xfc = clipboard->xfc;
1834 WINPR_ASSERT(xfc);
1835
1836 UINT32 numFormats = 0;
1837 CLIPRDR_FORMAT* formats = xf_cliprdr_get_client_formats(clipboard, &numFormats);
1838
1839 const UINT ret = xf_cliprdr_send_format_list(clipboard, formats, numFormats, force);
1840
1841 if (clipboard->owner && clipboard->owner != xfc->drawable)
1842 {
1843 /* Request the owner for TARGETS, and wait for SelectionNotify event */
1844 LogDynAndXConvertSelection(xfc->log, xfc->display, clipboard->clipboard_atom,
1845 clipboard->targets[1], clipboard->property_atom, xfc->drawable,
1846 CurrentTime);
1847 }
1848
1849 xf_cliprdr_free_formats(formats, numFormats);
1850
1851 return ret;
1852}
1853
1859static UINT xf_cliprdr_send_client_format_list_response(xfClipboard* clipboard, BOOL status)
1860{
1861 CLIPRDR_FORMAT_LIST_RESPONSE formatListResponse = WINPR_C_ARRAY_INIT;
1862
1863 formatListResponse.common.msgType = CB_FORMAT_LIST_RESPONSE;
1864 formatListResponse.common.msgFlags = status ? CB_RESPONSE_OK : CB_RESPONSE_FAIL;
1865 formatListResponse.common.dataLen = 0;
1866
1867 WINPR_ASSERT(clipboard);
1868 WINPR_ASSERT(clipboard->context);
1869 WINPR_ASSERT(clipboard->context->ClientFormatListResponse);
1870 return clipboard->context->ClientFormatListResponse(clipboard->context, &formatListResponse);
1871}
1872
1878static UINT xf_cliprdr_monitor_ready(CliprdrClientContext* context,
1879 const CLIPRDR_MONITOR_READY* monitorReady)
1880{
1881 WINPR_ASSERT(context);
1882 WINPR_ASSERT(monitorReady);
1883
1884 xfClipboard* clipboard = cliprdr_file_context_get_context(context->custom);
1885 WINPR_ASSERT(clipboard);
1886
1887 WINPR_UNUSED(monitorReady);
1888
1889 const UINT ret = xf_cliprdr_send_client_capabilities(clipboard);
1890 if (ret != CHANNEL_RC_OK)
1891 return ret;
1892
1893 xf_clipboard_formats_free(clipboard);
1894
1895 const UINT ret2 = xf_cliprdr_send_client_format_list(clipboard, TRUE);
1896 if (ret2 != CHANNEL_RC_OK)
1897 return ret2;
1898
1899 clipboard->sync = TRUE;
1900 return CHANNEL_RC_OK;
1901}
1902
1908static UINT xf_cliprdr_server_capabilities(CliprdrClientContext* context,
1909 const CLIPRDR_CAPABILITIES* capabilities)
1910{
1911 WINPR_ASSERT(context);
1912 WINPR_ASSERT(capabilities);
1913
1914 xfClipboard* clipboard = cliprdr_file_context_get_context(context->custom);
1915 WINPR_ASSERT(clipboard);
1916
1917 const BYTE* capsPtr = (const BYTE*)capabilities->capabilitySets;
1918 WINPR_ASSERT(capsPtr);
1919
1920 if (!cliprdr_file_context_remote_set_flags(clipboard->file, 0))
1921 return ERROR_INTERNAL_ERROR;
1922
1923 for (UINT32 i = 0; i < capabilities->cCapabilitiesSets; i++)
1924 {
1925 const CLIPRDR_CAPABILITY_SET* caps = (const CLIPRDR_CAPABILITY_SET*)capsPtr;
1926
1927 if (caps->capabilitySetType == CB_CAPSTYPE_GENERAL)
1928 {
1929 const CLIPRDR_GENERAL_CAPABILITY_SET* generalCaps =
1930 (const CLIPRDR_GENERAL_CAPABILITY_SET*)caps;
1931
1932 if (!cliprdr_file_context_remote_set_flags(clipboard->file, generalCaps->generalFlags))
1933 return ERROR_INTERNAL_ERROR;
1934 }
1935
1936 capsPtr += caps->capabilitySetLength;
1937 }
1938
1939 return CHANNEL_RC_OK;
1940}
1941
1942static void xf_cliprdr_prepare_to_set_selection_owner(xfContext* xfc, xfClipboard* clipboard)
1943{
1944 WINPR_ASSERT(xfc);
1945 WINPR_ASSERT(clipboard);
1946 /*
1947 * When you're writing to the selection in response to a
1948 * normal X event like a mouse click or keyboard action, you
1949 * get the selection timestamp by copying the time field out
1950 * of that X event. Here, we're doing it on our own
1951 * initiative, so we have to _request_ the X server time.
1952 *
1953 * There isn't a GetServerTime request in the X protocol, so I
1954 * work around it by setting a property on our own window, and
1955 * waiting for a PropertyNotify event to come back telling me
1956 * it's been done - which will have a timestamp we can use.
1957 */
1958
1959 /* We have to set the property to some value, but it doesn't
1960 * matter what. Set it to its own name, which we have here
1961 * anyway! */
1962 Atom value = clipboard->timestamp_property_atom;
1963
1964 LogDynAndXChangeProperty(xfc->log, xfc->display, xfc->drawable,
1965 clipboard->timestamp_property_atom, XA_ATOM, 32, PropModeReplace,
1966 (const BYTE*)&value, 1);
1967 LogDynAndXFlush(xfc->log, xfc->display);
1968}
1969
1970static void xf_cliprdr_set_selection_owner(xfContext* xfc, xfClipboard* clipboard, Time timestamp)
1971{
1972 WINPR_ASSERT(xfc);
1973 WINPR_ASSERT(clipboard);
1974 /*
1975 * Actually set ourselves up as the selection owner, now that
1976 * we have a timestamp to use.
1977 */
1978
1979 clipboard->selection_ownership_timestamp = timestamp;
1980 LogDynAndXSetSelectionOwner(xfc->log, xfc->display, clipboard->clipboard_atom, xfc->drawable,
1981 timestamp);
1982 LogDynAndXFlush(xfc->log, xfc->display);
1983}
1984
1990static UINT xf_cliprdr_server_format_list(CliprdrClientContext* context,
1991 const CLIPRDR_FORMAT_LIST* formatList)
1992{
1993 xfContext* xfc = nullptr;
1994 UINT ret = 0;
1995 xfClipboard* clipboard = nullptr;
1996
1997 WINPR_ASSERT(context);
1998 WINPR_ASSERT(formatList);
1999
2000 clipboard = cliprdr_file_context_get_context(context->custom);
2001 WINPR_ASSERT(clipboard);
2002
2003 xfc = clipboard->xfc;
2004 WINPR_ASSERT(xfc);
2005
2006 xf_lock_x11(xfc);
2007
2008 /* Clear the active SelectionRequest, as it is now invalid */
2009 free(clipboard->respond);
2010 clipboard->respond = nullptr;
2011
2012 xf_clipboard_formats_free(clipboard);
2013 xf_cliprdr_clear_cached_data(clipboard);
2014 requested_format_free(&clipboard->requestedFormat);
2015
2016 xf_clipboard_free_server_formats(clipboard);
2017
2018 clipboard->numServerFormats = formatList->numFormats + 1; /* +1 for CF_RAW */
2019
2020 if (!(clipboard->serverFormats =
2021 (CLIPRDR_FORMAT*)calloc(clipboard->numServerFormats, sizeof(CLIPRDR_FORMAT))))
2022 {
2023 WLog_ERR(TAG, "failed to allocate %" PRIu32 " CLIPRDR_FORMAT structs",
2024 clipboard->numServerFormats);
2025 ret = CHANNEL_RC_NO_MEMORY;
2026 goto out;
2027 }
2028
2029 for (size_t i = 0; i < formatList->numFormats; i++)
2030 {
2031 const CLIPRDR_FORMAT* format = &formatList->formats[i];
2032 CLIPRDR_FORMAT* srvFormat = &clipboard->serverFormats[i];
2033
2034 srvFormat->formatId = format->formatId;
2035
2036 if (format->formatName)
2037 {
2038 srvFormat->formatName = _strdup(format->formatName);
2039
2040 if (!srvFormat->formatName)
2041 {
2042 for (UINT32 k = 0; k < i; k++)
2043 free(clipboard->serverFormats[k].formatName);
2044
2045 clipboard->numServerFormats = 0;
2046 free(clipboard->serverFormats);
2047 clipboard->serverFormats = nullptr;
2048 ret = CHANNEL_RC_NO_MEMORY;
2049 goto out;
2050 }
2051 }
2052 }
2053
2054 ClipboardLock(clipboard->system);
2055 ret = cliprdr_file_context_notify_new_server_format_list(clipboard->file);
2056 ClipboardUnlock(clipboard->system);
2057 if (ret)
2058 goto out;
2059
2060 /* CF_RAW is always implicitly supported by the server */
2061 {
2062 CLIPRDR_FORMAT* format = &clipboard->serverFormats[formatList->numFormats];
2063 format->formatId = CF_RAW;
2064 format->formatName = nullptr;
2065 }
2066 xf_cliprdr_provide_server_format_list(clipboard);
2067 clipboard->numTargets = 2;
2068
2069 for (size_t i = 0; i < formatList->numFormats; i++)
2070 {
2071 const CLIPRDR_FORMAT* format = &formatList->formats[i];
2072
2073 for (size_t j = 0; j < clipboard->numClientFormats; j++)
2074 {
2075 const xfCliprdrFormat* clientFormat = &clipboard->clientFormats[j];
2076 if (xf_cliprdr_formats_equal(format, clientFormat))
2077 {
2078 if ((clientFormat->formatName != nullptr) &&
2079 (strcmp(type_FileGroupDescriptorW, clientFormat->formatName) == 0))
2080 {
2081 if (!cliprdr_file_context_has_local_support(clipboard->file))
2082 continue;
2083 }
2084 xf_cliprdr_append_target(clipboard, clientFormat->atom);
2085 }
2086 }
2087 }
2088
2089 ret = xf_cliprdr_send_client_format_list_response(clipboard, TRUE);
2090 if (xfc->remote_app)
2091 xf_cliprdr_set_selection_owner(xfc, clipboard, CurrentTime);
2092 else
2093 xf_cliprdr_prepare_to_set_selection_owner(xfc, clipboard);
2094
2095out:
2096 xf_unlock_x11(xfc);
2097
2098 return ret;
2099}
2100
2106static UINT xf_cliprdr_server_format_list_response(
2107 WINPR_ATTR_UNUSED CliprdrClientContext* context,
2108 WINPR_ATTR_UNUSED const CLIPRDR_FORMAT_LIST_RESPONSE* formatListResponse)
2109{
2110 WINPR_ASSERT(context);
2111 WINPR_ASSERT(formatListResponse);
2112 // xfClipboard* clipboard = (xfClipboard*) context->custom;
2113 return CHANNEL_RC_OK;
2114}
2115
2121static UINT
2122xf_cliprdr_server_format_data_request(CliprdrClientContext* context,
2123 const CLIPRDR_FORMAT_DATA_REQUEST* formatDataRequest)
2124{
2125 const xfCliprdrFormat* format = nullptr;
2126
2127 WINPR_ASSERT(context);
2128 WINPR_ASSERT(formatDataRequest);
2129
2130 xfClipboard* clipboard = cliprdr_file_context_get_context(context->custom);
2131 WINPR_ASSERT(clipboard);
2132
2133 xfContext* xfc = clipboard->xfc;
2134 WINPR_ASSERT(xfc);
2135
2136 const uint32_t formatId = formatDataRequest->requestedFormatId;
2137
2138 const BOOL rawTransfer = xf_cliprdr_is_raw_transfer_available(clipboard);
2139
2140 if (rawTransfer)
2141 {
2142 format = xf_cliprdr_get_client_format_by_id(clipboard, CF_RAW);
2143 LogDynAndXChangeProperty(xfc->log, xfc->display, xfc->drawable, clipboard->property_atom,
2144 XA_INTEGER, 32, PropModeReplace, (const BYTE*)&formatId, 1);
2145 }
2146 else
2147 format = xf_cliprdr_get_client_format_by_id(clipboard, formatId);
2148
2149 clipboard->requestedFormatId = rawTransfer ? CF_RAW : formatId;
2150 if (!format)
2151 return xf_cliprdr_send_data_response(clipboard, format, nullptr, 0);
2152
2153 DEBUG_CLIPRDR("requested format 0x%08" PRIx32 " [%s] {local 0x%08" PRIx32 " [%s]} [%s]",
2154 format->formatToRequest, ClipboardGetFormatIdString(format->formatToRequest),
2155 format->localFormat,
2156 ClipboardGetFormatName(clipboard->system, format->localFormat),
2157 format->formatName);
2158 LogDynAndXConvertSelection(xfc->log, xfc->display, clipboard->clipboard_atom, format->atom,
2159 clipboard->property_atom, xfc->drawable, CurrentTime);
2160 LogDynAndXFlush(xfc->log, xfc->display);
2161 /* After this point, we expect a SelectionNotify event from the clipboard owner. */
2162 return CHANNEL_RC_OK;
2163}
2164
2170static UINT
2171xf_cliprdr_server_format_data_response(CliprdrClientContext* context,
2172 const CLIPRDR_FORMAT_DATA_RESPONSE* formatDataResponse)
2173{
2174 BOOL bSuccess = 0;
2175 BYTE* pDstData = nullptr;
2176 UINT32 DstSize = 0;
2177 UINT32 SrcSize = 0;
2178 UINT32 srcFormatId = 0;
2179 UINT32 dstFormatId = 0;
2180 BOOL nullTerminated = FALSE;
2181 xfCachedData* cached_data = nullptr;
2182
2183 WINPR_ASSERT(context);
2184 WINPR_ASSERT(formatDataResponse);
2185
2186 xfClipboard* clipboard = cliprdr_file_context_get_context(context->custom);
2187 WINPR_ASSERT(clipboard);
2188
2189 xfContext* xfc = clipboard->xfc;
2190 WINPR_ASSERT(xfc);
2191
2192 const UINT32 size = formatDataResponse->common.dataLen;
2193 const BYTE* data = formatDataResponse->requestedFormatData;
2194
2195 if (formatDataResponse->common.msgFlags == CB_RESPONSE_FAIL)
2196 {
2197 WLog_WARN(TAG, "Format Data Response PDU msgFlags is CB_RESPONSE_FAIL");
2198 free(clipboard->respond);
2199 clipboard->respond = nullptr;
2200 return CHANNEL_RC_OK;
2201 }
2202
2203 if (!clipboard->respond)
2204 return CHANNEL_RC_OK;
2205
2206 const RequestedFormat* format = clipboard->requestedFormat;
2207 if (clipboard->data_raw_format)
2208 {
2209 srcFormatId = CF_RAW;
2210 dstFormatId = CF_RAW;
2211 }
2212 else if (!format)
2213 return ERROR_INTERNAL_ERROR;
2214 else if (format->formatName)
2215 {
2216 dstFormatId = format->localFormat;
2217
2218 ClipboardLock(clipboard->system);
2219 if (strcmp(format->formatName, type_HtmlFormat) == 0)
2220 {
2221 srcFormatId = ClipboardGetFormatId(clipboard->system, type_HtmlFormat);
2222 dstFormatId = ClipboardGetFormatId(clipboard->system, mime_html);
2223 nullTerminated = TRUE;
2224 }
2225
2226 if (strcmp(format->formatName, type_FileGroupDescriptorW) == 0)
2227 {
2228 if (!cliprdr_file_context_update_server_data(clipboard->file, clipboard->system, data,
2229 size))
2230 WLog_WARN(TAG, "failed to update file descriptors");
2231
2232 srcFormatId = ClipboardGetFormatId(clipboard->system, type_FileGroupDescriptorW);
2233 const xfCliprdrFormat* dstTargetFormat =
2234 xf_cliprdr_get_client_format_by_atom(clipboard, clipboard->respond->target);
2235 if (!dstTargetFormat)
2236 {
2237 dstFormatId = ClipboardGetFormatId(clipboard->system, mime_uri_list);
2238 }
2239 else
2240 {
2241 dstFormatId = dstTargetFormat->localFormat;
2242 }
2243
2244 nullTerminated = TRUE;
2245 }
2246 ClipboardUnlock(clipboard->system);
2247 }
2248 else
2249 {
2250 srcFormatId = format->formatToRequest;
2251 dstFormatId = format->localFormat;
2252 switch (format->formatToRequest)
2253 {
2254 case CF_TEXT:
2255 nullTerminated = TRUE;
2256 break;
2257
2258 case CF_OEMTEXT:
2259 nullTerminated = TRUE;
2260 break;
2261
2262 case CF_UNICODETEXT:
2263 nullTerminated = TRUE;
2264 break;
2265
2266 case CF_DIB:
2267 srcFormatId = CF_DIB;
2268 break;
2269
2270 case CF_TIFF:
2271 srcFormatId = CF_TIFF;
2272 break;
2273
2274 default:
2275 break;
2276 }
2277 }
2278
2279 DEBUG_CLIPRDR("requested format 0x%08" PRIx32 " [%s] {local 0x%08" PRIx32 " [%s]} [%s]",
2280 format->formatToRequest, ClipboardGetFormatIdString(format->formatToRequest),
2281 format->localFormat,
2282 ClipboardGetFormatName(clipboard->system, format->localFormat),
2283 format->formatName);
2284 SrcSize = size;
2285
2286 DEBUG_CLIPRDR("srcFormatId: 0x%08" PRIx32 ", dstFormatId: 0x%08" PRIx32 "", srcFormatId,
2287 dstFormatId);
2288
2289 ClipboardLock(clipboard->system);
2290 bSuccess = ClipboardSetData(clipboard->system, srcFormatId, data, SrcSize);
2291
2292 BOOL willQuit = FALSE;
2293 if (bSuccess)
2294 {
2295 if (SrcSize == 0)
2296 {
2297 WLog_DBG(TAG, "skipping, empty data detected!");
2298 free(clipboard->respond);
2299 clipboard->respond = nullptr;
2300 willQuit = TRUE;
2301 }
2302 else
2303 {
2304 pDstData = (BYTE*)ClipboardGetData(clipboard->system, dstFormatId, &DstSize);
2305
2306 if (!pDstData)
2307 {
2308 WLog_WARN(TAG, "failed to get clipboard data in format %s [source format %s]",
2309 ClipboardGetFormatName(clipboard->system, dstFormatId),
2310 ClipboardGetFormatName(clipboard->system, srcFormatId));
2311 }
2312
2313 if (nullTerminated && pDstData)
2314 {
2315 BYTE* nullTerminator = memchr(pDstData, '\0', DstSize);
2316 if (nullTerminator)
2317 {
2318 const intptr_t diff = nullTerminator - pDstData;
2319 WINPR_ASSERT(diff >= 0);
2320 WINPR_ASSERT(diff <= UINT32_MAX);
2321 DstSize = (UINT32)diff;
2322 }
2323 }
2324 }
2325 }
2326 ClipboardUnlock(clipboard->system);
2327 if (willQuit)
2328 return CHANNEL_RC_OK;
2329
2330 /* Cache converted and original data to avoid doing a possibly costly
2331 * conversion again on subsequent requests */
2332 if (pDstData)
2333 {
2334 cached_data = xf_cached_data_new(pDstData, DstSize);
2335 if (!cached_data)
2336 {
2337 WLog_WARN(TAG, "Failed to allocate cache entry");
2338 free(pDstData);
2339 return CHANNEL_RC_OK;
2340 }
2341 if (!HashTable_Insert(clipboard->cachedData, format_to_cache_slot(dstFormatId),
2342 cached_data))
2343 {
2344 WLog_WARN(TAG, "Failed to cache clipboard data");
2345 xf_cached_data_free(cached_data);
2346 return CHANNEL_RC_OK;
2347 }
2348 }
2349
2350 /* We have to copy the original data again, as pSrcData is now owned
2351 * by clipboard->system. Memory allocation failure is not fatal here
2352 * as this is only a cached value. */
2353 {
2354 // clipboard->cachedData owns cached_data
2355 // NOLINTNEXTLINE(clang-analyzer-unix.Malloc
2356 xfCachedData* cached_raw_data = xf_cached_data_new_copy(data, size);
2357 if (!cached_raw_data)
2358 WLog_WARN(TAG, "Failed to allocate cache entry");
2359 else
2360 {
2361 if (!HashTable_Insert(clipboard->cachedRawData, (void*)(UINT_PTR)srcFormatId,
2362 cached_raw_data))
2363 {
2364 WLog_WARN(TAG, "Failed to cache clipboard data");
2365 xf_cached_data_free(cached_raw_data);
2366 }
2367 }
2368 }
2369
2370 // clipboard->cachedRawData owns cached_raw_data
2371 // NOLINTNEXTLINE(clang-analyzer-unix.Malloc)
2372 xf_cliprdr_provide_data(clipboard, clipboard->respond, pDstData, DstSize);
2373 {
2374 union
2375 {
2376 XEvent* ev;
2377 XSelectionEvent* sev;
2378 } conv;
2379
2380 conv.sev = clipboard->respond;
2381
2382 LogDynAndXSendEvent(xfc->log, xfc->display, clipboard->respond->requestor, 0, 0, conv.ev);
2383 LogDynAndXFlush(xfc->log, xfc->display);
2384 }
2385 free(clipboard->respond);
2386 clipboard->respond = nullptr;
2387 return CHANNEL_RC_OK;
2388}
2389
2390static BOOL xf_cliprdr_is_valid_unix_filename(LPCWSTR filename)
2391{
2392 if (!filename)
2393 return FALSE;
2394
2395 if (filename[0] == L'\0')
2396 return FALSE;
2397
2398 /* Reserved characters */
2399 for (const WCHAR* c = filename; *c; ++c)
2400 {
2401 if (*c == L'/')
2402 return FALSE;
2403 }
2404
2405 return TRUE;
2406}
2407
2408xfClipboard* xf_clipboard_new(xfContext* xfc, BOOL relieveFilenameRestriction)
2409{
2410 int n = 0;
2411 rdpChannels* channels = nullptr;
2412 xfClipboard* clipboard = nullptr;
2413 const char* selectionAtom = nullptr;
2414 xfCliprdrFormat* clientFormat = nullptr;
2415 wObject* obj = nullptr;
2416
2417 WINPR_ASSERT(xfc);
2418 WINPR_ASSERT(xfc->common.context.settings);
2419
2420 if (!(clipboard = (xfClipboard*)calloc(1, sizeof(xfClipboard))))
2421 {
2422 WLog_ERR(TAG, "failed to allocate xfClipboard data");
2423 return nullptr;
2424 }
2425
2426 clipboard->file = cliprdr_file_context_new(clipboard);
2427 if (!clipboard->file)
2428 goto fail;
2429
2430 xfc->clipboard = clipboard;
2431 clipboard->xfc = xfc;
2432 channels = xfc->common.context.channels;
2433 clipboard->channels = channels;
2434 clipboard->system = ClipboardCreate();
2435 clipboard->requestedFormatId = UINT32_MAX;
2436 clipboard->root_window = DefaultRootWindow(xfc->display);
2437
2438 selectionAtom =
2439 freerdp_settings_get_string(xfc->common.context.settings, FreeRDP_ClipboardUseSelection);
2440 if (!selectionAtom)
2441 selectionAtom = "CLIPBOARD";
2442
2443 clipboard->clipboard_atom = Logging_XInternAtom(xfc->log, xfc->display, selectionAtom, FALSE);
2444
2445 if (clipboard->clipboard_atom == None)
2446 {
2447 WLog_ERR(TAG, "unable to get %s atom", selectionAtom);
2448 goto fail;
2449 }
2450
2451 clipboard->timestamp_property_atom =
2452 Logging_XInternAtom(xfc->log, xfc->display, "_FREERDP_TIMESTAMP_PROPERTY", FALSE);
2453 clipboard->property_atom =
2454 Logging_XInternAtom(xfc->log, xfc->display, "_FREERDP_CLIPRDR", FALSE);
2455 clipboard->raw_transfer_atom =
2456 Logging_XInternAtom(xfc->log, xfc->display, "_FREERDP_CLIPRDR_RAW", FALSE);
2457 clipboard->raw_format_list_atom =
2458 Logging_XInternAtom(xfc->log, xfc->display, "_FREERDP_CLIPRDR_FORMATS", FALSE);
2459 xf_cliprdr_set_raw_transfer_enabled(clipboard, TRUE);
2460 XSelectInput(xfc->display, clipboard->root_window, PropertyChangeMask);
2461#ifdef WITH_XFIXES
2462
2463 if (XFixesQueryExtension(xfc->display, &clipboard->xfixes_event_base,
2464 &clipboard->xfixes_error_base))
2465 {
2466 int xfmajor = 0;
2467 int xfminor = 0;
2468
2469 if (XFixesQueryVersion(xfc->display, &xfmajor, &xfminor))
2470 {
2471 XFixesSelectSelectionInput(xfc->display, clipboard->root_window,
2472 clipboard->clipboard_atom,
2473 XFixesSetSelectionOwnerNotifyMask);
2474 clipboard->xfixes_supported = TRUE;
2475 }
2476 else
2477 {
2478 WLog_ERR(TAG, "Error querying X Fixes extension version");
2479 }
2480 }
2481 else
2482 {
2483 WLog_ERR(TAG, "Error loading X Fixes extension");
2484 }
2485
2486#else
2487 WLog_ERR(
2488 TAG,
2489 "Warning: Using clipboard redirection without XFIXES extension is strongly discouraged!");
2490#endif
2491 clientFormat = &clipboard->clientFormats[n++];
2492 clientFormat->atom = Logging_XInternAtom(xfc->log, xfc->display, "_FREERDP_RAW", False);
2493 clientFormat->localFormat = clientFormat->formatToRequest = CF_RAW;
2494
2495 clientFormat = &clipboard->clientFormats[n++];
2496 clientFormat->atom = Logging_XInternAtom(xfc->log, xfc->display, "UTF8_STRING", False);
2497 clientFormat->formatToRequest = CF_UNICODETEXT;
2498 clientFormat->localFormat = ClipboardGetFormatId(xfc->clipboard->system, mime_text_plain);
2499
2500 clientFormat = &clipboard->clientFormats[n++];
2501 clientFormat->atom = XA_STRING;
2502 clientFormat->formatToRequest = CF_TEXT;
2503 clientFormat->localFormat = ClipboardGetFormatId(xfc->clipboard->system, mime_text_plain);
2504
2505 clientFormat = &clipboard->clientFormats[n++];
2506 clientFormat->atom = Logging_XInternAtom(xfc->log, xfc->display, mime_tiff, False);
2507 clientFormat->formatToRequest = clientFormat->localFormat = CF_TIFF;
2508
2509 for (size_t x = 0; x < ARRAYSIZE(mime_bitmap); x++)
2510 {
2511 const char* mime_bmp = mime_bitmap[x];
2512 const DWORD format = ClipboardGetFormatId(xfc->clipboard->system, mime_bmp);
2513 if (format == 0)
2514 {
2515 WLog_DBG(TAG, "skipping local bitmap format %s [NOT SUPPORTED]", mime_bmp);
2516 continue;
2517 }
2518
2519 WLog_DBG(TAG, "register local bitmap format %s [0x%08" PRIx32 "]", mime_bmp, format);
2520 clientFormat = &clipboard->clientFormats[n++];
2521 clientFormat->localFormat = format;
2522 clientFormat->atom = Logging_XInternAtom(xfc->log, xfc->display, mime_bmp, False);
2523 clientFormat->formatToRequest = CF_DIB;
2524 clientFormat->isImage = TRUE;
2525 }
2526
2527 for (size_t x = 0; x < ARRAYSIZE(mime_images); x++)
2528 {
2529 const char* mime_bmp = mime_images[x];
2530 const DWORD format = ClipboardGetFormatId(xfc->clipboard->system, mime_bmp);
2531 if (format == 0)
2532 {
2533 WLog_DBG(TAG, "skipping local bitmap format %s [NOT SUPPORTED]", mime_bmp);
2534 continue;
2535 }
2536
2537 WLog_DBG(TAG, "register local bitmap format %s [0x%08" PRIx32 "]", mime_bmp, format);
2538 clientFormat = &clipboard->clientFormats[n++];
2539 clientFormat->localFormat = format;
2540 clientFormat->atom = Logging_XInternAtom(xfc->log, xfc->display, mime_bmp, False);
2541 clientFormat->formatToRequest = CF_DIB;
2542 clientFormat->isImage = TRUE;
2543 }
2544
2545 clientFormat = &clipboard->clientFormats[n++];
2546 clientFormat->atom = Logging_XInternAtom(xfc->log, xfc->display, mime_html, False);
2547 clientFormat->formatToRequest = ClipboardGetFormatId(xfc->clipboard->system, type_HtmlFormat);
2548 clientFormat->localFormat = ClipboardGetFormatId(xfc->clipboard->system, mime_html);
2549 clientFormat->formatName = _strdup(type_HtmlFormat);
2550
2551 if (!clientFormat->formatName)
2552 goto fail;
2553
2554 clientFormat = &clipboard->clientFormats[n++];
2555
2556 /*
2557 * Existence of registered format IDs for file formats does not guarantee that they are
2558 * in fact supported by wClipboard (as further initialization may have failed after format
2559 * registration). However, they are definitely not supported if there are no registered
2560 * formats. In this case we should not list file formats in TARGETS.
2561 */
2562 {
2563 const UINT32 fgid = ClipboardGetFormatId(clipboard->system, type_FileGroupDescriptorW);
2564 {
2565 const UINT32 uid = ClipboardGetFormatId(clipboard->system, mime_uri_list);
2566 if (uid)
2567 {
2568 if (!cliprdr_file_context_set_locally_available(clipboard->file, TRUE))
2569 goto fail;
2570 clientFormat->atom =
2571 Logging_XInternAtom(xfc->log, xfc->display, mime_uri_list, False);
2572 clientFormat->localFormat = uid;
2573 clientFormat->formatToRequest = fgid;
2574 clientFormat->formatName = _strdup(type_FileGroupDescriptorW);
2575
2576 if (!clientFormat->formatName)
2577 goto fail;
2578
2579 clientFormat = &clipboard->clientFormats[n++];
2580 }
2581 }
2582
2583 {
2584 const UINT32 gid = ClipboardGetFormatId(clipboard->system, mime_gnome_copied_files);
2585 if (gid != 0)
2586 {
2587 if (!cliprdr_file_context_set_locally_available(clipboard->file, TRUE))
2588 goto fail;
2589 clientFormat->atom =
2590 Logging_XInternAtom(xfc->log, xfc->display, mime_gnome_copied_files, False);
2591 clientFormat->localFormat = gid;
2592 clientFormat->formatToRequest = fgid;
2593 clientFormat->formatName = _strdup(type_FileGroupDescriptorW);
2594
2595 if (!clientFormat->formatName)
2596 goto fail;
2597
2598 clientFormat = &clipboard->clientFormats[n++];
2599 }
2600 }
2601
2602 {
2603 const UINT32 mid = ClipboardGetFormatId(clipboard->system, mime_mate_copied_files);
2604 if (mid != 0)
2605 {
2606 if (!cliprdr_file_context_set_locally_available(clipboard->file, TRUE))
2607 goto fail;
2608 clientFormat->atom =
2609 Logging_XInternAtom(xfc->log, xfc->display, mime_mate_copied_files, False);
2610 clientFormat->localFormat = mid;
2611 clientFormat->formatToRequest = fgid;
2612 clientFormat->formatName = _strdup(type_FileGroupDescriptorW);
2613
2614 if (!clientFormat->formatName)
2615 goto fail;
2616 }
2617 }
2618 }
2619
2620 clipboard->numClientFormats = WINPR_ASSERTING_INT_CAST(uint32_t, n);
2621 clipboard->targets[0] = Logging_XInternAtom(xfc->log, xfc->display, "TIMESTAMP", FALSE);
2622 clipboard->targets[1] = Logging_XInternAtom(xfc->log, xfc->display, "TARGETS", FALSE);
2623 clipboard->numTargets = 2;
2624 clipboard->incr_atom = Logging_XInternAtom(xfc->log, xfc->display, "INCR", FALSE);
2625
2626 if (relieveFilenameRestriction)
2627 {
2628 WLog_DBG(TAG, "Relieving CLIPRDR filename restriction");
2629 ClipboardGetDelegate(clipboard->system)->IsFileNameComponentValid =
2630 xf_cliprdr_is_valid_unix_filename;
2631 }
2632
2633 clipboard->cachedData = HashTable_New(TRUE);
2634 if (!clipboard->cachedData)
2635 goto fail;
2636
2637 obj = HashTable_ValueObject(clipboard->cachedData);
2638 obj->fnObjectFree = xf_cached_data_free;
2639
2640 clipboard->cachedRawData = HashTable_New(TRUE);
2641 if (!clipboard->cachedRawData)
2642 goto fail;
2643
2644 obj = HashTable_ValueObject(clipboard->cachedRawData);
2645 obj->fnObjectFree = xf_cached_data_free;
2646
2647 return clipboard;
2648
2649fail:
2650 WINPR_PRAGMA_DIAG_PUSH
2651 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
2652 xf_clipboard_free(clipboard);
2653 WINPR_PRAGMA_DIAG_POP
2654 return nullptr;
2655}
2656
2657void xf_clipboard_free(xfClipboard* clipboard)
2658{
2659 if (!clipboard)
2660 return;
2661
2662 xf_clipboard_free_server_formats(clipboard);
2663
2664 if (clipboard->numClientFormats)
2665 {
2666 for (UINT32 i = 0; i < clipboard->numClientFormats; i++)
2667 {
2668 xfCliprdrFormat* format = &clipboard->clientFormats[i];
2669 free(format->formatName);
2670 }
2671 }
2672
2673 cliprdr_file_context_free(clipboard->file);
2674
2675 ClipboardDestroy(clipboard->system);
2676 xf_clipboard_formats_free(clipboard);
2677 HashTable_Free(clipboard->cachedRawData);
2678 HashTable_Free(clipboard->cachedData);
2679 requested_format_free(&clipboard->requestedFormat);
2680 free(clipboard->respond);
2681 free(clipboard->incr_data);
2682 free(clipboard);
2683}
2684
2685void xf_cliprdr_init(xfContext* xfc, CliprdrClientContext* cliprdr)
2686{
2687 WINPR_ASSERT(xfc);
2688 WINPR_ASSERT(cliprdr);
2689
2690 xfc->cliprdr = cliprdr;
2691 xfc->clipboard->context = cliprdr;
2692
2693 cliprdr->MonitorReady = xf_cliprdr_monitor_ready;
2694 cliprdr->ServerCapabilities = xf_cliprdr_server_capabilities;
2695 cliprdr->ServerFormatList = xf_cliprdr_server_format_list;
2696 cliprdr->ServerFormatListResponse = xf_cliprdr_server_format_list_response;
2697 cliprdr->ServerFormatDataRequest = xf_cliprdr_server_format_data_request;
2698 cliprdr->ServerFormatDataResponse = xf_cliprdr_server_format_data_response;
2699
2700 cliprdr_file_context_init(xfc->clipboard->file, cliprdr);
2701}
2702
2703void xf_cliprdr_uninit(xfContext* xfc, CliprdrClientContext* cliprdr)
2704{
2705 WINPR_ASSERT(xfc);
2706 WINPR_ASSERT(cliprdr);
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:58