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