1 | // Copyright (C) 2020 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 | |
4 | #ifndef QABSTRACTEVENTDISPATCHER_H |
5 | #define QABSTRACTEVENTDISPATCHER_H |
6 | |
7 | #include <QtCore/qobject.h> |
8 | #include <QtCore/qeventloop.h> |
9 | |
10 | QT_BEGIN_NAMESPACE |
11 | |
12 | class QAbstractNativeEventFilter; |
13 | class QAbstractEventDispatcherPrivate; |
14 | class QSocketNotifier; |
15 | |
16 | class Q_CORE_EXPORT QAbstractEventDispatcher : public QObject |
17 | { |
18 | Q_OBJECT |
19 | Q_DECLARE_PRIVATE(QAbstractEventDispatcher) |
20 | |
21 | public: |
22 | using Duration = std::chrono::nanoseconds; |
23 | struct TimerInfo |
24 | { |
25 | int timerId; |
26 | int interval; |
27 | Qt::TimerType timerType; |
28 | |
29 | inline TimerInfo(int id, int i, Qt::TimerType t) |
30 | : timerId(id), interval(i), timerType(t) { } |
31 | }; |
32 | struct TimerInfoV2 |
33 | { |
34 | Duration interval; |
35 | Qt::TimerId timerId; |
36 | Qt::TimerType timerType; |
37 | }; |
38 | |
39 | explicit QAbstractEventDispatcher(QObject *parent = nullptr); |
40 | ~QAbstractEventDispatcher(); |
41 | |
42 | static QAbstractEventDispatcher *instance(QThread *thread = nullptr); |
43 | |
44 | virtual bool processEvents(QEventLoop::ProcessEventsFlags flags) = 0; |
45 | |
46 | virtual void registerSocketNotifier(QSocketNotifier *notifier) = 0; |
47 | virtual void unregisterSocketNotifier(QSocketNotifier *notifier) = 0; |
48 | |
49 | Qt::TimerId registerTimer(Duration interval, Qt::TimerType timerType, QObject *object); |
50 | |
51 | #if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) |
52 | int registerTimer(qint64 interval, Qt::TimerType timerType, QObject *object); |
53 | |
54 | // old, integer-based API |
55 | virtual void registerTimer(int timerId, qint64 interval, Qt::TimerType timerType, QObject *object) = 0; |
56 | virtual bool unregisterTimer(int timerId) = 0; |
57 | virtual bool unregisterTimers(QObject *object) = 0; |
58 | virtual QList<TimerInfo> registeredTimers(QObject *object) const = 0; |
59 | virtual int remainingTime(int timerId) = 0; |
60 | |
61 | void registerTimer(Qt::TimerId timerId, Duration interval, Qt::TimerType timerType, QObject *object); |
62 | bool unregisterTimer(Qt::TimerId timerId); |
63 | QList<TimerInfoV2> timersForObject(QObject *object) const; |
64 | Duration remainingTime(Qt::TimerId timerId) const; |
65 | #else |
66 | virtual void registerTimer(Qt::TimerId timerId, Duration interval, Qt::TimerType timerType, QObject *object) = 0; |
67 | virtual bool unregisterTimer(Qt::TimerId timerId) = 0; |
68 | virtual bool unregisterTimers(QObject *object) = 0; |
69 | virtual QList<TimerInfoV2> timersForObject(QObject *object) const = 0; |
70 | virtual Duration remainingTime(Qt::TimerId timerId) const = 0; |
71 | #endif |
72 | |
73 | virtual void wakeUp() = 0; |
74 | virtual void interrupt() = 0; |
75 | |
76 | virtual void startingUp(); |
77 | virtual void closingDown(); |
78 | |
79 | void installNativeEventFilter(QAbstractNativeEventFilter *filterObj); |
80 | void removeNativeEventFilter(QAbstractNativeEventFilter *filterObj); |
81 | bool filterNativeEvent(const QByteArray &eventType, void *message, qintptr *result); |
82 | |
83 | Q_SIGNALS: |
84 | void aboutToBlock(); |
85 | void awake(); |
86 | |
87 | protected: |
88 | QAbstractEventDispatcher(QAbstractEventDispatcherPrivate &, |
89 | QObject *parent); |
90 | }; |
91 | |
92 | Q_DECLARE_TYPEINFO(QAbstractEventDispatcher::TimerInfo, Q_PRIMITIVE_TYPE); |
93 | Q_DECLARE_TYPEINFO(QAbstractEventDispatcher::TimerInfoV2, Q_PRIMITIVE_TYPE); |
94 | |
95 | #if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) |
96 | class Q_CORE_EXPORT QAbstractEventDispatcherV2 : public QAbstractEventDispatcher |
97 | { |
98 | Q_OBJECT |
99 | Q_DECLARE_PRIVATE(QAbstractEventDispatcher) // not V2 |
100 | |
101 | public: |
102 | explicit QAbstractEventDispatcherV2(QObject *parent = nullptr); |
103 | ~QAbstractEventDispatcherV2() override; |
104 | |
105 | // new virtuals |
106 | virtual void registerTimer(Qt::TimerId timerId, Duration interval, Qt::TimerType timerType, |
107 | QObject *object) = 0; |
108 | virtual bool unregisterTimer(Qt::TimerId timerId) = 0; |
109 | virtual QList<TimerInfoV2> timersForObject(QObject *object) const = 0; |
110 | virtual Duration remainingTime(Qt::TimerId timerId) const = 0; |
111 | virtual bool processEventsWithDeadline(QEventLoop::ProcessEventsFlags flags, QDeadlineTimer deadline); // reserved for 6.9 |
112 | |
113 | protected: |
114 | QAbstractEventDispatcherV2(QAbstractEventDispatcherPrivate &, QObject *parent); |
115 | |
116 | private: |
117 | // final overrides from V1 |
118 | void registerTimer(int timerId, qint64 interval, Qt::TimerType timerType, |
119 | QObject *object) final; |
120 | bool unregisterTimer(int timerId) final; |
121 | QList<TimerInfo> registeredTimers(QObject *object) const final; |
122 | int remainingTime(int timerId) final; |
123 | }; |
124 | #else |
125 | using QAbstractEventDispatcherV2 = QAbstractEventDispatcher; |
126 | #endif // Qt 7 |
127 | |
128 | QT_END_NAMESPACE |
129 | |
130 | #endif // QABSTRACTEVENTDISPATCHER_H |
131 | |