From da7847adb43726079c7a4be1f06acbebe0bdde57 Mon Sep 17 00:00:00 2001 From: tpearson Date: Wed, 10 Feb 2010 19:54:13 +0000 Subject: Added old abandoned KDE3 version of Kima git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/applications/kima@1088422 283d02a7-25f6-0310-bc7c-ecb5cbfe19da --- src/cpufreqd/Makefile.am | 8 ++ src/cpufreqd/cpufreqd.cpp | 156 ++++++++++++++++++++++++++++++++++++ src/cpufreqd/cpufreqd.h | 65 +++++++++++++++ src/cpufreqd/cpufreqdconnection.cpp | 125 +++++++++++++++++++++++++++++ src/cpufreqd/cpufreqdconnection.h | 69 ++++++++++++++++ src/cpufreqd/cpufreqdprofile.cpp | 67 ++++++++++++++++ src/cpufreqd/cpufreqdprofile.h | 48 +++++++++++ 7 files changed, 538 insertions(+) create mode 100644 src/cpufreqd/Makefile.am create mode 100644 src/cpufreqd/cpufreqd.cpp create mode 100644 src/cpufreqd/cpufreqd.h create mode 100644 src/cpufreqd/cpufreqdconnection.cpp create mode 100644 src/cpufreqd/cpufreqdconnection.h create mode 100644 src/cpufreqd/cpufreqdprofile.cpp create mode 100644 src/cpufreqd/cpufreqdprofile.h (limited to 'src/cpufreqd') diff --git a/src/cpufreqd/Makefile.am b/src/cpufreqd/Makefile.am new file mode 100644 index 0000000..3b81871 --- /dev/null +++ b/src/cpufreqd/Makefile.am @@ -0,0 +1,8 @@ +INCLUDES = -I$(top_srcdir)/src/cpufreqd -I$(top_srcdir)/src/sources $(all_includes) +METASOURCES = AUTO +libcpufreqd_la_LDFLAGS = $(all_libraries) +noinst_LTLIBRARIES = libcpufreqd.la +libcpufreqd_la_SOURCES = cpufreqd.cpp cpufreqdconnection.cpp \ + cpufreqdprofile.cpp +noinst_HEADERS = cpufreqd.h cpufreqdconnection.h cpufreqdprofile.h cpufreqd.h \ + cpufreqdconnection.h cpufreqdprofile.h diff --git a/src/cpufreqd/cpufreqd.cpp b/src/cpufreqd/cpufreqd.cpp new file mode 100644 index 0000000..833db29 --- /dev/null +++ b/src/cpufreqd/cpufreqd.cpp @@ -0,0 +1,156 @@ +/*************************************************************************** + * Copyright (C) 2006 by Valentine Sinitsyn * + * e_val@inbox.ru * + * * + * 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 2 of the License, or * + * (at your option) 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, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#include "cpufreqd.h" +#include +#include +#include +#include + +#define CHUNK_SIZE 4096 + +/** + * cpufreqd control module main class + *@author: Valentine Sinitsyn (Valentine.Sinitsyn@usu.ru) + *@todo: We assume that cpufreqd is in dynamic mode when applet is started because as for now it has + * no means to detect mode. This could lead to inconsistency if, ex. applet is restarted after making + * some changes to cpufreqd state. + */ + +CPUFreqd::CPUFreqd() { + m_menu = new QPopupMenu(); + QObject::connect(m_menu, SIGNAL(aboutToShow()), this, SLOT(updateMenu())); + + m_dynamic = new QAction(i18n("Select dynamically"), QKeySequence(), this); + QObject::connect(m_dynamic, SIGNAL(activated()), this, SLOT(setDynamic())); + m_dynamic->setToggleAction(true); + m_dynamic->setOn(true); + + m_items = new QActionGroup(this); + m_items->setExclusive(true); + + m_actions = new QPtrList(); + m_actions->setAutoDelete(true); + + m_mapper = new QSignalMapper(this); + QObject::connect(m_mapper, SIGNAL(mapped(int)), this, SLOT(setProfile(int))); +} + +CPUFreqd::~CPUFreqd() { +} + +bool CPUFreqd::enabled() const { + return m_conn.available(); +} + +QValueVector & CPUFreqd::getProfiles(bool reconnect) { + char chunk[CHUNK_SIZE]; + QString buffer; + + if (!m_profiles.empty()) + m_profiles.clear(); + + if (!m_conn.open()) + if (reconnect) { + m_dynamic->setOn(m_conn.lookup()); + return getProfiles(false); + } else { + return m_profiles; + } + + if (!m_conn.write(CMD_LIST_PROFILES, 0)) + return m_profiles; + + int bytes = 0; + while ( (bytes = m_conn.read(chunk, CHUNK_SIZE - 1)) ) { + chunk[bytes] = '\0'; + buffer.append(chunk); + } + + QStringList profiles = QStringList::split("\n", buffer); + for (QStringList::Iterator it = profiles.begin(); it != profiles.end(); it++) + m_profiles.push_back( CPUFreqdProfile(*it) ); + + m_conn.close(); + + return m_profiles; +} + +QPopupMenu* CPUFreqd::menu() { + return m_menu; +} + +void CPUFreqd::updateMenu() { + QAction *cur; + + m_menu->clear(); + m_actions->clear(); + + getProfiles(true); + if (!m_profiles.isEmpty()) { + m_dynamic->addTo(m_menu); + m_menu->insertSeparator(); + + for (unsigned int i = 0; i < m_profiles.count(); i++) + if (m_profiles[i].isValid()) { + cur = new QAction(m_profiles[i].name(), QKeySequence(), m_items); + QObject::connect(cur, SIGNAL(activated()), m_mapper, SLOT(map())); + cur->setToggleAction(true); + cur->setOn(m_profiles[i].active()); + + m_mapper->setMapping(cur, i+1); + m_actions->append(cur); + } + m_items->addTo(m_menu); + } else { + int errmsg = m_menu->insertItem(i18n("Can't talk to cpufreqd"), 0, 0); + m_menu->setItemEnabled(errmsg, false); + } +} + +void CPUFreqd::setManual() { + setMode(ARG_MANUAL); + m_dynamic->setOn(false); +} + +void CPUFreqd::setDynamic() { + setMode(ARG_DYNAMIC); + m_dynamic->setOn(true); +} + +void CPUFreqd::setProfile(int id) { + //@fixme: make it return bool and bail out if it falis + if (m_dynamic->isOn()) + setManual(); + + if (!m_conn.open()) + return; + + m_conn.write(CMD_SET_PROFILE, (uint32_t)id); + m_conn.close(); +} + +void CPUFreqd::setMode(uint32_t mode) { + if (!m_conn.open()) + return; + + m_conn.write(CMD_SET_MODE, mode); + m_conn.close(); +} diff --git a/src/cpufreqd/cpufreqd.h b/src/cpufreqd/cpufreqd.h new file mode 100644 index 0000000..42e4b0e --- /dev/null +++ b/src/cpufreqd/cpufreqd.h @@ -0,0 +1,65 @@ +/*************************************************************************** + * Copyright (C) 2006 by Valentine Sinitsyn * + * e_val@inbox.ru * + * * + * 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 2 of the License, or * + * (at your option) 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, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef CPUFREQD_H +#define CPUFREQD_H + +#include "cpufreqdconnection.h" +#include "cpufreqdprofile.h" +#include +#include +#include +#include +#include +#include +#include +#include + +class CPUFreqd: public QObject { + Q_OBJECT + + public: + CPUFreqd(); + virtual ~CPUFreqd(); + + bool enabled() const; + QValueVector & getProfiles(bool reconnect = false); + QPopupMenu* menu(); + + public slots: + void setManual(); + void setDynamic(); + void setProfile(int id); + + private: + CPUFreqdConnection m_conn; + QValueVector m_profiles; + QPopupMenu *m_menu; + QSignalMapper *m_mapper; + QAction* m_dynamic; + QActionGroup* m_items; + QPtrList* m_actions; + + void setMode(uint32_t mode); + + private slots: + void updateMenu(); +}; + +#endif diff --git a/src/cpufreqd/cpufreqdconnection.cpp b/src/cpufreqd/cpufreqdconnection.cpp new file mode 100644 index 0000000..4c9ee03 --- /dev/null +++ b/src/cpufreqd/cpufreqdconnection.cpp @@ -0,0 +1,125 @@ +/*************************************************************************** + * Copyright (C) 2006 by Valentine Sinitsyn * + * e_val@inbox.ru * + * * + * 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 2 of the License, or * + * (at your option) 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, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#include "cpufreqdconnection.h" + +#include + +#include +#include +#include +#include +#include +#include +#include + +/* Excerpt from cpufreqd_remote.h (cpufreqd-2.0.0) + * + * Copyright (C) 2005 Mattia Dongili + * Hrvoje Zeba + * + * Format: + * it is an uint32_t used as bitmask + * + * 31-16 15-0 + * + * + * The response may be longer than a single line and is + * terminated by the RESPONSE_END (see defines). + */ + +#define CMD_SHIFT 16 +#define ARG_MASK 0x0000ffff + +#define REMOTE_CMD(c) (c >> CMD_SHIFT) +#define REMOTE_ARG(c) (c & ARG_MASK) +#define MAKE_COMMAND(cmd, arg) ((cmd << CMD_SHIFT) | arg) +#define INVALID_CMD 0xffffffff + +/** + * This class encapsulates cpufreqd connection performed via Unix sockets + *@author: Valentine Sinitsyn (Valentine.Sinitsyn@usu.ru) + */ + +CPUFreqdConnection::CPUFreqdConnection(): m_fd(-1) { + lookup(); +} + + +CPUFreqdConnection::~CPUFreqdConnection() { +} + +bool CPUFreqdConnection::open() { + struct sockaddr_un sck; + + // socket name is too long - it can't be copied to to sun_path + if (m_socket.isEmpty() || m_socket.length() >= 108) + return false; + + if (m_fd > 0) + close(); + + if ((m_fd = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) + return false; + + sck.sun_family = AF_UNIX; + //It is guaranted that socket.ascii() is shorter than 108 bytes, but we want to be sure + strncpy(sck.sun_path, m_socket.ascii(), 108); + + if (::connect(m_fd, (struct sockaddr *)&sck, sizeof(sck)) == -1) + return false; + + return true; +} + +ssize_t CPUFreqdConnection::read(void *buf, size_t size) { + return ::read(m_fd, buf, size); +} + +bool CPUFreqdConnection::write(uint32_t cmd, uint32_t arg) { + uint32_t command; + + command = MAKE_COMMAND(cmd, arg); + return (::write(m_fd, &command, sizeof(command)) == (int)sizeof(command)); +} + +void CPUFreqdConnection::close() { + ::close(m_fd); + m_fd = -1; +} + +bool CPUFreqdConnection::available() const { + return !m_socket.isEmpty(); +} + +bool CPUFreqdConnection::lookup() { + QString candidate; + QDir tmp("/tmp", "cpufreqd-*", QDir::Time, QDir::Dirs); + + if (tmp.count()) + candidate = "/tmp/" + tmp[0] + "/cpufreqd"; + + if (candidate != m_socket) { + m_socket = candidate; + return true; + } + + return false; +} diff --git a/src/cpufreqd/cpufreqdconnection.h b/src/cpufreqd/cpufreqdconnection.h new file mode 100644 index 0000000..4f15ad8 --- /dev/null +++ b/src/cpufreqd/cpufreqdconnection.h @@ -0,0 +1,69 @@ +/*************************************************************************** + * Copyright (C) 2006 by Valentine Sinitsyn * + * e_val@inbox.ru * + * * + * 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 2 of the License, or * + * (at your option) 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, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef CPUFREQDCONNECTION_H +#define CPUFREQDCONNECTION_H + +#include +#include + +/* Excerpt from cpufreqd_remote.h (cpufreqd-2.0.0) + * + * Copyright (C) 2005 Mattia Dongili + * Hrvoje Zeba + * + * Format: + * it is an uint32_t used as bitmask + * + * 31-16 15-0 + * + * + * The response may be longer than a single line and is + * terminated by the RESPONSE_END (see defines). + */ + +#define CMD_UPDATE_STATE 1 /* no arguments */ +#define CMD_SET_PROFILE 2 /* */ +#define CMD_LIST_PROFILES 3 /* no arguments */ +#define CMD_SET_RULE 4 /* */ +#define CMD_LIST_RULES 5 /* no arguments */ +#define CMD_SET_MODE 6 /* */ + +#define ARG_DYNAMIC (1) +#define ARG_MANUAL (2) + +class CPUFreqdConnection{ + public: + CPUFreqdConnection(); + ~CPUFreqdConnection(); + + bool open(); + ssize_t read(void *buf, size_t size); + bool write(uint32_t cmd, uint32_t arg); + void close(); + + bool available() const; + bool lookup(); + + private: + QString m_socket; + int m_fd; +}; + +#endif diff --git a/src/cpufreqd/cpufreqdprofile.cpp b/src/cpufreqd/cpufreqdprofile.cpp new file mode 100644 index 0000000..96d7fe6 --- /dev/null +++ b/src/cpufreqd/cpufreqdprofile.cpp @@ -0,0 +1,67 @@ +/*************************************************************************** + * Copyright (C) 2006 by Valentine Sinitsyn * + * e_val@inbox.ru * + * * + * 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 2 of the License, or * + * (at your option) 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, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#include "cpufreqdprofile.h" +#include + +/** + * This class encapsulates cpufreqd profile data: name, frequencies and governors + *@author: Valentine Sinitsyn (Valentine.Sinitsyn@usu.ru) + */ + +CPUFreqdProfile::CPUFreqdProfile(QString & descr) { + QStringList fields = QStringList::split("/", descr); + + //@fixme: if name contains a slash, the results will be incorrect + if (fields.count() == 5) { + m_active = fields[0].toInt(); + m_name = fields[1]; + m_minfreq = fields[2].toInt(); + m_maxfreq = fields[3].toInt(); + m_policy = fields[4]; + } +} + +CPUFreqdProfile::~ CPUFreqdProfile() { +} + +bool CPUFreqdProfile::isValid() const { + return (!m_name.isEmpty() && !m_policy.isEmpty() && m_minfreq <= m_maxfreq); +} + +bool CPUFreqdProfile::active() const { + return (m_active == 1); +} + +const QString & CPUFreqdProfile::name() const { + return m_name; +} + +int CPUFreqdProfile::minfreq() const { + return m_minfreq; +} + +int CPUFreqdProfile::maxfreq() const { + return m_maxfreq; +} + +const QString & CPUFreqdProfile::policy() const { + return m_policy; +} diff --git a/src/cpufreqd/cpufreqdprofile.h b/src/cpufreqd/cpufreqdprofile.h new file mode 100644 index 0000000..f57e36d --- /dev/null +++ b/src/cpufreqd/cpufreqdprofile.h @@ -0,0 +1,48 @@ +/*************************************************************************** + * Copyright (C) 2006 by Valentine Sinitsyn * + * e_val@inbox.ru * + * * + * 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 2 of the License, or * + * (at your option) 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, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef CPUFREQDPROFILE_H +#define CPUFREQDPROFILE_H + +#include + +class CPUFreqdProfile { + public: + CPUFreqdProfile() {}; + CPUFreqdProfile(QString & descr); + virtual ~CPUFreqdProfile(); + + bool isValid() const; + + bool active() const; + const QString & name() const; + int minfreq() const; + int maxfreq() const; + const QString & policy() const; + + private: + int m_active; + QString m_name; + int m_id; + int m_minfreq; + int m_maxfreq; + QString m_policy; +}; + +#endif -- cgit v1.2.1