Clipboard.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Badge.h>
  27. #include <AK/SharedBuffer.h>
  28. #include <Clipboard/ClipboardClientEndpoint.h>
  29. #include <Clipboard/ClipboardServerEndpoint.h>
  30. #include <LibGUI/Clipboard.h>
  31. #include <LibIPC/ServerConnection.h>
  32. namespace GUI {
  33. class ClipboardServerConnection : public IPC::ServerConnection<ClipboardClientEndpoint, ClipboardServerEndpoint>
  34. , public ClipboardClientEndpoint {
  35. C_OBJECT(ClipboardServerConnection);
  36. public:
  37. virtual void handshake() override
  38. {
  39. auto response = send_sync<Messages::ClipboardServer::Greet>();
  40. set_my_client_id(response->client_id());
  41. }
  42. private:
  43. ClipboardServerConnection()
  44. : IPC::ServerConnection<ClipboardClientEndpoint, ClipboardServerEndpoint>(*this, "/tmp/portal/clipboard")
  45. {
  46. }
  47. virtual void handle(const Messages::ClipboardClient::ClipboardDataChanged&) override;
  48. };
  49. Clipboard& Clipboard::the()
  50. {
  51. static Clipboard* s_the;
  52. if (!s_the)
  53. s_the = new Clipboard;
  54. return *s_the;
  55. }
  56. ClipboardServerConnection* s_connection;
  57. static ClipboardServerConnection& connection()
  58. {
  59. return *s_connection;
  60. }
  61. void Clipboard::initialize(Badge<Application>)
  62. {
  63. s_connection = &ClipboardServerConnection::construct().leak_ref();
  64. }
  65. Clipboard::Clipboard()
  66. {
  67. }
  68. Clipboard::DataAndType Clipboard::data_and_type() const
  69. {
  70. auto response = connection().send_sync<Messages::ClipboardServer::GetClipboardData>();
  71. if (response->shbuf_id() < 0)
  72. return {};
  73. auto shared_buffer = SharedBuffer::create_from_shbuf_id(response->shbuf_id());
  74. if (!shared_buffer) {
  75. dbgln("GUI::Clipboard::data() failed to attach to the shared buffer");
  76. return {};
  77. }
  78. if (response->data_size() > shared_buffer->size()) {
  79. dbgln("GUI::Clipboard::data() clipping contents size is greater than shared buffer size");
  80. return {};
  81. }
  82. auto data = ByteBuffer::copy(shared_buffer->data<void>(), response->data_size());
  83. auto type = response->mime_type();
  84. auto metadata = response->metadata().entries();
  85. return { data, type, metadata };
  86. }
  87. void Clipboard::set_data(ReadonlyBytes data, const String& type, const HashMap<String, String>& metadata)
  88. {
  89. auto shared_buffer = SharedBuffer::create_with_size(data.size());
  90. if (!shared_buffer) {
  91. dbgln("GUI::Clipboard::set_data() failed to create a shared buffer");
  92. return;
  93. }
  94. if (!data.is_empty())
  95. memcpy(shared_buffer->data<void>(), data.data(), data.size());
  96. shared_buffer->seal();
  97. shared_buffer->share_with(connection().server_pid());
  98. connection().send_sync<Messages::ClipboardServer::SetClipboardData>(shared_buffer->shbuf_id(), data.size(), type, metadata);
  99. }
  100. void ClipboardServerConnection::handle(const Messages::ClipboardClient::ClipboardDataChanged& message)
  101. {
  102. auto& clipboard = Clipboard::the();
  103. if (clipboard.on_change)
  104. clipboard.on_change(message.mime_type());
  105. }
  106. RefPtr<Gfx::Bitmap> Clipboard::bitmap() const
  107. {
  108. auto clipping = data_and_type();
  109. if (clipping.mime_type != "image/x-serenityos")
  110. return nullptr;
  111. auto width = clipping.metadata.get("width").value_or("0").to_uint();
  112. if (!width.has_value() || width.value() == 0)
  113. return nullptr;
  114. auto height = clipping.metadata.get("height").value_or("0").to_uint();
  115. if (!height.has_value() || height.value() == 0)
  116. return nullptr;
  117. auto pitch = clipping.metadata.get("pitch").value_or("0").to_uint();
  118. if (!pitch.has_value() || pitch.value() == 0)
  119. return nullptr;
  120. auto format = clipping.metadata.get("format").value_or("0").to_uint();
  121. if (!format.has_value() || format.value() == 0)
  122. return nullptr;
  123. auto clipping_bitmap = Gfx::Bitmap::create_wrapper((Gfx::BitmapFormat)format.value(), { (int)width.value(), (int)height.value() }, pitch.value(), clipping.data.data());
  124. auto bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::RGBA32, { (int)width.value(), (int)height.value() });
  125. for (int y = 0; y < clipping_bitmap->height(); ++y) {
  126. for (int x = 0; x < clipping_bitmap->width(); ++x) {
  127. auto pixel = clipping_bitmap->get_pixel(x, y);
  128. bitmap->set_pixel(x, y, pixel);
  129. }
  130. }
  131. return bitmap;
  132. }
  133. void Clipboard::set_bitmap(const Gfx::Bitmap& bitmap)
  134. {
  135. HashMap<String, String> metadata;
  136. metadata.set("width", String::number(bitmap.width()));
  137. metadata.set("height", String::number(bitmap.height()));
  138. metadata.set("format", String::number((int)bitmap.format()));
  139. metadata.set("pitch", String::number(bitmap.pitch()));
  140. set_data({ bitmap.scanline(0), bitmap.size_in_bytes() }, "image/x-serenityos", metadata);
  141. }
  142. }