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"
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
46 //this is a stacked window on Maemo 5
47 #if defined(Q_WS_MAEMO_5)
48 setAttribute(Qt::WA_Maemo5StackedWindow);
54 settings = new Settings;
59 //applies the settings from the settings dialog
62 //initialize orientation
65 //refresh UI every 10 seconds
66 refreshTimer.start( 10000 );
68 // initialize stackable help window
69 #if defined(Q_WS_MAEMO_5)
70 helpWindow.setAttribute(Qt::WA_Maemo5StackedWindow);
72 helpWindow.setWindowFlags( windowFlags() | Qt::Window );
74 //show errors about the sudo setup only once
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->actionSettings, SIGNAL(triggered()), this, SLOT(showSettings()));
85 connect(settings, SIGNAL(settingsChanged()), this, SLOT(applySettings()));
89 MainWindow::~MainWindow()
97 * SLOT: Displays an about box
99 void MainWindow::about()
101 QMessageBox::about(this, APPNAME " " APPVERSION, "<p style=\"align:center;\">© 2010 Daniel Klaffenbach</p>" );
107 * SLOT: Adjusts the maximum CPU frequency according to the scaler
109 void MainWindow::adjustFreq()
111 int newmax = getScalingFreq( ui->freq_adjust->sliderPosition() );
113 if (newmax == getMaxFreq() ) {
114 //we do not need to change anything in this case
120 //maxfreq should not be smaller than minfreq, because we do not want to decrease minfreq
121 if (newmax < getMinFreq())
122 newmax = getMinFreq();
124 max.setNum( newmax );
126 //check for overclocking
127 #if defined(Q_WS_MAEMO_5)
128 if (!settings->useOverclocking() && newmax > 600000) {
129 QMaemo5InformationBox::information(this, tr( "You need to enable overclocking in QCPUFreq's settings in order to set frequencies above 600MHz!"), 0);
135 callHelper( "set_maxfreq", max );
142 * SLOT: applies the settings from the Settings dialog.
144 void MainWindow::applySettings()
147 setAdvancedTemperature();
149 //refresh the GUI after applying the settings
155 * Calls the QCPUFreq helper script with "sudo action param"
157 * @param action : the action of the helper script
158 * @param param : the parameter for the action
161 int MainWindow::callHelper(QString action, QString param)
163 QStringList arguments;
165 #if defined(Q_WS_MAEMO_5)
166 //On Maemo 5 the helper script resides in /opt/usr/bin, which is usually not in $PATH
167 arguments.append( "/opt/usr/bin/QCPUFreq.helper" );
169 arguments.append( "QCPUFreq.helper" );
172 arguments.append( action );
173 arguments.append( param );
175 helperProcess.start( "sudo", arguments, QIODevice::NotOpen );
177 if ( showSudoError && !helperProcess.waitForFinished( 400 )) {
178 //do not show this error again
179 showSudoError = false;
180 QMessageBox::critical(this, tr("QCPUFreq"), tr("There seems to be a problem with your sudo setup!"));
183 return helperProcess.exitCode();
188 * Returns the current CPU temperature
190 QString MainWindow::getCPUTemp()
192 #if defined(Q_WS_MAEMO_5)
193 QFile file( "/sys/class/power_supply/bq27200-0/temp" );
195 //check if we can read a more accurate temperature (only for power kernel)
197 return QString( readSysFile( "class/power_supply/bq27200-0/temp" ) + " " + QString::fromUtf8("\302\260") + "C" );
200 We actually only need to read the raw temperature, but it appears that by also reading temp1_input
201 the raw temperature (temp1_input_raw) is being updated more frequently.
203 readSysFile( "devices/platform/omap34xx_temp/temp1_input" );
205 //read the current system temperature
206 QString tstring = readSysFile( "devices/platform/omap34xx_temp/temp1_input_raw" );
208 return tr( "Unknown" );
210 //convert it to an integer and calculate the approx. temperature from the raw value
211 int tint = tstring.toInt();
212 tint = ( 0.65 * tint );
213 tstring.setNum(tint);
214 return QString( tstring + " " + QString::fromUtf8("\302\260") + "C" );
217 return tr( "Unknown" );
222 * Returns the maximum CPU frequency
224 int MainWindow::getMaxFreq()
226 QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_max_freq" );
232 * Returns the minimum CPU frequency
234 int MainWindow::getMinFreq()
236 return this->minFreq;
241 * Returns the CPU frequency for the specified scaling step
243 int MainWindow::getScalingFreq(int step)
248 if ( step > getScalingSteps() - 1 )
249 step = getScalingSteps() - 1;
251 return this->scalingFrequencies[ step ].toInt();
256 * Returns the name of the current CPU frequency scaling governor
258 * @return QString - name of governor
260 QString MainWindow::getScalingGovernor()
262 return readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_governor" );
267 * Returns the amount of available scaling steps.
271 int MainWindow::getScalingSteps()
273 return this->scalingSteps;
278 * Returns the scaling step for the specified frequency.
282 int MainWindow::getScalingStep( int freq )
286 return this->scalingFrequencies.indexOf(tmp) + 1;
291 * Returns the SmartReflex(tm) state
295 int MainWindow::getSmartReflexState()
297 //SmartReflex is only supprted on Maemo5
298 #if defined(Q_WS_MAEMO_5)
299 QString tmp = readSysFile( "power/sr_vdd1_autocomp" );
307 //disable UI checkbox
308 ui->sr_box->setDisabled( true );
316 * Initializes internal variables, such as:
318 * - scalingFrequencies
321 void MainWindow::init()
324 QString freqs = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies" );
325 QStringList freqList = freqs.split( " " );
326 //change the order of the QStringList - last element becomes first
327 for (int i=freqList.size() - 1; i>=0; --i) {
328 if (freqList.at(i) != "")
329 this->scalingFrequencies << freqList.at(i);
331 this->scalingSteps = (this->scalingFrequencies.size());
333 //set minFreq and check avoid_frequencies
334 QString min = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_min_freq" );
335 //check if avoid file exists (only on power kernel)
336 QFile file( "/sys/devices/system/cpu/cpu0/cpufreq/ondemand/avoid_frequencies" );
338 QString avoid = readSysFile( "devices/system/cpu/cpu0/cpufreq/ondemand/avoid_frequencies" );
339 QStringList avoidList = avoid.split( " " );
341 //check if min is in avoid_frequencies
342 for (int i = getScalingStep( min.toInt() ); i <= this->scalingSteps; ++i) {
343 min.setNum( getScalingFreq(i) );
344 if (!avoidList.contains(min)) {
345 this->minFreq = min.toInt();
350 this->minFreq = min.toInt();
356 * Reads any file in /sys/
358 * \param sys_file : full path to sys file - omit "/sys/"
359 * \return content of sys file
361 QString MainWindow::readSysFile(QString sys_file)
363 QFile file( "/sys/"+sys_file );
366 if ( !file.exists() || !file.open( QIODevice::ReadOnly ) ) {
367 QMessageBox::critical(this, tr("QCPUFreq"), tr("Could not get information from /sys!"));
372 QTextStream in( &file );
373 QString txt = in.readLine();
383 * Refreshes all of the values to display
385 void MainWindow::refresh()
387 //get the current frequency and calculate the MHz value
388 int freq = ( getMinFreq() / 1000 );
390 display.setNum( freq );
391 display.append( " MHz" );
392 ui->freq_min->setText( display );
394 //do the same thing for the maximum frequency
395 freq = ( getMaxFreq() / 1000 );
396 display.setNum( freq );
397 display.append( " MHz" );
398 ui->freq_max->setText( display );
400 //display the current governor
401 ui->freq_governor->setText( getScalingGovernor() );
403 //display current temperature
404 ui->cpu_temp->setText( getCPUTemp() );
406 //smart reflex button
407 if ( getSmartReflexState() == 1 )
408 ui->sr_box->setCheckState( Qt::Checked );
410 ui->sr_box->setCheckState( Qt::Unchecked );
413 //display frequency slider
414 ui->freq_adjust->setMinimum( 1 );
415 ui->freq_adjust->setMaximum( getScalingSteps() );
416 ui->freq_adjust->setSliderPosition( getScalingStep(getMaxFreq()) );
421 * Repaints part of the GUI after the device was rotated
423 void MainWindow::orientationChanged()
427 //check whether we are using portrait or landscape mode
428 if ( usePortrait() ) {
429 //in portrait mode we want to display the large image
430 image.load( ":/img/chip256" );
432 scene.addPixmap( image );
434 ui->graphicsPortrait->setScene( &scene );
435 ui->graphicsPortrait->setMaximumSize( 256, 256 );
436 ui->graphicsLandscape->setMaximumSize( 0, 0 );
438 image.load( ":/img/chip128" );
440 scene.addPixmap( image );
442 ui->graphicsLandscape->setScene( &scene );
443 ui->graphicsLandscape->setMaximumSize( 128, 128 );
444 ui->graphicsPortrait->setMaximumSize( 0, 0 );
450 * Checks the settings if the "bq27x00_battery" needs to be loaded.
452 void MainWindow::setAdvancedTemperature()
454 if (settings->usePowerKernel() && settings->useAdvancedTemperature()) {
455 callHelper( "load_bq27", "null" );
461 * Enables or disables the auto-rotation feature of Maemo5 devices.
463 void MainWindow::setAutoRotation()
465 #if defined(Q_WS_MAEMO_5)
466 setAttribute(Qt::WA_Maemo5AutoOrientation, settings->useAutoRotate());
472 * SLOT: Enables or disables Smart Reflex(tm) after pressing sr_btn
474 void MainWindow::setSmartReflex()
476 //SmartReflex is only supported on Maemo5
477 #if defined(Q_WS_MAEMO_5)
478 if ( getSmartReflexState() == 1 )
479 callHelper( "set_sr", "off");
481 QMaemo5InformationBox::information(this, tr( "SmartReflex support is known to be unstable on some devices and may cause random reboots." ), 0);
482 callHelper( "set_sr", "on");
492 * SLOT: display the help window
494 void MainWindow::showHelp()
501 * SLOT: displays the settings widget
503 void MainWindow::showSettings()
511 * Returns true when the device is in portrait mode
513 bool MainWindow::usePortrait()
515 QRect screenGeometry = QApplication::desktop()->screenGeometry();
516 if (screenGeometry.width() > screenGeometry.height())