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>
27 #if defined(Q_WS_MAEMO_5)
28 #include <QMaemo5InformationBox>
32 #define APPNAME "QCPUFreq"
33 #define APPVERSION "0.1"
35 MainWindow::MainWindow(QWidget *parent) :
37 ui(new Ui::MainWindow)
42 // enable auto rotation
45 //create a QGraphicsScene for the little chip icon
46 scene = new QGraphicsScene();
49 //connect signals and slots
50 connect( ui->actionAbout, SIGNAL(triggered()), this, SLOT(about()) );
51 connect( ui->freq_adjust, SIGNAL(valueChanged(int)), this, SLOT(adjustFreq()) );
52 connect(QApplication::desktop(), SIGNAL(resized(int)), this, SLOT(orientationChanged()));
53 connect( ui->sr_btn, SIGNAL(clicked()), this, SLOT(setSmartReflex()) );
57 MainWindow::~MainWindow()
65 * SLOT: Displays an about box
67 void MainWindow::about()
69 QMessageBox::about(this, APPNAME " " APPVERSION, "<p style=\"align:center;\">© 2010 Daniel Klaffenbach</p>" );
75 * SLOT: Adjusts the maximum CPU frequency according to the scaler
77 void MainWindow::adjustFreq()
79 int newmax = getScalingFreq( ui->freq_adjust->sliderPosition() );
82 QStringList arguments;
83 #if defined(Q_WS_MAEMO_5)
84 //on Maemo5 the set_scalingmaxfreq-Script is not in $PATH
85 arguments.append( "/opt/usr/bin/set_scalingmaxfreq" );
87 arguments.append( "set_scalingmaxfreq" );
89 arguments.append( max );
91 //execute the scaling script
93 script.execute( "sudo", arguments );
100 * Returns the current CPU temperature
102 QString MainWindow::getCPUTemp()
104 #if defined(Q_WS_MAEMO_5)
105 return readSysFile( "devices/platform/omap34xx_temp/temp1_input_raw" );
107 return tr( "Unknown" );
112 * Returns the maximum CPU frequency
114 int MainWindow::getMaxFreq()
116 QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_max_freq" );
122 * Returns the minimum CPU frequency
124 int MainWindow::getMinFreq()
126 QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_min_freq" );
132 * Returns the CPU frequency for the specified scaling step
134 int MainWindow::getScalingFreq(int step)
136 QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies" );
137 QStringList freqs = tmp.split( " " );
141 if ( step > getScalingSteps() )
142 step = getScalingSteps();
150 * Returns the name of the current CPU frequency scaling governor
152 * \return name of governor
154 QString MainWindow::getScalingGovernor()
156 return readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_governor" );
161 * Returns the amount of available scaling steps.
163 int MainWindow::getScalingSteps()
165 QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies" );
166 QStringList freqs = tmp.split( " " );
167 return (freqs.size() - 1);
172 * Returns the scaling step for the specified frequency.
174 int MainWindow::getScalingStep( int freq )
176 for( int i = 1; i <= getScalingSteps(); ++i ) {
177 if ( getScalingFreq(i) == freq )
186 * Returns the SmartReflex(tm) state
190 int MainWindow::getSmartReflexState()
192 //SmartReflex is only supprted on Maemo5
193 #if defined(Q_WS_MAEMO_5)
194 QString tmp = readSysFile( "power/sr_vdd1_autocomp" );
207 * Reads any file in /sys/
209 * \param sys_file : full path to sys file - omit "/sys/"
210 * \return content of sys file
212 QString MainWindow::readSysFile(QString sys_file)
214 QFile file( "/sys/"+sys_file );
217 if ( !file.exists() || !file.open( QIODevice::ReadOnly ) ) {
218 QMessageBox::critical(this, tr("QCPUFreq"), tr("Could not get information from /sys!"));
223 QTextStream in( &file );
224 QString txt = in.readLine();
231 * Refreshes all of the values to display
233 void MainWindow::refresh()
235 //get the current frequency and calculate the MHz value
236 int freq = ( getMinFreq() / 1000 );
238 display.setNum( freq );
239 display.append( " MHz" );
240 ui->freq_min->setText( display );
242 //do the same thing for the maximum frequency
243 freq = ( getMaxFreq() / 1000 );
244 display.setNum( freq );
245 display.append( " MHz" );
246 ui->freq_max->setText( display );
248 //display the current governor
249 ui->freq_governor->setText( getScalingGovernor() );
251 //display current temperature
252 ui->cpu_temp->setText( getCPUTemp() );
254 //smart reflex button
255 if ( getSmartReflexState() == 1 ) {
256 ui->sr_btn->setDown( true );
257 ui->sr_btn->setText( tr( "Enabled" ) );
259 ui->sr_btn->setDown( false );
260 ui->sr_btn->setText( tr( "Disabled" ) );
264 //display frequency slider
265 ui->freq_adjust->setMinimum( 1 );
266 ui->freq_adjust->setMaximum( getScalingSteps() );
267 ui->freq_adjust->setInvertedAppearance( true );
268 ui->freq_adjust->setSliderPosition( getScalingStep(getMaxFreq()) );
273 * Repaints part of the GUI after the device was rotated
275 void MainWindow::orientationChanged()
279 //check whether we are using portrait or landscape mode
280 if ( usePortrait() ) {
281 //in portrait mode we want to display the large image
282 image.load( ":/img/chip256" );
283 this->scene->clear();
284 this->scene->addPixmap( image );
286 ui->graphicsPortrait->setScene( this->scene );
287 ui->graphicsPortrait->setMaximumSize( 256, 256 );
288 ui->graphicsLandscape->setMaximumSize( 0, 0 );
290 image.load( ":/img/chip128" );
291 this->scene->clear();
292 this->scene->addPixmap( image );
294 ui->graphicsLandscape->setScene( this->scene );
295 ui->graphicsLandscape->setMaximumSize( 128, 128 );
296 ui->graphicsPortrait->setMaximumSize( 0, 0 );
302 * Enables the auto-rotation feature of Maemo5 devices
304 void MainWindow::setAutoRotaion()
306 #if defined(Q_WS_MAEMO_5)
307 setAttribute(Qt::WA_Maemo5AutoOrientation, true);
313 * SLOT: Enables or disables Smart Reflex(tm) after pressing sr_btn
315 void MainWindow::setSmartReflex()
317 //SmartReflex is only supprted on Maemo5
318 #if defined(Q_WS_MAEMO_5)
319 QStringList arguments;
320 arguments.append( "/opt/usr/bin/set_sr" );
322 if ( getSmartReflexState() == 1 )
323 arguments.append( "off" );
325 QMaemo5InformationBox::information(this, tr( "SmartReflex support is known to be unstable on some devices and may cause random reboots." ), QMaemo5InformationBox::DefaultTimeout);
326 arguments.append( "on" );
329 //execute the sr script script
331 script.execute( "sudo", arguments );
340 * Returns true when the device is in portrait mode
342 bool MainWindow::usePortrait()
344 QRect screenGeometry = QApplication::desktop()->screenGeometry();
345 if (screenGeometry.width() > screenGeometry.height())