Clipboard.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. ErrorOr<void> Clipboard::initialize(Badge<Application>)
  33. {
  34. s_connection = TRY(ConnectionToClipboardServer::try_create());
  35. return {};
  36. }
  37. Clipboard& Clipboard::the()
  38. {
  39. static bool s_destructed = false;
  40. static ScopeGuard destructed_guard([] {
  41. s_destructed = true;
  42. });
  43. VERIFY(!s_destructed); // Catch use-after-free
  44. static Clipboard s_the;
  45. return s_the;
  46. }
  47. Clipboard::DataAndType Clipboard::fetch_data_and_type() const
  48. {
  49. auto response = connection().get_clipboard_data();
  50. auto type = response.mime_type();
  51. auto metadata = response.metadata().entries();
  52. if (!response.data().is_valid())
  53. return { {}, type, metadata };
  54. auto data = ByteBuffer::copy(response.data().data<void>(), response.data().size());
  55. if (data.is_error())
  56. return {};
  57. return { data.release_value(), type, metadata };
  58. }
  59. RefPtr<Gfx::Bitmap> Clipboard::DataAndType::as_bitmap() const
  60. {
  61. if (mime_type != "image/x-serenityos")
  62. return nullptr;
  63. auto width = metadata.get("width").value_or("0").to_uint();
  64. if (!width.has_value() || width.value() == 0)
  65. return nullptr;
  66. auto height = metadata.get("height").value_or("0").to_uint();
  67. if (!height.has_value() || height.value() == 0)
  68. return nullptr;
  69. auto scale = metadata.get("scale").value_or("0").to_uint();
  70. if (!scale.has_value() || scale.value() == 0)
  71. return nullptr;
  72. auto pitch = metadata.get("pitch").value_or("0").to_uint();
  73. if (!pitch.has_value() || pitch.value() == 0)
  74. return nullptr;
  75. auto format = metadata.get("format").value_or("0").to_uint();
  76. if (!format.has_value() || format.value() == 0)
  77. return nullptr;
  78. if (!Gfx::is_valid_bitmap_format(format.value()))
  79. return nullptr;
  80. auto bitmap_format = (Gfx::BitmapFormat)format.value();
  81. // We cannot handle indexed bitmaps, as the palette would be lost.
  82. // Thankfully, everything that copies bitmaps also transforms them to RGB beforehand.
  83. if (Gfx::determine_storage_format(bitmap_format) == Gfx::StorageFormat::Indexed8)
  84. return nullptr;
  85. // We won't actually write to the clipping_bitmap, so casting away the const is okay.
  86. auto clipping_data = const_cast<u8*>(data.data());
  87. auto clipping_bitmap_or_error = Gfx::Bitmap::create_wrapper(bitmap_format, { (int)width.value(), (int)height.value() }, scale.value(), pitch.value(), clipping_data);
  88. if (clipping_bitmap_or_error.is_error())
  89. return nullptr;
  90. auto clipping_bitmap = clipping_bitmap_or_error.release_value_but_fixme_should_propagate_errors();
  91. auto bitmap_or_error = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, { (int)width.value(), (int)height.value() }, scale.value());
  92. if (bitmap_or_error.is_error())
  93. return nullptr;
  94. auto bitmap = bitmap_or_error.release_value_but_fixme_should_propagate_errors();
  95. for (int y = 0; y < clipping_bitmap->physical_height(); ++y) {
  96. for (int x = 0; x < clipping_bitmap->physical_width(); ++x) {
  97. auto pixel = clipping_bitmap->get_pixel(x, y);
  98. bitmap->set_pixel(x, y, pixel);
  99. }
  100. }
  101. return bitmap;
  102. }
  103. ErrorOr<Clipboard::DataAndType> Clipboard::DataAndType::from_json(JsonObject const& object)
  104. {
  105. if (!object.has("data"sv) && !object.has("mime_type"sv))
  106. return Error::from_string_literal("JsonObject does not contain necessary fields");
  107. DataAndType result;
  108. result.data = object.get_deprecated_string("data"sv)->to_byte_buffer();
  109. result.mime_type = *object.get_deprecated_string("mime_type"sv);
  110. // FIXME: Also read metadata
  111. return result;
  112. }
  113. ErrorOr<JsonObject> Clipboard::DataAndType::to_json() const
  114. {
  115. JsonObject object;
  116. object.set("data", TRY(DeprecatedString::from_utf8(data.bytes())));
  117. object.set("mime_type", mime_type);
  118. // FIXME: Also write metadata
  119. return object;
  120. }
  121. void Clipboard::set_data(ReadonlyBytes data, DeprecatedString const& type, HashMap<DeprecatedString, DeprecatedString> const& metadata)
  122. {
  123. if (data.is_empty()) {
  124. connection().async_set_clipboard_data({}, type, metadata);
  125. return;
  126. }
  127. auto buffer_or_error = Core::AnonymousBuffer::create_with_size(data.size());
  128. if (buffer_or_error.is_error()) {
  129. dbgln("GUI::Clipboard::set_data() failed to create a buffer");
  130. return;
  131. }
  132. auto buffer = buffer_or_error.release_value();
  133. memcpy(buffer.data<void>(), data.data(), data.size());
  134. connection().async_set_clipboard_data(move(buffer), type, metadata);
  135. }
  136. void Clipboard::set_bitmap(Gfx::Bitmap const& bitmap, HashMap<DeprecatedString, DeprecatedString> const& additional_metadata)
  137. {
  138. HashMap<DeprecatedString, DeprecatedString> metadata(additional_metadata);
  139. metadata.set("width", DeprecatedString::number(bitmap.width()));
  140. metadata.set("height", DeprecatedString::number(bitmap.height()));
  141. metadata.set("scale", DeprecatedString::number(bitmap.scale()));
  142. metadata.set("format", DeprecatedString::number((int)bitmap.format()));
  143. metadata.set("pitch", DeprecatedString::number(bitmap.pitch()));
  144. set_data({ bitmap.scanline(0), bitmap.size_in_bytes() }, "image/x-serenityos", metadata);
  145. }
  146. void Clipboard::clear()
  147. {
  148. connection().async_set_clipboard_data({}, {}, {});
  149. }
  150. void Clipboard::clipboard_data_changed(Badge<ConnectionToClipboardServer>, DeprecatedString const& mime_type)
  151. {
  152. if (on_change)
  153. on_change(mime_type);
  154. for (auto* client : m_clients)
  155. client->clipboard_content_did_change(mime_type);
  156. }
  157. Clipboard::ClipboardClient::ClipboardClient()
  158. {
  159. Clipboard::the().register_client({}, *this);
  160. }
  161. Clipboard::ClipboardClient::~ClipboardClient()
  162. {
  163. Clipboard::the().unregister_client({}, *this);
  164. }
  165. }