File indexing completed on 2024-11-10 04:57:45

0001 /*
0002     SPDX-FileCopyrightText: 2021 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "idledetector.h"
0008 #include "input.h"
0009 
0010 namespace KWin
0011 {
0012 
0013 IdleDetector::IdleDetector(std::chrono::milliseconds timeout, QObject *parent)
0014     : QObject(parent)
0015     , m_timer(new QTimer(this))
0016 {
0017     m_timer->setSingleShot(true);
0018     m_timer->setInterval(timeout);
0019     connect(m_timer, &QTimer::timeout, this, &IdleDetector::markAsIdle);
0020     m_timer->start();
0021 
0022     input()->addIdleDetector(this);
0023 }
0024 
0025 IdleDetector::~IdleDetector()
0026 {
0027     if (input()) {
0028         input()->removeIdleDetector(this);
0029     }
0030 }
0031 
0032 bool IdleDetector::isInhibited() const
0033 {
0034     return m_isInhibited;
0035 }
0036 
0037 void IdleDetector::setInhibited(bool inhibited)
0038 {
0039     if (m_isInhibited == inhibited) {
0040         return;
0041     }
0042     m_isInhibited = inhibited;
0043     if (inhibited) {
0044         m_timer->stop();
0045     } else {
0046         m_timer->start();
0047     }
0048 }
0049 
0050 void IdleDetector::activity()
0051 {
0052     if (!m_isInhibited) {
0053         m_timer->start();
0054         markAsResumed();
0055     }
0056 }
0057 
0058 void IdleDetector::markAsIdle()
0059 {
0060     if (!m_isIdle) {
0061         m_isIdle = true;
0062         Q_EMIT idle();
0063     }
0064 }
0065 
0066 void IdleDetector::markAsResumed()
0067 {
0068     if (m_isIdle) {
0069         m_isIdle = false;
0070         Q_EMIT resumed();
0071     }
0072 }
0073 
0074 } // namespace KWin
0075 
0076 #include "moc_idledetector.cpp"