pointer_interface.cpp 13.9 KB
Newer Older
1
2
/*
    SPDX-FileCopyrightText: 2014 Martin Gräßlin <mgraesslin@kde.org>
3

4
5
    SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/
6
#include "pointer_interface.h"
7
#include "pointer_interface_p.h"
8
#include "pointerconstraints_interface.h"
9
#include "pointergestures_interface_p.h"
10
#include "resource_p.h"
11
#include "relativepointer_interface_p.h"
12
13
#include "seat_interface.h"
#include "display.h"
14
#include "subcompositor_interface.h"
15
#include "surface_interface.h"
16
#include "datadevice_interface.h"
17
18
19
// Wayland
#include <wayland-server.h>

20
namespace KWaylandServer
21
22
{

23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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;
};

38
39
40
PointerInterface::Private::Private(SeatInterface *parent, wl_resource *parentResource, PointerInterface *q)
    : Resource::Private(q, parent, parentResource, &wl_pointer_interface, &s_interface)
    , seat(parent)
41
42
43
{
}

44
45
46
47
48
void PointerInterface::Private::setCursor(quint32 serial, SurfaceInterface *surface, const QPoint &hotspot)
{
    if (!cursor) {
        Q_Q(PointerInterface);
        cursor = new Cursor(q);
49
        cursor->d->update(QPointer<SurfaceInterface>(surface), serial, hotspot);
50
51
52
53
54
55
        QObject::connect(cursor, &Cursor::changed, q, &PointerInterface::cursorChanged);
        emit q->cursorChanged();
    } else {
        cursor->d->update(QPointer<SurfaceInterface>(surface), serial, hotspot);
    }
}
56

57
58
59
60
61
62
63
64
65
66
void PointerInterface::Private::sendLeave(SurfaceInterface *surface, quint32 serial)
{
    if (!surface) {
        return;
    }
    if (resource && surface->resource()) {
        wl_pointer_send_leave(resource, serial, surface->resource());
    }
}

67
68
69
70
71
72
73
74
75
76
void PointerInterface::Private::registerRelativePointer(RelativePointerInterface *relativePointer)
{
    relativePointers << relativePointer;
    QObject::connect(relativePointer, &QObject::destroyed, q,
        [this, relativePointer] {
            relativePointers.removeOne(relativePointer);
        }
    );
}

77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

void PointerInterface::Private::registerSwipeGesture(PointerSwipeGestureInterface *gesture)
{
    swipeGestures << gesture;
    QObject::connect(gesture, &QObject::destroyed, q,
        [this, gesture] {
            swipeGestures.removeOne(gesture);
        }
    );
}

void PointerInterface::Private::registerPinchGesture(PointerPinchGestureInterface *gesture)
{
    pinchGestures << gesture;
    QObject::connect(gesture, &QObject::destroyed, q,
        [this, gesture] {
            pinchGestures.removeOne(gesture);
        }
    );
}

98
99
100
101
102
103
104
105
106
107
108
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)
{
109
    if (!surface || !surface->resource()) {
110
111
112
113
114
115
116
117
        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()));
}

118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
void PointerInterface::Private::startSwipeGesture(quint32 serial, quint32 fingerCount)
{
    if (swipeGestures.isEmpty()) {
        return;
    }
    for (auto it = swipeGestures.constBegin(), end = swipeGestures.constEnd(); it != end; it++) {
        (*it)->start(serial, fingerCount);
    }
}

void PointerInterface::Private::updateSwipeGesture(const QSizeF &delta)
{
    if (swipeGestures.isEmpty()) {
        return;
    }
    for (auto it = swipeGestures.constBegin(), end = swipeGestures.constEnd(); it != end; it++) {
        (*it)->update(delta);
    }
}

void PointerInterface::Private::endSwipeGesture(quint32 serial)
{
    if (swipeGestures.isEmpty()) {
        return;
    }
    for (auto it = swipeGestures.constBegin(), end = swipeGestures.constEnd(); it != end; it++) {
        (*it)->end(serial);
    }
}

void PointerInterface::Private::cancelSwipeGesture(quint32 serial)
{
    if (swipeGestures.isEmpty()) {
        return;
    }
    for (auto it = swipeGestures.constBegin(), end = swipeGestures.constEnd(); it != end; it++) {
        (*it)->cancel(serial);
    }
}

void PointerInterface::Private::startPinchGesture(quint32 serial, quint32 fingerCount)
{
    if (pinchGestures.isEmpty()) {
        return;
    }
    for (auto it = pinchGestures.constBegin(), end = pinchGestures.constEnd(); it != end; it++) {
        (*it)->start(serial, fingerCount);
    }
}

void PointerInterface::Private::updatePinchGesture(const QSizeF &delta, qreal scale, qreal rotation)
{
    if (pinchGestures.isEmpty()) {
        return;
    }
    for (auto it = pinchGestures.constBegin(), end = pinchGestures.constEnd(); it != end; it++) {
        (*it)->update(delta, scale, rotation);
    }
}

void PointerInterface::Private::endPinchGesture(quint32 serial)
{
    if (pinchGestures.isEmpty()) {
        return;
    }
    for (auto it = pinchGestures.constBegin(), end = pinchGestures.constEnd(); it != end; it++) {
        (*it)->end(serial);
    }
}

void PointerInterface::Private::cancelPinchGesture(quint32 serial)
{
    if (pinchGestures.isEmpty()) {
        return;
    }
    for (auto it = pinchGestures.constBegin(), end = pinchGestures.constEnd(); it != end; it++) {
        (*it)->cancel(serial);
    }
}

198
199
200
201
202
203
204
205
void PointerInterface::Private::sendFrame()
{
    if (!resource || wl_resource_get_version(resource) < WL_POINTER_FRAME_SINCE_VERSION) {
        return;
    }
    wl_pointer_send_frame(resource);
}

206
#ifndef K_DOXYGEN
207
208
const struct wl_pointer_interface PointerInterface::Private::s_interface = {
    setCursorCallback,
209
    resourceDestroyedCallback
210
};
Martin Flöser's avatar
Martin Flöser committed
211
#endif
212

213
PointerInterface::PointerInterface(SeatInterface *parent, wl_resource *parentResource)
214
    : Resource(new Private(parent, parentResource, this))
215
{
216
    // TODO: handle touch
217
    connect(parent, &SeatInterface::pointerPosChanged, this, [this] {
218
        Q_D();
219
        if (!d->focusedSurface || !d->resource) {
220
221
            return;
        }
222
223
224
225
226
        if (d->seat->isDragPointer()) {
            const auto *originSurface = d->seat->dragSource()->origin();
            const bool proxyRemoteFocused = originSurface->dataProxy() && originSurface == d->focusedSurface;
            if (!proxyRemoteFocused) {
                // handled by DataDevice
227
228
                return;
            }
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
        }
        if (!d->focusedSurface->lockedPointer().isNull() && d->focusedSurface->lockedPointer()->isLocked()) {
            return;
        }
        const QPointF pos = d->seat->focusedPointerSurfaceTransformation().map(d->seat->pointerPos());
        auto targetSurface = d->focusedSurface->inputSurfaceAt(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->sendFrame();
            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()));
            d->sendFrame();
250
251
        }
    });
252
253
}

254
PointerInterface::~PointerInterface() = default;
255

256
void PointerInterface::setFocusedSurface(SurfaceInterface *surface, quint32 serial)
257
{
258
    Q_D();
259
260
    d->sendLeave(d->focusedChildSurface.data(), serial);
    disconnect(d->destroyConnection);
261
262
    if (!surface) {
        d->focusedSurface = nullptr;
263
        d->focusedChildSurface.clear();
264
265
        return;
    }
266
    d->focusedSurface = surface;
267
    d->destroyConnection = connect(d->focusedSurface, &SurfaceInterface::aboutToBeDestroyed, this,
268
269
        [this] {
            Q_D();
270
            d->sendLeave(d->focusedChildSurface.data(), d->global->display()->nextSerial());
271
            d->sendFrame();
272
            d->focusedSurface = nullptr;
273
            d->focusedChildSurface.clear();
274
275
        }
    );
276

277
    const QPointF pos = d->seat->focusedPointerSurfaceTransformation().map(d->seat->pointerPos());
278
    d->focusedChildSurface = QPointer<SurfaceInterface>(d->focusedSurface->inputSurfaceAt(pos));
279
280
281
282
    if (!d->focusedChildSurface) {
        d->focusedChildSurface = QPointer<SurfaceInterface>(d->focusedSurface);
    }
    d->sendEnter(d->focusedChildSurface.data(), pos, serial);
283
    d->client->flush();
284
285
}

286
void PointerInterface::buttonPressed(quint32 button, quint32 serial)
287
{
288
    Q_D();
289
    Q_ASSERT(d->focusedSurface);
290
291
292
    if (!d->resource) {
        return;
    }
293
    wl_pointer_send_button(d->resource, serial, d->seat->timestamp(), button, WL_POINTER_BUTTON_STATE_PRESSED);
294
    d->sendFrame();
295
296
}

297
void PointerInterface::buttonReleased(quint32 button, quint32 serial)
298
{
299
    Q_D();
300
    Q_ASSERT(d->focusedSurface);
301
302
303
    if (!d->resource) {
        return;
    }
304
    wl_pointer_send_button(d->resource, serial, d->seat->timestamp(), button, WL_POINTER_BUTTON_STATE_RELEASED);
305
    d->sendFrame();
306
307
}

308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
void PointerInterface::axis(Qt::Orientation orientation, qreal delta, qint32 discreteDelta, PointerAxisSource source)
{
    Q_D();
    Q_ASSERT(d->focusedSurface);
    if (!d->resource) {
        return;
    }

    const quint32 version = wl_resource_get_version(d->resource);

    const auto wlOrientation = (orientation == Qt::Vertical)
        ? WL_POINTER_AXIS_VERTICAL_SCROLL
        : WL_POINTER_AXIS_HORIZONTAL_SCROLL;

    if (source != PointerAxisSource::Unknown && version >= WL_POINTER_AXIS_SOURCE_SINCE_VERSION) {
        wl_pointer_axis_source wlSource;
        switch (source) {
        case PointerAxisSource::Wheel:
            wlSource = WL_POINTER_AXIS_SOURCE_WHEEL;
            break;
        case PointerAxisSource::Finger:
            wlSource = WL_POINTER_AXIS_SOURCE_FINGER;
            break;
        case PointerAxisSource::Continuous:
            wlSource = WL_POINTER_AXIS_SOURCE_CONTINUOUS;
            break;
        case PointerAxisSource::WheelTilt:
            wlSource = WL_POINTER_AXIS_SOURCE_WHEEL_TILT;
            break;
        default:
            Q_UNREACHABLE();
            break;
        }
        wl_pointer_send_axis_source(d->resource, wlSource);
    }

    if (delta != 0.0) {
        if (discreteDelta && version >= WL_POINTER_AXIS_DISCRETE_SINCE_VERSION) {
            wl_pointer_send_axis_discrete(d->resource, wlOrientation, discreteDelta);
        }
        wl_pointer_send_axis(d->resource, d->seat->timestamp(), wlOrientation, wl_fixed_from_double(delta));
    } else if (version >= WL_POINTER_AXIS_STOP_SINCE_VERSION) {
        wl_pointer_send_axis_stop(d->resource, d->seat->timestamp(), wlOrientation);
    }

    d->sendFrame();
}

356
357
void PointerInterface::axis(Qt::Orientation orientation, quint32 delta)
{
358
    Q_D();
359
    Q_ASSERT(d->focusedSurface);
360
361
362
    if (!d->resource) {
        return;
    }
363
    wl_pointer_send_axis(d->resource, d->seat->timestamp(),
364
365
                         (orientation == Qt::Vertical) ? WL_POINTER_AXIS_VERTICAL_SCROLL : WL_POINTER_AXIS_HORIZONTAL_SCROLL,
                         wl_fixed_from_int(delta));
366
    d->sendFrame();
367
368
369
370
371
}

void PointerInterface::Private::setCursorCallback(wl_client *client, wl_resource *resource, uint32_t serial,
                                         wl_resource *surface, int32_t hotspot_x, int32_t hotspot_y)
{
372
373
374
    auto p = cast<Private>(resource);
    Q_ASSERT(p->client->client() == client);
    p->setCursor(serial, SurfaceInterface::get(surface), QPoint(hotspot_x, hotspot_y));
375
376
}

377
378
379
380
381
382
Cursor *PointerInterface::cursor() const
{
    Q_D();
    return d->cursor;
}

383
384
385
386
387
388
389
390
391
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);
    }
392
    d->sendFrame();
393
394
}

395
PointerInterface::Private *PointerInterface::d_func() const
396
{
397
    return reinterpret_cast<Private*>(d.data());
398
399
}

400
401
402
403
404
PointerInterface *PointerInterface::get(wl_resource *native)
{
    return Private::get<PointerInterface>(native);
}

405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
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;
}

468
}