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