wmload: Replace deprecated Imakefile with Makefile.
[dockapps.git] / wmmemload / src / main.c
blobee038a993c7b137adcd402d41b137676c5d08c49
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;
57 /* prototypes */
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)
67 XEvent event;
68 XpmColorSymbol colors[2] = { {"Back0", NULL, 0}, {"Back1", NULL, 0} };
69 int ncolor = 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 */
77 mem_init();
78 dockapp_open_window(display_name, PACKAGE, SIZE, SIZE, argc, argv);
79 dockapp_set_eventmask(ButtonPressMask);
81 if (light_color) {
82 colors[0].pixel = dockapp_getcolor(light_color);
83 colors[1].pixel = dockapp_blendedcolor(light_color, -24, -24, -24, 1.0);
84 ncolor = 2;
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");
93 exit(1);
95 if (!dockapp_xpm2pixmap(backlight_off_xpm, &backdrop_off, NULL, NULL, 0)) {
96 fprintf(stderr, "Error initializing background image.\n");
97 exit(1);
99 if (!dockapp_xpm2pixmap(parts_xpm, &parts, NULL, colors, ncolor)) {
100 fprintf(stderr, "Error initializing parts image.\n");
101 exit(1);
104 /* shape window */
105 if (!dockapp_iswindowed)
106 dockapp_setshape(mask, 0, 0);
107 if (mask)
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);
116 else
117 dockapp_copyarea(backdrop_off, pixmap, 0, 0, SIZE, SIZE, 0, 0);
119 dockapp_set_background(pixmap);
120 dockapp_show();
122 /* Main loop */
123 while (1) {
124 if (dockapp_nextevent_or_timeout(&event, update_interval * 1000)) {
125 /* Next Event */
126 switch (event.type) {
127 case ButtonPress:
128 switch_light();
129 break;
130 default: /* make gcc happy */
131 break;
133 } else {
134 /* Time Out */
135 update();
139 return 0;
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);
151 /* alarm mode */
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) {
158 switch_light();
159 return;
161 } else {
162 if (in_alarm_mode) {
163 in_alarm_mode = False;
164 if (backlight != pre_backlight) {
165 switch_light();
166 return;
171 /* all clear */
172 if (backlight == LIGHTON)
173 dockapp_copyarea(backdrop_on, pixmap, 0, 0, 58, 58, 0, 0);
174 else
175 dockapp_copyarea(backdrop_off, pixmap, 0, 0, 58, 58, 0, 0);
177 /* draw digit */
178 draw_memdigit(mem_usage);
179 draw_swapdigit(swap_usage);
181 /* show */
182 dockapp_copy2window(pixmap);
185 /* called when mouse button pressed */
186 static void switch_light(void)
188 switch (backlight) {
189 case LIGHTOFF:
190 backlight = LIGHTON;
191 dockapp_copyarea(backdrop_on, pixmap, 0, 0, 58, 58, 0, 0);
192 break;
193 case LIGHTON:
194 backlight = LIGHTOFF;
195 dockapp_copyarea(backdrop_off, pixmap, 0, 0, 58, 58, 0, 0);
196 break;
199 /* redraw digit */
200 mem_getusage(&mem_usage, &swap_usage, &mem_opts);
201 draw_memdigit(mem_usage);
202 draw_swapdigit(swap_usage);
204 /* show */
205 dockapp_copy2window(pixmap);
208 static void draw_memdigit(int per)
210 int v100, v10, v1;
211 int y = 0;
213 if (per < 0)
214 per = 0;
215 if (per > 100)
216 per = 100;
218 v100 = per / 100;
219 v10 = (per - v100 * 100) / 10;
220 v1 = (per - v100 * 100 - v10 * 10);
222 if (backlight == LIGHTON)
223 y = 20;
225 /* draw digit */
226 dockapp_copyarea(parts, pixmap, v1 * 10, y, 10, 20, 29, 7);
227 if (v10 != 0)
228 dockapp_copyarea(parts, pixmap, v10 * 10, y, 10, 20, 17, 7);
229 if (v100 == 1) {
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)
238 int v100, v10, v1;
239 int y = 0;
241 if (per < 0)
242 per = 0;
243 if (per > 100)
244 per = 100;
246 v100 = per / 100;
247 v10 = (per - v100 * 100) / 10;
248 v1 = (per - v100 * 100 - v10 * 10);
250 if (backlight == LIGHTON)
251 y = 20;
253 /* draw digit */
254 dockapp_copyarea(parts, pixmap, v1 * 10, y, 10, 20, 29, 34);
255 if (v10 != 0)
256 dockapp_copyarea(parts, pixmap, v10 * 10, y, 10, 20, 17, 34);
257 if (v100 == 1) {
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)
265 int i;
266 int integer;
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];
274 i++;
275 } else if (!strcmp(argv[i], "--alarm-mem") || !strcmp(argv[i], "-am")) {
276 if (argc == i + 1)
277 alarm_mem = 90;
278 else if (sscanf(argv[i + 1], "%i", &integer) != 1)
279 alarm_mem = 90;
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);
283 else
284 alarm_mem = integer, i++;
285 } else if (!strcmp(argv[i], "--alarm-swap") || !strcmp(argv[i], "-as")) {
286 if (argc == i + 1)
287 alarm_swap = 50;
288 else if (sscanf(argv[i + 1], "%i", &integer) != 1)
289 alarm_swap = 50;
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);
293 else
294 alarm_swap = integer, i++;
295 } else if (!strcmp(argv[i], "--backlight") || !strcmp(argv[i], "-bl"))
296 backlight = LIGHTON;
297 else if (!strcmp(argv[i], "--light-color") || !strcmp(argv[i], "-lc")) {
298 light_color = argv[i + 1];
299 i++;
300 } else if (!strcmp(argv[i], "--interval") || !strcmp(argv[i], "-i")) {
301 if (argc == i + 1)
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);
307 if (integer < 1)
308 fprintf(stderr, "%s: argument %s must be >=1\n",
309 argv[0], argv[i]), exit(1);
310 update_interval = integer;
311 i++;
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;
319 #endif
320 #ifdef IGNORE_CACHED
321 else if (!strcmp(argv[i], "--ignore-cached") || !strcmp(argv[i], "-c"))
322 mem_opts.ignore_cached = True;
323 #endif
324 #ifdef IGNORE_WIRED
325 else if (!strcmp(argv[i], "--ignore-wired") || !strcmp(argv[i], "-wr"))
326 mem_opts.ignore_wired = True;
327 #endif
328 else {
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) {
334 fprintf(stderr,
335 "%s: select either '-am, --alarm-mem' or '-as, --alarm-swap'\n",
336 argv[0]);
337 exit(1);
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");
351 #endif
352 #ifdef IGNORE_CACHED
353 printf(" -c, --ignore-cached ignore cached pages\n");
354 #endif
355 #ifdef IGNORE_WIRED
356 printf(" -wr, --ignore-wired ignore wired pages\n");
357 #endif
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");