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;
56 time_t last_update
= 0;
59 static void update(void);
60 static void switch_light(void);
61 static void draw_memdigit(int per
);
62 static void draw_swapdigit(int per
);
63 static void parse_arguments(int argc
, char **argv
);
64 static void print_help(char *prog
);
66 int main(int argc
, char **argv
)
69 XpmColorSymbol colors
[2] = { {"Back0", NULL
, 0}, {"Back1", NULL
, 0} };
72 /* Parse CommandLine */
73 mem_opts
.ignore_buffers
= mem_opts
.ignore_cached
74 = mem_opts
.ignore_wired
= False
;
75 parse_arguments(argc
, argv
);
77 /* Initialize Application */
79 dockapp_open_window(display_name
, PACKAGE
, SIZE
, SIZE
, argc
, argv
);
80 dockapp_set_eventmask(ButtonPressMask
);
83 colors
[0].pixel
= dockapp_getcolor(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_iswindowed
)
90 backlight_on_xpm
[1] = backlight_off_xpm
[1] = WINDOWED_BG
;
92 if (!dockapp_xpm2pixmap(backlight_on_xpm
, &backdrop_on
, &mask
, colors
, ncolor
)) {
93 fprintf(stderr
, "Error initializing backlit background image.\n");
96 if (!dockapp_xpm2pixmap(backlight_off_xpm
, &backdrop_off
, NULL
, NULL
, 0)) {
97 fprintf(stderr
, "Error initializing background image.\n");
100 if (!dockapp_xpm2pixmap(parts_xpm
, &parts
, NULL
, colors
, ncolor
)) {
101 fprintf(stderr
, "Error initializing parts image.\n");
106 if (!dockapp_iswindowed
)
107 dockapp_setshape(mask
, 0, 0);
109 XFreePixmap(display
, mask
);
111 /* pixmap : draw area */
112 pixmap
= dockapp_XCreatePixmap(SIZE
, SIZE
);
114 /* Initialize pixmap */
115 if (backlight
== LIGHTON
)
116 dockapp_copyarea(backdrop_on
, pixmap
, 0, 0, SIZE
, SIZE
, 0, 0);
118 dockapp_copyarea(backdrop_off
, pixmap
, 0, 0, SIZE
, SIZE
, 0, 0);
120 dockapp_set_background(pixmap
);
125 if (time(NULL
) != last_update
)
128 while (XPending(display
)) {
129 XNextEvent(display
, &event
);
131 switch (event
.type
) {
135 default: /* make gcc happy */
139 usleep(update_interval
*1000000L);
145 /* called by timer */
146 static void update(void)
148 static light pre_backlight
;
149 static Bool in_alarm_mode
= False
;
151 /* get current cpu usage in percent */
152 mem_getusage(&mem_usage
, &swap_usage
, &mem_opts
);
155 if (mem_usage
>= alarm_mem
|| swap_usage
>= alarm_swap
) {
156 if (!in_alarm_mode
) {
157 in_alarm_mode
= True
;
158 pre_backlight
= backlight
;
160 if (backlight
== LIGHTOFF
) {
166 in_alarm_mode
= False
;
167 if (backlight
!= pre_backlight
) {
175 if (backlight
== LIGHTON
)
176 dockapp_copyarea(backdrop_on
, pixmap
, 0, 0, 58, 58, 0, 0);
178 dockapp_copyarea(backdrop_off
, pixmap
, 0, 0, 58, 58, 0, 0);
181 draw_memdigit(mem_usage
);
182 draw_swapdigit(swap_usage
);
185 dockapp_copy2window(pixmap
);
189 /* called when mouse button pressed */
190 static void switch_light(void)
195 dockapp_copyarea(backdrop_on
, pixmap
, 0, 0, 58, 58, 0, 0);
198 backlight
= LIGHTOFF
;
199 dockapp_copyarea(backdrop_off
, pixmap
, 0, 0, 58, 58, 0, 0);
204 mem_getusage(&mem_usage
, &swap_usage
, &mem_opts
);
205 draw_memdigit(mem_usage
);
206 draw_swapdigit(swap_usage
);
209 dockapp_copy2window(pixmap
);
212 static void draw_memdigit(int per
)
223 v10
= (per
- v100
* 100) / 10;
224 v1
= (per
- v100
* 100 - v10
* 10);
226 if (backlight
== LIGHTON
)
230 dockapp_copyarea(parts
, pixmap
, v1
* 10, y
, 10, 20, 29, 7);
232 dockapp_copyarea(parts
, pixmap
, v10
* 10, y
, 10, 20, 17, 7);
234 dockapp_copyarea(parts
, pixmap
, 10, y
, 10, 20, 5, 7);
235 dockapp_copyarea(parts
, pixmap
, 0, y
, 10, 20, 17, 7);
240 static void draw_swapdigit(int per
)
251 v10
= (per
- v100
* 100) / 10;
252 v1
= (per
- v100
* 100 - v10
* 10);
254 if (backlight
== LIGHTON
)
258 dockapp_copyarea(parts
, pixmap
, v1
* 10, y
, 10, 20, 29, 34);
260 dockapp_copyarea(parts
, pixmap
, v10
* 10, y
, 10, 20, 17, 34);
262 dockapp_copyarea(parts
, pixmap
, 10, y
, 10, 20, 5, 34);
263 dockapp_copyarea(parts
, pixmap
, 0, y
, 10, 20, 17, 34);
267 static void parse_arguments(int argc
, char **argv
)
271 for (i
= 1; i
< argc
; i
++) {
272 if (!strcmp(argv
[i
], "--help") || !strcmp(argv
[i
], "-h"))
273 print_help(argv
[0]), exit(0);
274 else if (!strcmp(argv
[i
], "--version") || !strcmp(argv
[i
], "-v"))
275 printf("%s version %s\n", PACKAGE
, VERSION
), exit(0);
276 else if (!strcmp(argv
[i
], "--display") || !strcmp(argv
[i
], "-d")) {
277 display_name
= argv
[i
+ 1];
279 } else if (!strcmp(argv
[i
], "--alarm-mem") || !strcmp(argv
[i
], "-am")) {
282 else if (sscanf(argv
[i
+ 1], "%i", &integer
) != 1)
284 else if (integer
< 0 || integer
> 100)
285 fprintf(stderr
, "%s: argument %s must be from 0 to 100\n",
286 argv
[0], argv
[i
]), exit(1);
288 alarm_mem
= integer
, i
++;
289 } else if (!strcmp(argv
[i
], "--alarm-swap") || !strcmp(argv
[i
], "-as")) {
292 else if (sscanf(argv
[i
+ 1], "%i", &integer
) != 1)
294 else if (integer
< 0 || integer
> 100)
295 fprintf(stderr
, "%s: argument %s must be from 0 to 100\n",
296 argv
[0], argv
[i
]), exit(1);
298 alarm_swap
= integer
, i
++;
299 } else if (!strcmp(argv
[i
], "--backlight") || !strcmp(argv
[i
], "-bl"))
301 else if (!strcmp(argv
[i
], "--light-color") || !strcmp(argv
[i
], "-lc")) {
302 light_color
= argv
[i
+ 1];
304 } else if (!strcmp(argv
[i
], "--interval") || !strcmp(argv
[i
], "-i")) {
306 fprintf(stderr
, "%s: error parsing argument for option %s\n",
307 argv
[0], argv
[i
]), exit(1);
308 if (sscanf(argv
[i
+ 1], "%i", &integer
) != 1)
309 fprintf(stderr
, "%s: error parsing argument for option %s\n",
310 argv
[0], argv
[i
]), exit(1);
312 fprintf(stderr
, "%s: argument %s must be >=1\n",
313 argv
[0], argv
[i
]), exit(1);
314 update_interval
= integer
;
316 } else if (!strcmp(argv
[i
], "--windowed") || !strcmp(argv
[i
], "-w"))
317 dockapp_iswindowed
= True
;
318 else if (!strcmp(argv
[i
], "--broken-wm") || !strcmp(argv
[i
], "-bw"))
319 dockapp_isbrokenwm
= True
;
320 #ifdef IGNORE_BUFFERS
321 else if (!strcmp(argv
[i
], "--ignore-buffers") || !strcmp(argv
[i
], "-b"))
322 mem_opts
.ignore_buffers
= True
;
325 else if (!strcmp(argv
[i
], "--ignore-cached") || !strcmp(argv
[i
], "-c"))
326 mem_opts
.ignore_cached
= True
;
329 else if (!strcmp(argv
[i
], "--ignore-wired") || !strcmp(argv
[i
], "-wr"))
330 mem_opts
.ignore_wired
= True
;
333 fprintf(stderr
, "%s: unrecognized option '%s'\n", argv
[0], argv
[i
]);
334 print_help(argv
[0]), exit(1);
337 if (alarm_mem
!= 101 && alarm_swap
!= 101) {
339 "%s: select either '-am, --alarm-mem' or '-as, --alarm-swap'\n",
345 static void print_help(char *prog
)
347 printf("Usage : %s [OPTIONS]\n", prog
);
348 printf("WMMemMon - Window Maker memory/swap monitor dockapp\n");
349 printf(" -d, --display <string> display to use\n");
350 printf(" -bl, --backlight turn on back-light\n");
351 printf(" -lc, --light-color <string> back-light color(rgb:6E/C6/3B is default)\n");
352 printf(" -i, --interval <number> number of secs between updates (1 is default)\n");
353 #ifdef IGNORE_BUFFERS
354 printf(" -b, --ignore-buffers ignore buffers\n");
357 printf(" -c, --ignore-cached ignore cached pages\n");
360 printf(" -wr, --ignore-wired ignore wired pages\n");
362 printf(" -h, --help show this help text and exit\n");
363 printf(" -v, --version show program version and exit\n");
364 printf(" -w, --windowed run the application in windowed mode\n");
365 printf(" -bw, --broken-wm activate broken window manager fix\n");
366 printf(" -am, --alarm-mem <percentage> activate alarm mode of memory. <percentage>\n");
367 printf(" is threshold of percentage from 0 to 100.\n");
368 printf(" (90 is default)\n");
369 printf(" -as, --alarm-swap <percentage> activate alarm mode of swap. <percentage> is\n");
370 printf(" threshold of percentage from 0 to 100.\n");
371 printf(" (50 is default)\n");