Display current CPU temperature
[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 #include <QDesktopWidget>
27
28
29 #define APPNAME "QCPUFreq"
30 #define APPVERSION "0.1"
31
32 MainWindow::MainWindow(QWidget *parent) :
33     QMainWindow(parent),
34     ui(new Ui::MainWindow)
35 {
36     ui->setupUi(this);
37     refresh();
38
39     // enable auto rotation
40     setAutoRotaion();
41
42     //create a QGraphicsScene for the little chip icon
43     scene = new QGraphicsScene();
44     orientationChanged();
45
46     //connect signals and slots
47     connect( ui->actionAbout, SIGNAL(triggered()), this, SLOT(about()) );
48     connect( ui->freq_adjust, SIGNAL(valueChanged(int)), this, SLOT(adjustFreq()) );
49     connect(QApplication::desktop(), SIGNAL(resized(int)), this, SLOT(orientationChanged()));
50     connect( ui->sr_btn, SIGNAL(clicked()), this, SLOT(setSmartReflex()) );
51
52 }
53
54 MainWindow::~MainWindow()
55 {
56     delete ui;
57     delete scene;
58 }
59
60
61 /**
62   * SLOT: Displays an about box
63   */
64 void MainWindow::about()
65 {
66     QMessageBox::about(this, APPNAME " " APPVERSION, "<p style=\"align:center;\">&copy; 2010 Daniel Klaffenbach</p>" );
67 }
68
69
70 /**
71   * SLOT: Adjusts the maximum CPU frequency according to the scaler
72   */
73 void MainWindow::adjustFreq()
74 {
75     int newmax = getScalingFreq( ui->freq_adjust->sliderPosition() );
76     QString max;
77     max.setNum( newmax );
78     QStringList arguments;
79 #if defined(Q_WS_MAEMO_5)
80     //on Maemo5 the set_scalingmaxfreq-Script is not in $PATH
81     arguments.append( "/opt/usr/bin/set_scalingmaxfreq" );
82 #else
83     arguments.append( "set_scalingmaxfreq" );
84 #endif
85     arguments.append( max );
86
87     //execute the scaling script
88     QProcess script;
89     script.execute( "sudo", arguments );
90
91     refresh();
92 }
93
94
95 /**
96   * Returns the current CPU temperature
97   */
98 QString MainWindow::getCPUTemp()
99 {
100 #if defined(Q_WS_MAEMO_5)
101     return readSysFile( "devices/platform/omap34xx_temp/temp1_input_raw" );
102 #endif
103     return "Unknown";
104 }
105
106
107 /**
108   * Returns the maximum CPU frequency
109   */
110 int MainWindow::getMaxFreq()
111 {
112     QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_max_freq" );
113     return tmp.toInt();
114 }
115
116
117 /**
118   * Returns the minimum CPU frequency
119   */
120 int MainWindow::getMinFreq()
121 {
122     QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_min_freq" );
123     return tmp.toInt();
124 }
125
126
127 /**
128   * Returns the CPU frequency for the specified scaling step
129   */
130 int MainWindow::getScalingFreq(int step)
131 {
132     QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies" );
133     QStringList freqs = tmp.split( " " );
134     step = step - 1;
135     if ( step < 0 )
136          step = 0;
137     if ( step > getScalingSteps() )
138         step = getScalingSteps();
139
140     tmp = freqs[ step ];
141     return tmp.toInt();
142 }
143
144
145 /**
146   * Returns the name of the current CPU frequency scaling governor
147   *
148   * \return     name of governor
149   */
150 QString MainWindow::getScalingGovernor()
151 {
152     return readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_governor" );
153 }
154
155
156 /**
157   * Returns the amount of available scaling steps.
158   */
159 int MainWindow::getScalingSteps()
160 {
161     QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies" );
162     QStringList freqs = tmp.split( " " );
163     return (freqs.size() - 1);
164 }
165
166
167 /**
168   * Returns the scaling step for the specified frequency.
169   */
170 int MainWindow::getScalingStep( int freq )
171 {
172     for( int i = 1; i <= getScalingSteps(); ++i ) {
173            if ( getScalingFreq(i) == freq )
174                 return i;
175     }
176
177     return 1;
178 }
179
180
181 /**
182   * Returns the SmartReflex(tm) state
183   *
184   * \return     0|1
185   */
186 int MainWindow::getSmartReflexState()
187 {
188 //SmartReflex is only supprted on Maemo5
189 #if defined(Q_WS_MAEMO_5)
190     QString tmp = readSysFile( "power/sr_vdd1_autocomp" );
191
192     if ( tmp == "1" )
193         return 1;
194     else
195         return 0;
196 #else
197     return 0;
198 #endif
199 }
200
201
202 /**
203   * Reads any file in /sys/
204   *
205   * \param      sys_file : full path to sys file - omit "/sys/"
206   * \return     content of sys file
207   */
208 QString MainWindow::readSysFile(QString sys_file)
209 {
210     QFile file( "/sys/"+sys_file );
211
212     //open the file
213     if ( !file.exists() || !file.open( QIODevice::ReadOnly ) ) {
214         QMessageBox::critical(this, tr("QCPUFreq"), tr("Could not get information from /sys!"));
215         return "";
216     }
217
218     //read the file
219     QTextStream in( &file );
220     QString txt = in.readLine();
221
222     return txt;
223 }
224
225
226 /**
227   * Refreshes all of the values to display
228   */
229 void MainWindow::refresh()
230 {
231     //get the current frequency and calculate the MHz value
232     int freq = ( getMinFreq() / 1000 );
233     QString display;
234     display.setNum( freq );
235     display.append( " MHz" );
236     ui->freq_min->setText( display );
237
238     //do the same thing for the maximum frequency
239     freq = ( getMaxFreq() / 1000 );
240     display.setNum( freq );
241     display.append( " MHz" );
242     ui->freq_max->setText( display );
243
244     //display the current governor
245     ui->freq_governor->setText( getScalingGovernor() );
246
247     //display current temperature
248     ui->cpu_temp->setText( getCPUTemp() );
249
250     //smart reflex button
251     if ( getSmartReflexState() == 1 ) {
252         ui->sr_btn->setDown( true );
253         ui->sr_btn->setText( tr( "Enabled" ) );
254     } else {
255         ui->sr_btn->setDown( false );
256         ui->sr_btn->setText( tr( "Disabled" ) );
257     }
258
259
260     //display frequency slider
261     ui->freq_adjust->setMinimum( 1 );
262     ui->freq_adjust->setMaximum( getScalingSteps() );
263     ui->freq_adjust->setInvertedAppearance( true );
264     ui->freq_adjust->setSliderPosition( getScalingStep(getMaxFreq()) );
265 }
266
267
268 /**
269   * Repaints part of the GUI after the device was rotated
270   */
271 void MainWindow::orientationChanged()
272 {
273     QPixmap image;
274
275     //check whether we are using portrait or landscape mode
276     if ( usePortrait() ) {
277         //in portrait mode we want to display the large image
278         image.load( ":/img/chip256" );
279         this->scene->clear();
280         this->scene->addPixmap(  image  );
281
282         ui->graphicsPortrait->setScene( this->scene );
283         ui->graphicsPortrait->setMaximumSize( 256, 256 );
284         ui->graphicsLandscape->setMaximumSize( 0, 0 );
285     } else {
286         image.load( ":/img/chip128" );
287         this->scene->clear();
288         this->scene->addPixmap(  image  );
289
290         ui->graphicsLandscape->setScene( this->scene );
291         ui->graphicsLandscape->setMaximumSize( 128, 128 );
292         ui->graphicsPortrait->setMaximumSize( 0, 0 );
293     }
294 }
295
296
297 /**
298   * Enables the auto-rotation feature of Maemo5 devices
299   */
300 void MainWindow::setAutoRotaion()
301 {
302 #if defined(Q_WS_MAEMO_5)
303     setAttribute(Qt::WA_Maemo5AutoOrientation, true);
304 #endif
305 }
306
307
308 /**
309   * SLOT: Enables or disables Smart Reflex(tm) after pressing sr_btn
310   */
311 void MainWindow::setSmartReflex()
312 {
313 //SmartReflex is only supprted on Maemo5
314 #if defined(Q_WS_MAEMO_5)
315     QStringList arguments;
316     arguments.append( "/opt/usr/bin/set_sr" );
317
318     if ( getSmartReflexState() == 1 )
319         arguments.append( "off" );
320     else
321         arguments.append( "on" );
322
323     //execute the sr script script
324     QProcess script;
325     script.execute( "sudo", arguments );
326
327 #endif
328     //refresh the UI
329     refresh();
330 }
331
332
333 /**
334   * Returns true when the device is in portrait mode
335   */
336 bool MainWindow::usePortrait()
337 {
338     QRect screenGeometry = QApplication::desktop()->screenGeometry();
339     if (screenGeometry.width() > screenGeometry.height())
340         return false;
341     else
342         return true;
343 }