From cfaffe2f34e3b5607c0df5cab56b08a93b0a0262 Mon Sep 17 00:00:00 2001 From: Ivailo Monev Date: Wed, 20 Apr 2022 05:26:05 +0300 Subject: [PATCH] kutils: implement KPowerManager::setCPUGovernor() Signed-off-by: Ivailo Monev --- kutils/kpowermanager/CMakeLists.txt | 17 +++++ kutils/kpowermanager/kpowermanager.cpp | 10 ++- kutils/kpowermanager/kpowermanager_helper.cpp | 65 +++++++++++++++++++ kutils/kpowermanager/kpowermanager_helper.h | 34 ++++++++++ 4 files changed, 124 insertions(+), 2 deletions(-) create mode 100644 kutils/kpowermanager/kpowermanager_helper.cpp create mode 100644 kutils/kpowermanager/kpowermanager_helper.h diff --git a/kutils/kpowermanager/CMakeLists.txt b/kutils/kpowermanager/CMakeLists.txt index bcba3add..24b52f0b 100644 --- a/kutils/kpowermanager/CMakeLists.txt +++ b/kutils/kpowermanager/CMakeLists.txt @@ -47,3 +47,20 @@ install( kded/org.freedesktop.PowerManagement.Inhibit.xml DESTINATION ${KDE4_DBUS_INTERFACES_INSTALL_DIR} ) + +########### next target ############### + +set(kpowermanager_helper_srcs + kpowermanager_helper.cpp +) + +add_executable(kpowermanager_helper ${kpowermanager_helper_srcs}) +target_link_libraries(kpowermanager_helper PUBLIC ${KDE4_KDECORE_LIBS}) + +install( + TARGETS kpowermanager_helper + DESTINATION ${KDE4_LIBEXEC_INSTALL_DIR} +) + +kde4_install_auth_helper_files(kpowermanager_helper org.kde.kpowermanager.helper root) + diff --git a/kutils/kpowermanager/kpowermanager.cpp b/kutils/kpowermanager/kpowermanager.cpp index f5ce8273..5d29f6d9 100644 --- a/kutils/kpowermanager/kpowermanager.cpp +++ b/kutils/kpowermanager/kpowermanager.cpp @@ -17,6 +17,7 @@ */ #include "kpowermanager.h" +#include "kauthaction.h" #include "kdebug.h" #include @@ -66,8 +67,13 @@ bool KPowerManager::setCPUGovernor(const QString &governor) kWarning() << "Invalid CPU governor" << governor; return false; } - // TODO: - return false; + + KAuth::Action helperaction("org.kde.kpowermanager.helper.setgovernor"); + helperaction.setHelperID("org.kde.kpowermanager.helper"); + helperaction.addArgument("governor", governor); + KAuth::ActionReply helperreply = helperaction.execute(); + // qDebug() << helperreply.errorCode() << helperreply.errorDescription(); + return (helperreply == KAuth::ActionReply::SuccessReply); } int KPowerManager::screenBrightness() const diff --git a/kutils/kpowermanager/kpowermanager_helper.cpp b/kutils/kpowermanager/kpowermanager_helper.cpp new file mode 100644 index 00000000..7fbacfed --- /dev/null +++ b/kutils/kpowermanager/kpowermanager_helper.cpp @@ -0,0 +1,65 @@ +/* This file is part of the KDE libraries + Copyright (C) 2022 Ivailo Monev + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License version 2, as published by the Free Software Foundation. + + This library 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 + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include "kpowermanager_helper.h" +#include "kauthhelpersupport.h" + +#include +#include + +ActionReply KPowerManagerHelper::setgovernor(const QVariantMap ¶meters) +{ + if (!parameters.contains("governor")) { + return KAuth::ActionReply::HelperErrorReply; + } + + const QByteArray governorbytes = parameters.value("governor").toByteArray(); + QDir cpudir("/sys/devices/system/cpu"); + foreach (const QFileInfo &cpuinfo, cpudir.entryInfoList()) { + QFile cpufile(cpuinfo.filePath() + QLatin1String("/cpufreq/scaling_governor")); + if (!cpufile.exists()) { + continue; + } + if (!cpufile.open(QFile::WriteOnly)) { + KAuth::ActionReply errorreply(KAuth::ActionReply::HelperError); + errorreply.setErrorDescription( + QString::fromLatin1("Could not open: %1 (%2)") + .arg(cpufile.fileName()) + .arg(cpufile.errorString() + ) + ); + errorreply.setErrorCode(1); + return errorreply; + } + if (cpufile.write(governorbytes) != governorbytes.size()) { + KAuth::ActionReply errorreply(KAuth::ActionReply::HelperError); + errorreply.setErrorDescription( + QString::fromLatin1("Could not write to: %1 (%2)") + .arg(cpufile.fileName()) + .arg(cpufile.errorString() + ) + ); + errorreply.setErrorCode(2); + return errorreply; + } + } + + return KAuth::ActionReply::SuccessReply; +} + +KDE4_AUTH_HELPER_MAIN("org.kde.kpowermanager.helper", KPowerManagerHelper) diff --git a/kutils/kpowermanager/kpowermanager_helper.h b/kutils/kpowermanager/kpowermanager_helper.h new file mode 100644 index 00000000..962edb19 --- /dev/null +++ b/kutils/kpowermanager/kpowermanager_helper.h @@ -0,0 +1,34 @@ +/* This file is part of the KDE libraries + Copyright (C) 2022 Ivailo Monev + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License version 2, as published by the Free Software Foundation. + + This library 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 + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#ifndef KPOWERMANAGER_HELPER_H +#define KPOWERMANAGER_HELPER_H + +#include + +// methods return type must be ActionReply otherwise QMetaObject::invokeMethod() fails +using namespace KAuth; + +class KPowerManagerHelper : public QObject +{ + Q_OBJECT +public slots: + ActionReply setgovernor(const QVariantMap ¶meters); +}; + +#endif // KPOWERMANAGER_HELPER_H