2020-01-01 18:53:34 +01:00
|
|
|
#include <AK/SharedBuffer.h>
|
2019-03-08 13:27:19 +01:00
|
|
|
#include <LibGUI/GClipboard.h>
|
2019-11-08 11:39:45 +01:00
|
|
|
#include <LibGUI/GWindowServerConnection.h>
|
2019-03-08 13:27:19 +01:00
|
|
|
|
2019-04-05 05:10:18 +02:00
|
|
|
GClipboard& GClipboard::the()
|
|
|
|
{
|
|
|
|
static GClipboard* s_the;
|
|
|
|
if (!s_the)
|
|
|
|
s_the = new GClipboard;
|
|
|
|
return *s_the;
|
|
|
|
}
|
|
|
|
|
|
|
|
GClipboard::GClipboard()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-09-14 09:19:05 +02:00
|
|
|
GClipboard::DataAndType GClipboard::data_and_type() const
|
2019-03-08 13:27:19 +01:00
|
|
|
{
|
2019-12-02 09:33:37 +01:00
|
|
|
auto response = GWindowServerConnection::the().send_sync<WindowServer::GetClipboardContents>();
|
|
|
|
if (response->shared_buffer_id() < 0)
|
2019-06-07 11:46:02 +02:00
|
|
|
return {};
|
2019-12-02 09:33:37 +01:00
|
|
|
auto shared_buffer = SharedBuffer::create_from_shared_buffer_id(response->shared_buffer_id());
|
2019-03-08 13:27:19 +01:00
|
|
|
if (!shared_buffer) {
|
|
|
|
dbgprintf("GClipboard::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()) {
|
2019-03-08 13:27:19 +01:00
|
|
|
dbgprintf("GClipboard::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
|
|
|
}
|
|
|
|
|
2019-09-14 09:19:05 +02:00
|
|
|
void GClipboard::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) {
|
|
|
|
dbgprintf("GClipboard::set_data() failed to create a shared buffer\n");
|
|
|
|
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();
|
2019-07-18 09:52:22 +02:00
|
|
|
shared_buffer->share_with(GWindowServerConnection::the().server_pid());
|
2019-09-14 09:19:05 +02:00
|
|
|
|
2019-12-02 09:33:37 +01:00
|
|
|
GWindowServerConnection::the().send_sync<WindowServer::SetClipboardContents>(shared_buffer->shared_buffer_id(), data.length(), type);
|
2019-03-08 13:27:19 +01:00
|
|
|
}
|
2019-09-14 09:19:05 +02:00
|
|
|
|
|
|
|
void GClipboard::did_receive_clipboard_contents_changed(Badge<GWindowServerConnection>, const String& data_type)
|
|
|
|
{
|
|
|
|
if (on_content_change)
|
|
|
|
on_content_change(data_type);
|
|
|
|
}
|