2019-06-07 11:46:02 +02:00
|
|
|
#include <LibC/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
|
|
|
#include <WindowServer/WSAPITypes.h>
|
|
|
|
|
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
|
|
|
{
|
|
|
|
WSAPI_ClientMessage request;
|
|
|
|
request.type = WSAPI_ClientMessage::Type::GetClipboardContents;
|
2019-07-17 20:57:27 +02:00
|
|
|
auto response = GWindowServerConnection::the().sync_request(request, WSAPI_ServerMessage::Type::DidGetClipboardContents);
|
2019-03-08 13:27:19 +01:00
|
|
|
if (response.clipboard.shared_buffer_id < 0)
|
2019-06-07 11:46:02 +02:00
|
|
|
return {};
|
2019-03-08 13:27:19 +01:00
|
|
|
auto shared_buffer = SharedBuffer::create_from_shared_buffer_id(response.clipboard.shared_buffer_id);
|
|
|
|
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
|
|
|
}
|
|
|
|
if (response.clipboard.contents_size > shared_buffer->size()) {
|
|
|
|
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-09-14 09:19:05 +02:00
|
|
|
auto data = String((const char*)shared_buffer->data(), response.clipboard.contents_size);
|
|
|
|
auto type = String(response.text, response.text_length);
|
|
|
|
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
|
|
|
{
|
|
|
|
WSAPI_ClientMessage request;
|
|
|
|
request.type = WSAPI_ClientMessage::Type::SetClipboardContents;
|
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-03-08 13:27:19 +01:00
|
|
|
request.clipboard.shared_buffer_id = shared_buffer->shared_buffer_id();
|
|
|
|
request.clipboard.contents_size = data.length();
|
2019-09-14 09:19:05 +02:00
|
|
|
|
|
|
|
ASSERT(type.length() < (ssize_t)sizeof(request.text));
|
|
|
|
if (!type.is_null())
|
|
|
|
strcpy(request.text, type.characters());
|
|
|
|
request.text_length = type.length();
|
|
|
|
|
2019-07-17 20:57:27 +02:00
|
|
|
auto response = GWindowServerConnection::the().sync_request(request, WSAPI_ServerMessage::Type::DidSetClipboardContents);
|
2019-03-08 13:27:19 +01:00
|
|
|
ASSERT(response.clipboard.shared_buffer_id == shared_buffer->shared_buffer_id());
|
|
|
|
}
|
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);
|
|
|
|
}
|