File: wmcpu.c

package info (click to toggle)
wmcpu 1.4-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, lenny, squeeze, stretch, wheezy
  • size: 108 kB
  • ctags: 26
  • sloc: ansic: 323; makefile: 20
file content (427 lines) | stat: -rw-r--r-- 11,583 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/* WMCPU 1.3 (c) 1998, 1999, 2000, 2001 timecop@japan.co.jp
 * Dockapp CPU usage monitor similar to XosView.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * LOADAVG patch thanks to Darren Benham <gecko@benham.net>
 * now use -l switch on the command line to toggle between graphical and 
 * numeric loadavg display.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <getopt.h>

#include <X11/Xlib.h>
#include <X11/xpm.h>
#include <X11/extensions/shape.h>

#include "master.xpm"

#define WMCPU_VERSION "1.4"

#define copy_xpm_area(x, y, w, h, dx, dy) \
	XCopyArea(display, pixmap, pixmap, gc, x, y, w, h, dx, dy)
#define MIN(a, b) (((a) < (b)) ? (a) : (b))

#define HELP_TEXT \
	"wmcpu: cpu usage monitor dockapp that looks like xosview\n" \
	"   timecop@japan.co.jp\n" \
	"usage:\n" \
	"\t-display <display name>\n" \
	"\t-h\tthis screen\n" \
	"\t-v\tprint the version number\n" \
	"\t-l\tshow load as numbers\n" \
	"\t-t\ttime between refresh in usec (def=250000)\n"

static char *display_name = NULL;
static Display *display;
static Window win;
static Window iconwin;
static GC gc;
static Pixmap pixmap;
static Pixmap mask;
static unsigned long udelay = 250000;
static int gfx_loadbar = 1;

static void wmcpu_routine(void);
static void redraw_window(void);
static void new_window(char *name, int width, int height);

int main(int argc, char **argv)
{
	int ch;

	while ((ch = getopt(argc, argv, "d:hvlt:")) != EOF) {
		switch (ch) {
			case 'd':
				if (optarg)
					display_name = strdup(optarg);
				break;
			case 'v':
				fprintf(stdout, "%s\n", WMCPU_VERSION);
				exit(0);
				break;
			case 'l':
				gfx_loadbar = 0;
				break;
			case 't':
				if (optarg)
					udelay = atol(optarg);
				break;
			default:
				fputs(HELP_TEXT, stdout);
				exit(0);
				break;
		}
	}

	if ((display = XOpenDisplay(display_name)) == NULL) {
		fprintf(stderr, "Unable to open display \"%s\"\n", display_name);
		return EXIT_FAILURE;
	}

	wmcpu_routine();
	return EXIT_SUCCESS;
}

void wmcpu_routine(void)
{
	int i;
	int tempy;
	const int xoffset = 5;
	const int barwidth = 54;
	XEvent Event;
	FILE *fp;
	char str[128];

	static float cpustat[4];	/* remember the statistics read last time */
	float info[4];

	int old_mins = -1;

	char has_meminfo = 0;
	struct {
		short int line;
		int *var;
	} memlines[4];
	int memtotal=0, memfree=0, membuffers=0, memcached=0, memuser=0;
	
	/* /proc/meminfo Initialization {{{
	   This will find which line numbers correspond to
	   each meminfo value. This is done now to avoid
	   string comparing inside of main loop. */
	if((fp = fopen("/proc/meminfo", "r")) != NULL) {
		short int line=0, last_item=0;
		struct {
			char *name;
			int *var;
		} meminfolines[4] = {
			{"MemTotal:",&memtotal  },
			{"MemFree:" ,&memfree   },
			{"Buffers:" ,&membuffers},
			{"Cached:"  ,&memcached }
		};
		while(fscanf(fp," %127s%*[^\n]",str) > 0) {
			line++;
			for(i=0; i<4; i++)
				if(strcmp(str,meminfolines[i].name)==0) {
					memlines[last_item].line=line;
					memlines[last_item].var=meminfolines[i].var;
					last_item++;
					break;
				}
			if(last_item>=4)
				break;
		}
		fclose(fp);
		/* Next loop is just to not leave unitialized items. */
		while(last_item<4) {
			memlines[last_item].line=0;
			memlines[last_item].var=0;
			last_item++;
		}
		has_meminfo=1;
	}
	/* /proc/meminfo Initialization }}} */

	new_window("wmcpu", 64,  64);

	while (1) {

	/* /proc/stat	kernel/system statistics  {{{
	   cpu	3357 0 4313 1362393
	   The number of jiffies (1/100ths of a second)
	   that the system spent in user mode, user
	   mode with low priority (nice), system mode,
	   and the idle task, respectively.  The last
	   value should be 100 times the second entry
	   in the uptime pseudo-file. */

	if ((fp = fopen("/proc/stat", "r")) != NULL) {
		long int CPUuser, CPUnice, CPUsyst;
		fscanf(fp, "%127s%f%f%f%f", str, info, info + 1, info + 2, info + 3);
		fclose(fp);

		if (cpustat[0] != 0) {
			float fields[4], total = 0.0;
			for (i = 0; i < 4; i++) {
				fields[i] = info[i] - cpustat[i];
				total += fields[i];
			}
			if (total > 0) {
				CPUuser = fields[0] / total * (barwidth - 0.5);
				CPUnice = fields[1] / total * (barwidth - 0.5);
				CPUsyst = fields[2] / total * (barwidth - 0.5);
				copy_xpm_area(3, 93, CPUuser, 9, xoffset, 5);
				copy_xpm_area(3, 75, CPUnice, 9, xoffset + CPUuser, 5);
				copy_xpm_area(3, 84, CPUsyst, 9,
					xoffset + CPUuser + CPUnice, 5);
				copy_xpm_area(3, 102,
					(54 - (CPUuser + CPUnice + CPUsyst)), 9,
					xoffset + CPUuser + CPUnice + CPUsyst, 5);
			}
		}
		for (i = 0; i < 4; i++)
			cpustat[i] = info[i];
	}
	/* /proc/stat	kernel/system statistics  }}} */

	/* /proc/loadavg  {{{
	   The load average numbers give the number of jobs in
	   the run queue averaged over 1, 5 and 15 minutes.
	   They are the same as the load average numbers given
	   by uptime(1) and other programs. */
	if ((fp = fopen("/proc/loadavg", "r")) != NULL) {
		static float oldv = 0.0;
		float ftmp;
		fscanf(fp, "%f", &ftmp);
		fclose(fp);

		if (oldv != ftmp) {
			int tempx;
			oldv = info[0];
			if (gfx_loadbar) {
				oldv = ftmp;
				i = MIN(54, (ftmp * 54));
				copy_xpm_area(3, 75, i, 9, 5, 33);
				copy_xpm_area(3, 102, 54 - i, 9, 5 + i, 33);
			} else {
				tempx = ftmp * 100;
				if (tempx > 9999)
					tempx = 9999;
				if (tempx > 1000) {
					tempy = tempx / 1000;
					copy_xpm_area(3 + (tempy * 6), 66, 6, 9, 15, 33);
				} else
					copy_xpm_area(68, 66, 6, 9, 15, 33);
				tempy = (tempx / 100) % 10;
				copy_xpm_area(3 + (tempy * 6), 66, 6, 9, 21, 33);

				copy_xpm_area(65, 66, 3, 9, 28, 33);

				tempy = (tempx / 10) % 10;
				copy_xpm_area(3 + (tempy * 6), 66, 6, 9, 31, 33);

				tempy = tempx % 10;
				copy_xpm_area(3 + (tempy * 6), 66, 6, 9, 37, 33);
			}
		}
	}
	/* /proc/loadavg  }}} */

	/* /proc/meminfo  {{{
	   This is used by free(1) to report the amount of
	   free and used memory (both physical and swap) on
	   the system as well as the shared memory and buffers
	   used by the kernel. */
	if (has_meminfo && (fp = fopen("/proc/meminfo", "r")) != NULL) {
		short int line=0, last_item=0;
		int oldtotal, oldfree, oldbuffers, oldcached, olduser;
		int                    barbuffers, barcached, baruser;

		oldtotal  =memtotal;
		oldfree   =memfree;
		oldbuffers=membuffers;
		oldcached =memcached;
		olduser   =memuser;  /* Note: value of this var is calculated from other vars. */
		while( fgets(str,128,fp) ) {
			line++;
			if(line == memlines[last_item].line) {
				sscanf(str,"%*s%d",memlines[last_item].var);
				last_item++;
			}
			if(last_item >= 4 || memlines[last_item].line==0)
				break;
		}
		fclose(fp);
		memuser = memtotal - memfree - membuffers - memcached;

		if ( memfree   != oldfree   || membuffers != oldbuffers
		||   memcached != oldcached || memtotal   != oldtotal   ) {

			baruser    = (memuser    * barwidth) / memtotal;
			barcached  = (memcached  * barwidth) / memtotal;
			barbuffers = (membuffers * barwidth) / memtotal;

			copy_xpm_area(3, 111, baruser   , 9, xoffset, 19);
			copy_xpm_area(3, 120, barbuffers, 9, xoffset + baruser, 19);
			copy_xpm_area(3,  84, barcached , 9, xoffset + baruser + barbuffers, 19);
			copy_xpm_area(3, 102,
				barwidth - (baruser + barbuffers + barcached), 9,
				xoffset  + (baruser + barbuffers + barcached), 19);
		}
	}
	/* /proc/meminfo  }}} */

	/* /proc/uptime  {{{
	   Tell how long the system has been running. */
	if ((fp = fopen("/proc/uptime", "r")) != NULL) {
		int upt, days, hours, mins;
		fscanf(fp, "%d", &upt);
		fclose(fp);

		mins = (upt / 60) % 60;
		hours = (upt / 3600) % 24;
		days = (upt / 86400);
		if (old_mins != mins) {
			old_mins = mins;
			tempy = mins % 10;
			copy_xpm_area(3 + (tempy * 6), 66, 6, 9, 50, 48);
			tempy = mins / 10;
			copy_xpm_area(3 + (tempy * 6), 66, 6, 9, 44, 48);
			copy_xpm_area(63, 66, 3, 9, 41, 48);
			tempy = hours % 10;
			copy_xpm_area(3 + (tempy * 6), 66, 6, 9, 34, 48);
			tempy = hours / 10;
			copy_xpm_area(3 + (tempy * 6), 66, 6, 9, 28, 48);
			copy_xpm_area(63, 66, 3, 9, 25, 48);
			tempy = days % 10;
			copy_xpm_area(3 + (tempy * 6), 66, 6, 9, 18, 48);
			tempy = days / 10;
			copy_xpm_area(3 + (tempy * 6), 66, 6, 9, 12, 48);
			tempy = days / 100;
			copy_xpm_area(3 + (tempy * 6), 66, 6, 9, 6, 48);
		}
	}
	/* /proc/uptime }}} */

	while (XPending(display)) {
		XNextEvent(display, &Event);
		switch (Event.type) {
			case Expose:
				redraw_window();
				break;
			case DestroyNotify:
				XCloseDisplay(display);
				exit(0);
				break;
		}
	}
	redraw_window();
	usleep(udelay);

	}  /* End of while(1) loop */
}

static void new_window(char *name, int width, int height)
{
	XpmAttributes attr;
	Pixel fg, bg;
	XGCValues gcval;
	XSizeHints sizehints;
	XClassHint classhint;
	XWMHints wmhints;
	XTextProperty wname;

	sizehints.flags = USSize | USPosition;
	sizehints.x = 0;
	sizehints.y = 0;
	sizehints.width = width;
	sizehints.height = height;

	fg = BlackPixel(display, DefaultScreen(display));
	bg = WhitePixel(display, DefaultScreen(display));

	win = XCreateSimpleWindow(display, DefaultRootWindow(display),
			sizehints.x, sizehints.y,
			sizehints.width, sizehints.height, 1, fg,
			bg);

	iconwin = XCreateSimpleWindow(display, win, sizehints.x, sizehints.y,
			sizehints.width, sizehints.height, 1, fg,
			bg);

	XSetWMNormalHints(display, win, &sizehints);
	classhint.res_name = name;
	classhint.res_class = name;
	XSetClassHint(display, win, &classhint);

#define INPUT_MASK \
	ButtonPressMask \
	| ExposureMask

	XSelectInput(display, win, INPUT_MASK);
	XSelectInput(display, iconwin, INPUT_MASK);

#undef INPUT_MASk

	XStringListToTextProperty(&name, 1, &wname);
	XSetWMName(display, win, &wname);

	gcval.foreground = fg;
	gcval.background = bg;
	gcval.graphics_exposures = 0;

	gc = XCreateGC(display, win,
		GCForeground | GCBackground | GCGraphicsExposures,
		&gcval);

	attr.exactColors = 0;
	attr.alloc_close_colors = 1;
	attr.closeness = 30000;
	attr.valuemask = (XpmExactColors | XpmAllocCloseColors | XpmCloseness);
	if ((XpmCreatePixmapFromData(display, DefaultRootWindow(display),
			master_xpm, &pixmap, &mask,
			&attr) != XpmSuccess)) {
		fputs("Cannot allocate colors for the dockapp pixmap!\n", stderr);
		exit(EXIT_FAILURE);
	}

	XShapeCombineMask(display, win, ShapeBounding, 0, 0, mask,
		  ShapeSet);
	XShapeCombineMask(display, iconwin, ShapeBounding, 0, 0, mask,
		  ShapeSet);

	wmhints.initial_state = WithdrawnState;
	wmhints.icon_window = iconwin;
	wmhints.icon_x = sizehints.x;
	wmhints.icon_y = sizehints.y;
	wmhints.window_group = win;
	wmhints.flags =
	StateHint | IconWindowHint | IconPositionHint | WindowGroupHint;
	XSetWMHints(display, win, &wmhints);

	XMapWindow(display, win);
}

static void redraw_window(void)
{
	XCopyArea(display, pixmap, iconwin, gc, 0, 0, 64, 64, 0, 0);
	XCopyArea(display, pixmap, win, gc, 0, 0, 64, 64, 0, 0);
}