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.h"
|
|
|
|
#include "databridge.h"
|
|
|
|
#include "selection_source.h"
|
|
|
|
#include "transfer.h"
|
|
|
|
|
|
|
|
#include "atoms.h"
|
2019-07-02 19:56:03 +00:00
|
|
|
#include "workspace.h"
|
2020-08-06 07:46:01 +00:00
|
|
|
#include "x11client.h"
|
|
|
|
#include "xcbutils.h"
|
2018-08-21 20:06:42 +00:00
|
|
|
|
|
|
|
#include <xcb/xcb_event.h>
|
|
|
|
#include <xcb/xfixes.h>
|
|
|
|
|
|
|
|
#include <QTimer>
|
|
|
|
|
2019-07-02 19:56:03 +00:00
|
|
|
namespace KWin
|
|
|
|
{
|
|
|
|
namespace Xwl
|
|
|
|
{
|
2018-08-21 20:06:42 +00:00
|
|
|
|
|
|
|
xcb_atom_t Selection::mimeTypeToAtom(const QString &mimeType)
|
|
|
|
{
|
|
|
|
if (mimeType == QLatin1String("text/plain;charset=utf-8")) {
|
|
|
|
return atoms->utf8_string;
|
2019-07-02 19:56:03 +00:00
|
|
|
}
|
|
|
|
if (mimeType == QLatin1String("text/plain")) {
|
2018-08-21 20:06:42 +00:00
|
|
|
return atoms->text;
|
2019-07-02 19:56:03 +00:00
|
|
|
}
|
|
|
|
if (mimeType == QLatin1String("text/x-uri")) {
|
2018-08-21 20:06:42 +00:00
|
|
|
return atoms->uri_list;
|
|
|
|
}
|
|
|
|
return mimeTypeToAtomLiteral(mimeType);
|
|
|
|
}
|
|
|
|
|
|
|
|
xcb_atom_t Selection::mimeTypeToAtomLiteral(const QString &mimeType)
|
|
|
|
{
|
|
|
|
return Xcb::Atom(mimeType.toLatin1(), false, kwinApp()->x11Connection());
|
|
|
|
}
|
|
|
|
|
2018-08-24 21:05:35 +00:00
|
|
|
QString Selection::atomName(xcb_atom_t atom)
|
|
|
|
{
|
2019-07-02 19:56:03 +00:00
|
|
|
xcb_connection_t *xcbConn = kwinApp()->x11Connection();
|
2018-08-24 21:05:35 +00:00
|
|
|
xcb_get_atom_name_cookie_t nameCookie = xcb_get_atom_name(xcbConn, atom);
|
2019-07-02 19:56:03 +00:00
|
|
|
xcb_get_atom_name_reply_t *nameReply = xcb_get_atom_name_reply(xcbConn, nameCookie, nullptr);
|
|
|
|
if (!nameReply) {
|
2018-08-24 21:05:35 +00:00
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
2019-07-02 19:56:03 +00:00
|
|
|
const size_t length = xcb_get_atom_name_name_length(nameReply);
|
|
|
|
QString name = QString::fromLatin1(xcb_get_atom_name_name(nameReply), length);
|
2018-08-24 21:05:35 +00:00
|
|
|
free(nameReply);
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2018-08-21 20:06:42 +00:00
|
|
|
QStringList Selection::atomToMimeTypes(xcb_atom_t atom)
|
|
|
|
{
|
|
|
|
QStringList mimeTypes;
|
|
|
|
|
|
|
|
if (atom == atoms->utf8_string) {
|
|
|
|
mimeTypes << QString::fromLatin1("text/plain;charset=utf-8");
|
|
|
|
} else if (atom == atoms->text) {
|
|
|
|
mimeTypes << QString::fromLatin1("text/plain");
|
|
|
|
} else if (atom == atoms->uri_list) {
|
|
|
|
mimeTypes << "text/uri-list" << "text/x-uri";
|
|
|
|
} else {
|
2018-08-24 21:05:35 +00:00
|
|
|
mimeTypes << atomName(atom);
|
2018-08-21 20:06:42 +00:00
|
|
|
}
|
|
|
|
return mimeTypes;
|
|
|
|
}
|
|
|
|
|
|
|
|
Selection::Selection(xcb_atom_t atom, QObject *parent)
|
2019-07-02 19:56:03 +00:00
|
|
|
: QObject(parent)
|
|
|
|
, m_atom(atom)
|
2018-08-21 20:06:42 +00:00
|
|
|
{
|
2019-07-02 19:56:03 +00:00
|
|
|
xcb_connection_t *xcbConn = kwinApp()->x11Connection();
|
2018-08-21 20:06:42 +00:00
|
|
|
m_window = xcb_generate_id(kwinApp()->x11Connection());
|
2018-08-22 12:56:48 +00:00
|
|
|
m_requestorWindow = m_window;
|
2018-08-21 20:06:42 +00:00
|
|
|
xcb_flush(xcbConn);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Selection::handleXfixesNotify(xcb_xfixes_selection_notify_event_t *event)
|
|
|
|
{
|
|
|
|
if (event->window != m_window) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (event->selection != m_atom) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (m_disownPending) {
|
|
|
|
// notify of our own disown - ignore it
|
|
|
|
m_disownPending = false;
|
|
|
|
return true;
|
|
|
|
}
|
2019-07-02 19:56:03 +00:00
|
|
|
if (event->owner == m_window && m_waylandSource) {
|
2018-08-21 20:06:42 +00:00
|
|
|
// When we claim a selection we must use XCB_TIME_CURRENT,
|
|
|
|
// grab the actual timestamp here to answer TIMESTAMP requests
|
|
|
|
// correctly
|
2019-07-02 19:56:03 +00:00
|
|
|
m_waylandSource->setTimestamp(event->timestamp);
|
2018-08-21 20:06:42 +00:00
|
|
|
m_timestamp = event->timestamp;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Being here means some other X window has claimed the selection.
|
|
|
|
doHandleXfixesNotify(event);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Selection::filterEvent(xcb_generic_event_t *event)
|
|
|
|
{
|
|
|
|
switch (event->response_type & XCB_EVENT_RESPONSE_TYPE_MASK) {
|
|
|
|
case XCB_SELECTION_NOTIFY:
|
2020-07-29 12:03:49 +00:00
|
|
|
return handleSelectionNotify(reinterpret_cast<xcb_selection_notify_event_t *>(event));
|
2018-08-21 20:06:42 +00:00
|
|
|
case XCB_PROPERTY_NOTIFY:
|
2020-07-29 12:03:49 +00:00
|
|
|
return handlePropertyNotify(reinterpret_cast<xcb_property_notify_event_t *>(event));
|
2018-08-21 20:06:42 +00:00
|
|
|
case XCB_SELECTION_REQUEST:
|
2020-07-29 12:03:49 +00:00
|
|
|
return handleSelectionRequest(reinterpret_cast<xcb_selection_request_event_t *>(event));
|
2018-08-21 20:06:42 +00:00
|
|
|
case XCB_CLIENT_MESSAGE:
|
2020-07-29 12:03:49 +00:00
|
|
|
return handleClientMessage(reinterpret_cast<xcb_client_message_event_t *>(event));
|
2018-08-21 20:06:42 +00:00
|
|
|
default:
|
2020-08-06 07:46:01 +00:00
|
|
|
if (event->response_type == Xcb::Extensions::self()->fixesSelectionNotifyEvent()) {
|
|
|
|
return handleXfixesNotify(reinterpret_cast<xcb_xfixes_selection_notify_event_t *>(event));
|
|
|
|
}
|
2018-08-21 20:06:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-02 19:56:03 +00:00
|
|
|
void Selection::sendSelectionNotify(xcb_selection_request_event_t *event, bool success)
|
2018-08-21 20:06:42 +00:00
|
|
|
{
|
|
|
|
xcb_selection_notify_event_t notify;
|
|
|
|
notify.response_type = XCB_SELECTION_NOTIFY;
|
|
|
|
notify.sequence = 0;
|
|
|
|
notify.time = event->time;
|
|
|
|
notify.requestor = event->requestor;
|
|
|
|
notify.selection = event->selection;
|
|
|
|
notify.target = event->target;
|
2019-07-02 19:56:03 +00:00
|
|
|
notify.property = success ? event->property : xcb_atom_t(XCB_ATOM_NONE);
|
2018-08-21 20:06:42 +00:00
|
|
|
|
2019-07-02 19:56:03 +00:00
|
|
|
xcb_connection_t *xcbConn = kwinApp()->x11Connection();
|
2018-08-21 20:06:42 +00:00
|
|
|
xcb_send_event(xcbConn,
|
|
|
|
0,
|
|
|
|
event->requestor,
|
|
|
|
XCB_EVENT_MASK_NO_EVENT,
|
|
|
|
(const char *)¬ify);
|
|
|
|
xcb_flush(xcbConn);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Selection::registerXfixes()
|
|
|
|
{
|
2019-07-02 19:56:03 +00:00
|
|
|
xcb_connection_t *xcbConn = kwinApp()->x11Connection();
|
2018-08-21 20:06:42 +00:00
|
|
|
const uint32_t mask = XCB_XFIXES_SELECTION_EVENT_MASK_SET_SELECTION_OWNER |
|
|
|
|
XCB_XFIXES_SELECTION_EVENT_MASK_SELECTION_WINDOW_DESTROY |
|
|
|
|
XCB_XFIXES_SELECTION_EVENT_MASK_SELECTION_CLIENT_CLOSE;
|
2020-10-20 00:14:18 +00:00
|
|
|
xcb_xfixes_select_selection_input(xcbConn,
|
2018-08-21 20:06:42 +00:00
|
|
|
m_window,
|
|
|
|
m_atom,
|
|
|
|
mask);
|
|
|
|
xcb_flush(xcbConn);
|
|
|
|
}
|
|
|
|
|
2019-07-02 19:56:03 +00:00
|
|
|
void Selection::setWlSource(WlSource *source)
|
2018-08-21 20:06:42 +00:00
|
|
|
{
|
2020-10-28 17:47:45 +00:00
|
|
|
if (m_waylandSource) {
|
|
|
|
m_waylandSource->deleteLater();
|
|
|
|
m_waylandSource = nullptr;
|
|
|
|
}
|
2019-07-02 19:56:03 +00:00
|
|
|
delete m_xSource;
|
|
|
|
m_xSource = nullptr;
|
|
|
|
if (source) {
|
|
|
|
m_waylandSource = source;
|
|
|
|
connect(source, &WlSource::transferReady, this, &Selection::startTransferToX);
|
2018-08-21 20:06:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Selection::createX11Source(xcb_xfixes_selection_notify_event_t *event)
|
|
|
|
{
|
2020-10-28 17:47:45 +00:00
|
|
|
setWlSource(nullptr);
|
2018-08-22 12:56:48 +00:00
|
|
|
if (!event || event->owner == XCB_WINDOW_NONE) {
|
2018-08-21 20:06:42 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-07-02 19:56:03 +00:00
|
|
|
m_xSource = new X11Source(this, event);
|
2018-08-21 20:06:42 +00:00
|
|
|
|
2019-07-02 19:56:03 +00:00
|
|
|
connect(m_xSource, &X11Source::offersChanged, this, &Selection::x11OffersChanged);
|
|
|
|
connect(m_xSource, &X11Source::transferReady, this, &Selection::startTransferToWayland);
|
2018-08-21 20:06:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Selection::ownSelection(bool own)
|
|
|
|
{
|
2019-07-02 19:56:03 +00:00
|
|
|
xcb_connection_t *xcbConn = kwinApp()->x11Connection();
|
2018-08-21 20:06:42 +00:00
|
|
|
if (own) {
|
|
|
|
xcb_set_selection_owner(xcbConn,
|
|
|
|
m_window,
|
|
|
|
m_atom,
|
|
|
|
XCB_TIME_CURRENT_TIME);
|
|
|
|
} else {
|
|
|
|
m_disownPending = true;
|
|
|
|
xcb_set_selection_owner(xcbConn,
|
|
|
|
XCB_WINDOW_NONE,
|
|
|
|
m_atom,
|
|
|
|
m_timestamp);
|
|
|
|
}
|
|
|
|
xcb_flush(xcbConn);
|
|
|
|
}
|
|
|
|
|
2018-08-22 12:56:48 +00:00
|
|
|
void Selection::overwriteRequestorWindow(xcb_window_t window)
|
|
|
|
{
|
2019-07-02 19:56:03 +00:00
|
|
|
Q_ASSERT(m_xSource);
|
2018-08-22 12:56:48 +00:00
|
|
|
if (window == XCB_WINDOW_NONE) {
|
|
|
|
// reset
|
|
|
|
window = m_window;
|
|
|
|
}
|
|
|
|
m_requestorWindow = window;
|
2019-07-02 19:56:03 +00:00
|
|
|
m_xSource->setRequestor(window);
|
2018-08-22 12:56:48 +00:00
|
|
|
}
|
|
|
|
|
2019-07-02 19:56:03 +00:00
|
|
|
bool Selection::handleSelectionRequest(xcb_selection_request_event_t *event)
|
2018-08-21 20:06:42 +00:00
|
|
|
{
|
|
|
|
if (event->selection != m_atom) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-09-24 08:48:08 +00:00
|
|
|
if (qobject_cast<X11Client *>(workspace()->activeClient()) == nullptr) {
|
2018-08-21 20:06:42 +00:00
|
|
|
// Receiving Wayland selection not allowed when no Xwayland surface active
|
|
|
|
// filter the event, but don't act upon it
|
2019-07-02 19:56:03 +00:00
|
|
|
sendSelectionNotify(event, false);
|
2018-08-21 20:06:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-02 19:56:03 +00:00
|
|
|
if (m_window != event->owner || !m_waylandSource) {
|
2018-08-21 20:06:42 +00:00
|
|
|
if (event->time < m_timestamp) {
|
|
|
|
// cancel earlier attempts at receiving a selection
|
|
|
|
// TODO: is this for sure without problems?
|
2019-07-02 19:56:03 +00:00
|
|
|
sendSelectionNotify(event, false);
|
2018-08-21 20:06:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2019-07-02 19:56:03 +00:00
|
|
|
return m_waylandSource->handleSelectionRequest(event);
|
2018-08-21 20:06:42 +00:00
|
|
|
}
|
|
|
|
|
2019-07-02 19:56:03 +00:00
|
|
|
bool Selection::handleSelectionNotify(xcb_selection_notify_event_t *event)
|
2018-08-21 20:06:42 +00:00
|
|
|
{
|
2019-07-02 19:56:03 +00:00
|
|
|
if (m_xSource && m_xSource->handleSelectionNotify(event)) {
|
2018-08-21 20:06:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
2021-05-13 16:41:39 +00:00
|
|
|
for (TransferXtoWl *transfer : qAsConst(m_xToWlTransfers)) {
|
2019-07-02 19:56:03 +00:00
|
|
|
if (transfer->handleSelectionNotify(event)) {
|
2018-08-21 20:06:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-07-02 19:56:03 +00:00
|
|
|
bool Selection::handlePropertyNotify(xcb_property_notify_event_t *event)
|
2018-08-21 20:06:42 +00:00
|
|
|
{
|
2021-05-13 16:41:39 +00:00
|
|
|
for (TransferXtoWl *transfer : qAsConst(m_xToWlTransfers)) {
|
2019-07-02 19:56:03 +00:00
|
|
|
if (transfer->handlePropertyNotify(event)) {
|
2018-08-21 20:06:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2021-05-13 16:41:39 +00:00
|
|
|
for (TransferWltoX *transfer : qAsConst(m_wlToXTransfers)) {
|
2019-07-02 19:56:03 +00:00
|
|
|
if (transfer->handlePropertyNotify(event)) {
|
2018-08-21 20:06:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Selection::startTransferToWayland(xcb_atom_t target, qint32 fd)
|
|
|
|
{
|
|
|
|
// create new x to wl data transfer object
|
2019-07-02 19:56:03 +00:00
|
|
|
auto *transfer = new TransferXtoWl(m_atom, target, fd, m_xSource->timestamp(), m_requestorWindow, this);
|
2018-08-21 20:06:42 +00:00
|
|
|
m_xToWlTransfers << transfer;
|
|
|
|
|
|
|
|
connect(transfer, &TransferXtoWl::finished, this, [this, transfer]() {
|
|
|
|
Q_EMIT transferFinished(transfer->timestamp());
|
2021-02-05 17:40:19 +00:00
|
|
|
transfer->deleteLater();
|
2018-08-21 20:06:42 +00:00
|
|
|
m_xToWlTransfers.removeOne(transfer);
|
|
|
|
endTimeoutTransfersTimer();
|
|
|
|
});
|
|
|
|
startTimeoutTransfersTimer();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Selection::startTransferToX(xcb_selection_request_event_t *event, qint32 fd)
|
|
|
|
{
|
|
|
|
// create new wl to x data transfer object
|
|
|
|
auto *transfer = new TransferWltoX(m_atom, event, fd, this);
|
|
|
|
|
2019-07-02 19:56:03 +00:00
|
|
|
connect(transfer, &TransferWltoX::selectionNotify, this, &Selection::sendSelectionNotify);
|
2018-08-21 20:06:42 +00:00
|
|
|
connect(transfer, &TransferWltoX::finished, this, [this, transfer]() {
|
|
|
|
Q_EMIT transferFinished(transfer->timestamp());
|
|
|
|
|
|
|
|
// TODO: serialize? see comment below.
|
|
|
|
// const bool wasActive = (transfer == m_wlToXTransfers[0]);
|
2021-02-05 17:40:19 +00:00
|
|
|
transfer->deleteLater();
|
2018-08-21 20:06:42 +00:00
|
|
|
m_wlToXTransfers.removeOne(transfer);
|
|
|
|
endTimeoutTransfersTimer();
|
|
|
|
// if (wasActive && !m_wlToXTransfers.isEmpty()) {
|
|
|
|
// m_wlToXTransfers[0]->startTransferFromSource();
|
|
|
|
// }
|
|
|
|
});
|
|
|
|
|
|
|
|
// add it to list of queued transfers
|
|
|
|
m_wlToXTransfers.append(transfer);
|
|
|
|
|
|
|
|
// TODO: Do we need to serialize the transfers, or can we do
|
|
|
|
// them in parallel as we do it right now?
|
|
|
|
transfer->startTransferFromSource();
|
|
|
|
// if (m_wlToXTransfers.size() == 1) {
|
|
|
|
// transfer->startTransferFromSource();
|
|
|
|
// }
|
|
|
|
startTimeoutTransfersTimer();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Selection::startTimeoutTransfersTimer()
|
|
|
|
{
|
|
|
|
if (m_timeoutTransfers) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_timeoutTransfers = new QTimer(this);
|
|
|
|
connect(m_timeoutTransfers, &QTimer::timeout, this, &Selection::timeoutTransfers);
|
|
|
|
m_timeoutTransfers->start(5000);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Selection::endTimeoutTransfersTimer()
|
|
|
|
{
|
|
|
|
if (m_xToWlTransfers.isEmpty() && m_wlToXTransfers.isEmpty()) {
|
|
|
|
delete m_timeoutTransfers;
|
|
|
|
m_timeoutTransfers = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Selection::timeoutTransfers()
|
|
|
|
{
|
2021-05-13 16:41:39 +00:00
|
|
|
for (TransferXtoWl *transfer : qAsConst(m_xToWlTransfers)) {
|
2018-08-21 20:06:42 +00:00
|
|
|
transfer->timeout();
|
|
|
|
}
|
2021-05-13 16:41:39 +00:00
|
|
|
for (TransferWltoX *transfer : qAsConst(m_wlToXTransfers)) {
|
2018-08-21 20:06:42 +00:00
|
|
|
transfer->timeout();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-02 19:56:03 +00:00
|
|
|
} // namespace Xwl
|
|
|
|
} // namespace KWin
|