]> git.sur5r.net Git - i3/i3status/blob - src/print_battery_info.c
6ee89896cfed8c0e226e7a9bb77dcc2e19bc354d
[i3/i3status] / src / print_battery_info.c
1 // vim:ts=8:expandtab
2 #include <time.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6
7 #include "i3status.h"
8
9 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
10 #include <sys/types.h>
11 #include <sys/sysctl.h>
12 #endif
13
14 /*
15  * Get battery information from /sys. Note that it uses the design capacity to
16  * calculate the percentage, not the last full capacity, so you can see how
17  * worn off your battery is.
18  *
19  */
20 void print_battery_info(int number, const char *path, const char *format, bool last_full_capacity) {
21         time_t empty_time;
22         struct tm *empty_tm;
23         char buf[1024];
24         char statusbuf[16];
25         char percentagebuf[16];
26         char remainingbuf[256];
27         char emptytimebuf[256];
28         const char *walk, *last;
29         int full_design = -1,
30             remaining = -1,
31             present_rate = -1;
32         charging_status_t status = CS_DISCHARGING;
33
34         memset(statusbuf, '\0', sizeof(statusbuf));
35         memset(percentagebuf, '\0', sizeof(percentagebuf));
36         memset(remainingbuf, '\0', sizeof(remainingbuf));
37         memset(emptytimebuf, '\0', sizeof(emptytimebuf));
38
39 #if defined(LINUX)
40         static char batpath[512];
41         sprintf(batpath, path, number);
42         if (!slurp(batpath, buf, sizeof(buf))) {
43                 printf("No battery");
44                 return;
45         }
46
47         for (walk = buf, last = buf; (walk-buf) < 1024; walk++) {
48                 if (*walk == '\n') {
49                         last = walk+1;
50                         continue;
51                 }
52
53                 if (*walk != '=')
54                         continue;
55
56                 if (BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_NOW") ||
57                     BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_NOW"))
58                         remaining = atoi(walk+1);
59                 else if (BEGINS_WITH(last, "POWER_SUPPLY_CURRENT_NOW"))
60                         present_rate = atoi(walk+1);
61                 else if (BEGINS_WITH(last, "POWER_SUPPLY_POWER_NOW"))
62                         present_rate = atoi(walk+1);
63                 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Charging"))
64                         status = CS_CHARGING;
65                 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Full"))
66                         status = CS_FULL;
67                 else {
68                         /* The only thing left is the full capacity */
69                         if (last_full_capacity) {
70                                 if (!BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL") &&
71                                     !BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL"))
72                                         continue;
73                         } else {
74                                 if (!BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL_DESIGN") &&
75                                     !BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL_DESIGN"))
76                                         continue;
77                         }
78
79                         full_design = atoi(walk+1);
80                 }
81         }
82
83         if ((full_design == 1) || (remaining == -1))
84                 return;
85
86         (void)snprintf(statusbuf, sizeof(statusbuf), "%s",
87                         (status == CS_CHARGING ? "CHR" :
88                          (status == CS_DISCHARGING ? "BAT" : "FULL")));
89
90         (void)snprintf(percentagebuf, sizeof(percentagebuf), "%.02f%%",
91                        (((float)remaining / (float)full_design) * 100));
92
93         if (present_rate > 0) {
94                 float remaining_time;
95                 int seconds, hours, minutes, seconds_remaining;
96                 if (status == CS_CHARGING)
97                         remaining_time = ((float)full_design - (float)remaining) / (float)present_rate;
98                 else if (status == CS_DISCHARGING)
99                         remaining_time = ((float)remaining / (float)present_rate);
100                 else remaining_time = 0;
101
102                 seconds_remaining = (int)(remaining_time * 3600.0);
103
104                 hours = seconds_remaining / 3600;
105                 seconds = seconds_remaining - (hours * 3600);
106                 minutes = seconds / 60;
107                 seconds -= (minutes * 60);
108
109                 (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02d:%02d:%02d",
110                         max(hours, 0), max(minutes, 0), max(seconds, 0));
111
112                 empty_time = time(NULL);
113                 empty_time += seconds_remaining;
114                 empty_tm = localtime(&empty_time);
115
116                 (void)snprintf(emptytimebuf, sizeof(emptytimebuf), "%02d:%02d:%02d",
117                         max(empty_tm->tm_hour, 0), max(empty_tm->tm_min, 0), max(empty_tm->tm_sec, 0));
118         }
119 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
120         int state;
121         int sysctl_rslt;
122         size_t sysctl_size = sizeof(sysctl_rslt);
123
124         if (sysctlbyname(BATT_LIFE, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
125                 printf("No battery");
126                 return;
127         }
128
129         present_rate = sysctl_rslt;
130         if (sysctlbyname(BATT_TIME, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
131                 printf("No battery");
132                 return;
133         }
134
135         remaining = sysctl_rslt;
136         if (sysctlbyname(BATT_STATE, &sysctl_rslt, &sysctl_size, NULL,0) != 0) {
137                 printf("No battery");
138                 return;
139         }
140
141         state = sysctl_rslt;
142         if (state == 0 && present_rate == 100)
143                 status = CS_FULL;
144         else if (state == 0 && present_rate < 100)
145                 status = CS_CHARGING;
146         else
147                 status = CS_DISCHARGING;
148
149         full_design = sysctl_rslt;
150
151         (void)snprintf(statusbuf, sizeof(statusbuf), "%s",
152                         (status == CS_CHARGING ? "CHR" :
153                          (status == CS_DISCHARGING ? "BAT" : "FULL")));
154
155         (void)snprintf(percentagebuf, sizeof(percentagebuf), "%02d%%",
156                        present_rate);
157
158         if (state == 1) {
159                 int hours, minutes;
160                 minutes = remaining;
161                 hours = minutes / 60;
162                 minutes -= (hours * 60);
163                 (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02dh%02d",
164                                max(hours, 0), max(minutes, 0));
165         }
166 #endif
167
168         for (walk = format; *walk != '\0'; walk++) {
169                 if (*walk != '%') {
170                         putchar(*walk);
171                         continue;
172                 }
173
174                 if (strncmp(walk+1, "status", strlen("status")) == 0) {
175                         printf("%s", statusbuf);
176                         walk += strlen("status");
177                 } else if (strncmp(walk+1, "percentage", strlen("percentage")) == 0) {
178                         printf("%s", percentagebuf);
179                         walk += strlen("percentage");
180                 } else if (strncmp(walk+1, "remaining", strlen("remaining")) == 0) {
181                         printf("%s", remainingbuf);
182                         walk += strlen("remaining");
183                 } else if (strncmp(walk+1, "emptytime", strlen("emptytime")) == 0) {
184                         printf("%s", emptytimebuf);
185                         walk += strlen("emptytime");
186                 }
187         }
188 }