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