2014-11-25 12:52:40 +00:00
|
|
|
/********************************************************************
|
|
|
|
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"
|
2016-04-06 09:27:42 +00:00
|
|
|
#include "pointer_interface_p.h"
|
2014-11-26 10:03:32 +00:00
|
|
|
#include "resource_p.h"
|
2014-11-25 12:52:40 +00:00
|
|
|
#include "seat_interface.h"
|
|
|
|
#include "display.h"
|
2016-04-06 07:52:13 +00:00
|
|
|
#include "subcompositor_interface.h"
|
2014-11-25 12:52:40 +00:00
|
|
|
#include "surface_interface.h"
|
|
|
|
// Wayland
|
|
|
|
#include <wayland-server.h>
|
|
|
|
|
|
|
|
namespace KWayland
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace Server
|
|
|
|
{
|
|
|
|
|
2015-02-27 16:36:43 +00:00
|
|
|
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)
|
2014-11-25 12:52:40 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-02-27 16:36:43 +00:00
|
|
|
void PointerInterface::Private::setCursor(quint32 serial, SurfaceInterface *surface, const QPoint &hotspot)
|
|
|
|
{
|
|
|
|
if (!cursor) {
|
|
|
|
Q_Q(PointerInterface);
|
|
|
|
cursor = new Cursor(q);
|
2016-02-24 15:46:21 +00:00
|
|
|
cursor->d->update(QPointer<SurfaceInterface>(surface), serial, hotspot);
|
2015-02-27 16:36:43 +00:00
|
|
|
QObject::connect(cursor, &Cursor::changed, q, &PointerInterface::cursorChanged);
|
|
|
|
emit q->cursorChanged();
|
|
|
|
} else {
|
|
|
|
cursor->d->update(QPointer<SurfaceInterface>(surface), serial, hotspot);
|
|
|
|
}
|
|
|
|
}
|
2014-11-25 12:52:40 +00:00
|
|
|
|
2016-04-06 07:52:13 +00:00
|
|
|
void PointerInterface::Private::sendLeave(SurfaceInterface *surface, quint32 serial)
|
|
|
|
{
|
|
|
|
if (!surface) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (resource && surface->resource()) {
|
|
|
|
wl_pointer_send_leave(resource, serial, surface->resource());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
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()));
|
|
|
|
}
|
|
|
|
|
2015-09-09 14:39:50 +00:00
|
|
|
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
2014-11-25 12:52:40 +00:00
|
|
|
const struct wl_pointer_interface PointerInterface::Private::s_interface = {
|
|
|
|
setCursorCallback,
|
|
|
|
releaseCallback
|
|
|
|
};
|
2015-09-09 14:39:50 +00:00
|
|
|
#endif
|
2014-11-25 12:52:40 +00:00
|
|
|
|
2014-11-26 10:03:32 +00:00
|
|
|
PointerInterface::PointerInterface(SeatInterface *parent, wl_resource *parentResource)
|
2016-05-19 13:17:07 +00:00
|
|
|
: Resource(new Private(parent, parentResource, this))
|
2014-11-25 12:52:40 +00:00
|
|
|
{
|
2016-03-01 06:49:04 +00:00
|
|
|
// TODO: handle touch
|
2014-11-27 12:59:31 +00:00
|
|
|
connect(parent, &SeatInterface::pointerPosChanged, this, [this] {
|
2014-11-26 10:03:32 +00:00
|
|
|
Q_D();
|
2016-03-01 06:49:04 +00:00
|
|
|
if (d->seat->isDragPointer()) {
|
|
|
|
// handled by DataDevice
|
|
|
|
return;
|
|
|
|
}
|
2015-04-20 07:09:23 +00:00
|
|
|
if (d->focusedSurface && d->resource) {
|
2015-12-07 10:45:34 +00:00
|
|
|
const QPointF pos = d->seat->focusedPointerSurfaceTransformation().map(d->seat->pointerPos());
|
2016-04-06 07:52:13 +00:00
|
|
|
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-25 13:24:52 +00:00
|
|
|
}
|
|
|
|
});
|
2014-11-25 12:52:40 +00:00
|
|
|
}
|
|
|
|
|
2014-11-26 10:03:32 +00:00
|
|
|
PointerInterface::~PointerInterface() = default;
|
2014-11-25 12:52:40 +00:00
|
|
|
|
2014-11-26 09:34:23 +00:00
|
|
|
void PointerInterface::setFocusedSurface(SurfaceInterface *surface, quint32 serial)
|
2014-11-25 12:52:40 +00:00
|
|
|
{
|
2014-11-26 10:03:32 +00:00
|
|
|
Q_D();
|
2016-04-06 07:52:13 +00:00
|
|
|
d->sendLeave(d->focusedChildSurface.data(), serial);
|
|
|
|
disconnect(d->destroyConnection);
|
2014-11-26 09:34:23 +00:00
|
|
|
if (!surface) {
|
|
|
|
d->focusedSurface = nullptr;
|
2016-04-06 07:52:13 +00:00
|
|
|
d->focusedChildSurface.clear();
|
2014-11-25 12:52:40 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-11-26 09:34:23 +00:00
|
|
|
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;
|
2016-04-06 07:52:13 +00:00
|
|
|
d->focusedChildSurface.clear();
|
2014-11-26 10:03:32 +00:00
|
|
|
}
|
|
|
|
);
|
2014-11-25 12:52:40 +00:00
|
|
|
|
2015-12-07 10:45:34 +00:00
|
|
|
const QPointF pos = d->seat->focusedPointerSurfaceTransformation().map(d->seat->pointerPos());
|
2016-04-06 07:52:13 +00:00
|
|
|
d->focusedChildSurface = QPointer<SurfaceInterface>(d->focusedSurface->surfaceAt(pos));
|
|
|
|
if (!d->focusedChildSurface) {
|
|
|
|
d->focusedChildSurface = QPointer<SurfaceInterface>(d->focusedSurface);
|
|
|
|
}
|
|
|
|
d->sendEnter(d->focusedChildSurface.data(), pos, serial);
|
2015-04-21 06:56:19 +00:00
|
|
|
d->client->flush();
|
2014-11-25 12:52:40 +00:00
|
|
|
}
|
|
|
|
|
2014-11-26 10:50:52 +00:00
|
|
|
void PointerInterface::buttonPressed(quint32 button, quint32 serial)
|
2014-11-25 12:52:40 +00:00
|
|
|
{
|
2014-11-26 10:03:32 +00:00
|
|
|
Q_D();
|
2014-11-26 10:50:52 +00:00
|
|
|
Q_ASSERT(d->focusedSurface);
|
2015-04-20 07:09:23 +00:00
|
|
|
if (!d->resource) {
|
|
|
|
return;
|
|
|
|
}
|
2014-11-26 09:34:23 +00:00
|
|
|
wl_pointer_send_button(d->resource, serial, d->seat->timestamp(), button, WL_POINTER_BUTTON_STATE_PRESSED);
|
2014-11-25 12:52:40 +00:00
|
|
|
}
|
|
|
|
|
2014-11-26 10:50:52 +00:00
|
|
|
void PointerInterface::buttonReleased(quint32 button, quint32 serial)
|
2014-11-25 12:52:40 +00:00
|
|
|
{
|
2014-11-26 10:03:32 +00:00
|
|
|
Q_D();
|
2014-11-26 10:50:52 +00:00
|
|
|
Q_ASSERT(d->focusedSurface);
|
2015-04-20 07:09:23 +00:00
|
|
|
if (!d->resource) {
|
|
|
|
return;
|
|
|
|
}
|
2014-11-26 09:34:23 +00:00
|
|
|
wl_pointer_send_button(d->resource, serial, d->seat->timestamp(), button, WL_POINTER_BUTTON_STATE_RELEASED);
|
2014-11-25 12:52:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PointerInterface::axis(Qt::Orientation orientation, quint32 delta)
|
|
|
|
{
|
2014-11-26 10:03:32 +00:00
|
|
|
Q_D();
|
2014-11-26 10:50:52 +00:00
|
|
|
Q_ASSERT(d->focusedSurface);
|
2015-04-20 07:09:23 +00:00
|
|
|
if (!d->resource) {
|
|
|
|
return;
|
|
|
|
}
|
2014-11-26 09:34:23 +00:00
|
|
|
wl_pointer_send_axis(d->resource, d->seat->timestamp(),
|
2014-11-25 12:52:40 +00:00
|
|
|
(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)
|
|
|
|
{
|
2015-02-27 16:36:43 +00:00
|
|
|
auto p = cast<Private>(resource);
|
|
|
|
Q_ASSERT(p->client->client() == client);
|
|
|
|
p->setCursor(serial, SurfaceInterface::get(surface), QPoint(hotspot_x, hotspot_y));
|
2014-11-25 12:52:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PointerInterface::Private::releaseCallback(wl_client *client, wl_resource *resource)
|
|
|
|
{
|
|
|
|
Q_UNUSED(client)
|
|
|
|
unbind(resource);
|
|
|
|
}
|
|
|
|
|
2015-02-27 16:36:43 +00:00
|
|
|
Cursor *PointerInterface::cursor() const
|
|
|
|
{
|
|
|
|
Q_D();
|
|
|
|
return d->cursor;
|
|
|
|
}
|
|
|
|
|
2014-11-26 10:03:32 +00:00
|
|
|
PointerInterface::Private *PointerInterface::d_func() const
|
2014-11-25 12:52:40 +00:00
|
|
|
{
|
2014-11-26 10:03:32 +00:00
|
|
|
return reinterpret_cast<Private*>(d.data());
|
2014-11-25 12:52:40 +00:00
|
|
|
}
|
|
|
|
|
2015-02-27 16:36:43 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-11-25 12:52:40 +00:00
|
|
|
}
|
|
|
|
}
|