From 5584fb1a7bb286861ec5fb670d411f45a9c2eb80 Mon Sep 17 00:00:00 2001 From: reionwong Date: Sun, 25 Jul 2021 21:01:20 +0800 Subject: [PATCH] Add cpufreq tool --- CMakeLists.txt | 1 + cpufreq/CMakeLists.txt | 17 ++++++ cpufreq/main.cpp | 63 ++++++++++++++++++++++ cpufreq/org.cutefish.cpufreq.pkexec.policy | 15 ++++++ powerman/cpu/cpumanagement.cpp | 20 ++++--- 5 files changed, 109 insertions(+), 7 deletions(-) create mode 100644 cpufreq/CMakeLists.txt create mode 100644 cpufreq/main.cpp create mode 100644 cpufreq/org.cutefish.cpufreq.pkexec.policy diff --git a/CMakeLists.txt b/CMakeLists.txt index 142f5a5..608f7cd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,3 +25,4 @@ add_subdirectory(settings-daemon) add_subdirectory(shutdown-ui) add_subdirectory(xembed-sni-proxy) add_subdirectory(powerman) +add_subdirectory(cpufreq) diff --git a/cpufreq/CMakeLists.txt b/cpufreq/CMakeLists.txt new file mode 100644 index 0000000..01fb736 --- /dev/null +++ b/cpufreq/CMakeLists.txt @@ -0,0 +1,17 @@ +project(cutefish-cpufreq) +set(TARGET cutefish-cpufreq) + +set(SOURCES + main.cpp +) + +add_executable(${TARGET} ${SOURCES} ${DBUS_SOURCES}) +target_link_libraries(${TARGET} + Qt5::Core + Qt5::Quick + Qt5::DBus + Qt5::X11Extras +) + +install(TARGETS ${TARGET} DESTINATION /usr/bin) +install(FILES org.cutefish.cpufreq.pkexec.policy DESTINATION /usr/share/polkit-1/actions/) diff --git a/cpufreq/main.cpp b/cpufreq/main.cpp new file mode 100644 index 0000000..ea45827 --- /dev/null +++ b/cpufreq/main.cpp @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2021 CutefishOS Team. + * + * Author: Reion Wong + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * 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, see . + */ + +#include +#include +#include +#include + +int main(int argc, char *argv[]) +{ + QCoreApplication a(argc, argv); + + QCommandLineParser parser; + parser.setApplicationDescription(QStringLiteral("Cutefish CPU frequency tool")); + parser.addHelpOption(); + + QCommandLineOption setOption(QStringList() << "s" << "set" << "Setting"); + parser.addOption(setOption); + + QCommandLineOption numberOption(QStringList() << "c" << "number" << "Number"); + parser.addOption(numberOption); + + QCommandLineOption modeOption(QStringList() << "m" << "mode" << "Mode"); + parser.addOption(modeOption); + + parser.process(a); + + // cutefish-cpufreq --set -n 0 -m performance + if (parser.isSet(setOption) && parser.isSet(numberOption) && parser.isSet(modeOption)) { + QString modeStr = parser.positionalArguments().last(); + + if (modeStr != "powersave" && modeStr != "performance") { + return -1; + } + + QFile file(QString("/sys/devices/system/cpu/cpu%1/cpufreq/scaling_governor").arg(parser.positionalArguments().first())); + + if (!file.open(QIODevice::WriteOnly)) { + return -1; + } + + file.write(modeStr.toUtf8()); + file.close(); + } + + return 0; +} diff --git a/cpufreq/org.cutefish.cpufreq.pkexec.policy b/cpufreq/org.cutefish.cpufreq.pkexec.policy new file mode 100644 index 0000000..8f134e9 --- /dev/null +++ b/cpufreq/org.cutefish.cpufreq.pkexec.policy @@ -0,0 +1,15 @@ + + + + Authentication is required to Change CPU configuration + battery + + no + no + yes + + /usr/bin/cutefish-cpufreq + + \ No newline at end of file diff --git a/powerman/cpu/cpumanagement.cpp b/powerman/cpu/cpumanagement.cpp index ae2ab1c..ef9eb05 100644 --- a/powerman/cpu/cpumanagement.cpp +++ b/powerman/cpu/cpumanagement.cpp @@ -20,6 +20,7 @@ #include "cpumanagement.h" #include "cpumanagementadaptor.h" +#include #include #include #include @@ -42,15 +43,16 @@ CPUManagement::CPUManagement(QObject *parent) } // Init mode - bool performance = true; + bool performance = false; for (CpuItem *item : m_items) { - if (item->policy() == "powersave") - performance = false; + qDebug() << item->policy(); + if (item->policy() == "performance") { + performance = true; + break; + } } if (performance) m_currentMode = CPUManagement::Performance; - - setMode(PowerSave); } void CPUManagement::setMode(int value) @@ -61,8 +63,12 @@ void CPUManagement::setMode(int value) if (modeString.isEmpty() || mode == m_currentMode) return; - for (CpuItem *item : m_items) { - item->setPolicy(modeString); + QProcess process; + for (int i = 0; i <= m_items.count(); ++i) { + process.start("pkexec", QStringList() << "cutefish-cpufreq" + << "-s" << "-c" << QString::number(i) + << "-m" << modeString); + process.waitForFinished(-1); } m_currentMode = mode;