2020-08-02 22:22:19 +00:00
|
|
|
/*
|
|
|
|
KWin - the KDE window manager
|
|
|
|
This file is part of the KDE project.
|
2018-08-22 12:56:48 +00:00
|
|
|
|
2020-08-02 22:22:19 +00:00
|
|
|
SPDX-FileCopyrightText: 2019 Roman Gilg <subdiff@gmail.com>
|
2021-09-03 11:47:57 +00:00
|
|
|
SPDX-FileCopyrightText: 2021 David Edmundson <davidedmundson@kde.org>
|
|
|
|
SPDX-FileCopyrightText: 2021 David Redondo <kde@david-redondo.de>
|
2018-08-22 12:56:48 +00:00
|
|
|
|
2020-08-02 22:22:19 +00:00
|
|
|
SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
2018-08-22 12:56:48 +00:00
|
|
|
#include "drag_wl.h"
|
|
|
|
|
|
|
|
#include "databridge.h"
|
|
|
|
#include "dnd.h"
|
2019-07-02 19:56:03 +00:00
|
|
|
#include "xwayland.h"
|
2021-09-03 11:47:57 +00:00
|
|
|
#include "xwldrophandler.h"
|
2018-08-22 12:56:48 +00:00
|
|
|
|
|
|
|
#include "atoms.h"
|
|
|
|
#include "wayland_server.h"
|
|
|
|
#include "workspace.h"
|
2022-03-23 10:13:38 +00:00
|
|
|
#include "x11client.h"
|
2018-08-22 12:56:48 +00:00
|
|
|
|
2021-02-05 17:48:19 +00:00
|
|
|
#include <KWaylandServer/datadevice_interface.h>
|
2022-03-23 10:13:38 +00:00
|
|
|
#include <KWaylandServer/datasource_interface.h>
|
2020-04-29 15:18:41 +00:00
|
|
|
#include <KWaylandServer/seat_interface.h>
|
|
|
|
#include <KWaylandServer/surface_interface.h>
|
2018-08-22 12:56:48 +00:00
|
|
|
|
|
|
|
#include <QMouseEvent>
|
|
|
|
#include <QTimer>
|
|
|
|
|
2021-06-03 09:04:26 +00:00
|
|
|
using DnDAction = KWaylandServer::DataDeviceManagerInterface::DnDAction;
|
|
|
|
using DnDActions = KWaylandServer::DataDeviceManagerInterface::DnDActions;
|
|
|
|
|
2021-09-03 11:47:57 +00:00
|
|
|
namespace KWin
|
2021-06-03 09:04:26 +00:00
|
|
|
{
|
2021-09-03 11:47:57 +00:00
|
|
|
namespace Xwl
|
2018-08-22 12:56:48 +00:00
|
|
|
{
|
|
|
|
|
2022-04-18 07:42:11 +00:00
|
|
|
DragEventReply WlToXDrag::moveFilter(AbstractClient *target, const QPoint &pos)
|
2018-08-22 12:56:48 +00:00
|
|
|
{
|
2021-09-03 11:47:57 +00:00
|
|
|
Q_UNUSED(target)
|
|
|
|
Q_UNUSED(pos)
|
|
|
|
return DragEventReply::Wayland;
|
2018-08-22 12:56:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool WlToXDrag::handleClientMessage(xcb_client_message_event_t *event)
|
|
|
|
{
|
2021-09-03 11:47:57 +00:00
|
|
|
return DataBridge::self()->dnd()->dropHandler()->handleClientMessage(event);
|
2018-08-22 12:56:48 +00:00
|
|
|
}
|
|
|
|
|
2021-09-03 11:47:57 +00:00
|
|
|
Xvisit::Xvisit(AbstractClient *target, KWaylandServer::AbstractDataSource *dataSource, QObject *parent)
|
2022-03-23 10:13:38 +00:00
|
|
|
: QObject(parent)
|
|
|
|
, m_target(target)
|
|
|
|
, m_dataSource(dataSource)
|
2018-08-22 12:56:48 +00:00
|
|
|
{
|
|
|
|
// first check supported DND version
|
2019-07-02 19:56:03 +00:00
|
|
|
xcb_connection_t *xcbConn = kwinApp()->x11Connection();
|
2018-08-22 12:56:48 +00:00
|
|
|
xcb_get_property_cookie_t cookie = xcb_get_property(xcbConn,
|
|
|
|
0,
|
|
|
|
m_target->window(),
|
|
|
|
atoms->xdnd_aware,
|
|
|
|
XCB_GET_PROPERTY_TYPE_ANY,
|
|
|
|
0, 1);
|
2019-07-02 19:56:03 +00:00
|
|
|
auto *reply = xcb_get_property_reply(xcbConn, cookie, nullptr);
|
|
|
|
if (!reply) {
|
2018-08-22 12:56:48 +00:00
|
|
|
doFinish();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (reply->type != XCB_ATOM_ATOM) {
|
|
|
|
doFinish();
|
|
|
|
free(reply);
|
|
|
|
return;
|
|
|
|
}
|
2019-07-02 19:56:03 +00:00
|
|
|
xcb_atom_t *value = static_cast<xcb_atom_t *>(xcb_get_property_value(reply));
|
2018-08-22 12:56:48 +00:00
|
|
|
m_version = qMin(*value, Dnd::version());
|
|
|
|
if (m_version < 1) {
|
|
|
|
// minimal version we accept is 1
|
|
|
|
doFinish();
|
|
|
|
free(reply);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
free(reply);
|
|
|
|
|
2021-06-03 09:04:26 +00:00
|
|
|
receiveOffer();
|
2018-08-22 12:56:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Xvisit::handleClientMessage(xcb_client_message_event_t *event)
|
|
|
|
{
|
|
|
|
if (event->type == atoms->xdnd_status) {
|
|
|
|
return handleStatus(event);
|
|
|
|
} else if (event->type == atoms->xdnd_finished) {
|
|
|
|
return handleFinished(event);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-07-02 19:56:03 +00:00
|
|
|
bool Xvisit::handleStatus(xcb_client_message_event_t *event)
|
2018-08-22 12:56:48 +00:00
|
|
|
{
|
2019-07-02 19:56:03 +00:00
|
|
|
xcb_client_message_data_t *data = &event->data;
|
2018-08-22 12:56:48 +00:00
|
|
|
if (data->data32[0] != m_target->window()) {
|
|
|
|
// wrong target window
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_accepts = data->data32[1] & 1;
|
|
|
|
xcb_atom_t actionAtom = data->data32[4];
|
|
|
|
|
2021-09-03 11:47:57 +00:00
|
|
|
if (m_dataSource && !m_dataSource->mimeTypes().isEmpty()) {
|
|
|
|
m_dataSource->accept(m_accepts ? m_dataSource->mimeTypes().constFirst() : QString());
|
2021-05-24 14:06:57 +00:00
|
|
|
}
|
2018-08-22 12:56:48 +00:00
|
|
|
// TODO: we could optimize via rectangle in data32[2] and data32[3]
|
|
|
|
|
|
|
|
// position round trip finished
|
|
|
|
m_pos.pending = false;
|
|
|
|
|
|
|
|
if (!m_state.dropped) {
|
|
|
|
// as long as the drop is not yet done determine requested action
|
2021-09-03 11:47:57 +00:00
|
|
|
m_preferredAction = Dnd::atomToClientAction(actionAtom);
|
2018-08-22 12:56:48 +00:00
|
|
|
determineProposedAction();
|
|
|
|
requestDragAndDropAction();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_pos.cached) {
|
|
|
|
// send cached position
|
|
|
|
m_pos.cached = false;
|
|
|
|
sendPosition(m_pos.cache);
|
|
|
|
} else if (m_state.dropped) {
|
|
|
|
// drop was done in between, now close it out
|
|
|
|
drop();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-02 19:56:03 +00:00
|
|
|
bool Xvisit::handleFinished(xcb_client_message_event_t *event)
|
2018-08-22 12:56:48 +00:00
|
|
|
{
|
2019-07-02 19:56:03 +00:00
|
|
|
xcb_client_message_data_t *data = &event->data;
|
2018-08-22 12:56:48 +00:00
|
|
|
|
|
|
|
if (data->data32[0] != m_target->window()) {
|
|
|
|
// different target window
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!m_state.dropped) {
|
|
|
|
// drop was never done
|
|
|
|
doFinish();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const bool success = m_version > 4 ? data->data32[1] & 1 : true;
|
2022-03-23 10:13:38 +00:00
|
|
|
const xcb_atom_t usedActionAtom = m_version > 4 ? data->data32[2] : static_cast<uint32_t>(XCB_ATOM_NONE);
|
2018-08-22 12:56:48 +00:00
|
|
|
Q_UNUSED(success);
|
|
|
|
Q_UNUSED(usedActionAtom);
|
|
|
|
|
2022-03-23 10:13:38 +00:00
|
|
|
if (m_dataSource) {
|
2021-09-03 11:47:57 +00:00
|
|
|
m_dataSource->dndFinished();
|
2018-08-22 12:56:48 +00:00
|
|
|
}
|
|
|
|
doFinish();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Xvisit::sendPosition(const QPointF &globalPos)
|
|
|
|
{
|
|
|
|
const int16_t x = globalPos.x();
|
|
|
|
const int16_t y = globalPos.y();
|
|
|
|
|
|
|
|
if (m_pos.pending) {
|
|
|
|
m_pos.cache = QPoint(x, y);
|
|
|
|
m_pos.cached = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_pos.pending = true;
|
|
|
|
|
2020-08-26 17:24:02 +00:00
|
|
|
xcb_client_message_data_t data = {};
|
2018-08-22 12:56:48 +00:00
|
|
|
data.data32[0] = DataBridge::self()->dnd()->window();
|
|
|
|
data.data32[2] = (x << 16) | y;
|
|
|
|
data.data32[3] = XCB_CURRENT_TIME;
|
2021-09-03 11:47:57 +00:00
|
|
|
data.data32[4] = Dnd::clientActionToAtom(m_proposedAction);
|
2018-08-22 12:56:48 +00:00
|
|
|
|
|
|
|
Drag::sendClientMessage(m_target->window(), atoms->xdnd_position, &data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Xvisit::leave()
|
|
|
|
{
|
2021-06-03 09:04:26 +00:00
|
|
|
if (m_state.dropped) {
|
|
|
|
// dropped, but not yet finished, it'll be cleaned up when the drag finishes
|
|
|
|
return;
|
|
|
|
}
|
2018-08-22 12:56:48 +00:00
|
|
|
if (m_state.finished) {
|
|
|
|
// was already finished
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// we only need to leave, when we entered before
|
|
|
|
if (m_state.entered) {
|
|
|
|
sendLeave();
|
|
|
|
}
|
|
|
|
doFinish();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Xvisit::receiveOffer()
|
|
|
|
{
|
|
|
|
retrieveSupportedActions();
|
2021-09-03 11:47:57 +00:00
|
|
|
connect(m_dataSource, &KWaylandServer::AbstractDataSource::supportedDragAndDropActionsChanged,
|
2022-03-23 10:13:38 +00:00
|
|
|
this, &Xvisit::retrieveSupportedActions);
|
2018-08-22 12:56:48 +00:00
|
|
|
enter();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Xvisit::enter()
|
|
|
|
{
|
|
|
|
m_state.entered = true;
|
|
|
|
// send enter event and current position to X client
|
|
|
|
sendEnter();
|
|
|
|
sendPosition(waylandServer()->seat()->pointerPos());
|
|
|
|
|
|
|
|
// proxy future pointer position changes
|
2019-07-02 19:56:03 +00:00
|
|
|
m_motionConnection = connect(waylandServer()->seat(),
|
2022-03-23 10:13:38 +00:00
|
|
|
&KWaylandServer::SeatInterface::pointerPosChanged,
|
|
|
|
this, &Xvisit::sendPosition);
|
2018-08-22 12:56:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Xvisit::sendEnter()
|
|
|
|
{
|
2021-09-03 11:47:57 +00:00
|
|
|
if (!m_dataSource) {
|
2021-02-05 17:48:19 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-26 17:24:02 +00:00
|
|
|
xcb_client_message_data_t data = {};
|
2018-08-22 12:56:48 +00:00
|
|
|
data.data32[0] = DataBridge::self()->dnd()->window();
|
|
|
|
data.data32[1] = m_version << 24;
|
|
|
|
|
2021-09-03 11:47:57 +00:00
|
|
|
const auto mimeTypesNames = m_dataSource->mimeTypes();
|
2018-08-22 12:56:48 +00:00
|
|
|
const int mimesCount = mimeTypesNames.size();
|
2022-01-25 08:29:00 +00:00
|
|
|
// Number of written entries in data32
|
|
|
|
size_t cnt = 2;
|
|
|
|
// Number of mimeTypes
|
2018-08-22 12:56:48 +00:00
|
|
|
size_t totalCnt = 0;
|
2020-08-26 17:24:02 +00:00
|
|
|
for (const auto &mimeName : mimeTypesNames) {
|
2018-08-22 12:56:48 +00:00
|
|
|
// 3 mimes and less can be sent directly in the XdndEnter message
|
|
|
|
if (totalCnt == 3) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
const auto atom = Selection::mimeTypeToAtom(mimeName);
|
|
|
|
|
|
|
|
if (atom != XCB_ATOM_NONE) {
|
2022-01-25 08:29:00 +00:00
|
|
|
data.data32[cnt] = atom;
|
2018-08-22 12:56:48 +00:00
|
|
|
cnt++;
|
|
|
|
}
|
|
|
|
totalCnt++;
|
|
|
|
}
|
2022-01-25 08:29:00 +00:00
|
|
|
for (int i = cnt; i < 5; i++) {
|
|
|
|
data.data32[i] = XCB_ATOM_NONE;
|
2018-08-22 12:56:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mimesCount > 3) {
|
|
|
|
// need to first transfer all available mime types
|
|
|
|
data.data32[1] |= 1;
|
|
|
|
|
|
|
|
QVector<xcb_atom_t> targets;
|
|
|
|
targets.resize(mimesCount);
|
|
|
|
|
|
|
|
size_t cnt = 0;
|
2020-08-26 17:24:02 +00:00
|
|
|
for (const auto &mimeName : mimeTypesNames) {
|
2018-08-22 12:56:48 +00:00
|
|
|
const auto atom = Selection::mimeTypeToAtom(mimeName);
|
|
|
|
if (atom != XCB_ATOM_NONE) {
|
|
|
|
targets[cnt] = atom;
|
|
|
|
cnt++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
xcb_change_property(kwinApp()->x11Connection(),
|
|
|
|
XCB_PROP_MODE_REPLACE,
|
|
|
|
DataBridge::self()->dnd()->window(),
|
|
|
|
atoms->xdnd_type_list,
|
|
|
|
XCB_ATOM_ATOM,
|
|
|
|
32, cnt, targets.data());
|
|
|
|
}
|
|
|
|
Drag::sendClientMessage(m_target->window(), atoms->xdnd_enter, &data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Xvisit::sendDrop(uint32_t time)
|
|
|
|
{
|
2020-08-26 17:24:02 +00:00
|
|
|
xcb_client_message_data_t data = {};
|
2018-08-22 12:56:48 +00:00
|
|
|
data.data32[0] = DataBridge::self()->dnd()->window();
|
|
|
|
data.data32[2] = time;
|
|
|
|
|
|
|
|
Drag::sendClientMessage(m_target->window(), atoms->xdnd_drop, &data);
|
|
|
|
|
|
|
|
if (m_version < 2) {
|
|
|
|
doFinish();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Xvisit::sendLeave()
|
|
|
|
{
|
2020-08-26 17:24:02 +00:00
|
|
|
xcb_client_message_data_t data = {};
|
2018-08-22 12:56:48 +00:00
|
|
|
data.data32[0] = DataBridge::self()->dnd()->window();
|
|
|
|
Drag::sendClientMessage(m_target->window(), atoms->xdnd_leave, &data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Xvisit::retrieveSupportedActions()
|
|
|
|
{
|
2021-09-03 11:47:57 +00:00
|
|
|
m_supportedActions = m_dataSource->supportedDragAndDropActions();
|
2018-08-22 12:56:48 +00:00
|
|
|
determineProposedAction();
|
|
|
|
requestDragAndDropAction();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Xvisit::determineProposedAction()
|
|
|
|
{
|
|
|
|
DnDAction oldProposedAction = m_proposedAction;
|
|
|
|
if (m_supportedActions.testFlag(m_preferredAction)) {
|
|
|
|
m_proposedAction = m_preferredAction;
|
|
|
|
} else if (m_supportedActions.testFlag(DnDAction::Copy)) {
|
|
|
|
m_proposedAction = DnDAction::Copy;
|
|
|
|
} else {
|
|
|
|
m_proposedAction = DnDAction::None;
|
|
|
|
}
|
|
|
|
// send updated action to X target
|
2021-09-02 09:17:53 +00:00
|
|
|
if (oldProposedAction != m_proposedAction && m_state.entered) {
|
2018-08-22 12:56:48 +00:00
|
|
|
sendPosition(waylandServer()->seat()->pointerPos());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Xvisit::requestDragAndDropAction()
|
|
|
|
{
|
2022-03-23 10:13:38 +00:00
|
|
|
DnDAction action = m_preferredAction != DnDAction::None ? m_preferredAction : DnDAction::Copy;
|
2018-08-22 12:56:48 +00:00
|
|
|
// we assume the X client supports Move, but this might be wrong - then
|
|
|
|
// the drag just cancels, if the user tries to force it.
|
|
|
|
|
2021-06-03 09:04:26 +00:00
|
|
|
// As we skip the client data device, we do action negotiation directly then tell the source.
|
|
|
|
if (m_supportedActions.testFlag(action)) {
|
|
|
|
// everything is supported, no changes are needed
|
|
|
|
} else if (m_supportedActions.testFlag(DnDAction::Copy)) {
|
|
|
|
action = DnDAction::Copy;
|
|
|
|
} else if (m_supportedActions.testFlag(DnDAction::Move)) {
|
|
|
|
action = DnDAction::Move;
|
|
|
|
}
|
2021-09-03 11:47:57 +00:00
|
|
|
if (m_dataSource) {
|
|
|
|
m_dataSource->dndAction(action);
|
2021-06-03 09:04:26 +00:00
|
|
|
}
|
2018-08-22 12:56:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Xvisit::drop()
|
|
|
|
{
|
|
|
|
Q_ASSERT(!m_state.finished);
|
|
|
|
m_state.dropped = true;
|
|
|
|
// stop further updates
|
|
|
|
// TODO: revisit when we allow ask action
|
|
|
|
stopConnections();
|
|
|
|
if (!m_state.entered) {
|
|
|
|
// wait for enter (init + offers)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (m_pos.pending) {
|
|
|
|
// wait for pending position roundtrip
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!m_accepts) {
|
|
|
|
// target does not accept current action/offer
|
|
|
|
sendLeave();
|
|
|
|
doFinish();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// dnd session ended successfully
|
|
|
|
sendDrop(XCB_CURRENT_TIME);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Xvisit::doFinish()
|
|
|
|
{
|
|
|
|
m_state.finished = true;
|
|
|
|
m_pos.cached = false;
|
|
|
|
stopConnections();
|
|
|
|
Q_EMIT finish(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Xvisit::stopConnections()
|
|
|
|
{
|
|
|
|
// final outcome has been determined from Wayland side
|
|
|
|
// no more updates needed
|
2019-07-02 19:56:03 +00:00
|
|
|
disconnect(m_motionConnection);
|
|
|
|
m_motionConnection = QMetaObject::Connection();
|
2018-08-22 12:56:48 +00:00
|
|
|
}
|
|
|
|
|
2019-07-02 19:56:03 +00:00
|
|
|
} // namespace Xwl
|
|
|
|
} // namespace KWin
|