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