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