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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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
;
44 static char *display_name
= "";
45 static char *title
= NULL
;
46 static char *light_color
= NULL
; /* back-light color */
47 static unsigned update_interval
= 1;
48 static light backlight
= LIGHTOFF
;
49 static unsigned alarm_threshold
= 101;
50 static cpu_options cpu_opts
;
51 static int history
[MAX_HISTORY
]; /* history of cpu usage */
52 static int hindex
= 0;
55 static void update(void);
56 static void switch_light(void);
57 static void draw_digit(int per
);
58 static void parse_arguments(int argc
, char **argv
);
59 static void print_help(char *prog
);
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 cpu_opts
.ignore_procs
= 0;
75 parse_arguments(argc
, argv
);
77 /* Initialize Application */
79 dockapp_open_window(display_name
, title
== NULL
? PACKAGE
: title
,
80 SIZE
, SIZE
, argc
, argv
);
81 dockapp_set_eventmask(ButtonPressMask
);
84 colors
[0].pixel
= dockapp_getcolor_pixel(light_color
);
85 colors
[1].pixel
= dockapp_blendedcolor(light_color
, -24, -24, -24, 1.0);
89 /* change raw xpm data to pixmap */
90 if (dockapp_stat
== WINDOWED_WITH_PANEL
) {
91 backlight_on_xpm
[1] = backlight_off_xpm
[1] = WINDOWED_BG
;
93 dockapp_xpm2pixmap(backlight_on_xpm
, &backdrop_on
, &mask
, colors
, ncolor
);
94 dockapp_xpm2pixmap(backlight_off_xpm
, &backdrop_off
, NULL
, NULL
, 0);
95 dockapp_xpm2pixmap(parts_xpm
, &parts
, NULL
, colors
, ncolor
);
97 if (dockapp_stat
== DOCKABLE_ICON
|| dockapp_stat
== WINDOWED
) {
98 dockapp_setshape(mask
, 0, 0);
100 if (mask
) XFreePixmap(display
, mask
);
101 /* pixmap : draw area */
102 pixmap
= dockapp_XCreatePixmap(SIZE
, SIZE
);
103 pix_chartbuf
= dockapp_XCreatePixmap(SIZE
, SIZE
);
105 /* Initialize pixmap */
106 if (backlight
== LIGHTON
) {
107 dockapp_copyarea(backdrop_on
, pixmap
, 0, 0, SIZE
, SIZE
, 0, 0);
109 dockapp_copyarea(backdrop_off
, pixmap
, 0, 0, SIZE
, SIZE
, 0, 0);
111 dockapp_set_background(pixmap
);
116 if (dockapp_nextevent_or_timeout(&event
, update_interval
* 1000)) {
122 default: /* make gcc happy */
134 /* called by timer */
141 static light pre_backlight
;
142 static Bool in_alarm_mode
= False
;
144 /* get current cpu usage in percent */
145 usage
= cpu_get_usage(&cpu_opts
);
147 if (hindex
>= MAX_HISTORY
) {
150 history
[hindex
] = usage
;
153 if (usage
>= alarm_threshold
) {
154 if (!in_alarm_mode
) {
155 in_alarm_mode
= True
;
156 pre_backlight
= backlight
;
158 if (backlight
== LIGHTOFF
) {
164 in_alarm_mode
= False
;
165 if (backlight
!= pre_backlight
) {
172 /* save current chart */
173 dockapp_copyarea(pixmap
, pix_chartbuf
, 9, 33, 44, 21, 0, 0);
176 if (backlight
== LIGHTON
) {
177 dockapp_copyarea(backdrop_on
, pixmap
, 0, 0, 58, 58, 0, 0);
180 dockapp_copyarea(backdrop_off
, pixmap
, 0, 0, 58, 58, 0, 0);
188 /* draw cpu number */
189 if (cpu_opts
.cpu_number
!= CPUNUM_NONE
)
194 h
= (21 * usage
) / 100;
195 dockapp_copyarea(pix_chartbuf
, pixmap
, 0, 0, 44, 21, 6, 33);
196 dockapp_copyarea(parts
, pixmap
,100+x
, 21-h
, 2, h
, 51, 54-h
);
199 dockapp_copy2window(pixmap
);
203 /* called when mouse button pressed */
207 int h
, i
, j
= hindex
;
213 dockapp_copyarea(backdrop_on
, pixmap
, 0, 0, 58, 58, 0, 0);
217 backlight
= LIGHTOFF
;
218 dockapp_copyarea(backdrop_off
, pixmap
, 0, 0, 58, 58, 0, 0);
224 draw_digit(history
[hindex
]);
227 /* draw cpu number */
228 if (cpu_opts
.cpu_number
!= CPUNUM_NONE
)
233 for (i
= 0; i
< MAX_HISTORY
; i
++) {
234 h
= (21 * history
[j
]) / 100;
235 dockapp_copyarea(parts
, pixmap
, 100+x
, 21-h
, 2, h
, 51-3*i
, 54-h
);
237 if (j
< 0) j
= MAX_HISTORY
- 1;
241 dockapp_copy2window(pixmap
);
250 if (per
< 0) per
= 0;
251 if (per
> 100) per
= 100;
254 v10
= (per
- v100
* 100) / 10;
255 v1
= (per
- v100
* 100 - v10
* 10);
257 if (backlight
== LIGHTON
) {
262 dockapp_copyarea(parts
, pixmap
, v1
* 10, y
, 10, 20, 29, 7);
264 dockapp_copyarea(parts
, pixmap
, v10
* 10, y
, 10, 20, 17, 7);
267 dockapp_copyarea(parts
, pixmap
, 10, y
, 10, 20, 5, 7);
268 dockapp_copyarea(parts
, pixmap
, 0, y
, 10, 20, 17, 7);
269 dockapp_copyarea(parts
, pixmap
, 0, y
, 10, 20, 29, 7);
281 v10
= cpu_opts
.cpu_number
/ 10;
282 v1
= cpu_opts
.cpu_number
- v10
* 10;
284 if (backlight
== LIGHTON
) {
289 dockapp_copyarea(parts
, pixmap
, x_offset
+ v10
* 5, 40, 5, 9, 44, 10);
291 dockapp_copyarea(parts
, pixmap
, x_offset
+ v1
* 5, 40, 5, 9, 50, 10);
296 parse_arguments(int argc
, char **argv
)
299 for (i
= 1; i
< argc
; i
++) {
300 if (!strcmp(argv
[i
], "--help") || !strcmp(argv
[i
], "-h"))
301 print_help(argv
[0]), exit(0);
303 else if (!strcmp(argv
[i
], "--version") || !strcmp(argv
[i
], "-v"))
304 printf("%s version %s\n", PACKAGE
, VERSION
), exit(0);
306 else if (!strcmp(argv
[i
], "--display") || !strcmp(argv
[i
], "-d")) {
307 display_name
= argv
[i
+ 1];
311 else if (!strcmp(argv
[i
], "--backlight") || !strcmp(argv
[i
], "-bl"))
314 else if (!strcmp(argv
[i
], "--light-color") || !strcmp(argv
[i
], "-lc")) {
315 light_color
= argv
[i
+ 1];
319 else if (!strcmp(argv
[i
], "--ignore-nice") || !strcmp(argv
[i
], "-n"))
320 cpu_opts
.ignore_nice
= True
;
323 else if (!strcmp(argv
[i
], "--interval") || !strcmp(argv
[i
], "-i")) {
327 "%s: error parsing argument for option %s\n",
328 argv
[0], argv
[i
]), exit(1);
329 if (sscanf(argv
[i
+ 1], "%i", &integer
) != 1)
331 "%s: error parsing argument for option %s\n",
332 argv
[0], argv
[i
]), exit(1);
334 fprintf(stderr
, "%s: argument %s must be >=1\n",
335 argv
[0], argv
[i
]), exit(1);
336 update_interval
= integer
;
338 } else if (!strcmp(argv
[i
], "--alarm") || !strcmp(argv
[i
], "-a")) {
341 alarm_threshold
= 90;
342 else if (sscanf(argv
[i
+ 1], "%i", &integer
) != 1)
343 alarm_threshold
= 90;
344 else if (integer
< 0 || integer
> 100)
345 fprintf(stderr
, "%s: argument %s must be from 0 to 100\n",
346 argv
[0], argv
[i
]), exit(1);
348 alarm_threshold
= integer
, i
++;
349 } else if (!strcmp(argv
[i
], "--windowed")
350 || !strcmp(argv
[i
], "-w"))
351 dockapp_stat
= WINDOWED
;
353 else if (!strcmp(argv
[i
], "--windowed-withpanel")
354 || !strcmp(argv
[i
], "-wp"))
355 dockapp_stat
= WINDOWED_WITH_PANEL
;
357 else if (!strcmp(argv
[i
], "--broken-wm") || !strcmp(argv
[i
], "-bw"))
358 dockapp_isbrokenwm
= True
;
360 else if (!strcmp(argv
[i
], "--title") || !strcmp(argv
[i
], "-t")) {
366 else if (!strcmp(argv
[i
], "--ignore-proc") || !strcmp(argv
[i
], "-p")) {
368 fprintf(stderr
, "%s: error parsing argument for option %s\n",
369 argv
[0], argv
[i
]), exit(1);
370 if (argv
[i
+ 1][0] == '-')
371 fprintf(stderr
, "%s: error parsing argument for option %s\n",
372 argv
[0], argv
[i
]), exit(1);
373 while (i
+ 1 < argc
) {
374 if (!(argv
[i
+ 1][0] == '-')) {
375 if (strlen(argv
[i
+ 1]) >= COMM_LEN
)
376 fprintf(stderr
, "%s: command name %s is longer than 15 characters\n",
377 argv
[0], argv
[i
+ 1]), exit(1);
378 if (cpu_opts
.ignore_procs
== MAX_PROC
)
379 fprintf(stderr
, "%s: maximum number of command names is %d\n",
380 argv
[0], MAX_PROC
), exit(1);
381 cpu_opts
.ignore_proc_list
[cpu_opts
.ignore_procs
] = argv
[i
+ 1];
382 cpu_opts
.ignore_procs
++;
390 #endif /* IGNORE_PROC */
393 else if (!strcmp(argv
[i
], "--cpu") || !strcmp(argv
[i
], "-c")) {
396 fprintf(stderr
, "%s: error parsing argument for option %s\n",
397 argv
[0], argv
[i
]), exit(1);
398 if (sscanf(argv
[i
+ 1], "%i", &integer
) != 1)
399 fprintf(stderr
, "%s: error parsing argument for option %s\n",
400 argv
[0], argv
[i
]), exit(1);
402 fprintf(stderr
, "%s: argument %s must be >=0\n",
403 argv
[0], argv
[i
]), exit(1);
404 cpu_opts
.cpu_number
= integer
;
410 fprintf(stderr
, "%s: unrecognized option '%s'\n", argv
[0],
412 print_help(argv
[0]), exit(1);
416 #if defined(USE_SMP) && defined(IGNORE_PROC)
417 if (cpu_opts
.cpu_number
!= CPUNUM_NONE
&& cpu_opts
.ignore_procs
) {
418 fprintf(stderr
, "You can't use '-c, --cpu' option with '-p, --ignore_procs' option");
425 print_help(char *prog
)
427 printf("Usage : %s [OPTIONS]\n", prog
);
428 printf("WMCPULoad - A dockapp to monitor CPU usage\n");
429 printf(" -d, --display <string> display to use\n");
430 printf(" -t, --title <string> application title name\n");
431 printf(" -bl, --backlight turn on back-light\n");
432 printf(" -lc, --light-color <string> back-light color(rgb:6E/C6/3B is default)\n");
433 printf(" -i, --interval <number> number of secs between updates (1 is default)\n");
435 printf(" -c, --cpu <number> CPU number (0, 1, ... )\n");
438 printf(" -n, --ignore-nice ignore a nice value\n");
441 printf(" -p, --ignore-proc <name> .. ignore all processes specified by command name\n");
443 printf(" -h, --help show this help text and exit\n");
444 printf(" -v, --version show program version and exit\n");
445 printf(" -w, --windowed run the application in windowed mode\n");
446 printf(" -wp, --windowed-withpanel run the application in windowed mode\n");
447 printf(" with background panel\n");
448 printf(" -bw, --broken-wm activate broken window manager fix\n");
449 printf(" -a, --alarm <percentage> activate alarm mode. <percentage> is threshold\n");
450 printf(" of percentage from 0 to 100.(90 is default)\n");