1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 22:07:35 +00:00

SpiceAgent: Implement setting the user's clipboard contents for text

We also now use GUI::Clipboard for setting the clipboard contents,
instead of using a custom connection to the Clipboard server.
This commit is contained in:
Caoimhe 2023-05-13 14:20:41 +01:00 committed by Andreas Kling
parent 9c4538a9a8
commit 3b6d63f723
8 changed files with 96 additions and 124 deletions

View file

@ -6,21 +6,19 @@
*/
#include "SpiceAgent.h"
#include "ConnectionToClipboardServer.h"
#include <AK/Debug.h>
#include <LibGUI/Clipboard.h>
namespace SpiceAgent {
ErrorOr<NonnullOwnPtr<SpiceAgent>> SpiceAgent::create(StringView device_path)
{
auto device = TRY(Core::File::open(device_path, Core::File::OpenMode::ReadWrite | Core::File::OpenMode::Nonblocking));
auto clipboard_connection = TRY(ConnectionToClipboardServer::try_create());
return try_make<SpiceAgent>(move(device), clipboard_connection, Vector { Capability::ClipboardByDemand });
return try_make<SpiceAgent>(move(device), Vector { Capability::ClipboardByDemand });
}
SpiceAgent::SpiceAgent(NonnullOwnPtr<Core::File> spice_device, ConnectionToClipboardServer& clipboard_connection, Vector<Capability> const& capabilities)
SpiceAgent::SpiceAgent(NonnullOwnPtr<Core::File> spice_device, Vector<Capability> const& capabilities)
: m_spice_device(move(spice_device))
, m_clipboard_connection(clipboard_connection)
, m_capabilities(capabilities)
{
m_notifier = Core::Notifier::construct(
@ -82,6 +80,16 @@ ErrorOr<void> SpiceAgent::on_message_received()
break;
}
case Message::Type::Clipboard: {
auto message = TRY(ClipboardMessage::read_from_stream(stream));
if (message.data_type() == ClipboardDataType::None)
break;
TRY(this->did_receive_clipboard_message(message));
break;
}
// We ignore certain messages to prevent it from clogging up the logs.
case Message::Type::MonitorsConfig:
dbgln_if(SPICE_AGENT_DEBUG, "Ignored message: {}", header);
@ -95,6 +103,21 @@ ErrorOr<void> SpiceAgent::on_message_received()
return {};
}
ErrorOr<void> SpiceAgent::did_receive_clipboard_message(ClipboardMessage& message)
{
dbgln_if(SPICE_AGENT_DEBUG, "Attempting to parse clipboard data of type: {}", message.data_type());
switch (message.data_type()) {
case ClipboardDataType::Text: {
// The default mime_type for set_data is `text/plain`.
GUI::Clipboard::the().set_data(message.contents());
return {};
}
default:
return Error::from_string_literal("Unsupported clipboard data type!");
}
}
ErrorOr<ByteBuffer> SpiceAgent::read_message_buffer()
{
auto port = TRY(m_spice_device->read_value<Port>());