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>
30 #define APPNAME "QCPUFreq"
31 #define APPVERSION "0.3.3"
33 MainWindow::MainWindow(QWidget *parent) :
35 ui(new Ui::MainWindow),
36 //do not allow overclocking per default
37 allowOverclocking(false),
38 //create helper process
39 helperProcess( this ),
40 //create a new, stackable help window
44 //are we using a power kernel?
46 //create UI refresh timer
48 //create a QGraphicsScene for the little chip icon
50 //show errors about the sudo setup only once
53 //this is a stacked window on Maemo 5
54 #if defined(Q_WS_MAEMO_5)
55 setAttribute(Qt::WA_Maemo5StackedWindow);
62 // enable auto rotation
65 //initialize orientation
68 //refresh UI every 10 seconds
69 refreshTimer.start( 10000 );
71 // initialize stackable help window
72 #if defined(Q_WS_MAEMO_5)
73 helpWindow.setAttribute(Qt::WA_Maemo5StackedWindow);
75 helpWindow.setWindowFlags( windowFlags() | Qt::Window );
77 //connect signals and slots
78 connect(ui->actionHelp, SIGNAL(triggered()), this, SLOT(showHelp()));
79 connect( ui->actionAbout, SIGNAL(triggered()), this, SLOT(about()) );
80 connect( ui->freq_adjust, SIGNAL(valueChanged(int)), this, SLOT(adjustFreq()) );
81 connect(QApplication::desktop(), SIGNAL(resized(int)), this, SLOT(orientationChanged()));
82 connect(ui->sr_box, SIGNAL(clicked()), this, SLOT(setSmartReflex()));
83 connect(&refreshTimer, SIGNAL(timeout()), this, SLOT(refresh()));
84 connect(ui->actionOverclocking, SIGNAL(toggled(bool)), this, SLOT(setOverclocking()));
87 //check if we are running on a power kernel
88 if ( getScalingFreq(0) > 600000 ) {
94 //disable overclocking button on vanilla kernels
96 ui->actionOverclocking->setDisabled(true);
101 MainWindow::~MainWindow()
108 * SLOT: Displays an about box
110 void MainWindow::about()
112 QMessageBox::about(this, APPNAME " " APPVERSION, "<p style=\"align:center;\">© 2010 Daniel Klaffenbach</p>" );
118 * SLOT: Adjusts the maximum CPU frequency according to the scaler
120 void MainWindow::adjustFreq()
122 int newmax = getScalingFreq( ui->freq_adjust->sliderPosition() );
124 if (newmax == getMaxFreq() ) {
125 //we do not need to change anything in this case
131 //maxfreq should not be smaller than minfreq, because we do not want to decrease minfreq
132 if (newmax < getMinFreq())
133 newmax = getMinFreq();
135 max.setNum( newmax );
137 //check for overclocking
138 #if defined(Q_WS_MAEMO_5)
139 if (this->allowOverclocking == false && newmax > 600000) {
140 QMaemo5InformationBox::information(this, tr( "You need to enable overclocking in QCPUFreq's menu for setting frequencies above 600MHz!"), 0);
146 callHelper( "set_maxfreq", max );
153 * Calls the QCPUFreq helper script with "sudo action param"
155 * @param action : the action of the helper script
156 * @param param : the parameter for the action
159 int MainWindow::callHelper(QString action, QString param)
161 QStringList arguments;
163 #if defined(Q_WS_MAEMO_5)
164 //On Maemo 5 the helper script resides in /opt/usr/bin, which is usually not in $PATH
165 arguments.append( "/opt/usr/bin/QCPUFreq.helper" );
167 arguments.append( "QCPUFreq.helper" );
170 arguments.append( action );
171 arguments.append( param );
173 helperProcess.start( "sudo", arguments, QIODevice::NotOpen );
175 if ( showSudoError && !helperProcess.waitForFinished( 400 )) {
176 //do not show this error again
177 showSudoError = false;
178 QMessageBox::critical(this, tr("QCPUFreq"), tr("There seems to be a problem with your sudo setup!"));
181 return helperProcess.exitCode();
186 * Returns the current CPU temperature
188 QString MainWindow::getCPUTemp()
190 #if defined(Q_WS_MAEMO_5)
191 QFile file( "/sys/class/power_supply/bq27200-0/temp" );
193 //check if we can read a more accurate temperature (only for power kernel)
195 return QString( readSysFile( "class/power_supply/bq27200-0/temp" ) + " " + QString::fromUtf8("\302\260") + "C" );
198 We actually only need to read the raw temperature, but it appears that by also reading temp1_input
199 the raw temperature (temp1_input_raw) is being updated more frequently.
201 readSysFile( "devices/platform/omap34xx_temp/temp1_input" );
203 //read the current system temperature
204 QString tstring = readSysFile( "devices/platform/omap34xx_temp/temp1_input_raw" );
206 return tr( "Unknown" );
208 //convert it to an integer and calculate the approx. temperature from the raw value
209 int tint = tstring.toInt();
210 tint = ( 0.65 * tint );
211 tstring.setNum(tint);
212 return QString( tstring + " " + QString::fromUtf8("\302\260") + "C" );
215 return tr( "Unknown" );
220 * Returns the maximum CPU frequency
222 int MainWindow::getMaxFreq()
224 QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_max_freq" );
230 * Returns the minimum CPU frequency
232 int MainWindow::getMinFreq()
234 if (this->minFreq == 0) {
235 QString min = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_min_freq" );
236 //check if avoid file exists (only on power kernel)
237 QFile file( "/sys/devices/system/cpu/cpu0/cpufreq/ondemand/avoid_frequencies" );
239 QString avoid = readSysFile( "devices/system/cpu/cpu0/cpufreq/ondemand/avoid_frequencies" );
240 QStringList avoidList = avoid.split( " " );
242 //check if min is in avoid_frequencies
243 for (int i = getScalingStep( min.toInt() ); i>0; --i) {
244 if (!avoidList.contains(min.setNum( getScalingFreq(i) ))) {
245 this->minFreq = min.toInt();
246 return this->minFreq;
250 //should not happen at all
251 this->minFreq = 125000;
252 return this->minFreq;
254 this->minFreq = min.toInt();
255 return this->minFreq;
258 return this->minFreq;
264 * Returns the CPU frequency for the specified scaling step
266 int MainWindow::getScalingFreq(int step)
268 QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies" );
269 QStringList freqs = tmp.split( " " );
273 if ( step > getScalingSteps() )
274 step = getScalingSteps();
282 * Returns the name of the current CPU frequency scaling governor
284 * @return QString - name of governor
286 QString MainWindow::getScalingGovernor()
288 return readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_governor" );
293 * Returns the amount of available scaling steps.
297 int MainWindow::getScalingSteps()
299 QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies" );
300 QStringList freqs = tmp.split( " " );
301 return (freqs.size() - 1);
306 * Returns the scaling step for the specified frequency.
310 int MainWindow::getScalingStep( int freq )
312 for( int i = 1; i <= getScalingSteps(); ++i ) {
313 if ( getScalingFreq(i) == freq )
322 * Returns the SmartReflex(tm) state
326 int MainWindow::getSmartReflexState()
328 //SmartReflex is only supprted on Maemo5
329 #if defined(Q_WS_MAEMO_5)
330 QString tmp = readSysFile( "power/sr_vdd1_autocomp" );
338 //disable UI checkbox
339 ui->sr_box->setDisabled( true );
347 * Reads any file in /sys/
349 * \param sys_file : full path to sys file - omit "/sys/"
350 * \return content of sys file
352 QString MainWindow::readSysFile(QString sys_file)
354 QFile file( "/sys/"+sys_file );
357 if ( !file.exists() || !file.open( QIODevice::ReadOnly ) ) {
358 QMessageBox::critical(this, tr("QCPUFreq"), tr("Could not get information from /sys!"));
363 QTextStream in( &file );
364 QString txt = in.readLine();
374 * Refreshes all of the values to display
376 void MainWindow::refresh()
378 //get the current frequency and calculate the MHz value
379 int freq = ( getMinFreq() / 1000 );
381 display.setNum( freq );
382 display.append( " MHz" );
383 ui->freq_min->setText( display );
385 //do the same thing for the maximum frequency
386 freq = ( getMaxFreq() / 1000 );
387 display.setNum( freq );
388 display.append( " MHz" );
389 ui->freq_max->setText( display );
391 //display the current governor
392 ui->freq_governor->setText( getScalingGovernor() );
394 //display current temperature
395 ui->cpu_temp->setText( getCPUTemp() );
397 //smart reflex button
398 if ( getSmartReflexState() == 1 )
399 ui->sr_box->setCheckState( Qt::Checked );
401 ui->sr_box->setCheckState( Qt::Unchecked );
404 //display frequency slider
405 ui->freq_adjust->setMinimum( 1 );
406 ui->freq_adjust->setMaximum( getScalingSteps() );
407 ui->freq_adjust->setInvertedAppearance( true );
408 ui->freq_adjust->setSliderPosition( getScalingStep(getMaxFreq()) );
410 //ui->retranslateUi(this);
415 * Repaints part of the GUI after the device was rotated
417 void MainWindow::orientationChanged()
421 //check whether we are using portrait or landscape mode
422 if ( usePortrait() ) {
423 //in portrait mode we want to display the large image
424 image.load( ":/img/chip256" );
426 scene.addPixmap( image );
428 ui->graphicsPortrait->setScene( &scene );
429 ui->graphicsPortrait->setMaximumSize( 256, 256 );
430 ui->graphicsLandscape->setMaximumSize( 0, 0 );
432 image.load( ":/img/chip128" );
434 scene.addPixmap( image );
436 ui->graphicsLandscape->setScene( &scene );
437 ui->graphicsLandscape->setMaximumSize( 128, 128 );
438 ui->graphicsPortrait->setMaximumSize( 0, 0 );
444 * Enables the auto-rotation feature of Maemo5 devices
446 void MainWindow::setAutoRotation()
448 #if defined(Q_WS_MAEMO_5)
449 setAttribute(Qt::WA_Maemo5AutoOrientation, true);
455 * SLOT: enable/disable overclocking.
457 void MainWindow::setOverclocking()
459 if (ui->actionOverclocking->isChecked()) {
460 #if defined(Q_WS_MAEMO_5)
461 QMaemo5InformationBox::information(this, tr( "Please note that overclocking voids your warranty and may break your device! Be careful!"), 0);
463 this->allowOverclocking = true;
465 this->allowOverclocking = false;
471 * SLOT: Enables or disables Smart Reflex(tm) after pressing sr_btn
473 void MainWindow::setSmartReflex()
475 //SmartReflex is only supported on Maemo5
476 #if defined(Q_WS_MAEMO_5)
477 if ( getSmartReflexState() == 1 )
478 callHelper( "set_sr", "off");
480 QMaemo5InformationBox::information(this, tr( "SmartReflex support is known to be unstable on some devices and may cause random reboots." ), 0);
481 callHelper( "set_sr", "on");
491 * SLOT: display the help window
493 void MainWindow::showHelp()
500 * Returns true when the device is in portrait mode
502 bool MainWindow::usePortrait()
504 QRect screenGeometry = QApplication::desktop()->screenGeometry();
505 if (screenGeometry.width() > screenGeometry.height())