Skip to content

Commit

Permalink
More cleanup + more client opts
Browse files Browse the repository at this point in the history
  • Loading branch information
rodlie committed Feb 8, 2024
1 parent 95fc98f commit dd52dee
Show file tree
Hide file tree
Showing 10 changed files with 180 additions and 162 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ qt_add_dbus_adaptor(
ADAPTERS
share/org.freedesktop.PowerManagement.Inhibit.xml
${PROJECT_NAME}_powermanagement.h
PowerManagement
PowerKit::PowerManagement
InhibitAdaptor
)

Expand Down
11 changes: 10 additions & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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``*
Expand Down
15 changes: 15 additions & 0 deletions docs/powerkit.1
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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]
Expand Down
38 changes: 34 additions & 4 deletions src/powerkit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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[])
{
Expand All @@ -23,21 +28,46 @@ 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;
}
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,
Expand Down
Loading

0 comments on commit dd52dee

Please sign in to comment.