2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
|
*
|
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
|
*
|
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
|
*
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
*/
|
|
|
|
|
|
2020-02-16 09:17:49 +01:00
|
|
|
#include <AK/Badge.h>
|
2020-01-01 18:53:34 +01:00
|
|
|
#include <AK/SharedBuffer.h>
|
2020-02-06 20:33:02 +01:00
|
|
|
#include <LibGUI/Clipboard.h>
|
|
|
|
|
#include <LibGUI/WindowServerConnection.h>
|
2019-03-08 13:27:19 +01:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
namespace GUI {
|
|
|
|
|
|
|
|
|
|
Clipboard& Clipboard::the()
|
2019-04-05 05:10:18 +02:00
|
|
|
{
|
2020-02-02 15:07:41 +01:00
|
|
|
static Clipboard* s_the;
|
2019-04-05 05:10:18 +02:00
|
|
|
if (!s_the)
|
2020-02-02 15:07:41 +01:00
|
|
|
s_the = new Clipboard;
|
2019-04-05 05:10:18 +02:00
|
|
|
return *s_the;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
Clipboard::Clipboard()
|
2019-04-05 05:10:18 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
Clipboard::DataAndType Clipboard::data_and_type() const
|
2019-03-08 13:27:19 +01:00
|
|
|
{
|
2020-02-06 20:20:44 +01:00
|
|
|
auto response = WindowServerConnection::the().send_sync<Messages::WindowServer::GetClipboardContents>();
|
2020-02-28 11:45:19 +01:00
|
|
|
if (response->shbuf_id() < 0)
|
2019-06-07 11:46:02 +02:00
|
|
|
return {};
|
2020-02-28 11:45:19 +01:00
|
|
|
auto shared_buffer = SharedBuffer::create_from_shbuf_id(response->shbuf_id());
|
2019-03-08 13:27:19 +01:00
|
|
|
if (!shared_buffer) {
|
2020-02-02 15:07:41 +01:00
|
|
|
dbgprintf("GUI::Clipboard::data() failed to attach to the shared buffer\n");
|
2019-06-07 11:46:02 +02:00
|
|
|
return {};
|
2019-03-08 13:27:19 +01:00
|
|
|
}
|
2019-12-02 09:33:37 +01:00
|
|
|
if (response->content_size() > shared_buffer->size()) {
|
2020-02-02 15:07:41 +01:00
|
|
|
dbgprintf("GUI::Clipboard::data() clipping contents size is greater than shared buffer size\n");
|
2019-06-07 11:46:02 +02:00
|
|
|
return {};
|
2019-03-08 13:27:19 +01:00
|
|
|
}
|
2019-12-02 09:33:37 +01:00
|
|
|
auto data = String((const char*)shared_buffer->data(), response->content_size());
|
|
|
|
|
auto type = response->content_type();
|
2019-09-14 09:19:05 +02:00
|
|
|
return { data, type };
|
2019-03-08 13:27:19 +01:00
|
|
|
}
|
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
void Clipboard::set_data(const StringView& data, const String& type)
|
2019-03-08 13:27:19 +01:00
|
|
|
{
|
2019-07-18 09:52:22 +02:00
|
|
|
auto shared_buffer = SharedBuffer::create_with_size(data.length() + 1);
|
2019-03-08 13:27:19 +01:00
|
|
|
if (!shared_buffer) {
|
2020-02-02 15:07:41 +01:00
|
|
|
dbgprintf("GUI::Clipboard::set_data() failed to create a shared buffer\n");
|
2019-03-08 13:27:19 +01:00
|
|
|
return;
|
|
|
|
|
}
|
2019-03-18 02:59:08 +01:00
|
|
|
if (!data.is_empty())
|
2019-07-08 15:38:44 +02:00
|
|
|
memcpy(shared_buffer->data(), data.characters_without_null_termination(), data.length() + 1);
|
2019-03-18 02:59:08 +01:00
|
|
|
else
|
2019-07-03 21:17:35 +02:00
|
|
|
((u8*)shared_buffer->data())[0] = '\0';
|
2019-03-08 13:33:35 +01:00
|
|
|
shared_buffer->seal();
|
2020-02-02 15:07:41 +01:00
|
|
|
shared_buffer->share_with(WindowServerConnection::the().server_pid());
|
2019-09-14 09:19:05 +02:00
|
|
|
|
2020-02-28 11:45:19 +01:00
|
|
|
WindowServerConnection::the().send_sync<Messages::WindowServer::SetClipboardContents>(shared_buffer->shbuf_id(), data.length(), type);
|
2019-03-08 13:27:19 +01:00
|
|
|
}
|
2019-09-14 09:19:05 +02:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
void Clipboard::did_receive_clipboard_contents_changed(Badge<WindowServerConnection>, const String& data_type)
|
2019-09-14 09:19:05 +02:00
|
|
|
{
|
|
|
|
|
if (on_content_change)
|
|
|
|
|
on_content_change(data_type);
|
|
|
|
|
}
|
2020-02-02 15:07:41 +01:00
|
|
|
|
|
|
|
|
}
|