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()
60 * SLOT: Displays an about box
62 void MainWindow::about()
64 QMessageBox::about(this, APPNAME " " APPVERSION, "<p style=\"align:center;\">© 2010 Daniel Klaffenbach</p>" );
69 * SLOT: Adjusts the maximum CPU frequency according to the scaler
71 void MainWindow::adjustFreq()
73 int newmax = getScalingFreq( ui->freq_adjust->sliderPosition() );
76 QStringList arguments;
77 #if defined(Q_WS_MAEMO_5)
78 //on Maemo5 the set_scalingmaxfreq-Script is not in $PATH
79 arguments.append( "/opt/usr/bin/set_scalingmaxfreq" );
81 arguments.append( "set_scalingmaxfreq" );
83 arguments.append( max );
85 //execute the scaling script
87 script.execute( "sudo", arguments );
94 * Returns the current CPU frequency
96 int MainWindow::getCurFreq()
98 QString tmp = readScalingFile( "scaling_cur_freq" );
102 int MainWindow::getMaxFreq()
104 QString tmp = readScalingFile( "scaling_max_freq" );
108 int MainWindow::getMinFreq()
110 QString tmp = readScalingFile( "scaling_min_freq" );
117 * Returns the CPU frequency for the specified scaling step
119 int MainWindow::getScalingFreq(int step)
121 QString tmp = readScalingFile( "scaling_available_frequencies" );
122 QStringList freqs = tmp.split( " " );
126 if ( step > getScalingSteps() )
127 step = getScalingSteps();
134 QString MainWindow::getScalingGovernor()
136 return readScalingFile( "scaling_governor" );
140 * Returns the amount of available scaling steps.
142 int MainWindow::getScalingSteps()
144 QString tmp = readScalingFile( "scaling_available_frequencies" );
145 QStringList freqs = tmp.split( " " );
146 return (freqs.size() - 1);
151 * Returns the scaling step for the specified frequency.
153 int MainWindow::getScalingStep( int freq )
155 for( int i = 1; i <= getScalingSteps(); ++i ) {
156 if ( getScalingFreq(i) == freq )
163 QString MainWindow::readScalingFile(QString scaling_file)
165 QFile file( "/sys/devices/system/cpu/cpu0/cpufreq/"+scaling_file );
168 if ( !file.exists() || !file.open( QIODevice::ReadOnly ) ) {
169 QMessageBox::critical(this, tr("QCPUFreq"), tr("Could not get information from /sys!"));
174 QTextStream in( &file );
175 QString txt = in.readLine();
182 * Refreshes all of the values to display
184 void MainWindow::refresh()
186 //get the current frequency and calculate the MHz value
187 int freq = ( getMinFreq() / 1000 );
189 display.setNum( freq );
190 display.append( " MHz" );
191 ui->freq_min->setText( display );
193 //do the same thing for the maximum frequency
194 freq = ( getMaxFreq() / 1000 );
195 display.setNum( freq );
196 display.append( " MHz" );
197 ui->freq_max->setText( display );
199 //display the current governor
200 ui->freq_governor->setText( getScalingGovernor() );
202 //display.setNum( getScalingFreq(1) );
203 //ui->freq_max->setText( display );
204 ui->freq_adjust->setMinimum( 1 );
205 ui->freq_adjust->setMaximum( getScalingSteps() );
206 ui->freq_adjust->setInvertedAppearance( true );
207 ui->freq_adjust->setSliderPosition( getScalingStep(getMaxFreq()) );
212 * Repaints part of the GUI after the device was rotated
214 void MainWindow::orientationChanged()
218 //check whether we are using portrait or landscape mode
219 if ( usePortrait() ) {
220 //in portrait mode we want to display the large image
221 image.load( ":/img/chip256" );
222 this->scene->clear();
223 this->scene->addPixmap( image );
225 ui->graphicsPortrait->setScene( this->scene );
226 ui->graphicsPortrait->setMaximumSize( 256, 256 );
227 ui->graphicsLandscape->setMaximumSize( 0, 0 );
229 image.load( ":/img/chip128" );
230 this->scene->clear();
231 this->scene->addPixmap( image );
233 ui->graphicsLandscape->setScene( this->scene );
234 ui->graphicsLandscape->setMaximumSize( 128, 128 );
235 ui->graphicsPortrait->setMaximumSize( 0, 0 );
241 * Enables the auto-rotation feature of Maemo5 devices
243 void MainWindow::setAutoRotaion()
245 #if defined(Q_WS_MAEMO_5)
246 setAttribute(Qt::WA_Maemo5AutoOrientation, true);
252 * Returns true when the device is in portrait mode
254 bool MainWindow::usePortrait()
256 QRect screenGeometry = QApplication::desktop()->screenGeometry();
257 if (screenGeometry.width() > screenGeometry.height())