Clipboard.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <Clipboard/ClipboardClientEndpoint.h>
  8. #include <Clipboard/ClipboardServerEndpoint.h>
  9. #include <LibGUI/Clipboard.h>
  10. #include <LibGfx/Bitmap.h>
  11. #include <LibIPC/ConnectionToServer.h>
  12. namespace GUI {
  13. class ConnectionToClipboardServer final
  14. : public IPC::ConnectionToServer<ClipboardClientEndpoint, ClipboardServerEndpoint>
  15. , public ClipboardClientEndpoint {
  16. IPC_CLIENT_CONNECTION(ConnectionToClipboardServer, "/tmp/session/%sid/portal/clipboard"sv)
  17. private:
  18. ConnectionToClipboardServer(NonnullOwnPtr<Core::LocalSocket> socket)
  19. : IPC::ConnectionToServer<ClipboardClientEndpoint, ClipboardServerEndpoint>(*this, move(socket))
  20. {
  21. }
  22. virtual void clipboard_data_changed(DeprecatedString const& mime_type) override
  23. {
  24. Clipboard::the().clipboard_data_changed({}, mime_type);
  25. }
  26. };
  27. static RefPtr<ConnectionToClipboardServer> s_connection;
  28. static ConnectionToClipboardServer& connection()
  29. {
  30. return *s_connection;
  31. }
  32. void Clipboard::initialize(Badge<Application>)
  33. {
  34. s_connection = ConnectionToClipboardServer::try_create().release_value_but_fixme_should_propagate_errors();
  35. }
  36. Clipboard& Clipboard::the()
  37. {
  38. static bool s_destructed = false;
  39. static ScopeGuard destructed_guard([] {
  40. s_destructed = true;
  41. });
  42. VERIFY(!s_destructed); // Catch use-after-free
  43. static Clipboard s_the;
  44. return s_the;
  45. }
  46. Clipboard::DataAndType Clipboard::fetch_data_and_type() const
  47. {
  48. auto response = connection().get_clipboard_data();
  49. auto type = response.mime_type();
  50. auto metadata = response.metadata().entries();
  51. if (!response.data().is_valid())
  52. return { {}, type, metadata };
  53. auto data = ByteBuffer::copy(response.data().data<void>(), response.data().size());
  54. if (data.is_error())
  55. return {};
  56. return { data.release_value(), type, metadata };
  57. }
  58. RefPtr<Gfx::Bitmap> Clipboard::DataAndType::as_bitmap() const
  59. {
  60. if (mime_type != "image/x-serenityos")
  61. return nullptr;
  62. auto width = metadata.get("width").value_or("0").to_uint();
  63. if (!width.has_value() || width.value() == 0)
  64. return nullptr;
  65. auto height = metadata.get("height").value_or("0").to_uint();
  66. if (!height.has_value() || height.value() == 0)
  67. return nullptr;
  68. auto scale = metadata.get("scale").value_or("0").to_uint();
  69. if (!scale.has_value() || scale.value() == 0)
  70. return nullptr;
  71. auto pitch = metadata.get("pitch").value_or("0").to_uint();
  72. if (!pitch.has_value() || pitch.value() == 0)
  73. return nullptr;
  74. auto format = metadata.get("format").value_or("0").to_uint();
  75. if (!format.has_value() || format.value() == 0)
  76. return nullptr;
  77. if (!Gfx::is_valid_bitmap_format(format.value()))
  78. return nullptr;
  79. auto bitmap_format = (Gfx::BitmapFormat)format.value();
  80. // We cannot handle indexed bitmaps, as the palette would be lost.
  81. // Thankfully, everything that copies bitmaps also transforms them to RGB beforehand.
  82. if (Gfx::determine_storage_format(bitmap_format) == Gfx::StorageFormat::Indexed8)
  83. return nullptr;
  84. // We won't actually write to the clipping_bitmap, so casting away the const is okay.
  85. auto clipping_data = const_cast<u8*>(data.data());
  86. auto clipping_bitmap_or_error = Gfx::Bitmap::create_wrapper(bitmap_format, { (int)width.value(), (int)height.value() }, scale.value(), pitch.value(), clipping_data);
  87. if (clipping_bitmap_or_error.is_error())
  88. return nullptr;
  89. auto clipping_bitmap = clipping_bitmap_or_error.release_value_but_fixme_should_propagate_errors();
  90. auto bitmap_or_error = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, { (int)width.value(), (int)height.value() }, scale.value());
  91. if (bitmap_or_error.is_error())
  92. return nullptr;
  93. auto bitmap = bitmap_or_error.release_value_but_fixme_should_propagate_errors();
  94. for (int y = 0; y < clipping_bitmap->physical_height(); ++y) {
  95. for (int x = 0; x < clipping_bitmap->physical_width(); ++x) {
  96. auto pixel = clipping_bitmap->get_pixel(x, y);
  97. bitmap->set_pixel(x, y, pixel);
  98. }
  99. }
  100. return bitmap;
  101. }
  102. void Clipboard::set_data(ReadonlyBytes data, DeprecatedString const& type, HashMap<DeprecatedString, DeprecatedString> const& metadata)
  103. {
  104. if (data.is_empty()) {
  105. connection().async_set_clipboard_data({}, type, metadata);
  106. return;
  107. }
  108. auto buffer_or_error = Core::AnonymousBuffer::create_with_size(data.size());
  109. if (buffer_or_error.is_error()) {
  110. dbgln("GUI::Clipboard::set_data() failed to create a buffer");
  111. return;
  112. }
  113. auto buffer = buffer_or_error.release_value();
  114. memcpy(buffer.data<void>(), data.data(), data.size());
  115. connection().async_set_clipboard_data(move(buffer), type, metadata);
  116. }
  117. void Clipboard::set_bitmap(Gfx::Bitmap const& bitmap, HashMap<DeprecatedString, DeprecatedString> const& additional_metadata)
  118. {
  119. HashMap<DeprecatedString, DeprecatedString> metadata(additional_metadata);
  120. metadata.set("width", DeprecatedString::number(bitmap.width()));
  121. metadata.set("height", DeprecatedString::number(bitmap.height()));
  122. metadata.set("scale", DeprecatedString::number(bitmap.scale()));
  123. metadata.set("format", DeprecatedString::number((int)bitmap.format()));
  124. metadata.set("pitch", DeprecatedString::number(bitmap.pitch()));
  125. set_data({ bitmap.scanline(0), bitmap.size_in_bytes() }, "image/x-serenityos", metadata);
  126. }
  127. void Clipboard::clear()
  128. {
  129. connection().async_set_clipboard_data({}, {}, {});
  130. }
  131. void Clipboard::clipboard_data_changed(Badge<ConnectionToClipboardServer>, DeprecatedString const& mime_type)
  132. {
  133. if (on_change)
  134. on_change(mime_type);
  135. for (auto* client : m_clients)
  136. client->clipboard_content_did_change(mime_type);
  137. }
  138. Clipboard::ClipboardClient::ClipboardClient()
  139. {
  140. Clipboard::the().register_client({}, *this);
  141. }
  142. Clipboard::ClipboardClient::~ClipboardClient()
  143. {
  144. Clipboard::the().unregister_client({}, *this);
  145. }
  146. }