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() );
83 QStringList arguments;
84 #if defined(Q_WS_MAEMO_5)
85 //on Maemo5 the set_scalingmaxfreq-Script is not in $PATH
86 arguments.append( "/opt/usr/bin/set_scalingmaxfreq" );
88 arguments.append( "set_scalingmaxfreq" );
90 arguments.append( max );
92 //execute the scaling script
94 script.execute( "sudo", arguments );
101 * Returns the current CPU temperature
103 QString MainWindow::getCPUTemp()
105 #if defined(Q_WS_MAEMO_5)
106 return readSysFile( "devices/platform/omap34xx_temp/temp1_input_raw" );
108 return tr( "Unknown" );
113 * Returns the maximum CPU frequency
115 int MainWindow::getMaxFreq()
117 QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_max_freq" );
123 * Returns the minimum CPU frequency
125 int MainWindow::getMinFreq()
127 QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_min_freq" );
133 * Returns the CPU frequency for the specified scaling step
135 int MainWindow::getScalingFreq(int step)
137 QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies" );
138 QStringList freqs = tmp.split( " " );
142 if ( step > getScalingSteps() )
143 step = getScalingSteps();
151 * Returns the name of the current CPU frequency scaling governor
153 * \return name of governor
155 QString MainWindow::getScalingGovernor()
157 return readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_governor" );
162 * Returns the amount of available scaling steps.
164 int MainWindow::getScalingSteps()
166 QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies" );
167 QStringList freqs = tmp.split( " " );
168 return (freqs.size() - 1);
173 * Returns the scaling step for the specified frequency.
175 int MainWindow::getScalingStep( int freq )
177 for( int i = 1; i <= getScalingSteps(); ++i ) {
178 if ( getScalingFreq(i) == freq )
187 * Returns the SmartReflex(tm) state
191 int MainWindow::getSmartReflexState()
193 //SmartReflex is only supprted on Maemo5
194 #if defined(Q_WS_MAEMO_5)
195 QString tmp = readSysFile( "power/sr_vdd1_autocomp" );
208 * Reads any file in /sys/
210 * \param sys_file : full path to sys file - omit "/sys/"
211 * \return content of sys file
213 QString MainWindow::readSysFile(QString sys_file)
215 QFile file( "/sys/"+sys_file );
218 if ( !file.exists() || !file.open( QIODevice::ReadOnly ) ) {
219 QMessageBox::critical(this, tr("QCPUFreq"), tr("Could not get information from /sys!"));
224 QTextStream in( &file );
225 QString txt = in.readLine();
232 * Refreshes all of the values to display
234 void MainWindow::refresh()
236 //get the current frequency and calculate the MHz value
237 int freq = ( getMinFreq() / 1000 );
239 display.setNum( freq );
240 display.append( " MHz" );
241 ui->freq_min->setText( display );
243 //do the same thing for the maximum frequency
244 freq = ( getMaxFreq() / 1000 );
245 display.setNum( freq );
246 display.append( " MHz" );
247 ui->freq_max->setText( display );
249 //display the current governor
250 ui->freq_governor->setText( getScalingGovernor() );
252 //display current temperature
253 ui->cpu_temp->setText( getCPUTemp() );
255 //smart reflex button
256 if ( getSmartReflexState() == 1 ) {
257 ui->sr_btn->setDown( true );
258 ui->sr_btn->setText( tr( "Enabled" ) );
260 ui->sr_btn->setDown( false );
261 ui->sr_btn->setText( tr( "Disabled" ) );
265 //display frequency slider
266 ui->freq_adjust->setMinimum( 1 );
267 ui->freq_adjust->setMaximum( getScalingSteps() );
268 ui->freq_adjust->setInvertedAppearance( true );
269 ui->freq_adjust->setSliderPosition( getScalingStep(getMaxFreq()) );
271 //ui->retranslateUi(this);
276 * Repaints part of the GUI after the device was rotated
278 void MainWindow::orientationChanged()
282 //check whether we are using portrait or landscape mode
283 if ( usePortrait() ) {
284 //in portrait mode we want to display the large image
285 image.load( ":/img/chip256" );
286 this->scene->clear();
287 this->scene->addPixmap( image );
289 ui->graphicsPortrait->setScene( this->scene );
290 ui->graphicsPortrait->setMaximumSize( 256, 256 );
291 ui->graphicsLandscape->setMaximumSize( 0, 0 );
293 image.load( ":/img/chip128" );
294 this->scene->clear();
295 this->scene->addPixmap( image );
297 ui->graphicsLandscape->setScene( this->scene );
298 ui->graphicsLandscape->setMaximumSize( 128, 128 );
299 ui->graphicsPortrait->setMaximumSize( 0, 0 );
305 * Enables the auto-rotation feature of Maemo5 devices
307 void MainWindow::setAutoRotaion()
309 #if defined(Q_WS_MAEMO_5)
310 setAttribute(Qt::WA_Maemo5AutoOrientation, true);
316 * SLOT: Enables or disables Smart Reflex(tm) after pressing sr_btn
318 void MainWindow::setSmartReflex()
320 //SmartReflex is only supprted on Maemo5
321 #if defined(Q_WS_MAEMO_5)
322 QStringList arguments;
323 arguments.append( "/opt/usr/bin/set_sr" );
325 if ( getSmartReflexState() == 1 )
326 arguments.append( "off" );
328 QMaemo5InformationBox::information(this, tr( "SmartReflex support is known to be unstable on some devices and may cause random reboots." ), QMaemo5InformationBox::DefaultTimeout);
329 arguments.append( "on" );
332 //execute the sr script script
334 script.execute( "sudo", arguments );
343 * Returns true when the device is in portrait mode
345 bool MainWindow::usePortrait()
347 QRect screenGeometry = QApplication::desktop()->screenGeometry();
348 if (screenGeometry.width() > screenGeometry.height())