FreeRDP
Loading...
Searching...
No Matches
sdl_clip.cpp
1
20
21#include <memory>
22#include <string>
23#include <sstream>
24#include <mutex>
25#include <iterator>
26#include <algorithm>
27#include <utility>
28#include <cstring>
29
30#include <winpr/wlog.h>
31#include <winpr/image.h>
32
33#include <string_view>
34#include "sdl_clip.hpp"
35#include "sdl_context.hpp"
36
37#if defined(__EMSCRIPTEN__)
38#include <mutex>
39class sdlClip;
40/* The clipboard the page talks to. s_web_lock keeps uninit() from tearing it down while an
41 * offer from the page is being sent. */
42static std::mutex s_web_lock;
43static sdlClip* s_web_clip = nullptr;
44#include <emscripten.h>
45#include <emscripten/proxying.h>
46#include <emscripten/threading.h>
47#include <winpr/string.h>
48#include <pthread.h>
49#endif
50
51#define TAG CLIENT_TAG("sdl.cliprdr")
52
53#define mime_text_plain "text/plain"
54const char mime_text_utf8[] = "text/plain;charset=utf-8";
55
56static const char* first_announcement[] = {
57 "sdl-clipboard-monitor-ready-4b8c7d9b-6d5c-4dcc-add1-21573ca564b0"
58};
59
60[[nodiscard]] static const std::vector<const char*>& s_mime_text()
61{
62 static std::vector<const char*> values;
63 if (values.empty())
64 {
65 values = std::vector<const char*>(
66 { mime_text_utf8, "UTF8_STRING", mime_text_plain, "COMPOUND_TEXT", "TEXT", "STRING" });
67 }
68 return values;
69}
70
71static const char s_mime_jxl[] = "image/jxl";
72static const char s_mime_avif[] = "image/avif";
73static const char s_mime_png[] = "image/png";
74static const char s_mime_webp[] = "image/webp";
75static const char s_mime_jpg[] = "image/jpeg";
76
77static const char s_mime_tiff[] = "image/tiff";
78static const char s_mime_uri_list[] = "text/uri-list";
79/* SDL reports the native pasteboard types on macOS, Finder files have no MIME type */
80static const char s_type_file_url[] = "public.file-url";
81static const char s_mime_html[] = "text/html";
82
83#define BMP_MIME_LIST "image/bmp", "image/x-bmp", "image/x-MS-bmp", "image/x-win-bitmap"
84
85[[nodiscard]] static const std::vector<const char*>& s_mime_bitmap()
86{
87 static std::vector<const char*> values;
88 if (values.empty())
89 {
90 values = std::vector<const char*>({ BMP_MIME_LIST });
91 }
92 return values;
93}
94
95[[nodiscard]] static const std::vector<const char*>& s_mime_image()
96{
97 static std::vector<const char*> values;
98 if (values.empty())
99 {
100 if (winpr_image_format_is_supported(WINPR_IMAGE_WEBP))
101 values.push_back(s_mime_webp);
102
103 if (winpr_image_format_is_supported(WINPR_IMAGE_PNG))
104 values.push_back(s_mime_png);
105
106 if (winpr_image_format_is_supported(WINPR_IMAGE_JPEG))
107 values.push_back(s_mime_jpg);
108
109 auto bmp = std::vector<const char*>({ BMP_MIME_LIST });
110 values.insert(values.end(), bmp.begin(), bmp.end());
111 }
112 return values;
113}
114
115/* Local image types winpr can turn into a CF_DIB, in order of preference: BMP only needs its
116 * file header stripped, the others need the matching winpr image support compiled in. */
117[[nodiscard]] static const std::vector<const char*>& s_mime_dib_sources()
118{
119 static const std::vector<const char*> values = []()
120 {
121 std::vector<const char*> v({ BMP_MIME_LIST });
122 if (winpr_image_format_is_supported(WINPR_IMAGE_PNG))
123 v.push_back(s_mime_png);
124 if (winpr_image_format_is_supported(WINPR_IMAGE_WEBP))
125 v.push_back(s_mime_webp);
126 if (winpr_image_format_is_supported(WINPR_IMAGE_JPEG))
127 v.push_back(s_mime_jpg);
128 return v;
129 }();
130 return values;
131}
132
133/* Local image types an "HTML Format" request may be answered from */
134[[nodiscard]] static const std::vector<const char*>& s_mime_html_image_sources()
135{
136 static const std::vector<const char*> values({ s_mime_jpg, s_mime_png, s_mime_webp, s_mime_avif,
137 s_mime_jxl, s_mime_tiff, BMP_MIME_LIST });
138 return values;
139}
140
141/* The mime types SDL has cached for the current clipboard owner. Unlike SDL_HasClipboardData
142 * this does no clipboard I/O: on X11, SDL_HasClipboardData transfers the data, and calls our
143 * own ClipDataCb when we are the owner. */
144[[nodiscard]] static std::vector<std::string> localMimeTypes()
145{
146 std::vector<std::string> list;
147 size_t count = 0;
148 auto mimes = SDL_GetClipboardMimeTypes(&count);
149 for (size_t x = 0; mimes && (x < count); x++)
150 {
151 if (mimes[x])
152 list.emplace_back(mimes[x]);
153 }
154 SDL_free(static_cast<void*>(mimes));
155 return list;
156}
157
158[[nodiscard]] static bool hasMime(const std::vector<std::string>& list, const char* mime)
159{
160 return std::find(list.begin(), list.end(), mime) != list.end();
161}
162
163[[nodiscard]] static const char* firstOffered(const std::vector<std::string>& offered,
164 const std::vector<const char*>& candidates)
165{
166 for (const auto& m : candidates)
167 {
168 if (hasMime(offered, m))
169 return m;
170 }
171 return nullptr;
172}
173
174/* WinPR synthesizes text with its terminating '\0' included in the size, as the RDP
175 * wire formats require. Local applications expect the bare string, so cut it at the
176 * first '\0' (as the X11 client does). */
177[[nodiscard]] static uint32_t textLength(const void* data, uint32_t size)
178{
179 const auto end = memchr(data, '\0', size);
180 if (!end)
181 return size;
182 return WINPR_ASSERTING_INT_CAST(uint32_t,
183 static_cast<const char*>(end) - static_cast<const char*>(data));
184}
185
186static const char s_mime_gnome_copied_files[] = "x-special/gnome-copied-files";
187static const char s_mime_mate_copied_files[] = "x-special/mate-copied-files";
188
189static const char s_mime_freerdp_update[] = "x-special/freerdp-clipboard-update";
190
191static const char* s_type_HtmlFormat = "HTML Format";
192static const char* s_type_FileGroupDescriptorW = "FileGroupDescriptorW";
193
194class ClipboardLockGuard
195{
196 public:
197 explicit ClipboardLockGuard(wClipboard* clipboard) : _clipboard(clipboard)
198 {
199 ClipboardLock(_clipboard);
200 }
201 ClipboardLockGuard(const ClipboardLockGuard& other) = delete;
202 ClipboardLockGuard(ClipboardLockGuard&& other) = delete;
203
204 ClipboardLockGuard& operator=(const ClipboardLockGuard& rhs) = delete;
205 ClipboardLockGuard& operator=(ClipboardLockGuard&& rhs) = delete;
206
207 ~ClipboardLockGuard()
208 {
209 ClipboardUnlock(_clipboard);
210 }
211
212 private:
213 wClipboard* _clipboard;
214};
215
216static bool operator<(const CLIPRDR_FORMAT& lhs, const CLIPRDR_FORMAT& rhs)
217{
218 return (lhs.formatId < rhs.formatId);
219}
220
221static bool operator==(const CLIPRDR_FORMAT& lhs, const CLIPRDR_FORMAT& rhs)
222{
223 return (lhs.formatId == rhs.formatId);
224}
225
226sdlClip::sdlClip(SdlContext* sdl)
227 : _sdl(sdl), _file(cliprdr_file_context_new(this)), _log(WLog_Get(TAG)),
228 _system(ClipboardCreate()), _event(CreateEventA(nullptr, TRUE, FALSE, nullptr)),
229 _uuid(sdl::utils::generate_uuid_v4())
230{
231 WINPR_ASSERT(sdl);
232
233 std::stringstream ss;
234 ss << s_mime_freerdp_update << "-" << _uuid;
235 _mime_uuid = ss.str();
236
237 std::ignore = cliprdr_file_context_set_locally_available(_file, TRUE);
238}
239
240sdlClip::~sdlClip()
241{
242#if defined(__EMSCRIPTEN__)
243 {
244 std::scoped_lock web(s_web_lock);
245 if (s_web_clip == this)
246 s_web_clip = nullptr;
247 }
248#endif
249 cliprdr_file_context_free(_file);
250 ClipboardDestroy(_system);
251 std::ignore = CloseHandle(_event);
252}
253
254bool sdlClip::init(CliprdrClientContext* clip)
255{
256 WINPR_ASSERT(clip);
257 ClipboardLockGuard systemlock(_system);
258 std::scoped_lock lock(_lock);
259 _ctx = clip;
260 clip->custom = this;
261 _ctx->MonitorReady = sdlClip::MonitorReady;
262 _ctx->ServerCapabilities = sdlClip::ReceiveServerCapabilities;
263 _ctx->ServerFormatList = sdlClip::ReceiveServerFormatList;
264 _ctx->ServerFormatListResponse = sdlClip::ReceiveFormatListResponse;
265 _ctx->ServerFormatDataRequest = sdlClip::ReceiveFormatDataRequest;
266 _ctx->ServerFormatDataResponse = sdlClip::ReceiveFormatDataResponse;
267#if defined(__EMSCRIPTEN__)
268 {
269 std::scoped_lock web(s_web_lock);
270 s_web_clip = this;
271 }
272#endif
273
274 return cliprdr_file_context_init(_file, _ctx);
275}
276
277bool sdlClip::uninit(CliprdrClientContext* clip)
278{
279 WINPR_ASSERT(clip);
280#if defined(__EMSCRIPTEN__)
281 {
282 std::scoped_lock web(s_web_lock);
283 if (s_web_clip == this)
284 s_web_clip = nullptr;
285 }
286#endif
287 ClipboardLockGuard systemlock(_system);
288 std::scoped_lock lock(_lock);
289 if (!cliprdr_file_context_uninit(_file, _ctx))
290 return false;
291 _ctx = nullptr;
292 clip->custom = nullptr;
293 _server_requests.clear();
294 return true;
295}
296
297bool sdlClip::contains(const char** mime_types, Sint32 count)
298{
299 for (Sint32 x = 0; x < count; x++)
300 {
301 const auto mime = mime_types[x];
302 if (mime && (strcmp(_mime_uuid.c_str(), mime) == 0))
303 return true;
304 }
305 return false;
306}
307
308bool sdlClip::ownsClipboard() const
309{
310 return hasMime(localMimeTypes(), _mime_uuid.c_str());
311}
312
313bool sdlClip::handleEvent(const SDL_ClipboardEvent& ev)
314{
315 std::unique_ptr<char*, decltype(&SDL_free)> current(nullptr, SDL_free);
316 const char** mime_types = ev.mime_types;
317 size_t nformats = WINPR_ASSERTING_INT_CAST(size_t, ev.num_mime_types);
318 bool connected = false;
319 {
320 std::scoped_lock lock(_lock);
321 connected = (_ctx != nullptr);
322 }
323 if (connected && _ctx && _sync && ev.owner && (ev.num_mime_types == 1))
324 {
325 if (strcmp(first_announcement[0], ev.mime_types[0]) == 0)
326 {
327 current.reset(SDL_GetClipboardMimeTypes(&nformats));
328 mime_types = const_cast<const char**>(current.get());
329 if (!mime_types)
330 nformats = 0;
331 }
332 }
333 else if (!connected || !_ctx || !_sync || ev.owner)
334 {
335 _last_timestamp = ev.timestamp;
336
337 /* The channel thread replaces _current_mimetypes on every server format list, so take
338 * them under the lock instead of trusting pointers carried in the event. */
339 std::vector<std::string> mimes;
340 {
341 ClipboardLockGuard systemlock(_system);
342 std::scoped_lock lock(_lock);
343 mimes.swap(_current_mimetypes);
344 if (!mimes.empty())
345 _cache_data.clear();
346 }
347 if (mimes.empty())
348 return true;
349
350 std::vector<const char*> cmimes;
351 cmimes.reserve(mimes.size());
352 for (const auto& m : mimes)
353 cmimes.push_back(m.c_str());
354
355 /* Server files need native clipboard items on some platforms (macOS), which only the
356 * file context can offer. It requests the data with ClipDataCb like SDL does. */
357 if (cliprdr_file_context_set_local_clipboard_data(_file, sdlClip::ClipDataCb, this,
358 cmimes.data(), cmimes.size()))
359 return true;
360
361 /* SDL copies the mime type strings */
362 return SDL_SetClipboardData(sdlClip::ClipDataCb, sdlClip::ClipCleanCb, this, cmimes.data(),
363 cmimes.size());
364 }
365
366 if (ev.timestamp == _last_timestamp)
367 return true;
368
369 if (contains(mime_types, WINPR_ASSERTING_INT_CAST(Sint32, nformats)))
370 return true;
371
372 /* SDL holds this session's server data right now, so this update was queued before our
373 * later SDL_SetClipboardData took effect: typically the selection a compositor re-sends on
374 * focus-in, racing a server format list. Announcing it would replace the fresh server
375 * clipboard with the stale local one (and the server would then ask us for data we no
376 * longer own). A genuine local change cancels our data before its update is queued. */
377 if (ownsClipboard())
378 {
379 WLog_Print(_log, WLOG_DEBUG, "ignoring stale local clipboard update");
380 return true;
381 }
382
383 {
384 ClipboardLockGuard systemlock(_system);
385 std::scoped_lock lock(_lock);
386 clearServerFormats();
387 }
388
389 const std::string mime_html = s_mime_html;
390
391 const std::vector<std::string> mime_bitmap = { BMP_MIME_LIST };
392 const std::string mime_webp = s_mime_webp;
393 const std::string mime_png = s_mime_png;
394 const std::string mime_jpeg = s_mime_jpg;
395 const std::string mime_tiff = s_mime_tiff;
396 const std::vector<std::string> mime_images = { mime_webp, mime_png, mime_jpeg,
397 mime_tiff, s_mime_avif, s_mime_jxl };
398
399 std::vector<std::string> clientFormatNames;
400 std::vector<CLIPRDR_FORMAT> clientFormats;
401
402 const char** clipboard_mime_formats = mime_types;
403
404 WLog_Print(_log, WLOG_TRACE, "SDL has %" PRIuz " formats", nformats);
405
406 bool textPushed = false;
407 bool imgPushed = false;
408 bool filePushed = false;
409
410 for (size_t i = 0; i < nformats; i++)
411 {
412 std::string local_mime = clipboard_mime_formats[i];
413 WLog_Print(_log, WLOG_TRACE, " - %s", local_mime.c_str());
414
415 if (std::find(s_mime_text().begin(), s_mime_text().end(), local_mime) !=
416 s_mime_text().end())
417 {
418 /* text formats */
419 if (!textPushed)
420 {
421 clientFormats.push_back({ CF_TEXT, nullptr });
422 clientFormats.push_back({ CF_OEMTEXT, nullptr });
423 clientFormats.push_back({ CF_UNICODETEXT, nullptr });
424 textPushed = true;
425 }
426 }
427 else if (local_mime == mime_html)
428 /* html */
429 clientFormatNames.emplace_back(s_type_HtmlFormat);
430 else if ((std::find(mime_bitmap.begin(), mime_bitmap.end(), local_mime) !=
431 mime_bitmap.end()) ||
432 (std::find(mime_images.begin(), mime_images.end(), local_mime) !=
433 mime_images.end()))
434 {
435 /* image formats */
436 if (!imgPushed)
437 {
438 clientFormats.push_back({ CF_DIB, nullptr });
439#if defined(WINPR_UTILS_IMAGE_DIBv5)
440 clientFormats.push_back({ CF_DIBV5, nullptr });
441#endif
442
443 if (winpr_image_format_is_supported(WINPR_IMAGE_BITMAP))
444 {
445 for (auto& bmp : mime_bitmap)
446 clientFormatNames.emplace_back(bmp);
447 }
448
449 if (winpr_image_format_is_supported(WINPR_IMAGE_JPEG))
450 clientFormatNames.emplace_back(mime_jpeg);
451 if (winpr_image_format_is_supported(WINPR_IMAGE_WEBP))
452 clientFormatNames.emplace_back(mime_webp);
453 if (winpr_image_format_is_supported(WINPR_IMAGE_PNG))
454 clientFormatNames.emplace_back(mime_png);
455
456 clientFormatNames.emplace_back(s_type_HtmlFormat);
457 imgPushed = true;
458 }
459 clientFormatNames.emplace_back(local_mime);
460 }
461 else if (mime_is_file(local_mime) || (local_mime == s_type_file_url))
462 {
463 if (!filePushed)
464 {
465 clientFormatNames.emplace_back(s_type_FileGroupDescriptorW);
466 filePushed = true;
467 }
468 }
469 }
470
471 std::sort(clientFormatNames.begin(), clientFormatNames.end());
472 clientFormatNames.erase(std::unique(clientFormatNames.begin(), clientFormatNames.end()),
473 clientFormatNames.end());
474
475 for (auto& name : clientFormatNames)
476 {
477 clientFormats.push_back({ ClipboardRegisterFormat(_system, name.c_str()), name.data() });
478 }
479
480 std::sort(clientFormats.begin(), clientFormats.end(),
481 [](const auto& a, const auto& b) { return a < b; });
482 auto u = std::unique(clientFormats.begin(), clientFormats.end());
483 clientFormats.erase(u, clientFormats.end());
484
485 const CLIPRDR_FORMAT_LIST formatList = {
486 { CB_FORMAT_LIST, 0, 0 },
487 static_cast<UINT32>(clientFormats.size()),
488 clientFormats.data(),
489 };
490
491 WLog_Print(_log, WLOG_TRACE,
492 "-------------- client format list [%" PRIu32 "] ------------------",
493 formatList.numFormats);
494 for (UINT32 x = 0; x < formatList.numFormats; x++)
495 {
496 auto format = &formatList.formats[x];
497 WLog_Print(_log, WLOG_TRACE, "client announces %" PRIu32 " [%s][%s]", format->formatId,
498 ClipboardGetFormatIdString(format->formatId), format->formatName);
499 }
500
501 /* The RDP thread may have closed the channel since the check above (see uninit) */
502 ClipboardLockGuard systemlock(_system);
503 std::scoped_lock lock(_lock);
504 if (!_ctx)
505 return true;
506
507 if (cliprdr_file_context_notify_new_client_format_list(_file) != CHANNEL_RC_OK)
508 return false;
509
510 WINPR_ASSERT(_ctx);
511 WINPR_ASSERT(_ctx->ClientFormatList);
512 return _ctx->ClientFormatList(_ctx, &formatList) == CHANNEL_RC_OK;
513}
514
515UINT sdlClip::MonitorReady(CliprdrClientContext* context, const CLIPRDR_MONITOR_READY* monitorReady)
516{
517 WINPR_UNUSED(monitorReady);
518 WINPR_ASSERT(context);
519 WINPR_ASSERT(monitorReady);
520
521 auto clipboard = static_cast<sdlClip*>(
522 cliprdr_file_context_get_context(static_cast<CliprdrFileContext*>(context->custom)));
523 WINPR_ASSERT(clipboard);
524
525 auto ret = clipboard->SendClientCapabilities();
526 if (ret != CHANNEL_RC_OK)
527 return ret;
528
529 clipboard->_sync = true;
530 SDL_Event ev = {};
531 ev.clipboard.type = SDL_EVENT_CLIPBOARD_UPDATE;
532 ev.clipboard.owner = true;
533 ev.clipboard.timestamp = SDL_GetTicksNS();
534 ev.clipboard.num_mime_types = ARRAYSIZE(first_announcement);
535 ev.clipboard.mime_types = first_announcement;
536
537 auto rc = SDL_PushEvent(&ev);
538 if (rc != 1)
539 return ERROR_INTERNAL_ERROR;
540
541 return CHANNEL_RC_OK;
542}
543
544UINT sdlClip::SendClientCapabilities()
545{
546 CLIPRDR_GENERAL_CAPABILITY_SET generalCapabilitySet = {
547 CB_CAPSTYPE_GENERAL, 12, CB_CAPS_VERSION_2,
548 CB_USE_LONG_FORMAT_NAMES | cliprdr_file_context_current_flags(_file)
549 };
550 const CLIPRDR_CAPABILITIES capabilities = {
551 { CB_TYPE_NONE, 0, 0 }, 1, reinterpret_cast<CLIPRDR_CAPABILITY_SET*>(&generalCapabilitySet)
552 };
553
554 WINPR_ASSERT(_ctx);
555 WINPR_ASSERT(_ctx->ClientCapabilities);
556 return _ctx->ClientCapabilities(_ctx, &capabilities);
557}
558
559void sdlClip::clearServerFormats()
560{
561 {
562 ClipboardLockGuard systemlock(_system);
563 std::scoped_lock lock(_lock);
564 _serverFormats.clear();
565 _cache_data.clear();
566 /* The WinPR clipboard may still hold local data converted for an earlier server request.
567 * ClipDataCb() would hand that out instead of requesting the new server data. */
568 ClipboardEmpty(_system);
569 }
570 cliprdr_file_context_clear(_file);
571}
572
573UINT sdlClip::SendFormatListResponse(BOOL status)
574{
575 const CLIPRDR_FORMAT_LIST_RESPONSE formatListResponse = {
576 { CB_FORMAT_LIST_RESPONSE, static_cast<UINT16>(status ? CB_RESPONSE_OK : CB_RESPONSE_FAIL),
577 0 }
578 };
579 WINPR_ASSERT(_ctx);
580 WINPR_ASSERT(_ctx->ClientFormatListResponse);
581 return _ctx->ClientFormatListResponse(_ctx, &formatListResponse);
582}
583
584UINT sdlClip::SendDataResponse(const BYTE* data, size_t size)
585{
586 CLIPRDR_FORMAT_DATA_RESPONSE response = {};
587
588 if (size > UINT32_MAX)
589 return ERROR_INVALID_PARAMETER;
590
591 response.common.msgFlags = (data) ? CB_RESPONSE_OK : CB_RESPONSE_FAIL;
592 response.common.dataLen = static_cast<UINT32>(size);
593 response.requestedFormatData = data;
594
595 WINPR_ASSERT(_ctx);
596 WINPR_ASSERT(_ctx->ClientFormatDataResponse);
597 return _ctx->ClientFormatDataResponse(_ctx, &response);
598}
599
600UINT sdlClip::SendDataRequest(uint32_t formatID, const std::string& mime)
601{
602 const CLIPRDR_FORMAT_DATA_REQUEST request = { { CB_TYPE_NONE, 0, 0 }, formatID };
603
604 _request_queue.emplace(formatID, mime);
605
606 WINPR_ASSERT(_ctx);
607 WINPR_ASSERT(_ctx->ClientFormatDataRequest);
608 UINT ret = _ctx->ClientFormatDataRequest(_ctx, &request);
609 if (ret != CHANNEL_RC_OK)
610 {
611 WLog_Print(_log, WLOG_ERROR, "error sending ClientFormatDataRequest, cancelling request");
612 _request_queue.pop();
613 }
614
615 return ret;
616}
617
618std::string sdlClip::getServerFormat(uint32_t id)
619{
620 for (auto& fmt : _serverFormats)
621 {
622 if (fmt.formatId() == id)
623 {
624 if (fmt.formatName())
625 return fmt.formatName();
626 break;
627 }
628 }
629
630 return "";
631}
632
633uint32_t sdlClip::serverIdForMime(const std::string& mime)
634{
635 std::string cmp = mime;
636 if (mime_is_html(mime))
637 cmp = s_type_HtmlFormat;
638 if (mime_is_file(mime))
639 cmp = s_type_FileGroupDescriptorW;
640
641 for (auto& format : _serverFormats)
642 {
643 if (!format.formatName())
644 continue;
645 if (cmp == format.formatName())
646 return format.formatId();
647 }
648
649 if (mime_is_image(mime) || mime_is_bmp(mime))
650 {
651 /* not offered under this name: CF_DIB, or any other image format of the server; the
652 * WinPR clipboard converts it */
653 uint32_t named = 0;
654 for (auto& format : _serverFormats)
655 {
656 if ((format.formatId() == CF_DIB) || (format.formatId() == CF_DIBV5))
657 return format.formatId();
658 if (!named && format.formatName() &&
659 (mime_is_image(format.formatName()) || mime_is_bmp(format.formatName())))
660 named = format.formatId();
661 }
662 return named ? named : CF_DIB;
663 }
664 if (mime_is_text(mime))
665 return CF_UNICODETEXT;
666
667 return 0;
668}
669
670UINT sdlClip::ReceiveServerCapabilities(CliprdrClientContext* context,
671 const CLIPRDR_CAPABILITIES* capabilities)
672{
673 WINPR_ASSERT(context);
674 WINPR_ASSERT(capabilities);
675
676 auto capsPtr = reinterpret_cast<const BYTE*>(capabilities->capabilitySets);
677 WINPR_ASSERT(capsPtr);
678
679 auto clipboard = static_cast<sdlClip*>(
680 cliprdr_file_context_get_context(static_cast<CliprdrFileContext*>(context->custom)));
681 WINPR_ASSERT(clipboard);
682
683 if (!cliprdr_file_context_remote_set_flags(clipboard->_file, 0))
684 return ERROR_INTERNAL_ERROR;
685
686 for (UINT32 i = 0; i < capabilities->cCapabilitiesSets; i++)
687 {
688 auto caps = reinterpret_cast<const CLIPRDR_CAPABILITY_SET*>(capsPtr);
689
690 if (caps->capabilitySetType == CB_CAPSTYPE_GENERAL)
691 {
692 auto generalCaps = reinterpret_cast<const CLIPRDR_GENERAL_CAPABILITY_SET*>(caps);
693
694 if (!cliprdr_file_context_remote_set_flags(clipboard->_file, generalCaps->generalFlags))
695 return ERROR_INTERNAL_ERROR;
696 }
697
698 capsPtr += caps->capabilitySetLength;
699 }
700
701 return CHANNEL_RC_OK;
702}
703
704UINT sdlClip::ReceiveServerFormatList(CliprdrClientContext* context,
705 const CLIPRDR_FORMAT_LIST* formatList)
706{
707 BOOL html = FALSE;
708 BOOL text = FALSE;
709 BOOL image = FALSE;
710 BOOL file = FALSE;
711
712 if (!context || !context->custom)
713 return ERROR_INVALID_PARAMETER;
714
715 auto filecontext = static_cast<CliprdrFileContext*>(context->custom);
716 auto clipboard = static_cast<sdlClip*>(cliprdr_file_context_get_context(filecontext));
717 WINPR_ASSERT(clipboard);
718
719 ClipboardLockGuard systemlock(clipboard->_system);
720 std::scoped_lock lock(clipboard->_lock);
721
722 clipboard->clearServerFormats();
723 /* _system may still hold local clipboard data converted for an earlier server request.
724 * ClipDataCb converts from _system before asking the server, so left in place it would
725 * answer the next local paste with that stale local data instead of the new server data. */
726 ClipboardEmpty(clipboard->_system);
727
728 for (UINT32 i = 0; i < formatList->numFormats; i++)
729 {
730 const CLIPRDR_FORMAT* format = &formatList->formats[i];
731
732 clipboard->_serverFormats.emplace_back(format->formatId, format->formatName);
733
734 if (format->formatName)
735 {
736 if (strcmp(format->formatName, s_type_HtmlFormat) == 0)
737 {
738 text = TRUE;
739 html = TRUE;
740 }
741 else if (strcmp(format->formatName, s_type_FileGroupDescriptorW) == 0)
742 {
743 file = TRUE;
744 text = TRUE;
745 }
746 else if (mime_is_image(format->formatName) || mime_is_bmp(format->formatName))
747 {
748 /* e.g. GNOME Remote Desktop offers image/png alone for PNG-only sources */
749 image = TRUE;
750 }
751 }
752 else
753 {
754 switch (format->formatId)
755 {
756 case CF_TEXT:
757 case CF_OEMTEXT:
758 case CF_UNICODETEXT:
759 text = TRUE;
760 break;
761
762 case CF_DIB:
763 image = TRUE;
764 break;
765
766 default:
767 break;
768 }
769 }
770 }
771
772 clipboard->_current_mimetypes.clear();
773
774 {
775 auto res = cliprdr_file_context_notify_new_server_format_list(filecontext);
776 if (res != CHANNEL_RC_OK)
777 return res;
778 }
779
780 if (text)
781 {
782 clipboard->_current_mimetypes.insert(clipboard->_current_mimetypes.end(),
783 s_mime_text().begin(), s_mime_text().end());
784 }
785 if (image)
786 {
787 clipboard->_current_mimetypes.insert(clipboard->_current_mimetypes.end(),
788 s_mime_bitmap().begin(), s_mime_bitmap().end());
789 clipboard->_current_mimetypes.insert(clipboard->_current_mimetypes.end(),
790 s_mime_image().begin(), s_mime_image().end());
791 }
792 if (html)
793 {
794 clipboard->_current_mimetypes.emplace_back(s_mime_html);
795 }
796 if (file)
797 {
798 clipboard->_current_mimetypes.emplace_back(s_mime_uri_list);
799 clipboard->_current_mimetypes.emplace_back(s_mime_gnome_copied_files);
800 clipboard->_current_mimetypes.emplace_back(s_mime_mate_copied_files);
801 }
802 clipboard->_current_mimetypes.emplace_back(clipboard->_mime_uuid.c_str());
803
804 auto& mime = clipboard->_current_mimetypes;
805 std::sort(mime.begin(), mime.end());
806 mime.erase(std::unique(mime.begin(), mime.end()), mime.end());
807
808 WLog_Print(clipboard->_log, WLOG_TRACE,
809 "-------------- server mime types [%" PRIuz "] ------------------", mime.size());
810 for (const auto& m : mime)
811 {
812 WLog_Print(clipboard->_log, WLOG_TRACE, "server announces %s]", m.c_str());
813 }
814
815 /* handleEvent picks the mime types up from _current_mimetypes. Set every field: an
816 * initializer list only initializes the union's first member (type). */
817 SDL_Event ev = {};
818 ev.clipboard.type = SDL_EVENT_CLIPBOARD_UPDATE;
819 ev.clipboard.owner = true;
820 ev.clipboard.timestamp = SDL_GetTicksNS();
821 ev.clipboard.num_mime_types = 0;
822 ev.clipboard.mime_types = nullptr;
823
824 auto rc = (SDL_PushEvent(&ev) == 1);
825 const UINT ret = clipboard->SendFormatListResponse(rc);
826#if defined(__EMSCRIPTEN__)
827 clipboard->_web_offers.serverAnnounced();
828 if (ret == CHANNEL_RC_OK)
829 std::ignore = clipboard->webRequestServerText();
830#endif
831 return ret;
832}
833
834UINT sdlClip::ReceiveFormatListResponse(WINPR_ATTR_UNUSED CliprdrClientContext* context,
835 const CLIPRDR_FORMAT_LIST_RESPONSE* formatListResponse)
836{
837 WINPR_ASSERT(context);
838 WINPR_ASSERT(formatListResponse);
839
840 if (formatListResponse->common.msgFlags & CB_RESPONSE_FAIL)
841 WLog_WARN(TAG, "format list update failed");
842 return CHANNEL_RC_OK;
843}
844
845std::shared_ptr<BYTE> sdlClip::getLocalData(uint32_t formatId, uint32_t& len)
846{
847 len = 0;
848
849 const auto offered = localMimeTypes();
850
851 /* The local clipboard still holds what this session received from the server (the
852 * server asked before our format list reached it). Reading it through SDL would call
853 * ClipDataCb and ask the server for its own data, so fail the request instead. */
854 if (hasMime(offered, _mime_uuid.c_str()))
855 {
856 WLog_Print(_log, WLOG_DEBUG, "local clipboard holds server data, not answering request");
857 return {};
858 }
859
860 UINT32 fileFormatId = 0;
861 UINT32 htmlFormatId = 0;
862 {
863 ClipboardLockGuard systemlock(_system);
864 std::scoped_lock lock(_lock);
865 WLog_Print(_log, WLOG_DEBUG, "Requesting format %s [0x%08" PRIx32 "] [%s]",
866 ClipboardGetFormatIdString(formatId), formatId,
867 ClipboardGetFormatName(_system, formatId));
868 fileFormatId = ClipboardGetFormatId(_system, s_type_FileGroupDescriptorW);
869 htmlFormatId = ClipboardGetFormatId(_system, s_type_HtmlFormat);
870 }
871
872 /* Local mime types to try, in order, and the _system format each is stored as for winpr to
873 * convert: its registered format, the requested one, or a newly registered one. */
874 enum class Store
875 {
876 Lookup,
877 Requested,
878 Register
879 };
880 struct Candidate
881 {
882 const char* mime;
883 Store store;
884 };
885 std::vector<Candidate> candidates;
886
887 switch (formatId)
888 {
889 case CF_TEXT:
890 case CF_OEMTEXT:
891 case CF_UNICODETEXT:
892 {
893 const char* mime = firstOffered(offered, s_mime_text());
894 if (mime)
895 candidates.push_back({ mime, Store::Lookup });
896 }
897 break;
898
899 case CF_DIB:
900 case CF_DIBV5:
901 /* Every convertible type on offer (screenshot tools offer image/png only, never
902 * image/bmp), in case a decoder rejects one of them. */
903 for (const auto& mime : s_mime_dib_sources())
904 {
905 if (hasMime(offered, mime))
906 candidates.push_back({ mime, Store::Register });
907 }
908 break;
909
910 case CF_TIFF:
911 candidates.push_back({ s_mime_tiff, Store::Requested });
912 break;
913
914 default:
915 if (formatId == fileFormatId)
916 candidates.push_back({ s_mime_uri_list, Store::Lookup });
917 else if (formatId == htmlFormatId)
918 {
919 /* In case HTML format was requested but we only have images in local clipboard */
920 const char* mime = hasMime(offered, s_mime_html)
921 ? s_mime_html
922 : firstOffered(offered, s_mime_html_image_sources());
923 if (mime)
924 candidates.push_back({ mime, Store::Lookup });
925 }
926 else
927 {
928 ClipboardLockGuard systemlock(_system);
929 std::scoped_lock lock(_lock);
930 const char* formatName = ClipboardGetFormatName(_system, formatId);
931 if (formatName && hasMime(offered, formatName))
932 {
933 /* the name is owned by _system and outlives this function */
934 candidates.push_back({ formatName, Store::Lookup });
935 }
936 else if (formatName && (mime_is_image(formatName) || mime_is_bmp(formatName)))
937 {
938 /* We announce every image format WinPR can convert to, but the SDL backend
939 * may only provide some of them (Windows: image/bmp). Take the ones on
940 * offer and let the WinPR clipboard convert to the requested format. */
941 for (const auto& mime : s_mime_dib_sources())
942 {
943 if (hasMime(offered, mime))
944 candidates.push_back({ mime, Store::Register });
945 }
946 }
947 }
948 break;
949 }
950
951 std::shared_ptr<BYTE> data;
952 for (const auto& candidate : candidates)
953 {
954 /* Read without holding our locks: on X11 this pumps events and can take a while, and
955 * the channel thread needs the locks to deliver server data. */
956 size_t size = 0;
957 std::shared_ptr<void> sdldata;
958 if (formatId == fileFormatId)
959 sdldata =
960 std::shared_ptr<void>(cliprdr_file_context_get_local_uri_list(_file, &size), free);
961 if (!sdldata)
962 sdldata = std::shared_ptr<void>(SDL_GetClipboardData(candidate.mime, &size), SDL_free);
963 if (!sdldata || (size > UINT32_MAX))
964 continue;
965
966 ClipboardLockGuard systemlock(_system);
967 std::scoped_lock lock(_lock);
968
969 if (formatId == fileFormatId)
970 {
971 auto bdata = static_cast<const char*>(sdldata.get());
972 if (!cliprdr_file_context_update_client_data(_file, bdata, size))
973 continue;
974 }
975
976 UINT32 storeId = formatId;
977 if (candidate.store == Store::Lookup)
978 storeId = ClipboardGetFormatId(_system, candidate.mime);
979 else if (candidate.store == Store::Register)
980 storeId = ClipboardRegisterFormat(_system, candidate.mime);
981
982 uint32_t ptrlen = 0;
983 BYTE* ptr = nullptr;
984 if (ClipboardSetData(_system, storeId, sdldata.get(), static_cast<uint32_t>(size)))
985 ptr = static_cast<BYTE*>(ClipboardGetData(_system, formatId, &ptrlen));
986
987 /* _system is also where ClipDataCb looks for server data: leave no local data behind,
988 * or a later local paste would be answered with it. */
989 ClipboardEmpty(_system);
990
991 if (!ptr)
992 {
993 WLog_Print(_log, WLOG_DEBUG, "could not convert local %s", candidate.mime);
994 continue;
995 }
996
997 data = std::shared_ptr<BYTE>(ptr, free);
998 len = ptrlen;
999
1000 if (formatId == fileFormatId)
1001 {
1002 BYTE* ddata = nullptr;
1003 UINT32 dsize = 0;
1004 const UINT32 flags = cliprdr_file_context_remote_get_flags(_file);
1005 const UINT32 error = cliprdr_serialize_file_list_ex(
1006 flags, reinterpret_cast<const FILEDESCRIPTORW*>(data.get()),
1007 ptrlen / sizeof(FILEDESCRIPTORW), &ddata, &dsize);
1008 data = std::shared_ptr<BYTE>(ddata, free);
1009 len = dsize;
1010 if (error)
1011 {
1012 data.reset();
1013 len = 0;
1014 }
1015 }
1016 break;
1017 }
1018 return data;
1019}
1020
1021UINT sdlClip::ReceiveFormatDataRequest(CliprdrClientContext* context,
1022 const CLIPRDR_FORMAT_DATA_REQUEST* formatDataRequest)
1023{
1024 WINPR_ASSERT(context);
1025 WINPR_ASSERT(formatDataRequest);
1026
1027 auto clipboard = static_cast<sdlClip*>(
1028 cliprdr_file_context_get_context(static_cast<CliprdrFileContext*>(context->custom)));
1029 WINPR_ASSERT(clipboard);
1030
1031#if defined(__EMSCRIPTEN__)
1032 /* In the browser, never fall through to the SDL path below: SDL has no browser clipboard,
1033 * its internal one ends in ClipDataCb(), which requests the data from the server and waits
1034 * for the reply on this very channel thread, so nothing can arrive (the server times out and
1035 * all clipboard traffic stalls). Answer from the browser data, or fail at once. */
1036 if (!clipboard->webAnswerDataRequest(formatDataRequest))
1037 return clipboard->SendDataResponse(nullptr, 0);
1038 return CHANNEL_RC_OK;
1039#endif
1040 {
1041 std::scoped_lock lock(clipboard->_lock);
1042 const UINT32 mask = freerdp_settings_get_uint32(context->rdpcontext->settings,
1043 FreeRDP_ClipboardFeatureMask);
1044
1045 const UINT32 fileId =
1046 ClipboardRegisterFormat(clipboard->_system, s_type_FileGroupDescriptorW);
1047 if (fileId == formatDataRequest->requestedFormatId)
1048 {
1049 if ((mask & CLIPRDR_FLAG_LOCAL_TO_REMOTE_FILES) == 0)
1050 return clipboard->SendDataResponse(nullptr, 0);
1051 }
1052 else
1053 {
1054 if ((mask & CLIPRDR_FLAG_LOCAL_TO_REMOTE) == 0)
1055 return clipboard->SendDataResponse(nullptr, 0);
1056 }
1057
1058 /* ClipDataCb is waiting on the main thread for the server: a local application is
1059 * reading our offer of server data, so the local clipboard holds no client data to
1060 * give. Answer now. Queued for the main thread, the request would wait for the end of
1061 * that read while the server may hold its reply until we answer (seen with Windows:
1062 * a 9 s freeze until ClipDataCb timed out). */
1063 if (clipboard->_reading_server)
1064 {
1065 WLog_Print(clipboard->_log, WLOG_DEBUG,
1066 "local read of server data in progress, failing server request");
1067 return clipboard->SendDataResponse(nullptr, 0);
1068 }
1069 clipboard->_server_requests.push_back(formatDataRequest->requestedFormatId);
1070 }
1071
1072 /* This runs on the channel thread, but the SDL clipboard API is main-thread only: on
1073 * Wayland, reading the selection offer here races the main thread replacing it. */
1074 if (!SDL_RunOnMainThread([](void* userdata)
1075 { static_cast<sdlClip*>(userdata)->handleDataRequests(); }, clipboard,
1076 false))
1077 {
1078 std::scoped_lock lock(clipboard->_lock);
1079 if (!clipboard->_server_requests.empty())
1080 clipboard->_server_requests.pop_back();
1081 return clipboard->SendDataResponse(nullptr, 0);
1082 }
1083 return CHANNEL_RC_OK;
1084}
1085
1086void sdlClip::handleDataRequests()
1087{
1088 for (;;)
1089 {
1090 uint32_t formatId = 0;
1091 {
1092 std::scoped_lock lock(_lock);
1093 if (_server_requests.empty())
1094 return;
1095 formatId = _server_requests.front();
1096 _server_requests.pop_front();
1097 if (!_ctx)
1098 continue;
1099 }
1100
1101 uint32_t len = 0;
1102 auto data = getLocalData(formatId, len);
1103 std::scoped_lock lock(_lock);
1104 if (!_ctx)
1105 continue;
1106 if (SendDataResponse(data.get(), len) != CHANNEL_RC_OK)
1107 WLog_Print(_log, WLOG_WARN, "failed to send clipboard data response");
1108 }
1109}
1110
1111UINT sdlClip::ReceiveFormatDataResponse(CliprdrClientContext* context,
1112 const CLIPRDR_FORMAT_DATA_RESPONSE* formatDataResponse)
1113{
1114 WINPR_ASSERT(context);
1115 WINPR_ASSERT(formatDataResponse);
1116
1117 const UINT32 size = formatDataResponse->common.dataLen;
1118 const BYTE* data = formatDataResponse->requestedFormatData;
1119
1120 auto clipboard = static_cast<sdlClip*>(
1121 cliprdr_file_context_get_context(static_cast<CliprdrFileContext*>(context->custom)));
1122 WINPR_ASSERT(clipboard);
1123
1124#if defined(__EMSCRIPTEN__)
1125 if (clipboard->webTakeDataResponse(formatDataResponse))
1126 return CHANNEL_RC_OK;
1127#endif
1128 ClipboardLockGuard systemlock(clipboard->_system);
1129 std::scoped_lock lock(clipboard->_lock);
1130 if (clipboard->_request_queue.empty())
1131 {
1132 /* The matching request already timed out (see the wait below) and was
1133 * popped, so this is a late reply on a slow/high-latency link. Dropping it
1134 * is harmless; returning an error here would kill the cliprdr channel thread
1135 * and tear down the whole session. Warn and continue instead. */
1136 WLog_Print(clipboard->_log, WLOG_WARN,
1137 "format data response with no pending request (late reply?), ignoring");
1138 return CHANNEL_RC_OK;
1139 }
1140
1141 do
1142 {
1143 UINT32 srcFormatId = 0;
1144 auto& request = clipboard->_request_queue.front();
1145 bool success = (formatDataResponse->common.msgFlags & CB_RESPONSE_OK) &&
1146 !(formatDataResponse->common.msgFlags & CB_RESPONSE_FAIL);
1147 request.setSuccess(success);
1148
1149 if (!success)
1150 {
1151 WLog_Print(clipboard->_log, WLOG_WARN,
1152 "clipboard data request for format %" PRIu32 " [%s], mime %s failed",
1153 request.format(), request.formatstr().c_str(), request.mime().c_str());
1154 break;
1155 }
1156
1157 switch (request.format())
1158 {
1159 case CF_TEXT:
1160 case CF_OEMTEXT:
1161 case CF_UNICODETEXT:
1162 srcFormatId = request.format();
1163 break;
1164
1165 case CF_DIB:
1166 case CF_DIBV5:
1167 srcFormatId = request.format();
1168 break;
1169
1170 default:
1171 {
1172 auto name = clipboard->getServerFormat(request.format());
1173 if (!name.empty())
1174 {
1175 if (name == s_type_FileGroupDescriptorW)
1176 {
1177 srcFormatId =
1178 ClipboardGetFormatId(clipboard->_system, s_type_FileGroupDescriptorW);
1179
1180 if (cliprdr_file_context_has_local_support(clipboard->_file))
1181 {
1182 if (!cliprdr_file_context_update_server_data(
1183 clipboard->_file, clipboard->_system, data, size))
1184 {
1185 WLog_Print(clipboard->_log, WLOG_WARN,
1186 "File clipboard failed to update");
1187 }
1188 }
1189 }
1190 else if (name == s_type_HtmlFormat)
1191 {
1192 srcFormatId = ClipboardGetFormatId(clipboard->_system, s_type_HtmlFormat);
1193 }
1194 else if (mime_is_image(name) || mime_is_bmp(name))
1195 {
1196 srcFormatId = ClipboardGetFormatId(clipboard->_system, name.c_str());
1197 }
1198 }
1199 }
1200 break;
1201 }
1202
1203 if (!ClipboardSetData(clipboard->_system, srcFormatId, data, size))
1204 {
1205 /* Fail this request only: an error return here would end the clipboard channel
1206 * and, without auto-reconnect, the session. */
1207 WLog_Print(clipboard->_log, WLOG_ERROR, "error when setting clipboard data");
1208 request.setSuccess(false);
1209 break;
1210 }
1211 WLog_Print(clipboard->_log, WLOG_DEBUG, "updated clipboard data %s [0x%08" PRIx32 "]",
1212 ClipboardGetFormatName(clipboard->_system, srcFormatId), srcFormatId);
1213 } while (false);
1214
1215 if (!SetEvent(clipboard->_event))
1216 {
1217 WLog_Print(clipboard->_log, WLOG_ERROR, "error when setting clipboard event");
1218 return ERROR_INTERNAL_ERROR;
1219 }
1220
1221 return CHANNEL_RC_OK;
1222}
1223
1224const void* sdlClip::ClipDataCb(void* userdata, const char* mime_type, size_t* size)
1225{
1226 auto clip = static_cast<sdlClip*>(userdata);
1227 WINPR_ASSERT(clip);
1228 WINPR_ASSERT(size);
1229 WINPR_ASSERT(mime_type);
1230
1231 *size = 0;
1232 uint32_t len = 0;
1233
1234 if (mime_is_text(mime_type))
1235 {
1236 if (mime_is_utf8(mime_type))
1237 mime_type = mime_text_utf8;
1238 else
1239 mime_type = mime_text_plain;
1240 }
1241
1242 {
1243 ClipboardLockGuard systemlock(clip->_system);
1244 std::scoped_lock lock(clip->_lock);
1245
1246 if (!clip->_ctx)
1247 return nullptr;
1248
1249 /* check if we already used this mime type */
1250 auto cache = clip->_cache_data.find(mime_type);
1251 if (cache != clip->_cache_data.end())
1252 {
1253 *size = cache->second.size;
1254 return cache->second.ptr.get();
1255 }
1256
1257 auto formatID = clip->serverIdForMime(mime_type);
1258
1259 /* Can we convert the data from existing formats in the clibpard? */
1260 uint32_t fsize = 0;
1261 auto mimeFormatID = ClipboardRegisterFormat(clip->_system, mime_type);
1262 auto fptr = ClipboardGetData(clip->_system, mimeFormatID, &fsize);
1263 if (fptr)
1264 {
1265 auto ptr = std::shared_ptr<void>(fptr, free);
1266 if (mime_is_text(mime_type))
1267 fsize = textLength(fptr, fsize);
1268 clip->_cache_data.emplace(mime_type, cache_entry{ fsize, ptr });
1269
1270 auto fcache = clip->_cache_data.find(mime_type);
1271 if (fcache != clip->_cache_data.end())
1272 {
1273 *size = fcache->second.size;
1274 return fcache->second.ptr.get();
1275 }
1276 }
1277
1278 WLog_Print(clip->_log, WLOG_DEBUG, "requesting format %s [%s 0x%08" PRIx32 "]", mime_type,
1279 ClipboardGetFormatName(clip->_system, formatID), formatID);
1280
1281 /* From here until the reply, server data requests are answered on arrival (see
1282 * ReceiveFormatDataRequest). Those already queued get the same answer now: a local
1283 * application is reading our offer, so there is no client data to give. */
1284 clip->_reading_server = true;
1285 while (!clip->_server_requests.empty())
1286 {
1287 clip->_server_requests.pop_front();
1288 WLog_Print(clip->_log, WLOG_DEBUG,
1289 "local read of server data in progress, failing queued server request");
1290 std::ignore = clip->SendDataResponse(nullptr, 0);
1291 }
1292
1293 if (clip->SendDataRequest(formatID, mime_type))
1294 {
1295 clip->_reading_server = false;
1296 return nullptr;
1297 }
1298 }
1299 {
1300 HANDLE hdl[2] = { freerdp_abort_event(clip->_sdl->context()), clip->_event };
1301
1302 const UINT32 timeout =
1303 freerdp_settings_get_uint32(clip->_sdl->context()->settings, FreeRDP_TcpAckTimeout);
1304 DWORD status = WaitForMultipleObjects(ARRAYSIZE(hdl), hdl, FALSE, timeout);
1305
1306 if (status != WAIT_OBJECT_0 + 1)
1307 {
1308 std::scoped_lock lock(clip->_lock);
1309 clip->_reading_server = false;
1310 if (!clip->_request_queue.empty())
1311 clip->_request_queue.pop();
1312
1313 if (status == WAIT_TIMEOUT)
1314 WLog_Print(clip->_log, WLOG_ERROR,
1315 "no reply in 10 seconds, returning empty content");
1316
1317 return nullptr;
1318 }
1319 }
1320
1321 {
1322 ClipboardLockGuard systemlock(clip->_system);
1323 std::scoped_lock lock(clip->_lock);
1324 clip->_reading_server = false;
1325 if (clip->_request_queue.empty())
1326 return nullptr;
1327 /* take the request out of the queue: pop() destroys the queued one */
1328 auto request = std::move(clip->_request_queue.front());
1329 clip->_request_queue.pop();
1330
1331 if (clip->_request_queue.empty())
1332 std::ignore = ResetEvent(clip->_event);
1333
1334 if (request.success())
1335 {
1336 auto formatID = ClipboardRegisterFormat(clip->_system, mime_type);
1337 auto data = ClipboardGetData(clip->_system, formatID, &len);
1338 if (!data)
1339 {
1340 WLog_Print(clip->_log, WLOG_ERROR, "error retrieving clipboard data");
1341 return nullptr;
1342 }
1343
1344 auto ptr = std::shared_ptr<void>(data, free);
1345 if (mime_is_text(mime_type))
1346 len = textLength(data, len);
1347 clip->_cache_data.emplace(mime_type, cache_entry{ len, ptr });
1348 *size = len;
1349 return ptr.get();
1350 }
1351
1352 return nullptr;
1353 }
1354}
1355
1356void sdlClip::ClipCleanCb(void* userdata)
1357{
1358 auto clip = static_cast<sdlClip*>(userdata);
1359 WINPR_ASSERT(clip);
1360 ClipboardLockGuard give_me_a_name(clip->_system);
1361 std::scoped_lock lock(clip->_lock);
1362 ClipboardEmpty(clip->_system);
1363}
1364
1365bool sdlClip::mime_is_file(const std::string& mime)
1366{
1367 if (strncmp(s_mime_uri_list, mime.c_str(), sizeof(s_mime_uri_list)) == 0)
1368 return true;
1369 if (strncmp(s_mime_gnome_copied_files, mime.c_str(), sizeof(s_mime_gnome_copied_files)) == 0)
1370 return true;
1371 if (strncmp(s_mime_mate_copied_files, mime.c_str(), sizeof(s_mime_mate_copied_files)) == 0)
1372 return true;
1373 return false;
1374}
1375
1376bool sdlClip::mime_is_text(const std::string& mime)
1377{
1378 for (const auto& tmime : s_mime_text())
1379 {
1380 assert(tmime != nullptr);
1381 if (mime == tmime)
1382 return true;
1383 }
1384
1385 return false;
1386}
1387
1388bool sdlClip::mime_is_utf8(const std::string& mime)
1389{
1390 if (mime == std::string(mime_text_utf8))
1391 return true;
1392
1393 return mime == std::string("UTF8_STRING");
1394}
1395
1396bool sdlClip::mime_is_image(const std::string& mime)
1397{
1398 for (const auto& imime : s_mime_image())
1399 {
1400 assert(imime != nullptr);
1401 if (mime == imime)
1402 return true;
1403 }
1404
1405 return false;
1406}
1407
1408bool sdlClip::mime_is_bmp(const std::string& mime)
1409{
1410 for (const auto& imime : s_mime_bitmap())
1411 {
1412 assert(imime != nullptr);
1413 if (mime == imime)
1414 return true;
1415 }
1416
1417 return false;
1418}
1419
1420bool sdlClip::mime_is_html(const std::string& mime)
1421{
1422 return mime.compare(s_mime_html) == 0;
1423}
1424
1425ClipRequest::ClipRequest(UINT32 format, const std::string& mime)
1426 : _format(format), _mime(mime), _success(false)
1427{
1428}
1429
1430uint32_t ClipRequest::format() const
1431{
1432 return _format;
1433}
1434
1435std::string ClipRequest::formatstr() const
1436{
1437 return ClipboardGetFormatIdString(_format);
1438}
1439
1440std::string ClipRequest::mime() const
1441{
1442 return _mime;
1443}
1444
1445bool ClipRequest::success() const
1446{
1447 return _success;
1448}
1449
1450void ClipRequest::setSuccess(bool status)
1451{
1452 _success = status;
1453}
1454
1455CliprdrFormat::CliprdrFormat(uint32_t formatID, const char* formatName) : _formatID(formatID)
1456{
1457 if (formatName)
1458 _formatName = formatName;
1459}
1460
1461uint32_t CliprdrFormat::formatId() const
1462{
1463 return _formatID;
1464}
1465
1466const char* CliprdrFormat::formatName() const
1467{
1468 if (_formatName.empty())
1469 return nullptr;
1470 return _formatName.c_str();
1471}
1472
1473#if defined(__EMSCRIPTEN__)
1474/* Browser clipboard (text and images). The page calls freerdp_web_clipboard_offer() with what
1475 * the browser clipboard holds (UTF-8 text and/or an image converted to CF_DIB); we announce the
1476 * formats and answer the server's requests from that data. When the server announces text or a
1477 * CF_DIB image, we fetch it at once and hand it to the page (Module.freerdpClipboardWrite /
1478 * freerdpClipboardWriteImage), which writes it with the async Clipboard API (images as PNG). */
1479static const char s_web_mime_text[] = "x-freerdp-web-text";
1480static const char s_web_mime_dib[] = "x-freerdp-web-dib";
1481static const char s_web_mime_png[] = "x-freerdp-web-png";
1482
1483/* in sdl_clip_emscripten.js */
1484extern "C" void freerdp_web_clipboard_write_js(const char* utf8);
1485extern "C" void freerdp_web_clipboard_write_image_js(const BYTE* data, size_t size, int png);
1486
1487typedef struct
1488{
1489 char* text;
1490 BYTE* dib;
1491 size_t size;
1492 bool png;
1493} web_write_job;
1494
1495static void web_write_on_main(void* arg)
1496{
1497 auto job = static_cast<web_write_job*>(arg);
1498 if (job->text)
1499 freerdp_web_clipboard_write_js(job->text);
1500 if (job->dib)
1501 freerdp_web_clipboard_write_image_js(job->dib, job->size, job->png ? 1 : 0);
1502 free(job->text);
1503 free(job->dib);
1504 free(job);
1505}
1506
1507static void web_write(char* text, BYTE* dib, size_t size, bool png = false)
1508{
1509 auto job = static_cast<web_write_job*>(calloc(1, sizeof(web_write_job)));
1510 if (!job)
1511 {
1512 free(text);
1513 free(dib);
1514 return;
1515 }
1516 job->text = text;
1517 job->dib = dib;
1518 job->size = size;
1519 job->png = png;
1520 if (emscripten_is_main_browser_thread())
1521 web_write_on_main(job);
1522 else if (!emscripten_proxy_async(emscripten_proxy_get_system_queue(),
1523 emscripten_main_runtime_thread_id(), web_write_on_main, job))
1524 {
1525 free(text);
1526 free(dib);
1527 free(job);
1528 }
1529}
1530
1531bool sdlClip::webOffer(const char* utf8, const BYTE* dib, size_t dibSize)
1532{
1533 std::scoped_lock web(s_web_lock);
1534 auto clip = s_web_clip;
1535 if (!clip || !clip->_ctx || !clip->_ctx->ClientFormatList)
1536 return false;
1537 std::vector<CLIPRDR_FORMAT> formats;
1538 {
1539 std::scoped_lock lock(clip->_lock);
1540 const std::string text = utf8 ? utf8 : "";
1541 if (!clip->_web_offers.offer(text, dib, dibSize))
1542 return true;
1543 clip->_web_local = text;
1544 clip->_web_local_dib.assign(dib, dib ? dib + dibSize : dib);
1545 /* remember what we offered: the server may announce it back much later */
1546 if (!clip->_web_local.empty())
1547 clip->_web_recent.push_back(std::hash<std::string>{}(clip->_web_local));
1548 if (dib && dibSize)
1549 clip->_web_recent.push_back(std::hash<std::string_view>{}(
1550 std::string_view(reinterpret_cast<const char*>(dib), dibSize)));
1551 while (clip->_web_recent.size() > 16)
1552 clip->_web_recent.pop_front();
1553 if (!clip->_web_local.empty())
1554 {
1555 formats.push_back({ CF_UNICODETEXT, nullptr });
1556 formats.push_back({ CF_TEXT, nullptr });
1557 }
1558 if (!clip->_web_local_dib.empty())
1559 formats.push_back({ CF_DIB, nullptr });
1560 }
1561 if (formats.empty())
1562 return false;
1563 CLIPRDR_FORMAT_LIST list = {};
1564 list.common.msgType = CB_FORMAT_LIST;
1565 list.numFormats = static_cast<UINT32>(formats.size());
1566 list.formats = formats.data();
1567 return clip->_ctx->ClientFormatList(clip->_ctx, &list) == CHANNEL_RC_OK;
1568}
1569
1570bool sdlClip::webRequestServerText()
1571{
1572 bool has_text = false;
1573 bool has_dib = false;
1574 UINT32 png_id = 0;
1575 std::scoped_lock lock(_lock);
1576 for (const auto& f : _serverFormats)
1577 {
1578 has_text |= (f.formatId() == CF_UNICODETEXT);
1579 has_dib |= (f.formatId() == CF_DIB);
1580 /* GNOME Remote Desktop announces a PNG-only clipboard as "image/png" alone */
1581 if (f.formatName() && (strcmp(f.formatName(), s_mime_png) == 0))
1582 png_id = f.formatId();
1583 }
1584 if (!has_text && !has_dib && !png_id)
1585 return false;
1586 /* Keep the browser data for now: servers often announce our own offer back (an echo, e.g.
1587 * GNOME's clipboard handling). Clearing it here left nothing to answer the following data
1588 * request with. webTakeDataResponse() compares and only then hands over ownership. */
1589 bool ok = true;
1590 if (has_text)
1591 ok &= (SendDataRequest(CF_UNICODETEXT, s_web_mime_text) == CHANNEL_RC_OK);
1592 if (has_dib)
1593 ok &= (SendDataRequest(CF_DIB, s_web_mime_dib) == CHANNEL_RC_OK);
1594 else if (png_id) /* browsers write PNG anyway: take it as it is */
1595 ok &= (SendDataRequest(png_id, s_web_mime_png) == CHANNEL_RC_OK);
1596 return ok;
1597}
1598
1599bool sdlClip::webTakeDataResponse(const CLIPRDR_FORMAT_DATA_RESPONSE* response)
1600{
1601 std::string kind;
1602 {
1603 std::scoped_lock lock(_lock);
1604 if (_request_queue.empty())
1605 return false;
1606 kind = _request_queue.front().mime();
1607 if ((kind != s_web_mime_text) && (kind != s_web_mime_dib) && (kind != s_web_mime_png))
1608 return false;
1609 _request_queue.pop();
1610 }
1611
1612 if (!(response->common.msgFlags & CB_RESPONSE_OK) || !response->requestedFormatData)
1613 return true;
1614 const BYTE* data = response->requestedFormatData;
1615 const size_t size = response->common.dataLen;
1616 if (kind == s_web_mime_text)
1617 {
1618 char* utf8 = ConvertWCharNToUtf8Alloc(reinterpret_cast<const WCHAR*>(data),
1619 size / sizeof(WCHAR), nullptr);
1620 if (!utf8)
1621 return true;
1622 {
1623 std::scoped_lock lock(_lock);
1624 const auto hv = std::hash<std::string>{}(std::string(utf8));
1625 if (std::find(_web_recent.begin(), _web_recent.end(), hv) != _web_recent.end())
1626 {
1627 free(utf8); /* one of our own offers announced back: keep serving ours */
1628 return true;
1629 }
1630 _web_local.clear(); /* new content from the server: it owns the clipboard now */
1631 _web_local_dib.clear();
1632 _web_offers.serverSentText(utf8);
1633 }
1634 web_write(utf8, nullptr, 0);
1635 }
1636 else if ((kind == s_web_mime_png) ? (size > 8)
1637 : (size > 40)) /* PNG signature, BITMAPINFOHEADER */
1638 {
1639 {
1640 std::scoped_lock lock(_lock);
1641 const auto hv = std::hash<std::string_view>{}(
1642 std::string_view(reinterpret_cast<const char*>(data), size));
1643 if (std::find(_web_recent.begin(), _web_recent.end(), hv) != _web_recent.end())
1644 return true; /* one of our own images announced back */
1645 _web_local.clear();
1646 _web_local_dib.clear();
1647 _web_offers.serverSentImage();
1648 }
1649 auto copy = static_cast<BYTE*>(malloc(size));
1650 if (copy)
1651 {
1652 memcpy(copy, data, size);
1653 web_write(nullptr, copy, size, kind == s_web_mime_png);
1654 }
1655 }
1656 return true;
1657}
1658
1659bool sdlClip::webAnswerDataRequest(const CLIPRDR_FORMAT_DATA_REQUEST* request)
1660{
1661 std::string text;
1662 std::vector<BYTE> dib;
1663 {
1664 std::scoped_lock lock(_lock);
1665 if (_web_local.empty() && _web_local_dib.empty())
1666 return false;
1667 text = _web_local;
1668 dib = _web_local_dib;
1669 }
1670 switch (request->requestedFormatId)
1671 {
1672 case CF_UNICODETEXT:
1673 {
1674 if (text.empty())
1675 return false;
1676 WCHAR* wstr = ConvertUtf8NToWCharAlloc(text.c_str(), text.size() + 1, nullptr);
1677 if (!wstr)
1678 return false;
1679 /* UTF-16 including the terminating NUL, as CF_UNICODETEXT requires */
1680 std::ignore = SendDataResponse(reinterpret_cast<const BYTE*>(wstr),
1681 (_wcslen(wstr) + 1) * sizeof(WCHAR));
1682 free(wstr);
1683 return true;
1684 }
1685 case CF_TEXT:
1686 if (text.empty())
1687 return false;
1688 std::ignore =
1689 SendDataResponse(reinterpret_cast<const BYTE*>(text.c_str()), text.size() + 1);
1690 return true;
1691 case CF_DIB:
1692 if (dib.empty())
1693 return false;
1694 std::ignore = SendDataResponse(dib.data(), dib.size());
1695 return true;
1696 default:
1697 return false;
1698 }
1699}
1700
1701/* Called by the page on the main browser thread. That thread also runs FreeRDP's main loop
1702 * under Asyncify: when it is suspended in the middle of a transport write (e.g. waiting inside
1703 * a TLS write), calling into the channel from here would re-enter the same TLS connection on the
1704 * same thread; the transport lock does not stop that (same owner), the records interleave and
1705 * the server fails the connection with "bad record mac". So copy the data and send the format
1706 * list from a separate thread. */
1707typedef struct
1708{
1709 char* text;
1710 BYTE* dib;
1711 size_t size;
1712} web_offer_job;
1713
1714static void* web_offer_thread(void* arg)
1715{
1716 auto job = static_cast<web_offer_job*>(arg);
1717 std::ignore = sdlClip::webOffer(job->text, job->dib, job->size);
1718 free(job->text);
1719 free(job->dib);
1720 free(job);
1721 return nullptr;
1722}
1723
1724extern "C" EMSCRIPTEN_KEEPALIVE int freerdp_web_clipboard_offer(const char* utf8, const BYTE* dib,
1725 size_t dibSize)
1726{
1727 {
1728 std::scoped_lock web(s_web_lock);
1729 if (!s_web_clip)
1730 return 0;
1731 }
1732 auto job = static_cast<web_offer_job*>(calloc(1, sizeof(web_offer_job)));
1733 if (!job)
1734 return 0;
1735 job->text = utf8 ? _strdup(utf8) : nullptr;
1736 if (dib && dibSize)
1737 {
1738 job->dib = static_cast<BYTE*>(malloc(dibSize));
1739 if (job->dib)
1740 {
1741 memcpy(job->dib, dib, dibSize);
1742 job->size = dibSize;
1743 }
1744 }
1745 pthread_t tid = {};
1746 if (pthread_create(&tid, nullptr, web_offer_thread, job) != 0)
1747 {
1748 free(job->text);
1749 free(job->dib);
1750 free(job);
1751 return 0;
1752 }
1753 pthread_detach(tid);
1754 return 1;
1755}
1756#endif
object that handles clipboard context for the SDL3 client
Definition sdl_clip.hpp:78
WINPR_ATTR_NODISCARD FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.
#define TAG