GClipboard.cpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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/SharedBuffer.h>
  27. #include <LibGUI/GClipboard.h>
  28. #include <LibGUI/GWindowServerConnection.h>
  29. GClipboard& GClipboard::the()
  30. {
  31. static GClipboard* s_the;
  32. if (!s_the)
  33. s_the = new GClipboard;
  34. return *s_the;
  35. }
  36. GClipboard::GClipboard()
  37. {
  38. }
  39. GClipboard::DataAndType GClipboard::data_and_type() const
  40. {
  41. auto response = GWindowServerConnection::the().send_sync<WindowServer::GetClipboardContents>();
  42. if (response->shared_buffer_id() < 0)
  43. return {};
  44. auto shared_buffer = SharedBuffer::create_from_shared_buffer_id(response->shared_buffer_id());
  45. if (!shared_buffer) {
  46. dbgprintf("GClipboard::data() failed to attach to the shared buffer\n");
  47. return {};
  48. }
  49. if (response->content_size() > shared_buffer->size()) {
  50. dbgprintf("GClipboard::data() clipping contents size is greater than shared buffer size\n");
  51. return {};
  52. }
  53. auto data = String((const char*)shared_buffer->data(), response->content_size());
  54. auto type = response->content_type();
  55. return { data, type };
  56. }
  57. void GClipboard::set_data(const StringView& data, const String& type)
  58. {
  59. auto shared_buffer = SharedBuffer::create_with_size(data.length() + 1);
  60. if (!shared_buffer) {
  61. dbgprintf("GClipboard::set_data() failed to create a shared buffer\n");
  62. return;
  63. }
  64. if (!data.is_empty())
  65. memcpy(shared_buffer->data(), data.characters_without_null_termination(), data.length() + 1);
  66. else
  67. ((u8*)shared_buffer->data())[0] = '\0';
  68. shared_buffer->seal();
  69. shared_buffer->share_with(GWindowServerConnection::the().server_pid());
  70. GWindowServerConnection::the().send_sync<WindowServer::SetClipboardContents>(shared_buffer->shared_buffer_id(), data.length(), type);
  71. }
  72. void GClipboard::did_receive_clipboard_contents_changed(Badge<GWindowServerConnection>, const String& data_type)
  73. {
  74. if (on_content_change)
  75. on_content_change(data_type);
  76. }