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