2020-08-02 22:22:19 +00:00
|
|
|
/*
|
|
|
|
KWin - the KDE window manager
|
|
|
|
This file is part of the KDE project.
|
2018-08-21 20:06:42 +00:00
|
|
|
|
2020-08-02 22:22:19 +00:00
|
|
|
SPDX-FileCopyrightText: 2019 Roman Gilg <subdiff@gmail.com>
|
2018-08-21 20:06:42 +00:00
|
|
|
|
2020-08-02 22:22:19 +00:00
|
|
|
SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
2018-08-21 20:06:42 +00:00
|
|
|
#include "selection_source.h"
|
|
|
|
#include "selection.h"
|
|
|
|
#include "transfer.h"
|
|
|
|
|
|
|
|
#include "atoms.h"
|
|
|
|
#include "wayland_server.h"
|
|
|
|
|
|
|
|
#include <KWayland/Client/connection_thread.h>
|
|
|
|
#include <KWayland/Client/datadevicemanager.h>
|
|
|
|
#include <KWayland/Client/datadevice.h>
|
|
|
|
#include <KWayland/Client/datasource.h>
|
|
|
|
|
2020-04-29 15:18:41 +00:00
|
|
|
#include <KWaylandServer/datadevice_interface.h>
|
|
|
|
#include <KWaylandServer/datasource_interface.h>
|
|
|
|
#include <KWaylandServer/seat_interface.h>
|
2018-08-21 20:06:42 +00:00
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <xwayland_logging.h>
|
|
|
|
|
2019-07-02 19:56:03 +00:00
|
|
|
namespace KWin
|
|
|
|
{
|
|
|
|
namespace Xwl
|
|
|
|
{
|
2018-08-21 20:06:42 +00:00
|
|
|
|
2019-07-02 19:56:03 +00:00
|
|
|
SelectionSource::SelectionSource(Selection *selection)
|
|
|
|
: QObject(selection)
|
|
|
|
, m_selection(selection)
|
|
|
|
, m_window(selection->window())
|
2018-08-21 20:06:42 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-05-26 08:55:54 +00:00
|
|
|
WlSource::WlSource(Selection *selection)
|
2019-07-02 19:56:03 +00:00
|
|
|
: SelectionSource(selection)
|
2018-08-21 20:06:42 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-05-26 08:55:54 +00:00
|
|
|
void WlSource::setDataSourceIface(KWaylandServer::AbstractDataSource *dsi)
|
2018-08-21 20:06:42 +00:00
|
|
|
{
|
|
|
|
if (m_dsi == dsi) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (const auto &mime : dsi->mimeTypes()) {
|
|
|
|
m_offers << mime;
|
|
|
|
}
|
2019-07-02 19:56:03 +00:00
|
|
|
m_offerConnection = connect(dsi,
|
2020-04-29 15:18:41 +00:00
|
|
|
&KWaylandServer::DataSourceInterface::mimeTypeOffered,
|
2018-08-21 20:06:42 +00:00
|
|
|
this, &WlSource::receiveOffer);
|
|
|
|
m_dsi = dsi;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WlSource::receiveOffer(const QString &mime)
|
|
|
|
{
|
|
|
|
m_offers << mime;
|
|
|
|
}
|
|
|
|
|
2019-07-02 19:56:03 +00:00
|
|
|
void WlSource::sendSelectionNotify(xcb_selection_request_event_t *event, bool success)
|
2018-08-21 20:06:42 +00:00
|
|
|
{
|
2019-07-02 19:56:03 +00:00
|
|
|
Selection::sendSelectionNotify(event, success);
|
2018-08-21 20:06:42 +00:00
|
|
|
}
|
|
|
|
|
2019-07-02 19:56:03 +00:00
|
|
|
bool WlSource::handleSelectionRequest(xcb_selection_request_event_t *event)
|
2018-08-21 20:06:42 +00:00
|
|
|
{
|
|
|
|
if (event->target == atoms->targets) {
|
|
|
|
sendTargets(event);
|
|
|
|
} else if (event->target == atoms->timestamp) {
|
|
|
|
sendTimestamp(event);
|
|
|
|
} else if (event->target == atoms->delete_atom) {
|
2019-07-02 19:56:03 +00:00
|
|
|
sendSelectionNotify(event, true);
|
2018-08-21 20:06:42 +00:00
|
|
|
} else {
|
|
|
|
// try to send mime data
|
|
|
|
if (!checkStartTransfer(event)) {
|
2019-07-02 19:56:03 +00:00
|
|
|
sendSelectionNotify(event, false);
|
2018-08-21 20:06:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WlSource::sendTargets(xcb_selection_request_event_t *event)
|
|
|
|
{
|
|
|
|
QVector<xcb_atom_t> targets;
|
|
|
|
targets.resize(m_offers.size() + 2);
|
|
|
|
targets[0] = atoms->timestamp;
|
|
|
|
targets[1] = atoms->targets;
|
|
|
|
|
|
|
|
size_t cnt = 2;
|
2021-05-13 16:41:39 +00:00
|
|
|
for (const auto &mime : qAsConst(m_offers)) {
|
2018-08-21 20:06:42 +00:00
|
|
|
targets[cnt] = Selection::mimeTypeToAtom(mime);
|
|
|
|
cnt++;
|
|
|
|
}
|
|
|
|
|
|
|
|
xcb_change_property(kwinApp()->x11Connection(),
|
|
|
|
XCB_PROP_MODE_REPLACE,
|
|
|
|
event->requestor,
|
|
|
|
event->property,
|
|
|
|
XCB_ATOM_ATOM,
|
|
|
|
32, cnt, targets.data());
|
2019-07-02 19:56:03 +00:00
|
|
|
sendSelectionNotify(event, true);
|
2018-08-21 20:06:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WlSource::sendTimestamp(xcb_selection_request_event_t *event)
|
|
|
|
{
|
|
|
|
const xcb_timestamp_t time = timestamp();
|
|
|
|
xcb_change_property(kwinApp()->x11Connection(),
|
|
|
|
XCB_PROP_MODE_REPLACE,
|
|
|
|
event->requestor,
|
|
|
|
event->property,
|
|
|
|
XCB_ATOM_INTEGER,
|
|
|
|
32, 1, &time);
|
|
|
|
|
2019-07-02 19:56:03 +00:00
|
|
|
sendSelectionNotify(event, true);
|
2018-08-21 20:06:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool WlSource::checkStartTransfer(xcb_selection_request_event_t *event)
|
|
|
|
{
|
|
|
|
// check interfaces available
|
2020-05-26 08:55:54 +00:00
|
|
|
if (!m_dsi) {
|
2018-08-21 20:06:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto targets = Selection::atomToMimeTypes(event->target);
|
|
|
|
if (targets.isEmpty()) {
|
|
|
|
qCDebug(KWIN_XWL) << "Unknown selection atom. Ignoring request.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const auto firstTarget = targets[0];
|
|
|
|
|
|
|
|
auto cmp = [firstTarget](const QString &b) {
|
|
|
|
if (firstTarget == "text/uri-list") {
|
|
|
|
// Wayland sources might announce the old mime or the new standard
|
|
|
|
return firstTarget == b || b == "text/x-uri";
|
|
|
|
}
|
|
|
|
return firstTarget == b;
|
|
|
|
};
|
|
|
|
// check supported mimes
|
|
|
|
const auto offers = m_dsi->mimeTypes();
|
|
|
|
const auto mimeIt = std::find_if(offers.begin(), offers.end(), cmp);
|
|
|
|
if (mimeIt == offers.end()) {
|
|
|
|
// Requested Mime not supported. Not sending selection.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int p[2];
|
|
|
|
if (pipe(p) == -1) {
|
|
|
|
qCWarning(KWIN_XWL) << "Pipe failed. Not sending selection.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_dsi->requestData(*mimeIt, p[1]);
|
|
|
|
waylandServer()->dispatch();
|
|
|
|
|
|
|
|
Q_EMIT transferReady(new xcb_selection_request_event_t(*event), p[0]);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-02 19:56:03 +00:00
|
|
|
X11Source::X11Source(Selection *selection, xcb_xfixes_selection_notify_event_t *event)
|
|
|
|
: SelectionSource(selection)
|
|
|
|
, m_owner(event->owner)
|
2018-08-21 20:06:42 +00:00
|
|
|
{
|
|
|
|
setTimestamp(event->timestamp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void X11Source::getTargets()
|
|
|
|
{
|
2019-07-02 19:56:03 +00:00
|
|
|
xcb_connection_t *xcbConn = kwinApp()->x11Connection();
|
2018-08-21 20:06:42 +00:00
|
|
|
/* will lead to a selection request event for the new owner */
|
|
|
|
xcb_convert_selection(xcbConn,
|
2018-08-22 12:56:48 +00:00
|
|
|
window(),
|
2018-08-21 20:06:42 +00:00
|
|
|
selection()->atom(),
|
|
|
|
atoms->targets,
|
|
|
|
atoms->wl_selection,
|
|
|
|
timestamp());
|
|
|
|
xcb_flush(xcbConn);
|
|
|
|
}
|
|
|
|
|
|
|
|
using Mime = QPair<QString, xcb_atom_t>;
|
|
|
|
|
|
|
|
void X11Source::handleTargets()
|
|
|
|
{
|
|
|
|
// receive targets
|
2019-07-02 19:56:03 +00:00
|
|
|
xcb_connection_t *xcbConn = kwinApp()->x11Connection();
|
2018-08-21 20:06:42 +00:00
|
|
|
xcb_get_property_cookie_t cookie = xcb_get_property(xcbConn,
|
|
|
|
1,
|
2018-08-22 12:56:48 +00:00
|
|
|
window(),
|
2018-08-21 20:06:42 +00:00
|
|
|
atoms->wl_selection,
|
|
|
|
XCB_GET_PROPERTY_TYPE_ANY,
|
|
|
|
0,
|
|
|
|
4096
|
|
|
|
);
|
2019-07-02 19:56:03 +00:00
|
|
|
auto *reply = xcb_get_property_reply(xcbConn, cookie, nullptr);
|
|
|
|
if (!reply) {
|
2020-10-27 17:20:21 +00:00
|
|
|
qCDebug(KWIN_XWL) << "Failed to get selection property";
|
2018-08-21 20:06:42 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (reply->type != XCB_ATOM_ATOM) {
|
2020-10-27 17:20:21 +00:00
|
|
|
qCDebug(KWIN_XWL) << "Wrong reply type";
|
2018-08-21 20:06:42 +00:00
|
|
|
free(reply);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-02 19:56:03 +00:00
|
|
|
QStringList added;
|
|
|
|
QStringList removed;
|
|
|
|
|
2018-08-21 20:06:42 +00:00
|
|
|
Mimes all;
|
2019-07-02 19:56:03 +00:00
|
|
|
xcb_atom_t *value = static_cast<xcb_atom_t *>(xcb_get_property_value(reply));
|
2018-08-21 20:06:42 +00:00
|
|
|
for (uint32_t i = 0; i < reply->value_len; i++) {
|
|
|
|
if (value[i] == XCB_ATOM_NONE) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto mimeStrings = Selection::atomToMimeTypes(value[i]);
|
|
|
|
if (mimeStrings.isEmpty()) {
|
|
|
|
// TODO: this should never happen? assert?
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const auto mimeIt = std::find_if(m_offers.begin(), m_offers.end(),
|
2019-07-02 19:56:03 +00:00
|
|
|
[value, i](const Mime &mime) {
|
|
|
|
return mime.second == value[i];
|
|
|
|
}
|
|
|
|
);
|
2018-08-21 20:06:42 +00:00
|
|
|
|
|
|
|
auto mimePair = Mime(mimeStrings[0], value[i]);
|
|
|
|
if (mimeIt == m_offers.end()) {
|
2019-07-02 19:56:03 +00:00
|
|
|
added << mimePair.first;
|
2018-08-21 20:06:42 +00:00
|
|
|
} else {
|
|
|
|
m_offers.removeAll(mimePair);
|
|
|
|
}
|
|
|
|
all << mimePair;
|
|
|
|
}
|
|
|
|
// all left in m_offers are not in the updated targets
|
2021-05-13 16:41:39 +00:00
|
|
|
for (const auto &mimePair : qAsConst(m_offers)) {
|
2019-07-02 19:56:03 +00:00
|
|
|
removed << mimePair.first;
|
2018-08-21 20:06:42 +00:00
|
|
|
}
|
|
|
|
m_offers = all;
|
|
|
|
|
2019-07-02 19:56:03 +00:00
|
|
|
if (!added.isEmpty() || !removed.isEmpty()) {
|
|
|
|
Q_EMIT offersChanged(added, removed);
|
2018-08-21 20:06:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
free(reply);
|
|
|
|
}
|
|
|
|
|
2019-07-02 19:56:03 +00:00
|
|
|
void X11Source::setDataSource(KWayland::Client::DataSource *dataSource)
|
2018-08-21 20:06:42 +00:00
|
|
|
{
|
2019-07-02 19:56:03 +00:00
|
|
|
Q_ASSERT(dataSource);
|
|
|
|
if (m_dataSource) {
|
|
|
|
delete m_dataSource;
|
2018-08-21 20:06:42 +00:00
|
|
|
}
|
2019-07-02 19:56:03 +00:00
|
|
|
|
|
|
|
m_dataSource = dataSource;
|
|
|
|
|
2021-05-13 16:41:39 +00:00
|
|
|
for (const Mime &offer : qAsConst(m_offers)) {
|
2019-07-02 19:56:03 +00:00
|
|
|
dataSource->offer(offer.first);
|
|
|
|
}
|
|
|
|
|
|
|
|
connect(dataSource, &KWayland::Client::DataSource::sendDataRequested,
|
|
|
|
this, &X11Source::startTransfer);
|
2018-08-21 20:06:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void X11Source::setOffers(const Mimes &offers)
|
|
|
|
{
|
|
|
|
// TODO: share code with handleTargets and emit signals accordingly?
|
|
|
|
m_offers = offers;
|
|
|
|
}
|
|
|
|
|
2019-07-02 19:56:03 +00:00
|
|
|
bool X11Source::handleSelectionNotify(xcb_selection_notify_event_t *event)
|
2018-08-21 20:06:42 +00:00
|
|
|
{
|
2018-08-22 12:56:48 +00:00
|
|
|
if (event->requestor != window()) {
|
2018-08-21 20:06:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (event->selection != selection()->atom()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (event->property == XCB_ATOM_NONE) {
|
|
|
|
qCWarning(KWIN_XWL) << "Incoming X selection conversion failed";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (event->target == atoms->targets) {
|
|
|
|
handleTargets();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void X11Source::startTransfer(const QString &mimeName, qint32 fd)
|
|
|
|
{
|
|
|
|
const auto mimeIt = std::find_if(m_offers.begin(), m_offers.end(),
|
2019-07-02 19:56:03 +00:00
|
|
|
[mimeName](const Mime &mime) {
|
|
|
|
return mime.first == mimeName;
|
|
|
|
}
|
|
|
|
);
|
2018-08-21 20:06:42 +00:00
|
|
|
if (mimeIt == m_offers.end()) {
|
|
|
|
qCDebug(KWIN_XWL) << "Sending X11 clipboard to Wayland failed: unsupported MIME.";
|
|
|
|
close(fd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Q_EMIT transferReady((*mimeIt).second, fd);
|
|
|
|
}
|
|
|
|
|
2019-07-02 19:56:03 +00:00
|
|
|
} // namespace Xwl
|
|
|
|
} // namespace KWin
|