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>
26 #include <QDesktopWidget>
27 #if defined(Q_WS_MAEMO_5)
28 #include <QMaemo5InformationBox>
32 #define APPNAME "QCPUFreq"
33 #define APPVERSION "0.2"
35 MainWindow::MainWindow(QWidget *parent) :
37 ui(new Ui::MainWindow)
43 // enable auto rotation
46 //create a QGraphicsScene for the little chip icon
47 scene = new QGraphicsScene();
50 //create the refresh timer
51 refreshTimer = new QTimer();
52 //refresh UI every 10 seconds
53 refreshTimer->start( 10000 );
55 //connect signals and slots
56 connect( ui->actionAbout, SIGNAL(triggered()), this, SLOT(about()) );
57 connect( ui->freq_adjust, SIGNAL(valueChanged(int)), this, SLOT(adjustFreq()) );
58 connect(QApplication::desktop(), SIGNAL(resized(int)), this, SLOT(orientationChanged()));
59 connect( ui->sr_btn, SIGNAL(clicked()), this, SLOT(setSmartReflex()) );
60 connect(refreshTimer, SIGNAL(timeout()), this, SLOT(refresh()));
64 MainWindow::~MainWindow()
73 * SLOT: Displays an about box
75 void MainWindow::about()
77 QMessageBox::about(this, APPNAME " " APPVERSION, "<p style=\"align:center;\">© 2010 Daniel Klaffenbach</p>" );
83 * SLOT: Adjusts the maximum CPU frequency according to the scaler
85 void MainWindow::adjustFreq()
87 int newmax = getScalingFreq( ui->freq_adjust->sliderPosition() );
91 callHelper( "set_maxfreq", max );
98 * Calls the QCPUFreq helper script with "sudo action param"
100 * @param action : the action of the helper script
101 * @param param : the parameter for the action
104 int MainWindow::callHelper(QString action, QString param)
107 QStringList arguments;
108 //run sudo in non-interactive mode
109 arguments.append( "-n" );
111 #if defined(Q_WS_MAEMO_5)
112 //On Maemo 5 the helper script resides in /opt/usr/bin, which us usually not in $PATH
113 arguments.append( "/opt/usr/bin/QCPUFreq.helper" );
115 arguments.append( "QCPUFreq.helper" );
118 arguments.append( action );
119 arguments.append( param );
121 helper.execute( "sudo", arguments );
123 return helper.exitCode();
128 * Returns the current CPU temperature
130 QString MainWindow::getCPUTemp()
132 #if defined(Q_WS_MAEMO_5)
133 return readSysFile( "devices/platform/omap34xx_temp/temp1_input_raw" );
135 return tr( "Unknown" );
140 * Returns the maximum CPU frequency
142 int MainWindow::getMaxFreq()
144 QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_max_freq" );
150 * Returns the minimum CPU frequency
152 int MainWindow::getMinFreq()
154 QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_min_freq" );
160 * Returns the CPU frequency for the specified scaling step
162 int MainWindow::getScalingFreq(int step)
164 QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies" );
165 QStringList freqs = tmp.split( " " );
169 if ( step > getScalingSteps() )
170 step = getScalingSteps();
178 * Returns the name of the current CPU frequency scaling governor
180 * \return name of governor
182 QString MainWindow::getScalingGovernor()
184 return readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_governor" );
189 * Returns the amount of available scaling steps.
191 int MainWindow::getScalingSteps()
193 QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies" );
194 QStringList freqs = tmp.split( " " );
195 return (freqs.size() - 1);
200 * Returns the scaling step for the specified frequency.
202 int MainWindow::getScalingStep( int freq )
204 for( int i = 1; i <= getScalingSteps(); ++i ) {
205 if ( getScalingFreq(i) == freq )
214 * Returns the SmartReflex(tm) state
218 int MainWindow::getSmartReflexState()
220 //SmartReflex is only supprted on Maemo5
221 #if defined(Q_WS_MAEMO_5)
222 QString tmp = readSysFile( "power/sr_vdd1_autocomp" );
235 * Reads any file in /sys/
237 * \param sys_file : full path to sys file - omit "/sys/"
238 * \return content of sys file
240 QString MainWindow::readSysFile(QString sys_file)
242 QFile file( "/sys/"+sys_file );
245 if ( !file.exists() || !file.open( QIODevice::ReadOnly ) ) {
246 QMessageBox::critical(this, tr("QCPUFreq"), tr("Could not get information from /sys!"));
251 QTextStream in( &file );
252 QString txt = in.readLine();
259 * Refreshes all of the values to display
261 void MainWindow::refresh()
263 //get the current frequency and calculate the MHz value
264 int freq = ( getMinFreq() / 1000 );
266 display.setNum( freq );
267 display.append( " MHz" );
268 ui->freq_min->setText( display );
270 //do the same thing for the maximum frequency
271 freq = ( getMaxFreq() / 1000 );
272 display.setNum( freq );
273 display.append( " MHz" );
274 ui->freq_max->setText( display );
276 //display the current governor
277 ui->freq_governor->setText( getScalingGovernor() );
279 //display current temperature
280 ui->cpu_temp->setText( getCPUTemp() );
282 //smart reflex button
283 if ( getSmartReflexState() == 1 )
284 ui->sr_btn->setText( tr( "Enabled" ) );
286 ui->sr_btn->setText( tr( "Disabled" ) );
289 //display frequency slider
290 ui->freq_adjust->setMinimum( 1 );
291 ui->freq_adjust->setMaximum( getScalingSteps() );
292 ui->freq_adjust->setInvertedAppearance( true );
293 ui->freq_adjust->setSliderPosition( getScalingStep(getMaxFreq()) );
295 //ui->retranslateUi(this);
300 * Repaints part of the GUI after the device was rotated
302 void MainWindow::orientationChanged()
306 //check whether we are using portrait or landscape mode
307 if ( usePortrait() ) {
308 //in portrait mode we want to display the large image
309 image.load( ":/img/chip256" );
310 this->scene->clear();
311 this->scene->addPixmap( image );
313 ui->graphicsPortrait->setScene( this->scene );
314 ui->graphicsPortrait->setMaximumSize( 256, 256 );
315 ui->graphicsLandscape->setMaximumSize( 0, 0 );
317 image.load( ":/img/chip128" );
318 this->scene->clear();
319 this->scene->addPixmap( image );
321 ui->graphicsLandscape->setScene( this->scene );
322 ui->graphicsLandscape->setMaximumSize( 128, 128 );
323 ui->graphicsPortrait->setMaximumSize( 0, 0 );
329 * Enables the auto-rotation feature of Maemo5 devices
331 void MainWindow::setAutoRotaion()
333 #if defined(Q_WS_MAEMO_5)
334 setAttribute(Qt::WA_Maemo5AutoOrientation, true);
340 * SLOT: Enables or disables Smart Reflex(tm) after pressing sr_btn
342 void MainWindow::setSmartReflex()
344 //SmartReflex is only supported on Maemo5
345 #if defined(Q_WS_MAEMO_5)
346 if ( getSmartReflexState() == 1 )
347 callHelper( "set_sr", "off");
349 QMaemo5InformationBox::information(this, tr( "SmartReflex support is known to be unstable on some devices and may cause random reboots." ), QMaemo5InformationBox::DefaultTimeout);
350 callHelper( "set_sr", "on");
360 * Returns true when the device is in portrait mode
362 bool MainWindow::usePortrait()
364 QRect screenGeometry = QApplication::desktop()->screenGeometry();
365 if (screenGeometry.width() > screenGeometry.height())