2025-06-03 01:28:01 +00:00
|
|
|
/*
|
|
|
|
|
* Deskflow -- mouse and keyboard sharing utility
|
|
|
|
|
* SPDX-FileCopyrightText: (C) 2025 Deskflow Developers
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
|
|
|
|
|
*/
|
|
|
|
|
|
2025-11-17 13:20:48 +00:00
|
|
|
#include "platform/WlClipboardCollection.h"
|
2025-06-03 01:28:01 +00:00
|
|
|
|
|
|
|
|
#include "base/Log.h"
|
|
|
|
|
#include "deskflow/ClipboardTypes.h"
|
|
|
|
|
|
|
|
|
|
namespace deskflow {
|
|
|
|
|
|
2025-11-17 13:20:48 +00:00
|
|
|
WlClipboardCollection::WlClipboardCollection()
|
2025-06-03 01:28:01 +00:00
|
|
|
{
|
|
|
|
|
initialize();
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-17 13:20:48 +00:00
|
|
|
WlClipboardCollection::~WlClipboardCollection()
|
2025-06-03 01:28:01 +00:00
|
|
|
{
|
|
|
|
|
cleanup();
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-17 13:20:48 +00:00
|
|
|
bool WlClipboardCollection::isAvailable() const
|
2025-06-03 01:28:01 +00:00
|
|
|
{
|
|
|
|
|
return m_available;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-17 13:20:48 +00:00
|
|
|
IClipboard *WlClipboardCollection::getClipboard(ClipboardID id) const
|
2025-06-03 01:28:01 +00:00
|
|
|
{
|
|
|
|
|
if (!m_available || id >= m_clipboards.size()) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return m_clipboards[id].get();
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-17 13:20:48 +00:00
|
|
|
bool WlClipboardCollection::hasChanged() const
|
2025-06-03 01:28:01 +00:00
|
|
|
{
|
|
|
|
|
if (!m_available) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const auto &clipboard : m_clipboards) {
|
|
|
|
|
if (clipboard && clipboard->hasChanged()) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-17 13:20:48 +00:00
|
|
|
void WlClipboardCollection::startMonitoring()
|
2025-06-03 01:28:01 +00:00
|
|
|
{
|
|
|
|
|
if (!m_available || m_monitoring) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-16 17:04:40 +00:00
|
|
|
for (const auto &clipboard : m_clipboards) {
|
2025-06-03 01:28:01 +00:00
|
|
|
if (clipboard) {
|
|
|
|
|
clipboard->startMonitoring();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_monitoring = true;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-17 13:20:48 +00:00
|
|
|
void WlClipboardCollection::stopMonitoring()
|
2025-06-03 01:28:01 +00:00
|
|
|
{
|
|
|
|
|
if (!m_available || !m_monitoring) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-16 17:04:40 +00:00
|
|
|
for (const auto &clipboard : m_clipboards) {
|
2025-06-03 01:28:01 +00:00
|
|
|
if (clipboard) {
|
|
|
|
|
clipboard->stopMonitoring();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_monitoring = false;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-16 17:04:40 +00:00
|
|
|
void WlClipboardCollection::resetChanged() const
|
2025-06-03 01:28:01 +00:00
|
|
|
{
|
|
|
|
|
if (!m_available) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-16 17:04:40 +00:00
|
|
|
for (const auto &clipboard : m_clipboards) {
|
2025-06-03 01:28:01 +00:00
|
|
|
if (clipboard) {
|
|
|
|
|
clipboard->resetChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-17 13:20:48 +00:00
|
|
|
void WlClipboardCollection::initialize()
|
2025-06-03 01:28:01 +00:00
|
|
|
{
|
2025-11-16 13:08:28 +00:00
|
|
|
if (!WlClipboard::isEnabled()) {
|
|
|
|
|
LOG_DEBUG("wl-clipboard setting disabled");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-17 13:10:17 +00:00
|
|
|
if (!WlClipboard::isAvailable()) {
|
2025-06-03 01:28:01 +00:00
|
|
|
LOG_WARN("wl-clipboard tools not found, clipboard functionality disabled");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create clipboard instances for each clipboard type
|
|
|
|
|
m_clipboards.resize(kClipboardEnd);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// Primary clipboard (selection)
|
2025-11-17 13:10:17 +00:00
|
|
|
m_clipboards[kClipboardSelection] = std::make_unique<WlClipboard>(kClipboardSelection);
|
2025-06-03 01:28:01 +00:00
|
|
|
|
|
|
|
|
// Standard clipboard
|
2025-11-17 13:10:17 +00:00
|
|
|
m_clipboards[kClipboardClipboard] = std::make_unique<WlClipboard>(kClipboardClipboard);
|
2025-06-03 01:28:01 +00:00
|
|
|
|
|
|
|
|
m_available = true;
|
2026-05-09 01:10:59 +00:00
|
|
|
LOG_VERBOSE("initialized Wayland clipboard support");
|
2025-06-03 01:28:01 +00:00
|
|
|
|
|
|
|
|
} catch (const std::exception &e) {
|
|
|
|
|
LOG_ERR("failed to initialize clipboard: %s", e.what());
|
|
|
|
|
cleanup();
|
|
|
|
|
m_available = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-17 13:20:48 +00:00
|
|
|
void WlClipboardCollection::cleanup()
|
2025-06-03 01:28:01 +00:00
|
|
|
{
|
|
|
|
|
if (m_monitoring) {
|
|
|
|
|
stopMonitoring();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_clipboards.clear();
|
|
|
|
|
m_available = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace deskflow
|