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();
132 QString MainWindow::getScalingGovernor()
134 return readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_governor" );
138 * Returns the amount of available scaling steps.
140 int MainWindow::getScalingSteps()
142 QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies" );
143 QStringList freqs = tmp.split( " " );
144 return (freqs.size() - 1);
149 * Returns the scaling step for the specified frequency.
151 int MainWindow::getScalingStep( int freq )
153 for( int i = 1; i <= getScalingSteps(); ++i ) {
154 if ( getScalingFreq(i) == freq )
161 QString MainWindow::readSysFile(QString sys_file)
163 QFile file( "/sys/"+sys_file );
166 if ( !file.exists() || !file.open( QIODevice::ReadOnly ) ) {
167 QMessageBox::critical(this, tr("QCPUFreq"), tr("Could not get information from /sys!"));
172 QTextStream in( &file );
173 QString txt = in.readLine();
180 * Refreshes all of the values to display
182 void MainWindow::refresh()
184 //get the current frequency and calculate the MHz value
185 int freq = ( getMinFreq() / 1000 );
187 display.setNum( freq );
188 display.append( " MHz" );
189 ui->freq_min->setText( display );
191 //do the same thing for the maximum frequency
192 freq = ( getMaxFreq() / 1000 );
193 display.setNum( freq );
194 display.append( " MHz" );
195 ui->freq_max->setText( display );
197 //display the current governor
198 ui->freq_governor->setText( getScalingGovernor() );
200 //display.setNum( getScalingFreq(1) );
201 //ui->freq_max->setText( display );
202 ui->freq_adjust->setMinimum( 1 );
203 ui->freq_adjust->setMaximum( getScalingSteps() );
204 ui->freq_adjust->setInvertedAppearance( true );
205 ui->freq_adjust->setSliderPosition( getScalingStep(getMaxFreq()) );
210 * Repaints part of the GUI after the device was rotated
212 void MainWindow::orientationChanged()
216 //check whether we are using portrait or landscape mode
217 if ( usePortrait() ) {
218 //in portrait mode we want to display the large image
219 image.load( ":/img/chip256" );
220 this->scene->clear();
221 this->scene->addPixmap( image );
223 ui->graphicsPortrait->setScene( this->scene );
224 ui->graphicsPortrait->setMaximumSize( 256, 256 );
225 ui->graphicsLandscape->setMaximumSize( 0, 0 );
227 image.load( ":/img/chip128" );
228 this->scene->clear();
229 this->scene->addPixmap( image );
231 ui->graphicsLandscape->setScene( this->scene );
232 ui->graphicsLandscape->setMaximumSize( 128, 128 );
233 ui->graphicsPortrait->setMaximumSize( 0, 0 );
239 * Enables the auto-rotation feature of Maemo5 devices
241 void MainWindow::setAutoRotaion()
243 #if defined(Q_WS_MAEMO_5)
244 setAttribute(Qt::WA_Maemo5AutoOrientation, true);
250 * Returns true when the device is in portrait mode
252 bool MainWindow::usePortrait()
254 QRect screenGeometry = QApplication::desktop()->screenGeometry();
255 if (screenGeometry.width() > screenGeometry.height())