kutils: implement KPowerManager::setCPUGovernor()

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2022-04-20 05:26:05 +03:00
parent a67fb69ff7
commit cfaffe2f34
4 changed files with 124 additions and 2 deletions

View file

@ -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)

View file

@ -17,6 +17,7 @@
*/
#include "kpowermanager.h"
#include "kauthaction.h"
#include "kdebug.h"
#include <QFile>
@ -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

View file

@ -0,0 +1,65 @@
/* This file is part of the KDE libraries
Copyright (C) 2022 Ivailo Monev <xakepa10@gmail.com>
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 <QFile>
#include <QDir>
ActionReply KPowerManagerHelper::setgovernor(const QVariantMap &parameters)
{
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)

View file

@ -0,0 +1,34 @@
/* This file is part of the KDE libraries
Copyright (C) 2022 Ivailo Monev <xakepa10@gmail.com>
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 <kauthactionreply.h>
// 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 &parameters);
};
#endif // KPOWERMANAGER_HELPER_H