Renamed readScalingFile() to readSysFile()
[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 QString MainWindow::getScalingGovernor()
133 {
134     return readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_governor" );
135 }
136
137 /**
138   * Returns the amount of available scaling steps.
139   */
140 int MainWindow::getScalingSteps()
141 {
142     QString tmp = readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies" );
143     QStringList freqs = tmp.split( " " );
144     return (freqs.size() - 1);
145 }
146
147
148 /**
149   * Returns the scaling step for the specified frequency.
150   */
151 int MainWindow::getScalingStep( int freq )
152 {
153     for( int i = 1; i <= getScalingSteps(); ++i ) {
154            if ( getScalingFreq(i) == freq )
155                 return i;
156     }
157
158     return 1;
159 }
160
161 QString MainWindow::readSysFile(QString sys_file)
162 {
163     QFile file( "/sys/"+sys_file );
164
165     //open the file
166     if ( !file.exists() || !file.open( QIODevice::ReadOnly ) ) {
167         QMessageBox::critical(this, tr("QCPUFreq"), tr("Could not get information from /sys!"));
168         return "";
169     }
170
171     //read the file
172     QTextStream in( &file );
173     QString txt = in.readLine();
174
175     return txt;
176 }
177
178
179 /**
180   * Refreshes all of the values to display
181   */
182 void MainWindow::refresh()
183 {
184     //get the current frequency and calculate the MHz value
185     int freq = ( getMinFreq() / 1000 );
186     QString display;
187     display.setNum( freq );
188     display.append( " MHz" );
189     ui->freq_min->setText( display );
190
191     //do the same thing for the maximum frequency
192     freq = ( getMaxFreq() / 1000 );
193     display.setNum( freq );
194     display.append( " MHz" );
195     ui->freq_max->setText( display );
196
197     //display the current governor
198     ui->freq_governor->setText( getScalingGovernor() );
199
200     //display.setNum( getScalingFreq(1) );
201     //ui->freq_max->setText( display );
202     ui->freq_adjust->setMinimum( 1 );
203     ui->freq_adjust->setMaximum( getScalingSteps() );
204     ui->freq_adjust->setInvertedAppearance( true );
205     ui->freq_adjust->setSliderPosition( getScalingStep(getMaxFreq()) );
206 }
207
208
209 /**
210   * Repaints part of the GUI after the device was rotated
211   */
212 void MainWindow::orientationChanged()
213 {
214     QPixmap image;
215
216     //check whether we are using portrait or landscape mode
217     if ( usePortrait() ) {
218         //in portrait mode we want to display the large image
219         image.load( ":/img/chip256" );
220         this->scene->clear();
221         this->scene->addPixmap(  image  );
222
223         ui->graphicsPortrait->setScene( this->scene );
224         ui->graphicsPortrait->setMaximumSize( 256, 256 );
225         ui->graphicsLandscape->setMaximumSize( 0, 0 );
226     } else {
227         image.load( ":/img/chip128" );
228         this->scene->clear();
229         this->scene->addPixmap(  image  );
230
231         ui->graphicsLandscape->setScene( this->scene );
232         ui->graphicsLandscape->setMaximumSize( 128, 128 );
233         ui->graphicsPortrait->setMaximumSize( 0, 0 );
234     }
235 }
236
237
238 /**
239   * Enables the auto-rotation feature of Maemo5 devices
240   */
241 void MainWindow::setAutoRotaion()
242 {
243 #if defined(Q_WS_MAEMO_5)
244     setAttribute(Qt::WA_Maemo5AutoOrientation, true);
245 #endif
246 }
247
248
249 /**
250   * Returns true when the device is in portrait mode
251   */
252 bool MainWindow::usePortrait()
253 {
254     QRect screenGeometry = QApplication::desktop()->screenGeometry();
255     if (screenGeometry.width() > screenGeometry.height())
256         return false;
257     else
258         return true;
259 }