2 * WMMemLoad - A dockapp to monitor memory usage
3 * Copyright (C) 2002 Mark Staggs <me@markstaggs.net>
5 * Based on work by Seiichi SATO <ssato@sh.rim.or.jp>
6 * Copyright (C) 2001,2002 Seiichi SATO <ssato@sh.rim.or.jp>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 #include "backlight_on.xpm"
31 #include "backlight_off.xpm"
35 #define WINDOWED_BG " \tc #AEAAAE"
36 #define MAX_HISTORY 16
37 #define CPUNUM_NONE -1
39 typedef enum { LIGHTON
, LIGHTOFF
} light
;
46 static char *display_name
= "";
47 static char *light_color
; /* back-light color */
48 static unsigned update_interval
= 1;
49 static light backlight
= LIGHTOFF
;
51 static struct mem_options mem_opts
;
53 static int swap_usage
;
54 static unsigned alarm_mem
= 101;
55 static unsigned alarm_swap
= 101;
58 static void update(void);
59 static void switch_light(void);
60 static void draw_memdigit(int per
);
61 static void draw_swapdigit(int per
);
62 static void parse_arguments(int argc
, char **argv
);
63 static void print_help(char *prog
);
65 int main(int argc
, char **argv
)
68 XpmColorSymbol colors
[2] = { {"Back0", NULL
, 0}, {"Back1", NULL
, 0} };
71 /* Parse CommandLine */
72 mem_opts
.ignore_buffers
= mem_opts
.ignore_cached
73 = mem_opts
.ignore_wired
= False
;
74 parse_arguments(argc
, argv
);
76 /* Initialize Application */
78 dockapp_open_window(display_name
, PACKAGE
, SIZE
, SIZE
, argc
, argv
);
79 dockapp_set_eventmask(ButtonPressMask
);
82 colors
[0].pixel
= dockapp_getcolor(light_color
);
83 colors
[1].pixel
= dockapp_blendedcolor(light_color
, -24, -24, -24, 1.0);
87 /* change raw xpm data to pixmap */
88 if (dockapp_iswindowed
)
89 backlight_on_xpm
[1] = backlight_off_xpm
[1] = WINDOWED_BG
;
91 if (!dockapp_xpm2pixmap(backlight_on_xpm
, &backdrop_on
, &mask
, colors
, ncolor
)) {
92 fprintf(stderr
, "Error initializing backlit background image.\n");
95 if (!dockapp_xpm2pixmap(backlight_off_xpm
, &backdrop_off
, NULL
, NULL
, 0)) {
96 fprintf(stderr
, "Error initializing background image.\n");
99 if (!dockapp_xpm2pixmap(parts_xpm
, &parts
, NULL
, colors
, ncolor
)) {
100 fprintf(stderr
, "Error initializing parts image.\n");
105 if (!dockapp_iswindowed
)
106 dockapp_setshape(mask
, 0, 0);
108 XFreePixmap(display
, mask
);
110 /* pixmap : draw area */
111 pixmap
= dockapp_XCreatePixmap(SIZE
, SIZE
);
113 /* Initialize pixmap */
114 if (backlight
== LIGHTON
)
115 dockapp_copyarea(backdrop_on
, pixmap
, 0, 0, SIZE
, SIZE
, 0, 0);
117 dockapp_copyarea(backdrop_off
, pixmap
, 0, 0, SIZE
, SIZE
, 0, 0);
119 dockapp_set_background(pixmap
);
124 if (dockapp_nextevent_or_timeout(&event
, update_interval
* 1000)) {
126 switch (event
.type
) {
130 default: /* make gcc happy */
142 /* called by timer */
143 static void update(void)
145 static light pre_backlight
;
146 static Bool in_alarm_mode
= False
;
148 /* get current cpu usage in percent */
149 mem_getusage(&mem_usage
, &swap_usage
, &mem_opts
);
152 if (mem_usage
>= alarm_mem
|| swap_usage
>= alarm_swap
) {
153 if (!in_alarm_mode
) {
154 in_alarm_mode
= True
;
155 pre_backlight
= backlight
;
157 if (backlight
== LIGHTOFF
) {
163 in_alarm_mode
= False
;
164 if (backlight
!= pre_backlight
) {
172 if (backlight
== LIGHTON
)
173 dockapp_copyarea(backdrop_on
, pixmap
, 0, 0, 58, 58, 0, 0);
175 dockapp_copyarea(backdrop_off
, pixmap
, 0, 0, 58, 58, 0, 0);
178 draw_memdigit(mem_usage
);
179 draw_swapdigit(swap_usage
);
182 dockapp_copy2window(pixmap
);
185 /* called when mouse button pressed */
186 static void switch_light(void)
191 dockapp_copyarea(backdrop_on
, pixmap
, 0, 0, 58, 58, 0, 0);
194 backlight
= LIGHTOFF
;
195 dockapp_copyarea(backdrop_off
, pixmap
, 0, 0, 58, 58, 0, 0);
200 mem_getusage(&mem_usage
, &swap_usage
, &mem_opts
);
201 draw_memdigit(mem_usage
);
202 draw_swapdigit(swap_usage
);
205 dockapp_copy2window(pixmap
);
208 static void draw_memdigit(int per
)
219 v10
= (per
- v100
* 100) / 10;
220 v1
= (per
- v100
* 100 - v10
* 10);
222 if (backlight
== LIGHTON
)
226 dockapp_copyarea(parts
, pixmap
, v1
* 10, y
, 10, 20, 29, 7);
228 dockapp_copyarea(parts
, pixmap
, v10
* 10, y
, 10, 20, 17, 7);
230 dockapp_copyarea(parts
, pixmap
, 10, y
, 10, 20, 5, 7);
231 dockapp_copyarea(parts
, pixmap
, 0, y
, 10, 20, 17, 7);
236 static void draw_swapdigit(int per
)
247 v10
= (per
- v100
* 100) / 10;
248 v1
= (per
- v100
* 100 - v10
* 10);
250 if (backlight
== LIGHTON
)
254 dockapp_copyarea(parts
, pixmap
, v1
* 10, y
, 10, 20, 29, 34);
256 dockapp_copyarea(parts
, pixmap
, v10
* 10, y
, 10, 20, 17, 34);
258 dockapp_copyarea(parts
, pixmap
, 10, y
, 10, 20, 5, 34);
259 dockapp_copyarea(parts
, pixmap
, 0, y
, 10, 20, 17, 34);
263 static void parse_arguments(int argc
, char **argv
)
267 for (i
= 1; i
< argc
; i
++) {
268 if (!strcmp(argv
[i
], "--help") || !strcmp(argv
[i
], "-h"))
269 print_help(argv
[0]), exit(0);
270 else if (!strcmp(argv
[i
], "--version") || !strcmp(argv
[i
], "-v"))
271 printf("%s version %s\n", PACKAGE
, VERSION
), exit(0);
272 else if (!strcmp(argv
[i
], "--display") || !strcmp(argv
[i
], "-d")) {
273 display_name
= argv
[i
+ 1];
275 } else if (!strcmp(argv
[i
], "--alarm-mem") || !strcmp(argv
[i
], "-am")) {
278 else if (sscanf(argv
[i
+ 1], "%i", &integer
) != 1)
280 else if (integer
< 0 || integer
> 100)
281 fprintf(stderr
, "%s: argument %s must be from 0 to 100\n",
282 argv
[0], argv
[i
]), exit(1);
284 alarm_mem
= integer
, i
++;
285 } else if (!strcmp(argv
[i
], "--alarm-swap") || !strcmp(argv
[i
], "-as")) {
288 else if (sscanf(argv
[i
+ 1], "%i", &integer
) != 1)
290 else if (integer
< 0 || integer
> 100)
291 fprintf(stderr
, "%s: argument %s must be from 0 to 100\n",
292 argv
[0], argv
[i
]), exit(1);
294 alarm_swap
= integer
, i
++;
295 } else if (!strcmp(argv
[i
], "--backlight") || !strcmp(argv
[i
], "-bl"))
297 else if (!strcmp(argv
[i
], "--light-color") || !strcmp(argv
[i
], "-lc")) {
298 light_color
= argv
[i
+ 1];
300 } else if (!strcmp(argv
[i
], "--interval") || !strcmp(argv
[i
], "-i")) {
302 fprintf(stderr
, "%s: error parsing argument for option %s\n",
303 argv
[0], argv
[i
]), exit(1);
304 if (sscanf(argv
[i
+ 1], "%i", &integer
) != 1)
305 fprintf(stderr
, "%s: error parsing argument for option %s\n",
306 argv
[0], argv
[i
]), exit(1);
308 fprintf(stderr
, "%s: argument %s must be >=1\n",
309 argv
[0], argv
[i
]), exit(1);
310 update_interval
= integer
;
312 } else if (!strcmp(argv
[i
], "--windowed") || !strcmp(argv
[i
], "-w"))
313 dockapp_iswindowed
= True
;
314 else if (!strcmp(argv
[i
], "--broken-wm") || !strcmp(argv
[i
], "-bw"))
315 dockapp_isbrokenwm
= True
;
316 #ifdef IGNORE_BUFFERS
317 else if (!strcmp(argv
[i
], "--ignore-buffers") || !strcmp(argv
[i
], "-b"))
318 mem_opts
.ignore_buffers
= True
;
321 else if (!strcmp(argv
[i
], "--ignore-cached") || !strcmp(argv
[i
], "-c"))
322 mem_opts
.ignore_cached
= True
;
325 else if (!strcmp(argv
[i
], "--ignore-wired") || !strcmp(argv
[i
], "-wr"))
326 mem_opts
.ignore_wired
= True
;
329 fprintf(stderr
, "%s: unrecognized option '%s'\n", argv
[0], argv
[i
]);
330 print_help(argv
[0]), exit(1);
333 if (alarm_mem
!= 101 && alarm_swap
!= 101) {
335 "%s: select either '-am, --alarm-mem' or '-as, --alarm-swap'\n",
341 static void print_help(char *prog
)
343 printf("Usage : %s [OPTIONS]\n", prog
);
344 printf("WMMemMon - Window Maker memory/swap monitor dockapp\n");
345 printf(" -d, --display <string> display to use\n");
346 printf(" -bl, --backlight turn on back-light\n");
347 printf(" -lc, --light-color <string> back-light color(rgb:6E/C6/3B is default)\n");
348 printf(" -i, --interval <number> number of secs between updates (1 is default)\n");
349 #ifdef IGNORE_BUFFERS
350 printf(" -b, --ignore-buffers ignore buffers\n");
353 printf(" -c, --ignore-cached ignore cached pages\n");
356 printf(" -wr, --ignore-wired ignore wired pages\n");
358 printf(" -h, --help show this help text and exit\n");
359 printf(" -v, --version show program version and exit\n");
360 printf(" -w, --windowed run the application in windowed mode\n");
361 printf(" -bw, --broken-wm activate broken window manager fix\n");
362 printf(" -am, --alarm-mem <percentage> activate alarm mode of memory. <percentage>\n");
363 printf(" is threshold of percentage from 0 to 100.\n");
364 printf(" (90 is default)\n");
365 printf(" -as, --alarm-swap <percentage> activate alarm mode of swap. <percentage> is\n");
366 printf(" threshold of percentage from 0 to 100.\n");
367 printf(" (50 is default)\n");