1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtGui module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QGUIAPPLICATION_P_H
41#define QGUIAPPLICATION_P_H
42
43//
44// W A R N I N G
45// -------------
46//
47// This file is not part of the Qt API. It exists purely as an
48// implementation detail. This header file may change from version to
49// version without notice, or even be removed.
50//
51// We mean it.
52//
53
54#include <QtGui/private/qtguiglobal_p.h>
55#include <QtGui/qguiapplication.h>
56
57#include <QtCore/QPointF>
58#include <QtCore/QSharedPointer>
59#include <QtCore/private/qcoreapplication_p.h>
60
61#include <QtCore/private/qthread_p.h>
62
63#include <qpa/qwindowsysteminterface.h>
64#include <qpa/qwindowsysteminterface_p.h>
65#include "private/qshortcutmap_p.h"
66#include <qicon.h>
67
68QT_BEGIN_NAMESPACE
69
70class QColorTrcLut;
71class QPlatformIntegration;
72class QPlatformTheme;
73class QPlatformDragQtResponse;
74#if QT_CONFIG(draganddrop)
75class QDrag;
76#endif // QT_CONFIG(draganddrop)
77class QInputDeviceManager;
78
79class Q_GUI_EXPORT QGuiApplicationPrivate : public QCoreApplicationPrivate
80{
81 Q_DECLARE_PUBLIC(QGuiApplication)
82public:
83 QGuiApplicationPrivate(int &argc, char **argv, int flags);
84 ~QGuiApplicationPrivate();
85
86 void init();
87
88 void createPlatformIntegration();
89 void createEventDispatcher() override;
90 void eventDispatcherReady() override;
91
92 virtual void notifyLayoutDirectionChange();
93 virtual void notifyActiveWindowChange(QWindow *previous);
94
95#if QT_CONFIG(commandlineparser)
96 void addQtOptions(QList<QCommandLineOption> *options) override;
97#endif
98 virtual bool shouldQuit() override;
99
100 bool shouldQuitInternal(const QWindowList &processedWindows);
101 virtual bool tryCloseAllWindows();
102
103 static Qt::KeyboardModifiers modifier_buttons;
104 static Qt::MouseButtons mouse_buttons;
105
106 static QPlatformIntegration *platform_integration;
107
108 static QPlatformIntegration *platformIntegration()
109 { return platform_integration; }
110
111 static QPlatformTheme *platform_theme;
112
113 static QPlatformTheme *platformTheme()
114 { return platform_theme; }
115
116 static QAbstractEventDispatcher *qt_qpa_core_dispatcher()
117 {
118 if (QCoreApplication::instance())
119 return QCoreApplication::instance()->d_func()->threadData->eventDispatcher.loadRelaxed();
120 else
121 return nullptr;
122 }
123
124 static void processMouseEvent(QWindowSystemInterfacePrivate::MouseEvent *e);
125 static void processKeyEvent(QWindowSystemInterfacePrivate::KeyEvent *e);
126 static void processWheelEvent(QWindowSystemInterfacePrivate::WheelEvent *e);
127 static void processTouchEvent(QWindowSystemInterfacePrivate::TouchEvent *e);
128
129 static void processCloseEvent(QWindowSystemInterfacePrivate::CloseEvent *e);
130
131 static void processGeometryChangeEvent(QWindowSystemInterfacePrivate::GeometryChangeEvent *e);
132
133 static void processEnterEvent(QWindowSystemInterfacePrivate::EnterEvent *e);
134 static void processLeaveEvent(QWindowSystemInterfacePrivate::LeaveEvent *e);
135
136 static void processActivatedEvent(QWindowSystemInterfacePrivate::ActivatedWindowEvent *e);
137 static void processWindowStateChangedEvent(QWindowSystemInterfacePrivate::WindowStateChangedEvent *e);
138 static void processWindowScreenChangedEvent(QWindowSystemInterfacePrivate::WindowScreenChangedEvent *e);
139
140 static void processSafeAreaMarginsChangedEvent(QWindowSystemInterfacePrivate::SafeAreaMarginsChangedEvent *e);
141
142 static void processWindowSystemEvent(QWindowSystemInterfacePrivate::WindowSystemEvent *e);
143
144 static void updateFilteredScreenOrientation(QScreen *screen);
145 static void reportScreenOrientationChange(QScreen *screen);
146 static void processScreenOrientationChange(QWindowSystemInterfacePrivate::ScreenOrientationEvent *e);
147 static void processScreenGeometryChange(QWindowSystemInterfacePrivate::ScreenGeometryEvent *e);
148 static void processScreenLogicalDotsPerInchChange(QWindowSystemInterfacePrivate::ScreenLogicalDotsPerInchEvent *e);
149 static void processScreenRefreshRateChange(QWindowSystemInterfacePrivate::ScreenRefreshRateEvent *e);
150 static void processThemeChanged(QWindowSystemInterfacePrivate::ThemeChangeEvent *tce);
151
152 static void processExposeEvent(QWindowSystemInterfacePrivate::ExposeEvent *e);
153
154 static void processFileOpenEvent(QWindowSystemInterfacePrivate::FileOpenEvent *e);
155
156 static void processTabletEvent(QWindowSystemInterfacePrivate::TabletEvent *e);
157 static void processTabletEnterProximityEvent(QWindowSystemInterfacePrivate::TabletEnterProximityEvent *e);
158 static void processTabletLeaveProximityEvent(QWindowSystemInterfacePrivate::TabletLeaveProximityEvent *e);
159
160#ifndef QT_NO_GESTURES
161 static void processGestureEvent(QWindowSystemInterfacePrivate::GestureEvent *e);
162#endif
163
164 static void processPlatformPanelEvent(QWindowSystemInterfacePrivate::PlatformPanelEvent *e);
165#ifndef QT_NO_CONTEXTMENU
166 static void processContextMenuEvent(QWindowSystemInterfacePrivate::ContextMenuEvent *e);
167#endif
168
169#if QT_CONFIG(draganddrop)
170 static QPlatformDragQtResponse processDrag(QWindow *w, const QMimeData *dropData,
171 const QPoint &p, Qt::DropActions supportedActions,
172 Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers);
173 static QPlatformDropQtResponse processDrop(QWindow *w, const QMimeData *dropData,
174 const QPoint &p, Qt::DropActions supportedActions,
175 Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers);
176#endif
177
178#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
179 static bool processNativeEvent(QWindow *window, const QByteArray &eventType, void *message, qintptr *result);
180#else
181 static bool processNativeEvent(QWindow *window, const QByteArray &eventType, void *message, long *result);
182#endif
183
184 static bool sendQWindowEventToQPlatformWindow(QWindow *window, QEvent *event);
185
186 static inline Qt::Alignment visualAlignment(Qt::LayoutDirection direction, Qt::Alignment alignment)
187 {
188 if (!(alignment & Qt::AlignHorizontal_Mask))
189 alignment |= Qt::AlignLeft;
190 if (!(alignment & Qt::AlignAbsolute) && (alignment & (Qt::AlignLeft | Qt::AlignRight))) {
191 if (direction == Qt::RightToLeft)
192 alignment ^= (Qt::AlignLeft | Qt::AlignRight);
193 alignment |= Qt::AlignAbsolute;
194 }
195 return alignment;
196 }
197
198 static void emitLastWindowClosed();
199
200 QPixmap getPixmapCursor(Qt::CursorShape cshape);
201
202 void _q_updateFocusObject(QObject *object);
203
204 static QGuiApplicationPrivate *instance() { return self; }
205
206 static QIcon *app_icon;
207 static QString *platform_name;
208 static QString *displayName;
209 static QString *desktopFileName;
210
211 QWindowList modalWindowList;
212 static void showModalWindow(QWindow *window);
213 static void hideModalWindow(QWindow *window);
214 static void updateBlockedStatus(QWindow *window);
215 virtual bool isWindowBlocked(QWindow *window, QWindow **blockingWindow = nullptr) const;
216 virtual bool popupActive() { return false; }
217
218 static ulong mousePressTime;
219 static Qt::MouseButton mousePressButton;
220 static int mousePressX;
221 static int mousePressY;
222 static QPointF lastCursorPosition;
223 static QWindow *currentMouseWindow;
224 static QWindow *currentMousePressWindow;
225 static Qt::ApplicationState applicationState;
226 static bool highDpiScalingUpdated;
227 static QPointer<QWindow> currentDragWindow;
228
229 struct TabletPointData {
230 TabletPointData(qint64 devId = 0) : deviceId(devId), state(Qt::NoButton), target(nullptr) {}
231 qint64 deviceId;
232 Qt::MouseButtons state;
233 QWindow *target;
234 };
235 static QVector<TabletPointData> tabletDevicePoints;
236 static TabletPointData &tabletDevicePoint(qint64 deviceId);
237
238#ifndef QT_NO_CLIPBOARD
239 static QClipboard *qt_clipboard;
240#endif
241
242 static QPalette *app_pal;
243
244 static QWindowList window_list;
245 static QWindow *focus_window;
246
247#ifndef QT_NO_CURSOR
248 QList<QCursor> cursor_list;
249#endif
250 static QList<QScreen *> screen_list;
251
252 static QFont *app_font;
253
254 static QString styleOverride;
255 static QStyleHints *styleHints;
256 static bool obey_desktop_settings;
257 QInputMethod *inputMethod;
258
259 QString firstWindowTitle;
260 QIcon forcedWindowIcon;
261
262 static QList<QObject *> generic_plugin_list;
263#ifndef QT_NO_SHORTCUT
264 QShortcutMap shortcutMap;
265#endif
266
267#ifndef QT_NO_SESSIONMANAGER
268 static bool is_fallback_session_management_enabled;
269 QSessionManager *session_manager;
270 bool is_session_restored;
271 bool is_saving_session;
272 void commitData();
273 void saveState();
274#endif
275
276 struct ActiveTouchPointsKey {
277 ActiveTouchPointsKey(QTouchDevice *dev, int id) : device(dev), touchPointId(id) { }
278 QTouchDevice *device;
279 int touchPointId;
280 };
281 struct ActiveTouchPointsValue {
282 QPointer<QWindow> window;
283 QPointer<QObject> target;
284 QTouchEvent::TouchPoint touchPoint;
285 };
286 QHash<ActiveTouchPointsKey, ActiveTouchPointsValue> activeTouchPoints;
287 QEvent::Type lastTouchType;
288 struct SynthesizedMouseData {
289 SynthesizedMouseData(const QPointF &p, const QPointF &sp, QWindow *w)
290 : pos(p), screenPos(sp), window(w) { }
291 QPointF pos;
292 QPointF screenPos;
293 QPointer<QWindow> window;
294 };
295 QHash<QWindow *, SynthesizedMouseData> synthesizedMousePoints;
296
297 static int mouseEventCaps(QMouseEvent *event);
298 static QVector2D mouseEventVelocity(QMouseEvent *event);
299 static void setMouseEventCapsAndVelocity(QMouseEvent *event, int caps, const QVector2D &velocity);
300
301 static Qt::MouseEventSource mouseEventSource(const QMouseEvent *event);
302 static void setMouseEventSource(QMouseEvent *event, Qt::MouseEventSource source);
303
304 static Qt::MouseEventFlags mouseEventFlags(const QMouseEvent *event);
305 static void setMouseEventFlags(QMouseEvent *event, Qt::MouseEventFlags flags);
306
307 static QInputDeviceManager *inputDeviceManager();
308
309 const QColorTrcLut *colorProfileForA8Text();
310 const QColorTrcLut *colorProfileForA32Text();
311
312 // hook reimplemented in QApplication to apply the QStyle function on the QIcon
313 virtual QPixmap applyQIconStyleHelper(QIcon::Mode, const QPixmap &basePixmap) const { return basePixmap; }
314
315 virtual void notifyWindowIconChanged();
316
317 static void applyWindowGeometrySpecificationTo(QWindow *window);
318
319 static void setApplicationState(Qt::ApplicationState state, bool forcePropagate = false);
320
321 static void resetCachedDevicePixelRatio();
322
323protected:
324 virtual void notifyThemeChanged();
325 virtual void sendApplicationPaletteChange(bool toAllWidgets = false, const char *className = nullptr);
326 bool tryCloseRemainingWindows(QWindowList processedWindows);
327#if QT_CONFIG(draganddrop)
328 virtual void notifyDragStarted(const QDrag *);
329#endif // QT_CONFIG(draganddrop)
330
331private:
332 friend class QDragManager;
333
334 static QGuiApplicationPrivate *self;
335 static QTouchDevice *m_fakeTouchDevice;
336 static int m_fakeMouseSourcePointId;
337 QSharedPointer<QColorTrcLut> m_a8ColorProfile;
338 QSharedPointer<QColorTrcLut> m_a32ColorProfile;
339
340 bool ownGlobalShareContext;
341
342 static QInputDeviceManager *m_inputDeviceManager;
343
344 // Cache the maximum device pixel ratio, to iterate through the screen list
345 // only the first time it's required, or when devices are added or removed.
346 static qreal m_maxDevicePixelRatio;
347};
348
349Q_GUI_EXPORT uint qHash(const QGuiApplicationPrivate::ActiveTouchPointsKey &k);
350
351Q_GUI_EXPORT bool operator==(const QGuiApplicationPrivate::ActiveTouchPointsKey &a,
352 const QGuiApplicationPrivate::ActiveTouchPointsKey &b);
353
354QT_END_NAMESPACE
355
356#endif // QGUIAPPLICATION_P_H
357