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