wmbutton: Better Makefile
[dockapps.git] / wmacpi / libapm.c
blob684da7637b1db3ffc98f0d07bdaaa4b37f931777
1 #include <stdio.h>
2 #include "wmacpi.h"
4 #ifdef APM
5 #ifdef PRO
6 extern char *state[];
7 #endif
8 extern APMInfo *apminfo;
9 extern int crit_level;
11 int power_init(void)
13 FILE *apm;
15 if (!(apm = fopen("/proc/apm", "r"))) {
16 fprintf(stderr, "This system does not support APM\n");
17 return 1;
19 fclose(apm);
21 return 0;
24 void acquire_info(void)
26 FILE *apm;
27 char buf[256];
28 char min[10];
30 int ac_line, batt, percent, rtime;
32 #ifdef PRO
33 /* testing */
34 if (!(apm = fopen("apm", "r")))
35 return;
36 #else
37 if (!(apm = fopen("/proc/apm", "r")))
38 return;
39 #endif
41 fgets(buf, 255, apm);
42 sscanf(buf, "%*s %*s %*s %x %x %*s %d%% %d %s",
43 &ac_line, &batt, &percent, &rtime, min);
45 eprint(0, "%02x %02x, %03d%%, %d", ac_line, batt, percent, rtime);
46 apminfo->percentage = percent;
47 apminfo->rtime = rtime;
49 switch (ac_line) {
50 case 0: /* on battery. calculate status. handle charging under AC */
51 switch (batt) {
52 case 0:
53 apminfo->power = HIGH;
54 break;
55 case 1:
56 apminfo->power = LOW;
57 break;
58 case 2:
59 apminfo->power = CRIT;
60 break;
63 /* check user-defined critical alarm */
64 if (apminfo->percentage <= apminfo->crit_level)
65 apminfo->power = CRIT;
67 break;
68 case 1: /* on AC power. Check if battery is being charged */
69 #ifdef RETARDED_APM
70 /* this is incase your battery is "charging" all the fucking time,
71 * even when it's actually done charging */
72 if ((batt == 3) && (percent != 100))
73 #else
74 if (batt == 3)
75 #endif
76 apminfo->power = CHARGING;
77 else
78 apminfo->power = POWER;
79 break;
80 #ifdef STUPID_APM
81 /* treatment for GAY apm bioses that show wrong time
82 * remaining when AC is plugged in */
83 apminfo->rtime = 0;
84 #endif
86 fclose(apm);
87 process_plugin_timer();
89 eprint(1, "current state: %s (%d)", state[apminfo->power], apminfo->power);
91 #endif /* APM */