mirror of https://github.com/cutefishos/core
				
				
				
			Add powerman module
							parent
							
								
									ea6365982e
								
							
						
					
					
						commit
						df45bdb571
					
				@ -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);
 | 
			
		||||
@ -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)
 | 
			
		||||
@ -0,0 +1,28 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright (C) 2021 CutefishOS Team.
 | 
			
		||||
 *
 | 
			
		||||
 * Author:     Reion Wong <reionwong@gmail.com>
 | 
			
		||||
 *
 | 
			
		||||
 * 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 <http://www.gnu.org/licenses/>.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include "application.h"
 | 
			
		||||
#include <QDBusConnection>
 | 
			
		||||
 | 
			
		||||
Application::Application(QObject *parent)
 | 
			
		||||
    : QObject(parent)
 | 
			
		||||
    , m_cpuManagement(new CPUManagement)
 | 
			
		||||
{
 | 
			
		||||
    QDBusConnection::sessionBus().registerService(QStringLiteral("org.cutefish.PowerManager"));
 | 
			
		||||
}
 | 
			
		||||
@ -0,0 +1,37 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright (C) 2021 CutefishOS Team.
 | 
			
		||||
 *
 | 
			
		||||
 * Author:     Reion Wong <reionwong@gmail.com>
 | 
			
		||||
 *
 | 
			
		||||
 * 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 <http://www.gnu.org/licenses/>.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#ifndef APPLICATION_H
 | 
			
		||||
#define APPLICATION_H
 | 
			
		||||
 | 
			
		||||
#include <QObject>
 | 
			
		||||
#include "cpu/cpumanagement.h"
 | 
			
		||||
 | 
			
		||||
class Application : public QObject
 | 
			
		||||
{
 | 
			
		||||
    Q_OBJECT
 | 
			
		||||
 | 
			
		||||
public:
 | 
			
		||||
    explicit Application(QObject *parent = nullptr);
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
    CPUManagement *m_cpuManagement;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#endif // APPLICATION_H
 | 
			
		||||
@ -0,0 +1,57 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright (C) 2021 CutefishOS Team.
 | 
			
		||||
 *
 | 
			
		||||
 * Author:     Reion Wong <reionwong@gmail.com>
 | 
			
		||||
 *
 | 
			
		||||
 * 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 <http://www.gnu.org/licenses/>.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include "cpuitem.h"
 | 
			
		||||
#include <QFile>
 | 
			
		||||
#include <QDebug>
 | 
			
		||||
 | 
			
		||||
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;
 | 
			
		||||
}
 | 
			
		||||
@ -0,0 +1,41 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright (C) 2021 CutefishOS Team.
 | 
			
		||||
 *
 | 
			
		||||
 * Author:     Reion Wong <reionwong@gmail.com>
 | 
			
		||||
 *
 | 
			
		||||
 * 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 <http://www.gnu.org/licenses/>.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#ifndef CPUITEM_H
 | 
			
		||||
#define CPUITEM_H
 | 
			
		||||
 | 
			
		||||
#include <QObject>
 | 
			
		||||
 | 
			
		||||
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
 | 
			
		||||
@ -0,0 +1,93 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright (C) 2021 CutefishOS Team.
 | 
			
		||||
 *
 | 
			
		||||
 * Author:     Reion Wong <reionwong@gmail.com>
 | 
			
		||||
 *
 | 
			
		||||
 * 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 <http://www.gnu.org/licenses/>.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include "cpumanagement.h"
 | 
			
		||||
#include "cpumanagementadaptor.h"
 | 
			
		||||
 | 
			
		||||
#include <QDebug>
 | 
			
		||||
#include <QDir>
 | 
			
		||||
#include <QFile>
 | 
			
		||||
 | 
			
		||||
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<CPUManagement::Mode>(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;
 | 
			
		||||
}
 | 
			
		||||
@ -0,0 +1,53 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright (C) 2021 CutefishOS Team.
 | 
			
		||||
 *
 | 
			
		||||
 * Author:     Reion Wong <reionwong@gmail.com>
 | 
			
		||||
 *
 | 
			
		||||
 * 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 <http://www.gnu.org/licenses/>.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#ifndef CPUMANAGEMENT_H
 | 
			
		||||
#define CPUMANAGEMENT_H
 | 
			
		||||
 | 
			
		||||
#include <QObject>
 | 
			
		||||
#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<CpuItem *> m_items;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#endif // CPUMANAGEMENT_H
 | 
			
		||||
@ -0,0 +1,10 @@
 | 
			
		||||
<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
 | 
			
		||||
<node>
 | 
			
		||||
  <interface name="org.cutefish.CPUManagement">
 | 
			
		||||
    <property name="mode" type="i" access="read"/>
 | 
			
		||||
 | 
			
		||||
    <method name="setMode">
 | 
			
		||||
        <arg name="value" type="i" direction="in"/>
 | 
			
		||||
    </method>
 | 
			
		||||
  </interface>
 | 
			
		||||
</node>
 | 
			
		||||
@ -0,0 +1,29 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright (C) 2021 CutefishOS Team.
 | 
			
		||||
 *
 | 
			
		||||
 * Author:     Reion Wong <reionwong@gmail.com>
 | 
			
		||||
 *
 | 
			
		||||
 * 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 <http://www.gnu.org/licenses/>.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include <QCoreApplication>
 | 
			
		||||
#include "application.h"
 | 
			
		||||
 | 
			
		||||
int main(int argc, char *argv[])
 | 
			
		||||
{
 | 
			
		||||
    QCoreApplication a(argc, argv);
 | 
			
		||||
    Application app;
 | 
			
		||||
    // a.setQuitOnLastWindowClosed(false);
 | 
			
		||||
    return a.exec();
 | 
			
		||||
}
 | 
			
		||||
					Loading…
					
					
				
		Reference in New Issue