2 * QCPUFreq - a simple cpufreq GUI
3 * Copyright (C) 2010 Daniel Klaffenbach <daniel.klaffenbach@cs.tu-chemnitz.de>
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.
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.
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/>.
19 #include "mainwindow.h"
20 #include "ui_mainwindow.h"
23 #include <QMessageBox>
24 #include <QTextStream>
25 #include <QDesktopWidget>
26 #if defined(Q_WS_MAEMO_5)
27 #include <QMaemo5InformationBox>
31 #define APPNAME "QCPUFreq"
32 #define APPVERSION "0.3"
34 MainWindow::MainWindow(QWidget *parent) :
36 ui(new Ui::MainWindow)
38 //this is a stacked window on Maemo 5
39 #if defined(Q_WS_MAEMO_5)
40 setAttribute(Qt::WA_Maemo5StackedWindow);
47 // enable auto rotation
50 //create a QGraphicsScene for the little chip icon
51 scene = new QGraphicsScene();
54 //create the refresh timer
55 refreshTimer = new QTimer();
56 //refresh UI every 10 seconds
57 refreshTimer->start( 10000 );
59 //create helper process
60 helperProcess = new QProcess;
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);
67 helpWindow->setWindowFlags( windowFlags() | Qt::Window );
69 //show errors about the sudo setup only once
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()));
82 MainWindow::~MainWindow()
92 * SLOT: Displays an about box
94 void MainWindow::about()
96 QMessageBox::about(this, APPNAME " " APPVERSION, "<p style=\"align:center;\">© 2010 Daniel Klaffenbach</p>" );
102 * SLOT: Adjusts the maximum CPU frequency according to the scaler
104 void MainWindow::adjustFreq()
106 int newmax = getScalingFreq( ui->freq_adjust->sliderPosition() );
108 max.setNum( newmax );
110 callHelper( "set_maxfreq", max );
117 * Calls the QCPUFreq helper script with "sudo action param"
119 * @param action : the action of the helper script
120 * @param param : the parameter for the action
123 int MainWindow::callHelper(QString action, QString param)
125 QStringList arguments;
127 #if defined(Q_WS_MAEMO_5)
128 //On Maemo 5 the helper script resides in /opt/usr/bin, which us usually not in $PATH
129 arguments.append( "/opt/usr/bin/QCPUFreq.helper" );
131 arguments.append( "QCPUFreq.helper" );
134 arguments.append( action );
135 arguments.append( param );
137 helperProcess->start( "sudo", arguments, QIODevice::NotOpen );
139 if ( showSudoError && !helperProcess->waitForFinished( 200 )) {
140 //do not show this error again
141 showSudoError = false;
142 QMessageBox::critical(this, tr("QCPUFreq"), tr("There seems to be a problem with your sudo setup!"));
145 return helperProcess->exitCode();
150 * Returns the current CPU temperature
152 QString MainWindow::getCPUTemp()
154 #if defined(Q_WS_MAEMO_5)
155 QFile file( "/sys/class/power_supply/bq27200-0/temp" );
157 //check if we can read a more accurate temperature (only for power kernel)
159 return QString( readSysFile( "class/power_supply/bq27200-0/temp" ) + " " + QString::fromUtf8("\302\260") + "C" );
161 //read the current system temperature
162 QString tstring = readSysFile( "devices/platform/omap34xx_temp/temp1_input_raw" );
164 return tr( "Unknown" );
166 //convert it to an integer and calculate the approx. temperature from the raw value
167 int tint = tstring.toInt();
168 tint = ( 0.65 * tint );
169 tstring.setNum(tint);
170 return QString( tstring + " " + QString::fromUtf8("\302\260") + "C" );
173 return tr( "Unknown" );
178 * Returns the maximum CPU frequency
180 int MainWindow::getMaxFreq()
182 QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_max_freq" );
188 * Returns the minimum CPU frequency
190 int MainWindow::getMinFreq()
192 QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_min_freq" );
198 * Returns the CPU frequency for the specified scaling step
200 int MainWindow::getScalingFreq(int step)
202 QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies" );
203 QStringList freqs = tmp.split( " " );
207 if ( step > getScalingSteps() )
208 step = getScalingSteps();
216 * Returns the name of the current CPU frequency scaling governor
218 * \return name of governor
220 QString MainWindow::getScalingGovernor()
222 return readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_governor" );
227 * Returns the amount of available scaling steps.
229 int MainWindow::getScalingSteps()
231 QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies" );
232 QStringList freqs = tmp.split( " " );
233 return (freqs.size() - 1);
238 * Returns the scaling step for the specified frequency.
240 int MainWindow::getScalingStep( int freq )
242 for( int i = 1; i <= getScalingSteps(); ++i ) {
243 if ( getScalingFreq(i) == freq )
252 * Returns the SmartReflex(tm) state
256 int MainWindow::getSmartReflexState()
258 //SmartReflex is only supprted on Maemo5
259 #if defined(Q_WS_MAEMO_5)
260 QString tmp = readSysFile( "power/sr_vdd1_autocomp" );
267 //disable UI checkbox
268 ui->sr_box->setDisabled( true );
276 * Reads any file in /sys/
278 * \param sys_file : full path to sys file - omit "/sys/"
279 * \return content of sys file
281 QString MainWindow::readSysFile(QString sys_file)
283 QFile file( "/sys/"+sys_file );
286 if ( !file.exists() || !file.open( QIODevice::ReadOnly ) ) {
287 QMessageBox::critical(this, tr("QCPUFreq"), tr("Could not get information from /sys!"));
292 QTextStream in( &file );
293 QString txt = in.readLine();
303 * Refreshes all of the values to display
305 void MainWindow::refresh()
307 //get the current frequency and calculate the MHz value
308 int freq = ( getMinFreq() / 1000 );
310 display.setNum( freq );
311 display.append( " MHz" );
312 ui->freq_min->setText( display );
314 //do the same thing for the maximum frequency
315 freq = ( getMaxFreq() / 1000 );
316 display.setNum( freq );
317 display.append( " MHz" );
318 ui->freq_max->setText( display );
320 //display the current governor
321 ui->freq_governor->setText( getScalingGovernor() );
323 //display current temperature
324 ui->cpu_temp->setText( getCPUTemp() );
326 //smart reflex button
327 if ( getSmartReflexState() == 1 )
328 ui->sr_box->setCheckState( Qt::Checked );
330 ui->sr_box->setCheckState( Qt::Unchecked );
333 //display frequency slider
334 ui->freq_adjust->setMinimum( 1 );
335 ui->freq_adjust->setMaximum( getScalingSteps() );
336 ui->freq_adjust->setInvertedAppearance( true );
337 ui->freq_adjust->setSliderPosition( getScalingStep(getMaxFreq()) );
339 //ui->retranslateUi(this);
344 * Repaints part of the GUI after the device was rotated
346 void MainWindow::orientationChanged()
350 //check whether we are using portrait or landscape mode
351 if ( usePortrait() ) {
352 //in portrait mode we want to display the large image
353 image.load( ":/img/chip256" );
354 this->scene->clear();
355 this->scene->addPixmap( image );
357 ui->graphicsPortrait->setScene( this->scene );
358 ui->graphicsPortrait->setMaximumSize( 256, 256 );
359 ui->graphicsLandscape->setMaximumSize( 0, 0 );
361 image.load( ":/img/chip128" );
362 this->scene->clear();
363 this->scene->addPixmap( image );
365 ui->graphicsLandscape->setScene( this->scene );
366 ui->graphicsLandscape->setMaximumSize( 128, 128 );
367 ui->graphicsPortrait->setMaximumSize( 0, 0 );
373 * Enables the auto-rotation feature of Maemo5 devices
375 void MainWindow::setAutoRotation()
377 #if defined(Q_WS_MAEMO_5)
378 setAttribute(Qt::WA_Maemo5AutoOrientation, true);
384 * SLOT: Enables or disables Smart Reflex(tm) after pressing sr_btn
386 void MainWindow::setSmartReflex()
388 //SmartReflex is only supported on Maemo5
389 #if defined(Q_WS_MAEMO_5)
390 if ( getSmartReflexState() == 1 )
391 callHelper( "set_sr", "off");
393 QMaemo5InformationBox::information(this, tr( "SmartReflex support is known to be unstable on some devices and may cause random reboots." ), QMaemo5InformationBox::DefaultTimeout);
394 callHelper( "set_sr", "on");
404 * SLOT: display the help window
406 void MainWindow::showHelp()
413 * Returns true when the device is in portrait mode
415 bool MainWindow::usePortrait()
417 QRect screenGeometry = QApplication::desktop()->screenGeometry();
418 if (screenGeometry.width() > screenGeometry.height())