wmauda: Fix installation dir
[dockapps.git] / wmacpiload-0.1.2 / src / main.c
blob4f41514b6ea977f101a93f79b290b3ded1da875c
1 /*
2 * WMAcpiLoad - A dockapp to monitor ACPI status
3 * Copyright (C) 2002 Thomas Nemeth <tnemeth@free.fr>
5 * Based on work by Seiichi SATO <ssato@sh.rim.or.jp>
6 * Copyright (C) 2001,2002 Seiichi SATO <ssato@sh.rim.or.jp>
7 * and on work by Mark Staggs <me@markstaggs.net>
8 * Copyright (C) 2002 Mark Staggs <me@markstaggs.net>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
30 #include <signal.h>
31 #include "dockapp.h"
32 #include "backlight_on.xpm"
33 #include "backlight_off.xpm"
34 #include "parts.xpm"
36 #ifdef linux
37 #include <sys/stat.h>
38 #endif
40 #define FREE(data) {if (data) free (data); data = NULL;}
42 #define SIZE 58
43 #define MAXSTRLEN 512
44 #define WINDOWED_BG ". c #AEAAAE"
45 #define MAX_HISTORY 16
46 #define CPUNUM_NONE -1
48 # ifdef linux
49 # define ACPIDEV "/proc/acpi/info"
50 # endif
52 typedef struct AcpiInfos {
53 const char driver_version[10];
54 int ac_line_status;
55 int battery_status;
56 int battery_percentage;
57 float remain;
58 float currcap;
59 int thermal_temp;
60 int thermal_state;
61 } AcpiInfos;
63 typedef enum { LIGHTOFF, LIGHTON } light;
66 Pixmap pixmap;
67 Pixmap backdrop_on;
68 Pixmap backdrop_off;
69 Pixmap parts;
70 Pixmap mask;
71 static char *display_name = "";
72 static char *light_color = NULL; /* back-light color */
73 static unsigned update_interval = 1;
74 static light backlight = LIGHTOFF;
75 static unsigned switch_authorized = True;
76 static unsigned alarm_level = 20;
77 static unsigned alarm_level_temp = 70;
78 static char *notif_cmd = NULL;
79 static char *suspend_cmd = NULL;
80 static char *standby_cmd = NULL;
82 static AcpiInfos cur_acpi_infos;
84 #ifdef linux
85 # ifndef ACPI_32_BIT_SUPPORT
86 # define ACPI_32_BIT_SUPPORT 0x0002
87 # endif
88 #endif
91 /* prototypes */
92 static void update();
93 static void switch_light();
94 static void draw_timedigit(AcpiInfos infos);
95 static void draw_pcdigit(AcpiInfos infos);
96 static void draw_statusdigit(AcpiInfos infos);
97 static void draw_pcgraph(AcpiInfos infos);
98 static void parse_arguments(int argc, char **argv);
99 static void print_help(char *prog);
100 static void acpi_getinfos(AcpiInfos *infos);
101 static int acpi_exists();
102 static int my_system (char *cmd);
103 #ifdef linux
104 int acpi_read(AcpiInfos *i);
105 int init_stats(AcpiInfos *k);
106 #endif
108 int count;
111 int main(int argc, char **argv) {
112 XEvent event;
113 XpmColorSymbol colors[2] = { {"Back0", NULL, 0}, {"Back1", NULL, 0} };
114 int ncolor = 0;
115 struct sigaction sa;
117 sa.sa_handler = SIG_IGN;
118 #ifdef SA_NOCLDWAIT
119 sa.sa_flags = SA_NOCLDWAIT;
120 #else
121 sa.sa_flags = 0;
122 #endif
123 sigemptyset(&sa.sa_mask);
124 sigaction(SIGCHLD, &sa, NULL);
126 /* Parse CommandLine */
127 parse_arguments(argc, argv);
129 /* Check for ACPI support */
130 if (!acpi_exists()) {
131 #ifdef linux
132 fprintf(stderr, "No ACPI support in kernel\n");
133 #else
134 fprintf(stderr, "Unable to access ACPI info\n");
135 #endif
136 exit(1);
139 /* Initialize Application */
140 init_stats(&cur_acpi_infos);
141 acpi_getinfos(&cur_acpi_infos);
142 dockapp_open_window(display_name, PACKAGE, SIZE, SIZE, argc, argv);
143 dockapp_set_eventmask(ButtonPressMask);
145 if (light_color) {
146 colors[0].pixel = dockapp_getcolor(light_color);
147 colors[1].pixel = dockapp_blendedcolor(light_color, -24, -24, -24, 1.0);
148 ncolor = 2;
151 /* change raw xpm data to pixmap */
152 if (dockapp_iswindowed)
153 backlight_on_xpm[1] = backlight_off_xpm[1] = WINDOWED_BG;
155 if (!dockapp_xpm2pixmap(backlight_on_xpm, &backdrop_on, &mask, colors, ncolor)) {
156 fprintf(stderr, "Error initializing backlit background image.\n");
157 exit(1);
159 if (!dockapp_xpm2pixmap(backlight_off_xpm, &backdrop_off, NULL, NULL, 0)) {
160 fprintf(stderr, "Error initializing background image.\n");
161 exit(1);
163 if (!dockapp_xpm2pixmap(parts_xpm, &parts, NULL, colors, ncolor)) {
164 fprintf(stderr, "Error initializing parts image.\n");
165 exit(1);
168 /* shape window */
169 if (!dockapp_iswindowed) dockapp_setshape(mask, 0, 0);
170 if (mask) XFreePixmap(display, mask);
172 /* pixmap : draw area */
173 pixmap = dockapp_XCreatePixmap(SIZE, SIZE);
175 /* Initialize pixmap */
176 if (backlight == LIGHTON)
177 dockapp_copyarea(backdrop_on, pixmap, 0, 0, SIZE, SIZE, 0, 0);
178 else
179 dockapp_copyarea(backdrop_off, pixmap, 0, 0, SIZE, SIZE, 0, 0);
181 dockapp_set_background(pixmap);
182 dockapp_show();
184 /* Main loop */
185 while (1) {
186 if (dockapp_nextevent_or_timeout(&event, update_interval * 1000)) {
187 /* Next Event */
188 switch (event.type) {
189 case ButtonPress:
190 switch (event.xbutton.button) {
191 case 1: switch_light(); break;
192 default: break;
194 break;
195 default: break;
197 } else {
198 /* Time Out */
199 update();
203 return 0;
205 int init_stats(AcpiInfos *k) {
207 FILE *fd;
208 char *buf;
209 char *ptr;
210 buf=(char *)malloc(sizeof(char)*512);
212 /* get info about full battery charge */
214 if ((fd = fopen("/proc/acpi/battery/BAT0/info", "r"))) {
215 fread(buf,512,1,fd);
216 fclose(fd);
217 if(ptr = strstr(buf,"last full capacity:")) {
218 ptr += 25;
219 sscanf(ptr,"%d",&k->currcap);
222 free(buf);
224 k->ac_line_status = 0;
225 k->battery_status = 0;
226 k->battery_percentage = 0;
227 k->remain = 0;
228 k->thermal_temp = 0;
229 k->thermal_state = 0;
232 /* called by timer */
233 static void update() {
234 static light pre_backlight;
235 static Bool in_alarm_mode = False;
237 /* get current battery usage in percent */
238 acpi_getinfos(&cur_acpi_infos);
240 /* alarm mode */
241 if ((cur_acpi_infos.battery_percentage < alarm_level) || (cur_acpi_infos.thermal_temp > alarm_level_temp)) {
242 if (!in_alarm_mode) {
243 in_alarm_mode = True;
244 pre_backlight = backlight;
245 my_system(notif_cmd);
247 if ( (switch_authorized) ||
248 ( (! switch_authorized) && (backlight != pre_backlight) ) ) {
249 switch_light();
250 return;
253 else {
254 if (in_alarm_mode) {
255 in_alarm_mode = False;
256 if (backlight != pre_backlight) {
257 switch_light();
258 return;
263 /* all clear */
264 if (backlight == LIGHTON)
265 dockapp_copyarea(backdrop_on, pixmap, 0, 0, 58, 58, 0, 0);
266 else
267 dockapp_copyarea(backdrop_off, pixmap, 0, 0, 58, 58, 0, 0);
269 /* draw digit */
270 draw_timedigit(cur_acpi_infos);
271 draw_pcdigit(cur_acpi_infos);
272 draw_statusdigit(cur_acpi_infos);
273 draw_pcgraph(cur_acpi_infos);
275 /* show */
276 dockapp_copy2window(pixmap);
280 /* called when mouse button pressed */
281 static void switch_light() {
282 switch (backlight) {
283 case LIGHTOFF:
284 backlight = LIGHTON;
285 dockapp_copyarea(backdrop_on, pixmap, 0, 0, 58, 58, 0, 0);
286 break;
287 case LIGHTON:
288 backlight = LIGHTOFF;
289 dockapp_copyarea(backdrop_off, pixmap, 0, 0, 58, 58, 0, 0);
290 break;
293 /* redraw digit */
294 acpi_getinfos(&cur_acpi_infos);
295 draw_timedigit(cur_acpi_infos);
296 draw_pcdigit(cur_acpi_infos);
297 draw_statusdigit(cur_acpi_infos);
298 draw_pcgraph(cur_acpi_infos);
300 /* show */
301 dockapp_copy2window(pixmap);
305 static void draw_timedigit(AcpiInfos infos) {
306 int y = 0;
307 int time_left, hour_left, min_left;
309 if (backlight == LIGHTON) y = 20;
311 dockapp_copyarea(parts, pixmap, (infos.thermal_temp/10)*10 , y, 10, 20, 5, 7);
312 dockapp_copyarea(parts, pixmap, (infos.thermal_temp % 10)*10 , y, 10, 20, 17, 7);
313 /*hour_left = time_left / 60;
314 min_left = time_left % 60;
315 dockapp_copyarea(parts, pixmap, (hour_left / 10) * 10, y, 10, 20, 5, 7);
316 dockapp_copyarea(parts, pixmap, (hour_left % 10) * 10, y, 10, 20, 17, 7);
317 dockapp_copyarea(parts, pixmap, (min_left / 10) * 10, y, 10, 20, 32, 7);
318 dockapp_copyarea(parts, pixmap, (min_left % 10) * 10, y, 10, 20, 44, 7);
322 static void draw_pcdigit(AcpiInfos infos) {
323 int v100, v10, v1;
324 int xd = 0;
325 int num = infos.battery_percentage;
327 if (num < 0) num = 0;
329 v100 = num / 100;
330 v10 = (num - v100 * 100) / 10;
331 v1 = (num - v100 * 100 - v10 * 10);
333 if (backlight == LIGHTON) xd = 50;
335 /* draw digit */
336 dockapp_copyarea(parts, pixmap, v1 * 5 + xd, 40, 5, 9, 17, 45);
337 if (v10 != 0)
338 dockapp_copyarea(parts, pixmap, v10 * 5 + xd, 40, 5, 9, 11, 45);
339 if (v100 == 1) {
340 dockapp_copyarea(parts, pixmap, 5 + xd, 40, 5, 9, 5, 45);
341 dockapp_copyarea(parts, pixmap, 0 + xd, 40, 5, 9, 11, 45);
346 static void draw_statusdigit(AcpiInfos infos) {
347 int xd = 0;
348 int y = 31;
350 if (backlight == LIGHTON) {
351 y = 40;
352 xd = 50;
355 /* draw digit */
356 if (infos.battery_status == 3) /* charging */
357 dockapp_copyarea(parts, pixmap, 100, y, 4, 9, 41, 45);
359 if (infos.ac_line_status == 1)
360 dockapp_copyarea(parts, pixmap, 0 + xd, 49, 5, 9, 34, 45);
361 else
362 dockapp_copyarea(parts, pixmap, 5 + xd, 49, 5, 9, 48, 45);
366 static void draw_pcgraph(AcpiInfos infos) {
367 int xd = 100;
368 int nb;
369 int num = infos.battery_percentage / 6.25 ;
371 if (num < 0) num = 0;
373 if (backlight == LIGHTON) xd = 102;
375 /* draw digit */
376 for (nb = 0 ; nb < num ; nb++)
377 dockapp_copyarea(parts, pixmap, xd, 0, 2, 9, 6 + nb * 3, 33);
381 static void parse_arguments(int argc, char **argv) {
382 int i;
383 int integer;
384 for (i = 1; i < argc; i++) {
385 if (!strcmp(argv[i], "--help") || !strcmp(argv[i], "-h")) {
386 print_help(argv[0]), exit(0);
387 } else if (!strcmp(argv[i], "--version") || !strcmp(argv[i], "-v")) {
388 printf("%s version %s\n", PACKAGE, VERSION), exit(0);
389 } else if (!strcmp(argv[i], "--display") || !strcmp(argv[i], "-d")) {
390 display_name = argv[i + 1];
391 i++;
392 } else if (!strcmp(argv[i], "--backlight") || !strcmp(argv[i], "-bl")) {
393 backlight = LIGHTON;
394 } else if (!strcmp(argv[i], "--light-color") || !strcmp(argv[i], "-lc")) {
395 light_color = argv[i + 1];
396 i++;
397 } else if (!strcmp(argv[i], "--interval") || !strcmp(argv[i], "-i")) {
398 if (argc == i + 1)
399 fprintf(stderr, "%s: error parsing argument for option %s\n",
400 argv[0], argv[i]), exit(1);
401 if (sscanf(argv[i + 1], "%i", &integer) != 1)
402 fprintf(stderr, "%s: error parsing argument for option %s\n",
403 argv[0], argv[i]), exit(1);
404 if (integer < 1)
405 fprintf(stderr, "%s: argument %s must be >=1\n",
406 argv[0], argv[i]), exit(1);
407 update_interval = integer;
408 i++;
409 } else if (!strcmp(argv[i], "--alarm") || !strcmp(argv[i], "-a")) {
410 if (argc == i + 1)
411 fprintf(stderr, "%s: error parsing argument for option %s\n",
412 argv[0], argv[i]), exit(1);
413 if (sscanf(argv[i + 1], "%i", &integer) != 1)
414 fprintf(stderr, "%s: error parsing argument for option %s\n",
415 argv[0], argv[i]), exit(1);
416 if ( (integer < 0) || (integer > 100) )
417 fprintf(stderr, "%s: argument %s must be >=0 and <=100\n",
418 argv[0], argv[i]), exit(1);
419 alarm_level = integer;
420 i++;
421 } else if (!strcmp(argv[i], "--windowed") || !strcmp(argv[i], "-w")) {
422 dockapp_iswindowed = True;
423 } else if (!strcmp(argv[i], "--broken-wm") || !strcmp(argv[i], "-bw")) {
424 dockapp_isbrokenwm = True;
425 } else if (!strcmp(argv[i], "--notify") || !strcmp(argv[i], "-n")) {
426 notif_cmd = argv[i + 1];
427 i++;
428 } else if (!strcmp(argv[i], "--suspend") || !strcmp(argv[i], "-s")) {
429 suspend_cmd = argv[i + 1];
430 i++;
431 } else if (!strcmp(argv[i], "--standby") || !strcmp(argv[i], "-S")) {
432 standby_cmd = argv[i + 1];
433 i++;
434 } else {
435 fprintf(stderr, "%s: unrecognized option '%s'\n", argv[0], argv[i]);
436 print_help(argv[0]), exit(1);
442 static void print_help(char *prog)
444 printf("Usage : %s [OPTIONS]\n"
445 "%s - Window Maker mails monitor dockapp\n"
446 " -d, --display <string> display to use\n"
447 " -bl, --backlight turn on back-light\n"
448 " -lc, --light-color <string> back-light color(rgb:6E/C6/3B is default)\n"
449 " -i, --interval <number> number of secs between updates (1 is default)\n"
450 " -a, --alarm <number> low battery level when to raise alarm (20 is default)\n"
451 " -h, --help show this help text and exit\n"
452 " -v, --version show program version and exit\n"
453 " -w, --windowed run the application in windowed mode\n"
454 " -bw, --broken-wm activate broken window manager fix\n"
455 " -n, --notify <string> command to launch when alarm is on\n"
456 " -s, --suspend <string> set command for acpi suspend\n"
457 " -S, --standby <string> set command for acpi standby\n",
458 prog, prog);
459 /* OPTIONS SUPP :
460 * ? -f, --file : configuration file
465 static void acpi_getinfos(AcpiInfos *infos) {
466 if (
467 #if defined(linux) || defined(solaris)
468 (acpi_read(infos))
469 #else
470 # ifdef freebsd
471 (acpi_read(&temp_info))
472 # endif
473 #endif
475 fprintf(stderr, "Cannot read ACPI information: %i\n");
476 exit(1);
481 int acpi_exists() {
482 if (access(ACPIDEV, R_OK))
483 return 0;
484 else
485 return 1;
489 static int my_system (char *cmd) {
490 int pid;
491 extern char **environ;
493 if (cmd == 0) return 1;
494 pid = fork ();
495 if (pid == -1) return -1;
496 if (pid == 0) {
497 pid = fork ();
498 if (pid == 0) {
499 char *argv[4];
500 argv[0] = "sh";
501 argv[1] = "-c";
502 argv[2] = cmd;
503 argv[3] = 0;
504 execve ("/bin/sh", argv, environ);
505 exit (0);
507 exit (0);
509 return 0;
513 #ifdef linux
514 int acpi_read(AcpiInfos *i) {
515 FILE *fd;
516 int retcode = 0;
517 int capacity,remain;
518 char *buf;
519 char *ptr;
520 char stat;
522 buf=(char *)malloc(sizeof(char)*512);
525 if ((fd = fopen("/proc/acpi/thermal_zone/THRM/state", "r"))) {
526 bzero(buf, 512);
527 fscanf(fd, "state: %s", buf);
528 fclose(fd);
529 if (strstr(buf, "ok")) i->thermal_state=1;
532 /* get acpi thermal cpu info */
533 if ((fd = fopen("/proc/acpi/thermal_zone/THRM/temperature", "r"))) {
534 fscanf(fd, "temperature: %d", &i->thermal_temp);
535 fclose(fd);
537 if ((fd = fopen("/proc/acpi/ac_adapter/AC/state", "r"))) {
538 bzero(buf, 512);
539 fscanf(fd, "state: %s", buf);
540 fclose(fd);
541 if(strstr(buf, "on-line") != NULL) i->ac_line_status=1;
542 if(strstr(buf, "off-line") != NULL) i->ac_line_status=0;
544 if ((fd = fopen("/proc/acpi/battery/BAT0/state", "r"))) {
545 bzero(buf, 512);
546 fread(buf,512,1,fd);
547 fclose(fd);
548 if(( ptr = strstr(buf,"charging state:"))) {
549 stat = *(ptr + 25);
550 switch (stat)
552 case 'd':
553 i->battery_status=1;
554 break;
555 case 'c':
556 i->battery_status=3;
557 break;
558 case 'u':
559 i->battery_status=0;
560 break;
563 if ((ptr = strstr (buf, "remaining capacity:"))) {
564 ptr += 25;
565 sscanf(ptr,"%d",&i->remain);
568 free(buf);
569 i->battery_percentage = ((cur_acpi_infos.remain*100)/cur_acpi_infos.currcap);
570 return retcode;
572 #endif