Added getSmartReflexState() method
[qcpufreq] / src / mainwindow.cpp
1 /*
2  * QCPUFreq - a simple cpufreq GUI
3  * Copyright (C) 2010 Daniel Klaffenbach <daniel.klaffenbach@cs.tu-chemnitz.de>
4  *
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.
9  *
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.
14  *
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/>.
17  */
18
19 #include "mainwindow.h"
20 #include "ui_mainwindow.h"
21
22 #include <QFile>
23 #include <QMessageBox>
24 #include <QTextStream>
25 #include <QProcess>
26 #include <QDesktopWidget>
27
28
29 #define APPNAME "QCPUFreq"
30 #define APPVERSION "0.1"
31
32 MainWindow::MainWindow(QWidget *parent) :
33     QMainWindow(parent),
34     ui(new Ui::MainWindow)
35 {
36     ui->setupUi(this);
37     refresh();
38
39     // enable auto rotation
40     setAutoRotaion();
41
42     //create a QGraphicsScene for the little chip icon
43     scene = new QGraphicsScene();
44     orientationChanged();
45
46     //connect signals and slots
47     connect( ui->actionAbout, SIGNAL(triggered()), this, SLOT(about()) );
48     connect( ui->freq_adjust, SIGNAL(valueChanged(int)), this, SLOT(adjustFreq()) );
49     connect(QApplication::desktop(), SIGNAL(resized(int)), this, SLOT(orientationChanged()));
50
51 }
52
53 MainWindow::~MainWindow()
54 {
55     delete ui;
56     delete scene;
57 }
58
59
60 /**
61   * SLOT: Displays an about box
62   */
63 void MainWindow::about()
64 {
65     QMessageBox::about(this, APPNAME " " APPVERSION, "<p style=\"align:center;\">&copy; 2010 Daniel Klaffenbach</p>" );
66 }
67
68
69 /**
70   * SLOT: Adjusts the maximum CPU frequency according to the scaler
71   */
72 void MainWindow::adjustFreq()
73 {
74     int newmax = getScalingFreq( ui->freq_adjust->sliderPosition() );
75     QString max;
76     max.setNum( newmax );
77     QStringList arguments;
78 #if defined(Q_WS_MAEMO_5)
79     //on Maemo5 the set_scalingmaxfreq-Script is not in $PATH
80     arguments.append( "/opt/usr/bin/set_scalingmaxfreq" );
81 #else
82     arguments.append( "set_scalingmaxfreq" );
83 #endif
84     arguments.append( max );
85
86     //execute the scaling script
87     QProcess script;
88     script.execute( "sudo", arguments );
89
90     refresh();
91 }
92
93
94 /**
95   * Returns the maximum CPU frequency
96   */
97 int MainWindow::getMaxFreq()
98 {
99     QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_max_freq" );
100     return tmp.toInt();
101 }
102
103
104 /**
105   * Returns the minimum CPU frequency
106   */
107 int MainWindow::getMinFreq()
108 {
109     QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_min_freq" );
110     return tmp.toInt();
111 }
112
113
114 /**
115   * Returns the CPU frequency for the specified scaling step
116   */
117 int MainWindow::getScalingFreq(int step)
118 {
119     QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies" );
120     QStringList freqs = tmp.split( " " );
121     step = step - 1;
122     if ( step < 0 )
123          step = 0;
124     if ( step > getScalingSteps() )
125         step = getScalingSteps();
126
127     tmp = freqs[ step ];
128     return tmp.toInt();
129 }
130
131
132 /**
133   * Returns the name of the current CPU frequency scaling governor
134   *
135   * \return     name of governor
136   */
137 QString MainWindow::getScalingGovernor()
138 {
139     return readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_governor" );
140 }
141
142
143 /**
144   * Returns the amount of available scaling steps.
145   */
146 int MainWindow::getScalingSteps()
147 {
148     QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies" );
149     QStringList freqs = tmp.split( " " );
150     return (freqs.size() - 1);
151 }
152
153
154 /**
155   * Returns the scaling step for the specified frequency.
156   */
157 int MainWindow::getScalingStep( int freq )
158 {
159     for( int i = 1; i <= getScalingSteps(); ++i ) {
160            if ( getScalingFreq(i) == freq )
161                 return i;
162     }
163
164     return 1;
165 }
166
167
168 /**
169   * Returns the SmartReflex(tm) state
170   *
171   * \return     0|1
172   */
173 int MainWindow::getSmartReflexState()
174 {
175 //SmartReflex is only supprted on Maemo5
176 #if defined(Q_WS_MAEMO_5)
177     QString tmp1 = readSysFile( "power/sr_vdd1_autocomp" );
178     QString tmp2 = readSysFile( "power/sr_vdd2_autocomp" );
179
180     if ( tmp1 == "1" && tmp2 == "1" )
181         return 1;
182     else
183         return 0;
184 #else
185     return 0;
186 #endif
187 }
188
189
190 /**
191   * Reads any file in /sys/
192   *
193   * \param      sys_file : full path to sys file - omit "/sys/"
194   * \return     content of sys file
195   */
196 QString MainWindow::readSysFile(QString sys_file)
197 {
198     QFile file( "/sys/"+sys_file );
199
200     //open the file
201     if ( !file.exists() || !file.open( QIODevice::ReadOnly ) ) {
202         QMessageBox::critical(this, tr("QCPUFreq"), tr("Could not get information from /sys!"));
203         return "";
204     }
205
206     //read the file
207     QTextStream in( &file );
208     QString txt = in.readLine();
209
210     return txt;
211 }
212
213
214 /**
215   * Refreshes all of the values to display
216   */
217 void MainWindow::refresh()
218 {
219     //get the current frequency and calculate the MHz value
220     int freq = ( getMinFreq() / 1000 );
221     QString display;
222     display.setNum( freq );
223     display.append( " MHz" );
224     ui->freq_min->setText( display );
225
226     //do the same thing for the maximum frequency
227     freq = ( getMaxFreq() / 1000 );
228     display.setNum( freq );
229     display.append( " MHz" );
230     ui->freq_max->setText( display );
231
232     //display the current governor
233     ui->freq_governor->setText( getScalingGovernor() );
234
235     //smart reflex button
236     if ( getSmartReflexState() == 1 ) {
237         ui->sr_btn->setDown( true );
238         ui->sr_btn->setText( tr( "Enabled" ) );
239     } else {
240         ui->sr_btn->setDown( false );
241         ui->sr_btn->setText( tr( "Disabled" ) );
242     }
243
244
245     //display frequency slider
246     ui->freq_adjust->setMinimum( 1 );
247     ui->freq_adjust->setMaximum( getScalingSteps() );
248     ui->freq_adjust->setInvertedAppearance( true );
249     ui->freq_adjust->setSliderPosition( getScalingStep(getMaxFreq()) );
250 }
251
252
253 /**
254   * Repaints part of the GUI after the device was rotated
255   */
256 void MainWindow::orientationChanged()
257 {
258     QPixmap image;
259
260     //check whether we are using portrait or landscape mode
261     if ( usePortrait() ) {
262         //in portrait mode we want to display the large image
263         image.load( ":/img/chip256" );
264         this->scene->clear();
265         this->scene->addPixmap(  image  );
266
267         ui->graphicsPortrait->setScene( this->scene );
268         ui->graphicsPortrait->setMaximumSize( 256, 256 );
269         ui->graphicsLandscape->setMaximumSize( 0, 0 );
270     } else {
271         image.load( ":/img/chip128" );
272         this->scene->clear();
273         this->scene->addPixmap(  image  );
274
275         ui->graphicsLandscape->setScene( this->scene );
276         ui->graphicsLandscape->setMaximumSize( 128, 128 );
277         ui->graphicsPortrait->setMaximumSize( 0, 0 );
278     }
279 }
280
281
282 /**
283   * Enables the auto-rotation feature of Maemo5 devices
284   */
285 void MainWindow::setAutoRotaion()
286 {
287 #if defined(Q_WS_MAEMO_5)
288     setAttribute(Qt::WA_Maemo5AutoOrientation, true);
289 #endif
290 }
291
292
293 /**
294   * Returns true when the device is in portrait mode
295   */
296 bool MainWindow::usePortrait()
297 {
298     QRect screenGeometry = QApplication::desktop()->screenGeometry();
299     if (screenGeometry.width() > screenGeometry.height())
300         return false;
301     else
302         return true;
303 }