Fixed support for logind power state calls.

Fixed bug in tdehw daemon power state code introduced in previous
commit.

Signed-off-by: Michele Calgaro <michele.calgaro@yahoo.it>
feat/fix-suspend-code
Michele Calgaro 6 years ago
parent 210f8e9691
commit 841ae5477c
Signed by: MicheleC
GPG Key ID: 2A75B7CA8ADED5CF

@ -254,7 +254,7 @@ void reply_SetBrightness(DBusMessage* msg, DBusConnection* conn) {
free(safepath);
}
bool check_CanSetSuspend(char* state, char* disk, char* mem) {
bool CanSetPowerState(const char* state, const char* disk, const char* mem) {
// check if required files are writable
bool files_writable = (access("/sys/power/state", W_OK) == 0);
if (disk)
@ -327,7 +327,7 @@ bool check_CanSetSuspend(char* state, char* disk, char* mem) {
return result;
}
bool do_SetSuspend(char* state, char* disk, char* mem) {
bool SetPowerState(const char* state, const char* disk, const char* mem) {
// check if required files are writable
bool files_writable = (access("/sys/power/state", W_OK) == 0);
if (disk)
@ -373,31 +373,6 @@ bool do_SetSuspend(char* state, char* disk, char* mem) {
return result;
}
void reply_SetSuspend(DBusMessage* msg, DBusConnection* conn, bool result) {
// create a reply from the message
DBusMessage *reply = dbus_message_new_method_return(msg);
const char* member = dbus_message_get_member(msg);
// add the arguments to the reply
DBusMessageIter args;
dbus_message_iter_init_append(reply, &args);
if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_BOOLEAN, &result)) {
fprintf(stderr, "[tde_dbus_hardwarecontrol] %s: dbus_message_iter_append_basic failed\n", member);
return;
}
// send the reply && flush the connection
dbus_uint32_t serial = 0;
if (!dbus_connection_send(conn, reply, &serial)) {
fprintf(stderr, "[tde_dbus_hardwarecontrol] %s: dbus_connection_send failed\n", member);
return;
}
dbus_connection_flush(conn);
// free the reply
dbus_message_unref(reply);
}
void reply_CanSetHibernationMethod(DBusMessage* msg, DBusConnection* conn) {
// check if path is writable
reply_CanSetGivenPath(msg, conn, "/sys/power/disk");
@ -778,69 +753,69 @@ void listen() {
reply_SetBrightness(msg, conn);
}
else if (dbus_message_is_method_call(msg, "org.trinitydesktop.hardwarecontrol.Power", "CanFreeze")) {
bool result = check_CanSetSuspend("freeze", NULL, NULL) || check_CanSetSuspend("mem", NULL, "s2idle");
bool result = CanSetPowerState("freeze", NULL, NULL) || CanSetPowerState("mem", NULL, "s2idle");
reply_Bool(msg, conn, result);
}
else if (dbus_message_is_method_call(msg, "org.trinitydesktop.hardwarecontrol.Power", "Freeze")) {
bool result = false;
if (check_CanSetSuspend("freeze", NULL, NULL)) {
result = do_SetSuspend("freeze", NULL, NULL);
if (CanSetPowerState("freeze", NULL, NULL)) {
result = SetPowerState("freeze", NULL, NULL);
}
else if (check_CanSetSuspend("mem", NULL, "s2idle")) {
result = do_SetSuspend("mem", NULL, "s2idle");
else if (CanSetPowerState("mem", NULL, "s2idle")) {
result = SetPowerState("mem", NULL, "s2idle");
}
reply_SetSuspend(msg, conn, result);
reply_Bool(msg, conn, result);
}
else if (dbus_message_is_method_call(msg, "org.trinitydesktop.hardwarecontrol.Power", "CanStandby")) {
bool result = check_CanSetSuspend("standby", NULL, NULL) || check_CanSetSuspend("mem", NULL, "shallow");
bool result = CanSetPowerState("standby", NULL, NULL) || CanSetPowerState("mem", NULL, "shallow");
reply_Bool(msg, conn, result);
}
else if (dbus_message_is_method_call(msg, "org.trinitydesktop.hardwarecontrol.Power", "Standby")) {
bool result = false;
if (check_CanSetSuspend("standby", NULL, NULL)) {
result = do_SetSuspend("standby", NULL, NULL);
if (CanSetPowerState("standby", NULL, NULL)) {
result = SetPowerState("standby", NULL, NULL);
}
else if (check_CanSetSuspend("mem", NULL, "shallow")) {
result = do_SetSuspend("mem", NULL, "shallow");
else if (CanSetPowerState("mem", NULL, "shallow")) {
result = SetPowerState("mem", NULL, "shallow");
}
reply_SetSuspend(msg, conn, result);
reply_Bool(msg, conn, result);
}
else if (dbus_message_is_method_call(msg, "org.trinitydesktop.hardwarecontrol.Power", "CanSuspend")) {
bool result = (check_CanSetSuspend("mem", NULL, NULL) && access("/sys/power/mem_sleep", R_OK) != 0) ||
check_CanSetSuspend("mem", NULL, "deep");
bool result = (CanSetPowerState("mem", NULL, NULL) && access("/sys/power/mem_sleep", R_OK) != 0) ||
CanSetPowerState("mem", NULL, "deep");
reply_Bool(msg, conn, result);
}
else if (dbus_message_is_method_call(msg, "org.trinitydesktop.hardwarecontrol.Power", "Suspend")) {
bool result = false;
if (check_CanSetSuspend("mem", NULL, NULL) && access("/sys/power/mem_sleep", R_OK) != 0) {
result = do_SetSuspend("mem", NULL, NULL);
if (CanSetPowerState("mem", NULL, NULL) && access("/sys/power/mem_sleep", R_OK) != 0) {
result = SetPowerState("mem", NULL, NULL);
}
else if (check_CanSetSuspend("mem", NULL, "deep")) {
result = do_SetSuspend("mem", NULL, "deep");
else if (CanSetPowerState("mem", NULL, "deep")) {
result = SetPowerState("mem", NULL, "deep");
}
reply_SetSuspend(msg, conn, result);
reply_Bool(msg, conn, result);
}
else if (dbus_message_is_method_call(msg, "org.trinitydesktop.hardwarecontrol.Power", "CanHybridSuspend")) {
bool result = check_CanSetSuspend("disk", "suspend", NULL);
bool result = CanSetPowerState("disk", "suspend", NULL);
reply_Bool(msg, conn, result);
}
else if (dbus_message_is_method_call(msg, "org.trinitydesktop.hardwarecontrol.Power", "HybridSuspend")) {
bool result = do_SetSuspend("disk", "suspend", NULL);
reply_SetSuspend(msg, conn, result);
bool result = SetPowerState("disk", "suspend", NULL);
reply_Bool(msg, conn, result);
}
else if (dbus_message_is_method_call(msg, "org.trinitydesktop.hardwarecontrol.Power", "CanHibernate")) {
bool result = check_CanSetSuspend("disk", "shutdown", NULL) || check_CanSetSuspend("disk", "platform", NULL);
bool result = CanSetPowerState("disk", "shutdown", NULL) || CanSetPowerState("disk", "platform", NULL);
reply_Bool(msg, conn, result);
}
else if (dbus_message_is_method_call(msg, "org.trinitydesktop.hardwarecontrol.Power", "Hibernate")) {
bool result = false;
if (check_CanSetSuspend("disk", "shutdown", NULL)) {
result = do_SetSuspend("disk", "shutdown", NULL);
if (CanSetPowerState("disk", "shutdown", NULL)) {
result = SetPowerState("disk", "shutdown", NULL);
}
else if (check_CanSetSuspend("disk", "platform", NULL)) {
result = do_SetSuspend("disk", "platform", NULL);
else if (CanSetPowerState("disk", "platform", NULL)) {
result = SetPowerState("disk", "platform", NULL);
}
reply_SetSuspend(msg, conn, result);
reply_Bool(msg, conn, result);
}
else if (dbus_message_is_method_call(msg, "org.trinitydesktop.hardwarecontrol.Power", "CanSetHibernationMethod")) {
reply_CanSetHibernationMethod(msg, conn);

@ -644,7 +644,7 @@ void TDERootSystemDevice::setHibernationMethod(TDESystemHibernationMethod::TDESy
}
}
bool check_CanSetSuspend(char* state, char* disk, char* mem) {
bool CanSetSuspend(const char* state, const char* disk, const char* mem) {
// check if required files are writable
bool files_writable = (access("/sys/power/state", W_OK) == 0);
if (disk)
@ -723,50 +723,41 @@ bool TDERootSystemDevice::setPowerState(TDESystemPowerState::TDESystemPowerState
(ps == TDESystemPowerState::HybridSuspend)) {
#ifdef WITH_LOGINDPOWER
{
// No support for "freeze" in org.freedesktop.login1
// No support for "freeze" and "standby" in org.freedesktop.login1
TQT_DBusConnection dbusConn;
dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
if ( dbusConn.isConnected() ) {
TQT_DBusProxy logindProxy("org.freedesktop.login1", "/org/freedesktop/login1",
"org.freedesktop.login1.Manager", dbusConn);
TQValueList<TQT_DBusData> params;
params << TQT_DBusData::fromBool(true);
if (logindProxy.canSend()) {
if (ps == TDESystemPowerState::Suspend) {
TQT_DBusMessage msg = TQT_DBusMessage::methodCall(
"org.freedesktop.login1",
"/org/freedesktop/login1",
"org.freedesktop.login1.Manager",
"Suspend");
TQT_DBusMessage reply = dbusConn.sendWithReply(msg);
TQT_DBusMessage reply = logindProxy.sendWithReply("Suspend", params);
if (reply.type() == TQT_DBusMessage::ReplyMessage) {
return true;
}
}
else if (ps == TDESystemPowerState::Hibernate) {
TQT_DBusMessage msg = TQT_DBusMessage::methodCall(
"org.freedesktop.login1",
"/org/freedesktop/login1",
"org.freedesktop.login1.Manager",
"Hibernate");
TQT_DBusMessage reply = dbusConn.sendWithReply(msg);
else if (ps == TDESystemPowerState::HybridSuspend) {
TQT_DBusMessage reply = logindProxy.sendWithReply("HybridSleep", params);
if (reply.type() == TQT_DBusMessage::ReplyMessage) {
return true;
}
}
else if (ps == TDESystemPowerState::HybridSuspend) {
TQT_DBusMessage msg = TQT_DBusMessage::methodCall(
"org.freedesktop.login1",
"/org/freedesktop/login1",
"org.freedesktop.login1.Manager",
"HybridSleep");
TQT_DBusMessage reply = dbusConn.sendWithReply(msg);
else if (ps == TDESystemPowerState::Hibernate) {
TQT_DBusMessage reply = logindProxy.sendWithReply("Hibernate", params);
if (reply.type() == TQT_DBusMessage::ReplyMessage) {
return true;
}
}
}
}
}
#endif // WITH_LOGINDPOWER
#ifdef WITH_UPOWER
{
// No support for "freeze" and "hybrid suspend" in org.freedesktop.UPower
// No support for "freeze", "standby" and "hybrid suspend" in org.freedesktop.UPower
TQT_DBusConnection dbusConn;
dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
if ( dbusConn.isConnected() ) {
@ -798,7 +789,7 @@ bool TDERootSystemDevice::setPowerState(TDESystemPowerState::TDESystemPowerState
#ifdef WITH_DEVKITPOWER
{
// No support for "freeze" and "hybrid suspend" in org.freedesktop.DeviceKit.Power
// No support for "freeze", "standby" and "hybrid suspend" in org.freedesktop.DeviceKit.Power
TQT_DBusConnection dbusConn;
dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
if ( dbusConn.isConnected() ) {
@ -830,7 +821,7 @@ bool TDERootSystemDevice::setPowerState(TDESystemPowerState::TDESystemPowerState
#ifdef WITH_HAL
{
// No support for "freeze" in org.freedesktop.Hal
// No support for "freeze" and "standby" in org.freedesktop.Hal
TQT_DBusConnection dbusConn;
dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
if ( dbusConn.isConnected() ) {
@ -847,26 +838,26 @@ bool TDERootSystemDevice::setPowerState(TDESystemPowerState::TDESystemPowerState
return true;
}
}
else if (ps == TDESystemPowerState::Hibernate) {
TQT_DBusMessage msg = TQT_DBusMessage::methodCall(
else if (ps == TDESystemPowerState::HybridSuspend) {
TQT_DBusProxy halPowerManagement(
"org.freedesktop.Hal",
"/org/freedesktop/Hal/devices/computer",
"org.freedesktop.Hal.Device.SystemPowerManagement",
"Hibernate");
TQT_DBusMessage reply = dbusConn.sendWithReply(msg);
dbusConn);
TQValueList<TQT_DBusData> params;
params << TQT_DBusData::fromInt32(0);
TQT_DBusMessage reply = halPowerManagement.sendWithReply("SuspendHybrid", params);
if (reply.type() == TQT_DBusMessage::ReplyMessage) {
return true;
}
}
else if (ps == TDESystemPowerState::HybridSuspend) {
TQT_DBusProxy halPowerManagement(
else if (ps == TDESystemPowerState::Hibernate) {
TQT_DBusMessage msg = TQT_DBusMessage::methodCall(
"org.freedesktop.Hal",
"/org/freedesktop/Hal/devices/computer",
"org.freedesktop.Hal.Device.SystemPowerManagement",
dbusConn);
TQValueList<TQT_DBusData> params;
params << TQT_DBusData::fromInt32(0);
TQT_DBusMessage reply = halPowerManagement.sendWithReply("SuspendHybrid", params);
"Hibernate");
TQT_DBusMessage reply = dbusConn.sendWithReply(msg);
if (reply.type() == TQT_DBusMessage::ReplyMessage) {
return true;
}
@ -952,7 +943,7 @@ bool TDERootSystemDevice::setPowerState(TDESystemPowerState::TDESystemPowerState
bool diskWriteOk = false;
bool memWriteOk = false;
if (ps == TDESystemPowerState::Freeze) {
if (check_CanSetSuspend("freeze", NULL, NULL)) {
if (CanSetSuspend("freeze", NULL, NULL)) {
stateCommand = "freeze";
}
else {
@ -961,7 +952,7 @@ bool TDERootSystemDevice::setPowerState(TDESystemPowerState::TDESystemPowerState
}
}
else if (ps == TDESystemPowerState::Standby) {
if (check_CanSetSuspend("standby", NULL, NULL)) {
if (CanSetSuspend("standby", NULL, NULL)) {
stateCommand = "standby";
}
else {
@ -971,7 +962,7 @@ bool TDERootSystemDevice::setPowerState(TDESystemPowerState::TDESystemPowerState
}
else if (ps == TDESystemPowerState::Suspend) {
stateCommand = "mem";
if (check_CanSetSuspend("mem", NULL, "deep")) {
if (CanSetSuspend("mem", NULL, "deep")) {
diskCommand = "deep";
}
}
@ -981,7 +972,7 @@ bool TDERootSystemDevice::setPowerState(TDESystemPowerState::TDESystemPowerState
}
else if (ps == TDESystemPowerState::Hibernate) {
stateCommand = "disk";
if (check_CanSetSuspend("disk", "shutdown", NULL)) {
if (CanSetSuspend("disk", "shutdown", NULL)) {
diskCommand = "shutdown";
}
else {
@ -1027,17 +1018,18 @@ bool TDERootSystemDevice::setPowerState(TDESystemPowerState::TDESystemPowerState
TQT_DBusConnection dbusConn;
dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
if ( dbusConn.isConnected() ) {
TQT_DBusMessage msg = TQT_DBusMessage::methodCall(
"org.trinitydesktop.login1",
"/org/freedesktop/login1",
"org.freedesktop.login1.Manager",
"PowerOff");
TQT_DBusMessage reply = dbusConn.sendWithReply(msg);
TQT_DBusProxy logindProxy("org.freedesktop.login1", "/org/freedesktop/login1",
"org.freedesktop.login1.Manager", dbusConn);
TQValueList<TQT_DBusData> params;
params << TQT_DBusData::fromBool(true);
if (logindProxy.canSend()) {
TQT_DBusMessage reply = logindProxy.sendWithReply("PowerOff", params);
if (reply.type() == TQT_DBusMessage::ReplyMessage) {
return true;
}
}
}
}
#endif // WITH_LOGINDPOWER
#ifdef WITH_CONSOLEKIT
@ -1096,17 +1088,18 @@ bool TDERootSystemDevice::setPowerState(TDESystemPowerState::TDESystemPowerState
TQT_DBusConnection dbusConn;
dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
if ( dbusConn.isConnected() ) {
TQT_DBusMessage msg = TQT_DBusMessage::methodCall(
"org.trinitydesktop.login1",
"/org/freedesktop/login1",
"org.freedesktop.login1.Manager",
"Reboot");
TQT_DBusMessage reply = dbusConn.sendWithReply(msg);
TQT_DBusProxy logindProxy("org.freedesktop.login1", "/org/freedesktop/login1",
"org.freedesktop.login1.Manager", dbusConn);
TQValueList<TQT_DBusData> params;
params << TQT_DBusData::fromBool(true);
if (logindProxy.canSend()) {
TQT_DBusMessage reply = logindProxy.sendWithReply("Reboot", params);
if (reply.type() == TQT_DBusMessage::ReplyMessage) {
return true;
}
}
}
}
#endif // WITH_LOGINDPOWER
#ifdef WITH_CONSOLEKIT

Loading…
Cancel
Save