2 * QCPUFreq - a simple cpufreq GUI
3 * Copyright (C) 2010 Daniel Klaffenbach <danielklaffenbach@gmail.com>
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.99"
33 MainWindow::MainWindow(QWidget *parent) :
35 //create helper process
36 helperProcess( this ),
37 //create a new, stackable help window
39 //create UI refresh timer
41 //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);
58 // setup auto rotation
61 //initialize orientation
64 //refresh UI every 10 seconds
65 refreshTimer.start( 10000 );
67 // initialize stackable help window
68 #if defined(Q_WS_MAEMO_5)
69 helpWindow.setAttribute(Qt::WA_Maemo5StackedWindow);
71 helpWindow.setWindowFlags( windowFlags() | Qt::Window );
76 //connect signals and slots
77 connect(ui->actionHelp, SIGNAL(triggered()), this, SLOT(showHelp()));
78 connect( ui->actionAbout, SIGNAL(triggered()), this, SLOT(about()) );
79 connect( ui->freq_adjust, SIGNAL(valueChanged(int)), this, SLOT(adjustFreq()) );
80 connect(QApplication::desktop(), SIGNAL(resized(int)), this, SLOT(orientationChanged()));
81 connect(ui->sr_box, SIGNAL(clicked()), this, SLOT(setSmartReflex()));
82 connect(&refreshTimer, SIGNAL(timeout()), this, SLOT(refresh()));
83 connect(ui->actionSettings, SIGNAL(triggered()), this, SLOT(showSettings()));
87 MainWindow::~MainWindow()
94 * SLOT: Displays an about box
96 void MainWindow::about()
98 QMessageBox::about(this, APPNAME " " APPVERSION, "<p style=\"align:center;\">© 2010 Daniel Klaffenbach</p>" );
104 * SLOT: Adjusts the maximum CPU frequency according to the scaler
106 void MainWindow::adjustFreq()
108 int newmax = getScalingFreq( ui->freq_adjust->sliderPosition() );
110 if (newmax == getMaxFreq() ) {
111 //we do not need to change anything in this case
117 //maxfreq should not be smaller than minfreq, because we do not want to decrease minfreq
118 if (newmax < getMinFreq())
119 newmax = getMinFreq();
121 max.setNum( newmax );
123 //check for overclocking
124 #if defined(Q_WS_MAEMO_5)
125 if (!settings.useOverclocking() && newmax > 600000) {
126 QMaemo5InformationBox::information(this, tr( "You need to enable overclocking in QCPUFreq's settings in order to set frequencies above 600MHz!"), 0);
132 callHelper( "set_maxfreq", max );
139 * Calls the QCPUFreq helper script with "sudo action param"
141 * @param action : the action of the helper script
142 * @param param : the parameter for the action
145 int MainWindow::callHelper(QString action, QString param)
147 QStringList arguments;
149 #if defined(Q_WS_MAEMO_5)
150 //On Maemo 5 the helper script resides in /opt/usr/bin, which is usually not in $PATH
151 arguments.append( "/opt/usr/bin/QCPUFreq.helper" );
153 arguments.append( "QCPUFreq.helper" );
156 arguments.append( action );
157 arguments.append( param );
159 helperProcess.start( "sudo", arguments, QIODevice::NotOpen );
161 if ( showSudoError && !helperProcess.waitForFinished( 400 )) {
162 //do not show this error again
163 showSudoError = false;
164 QMessageBox::critical(this, tr("QCPUFreq"), tr("There seems to be a problem with your sudo setup!"));
167 return helperProcess.exitCode();
172 * Returns the current CPU temperature
174 QString MainWindow::getCPUTemp()
176 #if defined(Q_WS_MAEMO_5)
177 QFile file( "/sys/class/power_supply/bq27200-0/temp" );
179 //check if we can read a more accurate temperature (only for power kernel)
181 return QString( readSysFile( "class/power_supply/bq27200-0/temp" ) + " " + QString::fromUtf8("\302\260") + "C" );
184 We actually only need to read the raw temperature, but it appears that by also reading temp1_input
185 the raw temperature (temp1_input_raw) is being updated more frequently.
187 readSysFile( "devices/platform/omap34xx_temp/temp1_input" );
189 //read the current system temperature
190 QString tstring = readSysFile( "devices/platform/omap34xx_temp/temp1_input_raw" );
192 return tr( "Unknown" );
194 //convert it to an integer and calculate the approx. temperature from the raw value
195 int tint = tstring.toInt();
196 tint = ( 0.65 * tint );
197 tstring.setNum(tint);
198 return QString( tstring + " " + QString::fromUtf8("\302\260") + "C" );
201 return tr( "Unknown" );
206 * Returns the maximum CPU frequency
208 int MainWindow::getMaxFreq()
210 QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_max_freq" );
216 * Returns the minimum CPU frequency
218 int MainWindow::getMinFreq()
220 return this->minFreq;
225 * Returns the CPU frequency for the specified scaling step
227 int MainWindow::getScalingFreq(int step)
232 if ( step > getScalingSteps() - 1 )
233 step = getScalingSteps() - 1;
235 return this->scalingFrequencies[ step ].toInt();
240 * Returns the name of the current CPU frequency scaling governor
242 * @return QString - name of governor
244 QString MainWindow::getScalingGovernor()
246 return readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_governor" );
251 * Returns the amount of available scaling steps.
255 int MainWindow::getScalingSteps()
257 return this->scalingSteps;
262 * Returns the scaling step for the specified frequency.
266 int MainWindow::getScalingStep( int freq )
270 return this->scalingFrequencies.indexOf(tmp) + 1;
275 * Returns the SmartReflex(tm) state
279 int MainWindow::getSmartReflexState()
281 //SmartReflex is only supprted on Maemo5
282 #if defined(Q_WS_MAEMO_5)
283 QString tmp = readSysFile( "power/sr_vdd1_autocomp" );
291 //disable UI checkbox
292 ui->sr_box->setDisabled( true );
300 * Initializes internal variables, such as:
302 * - scalingFrequencies
305 void MainWindow::init()
308 QString freqs = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies" );
309 QStringList freqList = freqs.split( " " );
310 //change the order of the QStringList - last element becomes first
311 for (int i=freqList.size() - 1; i>=0; --i) {
312 if (freqList.at(i) != "")
313 this->scalingFrequencies << freqList.at(i);
315 this->scalingSteps = (this->scalingFrequencies.size());
317 //set minFreq and check avoid_frequencies
318 QString min = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_min_freq" );
319 //check if avoid file exists (only on power kernel)
320 QFile file( "/sys/devices/system/cpu/cpu0/cpufreq/ondemand/avoid_frequencies" );
322 QString avoid = readSysFile( "devices/system/cpu/cpu0/cpufreq/ondemand/avoid_frequencies" );
323 QStringList avoidList = avoid.split( " " );
325 //check if min is in avoid_frequencies
326 for (int i = getScalingStep( min.toInt() ); i <= this->scalingSteps; ++i) {
327 min.setNum( getScalingFreq(i) );
328 if (!avoidList.contains(min)) {
329 this->minFreq = min.toInt();
334 this->minFreq = min.toInt();
340 * Reads any file in /sys/
342 * \param sys_file : full path to sys file - omit "/sys/"
343 * \return content of sys file
345 QString MainWindow::readSysFile(QString sys_file)
347 QFile file( "/sys/"+sys_file );
350 if ( !file.exists() || !file.open( QIODevice::ReadOnly ) ) {
351 QMessageBox::critical(this, tr("QCPUFreq"), tr("Could not get information from /sys!"));
356 QTextStream in( &file );
357 QString txt = in.readLine();
367 * Refreshes all of the values to display
369 void MainWindow::refresh()
371 //get the current frequency and calculate the MHz value
372 int freq = ( getMinFreq() / 1000 );
374 display.setNum( freq );
375 display.append( " MHz" );
376 ui->freq_min->setText( display );
378 //do the same thing for the maximum frequency
379 freq = ( getMaxFreq() / 1000 );
380 display.setNum( freq );
381 display.append( " MHz" );
382 ui->freq_max->setText( display );
384 //display the current governor
385 ui->freq_governor->setText( getScalingGovernor() );
387 //display current temperature
388 ui->cpu_temp->setText( getCPUTemp() );
390 //smart reflex button
391 if ( getSmartReflexState() == 1 )
392 ui->sr_box->setCheckState( Qt::Checked );
394 ui->sr_box->setCheckState( Qt::Unchecked );
397 //display frequency slider
398 ui->freq_adjust->setMinimum( 1 );
399 ui->freq_adjust->setMaximum( getScalingSteps() );
400 ui->freq_adjust->setSliderPosition( getScalingStep(getMaxFreq()) );
405 * Repaints part of the GUI after the device was rotated
407 void MainWindow::orientationChanged()
411 //check whether we are using portrait or landscape mode
412 if ( usePortrait() ) {
413 //in portrait mode we want to display the large image
414 image.load( ":/img/chip256" );
416 scene.addPixmap( image );
418 ui->graphicsPortrait->setScene( &scene );
419 ui->graphicsPortrait->setMaximumSize( 256, 256 );
420 ui->graphicsLandscape->setMaximumSize( 0, 0 );
422 image.load( ":/img/chip128" );
424 scene.addPixmap( image );
426 ui->graphicsLandscape->setScene( &scene );
427 ui->graphicsLandscape->setMaximumSize( 128, 128 );
428 ui->graphicsPortrait->setMaximumSize( 0, 0 );
434 * Enables or disables the auto-rotation feature of Maemo5 devices.
436 void MainWindow::setAutoRotation()
438 #if defined(Q_WS_MAEMO_5)
439 setAttribute(Qt::WA_Maemo5AutoOrientation, settings.useAutoRotate());
445 * SLOT: Enables or disables Smart Reflex(tm) after pressing sr_btn
447 void MainWindow::setSmartReflex()
449 //SmartReflex is only supported on Maemo5
450 #if defined(Q_WS_MAEMO_5)
451 if ( getSmartReflexState() == 1 )
452 callHelper( "set_sr", "off");
454 QMaemo5InformationBox::information(this, tr( "SmartReflex support is known to be unstable on some devices and may cause random reboots." ), 0);
455 callHelper( "set_sr", "on");
465 * SLOT: display the help window
467 void MainWindow::showHelp()
474 * SLOT: displays the settings widget
476 void MainWindow::showSettings()
484 * Returns true when the device is in portrait mode
486 bool MainWindow::usePortrait()
488 QRect screenGeometry = QApplication::desktop()->screenGeometry();
489 if (screenGeometry.width() > screenGeometry.height())