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