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()
72 * SLOT: Displays an about box
74 void MainWindow::about()
76 QMessageBox::about(this, APPNAME " " APPVERSION, "<p style=\"align:center;\">© 2010 Daniel Klaffenbach</p>" );
82 * SLOT: Adjusts the maximum CPU frequency according to the scaler
84 void MainWindow::adjustFreq()
86 int newmax = getScalingFreq( ui->freq_adjust->sliderPosition() );
90 callHelper( "set_maxfreq", max );
97 * Calls the QCPUFreq helper script with "sudo action param"
99 * @param action : the action of the helper script
100 * @param param : the parameter for the action
103 int MainWindow::callHelper(QString action, QString param)
106 QStringList arguments;
107 //run sudo in non-interactive mode
108 arguments.append( "-n" );
110 #if defined(Q_WS_MAEMO_5)
111 //On Maemo 5 the helper script resides in /opt/usr/bin, which us usually not in $PATH
112 arguments.append( "/opt/usr/bin/QCPUFreq.helper" );
114 arguments.append( "QCPUFreq.helper" );
117 arguments.append( action );
118 arguments.append( param );
120 helper.execute( "sudo", arguments );
122 return helper.exitCode();
127 * Returns the current CPU temperature
129 QString MainWindow::getCPUTemp()
131 #if defined(Q_WS_MAEMO_5)
132 return readSysFile( "devices/platform/omap34xx_temp/temp1_input_raw" );
134 return tr( "Unknown" );
139 * Returns the maximum CPU frequency
141 int MainWindow::getMaxFreq()
143 QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_max_freq" );
149 * Returns the minimum CPU frequency
151 int MainWindow::getMinFreq()
153 QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_min_freq" );
159 * Returns the CPU frequency for the specified scaling step
161 int MainWindow::getScalingFreq(int step)
163 QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies" );
164 QStringList freqs = tmp.split( " " );
168 if ( step > getScalingSteps() )
169 step = getScalingSteps();
177 * Returns the name of the current CPU frequency scaling governor
179 * \return name of governor
181 QString MainWindow::getScalingGovernor()
183 return readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_governor" );
188 * Returns the amount of available scaling steps.
190 int MainWindow::getScalingSteps()
192 QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies" );
193 QStringList freqs = tmp.split( " " );
194 return (freqs.size() - 1);
199 * Returns the scaling step for the specified frequency.
201 int MainWindow::getScalingStep( int freq )
203 for( int i = 1; i <= getScalingSteps(); ++i ) {
204 if ( getScalingFreq(i) == freq )
213 * Returns the SmartReflex(tm) state
217 int MainWindow::getSmartReflexState()
219 //SmartReflex is only supprted on Maemo5
220 #if defined(Q_WS_MAEMO_5)
221 QString tmp = readSysFile( "power/sr_vdd1_autocomp" );
234 * Reads any file in /sys/
236 * \param sys_file : full path to sys file - omit "/sys/"
237 * \return content of sys file
239 QString MainWindow::readSysFile(QString sys_file)
241 QFile file( "/sys/"+sys_file );
244 if ( !file.exists() || !file.open( QIODevice::ReadOnly ) ) {
245 QMessageBox::critical(this, tr("QCPUFreq"), tr("Could not get information from /sys!"));
250 QTextStream in( &file );
251 QString txt = in.readLine();
258 * Refreshes all of the values to display
260 void MainWindow::refresh()
262 //get the current frequency and calculate the MHz value
263 int freq = ( getMinFreq() / 1000 );
265 display.setNum( freq );
266 display.append( " MHz" );
267 ui->freq_min->setText( display );
269 //do the same thing for the maximum frequency
270 freq = ( getMaxFreq() / 1000 );
271 display.setNum( freq );
272 display.append( " MHz" );
273 ui->freq_max->setText( display );
275 //display the current governor
276 ui->freq_governor->setText( getScalingGovernor() );
278 //display current temperature
279 ui->cpu_temp->setText( getCPUTemp() );
281 //smart reflex button
282 if ( getSmartReflexState() == 1 )
283 ui->sr_btn->setText( tr( "Enabled" ) );
285 ui->sr_btn->setText( tr( "Disabled" ) );
288 //display frequency slider
289 ui->freq_adjust->setMinimum( 1 );
290 ui->freq_adjust->setMaximum( getScalingSteps() );
291 ui->freq_adjust->setInvertedAppearance( true );
292 ui->freq_adjust->setSliderPosition( getScalingStep(getMaxFreq()) );
294 //ui->retranslateUi(this);
299 * Repaints part of the GUI after the device was rotated
301 void MainWindow::orientationChanged()
305 //check whether we are using portrait or landscape mode
306 if ( usePortrait() ) {
307 //in portrait mode we want to display the large image
308 image.load( ":/img/chip256" );
309 this->scene->clear();
310 this->scene->addPixmap( image );
312 ui->graphicsPortrait->setScene( this->scene );
313 ui->graphicsPortrait->setMaximumSize( 256, 256 );
314 ui->graphicsLandscape->setMaximumSize( 0, 0 );
316 image.load( ":/img/chip128" );
317 this->scene->clear();
318 this->scene->addPixmap( image );
320 ui->graphicsLandscape->setScene( this->scene );
321 ui->graphicsLandscape->setMaximumSize( 128, 128 );
322 ui->graphicsPortrait->setMaximumSize( 0, 0 );
328 * Enables the auto-rotation feature of Maemo5 devices
330 void MainWindow::setAutoRotaion()
332 #if defined(Q_WS_MAEMO_5)
333 setAttribute(Qt::WA_Maemo5AutoOrientation, true);
339 * SLOT: Enables or disables Smart Reflex(tm) after pressing sr_btn
341 void MainWindow::setSmartReflex()
343 //SmartReflex is only supported on Maemo5
344 #if defined(Q_WS_MAEMO_5)
345 if ( getSmartReflexState() == 1 )
346 callHelper( "set_sr", "off");
348 QMaemo5InformationBox::information(this, tr( "SmartReflex support is known to be unstable on some devices and may cause random reboots." ), QMaemo5InformationBox::DefaultTimeout);
349 callHelper( "set_sr", "on");
359 * Returns true when the device is in portrait mode
361 bool MainWindow::usePortrait()
363 QRect screenGeometry = QApplication::desktop()->screenGeometry();
364 if (screenGeometry.width() > screenGeometry.height())