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