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/qguiapplication_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
5
#ifndef QGUIAPPLICATION_P_H
6
#define QGUIAPPLICATION_P_H
7
8
//
9
//  W A R N I N G
10
//  -------------
11
//
12
// This file is not part of the Qt API.  It exists purely as an
13
// implementation detail.  This header file may change from version to
14
// version without notice, or even be removed.
15
//
16
// We mean it.
17
//
18
19
#include <QtGui/private/qtguiglobal_p.h>
20
#include <QtGui/qguiapplication.h>
21
#include <QtGui/qicon.h>
22
23
#include <QtCore/QHash>
24
#include <QtCore/QPointF>
25
#include <QtCore/private/qcoreapplication_p.h>
26
#include <QtCore/qbasicatomic.h>
27
28
#include <QtCore/qnativeinterface.h>
29
#include <QtCore/private/qnativeinterface_p.h>
30
#include <QtCore/private/qnumeric_p.h>
31
#include <QtCore/private/qthread_p.h>
32
33
#include <qpa/qwindowsysteminterface.h>
34
#include <qpa/qwindowsysteminterface_p.h>
35
#if QT_CONFIG(shortcut)
36
#  include "private/qshortcutmap_p.h"
37
#endif
38
39
#include <QtCore/qpointer.h>
40
41
#include <memory>
42
43
QT_BEGIN_NAMESPACE
44
45
Q_DECLARE_LOGGING_CATEGORY(lcPopup)
46
Q_DECLARE_LOGGING_CATEGORY(lcVirtualKeyboard)
47
48
class QColorTrcLut;
49
class QPlatformIntegration;
50
class QPlatformTheme;
51
class QPlatformDragQtResponse;
52
#if QT_CONFIG(draganddrop)
53
class QDrag;
54
#endif // QT_CONFIG(draganddrop)
55
class QInputDeviceManager;
56
#ifndef QT_NO_ACTION
57
class QActionPrivate;
58
#endif
59
#if QT_CONFIG(shortcut)
60
class QShortcutPrivate;
61
#endif
62
class QThreadPool;
63
64
class Q_GUI_EXPORT QGuiApplicationPrivate : public QCoreApplicationPrivate
65
{
66
0
    Q_DECLARE_PUBLIC(QGuiApplication)
Unexecuted instantiation: QGuiApplicationPrivate::q_func()
Unexecuted instantiation: QGuiApplicationPrivate::q_func() const
67
0
public:
68
0
    QGuiApplicationPrivate(int &argc, char **argv);
69
0
    ~QGuiApplicationPrivate();
70
0
71
0
    void init();
72
0
73
0
    void createPlatformIntegration();
74
0
    void createEventDispatcher() override;
75
0
    void eventDispatcherReady() override;
76
0
77
0
    virtual void notifyLayoutDirectionChange();
78
0
    virtual void notifyActiveWindowChange(QWindow *previous);
79
0
80
0
#if QT_CONFIG(commandlineparser)
81
0
    void addQtOptions(QList<QCommandLineOption> *options) override;
82
0
#endif
83
0
    bool canQuitAutomatically() override;
84
0
    void quit() override;
85
0
86
0
    void maybeLastWindowClosed();
87
0
    bool lastWindowClosed() const;
88
0
    static bool quitOnLastWindowClosed;
89
0
90
0
    static void captureGlobalModifierState(QEvent *e);
91
0
    static Qt::KeyboardModifiers modifier_buttons;
92
0
    static Qt::MouseButtons mouse_buttons;
93
0
94
0
    static QPlatformIntegration *platform_integration;
95
0
96
0
    static QPlatformIntegration *platformIntegration()
97
12.3k
    { return platform_integration; }
98
99
    static QPlatformTheme *platform_theme;
100
101
    static QPlatformTheme *platformTheme()
102
0
    { return platform_theme; }
103
104
    static QAbstractEventDispatcher *qt_qpa_core_dispatcher()
105
0
    {
106
0
        if (QCoreApplication::instance())
107
0
            return QCoreApplication::instance()->d_func()->threadData.loadRelaxed()->eventDispatcher.loadRelaxed();
108
0
        else
109
0
            return nullptr;
110
0
    }
111
112
    static void processMouseEvent(QWindowSystemInterfacePrivate::MouseEvent *e);
113
    static void processKeyEvent(QWindowSystemInterfacePrivate::KeyEvent *e);
114
    static void processWheelEvent(QWindowSystemInterfacePrivate::WheelEvent *e);
115
    static void processTouchEvent(QWindowSystemInterfacePrivate::TouchEvent *e);
116
117
    static void processCloseEvent(QWindowSystemInterfacePrivate::CloseEvent *e);
118
119
    static void processGeometryChangeEvent(QWindowSystemInterfacePrivate::GeometryChangeEvent *e);
120
121
    static void processEnterEvent(QWindowSystemInterfacePrivate::EnterEvent *e);
122
    static void processLeaveEvent(QWindowSystemInterfacePrivate::LeaveEvent *e);
123
124
    static void processFocusWindowEvent(QWindowSystemInterfacePrivate::FocusWindowEvent *e);
125
126
    static void processWindowStateChangedEvent(QWindowSystemInterfacePrivate::WindowStateChangedEvent *e);
127
    static void processWindowScreenChangedEvent(QWindowSystemInterfacePrivate::WindowScreenChangedEvent *e);
128
    static void processWindowDevicePixelRatioChangedEvent(QWindowSystemInterfacePrivate::WindowDevicePixelRatioChangedEvent *e);
129
130
    static void processSafeAreaMarginsChangedEvent(QWindowSystemInterfacePrivate::SafeAreaMarginsChangedEvent *e);
131
132
    static void processWindowSystemEvent(QWindowSystemInterfacePrivate::WindowSystemEvent *e);
133
134
    static void processApplicationTermination(QWindowSystemInterfacePrivate::WindowSystemEvent *e);
135
136
    static void updateFilteredScreenOrientation(QScreen *screen);
137
    static void processScreenOrientationChange(QWindowSystemInterfacePrivate::ScreenOrientationEvent *e);
138
    static void processScreenGeometryChange(QWindowSystemInterfacePrivate::ScreenGeometryEvent *e);
139
    static void processScreenLogicalDotsPerInchChange(QWindowSystemInterfacePrivate::ScreenLogicalDotsPerInchEvent *e);
140
    static void processScreenRefreshRateChange(QWindowSystemInterfacePrivate::ScreenRefreshRateEvent *e);
141
    static void processThemeChanged(QWindowSystemInterfacePrivate::ThemeChangeEvent *tce);
142
143
    static void processExposeEvent(QWindowSystemInterfacePrivate::ExposeEvent *e);
144
    static void processPaintEvent(QWindowSystemInterfacePrivate::PaintEvent *e);
145
146
    static void processFileOpenEvent(QWindowSystemInterfacePrivate::FileOpenEvent *e);
147
148
    static void processTabletEvent(QWindowSystemInterfacePrivate::TabletEvent *e);
149
    static void processTabletEnterProximityEvent(QWindowSystemInterfacePrivate::TabletEnterProximityEvent *e);
150
    static void processTabletLeaveProximityEvent(QWindowSystemInterfacePrivate::TabletLeaveProximityEvent *e);
151
152
#ifndef QT_NO_GESTURES
153
    static void processGestureEvent(QWindowSystemInterfacePrivate::GestureEvent *e);
154
#endif
155
156
    static void processPlatformPanelEvent(QWindowSystemInterfacePrivate::PlatformPanelEvent *e);
157
#ifndef QT_NO_CONTEXTMENU
158
    static void processContextMenuEvent(QWindowSystemInterfacePrivate::ContextMenuEvent *e);
159
#endif
160
161
#if QT_CONFIG(draganddrop)
162
    static QPlatformDragQtResponse processDrag(QWindow *w, const QMimeData *dropData,
163
                                               const QPoint &p, Qt::DropActions supportedActions,
164
                                               Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers);
165
    static QPlatformDropQtResponse processDrop(QWindow *w, const QMimeData *dropData,
166
                                               const QPoint &p, Qt::DropActions supportedActions,
167
                                               Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers);
168
#endif
169
170
    static bool processNativeEvent(QWindow *window, const QByteArray &eventType, void *message, qintptr *result);
171
172
    static bool sendQWindowEventToQPlatformWindow(QWindow *window, QEvent *event);
173
174
    static bool maybeForwardEventToVirtualKeyboard(QEvent *e);
175
    static bool isUsingVirtualKeyboard();
176
177
    static inline Qt::Alignment visualAlignment(Qt::LayoutDirection direction, Qt::Alignment alignment)
178
0
    {
179
0
        if (!(alignment & Qt::AlignHorizontal_Mask))
180
0
            alignment |= Qt::AlignLeft;
181
0
        if (!(alignment & Qt::AlignAbsolute) && (alignment & (Qt::AlignLeft | Qt::AlignRight))) {
182
0
            if (direction == Qt::RightToLeft)
183
0
                alignment ^= (Qt::AlignLeft | Qt::AlignRight);
184
0
            alignment |= Qt::AlignAbsolute;
185
0
        }
186
0
        return alignment;
187
0
    }
188
189
    QPixmap getPixmapCursor(Qt::CursorShape cshape);
190
191
    void _q_updateFocusObject(QObject *object);
192
193
24.7k
    static QGuiApplicationPrivate *instance() { QT_IGNORE_DEPRECATIONS(return self;) }
194
195
    static QIcon *app_icon;
196
    static QString *platform_name;
197
    static QString *displayName;
198
    static QString *desktopFileName;
199
200
    QWindowList modalWindowList;
201
    static void showModalWindow(QWindow *window);
202
    static void hideModalWindow(QWindow *window);
203
    static void updateBlockedStatus(QWindow *window);
204
205
    virtual Qt::WindowModality defaultModality() const;
206
    virtual bool windowNeverBlocked(QWindow *window) const;
207
    bool isWindowBlocked(QWindow *window, QWindow **blockingWindow = nullptr) const;
208
0
    static qsizetype popupCount() { return QGuiApplicationPrivate::popup_list.size(); }
209
    static QWindow *activePopupWindow();
210
    static void activatePopup(QWindow *popup);
211
    static bool closePopup(QWindow *popup);
212
    static bool closeAllPopups();
213
214
    static Qt::MouseButton mousePressButton;
215
    static struct QLastCursorPosition {
216
        // Initialize to a far-offscreen position.  2^23 is small enough for accurate arithmetic
217
        // (even manhattanLength()) even when stored in the mantissa of a 32-bit float.
218
0
        constexpr inline QLastCursorPosition() noexcept : thePoint(1 << 23, 1 << 23) {}
219
0
        constexpr inline Q_IMPLICIT QLastCursorPosition(QPointF p) noexcept : thePoint(p) {}
220
0
        constexpr inline Q_IMPLICIT operator QPointF() const noexcept { return thePoint; }
221
0
        constexpr inline qreal x() const noexcept{ return thePoint.x(); }
222
0
        constexpr inline qreal y() const noexcept{ return thePoint.y(); }
223
        Q_GUI_EXPORT QPoint toPoint() const noexcept;
224
225
0
        constexpr void reset() noexcept { *this = QLastCursorPosition{}; }
226
227
        // QGuiApplicationPrivate::lastCursorPosition is used for mouse-move detection
228
        // but even QPointF's qFuzzCompare on doubles is too precise, and causes move-noise
229
        // e.g. on macOS (see QTBUG-111170). So we specialize the equality operators here
230
        // to use single-point precision.
231
        friend constexpr bool operator==(const QLastCursorPosition &p1, const QPointF &p2) noexcept
232
0
        {
233
0
            return QtPrivate::fuzzyCompare(float(p1.x()), float(p2.x()))
234
0
                && QtPrivate::fuzzyCompare(float(p1.y()), float(p2.y()));
235
0
        }
236
        friend constexpr bool operator!=(const QLastCursorPosition &p1, const QPointF &p2) noexcept
237
0
        {
238
0
            return !(p1 == p2);
239
0
        }
240
        friend constexpr bool operator==(const QPointF &p1, const QLastCursorPosition &p2) noexcept
241
0
        {
242
0
            return p2 == p1;
243
0
        }
244
        friend constexpr bool operator!=(const QPointF &p1, const QLastCursorPosition &p2) noexcept
245
0
        {
246
0
            return !(p2 == p1);
247
0
        }
248
249
    private:
250
        QPointF thePoint;
251
    } lastCursorPosition;
252
    static QWindow *currentMouseWindow;
253
    static QWindow *currentMousePressWindow;
254
    static Qt::ApplicationState applicationState;
255
    static Qt::HighDpiScaleFactorRoundingPolicy highDpiScaleFactorRoundingPolicy;
256
    static QPointer<QWindow> currentDragWindow;
257
258
    // TODO remove this: QPointingDevice can store what we need directly
259
    struct TabletPointData {
260
0
        TabletPointData(qint64 devId = 0) : deviceId(devId), state(Qt::NoButton), target(nullptr) {}
261
        qint64 deviceId;
262
        Qt::MouseButtons state;
263
        QWindow *target;
264
    };
265
    static QList<TabletPointData> tabletDevicePoints;
266
    static TabletPointData &tabletDevicePoint(qint64 deviceId);
267
268
#ifndef QT_NO_CLIPBOARD
269
    static QClipboard *qt_clipboard;
270
#endif
271
272
    static QPalette *app_pal;
273
274
    static QWindowList window_list;
275
    static QWindowList popup_list;
276
    static const QWindow *active_popup_on_press;
277
    static QWindow *focus_window;
278
279
#ifndef QT_NO_CURSOR
280
    QList<QCursor> cursor_list;
281
#endif
282
    static QList<QScreen *> screen_list;
283
284
    static QFont *app_font;
285
286
    static QString styleOverride;
287
    static QStyleHints *styleHints;
288
    static bool obey_desktop_settings;
289
    static bool popup_closed_on_press;
290
    QInputMethod *inputMethod;
291
292
    QString firstWindowTitle;
293
    QIcon forcedWindowIcon;
294
295
    static QList<QObject *> generic_plugin_list;
296
#if QT_CONFIG(shortcut)
297
    QShortcutMap shortcutMap;
298
#endif
299
300
#ifndef QT_NO_SESSIONMANAGER
301
    QSessionManager *session_manager;
302
    bool is_session_restored;
303
    bool is_saving_session;
304
    void commitData();
305
    void saveState();
306
#endif
307
308
    QEvent::Type lastTouchType;
309
    struct SynthesizedMouseData {
310
        SynthesizedMouseData(const QPointF &p, const QPointF &sp, QWindow *w)
311
0
            : pos(p), screenPos(sp), window(w) { }
312
        QPointF pos;
313
        QPointF screenPos;
314
        QPointer<QWindow> window;
315
    };
316
    QHash<QWindow *, SynthesizedMouseData> synthesizedMousePoints;
317
318
    static QInputDeviceManager *inputDeviceManager();
319
320
    const QColorTrcLut *colorProfileForA8Text();
321
    const QColorTrcLut *colorProfileForA32Text();
322
323
    // hook reimplemented in QApplication to apply the QStyle function on the QIcon
324
0
    virtual QPixmap applyQIconStyleHelper(QIcon::Mode, const QPixmap &basePixmap) const { return basePixmap; }
325
326
    virtual void notifyWindowIconChanged();
327
328
    static void applyWindowGeometrySpecificationTo(QWindow *window);
329
330
    static void setApplicationState(Qt::ApplicationState state, bool forcePropagate = false);
331
332
    static void resetCachedDevicePixelRatio();
333
334
#ifndef QT_NO_ACTION
335
    virtual QActionPrivate *createActionPrivate() const;
336
#endif
337
#ifndef QT_NO_SHORTCUT
338
    virtual QShortcutPrivate *createShortcutPrivate() const;
339
#endif
340
341
    static void updatePalette();
342
343
    static QEvent::Type contextMenuEventType();
344
345
    static QThreadPool *qtGuiThreadPool();
346
347
#ifndef QT_NO_OPENGL
348
    bool ownGlobalShareContext = false;
349
#endif
350
351
    void _q_updatePrimaryScreenDpis();
352
    static QBasicAtomicInt m_primaryScreenDpis;
353
354
protected:
355
    virtual void handleThemeChanged();
356
357
    static bool setPalette(const QPalette &palette);
358
    virtual QPalette basePalette() const;
359
    virtual void handlePaletteChanged(const char *className = nullptr);
360
361
#if QT_CONFIG(draganddrop)
362
    virtual void notifyDragStarted(const QDrag *);
363
#endif // QT_CONFIG(draganddrop)
364
365
private:
366
    static void clearPalette();
367
368
    friend class QDragManager;
369
    friend class QWindowPrivate;
370
371
    static QGuiApplicationPrivate *self;
372
    static int m_fakeMouseSourcePointId;
373
#ifdef Q_OS_WIN
374
    std::shared_ptr<QColorTrcLut> m_a8ColorProfile;
375
#endif
376
    std::shared_ptr<QColorTrcLut> m_a32ColorProfile;
377
378
    static QInputDeviceManager *m_inputDeviceManager;
379
380
    // Cache the maximum device pixel ratio, to iterate through the screen list
381
    // only the first time it's required, or when devices are added or removed.
382
    static qreal m_maxDevicePixelRatio;
383
};
384
385
// ----------------- QNativeInterface -----------------
386
387
class QWindowsMimeConverter;
388
389
namespace QNativeInterface::Private {
390
391
#if defined(Q_OS_WIN) || defined(Q_QDOC)
392
393
394
struct Q_GUI_EXPORT QWindowsApplication
395
{
396
    QT_DECLARE_NATIVE_INTERFACE(QWindowsApplication, 1, QGuiApplication)
397
398
    enum WindowActivationBehavior {
399
        DefaultActivateWindow,
400
        AlwaysActivateWindow
401
    };
402
403
    enum TouchWindowTouchType {
404
        NormalTouch   = 0x00000000,
405
        FineTouch     = 0x00000001,
406
        WantPalmTouch = 0x00000002
407
    };
408
409
    Q_DECLARE_FLAGS(TouchWindowTouchTypes, TouchWindowTouchType)
410
411
    enum DarkModeHandlingFlag {
412
        DarkModeWindowFrames = 0x1,
413
        DarkModeStyle = 0x2
414
    };
415
416
    Q_DECLARE_FLAGS(DarkModeHandling, DarkModeHandlingFlag)
417
418
    virtual void setTouchWindowTouchType(TouchWindowTouchTypes type) = 0;
419
    virtual TouchWindowTouchTypes touchWindowTouchType() const = 0;
420
421
    virtual WindowActivationBehavior windowActivationBehavior() const = 0;
422
    virtual void setWindowActivationBehavior(WindowActivationBehavior behavior) = 0;
423
424
    virtual void setHasBorderInFullScreenDefault(bool border) = 0;
425
426
    virtual bool isTabletMode() const = 0;
427
428
    virtual bool isWinTabEnabled() const = 0;
429
    virtual bool setWinTabEnabled(bool enabled) = 0;
430
431
    virtual DarkModeHandling darkModeHandling() const = 0;
432
    virtual void setDarkModeHandling(DarkModeHandling handling) = 0;
433
434
    virtual void registerMime(QWindowsMimeConverter *mime) = 0;
435
    virtual void unregisterMime(QWindowsMimeConverter *mime) = 0;
436
437
    virtual int registerMimeType(const QString &mime) = 0;
438
439
    virtual HWND createMessageWindow(const QString &classNameTemplate,
440
                                     const QString &windowName,
441
                                     QFunctionPointer eventProc = nullptr) const = 0;
442
443
    virtual bool asyncExpose() const = 0; // internal, used by Active Qt
444
    virtual void setAsyncExpose(bool value) = 0;
445
446
    virtual QVariant gpu() const = 0; // internal, used by qtdiag
447
    virtual QVariant gpuList() const = 0;
448
449
    virtual void populateLightSystemPalette(QPalette &pal) const = 0;
450
};
451
#endif // Q_OS_WIN
452
453
} // QNativeInterface::Private
454
455
#if defined(Q_OS_WIN)
456
Q_DECLARE_OPERATORS_FOR_FLAGS(QNativeInterface::Private::QWindowsApplication::TouchWindowTouchTypes)
457
Q_DECLARE_OPERATORS_FOR_FLAGS(QNativeInterface::Private::QWindowsApplication::DarkModeHandling)
458
#endif
459
460
QT_END_NAMESPACE
461
462
#endif // QGUIAPPLICATION_P_H