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