seat_interface.cpp 52 KB
Newer Older
1
2
/*
    SPDX-FileCopyrightText: 2014 Martin Gräßlin <mgraesslin@kde.org>
David Edmundson's avatar
David Edmundson committed
3
    SPDX-FileCopyrightText: 2020 David Edmundson <davidedmundson@kde.org>
4

5
6
    SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/
7
#include "abstract_data_source.h"
8
#include "seat_interface.h"
9
#include "seat_interface_p.h"
10
#include "display.h"
11
12
#include "datadevice_interface.h"
#include "datasource_interface.h"
13
14
#include "datacontroldevice_v1_interface.h"
#include "datacontrolsource_v1_interface.h"
15
#include "keyboard_interface.h"
16
#include "keyboard_interface_p.h"
17
#include "pointer_interface.h"
18
#include "pointer_interface_p.h"
19
20
#include "primaryselectiondevice_v1_interface.h"
#include "primaryselectionsource_v1_interface.h"
21
#include "surface_interface.h"
22
#include "textinput_interface_p.h"
23
24
// Qt
#include <QFile>
25
// Wayland
26
27
28
#ifndef WL_SEAT_NAME_SINCE_VERSION
#define WL_SEAT_NAME_SINCE_VERSION 2
#endif
29
// linux
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
30
#include <config-kwaylandserver.h>
31
#if HAVE_LINUX_INPUT_H
32
#include <linux/input.h>
33
#endif
34

35
36
#include <functional>

37
namespace KWaylandServer
38
39
{

40
41
42
43
const quint32 SeatInterface::Private::s_version = 5;
const qint32 SeatInterface::Private::s_pointerVersion = 5;
const qint32 SeatInterface::Private::s_touchVersion = 5;
const qint32 SeatInterface::Private::s_keyboardVersion = 5;
44

45
SeatInterface::Private::Private(SeatInterface *q, Display *display)
46
    : Global::Private(display, &wl_seat_interface, s_version)
47
    , q(q)
48
49
50
{
}

51
#ifndef K_DOXYGEN
52
53
54
const struct wl_seat_interface SeatInterface::Private::s_interface = {
    getPointerCallback,
    getKeyboardCallback,
55
56
    getTouchCallback,
    releaseCallback
57
};
Martin Flöser's avatar
Martin Flöser committed
58
#endif
59
60

SeatInterface::SeatInterface(Display *display, QObject *parent)
61
    : Global(new Private(this, display), parent)
62
{
63
    Q_D();
64
    connect(this, &SeatInterface::nameChanged, this,
65
        [d] {
66
67
            for (auto it = d->resources.constBegin(); it != d->resources.constEnd(); ++it) {
                d->sendName(*it);
68
69
70
            }
        }
    );
71
    auto sendCapabilitiesAll = [d] {
72
73
        for (auto it = d->resources.constBegin(); it != d->resources.constEnd(); ++it) {
            d->sendCapabilities(*it);
74
75
76
77
78
79
80
81
82
        }
    };
    connect(this, &SeatInterface::hasPointerChanged,  this, sendCapabilitiesAll);
    connect(this, &SeatInterface::hasKeyboardChanged, this, sendCapabilitiesAll);
    connect(this, &SeatInterface::hasTouchChanged,    this, sendCapabilitiesAll);
}

SeatInterface::~SeatInterface()
{
83
    Q_D();
84
85
    while (!d->resources.isEmpty()) {
        wl_resource_destroy(d->resources.takeLast());
86
87
88
    }
}

89
void SeatInterface::Private::bind(wl_client *client, uint32_t version, uint32_t id)
90
91
92
93
94
95
{
    wl_resource *r = wl_resource_create(client, &wl_seat_interface, qMin(s_version, version), id);
    if (!r) {
        wl_client_post_no_memory(client);
        return;
    }
96
    resources << r;
97

98
    wl_resource_set_implementation(r, &s_interface, this, unbind);
99
100
101
102
103

    sendCapabilities(r);
    sendName(r);
}

104
void SeatInterface::Private::unbind(wl_resource *r)
105
{
106
    cast(r)->resources.removeAll(r);
107
108
}

109
110
111
112
113
114
void SeatInterface::Private::releaseCallback(wl_client *client, wl_resource *resource)
{
    Q_UNUSED(client)
    wl_resource_destroy(resource);
}

115
116
117
118
119
120
121
122
123
124
void SeatInterface::Private::updatePointerButtonSerial(quint32 button, quint32 serial)
{
    auto it = globalPointer.buttonSerials.find(button);
    if (it == globalPointer.buttonSerials.end()) {
        globalPointer.buttonSerials.insert(button, serial);
        return;
    }
    it.value() = serial;
}

125
void SeatInterface::Private::updatePointerButtonState(quint32 button, Pointer::State state)
126
127
128
129
130
131
132
133
134
{
    auto it = globalPointer.buttonStates.find(button);
    if (it == globalPointer.buttonStates.end()) {
        globalPointer.buttonStates.insert(button, state);
        return;
    }
    it.value() = state;
}

135
bool SeatInterface::Private::updateKey(quint32 key, Keyboard::State state)
Martin Flöser's avatar
Martin Flöser committed
136
137
138
139
{
    auto it = keys.states.find(key);
    if (it == keys.states.end()) {
        keys.states.insert(key, state);
140
141
142
143
        return true;
    }
    if (it.value() == state) {
        return false;
Martin Flöser's avatar
Martin Flöser committed
144
145
    }
    it.value() = state;
146
    return true;
Martin Flöser's avatar
Martin Flöser committed
147
148
}

149
void SeatInterface::Private::sendName(wl_resource *r)
150
151
152
153
{
    if (wl_resource_get_version(r) < WL_SEAT_NAME_SINCE_VERSION) {
        return;
    }
154
    wl_seat_send_name(r, name.toUtf8().constData());
155
156
}

157
void SeatInterface::Private::sendCapabilities(wl_resource *r)
158
159
{
    uint32_t capabilities = 0;
160
    if (pointer) {
161
162
        capabilities |= WL_SEAT_CAPABILITY_POINTER;
    }
163
    if (keyboard) {
164
165
        capabilities |= WL_SEAT_CAPABILITY_KEYBOARD;
    }
166
    if (touch) {
167
168
169
170
171
        capabilities |= WL_SEAT_CAPABILITY_TOUCH;
    }
    wl_seat_send_capabilities(r, capabilities);
}

172
SeatInterface::Private *SeatInterface::Private::cast(wl_resource *r)
173
{
174
    return r ? reinterpret_cast<SeatInterface::Private*>(wl_resource_get_user_data(r)) : nullptr;
175
176
}

177
namespace {
178
179
180
template <typename T>
static
T *interfaceForSurface(SurfaceInterface *surface, const QVector<T*> &interfaces)
181
182
183
184
185
{
    if (!surface) {
        return nullptr;
    }

Frederik Gladhorn's avatar
Frederik Gladhorn committed
186
    for (auto it = interfaces.constBegin(); it != interfaces.constEnd(); ++it) {
187
        if ((*it)->client() == surface->client()) {
188
189
190
191
192
            return (*it);
        }
    }
    return nullptr;
}
193
194
195
196
197
198
199
200
201
202

template <typename T>
static
QVector<T *> interfacesForSurface(SurfaceInterface *surface, const QVector<T*> &interfaces)
{
    QVector<T *> ret;
    if (!surface) {
        return ret;
    }

Frederik Gladhorn's avatar
Frederik Gladhorn committed
203
    for (auto it = interfaces.constBegin(); it != interfaces.constEnd(); ++it) {
204
205
206
207
208
209
        if ((*it)->client() == surface->client() && (*it)->resource()) {
            ret << *it;
        }
    }
    return ret;
}
210
211
212
213
214
215
216
217
218

template <typename T>
static
bool forEachInterface(SurfaceInterface *surface, const QVector<T*> &interfaces, std::function<void (T*)> method)
{
    if (!surface) {
        return false;
    }
    bool calledAtLeastOne = false;
Frederik Gladhorn's avatar
Frederik Gladhorn committed
219
    for (auto it = interfaces.constBegin(); it != interfaces.constEnd(); ++it) {
220
221
222
223
224
225
226
227
        if ((*it)->client() == surface->client() && (*it)->resource()) {
            method(*it);
            calledAtLeastOne = true;
        }
    }
    return calledAtLeastOne;
}

228
}
229

230
QVector<PointerInterface *> SeatInterface::Private::pointersForSurface(SurfaceInterface *surface) const
Martin Flöser's avatar
Martin Flöser committed
231
{
232
    return interfacesForSurface(surface, pointers);
233
}
Martin Flöser's avatar
Martin Flöser committed
234

235
QVector<KeyboardInterface *> SeatInterface::Private::keyboardsForSurface(SurfaceInterface *surface) const
236
{
237
    return interfacesForSurface(surface, keyboards);
Martin Flöser's avatar
Martin Flöser committed
238
239
}

240
QVector<TouchInterface *> SeatInterface::Private::touchsForSurface(SurfaceInterface *surface) const
241
{
242
    return interfacesForSurface(surface, touchs);
243
244
}

245
QVector<DataDeviceInterface *> SeatInterface::Private::dataDevicesForSurface(SurfaceInterface *surface) const
246
{
247
    return interfacesForSurface(surface, dataDevices);
248
249
}

250
251
252
253
254
TextInputInterface *SeatInterface::Private::textInputForSurface(SurfaceInterface *surface) const
{
    return interfaceForSurface(surface, textInputs);
}

255
256
257
258
void SeatInterface::Private::registerDataDevice(DataDeviceInterface *dataDevice)
{
    Q_ASSERT(dataDevice->seat() == q);
    dataDevices << dataDevice;
259
260
    auto dataDeviceCleanup = [this, dataDevice] {
        dataDevices.removeOne(dataDevice);
261
        keys.focus.selections.removeOne(dataDevice);
262
263
264
    };
    QObject::connect(dataDevice, &QObject::destroyed, q, dataDeviceCleanup);
    QObject::connect(dataDevice, &Resource::unbound, q, dataDeviceCleanup);
265
266
    QObject::connect(dataDevice, &DataDeviceInterface::selectionChanged, q,
        [this, dataDevice] {
267
            updateSelection(dataDevice);
268
269
270
271
        }
    );
    QObject::connect(dataDevice, &DataDeviceInterface::selectionCleared, q,
        [this, dataDevice] {
272
            updateSelection(dataDevice);
273
274
        }
    );
275
276
    QObject::connect(dataDevice, &DataDeviceInterface::dragStarted, q,
        [this, dataDevice] {
Roman Gilg's avatar
Roman Gilg committed
277
278
279
            const auto dragSerial = dataDevice->dragImplicitGrabSerial();
            auto *dragSurface = dataDevice->origin();
            if (q->hasImplicitPointerGrab(dragSerial)) {
280
                drag.mode = Drag::Mode::Pointer;
Roman Gilg's avatar
Roman Gilg committed
281
282
283
284
285
286
                drag.sourcePointer = interfaceForSurface(dragSurface, pointers);
                drag.transformation = globalPointer.focus.transformation;
            } else if (q->hasImplicitTouchGrab(dragSerial)) {
                drag.mode = Drag::Mode::Touch;
                drag.sourceTouch = interfaceForSurface(dragSurface, touchs);
                // TODO: touch transformation
287
            } else {
Roman Gilg's avatar
Roman Gilg committed
288
                // no implicit grab, abort drag
289
290
                return;
            }
291
292
293
294
295
296
297
298
299
            auto *originSurface = dataDevice->origin();
            const bool proxied = originSurface->dataProxy();
            if (!proxied) {
                // origin surface
                drag.target = dataDevice;
                drag.surface = originSurface;
                // TODO: transformation needs to be either pointer or touch
                drag.transformation = globalPointer.focus.transformation;
            }
300
            drag.source = dataDevice;
301
            drag.sourcePointer = interfaceForSurface(originSurface, pointers);
302
303
304
305
306
            drag.destroyConnection = QObject::connect(dataDevice, &QObject::destroyed, q,
                [this] {
                    endDrag(display->nextSerial());
                }
            );
307
308
309
310
311
312
313
314
315
316
317
318
319
320
            if (dataDevice->dragSource()) {
                drag.dragSourceDestroyConnection = QObject::connect(dataDevice->dragSource(), &Resource::aboutToBeUnbound, q,
                    [this] {
                        const auto serial = display->nextSerial();
                        if (drag.target) {
                            drag.target->updateDragTarget(nullptr, serial);
                            drag.target = nullptr;
                        }
                        endDrag(serial);
                    }
                );
            } else {
                drag.dragSourceDestroyConnection = QMetaObject::Connection();
            }
321
            dataDevice->updateDragTarget(proxied ? nullptr : originSurface, dataDevice->dragImplicitGrabSerial());
322
323
324
325
            emit q->dragStarted();
            emit q->dragSurfaceChanged();
        }
    );
326
    // is the new DataDevice for the current keyoard focus?
327
    if (keys.focus.surface) {
328
329
        // same client?
        if (keys.focus.surface->client() == dataDevice->client()) {
330
            keys.focus.selections.append(dataDevice);
331
            if (currentSelection) {
332
333
334
335
336
337
                dataDevice->sendSelection(currentSelection);
            }
        }
    }
}

338
void SeatInterface::Private::registerDataControlDevice(DataControlDeviceV1Interface *dataDevice)
David Edmundson's avatar
David Edmundson committed
339
340
341
342
343
344
345
346
{
    Q_ASSERT(dataDevice->seat() == q);
    dataControlDevices << dataDevice;
    auto dataDeviceCleanup = [this, dataDevice] {
        dataControlDevices.removeOne(dataDevice);
    };
    QObject::connect(dataDevice, &QObject::destroyed, q, dataDeviceCleanup);

347
    QObject::connect(dataDevice, &DataControlDeviceV1Interface::selectionChanged, q,
David Edmundson's avatar
David Edmundson committed
348
349
350
351
352
        [this, dataDevice] {
            q->setSelection(dataDevice->selection());
        }
    );

353
    QObject::connect(dataDevice, &DataControlDeviceV1Interface::selectionCleared, q,
David Edmundson's avatar
David Edmundson committed
354
355
356
357
358
359
360
361
362
363
        [this, dataDevice] {
            Q_UNUSED(dataDevice);
            q->setSelection(nullptr);
        }
    );
    if (currentSelection) {
        dataDevice->sendSelection(currentSelection);
    }
}

364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
void SeatInterface::Private::registerPrimarySelectionDevice(PrimarySelectionDeviceV1Interface *primarySelectionDevice)
{
    Q_ASSERT(primarySelectionDevice->seat() == q);

    primarySelectionDevices << primarySelectionDevice;
    auto dataDeviceCleanup = [this, primarySelectionDevice] {
        primarySelectionDevices.removeOne(primarySelectionDevice);
        keys.focus.primarySelections.removeOne(primarySelectionDevice);
    };
    QObject::connect(primarySelectionDevice, &QObject::destroyed, q, dataDeviceCleanup);
    QObject::connect(primarySelectionDevice, &PrimarySelectionDeviceV1Interface::selectionChanged, q,
        [this, primarySelectionDevice] {
            updatePrimarySelection(primarySelectionDevice);
        }
    );
    QObject::connect(primarySelectionDevice, &PrimarySelectionDeviceV1Interface::selectionCleared, q,
        [this, primarySelectionDevice] {
            updatePrimarySelection(primarySelectionDevice);
        }
    );
    // is the new DataDevice for the current keyoard focus?
    if (keys.focus.surface) {
        // same client?
        if (*keys.focus.surface->client() == primarySelectionDevice->client()) {
            keys.focus.primarySelections.append(primarySelectionDevice);
            if (currentPrimarySelection) {
                primarySelectionDevice->sendSelection(currentPrimarySelection);
            }
        }
    }
}
David Edmundson's avatar
David Edmundson committed
395

396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
void SeatInterface::Private::registerTextInput(TextInputInterface *ti)
{
    // text input version 0 might call this multiple times
    if (textInputs.contains(ti)) {
        return;
    }
    textInputs << ti;
    if (textInput.focus.surface && textInput.focus.surface->client() == ti->client()) {
        // this is a text input for the currently focused text input surface
        if (!textInput.focus.textInput) {
            textInput.focus.textInput = ti;
            ti->d_func()->sendEnter(textInput.focus.surface, textInput.focus.serial);
            emit q->focusedTextInputChanged();
        }
    }
    QObject::connect(ti, &QObject::destroyed, q,
        [this, ti] {
            textInputs.removeAt(textInputs.indexOf(ti));
            if (textInput.focus.textInput == ti) {
                textInput.focus.textInput = nullptr;
                emit q->focusedTextInputChanged();
            }
        }
    );
}

422
423
424
425
void SeatInterface::Private::endDrag(quint32 serial)
{
    auto target = drag.target;
    QObject::disconnect(drag.destroyConnection);
426
427
    QObject::disconnect(drag.dragSourceDestroyConnection);
    if (drag.source && drag.source->dragSource()) {
428
429
        drag.source->dragSource()->dropPerformed();
    }
430
431
432
433
434
435
436
437
438
    if (target) {
        target->drop();
        target->updateDragTarget(nullptr, serial);
    }
    drag = Drag();
    emit q->dragSurfaceChanged();
    emit q->dragEnded();
}

439
void SeatInterface::Private::updateSelection(DataDeviceInterface *dataDevice)
440
{
441
442
    // if the update is from the focussed window we should inform the active client
    if (!(keys.focus.surface && (keys.focus.surface->client() == dataDevice->client()))) {
443
444
        return;
    }
445
    q->setSelection(dataDevice->selection());
446
447
}

448
449
450
451
452
453
454
455
456
void SeatInterface::Private::updatePrimarySelection(PrimarySelectionDeviceV1Interface *primarySelectionDevice)
{
    // if the update is from the focussed window we should inform the active client
    if (!(keys.focus.surface && (*keys.focus.surface->client() == primarySelectionDevice->client()))) {
        return;
    }
    q->setPrimarySelection(primarySelectionDevice->selection());
}

457
458
void SeatInterface::setHasKeyboard(bool has)
{
459
    Q_D();
460
    if (d->keyboard == has) {
461
462
        return;
    }
463
464
    d->keyboard = has;
    emit hasKeyboardChanged(d->keyboard);
465
466
467
468
}

void SeatInterface::setHasPointer(bool has)
{
469
    Q_D();
470
    if (d->pointer == has) {
471
472
        return;
    }
473
474
    d->pointer = has;
    emit hasPointerChanged(d->pointer);
475
476
477
478
}

void SeatInterface::setHasTouch(bool has)
{
479
    Q_D();
480
    if (d->touch == has) {
481
482
        return;
    }
483
484
    d->touch = has;
    emit hasTouchChanged(d->touch);
485
486
487
488
}

void SeatInterface::setName(const QString &name)
{
489
    Q_D();
490
    if (d->name == name) {
491
492
        return;
    }
493
494
    d->name = name;
    emit nameChanged(d->name);
495
496
}

497
void SeatInterface::Private::getPointerCallback(wl_client *client, wl_resource *resource, uint32_t id)
498
{
499
500
501
502
503
504
    cast(resource)->getPointer(client, resource, id);
}

void SeatInterface::Private::getPointer(wl_client *client, wl_resource *resource, uint32_t id)
{
    // TODO: only create if seat has pointer?
505
    PointerInterface *pointer = new PointerInterface(q, resource);
506
507
    auto clientConnection = display->getConnection(client);
    pointer->create(clientConnection, qMin(wl_resource_get_version(resource), s_pointerVersion), id);
508
509
510
511
512
    if (!pointer->resource()) {
        wl_resource_post_no_memory(resource);
        delete pointer;
        return;
    }
513
    pointers << pointer;
514
    if (globalPointer.focus.surface && globalPointer.focus.surface->client() == clientConnection) {
515
        // this is a pointer for the currently focused pointer surface
516
517
        globalPointer.focus.pointers << pointer;
        pointer->setFocusedSurface(globalPointer.focus.surface, globalPointer.focus.serial);
518
        pointer->d_func()->sendFrame();
519
520
        if (globalPointer.focus.pointers.count() == 1) {
            // got a new pointer
521
            emit q->focusedPointerChanged(pointer);
522
523
524
525
526
        }
    }
    QObject::connect(pointer, &QObject::destroyed, q,
        [pointer,this] {
            pointers.removeAt(pointers.indexOf(pointer));
527
528
529
530
            if (globalPointer.focus.pointers.removeOne(pointer)) {
                if (globalPointer.focus.pointers.isEmpty()) {
                    emit q->focusedPointerChanged(nullptr);
                }
531
532
533
534
            }
        }
    );
    emit q->pointerCreated(pointer);
535
536
}

537
void SeatInterface::Private::getKeyboardCallback(wl_client *client, wl_resource *resource, uint32_t id)
538
{
Martin Flöser's avatar
Martin Flöser committed
539
540
541
542
543
544
    cast(resource)->getKeyboard(client, resource, id);
}

void SeatInterface::Private::getKeyboard(wl_client *client, wl_resource *resource, uint32_t id)
{
    // TODO: only create if seat has keyboard?
545
    KeyboardInterface *keyboard = new KeyboardInterface(q, resource);
546
547
    auto clientConnection = display->getConnection(client);
    keyboard->create(clientConnection, qMin(wl_resource_get_version(resource), s_keyboardVersion) , id);
548
549
550
551
552
    if (!keyboard->resource()) {
        wl_resource_post_no_memory(resource);
        delete keyboard;
        return;
    }
553
    keyboard->repeatInfo(keys.keyRepeat.charactersPerSecond, keys.keyRepeat.delay);
554
    if (keys.keymap.xkbcommonCompatible) {
555
        keyboard->setKeymap(keys.keymap.content);
556
    }
Martin Flöser's avatar
Martin Flöser committed
557
    keyboards << keyboard;
558
    if (keys.focus.surface && keys.focus.surface->client() == clientConnection) {
Martin Flöser's avatar
Martin Flöser committed
559
        // this is a keyboard for the currently focused keyboard surface
560
561
        keys.focus.keyboards << keyboard;
        keyboard->setFocusedSurface(keys.focus.surface, keys.focus.serial);
Martin Flöser's avatar
Martin Flöser committed
562
563
564
565
    }
    QObject::connect(keyboard, &QObject::destroyed, q,
        [keyboard,this] {
            keyboards.removeAt(keyboards.indexOf(keyboard));
566
            keys.focus.keyboards.removeOne(keyboard);
Martin Flöser's avatar
Martin Flöser committed
567
568
569
        }
    );
    emit q->keyboardCreated(keyboard);
570
571
}

572
void SeatInterface::Private::getTouchCallback(wl_client *client, wl_resource *resource, uint32_t id)
573
{
574
575
576
577
578
579
580
    cast(resource)->getTouch(client, resource, id);
}

void SeatInterface::Private::getTouch(wl_client *client, wl_resource *resource, uint32_t id)
{
    // TODO: only create if seat has touch?
    TouchInterface *touch = new TouchInterface(q, resource);
581
582
    auto clientConnection = display->getConnection(client);
    touch->create(clientConnection, qMin(wl_resource_get_version(resource), s_touchVersion), id);
583
584
585
586
587
588
    if (!touch->resource()) {
        wl_resource_post_no_memory(resource);
        delete touch;
        return;
    }
    touchs << touch;
589
    if (globalTouch.focus.surface && globalTouch.focus.surface->client() == clientConnection) {
590
        // this is a touch for the currently focused touch surface
591
592
593
        globalTouch.focus.touchs << touch;
        if (!globalTouch.ids.isEmpty()) {
            // TODO: send out all the points
594
595
596
597
598
        }
    }
    QObject::connect(touch, &QObject::destroyed, q,
        [touch,this] {
            touchs.removeAt(touchs.indexOf(touch));
599
            globalTouch.focus.touchs.removeOne(touch);
600
601
602
        }
    );
    emit q->touchCreated(touch);
603
604
}

605
606
QString SeatInterface::name() const
{
607
    Q_D();
608
609
610
611
612
    return d->name;
}

bool SeatInterface::hasPointer() const
{
613
    Q_D();
614
615
616
617
618
    return d->pointer;
}

bool SeatInterface::hasKeyboard() const
{
619
    Q_D();
620
621
622
623
624
    return d->keyboard;
}

bool SeatInterface::hasTouch() const
{
625
    Q_D();
626
627
628
    return d->touch;
}

629
630
631
632
633
SeatInterface *SeatInterface::get(wl_resource *native)
{
    return Private::get(native);
}

634
635
636
637
638
SeatInterface::Private *SeatInterface::d_func() const
{
    return reinterpret_cast<Private*>(d.data());
}

639
640
641
QPointF SeatInterface::pointerPos() const
{
    Q_D();
642
    return d->globalPointer.pos;
643
644
645
646
647
}

void SeatInterface::setPointerPos(const QPointF &pos)
{
    Q_D();
648
    if (d->globalPointer.pos == pos) {
649
650
        return;
    }
651
    d->globalPointer.pos = pos;
652
653
654
    emit pointerPosChanged(pos);
}

655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
quint32 SeatInterface::timestamp() const
{
    Q_D();
    return d->timestamp;
}

void SeatInterface::setTimestamp(quint32 time)
{
    Q_D();
    if (d->timestamp == time) {
        return;
    }
    d->timestamp = time;
    emit timestampChanged(time);
}

671
672
673
674
675
676
677
678
679
680
681
void SeatInterface::setDragTarget(SurfaceInterface *surface, const QPointF &globalPosition, const QMatrix4x4 &inputTransformation)
{
    Q_D();
    if (surface == d->drag.surface) {
        // no change
        return;
    }
    const quint32 serial = d->display->nextSerial();
    if (d->drag.target) {
        d->drag.target->updateDragTarget(nullptr, serial);
    }
682

David Edmundson's avatar
David Edmundson committed
683

684
    // TODO: technically we can have mulitple data devices
685
686
687
688
689
690
691
    // and we should send the drag to all of them, but that seems overly complicated
    // in practice so far the only case for mulitple data devices is for clipboard overriding
    d->drag.target = nullptr;
    if (d->dataDevicesForSurface(surface).size() > 0) {
        d->drag.target = d->dataDevicesForSurface(surface).first();
    }

692
693
    if (d->drag.mode == Private::Drag::Mode::Pointer) {
        setPointerPos(globalPosition);
Roman Gilg's avatar
Roman Gilg committed
694
695
696
    } else if (d->drag.mode == Private::Drag::Mode::Touch &&
               d->globalTouch.focus.firstTouchPos != globalPosition) {
        touchMove(d->globalTouch.ids.first(), globalPosition);
697
698
699
700
701
702
703
704
705
706
707
708
709
710
    }
    if (d->drag.target) {
        d->drag.surface = surface;
        d->drag.transformation = inputTransformation;
        d->drag.target->updateDragTarget(surface, serial);
    } else {
        d->drag.surface = nullptr;
    }
    emit dragSurfaceChanged();
    return;
}

void SeatInterface::setDragTarget(SurfaceInterface *surface, const QMatrix4x4 &inputTransformation)
{
Roman Gilg's avatar
Roman Gilg committed
711
712
713
714
715
716
717
718
    Q_D();
    if (d->drag.mode == Private::Drag::Mode::Pointer) {
        setDragTarget(surface, pointerPos(), inputTransformation);
    } else {
        Q_ASSERT(d->drag.mode == Private::Drag::Mode::Touch);
        setDragTarget(surface, d->globalTouch.focus.firstTouchPos, inputTransformation);
    }

719
720
}

721
722
723
SurfaceInterface *SeatInterface::focusedPointerSurface() const
{
    Q_D();
724
    return d->globalPointer.focus.surface;
725
726
}

727
void SeatInterface::setFocusedPointerSurface(SurfaceInterface *surface, const QPointF &surfacePosition)
728
729
730
731
732
733
734
735
736
737
738
{
    QMatrix4x4 m;
    m.translate(-surfacePosition.x(), -surfacePosition.y());
    setFocusedPointerSurface(surface, m);
    Q_D();
    if (d->globalPointer.focus.surface) {
        d->globalPointer.focus.offset = surfacePosition;
    }
}

void SeatInterface::setFocusedPointerSurface(SurfaceInterface *surface, const QMatrix4x4 &transformation)
739
740
{
    Q_D();
741
742
743
744
    if (d->drag.mode == Private::Drag::Mode::Pointer) {
        // ignore
        return;
    }
745
    const quint32 serial = d->display->nextSerial();
746
    QSet<PointerInterface *> framePointers;
747
748
    for (auto it = d->globalPointer.focus.pointers.constBegin(), end = d->globalPointer.focus.pointers.constEnd(); it != end; ++it) {
        (*it)->setFocusedSurface(nullptr, serial);
749
        framePointers << *it;
750
    }
751
752
    if (d->globalPointer.focus.surface) {
        disconnect(d->globalPointer.focus.destroyConnection);
753
    }
754
755
    d->globalPointer.focus = Private::Pointer::Focus();
    d->globalPointer.focus.surface = surface;
756
757
    auto p = d->pointersForSurface(surface);
    d->globalPointer.focus.pointers = p;
758
759
    if (d->globalPointer.focus.surface) {
        d->globalPointer.focus.destroyConnection = connect(surface, &QObject::destroyed, this,
760
761
            [this] {
                Q_D();
762
                d->globalPointer.focus = Private::Pointer::Focus();
763
                emit focusedPointerChanged(nullptr);
764
765
            }
        );
766
767
        d->globalPointer.focus.offset = QPointF();
        d->globalPointer.focus.transformation = transformation;
768
        d->globalPointer.focus.serial = serial;
769
    }
770
771
    if (p.isEmpty()) {
        emit focusedPointerChanged(nullptr);
772
773
774
775
        for (auto p : qAsConst(framePointers))
        {
            p->d_func()->sendFrame();
        }
776
777
        return;
    }
778
779
780
781
    // TODO: signal with all pointers
    emit focusedPointerChanged(p.first());
    for (auto it = p.constBegin(), end = p.constEnd(); it != end; ++it) {
        (*it)->setFocusedSurface(surface, serial);
782
783
784
785
786
        framePointers << *it;
    }
    for (auto p : qAsConst(framePointers))
    {
        p->d_func()->sendFrame();
787
    }
788
789
}

790
791
792
PointerInterface *SeatInterface::focusedPointer() const
{
    Q_D();
793
794
795
796
    if (d->globalPointer.focus.pointers.isEmpty()) {
        return nullptr;
    }
    return d->globalPointer.focus.pointers.first();
797
798
799
800
801
}

void SeatInterface::setFocusedPointerSurfacePosition(const QPointF &surfacePosition)
{
    Q_D();
802
803
    if (d->globalPointer.focus.surface) {
        d->globalPointer.focus.offset = surfacePosition;
804
805
        d->globalPointer.focus.transformation = QMatrix4x4();
        d->globalPointer.focus.transformation.translate(-surfacePosition.x(), -surfacePosition.y());
806
807
808
809
810
811
    }
}

QPointF SeatInterface::focusedPointerSurfacePosition() const
{
    Q_D();
812
    return d->globalPointer.focus.offset;
813
814
}

815
816
817
818
819
820
821
822
823
824
825
826
827
828
void SeatInterface::setFocusedPointerSurfaceTransformation(const QMatrix4x4 &transformation)
{
    Q_D();
    if (d->globalPointer.focus.surface) {
        d->globalPointer.focus.transformation = transformation;
    }
}

QMatrix4x4 SeatInterface::focusedPointerSurfaceTransformation() const
{
    Q_D();
    return d->globalPointer.focus.transformation;
}

829
namespace {
830
831
static quint32 qtToWaylandButton(Qt::MouseButton button)
{
832
#if HAVE_LINUX_INPUT_H
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
    static const QHash<Qt::MouseButton, quint32> s_buttons({
        {Qt::LeftButton, BTN_LEFT},
        {Qt::RightButton, BTN_RIGHT},
        {Qt::MiddleButton, BTN_MIDDLE},
        {Qt::ExtraButton1, BTN_BACK},    // note: QtWayland maps BTN_SIDE
        {Qt::ExtraButton2, BTN_FORWARD}, // note: QtWayland maps BTN_EXTRA
        {Qt::ExtraButton3, BTN_TASK},    // note: QtWayland maps BTN_FORWARD
        {Qt::ExtraButton4, BTN_EXTRA},   // note: QtWayland maps BTN_BACK
        {Qt::ExtraButton5, BTN_SIDE},    // note: QtWayland maps BTN_TASK
        {Qt::ExtraButton6, BTN_TASK + 1},
        {Qt::ExtraButton7, BTN_TASK + 2},
        {Qt::ExtraButton8, BTN_TASK + 3},
        {Qt::ExtraButton9, BTN_TASK + 4},
        {Qt::ExtraButton10, BTN_TASK + 5},
        {Qt::ExtraButton11, BTN_TASK + 6},
        {Qt::ExtraButton12, BTN_TASK + 7},
        {Qt::ExtraButton13, BTN_TASK + 8}
        // further mapping not possible, 0x120 is BTN_JOYSTICK
    });
    return s_buttons.value(button, 0);
853
854
855
#else
    return 0;
#endif
Martin Flöser's avatar
Martin Flöser committed
856
}
857
}
858
859
860
861
862
863
864
865
866
867
868
869
870

bool SeatInterface::isPointerButtonPressed(Qt::MouseButton button) const
{
    return isPointerButtonPressed(qtToWaylandButton(button));
}

bool SeatInterface::isPointerButtonPressed(quint32 button) const
{
    Q_D();
    auto it = d->globalPointer.buttonStates.constFind(button);
    if (it == d->globalPointer.buttonStates.constEnd()) {
        return false;
    }
871
    return it.value() == Private::Pointer::State::Pressed ? true : false;
872
873
}

874
875
876
877
878
879
880
881
882
883
884
885
886
887
void SeatInterface::pointerAxisV5(Qt::Orientation orientation, qreal delta, qint32 discreteDelta, PointerAxisSource source)
{
    Q_D();
    if (d->drag.mode == Private::Drag::Mode::Pointer) {
        // ignore
        return;
    }
    if (d->globalPointer.focus.surface) {
        for (auto it = d->globalPointer.focus.pointers.constBegin(), end = d->globalPointer.focus.pointers.constEnd(); it != end; ++it) {
            (*it)->axis(orientation, delta, discreteDelta, source);
        }
    }
}

888
889
890
void SeatInterface::pointerAxis(Qt::Orientation orientation, quint32 delta)
{
    Q_D();
891
892
893
894
    if (d->drag.mode == Private::Drag::Mode::Pointer) {
        // ignore
        return;
    }
895
896
897
898
    if (d->globalPointer.focus.surface) {
        for (auto it = d->globalPointer.focus.pointers.constBegin(), end = d->globalPointer.focus.pointers.constEnd(); it != end; ++it) {
            (*it)->axis(orientation, delta);
        }
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
    }
}

void SeatInterface::pointerButtonPressed(Qt::MouseButton button)
{
    const quint32 nativeButton = qtToWaylandButton(button);
    if (nativeButton == 0) {
        return;
    }
    pointerButtonPressed(nativeButton);
}

void SeatInterface::pointerButtonPressed(quint32 button)
{
    Q_D();
    const quint32 serial = d->display->nextSerial();
    d->updatePointerButtonSerial(button, serial);
916
    d->updatePointerButtonState(button, Private::Pointer::State::Pressed);
917
918
919
920
    if (d->drag.mode == Private::Drag::Mode::Pointer) {
        // ignore
        return;
    }
921
    if (auto *focusSurface = d->globalPointer.focus.surface) {
922
923
924
        for (auto it = d->globalPointer.focus.pointers.constBegin(), end = d->globalPointer.focus.pointers.constEnd(); it != end; ++it) {
            (*it)->buttonPressed(button, serial);
        }
925
        if (focusSurface == d->keys.focus.surface) {
926
            // update the focused child surface
927
928
929
930
931
            auto p = focusedPointer();
            if (p) {
                for (auto it = d->keys.focus.keyboards.constBegin(), end = d->keys.focus.keyboards.constEnd(); it != end; ++it) {
                    (*it)->d_func()->focusChildSurface(p->d_func()->focusedChildSurface, serial);
                }
932
            }
933
        }
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
    }
}

void SeatInterface::pointerButtonReleased(Qt::MouseButton button)
{
    const quint32 nativeButton = qtToWaylandButton(button);
    if (nativeButton == 0) {
        return;
    }
    pointerButtonReleased(nativeButton);
}

void SeatInterface::pointerButtonReleased(quint32 button)
{
    Q_D();
    const quint32 serial = d->display->nextSerial();
950
    const quint32 currentButtonSerial = pointerButtonSerial(button);
951
    d->updatePointerButtonSerial(button, serial);
952
    d->updatePointerButtonState(button, Private::Pointer::State::Released);
953
954
955
956
957
958
959
960
    if (d->drag.mode == Private::Drag::Mode::Pointer) {
        if (d->drag.source->dragImplicitGrabSerial() != currentButtonSerial) {
            // not our drag button - ignore
            return;
        }
        d->endDrag(serial);
        return;
    }
961
962
963
964
    if (d->globalPointer.focus.surface) {
        for (auto it = d->globalPointer.focus.pointers.constBegin(), end = d->globalPointer.focus.pointers.constEnd(); it != end; ++it) {
            (*it)->buttonReleased(button, serial);
        }
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
    }
}

quint32 SeatInterface::pointerButtonSerial(Qt::MouseButton button) const
{
    return pointerButtonSerial(qtToWaylandButton(button));
}

quint32 SeatInterface::pointerButtonSerial(quint32 button) const
{
    Q_D();
    auto it = d->globalPointer.buttonSerials.constFind(button);
    if (it == d->globalPointer.buttonSerials.constEnd()) {
        return 0;
    }
    return it.value();
}

983
984
985
void SeatInterface::relativePointerMotion(const QSizeF &delta, const QSizeF &deltaNonAccelerated, quint64 microseconds)
{
    Q_D();
986
987
988
989
    if (d->globalPointer.focus.surface) {
        for (auto it = d->globalPointer.focus.pointers.constBegin(), end = d->globalPointer.focus.pointers.constEnd(); it != end; ++it) {
            (*it)->relativeMotion(delta, deltaNonAccelerated, microseconds);
        }
990
991
992
    }
}

993
994
995
996
997
998
999
1000
1001
1002
1003
void SeatInterface::startPointerSwipeGesture(quint32 fingerCount)
{
    Q_D();
    if (!d->globalPointer.gestureSurface.isNull()) {
        return;
    }
    d->globalPointer.gestureSurface = QPointer<SurfaceInterface>(d->globalPointer.focus.surface);
    if (d->globalPointer.gestureSurface.isNull()) {
        return;
    }
    const quint32 serial = d->display->nextSerial();
1004
1005
1006
1007
1008
    forEachInterface<PointerInterface>(d->globalPointer.gestureSurface.data(), d->pointers,
        [serial, fingerCount] (PointerInterface *p) {
            p->d_func()->startSwipeGesture(serial, fingerCount);
        }
    );
1009
1010
1011
1012
1013
1014
1015
1016
}

void SeatInterface::updatePointerSwipeGesture(const QSizeF &delta)
{
    Q_D();
    if (d->globalPointer.gestureSurface.isNull()) {
        return;
    }
1017
1018
1019
1020
1021
    forEachInterface<PointerInterface>(d->globalPointer.gestureSurface.data(), d->pointers,
        [delta] (PointerInterface *p) {
            p->d_func()->updateSwipeGesture(delta);
        }
    );
1022
1023
1024
1025
1026
1027
1028
1029
1030
}

void SeatInterface::endPointerSwipeGesture()
{
    Q_D();
    if (d->globalPointer.gestureSurface.isNull()) {
        return;
    }
    const quint32 serial = d->display->nextSerial();
1031
1032
1033
1034
1035
    forEachInterface<PointerInterface>(d->globalPointer.gestureSurface.data(), d->pointers,
        [serial] (PointerInterface *p) {
            p->d_func()->endSwipeGesture(serial);
        }
    );
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
    d->globalPointer.gestureSurface.clear();
}

void SeatInterface::cancelPointerSwipeGesture()
{
    Q_D();
    if (d->globalPointer.gestureSurface.isNull()) {
        return;
    }
    const quint32 serial = d->display->nextSerial();
1046
1047
1048
1049
1050
    forEachInterface<PointerInterface>(d->globalPointer.gestureSurface.data(), d->pointers,
        [serial] (PointerInterface *p) {
            p->d_func()->cancelSwipeGesture(serial);
        }
    );
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
    d->globalPointer.gestureSurface.clear();
}

void SeatInterface::startPointerPinchGesture(quint32 fingerCount)
{
    Q_D();
    if (!d->globalPointer.gestureSurface.isNull()) {
        return;
    }
    d->globalPointer.gestureSurface = QPointer<SurfaceInterface>(d->globalPointer.focus.surface);
    if (d->globalPointer.gestureSurface.isNull()) {
        return;
    }
    const quint32 serial = d->display->nextSerial();
1065
1066
1067
1068
1069
    forEachInterface<PointerInterface>(d->globalPointer.gestureSurface.data(), d->pointers,
        [serial, fingerCount] (PointerInterface *p) {
            p->d_func()->startPinchGesture(serial, fingerCount);
        }
    );
1070
1071
1072
1073
1074
1075
1076
1077
}

void SeatInterface::updatePointerPinchGesture(const QSizeF &delta, qreal scale, qreal rotation)
{
    Q_D();
    if (d->globalPointer.gestureSurface.isNull()) {
        return;
    }
1078
1079
1080
1081
1082
    forEachInterface<PointerInterface>(d->globalPointer.gestureSurface.data(), d->pointers,
        [delta, scale, rotation] (PointerInterface *p) {
            p->d_func()->updatePinchGesture(delta, scale, rotation);
        }
    );
1083
1084
1085
1086
1087
1088
1089
1090
1091
}

void SeatInterface::endPointerPinchGesture()
{
    Q_D();
    if (d->globalPointer.gestureSurface.isNull()) {
        return;
    }
    const quint32 serial = d->display->nextSerial();
1092
1093
1094
1095
1096
    forEachInterface<PointerInterface>(d->globalPointer.gestureSurface.data(), d->pointers,
        [serial] (PointerInterface *p) {
            p->d_func()->endPinchGesture(serial);
        }
    );
1097
    d->globalPointer.gestureSurface.clear();
1098
1099
1100
1101
1102
1103
1104
1105
1106
}

void SeatInterface::cancelPointerPinchGesture()
{
    Q_D();
    if (d->globalPointer.gestureSurface.isNull()) {
        return;
    }
    const quint32 serial = d->display->nextSerial();
1107
1108
1109
1110
1111
    forEachInterface<PointerInterface>(d->globalPointer.gestureSurface.data(), d->pointers,
        [serial] (PointerInterface *p) {
            p->d_func()->cancelPinchGesture(serial);
        }
    );
1112
    d->globalPointer.gestureSurface.clear();
1113
1114
}

Martin Flöser's avatar
Martin Flöser committed
1115
1116
1117
1118
void SeatInterface::keyPressed(quint32 key)
{
    Q_D();
    d->keys.lastStateSerial = d->display->nextSerial();
1119
1120
1121
    if (!d->updateKey(key, Private::Keyboard::State::Pressed)) {
        return;
    }
1122
1123
1124
1125
    if (d->keys.focus.surface) {
        for (auto it = d->keys.focus.keyboards.constBegin(), end = d->keys.focus.keyboards.constEnd(); it != end; ++it) {
            (*it)->keyPressed(key, d->keys.lastStateSerial);
        }
Martin Flöser's avatar
Martin Flöser committed
1126
1127
1128
1129
1130
1131
1132
    }
}

void SeatInterface::keyReleased(quint32 key)
{
    Q_D();
    d->keys.lastStateSerial = d->display->nextSerial();
1133
1134
1135
    if (!d->updateKey(key, Private::Keyboard::State::Released)) {
        return;
    }
1136
1137
1138
1139
    if (d->keys.focus.surface) {
        for (auto it = d->keys.focus.keyboards.constBegin(), end = d->keys.focus.keyboards.constEnd(); it != end; ++it) {
            (*it)->keyReleased(key, d->keys.lastStateSerial);
        }
Martin Flöser's avatar
Martin Flöser committed
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
    }
}

SurfaceInterface *SeatInterface::focusedKeyboardSurface() const
{
    Q_D();
    return d->keys.focus.surface;
}

void SeatInterface::setFocusedKeyboardSurface(SurfaceInterface *surface)
{
    Q_D();
    const quint32 serial = d->display->nextSerial();
1153
1154
    for (auto it = d->keys.focus.keyboards.constBegin(), end = d->keys.focus.keyboards.constEnd(); it != end; ++it) {
        (*it)->setFocusedSurface(nullptr, serial);
Martin Flöser's avatar
Martin Flöser committed
1155
1156
1157
1158
1159
1160
    }
    if (d->keys.focus.surface) {
        disconnect(d->keys.focus.destroyConnection);
    }
    d->keys.focus = Private::Keyboard::Focus();
    d->keys.focus.surface = surface;
1161
    d->keys.focus.keyboards = d->keyboardsForSurface(surface);
Martin Flöser's avatar
Martin Flöser committed
1162
1163
1164
1165
1166
1167
1168
1169
    if (d->keys.focus.surface) {
        d->keys.focus.destroyConnection = connect(surface, &QObject::destroyed, this,
            [this] {
                Q_D();
                d->keys.focus = Private::Keyboard::Focus();
            }
        );
        d->keys.focus.serial = serial;
1170
        // selection?
1171
1172
1173
1174
1175
        const QVector<DataDeviceInterface *> dataDevices = d->