Port IdleDetector to QBasicTimer

QBasicTimer is lighter and it properly handles timeout values bigger
than INT32_MAX.

CCBUG: 482077
This commit is contained in:
Vlad Zahorodnii 2024-03-08 12:51:05 +02:00
parent 18e414443e
commit e2cbed7060
2 changed files with 20 additions and 10 deletions

View file

@ -14,13 +14,10 @@ namespace KWin
IdleDetector::IdleDetector(std::chrono::milliseconds timeout, QObject *parent)
: QObject(parent)
, m_timer(new QTimer(this))
, m_timeout(timeout)
{
Q_ASSERT(timeout >= 0ms);
m_timer->setSingleShot(true);
m_timer->setInterval(timeout);
connect(m_timer, &QTimer::timeout, this, &IdleDetector::markAsIdle);
m_timer->start();
m_timer.start(timeout, this);
input()->addIdleDetector(this);
}
@ -32,6 +29,14 @@ IdleDetector::~IdleDetector()
}
}
void IdleDetector::timerEvent(QTimerEvent *event)
{
if (event->timerId() == m_timer.timerId()) {
m_timer.stop();
markAsIdle();
}
}
bool IdleDetector::isInhibited() const
{
return m_isInhibited;
@ -44,16 +49,16 @@ void IdleDetector::setInhibited(bool inhibited)
}
m_isInhibited = inhibited;
if (inhibited) {
m_timer->stop();
m_timer.stop();
} else {
m_timer->start();
m_timer.start(m_timeout, this);
}
}
void IdleDetector::activity()
{
if (!m_isInhibited) {
m_timer->start();
m_timer.start(m_timeout, this);
markAsResumed();
}
}

View file

@ -8,7 +8,8 @@
#include <kwin_export.h>
#include <QTimer>
#include <QBasicTimer>
#include <QObject>
namespace KWin
{
@ -30,11 +31,15 @@ Q_SIGNALS:
void idle();
void resumed();
protected:
void timerEvent(QTimerEvent *event) override;
private:
void markAsIdle();
void markAsResumed();
QTimer *m_timer;
QBasicTimer m_timer;
std::chrono::milliseconds m_timeout;
bool m_isIdle = false;
bool m_isInhibited = false;
};