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
341
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
359
360 if (cliprdr_file_context_set_local_clipboard_data(_file, sdlClip::ClipDataCb, this,
361 cmimes.data(), cmimes.size()))
362 return true;
363
364
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
376
377
378
379
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
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
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
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
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}