2 * QCPUFreq - a simple cpufreq GUI
3 * Copyright (C) 2010-11 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.3"
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();
61 //enable save and load option on power kernels
62 if (settings->usePowerKernel() && settings->isKernelConfigInstalled()) {
63 ui->actionSave->setEnabled(true);
64 //loading presets may cause overclocking - only enable it if overclokcing is enabled
65 if (settings->useOverclocking()) {
66 ui->actionLoad->setEnabled(true);
70 //display the correct minimum frequency
73 //applies the settings from the settings dialog
76 //initialize orientation
79 //refresh UI every 10 seconds
80 refreshTimer.start( 10000 );
82 // initialize stackable help window
83 #if defined(Q_WS_MAEMO_5)
84 helpWindow.setAttribute(Qt::WA_Maemo5StackedWindow);
86 helpWindow.setWindowFlags( windowFlags() | Qt::Window );
88 //show errors about the sudo setup only once
91 //connect signals and slots
92 connect(ui->actionHelp, SIGNAL(triggered()), this, SLOT(showHelp()));
93 connect( ui->actionAbout, SIGNAL(triggered()), this, SLOT(about()) );
94 connect( ui->freq_adjust, SIGNAL(sliderReleased()), this, SLOT(adjustFreq()) );
95 connect(ui->freq_adjust, SIGNAL(valueChanged(int)), this, SLOT(showTemporaryMaxFreq()));
96 connect(QApplication::desktop(), SIGNAL(resized(int)), this, SLOT(orientationChanged()));
97 connect(ui->sr_box, SIGNAL(clicked()), this, SLOT(setSmartReflex()));
98 connect(&refreshTimer, SIGNAL(timeout()), this, SLOT(refresh()));
99 connect(ui->actionSave, SIGNAL(triggered()), this, SLOT(save()));
100 connect(ui->actionLoad, SIGNAL(triggered()), loadPresetDialog, SLOT(show()));
101 connect(ui->actionSettings, SIGNAL(triggered()), this, SLOT(showSettings()));
102 connect(settings, SIGNAL(settingsChanged()), this, SLOT(applySettings()));
103 connect(loadPresetDialog, SIGNAL(load(QString)), this, SLOT(loadPreset(QString)));
107 MainWindow::~MainWindow()
109 delete loadPresetDialog;
116 * SLOT: Displays an about box
118 void MainWindow::about()
120 QMessageBox::about(this, APPNAME " " APPVERSION, "<p style=\"align:center;\">© 2011 Daniel Klaffenbach</p>" );
126 * SLOT: Adjusts the maximum CPU frequency according to the scaler
128 void MainWindow::adjustFreq()
130 int newmax = getScalingFreq( ui->freq_adjust->sliderPosition() );
132 if (newmax == getMaxFreq() ) {
133 //we do not need to change anything in this case
139 //maxfreq should not be smaller than minfreq, because we do not want to decrease minfreq
140 if (newmax < getMinFreq())
141 newmax = getMinFreq();
143 max.setNum( newmax );
145 //check for overclocking
146 #if defined(Q_WS_MAEMO_5)
147 if (!settings->useOverclocking() && newmax > 600000) {
148 QMaemo5InformationBox::information(this, tr( "You need to enable overclocking in QCPUFreq's settings in order to set frequencies above 600MHz!"), 0);
154 //check for 599MHz <-> 600MHz problem on power kernels
155 if (max == "600000" && settings->usePowerKernel()) {
156 //we really need to set the maximum to 599MHz
160 if (settings->useConfirmation()) {
162 #if defined(Q_WS_MAEMO_5)
163 box.setAttribute(Qt::WA_Maemo5AutoOrientation, settings->useAutoRotate());
165 box.setStandardButtons(QMessageBox::Apply | QMessageBox::Cancel);
166 box.setDefaultButton(QMessageBox::Apply);
167 box.setIcon(QMessageBox::Question);
169 verboseMax.setNum( newmax/1000 );
170 box.setText( tr("Do you really want to use %1 MHz as the new maximum frequency?").arg(verboseMax) );
171 int ret = box.exec();
173 if (ret != QMessageBox::Apply) {
179 callHelper( "set_maxfreq", max );
186 * SLOT: applies the settings from the Settings dialog.
188 void MainWindow::applySettings()
191 setAdvancedTemperature();
193 //if overclocking is/was enabled we can also enable the "Load preset" option
194 if (settings->useOverclocking() && settings->usePowerKernel() && settings->isKernelConfigInstalled()) {
195 ui->actionLoad->setEnabled(true);
197 ui->actionLoad->setEnabled(false);
200 //refresh the GUI after applying the settings
206 * Calculates the minimum frequency according to scaling_min_freq and avoid_frequencies.
208 * Since this is a somewhat complex calculation it sould only be performed when it is
209 * really necessary (on startup, after loading presets, etc.).
211 void MainWindow::calculateMinFreq()
214 QString freqs = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies" );
215 QStringList freqList = freqs.split( " " );
216 //change the order of the QStringList - last element becomes first
217 for (int i=freqList.size() - 1; i>=0; --i) {
218 if (freqList.at(i) != "")
219 this->scalingFrequencies << freqList.at(i);
221 this->scalingSteps = (this->scalingFrequencies.size());
223 //set minFreq and check avoid_frequencies
224 QString min = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_min_freq" );
225 //check if avoid file exists (only on power kernel)
226 QFile file( "/sys/devices/system/cpu/cpu0/cpufreq/ondemand/avoid_frequencies" );
228 QString avoid = readSysFile( "devices/system/cpu/cpu0/cpufreq/ondemand/avoid_frequencies" );
229 QStringList avoidList = avoid.split( " " );
231 //check if min is in avoid_frequencies
232 for (int i = getScalingStep( min.toInt() ); i <= this->scalingSteps; ++i) {
233 min.setNum( getScalingFreq(i) );
234 if (!avoidList.contains(min)) {
235 this->minFreq = min.toInt();
240 this->minFreq = min.toInt();
247 * Calls the QCPUFreq helper script with "sudo action param"
249 * @param action : the action of the helper script
250 * @param param : the parameter for the action
253 int MainWindow::callHelper(QString action, QString param)
255 QStringList arguments;
257 #if defined(Q_WS_MAEMO_5)
258 //On Maemo 5 the helper script resides in /opt/usr/bin, which is usually not in $PATH
259 arguments.append( "/opt/usr/bin/QCPUFreq.helper" );
261 arguments.append( "QCPUFreq.helper" );
264 arguments.append( action );
265 arguments.append( param );
267 helperProcess.start( "sudo", arguments, QIODevice::NotOpen );
269 if ( showSudoError && !helperProcess.waitForFinished( 2000 )) {
270 //do not show this error again
271 showSudoError = false;
272 QMessageBox::critical(this, tr("QCPUFreq"), tr("There seems to be a problem with your sudo setup!"));
275 return helperProcess.exitCode();
280 * Returns the current CPU temperature
282 QString MainWindow::getCPUTemp()
284 #if defined(Q_WS_MAEMO_5)
285 QFile file( "/sys/class/power_supply/bq27200-0/temp" );
287 //check if we can read a more accurate temperature (only for power kernel)
289 return QString( readSysFile( "class/power_supply/bq27200-0/temp" ) + " " + QString::fromUtf8("\302\260") + "C" );
292 We actually only need to read the raw temperature, but it appears that by also reading temp1_input
293 the raw temperature (temp1_input_raw) is being updated more frequently.
295 readSysFile( "devices/platform/omap34xx_temp/temp1_input" );
297 //read the current system temperature
298 QString tstring = readSysFile( "devices/platform/omap34xx_temp/temp1_input_raw" );
300 return tr( "Unknown" );
302 //convert it to an integer and calculate the approx. temperature from the raw value
303 int tint = tstring.toInt();
304 tint = ( 0.65 * tint );
305 tstring.setNum(tint);
306 return QString( tstring + " " + QString::fromUtf8("\302\260") + "C" );
309 return tr( "Unknown" );
314 * Returns the maximum CPU frequency
316 int MainWindow::getMaxFreq()
318 QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_max_freq" );
324 * Returns the minimum CPU frequency
326 int MainWindow::getMinFreq()
328 return this->minFreq;
333 * Returns the CPU frequency for the specified scaling step
335 int MainWindow::getScalingFreq(int step)
340 if ( step > getScalingSteps() - 1 )
341 step = getScalingSteps() - 1;
343 return this->scalingFrequencies[ step ].toInt();
348 * Returns the name of the current CPU frequency scaling governor
350 * @return QString - name of governor
352 QString MainWindow::getScalingGovernor()
354 return readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_governor" );
359 * Returns the amount of available scaling steps.
363 int MainWindow::getScalingSteps()
365 return this->scalingSteps;
370 * Returns the scaling step for the specified frequency.
374 int MainWindow::getScalingStep( int freq )
378 return this->scalingFrequencies.indexOf(tmp) + 1;
383 * Returns the SmartReflex(tm) state
387 int MainWindow::getSmartReflexState()
389 //SmartReflex is only supprted on Maemo5
390 #if defined(Q_WS_MAEMO_5)
391 QString tmp = readSysFile( "power/sr_vdd1_autocomp" );
399 //disable UI checkbox
400 ui->sr_box->setDisabled( true );
408 * Loads a voltage preset by calling kernel-config.
410 * Available presets are:
416 * - custom -> any preset named "custom"
418 void MainWindow::loadPreset(QString presetName)
420 #if defined(Q_WS_MAEMO_5)
421 callHelper("loadpreset", presetName);
423 QMaemo5InformationBox::information(this, tr( "The preset was loaded." ), QMaemo5InformationBox::DefaultTimeout);
430 * Reads any file in /sys/
432 * \param sys_file : full path to sys file - omit "/sys/"
433 * \return content of sys file
435 QString MainWindow::readSysFile(QString sys_file)
437 QFile file( "/sys/"+sys_file );
440 if ( !file.exists() || !file.open( QIODevice::ReadOnly ) ) {
441 QMessageBox::critical(this, tr("QCPUFreq"), tr("Could not get information from /sys!"));
446 QTextStream in( &file );
447 QString txt = in.readLine();
457 * Refreshes all of the values to display
459 void MainWindow::refresh()
461 //get the current frequency and calculate the MHz value
462 int freq = ( getMinFreq() / 1000 );
464 display.setNum( freq );
465 display.append( " MHz" );
466 ui->freq_min->setText( display );
468 //do the same thing for the maximum frequency
469 freq = ( getMaxFreq() / 1000 );
470 display.setNum( freq );
471 display.append( " MHz" );
472 ui->freq_max->setText( display );
474 //display the current governor
475 ui->freq_governor->setText( getScalingGovernor() );
477 //display current temperature
478 ui->cpu_temp->setText( getCPUTemp() );
480 //smart reflex button
481 if ( getSmartReflexState() == 1 )
482 ui->sr_box->setCheckState( Qt::Checked );
484 ui->sr_box->setCheckState( Qt::Unchecked );
487 //display frequency slider
488 ui->freq_adjust->setMinimum( 1 );
489 ui->freq_adjust->setMaximum( getScalingSteps() );
490 ui->freq_adjust->setSliderPosition( getScalingStep(getMaxFreq()) );
495 * Repaints part of the GUI after the device was rotated
497 void MainWindow::orientationChanged()
501 //check whether we are using portrait or landscape mode
502 if ( usePortrait() ) {
503 //in portrait mode we want to display the large image
504 image.load( ":/img/chip256" );
506 scene.addPixmap( image );
508 ui->graphicsPortrait->setScene( &scene );
509 ui->graphicsPortrait->setMaximumSize( 256, 256 );
510 ui->graphicsLandscape->setMaximumSize( 0, 0 );
512 image.load( ":/img/chip128" );
514 scene.addPixmap( image );
516 ui->graphicsLandscape->setScene( &scene );
517 ui->graphicsLandscape->setMaximumSize( 128, 128 );
518 ui->graphicsPortrait->setMaximumSize( 0, 0 );
524 * Saves the current maximim frequency as default (only on power kernel).
526 void MainWindow::save()
528 if (settings->usePowerKernel()) {
529 callHelper( "save", "null" );
530 #if defined(Q_WS_MAEMO_5)
531 QMaemo5InformationBox::information(this, tr( "The current frequency settings have been saved as default." ), QMaemo5InformationBox::DefaultTimeout);
538 * Checks the settings if the "bq27x00_battery" needs to be loaded.
540 void MainWindow::setAdvancedTemperature()
542 if (settings->usePowerKernel() && settings->useAdvancedTemperature()) {
543 callHelper( "load_bq27", "null" );
549 * Enables or disables the auto-rotation feature of Maemo5 devices.
551 void MainWindow::setAutoRotation()
553 #if defined(Q_WS_MAEMO_5)
554 setAttribute(Qt::WA_Maemo5AutoOrientation, settings->useAutoRotate());
555 loadPresetDialog->setAttribute(Qt::WA_Maemo5AutoOrientation, settings->useAutoRotate());
556 settings->setAttribute(Qt::WA_Maemo5AutoOrientation, settings->useAutoRotate());
562 * SLOT: Enables or disables Smart Reflex(tm) after pressing sr_btn
564 void MainWindow::setSmartReflex()
566 //SmartReflex is only supported on Maemo5
567 #if defined(Q_WS_MAEMO_5)
568 if ( getSmartReflexState() == 1 )
569 callHelper( "set_sr", "off");
571 QMaemo5InformationBox::information(this, tr( "SmartReflex support is known to be unstable on some devices and may cause random reboots." ), 0);
572 callHelper( "set_sr", "on");
582 * SLOT: display the help window
584 void MainWindow::showHelp()
591 * SLOT: displays the settings widget
593 void MainWindow::showSettings()
601 * SLOT: This temporarily updates the maximum frequency while using the
604 void MainWindow::showTemporaryMaxFreq()
606 //calulate frequency from slider position
607 int newmax = getScalingFreq( ui->freq_adjust->sliderPosition() ) / 1000;
608 //convert it to a string and display it in the UI
610 display.setNum( newmax );
611 display.append( " MHz" );
612 ui->freq_max->setText( display );
617 * Returns true when the device is in portrait mode
619 bool MainWindow::usePortrait()
621 QRect screenGeometry = QApplication::desktop()->screenGeometry();
622 if (screenGeometry.width() > screenGeometry.height())