diff options
Diffstat (limited to 'tdecore/tdehw/hwlibdaemons/tdedbus/PowerService.cpp')
-rw-r--r-- | tdecore/tdehw/hwlibdaemons/tdedbus/PowerService.cpp | 277 |
1 files changed, 277 insertions, 0 deletions
diff --git a/tdecore/tdehw/hwlibdaemons/tdedbus/PowerService.cpp b/tdecore/tdehw/hwlibdaemons/tdedbus/PowerService.cpp new file mode 100644 index 000000000..9b269a9de --- /dev/null +++ b/tdecore/tdehw/hwlibdaemons/tdedbus/PowerService.cpp @@ -0,0 +1,277 @@ +/* + * PowerService.cpp + * + * Created on: Feb 1, 2021 + * Author: emanoil + * + * hardwarecontrol Copyright (C) 2021 trinity desktop development team + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include <unistd.h> + +//#include <kdebug.h> +#include <tqfile.h> +#include <tqtextstream.h> + +#include "PowerService.h" + +#define POWER_STATE_PATH "/sys/power/state" +#define POWER_DISK_PATH "/sys/power/disk" + +PowerService::PowerService(TQT_DBusConnection &conn) +: DeviceServiceBase(conn) +{ + // TODO Auto-generated constructor stub + +} + +PowerService::~PowerService() +{ + // TODO Auto-generated destructor stub +} + +bool PowerService::canSetPower(TQString state, TQT_DBusError& error) { + + bool method = false; + if (canSetDeviceValue(POWER_STATE_PATH,error)) { + TQFile file1( POWER_STATE_PATH ); + if ( file1.open( IO_ReadOnly ) ) { + TQTextStream stream( &file1 ); + TQString line = stream.readLine(); // line of text excluding '\n' + if ( line.find(state, 0) != -1 ) + method = true; + file1.close(); + } else { + error = TQT_DBusError::stdInvalidArgs(TQString ("Can not write device: ").append(POWER_STATE_PATH)); + } + } + + // send reply + return method; +} + +bool PowerService::setPower(TQString state, TQT_DBusError& error) { + + bool written = false; + if (canSetPower(state,error)) { + if ( setDeviceValue(POWER_STATE_PATH, state, error) ) { + written = true; + } else { + error = TQT_DBusError::stdFailed(TQString ("Can not set state: " + state)); + } + } else { + error = TQT_DBusError::stdFailed(TQString ("Can not write device: ").append(POWER_STATE_PATH)); + } + + return written; +} + +bool PowerService::canSetHibernation(TQString state, TQT_DBusError& error) { + + // check if path is writable + bool state_writable = canSetDeviceValue(POWER_STATE_PATH,error); + bool disk_writable = canSetDeviceValue(POWER_DISK_PATH,error); + + // check if method is supported + bool method1 = false, method2 = false; + if (state_writable && disk_writable) { + TQFile file1( POWER_STATE_PATH ); + if ( file1.open( IO_ReadOnly ) ) { + TQTextStream stream( &file1 ); + TQString line = stream.readLine(); // line of text excluding '\n' + if ( line.find("disk", 0) != -1 ) + method1 = true; + file1.close(); +// kdDebug() << "Method1 for " << state << " is " << method1 << "\n"; + } else { + error = TQT_DBusError::stdInvalidArgs(TQString ("Could not open ").append(POWER_STATE_PATH)); + return false; + } + + TQFile file2(POWER_DISK_PATH); + if ( file2.open( IO_ReadOnly ) ) { + TQTextStream stream( &file2 ); + TQString line = stream.readLine(); // line of text excluding '\n' + if ( line.find(state, 0) != -1 ) + method2 = true; + file2.close(); + } else { + error = TQT_DBusError::stdInvalidArgs(TQString ("Could not open ").append(POWER_DISK_PATH)); + return false; + } + } + + // send reply + return state_writable && disk_writable && method1 && method2; +} + +bool PowerService::setHibernation(TQString state, TQT_DBusError& error) { + + // set hibernation state + bool written1 = false, written2 = false; + + if (canSetHibernation(state,error)) { + TQFile file1(POWER_DISK_PATH); + if (!file1.open( IO_WriteOnly ) ) { + error = TQT_DBusError::stdFailed(TQString ("Could not open device ").append(POWER_DISK_PATH)); + return false; + } + TQTextStream stream1( &file1 ); + stream1 << state; + file1.close(); + written1 = true; + + TQFile file2(POWER_STATE_PATH); + if (!file2.open( IO_WriteOnly ) ) { + error = TQT_DBusError::stdFailed(TQString ("Could not open device ").append(POWER_STATE_PATH)); + return false; + } + TQTextStream stream2( &file2 ); + stream2 << "disk"; + file2.close(); + written2 = true; + } else { + error = TQT_DBusError::stdFailed(TQString ("Could not set state: " + state)); + return false; + } + + return written1 && written2; +} + +/*! + * Implement virtual methods + * + */ + +bool PowerService::CanStandby(bool& value, TQT_DBusError& error) { + value = canSetPower("standby", error); + if (error.isValid()) { + tqDebug(error.message().local8Bit()); + return false; + } + return true; +} + +bool PowerService::Standby(bool& value, TQT_DBusError& error) { + value = setPower("standby", error); + if (error.isValid()) { + tqDebug(error.message().local8Bit()); + return false; + } + return true; +} + +bool PowerService::CanFreeze(bool& value, TQT_DBusError& error) { + value = canSetPower("freeze", error); + if (error.isValid()) { + tqDebug(error.message().local8Bit()); + return false; + } + return true; +} + +bool PowerService::Freeze(bool& value, TQT_DBusError& error) { + value = setPower("freeze", error); + if (error.isValid()) { + tqDebug(error.message().local8Bit()); + return false; + } + return true; +} + +bool PowerService::CanSuspend(bool& value, TQT_DBusError& error) { + value = canSetPower("mem", error); + if (error.isValid()) { + tqDebug(error.message().local8Bit()); + return false; + } + return true; +} + +bool PowerService::Suspend(bool& value, TQT_DBusError& error) { + value = setPower("mem", error); + if (error.isValid()) { + tqDebug(error.message().local8Bit()); + return false; + } + return true; +} + +bool PowerService::CanHibernate(bool& value, TQT_DBusError& error) { + value = canSetHibernation("platform", error); + if (error.isValid()) { + tqDebug(error.message().local8Bit()); + return false; + } + return true; +} + +bool PowerService::Hibernate(bool& value, TQT_DBusError& error) { + value = setHibernation("platform", error); + if (error.isValid()) { + tqDebug(error.message().local8Bit()); + return false; + } + return true; +} + +bool PowerService::CanHybridSuspend(bool& value, TQT_DBusError& error) { + value = canSetHibernation("suspend", error); + if (error.isValid()) { + tqDebug(error.message().local8Bit()); + return false; + } + return true; +} + +bool PowerService::HybridSuspend(bool& value, TQT_DBusError& error) { + value = setHibernation("suspend", error); + if (error.isValid()) { + tqDebug(error.message().local8Bit()); + return false; + } + return true; +} + +bool PowerService::CanSetHibernationMethod(bool& value, TQT_DBusError& error) { + value = canSetDeviceValue(POWER_DISK_PATH, error); + if (error.isValid()) { + tqDebug(error.message().local8Bit()); + return false; + } + return true; +} + +bool PowerService::SetHibernationMethod(const TQString& method, bool& value, TQT_DBusError& error) { + // set hibernation method + if (!method.isEmpty()) { + value = setDeviceValue(POWER_DISK_PATH, method, error); + } + else { + value = false; + error = TQT_DBusError::stdInvalidArgs(TQString ("Invalid argument for method: " + method)); + } + if (error.isValid()) { + tqDebug(error.message().local8Bit()); + return false; + } + return true; +} + + +void PowerService::handleMethodReply(const TQT_DBusMessage& reply) { + m_connection->send(reply); +} |