wmbattery: fix a couple of potential memory-leaks.
[dockapps.git] / wmcpuload / src / main.c
blobf3745ca24d6e630679783316d2a17be89a235a1a
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., 675 Mass Ave, Cambridge, MA 02139, 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 backdrop_on;
39 Pixmap backdrop_off;
40 Pixmap parts;
41 Pixmap mask;
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;
52 /* prototypes */
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);
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 parse_arguments(argc, argv);
76 /* Initialize Application */
77 cpu_init();
78 dockapp_open_window(display_name, title == NULL ? PACKAGE : title,
79 SIZE, SIZE, argc, argv);
80 dockapp_set_eventmask(ButtonPressMask | ExposureMask);
82 if (light_color) {
83 colors[0].pixel = dockapp_getcolor_pixel(light_color);
84 colors[1].pixel = dockapp_blendedcolor(light_color, -24, -24, -24, 1.0);
85 ncolor = 2;
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);
94 /* shape window */
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);
103 } else {
104 dockapp_set_background(backdrop_off);
106 dockapp_show();
108 /* Main loop */
109 for (;;) {
110 if (dockapp_nextevent_or_timeout(&event, update_interval * 1000)) {
111 /* Next Event */
112 switch(event.type) {
113 case ButtonPress:
114 switch_light();
115 redraw();
116 break;
117 case Expose:
118 redraw();
119 break;
120 default: /* make gcc happy */
121 break;
123 } else {
124 /* Time Out */
125 update();
129 return 0;
132 /* called by timer */
133 static void
134 update(void)
136 int usage;
137 int x, h;
138 Pixmap backdrop;
140 static light pre_backlight;
141 static Bool in_alarm_mode = False;
143 if (backlight == LIGHTON) {
144 backdrop = backdrop_on;
145 x = 2;
146 } else {
147 backdrop = backdrop_off;
148 x = 0;
151 /* get current cpu usage in percent */
152 usage = cpu_get_usage(&cpu_opts);
153 hindex++;
154 if (hindex >= MAX_HISTORY) {
155 hindex = 0;
157 history[hindex] = usage;
159 /* alarm mode */
160 if (usage >= alarm_threshold) {
161 if (!in_alarm_mode) {
162 in_alarm_mode = True;
163 pre_backlight = backlight;
165 if (backlight == LIGHTOFF) {
166 switch_light();
167 redraw();
168 return;
170 } else {
171 if (in_alarm_mode) {
172 in_alarm_mode = False;
173 if (backlight != pre_backlight) {
174 switch_light();
175 redraw();
176 return;
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);
185 /* clear digit */
186 dockapp_copy2window(backdrop, 5, 7, 34, 20, 5, 7);
188 /* draw digit */
189 draw_digit(usage);
191 #ifdef USE_SMP
192 /* draw cpu number */
193 if (cpu_opts.cpu_number != CPUNUM_NONE)
194 draw_cpunumber();
195 #endif
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 */
203 static void
204 switch_light(void)
206 switch (backlight) {
207 case LIGHTOFF:
208 backlight = LIGHTON;
209 break;
210 case LIGHTON:
211 backlight = LIGHTOFF;
212 break;
216 static void
217 redraw(void)
219 int h, i, j = hindex;
220 int x = 0;
221 Pixmap backdrop;
223 if (backlight == LIGHTON) {
224 backdrop = backdrop_on;
225 x = 2;
226 } else {
227 backdrop = backdrop_off;
228 x = 0;
231 dockapp_copy2window(backdrop, 0, 0, 58, 58, 0, 0);
233 /* redraw digit */
234 draw_digit(history[hindex]);
236 #ifdef USE_SMP
237 /* draw cpu number */
238 if (cpu_opts.cpu_number != CPUNUM_NONE)
239 draw_cpunumber();
240 #endif
242 /* redraw chart */
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);
246 j--;
247 if (j < 0) j = MAX_HISTORY - 1;
251 static void
252 draw_digit(int per)
254 int v100, v10, v1;
255 int y = 0;
257 if (per < 0) per = 0;
258 if (per > 100) per = 100;
260 v100 = per / 100;
261 v10 = (per - v100 * 100) / 10;
262 v1 = (per - v100 * 100 - v10 * 10);
264 if (backlight == LIGHTON) {
265 y = 20;
268 /* draw digit */
269 dockapp_copy2window(parts, v1 * 10, y, 10, 20, 29, 7);
270 if (v10 != 0) {
271 dockapp_copy2window(parts, v10 * 10, y, 10, 20, 17, 7);
273 if (v100 == 1) {
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);
282 #ifdef USE_SMP
283 static void
284 draw_cpunumber(void)
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;
293 if (v2)
294 dockapp_copy2window(parts, x + v2 * 5, 40, 5, 9, 44, 10);
296 dockapp_copy2window(parts, x + v1 * 5, 40, 5, 9, 50, 10);
298 #endif
300 static void
301 parse_arguments(int argc, char **argv)
303 int i;
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];
313 i++;
316 else if (!strcmp(argv[i], "--backlight") || !strcmp(argv[i], "-bl"))
317 backlight = LIGHTON;
319 else if (!strcmp(argv[i], "--light-color") || !strcmp(argv[i], "-lc")) {
320 light_color = argv[i + 1];
321 i++;
323 #ifdef IGNORE_NICE
324 else if (!strcmp(argv[i], "--ignore-nice") || !strcmp(argv[i], "-n"))
325 cpu_opts.ignore_nice = True;
326 #endif
328 else if (!strcmp(argv[i], "--interval") || !strcmp(argv[i], "-i")) {
329 int integer;
330 if (argc == i + 1)
331 fprintf(stderr,
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)
335 fprintf(stderr,
336 "%s: error parsing argument for option %s\n",
337 argv[0], argv[i]), exit(1);
338 if (integer < 1)
339 fprintf(stderr, "%s: argument %s must be >=1\n",
340 argv[0], argv[i]), exit(1);
341 update_interval = integer;
342 i++;
343 } else if (!strcmp(argv[i], "--alarm") || !strcmp(argv[i], "-a")) {
344 int integer;
345 if (argc == i + 1)
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);
352 else
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")) {
366 title = argv[i + 1];
367 i++;
370 #ifdef USE_SMP
371 else if (!strcmp(argv[i], "--cpu") || !strcmp(argv[i], "-c")) {
372 int integer;
373 if (argc == i + 1)
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);
379 if (integer < 0)
380 fprintf(stderr, "%s: argument %s must be >=0\n",
381 argv[0], argv[i]), exit(1);
382 cpu_opts.cpu_number = integer;
383 i++;
385 #endif /* USE_SMP */
387 else {
388 fprintf(stderr, "%s: unrecognized option '%s'\n", argv[0],
389 argv[i]);
390 print_help(argv[0]), exit(1);
395 static void
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");
407 #ifdef USE_SMP
408 printf(" -c, --cpu <number> "
409 "which CPU is monitored (0, 1, ... )\n");
410 #endif
411 #ifdef IGNORE_NICE
412 printf(" -n, --ignore-nice ignore a nice value\n");
413 #endif
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");
425 printf(" "
426 "of percentage from 0 to 100.(90 is default)\n");
428 /* ex:set sw=4 softtabstop=4: */