e62fac5c4ed27be8d141d8fe501c61676b837dca
[qcpufreq] / src / mainwindow.cpp
1 /*
2  * QCPUFreq - a simple cpufreq GUI
3  * Copyright (C) 2010 Daniel Klaffenbach <daniel.klaffenbach@cs.tu-chemnitz.de>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include "mainwindow.h"
20 #include "ui_mainwindow.h"
21
22 #include <QFile>
23 #include <QMessageBox>
24 #include <QTextStream>
25 #include <QProcess>
26
27
28 #define APPNAME "QCPUFreq"
29 #define APPVERSION "0.1"
30
31 MainWindow::MainWindow(QWidget *parent) :
32     QMainWindow(parent),
33     ui(new Ui::MainWindow)
34 {
35     ui->setupUi(this);
36     refresh();
37
38         // enable auto rotation
39         setAutoRotaion();
40
41     //connect signals and slots
42     connect( ui->actionAbout, SIGNAL(triggered()), this, SLOT(about()) );
43     connect( ui->freq_adjust, SIGNAL(valueChanged(int)), this, SLOT(adjustFreq()) );
44 }
45
46 MainWindow::~MainWindow()
47 {
48     delete ui;
49 }
50
51 /**
52   * SLOT: Displays an about box
53   */
54 void MainWindow::about()
55 {
56     QMessageBox::about(this, APPNAME " " APPVERSION, "<p style=\"align:center;\">&copy; 2010 Daniel Klaffenbach</p>" );
57 }
58
59
60 /**
61   * SLOT: Adjusts the maximum CPU frequency according to the scaler
62   */
63 void MainWindow::adjustFreq()
64 {
65     int newmax = getScalingFreq( ui->freq_adjust->sliderPosition() );
66     QString max;
67     max.setNum( newmax );
68     QStringList arguments;
69 #if defined(Q_WS_MAEMO_5)
70     //on Maemo5 the set_scalingmaxfreq-Script is not in $PATH
71     arguments.append( "/opt/usr/bin/set_scalingmaxfreq" );
72 #else
73     arguments.append( "set_scalingmaxfreq" );
74 #endif
75     arguments.append( max );
76
77     //execute the scaling script
78     QProcess script;
79     script.execute( "sudo", arguments );
80
81     refresh();
82 }
83
84
85 /**
86   * Returns the current CPU frequency
87   */
88 int MainWindow::getCurFreq()
89 {
90     QString tmp = readScalingFile( "scaling_cur_freq" );
91     return tmp.toInt();
92 }
93
94 int MainWindow::getMaxFreq()
95 {
96     QString tmp = readScalingFile( "scaling_max_freq" );
97     return tmp.toInt();
98 }
99
100 int MainWindow::getMinFreq()
101 {
102     QString tmp = readScalingFile( "scaling_min_freq" );
103     return tmp.toInt();
104 }
105
106
107
108 /**
109   * Returns the CPU frequency for the specified scaling step
110   */
111 int MainWindow::getScalingFreq(int step)
112 {
113     QString tmp = readScalingFile( "scaling_available_frequencies" );
114     QStringList freqs = tmp.split( " " );
115     step = step - 1;
116     if ( step < 0 )
117          step = 0;
118     if ( step > getScalingSteps() )
119         step = getScalingSteps();
120
121     tmp = freqs[ step ];
122     return tmp.toInt();
123 }
124
125
126 QString MainWindow::getScalingGovernor()
127 {
128     return readScalingFile( "scaling_governor" );
129 }
130
131 /**
132   * Returns the amount of available scaling steps.
133   */
134 int MainWindow::getScalingSteps()
135 {
136     QString tmp = readScalingFile( "scaling_available_frequencies" );
137     QStringList freqs = tmp.split( " " );
138     return (freqs.size() - 1);
139 }
140
141
142 /**
143   * Returns the scaling step for the specified frequency.
144   */
145 int MainWindow::getScalingStep( int freq )
146 {
147     for( int i = 1; i <= getScalingSteps(); ++i ) {
148            if ( getScalingFreq(i) == freq )
149                 return i;
150     }
151
152     return 1;
153 }
154
155 QString MainWindow::readScalingFile(QString scaling_file)
156 {
157     QFile file( "/sys/devices/system/cpu/cpu0/cpufreq/"+scaling_file );
158
159     //open the file
160     if ( !file.exists() || !file.open( QIODevice::ReadOnly ) ) {
161         QMessageBox::critical(this, tr("QCPUFreq"), tr("Could not get information from /sys!"));
162         return "";
163     }
164
165     //read the file
166     QTextStream in( &file );
167     QString txt = in.readLine();
168
169     return txt;
170 }
171
172 void MainWindow::refresh()
173 {
174     //get the current frequency and calculate the MHz value
175     int freq = ( getMinFreq() / 1000 );
176     QString display;
177     display.setNum( freq );
178     display.append( " MHz" );
179     ui->freq_min->setText( display );
180
181     //do the same thing for the maximum frequency
182     freq = ( getMaxFreq() / 1000 );
183     display.setNum( freq );
184     display.append( " MHz" );
185     ui->freq_max->setText( display );
186
187     //display the current governor
188     ui->freq_governor->setText( getScalingGovernor() );
189
190     //display.setNum( getScalingFreq(1) );
191     //ui->freq_max->setText( display );
192     ui->freq_adjust->setMinimum( 1 );
193     ui->freq_adjust->setMaximum( getScalingSteps() );
194     ui->freq_adjust->setInvertedAppearance( true );
195     ui->freq_adjust->setSliderPosition( getScalingStep(getMaxFreq()) );
196 }
197
198 void MainWindow::setAutoRotaion()
199 {
200 #if defined(Q_WS_MAEMO_5)
201     setAttribute(Qt::WA_Maemo5AutoOrientation, true);
202     //setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
203 #endif
204 }