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 ui(new Ui::MainWindow),
36 //create helper process
37 helperProcess( this ),
38 //create a new, stackable help window
40 //create UI refresh timer
42 //create a QGraphicsScene for the little chip icon
47 //this is a stacked window on Maemo 5
48 #if defined(Q_WS_MAEMO_5)
49 setAttribute(Qt::WA_Maemo5StackedWindow);
57 // setup 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 );
75 //show errors about the sudo setup only once
78 //connect signals and slots
79 connect(ui->actionHelp, SIGNAL(triggered()), this, SLOT(showHelp()));
80 connect( ui->actionAbout, SIGNAL(triggered()), this, SLOT(about()) );
81 connect( ui->freq_adjust, SIGNAL(valueChanged(int)), this, SLOT(adjustFreq()) );
82 connect(QApplication::desktop(), SIGNAL(resized(int)), this, SLOT(orientationChanged()));
83 connect(ui->sr_box, SIGNAL(clicked()), this, SLOT(setSmartReflex()));
84 connect(&refreshTimer, SIGNAL(timeout()), this, SLOT(refresh()));
85 connect(ui->actionSettings, SIGNAL(triggered()), this, SLOT(showSettings()));
89 MainWindow::~MainWindow()
96 * SLOT: Displays an about box
98 void MainWindow::about()
100 QMessageBox::about(this, APPNAME " " APPVERSION, "<p style=\"align:center;\">© 2010 Daniel Klaffenbach</p>" );
106 * SLOT: Adjusts the maximum CPU frequency according to the scaler
108 void MainWindow::adjustFreq()
110 int newmax = getScalingFreq( ui->freq_adjust->sliderPosition() );
112 if (newmax == getMaxFreq() ) {
113 //we do not need to change anything in this case
119 //maxfreq should not be smaller than minfreq, because we do not want to decrease minfreq
120 if (newmax < getMinFreq())
121 newmax = getMinFreq();
123 max.setNum( newmax );
125 //check for overclocking
126 #if defined(Q_WS_MAEMO_5)
127 if (!settings.useOverclocking() && newmax > 600000) {
128 QMaemo5InformationBox::information(this, tr( "You need to enable overclocking in QCPUFreq's settings in order to set frequencies above 600MHz!"), 0);
134 callHelper( "set_maxfreq", max );
141 * Calls the QCPUFreq helper script with "sudo action param"
143 * @param action : the action of the helper script
144 * @param param : the parameter for the action
147 int MainWindow::callHelper(QString action, QString param)
149 QStringList arguments;
151 #if defined(Q_WS_MAEMO_5)
152 //On Maemo 5 the helper script resides in /opt/usr/bin, which is usually not in $PATH
153 arguments.append( "/opt/usr/bin/QCPUFreq.helper" );
155 arguments.append( "QCPUFreq.helper" );
158 arguments.append( action );
159 arguments.append( param );
161 helperProcess.start( "sudo", arguments, QIODevice::NotOpen );
163 if ( showSudoError && !helperProcess.waitForFinished( 400 )) {
164 //do not show this error again
165 showSudoError = false;
166 QMessageBox::critical(this, tr("QCPUFreq"), tr("There seems to be a problem with your sudo setup!"));
169 return helperProcess.exitCode();
174 * Returns the current CPU temperature
176 QString MainWindow::getCPUTemp()
178 #if defined(Q_WS_MAEMO_5)
179 QFile file( "/sys/class/power_supply/bq27200-0/temp" );
181 //check if we can read a more accurate temperature (only for power kernel)
183 return QString( readSysFile( "class/power_supply/bq27200-0/temp" ) + " " + QString::fromUtf8("\302\260") + "C" );
186 We actually only need to read the raw temperature, but it appears that by also reading temp1_input
187 the raw temperature (temp1_input_raw) is being updated more frequently.
189 readSysFile( "devices/platform/omap34xx_temp/temp1_input" );
191 //read the current system temperature
192 QString tstring = readSysFile( "devices/platform/omap34xx_temp/temp1_input_raw" );
194 return tr( "Unknown" );
196 //convert it to an integer and calculate the approx. temperature from the raw value
197 int tint = tstring.toInt();
198 tint = ( 0.65 * tint );
199 tstring.setNum(tint);
200 return QString( tstring + " " + QString::fromUtf8("\302\260") + "C" );
203 return tr( "Unknown" );
208 * Returns the maximum CPU frequency
210 int MainWindow::getMaxFreq()
212 QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_max_freq" );
218 * Returns the minimum CPU frequency
220 int MainWindow::getMinFreq()
222 return this->minFreq;
227 * Returns the CPU frequency for the specified scaling step
229 int MainWindow::getScalingFreq(int step)
234 if ( step > getScalingSteps() - 1 )
235 step = getScalingSteps() - 1;
237 return this->scalingFrequencies[ step ].toInt();
242 * Returns the name of the current CPU frequency scaling governor
244 * @return QString - name of governor
246 QString MainWindow::getScalingGovernor()
248 return readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_governor" );
253 * Returns the amount of available scaling steps.
257 int MainWindow::getScalingSteps()
259 return this->scalingSteps;
264 * Returns the scaling step for the specified frequency.
268 int MainWindow::getScalingStep( int freq )
272 return this->scalingFrequencies.indexOf(tmp) + 1;
277 * Returns the SmartReflex(tm) state
281 int MainWindow::getSmartReflexState()
283 //SmartReflex is only supprted on Maemo5
284 #if defined(Q_WS_MAEMO_5)
285 QString tmp = readSysFile( "power/sr_vdd1_autocomp" );
293 //disable UI checkbox
294 ui->sr_box->setDisabled( true );
302 * Initializes internal variables, such as:
304 * - scalingFrequencies
307 void MainWindow::init()
310 QString freqs = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies" );
311 QStringList freqList = freqs.split( " " );
312 //change the order of the QStringList - last element becomes first
313 for (int i=freqList.size() - 1; i>=0; --i) {
314 if (freqList.at(i) != "")
315 this->scalingFrequencies << freqList.at(i);
317 this->scalingSteps = (this->scalingFrequencies.size());
319 //set minFreq and check avoid_frequencies
320 QString min = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_min_freq" );
321 //check if avoid file exists (only on power kernel)
322 QFile file( "/sys/devices/system/cpu/cpu0/cpufreq/ondemand/avoid_frequencies" );
324 QString avoid = readSysFile( "devices/system/cpu/cpu0/cpufreq/ondemand/avoid_frequencies" );
325 QStringList avoidList = avoid.split( " " );
327 //check if min is in avoid_frequencies
328 for (int i = getScalingStep( min.toInt() ); i <= this->scalingSteps; ++i) {
329 min.setNum( getScalingFreq(i) );
330 if (!avoidList.contains(min)) {
331 this->minFreq = min.toInt();
336 this->minFreq = min.toInt();
342 * Reads any file in /sys/
344 * \param sys_file : full path to sys file - omit "/sys/"
345 * \return content of sys file
347 QString MainWindow::readSysFile(QString sys_file)
349 QFile file( "/sys/"+sys_file );
352 if ( !file.exists() || !file.open( QIODevice::ReadOnly ) ) {
353 QMessageBox::critical(this, tr("QCPUFreq"), tr("Could not get information from /sys!"));
358 QTextStream in( &file );
359 QString txt = in.readLine();
369 * Refreshes all of the values to display
371 void MainWindow::refresh()
373 //get the current frequency and calculate the MHz value
374 int freq = ( getMinFreq() / 1000 );
376 display.setNum( freq );
377 display.append( " MHz" );
378 ui->freq_min->setText( display );
380 //do the same thing for the maximum frequency
381 freq = ( getMaxFreq() / 1000 );
382 display.setNum( freq );
383 display.append( " MHz" );
384 ui->freq_max->setText( display );
386 //display the current governor
387 ui->freq_governor->setText( getScalingGovernor() );
389 //display current temperature
390 ui->cpu_temp->setText( getCPUTemp() );
392 //smart reflex button
393 if ( getSmartReflexState() == 1 )
394 ui->sr_box->setCheckState( Qt::Checked );
396 ui->sr_box->setCheckState( Qt::Unchecked );
399 //display frequency slider
400 ui->freq_adjust->setMinimum( 1 );
401 ui->freq_adjust->setMaximum( getScalingSteps() );
402 ui->freq_adjust->setSliderPosition( getScalingStep(getMaxFreq()) );
407 * Repaints part of the GUI after the device was rotated
409 void MainWindow::orientationChanged()
413 //check whether we are using portrait or landscape mode
414 if ( usePortrait() ) {
415 //in portrait mode we want to display the large image
416 image.load( ":/img/chip256" );
418 scene.addPixmap( image );
420 ui->graphicsPortrait->setScene( &scene );
421 ui->graphicsPortrait->setMaximumSize( 256, 256 );
422 ui->graphicsLandscape->setMaximumSize( 0, 0 );
424 image.load( ":/img/chip128" );
426 scene.addPixmap( image );
428 ui->graphicsLandscape->setScene( &scene );
429 ui->graphicsLandscape->setMaximumSize( 128, 128 );
430 ui->graphicsPortrait->setMaximumSize( 0, 0 );
436 * Enables or disables the auto-rotation feature of Maemo5 devices.
438 void MainWindow::setAutoRotation()
440 #if defined(Q_WS_MAEMO_5)
441 setAttribute(Qt::WA_Maemo5AutoOrientation, settings.useAutoRotate());
447 * SLOT: Enables or disables Smart Reflex(tm) after pressing sr_btn
449 void MainWindow::setSmartReflex()
451 //SmartReflex is only supported on Maemo5
452 #if defined(Q_WS_MAEMO_5)
453 if ( getSmartReflexState() == 1 )
454 callHelper( "set_sr", "off");
456 QMaemo5InformationBox::information(this, tr( "SmartReflex support is known to be unstable on some devices and may cause random reboots." ), 0);
457 callHelper( "set_sr", "on");
467 * SLOT: display the help window
469 void MainWindow::showHelp()
476 * SLOT: displays the settings widget
478 void MainWindow::showSettings()
486 * Returns true when the device is in portrait mode
488 bool MainWindow::usePortrait()
490 QRect screenGeometry = QApplication::desktop()->screenGeometry();
491 if (screenGeometry.width() > screenGeometry.height())