]> git.sur5r.net Git - i3/i3status/blob - src/print_battery_info.c
Breaks configfiles! Major refactoring of i3status, see below
[i3/i3status] / src / print_battery_info.c
1 // vim:ts=8:expandtab
2 #include <string.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5
6 #include "i3status.h"
7
8 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
9 #include <sys/types.h>
10 #include <sys/sysctl.h>
11 #endif
12
13 /*
14  * Get battery information from /sys. Note that it uses the design capacity to
15  * calculate the percentage, not the last full capacity, so you can see how
16  * worn off your battery is.
17  *
18  */
19 void print_battery_info(int number, const char *format) {
20         char buf[1024];
21         char *walk, *last;
22         int full_design = -1,
23             remaining = -1,
24             present_rate = -1;
25         charging_status_t status = CS_DISCHARGING;
26
27 #if defined(LINUX)
28         static char batpath[512];
29         sprintf(batpath, "/sys/class/power_supply/BAT%d/uevent", number);
30         if (!slurp(batpath, buf, sizeof(buf))) {
31                 printf("No battery");
32                 return;
33         }
34
35         for (walk = buf, last = buf; (walk-buf) < 1024; walk++) {
36                 if (*walk == '\n') {
37                         last = walk+1;
38                         continue;
39                 }
40
41                 if (*walk != '=')
42                         continue;
43
44                 if (BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_NOW") ||
45                     BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_NOW"))
46                         remaining = atoi(walk+1);
47                 else if (BEGINS_WITH(last, "POWER_SUPPLY_CURRENT_NOW"))
48                         present_rate = atoi(walk+1);
49                 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Charging"))
50                         status = CS_CHARGING;
51                 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Full"))
52                         status = CS_FULL;
53                 else {
54                         /* The only thing left is the full capacity */
55 #if 0
56                         if (bat->use_last_full) {
57                                 if (!BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL") &&
58                                     !BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL"))
59                                         continue;
60                         } else {
61 #endif
62                                 if (!BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL_DESIGN") &&
63                                     !BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL_DESIGN"))
64                                         continue;
65                         //}
66
67                         full_design = atoi(walk+1);
68                 }
69         }
70
71         if ((full_design == 1) || (remaining == -1))
72                 return;
73
74         if (present_rate > 0) {
75                 float remaining_time;
76                 int seconds, hours, minutes;
77                 if (status == CS_CHARGING)
78                         remaining_time = ((float)full_design - (float)remaining) / (float)present_rate;
79                 else if (status == CS_DISCHARGING)
80                         remaining_time = ((float)remaining / (float)present_rate);
81                 else remaining_time = 0;
82
83                 seconds = (int)(remaining_time * 3600.0);
84                 hours = seconds / 3600;
85                 seconds -= (hours * 3600);
86                 minutes = seconds / 60;
87                 seconds -= (minutes * 60);
88
89                 (void)printf("%s %.02f%% %02d:%02d:%02d",
90                         (status == CS_CHARGING ? "CHR" :
91                          (status == CS_DISCHARGING ? "BAT" : "FULL")),
92                         (((float)remaining / (float)full_design) * 100),
93                         max(hours, 0), max(minutes, 0), max(seconds, 0));
94         } else {
95                 (void)printf("%s %.02f%%",
96                         (status == CS_CHARGING ? "CHR" :
97                          (status == CS_DISCHARGING ? "BAT" : "FULL")),
98                         (((float)remaining / (float)full_design) * 100));
99         }
100 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
101         int state;
102         int sysctl_rslt;
103         size_t sysctl_size = sizeof(sysctl_rslt);
104
105         if (sysctlbyname(BATT_LIFE, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
106                 printf("No battery");
107                 return;
108         }
109
110         present_rate = sysctl_rslt;
111         if (sysctlbyname(BATT_TIME, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
112                 printf("No battery");
113                 return;
114         }
115
116         remaining = sysctl_rslt;
117         if (sysctlbyname(BATT_STATE, &sysctl_rslt, &sysctl_size, NULL,0) != 0) {
118                 printf("No battery");
119                 return;
120         }
121
122         state = sysctl_rslt;
123         if (state == 0 && present_rate == 100)
124                 status = CS_FULL;
125         else if (state == 0 && present_rate < 100)
126                 status = CS_CHARGING;
127         else
128                 status = CS_DISCHARGING;
129
130         full_design = sysctl_rslt;
131
132         if (state == 1) {
133                 int hours, minutes;
134                 minutes = remaining;
135                 hours = minutes / 60;
136                 minutes -= (hours * 60);
137                 (void)printf("%s %02d%% %02dh%02d",
138                                (status == CS_CHARGING ? "CHR" :
139                                 (status == CS_DISCHARGING ? "BAT" : "FULL")),
140                                present_rate,
141                                max(hours, 0), max(minutes, 0));
142         } else {
143                 (void)printf("%s %02d%%",
144                                (status == CS_CHARGING ? "CHR" :
145                                 (status == CS_DISCHARGING ? "BAT" : "FULL")),
146                                present_rate);
147         }
148 #endif
149 }