Commit 548978bf authored by Roman Gilg's avatar Roman Gilg

[xwl] Drag and drop between Xwayland and Wayland native clients

Summary:
Building upon the generic X Selection support this patch establishes another
selection class representing the XDND selection and provides interfaces
to communicate drags originating from Xwayland windows to the Wayland
server KWin and drags originating from Wayland native drags to Xwayland.

For Wayland native drags KWin will claim the XDND selection as owner and
will simply translate all relevant events to the XDND protocol and receive
alike messages by X clients.

When an X client claims the XDND selection KWin is notified via the X protocol
and it decides if it allows the X drag to transcend into the Wayland protocol.
If this is the case the mouse position is tracked and on entering a Wayland
native window a proxy X Window is mapped to the top of the window stack. This
proxy window acts as a drag destination for the drag origin window and again
X messages will be translated into respective Wayland protocol calls. If the
cursor leaves the Wayland window geometry before a drop is registered, the
proxy window is unmapped, what triggers a subsequent drag leave event.

In both directions the necessary core integration is minimal. There is a single
call to be done in the drag and drop event filter through the Xwayland
interface class.

From my tests this patch facilitates drags between any Qt/KDE apps. What needs
extra care are the browsers, which use target formats, that are not directly
compatible with the Wayland protocol's MIME representation. For Chromium an
additional integration step must be done in order to provide it with a net
window stack containing the proxy window.

Test Plan: Manually. Auto tests planned.

Reviewers: #kwin

Subscribers: zzag, kwin, alexde

Tags: #kwin

Maniphest Tasks: T4611

Differential Revision: https://phabricator.kde.org/D15627
parent ad1bcbec
......@@ -659,6 +659,10 @@ set(kwin_XWAYLAND_SRCS
${CMAKE_CURRENT_SOURCE_DIR}/xwl/selection_source.cpp
${CMAKE_CURRENT_SOURCE_DIR}/xwl/transfer.cpp
${CMAKE_CURRENT_SOURCE_DIR}/xwl/clipboard.cpp
${CMAKE_CURRENT_SOURCE_DIR}/xwl/dnd.cpp
${CMAKE_CURRENT_SOURCE_DIR}/xwl/drag.cpp
${CMAKE_CURRENT_SOURCE_DIR}/xwl/drag_wl.cpp
${CMAKE_CURRENT_SOURCE_DIR}/xwl/drag_x.cpp
)
include(ECMQtDeclareLoggingCategory)
ecm_qt_declare_logging_category(kwin_XWAYLAND_SRCS
......
......@@ -43,8 +43,18 @@ Atoms::Atoms()
, kde_net_wm_user_creation_time(QByteArrayLiteral("_KDE_NET_WM_USER_CREATION_TIME"))
, net_wm_take_activity(QByteArrayLiteral("_NET_WM_TAKE_ACTIVITY"))
, net_wm_window_opacity(QByteArrayLiteral("_NET_WM_WINDOW_OPACITY"))
, xdnd_selection(QByteArrayLiteral("XdndSelection"))
, xdnd_aware(QByteArrayLiteral("XdndAware"))
, xdnd_enter(QByteArrayLiteral("XdndEnter"))
, xdnd_type_list(QByteArrayLiteral("XdndTypeList"))
, xdnd_position(QByteArrayLiteral("XdndPosition"))
, xdnd_status(QByteArrayLiteral("XdndStatus"))
, xdnd_action_copy(QByteArrayLiteral("XdndActionCopy"))
, xdnd_action_move(QByteArrayLiteral("XdndActionMove"))
, xdnd_action_ask(QByteArrayLiteral("XdndActionAsk"))
, xdnd_drop(QByteArrayLiteral("XdndDrop"))
, xdnd_leave(QByteArrayLiteral("XdndLeave"))
, xdnd_finished(QByteArrayLiteral("XdndFinished"))
, net_frame_extents(QByteArrayLiteral("_NET_FRAME_EXTENTS"))
, kde_net_wm_frame_strut(QByteArrayLiteral("_KDE_NET_WM_FRAME_STRUT"))
, net_wm_sync_request_counter(QByteArrayLiteral("_NET_WM_SYNC_REQUEST_COUNTER"))
......
......@@ -52,8 +52,18 @@ public:
Xcb::Atom kde_net_wm_user_creation_time;
Xcb::Atom net_wm_take_activity;
Xcb::Atom net_wm_window_opacity;
Xcb::Atom xdnd_selection;
Xcb::Atom xdnd_aware;
Xcb::Atom xdnd_enter;
Xcb::Atom xdnd_type_list;
Xcb::Atom xdnd_position;
Xcb::Atom xdnd_status;
Xcb::Atom xdnd_action_copy;
Xcb::Atom xdnd_action_move;
Xcb::Atom xdnd_action_ask;
Xcb::Atom xdnd_drop;
Xcb::Atom xdnd_leave;
Xcb::Atom xdnd_finished;
Xcb::Atom net_frame_extents;
Xcb::Atom kde_net_wm_frame_strut;
Xcb::Atom net_wm_sync_request_counter;
......
......@@ -44,6 +44,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "popup_input_filter.h"
#include "shell_client.h"
#include "wayland_server.h"
#include "xwl/xwayland_interface.h"
#include <KWayland/Server/display.h>
#include <KWayland/Server/fakeinput_interface.h>
#include <KWayland/Server/seat_interface.h>
......@@ -1473,7 +1474,20 @@ public:
case QEvent::MouseMove: {
const auto pos = input()->globalPointer();
seat->setPointerPos(pos);
if (Toplevel *t = input()->pointer()->at()) {
const auto eventPos = event->globalPos();
// TODO: use InputDeviceHandler::at() here and check isClient()?
Toplevel *t = input()->findManagedToplevel(eventPos);
if (auto *xwl = xwayland()) {
const auto ret = xwl->dragMoveFilter(t, eventPos);
if (ret == Xwl::DragEventReply::Ignore) {
return false;
} else if (ret == Xwl::DragEventReply::Take) {
break;
}
}
if (t) {
// TODO: consider decorations
if (t->surface() != seat->dragSurface()) {
if (AbstractClient *c = qobject_cast<AbstractClient*>(t)) {
......@@ -2082,6 +2096,15 @@ Toplevel *InputRedirection::findToplevel(const QPoint &pos)
}
}
}
return findManagedToplevel(pos);
}
Toplevel *InputRedirection::findManagedToplevel(const QPoint &pos)
{
if (!Workspace::self()) {
return nullptr;
}
const bool isScreenLocked = waylandServer() && waylandServer()->isScreenLocked();
const ToplevelList &stacking = Workspace::self()->stackingOrder();
if (stacking.isEmpty()) {
return NULL;
......
......@@ -165,6 +165,7 @@ public:
void uninstallInputEventSpy(InputEventSpy *spy);
Toplevel *findToplevel(const QPoint &pos);
Toplevel *findManagedToplevel(const QPoint &pos);
GlobalShortcutsManager *shortcuts() const {
return m_shortcuts;
}
......
......@@ -30,6 +30,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <KWayland/Client/connection_thread.h>
#include <KWayland/Client/event_queue.h>
#include <KWayland/Client/registry.h>
#include <KWayland/Client/compositor.h>
#include <KWayland/Client/seat.h>
#include <KWayland/Client/datadevicemanager.h>
#include <KWayland/Client/shm_pool.h>
......@@ -113,6 +114,7 @@ void WaylandServer::destroyInternalConnection()
}
delete m_internalConnection.registry;
delete m_internalConnection.compositor;
delete m_internalConnection.seat;
delete m_internalConnection.ddm;
delete m_internalConnection.shm;
......@@ -567,6 +569,10 @@ void WaylandServer::createInternalConnection()
[this, registry] {
m_internalConnection.interfacesAnnounced = true;
const auto compInterface = registry->interface(Registry::Interface::Compositor);
if (compInterface.name != 0) {
m_internalConnection.compositor = registry->createCompositor(compInterface.name, compInterface.version, this);
}
const auto seatInterface = registry->interface(Registry::Interface::Seat);
if (seatInterface.name != 0) {
m_internalConnection.seat = registry->createSeat(seatInterface.name, seatInterface.version, this);
......
......@@ -34,6 +34,7 @@ namespace Client
{
class ConnectionThread;
class Registry;
class Compositor;
class Seat;
class DataDeviceManager;
class ShmPool;
......@@ -178,6 +179,9 @@ public:
KWayland::Server::ClientConnection *screenLockerClientConnection() const {
return m_screenLockerClientConnection;
}
KWayland::Client::Compositor *internalCompositor() {
return m_internalConnection.compositor;
}
KWayland::Client::Seat *internalSeat() {
return m_internalConnection.seat;
}
......@@ -263,6 +267,7 @@ private:
KWayland::Client::ConnectionThread *client = nullptr;
QThread *clientThread = nullptr;
KWayland::Client::Registry *registry = nullptr;
KWayland::Client::Compositor *compositor = nullptr;
KWayland::Client::Seat *seat = nullptr;
KWayland::Client::DataDeviceManager *ddm = nullptr;
KWayland::Client::ShmPool *shm = nullptr;
......
......@@ -26,7 +26,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "wayland_server.h"
#include "workspace.h"
#include "abstract_client.h"
#include "client.h"
#include <KWayland/Client/connection_thread.h>
#include <KWayland/Client/datadevice.h>
......@@ -135,6 +135,14 @@ void Clipboard::checkWlSource()
void Clipboard::doHandleXfixesNotify(xcb_xfixes_selection_notify_event_t *event)
{
createX11Source(NULL);
const auto *ac = workspace()->activeClient();
if (!qobject_cast<const KWin::Client *>(ac)) {
// clipboard is only allowed to be acquired when Xwayland has focus
// TODO: can we make this stronger (window id comparision)?
return;
}
createX11Source(event);
auto *xSrc = x11Source();
if (xSrc) {
......
......@@ -21,6 +21,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "xwayland.h"
#include "selection.h"
#include "clipboard.h"
#include "dnd.h"
#include "atoms.h"
#include "wayland_server.h"
......@@ -76,6 +77,7 @@ DataBridge::~DataBridge()
void DataBridge::init()
{
m_clipboard = new Clipboard(atoms->clipboard, this);
m_dnd = new Dnd(atoms->xdnd_selection, this);
waylandServer()->dispatch();
}
......@@ -84,6 +86,9 @@ bool DataBridge::filterEvent(xcb_generic_event_t *event)
if (m_clipboard->filterEvent(event)) {
return true;
}
if (m_dnd->filterEvent(event)) {
return true;
}
if (event->response_type - Xwayland::self()->xfixes()->first_event == XCB_XFIXES_SELECTION_NOTIFY) {
return handleXfixesNotify((xcb_xfixes_selection_notify_event_t *)event);
}
......@@ -96,11 +101,22 @@ bool DataBridge::handleXfixesNotify(xcb_xfixes_selection_notify_event_t *event)
if (atom == atoms->clipboard) {
return m_clipboard;
}
if (atom == atoms->xdnd_selection) {
return m_dnd;
}
return nullptr;
};
auto *sel = getSelection(event->selection);
return sel && sel->handleXfixesNotify(event);
}
DragEventReply DataBridge::dragMoveFilter(Toplevel *target, QPoint pos)
{
if (!m_dnd) {
return DragEventReply::Wayland;
}
return m_dnd->dragMoveFilter(target, pos);
}
}
}
......@@ -21,6 +21,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#define KWIN_XWL_DATABRIDGE
#include <QObject>
#include <QPoint>
#include <xcb/xcb.h>
......@@ -38,11 +39,14 @@ class SurfaceInterface;
namespace KWin
{
class Toplevel;
namespace Xwl
{
class Xwayland;
class Clipboard;
class Dnd;
enum class DragEventReply;
/*
* Interface class for all data sharing in the context of X selections
......@@ -60,13 +64,20 @@ public:
~DataBridge();
bool filterEvent(xcb_generic_event_t *event);
DragEventReply dragMoveFilter(Toplevel *target, QPoint pos);
KWayland::Client::DataDevice *dataDevice() const {
KWayland::Client::DataDevice *dataDevice() const
{
return m_dd;
}
KWayland::Server::DataDeviceInterface *dataDeviceIface() const {
KWayland::Server::DataDeviceInterface *dataDeviceIface() const
{
return m_ddi;
}
Dnd* dnd() const
{
return m_dnd;
}
private:
void init();
......@@ -74,6 +85,7 @@ private:
bool handleXfixesNotify(xcb_xfixes_selection_notify_event_t *event);
Clipboard *m_clipboard = nullptr;
Dnd *m_dnd = nullptr;
/* Internal data device interface */
KWayland::Client::DataDevice *m_dd = nullptr;
......
/********************************************************************
KWin - the KDE window manager
This file is part of the KDE project.
Copyright 2019 Roman Gilg <subdiff@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************/
#include "dnd.h"
#include "databridge.h"
#include "selection_source.h"
#include "drag_wl.h"
#include "drag_x.h"
#include "atoms.h"
#include "wayland_server.h"
#include "workspace.h"
#include "xwayland.h"
#include "abstract_client.h"
#include <KWayland/Client/compositor.h>
#include <KWayland/Client/surface.h>
#include <KWayland/Server/seat_interface.h>
#include <KWayland/Server/compositor_interface.h>
#include <QMouseEvent>
#include <xcb/xcb.h>
namespace KWin
{
namespace Xwl
{
// version of DnD support in X
const static uint32_t s_version = 5;
uint32_t Dnd::version()
{
return s_version;
}
Dnd::Dnd(xcb_atom_t atom, QObject *parent)
: Selection(atom, parent)
{
auto *xcbConn = kwinApp()->x11Connection();
const uint32_t dndValues[] = { XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
XCB_EVENT_MASK_PROPERTY_CHANGE };
xcb_create_window(xcbConn,
XCB_COPY_FROM_PARENT,
window(),
kwinApp()->x11RootWindow(),
0, 0,
8192, 8192, // TODO: get current screen size and connect to changes
0,
XCB_WINDOW_CLASS_INPUT_OUTPUT,
Xwayland::self()->xcbScreen()->root_visual,
XCB_CW_EVENT_MASK,
dndValues);
registerXfixes();
xcb_change_property(xcbConn,
XCB_PROP_MODE_REPLACE,
window(),
atoms->xdnd_aware,
XCB_ATOM_ATOM,
32, 1, &s_version);
xcb_flush(xcbConn);
connect(waylandServer()->seat(), &KWayland::Server::SeatInterface::dragStarted, this, &Dnd::startDrag);
connect(waylandServer()->seat(), &KWayland::Server::SeatInterface::dragEnded, this, &Dnd::endDrag);
const auto *comp = waylandServer()->compositor();
m_surface = waylandServer()->internalCompositor()->createSurface(this);
m_surface->setInputRegion(nullptr);
m_surface->commit(KWayland::Client::Surface::CommitFlag::None);
auto *dc = new QMetaObject::Connection();
*dc = connect(comp, &KWayland::Server::CompositorInterface::surfaceCreated, this,
[this, dc](KWayland::Server::SurfaceInterface *si) {
// TODO: how to make sure that it is the iface of m_surface?
if (m_surfaceIface || si->client() != waylandServer()->internalConnection()) {
return;
}
QObject::disconnect(*dc);
delete dc;
m_surfaceIface = si;
connect(workspace(), &Workspace::clientActivated, this,
[this](AbstractClient *ac) {
if (!ac || !ac->inherits("KWin::Client")) {
return;
}
auto *surface = ac->surface();
if (surface) {
surface->setDataProxy(m_surfaceIface);
} else {
auto *dc = new QMetaObject::Connection();
*dc = connect(ac, &AbstractClient::surfaceChanged, this, [this, ac, dc] {
if (auto *surface = ac->surface()) {
surface->setDataProxy(m_surfaceIface);
QObject::disconnect(*dc);
delete dc;
}
}
);
}
});
}
);
waylandServer()->dispatch();
}
void Dnd::doHandleXfixesNotify(xcb_xfixes_selection_notify_event_t *event)
{
if (qobject_cast<XToWlDrag*>(m_currentDrag)) {
// X drag is in progress, rogue X client took over the selection.
return;
}
if (m_currentDrag) {
// Wl drag is in progress - don't overwrite by rogue X client,
// get it back instead!
ownSelection(true);
return;
}
createX11Source(NULL);
const auto *seat = waylandServer()->seat();
auto *originSurface = seat->focusedPointerSurface();
if (!originSurface) {
return;
}
if (originSurface->client() != waylandServer()->xWaylandConnection()) {
// focused surface client is not Xwayland - do not allow drag to start
// TODO: can we make this stronger (window id comparision)?
return;
}
if (!seat->isPointerButtonPressed(Qt::LeftButton)) {
// we only allow drags to be started on (left) pointer button being
// pressed for now
return;
}
createX11Source(event);
auto *xSrc = x11Source();
if (!xSrc) {
return;
}
DataBridge::self()->dataDeviceIface()->updateProxy(originSurface);
m_currentDrag = new XToWlDrag(xSrc);
}
void Dnd::x11OffersChanged(const QVector<QString> &added, const QVector<QString> &removed)
{
Q_UNUSED(added);
Q_UNUSED(removed);
// TODO: handled internally
}
bool Dnd::handleClientMessage(xcb_client_message_event_t *event)
{
for (auto *drag : m_oldDrags) {
if (drag->handleClientMessage(event)) {
return true;
}
}
if (m_currentDrag && m_currentDrag->handleClientMessage(event)) {
return true;
}
return false;
}
DragEventReply Dnd::dragMoveFilter(Toplevel *target, QPoint pos)
{
// this filter only is used when a drag is in process
Q_ASSERT(m_currentDrag);
return m_currentDrag->moveFilter(target, pos);
}
void Dnd::startDrag()
{
auto *ddi = waylandServer()->seat()->dragSource();
if (ddi == DataBridge::self()->dataDeviceIface()) {
// X to Wl drag, started by us, is in progress
Q_ASSERT(m_currentDrag);
return;
}
// there can only ever be one Wl native drag at the same time
Q_ASSERT(!m_currentDrag);
// new Wl to X drag, init drag and Wl source
m_currentDrag = new WlToXDrag();
auto *wls = new WlSource(this, ddi);
wls->setDataSourceIface(ddi->dragSource());
setWlSource(wls);
ownSelection(true);
}
void Dnd::endDrag()
{
Q_ASSERT(m_currentDrag);
if (m_currentDrag->end()) {
delete m_currentDrag;
} else {
connect(m_currentDrag, &Drag::finish, this, &Dnd::clearOldDrag);
m_oldDrags << m_currentDrag;
}
m_currentDrag = nullptr;
}
void Dnd::clearOldDrag(Drag *drag)
{
m_oldDrags.removeOne(drag);
delete drag;
}
}
}
/********************************************************************
KWin - the KDE window manager
This file is part of the KDE project.
Copyright 2019 Roman Gilg <subdiff@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************/
#ifndef KWIN_XWL_DND
#define KWIN_XWL_DND
#include "selection.h"
#include <QPoint>
namespace KWayland
{
namespace Client
{
class Surface;
}
namespace Server
{
class SurfaceInterface;
}
}
namespace KWin
{
class Toplevel;
namespace Xwl
{
class Drag;
enum class DragEventReply;
/**
* Represents the drag and drop mechanism, on X side this is the XDND protocol.
* For more information on XDND see: http://johnlindal.wixsite.com/xdnd
*/
class Dnd : public Selection
{
Q_OBJECT
public:
explicit Dnd(xcb_atom_t atom, QObject *parent);
static uint32_t version();
void doHandleXfixesNotify(xcb_xfixes_selection_notify_event_t *event) override;
void x11OffersChanged(const QVector<QString> &added, const QVector<QString> &removed) override;
bool handleClientMessage(xcb_client_message_event_t *event) override;
DragEventReply dragMoveFilter(Toplevel *target, QPoint pos);
KWayland::Server::SurfaceInterface *surfaceIface() const {
return m_surfaceIface;
}
KWayland::Client::Surface *surface() const {
return m_surface;
}
private:
// start and end Wl native client drags (Wl -> Xwl)
void startDrag();
void endDrag();
void clearOldDrag(Drag *drag);
// active drag or null when no drag active
Drag *m_currentDrag = nullptr;
QVector<Drag*> m_oldDrags;
KWayland::Client::Surface *m_surface;
KWayland::Server::SurfaceInterface *m_surfaceIface = nullptr;
};
}
}
#endif
/********************************************************************
KWin - the KDE window manager
This file is part of the KDE project.
Copyright 2019 Roman Gilg <subdiff@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************/
#include "drag.h"
#include "atoms.h"
namespace KWin
{
namespace Xwl
{
void Drag::sendClientMessage(xcb_window_t target, xcb_atom_t type, xcb_client_message_data_t *data)
{
xcb_client_message_event_t event {
XCB_CLIENT_MESSAGE, // response_type
32, // format
0, // sequence
target, // window
type, // type
*data, // data
};
auto *xcbConn = kwinApp()->x11Connection();
xcb_send_event(xcbConn,
0,
target,
XCB_EVENT_MASK_NO_EVENT,
reinterpret_cast<const char *>(&event));
xcb_flush(xcbConn);
}
DnDAction Drag::atomToClientAction(xcb_atom_t atom)
{
if (atom == atoms->xdnd_action_copy) {
return DnDAction::Copy;
} else if (atom == atoms->xdnd_action_move) {
return DnDAction::Move;