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.4.1"
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;
58 loadPresetDialog = new LoadPreset;
59 loadPresetDialog->hide();
63 //applies the settings from the settings dialog
66 //initialize orientation
69 //refresh UI every 10 seconds
70 refreshTimer.start( 10000 );
72 // initialize stackable help window
73 #if defined(Q_WS_MAEMO_5)
74 helpWindow.setAttribute(Qt::WA_Maemo5StackedWindow);
76 helpWindow.setWindowFlags( windowFlags() | Qt::Window );
78 //show errors about the sudo setup only once
81 //connect signals and slots
82 connect(ui->actionHelp, SIGNAL(triggered()), this, SLOT(showHelp()));
83 connect( ui->actionAbout, SIGNAL(triggered()), this, SLOT(about()) );
84 connect( ui->freq_adjust, SIGNAL(valueChanged(int)), this, SLOT(adjustFreq()) );
85 connect(QApplication::desktop(), SIGNAL(resized(int)), this, SLOT(orientationChanged()));
86 connect(ui->sr_box, SIGNAL(clicked()), this, SLOT(setSmartReflex()));
87 connect(&refreshTimer, SIGNAL(timeout()), this, SLOT(refresh()));
88 connect(ui->actionSave, SIGNAL(triggered()), this, SLOT(save()));
89 connect(ui->actionLoad, SIGNAL(triggered()), loadPresetDialog, SLOT(show()));
90 connect(ui->actionSettings, SIGNAL(triggered()), this, SLOT(showSettings()));
91 connect(settings, SIGNAL(settingsChanged()), this, SLOT(applySettings()));
92 connect(loadPresetDialog, SIGNAL(load(QString)), this, SLOT(loadPreset(QString)));
96 MainWindow::~MainWindow()
98 delete loadPresetDialog;
105 * SLOT: Displays an about box
107 void MainWindow::about()
109 QMessageBox::about(this, APPNAME " " APPVERSION, "<p style=\"align:center;\">© 2010 Daniel Klaffenbach</p>" );
115 * SLOT: Adjusts the maximum CPU frequency according to the scaler
117 void MainWindow::adjustFreq()
119 int newmax = getScalingFreq( ui->freq_adjust->sliderPosition() );
121 if (newmax == getMaxFreq() ) {
122 //we do not need to change anything in this case
128 //maxfreq should not be smaller than minfreq, because we do not want to decrease minfreq
129 if (newmax < getMinFreq())
130 newmax = getMinFreq();
132 max.setNum( newmax );
134 //check for overclocking
135 #if defined(Q_WS_MAEMO_5)
136 if (!settings->useOverclocking() && newmax > 600000) {
137 QMaemo5InformationBox::information(this, tr( "You need to enable overclocking in QCPUFreq's settings in order to set frequencies above 600MHz!"), 0);
143 //check for 599MHz <-> 600MHz problem on power kernels
144 if (max == "600000" && settings->usePowerKernel()) {
145 //we really need to set the maximum to 599MHz
149 callHelper( "set_maxfreq", max );
156 * SLOT: applies the settings from the Settings dialog.
158 void MainWindow::applySettings()
161 setAdvancedTemperature();
163 //refresh the GUI after applying the settings
169 * Calls the QCPUFreq helper script with "sudo action param"
171 * @param action : the action of the helper script
172 * @param param : the parameter for the action
175 int MainWindow::callHelper(QString action, QString param)
177 QStringList arguments;
179 #if defined(Q_WS_MAEMO_5)
180 //On Maemo 5 the helper script resides in /opt/usr/bin, which is usually not in $PATH
181 arguments.append( "/opt/usr/bin/QCPUFreq.helper" );
183 arguments.append( "QCPUFreq.helper" );
186 arguments.append( action );
187 arguments.append( param );
189 helperProcess.start( "sudo", arguments, QIODevice::NotOpen );
191 if ( showSudoError && !helperProcess.waitForFinished( 2000 )) {
192 //do not show this error again
193 showSudoError = false;
194 QMessageBox::critical(this, tr("QCPUFreq"), tr("There seems to be a problem with your sudo setup!"));
197 return helperProcess.exitCode();
202 * Returns the current CPU temperature
204 QString MainWindow::getCPUTemp()
206 #if defined(Q_WS_MAEMO_5)
207 QFile file( "/sys/class/power_supply/bq27200-0/temp" );
209 //check if we can read a more accurate temperature (only for power kernel)
211 return QString( readSysFile( "class/power_supply/bq27200-0/temp" ) + " " + QString::fromUtf8("\302\260") + "C" );
214 We actually only need to read the raw temperature, but it appears that by also reading temp1_input
215 the raw temperature (temp1_input_raw) is being updated more frequently.
217 readSysFile( "devices/platform/omap34xx_temp/temp1_input" );
219 //read the current system temperature
220 QString tstring = readSysFile( "devices/platform/omap34xx_temp/temp1_input_raw" );
222 return tr( "Unknown" );
224 //convert it to an integer and calculate the approx. temperature from the raw value
225 int tint = tstring.toInt();
226 tint = ( 0.65 * tint );
227 tstring.setNum(tint);
228 return QString( tstring + " " + QString::fromUtf8("\302\260") + "C" );
231 return tr( "Unknown" );
236 * Returns the maximum CPU frequency
238 int MainWindow::getMaxFreq()
240 QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_max_freq" );
246 * Returns the minimum CPU frequency
248 int MainWindow::getMinFreq()
250 return this->minFreq;
255 * Returns the CPU frequency for the specified scaling step
257 int MainWindow::getScalingFreq(int step)
262 if ( step > getScalingSteps() - 1 )
263 step = getScalingSteps() - 1;
265 return this->scalingFrequencies[ step ].toInt();
270 * Returns the name of the current CPU frequency scaling governor
272 * @return QString - name of governor
274 QString MainWindow::getScalingGovernor()
276 return readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_governor" );
281 * Returns the amount of available scaling steps.
285 int MainWindow::getScalingSteps()
287 return this->scalingSteps;
292 * Returns the scaling step for the specified frequency.
296 int MainWindow::getScalingStep( int freq )
300 return this->scalingFrequencies.indexOf(tmp) + 1;
305 * Returns the SmartReflex(tm) state
309 int MainWindow::getSmartReflexState()
311 //SmartReflex is only supprted on Maemo5
312 #if defined(Q_WS_MAEMO_5)
313 QString tmp = readSysFile( "power/sr_vdd1_autocomp" );
321 //disable UI checkbox
322 ui->sr_box->setDisabled( true );
330 * Initializes internal variables, such as:
332 * - scalingFrequencies
335 void MainWindow::init()
338 QString freqs = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies" );
339 QStringList freqList = freqs.split( " " );
340 //change the order of the QStringList - last element becomes first
341 for (int i=freqList.size() - 1; i>=0; --i) {
342 if (freqList.at(i) != "")
343 this->scalingFrequencies << freqList.at(i);
345 this->scalingSteps = (this->scalingFrequencies.size());
347 //set minFreq and check avoid_frequencies
348 QString min = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_min_freq" );
349 //check if avoid file exists (only on power kernel)
350 QFile file( "/sys/devices/system/cpu/cpu0/cpufreq/ondemand/avoid_frequencies" );
352 QString avoid = readSysFile( "devices/system/cpu/cpu0/cpufreq/ondemand/avoid_frequencies" );
353 QStringList avoidList = avoid.split( " " );
355 //check if min is in avoid_frequencies
356 for (int i = getScalingStep( min.toInt() ); i <= this->scalingSteps; ++i) {
357 min.setNum( getScalingFreq(i) );
358 if (!avoidList.contains(min)) {
359 this->minFreq = min.toInt();
364 this->minFreq = min.toInt();
367 //enable save and load option on power kernels
368 if (settings->usePowerKernel()) {
369 //only enable save if /usr/sbin/kernel-config is present
371 file.setFileName("/usr/sbin/kernel-config");
373 ui->actionSave->setEnabled(true);
374 ui->actionLoad->setEnabled(true);
381 * Loads a voltage preset by calling kernel-config.
383 * Available presets are:
389 * - custom -> any preset named "custom"
391 void MainWindow::loadPreset(QString presetName)
393 #if defined(Q_WS_MAEMO_5)
394 callHelper("loadpreset", presetName);
395 QMaemo5InformationBox::information(this, tr( "The preset %s was loaded." ), QMaemo5InformationBox::DefaultTimeout);
401 * Reads any file in /sys/
403 * \param sys_file : full path to sys file - omit "/sys/"
404 * \return content of sys file
406 QString MainWindow::readSysFile(QString sys_file)
408 QFile file( "/sys/"+sys_file );
411 if ( !file.exists() || !file.open( QIODevice::ReadOnly ) ) {
412 QMessageBox::critical(this, tr("QCPUFreq"), tr("Could not get information from /sys!"));
417 QTextStream in( &file );
418 QString txt = in.readLine();
428 * Refreshes all of the values to display
430 void MainWindow::refresh()
432 //get the current frequency and calculate the MHz value
433 int freq = ( getMinFreq() / 1000 );
435 display.setNum( freq );
436 display.append( " MHz" );
437 ui->freq_min->setText( display );
439 //do the same thing for the maximum frequency
440 freq = ( getMaxFreq() / 1000 );
441 display.setNum( freq );
442 display.append( " MHz" );
443 ui->freq_max->setText( display );
445 //display the current governor
446 ui->freq_governor->setText( getScalingGovernor() );
448 //display current temperature
449 ui->cpu_temp->setText( getCPUTemp() );
451 //smart reflex button
452 if ( getSmartReflexState() == 1 )
453 ui->sr_box->setCheckState( Qt::Checked );
455 ui->sr_box->setCheckState( Qt::Unchecked );
458 //display frequency slider
459 ui->freq_adjust->setMinimum( 1 );
460 ui->freq_adjust->setMaximum( getScalingSteps() );
461 ui->freq_adjust->setSliderPosition( getScalingStep(getMaxFreq()) );
466 * Repaints part of the GUI after the device was rotated
468 void MainWindow::orientationChanged()
472 //check whether we are using portrait or landscape mode
473 if ( usePortrait() ) {
474 //in portrait mode we want to display the large image
475 image.load( ":/img/chip256" );
477 scene.addPixmap( image );
479 ui->graphicsPortrait->setScene( &scene );
480 ui->graphicsPortrait->setMaximumSize( 256, 256 );
481 ui->graphicsLandscape->setMaximumSize( 0, 0 );
483 image.load( ":/img/chip128" );
485 scene.addPixmap( image );
487 ui->graphicsLandscape->setScene( &scene );
488 ui->graphicsLandscape->setMaximumSize( 128, 128 );
489 ui->graphicsPortrait->setMaximumSize( 0, 0 );
495 * Saves the current maximim frequency as default (only on power kernel).
497 void MainWindow::save()
499 if (settings->usePowerKernel()) {
500 callHelper( "save", "null" );
501 #if defined(Q_WS_MAEMO_5)
502 QMaemo5InformationBox::information(this, tr( "The current frequency settings have been saved as default." ), QMaemo5InformationBox::DefaultTimeout);
509 * Checks the settings if the "bq27x00_battery" needs to be loaded.
511 void MainWindow::setAdvancedTemperature()
513 if (settings->usePowerKernel() && settings->useAdvancedTemperature()) {
514 callHelper( "load_bq27", "null" );
520 * Enables or disables the auto-rotation feature of Maemo5 devices.
522 void MainWindow::setAutoRotation()
524 #if defined(Q_WS_MAEMO_5)
525 setAttribute(Qt::WA_Maemo5AutoOrientation, settings->useAutoRotate());
526 loadPresetDialog->setAttribute(Qt::WA_Maemo5AutoOrientation, settings->useAutoRotate());
527 settings->setAttribute(Qt::WA_Maemo5AutoOrientation, settings->useAutoRotate());
533 * SLOT: Enables or disables Smart Reflex(tm) after pressing sr_btn
535 void MainWindow::setSmartReflex()
537 //SmartReflex is only supported on Maemo5
538 #if defined(Q_WS_MAEMO_5)
539 if ( getSmartReflexState() == 1 )
540 callHelper( "set_sr", "off");
542 QMaemo5InformationBox::information(this, tr( "SmartReflex support is known to be unstable on some devices and may cause random reboots." ), 0);
543 callHelper( "set_sr", "on");
553 * SLOT: display the help window
555 void MainWindow::showHelp()
562 * SLOT: displays the settings widget
564 void MainWindow::showSettings()
572 * Returns true when the device is in portrait mode
574 bool MainWindow::usePortrait()
576 QRect screenGeometry = QApplication::desktop()->screenGeometry();
577 if (screenGeometry.width() > screenGeometry.height())