e33d4d5b1d7392663f6991e7f3b5050791e7791f
[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   * SLOT: Displays an about box
61   */
62 void MainWindow::about()
63 {
64     QMessageBox::about(this, APPNAME " " APPVERSION, "<p style=\"align:center;\">&copy; 2010 Daniel Klaffenbach</p>" );
65 }
66
67
68 /**
69   * SLOT: Adjusts the maximum CPU frequency according to the scaler
70   */
71 void MainWindow::adjustFreq()
72 {
73     int newmax = getScalingFreq( ui->freq_adjust->sliderPosition() );
74     QString max;
75     max.setNum( newmax );
76     QStringList arguments;
77 #if defined(Q_WS_MAEMO_5)
78     //on Maemo5 the set_scalingmaxfreq-Script is not in $PATH
79     arguments.append( "/opt/usr/bin/set_scalingmaxfreq" );
80 #else
81     arguments.append( "set_scalingmaxfreq" );
82 #endif
83     arguments.append( max );
84
85     //execute the scaling script
86     QProcess script;
87     script.execute( "sudo", arguments );
88
89     refresh();
90 }
91
92
93 /**
94   * Returns the current CPU frequency
95   */
96 int MainWindow::getCurFreq()
97 {
98     QString tmp = readScalingFile( "scaling_cur_freq" );
99     return tmp.toInt();
100 }
101
102 int MainWindow::getMaxFreq()
103 {
104     QString tmp = readScalingFile( "scaling_max_freq" );
105     return tmp.toInt();
106 }
107
108 int MainWindow::getMinFreq()
109 {
110     QString tmp = readScalingFile( "scaling_min_freq" );
111     return tmp.toInt();
112 }
113
114
115
116 /**
117   * Returns the CPU frequency for the specified scaling step
118   */
119 int MainWindow::getScalingFreq(int step)
120 {
121     QString tmp = readScalingFile( "scaling_available_frequencies" );
122     QStringList freqs = tmp.split( " " );
123     step = step - 1;
124     if ( step < 0 )
125          step = 0;
126     if ( step > getScalingSteps() )
127         step = getScalingSteps();
128
129     tmp = freqs[ step ];
130     return tmp.toInt();
131 }
132
133
134 QString MainWindow::getScalingGovernor()
135 {
136     return readScalingFile( "scaling_governor" );
137 }
138
139 /**
140   * Returns the amount of available scaling steps.
141   */
142 int MainWindow::getScalingSteps()
143 {
144     QString tmp = readScalingFile( "scaling_available_frequencies" );
145     QStringList freqs = tmp.split( " " );
146     return (freqs.size() - 1);
147 }
148
149
150 /**
151   * Returns the scaling step for the specified frequency.
152   */
153 int MainWindow::getScalingStep( int freq )
154 {
155     for( int i = 1; i <= getScalingSteps(); ++i ) {
156            if ( getScalingFreq(i) == freq )
157                 return i;
158     }
159
160     return 1;
161 }
162
163 QString MainWindow::readScalingFile(QString scaling_file)
164 {
165     QFile file( "/sys/devices/system/cpu/cpu0/cpufreq/"+scaling_file );
166
167     //open the file
168     if ( !file.exists() || !file.open( QIODevice::ReadOnly ) ) {
169         QMessageBox::critical(this, tr("QCPUFreq"), tr("Could not get information from /sys!"));
170         return "";
171     }
172
173     //read the file
174     QTextStream in( &file );
175     QString txt = in.readLine();
176
177     return txt;
178 }
179
180
181 /**
182   * Refreshes all of the values to display
183   */
184 void MainWindow::refresh()
185 {
186     //get the current frequency and calculate the MHz value
187     int freq = ( getMinFreq() / 1000 );
188     QString display;
189     display.setNum( freq );
190     display.append( " MHz" );
191     ui->freq_min->setText( display );
192
193     //do the same thing for the maximum frequency
194     freq = ( getMaxFreq() / 1000 );
195     display.setNum( freq );
196     display.append( " MHz" );
197     ui->freq_max->setText( display );
198
199     //display the current governor
200     ui->freq_governor->setText( getScalingGovernor() );
201
202     //display.setNum( getScalingFreq(1) );
203     //ui->freq_max->setText( display );
204     ui->freq_adjust->setMinimum( 1 );
205     ui->freq_adjust->setMaximum( getScalingSteps() );
206     ui->freq_adjust->setInvertedAppearance( true );
207     ui->freq_adjust->setSliderPosition( getScalingStep(getMaxFreq()) );
208 }
209
210
211 /**
212   * Repaints part of the GUI after the device was rotated
213   */
214 void MainWindow::orientationChanged()
215 {
216     QPixmap image;
217
218     //check whether we are using portrait or landscape mode
219     if ( usePortrait() ) {
220         //in portrait mode we want to display the large image
221         image.load( ":/img/chip256" );
222         this->scene->clear();
223         this->scene->addPixmap(  image  );
224
225         ui->graphicsPortrait->setScene( this->scene );
226         ui->graphicsPortrait->setMaximumSize( 256, 256 );
227         ui->graphicsLandscape->setMaximumSize( 0, 0 );
228     } else {
229         image.load( ":/img/chip128" );
230         this->scene->clear();
231         this->scene->addPixmap(  image  );
232
233         ui->graphicsLandscape->setScene( this->scene );
234         ui->graphicsLandscape->setMaximumSize( 128, 128 );
235         ui->graphicsPortrait->setMaximumSize( 0, 0 );
236     }
237 }
238
239
240 /**
241   * Enables the auto-rotation feature of Maemo5 devices
242   */
243 void MainWindow::setAutoRotaion()
244 {
245 #if defined(Q_WS_MAEMO_5)
246     setAttribute(Qt::WA_Maemo5AutoOrientation, true);
247 #endif
248 }
249
250
251 /**
252   * Returns true when the device is in portrait mode
253   */
254 bool MainWindow::usePortrait()
255 {
256     QRect screenGeometry = QApplication::desktop()->screenGeometry();
257     if (screenGeometry.width() > screenGeometry.height())
258         return false;
259     else
260         return true;
261 }