FreeRDP
Loading...
Searching...
No Matches
sdlClip Class Reference

object that handles clipboard context for the SDL3 client More...

#include <sdl_clip.hpp>

Public Member Functions

 sdlClip (SdlContext *sdl)
 sdlClip (const sdlClip &)=delete
 sdlClip (sdlClip &&)=delete
sdlClip & operator= (const sdlClip &)=delete
sdlClip & operator= (sdlClip &&)=delete
bool init (CliprdrClientContext *clip)
bool uninit (CliprdrClientContext *clip)
bool handleEvent (const SDL_ClipboardEvent &ev)
void handleDataRequests ()

Detailed Description

object that handles clipboard context for the SDL3 client

Definition at line 65 of file sdl_clip.hpp.

Constructor & Destructor Documentation

◆ sdlClip()

sdlClip::sdlClip ( SdlContext * sdl)
explicit

Definition at line 226 of file sdl_clip.cpp.

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}
#define TAG

◆ ~sdlClip()

sdlClip::~sdlClip ( )
virtual

Definition at line 240 of file sdl_clip.cpp.

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}

Member Function Documentation

◆ handleDataRequests()

void sdlClip::handleDataRequests ( )

Definition at line 1077 of file sdl_clip.cpp.

1078{
1079 for (;;)
1080 {
1081 uint32_t formatId = 0;
1082 {
1083 std::scoped_lock lock(_lock);
1084 if (_server_requests.empty())
1085 return;
1086 formatId = _server_requests.front();
1087 _server_requests.pop_front();
1088 if (!_ctx)
1089 continue;
1090 }
1091
1092 uint32_t len = 0;
1093 auto data = getLocalData(formatId, len);
1094 std::scoped_lock lock(_lock);
1095 if (!_ctx)
1096 continue;
1097 if (SendDataResponse(data.get(), len) != CHANNEL_RC_OK)
1098 WLog_Print(_log, WLOG_WARN, "failed to send clipboard data response");
1099 }
1100}

◆ handleEvent()

bool sdlClip::handleEvent ( const SDL_ClipboardEvent & ev)
nodiscard

Definition at line 316 of file sdl_clip.cpp.

317{
318 std::unique_ptr<char*, decltype(&SDL_free)> current(nullptr, SDL_free);
319 const char** mime_types = ev.mime_types;
320 size_t nformats = WINPR_ASSERTING_INT_CAST(size_t, ev.num_mime_types);
321 bool connected = false;
322 {
323 std::scoped_lock lock(_lock);
324 connected = (_ctx != nullptr);
325 }
326 if (connected && _ctx && _sync && ev.owner && (ev.num_mime_types == 1))
327 {
328 if (strcmp(first_announcement[0], ev.mime_types[0]) == 0)
329 {
330 current.reset(SDL_GetClipboardMimeTypes(&nformats));
331 mime_types = const_cast<const char**>(current.get());
332 if (!mime_types)
333 nformats = 0;
334 }
335 }
336 else if (!connected || !_ctx || !_sync || ev.owner)
337 {
338 _last_timestamp = ev.timestamp;
339
340 /* The channel thread replaces _current_mimetypes on every server format list, so take
341 * them under the lock instead of trusting pointers carried in the event. */
342 std::vector<std::string> mimes;
343 {
344 ClipboardLockGuard systemlock(_system);
345 std::scoped_lock lock(_lock);
346 mimes.swap(_current_mimetypes);
347 if (!mimes.empty())
348 _cache_data.clear();
349 }
350 if (mimes.empty())
351 return true;
352
353 std::vector<const char*> cmimes;
354 cmimes.reserve(mimes.size());
355 for (const auto& m : mimes)
356 cmimes.push_back(m.c_str());
357
358 /* Server files need native clipboard items on some platforms (macOS), which only the
359 * file context can offer. It requests the data with ClipDataCb like SDL does. */
360 if (cliprdr_file_context_set_local_clipboard_data(_file, sdlClip::ClipDataCb, this,
361 cmimes.data(), cmimes.size()))
362 return true;
363
364 /* SDL copies the mime type strings */
365 return SDL_SetClipboardData(sdlClip::ClipDataCb, sdlClip::ClipCleanCb, this, cmimes.data(),
366 cmimes.size());
367 }
368
369 if (ev.timestamp == _last_timestamp)
370 return true;
371
372 if (contains(mime_types, WINPR_ASSERTING_INT_CAST(Sint32, nformats)))
373 return true;
374
375 /* SDL holds this session's server data right now, so this update was queued before our
376 * later SDL_SetClipboardData took effect: typically the selection a compositor re-sends on
377 * focus-in, racing a server format list. Announcing it would replace the fresh server
378 * clipboard with the stale local one (and the server would then ask us for data we no
379 * longer own). A genuine local change cancels our data before its update is queued. */
380 if (ownsClipboard())
381 {
382 WLog_Print(_log, WLOG_DEBUG, "ignoring stale local clipboard update");
383 return true;
384 }
385
386 {
387 ClipboardLockGuard systemlock(_system);
388 std::scoped_lock lock(_lock);
389 clearServerFormats();
390 }
391
392 const std::string mime_html = s_mime_html;
393
394 const std::vector<std::string> mime_bitmap = { BMP_MIME_LIST };
395 const std::string mime_webp = s_mime_webp;
396 const std::string mime_png = s_mime_png;
397 const std::string mime_jpeg = s_mime_jpg;
398 const std::string mime_tiff = s_mime_tiff;
399 const std::vector<std::string> mime_images = { mime_webp, mime_png, mime_jpeg,
400 mime_tiff, s_mime_avif, s_mime_jxl };
401
402 std::vector<std::string> clientFormatNames;
403 std::vector<CLIPRDR_FORMAT> clientFormats;
404
405 const char** clipboard_mime_formats = mime_types;
406
407 WLog_Print(_log, WLOG_TRACE, "SDL has %" PRIuz " formats", nformats);
408
409 bool textPushed = false;
410 bool imgPushed = false;
411 bool filePushed = false;
412
413 for (size_t i = 0; i < nformats; i++)
414 {
415 std::string local_mime = clipboard_mime_formats[i];
416 WLog_Print(_log, WLOG_TRACE, " - %s", local_mime.c_str());
417
418 if (std::find(s_mime_text().begin(), s_mime_text().end(), local_mime) !=
419 s_mime_text().end())
420 {
421 /* text formats */
422 if (!textPushed)
423 {
424 clientFormats.push_back({ CF_TEXT, nullptr });
425 clientFormats.push_back({ CF_OEMTEXT, nullptr });
426 clientFormats.push_back({ CF_UNICODETEXT, nullptr });
427 textPushed = true;
428 }
429 }
430 else if (local_mime == mime_html)
431 /* html */
432 clientFormatNames.emplace_back(s_type_HtmlFormat);
433 else if ((std::find(mime_bitmap.begin(), mime_bitmap.end(), local_mime) !=
434 mime_bitmap.end()) ||
435 (std::find(mime_images.begin(), mime_images.end(), local_mime) !=
436 mime_images.end()))
437 {
438 /* image formats */
439 if (!imgPushed)
440 {
441 clientFormats.push_back({ CF_DIB, nullptr });
442#if defined(WINPR_UTILS_IMAGE_DIBv5)
443 clientFormats.push_back({ CF_DIBV5, nullptr });
444#endif
445
446 if (winpr_image_format_is_supported(WINPR_IMAGE_BITMAP))
447 {
448 for (auto& bmp : mime_bitmap)
449 clientFormatNames.emplace_back(bmp);
450 }
451
452 if (winpr_image_format_is_supported(WINPR_IMAGE_JPEG))
453 clientFormatNames.emplace_back(mime_jpeg);
454 if (winpr_image_format_is_supported(WINPR_IMAGE_WEBP))
455 clientFormatNames.emplace_back(mime_webp);
456 if (winpr_image_format_is_supported(WINPR_IMAGE_PNG))
457 clientFormatNames.emplace_back(mime_png);
458
459 clientFormatNames.emplace_back(s_type_HtmlFormat);
460 imgPushed = true;
461 }
462 clientFormatNames.emplace_back(local_mime);
463 }
464 else if (mime_is_file(local_mime) || (local_mime == s_type_file_url))
465 {
466 if (!filePushed)
467 {
468 clientFormatNames.emplace_back(s_type_FileGroupDescriptorW);
469 filePushed = true;
470 }
471 }
472 }
473
474 std::sort(clientFormatNames.begin(), clientFormatNames.end());
475 clientFormatNames.erase(std::unique(clientFormatNames.begin(), clientFormatNames.end()),
476 clientFormatNames.end());
477
478 for (auto& name : clientFormatNames)
479 {
480 clientFormats.push_back({ ClipboardRegisterFormat(_system, name.c_str()), name.data() });
481 }
482
483 std::sort(clientFormats.begin(), clientFormats.end(),
484 [](const auto& a, const auto& b) { return a < b; });
485 auto u = std::unique(clientFormats.begin(), clientFormats.end());
486 clientFormats.erase(u, clientFormats.end());
487
488 const CLIPRDR_FORMAT_LIST formatList = {
489 { CB_FORMAT_LIST, 0, 0 },
490 static_cast<UINT32>(clientFormats.size()),
491 clientFormats.data(),
492 };
493
494 WLog_Print(_log, WLOG_TRACE,
495 "-------------- client format list [%" PRIu32 "] ------------------",
496 formatList.numFormats);
497 for (UINT32 x = 0; x < formatList.numFormats; x++)
498 {
499 auto format = &formatList.formats[x];
500 WLog_Print(_log, WLOG_TRACE, "client announces %" PRIu32 " [%s][%s]", format->formatId,
501 ClipboardGetFormatIdString(format->formatId), format->formatName);
502 }
503
504 /* The RDP thread may have closed the channel since the check above (see uninit) */
505 ClipboardLockGuard systemlock(_system);
506 std::scoped_lock lock(_lock);
507 if (!_ctx || !_sync)
508 return true;
509
510 if (cliprdr_file_context_notify_new_client_format_list(_file) != CHANNEL_RC_OK)
511 return false;
512
513 WINPR_ASSERT(_ctx);
514 WINPR_ASSERT(_ctx->ClientFormatList);
515 return _ctx->ClientFormatList(_ctx, &formatList) == CHANNEL_RC_OK;
516}

◆ init()

bool sdlClip::init ( CliprdrClientContext * clip)
nodiscard

Definition at line 254 of file sdl_clip.cpp.

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}

◆ uninit()

bool sdlClip::uninit ( CliprdrClientContext * clip)
nodiscard

Definition at line 277 of file sdl_clip.cpp.

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 /* The object outlives the channel: after a reconnect or redirect the new channel must see
290 * its Monitor Ready before we announce the local clipboard ([MS-RDPECLIP] 1.3.2.1). */
291 _sync = false;
292 if (!cliprdr_file_context_uninit(_file, _ctx))
293 return false;
294 _ctx = nullptr;
295 clip->custom = nullptr;
296 _server_requests.clear();
297 return true;
298}

The documentation for this class was generated from the following files: