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>
25 #include <QDesktopWidget>
26 #if defined(Q_WS_MAEMO_5)
27 #include <QMaemo5InformationBox>
31 #define APPNAME "QCPUFreq"
32 #define APPVERSION "0.3.2"
34 MainWindow::MainWindow(QWidget *parent) :
36 ui(new Ui::MainWindow),
37 //create helper process
38 helperProcess( this ),
39 //create a new, stackable help window
41 //create UI refresh timer
43 //create a QGraphicsScene for the little chip icon
45 //show errors about the sudo setup only once
48 //this is a stacked window on Maemo 5
49 #if defined(Q_WS_MAEMO_5)
50 setAttribute(Qt::WA_Maemo5StackedWindow);
57 // enable auto rotation
60 //initialize orientation
63 //refresh UI every 10 seconds
64 refreshTimer.start( 10000 );
66 // initialize stackable help window
67 #if defined(Q_WS_MAEMO_5)
68 helpWindow.setAttribute(Qt::WA_Maemo5StackedWindow);
70 helpWindow.setWindowFlags( windowFlags() | Qt::Window );
72 //connect signals and slots
73 connect(ui->actionHelp, SIGNAL(triggered()), this, SLOT(showHelp()));
74 connect( ui->actionAbout, SIGNAL(triggered()), this, SLOT(about()) );
75 connect( ui->freq_adjust, SIGNAL(valueChanged(int)), this, SLOT(adjustFreq()) );
76 connect(QApplication::desktop(), SIGNAL(resized(int)), this, SLOT(orientationChanged()));
77 connect(ui->sr_box, SIGNAL(clicked()), this, SLOT(setSmartReflex()));
78 connect(&refreshTimer, SIGNAL(timeout()), this, SLOT(refresh()));
82 MainWindow::~MainWindow()
89 * SLOT: Displays an about box
91 void MainWindow::about()
93 QMessageBox::about(this, APPNAME " " APPVERSION, "<p style=\"align:center;\">© 2010 Daniel Klaffenbach</p>" );
99 * SLOT: Adjusts the maximum CPU frequency according to the scaler
101 void MainWindow::adjustFreq()
103 int newmax = getScalingFreq( ui->freq_adjust->sliderPosition() );
106 //maxfreq should not be smaller than minfreq, because we do not want to decrease minfreq
107 if (newmax < getMinFreq())
108 newmax = getMinFreq();
110 max.setNum( newmax );
112 callHelper( "set_maxfreq", max );
119 * Calls the QCPUFreq helper script with "sudo action param"
121 * @param action : the action of the helper script
122 * @param param : the parameter for the action
125 int MainWindow::callHelper(QString action, QString param)
127 QStringList arguments;
129 #if defined(Q_WS_MAEMO_5)
130 //On Maemo 5 the helper script resides in /opt/usr/bin, which is usually not in $PATH
131 arguments.append( "/opt/usr/bin/QCPUFreq.helper" );
133 arguments.append( "QCPUFreq.helper" );
136 arguments.append( action );
137 arguments.append( param );
139 helperProcess.start( "sudo", arguments, QIODevice::NotOpen );
141 if ( showSudoError && !helperProcess.waitForFinished( 400 )) {
142 //do not show this error again
143 showSudoError = false;
144 QMessageBox::critical(this, tr("QCPUFreq"), tr("There seems to be a problem with your sudo setup!"));
147 return helperProcess.exitCode();
152 * Returns the current CPU temperature
154 QString MainWindow::getCPUTemp()
156 #if defined(Q_WS_MAEMO_5)
157 QFile file( "/sys/class/power_supply/bq27200-0/temp" );
159 //check if we can read a more accurate temperature (only for power kernel)
161 return QString( readSysFile( "class/power_supply/bq27200-0/temp" ) + " " + QString::fromUtf8("\302\260") + "C" );
164 We actually only need to read the raw temperature, but it appears that by also reading temp1_input
165 the raw temperature (temp1_input_raw) is being updated more frequently.
167 readSysFile( "devices/platform/omap34xx_temp/temp1_input" );
169 //read the current system temperature
170 QString tstring = readSysFile( "devices/platform/omap34xx_temp/temp1_input_raw" );
172 return tr( "Unknown" );
174 //convert it to an integer and calculate the approx. temperature from the raw value
175 int tint = tstring.toInt();
176 tint = ( 0.65 * tint );
177 tstring.setNum(tint);
178 return QString( tstring + " " + QString::fromUtf8("\302\260") + "C" );
181 return tr( "Unknown" );
186 * Returns the maximum CPU frequency
188 int MainWindow::getMaxFreq()
190 QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_max_freq" );
196 * Returns the minimum CPU frequency
198 int MainWindow::getMinFreq()
200 QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_min_freq" );
206 * Returns the CPU frequency for the specified scaling step
208 int MainWindow::getScalingFreq(int step)
210 QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies" );
211 QStringList freqs = tmp.split( " " );
215 if ( step > getScalingSteps() )
216 step = getScalingSteps();
224 * Returns the name of the current CPU frequency scaling governor
226 * \return name of governor
228 QString MainWindow::getScalingGovernor()
230 return readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_governor" );
235 * Returns the amount of available scaling steps.
237 int MainWindow::getScalingSteps()
239 QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies" );
240 QStringList freqs = tmp.split( " " );
241 return (freqs.size() - 1);
246 * Returns the scaling step for the specified frequency.
248 int MainWindow::getScalingStep( int freq )
250 for( int i = 1; i <= getScalingSteps(); ++i ) {
251 if ( getScalingFreq(i) == freq )
260 * Returns the SmartReflex(tm) state
264 int MainWindow::getSmartReflexState()
266 //SmartReflex is only supprted on Maemo5
267 #if defined(Q_WS_MAEMO_5)
268 QString tmp = readSysFile( "power/sr_vdd1_autocomp" );
275 //disable UI checkbox
276 ui->sr_box->setDisabled( true );
284 * Reads any file in /sys/
286 * \param sys_file : full path to sys file - omit "/sys/"
287 * \return content of sys file
289 QString MainWindow::readSysFile(QString sys_file)
291 QFile file( "/sys/"+sys_file );
294 if ( !file.exists() || !file.open( QIODevice::ReadOnly ) ) {
295 QMessageBox::critical(this, tr("QCPUFreq"), tr("Could not get information from /sys!"));
300 QTextStream in( &file );
301 QString txt = in.readLine();
311 * Refreshes all of the values to display
313 void MainWindow::refresh()
315 //get the current frequency and calculate the MHz value
316 int freq = ( getMinFreq() / 1000 );
318 display.setNum( freq );
319 display.append( " MHz" );
320 ui->freq_min->setText( display );
322 //do the same thing for the maximum frequency
323 freq = ( getMaxFreq() / 1000 );
324 display.setNum( freq );
325 display.append( " MHz" );
326 ui->freq_max->setText( display );
328 //display the current governor
329 ui->freq_governor->setText( getScalingGovernor() );
331 //display current temperature
332 ui->cpu_temp->setText( getCPUTemp() );
334 //smart reflex button
335 if ( getSmartReflexState() == 1 )
336 ui->sr_box->setCheckState( Qt::Checked );
338 ui->sr_box->setCheckState( Qt::Unchecked );
341 //display frequency slider
342 ui->freq_adjust->setMinimum( 1 );
343 ui->freq_adjust->setMaximum( getScalingSteps() );
344 ui->freq_adjust->setInvertedAppearance( true );
345 ui->freq_adjust->setSliderPosition( getScalingStep(getMaxFreq()) );
347 //ui->retranslateUi(this);
352 * Repaints part of the GUI after the device was rotated
354 void MainWindow::orientationChanged()
358 //check whether we are using portrait or landscape mode
359 if ( usePortrait() ) {
360 //in portrait mode we want to display the large image
361 image.load( ":/img/chip256" );
363 scene.addPixmap( image );
365 ui->graphicsPortrait->setScene( &scene );
366 ui->graphicsPortrait->setMaximumSize( 256, 256 );
367 ui->graphicsLandscape->setMaximumSize( 0, 0 );
369 image.load( ":/img/chip128" );
371 scene.addPixmap( image );
373 ui->graphicsLandscape->setScene( &scene );
374 ui->graphicsLandscape->setMaximumSize( 128, 128 );
375 ui->graphicsPortrait->setMaximumSize( 0, 0 );
381 * Enables the auto-rotation feature of Maemo5 devices
383 void MainWindow::setAutoRotation()
385 #if defined(Q_WS_MAEMO_5)
386 setAttribute(Qt::WA_Maemo5AutoOrientation, true);
392 * SLOT: Enables or disables Smart Reflex(tm) after pressing sr_btn
394 void MainWindow::setSmartReflex()
396 //SmartReflex is only supported on Maemo5
397 #if defined(Q_WS_MAEMO_5)
398 if ( getSmartReflexState() == 1 )
399 callHelper( "set_sr", "off");
401 QMaemo5InformationBox::information(this, tr( "SmartReflex support is known to be unstable on some devices and may cause random reboots." ), QMaemo5InformationBox::DefaultTimeout);
402 callHelper( "set_sr", "on");
412 * SLOT: display the help window
414 void MainWindow::showHelp()
421 * Returns true when the device is in portrait mode
423 bool MainWindow::usePortrait()
425 QRect screenGeometry = QApplication::desktop()->screenGeometry();
426 if (screenGeometry.width() > screenGeometry.height())