Clipboard.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 <Clipboard/ClipboardClientEndpoint.h>
  28. #include <Clipboard/ClipboardServerEndpoint.h>
  29. #include <LibGUI/Clipboard.h>
  30. #include <LibIPC/ServerConnection.h>
  31. namespace GUI {
  32. class ClipboardServerConnection : public IPC::ServerConnection<ClipboardClientEndpoint, ClipboardServerEndpoint>
  33. , public ClipboardClientEndpoint {
  34. C_OBJECT(ClipboardServerConnection);
  35. public:
  36. virtual void handshake() override
  37. {
  38. auto response = send_sync<Messages::ClipboardServer::Greet>();
  39. set_my_client_id(response->client_id());
  40. }
  41. private:
  42. ClipboardServerConnection()
  43. : IPC::ServerConnection<ClipboardClientEndpoint, ClipboardServerEndpoint>(*this, "/tmp/portal/clipboard")
  44. {
  45. }
  46. virtual void handle(const Messages::ClipboardClient::ClipboardDataChanged&) override;
  47. };
  48. Clipboard& Clipboard::the()
  49. {
  50. static Clipboard* s_the;
  51. if (!s_the)
  52. s_the = new Clipboard;
  53. return *s_the;
  54. }
  55. ClipboardServerConnection* s_connection;
  56. static ClipboardServerConnection& connection()
  57. {
  58. return *s_connection;
  59. }
  60. void Clipboard::initialize(Badge<Application>)
  61. {
  62. s_connection = &ClipboardServerConnection::construct().leak_ref();
  63. }
  64. Clipboard::Clipboard()
  65. {
  66. }
  67. Clipboard::DataAndType Clipboard::data_and_type() const
  68. {
  69. auto response = connection().send_sync<Messages::ClipboardServer::GetClipboardData>();
  70. if (!response->data().is_valid())
  71. return {};
  72. auto data = ByteBuffer::copy(response->data().data<void>(), response->data().size());
  73. auto type = response->mime_type();
  74. auto metadata = response->metadata().entries();
  75. return { data, type, metadata };
  76. }
  77. void Clipboard::set_data(ReadonlyBytes data, const String& type, const HashMap<String, String>& metadata)
  78. {
  79. auto buffer = Core::AnonymousBuffer::create_with_size(data.size());
  80. if (!buffer.is_valid()) {
  81. dbgln("GUI::Clipboard::set_data() failed to create a buffer");
  82. return;
  83. }
  84. if (!data.is_empty())
  85. memcpy(buffer.data<void>(), data.data(), data.size());
  86. connection().send_sync<Messages::ClipboardServer::SetClipboardData>(move(buffer), type, metadata);
  87. }
  88. void ClipboardServerConnection::handle(const Messages::ClipboardClient::ClipboardDataChanged& message)
  89. {
  90. auto& clipboard = Clipboard::the();
  91. if (clipboard.on_change)
  92. clipboard.on_change(message.mime_type());
  93. }
  94. RefPtr<Gfx::Bitmap> Clipboard::bitmap() const
  95. {
  96. auto clipping = data_and_type();
  97. if (clipping.mime_type != "image/x-serenityos")
  98. return nullptr;
  99. auto width = clipping.metadata.get("width").value_or("0").to_uint();
  100. if (!width.has_value() || width.value() == 0)
  101. return nullptr;
  102. auto height = clipping.metadata.get("height").value_or("0").to_uint();
  103. if (!height.has_value() || height.value() == 0)
  104. return nullptr;
  105. auto scale = clipping.metadata.get("scale").value_or("0").to_uint();
  106. if (!scale.has_value() || scale.value() == 0)
  107. return nullptr;
  108. auto pitch = clipping.metadata.get("pitch").value_or("0").to_uint();
  109. if (!pitch.has_value() || pitch.value() == 0)
  110. return nullptr;
  111. auto format = clipping.metadata.get("format").value_or("0").to_uint();
  112. if (!format.has_value() || format.value() == 0)
  113. return nullptr;
  114. auto clipping_bitmap = Gfx::Bitmap::create_wrapper((Gfx::BitmapFormat)format.value(), { (int)width.value(), (int)height.value() }, scale.value(), pitch.value(), clipping.data.data());
  115. auto bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::RGBA32, { (int)width.value(), (int)height.value() }, scale.value());
  116. for (int y = 0; y < clipping_bitmap->physical_height(); ++y) {
  117. for (int x = 0; x < clipping_bitmap->physical_width(); ++x) {
  118. auto pixel = clipping_bitmap->get_pixel(x, y);
  119. bitmap->set_pixel(x, y, pixel);
  120. }
  121. }
  122. return bitmap;
  123. }
  124. void Clipboard::set_bitmap(const Gfx::Bitmap& bitmap)
  125. {
  126. HashMap<String, String> metadata;
  127. metadata.set("width", String::number(bitmap.width()));
  128. metadata.set("height", String::number(bitmap.height()));
  129. metadata.set("scale", String::number(bitmap.scale()));
  130. metadata.set("format", String::number((int)bitmap.format()));
  131. metadata.set("pitch", String::number(bitmap.pitch()));
  132. set_data({ bitmap.scanline(0), bitmap.size_in_bytes() }, "image/x-serenityos", metadata);
  133. }
  134. }