Clipboard.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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();
  52. auto metadata_clone_or_error = metadata.clone();
  53. if (metadata_clone_or_error.is_error())
  54. return {};
  55. if (!response.data().is_valid())
  56. return { {}, type, metadata_clone_or_error.release_value() };
  57. auto data = ByteBuffer::copy(response.data().data<void>(), response.data().size());
  58. if (data.is_error())
  59. return {};
  60. return { data.release_value(), type, metadata_clone_or_error.release_value() };
  61. }
  62. RefPtr<Gfx::Bitmap> Clipboard::DataAndType::as_bitmap() const
  63. {
  64. if (mime_type != "image/x-serenityos")
  65. return nullptr;
  66. auto width = metadata.get("width").value_or("0").to_uint();
  67. if (!width.has_value() || width.value() == 0)
  68. return nullptr;
  69. auto height = metadata.get("height").value_or("0").to_uint();
  70. if (!height.has_value() || height.value() == 0)
  71. return nullptr;
  72. auto scale = metadata.get("scale").value_or("0").to_uint();
  73. if (!scale.has_value() || scale.value() == 0)
  74. return nullptr;
  75. auto pitch = metadata.get("pitch").value_or("0").to_uint();
  76. if (!pitch.has_value() || pitch.value() == 0)
  77. return nullptr;
  78. auto format = metadata.get("format").value_or("0").to_uint();
  79. if (!format.has_value() || format.value() == 0)
  80. return nullptr;
  81. if (!Gfx::is_valid_bitmap_format(format.value()))
  82. return nullptr;
  83. auto bitmap_format = (Gfx::BitmapFormat)format.value();
  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. ErrorOr<Clipboard::DataAndType> Clipboard::DataAndType::from_json(JsonObject const& object)
  103. {
  104. if (!object.has("data"sv) && !object.has("mime_type"sv))
  105. return Error::from_string_literal("JsonObject does not contain necessary fields");
  106. DataAndType result;
  107. result.data = object.get_deprecated_string("data"sv)->to_byte_buffer();
  108. result.mime_type = *object.get_deprecated_string("mime_type"sv);
  109. // FIXME: Also read metadata
  110. return result;
  111. }
  112. ErrorOr<JsonObject> Clipboard::DataAndType::to_json() const
  113. {
  114. JsonObject object;
  115. object.set("data", TRY(DeprecatedString::from_utf8(data.bytes())));
  116. object.set("mime_type", mime_type);
  117. // FIXME: Also write metadata
  118. return object;
  119. }
  120. void Clipboard::set_data(ReadonlyBytes data, DeprecatedString const& type, HashMap<DeprecatedString, DeprecatedString> const& metadata)
  121. {
  122. if (data.is_empty()) {
  123. connection().async_set_clipboard_data({}, type, metadata.clone().release_value_but_fixme_should_propagate_errors());
  124. return;
  125. }
  126. auto buffer_or_error = Core::AnonymousBuffer::create_with_size(data.size());
  127. if (buffer_or_error.is_error()) {
  128. dbgln("GUI::Clipboard::set_data() failed to create a buffer");
  129. return;
  130. }
  131. auto buffer = buffer_or_error.release_value();
  132. memcpy(buffer.data<void>(), data.data(), data.size());
  133. connection().async_set_clipboard_data(move(buffer), type, metadata.clone().release_value_but_fixme_should_propagate_errors());
  134. }
  135. void Clipboard::set_bitmap(Gfx::Bitmap const& bitmap, HashMap<DeprecatedString, DeprecatedString> const& additional_metadata)
  136. {
  137. HashMap<DeprecatedString, DeprecatedString> metadata = additional_metadata.clone().release_value_but_fixme_should_propagate_errors();
  138. metadata.set("width", DeprecatedString::number(bitmap.width()));
  139. metadata.set("height", DeprecatedString::number(bitmap.height()));
  140. metadata.set("scale", DeprecatedString::number(bitmap.scale()));
  141. metadata.set("format", DeprecatedString::number((int)bitmap.format()));
  142. metadata.set("pitch", DeprecatedString::number(bitmap.pitch()));
  143. set_data({ bitmap.scanline(0), bitmap.size_in_bytes() }, "image/x-serenityos", move(metadata));
  144. }
  145. void Clipboard::clear()
  146. {
  147. connection().async_set_clipboard_data({}, {}, {});
  148. }
  149. void Clipboard::clipboard_data_changed(Badge<ConnectionToClipboardServer>, DeprecatedString const& mime_type)
  150. {
  151. if (on_change)
  152. on_change(mime_type);
  153. for (auto* client : m_clients)
  154. client->clipboard_content_did_change(mime_type);
  155. }
  156. Clipboard::ClipboardClient::ClipboardClient()
  157. {
  158. Clipboard::the().register_client({}, *this);
  159. }
  160. Clipboard::ClipboardClient::~ClipboardClient()
  161. {
  162. Clipboard::the().unregister_client({}, *this);
  163. }
  164. }