File: wmcpu.c

package info (click to toggle)
wmcpu 1.3-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 80 kB
  • ctags: 27
  • sloc: ansic: 298; makefile: 55
file content (382 lines) | stat: -rw-r--r-- 10,734 bytes parent folder | download | duplicates (3)
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
/* 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.3"

#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 int  uptime(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(stderr, "%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)
{
    /* /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. */

    static float cpustat[4];	/* remember the statistics read last time */
    int tempy;
    int upt, days = 0, hours = 0, mins = 0;
    XEvent Event;
    FILE *fp;
    char str[32];
    float info[4], ftmp;
    int old_mins = -1;
    int i;

    new_window("wmcpu", 64,  64);

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

	    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) {
		    int xoffset = 5;
		    int barwidth = 54;
		    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];
	    fclose(fp);
	}

	/* /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;
	    fscanf(fp, "%f", &ftmp);
	    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);
		}
	    }
	    fclose(fp);
	}

	/* /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 ((fp = fopen("/proc/meminfo", "r")) != NULL) {
	    static float stotal = 0.0;
	    static float suser = 0.0;
	    static float sshared = 0.0;
	    static float sbuffers = 0.0;
	    char junk[512];
	    float total, used, freeM, shared, buffers, cached, progs;
	    long int MEMshar, MEMbuff, MEMcach, MEMprog;

	    fgets(junk, 500, fp);
	    fscanf(fp, "%s %f %f %f %f %f %f", junk, &total, &used, &freeM,
		   &shared, &buffers, &cached);
	    progs = used - cached - buffers - shared;
	    if (stotal != total || suser != progs || sshared != shared
		|| sbuffers != buffers) {
		int xoffset = 5;
		int barwidth = 54;
		stotal = total;
		suser = progs;
		sshared = shared;
		sbuffers = buffers;
		MEMprog = (progs * barwidth) / total;
		MEMshar = (shared * barwidth) / total;
		MEMbuff = (buffers * barwidth) / total;
		MEMcach = (cached * barwidth) / total;
		copy_xpm_area(3, 111, MEMprog, 9, xoffset, 19);
		copy_xpm_area(3, 129, MEMshar, 9, xoffset + MEMprog, 19);
		copy_xpm_area(3, 120, MEMbuff, 9,
			    xoffset + MEMprog + MEMshar, 19);
		copy_xpm_area(3, 84, MEMcach, 9,
			    xoffset + MEMprog + MEMshar + MEMbuff, 19);
		copy_xpm_area(3, 102,
			    (barwidth -
			     (MEMprog + MEMshar + MEMbuff + MEMcach)), 9,
			    xoffset + MEMprog + MEMshar + MEMbuff +
			    MEMcach, 19);
	    }
	    fclose(fp);
	}

	/* End meminfo */
	upt = uptime();
	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);
	}

	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);
    }
}

static int uptime(void)
{
    FILE *fp;
    float fupt;
    if ((fp = fopen("/proc/uptime", "r")) != NULL)
	fscanf(fp, "%f", &fupt);
    fclose(fp);
    return fupt;
}

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);
}