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