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()));
53 MainWindow::~MainWindow()
61 * SLOT: Displays an about box
63 void MainWindow::about()
65 QMessageBox::about(this, APPNAME " " APPVERSION, "<p style=\"align:center;\">© 2010 Daniel Klaffenbach</p>" );
70 * SLOT: Adjusts the maximum CPU frequency according to the scaler
72 void MainWindow::adjustFreq()
74 int newmax = getScalingFreq( ui->freq_adjust->sliderPosition() );
77 QStringList arguments;
78 #if defined(Q_WS_MAEMO_5)
79 //on Maemo5 the set_scalingmaxfreq-Script is not in $PATH
80 arguments.append( "/opt/usr/bin/set_scalingmaxfreq" );
82 arguments.append( "set_scalingmaxfreq" );
84 arguments.append( max );
86 //execute the scaling script
88 script.execute( "sudo", arguments );
95 * Returns the maximum CPU frequency
97 int MainWindow::getMaxFreq()
99 QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_max_freq" );
105 * Returns the minimum CPU frequency
107 int MainWindow::getMinFreq()
109 QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_min_freq" );
115 * Returns the CPU frequency for the specified scaling step
117 int MainWindow::getScalingFreq(int step)
119 QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies" );
120 QStringList freqs = tmp.split( " " );
124 if ( step > getScalingSteps() )
125 step = getScalingSteps();
133 * Returns the name of the current CPU frequency scaling governor
135 * \return name of governor
137 QString MainWindow::getScalingGovernor()
139 return readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_governor" );
144 * Returns the amount of available scaling steps.
146 int MainWindow::getScalingSteps()
148 QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies" );
149 QStringList freqs = tmp.split( " " );
150 return (freqs.size() - 1);
155 * Returns the scaling step for the specified frequency.
157 int MainWindow::getScalingStep( int freq )
159 for( int i = 1; i <= getScalingSteps(); ++i ) {
160 if ( getScalingFreq(i) == freq )
169 * Returns the SmartReflex(tm) state
173 int MainWindow::getSmartReflexState()
175 //SmartReflex is only supprted on Maemo5
176 #if defined(Q_WS_MAEMO_5)
177 QString tmp = readSysFile( "power/sr_vdd1_autocomp" );
190 * Reads any file in /sys/
192 * \param sys_file : full path to sys file - omit "/sys/"
193 * \return content of sys file
195 QString MainWindow::readSysFile(QString sys_file)
197 QFile file( "/sys/"+sys_file );
200 if ( !file.exists() || !file.open( QIODevice::ReadOnly ) ) {
201 QMessageBox::critical(this, tr("QCPUFreq"), tr("Could not get information from /sys!"));
206 QTextStream in( &file );
207 QString txt = in.readLine();
214 * Refreshes all of the values to display
216 void MainWindow::refresh()
218 //get the current frequency and calculate the MHz value
219 int freq = ( getMinFreq() / 1000 );
221 display.setNum( freq );
222 display.append( " MHz" );
223 ui->freq_min->setText( display );
225 //do the same thing for the maximum frequency
226 freq = ( getMaxFreq() / 1000 );
227 display.setNum( freq );
228 display.append( " MHz" );
229 ui->freq_max->setText( display );
231 //display the current governor
232 ui->freq_governor->setText( getScalingGovernor() );
234 //smart reflex button
235 if ( getSmartReflexState() == 1 ) {
236 ui->sr_btn->setDown( true );
237 ui->sr_btn->setText( tr( "Enabled" ) );
239 ui->sr_btn->setDown( false );
240 ui->sr_btn->setText( tr( "Disabled" ) );
244 //display frequency slider
245 ui->freq_adjust->setMinimum( 1 );
246 ui->freq_adjust->setMaximum( getScalingSteps() );
247 ui->freq_adjust->setInvertedAppearance( true );
248 ui->freq_adjust->setSliderPosition( getScalingStep(getMaxFreq()) );
253 * Repaints part of the GUI after the device was rotated
255 void MainWindow::orientationChanged()
259 //check whether we are using portrait or landscape mode
260 if ( usePortrait() ) {
261 //in portrait mode we want to display the large image
262 image.load( ":/img/chip256" );
263 this->scene->clear();
264 this->scene->addPixmap( image );
266 ui->graphicsPortrait->setScene( this->scene );
267 ui->graphicsPortrait->setMaximumSize( 256, 256 );
268 ui->graphicsLandscape->setMaximumSize( 0, 0 );
270 image.load( ":/img/chip128" );
271 this->scene->clear();
272 this->scene->addPixmap( image );
274 ui->graphicsLandscape->setScene( this->scene );
275 ui->graphicsLandscape->setMaximumSize( 128, 128 );
276 ui->graphicsPortrait->setMaximumSize( 0, 0 );
282 * Enables the auto-rotation feature of Maemo5 devices
284 void MainWindow::setAutoRotaion()
286 #if defined(Q_WS_MAEMO_5)
287 setAttribute(Qt::WA_Maemo5AutoOrientation, true);
293 * Returns true when the device is in portrait mode
295 bool MainWindow::usePortrait()
297 QRect screenGeometry = QApplication::desktop()->screenGeometry();
298 if (screenGeometry.width() > screenGeometry.height())