input.cpp 95.2 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
44
45
46
47
48
#include <KWaylandServer/display.h>
#include <KWaylandServer/fakeinput_interface.h>
#include <KWaylandServer/relativepointer_interface.h>
#include <KWaylandServer/seat_interface.h>
#include <KWaylandServer/buffer_interface.h>
#include <KWaylandServer/surface_interface.h>
#include <KWaylandServer/tablet_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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
{
    Q_UNUSED(event)
    return false;
}

bool InputEventFilter::tabletToolButtonEvent(const QSet<uint> &pressedButtons)
{
    Q_UNUSED(pressedButtons)
    return false;
}

bool InputEventFilter::tabletPadButtonEvent(const QSet<uint> &pressedButtons)
{
    Q_UNUSED(pressedButtons)
    return false;
}

bool InputEventFilter::tabletPadStripEvent(int number, int position, bool isFinger)
{
    Q_UNUSED(number)
    Q_UNUSED(position)
    Q_UNUSED(isFinger)
    return false;
}

bool InputEventFilter::tabletPadRingEvent(int number, int position, bool isFinger)
{
    Q_UNUSED(number)
    Q_UNUSED(position)
    Q_UNUSED(isFinger)
    return false;
}

206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
void InputEventFilter::passToWaylandServer(QKeyEvent *event)
{
    Q_ASSERT(waylandServer());
    if (event->isAutoRepeat()) {
        return;
    }
    switch (event->type()) {
    case QEvent::KeyPress:
        waylandServer()->seat()->keyPressed(event->nativeScanCode());
        break;
    case QEvent::KeyRelease:
        waylandServer()->seat()->keyReleased(event->nativeScanCode());
        break;
    default:
        break;
    }
}

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

239
240
241
242
243
244
245
246
247
248
249
250
251
252
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;
    }
};

253
254
255
256
257
258
259
260
261
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) {
262
            if (pointerSurfaceAllowed()) {
263
                // TODO: should the pointer position always stay in sync, i.e. not do the check?
264
                seat->setPointerPos(event->screenPos().toPoint());
265
266
            }
        } else if (event->type() == QEvent::MouseButtonPress || event->type() == QEvent::MouseButtonRelease) {
267
            if (pointerSurfaceAllowed()) {
268
269
                // TODO: can we leak presses/releases here when we move the mouse in between from an allowed surface to
                //       disallowed one or vice versa?
270
                event->type() == QEvent::MouseButtonPress ? seat->pointerButtonPressed(nativeButton) : seat->pointerButtonReleased(nativeButton);
271
272
273
274
275
276
277
278
279
            }
        }
        return true;
    }
    bool wheelEvent(QWheelEvent *event) override {
        if (!waylandServer()->isScreenLocked()) {
            return false;
        }
        auto seat = waylandServer()->seat();
280
281
282
283
        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());
284
285
286
287
288
289
290
        }
        return true;
    }
    bool keyEvent(QKeyEvent * event) override {
        if (!waylandServer()->isScreenLocked()) {
            return false;
        }
291
292
293
294
        if (event->isAutoRepeat()) {
            // wayland client takes care of it
            return true;
        }
295
296
297
298
299
300
301
302
303
304
        // 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
305
        input()->keyboard()->update();
306
307
        auto seat = waylandServer()->seat();
        seat->setTimestamp(event->timestamp());
308
309
310
        if (!keyboardSurfaceAllowed()) {
            // don't pass event to seat
            return true;
311
312
313
314
315
316
317
318
319
320
321
322
323
        }
        switch (event->type()) {
        case QEvent::KeyPress:
            seat->keyPressed(event->nativeScanCode());
            break;
        case QEvent::KeyRelease:
            seat->keyReleased(event->nativeScanCode());
            break;
        default:
            break;
        }
        return true;
    }
324
    bool touchDown(qint32 id, const QPointF &pos, quint32 time) override {
325
326
327
328
329
330
        if (!waylandServer()->isScreenLocked()) {
            return false;
        }
        auto seat = waylandServer()->seat();
        seat->setTimestamp(time);
        if (touchSurfaceAllowed()) {
331
            input()->touch()->insertId(id, seat->touchDown(pos));
332
333
334
        }
        return true;
    }
335
    bool touchMotion(qint32 id, const QPointF &pos, quint32 time) override {
336
337
338
339
340
341
        if (!waylandServer()->isScreenLocked()) {
            return false;
        }
        auto seat = waylandServer()->seat();
        seat->setTimestamp(time);
        if (touchSurfaceAllowed()) {
342
            const qint32 kwaylandId = input()->touch()->mappedId(id);
343
344
345
346
347
348
            if (kwaylandId != -1) {
                seat->touchMove(kwaylandId, pos);
            }
        }
        return true;
    }
349
    bool touchUp(qint32 id, quint32 time) override {
350
351
352
353
354
355
        if (!waylandServer()->isScreenLocked()) {
            return false;
        }
        auto seat = waylandServer()->seat();
        seat->setTimestamp(time);
        if (touchSurfaceAllowed()) {
356
            const qint32 kwaylandId = input()->touch()->mappedId(id);
357
358
            if (kwaylandId != -1) {
                seat->touchUp(kwaylandId);
359
                input()->touch()->removeId(id);
360
361
362
363
            }
        }
        return true;
    }
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
    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();
    }
411
private:
412
413
    bool surfaceAllowed(KWaylandServer::SurfaceInterface *(KWaylandServer::SeatInterface::*method)() const) const {
        if (KWaylandServer::SurfaceInterface *s = (waylandServer()->seat()->*method)()) {
414
415
416
417
418
419
420
            if (Toplevel *t = waylandServer()->findClient(s)) {
                return t->isLockScreen() || t->isInputMethod();
            }
            return false;
        }
        return true;
    }
421
    bool pointerSurfaceAllowed() const {
422
        return surfaceAllowed(&KWaylandServer::SeatInterface::focusedPointerSurface);
423
424
    }
    bool keyboardSurfaceAllowed() const {
425
        return surfaceAllowed(&KWaylandServer::SeatInterface::focusedKeyboardSurface);
426
    }
427
    bool touchSurfaceAllowed() const {
428
        return surfaceAllowed(&KWaylandServer::SeatInterface::focusedTouchSurface);
429
    }
430
431
432
433
434
435
436
437
438
439
440
};

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);
    }
441
442
443
444
445
446
    bool wheelEvent(QWheelEvent *event) override {
        if (!effects) {
            return false;
        }
        return static_cast<EffectsHandlerImpl*>(effects)->checkInputWindowEvent(event);
    }
447
448
449
450
    bool keyEvent(QKeyEvent *event) override {
        if (!effects || !static_cast< EffectsHandlerImpl* >(effects)->hasKeyboardGrab()) {
            return false;
        }
451
        waylandServer()->seat()->setFocusedKeyboardSurface(nullptr);
452
        passToWaylandServer(event);
453
454
455
        static_cast< EffectsHandlerImpl* >(effects)->grabbedKeyboardEvent(event);
        return true;
    }
456
    bool touchDown(qint32 id, const QPointF &pos, quint32 time) override {
457
458
459
460
461
        if (!effects) {
            return false;
        }
        return static_cast< EffectsHandlerImpl* >(effects)->touchDown(id, pos, time);
    }
462
    bool touchMotion(qint32 id, const QPointF &pos, quint32 time) override {
463
464
465
466
467
        if (!effects) {
            return false;
        }
        return static_cast< EffectsHandlerImpl* >(effects)->touchMotion(id, pos, time);
    }
468
    bool touchUp(qint32 id, quint32 time) override {
469
470
471
472
473
        if (!effects) {
            return false;
        }
        return static_cast< EffectsHandlerImpl* >(effects)->touchUp(id, time);
    }
474
475
476
477
478
479
};

class MoveResizeFilter : public InputEventFilter {
public:
    bool pointerEvent(QMouseEvent *event, quint32 nativeButton) override {
        Q_UNUSED(nativeButton)
480
        AbstractClient *c = workspace()->moveResizeClient();
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
        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
501
        return workspace()->moveResizeClient() != nullptr;
502
503
    }
    bool keyEvent(QKeyEvent *event) override {
504
        AbstractClient *c = workspace()->moveResizeClient();
505
506
507
508
509
510
511
512
513
514
515
516
        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;
    }
517

518
    bool touchDown(qint32 id, const QPointF &pos, quint32 time) override {
519
520
521
        Q_UNUSED(id)
        Q_UNUSED(pos)
        Q_UNUSED(time)
522
        AbstractClient *c = workspace()->moveResizeClient();
523
524
525
526
527
528
        if (!c) {
            return false;
        }
        return true;
    }

529
    bool touchMotion(qint32 id, const QPointF &pos, quint32 time) override {
530
        Q_UNUSED(time)
531
        AbstractClient *c = workspace()->moveResizeClient();
532
533
534
535
536
537
538
539
540
541
542
543
544
        if (!c) {
            return false;
        }
        if (!m_set) {
            m_id = id;
            m_set = true;
        }
        if (m_id == id) {
            c->updateMoveResize(pos.toPoint());
        }
        return true;
    }

545
    bool touchUp(qint32 id, quint32 time) override {
546
        Q_UNUSED(time)
547
        AbstractClient *c = workspace()->moveResizeClient();
548
549
550
551
552
553
554
555
556
557
558
559
560
        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:
561
    qint32 m_id = 0;
562
    bool m_set = false;
563
564
};

565
566
567
568
569
570
571
572
573
574
575
576
577
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 {
578
                    accept(event->globalPos());
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
                }
            }
            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) {
607
                accept(input()->globalPointer());
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
            }
            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;
    }

635
    bool touchDown(qint32 id, const QPointF &pos, quint32 time) override {
636
637
638
639
640
641
642
643
        Q_UNUSED(time)
        if (!isActive()) {
            return false;
        }
        m_touchPoints.insert(id, pos);
        return true;
    }

644
    bool touchMotion(qint32 id, const QPointF &pos, quint32 time) override {
645
646
647
648
649
650
651
652
653
654
655
        Q_UNUSED(time)
        if (!isActive()) {
            return false;
        }
        auto it = m_touchPoints.find(id);
        if (it != m_touchPoints.end()) {
            *it = pos;
        }
        return true;
    }

656
    bool touchUp(qint32 id, quint32 time) override {
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
        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;
    }

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

726
727
class GlobalShortcutFilter : public InputEventFilter {
public:
728
729
730
731
732
733
734
735
736
    GlobalShortcutFilter() {
        m_powerDown = new QTimer;
        m_powerDown->setSingleShot(true);
        m_powerDown->setInterval(1000);
    }
    ~GlobalShortcutFilter() {
        delete m_powerDown;
    }

737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
    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 {
763
        if (event->key() == Qt::Key_PowerOff) {
764
765
766
767
768
769
770
771
772
            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;
773
            } else if (event->type() == QEvent::KeyRelease) {
774
775
776
                const bool ret = !m_powerDown->isActive() || input()->shortcuts()->processKey(modifiers, event->key());
                m_powerDown->stop();
                return ret;
777
778
            }
        } else if (event->type() == QEvent::KeyPress) {
779
780
781
            if (!waylandServer()->isKeyboardShortcutsInhibited()) {
                return input()->shortcuts()->processKey(static_cast<KeyEvent*>(event)->modifiersRelevantForGlobalShortcuts(), event->key());
            }
782
783
784
        }
        return false;
    }
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
    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;
    }
805
806

private:
807
    QTimer* m_powerDown = nullptr;
808
809
};

810
811
812
813
814
815
816
817
818
819
820
821

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()) {
822
        if (!input()->pointer()->isConstrained() && !workspace()->globalShortcutsDisabled()) {
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
            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;
            }
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
        }
    } 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()) {
855
856
857
858
        if (!input()->pointer()->isConstrained() && !workspace()->globalShortcutsDisabled()) {
            wasAction = true;
            command = options->operationWindowMouseWheel(-1 * event->angleDelta().y());
        }
859
860
861
862
863
864
865
866
867
868
869
870
871
    } 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);
}

}

872
873
874
class InternalWindowEventFilter : public InputEventFilter {
    bool pointerEvent(QMouseEvent *event, quint32 nativeButton) override {
        Q_UNUSED(nativeButton)
875
        auto internal = input()->pointer()->internalWindow();
876
877
878
        if (!internal) {
            return false;
        }
879
880
881
882
883
        // find client
        switch (event->type())
        {
        case QEvent::MouseButtonPress:
        case QEvent::MouseButtonRelease: {
Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
884
            auto s = qobject_cast<InternalClient *>(workspace()->findInternal(internal));
885
886
887
888
889
890
891
892
893
894
895
896
            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;
        }
897
898
899
900
901
902
903
904
905
        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 {
906
        auto internal = input()->pointer()->internalWindow();
907
908
909
        if (!internal) {
            return false;
        }
910
        if (event->angleDelta().y() != 0) {
Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
911
            auto s = qobject_cast<InternalClient *>(workspace()->findInternal(internal));
912
913
914
915
916
917
918
919
            if (s && s->isDecorated()) {
                // client window action only on vertical scrolling
                const auto actionResult = performClientWheelAction(event, s);
                if (actionResult.first) {
                    return actionResult.second;
                }
            }
        }
920
921
922
923
        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(),
924
925
                        event->angleDelta() * -1,
                        delta * -1,
926
927
928
929
930
931
932
933
                        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
934
        const QList<InternalClient *> &internalClients = workspace()->internalClients();
935
936
937
938
939
940
941
942
943
944
945
        if (internalClients.isEmpty()) {
            return false;
        }
        QWindow *found = nullptr;
        auto it = internalClients.end();
        do {
            it--;
            if (QWindow *w = (*it)->internalWindow()) {
                if (!w->isVisible()) {
                    continue;
                }
946
947
948
                if (!screens()->geometry().contains(w->geometry())) {
                    continue;
                }
949
950
951
                if (w->property("_q_showWithoutActivating").toBool()) {
                    continue;
                }
952
953
954
                if (w->property("outputOnly").toBool()) {
                    continue;
                }
955
956
957
                if (w->flags().testFlag(Qt::ToolTip)) {
                    continue;
                }
958
959
960
961
962
                found = w;
                break;
            }
        } while (it != internalClients.begin());
        if (!found) {
963
964
            return false;
        }
965
966
967
968
969
970
971
972
973
974
975
        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)) {
976
            waylandServer()->seat()->setFocusedKeyboardSurface(nullptr);
977
            passToWaylandServer(event);
978
979
980
            return true;
        }
        return false;
981
    }
982

983
    bool touchDown(qint32 id, const QPointF &pos, quint32 time) override {
984
985
986
987
988
989
990
        auto seat = waylandServer()->seat();
        if (seat->isTouchSequence()) {
            // something else is getting the events
            return false;
        }
        auto touch = input()->touch();
        if (touch->internalPressId() != -1) {
991
            // already on internal window, ignore further touch points, but filter out
992
993
994
995
996
997
998
999
1000
            return true;
        }
        // a new touch point
        seat->setTimestamp(time);
        auto internal = touch->internalWindow();
        if (!internal) {
            return false;
        }
        touch->setInternalPressId(id);
For faster browsing, not all history is shown. View entire blame