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"
|
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"
|
|
|
|
#include "surface_interface.h"
|
|
|
|
// Wayland
|
|
|
|
#include <wayland-server.h>
|
|
|
|
|
|
|
|
namespace KWayland
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace Server
|
|
|
|
{
|
|
|
|
|
2014-11-26 10:03:32 +00:00
|
|
|
class PointerInterface::Private : public Resource::Private
|
2014-11-25 12:52:40 +00:00
|
|
|
{
|
|
|
|
public:
|
2014-11-26 10:03:32 +00:00
|
|
|
Private(SeatInterface *parent, wl_resource *parentResource, PointerInterface *q);
|
2014-11-25 12:52:40 +00:00
|
|
|
|
|
|
|
SeatInterface *seat;
|
2014-11-26 09:34:23 +00:00
|
|
|
SurfaceInterface *focusedSurface = nullptr;
|
2014-11-25 12:52:40 +00:00
|
|
|
QMetaObject::Connection destroyConnection;
|
2015-02-27 16:36:43 +00:00
|
|
|
Cursor *cursor = nullptr;
|
2014-11-25 12:52:40 +00:00
|
|
|
|
|
|
|
private:
|
2015-02-27 16:36:43 +00:00
|
|
|
PointerInterface *q_func() {
|
|
|
|
return reinterpret_cast<PointerInterface *>(q);
|
|
|
|
}
|
|
|
|
void setCursor(quint32 serial, SurfaceInterface *surface, const QPoint &hotspot);
|
2014-11-25 12:52:40 +00:00
|
|
|
// interface
|
|
|
|
static void setCursorCallback(wl_client *client, wl_resource *resource, uint32_t serial,
|
|
|
|
wl_resource *surface, int32_t hotspot_x, int32_t hotspot_y);
|
|
|
|
// since version 3
|
|
|
|
static void releaseCallback(wl_client *client, wl_resource *resource);
|
|
|
|
|
|
|
|
static const struct wl_pointer_interface s_interface;
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
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)
|
|
|
|
: Resource(new Private(parent, parentResource, this), parent)
|
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());
|
2014-11-26 09:34:23 +00:00
|
|
|
wl_pointer_send_motion(d->resource, d->seat->timestamp(),
|
2014-11-25 13:24:52 +00:00
|
|
|
wl_fixed_from_double(pos.x()), wl_fixed_from_double(pos.y()));
|
|
|
|
}
|
|
|
|
});
|
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();
|
2014-11-26 09:34:23 +00:00
|
|
|
if (d->focusedSurface) {
|
2015-05-26 07:26:19 +00:00
|
|
|
if (d->resource && d->focusedSurface->resource()) {
|
|
|
|
wl_pointer_send_leave(d->resource, serial, d->focusedSurface->resource());
|
|
|
|
}
|
2014-11-25 12:52:40 +00:00
|
|
|
disconnect(d->destroyConnection);
|
|
|
|
}
|
2014-11-26 09:34:23 +00:00
|
|
|
if (!surface) {
|
|
|
|
d->focusedSurface = nullptr;
|
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;
|
|
|
|
}
|
|
|
|
);
|
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());
|
2014-11-26 09:34:23 +00:00
|
|
|
wl_pointer_send_enter(d->resource, serial,
|
|
|
|
d->focusedSurface->resource(),
|
|
|
|
wl_fixed_from_double(pos.x()), wl_fixed_from_double(pos.y()));
|
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
|
|
|
}
|
|
|
|
}
|