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>
29 #define APPNAME "QCPUFreq"
30 #define APPVERSION "0.1"
32 MainWindow::MainWindow(QWidget *parent) :
34 ui(new Ui::MainWindow)
39 // enable auto rotation
42 //create a QGraphicsScene for the little chip icon
43 scene = new QGraphicsScene();
46 //connect signals and slots
47 connect( ui->actionAbout, SIGNAL(triggered()), this, SLOT(about()) );
48 connect( ui->freq_adjust, SIGNAL(valueChanged(int)), this, SLOT(adjustFreq()) );
49 connect(QApplication::desktop(), SIGNAL(resized(int)), this, SLOT(orientationChanged()));
50 connect( ui->sr_btn, SIGNAL(clicked()), this, SLOT(setSmartReflex()) );
54 MainWindow::~MainWindow()
62 * SLOT: Displays an about box
64 void MainWindow::about()
66 QMessageBox::about(this, APPNAME " " APPVERSION, "<p style=\"align:center;\">© 2010 Daniel Klaffenbach</p>" );
71 * SLOT: Adjusts the maximum CPU frequency according to the scaler
73 void MainWindow::adjustFreq()
75 int newmax = getScalingFreq( ui->freq_adjust->sliderPosition() );
78 QStringList arguments;
79 #if defined(Q_WS_MAEMO_5)
80 //on Maemo5 the set_scalingmaxfreq-Script is not in $PATH
81 arguments.append( "/opt/usr/bin/set_scalingmaxfreq" );
83 arguments.append( "set_scalingmaxfreq" );
85 arguments.append( max );
87 //execute the scaling script
89 script.execute( "sudo", arguments );
96 * Returns the current CPU temperature
98 QString MainWindow::getCPUTemp()
100 #if defined(Q_WS_MAEMO_5)
101 return readSysFile( "devices/platform/omap34xx_temp/temp1_input_raw" );
108 * Returns the maximum CPU frequency
110 int MainWindow::getMaxFreq()
112 QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_max_freq" );
118 * Returns the minimum CPU frequency
120 int MainWindow::getMinFreq()
122 QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_min_freq" );
128 * Returns the CPU frequency for the specified scaling step
130 int MainWindow::getScalingFreq(int step)
132 QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies" );
133 QStringList freqs = tmp.split( " " );
137 if ( step > getScalingSteps() )
138 step = getScalingSteps();
146 * Returns the name of the current CPU frequency scaling governor
148 * \return name of governor
150 QString MainWindow::getScalingGovernor()
152 return readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_governor" );
157 * Returns the amount of available scaling steps.
159 int MainWindow::getScalingSteps()
161 QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies" );
162 QStringList freqs = tmp.split( " " );
163 return (freqs.size() - 1);
168 * Returns the scaling step for the specified frequency.
170 int MainWindow::getScalingStep( int freq )
172 for( int i = 1; i <= getScalingSteps(); ++i ) {
173 if ( getScalingFreq(i) == freq )
182 * Returns the SmartReflex(tm) state
186 int MainWindow::getSmartReflexState()
188 //SmartReflex is only supprted on Maemo5
189 #if defined(Q_WS_MAEMO_5)
190 QString tmp = readSysFile( "power/sr_vdd1_autocomp" );
203 * Reads any file in /sys/
205 * \param sys_file : full path to sys file - omit "/sys/"
206 * \return content of sys file
208 QString MainWindow::readSysFile(QString sys_file)
210 QFile file( "/sys/"+sys_file );
213 if ( !file.exists() || !file.open( QIODevice::ReadOnly ) ) {
214 QMessageBox::critical(this, tr("QCPUFreq"), tr("Could not get information from /sys!"));
219 QTextStream in( &file );
220 QString txt = in.readLine();
227 * Refreshes all of the values to display
229 void MainWindow::refresh()
231 //get the current frequency and calculate the MHz value
232 int freq = ( getMinFreq() / 1000 );
234 display.setNum( freq );
235 display.append( " MHz" );
236 ui->freq_min->setText( display );
238 //do the same thing for the maximum frequency
239 freq = ( getMaxFreq() / 1000 );
240 display.setNum( freq );
241 display.append( " MHz" );
242 ui->freq_max->setText( display );
244 //display the current governor
245 ui->freq_governor->setText( getScalingGovernor() );
247 //display current temperature
248 ui->cpu_temp->setText( getCPUTemp() );
250 //smart reflex button
251 if ( getSmartReflexState() == 1 ) {
252 ui->sr_btn->setDown( true );
253 ui->sr_btn->setText( tr( "Enabled" ) );
255 ui->sr_btn->setDown( false );
256 ui->sr_btn->setText( tr( "Disabled" ) );
260 //display frequency slider
261 ui->freq_adjust->setMinimum( 1 );
262 ui->freq_adjust->setMaximum( getScalingSteps() );
263 ui->freq_adjust->setInvertedAppearance( true );
264 ui->freq_adjust->setSliderPosition( getScalingStep(getMaxFreq()) );
269 * Repaints part of the GUI after the device was rotated
271 void MainWindow::orientationChanged()
275 //check whether we are using portrait or landscape mode
276 if ( usePortrait() ) {
277 //in portrait mode we want to display the large image
278 image.load( ":/img/chip256" );
279 this->scene->clear();
280 this->scene->addPixmap( image );
282 ui->graphicsPortrait->setScene( this->scene );
283 ui->graphicsPortrait->setMaximumSize( 256, 256 );
284 ui->graphicsLandscape->setMaximumSize( 0, 0 );
286 image.load( ":/img/chip128" );
287 this->scene->clear();
288 this->scene->addPixmap( image );
290 ui->graphicsLandscape->setScene( this->scene );
291 ui->graphicsLandscape->setMaximumSize( 128, 128 );
292 ui->graphicsPortrait->setMaximumSize( 0, 0 );
298 * Enables the auto-rotation feature of Maemo5 devices
300 void MainWindow::setAutoRotaion()
302 #if defined(Q_WS_MAEMO_5)
303 setAttribute(Qt::WA_Maemo5AutoOrientation, true);
309 * SLOT: Enables or disables Smart Reflex(tm) after pressing sr_btn
311 void MainWindow::setSmartReflex()
313 //SmartReflex is only supprted on Maemo5
314 #if defined(Q_WS_MAEMO_5)
315 QStringList arguments;
316 arguments.append( "/opt/usr/bin/set_sr" );
318 if ( getSmartReflexState() == 1 )
319 arguments.append( "off" );
321 arguments.append( "on" );
323 //execute the sr script script
325 script.execute( "sudo", arguments );
334 * Returns true when the device is in portrait mode
336 bool MainWindow::usePortrait()
338 QRect screenGeometry = QApplication::desktop()->screenGeometry();
339 if (screenGeometry.width() > screenGeometry.height())