| 1 |
// SPDX-License-Identifier: GPL-3.0-or-later |
| 2 |
|
| 3 |
#pragma once |
| 4 |
|
| 5 |
|
| 6 |
#include "cfref.h" |
| 7 |
|
| 8 |
#include <QObject> |
| 9 |
|
| 10 |
#include <CoreFoundation/CoreFoundation.h> |
| 11 |
/** |
| 12 |
* Class similar to a QTimer but allows setting a tolerance, which |
| 13 |
* makes timers more battery-friendly on OSX. |
| 14 |
*/ |
| 15 |
class MacTimer final : public QObject |
| 16 |
{ |
| 17 |
Q_OBJECT |
| 18 |
|
| 19 |
public: |
| 20 |
explicit MacTimer(QObject *parent = 0); |
| 21 |
virtual ~MacTimer(); |
| 22 |
|
| 23 |
void setInterval(int msec); |
| 24 |
int interval() const { return m_interval; } |
| 25 |
inline void setSingleShot(bool singleShot); |
| 26 |
|
| 27 |
/** |
| 28 |
* Set the tolerance for the timer. |
| 29 |
* See CFRunLoopTimerSetTolerance. |
| 30 |
*/ |
| 31 |
void setTolerance(int msec); |
| 32 |
int tolerance() const { return m_tolerance; } |
| 33 |
|
| 34 |
public Q_SLOTS: |
| 35 |
void start(); |
| 36 |
void stop(); |
| 37 |
inline bool isSingleShot() const { return m_singleShot; } |
| 38 |
|
| 39 |
Q_SIGNALS: |
| 40 |
void timeout(); |
| 41 |
|
| 42 |
private: |
| 43 |
Q_DISABLE_COPY(MacTimer) |
| 44 |
|
| 45 |
void restart(); |
| 46 |
|
| 47 |
int m_interval; |
| 48 |
int m_tolerance; |
| 49 |
bool m_singleShot; |
| 50 |
|
| 51 |
CFRef<CFRunLoopTimerRef> m_cfTimer; |
| 52 |
}; |