Coverage Report

Created: 2026-03-31 07:41

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qtbase/src/gui/kernel/qwindowsysteminterface_p.h
Line
Count
Source
1
// Copyright (C) 2016 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
// Qt-Security score:significant reason:default
4
#ifndef QWINDOWSYSTEMINTERFACE_P_H
5
#define QWINDOWSYSTEMINTERFACE_P_H
6
7
//
8
//  W A R N I N G
9
//  -------------
10
//
11
// This file is not part of the Qt API.  It exists purely as an
12
// implementation detail.  This header file may change from version to
13
// version without notice, or even be removed.
14
//
15
// We mean it.
16
//
17
18
#include <QtGui/private/qevent_p.h>
19
#include <QtGui/private/qtguiglobal_p.h>
20
#include "qwindowsysteminterface.h"
21
22
#include <QElapsedTimer>
23
#include <QPointer>
24
#include <QMutex>
25
#include <QList>
26
#include <QWaitCondition>
27
#include <QAtomicInt>
28
#include <QLoggingCategory>
29
30
QT_BEGIN_NAMESPACE
31
32
QT_DECLARE_EXPORTED_QT_LOGGING_CATEGORY(lcQpaInputDevices, Q_GUI_EXPORT)
33
34
class QWindowSystemEventHandler;
35
36
class Q_GUI_EXPORT QWindowSystemInterfacePrivate {
37
public:
38
    enum EventType {
39
        UserInputEvent = 0x100,
40
        Close = UserInputEvent | 0x01,
41
        GeometryChange = 0x02,
42
        Enter = UserInputEvent | 0x03,
43
        Leave = UserInputEvent | 0x04,
44
        FocusWindow = 0x05,
45
        WindowStateChanged = 0x06,
46
        Mouse = UserInputEvent | 0x07,
47
        Wheel = UserInputEvent | 0x09,
48
        Key = UserInputEvent | 0x0a,
49
        Touch = UserInputEvent | 0x0b,
50
        ScreenOrientation = 0x0c,
51
        ScreenGeometry = 0x0d,
52
        ScreenAvailableGeometry = 0x0e,
53
        ScreenLogicalDotsPerInch = 0x0f,
54
        ScreenRefreshRate = 0x10,
55
        ThemeChange = 0x11,
56
        Expose = 0x12,
57
        FileOpen = UserInputEvent | 0x13,
58
        Tablet = UserInputEvent | 0x14,
59
        TabletEnterProximity = UserInputEvent | 0x15,
60
        TabletLeaveProximity = UserInputEvent | 0x16,
61
        PlatformPanel = UserInputEvent | 0x17,
62
        ContextMenu = UserInputEvent | 0x18,
63
        EnterWhatsThisMode = UserInputEvent | 0x19,
64
#ifndef QT_NO_GESTURES
65
        Gesture = UserInputEvent | 0x1a,
66
#endif
67
        ApplicationStateChanged = 0x19,
68
        FlushEvents = 0x20,
69
        WindowScreenChanged = 0x21,
70
        SafeAreaMarginsChanged = 0x22,
71
        ApplicationTermination = 0x23,
72
        Paint = 0x24,
73
        WindowDevicePixelRatioChanged = 0x25,
74
    };
75
76
    class WindowSystemEvent {
77
    public:
78
        enum {
79
            Synthetic = 0x1,
80
            NullWindow = 0x2
81
        };
82
83
        explicit WindowSystemEvent(EventType t)
84
0
            : type(t), flags(0), eventAccepted(true) { }
85
0
        virtual ~WindowSystemEvent() { }
86
87
0
        bool synthetic() const  { return flags & Synthetic; }
88
0
        bool nullWindow() const { return flags & NullWindow; }
89
90
        EventType type;
91
        int flags;
92
        bool eventAccepted;
93
    };
94
95
    class CloseEvent : public WindowSystemEvent {
96
    public:
97
        explicit CloseEvent(QWindow *w)
98
0
            : WindowSystemEvent(Close), window(w)
99
0
            { }
100
        QPointer<QWindow> window;
101
    };
102
103
    class GeometryChangeEvent : public WindowSystemEvent {
104
    public:
105
        GeometryChangeEvent(QWindow *window, QRect requestedGeometry, QRect newGeometry);
106
        QPointer<QWindow> window;
107
        QRect requestedGeometry;
108
        QRect newGeometry;
109
    };
110
111
    class EnterEvent : public WindowSystemEvent {
112
    public:
113
        explicit EnterEvent(QWindow *enter, const QPointF &local, const QPointF &global)
114
0
            : WindowSystemEvent(Enter), enter(enter), localPos(local), globalPos(global)
115
0
        { }
116
        QPointer<QWindow> enter;
117
        const QPointF localPos;
118
        const QPointF globalPos;
119
    };
120
121
    class LeaveEvent : public WindowSystemEvent {
122
    public:
123
        explicit LeaveEvent(QWindow *leave)
124
0
            : WindowSystemEvent(Leave), leave(leave)
125
0
        { }
126
        QPointer<QWindow> leave;
127
    };
128
129
    class FocusWindowEvent : public WindowSystemEvent {
130
    public:
131
        explicit FocusWindowEvent(QWindow *focusedWindow, Qt::FocusReason r)
132
0
            : WindowSystemEvent(FocusWindow), focused(focusedWindow), reason(r)
133
0
        { }
134
        QPointer<QWindow> focused;
135
        Qt::FocusReason reason;
136
    };
137
138
    class WindowStateChangedEvent : public WindowSystemEvent {
139
    public:
140
        WindowStateChangedEvent(QWindow *_window, Qt::WindowStates _newState, Qt::WindowStates _oldState)
141
0
            : WindowSystemEvent(WindowStateChanged), window(_window), newState(_newState), oldState(_oldState)
142
0
        { }
143
144
        QPointer<QWindow> window;
145
        Qt::WindowStates newState;
146
        Qt::WindowStates oldState;
147
    };
148
149
    class WindowScreenChangedEvent : public WindowSystemEvent {
150
    public:
151
        WindowScreenChangedEvent(QWindow *w, QScreen *s)
152
0
            : WindowSystemEvent(WindowScreenChanged), window(w), screen(s)
153
0
        { }
154
155
        QPointer<QWindow> window;
156
        QPointer<QScreen> screen;
157
    };
158
159
    class WindowDevicePixelRatioChangedEvent : public WindowSystemEvent {
160
    public:
161
        WindowDevicePixelRatioChangedEvent(QWindow *w)
162
0
            : WindowSystemEvent(WindowDevicePixelRatioChanged), window(w)
163
0
        { }
164
165
        QPointer<QWindow> window;
166
    };
167
168
    class SafeAreaMarginsChangedEvent : public WindowSystemEvent {
169
    public:
170
        SafeAreaMarginsChangedEvent(QWindow *w)
171
0
            : WindowSystemEvent(SafeAreaMarginsChanged), window(w)
172
0
        { }
173
174
        QPointer<QWindow> window;
175
    };
176
177
    class ApplicationStateChangedEvent : public WindowSystemEvent {
178
    public:
179
        ApplicationStateChangedEvent(Qt::ApplicationState newState, bool forcePropagate = false)
180
0
            : WindowSystemEvent(ApplicationStateChanged), newState(newState), forcePropagate(forcePropagate)
181
0
        { }
182
183
        Qt::ApplicationState newState;
184
        bool forcePropagate;
185
    };
186
187
    class FlushEventsEvent : public WindowSystemEvent {
188
    public:
189
        FlushEventsEvent(QEventLoop::ProcessEventsFlags f = QEventLoop::AllEvents)
190
0
            : WindowSystemEvent(FlushEvents)
191
0
            , flags(f)
192
0
        { }
193
        QEventLoop::ProcessEventsFlags flags;
194
    };
195
196
    class UserEvent : public WindowSystemEvent {
197
    public:
198
        UserEvent(QWindow * w, ulong time, EventType t)
199
0
            : WindowSystemEvent(t), window(w), timestamp(time)
200
0
        {
201
0
            if (!w)
202
0
                flags |= NullWindow;
203
0
        }
204
        QPointer<QWindow> window;
205
        unsigned long timestamp;
206
    };
207
208
    class InputEvent: public UserEvent {
209
    public:
210
        InputEvent(QWindow *w, ulong time, EventType t, Qt::KeyboardModifiers mods, const QInputDevice *dev)
211
0
            : UserEvent(w, time, t), modifiers(mods), device(dev) {}
212
        Qt::KeyboardModifiers modifiers;
213
        const QInputDevice *device;
214
    };
215
216
    class PointerEvent : public InputEvent {
217
    public:
218
        PointerEvent(QWindow * w, ulong time, EventType t, Qt::KeyboardModifiers mods, const QPointingDevice *device)
219
0
            : InputEvent(w, time, t, mods, device) {}
220
    };
221
222
    class MouseEvent : public PointerEvent {
223
    public:
224
        MouseEvent(QWindow *w, ulong time, const QPointF &local, const QPointF &global,
225
                   Qt::MouseButtons state, Qt::KeyboardModifiers mods,
226
                   Qt::MouseButton b, QEvent::Type type,
227
                   Qt::MouseEventSource src = Qt::MouseEventNotSynthesized, bool frame = false,
228
                   const QPointingDevice *device = QPointingDevice::primaryPointingDevice(),
229
                   int evPtId = -1)
230
0
            : PointerEvent(w, time, Mouse, mods, device), localPos(local), globalPos(global),
231
0
              buttons(state), source(src), nonClientArea(frame), button(b), buttonType(type),
232
0
              eventPointId(evPtId) { }
233
234
        QPointF localPos;
235
        QPointF globalPos;
236
        Qt::MouseButtons buttons;
237
        Qt::MouseEventSource source;
238
        bool nonClientArea;
239
        Qt::MouseButton button;
240
        QEvent::Type buttonType;
241
        int eventPointId; // from the original device if synth-mouse, otherwise -1
242
    };
243
244
    class WheelEvent : public PointerEvent {
245
    public:
246
        WheelEvent(QWindow *w, ulong time, const QPointF &local, const QPointF &global, QPoint pixelD, QPoint angleD, int qt4D, Qt::Orientation qt4O,
247
                   Qt::KeyboardModifiers mods, Qt::ScrollPhase phase = Qt::NoScrollPhase, Qt::MouseEventSource src = Qt::MouseEventNotSynthesized,
248
                   bool inverted = false, const QPointingDevice *device = QPointingDevice::primaryPointingDevice())
249
0
            : PointerEvent(w, time, Wheel, mods, device), pixelDelta(pixelD), angleDelta(angleD), qt4Delta(qt4D),
250
0
              qt4Orientation(qt4O), localPos(local), globalPos(global), phase(phase), source(src), inverted(inverted) { }
251
        QPoint pixelDelta;
252
        QPoint angleDelta;
253
        int qt4Delta;
254
        Qt::Orientation qt4Orientation;
255
        QPointF localPos;
256
        QPointF globalPos;
257
        Qt::ScrollPhase phase;
258
        Qt::MouseEventSource source;
259
        bool inverted;
260
    };
261
262
    class KeyEvent : public InputEvent {
263
    public:
264
        KeyEvent(QWindow *w, ulong time, QEvent::Type t, int k, Qt::KeyboardModifiers mods,
265
                 const QString & text = QString(), bool autorep = false, ushort count = 1,
266
                 const QInputDevice *device = QInputDevice::primaryKeyboard())
267
0
            : InputEvent(w, time, Key, mods, device), source(nullptr), key(k), unicode(text),
268
0
             repeat(autorep), repeatCount(count), keyType(t),
269
0
             nativeScanCode(0), nativeVirtualKey(0), nativeModifiers(0) { }
270
        KeyEvent(QWindow *w, ulong time, QEvent::Type t, int k, Qt::KeyboardModifiers mods,
271
                 quint32 nativeSC, quint32 nativeVK, quint32 nativeMods,
272
                 const QString & text = QString(), bool autorep = false, ushort count = 1,
273
                 const QInputDevice *device = QInputDevice::primaryKeyboard())
274
0
            : InputEvent(w, time, Key, mods, device), source(nullptr), key(k), unicode(text),
275
0
             repeat(autorep), repeatCount(count), keyType(t),
276
0
             nativeScanCode(nativeSC), nativeVirtualKey(nativeVK), nativeModifiers(nativeMods) { }
277
        const QInputDevice *source;
278
        int key;
279
        QString unicode;
280
        bool repeat;
281
        ushort repeatCount;
282
        QEvent::Type keyType;
283
        quint32 nativeScanCode;
284
        quint32 nativeVirtualKey;
285
        quint32 nativeModifiers;
286
    };
287
288
    class TouchEvent : public PointerEvent {
289
    public:
290
        TouchEvent(QWindow *w, ulong time, QEvent::Type t, const QPointingDevice *device,
291
                   const QList<QEventPoint> &p, Qt::KeyboardModifiers mods)
292
0
            : PointerEvent(w, time, Touch, mods, device), points(p), touchType(t) { }
293
        QList<QEventPoint> points;
294
        QEvent::Type touchType;
295
    };
296
297
    class ScreenOrientationEvent : public WindowSystemEvent {
298
    public:
299
        ScreenOrientationEvent(QScreen *s, Qt::ScreenOrientation o)
300
0
            : WindowSystemEvent(ScreenOrientation), screen(s), orientation(o) { }
301
        QPointer<QScreen> screen;
302
        Qt::ScreenOrientation orientation;
303
    };
304
305
    class ScreenGeometryEvent : public WindowSystemEvent {
306
    public:
307
        ScreenGeometryEvent(QScreen *s, const QRect &g, const QRect &ag)
308
0
            : WindowSystemEvent(ScreenGeometry), screen(s), geometry(g), availableGeometry(ag) { }
309
        QPointer<QScreen> screen;
310
        QRect geometry;
311
        QRect availableGeometry;
312
    };
313
314
    class ScreenLogicalDotsPerInchEvent : public WindowSystemEvent {
315
    public:
316
        ScreenLogicalDotsPerInchEvent(QScreen *s, qreal dx, qreal dy)
317
0
            : WindowSystemEvent(ScreenLogicalDotsPerInch), screen(s), dpiX(dx), dpiY(dy) { }
318
        QPointer<QScreen> screen;
319
        qreal dpiX;
320
        qreal dpiY;
321
    };
322
323
    class ScreenRefreshRateEvent : public WindowSystemEvent {
324
    public:
325
        ScreenRefreshRateEvent(QScreen *s, qreal r)
326
0
            : WindowSystemEvent(ScreenRefreshRate), screen(s), rate(r) { }
327
        QPointer<QScreen> screen;
328
        qreal rate;
329
    };
330
331
    class ThemeChangeEvent : public WindowSystemEvent {
332
    public:
333
        explicit ThemeChangeEvent()
334
0
            : WindowSystemEvent(ThemeChange) { }
335
    };
336
337
    class ExposeEvent : public WindowSystemEvent {
338
    public:
339
        ExposeEvent(QWindow *window, const QRegion &region);
340
        QPointer<QWindow> window;
341
        bool isExposed;
342
        QRegion region;
343
    };
344
345
    class PaintEvent : public WindowSystemEvent {
346
    public:
347
        PaintEvent(QWindow *window, const QRegion &region)
348
0
            :  WindowSystemEvent(Paint), window(window), region(region) {}
349
        QPointer<QWindow> window;
350
        QRegion region;
351
    };
352
353
    class FileOpenEvent : public WindowSystemEvent {
354
    public:
355
        FileOpenEvent(const QString& fileName)
356
0
            : WindowSystemEvent(FileOpen), url(QUrl::fromLocalFile(fileName))
357
0
        { }
358
        FileOpenEvent(const QUrl &url)
359
0
            : WindowSystemEvent(FileOpen), url(url)
360
0
        { }
361
        QUrl url;
362
    };
363
364
    class Q_GUI_EXPORT TabletEvent : public PointerEvent {
365
    public:
366
        // TODO take QPointingDevice* instead of types and IDs
367
        static void handleTabletEvent(QWindow *w, const QPointF &local, const QPointF &global,
368
                                      int device, int pointerType, Qt::MouseButtons buttons, qreal pressure, qreal xTilt, qreal yTilt,
369
                                      qreal tangentialPressure, qreal rotation, int z, qint64 uid,
370
                                      Qt::KeyboardModifiers modifiers = Qt::NoModifier);
371
        static void setPlatformSynthesizesMouse(bool v);
372
373
        TabletEvent(QWindow *w, ulong time, const QPointF &local, const QPointF &global,
374
                    const QPointingDevice *device, Qt::MouseButtons b, qreal pressure, qreal xTilt, qreal yTilt, qreal tpressure,
375
                    qreal rotation, int z, Qt::KeyboardModifiers mods)
376
0
            : PointerEvent(w, time, Tablet, mods, device),
377
0
              buttons(b), local(local), global(global),
378
0
              pressure(pressure), xTilt(xTilt), yTilt(yTilt), tangentialPressure(tpressure),
379
0
              rotation(rotation), z(z) { }
380
        Qt::MouseButtons buttons;
381
        QPointF local;
382
        QPointF global;
383
        qreal pressure;
384
        qreal xTilt;
385
        qreal yTilt;
386
        qreal tangentialPressure;
387
        qreal rotation;
388
        int z;
389
        static bool platformSynthesizesMouse;
390
    };
391
392
    class TabletEnterProximityEvent : public PointerEvent {
393
    public:
394
        // TODO store more info: position and whatever else we can get on most platforms
395
        TabletEnterProximityEvent(ulong time, const QPointingDevice *device)
396
0
            : PointerEvent(nullptr, time, TabletEnterProximity, Qt::NoModifier, device) { }
397
    };
398
399
    class TabletLeaveProximityEvent : public PointerEvent {
400
    public:
401
        // TODO store more info: position and whatever else we can get on most platforms
402
        TabletLeaveProximityEvent(ulong time, const QPointingDevice *device)
403
0
            : PointerEvent(nullptr, time, TabletLeaveProximity, Qt::NoModifier, device) { }
404
    };
405
406
    class PlatformPanelEvent : public WindowSystemEvent {
407
    public:
408
        explicit PlatformPanelEvent(QWindow *w)
409
0
            : WindowSystemEvent(PlatformPanel), window(w) { }
410
        QPointer<QWindow> window;
411
    };
412
413
#ifndef QT_NO_CONTEXTMENU
414
    class ContextMenuEvent : public WindowSystemEvent {
415
    public:
416
        explicit ContextMenuEvent(QWindow *w, bool mouseTriggered, const QPoint &pos,
417
                                  const QPoint &globalPos, Qt::KeyboardModifiers modifiers)
418
0
            : WindowSystemEvent(ContextMenu), window(w), mouseTriggered(mouseTriggered), pos(pos),
419
0
              globalPos(globalPos), modifiers(modifiers) { }
420
        QPointer<QWindow> window;
421
        bool mouseTriggered;
422
        QPoint pos;       // Only valid if triggered by mouse
423
        QPoint globalPos; // Only valid if triggered by mouse
424
        Qt::KeyboardModifiers modifiers;
425
    };
426
#endif
427
428
#ifndef QT_NO_GESTURES
429
    class GestureEvent : public PointerEvent {
430
    public:
431
        GestureEvent(QWindow *window, ulong time, Qt::NativeGestureType type, const QPointingDevice *dev,
432
                     int fingerCount, QPointF pos, QPointF globalPos, qreal realValue, QPointF delta)
433
0
            : PointerEvent(window, time, Gesture, Qt::NoModifier, dev), type(type), pos(pos), globalPos(globalPos),
434
0
              delta(delta), fingerCount(fingerCount), realValue(realValue), sequenceId(0), intValue(0) { }
435
        Qt::NativeGestureType type;
436
        QPointF pos;
437
        QPointF globalPos;
438
        QPointF delta;
439
        int fingerCount;
440
        // Mac
441
        qreal realValue;
442
        // Windows
443
        ulong sequenceId;
444
        quint64 intValue;
445
    };
446
#endif
447
448
    class WindowSystemEventList {
449
        QList<WindowSystemEvent *> impl;
450
        mutable QMutex mutex;
451
    public:
452
10
        WindowSystemEventList() : impl(), mutex() {}
453
0
        ~WindowSystemEventList() { clear(); }
454
455
        void clear()
456
0
        { const QMutexLocker locker(&mutex); qDeleteAll(impl); impl.clear(); }
457
        void prepend(WindowSystemEvent *e)
458
0
        { const QMutexLocker locker(&mutex); impl.prepend(e); }
459
        WindowSystemEvent *takeFirstOrReturnNull()
460
0
        { const QMutexLocker locker(&mutex); return impl.empty() ? nullptr : impl.takeFirst(); }
461
        WindowSystemEvent *takeFirstNonUserInputOrReturnNull()
462
0
        {
463
0
            const QMutexLocker locker(&mutex);
464
0
            for (int i = 0; i < impl.size(); ++i)
465
0
                if (!(impl.at(i)->type & QWindowSystemInterfacePrivate::UserInputEvent))
466
0
                    return impl.takeAt(i);
467
0
            return nullptr;
468
0
        }
469
        bool nonUserInputEventsQueued()
470
0
        {
471
0
            const QMutexLocker locker(&mutex);
472
0
            for (int i = 0; i < impl.size(); ++i)
473
0
                if (!(impl.at(i)->type & QWindowSystemInterfacePrivate::UserInputEvent))
474
0
                    return true;
475
0
            return false;
476
0
        }
477
        void append(WindowSystemEvent *e)
478
0
        { const QMutexLocker locker(&mutex); impl.append(e); }
479
        qsizetype count() const
480
0
        { const QMutexLocker locker(&mutex); return impl.size(); }
481
        WindowSystemEvent *peekAtFirstOfType(EventType t) const
482
0
        {
483
0
            const QMutexLocker locker(&mutex);
484
0
            for (int i = 0; i < impl.size(); ++i) {
485
0
                if (impl.at(i)->type == t)
486
0
                    return impl.at(i);
487
0
            }
488
0
            return nullptr;
489
0
        }
490
        void remove(const WindowSystemEvent *e)
491
0
        {
492
0
            const QMutexLocker locker(&mutex);
493
0
            for (int i = 0; i < impl.size(); ++i) {
494
0
                if (impl.at(i) == e) {
495
0
                    delete impl.takeAt(i);
496
0
                    break;
497
0
                }
498
0
            }
499
0
        }
500
    private:
501
        Q_DISABLE_COPY_MOVE(WindowSystemEventList)
502
    };
503
504
    static WindowSystemEventList windowSystemEventQueue;
505
506
    static qsizetype windowSystemEventsQueued();
507
    static bool nonUserInputEventsQueued();
508
    static WindowSystemEvent *getWindowSystemEvent();
509
    static WindowSystemEvent *getNonUserInputWindowSystemEvent();
510
    static WindowSystemEvent *peekWindowSystemEvent(EventType t);
511
    static void removeWindowSystemEvent(WindowSystemEvent *event);
512
513
public:
514
    static QElapsedTimer eventTime;
515
    static bool synchronousWindowSystemEvents;
516
    static bool platformFiltersEvents;
517
518
    static QWaitCondition eventsFlushed;
519
    static QMutex flushEventMutex;
520
    static QAtomicInt eventAccepted;
521
522
    static QList<QEventPoint>
523
        fromNativeTouchPoints(const QList<QWindowSystemInterface::TouchPoint> &points,
524
                              const QWindow *window, QEvent::Type *type = nullptr);
525
    template<class EventPointList>
526
    static QList<QWindowSystemInterface::TouchPoint>
527
        toNativeTouchPoints(const EventPointList &pointList, const QWindow *window)
528
0
    {
529
0
        QList<QWindowSystemInterface::TouchPoint> newList;
530
0
        newList.reserve(pointList.size());
531
0
        for (const auto &point : pointList) {
532
0
            newList.append(toNativeTouchPoint(point, window));
533
0
        }
534
0
        return newList;
535
0
    }
536
    static QWindowSystemInterface::TouchPoint
537
        toNativeTouchPoint(const QEventPoint &point, const QWindow *window);
538
539
    static void installWindowSystemEventHandler(QWindowSystemEventHandler *handler);
540
    static void removeWindowSystemEventhandler(QWindowSystemEventHandler *handler);
541
    static QWindowSystemEventHandler *eventHandler;
542
};
543
544
class Q_GUI_EXPORT QWindowSystemEventHandler
545
{
546
public:
547
    virtual ~QWindowSystemEventHandler();
548
    virtual bool sendEvent(QWindowSystemInterfacePrivate::WindowSystemEvent *event);
549
};
550
551
QT_END_NAMESPACE
552
553
#endif // QWINDOWSYSTEMINTERFACE_P_H