diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..05b59d3 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,27 @@ +name: Build + +on: + push: + branches: [ main ] + pull_request: + branches: [ main ] + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Checkout Source + uses: actions/checkout@v2 + + - name: Update repository + run: sudo apt-get update -y + + - name: Install the basic dev packages + run: sudo apt-get install -y equivs curl git devscripts lintian build-essential automake autotools-dev cmake g++ + + - name: Install build dependencies + run: sudo mk-build-deps -i -t "apt-get --yes" -r + + - name: Build + run: mkdir build; cd build; cmake .. ; make -j$(nproc); \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 74cc247..1c8a09f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,3 +24,4 @@ add_subdirectory(session) add_subdirectory(settings-daemon) add_subdirectory(shutdown-ui) add_subdirectory(xembed-sni-proxy) +add_subdirectory(powerman) diff --git a/powerman/CMakeLists.txt b/powerman/CMakeLists.txt new file mode 100644 index 0000000..ecc6b93 --- /dev/null +++ b/powerman/CMakeLists.txt @@ -0,0 +1,26 @@ +project(cutefish-powerman) +set(TARGET cutefish-powerman) + +set(SOURCES + main.cpp + application.cpp + cpu/cpuitem.cpp + cpu/cpumanagement.cpp +) + +qt5_add_dbus_adaptor(DBUS_SOURCES + cpu/org.cutefish.CPUManagement.xml + cpu/cpumanagement.h CPUManagement) +set_source_files_properties(${DBUS_SOURCES} PROPERTIES SKIP_AUTOGEN ON) + +add_executable(${TARGET} ${SOURCES} ${DBUS_SOURCES}) +target_link_libraries(${TARGET} + Qt5::Core + Qt5::Gui + Qt5::Widgets + Qt5::Quick + Qt5::DBus + Qt5::X11Extras +) + +install(TARGETS ${TARGET} DESTINATION /usr/bin) diff --git a/powerman/application.cpp b/powerman/application.cpp new file mode 100644 index 0000000..71711e4 --- /dev/null +++ b/powerman/application.cpp @@ -0,0 +1,28 @@ +/* + * 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 "application.h" +#include + +Application::Application(QObject *parent) + : QObject(parent) + , m_cpuManagement(new CPUManagement) +{ + QDBusConnection::sessionBus().registerService(QStringLiteral("org.cutefish.PowerManager")); +} diff --git a/powerman/application.h b/powerman/application.h new file mode 100644 index 0000000..9b4c08b --- /dev/null +++ b/powerman/application.h @@ -0,0 +1,37 @@ +/* + * 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 . + */ + +#ifndef APPLICATION_H +#define APPLICATION_H + +#include +#include "cpu/cpumanagement.h" + +class Application : public QObject +{ + Q_OBJECT + +public: + explicit Application(QObject *parent = nullptr); + +private: + CPUManagement *m_cpuManagement; +}; + +#endif // APPLICATION_H diff --git a/powerman/cpu/cpuitem.cpp b/powerman/cpu/cpuitem.cpp new file mode 100644 index 0000000..979f457 --- /dev/null +++ b/powerman/cpu/cpuitem.cpp @@ -0,0 +1,57 @@ +/* + * 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 "cpuitem.h" +#include +#include + +CpuItem::CpuItem(const QString &name, QObject *parent) + : QObject(parent) + , m_name(name) +{ +} + +bool CpuItem::setPolicy(const QString &value) +{ + bool result = false; + + QFile file(QString("/sys/devices/system/cpu/%1/cpufreq/scaling_governor").arg(m_name)); + if (file.open(QIODevice::WriteOnly)) { + if (file.write(value.toLatin1()) != -1) + result = true; + file.close(); + } + + qDebug() << m_name << "set policy: " << value << result; + + return result; +} + +QString CpuItem::policy() +{ + QString result; + QFile file(QString("/sys/devices/system/cpu/%1/cpufreq/scaling_governor").arg(m_name)); + + if (file.open(QIODevice::ReadOnly)) { + result = file.readAll().simplified(); + file.close(); + } + + return result; +} diff --git a/powerman/cpu/cpuitem.h b/powerman/cpu/cpuitem.h new file mode 100644 index 0000000..c932a63 --- /dev/null +++ b/powerman/cpu/cpuitem.h @@ -0,0 +1,41 @@ +/* + * 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 . + */ + +#ifndef CPUITEM_H +#define CPUITEM_H + +#include + +class CpuItem : public QObject +{ + Q_OBJECT + +public: + explicit CpuItem(const QString &name, QObject *parent = nullptr); + + QString name(); + + bool setPolicy(const QString &value); + QString policy(); + +private: + QString m_name; +}; + +#endif // CPUITEM_H diff --git a/powerman/cpu/cpumanagement.cpp b/powerman/cpu/cpumanagement.cpp new file mode 100644 index 0000000..ae2ab1c --- /dev/null +++ b/powerman/cpu/cpumanagement.cpp @@ -0,0 +1,93 @@ +/* + * 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 "cpumanagement.h" +#include "cpumanagementadaptor.h" + +#include +#include +#include + +CPUManagement::CPUManagement(QObject *parent) + : QObject(parent) + , m_currentMode(PowerSave) +{ + new CPUManagementAdaptor(this); + QDBusConnection::sessionBus().registerObject(QStringLiteral("/CPUManagement"), this); + + // Init cpu items. + QDir dir("/sys/devices/system/cpu"); + QStringList dirList = dir.entryList(QDir::Dirs | QDir::NoDot | QDir::NoDotAndDotDot); + + for (const QString &dirName : dirList) { + if (dirName.startsWith("cpu") && dirName[3].isNumber()) { + m_items.append(new CpuItem(dirName)); + } + } + + // Init mode + bool performance = true; + for (CpuItem *item : m_items) { + if (item->policy() == "powersave") + performance = false; + } + if (performance) + m_currentMode = CPUManagement::Performance; + + setMode(PowerSave); +} + +void CPUManagement::setMode(int value) +{ + CPUManagement::Mode mode = static_cast(value); + QString modeString = modeConvertToString(mode); + + if (modeString.isEmpty() || mode == m_currentMode) + return; + + for (CpuItem *item : m_items) { + item->setPolicy(modeString); + } + + m_currentMode = mode; + emit modeChanged(); +} + +int CPUManagement::mode() +{ + return m_currentMode; +} + +QString CPUManagement::modeConvertToString(CPUManagement::Mode mode) +{ + QString result; + + switch (mode) { + case CPUManagement::PowerSave: + result = "powersave"; + break; + case CPUManagement::Performance: + result = "performance"; + break; + default: + break; + } + + return result; +} diff --git a/powerman/cpu/cpumanagement.h b/powerman/cpu/cpumanagement.h new file mode 100644 index 0000000..d647edc --- /dev/null +++ b/powerman/cpu/cpumanagement.h @@ -0,0 +1,53 @@ +/* + * 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 . + */ + +#ifndef CPUMANAGEMENT_H +#define CPUMANAGEMENT_H + +#include +#include "cpuitem.h" + +class CPUManagement : public QObject +{ + Q_OBJECT + Q_PROPERTY(int mode READ mode WRITE setMode NOTIFY modeChanged) + +public: + enum Mode { + PowerSave = 0, + Performance, + Normal, + }; + + explicit CPUManagement(QObject *parent = nullptr); + + void setMode(int i); + int mode(); + + QString modeConvertToString(Mode mode); + +signals: + void modeChanged(); + +private: + Mode m_currentMode; + QList m_items; +}; + +#endif // CPUMANAGEMENT_H diff --git a/powerman/cpu/org.cutefish.CPUManagement.xml b/powerman/cpu/org.cutefish.CPUManagement.xml new file mode 100644 index 0000000..86a7955 --- /dev/null +++ b/powerman/cpu/org.cutefish.CPUManagement.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/powerman/main.cpp b/powerman/main.cpp new file mode 100644 index 0000000..0b16d7c --- /dev/null +++ b/powerman/main.cpp @@ -0,0 +1,29 @@ +/* + * 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 "application.h" + +int main(int argc, char *argv[]) +{ + QCoreApplication a(argc, argv); + Application app; + // a.setQuitOnLastWindowClosed(false); + return a.exec(); +} diff --git a/session/processmanager.cpp b/session/processmanager.cpp index d3c759a..5f4bf0d 100644 --- a/session/processmanager.cpp +++ b/session/processmanager.cpp @@ -132,6 +132,7 @@ void ProcessManager::startDaemonProcess() { QList> list; list << qMakePair(QString("cutefish-settings-daemon"), QStringList()); + list << qMakePair(QString("cutefish-powerman"), QStringList()); list << qMakePair(QString("cutefish-xembedsniproxy"), QStringList()); for (QPair pair : list) {