Clipboard.cpp 5.6 KB

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