From dd52dee28ef4a97faee74b27bd5254391ea4e9bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ole-Andr=C3=A9=20Rodlie?= Date: Thu, 8 Feb 2024 22:14:31 +0100 Subject: [PATCH] More cleanup + more client opts --- CMakeLists.txt | 2 +- docs/README.md | 11 ++- docs/powerkit.1 | 15 ++++ src/powerkit.cpp | 38 +++++++- src/powerkit_app.cpp | 149 +++++++++++-------------------- src/powerkit_app.h | 3 +- src/powerkit_client.cpp | 44 ++++----- src/powerkit_client.h | 7 +- src/powerkit_powermanagement.cpp | 2 + src/powerkit_powermanagement.h | 71 ++++++++------- 10 files changed, 180 insertions(+), 162 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d8da772..7c5b6a0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -55,7 +55,7 @@ qt_add_dbus_adaptor( ADAPTERS share/org.freedesktop.PowerManagement.Inhibit.xml ${PROJECT_NAME}_powermanagement.h - PowerManagement + PowerKit::PowerManagement InhibitAdaptor ) diff --git a/docs/README.md b/docs/README.md index da08856..197a90c 100644 --- a/docs/README.md +++ b/docs/README.md @@ -4,7 +4,7 @@ # SYNOPSIS -powerkit *`[--config]`* *`[--set-brightness-up]`* *`[--set-brightness-down]`* +powerkit *`[--config]`* *`[--set-brightness-up]`* *`[--set-brightness-down]`* *`[--sleep]`* *`[--hibernate]`* *`[--lock]`* # DESCRIPTION @@ -175,6 +175,15 @@ cpack -G RPM *`--set-brightness-down`* : Set default display brightness down. +*`--sleep`* +: Suspend computer now. Can be combined with *`--hibernate`* for suspend then hibernate after X amount of time. + +*`--hibernate`* +: Hibernate computer now. Can be combined with *`--sleep`* for suspend then hibernate after X amount of time. + +*`--lock`* +: Lock screen. + # FILES *``~/.config/powerkit/powerkit.conf``* diff --git a/docs/powerkit.1 b/docs/powerkit.1 index 5534aa8..2f3d4b2 100644 --- a/docs/powerkit.1 +++ b/docs/powerkit.1 @@ -7,6 +7,8 @@ powerkit \f[I]\f[CI][--config]\f[I]\f[R] \f[I]\f[CI][--set-brightness-up]\f[I]\f[R] \f[I]\f[CI][--set-brightness-down]\f[I]\f[R] +\f[I]\f[CI][--sleep]\f[I]\f[R] \f[I]\f[CI][--hibernate]\f[I]\f[R] +\f[I]\f[CI][--lock]\f[I]\f[R] .SH DESCRIPTION Desktop independent power manager for use with alternative X11 desktop environments and window managers on Linux. @@ -221,6 +223,19 @@ Set default display brightness up. .TP \f[I]\f[CI]--set-brightness-down\f[I]\f[R] Set default display brightness down. +.TP +\f[I]\f[CI]--sleep\f[I]\f[R] +Suspend computer now. +Can be combined with \f[I]\f[CI]--hibernate\f[I]\f[R] for suspend then +hibernate after X amount of time. +.TP +\f[I]\f[CI]--hibernate\f[I]\f[R] +Hibernate computer now. +Can be combined with \f[I]\f[CI]--sleep\f[I]\f[R] for suspend then +hibernate after X amount of time. +.TP +\f[I]\f[CI]--lock\f[I]\f[R] +Lock screen. .SH FILES .TP \f[I]\f[CI]\[ti]/.config/powerkit/powerkit.conf\f[I]\f[R] diff --git a/src/powerkit.cpp b/src/powerkit.cpp index 264703f..7de32ae 100644 --- a/src/powerkit.cpp +++ b/src/powerkit.cpp @@ -12,9 +12,14 @@ #include "powerkit_dialog.h" #include "powerkit_common.h" #include "powerkit_backlight.h" +#include "powerkit_client.h" +#define CMD_OPT_CONFIG "--config" #define CMD_OPT_BRIGHTNESS_UP "--set-brightness-up" #define CMD_OPT_BRIGHTNESS_DOWN "--set-brightness-down" +#define CMD_OPT_SLEEP "--sleep" +#define CMD_OPT_HIBERNATE "--hibernate" +#define CMD_OPT_LOCK "--lock" int main(int argc, char *argv[]) { @@ -23,8 +28,15 @@ int main(int argc, char *argv[]) QCoreApplication::setOrganizationDomain("org"); QCoreApplication::setApplicationVersion(QString::fromUtf8(POWERKIT_VERSION)); - QStringList args = QApplication::arguments(); - if (args.contains("--config")) { + const auto args = QApplication::arguments(); + bool openConfig = args.contains(CMD_OPT_CONFIG); + bool setBrightness = (args.contains(CMD_OPT_BRIGHTNESS_UP) || + args.contains(CMD_OPT_BRIGHTNESS_DOWN)); + bool setSleep = args.contains(CMD_OPT_SLEEP); + bool setHibernate = args.contains(CMD_OPT_HIBERNATE); + bool setLock = args.contains(CMD_OPT_LOCK); + + if (openConfig) { if (!QDBusConnection::sessionBus().registerService(POWERKIT_CONFIG)) { qWarning() << QObject::tr("A powerkit config instance is already running"); return 1; @@ -32,12 +44,30 @@ int main(int argc, char *argv[]) PowerKit::Dialog dialog; dialog.show(); return a.exec(); - } else if (args.contains(CMD_OPT_BRIGHTNESS_UP) || - args.contains(CMD_OPT_BRIGHTNESS_DOWN)) { + } else if (setBrightness) { int val = PowerKit::Backlight::getCurrentBrightness(); if (args.contains(CMD_OPT_BRIGHTNESS_UP)) { val += BACKLIGHT_MOVE_VALUE; } else if (args.contains(CMD_OPT_BRIGHTNESS_DOWN)) { val -= BACKLIGHT_MOVE_VALUE; } return PowerKit::Backlight::setBrightness(val); + } else if (setSleep || setHibernate || setLock) { + QDBusInterface manager(POWERKIT_SERVICE, + POWERKIT_PATH, + POWERKIT_MANAGER, + QDBusConnection::sessionBus()); + if (!manager.isValid()) { + qWarning() << QObject::tr("Unable to connect to the powerkit service"); + return 1; + } + if (setSleep && setHibernate) { + return PowerKit::Client::suspendThenHibernate(&manager); + } else if (setSleep) { + return PowerKit::Client::suspend(&manager); + } else if (setHibernate) { + return PowerKit::Client::hibernate(&manager); + } else if (setLock) { + return PowerKit::Client::lockScreen(&manager); + } + return 1; } QDBusInterface session(POWERKIT_SERVICE, diff --git a/src/powerkit_app.cpp b/src/powerkit_app.cpp index abf0bfc..3bd9e6e 100644 --- a/src/powerkit_app.cpp +++ b/src/powerkit_app.cpp @@ -238,56 +238,24 @@ void App::checkDevices() if (!showTray && tray->isVisible()) { tray->hide(); } - // get battery left and add tooltip double batteryLeft = man->BatteryLeft(); qDebug() << "battery at" << batteryLeft; - if (batteryLeft > 0 && man->HasBattery()) { - tray->setToolTip(QString("%1 %2%").arg(tr("Battery at")).arg(batteryLeft)); - if (man->TimeToEmpty()>0 && man->OnBattery()) { - tray->setToolTip(tray->toolTip() - .append(QString(", %1 %2") - .arg(QDateTime::fromTime_t((uint)man->TimeToEmpty()) - .toUTC().toString("hh:mm"))) - .arg(tr("left"))); - } - if (batteryLeft > 99) { tray->setToolTip(tr("Charged")); } - if (!man->OnBattery() && - man->BatteryLeft() <= 99) { - if (man->TimeToFull()>0) { - tray->setToolTip(tray->toolTip() - .append(QString(", %1 %2") - .arg(QDateTime::fromTime_t((uint)man->TimeToFull()) - .toUTC().toString("hh:mm"))) - .arg(tr("left"))); - } - tray->setToolTip(tray->toolTip().append(QString(" (%1)").arg(tr("Charging")))); - } - } else { tray->setToolTip(tr("On AC")); } - // draw battery systray drawBattery(batteryLeft); + updateToolTip(); - // low battery? handleLow(batteryLeft); - - // very low battery? handleVeryLow(batteryLeft); - - // critical battery? handleCritical(batteryLeft); - - // Register service if not already registered - if (!hasService) { registerService(); } } -// what to do when user close lid void App::handleClosedLid() { qDebug() << "lid closed"; lidWasClosed = true; int type = lidNone; - if (man->OnBattery()) { // on battery + if (man->OnBattery()) { type = lidActionBattery; } else { // on ac type = lidActionAC; @@ -324,7 +292,6 @@ void App::handleClosedLid() } } -// what to do when user open lid void App::handleOpenedLid() { qDebug() << "lid is now open"; @@ -334,7 +301,6 @@ void App::handleOpenedLid() } } -// do something when switched to battery power void App::handleOnBattery() { if (notifyOnBattery) { @@ -343,24 +309,18 @@ void App::handleOnBattery() } // brightness - if (/*hasBacklight &&*/ - backlightOnBattery && - backlightBatteryValue>0) { + if (backlightOnBattery && backlightBatteryValue > 0) { qDebug() << "set brightness on battery"; if (backlightBatteryDisableIfLower && - backlightBatteryValue>Backlight::getCurrentBrightness(backlightDevice)) { + backlightBatteryValue > Backlight::getCurrentBrightness(backlightDevice)) { qDebug() << "brightness is lower than battery value, ignore"; return; } - /*if (hasBacklight) { - Common::adjustBacklight(backlightDevice, backlightBatteryValue); - } else {*/ - Backlight::setBrightness(backlightDevice, backlightBatteryValue); - //} + Backlight::setBrightness(backlightDevice, + backlightBatteryValue); } } -// do something when switched to ac power void App::handleOnAC() { if (notifyOnAC) { @@ -372,24 +332,18 @@ void App::handleOnAC() wasVeryLowBattery = false; // brightness - if (/*hasBacklight &&*/ - backlightOnAC && - backlightACValue>0) { + if (backlightOnAC && backlightACValue > 0) { qDebug() << "set brightness on ac"; if (backlightACDisableIfHigher && - backlightACValueUpdate(); } -// register session services void App::registerService() { if (hasService) { return; } if (!QDBusConnection::sessionBus().isConnected()) { - qWarning("Cannot connect to D-Bus."); + qWarning() << tr("Cannot connect to D-Bus."); return; } hasService = true; + // register org.freedesktop.PowerManagement bool hasDesktopPM = true; if (!QDBusConnection::sessionBus().registerService(PM_SERVICE)) { qWarning() << QDBusConnection::sessionBus().lastError().message(); hasDesktopPM = false; - } - if (!QDBusConnection::sessionBus().registerObject(PM_PATH, - pm, - QDBusConnection::ExportAllSlots)) { - qWarning() << QDBusConnection::sessionBus().lastError().message(); - hasDesktopPM = false; - } - if (!QDBusConnection::sessionBus().registerObject(PM_FULL_PATH, - pm, - QDBusConnection::ExportAllSlots)) { - qWarning() << QDBusConnection::sessionBus().lastError().message(); - hasDesktopPM = false; - } - - new InhibitAdaptor(pm); - if (!QDBusConnection::sessionBus().registerObject(PM_FULL_PATH_INHIBIT, pm)) { - qWarning() << QDBusConnection::sessionBus().lastError().message(); - hasDesktopPM = false; - } - if (!QDBusConnection::sessionBus().registerService(PM_SERVICE_INHIBIT)) { - qWarning() << QDBusConnection::sessionBus().lastError().message(); - hasDesktopPM = false; + } else { + if (!QDBusConnection::sessionBus().registerService(PM_SERVICE_INHIBIT)) { + qWarning() << QDBusConnection::sessionBus().lastError().message(); + hasDesktopPM = false; + } else { + new InhibitAdaptor(pm); + if (!QDBusConnection::sessionBus().registerObject(PM_FULL_PATH_INHIBIT, pm)) { + qWarning() << QDBusConnection::sessionBus().lastError().message(); + hasDesktopPM = false; + } + } } qWarning() << "Enabled org.freedesktop.PowerManagement" << hasDesktopPM; @@ -582,7 +525,7 @@ void App::registerService() if (!hasDesktopPK || !hasDesktopPM || !hasScreenSaver) { hasService = false; } - if (!hasService) { handleWarning(tr("Failed to setup and/or connect required services!")); } + if (!hasService) { handleError(tr("Failed to setup and/or connect required services!")); } } // dbus session inhibit status handler @@ -623,7 +566,6 @@ void App::handleVeryLow(double left) } } -// handle critical battery void App::handleCritical(double left) { if (left<=0 || @@ -644,7 +586,6 @@ void App::handleCritical(double left) } } -// draw battery tray icon void App::drawBattery(double left) { if (!showTray && @@ -684,6 +625,34 @@ void App::drawBattery(double left) colorFg)); } +void App::updateToolTip() +{ + if (!tray->isSystemTrayAvailable() || !tray->isVisible()) { return; } + double batteryLeft = man->BatteryLeft(); + if (batteryLeft > 0 && man->HasBattery()) { + tray->setToolTip(QString("%1 %2%").arg(tr("Battery at")).arg(batteryLeft)); + if (man->TimeToEmpty()>0 && man->OnBattery()) { + tray->setToolTip(tray->toolTip() + .append(QString(", %1 %2") + .arg(QDateTime::fromTime_t((uint)man->TimeToEmpty()) + .toUTC().toString("hh:mm"))) + .arg(tr("left"))); + } + if (batteryLeft > 99) { tray->setToolTip(tr("Charged")); } + if (!man->OnBattery() && + man->BatteryLeft() <= 99) { + if (man->TimeToFull()>0) { + tray->setToolTip(tray->toolTip() + .append(QString(", %1 %2") + .arg(QDateTime::fromTime_t((uint)man->TimeToFull()) + .toUTC().toString("hh:mm"))) + .arg(tr("left"))); + } + tray->setToolTip(tray->toolTip().append(QString(" (%1)").arg(tr("Charging")))); + } + } else { tray->setToolTip(tr("On AC")); } +} + // timeout, check if idle // timeouts and xss must be >= user value and service has to be empty before suspend void App::timeout() @@ -739,13 +708,11 @@ void App::timeout() } } -// reset the idle timer void App::resetTimer() { timeouts = 0; } -// set "internal" monitor void App::setInternalMonitor() { internalMonitor = ss->GetInternalDisplay(); @@ -753,7 +720,6 @@ void App::setInternalMonitor() qDebug() << ss->GetDisplays(); } -// is "internal" monitor connected? bool App::internalMonitorIsConnected() { QMapIterator i(ss->GetDisplays()); @@ -767,7 +733,6 @@ bool App::internalMonitorIsConnected() return false; } -// is "external" monitor(s) connected? bool App::externalMonitorIsConnected() { QMapIterator i(ss->GetDisplays()); @@ -782,7 +747,6 @@ bool App::externalMonitorIsConnected() return false; } -// handle new inhibits void App::handleNewInhibitScreenSaver(const QString &application, const QString &reason, quint32 cookie) @@ -821,7 +785,6 @@ void App::handleDelInhibitPowerManagement(quint32 cookie) checkDevices(); } -// show notifications void App::showMessage(const QString &title, const QString &msg, bool critical) @@ -842,14 +805,12 @@ void App::showMessage(const QString &title, } } -// reload settings if conf changed void App::handleConfChanged(const QString &file) { Q_UNUSED(file) loadSettings(); } -// disable hibernate if enabled void App::disableHibernate() { if (criticalAction == criticalHibernate) { @@ -884,7 +845,6 @@ void App::disableHibernate() } } -// disable suspend if enabled void App::disableSuspend() { if (lidActionBattery == lidSleep) { @@ -913,7 +873,6 @@ void App::disableSuspend() } } -// prepare for suspend void App::handlePrepareForSuspend() { /*qDebug() << "prepare for suspend"; @@ -922,7 +881,6 @@ void App::handlePrepareForSuspend() qDebug() << "do nothing"; } -// prepare for resume void App::handlePrepareForResume() { qDebug() << "prepare for resume ..."; @@ -957,11 +915,9 @@ void App::handleTrayWheel(TrayIcon::WheelAction action) } } -// check devices if changed void App::handleDeviceChanged(const QString &path) { Q_UNUSED(path) - qDebug() << "DEVICE CHANGED" << path; checkDevices(); } @@ -984,7 +940,6 @@ void App::handleWarning(const QString &message) showMessage(tr("Warning"), message, true); } -// catch wheel events bool TrayIcon::event(QEvent *e) { if (e->type() == QEvent::Wheel) { diff --git a/src/powerkit_app.h b/src/powerkit_app.h index f428453..9df97d8 100644 --- a/src/powerkit_app.h +++ b/src/powerkit_app.h @@ -53,7 +53,7 @@ namespace PowerKit private: TrayIcon *tray; PowerKit::Manager *man; - PowerManagement *pm; + PowerKit::PowerManagement *pm; PowerKit::ScreenSaver *ss; bool wasLowBattery; bool wasVeryLowBattery; @@ -106,6 +106,7 @@ namespace PowerKit void handleVeryLow(double left); void handleCritical(double left); void drawBattery(double left); + void updateToolTip(); void timeout(); void resetTimer(); void setInternalMonitor(); diff --git a/src/powerkit_client.cpp b/src/powerkit_client.cpp index 18d63c2..98646dd 100644 --- a/src/powerkit_client.cpp +++ b/src/powerkit_client.cpp @@ -131,34 +131,36 @@ bool Client::lidIsPresent(QDBusInterface *iface) return ok; } -void Client::lockScreen(QDBusInterface *iface) +bool Client::lockScreen(QDBusInterface *iface) { - if (!iface) { return; } - qDebug() << "lock screen"; - if (!iface->isValid()) { return; } - QDBusMessage reply = iface->call("LockScreen"); - bool ok = reply.errorMessage().isEmpty(); - qDebug() << "locked screen?" << ok; + if (!iface) { return false; } + if (!iface->isValid()) { return false; } + const auto reply = iface->call("LockScreen"); + return reply.errorMessage().isEmpty(); } -void Client::hibernate(QDBusInterface *iface) +bool Client::hibernate(QDBusInterface *iface) { - if (!iface) { return; } - qDebug() << "hibernate"; - if (!iface->isValid()) { return; } - QDBusMessage reply = iface->call("Hibernate"); - bool ok = reply.errorMessage().isEmpty(); - qDebug() << "reply" << ok; + if (!iface) { return false; } + if (!iface->isValid()) { return false; } + const auto reply = iface->call("Hibernate"); + return reply.errorMessage().isEmpty(); } -void Client::suspend(QDBusInterface *iface) +bool Client::suspend(QDBusInterface *iface) { - if (!iface) { return; } - qDebug() << "suspend"; - if (!iface->isValid()) { return; } - QDBusMessage reply = iface->call("Suspend"); - bool ok = reply.errorMessage().isEmpty(); - qDebug() << "reply" << ok; + if (!iface) { return false; } + if (!iface->isValid()) { return false; } + const auto reply = iface->call("Suspend"); + return reply.errorMessage().isEmpty(); +} + +bool Client::suspendThenHibernate(QDBusInterface *iface) +{ + if (!iface) { return false; } + if (!iface->isValid()) { return false; } + const auto reply = iface->call("SuspendThenHibernate"); + return reply.errorMessage().isEmpty(); } bool Client::restart(QDBusInterface *iface) diff --git a/src/powerkit_client.h b/src/powerkit_client.h index 4d3e202..803a1c8 100644 --- a/src/powerkit_client.h +++ b/src/powerkit_client.h @@ -26,9 +26,10 @@ namespace PowerKit static bool canRestart(QDBusInterface *iface); static bool canPowerOff(QDBusInterface *iface); static bool lidIsPresent(QDBusInterface *iface); - static void lockScreen(QDBusInterface *iface); - static void hibernate(QDBusInterface *iface); - static void suspend(QDBusInterface *iface); + static bool lockScreen(QDBusInterface *iface); + static bool hibernate(QDBusInterface *iface); + static bool suspend(QDBusInterface *iface); + static bool suspendThenHibernate(QDBusInterface *iface); static bool restart(QDBusInterface *iface); static bool poweroff(QDBusInterface *iface); }; diff --git a/src/powerkit_powermanagement.cpp b/src/powerkit_powermanagement.cpp index eb1c77a..5610aaa 100644 --- a/src/powerkit_powermanagement.cpp +++ b/src/powerkit_powermanagement.cpp @@ -16,6 +16,8 @@ #include "powerkit_common.h" +using namespace PowerKit; + PowerManagement::PowerManagement(QObject *parent) : QObject(parent) { timer.setInterval(PM_TIMEOUT); diff --git a/src/powerkit_powermanagement.h b/src/powerkit_powermanagement.h index b054498..8865427 100644 --- a/src/powerkit_powermanagement.h +++ b/src/powerkit_powermanagement.h @@ -6,8 +6,8 @@ # See the LICENSE file for full details */ -#ifndef POWERMANAGEMENT_H -#define POWERMANAGEMENT_H +#ifndef POWERKIT_POWERMANAGEMENT_H +#define POWERKIT_POWERMANAGEMENT_H #include #include @@ -15,36 +15,39 @@ #include #include -class PowerManagement : public QObject +namespace PowerKit { - Q_OBJECT - -public: - explicit PowerManagement(QObject *parent = NULL); - -private: - QTimer timer; - QMap clients; - -signals: - void HasInhibitChanged(bool has_inhibit_changed); - void newInhibit(const QString &application, - const QString &reason, - quint32 cookie); - void removedInhibit(quint32 cookie); - -private slots: - quint32 genCookie(); - void checkForExpiredClients(); - bool canInhibit(); - void timeOut(); - -public slots: - void SimulateUserActivity(); - quint32 Inhibit(const QString &application, - const QString &reason); - void UnInhibit(quint32 cookie); - bool HasInhibit(); -}; - -#endif // POWERMANAGEMENT_H + class PowerManagement : public QObject + { + Q_OBJECT + + public: + explicit PowerManagement(QObject *parent = NULL); + + private: + QTimer timer; + QMap clients; + + signals: + void HasInhibitChanged(bool has_inhibit_changed); + void newInhibit(const QString &application, + const QString &reason, + quint32 cookie); + void removedInhibit(quint32 cookie); + + private slots: + quint32 genCookie(); + void checkForExpiredClients(); + bool canInhibit(); + void timeOut(); + + public slots: + void SimulateUserActivity(); + quint32 Inhibit(const QString &application, + const QString &reason); + void UnInhibit(quint32 cookie); + bool HasInhibit(); + }; +} + +#endif // POWERKIT_POWERMANAGEMENT_H