input.cpp 99.6 KB
Newer Older
Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
1
2
3
/*
    KWin - the KDE window manager
    This file is part of the KDE project.
4

Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
5
6
7
    SPDX-FileCopyrightText: 2013 Martin Gräßlin <mgraesslin@kde.org>
    SPDX-FileCopyrightText: 2018 Roman Gilg <subdiff@gmail.com>
    SPDX-FileCopyrightText: 2019 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
8

Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
9
10
    SPDX-License-Identifier: GPL-2.0-or-later
*/
11
#include "input.h"
12
13
14
#include "effects.h"
#include "gestures.h"
#include "globalshortcuts.h"
15
#include "input_event.h"
16
#include "input_event_spy.h"
17
#include "keyboard_input.h"
18
19
#include "logind.h"
#include "main.h"
20
#include "pointer_input.h"
21
#include "tablet_input.h"
22
#include "touch_hide_cursor_spy.h"
23
#include "touch_input.h"
Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
24
#include "x11client.h"
25
26
27
#ifdef KWIN_BUILD_TABBOX
#include "tabbox/tabbox.h"
#endif
28
#include "internal_client.h"
Martin Flöser's avatar
Martin Flöser committed
29
#include "libinput/connection.h"
30
#include "libinput/device.h"
31
#include "platform.h"
32
#include "popup_input_filter.h"
33
34
35
#include "screenedge.h"
#include "screens.h"
#include "unmanaged.h"
36
#include "wayland_server.h"
37
#include "workspace.h"
38
#include "xwl/xwayland_interface.h"
39
40
41
#include "cursor.h"
#include <KDecoration2/Decoration>
#include <KGlobalAccel>
42
43
#include <KWaylandServer/display.h>
#include <KWaylandServer/fakeinput_interface.h>
44
#include <KWaylandServer/relativepointer_v1_interface.h>
45
46
47
#include <KWaylandServer/seat_interface.h>
#include <KWaylandServer/buffer_interface.h>
#include <KWaylandServer/surface_interface.h>
48
#include <KWaylandServer/tablet_v2_interface.h>
49
#include <decorations/decoratedclient.h>
50

51
52
//screenlocker
#include <KScreenLocker/KsldApp>
53
54
// Qt
#include <QKeyEvent>
55

56
#include <xkbcommon/xkbcommon.h>
57
58
59
60

namespace KWin
{

61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
InputEventFilter::InputEventFilter() = default;

InputEventFilter::~InputEventFilter()
{
    if (input()) {
        input()->uninstallInputEventFilter(this);
    }
}

bool InputEventFilter::pointerEvent(QMouseEvent *event, quint32 nativeButton)
{
    Q_UNUSED(event)
    Q_UNUSED(nativeButton)
    return false;
}

bool InputEventFilter::wheelEvent(QWheelEvent *event)
{
    Q_UNUSED(event)
    return false;
}

bool InputEventFilter::keyEvent(QKeyEvent *event)
{
    Q_UNUSED(event)
    return false;
}

89
bool InputEventFilter::touchDown(qint32 id, const QPointF &point, quint32 time)
90
91
92
93
94
95
96
{
    Q_UNUSED(id)
    Q_UNUSED(point)
    Q_UNUSED(time)
    return false;
}

97
bool InputEventFilter::touchMotion(qint32 id, const QPointF &point, quint32 time)
98
99
100
101
102
103
104
{
    Q_UNUSED(id)
    Q_UNUSED(point)
    Q_UNUSED(time)
    return false;
}

105
bool InputEventFilter::touchUp(qint32 id, quint32 time)
106
107
108
109
110
111
{
    Q_UNUSED(id)
    Q_UNUSED(time)
    return false;
}

112
113
114
115
116
117
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
bool InputEventFilter::pinchGestureBegin(int fingerCount, quint32 time)
{
    Q_UNUSED(fingerCount)
    Q_UNUSED(time)
    return false;
}

bool InputEventFilter::pinchGestureUpdate(qreal scale, qreal angleDelta, const QSizeF &delta, quint32 time)
{
    Q_UNUSED(scale)
    Q_UNUSED(angleDelta)
    Q_UNUSED(delta)
    Q_UNUSED(time)
    return false;
}

bool InputEventFilter::pinchGestureEnd(quint32 time)
{
    Q_UNUSED(time)
    return false;
}

bool InputEventFilter::pinchGestureCancelled(quint32 time)
{
    Q_UNUSED(time)
    return false;
}

bool InputEventFilter::swipeGestureBegin(int fingerCount, quint32 time)
{
    Q_UNUSED(fingerCount)
    Q_UNUSED(time)
    return false;
}

bool InputEventFilter::swipeGestureUpdate(const QSizeF &delta, quint32 time)
{
    Q_UNUSED(delta)
    Q_UNUSED(time)
    return false;
}

bool InputEventFilter::swipeGestureEnd(quint32 time)
{
    Q_UNUSED(time)
    return false;
}

bool InputEventFilter::swipeGestureCancelled(quint32 time)
{
    Q_UNUSED(time)
    return false;
}

166
167
168
169
170
171
bool InputEventFilter::switchEvent(SwitchEvent *event)
{
    Q_UNUSED(event)
    return false;
}

172
bool InputEventFilter::tabletToolEvent(TabletEvent *event)
173
174
175
176
177
{
    Q_UNUSED(event)
    return false;
}

178
bool InputEventFilter::tabletToolButtonEvent(uint button, bool pressed, const TabletToolId &tabletId)
179
{
180
181
182
    Q_UNUSED(button)
    Q_UNUSED(pressed)
    Q_UNUSED(tabletId)
183
184
185
    return false;
}

186
bool InputEventFilter::tabletPadButtonEvent(uint button, bool pressed, const TabletPadId &tabletPadId)
187
{
188
189
    Q_UNUSED(button)
    Q_UNUSED(pressed)
190
    Q_UNUSED(tabletPadId)
191
192
193
    return false;
}

194
bool InputEventFilter::tabletPadStripEvent(int number, int position, bool isFinger, const TabletPadId &tabletPadId)
195
196
197
198
{
    Q_UNUSED(number)
    Q_UNUSED(position)
    Q_UNUSED(isFinger)
199
    Q_UNUSED(tabletPadId)
200
201
202
    return false;
}

203
bool InputEventFilter::tabletPadRingEvent(int number, int position, bool isFinger, const TabletPadId &tabletPadId)
204
205
206
207
{
    Q_UNUSED(number)
    Q_UNUSED(position)
    Q_UNUSED(isFinger)
208
    Q_UNUSED(tabletPadId)
209
210
211
    return false;
}

212
213
214
215
216
217
218
219
void InputEventFilter::passToWaylandServer(QKeyEvent *event)
{
    Q_ASSERT(waylandServer());
    if (event->isAutoRepeat()) {
        return;
    }
    switch (event->type()) {
    case QEvent::KeyPress:
220
        waylandServer()->seat()->keyboard()->keyPressed(event->nativeScanCode());
221
222
        break;
    case QEvent::KeyRelease:
223
        waylandServer()->seat()->keyboard()->keyReleased(event->nativeScanCode());
224
225
226
227
228
229
        break;
    default:
        break;
    }
}

230
231
232
233
class VirtualTerminalFilter : public InputEventFilter {
public:
    bool keyEvent(QKeyEvent *event) override {
        // really on press and not on release? X11 switches on press.
234
        if (event->type() == QEvent::KeyPress && !event->isAutoRepeat()) {
235
236
            const xkb_keysym_t keysym = event->nativeVirtualKey();
            if (keysym >= XKB_KEY_XF86Switch_VT_1 && keysym <= XKB_KEY_XF86Switch_VT_12) {
237
                LogindIntegration::self()->switchVirtualTerminal(keysym - XKB_KEY_XF86Switch_VT_1 + 1);
238
239
240
241
242
243
244
                return true;
            }
        }
        return false;
    }
};

245
246
247
248
249
250
251
252
253
254
255
256
257
258
class TerminateServerFilter : public InputEventFilter {
public:
    bool keyEvent(QKeyEvent *event) override {
        if (event->type() == QEvent::KeyPress && !event->isAutoRepeat()) {
            if (event->nativeVirtualKey() == XKB_KEY_Terminate_Server) {
                qCWarning(KWIN_CORE) << "Request to terminate server";
                QMetaObject::invokeMethod(qApp, "quit", Qt::QueuedConnection);
                return true;
            }
        }
        return false;
    }
};

259
260
261
262
263
264
265
266
267
class LockScreenFilter : public InputEventFilter {
public:
    bool pointerEvent(QMouseEvent *event, quint32 nativeButton) override {
        if (!waylandServer()->isScreenLocked()) {
            return false;
        }
        auto seat = waylandServer()->seat();
        seat->setTimestamp(event->timestamp());
        if (event->type() == QEvent::MouseMove) {
268
            if (pointerSurfaceAllowed()) {
269
                // TODO: should the pointer position always stay in sync, i.e. not do the check?
270
                seat->setPointerPos(event->screenPos().toPoint());
271
272
            }
        } else if (event->type() == QEvent::MouseButtonPress || event->type() == QEvent::MouseButtonRelease) {
273
            if (pointerSurfaceAllowed()) {
274
275
                // TODO: can we leak presses/releases here when we move the mouse in between from an allowed surface to
                //       disallowed one or vice versa?
276
                event->type() == QEvent::MouseButtonPress ? seat->pointerButtonPressed(nativeButton) : seat->pointerButtonReleased(nativeButton);
277
278
279
280
281
282
283
284
285
            }
        }
        return true;
    }
    bool wheelEvent(QWheelEvent *event) override {
        if (!waylandServer()->isScreenLocked()) {
            return false;
        }
        auto seat = waylandServer()->seat();
286
287
288
289
        if (pointerSurfaceAllowed()) {
            seat->setTimestamp(event->timestamp());
            const Qt::Orientation orientation = event->angleDelta().x() == 0 ? Qt::Vertical : Qt::Horizontal;
            seat->pointerAxis(orientation, orientation == Qt::Horizontal ? event->angleDelta().x() : event->angleDelta().y());
290
291
292
293
294
295
296
        }
        return true;
    }
    bool keyEvent(QKeyEvent * event) override {
        if (!waylandServer()->isScreenLocked()) {
            return false;
        }
297
298
299
300
        if (event->isAutoRepeat()) {
            // wayland client takes care of it
            return true;
        }
301
302
303
304
305
306
307
308
309
310
        // send event to KSldApp for global accel
        // if event is set to accepted it means a whitelisted shortcut was triggered
        // in that case we filter it out and don't process it further
        event->setAccepted(false);
        QCoreApplication::sendEvent(ScreenLocker::KSldApp::self(), event);
        if (event->isAccepted()) {
            return true;
        }

        // continue normal processing
311
        input()->keyboard()->update();
312
313
        auto seat = waylandServer()->seat();
        seat->setTimestamp(event->timestamp());
314
315
316
        if (!keyboardSurfaceAllowed()) {
            // don't pass event to seat
            return true;
317
318
319
        }
        switch (event->type()) {
        case QEvent::KeyPress:
320
            seat->keyboard()->keyPressed(event->nativeScanCode());
321
322
            break;
        case QEvent::KeyRelease:
323
            seat->keyboard()->keyReleased(event->nativeScanCode());
324
325
326
327
328
329
            break;
        default:
            break;
        }
        return true;
    }
330
    bool touchDown(qint32 id, const QPointF &pos, quint32 time) override {
331
332
333
334
335
336
        if (!waylandServer()->isScreenLocked()) {
            return false;
        }
        auto seat = waylandServer()->seat();
        seat->setTimestamp(time);
        if (touchSurfaceAllowed()) {
337
            input()->touch()->insertId(id, seat->touchDown(pos));
338
339
340
        }
        return true;
    }
341
    bool touchMotion(qint32 id, const QPointF &pos, quint32 time) override {
342
343
344
345
346
347
        if (!waylandServer()->isScreenLocked()) {
            return false;
        }
        auto seat = waylandServer()->seat();
        seat->setTimestamp(time);
        if (touchSurfaceAllowed()) {
348
            const qint32 kwaylandId = input()->touch()->mappedId(id);
349
350
351
352
353
354
            if (kwaylandId != -1) {
                seat->touchMove(kwaylandId, pos);
            }
        }
        return true;
    }
355
    bool touchUp(qint32 id, quint32 time) override {
356
357
358
359
360
361
        if (!waylandServer()->isScreenLocked()) {
            return false;
        }
        auto seat = waylandServer()->seat();
        seat->setTimestamp(time);
        if (touchSurfaceAllowed()) {
362
            const qint32 kwaylandId = input()->touch()->mappedId(id);
363
364
            if (kwaylandId != -1) {
                seat->touchUp(kwaylandId);
365
                input()->touch()->removeId(id);
366
367
368
369
            }
        }
        return true;
    }
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
    bool pinchGestureBegin(int fingerCount, quint32 time) override {
        Q_UNUSED(fingerCount)
        Q_UNUSED(time)
        // no touchpad multi-finger gestures on lock screen
        return waylandServer()->isScreenLocked();
    }
    bool pinchGestureUpdate(qreal scale, qreal angleDelta, const QSizeF &delta, quint32 time) override {
        Q_UNUSED(scale)
        Q_UNUSED(angleDelta)
        Q_UNUSED(delta)
        Q_UNUSED(time)
        // no touchpad multi-finger gestures on lock screen
        return waylandServer()->isScreenLocked();
    }
    bool pinchGestureEnd(quint32 time) override {
        Q_UNUSED(time)
        // no touchpad multi-finger gestures on lock screen
        return waylandServer()->isScreenLocked();
    }
    bool pinchGestureCancelled(quint32 time) override {
        Q_UNUSED(time)
        // no touchpad multi-finger gestures on lock screen
        return waylandServer()->isScreenLocked();
    }

    bool swipeGestureBegin(int fingerCount, quint32 time) override {
        Q_UNUSED(fingerCount)
        Q_UNUSED(time)
        // no touchpad multi-finger gestures on lock screen
        return waylandServer()->isScreenLocked();
    }
    bool swipeGestureUpdate(const QSizeF &delta, quint32 time) override {
        Q_UNUSED(delta)
        Q_UNUSED(time)
        // no touchpad multi-finger gestures on lock screen
        return waylandServer()->isScreenLocked();
    }
    bool swipeGestureEnd(quint32 time) override {
        Q_UNUSED(time)
        // no touchpad multi-finger gestures on lock screen
        return waylandServer()->isScreenLocked();
    }
    bool swipeGestureCancelled(quint32 time) override {
        Q_UNUSED(time)
        // no touchpad multi-finger gestures on lock screen
        return waylandServer()->isScreenLocked();
    }
417
private:
418
419
    bool surfaceAllowed(KWaylandServer::SurfaceInterface *(KWaylandServer::SeatInterface::*method)() const) const {
        if (KWaylandServer::SurfaceInterface *s = (waylandServer()->seat()->*method)()) {
420
421
422
423
424
425
426
            if (Toplevel *t = waylandServer()->findClient(s)) {
                return t->isLockScreen() || t->isInputMethod();
            }
            return false;
        }
        return true;
    }
427
    bool pointerSurfaceAllowed() const {
428
        return surfaceAllowed(&KWaylandServer::SeatInterface::focusedPointerSurface);
429
430
    }
    bool keyboardSurfaceAllowed() const {
431
        return surfaceAllowed(&KWaylandServer::SeatInterface::focusedKeyboardSurface);
432
    }
433
    bool touchSurfaceAllowed() const {
434
        return surfaceAllowed(&KWaylandServer::SeatInterface::focusedTouchSurface);
435
    }
436
437
438
439
440
441
442
443
444
445
446
};

class EffectsFilter : public InputEventFilter {
public:
    bool pointerEvent(QMouseEvent *event, quint32 nativeButton) override {
        Q_UNUSED(nativeButton)
        if (!effects) {
            return false;
        }
        return static_cast<EffectsHandlerImpl*>(effects)->checkInputWindowEvent(event);
    }
447
448
449
450
451
452
    bool wheelEvent(QWheelEvent *event) override {
        if (!effects) {
            return false;
        }
        return static_cast<EffectsHandlerImpl*>(effects)->checkInputWindowEvent(event);
    }
453
454
455
456
    bool keyEvent(QKeyEvent *event) override {
        if (!effects || !static_cast< EffectsHandlerImpl* >(effects)->hasKeyboardGrab()) {
            return false;
        }
457
        waylandServer()->seat()->setFocusedKeyboardSurface(nullptr);
458
        passToWaylandServer(event);
459
460
461
        static_cast< EffectsHandlerImpl* >(effects)->grabbedKeyboardEvent(event);
        return true;
    }
462
    bool touchDown(qint32 id, const QPointF &pos, quint32 time) override {
463
464
465
466
467
        if (!effects) {
            return false;
        }
        return static_cast< EffectsHandlerImpl* >(effects)->touchDown(id, pos, time);
    }
468
    bool touchMotion(qint32 id, const QPointF &pos, quint32 time) override {
469
470
471
472
473
        if (!effects) {
            return false;
        }
        return static_cast< EffectsHandlerImpl* >(effects)->touchMotion(id, pos, time);
    }
474
    bool touchUp(qint32 id, quint32 time) override {
475
476
477
478
479
        if (!effects) {
            return false;
        }
        return static_cast< EffectsHandlerImpl* >(effects)->touchUp(id, time);
    }
480
481
482
483
484
485
};

class MoveResizeFilter : public InputEventFilter {
public:
    bool pointerEvent(QMouseEvent *event, quint32 nativeButton) override {
        Q_UNUSED(nativeButton)
486
        AbstractClient *c = workspace()->moveResizeClient();
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
        if (!c) {
            return false;
        }
        switch (event->type()) {
        case QEvent::MouseMove:
            c->updateMoveResize(event->screenPos().toPoint());
            break;
        case QEvent::MouseButtonRelease:
            if (event->buttons() == Qt::NoButton) {
                c->endMoveResize();
            }
            break;
        default:
            break;
        }
        return true;
    }
    bool wheelEvent(QWheelEvent *event) override {
        Q_UNUSED(event)
        // filter out while moving a window
507
        return workspace()->moveResizeClient() != nullptr;
508
509
    }
    bool keyEvent(QKeyEvent *event) override {
510
        AbstractClient *c = workspace()->moveResizeClient();
511
512
513
514
515
516
517
518
519
520
521
522
        if (!c) {
            return false;
        }
        if (event->type() == QEvent::KeyPress) {
            c->keyPressEvent(event->key() | event->modifiers());
            if (c->isMove() || c->isResize()) {
                // only update if mode didn't end
                c->updateMoveResize(input()->globalPointer());
            }
        }
        return true;
    }
523

524
    bool touchDown(qint32 id, const QPointF &pos, quint32 time) override {
525
526
527
        Q_UNUSED(id)
        Q_UNUSED(pos)
        Q_UNUSED(time)
528
        AbstractClient *c = workspace()->moveResizeClient();
529
530
531
532
533
534
        if (!c) {
            return false;
        }
        return true;
    }

535
    bool touchMotion(qint32 id, const QPointF &pos, quint32 time) override {
536
        Q_UNUSED(time)
537
        AbstractClient *c = workspace()->moveResizeClient();
538
539
540
541
542
543
544
545
546
547
548
549
550
        if (!c) {
            return false;
        }
        if (!m_set) {
            m_id = id;
            m_set = true;
        }
        if (m_id == id) {
            c->updateMoveResize(pos.toPoint());
        }
        return true;
    }

551
    bool touchUp(qint32 id, quint32 time) override {
552
        Q_UNUSED(time)
553
        AbstractClient *c = workspace()->moveResizeClient();
554
555
556
557
558
559
560
561
562
563
564
565
566
        if (!c) {
            return false;
        }
        if (m_id == id || !m_set) {
            c->endMoveResize();
            m_set = false;
            // pass through to update decoration filter later on
            return false;
        }
        m_set = false;
        return true;
    }
private:
567
    qint32 m_id = 0;
568
    bool m_set = false;
569
570
};

571
572
573
574
575
576
577
578
579
580
581
582
583
class WindowSelectorFilter : public InputEventFilter {
public:
    bool pointerEvent(QMouseEvent *event, quint32 nativeButton) override {
        Q_UNUSED(nativeButton)
        if (!m_active) {
            return false;
        }
        switch (event->type()) {
        case QEvent::MouseButtonRelease:
            if (event->buttons() == Qt::NoButton) {
                if (event->button() == Qt::RightButton) {
                    cancel();
                } else {
584
                    accept(event->globalPos());
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
                }
            }
            break;
        default:
            break;
        }
        return true;
    }
    bool wheelEvent(QWheelEvent *event) override {
        Q_UNUSED(event)
        // filter out while selecting a window
        return m_active;
    }
    bool keyEvent(QKeyEvent *event) override {
        Q_UNUSED(event)
        if (!m_active) {
            return false;
        }
        waylandServer()->seat()->setFocusedKeyboardSurface(nullptr);
        passToWaylandServer(event);

        if (event->type() == QEvent::KeyPress) {
            // x11 variant does this on key press, so do the same
            if (event->key() == Qt::Key_Escape) {
                cancel();
            } else if (event->key() == Qt::Key_Enter ||
                       event->key() == Qt::Key_Return ||
                       event->key() == Qt::Key_Space) {
613
                accept(input()->globalPointer());
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
            }
            if (input()->supportsPointerWarping()) {
                int mx = 0;
                int my = 0;
                if (event->key() == Qt::Key_Left) {
                    mx = -10;
                }
                if (event->key() == Qt::Key_Right) {
                    mx = 10;
                }
                if (event->key() == Qt::Key_Up) {
                    my = -10;
                }
                if (event->key() == Qt::Key_Down) {
                    my = 10;
                }
                if (event->modifiers() & Qt::ControlModifier) {
                    mx /= 10;
                    my /= 10;
                }
                input()->warpPointer(input()->globalPointer() + QPointF(mx, my));
            }
        }
        // filter out while selecting a window
        return true;
    }

641
    bool touchDown(qint32 id, const QPointF &pos, quint32 time) override {
642
643
644
645
646
647
648
649
        Q_UNUSED(time)
        if (!isActive()) {
            return false;
        }
        m_touchPoints.insert(id, pos);
        return true;
    }

650
    bool touchMotion(qint32 id, const QPointF &pos, quint32 time) override {
651
652
653
654
655
656
657
658
659
660
661
        Q_UNUSED(time)
        if (!isActive()) {
            return false;
        }
        auto it = m_touchPoints.find(id);
        if (it != m_touchPoints.end()) {
            *it = pos;
        }
        return true;
    }

662
    bool touchUp(qint32 id, quint32 time) override {
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
        Q_UNUSED(time)
        if (!isActive()) {
            return false;
        }
        auto it = m_touchPoints.find(id);
        if (it != m_touchPoints.end()) {
            const auto pos = it.value();
            m_touchPoints.erase(it);
            if (m_touchPoints.isEmpty()) {
                accept(pos);
            }
        }
        return true;
    }

678
679
680
681
682
683
684
685
    bool isActive() const {
        return m_active;
    }
    void start(std::function<void(KWin::Toplevel*)> callback) {
        Q_ASSERT(!m_active);
        m_active = true;
        m_callback = callback;
        input()->keyboard()->update();
686
        input()->cancelTouch();
687
    }
688
689
690
691
692
    void start(std::function<void(const QPoint &)> callback) {
        Q_ASSERT(!m_active);
        m_active = true;
        m_pointSelectionFallback = callback;
        input()->keyboard()->update();
693
        input()->cancelTouch();
694
    }
695
696
697
698
private:
    void deactivate() {
        m_active = false;
        m_callback = std::function<void(KWin::Toplevel*)>();
699
        m_pointSelectionFallback = std::function<void(const QPoint &)>();
700
701
        input()->pointer()->removeWindowSelectionCursor();
        input()->keyboard()->update();
702
        m_touchPoints.clear();
703
704
    }
    void cancel() {
705
706
707
708
709
710
        if (m_callback) {
            m_callback(nullptr);
        }
        if (m_pointSelectionFallback) {
            m_pointSelectionFallback(QPoint(-1, -1));
        }
711
712
        deactivate();
    }
713
    void accept(const QPoint &pos) {
714
715
        if (m_callback) {
            // TODO: this ignores shaped windows
716
            m_callback(input()->findToplevel(pos));
717
718
        }
        if (m_pointSelectionFallback) {
719
            m_pointSelectionFallback(pos);
720
        }
721
722
        deactivate();
    }
723
724
725
    void accept(const QPointF &pos) {
        accept(pos.toPoint());
    }
726
727
    bool m_active = false;
    std::function<void(KWin::Toplevel*)> m_callback;
728
    std::function<void(const QPoint &)> m_pointSelectionFallback;
729
    QMap<quint32, QPointF> m_touchPoints;
730
731
};

732
733
class GlobalShortcutFilter : public InputEventFilter {
public:
734
735
736
737
738
739
740
741
742
    GlobalShortcutFilter() {
        m_powerDown = new QTimer;
        m_powerDown->setSingleShot(true);
        m_powerDown->setInterval(1000);
    }
    ~GlobalShortcutFilter() {
        delete m_powerDown;
    }

743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
    bool pointerEvent(QMouseEvent *event, quint32 nativeButton) override {
        Q_UNUSED(nativeButton);
        if (event->type() == QEvent::MouseButtonPress) {
            if (input()->shortcuts()->processPointerPressed(event->modifiers(), event->buttons())) {
                return true;
            }
        }
        return false;
    }
    bool wheelEvent(QWheelEvent *event) override {
        if (event->modifiers() == Qt::NoModifier) {
            return false;
        }
        PointerAxisDirection direction = PointerAxisUp;
        if (event->angleDelta().x() < 0) {
            direction = PointerAxisRight;
        } else if (event->angleDelta().x() > 0) {
            direction = PointerAxisLeft;
        } else if (event->angleDelta().y() < 0) {
            direction = PointerAxisDown;
        } else if (event->angleDelta().y() > 0) {
            direction = PointerAxisUp;
        }
        return input()->shortcuts()->processAxis(event->modifiers(), direction);
    }
    bool keyEvent(QKeyEvent *event) override {
769
        if (event->key() == Qt::Key_PowerOff) {
770
771
772
773
774
775
776
777
778
            const auto modifiers = static_cast<KeyEvent*>(event)->modifiersRelevantForGlobalShortcuts();
            if (event->type() == QEvent::KeyPress && !event->isAutoRepeat()) {
                QObject::connect(m_powerDown, &QTimer::timeout, input()->shortcuts(), [this, modifiers] {
                    QObject::disconnect(m_powerDown, &QTimer::timeout, input()->shortcuts(), nullptr);
                    m_powerDown->stop();
                    input()->shortcuts()->processKey(modifiers, Qt::Key_PowerDown);
                });
                m_powerDown->start();
                return true;
779
            } else if (event->type() == QEvent::KeyRelease) {
780
781
782
                const bool ret = !m_powerDown->isActive() || input()->shortcuts()->processKey(modifiers, event->key());
                m_powerDown->stop();
                return ret;
783
784
            }
        } else if (event->type() == QEvent::KeyPress) {
785
786
787
            if (!waylandServer()->isKeyboardShortcutsInhibited()) {
                return input()->shortcuts()->processKey(static_cast<KeyEvent*>(event)->modifiersRelevantForGlobalShortcuts(), event->key());
            }
788
789
790
        }
        return false;
    }
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
    bool swipeGestureBegin(int fingerCount, quint32 time) override {
        Q_UNUSED(time)
        input()->shortcuts()->processSwipeStart(fingerCount);
        return false;
    }
    bool swipeGestureUpdate(const QSizeF &delta, quint32 time) override {
        Q_UNUSED(time)
        input()->shortcuts()->processSwipeUpdate(delta);
        return false;
    }
    bool swipeGestureCancelled(quint32 time) override {
        Q_UNUSED(time)
        input()->shortcuts()->processSwipeCancel();
        return false;
    }
    bool swipeGestureEnd(quint32 time) override {
        Q_UNUSED(time)
        input()->shortcuts()->processSwipeEnd();
        return false;
    }
811
812

private:
813
    QTimer* m_powerDown = nullptr;
814
815
};

816
817
818
819
820
821
822
823
824
825
826
827

namespace {

enum class MouseAction {
    ModifierOnly,
    ModifierAndWindow
};
std::pair<bool, bool> performClientMouseAction(QMouseEvent *event, AbstractClient *client, MouseAction action = MouseAction::ModifierOnly)
{
    Options::MouseCommand command = Options::MouseNothing;
    bool wasAction = false;
    if (static_cast<MouseEvent*>(event)->modifiersRelevantForGlobalShortcuts() == options->commandAllModifier()) {
828
        if (!input()->pointer()->isConstrained() && !workspace()->globalShortcutsDisabled()) {
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
            wasAction = true;
            switch (event->button()) {
            case Qt::LeftButton:
                command = options->commandAll1();
                break;
            case Qt::MiddleButton:
                command = options->commandAll2();
                break;
            case Qt::RightButton:
                command = options->commandAll3();
                break;
            default:
                // nothing
                break;
            }
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
        }
    } else {
        if (action == MouseAction::ModifierAndWindow) {
            command = client->getMouseCommand(event->button(), &wasAction);
        }
    }
    if (wasAction) {
        return std::make_pair(wasAction, !client->performMouseCommand(command, event->globalPos()));
    }
    return std::make_pair(wasAction, false);
}

std::pair<bool, bool> performClientWheelAction(QWheelEvent *event, AbstractClient *c, MouseAction action = MouseAction::ModifierOnly)
{
    bool wasAction = false;
    Options::MouseCommand command = Options::MouseNothing;
    if (static_cast<WheelEvent*>(event)->modifiersRelevantForGlobalShortcuts() == options->commandAllModifier()) {
861
862
863
864
        if (!input()->pointer()->isConstrained() && !workspace()->globalShortcutsDisabled()) {
            wasAction = true;
            command = options->operationWindowMouseWheel(-1 * event->angleDelta().y());
        }
865
866
867
868
869
870
871
872
873
874
875
876
877
    } else {
        if (action == MouseAction::ModifierAndWindow) {
            command = c->getWheelCommand(Qt::Vertical, &wasAction);
        }
    }
    if (wasAction) {
        return std::make_pair(wasAction, !c->performMouseCommand(command, event->globalPos()));
    }
    return std::make_pair(wasAction, false);
}

}

878
879
880
class InternalWindowEventFilter : public InputEventFilter {
    bool pointerEvent(QMouseEvent *event, quint32 nativeButton) override {
        Q_UNUSED(nativeButton)
881
        auto internal = input()->pointer()->internalWindow();
882
883
884
        if (!internal) {
            return false;
        }
885
886
887
888
889
        // find client
        switch (event->type())
        {
        case QEvent::MouseButtonPress:
        case QEvent::MouseButtonRelease: {
Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
890
            auto s = qobject_cast<InternalClient *>(workspace()->findInternal(internal));
891
892
893
894
895
896
897
898
899
900
901
902
            if (s && s->isDecorated()) {
                // only perform mouse commands on decorated internal windows
                const auto actionResult = performClientMouseAction(event, s);
                if (actionResult.first) {
                    return actionResult.second;
                }
            }
            break;
        }
        default:
            break;
        }
903
904
905
906
907
908
909
910
911
        QMouseEvent e(event->type(),
                        event->pos() - internal->position(),
                        event->globalPos(),
                        event->button(), event->buttons(), event->modifiers());
        e.setAccepted(false);
        QCoreApplication::sendEvent(internal.data(), &e);
        return e.isAccepted();
    }
    bool wheelEvent(QWheelEvent *event) override {
912
        auto internal = input()->pointer()->internalWindow();
913
914
915
        if (!internal) {
            return false;
        }
916
        if (event->angleDelta().y() != 0) {
Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
917
            auto s = qobject_cast<InternalClient *>(workspace()->findInternal(internal));
918
919
920
921
922
923
924
925
            if (s && s->isDecorated()) {
                // client window action only on vertical scrolling
                const auto actionResult = performClientWheelAction(event, s);
                if (actionResult.first) {
                    return actionResult.second;
                }
            }
        }
926
927
928
929
        const QPointF localPos = event->globalPosF() - QPointF(internal->x(), internal->y());
        const Qt::Orientation orientation = (event->angleDelta().x() != 0) ? Qt::Horizontal : Qt::Vertical;
        const int delta = event->angleDelta().x() != 0 ? event->angleDelta().x() : event->angleDelta().y();
        QWheelEvent e(localPos, event->globalPosF(), QPoint(),
930
931
                        event->angleDelta() * -1,
                        delta * -1,
932
933
934
935
936
937
938
939
                        orientation,
                        event->buttons(),
                        event->modifiers());
        e.setAccepted(false);
        QCoreApplication::sendEvent(internal.data(), &e);
        return e.isAccepted();
    }
    bool keyEvent(QKeyEvent *event) override {
Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
940
        const QList<InternalClient *> &internalClients = workspace()->internalClients();
941
942
943
944
945
946
947
948
949
950
951
        if (internalClients.isEmpty()) {
            return false;
        }
        QWindow *found = nullptr;
        auto it = internalClients.end();
        do {
            it--;
            if (QWindow *w = (*it)->internalWindow()) {
                if (!w->isVisible()) {
                    continue;
                }
952
953
954
                if (!screens()->geometry().contains(w->geometry())) {
                    continue;
                }
955
956
957
                if (w->property("_q_showWithoutActivating").toBool()) {
                    continue;
                }
958
959
960
                if (w->property("outputOnly").toBool()) {
                    continue;
                }
961
962
963
                if (w->flags().testFlag(Qt::ToolTip)) {
                    continue;
                }
964
965
966
967
968
                found = w;
                break;
            }
        } while (it != internalClients.begin());
        if (!found) {
969
970
            return false;
        }
971
972
973
974
975
976
977
978
979
980
981
        auto xkb = input()->keyboard()->xkb();
        Qt::Key key = xkb->toQtKey(xkb->toKeysym(event->nativeScanCode()));
        if (key == Qt::Key_Super_L || key == Qt::Key_Super_R) {
            // workaround for QTBUG-62102
            key = Qt::Key_Meta;
        }
        QKeyEvent internalEvent(event->type(), key,
                                event->modifiers(), event->nativeScanCode(), event->nativeVirtualKey(),
                                event->nativeModifiers(), event->text());
        internalEvent.setAccepted(false);
        if (QCoreApplication::sendEvent(found, &internalEvent)) {
982
            waylandServer()->seat()->setFocusedKeyboardSurface(nullptr);
983
            passToWaylandServer(event);
984
985
986
            return true;
        }
        return false;
987
    }
988

989
    bool touchDown(qint32 id, const QPointF &pos, quint32 time) override {
990
991
992
993
994
995
996
        auto seat = waylandServer()->seat();
        if (seat->isTouchSequence()) {
            // something else is getting the events
            return false;
        }
        auto touch = input()->touch();
        if (touch->internalPressId() != -1) {
997
            // already on internal window, ignore further touch points, but filter out
998
999
1000
            return true;
        }
        // a new touch point