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