summaryrefslogtreecommitdiff
path: root/noncore/applets/memoryapplet/load.cpp
Unidiff
Diffstat (limited to 'noncore/applets/memoryapplet/load.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/applets/memoryapplet/load.cpp212
1 files changed, 212 insertions, 0 deletions
diff --git a/noncore/applets/memoryapplet/load.cpp b/noncore/applets/memoryapplet/load.cpp
new file mode 100644
index 0000000..d9d7a66
--- a/dev/null
+++ b/noncore/applets/memoryapplet/load.cpp
@@ -0,0 +1,212 @@
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
23#include <qfile.h>
24#include <qlayout.h>
25#include <qlabel.h>
26#include <qpainter.h>
27#include <qpixmap.h>
28#include <qtextstream.h>
29#include <qtimer.h>
30#include <qwhatsthis.h>
31
32#include "load.h"
33
34LoadInfo::LoadInfo( QWidget *parent, const char *name, WFlags f )
35 : QWidget( parent, name, f )
36{
37 QVBoxLayout *vb = new QVBoxLayout( this, 6 );
38
39 QString cpuInfo = getCpuInfo();
40 if ( !cpuInfo.isNull() )
41 vb->addWidget( new QLabel( cpuInfo, this ) );
42 vb->addWidget( new Load( this ), 100 );
43 QLabel *l = new QLabel( this );
44 l->setPixmap( makeLabel( red, tr("Application CPU usage (%)") ) );
45 vb->addWidget( l, 1 );
46 l = new QLabel( this );
47 l->setPixmap( makeLabel( green, tr("System CPU usage (%)") ) );
48 vb->addWidget( l, 1 );
49 vb->addStretch(50);
50
51 QWhatsThis::add( this, tr( "This page shows how much this device's processor is being used." ) );
52}
53
54QPixmap LoadInfo::makeLabel( const QColor &col, const QString &text )
55{
56 int h = fontMetrics().height();
57 QPixmap pm( 20 + fontMetrics().width( text ), h );
58 QPainter p( &pm );
59 p.fillRect( pm.rect(), colorGroup().background() );
60 p.fillRect( 0, h/2-4, 18, h/2+3, black );
61 p.setPen( col );
62 p.drawLine( 2, h/2, 15, h/2 );
63 p.setPen( colorGroup().text() );
64 p.drawText( 20, fontMetrics().ascent(), text );
65
66 return pm;
67}
68
69QString LoadInfo::getCpuInfo()
70{
71 bool haveInfo = FALSE;
72 QString info = tr("Type: ");
73 QFile f( "/proc/cpuinfo" );
74 if ( f.open( IO_ReadOnly ) ) {
75 QTextStream ts( &f );
76
77 while ( !ts.atEnd() ) {
78 QString s = ts.readLine();
79 if ( s.find( "model name" ) == 0 ) {
80 info += s.mid( s.find( ':' ) + 2 );
81 haveInfo = TRUE;
82 } else if ( s.find( "cpu MHz" ) == 0 ) {
83 double mhz = s.mid( s.find( ':' ) + 2 ).toDouble();
84 info += " " + QString::number( mhz, 'f', 0 );
85 info += "MHz";
86 break;
87 } else if ( s.find( "Processor" ) == 0 ) {
88 info += s.mid( s.find( ':' ) + 2 );
89 haveInfo = TRUE;
90 break;
91#ifdef __MIPSEL__
92 } else if ( s.find( "cpu model" ) == 0 ) {
93 info += " " + s.mid( s.find( ':' ) + 2 );
94 break;
95 } else if ( s.find( "cpu" ) == 0 ) {
96 info += s.mid( s.find( ':' ) + 2 );
97 haveInfo = TRUE;
98#endif
99 }
100 }
101 }
102
103 if ( !haveInfo )
104 info = QString();
105
106 return info;
107}
108
109Load::Load( QWidget *parent, const char *name, WFlags f )
110 : QWidget( parent, name, f )
111{
112 setMinimumHeight( 30 );
113 setBackgroundColor( black );
114 points = 100;
115 setMinimumWidth( points );
116 userLoad = new double [points];
117 systemLoad = new double [points];
118 for ( int i = 0; i < points; i++ ) {
119 userLoad[i] = 0.0;
120 systemLoad[i] = 0.0;
121 }
122 maxLoad = 1.3;
123 QTimer *timer = new QTimer( this );
124 connect( timer, SIGNAL(timeout()), SLOT(timeout()) );
125 timer->start( 2000 );
126 gettimeofday( &last, 0 );
127 first = TRUE;
128 timeout();
129}
130
131void Load::paintEvent( QPaintEvent * )
132{
133 QPainter p( this );
134
135 int h = height() - 5;
136
137 int mult = (int)(h / maxLoad);
138
139 p.setPen( gray );
140 p.drawLine( 0, h - mult, width(), h - mult );
141 p.drawText( 0, h - mult, "100" );
142 p.drawText( 0, h, "0" );
143
144 p.setPen( green );
145 for ( int i = 1; i < points; i++ ) {
146 int x1 = (i - 1) * width() / points;
147 int x2 = i * width() / points;
148 p.drawLine( x1, h - systemLoad[i-1] * mult,
149 x2, h - systemLoad[i] * mult );
150 }
151
152 p.setPen( red );
153 for ( int i = 1; i < points; i++ ) {
154 int x1 = (i - 1) * width() / points;
155 int x2 = i * width() / points;
156 p.drawLine( x1, h - userLoad[i-1] * mult,
157 x2, h - userLoad[i] * mult );
158 }
159}
160
161void Load::timeout()
162{
163 int user;
164 int usernice;
165 int sys;
166 int idle;
167 FILE *fp;
168 fp = fopen( "/proc/stat", "r" );
169 fscanf( fp, "cpu %d %d %d %d", &user, &usernice, &sys, &idle );
170 fclose( fp );
171 struct timeval now;
172 gettimeofday( &now, 0 );
173 int tdiff = now.tv_usec - last.tv_usec;
174 tdiff += (now.tv_sec - last.tv_sec) * 1000000;
175 tdiff /= 10000;
176
177 int udiff = user - lastUser;
178 int sdiff = sys - lastSys;
179 if ( tdiff > 0 ) {
180 double uload = (double)udiff / (double)tdiff;
181 double sload = (double)sdiff / (double)tdiff;
182 if ( !first ) {
183 for ( int i = 1; i < points; i++ ) {
184 userLoad[i-1] = userLoad[i];
185 systemLoad[i-1] = systemLoad[i];
186 }
187 userLoad[points-1] = uload;
188 systemLoad[points-1] = sload;
189 // scroll( -width()/points, 0, QRect( 0, 0, width() - width()/points + 1, height() ) );
190 repaint( TRUE );
191 double ml = 1.3;
192 /*
193 for ( int i = 0; i < points; i++ ) {
194 if ( userLoad[i] > ml )
195 ml = userLoad[i];
196 }
197 */
198 if ( maxLoad != ml ) {
199 maxLoad = ml;
200 update();
201 }
202 }
203
204 last = now;
205 lastUser = user;
206 lastSys = sys;
207 first = FALSE;
208 } else if ( tdiff < 0 ) {
209 last = now;
210 }
211}
212