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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
32 #include "backlight_on.xpm"
33 #include "backlight_off.xpm"
40 #define FREE(data) {if (data) free (data); data = NULL;}
44 #define WINDOWED_BG ". c #AEAAAE"
45 #define MAX_HISTORY 16
46 #define CPUNUM_NONE -1
49 # define ACPIDEV "/proc/acpi/info"
52 typedef struct AcpiInfos
{
53 const char driver_version
[10];
56 int battery_percentage
;
63 typedef enum { LIGHTOFF
, LIGHTON
} light
;
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
;
85 # ifndef ACPI_32_BIT_SUPPORT
86 # define ACPI_32_BIT_SUPPORT 0x0002
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
);
104 int acpi_read(AcpiInfos
*i
);
105 int init_stats(AcpiInfos
*k
);
111 int main(int argc
, char **argv
) {
113 XpmColorSymbol colors
[2] = { {"Back0", NULL
, 0}, {"Back1", NULL
, 0} };
117 sa
.sa_handler
= SIG_IGN
;
119 sa
.sa_flags
= SA_NOCLDWAIT
;
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()) {
132 fprintf(stderr
, "No ACPI support in kernel\n");
134 fprintf(stderr
, "Unable to access ACPI info\n");
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
);
146 colors
[0].pixel
= dockapp_getcolor(light_color
);
147 colors
[1].pixel
= dockapp_blendedcolor(light_color
, -24, -24, -24, 1.0);
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");
159 if (!dockapp_xpm2pixmap(backlight_off_xpm
, &backdrop_off
, NULL
, NULL
, 0)) {
160 fprintf(stderr
, "Error initializing background image.\n");
163 if (!dockapp_xpm2pixmap(parts_xpm
, &parts
, NULL
, colors
, ncolor
)) {
164 fprintf(stderr
, "Error initializing parts image.\n");
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);
179 dockapp_copyarea(backdrop_off
, pixmap
, 0, 0, SIZE
, SIZE
, 0, 0);
181 dockapp_set_background(pixmap
);
186 if (dockapp_nextevent_or_timeout(&event
, update_interval
* 1000)) {
188 switch (event
.type
) {
190 switch (event
.xbutton
.button
) {
191 case 1: switch_light(); break;
205 int init_stats(AcpiInfos
*k
) {
210 buf
=(char *)malloc(sizeof(char)*512);
212 /* get info about full battery charge */
214 if ((fd
= fopen("/proc/acpi/battery/BAT0/info", "r"))) {
217 if(ptr
= strstr(buf
,"last full capacity:")) {
219 sscanf(ptr
,"%d",&k
->currcap
);
224 k
->ac_line_status
= 0;
225 k
->battery_status
= 0;
226 k
->battery_percentage
= 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
);
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
) ) ) {
255 in_alarm_mode
= False
;
256 if (backlight
!= pre_backlight
) {
264 if (backlight
== LIGHTON
)
265 dockapp_copyarea(backdrop_on
, pixmap
, 0, 0, 58, 58, 0, 0);
267 dockapp_copyarea(backdrop_off
, pixmap
, 0, 0, 58, 58, 0, 0);
270 draw_timedigit(cur_acpi_infos
);
271 draw_pcdigit(cur_acpi_infos
);
272 draw_statusdigit(cur_acpi_infos
);
273 draw_pcgraph(cur_acpi_infos
);
276 dockapp_copy2window(pixmap
);
280 /* called when mouse button pressed */
281 static void switch_light() {
285 dockapp_copyarea(backdrop_on
, pixmap
, 0, 0, 58, 58, 0, 0);
288 backlight
= LIGHTOFF
;
289 dockapp_copyarea(backdrop_off
, pixmap
, 0, 0, 58, 58, 0, 0);
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
);
301 dockapp_copy2window(pixmap
);
305 static void draw_timedigit(AcpiInfos infos
) {
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
) {
325 int num
= infos
.battery_percentage
;
327 if (num
< 0) num
= 0;
330 v10
= (num
- v100
* 100) / 10;
331 v1
= (num
- v100
* 100 - v10
* 10);
333 if (backlight
== LIGHTON
) xd
= 50;
336 dockapp_copyarea(parts
, pixmap
, v1
* 5 + xd
, 40, 5, 9, 17, 45);
338 dockapp_copyarea(parts
, pixmap
, v10
* 5 + xd
, 40, 5, 9, 11, 45);
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
) {
350 if (backlight
== LIGHTON
) {
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);
362 dockapp_copyarea(parts
, pixmap
, 5 + xd
, 49, 5, 9, 48, 45);
366 static void draw_pcgraph(AcpiInfos infos
) {
369 int num
= infos
.battery_percentage
/ 6.25 ;
371 if (num
< 0) num
= 0;
373 if (backlight
== LIGHTON
) xd
= 102;
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
) {
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];
392 } else if (!strcmp(argv
[i
], "--backlight") || !strcmp(argv
[i
], "-bl")) {
394 } else if (!strcmp(argv
[i
], "--light-color") || !strcmp(argv
[i
], "-lc")) {
395 light_color
= argv
[i
+ 1];
397 } else if (!strcmp(argv
[i
], "--interval") || !strcmp(argv
[i
], "-i")) {
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);
405 fprintf(stderr
, "%s: argument %s must be >=1\n",
406 argv
[0], argv
[i
]), exit(1);
407 update_interval
= integer
;
409 } else if (!strcmp(argv
[i
], "--alarm") || !strcmp(argv
[i
], "-a")) {
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
;
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];
428 } else if (!strcmp(argv
[i
], "--suspend") || !strcmp(argv
[i
], "-s")) {
429 suspend_cmd
= argv
[i
+ 1];
431 } else if (!strcmp(argv
[i
], "--standby") || !strcmp(argv
[i
], "-S")) {
432 standby_cmd
= argv
[i
+ 1];
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",
460 * ? -f, --file : configuration file
465 static void acpi_getinfos(AcpiInfos
*infos
) {
467 #if defined(linux) || defined(solaris)
471 (acpi_read(&temp_info
))
475 fprintf(stderr
, "Cannot read ACPI information: %i\n");
482 if (access(ACPIDEV
, R_OK
))
489 static int my_system (char *cmd
) {
491 extern char **environ
;
493 if (cmd
== 0) return 1;
495 if (pid
== -1) return -1;
504 execve ("/bin/sh", argv
, environ
);
514 int acpi_read(AcpiInfos
*i
) {
522 buf
=(char *)malloc(sizeof(char)*512);
525 if ((fd = fopen("/proc/acpi/thermal_zone/THRM/state", "r"))) {
527 fscanf(fd, "state: %s", buf);
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
);
537 if ((fd
= fopen("/proc/acpi/ac_adapter/AC/state", "r"))) {
539 fscanf(fd
, "state: %s", buf
);
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"))) {
548 if(( ptr
= strstr(buf
,"charging state:"))) {
563 if ((ptr
= strstr (buf
, "remaining capacity:"))) {
565 sscanf(ptr
,"%d",&i
->remain
);
569 i
->battery_percentage
= ((cur_acpi_infos
.remain
*100)/cur_acpi_infos
.currcap
);