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 //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()) );
58 MainWindow::~MainWindow()
66 * SLOT: Displays an about box
68 void MainWindow::about()
70 QMessageBox::about(this, APPNAME " " APPVERSION, "<p style=\"align:center;\">© 2010 Daniel Klaffenbach</p>" );
76 * SLOT: Adjusts the maximum CPU frequency according to the scaler
78 void MainWindow::adjustFreq()
80 int newmax = getScalingFreq( ui->freq_adjust->sliderPosition() );
84 callHelper( "set_maxfreq", max );
91 * Calls the QCPUFreq helper script with "sudo action param"
93 * @param action : the action of the helper script
94 * @param param : the parameter for the action
97 int MainWindow::callHelper(QString action, QString param)
100 QStringList arguments;
101 //run sudo in non-interactive mode
102 arguments.append( "-n" );
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" );
108 arguments.append( "QCPUFreq.helper" );
111 arguments.append( action );
112 arguments.append( param );
114 helper.execute( "sudo", arguments );
116 return helper.exitCode();
121 * Returns the current CPU temperature
123 QString MainWindow::getCPUTemp()
125 #if defined(Q_WS_MAEMO_5)
126 return readSysFile( "devices/platform/omap34xx_temp/temp1_input_raw" );
128 return tr( "Unknown" );
133 * Returns the maximum CPU frequency
135 int MainWindow::getMaxFreq()
137 QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_max_freq" );
143 * Returns the minimum CPU frequency
145 int MainWindow::getMinFreq()
147 QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_min_freq" );
153 * Returns the CPU frequency for the specified scaling step
155 int MainWindow::getScalingFreq(int step)
157 QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies" );
158 QStringList freqs = tmp.split( " " );
162 if ( step > getScalingSteps() )
163 step = getScalingSteps();
171 * Returns the name of the current CPU frequency scaling governor
173 * \return name of governor
175 QString MainWindow::getScalingGovernor()
177 return readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_governor" );
182 * Returns the amount of available scaling steps.
184 int MainWindow::getScalingSteps()
186 QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies" );
187 QStringList freqs = tmp.split( " " );
188 return (freqs.size() - 1);
193 * Returns the scaling step for the specified frequency.
195 int MainWindow::getScalingStep( int freq )
197 for( int i = 1; i <= getScalingSteps(); ++i ) {
198 if ( getScalingFreq(i) == freq )
207 * Returns the SmartReflex(tm) state
211 int MainWindow::getSmartReflexState()
213 //SmartReflex is only supprted on Maemo5
214 #if defined(Q_WS_MAEMO_5)
215 QString tmp = readSysFile( "power/sr_vdd1_autocomp" );
228 * Reads any file in /sys/
230 * \param sys_file : full path to sys file - omit "/sys/"
231 * \return content of sys file
233 QString MainWindow::readSysFile(QString sys_file)
235 QFile file( "/sys/"+sys_file );
238 if ( !file.exists() || !file.open( QIODevice::ReadOnly ) ) {
239 QMessageBox::critical(this, tr("QCPUFreq"), tr("Could not get information from /sys!"));
244 QTextStream in( &file );
245 QString txt = in.readLine();
252 * Refreshes all of the values to display
254 void MainWindow::refresh()
256 //get the current frequency and calculate the MHz value
257 int freq = ( getMinFreq() / 1000 );
259 display.setNum( freq );
260 display.append( " MHz" );
261 ui->freq_min->setText( display );
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 );
269 //display the current governor
270 ui->freq_governor->setText( getScalingGovernor() );
272 //display current temperature
273 ui->cpu_temp->setText( getCPUTemp() );
275 //smart reflex button
276 if ( getSmartReflexState() == 1 )
277 ui->sr_btn->setText( tr( "Enabled" ) );
279 ui->sr_btn->setText( tr( "Disabled" ) );
282 //display frequency slider
283 ui->freq_adjust->setMinimum( 1 );
284 ui->freq_adjust->setMaximum( getScalingSteps() );
285 ui->freq_adjust->setInvertedAppearance( true );
286 ui->freq_adjust->setSliderPosition( getScalingStep(getMaxFreq()) );
288 //ui->retranslateUi(this);
293 * Repaints part of the GUI after the device was rotated
295 void MainWindow::orientationChanged()
299 //check whether we are using portrait or landscape mode
300 if ( usePortrait() ) {
301 //in portrait mode we want to display the large image
302 image.load( ":/img/chip256" );
303 this->scene->clear();
304 this->scene->addPixmap( image );
306 ui->graphicsPortrait->setScene( this->scene );
307 ui->graphicsPortrait->setMaximumSize( 256, 256 );
308 ui->graphicsLandscape->setMaximumSize( 0, 0 );
310 image.load( ":/img/chip128" );
311 this->scene->clear();
312 this->scene->addPixmap( image );
314 ui->graphicsLandscape->setScene( this->scene );
315 ui->graphicsLandscape->setMaximumSize( 128, 128 );
316 ui->graphicsPortrait->setMaximumSize( 0, 0 );
322 * Enables the auto-rotation feature of Maemo5 devices
324 void MainWindow::setAutoRotaion()
326 #if defined(Q_WS_MAEMO_5)
327 setAttribute(Qt::WA_Maemo5AutoOrientation, true);
333 * SLOT: Enables or disables Smart Reflex(tm) after pressing sr_btn
335 void MainWindow::setSmartReflex()
337 //SmartReflex is only supported on Maemo5
338 #if defined(Q_WS_MAEMO_5)
339 if ( getSmartReflexState() == 1 )
340 callHelper( "set_sr", "off");
342 QMaemo5InformationBox::information(this, tr( "SmartReflex support is known to be unstable on some devices and may cause random reboots." ), QMaemo5InformationBox::DefaultTimeout);
343 callHelper( "set_sr", "on");
353 * Returns true when the device is in portrait mode
355 bool MainWindow::usePortrait()
357 QRect screenGeometry = QApplication::desktop()->screenGeometry();
358 if (screenGeometry.width() > screenGeometry.height())