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