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 //run sudo in non-interactive mode
85 arguments.append( "-n" );
87 #if defined(Q_WS_MAEMO_5)
88 //on Maemo5 the set_scalingmaxfreq-Script is not in $PATH
89 arguments.append( "/opt/usr/bin/set_scalingmaxfreq" );
91 arguments.append( "set_scalingmaxfreq" );
93 arguments.append( max );
95 //execute the scaling script
97 script.execute( "sudo", arguments );
104 * Returns the current CPU temperature
106 QString MainWindow::getCPUTemp()
108 #if defined(Q_WS_MAEMO_5)
109 return readSysFile( "devices/platform/omap34xx_temp/temp1_input_raw" );
111 return tr( "Unknown" );
116 * Returns the maximum CPU frequency
118 int MainWindow::getMaxFreq()
120 QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_max_freq" );
126 * Returns the minimum CPU frequency
128 int MainWindow::getMinFreq()
130 QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_min_freq" );
136 * Returns the CPU frequency for the specified scaling step
138 int MainWindow::getScalingFreq(int step)
140 QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies" );
141 QStringList freqs = tmp.split( " " );
145 if ( step > getScalingSteps() )
146 step = getScalingSteps();
154 * Returns the name of the current CPU frequency scaling governor
156 * \return name of governor
158 QString MainWindow::getScalingGovernor()
160 return readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_governor" );
165 * Returns the amount of available scaling steps.
167 int MainWindow::getScalingSteps()
169 QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies" );
170 QStringList freqs = tmp.split( " " );
171 return (freqs.size() - 1);
176 * Returns the scaling step for the specified frequency.
178 int MainWindow::getScalingStep( int freq )
180 for( int i = 1; i <= getScalingSteps(); ++i ) {
181 if ( getScalingFreq(i) == freq )
190 * Returns the SmartReflex(tm) state
194 int MainWindow::getSmartReflexState()
196 //SmartReflex is only supprted on Maemo5
197 #if defined(Q_WS_MAEMO_5)
198 QString tmp = readSysFile( "power/sr_vdd1_autocomp" );
211 * Reads any file in /sys/
213 * \param sys_file : full path to sys file - omit "/sys/"
214 * \return content of sys file
216 QString MainWindow::readSysFile(QString sys_file)
218 QFile file( "/sys/"+sys_file );
221 if ( !file.exists() || !file.open( QIODevice::ReadOnly ) ) {
222 QMessageBox::critical(this, tr("QCPUFreq"), tr("Could not get information from /sys!"));
227 QTextStream in( &file );
228 QString txt = in.readLine();
235 * Refreshes all of the values to display
237 void MainWindow::refresh()
239 //get the current frequency and calculate the MHz value
240 int freq = ( getMinFreq() / 1000 );
242 display.setNum( freq );
243 display.append( " MHz" );
244 ui->freq_min->setText( display );
246 //do the same thing for the maximum frequency
247 freq = ( getMaxFreq() / 1000 );
248 display.setNum( freq );
249 display.append( " MHz" );
250 ui->freq_max->setText( display );
252 //display the current governor
253 ui->freq_governor->setText( getScalingGovernor() );
255 //display current temperature
256 ui->cpu_temp->setText( getCPUTemp() );
258 //smart reflex button
259 if ( getSmartReflexState() == 1 ) {
260 ui->sr_btn->setDown( true );
261 ui->sr_btn->setText( tr( "Enabled" ) );
263 ui->sr_btn->setDown( false );
264 ui->sr_btn->setText( tr( "Disabled" ) );
268 //display frequency slider
269 ui->freq_adjust->setMinimum( 1 );
270 ui->freq_adjust->setMaximum( getScalingSteps() );
271 ui->freq_adjust->setInvertedAppearance( true );
272 ui->freq_adjust->setSliderPosition( getScalingStep(getMaxFreq()) );
274 //ui->retranslateUi(this);
279 * Repaints part of the GUI after the device was rotated
281 void MainWindow::orientationChanged()
285 //check whether we are using portrait or landscape mode
286 if ( usePortrait() ) {
287 //in portrait mode we want to display the large image
288 image.load( ":/img/chip256" );
289 this->scene->clear();
290 this->scene->addPixmap( image );
292 ui->graphicsPortrait->setScene( this->scene );
293 ui->graphicsPortrait->setMaximumSize( 256, 256 );
294 ui->graphicsLandscape->setMaximumSize( 0, 0 );
296 image.load( ":/img/chip128" );
297 this->scene->clear();
298 this->scene->addPixmap( image );
300 ui->graphicsLandscape->setScene( this->scene );
301 ui->graphicsLandscape->setMaximumSize( 128, 128 );
302 ui->graphicsPortrait->setMaximumSize( 0, 0 );
308 * Enables the auto-rotation feature of Maemo5 devices
310 void MainWindow::setAutoRotaion()
312 #if defined(Q_WS_MAEMO_5)
313 setAttribute(Qt::WA_Maemo5AutoOrientation, true);
319 * SLOT: Enables or disables Smart Reflex(tm) after pressing sr_btn
321 void MainWindow::setSmartReflex()
323 //SmartReflex is only supprted on Maemo5
324 #if defined(Q_WS_MAEMO_5)
325 QStringList arguments;
326 //run sudo in non-interactive mode
327 arguments.append( "-n" );
328 arguments.append( "/opt/usr/bin/set_sr" );
330 if ( getSmartReflexState() == 1 )
331 arguments.append( "off" );
333 QMaemo5InformationBox::information(this, tr( "SmartReflex support is known to be unstable on some devices and may cause random reboots." ), QMaemo5InformationBox::DefaultTimeout);
334 arguments.append( "on" );
337 //execute the sr script script
339 script.execute( "sudo", arguments );
348 * Returns true when the device is in portrait mode
350 bool MainWindow::usePortrait()
352 QRect screenGeometry = QApplication::desktop()->screenGeometry();
353 if (screenGeometry.width() > screenGeometry.height())