]> git.sur5r.net Git - i3/i3status/blob - src/print_battery_info.c
c4b15b1864abba806c09f013855bcffc32e87a1d
[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, bool last_full_capacity) {
20         char buf[1024];
21         char statusbuf[16];
22         char remainingbuf[256];
23         const char *walk, *last;
24         int full_design = -1,
25             remaining = -1,
26             present_rate = -1;
27         charging_status_t status = CS_DISCHARGING;
28
29         memset(statusbuf, '\0', sizeof(statusbuf));
30         memset(remainingbuf, '\0', sizeof(remainingbuf));
31
32 #if defined(LINUX)
33         static char batpath[512];
34         sprintf(batpath, "/sys/class/power_supply/BAT%d/uevent", number);
35         if (!slurp(batpath, buf, sizeof(buf))) {
36                 printf("No battery");
37                 return;
38         }
39
40         for (walk = buf, last = buf; (walk-buf) < 1024; walk++) {
41                 if (*walk == '\n') {
42                         last = walk+1;
43                         continue;
44                 }
45
46                 if (*walk != '=')
47                         continue;
48
49                 if (BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_NOW") ||
50                     BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_NOW"))
51                         remaining = atoi(walk+1);
52                 else if (BEGINS_WITH(last, "POWER_SUPPLY_CURRENT_NOW"))
53                         present_rate = atoi(walk+1);
54                 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Charging"))
55                         status = CS_CHARGING;
56                 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Full"))
57                         status = CS_FULL;
58                 else {
59                         /* The only thing left is the full capacity */
60                         if (last_full_capacity) {
61                                 if (!BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL") &&
62                                     !BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL"))
63                                         continue;
64                         } else {
65                                 if (!BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL_DESIGN") &&
66                                     !BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL_DESIGN"))
67                                         continue;
68                         }
69
70                         full_design = atoi(walk+1);
71                 }
72         }
73
74         if ((full_design == 1) || (remaining == -1))
75                 return;
76
77         (void)snprintf(statusbuf, sizeof(statusbuf), "%s",
78                         (status == CS_CHARGING ? "CHR" :
79                          (status == CS_DISCHARGING ? "BAT" : "FULL")));
80
81         if (present_rate > 0) {
82                 float remaining_time;
83                 int seconds, hours, minutes;
84                 if (status == CS_CHARGING)
85                         remaining_time = ((float)full_design - (float)remaining) / (float)present_rate;
86                 else if (status == CS_DISCHARGING)
87                         remaining_time = ((float)remaining / (float)present_rate);
88                 else remaining_time = 0;
89
90                 seconds = (int)(remaining_time * 3600.0);
91                 hours = seconds / 3600;
92                 seconds -= (hours * 3600);
93                 minutes = seconds / 60;
94                 seconds -= (minutes * 60);
95
96                 (void)snprintf(remainingbuf, sizeof(remainingbuf), "%.02f%% %02d:%02d:%02d",
97                         (((float)remaining / (float)full_design) * 100),
98                         max(hours, 0), max(minutes, 0), max(seconds, 0));
99         } else {
100                 (void)snprintf(remainingbuf, sizeof(remainingbuf), "%.02f%%", (((float)remaining / (float)full_design) * 100));
101         }
102 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
103         int state;
104         int sysctl_rslt;
105         size_t sysctl_size = sizeof(sysctl_rslt);
106
107         if (sysctlbyname(BATT_LIFE, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
108                 printf("No battery");
109                 return;
110         }
111
112         present_rate = sysctl_rslt;
113         if (sysctlbyname(BATT_TIME, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
114                 printf("No battery");
115                 return;
116         }
117
118         remaining = sysctl_rslt;
119         if (sysctlbyname(BATT_STATE, &sysctl_rslt, &sysctl_size, NULL,0) != 0) {
120                 printf("No battery");
121                 return;
122         }
123
124         state = sysctl_rslt;
125         if (state == 0 && present_rate == 100)
126                 status = CS_FULL;
127         else if (state == 0 && present_rate < 100)
128                 status = CS_CHARGING;
129         else
130                 status = CS_DISCHARGING;
131
132         full_design = sysctl_rslt;
133
134         (void)snprintf(statusbuf, sizeof(statusbuf), "%s",
135                         (status == CS_CHARGING ? "CHR" :
136                          (status == CS_DISCHARGING ? "BAT" : "FULL")));
137
138         if (state == 1) {
139                 int hours, minutes;
140                 minutes = remaining;
141                 hours = minutes / 60;
142                 minutes -= (hours * 60);
143                 (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02d%% %02dh%02d",
144                                present_rate,
145                                max(hours, 0), max(minutes, 0));
146         } else {
147                 (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02d%%",
148                                present_rate);
149         }
150 #endif
151
152         for (walk = format; *walk != '\0'; walk++) {
153                 if (*walk != '%') {
154                         putchar(*walk);
155                         continue;
156                 }
157
158                 if (strncmp(walk+1, "status", strlen("status")) == 0) {
159                         printf("%s", statusbuf);
160                         walk += strlen("status");
161                 } else if (strncmp(walk+1, "remaining", strlen("remaining")) == 0) {
162                         printf("%s", remainingbuf);
163                         walk += strlen("remaining");
164                 }
165         }
166 }