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