File indexing completed on 2024-06-23 05:45:43

0001 /*
0002  * Copyright 2020 Han Young <hanyoung@protonmail.com>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #pragma once
0008 
0009 #include "alarmwaitworker.h"
0010 
0011 #include <QDBusInterface>
0012 #include <QObject>
0013 #include <QStringList>
0014 
0015 const QString POWERDEVIL_SERVICE_NAME = QStringLiteral("org.kde.Solid.PowerManagement");
0016 
0017 class QTimer;
0018 
0019 class Utilities : public QObject
0020 {
0021     Q_OBJECT
0022     Q_CLASSINFO("D-Bus Interface", "org.kde.PowerManagement")
0023 
0024 public:
0025     static void disablePowerDevil(bool disable);
0026     static Utilities &instance();
0027 
0028     bool hasPowerDevil();
0029 
0030     int scheduleWakeup(quint64 timestamp);
0031     void clearWakeup(int cookie);
0032     void exitAfterTimeout();
0033     void incfActiveCount();
0034     void decfActiveCount();
0035 
0036     static void wakeupNow();
0037 
0038     static void pauseMprisSources();
0039     static void resumeMprisSources();
0040 
0041 Q_SIGNALS:
0042     void wakeup(int cookie);
0043     void needsReschedule();
0044 
0045 public Q_SLOTS:
0046     Q_SCRIPTABLE void wakeupCallback(int cookie);
0047     Q_SCRIPTABLE void keepAlive();
0048     Q_SCRIPTABLE void canExit();
0049 
0050 private:
0051     explicit Utilities(QObject *parent = nullptr);
0052 
0053     void schedule(); // For AlarmWaitWorker use
0054     void initWorker();
0055     bool hasWakeup();
0056     QDBusInterface *m_interface = nullptr;
0057 
0058     bool m_hasPowerDevil;
0059 
0060     QList<int> m_powerDevilCookies; // token for PowerDevil: https://invent.kde.org/plasma/powerdevil/-/merge_requests/13
0061     QList<std::pair<int, quint64>> m_waitWorkerCookies; // <cookie, timestamp>. For AlarmWaitWorker use
0062 
0063     int m_cookie = 1; // For AlarmWaitWorker use
0064     int m_currentCookie; // For AlarmWaitWorker use
0065 
0066     std::atomic<int> m_activeTimerCount{0};
0067 
0068     QThread *m_timerThread = nullptr;
0069     AlarmWaitWorker *m_worker = nullptr;
0070     QTimer *m_timer = nullptr;
0071 
0072     // which mpris media sources were paused when the alarm/timer started ringing
0073     static QStringList m_pausedSources;
0074 };