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 | |
4 | #ifndef QTIMERINFO_UNIX_P_H |
5 | #define QTIMERINFO_UNIX_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 "qplatformdefs.h" // _POSIX_MONOTONIC_CLOCK-0 |
19 | |
20 | #include <QtCore/private/qglobal_p.h> |
21 | |
22 | // #define QTIMERINFO_DEBUG |
23 | |
24 | #include "qabstracteventdispatcher.h" |
25 | |
26 | #include <sys/time.h> // struct timeval |
27 | |
28 | QT_BEGIN_NAMESPACE |
29 | |
30 | // internal timer info |
31 | struct QTimerInfo { |
32 | int id; // - timer identifier |
33 | Qt::TimerType timerType; // - timer type |
34 | std::chrono::milliseconds interval; // - timer interval |
35 | timespec timeout; // - when to actually fire |
36 | QObject *obj; // - object to receive event |
37 | QTimerInfo **activateRef; // - ref from activateTimers |
38 | |
39 | #ifdef QTIMERINFO_DEBUG |
40 | timeval expected; // when timer is expected to fire |
41 | float cumulativeError; |
42 | uint count; |
43 | #endif |
44 | }; |
45 | |
46 | class Q_CORE_EXPORT QTimerInfoList : public QList<QTimerInfo*> |
47 | { |
48 | // state variables used by activateTimers() |
49 | QTimerInfo *firstTimerInfo; |
50 | |
51 | public: |
52 | QTimerInfoList(); |
53 | |
54 | timespec currentTime; |
55 | timespec updateCurrentTime(); |
56 | |
57 | bool timerWait(timespec &); |
58 | void timerInsert(QTimerInfo *); |
59 | |
60 | qint64 timerRemainingTime(int timerId); |
61 | std::chrono::milliseconds remainingDuration(int timerId); |
62 | |
63 | void registerTimer(int timerId, qint64 interval, Qt::TimerType timerType, QObject *object); |
64 | void registerTimer(int timerId, std::chrono::milliseconds interval, Qt::TimerType timerType, |
65 | QObject *object); |
66 | bool unregisterTimer(int timerId); |
67 | bool unregisterTimers(QObject *object); |
68 | QList<QAbstractEventDispatcher::TimerInfo> registeredTimers(QObject *object) const; |
69 | |
70 | int activateTimers(); |
71 | |
72 | QList::const_iterator findTimerById(int timerId) const |
73 | { |
74 | auto matchesId = [timerId](const QTimerInfo *t) { return t->id == timerId; }; |
75 | return std::find_if(first: cbegin(), last: cend(), pred: matchesId); |
76 | } |
77 | }; |
78 | |
79 | QT_END_NAMESPACE |
80 | |
81 | #endif // QTIMERINFO_UNIX_P_H |
82 | |