kwin/src/wayland/pointer_interface.cpp

312 lines
9.5 KiB
C++
Raw Normal View History

/********************************************************************
Copyright 2014 Martin Gräßlin <mgraesslin@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) version 3, or any
later version accepted by the membership of KDE e.V. (or its
successor approved by the membership of KDE e.V.), which shall
act as a proxy defined in Section 6 of version 3 of the license.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************/
#include "pointer_interface.h"
#include "pointer_interface_p.h"
2014-11-26 10:03:32 +00:00
#include "resource_p.h"
#include "relativepointer_interface_p.h"
#include "seat_interface.h"
#include "display.h"
#include "subcompositor_interface.h"
#include "surface_interface.h"
// Wayland
#include <wayland-server.h>
namespace KWayland
{
namespace Server
{
class Cursor::Private
{
public:
Private(Cursor *q, PointerInterface *pointer);
PointerInterface *pointer;
quint32 enteredSerial = 0;
QPoint hotspot;
QPointer<SurfaceInterface> surface;
void update(const QPointer<SurfaceInterface> &surface, quint32 serial, const QPoint &hotspot);
private:
Cursor *q;
};
2014-11-26 10:03:32 +00:00
PointerInterface::Private::Private(SeatInterface *parent, wl_resource *parentResource, PointerInterface *q)
: Resource::Private(q, parent, parentResource, &wl_pointer_interface, &s_interface)
, seat(parent)
{
}
void PointerInterface::Private::setCursor(quint32 serial, SurfaceInterface *surface, const QPoint &hotspot)
{
if (!cursor) {
Q_Q(PointerInterface);
cursor = new Cursor(q);
cursor->d->update(QPointer<SurfaceInterface>(surface), serial, hotspot);
QObject::connect(cursor, &Cursor::changed, q, &PointerInterface::cursorChanged);
emit q->cursorChanged();
} else {
cursor->d->update(QPointer<SurfaceInterface>(surface), serial, hotspot);
}
}
void PointerInterface::Private::sendLeave(SurfaceInterface *surface, quint32 serial)
{
if (!surface) {
return;
}
if (resource && surface->resource()) {
wl_pointer_send_leave(resource, serial, surface->resource());
}
}
void PointerInterface::Private::registerRelativePointer(RelativePointerInterface *relativePointer)
{
relativePointers << relativePointer;
QObject::connect(relativePointer, &QObject::destroyed, q,
[this, relativePointer] {
relativePointers.removeOne(relativePointer);
}
);
}
namespace {
static QPointF surfacePosition(SurfaceInterface *surface) {
if (surface && surface->subSurface()) {
return surface->subSurface()->position() + surfacePosition(surface->subSurface()->parentSurface().data());
}
return QPointF();
}
}
void PointerInterface::Private::sendEnter(SurfaceInterface *surface, const QPointF &parentSurfacePosition, quint32 serial)
{
if (!surface || !surface->resource()) {
return;
}
const QPointF adjustedPos = parentSurfacePosition - surfacePosition(surface);
wl_pointer_send_enter(resource, serial,
surface->resource(),
wl_fixed_from_double(adjustedPos.x()), wl_fixed_from_double(adjustedPos.y()));
}
#ifndef DOXYGEN_SHOULD_SKIP_THIS
const struct wl_pointer_interface PointerInterface::Private::s_interface = {
setCursorCallback,
resourceDestroyedCallback
};
#endif
2014-11-26 10:03:32 +00:00
PointerInterface::PointerInterface(SeatInterface *parent, wl_resource *parentResource)
: Resource(new Private(parent, parentResource, this))
{
[server] Implement support for drag'n'drop through pointer device Summary: How drag'n'drop works on Wayland: When a surface has a pointer grab and a button pressed on the surface (implicit grab) the client can initiate a drag'n'drop operation on the data device. For this the client needs to provide a data source describing the data to be transmitted with the drag'n'drop operation. When a drag'n'drop operation is active all pointer events are interpreted as part of the drag'n'drop operation, the pointer device is grabbed. Pointer events are no longer sent to the focused pointer but to the focused data device. When the pointer moves to another surface an enter event is sent to a data device for that surface and a leave event is sent to the data device previously focused. An enter event carries a data offer which is created from the data source for the operation. During pointer motion there is a feedback mechanism. The data offer can signal to the data source that it can or cannot accept the data at the current pointer position. This can be used by the client being dragged from to update the cursor. The drag'n'drop operation ends with the implicit grab being removed, that is the pressed pointer button which triggered the operation gets released. The server sends a drop event to the focused data device. The data transfer can now be started. For that the receiving client creates a pipe and passes the file descriptor through the data offer to the sending data source. The sending client will write into the file descriptor and close it to finish the transfer. Drag'n'drop could also be initiated through a touch device grab, but this is not yet implemented. The implementation in this change focuses on the adjustments for pointer. For the user of the library drag'n'drop is implemented in the SeatInterface. Signals are emitted whenever drag is started or ended. The interaction for pointer events hardly changes. Motion, button press and button release can still be indicated in the same way. If a button release removes the implicit grab the drop is automatically performed, without the user of the library having to do anything. The only change during drag and drop for the library user is that setFocusedPointerSurface is blocked. To update the current drag target the library user should use setDragTarget. Sending the enter/leave to the data device gets performed automatically. The data device which triggered the drag and drop operation is exposed in the SeatInterface. The user of the library should make sure to render the additional drag icon provided on the data device. At least QtWayland based applications will freeze during drag and drop if the icon doesn't get rendered. The implementation is currently still lacking the client side and due to that also auto test. It's currently only tested with QtWayland clients. Reviewers: #plasma, sebas Subscribers: plasma-devel Projects: #plasma Differential Revision: https://phabricator.kde.org/D1046
2016-03-01 06:49:04 +00:00
// TODO: handle touch
connect(parent, &SeatInterface::pointerPosChanged, this, [this] {
2014-11-26 10:03:32 +00:00
Q_D();
[server] Implement support for drag'n'drop through pointer device Summary: How drag'n'drop works on Wayland: When a surface has a pointer grab and a button pressed on the surface (implicit grab) the client can initiate a drag'n'drop operation on the data device. For this the client needs to provide a data source describing the data to be transmitted with the drag'n'drop operation. When a drag'n'drop operation is active all pointer events are interpreted as part of the drag'n'drop operation, the pointer device is grabbed. Pointer events are no longer sent to the focused pointer but to the focused data device. When the pointer moves to another surface an enter event is sent to a data device for that surface and a leave event is sent to the data device previously focused. An enter event carries a data offer which is created from the data source for the operation. During pointer motion there is a feedback mechanism. The data offer can signal to the data source that it can or cannot accept the data at the current pointer position. This can be used by the client being dragged from to update the cursor. The drag'n'drop operation ends with the implicit grab being removed, that is the pressed pointer button which triggered the operation gets released. The server sends a drop event to the focused data device. The data transfer can now be started. For that the receiving client creates a pipe and passes the file descriptor through the data offer to the sending data source. The sending client will write into the file descriptor and close it to finish the transfer. Drag'n'drop could also be initiated through a touch device grab, but this is not yet implemented. The implementation in this change focuses on the adjustments for pointer. For the user of the library drag'n'drop is implemented in the SeatInterface. Signals are emitted whenever drag is started or ended. The interaction for pointer events hardly changes. Motion, button press and button release can still be indicated in the same way. If a button release removes the implicit grab the drop is automatically performed, without the user of the library having to do anything. The only change during drag and drop for the library user is that setFocusedPointerSurface is blocked. To update the current drag target the library user should use setDragTarget. Sending the enter/leave to the data device gets performed automatically. The data device which triggered the drag and drop operation is exposed in the SeatInterface. The user of the library should make sure to render the additional drag icon provided on the data device. At least QtWayland based applications will freeze during drag and drop if the icon doesn't get rendered. The implementation is currently still lacking the client side and due to that also auto test. It's currently only tested with QtWayland clients. Reviewers: #plasma, sebas Subscribers: plasma-devel Projects: #plasma Differential Revision: https://phabricator.kde.org/D1046
2016-03-01 06:49:04 +00:00
if (d->seat->isDragPointer()) {
// handled by DataDevice
return;
}
if (d->focusedSurface && d->resource) {
const QPointF pos = d->seat->focusedPointerSurfaceTransformation().map(d->seat->pointerPos());
auto targetSurface = d->focusedSurface->surfaceAt(pos);
if (!targetSurface) {
targetSurface = d->focusedSurface;
}
if (targetSurface != d->focusedChildSurface.data()) {
const quint32 serial = d->seat->display()->nextSerial();
d->sendLeave(d->focusedChildSurface.data(), serial);
d->focusedChildSurface = QPointer<SurfaceInterface>(targetSurface);
d->sendEnter(targetSurface, pos, serial);
d->client->flush();
} else {
const QPointF adjustedPos = pos - surfacePosition(d->focusedChildSurface);
wl_pointer_send_motion(d->resource, d->seat->timestamp(),
wl_fixed_from_double(adjustedPos.x()), wl_fixed_from_double(adjustedPos.y()));
}
}
});
}
2014-11-26 10:03:32 +00:00
PointerInterface::~PointerInterface() = default;
void PointerInterface::setFocusedSurface(SurfaceInterface *surface, quint32 serial)
{
2014-11-26 10:03:32 +00:00
Q_D();
d->sendLeave(d->focusedChildSurface.data(), serial);
disconnect(d->destroyConnection);
if (!surface) {
d->focusedSurface = nullptr;
d->focusedChildSurface.clear();
return;
}
d->focusedSurface = surface;
2014-11-26 10:03:32 +00:00
d->destroyConnection = connect(d->focusedSurface, &QObject::destroyed, this,
[this] {
Q_D();
d->focusedSurface = nullptr;
d->focusedChildSurface.clear();
2014-11-26 10:03:32 +00:00
}
);
const QPointF pos = d->seat->focusedPointerSurfaceTransformation().map(d->seat->pointerPos());
d->focusedChildSurface = QPointer<SurfaceInterface>(d->focusedSurface->surfaceAt(pos));
if (!d->focusedChildSurface) {
d->focusedChildSurface = QPointer<SurfaceInterface>(d->focusedSurface);
}
d->sendEnter(d->focusedChildSurface.data(), pos, serial);
d->client->flush();
}
void PointerInterface::buttonPressed(quint32 button, quint32 serial)
{
2014-11-26 10:03:32 +00:00
Q_D();
Q_ASSERT(d->focusedSurface);
if (!d->resource) {
return;
}
wl_pointer_send_button(d->resource, serial, d->seat->timestamp(), button, WL_POINTER_BUTTON_STATE_PRESSED);
}
void PointerInterface::buttonReleased(quint32 button, quint32 serial)
{
2014-11-26 10:03:32 +00:00
Q_D();
Q_ASSERT(d->focusedSurface);
if (!d->resource) {
return;
}
wl_pointer_send_button(d->resource, serial, d->seat->timestamp(), button, WL_POINTER_BUTTON_STATE_RELEASED);
}
void PointerInterface::axis(Qt::Orientation orientation, quint32 delta)
{
2014-11-26 10:03:32 +00:00
Q_D();
Q_ASSERT(d->focusedSurface);
if (!d->resource) {
return;
}
wl_pointer_send_axis(d->resource, d->seat->timestamp(),
(orientation == Qt::Vertical) ? WL_POINTER_AXIS_VERTICAL_SCROLL : WL_POINTER_AXIS_HORIZONTAL_SCROLL,
wl_fixed_from_int(delta));
}
void PointerInterface::Private::setCursorCallback(wl_client *client, wl_resource *resource, uint32_t serial,
wl_resource *surface, int32_t hotspot_x, int32_t hotspot_y)
{
auto p = cast<Private>(resource);
Q_ASSERT(p->client->client() == client);
p->setCursor(serial, SurfaceInterface::get(surface), QPoint(hotspot_x, hotspot_y));
}
Cursor *PointerInterface::cursor() const
{
Q_D();
return d->cursor;
}
void PointerInterface::relativeMotion(const QSizeF &delta, const QSizeF &deltaNonAccelerated, quint64 microseconds)
{
Q_D();
if (d->relativePointers.isEmpty()) {
return;
}
for (auto it = d->relativePointers.constBegin(), end = d->relativePointers.constEnd(); it != end; it++) {
(*it)->relativeMotion(delta, deltaNonAccelerated, microseconds);
}
client()->flush();
}
2014-11-26 10:03:32 +00:00
PointerInterface::Private *PointerInterface::d_func() const
{
2014-11-26 10:03:32 +00:00
return reinterpret_cast<Private*>(d.data());
}
PointerInterface *PointerInterface::get(wl_resource *native)
{
return Private::get<PointerInterface>(native);
}
Cursor::Private::Private(Cursor *q, PointerInterface *pointer)
: pointer(pointer)
, q(q)
{
}
void Cursor::Private::update(const QPointer< SurfaceInterface > &s, quint32 serial, const QPoint &p)
{
bool emitChanged = false;
if (enteredSerial != serial) {
enteredSerial = serial;
emitChanged = true;
emit q->enteredSerialChanged();
}
if (hotspot != p) {
hotspot = p;
emitChanged = true;
emit q->hotspotChanged();
}
if (surface != s) {
if (!surface.isNull()) {
QObject::disconnect(surface.data(), &SurfaceInterface::damaged, q, &Cursor::changed);
}
surface = s;
if (!surface.isNull()) {
QObject::connect(surface.data(), &SurfaceInterface::damaged, q, &Cursor::changed);
}
emitChanged = true;
emit q->surfaceChanged();
}
if (emitChanged) {
emit q->changed();
}
}
Cursor::Cursor(PointerInterface *parent)
: QObject(parent)
, d(new Private(this, parent))
{
}
Cursor::~Cursor() = default;
quint32 Cursor::enteredSerial() const
{
return d->enteredSerial;
}
QPoint Cursor::hotspot() const
{
return d->hotspot;
}
PointerInterface *Cursor::pointer() const
{
return d->pointer;
}
QPointer< SurfaceInterface > Cursor::surface() const
{
return d->surface;
}
}
}