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