Do not allow overclocking per default.
[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 <QDesktopWidget>
26 #if defined(Q_WS_MAEMO_5)
27     #include <QMaemo5InformationBox>
28 #endif
29
30 #define APPNAME "QCPUFreq"
31 #define APPVERSION "0.3.2"
32
33 MainWindow::MainWindow(QWidget *parent) :
34     QMainWindow(parent),
35     ui(new Ui::MainWindow),
36     //do not allow overclocking per default
37     allowOverclocking(false),
38     //create helper process
39     helperProcess( this ),
40     //create a new, stackable help window
41     helpWindow( this ),
42     //set minFreq to 0
43     minFreq(0),
44     //create UI refresh timer
45     refreshTimer( this ),
46     //create a QGraphicsScene for the little chip icon
47     scene( this ),
48     //show errors about the sudo setup only once
49     showSudoError( true )
50 {
51     //this is a stacked window on Maemo 5
52     #if defined(Q_WS_MAEMO_5)
53         setAttribute(Qt::WA_Maemo5StackedWindow);
54     #endif
55
56     ui->setupUi(this);
57
58     refresh();
59
60     // enable auto rotation
61     setAutoRotation();
62
63     //initialize orientation
64     orientationChanged();
65
66     //refresh UI every 10 seconds
67     refreshTimer.start( 10000 );
68
69     // initialize stackable help window
70     #if defined(Q_WS_MAEMO_5)
71         helpWindow.setAttribute(Qt::WA_Maemo5StackedWindow);
72     #endif
73     helpWindow.setWindowFlags( windowFlags() | Qt::Window );
74
75     //connect signals and slots
76     connect(ui->actionHelp, SIGNAL(triggered()), this, SLOT(showHelp()));
77     connect( ui->actionAbout, SIGNAL(triggered()), this, SLOT(about()) );
78     connect( ui->freq_adjust, SIGNAL(valueChanged(int)), this, SLOT(adjustFreq()) );
79     connect(QApplication::desktop(), SIGNAL(resized(int)), this, SLOT(orientationChanged()));
80     connect(ui->sr_box, SIGNAL(clicked()), this, SLOT(setSmartReflex()));
81     connect(&refreshTimer, SIGNAL(timeout()), this, SLOT(refresh()));
82     connect(ui->actionOverclocking, SIGNAL(toggled(bool)), this, SLOT(setOverclocking()));
83
84 }
85
86 MainWindow::~MainWindow()
87 {
88     delete ui;
89 }
90
91
92 /**
93   * SLOT: Displays an about box
94   */
95 void MainWindow::about()
96 {
97     QMessageBox::about(this, APPNAME " " APPVERSION, "<p style=\"align:center;\">&copy; 2010 Daniel Klaffenbach</p>" );
98     refresh();
99 }
100
101
102 /**
103   * SLOT: Adjusts the maximum CPU frequency according to the scaler
104   */
105 void MainWindow::adjustFreq()
106 {
107     int newmax = getScalingFreq( ui->freq_adjust->sliderPosition() );
108     QString max;
109
110     //maxfreq should not be smaller than minfreq, because we do not want to decrease minfreq
111     if (newmax < getMinFreq())
112         newmax = getMinFreq();
113
114     max.setNum( newmax );
115
116     //check for overclocking
117     #if defined(Q_WS_MAEMO_5)
118     if (this->allowOverclocking == false && newmax > 600000) {
119         QMaemo5InformationBox::information(this, tr( "You need to enable overclocking for setting frequencies above 600MHz!"), 0);
120         refresh();
121         return;
122     }
123     #endif
124
125     callHelper( "set_maxfreq", max );
126
127     refresh();
128 }
129
130
131 /**
132   * Calls the QCPUFreq helper script with "sudo action param"
133   *
134   * @param  action : the action of the helper script
135   * @param  param : the parameter for the action
136   * @return exit code
137   */
138 int MainWindow::callHelper(QString action, QString param)
139 {
140     QStringList arguments;
141
142     #if defined(Q_WS_MAEMO_5)
143     //On Maemo 5 the helper script resides in /opt/usr/bin, which is usually not in $PATH
144     arguments.append( "/opt/usr/bin/QCPUFreq.helper" );
145     #else
146     arguments.append( "QCPUFreq.helper" );
147     #endif
148
149     arguments.append( action );
150     arguments.append( param );
151
152     helperProcess.start( "sudo", arguments, QIODevice::NotOpen );
153
154     if ( showSudoError && !helperProcess.waitForFinished( 400 )) {
155         //do not show this error again
156         showSudoError = false;
157         QMessageBox::critical(this, tr("QCPUFreq"), tr("There seems to be a problem with your sudo setup!"));
158     }
159
160     return helperProcess.exitCode();
161 }
162
163
164 /**
165   * Returns the current CPU temperature
166   */
167 QString MainWindow::getCPUTemp()
168 {
169 #if defined(Q_WS_MAEMO_5)
170     QFile file( "/sys/class/power_supply/bq27200-0/temp" );
171
172     //check if we can read a more accurate temperature (only for power kernel)
173     if (file.exists())
174         return QString( readSysFile( "class/power_supply/bq27200-0/temp" ) + " " + QString::fromUtf8("\302\260") + "C" );
175     else {
176         /*
177           We actually only need to read the raw temperature, but it appears that by also reading temp1_input
178           the raw temperature (temp1_input_raw) is being updated more frequently.
179         */
180         readSysFile( "devices/platform/omap34xx_temp/temp1_input" );
181
182         //read the current system temperature
183         QString tstring = readSysFile( "devices/platform/omap34xx_temp/temp1_input_raw" );
184         if (tstring == "0")
185             return tr( "Unknown" );
186
187         //convert it to an integer and calculate the approx. temperature from the raw value
188         int tint = tstring.toInt();
189         tint = ( 0.65 * tint );
190         tstring.setNum(tint);
191         return QString( tstring + " " + QString::fromUtf8("\302\260") + "C" );
192     }
193 #endif
194     return tr( "Unknown" );
195 }
196
197
198 /**
199   * Returns the maximum CPU frequency
200   */
201 int MainWindow::getMaxFreq()
202 {
203     QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_max_freq" );
204     return tmp.toInt();
205 }
206
207
208 /**
209   * Returns the minimum CPU frequency
210   */
211 int MainWindow::getMinFreq()
212 {
213     if (this->minFreq == 0) {
214         QString min = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_min_freq" );
215         //check if avoid file exists (only on power kernel)
216         QFile file( "/sys/devices/system/cpu/cpu0/cpufreq/ondemand/avoid_frequencies" );
217         if (file.exists()) {
218             QString avoid = readSysFile( "devices/system/cpu/cpu0/cpufreq/ondemand/avoid_frequencies" );
219             QStringList avoidList = avoid.split( " " );
220
221             //check if min is in avoid_frequencies
222             for (int i = getScalingStep( min.toInt() ); i>0; --i) {
223                 if (!avoidList.contains(min.setNum( getScalingFreq(i) ))) {
224                     this->minFreq = min.toInt();
225                     return this->minFreq;
226                 }
227             }
228
229             //should not happen at all
230             this->minFreq = 125000;
231             return this->minFreq;
232         } else {
233             this->minFreq = min.toInt();
234             return this->minFreq;
235         }
236     } else {
237         return this->minFreq;
238     }
239 }
240
241
242 /**
243   * Returns the CPU frequency for the specified scaling step
244   */
245 int MainWindow::getScalingFreq(int step)
246 {
247     QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies" );
248     QStringList freqs = tmp.split( " " );
249     step = step - 1;
250     if ( step < 0 )
251          step = 0;
252     if ( step > getScalingSteps() )
253         step = getScalingSteps();
254
255     tmp = freqs[ step ];
256     return tmp.toInt();
257 }
258
259
260 /**
261   * Returns the name of the current CPU frequency scaling governor
262   *
263   * \return     name of governor
264   */
265 QString MainWindow::getScalingGovernor()
266 {
267     return readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_governor" );
268 }
269
270
271 /**
272   * Returns the amount of available scaling steps.
273   */
274 int MainWindow::getScalingSteps()
275 {
276     QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies" );
277     QStringList freqs = tmp.split( " " );
278     return (freqs.size() - 1);
279 }
280
281
282 /**
283   * Returns the scaling step for the specified frequency.
284   */
285 int MainWindow::getScalingStep( int freq )
286 {
287     for( int i = 1; i <= getScalingSteps(); ++i ) {
288            if ( getScalingFreq(i) == freq )
289                 return i;
290     }
291
292     return 1;
293 }
294
295
296 /**
297   * Returns the SmartReflex(tm) state
298   *
299   * \return     0|1
300   */
301 int MainWindow::getSmartReflexState()
302 {
303 //SmartReflex is only supprted on Maemo5
304 #if defined(Q_WS_MAEMO_5)
305     QString tmp = readSysFile( "power/sr_vdd1_autocomp" );
306
307     if ( tmp == "1" )
308         return 1;
309     else
310         return 0;
311 #else
312     //disable UI checkbox
313     ui->sr_box->setDisabled( true );
314
315     return 0;
316 #endif
317 }
318
319
320 /**
321   * Reads any file in /sys/
322   *
323   * \param      sys_file : full path to sys file - omit "/sys/"
324   * \return     content of sys file
325   */
326 QString MainWindow::readSysFile(QString sys_file)
327 {
328     QFile file( "/sys/"+sys_file );
329
330     //open the file
331     if ( !file.exists() || !file.open( QIODevice::ReadOnly ) ) {
332         QMessageBox::critical(this, tr("QCPUFreq"), tr("Could not get information from /sys!"));
333         return "";
334     }
335
336     //read the file
337     QTextStream in( &file );
338     QString txt = in.readLine();
339
340     //close the file
341     file.close();
342
343     return txt;
344 }
345
346
347 /**
348   * Refreshes all of the values to display
349   */
350 void MainWindow::refresh()
351 {
352     //get the current frequency and calculate the MHz value
353     int freq = ( getMinFreq() / 1000 );
354     QString display;
355     display.setNum( freq );
356     display.append( " MHz" );
357     ui->freq_min->setText( display );
358
359     //do the same thing for the maximum frequency
360     freq = ( getMaxFreq() / 1000 );
361     display.setNum( freq );
362     display.append( " MHz" );
363     ui->freq_max->setText( display );
364
365     //display the current governor
366     ui->freq_governor->setText( getScalingGovernor() );
367
368     //display current temperature
369     ui->cpu_temp->setText( getCPUTemp() );
370
371     //smart reflex button
372     if ( getSmartReflexState() == 1 )
373         ui->sr_box->setCheckState( Qt::Checked );
374     else
375         ui->sr_box->setCheckState( Qt::Unchecked );
376
377
378     //display frequency slider
379     ui->freq_adjust->setMinimum( 1 );
380     ui->freq_adjust->setMaximum( getScalingSteps() );
381     ui->freq_adjust->setInvertedAppearance( true );
382     ui->freq_adjust->setSliderPosition( getScalingStep(getMaxFreq()) );
383
384     //ui->retranslateUi(this);
385 }
386
387
388 /**
389   * Repaints part of the GUI after the device was rotated
390   */
391 void MainWindow::orientationChanged()
392 {
393     QPixmap image;
394
395     //check whether we are using portrait or landscape mode
396     if ( usePortrait() ) {
397         //in portrait mode we want to display the large image
398         image.load( ":/img/chip256" );
399         scene.clear();
400         scene.addPixmap(  image  );
401
402         ui->graphicsPortrait->setScene( &scene );
403         ui->graphicsPortrait->setMaximumSize( 256, 256 );
404         ui->graphicsLandscape->setMaximumSize( 0, 0 );
405     } else {
406         image.load( ":/img/chip128" );
407         scene.clear();
408         scene.addPixmap(  image  );
409
410         ui->graphicsLandscape->setScene( &scene );
411         ui->graphicsLandscape->setMaximumSize( 128, 128 );
412         ui->graphicsPortrait->setMaximumSize( 0, 0 );
413     }
414 }
415
416
417 /**
418   * Enables the auto-rotation feature of Maemo5 devices
419   */
420 void MainWindow::setAutoRotation()
421 {
422 #if defined(Q_WS_MAEMO_5)
423     setAttribute(Qt::WA_Maemo5AutoOrientation, true);
424 #endif
425 }
426
427
428 /**
429   * SLOT: enable/disable overclocking.
430   */
431 void MainWindow::setOverclocking()
432 {
433     if (ui->actionOverclocking->isChecked()) {
434         #if defined(Q_WS_MAEMO_5)
435         QMaemo5InformationBox::information(this, tr( "Please note that overclocking voids your warranty and may break your device! Be careful!"), 0);
436         #endif
437         this->allowOverclocking = true;
438     } else {
439         this->allowOverclocking = false;
440     }
441 }
442
443
444 /**
445   * SLOT: Enables or disables Smart Reflex(tm) after pressing sr_btn
446   */
447 void MainWindow::setSmartReflex()
448 {
449 //SmartReflex is only supported on Maemo5
450 #if defined(Q_WS_MAEMO_5)
451     if ( getSmartReflexState() == 1 )
452         callHelper( "set_sr", "off");
453     else {
454         QMaemo5InformationBox::information(this, tr( "SmartReflex support is known to be unstable on some devices and may cause random reboots." ), QMaemo5InformationBox::DefaultTimeout);
455         callHelper( "set_sr", "on");
456     }
457
458 #endif
459     //refresh the UI
460     refresh();
461 }
462
463
464 /**
465   * SLOT: display the help window
466   */
467 void MainWindow::showHelp()
468 {
469     helpWindow.show();
470 }
471
472
473 /**
474   * Returns true when the device is in portrait mode
475   */
476 bool MainWindow::usePortrait()
477 {
478     QRect screenGeometry = QApplication::desktop()->screenGeometry();
479     if (screenGeometry.width() > screenGeometry.height())
480         return false;
481     else
482         return true;
483 }