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.4"
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
42 //create UI refresh timer
44 //create a QGraphicsScene for the little chip icon
48 //show errors about the sudo setup only once
51 //this is a stacked window on Maemo 5
52 #if defined(Q_WS_MAEMO_5)
53 setAttribute(Qt::WA_Maemo5StackedWindow);
61 // setup auto rotation
64 //initialize orientation
67 //refresh UI every 10 seconds
68 refreshTimer.start( 10000 );
70 // initialize stackable help window
71 #if defined(Q_WS_MAEMO_5)
72 helpWindow.setAttribute(Qt::WA_Maemo5StackedWindow);
74 helpWindow.setWindowFlags( windowFlags() | Qt::Window );
79 //connect signals and slots
80 connect(ui->actionHelp, SIGNAL(triggered()), this, SLOT(showHelp()));
81 connect( ui->actionAbout, SIGNAL(triggered()), this, SLOT(about()) );
82 connect( ui->freq_adjust, SIGNAL(valueChanged(int)), this, SLOT(adjustFreq()) );
83 connect(QApplication::desktop(), SIGNAL(resized(int)), this, SLOT(orientationChanged()));
84 connect(ui->sr_box, SIGNAL(clicked()), this, SLOT(setSmartReflex()));
85 connect(&refreshTimer, SIGNAL(timeout()), this, SLOT(refresh()));
86 connect(ui->actionOverclocking, SIGNAL(toggled(bool)), this, SLOT(setOverclocking()));
87 connect(ui->actionSettings, SIGNAL(triggered()), this, SLOT(showSettings()));
90 //disable overclocking button on vanilla kernels
92 ui->actionOverclocking->setDisabled(true);
97 MainWindow::~MainWindow()
104 * SLOT: Displays an about box
106 void MainWindow::about()
108 QMessageBox::about(this, APPNAME " " APPVERSION, "<p style=\"align:center;\">© 2010 Daniel Klaffenbach</p>" );
114 * SLOT: Adjusts the maximum CPU frequency according to the scaler
116 void MainWindow::adjustFreq()
118 int newmax = getScalingFreq( ui->freq_adjust->sliderPosition() );
120 if (newmax == getMaxFreq() ) {
121 //we do not need to change anything in this case
127 //maxfreq should not be smaller than minfreq, because we do not want to decrease minfreq
128 if (newmax < getMinFreq())
129 newmax = getMinFreq();
131 max.setNum( newmax );
133 //check for overclocking
134 #if defined(Q_WS_MAEMO_5)
135 if (this->allowOverclocking == false && newmax > 600000) {
136 QMaemo5InformationBox::information(this, tr( "You need to enable overclocking in QCPUFreq's menu for setting frequencies above 600MHz!"), 0);
142 callHelper( "set_maxfreq", max );
149 * Calls the QCPUFreq helper script with "sudo action param"
151 * @param action : the action of the helper script
152 * @param param : the parameter for the action
155 int MainWindow::callHelper(QString action, QString param)
157 QStringList arguments;
159 #if defined(Q_WS_MAEMO_5)
160 //On Maemo 5 the helper script resides in /opt/usr/bin, which is usually not in $PATH
161 arguments.append( "/opt/usr/bin/QCPUFreq.helper" );
163 arguments.append( "QCPUFreq.helper" );
166 arguments.append( action );
167 arguments.append( param );
169 helperProcess.start( "sudo", arguments, QIODevice::NotOpen );
171 if ( showSudoError && !helperProcess.waitForFinished( 400 )) {
172 //do not show this error again
173 showSudoError = false;
174 QMessageBox::critical(this, tr("QCPUFreq"), tr("There seems to be a problem with your sudo setup!"));
177 return helperProcess.exitCode();
182 * Returns the current CPU temperature
184 QString MainWindow::getCPUTemp()
186 #if defined(Q_WS_MAEMO_5)
187 QFile file( "/sys/class/power_supply/bq27200-0/temp" );
189 //check if we can read a more accurate temperature (only for power kernel)
191 return QString( readSysFile( "class/power_supply/bq27200-0/temp" ) + " " + QString::fromUtf8("\302\260") + "C" );
194 We actually only need to read the raw temperature, but it appears that by also reading temp1_input
195 the raw temperature (temp1_input_raw) is being updated more frequently.
197 readSysFile( "devices/platform/omap34xx_temp/temp1_input" );
199 //read the current system temperature
200 QString tstring = readSysFile( "devices/platform/omap34xx_temp/temp1_input_raw" );
202 return tr( "Unknown" );
204 //convert it to an integer and calculate the approx. temperature from the raw value
205 int tint = tstring.toInt();
206 tint = ( 0.65 * tint );
207 tstring.setNum(tint);
208 return QString( tstring + " " + QString::fromUtf8("\302\260") + "C" );
211 return tr( "Unknown" );
216 * Returns the maximum CPU frequency
218 int MainWindow::getMaxFreq()
220 QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_max_freq" );
226 * Returns the minimum CPU frequency
228 int MainWindow::getMinFreq()
230 return this->minFreq;
235 * Returns the CPU frequency for the specified scaling step
237 int MainWindow::getScalingFreq(int step)
242 if ( step > getScalingSteps() - 1 )
243 step = getScalingSteps() - 1;
245 return this->scalingFrequencies[ step ].toInt();
250 * Returns the name of the current CPU frequency scaling governor
252 * @return QString - name of governor
254 QString MainWindow::getScalingGovernor()
256 return readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_governor" );
261 * Returns the amount of available scaling steps.
265 int MainWindow::getScalingSteps()
267 return this->scalingSteps;
272 * Returns the scaling step for the specified frequency.
276 int MainWindow::getScalingStep( int freq )
280 return this->scalingFrequencies.indexOf(tmp) + 1;
285 * Returns the SmartReflex(tm) state
289 int MainWindow::getSmartReflexState()
291 //SmartReflex is only supprted on Maemo5
292 #if defined(Q_WS_MAEMO_5)
293 QString tmp = readSysFile( "power/sr_vdd1_autocomp" );
301 //disable UI checkbox
302 ui->sr_box->setDisabled( true );
310 * Initializes internal variables, such as:
312 * - scalingFrequencies
316 void MainWindow::init()
319 QString freqs = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies" );
320 QStringList freqList = freqs.split( " " );
321 //change the order of the QStringList - last element becomes first
322 for (int i=freqList.size() - 1; i>=0; --i) {
323 if (freqList.at(i) != "")
324 this->scalingFrequencies << freqList.at(i);
326 this->scalingSteps = (this->scalingFrequencies.size());
328 //set minFreq and check avoid_frequencies
329 QString min = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_min_freq" );
330 //check if avoid file exists (only on power kernel)
331 QFile file( "/sys/devices/system/cpu/cpu0/cpufreq/ondemand/avoid_frequencies" );
333 QString avoid = readSysFile( "devices/system/cpu/cpu0/cpufreq/ondemand/avoid_frequencies" );
334 QStringList avoidList = avoid.split( " " );
336 //check if min is in avoid_frequencies
337 for (int i = getScalingStep( min.toInt() ); i <= this->scalingSteps; ++i) {
338 min.setNum( getScalingFreq(i) );
339 if (!avoidList.contains(min)) {
340 this->minFreq = min.toInt();
345 this->minFreq = min.toInt();
348 //check if we are using a power kernel
349 if ( getScalingFreq(getScalingSteps()) > 600000 ) {
350 this->powerKernel = true;
352 this->powerKernel = false;
358 * Reads any file in /sys/
360 * \param sys_file : full path to sys file - omit "/sys/"
361 * \return content of sys file
363 QString MainWindow::readSysFile(QString sys_file)
365 QFile file( "/sys/"+sys_file );
368 if ( !file.exists() || !file.open( QIODevice::ReadOnly ) ) {
369 QMessageBox::critical(this, tr("QCPUFreq"), tr("Could not get information from /sys!"));
374 QTextStream in( &file );
375 QString txt = in.readLine();
385 * Refreshes all of the values to display
387 void MainWindow::refresh()
389 //get the current frequency and calculate the MHz value
390 int freq = ( getMinFreq() / 1000 );
392 display.setNum( freq );
393 display.append( " MHz" );
394 ui->freq_min->setText( display );
396 //do the same thing for the maximum frequency
397 freq = ( getMaxFreq() / 1000 );
398 display.setNum( freq );
399 display.append( " MHz" );
400 ui->freq_max->setText( display );
402 //display the current governor
403 ui->freq_governor->setText( getScalingGovernor() );
405 //display current temperature
406 ui->cpu_temp->setText( getCPUTemp() );
408 //smart reflex button
409 if ( getSmartReflexState() == 1 )
410 ui->sr_box->setCheckState( Qt::Checked );
412 ui->sr_box->setCheckState( Qt::Unchecked );
415 //display frequency slider
416 ui->freq_adjust->setMinimum( 1 );
417 ui->freq_adjust->setMaximum( getScalingSteps() );
418 ui->freq_adjust->setSliderPosition( getScalingStep(getMaxFreq()) );
420 //ui->retranslateUi(this);
425 * Repaints part of the GUI after the device was rotated
427 void MainWindow::orientationChanged()
431 //check whether we are using portrait or landscape mode
432 if ( usePortrait() ) {
433 //in portrait mode we want to display the large image
434 image.load( ":/img/chip256" );
436 scene.addPixmap( image );
438 ui->graphicsPortrait->setScene( &scene );
439 ui->graphicsPortrait->setMaximumSize( 256, 256 );
440 ui->graphicsLandscape->setMaximumSize( 0, 0 );
442 image.load( ":/img/chip128" );
444 scene.addPixmap( image );
446 ui->graphicsLandscape->setScene( &scene );
447 ui->graphicsLandscape->setMaximumSize( 128, 128 );
448 ui->graphicsPortrait->setMaximumSize( 0, 0 );
454 * Enables or disables the auto-rotation feature of Maemo5 devices.
456 void MainWindow::setAutoRotation()
458 #if defined(Q_WS_MAEMO_5)
459 setAttribute(Qt::WA_Maemo5AutoOrientation, settings.useAutoRotate());
465 * SLOT: enable/disable overclocking.
467 void MainWindow::setOverclocking()
469 if (ui->actionOverclocking->isChecked()) {
470 #if defined(Q_WS_MAEMO_5)
471 QMaemo5InformationBox::information(this, tr( "Please note that overclocking voids your warranty and may break your device! Be careful!"), 0);
473 this->allowOverclocking = true;
475 this->allowOverclocking = false;
481 * SLOT: Enables or disables Smart Reflex(tm) after pressing sr_btn
483 void MainWindow::setSmartReflex()
485 //SmartReflex is only supported on Maemo5
486 #if defined(Q_WS_MAEMO_5)
487 if ( getSmartReflexState() == 1 )
488 callHelper( "set_sr", "off");
490 QMaemo5InformationBox::information(this, tr( "SmartReflex support is known to be unstable on some devices and may cause random reboots." ), 0);
491 callHelper( "set_sr", "on");
501 * SLOT: display the help window
503 void MainWindow::showHelp()
510 * SLOT: displays the settings widget
512 void MainWindow::showSettings()
520 * Returns true when the device is in portrait mode
522 bool MainWindow::usePortrait()
524 QRect screenGeometry = QApplication::desktop()->screenGeometry();
525 if (screenGeometry.width() > screenGeometry.height())