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>
28 #define APPNAME "QCPUFreq"
29 #define APPVERSION "0.1"
31 MainWindow::MainWindow(QWidget *parent) :
33 ui(new Ui::MainWindow)
38 // enable auto rotation
41 //connect signals and slots
42 connect( ui->actionAbout, SIGNAL(triggered()), this, SLOT(about()) );
43 connect( ui->freq_adjust, SIGNAL(valueChanged(int)), this, SLOT(adjustFreq()) );
46 MainWindow::~MainWindow()
52 * SLOT: Displays an about box
54 void MainWindow::about()
56 QMessageBox::about(this, APPNAME " " APPVERSION, "<p style=\"align:center;\">© 2010 Daniel Klaffenbach</p>" );
61 * SLOT: Adjusts the maximum CPU frequency according to the scaler
63 void MainWindow::adjustFreq()
65 int newmax = getScalingFreq( ui->freq_adjust->sliderPosition() );
68 QStringList arguments;
69 #if defined(Q_WS_MAEMO_5)
70 //on Maemo5 the set_scalingmaxfreq-Script is not in $PATH
71 arguments.append( "/opt/usr/bin/set_scalingmaxfreq" );
73 arguments.append( "set_scalingmaxfreq" );
75 arguments.append( max );
77 //execute the scaling script
79 script.execute( "sudo", arguments );
86 * Returns the current CPU frequency
88 int MainWindow::getCurFreq()
90 QString tmp = readScalingFile( "scaling_cur_freq" );
94 int MainWindow::getMaxFreq()
96 QString tmp = readScalingFile( "scaling_max_freq" );
100 int MainWindow::getMinFreq()
102 QString tmp = readScalingFile( "scaling_min_freq" );
109 * Returns the CPU frequency for the specified scaling step
111 int MainWindow::getScalingFreq(int step)
113 QString tmp = readScalingFile( "scaling_available_frequencies" );
114 QStringList freqs = tmp.split( " " );
118 if ( step > getScalingSteps() )
119 step = getScalingSteps();
126 QString MainWindow::getScalingGovernor()
128 return readScalingFile( "scaling_governor" );
132 * Returns the amount of available scaling steps.
134 int MainWindow::getScalingSteps()
136 QString tmp = readScalingFile( "scaling_available_frequencies" );
137 QStringList freqs = tmp.split( " " );
138 return (freqs.size() - 1);
143 * Returns the scaling step for the specified frequency.
145 int MainWindow::getScalingStep( int freq )
147 for( int i = 1; i <= getScalingSteps(); ++i ) {
148 if ( getScalingFreq(i) == freq )
155 QString MainWindow::readScalingFile(QString scaling_file)
157 QFile file( "/sys/devices/system/cpu/cpu0/cpufreq/"+scaling_file );
160 if ( !file.exists() || !file.open( QIODevice::ReadOnly ) ) {
161 QMessageBox::critical(this, tr("QCPUFreq"), tr("Could not get information from /sys!"));
166 QTextStream in( &file );
167 QString txt = in.readLine();
172 void MainWindow::refresh()
174 //get the current frequency and calculate the MHz value
175 int freq = ( getMinFreq() / 1000 );
177 display.setNum( freq );
178 display.append( " MHz" );
179 ui->freq_min->setText( display );
181 //do the same thing for the maximum frequency
182 freq = ( getMaxFreq() / 1000 );
183 display.setNum( freq );
184 display.append( " MHz" );
185 ui->freq_max->setText( display );
187 //display the current governor
188 ui->freq_governor->setText( getScalingGovernor() );
190 //display.setNum( getScalingFreq(1) );
191 //ui->freq_max->setText( display );
192 ui->freq_adjust->setMinimum( 1 );
193 ui->freq_adjust->setMaximum( getScalingSteps() );
194 ui->freq_adjust->setInvertedAppearance( true );
195 ui->freq_adjust->setSliderPosition( getScalingStep(getMaxFreq()) );
198 void MainWindow::setAutoRotaion()
200 #if defined(Q_WS_MAEMO_5)
201 setAttribute(Qt::WA_Maemo5AutoOrientation, true);
202 //setAttribute(Qt::WA_Maemo5PortraitOrientation, true);