wmbutton: Escape minus sign in manpage
[dockapps.git] / wmcpuload / src / main.c
blob9b5308f5c6443edec03287089f8d9af69d28e312
1 /*
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.
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
25 #include "libdockapp/dockapp.h"
26 #include "cpu.h"
27 #include "backlight_on.xpm"
28 #include "backlight_off.xpm"
29 #include "parts.xpm"
31 #define SIZE 58
32 #define WINDOWED_BG " \tc #AEAAAE"
33 #define MAX_HISTORY 16
34 #define CPUNUM_NONE -1
36 typedef enum { LIGHTON, LIGHTOFF } light;
38 Pixmap pixmap;
39 Pixmap backdrop_on;
40 Pixmap backdrop_off;
41 Pixmap parts;
42 Pixmap pix_chartbuf;
43 Pixmap mask;
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;
54 /* prototypes */
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);
60 #if USE_SMP
61 static void draw_cpunumber(void);
62 #endif
64 int
65 main(int argc, char **argv)
67 XEvent event;
68 XpmColorSymbol colors[2] = { {"Back0", NULL, 0}, {"Back1", NULL, 0} };
69 int ncolor = 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 */
78 cpu_init();
79 dockapp_open_window(display_name, title == NULL ? PACKAGE : title,
80 SIZE, SIZE, argc, argv);
81 dockapp_set_eventmask(ButtonPressMask);
83 if (light_color) {
84 colors[0].pixel = dockapp_getcolor_pixel(light_color);
85 colors[1].pixel = dockapp_blendedcolor(light_color, -24, -24, -24, 1.0);
86 ncolor = 2;
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);
96 /* shape window */
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);
108 } else {
109 dockapp_copyarea(backdrop_off, pixmap, 0, 0, SIZE, SIZE, 0, 0);
111 dockapp_set_background(pixmap);
112 dockapp_show();
114 /* Main loop */
115 for (;;) {
116 if (dockapp_nextevent_or_timeout(&event, update_interval * 1000)) {
117 /* Next Event */
118 switch(event.type) {
119 case ButtonPress:
120 switch_light();
121 break;
122 default: /* make gcc happy */
123 break;
125 } else {
126 /* Time Out */
127 update();
131 return 0;
134 /* called by timer */
135 static void
136 update(void)
138 int usage;
139 int x, h;
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);
146 hindex++;
147 if (hindex >= MAX_HISTORY) {
148 hindex = 0;
150 history[hindex] = usage;
152 /* alarm mode */
153 if (usage >= alarm_threshold) {
154 if (!in_alarm_mode) {
155 in_alarm_mode = True;
156 pre_backlight = backlight;
158 if (backlight == LIGHTOFF) {
159 switch_light();
160 return;
162 } else {
163 if (in_alarm_mode) {
164 in_alarm_mode = False;
165 if (backlight != pre_backlight) {
166 switch_light();
167 return;
172 /* save current chart */
173 dockapp_copyarea(pixmap, pix_chartbuf, 9, 33, 44, 21, 0, 0);
175 /* all clear */
176 if (backlight == LIGHTON) {
177 dockapp_copyarea(backdrop_on, pixmap, 0, 0, 58, 58, 0, 0);
178 x = 2;
179 } else {
180 dockapp_copyarea(backdrop_off, pixmap, 0, 0, 58, 58, 0, 0);
181 x = 0;
184 /* draw digit */
185 draw_digit(usage);
187 #ifdef USE_SMP
188 /* draw cpu number */
189 if (cpu_opts.cpu_number != CPUNUM_NONE)
190 draw_cpunumber();
191 #endif
193 /* draw chart */
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);
198 /* show */
199 dockapp_copy2window(pixmap);
203 /* called when mouse button pressed */
204 static void
205 switch_light(void)
207 int h, i, j = hindex;
208 int x = 0;
210 switch (backlight) {
211 case LIGHTOFF:
212 backlight = LIGHTON;
213 dockapp_copyarea(backdrop_on, pixmap, 0, 0, 58, 58, 0, 0);
214 x = 2;
215 break;
216 case LIGHTON:
217 backlight = LIGHTOFF;
218 dockapp_copyarea(backdrop_off, pixmap, 0, 0, 58, 58, 0, 0);
219 x = 0;
220 break;
223 /* redraw digit */
224 draw_digit(history[hindex]);
226 #ifdef USE_SMP
227 /* draw cpu number */
228 if (cpu_opts.cpu_number != CPUNUM_NONE)
229 draw_cpunumber();
230 #endif
232 /* redraw chart */
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);
236 j--;
237 if (j < 0) j = MAX_HISTORY - 1;
240 /* show */
241 dockapp_copy2window(pixmap);
244 static void
245 draw_digit(int per)
247 int v100, v10, v1;
248 int y = 0;
250 if (per < 0) per = 0;
251 if (per > 100) per = 100;
253 v100 = per / 100;
254 v10 = (per - v100 * 100) / 10;
255 v1 = (per - v100 * 100 - v10 * 10);
257 if (backlight == LIGHTON) {
258 y = 20;
261 /* draw digit */
262 dockapp_copyarea(parts, pixmap, v1 * 10, y, 10, 20, 29, 7);
263 if (v10 != 0) {
264 dockapp_copyarea(parts, pixmap, v10 * 10, y, 10, 20, 17, 7);
266 if (v100 == 1) {
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);
274 #ifdef USE_SMP
275 static void
276 draw_cpunumber(void)
278 int x_offset = 0;
279 int v10 = 0, v1 = 0;
281 v10 = cpu_opts.cpu_number / 10;
282 v1 = cpu_opts.cpu_number - v10 * 10;
284 if (backlight == LIGHTON) {
285 x_offset = 50;
288 if (v10) {
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);
293 #endif
295 static void
296 parse_arguments(int argc, char **argv)
298 int i;
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];
308 i++;
311 else if (!strcmp(argv[i], "--backlight") || !strcmp(argv[i], "-bl"))
312 backlight = LIGHTON;
314 else if (!strcmp(argv[i], "--light-color") || !strcmp(argv[i], "-lc")) {
315 light_color = argv[i + 1];
316 i++;
318 #ifdef IGNORE_NICE
319 else if (!strcmp(argv[i], "--ignore-nice") || !strcmp(argv[i], "-n"))
320 cpu_opts.ignore_nice = True;
321 #endif
323 else if (!strcmp(argv[i], "--interval") || !strcmp(argv[i], "-i")) {
324 int integer;
325 if (argc == i + 1)
326 fprintf(stderr,
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)
330 fprintf(stderr,
331 "%s: error parsing argument for option %s\n",
332 argv[0], argv[i]), exit(1);
333 if (integer < 1)
334 fprintf(stderr, "%s: argument %s must be >=1\n",
335 argv[0], argv[i]), exit(1);
336 update_interval = integer;
337 i++;
338 } else if (!strcmp(argv[i], "--alarm") || !strcmp(argv[i], "-a")) {
339 int integer;
340 if (argc == i + 1)
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);
347 else
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")) {
361 title = argv[i + 1];
362 i++;
365 #ifdef IGNORE_PROC
366 else if (!strcmp(argv[i], "--ignore-proc") || !strcmp(argv[i], "-p")) {
367 if (argc == i + 1)
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++;
383 } else {
384 break;
386 i++;
390 #endif /* IGNORE_PROC */
392 #ifdef USE_SMP
393 else if (!strcmp(argv[i], "--cpu") || !strcmp(argv[i], "-c")) {
394 int integer;
395 if (argc == i + 1)
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);
401 if (integer < 0)
402 fprintf(stderr, "%s: argument %s must be >=0\n",
403 argv[0], argv[i]), exit(1);
404 cpu_opts.cpu_number = integer;
405 i++;
407 #endif /* USE_SMP */
409 else {
410 fprintf(stderr, "%s: unrecognized option '%s'\n", argv[0],
411 argv[i]);
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");
419 exit (1);
421 #endif
424 static void
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");
434 #ifdef USE_SMP
435 printf(" -c, --cpu <number> CPU number (0, 1, ... )\n");
436 #endif
437 #ifdef IGNORE_NICE
438 printf(" -n, --ignore-nice ignore a nice value\n");
439 #endif
440 #ifdef IGNORE_PROC
441 printf(" -p, --ignore-proc <name> .. ignore all processes specified by command name\n");
442 #endif
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");