-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcpuset.cpp
More file actions
144 lines (126 loc) · 4.82 KB
/
cpuset.cpp
File metadata and controls
144 lines (126 loc) · 4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/***************************************************************************
* Copyright (C) 2013 by Tomáš Frýda <fryda.tomas@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 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 "cpuset.h"
#include <QPainter>
#include <QFontMetrics>
#include <QSizeF>
#include <QProcess>
#include <QTimer>
#include <KLocale>
#include <plasma/theme.h>
#include <mypushbutton.h>
#include <plasma/widgets/slider.h>
CPUset::CPUset ( QObject *parent, const QVariantList &args )
: Plasma::PopupApplet ( parent, args ),
m_freq ( 0 ),
m_gov ( 0 ),
m_widget ( 0 )
{
setPopupIcon ( KIcon ( "chronometer" ) );
setBackgroundHints ( DefaultBackground );
setHasConfigurationInterface ( false );
//resize(200,200);
}
CPUset::~CPUset()
{
if ( hasFailedToLaunch() )
{
// Do some cleanup here
}
else
{
// Save settings
}
}
void CPUset::init()
{
m_widget = new QGraphicsWidget ( this );
m_widget->setMinimumSize ( 260,280 );
MyPushButton * pb;
QGraphicsLinearLayout *layout = new QGraphicsLinearLayout ( m_widget );
layout->setOrientation ( Qt::Vertical );
m_freq = new Plasma::Label ( m_widget );
m_freq->setText ( "<b>Frequency:</b> " );
layout->addItem ( m_freq );
QProcess proc;
proc.start ( "sh", QStringList() << "-c" << "LANG=C cpufreq-info | grep 'available frequency steps' | head -1 | cut -d\\: -f2 | tr -d ' ' | tr ',' '\\n' | sort -u | paste -s -d ','" );
if ( !proc.waitForFinished() )
return;
m_frequencies = proc.readAll().simplified().split ( ',' );
Plasma::Slider * sld = new Plasma::Slider(this);
sld->setRange(0, m_frequencies.count()-1);
sld->setOrientation(Qt::Horizontal);
connect(sld, SIGNAL(valueChanged(int)), this, SLOT(setFrequency(int)));
layout->addItem(sld);
m_gov = new Plasma::Label ( m_widget );
m_gov->setText ( "<b>Governor:</b> " );
layout->addItem ( m_gov );
QProcess proc2;
proc2.start ( "cpufreq-info", QStringList() <<"-g" );
if ( !proc2.waitForFinished() )
return;
QList<QByteArray> lst;
lst = proc2.readAll().simplified().split ( ' ' );
foreach ( const QString &s, lst )
{
pb = new MyPushButton ( m_widget );
pb->setText ( s );
connect ( pb, SIGNAL ( pressed ( QString ) ), this, SLOT ( setGovernor ( QString ) ) );
layout->addItem ( pb );
}
m_widget->setLayout ( layout );
QTimer *timer = new QTimer ( this );
connect ( timer, SIGNAL ( timeout() ), this, SLOT ( Refresh() ) );
timer->start ( 1000 );
}
void CPUset::setFrequency ( int freq )
{
if ( system ( ( "cpufreq-set -r -f " + QString(m_frequencies.at(freq)) ).toAscii() ) )
m_freq->setText ( "<b>Frequency:</b> Error!" );
else
Refresh();
}
void CPUset::setGovernor ( QString gov )
{
if ( system ( ( "cpufreq-set -r -g " + gov ).toAscii() ) )
m_gov->setText ( "<b>Governor:</b> Error!" );
else
Refresh();
}
QGraphicsWidget* CPUset::graphicsWidget()
{
Refresh ( true );
return m_widget;
}
void CPUset::Refresh ( bool force )
{
if ( isPopupShowing() || force )
{
//refresh
QProcess gov;
gov.start ( "cpufreq-info", QStringList() << "-p" );
if ( !gov.waitForFinished() ) return;
m_gov->setText ( QString ( "<b>Governor:</b> %1" ).arg ( QString ( gov.readAll().simplified().split ( ' ' ).back() ) ) );
QProcess freq;
freq.start ( "cpufreq-info", QStringList() << "-f" );
if ( !freq.waitForFinished() ) return;
m_freq->setText ( QString ( "<b>Frequency:</b> %1" ).arg ( QString ( freq.readAll().simplified() ) ) );
}
}
#include "cpuset.moc"