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