Skip to content

Commit

Permalink
Fix lid actions
Browse files Browse the repository at this point in the history
Should fix all lid issues, will do some more testing.

Fix #44 #34
  • Loading branch information
rodlie committed Feb 6, 2024
1 parent 2a48167 commit a466725
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 18 deletions.
1 change: 1 addition & 0 deletions src/powerkit_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ namespace PowerKit
#define DBUS_PROPERTIES "org.freedesktop.DBus.Properties"
#define DBUS_DEVICE "Device"
#define DBUS_CHANGED "Changed"
#define DBUS_PROPERTIES_CHANGED "PropertiesChanged"

// config keys
#define CONF_DIALOG_GEOMETRY "dialog_geometry"
Expand Down
83 changes: 65 additions & 18 deletions src/powerkit_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,8 @@ void Manager::setup()
DBUS_DEVICE_REMOVED,
this,
SLOT(deviceRemoved(QString)));
system.connect(UPOWER_SERVICE,
// not used anymore?
/*system.connect(UPOWER_SERVICE,
UPOWER_PATH,
UPOWER_SERVICE,
DBUS_CHANGED,
Expand All @@ -276,7 +277,13 @@ void Manager::setup()
UPOWER_SERVICE,
DBUS_DEVICE_CHANGED,
this,
SLOT(deviceChanged()));
SLOT(deviceChanged()));*/
system.connect(UPOWER_SERVICE,
UPOWER_PATH,
DBUS_PROPERTIES,
DBUS_PROPERTIES_CHANGED,
this,
SLOT(propertiesChanged()));
system.connect(UPOWER_SERVICE,
UPOWER_PATH,
UPOWER_SERVICE,
Expand All @@ -289,13 +296,6 @@ void Manager::setup()
UPOWER_NOTIFY_SLEEP,
this,
SLOT(handleSuspend()));
// DONT WORK ANYMORE! WHY?
/*system.connect(LOGIND_SERVICE,
LOGIND_PATH,
LOGIND_MANAGER,
PK_PREPARE_FOR_SUSPEND,
this,
SLOT(handlePrepareForSuspend(bool)));*/
if (upower == NULL) {
upower = new QDBusInterface(UPOWER_SERVICE,
UPOWER_PATH,
Expand Down Expand Up @@ -327,6 +327,7 @@ void Manager::setup()
SLOT(handlePrepareForSuspend(bool)));
}
if (!suspendLock) { registerSuspendLock(); }
if (!lidLock) { registerLidLock(); }
scan();
} else { qWarning() << "Failed to connect to system bus"; }
}
Expand All @@ -338,6 +339,7 @@ void Manager::check()
return;
}
if (!suspendLock) { registerSuspendLock(); }
if (!lidLock) { registerLidLock(); }
if (!upower->isValid()) { scan(); }
}

Expand Down Expand Up @@ -393,31 +395,48 @@ void Manager::deviceRemoved(const QString &path)

void Manager::deviceChanged()
{
if (wasLidClosed != LidIsClosed()) {
if (!wasLidClosed && LidIsClosed()) {
qDebug() << "a device changed, tell the world!";
emit UpdatedDevices();
}

void Manager::propertiesChanged()
{
bool isLidClosed = LidIsClosed();
bool isOnBattery = OnBattery();

qDebug() << "properties changed:"
<< "lid closed?" << wasLidClosed << isLidClosed
<< "on battery?" << wasOnBattery << isOnBattery;

if (wasLidClosed != isLidClosed) {
if (!wasLidClosed && isLidClosed) {
qDebug() << "lid changed status to closed";
emit LidClosed();
} else if (wasLidClosed && !LidIsClosed()) {
} else if (wasLidClosed && !isLidClosed) {
qDebug() << "lid changed status to open";
emit LidOpened();
}
}
wasLidClosed = LidIsClosed();
wasLidClosed = isLidClosed;

if (wasOnBattery != OnBattery()) {
if (!wasOnBattery && OnBattery()) {
if (wasOnBattery != isOnBattery) {
if (!wasOnBattery && isOnBattery) {
qDebug() << "switched to battery power";
emit SwitchedToBattery();
} else if (wasOnBattery && !OnBattery()) {
} else if (wasOnBattery && !isOnBattery) {
qDebug() << "switched to ac power";
emit SwitchedToAC();
}
}
wasOnBattery = OnBattery();

emit UpdatedDevices();
deviceChanged();
}

void Manager::handleDeviceChanged(const QString &device)
{
Q_UNUSED(device)
qDebug() << "device changed" << device;
if (device.isEmpty()) { return; }
deviceChanged();
}

Expand Down Expand Up @@ -532,6 +551,28 @@ bool Manager::registerSuspendLock()
return false;
}

bool Manager::registerLidLock()
{
if (lidLock) { return false; }
qDebug() << "register lid lock";
QDBusReply<QDBusUnixFileDescriptor> reply;
if (HasLogind() && logind && logind->isValid()) {
reply = logind->call("Inhibit",
"handle-lid-switch",
"powerkit",
"Custom lid handler",
"block");
}
if (reply.isValid()) {
lidLock.reset(new QDBusUnixFileDescriptor(reply.value()));
qDebug() << "lidLock" << lidLock->fileDescriptor();
return true;
} else {
qWarning() << reply.error();
}
return false;
}

void Manager::SetWakeAlarmFromSettings()
{
if (!CanHibernate()) { return; }
Expand Down Expand Up @@ -861,6 +902,12 @@ void Manager::ReleaseSuspendLock()
suspendLock.reset(nullptr);
}

void Manager::ReleaseLidLock()
{
qDebug() << "release lid lock";
lidLock.reset(nullptr);
}

void Manager::SetSuspendWakeAlarmOnBattery(int value)
{
qDebug() << "set suspend wake alarm on battery" << value;
Expand Down
5 changes: 5 additions & 0 deletions src/powerkit_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ namespace PowerKit
QDateTime wakeAlarmDate;

QScopedPointer<QDBusUnixFileDescriptor> suspendLock;
QScopedPointer<QDBusUnixFileDescriptor> lidLock;

int suspendWakeupBattery;
int suspendWakeupAC;
Expand Down Expand Up @@ -113,6 +114,7 @@ namespace PowerKit
void deviceRemoved(const QDBusObjectPath &obj);
void deviceRemoved(const QString &path);
void deviceChanged();
void propertiesChanged();
void handleDeviceChanged(const QString &device);
void handleResume();
void handleSuspend();
Expand All @@ -128,6 +130,8 @@ namespace PowerKit
void handleDelInhibitPowerManagement(quint32 cookie);

bool registerSuspendLock();
bool registerLidLock();

void SetWakeAlarmFromSettings();

public slots:
Expand Down Expand Up @@ -168,6 +172,7 @@ namespace PowerKit
QMap<quint32, QString> GetInhibitors();
const QDateTime GetWakeAlarm();
void ReleaseSuspendLock();
void ReleaseLidLock();
void SetSuspendWakeAlarmOnBattery(int value);
void SetSuspendWakeAlarmOnAC(int value);
bool SetDisplayBacklight(QString const &device, int value);
Expand Down

0 comments on commit a466725

Please sign in to comment.