2 * WMCPULoad - A dockapp to monitor CPU usage
3 * Copyright (C) 2001,2002 Seiichi SATO <ssato@sh.rim.or.jp>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "libdockapp/dockapp.h"
27 #include "backlight_on.xpm"
28 #include "backlight_off.xpm"
32 #define WINDOWED_BG " \tc #AEAAAE"
33 #define MAX_HISTORY 16
34 #define CPUNUM_NONE -1
36 typedef enum { LIGHTON
, LIGHTOFF
} light
;
42 static char *display_name
= "";
43 static char *title
= NULL
;
44 static char *light_color
= NULL
; /* back-light color */
45 static unsigned update_interval
= 1;
46 static light backlight
= LIGHTOFF
;
47 static unsigned alarm_threshold
= 101;
48 static cpu_options cpu_opts
;
49 static int history
[MAX_HISTORY
]; /* history of cpu usage */
50 static int hindex
= 0;
53 static void update(void);
54 static void redraw(void);
55 static void switch_light(void);
56 static void draw_digit(int per
);
57 static void parse_arguments(int argc
, char **argv
);
58 static void print_help(char *prog
);
59 Window
dockapp_win(void);
61 static void draw_cpunumber(void);
65 main(int argc
, char **argv
)
68 XpmColorSymbol colors
[2] = { {"Back0", NULL
, 0}, {"Back1", NULL
, 0} };
71 /* Parse Command-Line */
72 cpu_opts
.ignore_nice
= False
;
73 cpu_opts
.cpu_number
= CPUNUM_NONE
;
74 parse_arguments(argc
, argv
);
76 /* Initialize Application */
78 dockapp_open_window(display_name
, title
== NULL
? PACKAGE
: title
,
79 SIZE
, SIZE
, argc
, argv
);
80 dockapp_set_eventmask(ButtonPressMask
| ExposureMask
);
83 colors
[0].pixel
= dockapp_getcolor_pixel(light_color
);
84 colors
[1].pixel
= dockapp_blendedcolor(light_color
, -24, -24, -24, 1.0);
88 /* change raw xpm data to pixmap */
89 if (dockapp_stat
== WINDOWED_WITH_PANEL
)
90 backlight_on_xpm
[1] = backlight_off_xpm
[1] = WINDOWED_BG
;
91 dockapp_xpm2pixmap(backlight_on_xpm
, &backdrop_on
, &mask
, colors
, ncolor
);
92 dockapp_xpm2pixmap(backlight_off_xpm
, &backdrop_off
, NULL
, NULL
, 0);
93 dockapp_xpm2pixmap(parts_xpm
, &parts
, NULL
, colors
, ncolor
);
95 if (dockapp_stat
== DOCKABLE_ICON
|| dockapp_stat
== WINDOWED
) {
96 dockapp_setshape(mask
, 0, 0);
98 if (mask
) XFreePixmap(display
, mask
);
100 /* Initialize pixmap */
101 if (backlight
== LIGHTON
) {
102 dockapp_set_background(backdrop_on
);
104 dockapp_set_background(backdrop_off
);
110 if (dockapp_nextevent_or_timeout(&event
, update_interval
* 1000)) {
120 default: /* make gcc happy */
132 /* called by timer */
140 static light pre_backlight
;
141 static Bool in_alarm_mode
= False
;
143 if (backlight
== LIGHTON
) {
144 backdrop
= backdrop_on
;
147 backdrop
= backdrop_off
;
151 /* get current cpu usage in percent */
152 usage
= cpu_get_usage(&cpu_opts
);
154 if (hindex
>= MAX_HISTORY
) {
157 history
[hindex
] = usage
;
160 if (usage
>= alarm_threshold
) {
161 if (!in_alarm_mode
) {
162 in_alarm_mode
= True
;
163 pre_backlight
= backlight
;
165 if (backlight
== LIGHTOFF
) {
172 in_alarm_mode
= False
;
173 if (backlight
!= pre_backlight
) {
181 /* slide past chart */
182 dockapp_copyarea(dockapp_win(), dockapp_win(), 9, 33, 44, 21, 6, 33);
183 dockapp_copy2window(backdrop
, 51, 33, 2, 21, 51, 33);
186 dockapp_copy2window(backdrop
, 5, 7, 34, 20, 5, 7);
192 /* draw cpu number */
193 if (cpu_opts
.cpu_number
!= CPUNUM_NONE
)
197 /* draw current chart */
198 h
= (21 * usage
) / 100;
199 dockapp_copy2window(parts
, 100+x
, 21-h
, 2, h
, 51, 54-h
);
202 /* called when mouse button pressed */
211 backlight
= LIGHTOFF
;
219 int h
, i
, j
= hindex
;
223 if (backlight
== LIGHTON
) {
224 backdrop
= backdrop_on
;
227 backdrop
= backdrop_off
;
231 dockapp_copy2window(backdrop
, 0, 0, 58, 58, 0, 0);
234 draw_digit(history
[hindex
]);
237 /* draw cpu number */
238 if (cpu_opts
.cpu_number
!= CPUNUM_NONE
)
243 for (i
= 0; i
< MAX_HISTORY
; i
++) {
244 h
= (21 * history
[j
]) / 100;
245 dockapp_copy2window(parts
, 100+x
, 21-h
, 2, h
, 51-3*i
, 54-h
);
247 if (j
< 0) j
= MAX_HISTORY
- 1;
257 if (per
< 0) per
= 0;
258 if (per
> 100) per
= 100;
261 v10
= (per
- v100
* 100) / 10;
262 v1
= (per
- v100
* 100 - v10
* 10);
264 if (backlight
== LIGHTON
) {
269 dockapp_copy2window(parts
, v1
* 10, y
, 10, 20, 29, 7);
271 dockapp_copy2window(parts
, v10
* 10, y
, 10, 20, 17, 7);
274 dockapp_copy2window(parts
, 10, y
, 10, 20, 5, 7);
275 dockapp_copy2window(parts
, 0, y
, 10, 20, 17, 7);
276 dockapp_copy2window(parts
, 0, y
, 10, 20, 29, 7);
286 int x
, v1
= 0, v2
= 0;
288 v2
= cpu_opts
.cpu_number
/ 10;
289 v1
= cpu_opts
.cpu_number
- v2
* 10;
291 x
= backlight
== LIGHTON
? 50 : 0;
294 dockapp_copy2window(parts
, x
+ v2
* 5, 40, 5, 9, 44, 10);
296 dockapp_copy2window(parts
, x
+ v1
* 5, 40, 5, 9, 50, 10);
301 parse_arguments(int argc
, char **argv
)
304 for (i
= 1; i
< argc
; i
++) {
305 if (!strcmp(argv
[i
], "--help") || !strcmp(argv
[i
], "-h"))
306 print_help(argv
[0]), exit(0);
308 else if (!strcmp(argv
[i
], "--version") || !strcmp(argv
[i
], "-v"))
309 printf("%s version %s\n", PACKAGE
, VERSION
), exit(0);
311 else if (!strcmp(argv
[i
], "--display") || !strcmp(argv
[i
], "-d")) {
312 display_name
= argv
[i
+ 1];
316 else if (!strcmp(argv
[i
], "--backlight") || !strcmp(argv
[i
], "-bl"))
319 else if (!strcmp(argv
[i
], "--light-color") || !strcmp(argv
[i
], "-lc")) {
320 light_color
= argv
[i
+ 1];
324 else if (!strcmp(argv
[i
], "--ignore-nice") || !strcmp(argv
[i
], "-n"))
325 cpu_opts
.ignore_nice
= True
;
328 else if (!strcmp(argv
[i
], "--interval") || !strcmp(argv
[i
], "-i")) {
332 "%s: error parsing argument for option %s\n",
333 argv
[0], argv
[i
]), exit(1);
334 if (sscanf(argv
[i
+ 1], "%i", &integer
) != 1)
336 "%s: error parsing argument for option %s\n",
337 argv
[0], argv
[i
]), exit(1);
339 fprintf(stderr
, "%s: argument %s must be >=1\n",
340 argv
[0], argv
[i
]), exit(1);
341 update_interval
= integer
;
343 } else if (!strcmp(argv
[i
], "--alarm") || !strcmp(argv
[i
], "-a")) {
346 alarm_threshold
= 90;
347 else if (sscanf(argv
[i
+ 1], "%i", &integer
) != 1)
348 alarm_threshold
= 90;
349 else if (integer
< 0 || integer
> 100)
350 fprintf(stderr
, "%s: argument %s must be from 0 to 100\n",
351 argv
[0], argv
[i
]), exit(1);
353 alarm_threshold
= integer
, i
++;
354 } else if (!strcmp(argv
[i
], "--windowed")
355 || !strcmp(argv
[i
], "-w"))
356 dockapp_stat
= WINDOWED
;
358 else if (!strcmp(argv
[i
], "--windowed-withpanel")
359 || !strcmp(argv
[i
], "-wp"))
360 dockapp_stat
= WINDOWED_WITH_PANEL
;
362 else if (!strcmp(argv
[i
], "--broken-wm") || !strcmp(argv
[i
], "-bw"))
363 dockapp_isbrokenwm
= True
;
365 else if (!strcmp(argv
[i
], "--title") || !strcmp(argv
[i
], "-t")) {
371 else if (!strcmp(argv
[i
], "--cpu") || !strcmp(argv
[i
], "-c")) {
374 fprintf(stderr
, "%s: error parsing argument for option %s\n",
375 argv
[0], argv
[i
]), exit(1);
376 if (sscanf(argv
[i
+ 1], "%i", &integer
) != 1)
377 fprintf(stderr
, "%s: error parsing argument for option %s\n",
378 argv
[0], argv
[i
]), exit(1);
380 fprintf(stderr
, "%s: argument %s must be >=0\n",
381 argv
[0], argv
[i
]), exit(1);
382 cpu_opts
.cpu_number
= integer
;
388 fprintf(stderr
, "%s: unrecognized option '%s'\n", argv
[0],
390 print_help(argv
[0]), exit(1);
396 print_help(char *prog
)
398 printf("Usage : %s [OPTIONS]\n", prog
);
399 printf("WMCPULoad - A dockapp to monitor CPU usage\n");
400 printf(" -d, --display <string> display to use\n");
401 printf(" -t, --title <string> application title name\n");
402 printf(" -bl, --backlight turn on back-light\n");
403 printf(" -lc, --light-color <string> "
404 "back-light color(rgb:6E/C6/3B is default)\n");
405 printf(" -i, --interval <number> "
406 "number of secs between updates (1 is default)\n");
408 printf(" -c, --cpu <number> "
409 "which CPU is monitored (0, 1, ... )\n");
412 printf(" -n, --ignore-nice ignore a nice value\n");
414 printf(" -h, --help show this help text and exit\n");
415 printf(" -v, --version show program version and exit\n");
416 printf(" -w, --windowed "
417 "run the application in windowed mode\n");
418 printf(" -wp, --windowed-withpanel "
419 "run the application in windowed mode\n");
420 printf(" with background panel\n");
421 printf(" -bw, --broken-wm "
422 "activate broken window manager fix\n");
423 printf(" -a, --alarm <percentage> "
424 "activate alarm mode. <percentage> is threshold\n");
426 "of percentage from 0 to 100.(90 is default)\n");
428 /* ex:set sw=4 softtabstop=4: */