Make AddMouseRegion's index unsigned
[dockapps.git] / wmSMPmon / wmSMPmon / wmSMPmon.c
blobb6743ffcb9ef1a3ebf2e49f895ff2ea959163896
1 /***************************************************************************
2 wmSMPmon III - Window Maker system monitor
3 VERSION : 3.2
4 DATE : 2005-11-06
5 ORIGINAL AUTHORS : redseb <redseb@goupilfr.org> and
6 PhiR <phir@gcu-squad.org>
7 CONTRIBUTORS : Alain Schröder <alain@parkautomat.net>
8 CURRENT MAINTAINER: Thomas Ribbrock <emgaron@gmx.net>
9 ****************************************************************************
10 This file is placed under the conditions of the GNU Library
11 General Public License, version 2, or any later version.
12 See file COPYING for information on distribution conditions.
13 ***************************************************************************/
15 #include <string.h>
16 #include <X11/Xlib.h>
17 #include <X11/xpm.h>
18 #include <X11/extensions/shape.h>
19 #include <libdockapp/wmgeneral.h>
20 #include "wmSMPmon_master.xpm"
21 #include "wmSMPmon_mask.xbm"
22 #include "general.h"
23 #include "standards.h"
25 #define VERSION "3.2"
27 /*###### Dividers for redraw-loops ######################################*/
28 #define DIV1 6
29 #define DIV2 10
31 /*###### Messages #######################################################*/
32 #define MSG_NO_SWAP "No swap mode : Swap is not monitored.\n"
34 /*###### Funcition definitions ##########################################*/
35 void usage(int cpus, const char *str);
38 /*###### MAIN PROGRAM ###################################################*/
39 int main(int argc, char **argv)
41 XEvent Event;
43 unsigned int t0[TAILLE_T], /* history for CPU 0 -> Graph */
44 t1[TAILLE_T], /* history for CPU 1 -> Graph */
45 tm[TAILLE_T], /* history for CPU 0+1 -> Graph */
46 c1 = DIV1,
47 c2 = DIV2,
48 etat = 1,
49 lecture = 1,
50 delay = 250000,
51 delta = 0,
52 load = 0,
53 load0o = 0,
54 load1o = 0,
55 no_swap = FAUX,
56 draw_graph = VRAI,
57 NumCPUs, /* number of CPUs */
58 i = 0, /* counter */
59 mem = 0, /* current memory/swap scaled to 0-100 */
60 prec_mem = 0, /* memory from previous round */
61 prec_swap = 0, /* swap from previous round */
62 load_width = 3; /* width of load bar: 3 for SMP, 8 for UP */
64 unsigned long load0t = 0, load1t = 0;
66 unsigned int *CPU_Load; /* CPU load per CPU array */
67 unsigned int t_idx = 0; /* Index to load history tables */
69 /********** Initialisation **********/
70 NumCPUs = NumCpus_DoInit();
71 CPU_Load = alloc_c((NumCPUs) * sizeof(int));
73 if(NumCPUs == 1) {
74 load_width = 8;
75 } else {
76 load_width = 3;
79 Myname = strrchr(argv[0], '/');
80 if (Myname)
81 ++Myname;
82 else
83 Myname = argv[0];
85 /* process command line args */
86 i = 1; /* skip program name (i=0) */
87 while (argc > i) {
88 if (!strncmp(argv[i], "-r", 2)) {
89 i++;
90 if (i == argc) {
91 /* parameter missing! */
92 usage(NumCPUs,
93 "no refresh rate given when using -r!");
94 } else {
95 delay = atol(argv[i]) ;
97 i++;
98 continue;
100 if (!strncmp(argv[i], "-h", 2)) {
101 usage(NumCPUs, NULL);
103 if (!strncmp(argv[i], "-g", 2) && NumCPUs > 1) {
104 /* we only support this on SMP systems */
105 i++;
106 if (i == argc) {
107 /* parameter missing! */
108 usage(NumCPUs,
109 "no graph style given when using -g!");
110 } else {
111 etat = atoi(argv[i]);
114 if (1 > etat || etat > 3)
115 usage(NumCPUs, "Unknown graph style");
116 i++;
117 continue;
119 if (!strncmp(argv[i], "-no-swap", 8)) {
120 puts(MSG_NO_SWAP);
121 no_swap = VRAI;
122 i++;
123 continue;
126 /* if we get here, we found an illegal option */
127 usage(NumCPUs, "Illegal option!");
130 /* open initial window */
131 if (NumCPUs == 1) {
132 /* we only have a single CPU - change the mask accordingly
133 * NOTE: The for loop was derived from the differences between
134 * wmSMPmon_mask.xbm and wmSMPmon_mask-single.xbm.
135 * wmSMPmon_mask-single.xbm as such is NOT used in this
136 * program!
138 for (i = 33; i <= 289; i = i+8) {
139 wmSMPmon_mask_bits[i] = 0xDF;
143 openXwindow(argc, argv, wmSMPmon_master_xpm, wmSMPmon_mask_bits,
144 wmSMPmon_mask_width, wmSMPmon_mask_height);
146 if(NumCPUs >= 2) {
147 /* we have two CPUs -> draw separator between CPU load bars */
148 copyXPMArea(12, 4, 2, HAUTEUR + 2, 7, 4);
151 delay = delay / 2 ;
153 for (i = 0; i < TAILLE_T; i ++) {
154 t0[i] = 0;
155 t1[i] = 0;
156 tm[i] = 0;
159 /* -no-swap option was given */
160 if (no_swap)
161 copyXPMArea(60, 63, 60, 10, 6, 50);
163 /* MAIN LOOP */
164 while (VRAI) {
165 if (lecture) {
166 CPU_Load = Get_CPU_Load(CPU_Load, NumCPUs);
168 load = CPU_Load[0];
169 for (i = 1; i < NumCPUs >> 1; i++) {
170 load += CPU_Load[i];
172 load = load / i;
173 load0t = load0t + load;
174 if (load != load0o) {
175 /* redraw only if cpu load changed */
176 delta = HAUTEUR - load;
177 copyXPMArea(108, 0, load_width, HAUTEUR, 4, 5);
178 copyXPMArea(108, delta + 32, load_width, load,
179 4, 5 + delta);
180 load0o = load;
183 if (NumCPUs >= 2) {
184 /* we have two CPUs -> do CPU 1 */
185 load = 0;
186 for (; i < NumCPUs; i++) {
187 load += CPU_Load[i];
189 load = load / (NumCPUs >> 1);
191 if (load != load1o) {
192 /* redraw only if cpu load changed */
193 delta = HAUTEUR - load;
194 copyXPMArea(108, 0, 3, HAUTEUR, 9, 5);
195 copyXPMArea(108, delta + 32, 3, load,
196 9, 5 + delta);
197 load1o = load;
201 /* we have to set load1t in any case to get the correct
202 * graph below. With only one CPU, 'load' will still be
203 * CPU_Load[0], on a SMP system, it will be CPU_Load[1].
205 load1t = load1t + load;
207 if (c1 > DIV1) {
208 mem = Get_Memory();
210 if (mem != prec_mem) {
211 /* redraw only if mem changed */
212 copyXPMArea(30, 63, 30, 8, 29, 39);
213 copyXPMArea(0, 63, (mem * 30 / 100), 8,
214 29, 39);
215 prec_mem = mem;
218 if (!no_swap) {
219 mem = Get_Swap();
221 if (mem != prec_swap) {
222 /* redraw if there was a change */
223 if (mem == 999) {
224 /* swap is disabled => show "none" */
225 copyXPMArea(60, 63, 60, 10, 6, 50);
226 } else {
227 /* draw swap usage */
228 copyXPMArea(30, 63, 30, 8, 29, 50);
229 copyXPMArea(0, 63, (mem * 30 / 100), 8, 29, 50);
231 prec_swap = mem;
234 c1 = 0;
237 if (c2 > DIV2) {
238 if ((t0[t_idx] = load0t / c2) > HAUTEUR)
239 t0[t_idx] = HAUTEUR;
240 t0[t_idx] /= 2;
241 if ((t1[t_idx] = load1t / c2) > HAUTEUR)
242 t1[t_idx] = HAUTEUR;
243 t1[t_idx] /= 2;
244 if ((tm[t_idx] = (load0t + load1t) / (2 * c2)) > HAUTEUR)
245 tm[t_idx] = HAUTEUR;
246 load0t = 0;
247 load1t = 0;
248 t_idx = (t_idx + 1) % TAILLE_T;
249 draw_graph = VRAI;
250 c2 = 0;
253 if (draw_graph) {
254 /* draw graph */
255 switch (etat) {
256 case 1 :
257 copyXPMArea(64, 32, TAILLE_T, HAUTEUR, 15, 5);
258 for (i = 0, load = t_idx; i < TAILLE_T; i ++, load++)
259 copyXPMArea(116, 0, 1, tm[load % TAILLE_T], 15 + i, HAUTEUR + 5 - tm[load % TAILLE_T]);
260 break;
261 case 2 :
262 copyXPMArea(64, 0, TAILLE_T, HAUTEUR, 15, 5);
263 for (i = 0, load = t_idx; i < TAILLE_T; i ++, load++) {
264 copyXPMArea(116, 0, 1, t0[load % TAILLE_T], 15 + i, HAUTEUR/2 + 5 - t0[load % TAILLE_T]);
265 copyXPMArea(116, 0, 1, t1[load % TAILLE_T], 15 + i, HAUTEUR/2 + 21 - t1[load % TAILLE_T]);
267 break;
268 case 3 :
269 copyXPMArea(64, 0, TAILLE_T, HAUTEUR, 15, 5);
270 for (i = 0, load = t_idx; i < TAILLE_T; i ++, load++) {
271 copyXPMArea(116, 0, 1, t0[load % TAILLE_T], 15 + i, HAUTEUR/2 + 5 - t0[load % TAILLE_T]);
272 copyXPMArea(117, HAUTEUR/2 - t1[load % TAILLE_T], 1, t1[load % TAILLE_T], 15 + i, HAUTEUR/2 + 6);
274 break;
276 draw_graph = FAUX;
278 c1++;
279 c2++;
281 lecture = 1 - lecture ;
282 RedrawWindow();
283 if (NumCPUs >= 2 &&
284 XCheckMaskEvent(display, ButtonPressMask, &Event)) {
285 /* changing graph style not supported on single CPU systems */
286 if (Event.type == ButtonPress) {
287 if ((etat++) >= 3)
288 etat = 1;
289 draw_graph = VRAI;
292 usleep(delay);
296 /*###### Usage Message ##################################################*/
297 void usage(int cpus, const char *str)
299 fflush(stdout);
301 if (str) {
302 fprintf(stderr, "\nERROR: %s\n", str);
305 fputs("\nwmSMPmon "VERSION" - display system load (", stderr);
306 if(cpus == 1) {
307 fputs("uniprocessor system)\n\n", stderr);
308 } else {
309 fputs("multiprocessor system)\n\n", stderr);
311 fputs("Options : -h this help screen.\n"
312 " -r RATE refresh rate (in microseconds, default 250000).\n",
313 stderr);
315 if(cpus > 1) {
316 fputs(" -g STYLE graph style (try 2 or 3, default is 1).\n",
317 stderr);
320 fputs(" -no-swap don't monitore swap size.\n\n"
321 "<redseb@goupilfr.org> http://goupilfr.org\n"
322 "<phir@gcu-squad.org> http://gcu-squad.org\n"
323 "<emgaron@gmx.net> http://www.ribbrock.org\n",
324 stderr);
326 exit(OK);