wmclockmon: update change-log
[dockapps.git] / wmmemload / src / main.c
blobf424398ed712372766a11d5e3a8fee8268ade9fa
1 /*
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.
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
28 #include "dockapp.h"
29 #include "mem.h"
30 #include "backlight_on.xpm"
31 #include "backlight_off.xpm"
32 #include "parts.xpm"
34 #define SIZE 58
35 #define WINDOWED_BG " \tc #AEAAAE"
36 #define MAX_HISTORY 16
37 #define CPUNUM_NONE -1
39 typedef enum { LIGHTON, LIGHTOFF } light;
41 Pixmap pixmap;
42 Pixmap backdrop_on;
43 Pixmap backdrop_off;
44 Pixmap parts;
45 Pixmap mask;
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;
52 static int mem_usage;
53 static int swap_usage;
54 static unsigned alarm_mem = 101;
55 static unsigned alarm_swap = 101;
56 time_t last_update = 0;
58 /* prototypes */
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)
68 XEvent event;
69 XpmColorSymbol colors[2] = { {"Back0", NULL, 0}, {"Back1", NULL, 0} };
70 int ncolor = 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 */
78 mem_init();
79 dockapp_open_window(display_name, PACKAGE, SIZE, SIZE, argc, argv);
80 dockapp_set_eventmask(ButtonPressMask);
82 if (light_color) {
83 colors[0].pixel = dockapp_getcolor(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_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");
94 exit(1);
96 if (!dockapp_xpm2pixmap(backlight_off_xpm, &backdrop_off, NULL, NULL, 0)) {
97 fprintf(stderr, "Error initializing background image.\n");
98 exit(1);
100 if (!dockapp_xpm2pixmap(parts_xpm, &parts, NULL, colors, ncolor)) {
101 fprintf(stderr, "Error initializing parts image.\n");
102 exit(1);
105 /* shape window */
106 if (!dockapp_iswindowed)
107 dockapp_setshape(mask, 0, 0);
108 if (mask)
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);
117 else
118 dockapp_copyarea(backdrop_off, pixmap, 0, 0, SIZE, SIZE, 0, 0);
120 dockapp_set_background(pixmap);
121 dockapp_show();
123 /* Main loop */
124 while (1) {
125 if (time(NULL) != last_update)
126 update();
128 while (XPending(display)) {
129 XNextEvent(display, &event);
131 switch (event.type) {
132 case ButtonPress:
133 switch_light();
134 break;
135 default: /* make gcc happy */
136 break;
139 usleep(update_interval*1000000L);
142 return 0;
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);
154 /* alarm mode */
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) {
161 switch_light();
162 return;
164 } else {
165 if (in_alarm_mode) {
166 in_alarm_mode = False;
167 if (backlight != pre_backlight) {
168 switch_light();
169 return;
174 /* all clear */
175 if (backlight == LIGHTON)
176 dockapp_copyarea(backdrop_on, pixmap, 0, 0, 58, 58, 0, 0);
177 else
178 dockapp_copyarea(backdrop_off, pixmap, 0, 0, 58, 58, 0, 0);
180 /* draw digit */
181 draw_memdigit(mem_usage);
182 draw_swapdigit(swap_usage);
184 /* show */
185 dockapp_copy2window(pixmap);
186 time(&last_update);
189 /* called when mouse button pressed */
190 static void switch_light(void)
192 switch (backlight) {
193 case LIGHTOFF:
194 backlight = LIGHTON;
195 dockapp_copyarea(backdrop_on, pixmap, 0, 0, 58, 58, 0, 0);
196 break;
197 case LIGHTON:
198 backlight = LIGHTOFF;
199 dockapp_copyarea(backdrop_off, pixmap, 0, 0, 58, 58, 0, 0);
200 break;
203 /* redraw digit */
204 mem_getusage(&mem_usage, &swap_usage, &mem_opts);
205 draw_memdigit(mem_usage);
206 draw_swapdigit(swap_usage);
208 /* show */
209 dockapp_copy2window(pixmap);
212 static void draw_memdigit(int per)
214 int v100, v10, v1;
215 int y = 0;
217 if (per < 0)
218 per = 0;
219 if (per > 100)
220 per = 100;
222 v100 = per / 100;
223 v10 = (per - v100 * 100) / 10;
224 v1 = (per - v100 * 100 - v10 * 10);
226 if (backlight == LIGHTON)
227 y = 20;
229 /* draw digit */
230 dockapp_copyarea(parts, pixmap, v1 * 10, y, 10, 20, 29, 7);
231 if (v10 != 0)
232 dockapp_copyarea(parts, pixmap, v10 * 10, y, 10, 20, 17, 7);
233 if (v100 == 1) {
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)
242 int v100, v10, v1;
243 int y = 0;
245 if (per < 0)
246 per = 0;
247 if (per > 100)
248 per = 100;
250 v100 = per / 100;
251 v10 = (per - v100 * 100) / 10;
252 v1 = (per - v100 * 100 - v10 * 10);
254 if (backlight == LIGHTON)
255 y = 20;
257 /* draw digit */
258 dockapp_copyarea(parts, pixmap, v1 * 10, y, 10, 20, 29, 34);
259 if (v10 != 0)
260 dockapp_copyarea(parts, pixmap, v10 * 10, y, 10, 20, 17, 34);
261 if (v100 == 1) {
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)
269 int i;
270 int integer;
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];
278 i++;
279 } else if (!strcmp(argv[i], "--alarm-mem") || !strcmp(argv[i], "-am")) {
280 if (argc == i + 1)
281 alarm_mem = 90;
282 else if (sscanf(argv[i + 1], "%i", &integer) != 1)
283 alarm_mem = 90;
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);
287 else
288 alarm_mem = integer, i++;
289 } else if (!strcmp(argv[i], "--alarm-swap") || !strcmp(argv[i], "-as")) {
290 if (argc == i + 1)
291 alarm_swap = 50;
292 else if (sscanf(argv[i + 1], "%i", &integer) != 1)
293 alarm_swap = 50;
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);
297 else
298 alarm_swap = integer, i++;
299 } else if (!strcmp(argv[i], "--backlight") || !strcmp(argv[i], "-bl"))
300 backlight = LIGHTON;
301 else if (!strcmp(argv[i], "--light-color") || !strcmp(argv[i], "-lc")) {
302 light_color = argv[i + 1];
303 i++;
304 } else if (!strcmp(argv[i], "--interval") || !strcmp(argv[i], "-i")) {
305 if (argc == i + 1)
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);
311 if (integer < 1)
312 fprintf(stderr, "%s: argument %s must be >=1\n",
313 argv[0], argv[i]), exit(1);
314 update_interval = integer;
315 i++;
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;
323 #endif
324 #ifdef IGNORE_CACHED
325 else if (!strcmp(argv[i], "--ignore-cached") || !strcmp(argv[i], "-c"))
326 mem_opts.ignore_cached = True;
327 #endif
328 #ifdef IGNORE_WIRED
329 else if (!strcmp(argv[i], "--ignore-wired") || !strcmp(argv[i], "-wr"))
330 mem_opts.ignore_wired = True;
331 #endif
332 else {
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) {
338 fprintf(stderr,
339 "%s: select either '-am, --alarm-mem' or '-as, --alarm-swap'\n",
340 argv[0]);
341 exit(1);
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");
355 #endif
356 #ifdef IGNORE_CACHED
357 printf(" -c, --ignore-cached ignore cached pages\n");
358 #endif
359 #ifdef IGNORE_WIRED
360 printf(" -wr, --ignore-wired ignore wired pages\n");
361 #endif
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");