summaryrefslogtreecommitdiff
path: root/noncore/settings/sysinfo/load.cpp
Unidiff
Diffstat (limited to 'noncore/settings/sysinfo/load.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/settings/sysinfo/load.cpp207
1 files changed, 207 insertions, 0 deletions
diff --git a/noncore/settings/sysinfo/load.cpp b/noncore/settings/sysinfo/load.cpp
new file mode 100644
index 0000000..0fcfa6b
--- a/dev/null
+++ b/noncore/settings/sysinfo/load.cpp
@@ -0,0 +1,207 @@
1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved.
3**
4** This file is part of Qtopia Environment.
5**
6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15**
16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you.
18**
19**********************************************************************/
20
21#include <stdio.h>
22#include <qlayout.h>
23#include <qlabel.h>
24#include <qpainter.h>
25#include <qpixmap.h>
26#include <qtimer.h>
27#include <qfile.h>
28#include <qtextstream.h>
29#include "load.h"
30
31LoadInfo::LoadInfo( QWidget *parent, const char *name, WFlags f )
32 : QWidget( parent, name, f )
33{
34 QVBoxLayout *vb = new QVBoxLayout( this, 6 );
35
36 QString cpuInfo = getCpuInfo();
37 if ( !cpuInfo.isNull() )
38 vb->addWidget( new QLabel( cpuInfo, this ) );
39 vb->addWidget( new Load( this ), 100 );
40 QLabel *l = new QLabel( this );
41 l->setPixmap( makeLabel( red, tr("Application CPU usage (%)") ) );
42 vb->addWidget( l, 1 );
43 l = new QLabel( this );
44 l->setPixmap( makeLabel( green, tr("System CPU usage (%)") ) );
45 vb->addWidget( l, 1 );
46 vb->addStretch(50);
47}
48
49QPixmap LoadInfo::makeLabel( const QColor &col, const QString &text )
50{
51 int h = fontMetrics().height();
52 QPixmap pm( 20 + fontMetrics().width( text ), h );
53 QPainter p( &pm );
54 p.fillRect( pm.rect(), colorGroup().background() );
55 p.fillRect( 0, h/2-4, 18, h/2+3, black );
56 p.setPen( col );
57 p.drawLine( 2, h/2, 15, h/2 );
58 p.setPen( colorGroup().text() );
59 p.drawText( 20, fontMetrics().ascent(), text );
60
61 return pm;
62}
63
64QString LoadInfo::getCpuInfo()
65{
66 bool haveInfo = FALSE;
67 QString info = tr("Type: ");
68 QFile f( "/proc/cpuinfo" );
69 if ( f.open( IO_ReadOnly ) ) {
70 QTextStream ts( &f );
71
72 while ( !ts.atEnd() ) {
73 QString s = ts.readLine();
74 if ( s.find( "model name" ) == 0 ) {
75 info += s.mid( s.find( ':' ) + 2 );
76 haveInfo = TRUE;
77 } else if ( s.find( "cpu MHz" ) == 0 ) {
78 double mhz = s.mid( s.find( ':' ) + 2 ).toDouble();
79 info += " " + QString::number( mhz, 'f', 0 );
80 info += "MHz";
81 break;
82 } else if ( s.find( "Processor" ) == 0 ) {
83 info += s.mid( s.find( ':' ) + 2 );
84 haveInfo = TRUE;
85 break;
86#ifdef __MIPSEL__
87 } else if ( s.find( "cpu model" ) == 0 ) {
88 info += " " + s.mid( s.find( ':' ) + 2 );
89 break;
90 } else if ( s.find( "cpu" ) == 0 ) {
91 info += s.mid( s.find( ':' ) + 2 );
92 haveInfo = TRUE;
93#endif
94 }
95 }
96 }
97
98 if ( !haveInfo )
99 info = QString();
100
101 return info;
102}
103
104Load::Load( QWidget *parent, const char *name, WFlags f )
105 : QWidget( parent, name, f )
106{
107 setMinimumHeight( 30 );
108 setBackgroundColor( black );
109 points = 100;
110 setMinimumWidth( points );
111 userLoad = new double [points];
112 systemLoad = new double [points];
113 for ( int i = 0; i < points; i++ ) {
114 userLoad[i] = 0.0;
115 systemLoad[i] = 0.0;
116 }
117 maxLoad = 1.3;
118 QTimer *timer = new QTimer( this );
119 connect( timer, SIGNAL(timeout()), SLOT(timeout()) );
120 timer->start( 2000 );
121 gettimeofday( &last, 0 );
122 first = TRUE;
123 timeout();
124}
125
126void Load::paintEvent( QPaintEvent *ev )
127{
128 QPainter p( this );
129
130 int h = height() - 5;
131
132 int mult = (int)(h / maxLoad);
133
134 p.setPen( gray );
135 p.drawLine( 0, h - mult, width(), h - mult );
136 p.drawText( 0, h - mult, "100" );
137 p.drawText( 0, h, "0" );
138
139 p.setPen( green );
140 for ( int i = 1; i < points; i++ ) {
141 int x1 = (i - 1) * width() / points;
142 int x2 = i * width() / points;
143 p.drawLine( x1, h - systemLoad[i-1] * mult,
144 x2, h - systemLoad[i] * mult );
145 }
146
147 p.setPen( red );
148 for ( int i = 1; i < points; i++ ) {
149 int x1 = (i - 1) * width() / points;
150 int x2 = i * width() / points;
151 p.drawLine( x1, h - userLoad[i-1] * mult,
152 x2, h - userLoad[i] * mult );
153 }
154}
155
156void Load::timeout()
157{
158 int user;
159 int usernice;
160 int sys;
161 int idle;
162 FILE *fp;
163 fp = fopen( "/proc/stat", "r" );
164 fscanf( fp, "cpu %d %d %d %d", &user, &usernice, &sys, &idle );
165 fclose( fp );
166 struct timeval now;
167 gettimeofday( &now, 0 );
168 int tdiff = now.tv_usec - last.tv_usec;
169 tdiff += (now.tv_sec - last.tv_sec) * 1000000;
170 tdiff /= 10000;
171
172 int udiff = user - lastUser;
173 int sdiff = sys - lastSys;
174 if ( tdiff > 0 ) {
175 double uload = (double)udiff / (double)tdiff;
176 double sload = (double)sdiff / (double)tdiff;
177 if ( !first ) {
178 for ( int i = 1; i < points; i++ ) {
179 userLoad[i-1] = userLoad[i];
180 systemLoad[i-1] = systemLoad[i];
181 }
182 userLoad[points-1] = uload;
183 systemLoad[points-1] = sload;
184 // scroll( -width()/points, 0, QRect( 0, 0, width() - width()/points + 1, height() ) );
185 repaint( TRUE );
186 double ml = 1.3;
187 /*
188 for ( int i = 0; i < points; i++ ) {
189 if ( userLoad[i] > ml )
190 ml = userLoad[i];
191 }
192 */
193 if ( maxLoad != ml ) {
194 maxLoad = ml;
195 update();
196 }
197 }
198
199 last = now;
200 lastUser = user;
201 lastSys = sys;
202 first = FALSE;
203 } else if ( tdiff < 0 ) {
204 last = now;
205 }
206}
207