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