9#include <config-kidletime.h>
11#include "kabstractidletimepoller_p.h"
15#include <QGuiApplication>
17#include <QPluginLoader>
32 KIdleTimeHelper(
const KIdleTimeHelper &) =
delete;
33 KIdleTimeHelper &operator=(
const KIdleTimeHelper &) =
delete;
37Q_GLOBAL_STATIC(KIdleTimeHelper, s_globalKIdleTime)
41 if (!s_globalKIdleTime()->q) {
45 return s_globalKIdleTime()->q;
61 void unloadCurrentSystem();
62 void resumingFromIdle();
63 void timeoutReached(
int msec);
74 , d_ptr(new KIdleTimePrivate())
76 Q_ASSERT(!s_globalKIdleTime()->q);
77 s_globalKIdleTime()->q =
this;
84 connect(d->poller.data(), &KAbstractIdleTimePoller::resumingFromIdle,
this, [d]() {
85 d->resumingFromIdle();
87 connect(d->poller.data(), &KAbstractIdleTimePoller::timeoutReached,
this, [d](
int msec) {
88 d->timeoutReached(msec);
95 d->unloadCurrentSystem();
102 if (!d->catchResume && d->poller) {
103 d->catchResume =
true;
104 d->poller.data()->catchIdleEvent();
112 if (d->catchResume && d->poller) {
113 d->catchResume =
false;
114 d->poller.data()->stopCatchingIdleEvents();
121 if (Q_UNLIKELY(msec < 0)) {
122 qCWarning(KIDLETIME,
"KIdleTime::addIdleTimeout: invalid timeout: %d", msec);
125 if (Q_UNLIKELY(!d->poller)) {
129 d->poller.data()->addTimeout(msec);
132 d->associations[d->currentId] = msec;
141 const auto it = d->associations.constFind(identifier);
142 if (it == d->associations.cend() || !d->poller) {
146 const int msec = it.value();
148 d->associations.erase(it);
150 const bool isFound = std::any_of(d->associations.cbegin(), d->associations.cend(), [msec](
int i) {
155 d->poller.data()->removeTimeout(msec);
163 std::vector<int> removed;
165 for (
auto it = d->associations.cbegin(); it != d->associations.cend(); ++it) {
166 const int msec = it.value();
167 const bool alreadyIns = std::find(removed.cbegin(), removed.cend(), msec) != removed.cend();
168 if (!alreadyIns && d->poller) {
169 removed.push_back(msec);
170 d->poller.data()->removeTimeout(msec);
174 d->associations.clear();
182 for (
const QString &path : libPath) {
184 const QDir pluginDir(path + QStringLiteral(
"/kf6/kidletime"));
186 const QDir pluginDir(path + QStringLiteral(
"/kf6/org.kde.kidletime.platforms"));
188 if (!pluginDir.exists()) {
195 for (
const QString &entry : entries) {
196 ret << pluginDir.absoluteFilePath(entry);
206 return std::any_of(platforms.
begin(), platforms.
end(), [&platformName](
const QJsonValue &value) {
207 return QString::compare(platformName, value.toString(), Qt::CaseInsensitive) == 0;
211static KAbstractIdleTimePoller *loadPoller()
217 const QJsonObject metadata = staticPlugin.metaData();
221 if (checkPlatform(metadata, platformName)) {
222 auto *poller = qobject_cast<KAbstractIdleTimePoller *>(staticPlugin.instance());
224 if (poller->isAvailable()) {
225 qCDebug(KIDLETIME) <<
"Loaded system poller from a static plugin";
234 for (
const QString &candidate : lstPlugins) {
239 if (checkPlatform(loader.metaData(), platformName)) {
240 auto *poller = qobject_cast<KAbstractIdleTimePoller *>(loader.instance());
242 qCDebug(KIDLETIME) <<
"Trying plugin" << candidate;
243 if (poller->isAvailable()) {
244 qCDebug(KIDLETIME) <<
"Using" << candidate <<
"for platform" << platformName;
252 qCWarning(KIDLETIME) <<
"Could not find any system poller plugin";
256void KIdleTimePrivate::loadSystem()
259 unloadCurrentSystem();
263 poller = loadPoller();
265 if (poller && !poller->isAvailable()) {
269 poller.
data()->setUpPoller();
273void KIdleTimePrivate::unloadCurrentSystem()
276 poller.
data()->unloadPoller();
277 poller.
data()->deleteLater();
281void KIdleTimePrivate::resumingFromIdle()
286 Q_EMIT q->resumingFromIdle();
287 q->stopCatchingResumeEvent();
291void KIdleTimePrivate::timeoutReached(
int msec)
295 const auto listKeys = associations.
keys(msec);
297 for (
const auto key : listKeys) {
298 Q_EMIT q->timeoutReached(key, msec);
306 if (Q_LIKELY(d->poller)) {
307 d->poller.data()->simulateUserActivity();
314 if (Q_LIKELY(d->poller)) {
315 return d->poller.data()->forcePollRequest();
324 return d->associations;
327#include "moc_kidletime.cpp"
KIdleTime is a singleton reporting information on idle time.
void catchNextResumeEvent()
Catches the next resume from idle event.
int addIdleTimeout(int msec)
Adds a new timeout to catch.
int idleTime() const
Retrieves the idle time of the system, in milliseconds.
~KIdleTime() override
The destructor.
void simulateUserActivity()
Attempts to simulate user activity.
void stopCatchingResumeEvent()
Stops listening for resume event.
void removeIdleTimeout(int identifier)
Stops catching the idle timeout identified by the token identifier, if it was registered earlier with...
void removeAllIdleTimeouts()
Stops catching every set timeout (if any).
QHash< int, int > idleTimeouts() const
Returns the list of timeout identifiers associated with their duration, in milliseconds,...
QStringList libraryPaths()
QList< Key > keys() const const
QJsonValue value(QLatin1StringView key) const const
QJsonArray toArray() const const
QJsonObject toObject() const const
bool isLibrary(const QString &fileName)
void reserve(qsizetype size)
qsizetype size() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QList< QStaticPlugin > staticPlugins()
bool isNull() const const