]> git.sur5r.net Git - i3/i3status/blob - src/print_battery_info.c
Added condition, for red color, when battery-time is low, namely battery is discharging
[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 #include <yajl/yajl_gen.h>
7 #include <yajl/yajl_version.h>
8
9 #include "i3status.h"
10
11 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
12 #include <sys/types.h>
13 #include <sys/sysctl.h>
14 #endif
15
16 #if defined(__OpenBSD__)
17 #include <sys/types.h>
18 #include <sys/ioctl.h>
19 #include <sys/fcntl.h>
20 #include <machine/apmvar.h>
21 #endif
22
23 #define BATT_STATUS_NAME(status) \
24     (status == CS_CHARGING ? "CHR" : \
25         (status == CS_DISCHARGING ? "BAT" : "FULL"))
26 /*
27  * Get battery information from /sys. Note that it uses the design capacity to
28  * calculate the percentage, not the last full capacity, so you can see how
29  * worn off your battery is.
30  *
31  */
32 void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char *path, const char *format, int threshold, bool last_full_capacity) {
33         time_t empty_time;
34         struct tm *empty_tm;
35         char buf[1024];
36         char statusbuf[16];
37         char percentagebuf[16];
38         char remainingbuf[256];
39         char emptytimebuf[256];
40         const char *walk, *last;
41         char *outwalk = buffer;
42         int full_design = -1,
43             remaining = -1,
44             present_rate = -1;
45         charging_status_t status = CS_DISCHARGING;
46
47         memset(statusbuf, '\0', sizeof(statusbuf));
48         memset(percentagebuf, '\0', sizeof(percentagebuf));
49         memset(remainingbuf, '\0', sizeof(remainingbuf));
50         memset(emptytimebuf, '\0', sizeof(emptytimebuf));
51
52         INSTANCE(path);
53
54 #if defined(LINUX)
55         static char batpath[512];
56         sprintf(batpath, path, number);
57         if (!slurp(batpath, buf, sizeof(buf))) {
58                 OUTPUT_FULL_TEXT("No battery");
59                 return;
60         }
61
62         for (walk = buf, last = buf; (walk-buf) < 1024; walk++) {
63                 if (*walk == '\n') {
64                         last = walk+1;
65                         continue;
66                 }
67
68                 if (*walk != '=')
69                         continue;
70
71                 if (BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_NOW") ||
72                     BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_NOW"))
73                         remaining = atoi(walk+1);
74                 else if (BEGINS_WITH(last, "POWER_SUPPLY_CURRENT_NOW"))
75                         present_rate = atoi(walk+1);
76                 else if (BEGINS_WITH(last, "POWER_SUPPLY_POWER_NOW"))
77                         present_rate = atoi(walk+1);
78                 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Charging"))
79                         status = CS_CHARGING;
80                 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Full"))
81                         status = CS_FULL;
82                 else {
83                         /* The only thing left is the full capacity */
84                         if (last_full_capacity) {
85                                 if (!BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL") &&
86                                     !BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL"))
87                                         continue;
88                         } else {
89                                 if (!BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL_DESIGN") &&
90                                     !BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL_DESIGN"))
91                                         continue;
92                         }
93
94                         full_design = atoi(walk+1);
95                 }
96         }
97
98         if ((full_design == -1) || (remaining == -1)) {
99                 OUTPUT_FULL_TEXT("No battery");
100                 return;
101         }
102
103         (void)snprintf(statusbuf, sizeof(statusbuf), "%s", BATT_STATUS_NAME(status));
104
105         (void)snprintf(percentagebuf, sizeof(percentagebuf), "%.02f%%",
106                        (((float)remaining / (float)full_design) * 100));
107
108         if (present_rate > 0) {
109                 float remaining_time;
110                 int seconds, hours, minutes, seconds_remaining;
111                 if (status == CS_CHARGING)
112                         remaining_time = ((float)full_design - (float)remaining) / (float)present_rate;
113                 else if (status == CS_DISCHARGING)
114                         remaining_time = ((float)remaining / (float)present_rate);
115                 else remaining_time = 0;
116
117                 seconds_remaining = (int)(remaining_time * 3600.0);
118
119                 hours = seconds_remaining / 3600;
120                 seconds = seconds_remaining - (hours * 3600);
121                 minutes = seconds / 60;
122                 seconds -= (minutes * 60);
123
124                 if (status == CS_DISCHARGING && threshold > 0 && seconds_remaining < 60 * threshold)
125                         START_COLOR("color_bad");
126
127                 (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02d:%02d:%02d",
128                         max(hours, 0), max(minutes, 0), max(seconds, 0));
129
130                 empty_time = time(NULL);
131                 empty_time += seconds_remaining;
132                 empty_tm = localtime(&empty_time);
133
134                 (void)snprintf(emptytimebuf, sizeof(emptytimebuf), "%02d:%02d:%02d",
135                         max(empty_tm->tm_hour, 0), max(empty_tm->tm_min, 0), max(empty_tm->tm_sec, 0));
136
137                 END_COLOR;
138         }
139 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
140         int state;
141         int sysctl_rslt;
142         size_t sysctl_size = sizeof(sysctl_rslt);
143
144         if (sysctlbyname(BATT_LIFE, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
145                 OUTPUT_FULL_TEXT("No battery");
146                 return;
147         }
148
149         present_rate = sysctl_rslt;
150         if (sysctlbyname(BATT_TIME, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
151                 OUTPUT_FULL_TEXT("No battery");
152                 return;
153         }
154
155         remaining = sysctl_rslt;
156         if (sysctlbyname(BATT_STATE, &sysctl_rslt, &sysctl_size, NULL,0) != 0) {
157                 OUTPUT_FULL_TEXT("No battery");
158                 return;
159         }
160
161         state = sysctl_rslt;
162         if (state == 0 && present_rate == 100)
163                 status = CS_FULL;
164         else if (state == 0 && present_rate < 100)
165                 status = CS_CHARGING;
166         else
167                 status = CS_DISCHARGING;
168
169         full_design = sysctl_rslt;
170
171         (void)snprintf(statusbuf, sizeof(statusbuf), "%s", BATT_STATUS_NAME(status));
172
173         (void)snprintf(percentagebuf, sizeof(percentagebuf), "%02d%%",
174                        present_rate);
175
176         if (state == 1) {
177                 int hours, minutes;
178                 minutes = remaining;
179                 hours = minutes / 60;
180                 minutes -= (hours * 60);
181                 (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02dh%02d",
182                                max(hours, 0), max(minutes, 0));
183         }
184 #elif defined(__OpenBSD__)
185         /*
186          * We're using apm(4) here, which is the interface to acpi(4) on amd64/i386 and
187          * the generic interface on macppc/sparc64/zaurus, instead of using sysctl(3) and
188          * probing acpi(4) devices.
189          */
190         struct apm_power_info apm_info;
191         int apm_fd, ac_status, charging;
192
193         apm_fd = open("/dev/apm", O_RDONLY);
194         if (apm_fd < 0) {
195                 OUTPUT_FULL_TEXT("can't open /dev/apm");
196                 return;
197         }
198         if (ioctl(apm_fd, APM_IOC_GETPOWER, &apm_info) < 0)
199                 OUTPUT_FULL_TEXT("can't read power info");
200
201         close(apm_fd);
202
203         /* Don't bother to go further if there's no battery present. */
204         if ((apm_info.battery_state == APM_BATTERY_ABSENT) ||
205             (apm_info.battery_state == APM_BATT_UNKNOWN)) {
206                 OUTPUT_FULL_TEXT("No battery");
207                 return;
208         }
209
210         switch(apm_info.ac_state) {
211         case APM_AC_OFF:
212                 ac_status = CS_DISCHARGING;
213                 break;
214         case APM_AC_ON:
215                 ac_status = CS_CHARGING;
216                 break;
217         default:
218                 /* If we don't know what's going on, just assume we're discharging. */
219                 ac_status = CS_DISCHARGING;
220                 break;
221         }
222
223         (void)snprintf(statusbuf, sizeof(statusbuf), "%s", BATT_STATUS_NAME(status));
224         (void)snprintf(percentagebuf, sizeof(percentagebuf), "%02d%%", apm_info.battery_life);
225
226         /* Can't give a meaningful value for remaining minutes if we're charging. */
227         if (ac_status == CS_CHARGING)
228                 charging = 1;
229
230         (void)snprintf(remainingbuf, sizeof(remainingbuf), (charging ? "%s" : "%d"),
231                        (charging ? "(CHR)" : apm_info.minutes_left));
232 #endif
233
234         for (walk = format; *walk != '\0'; walk++) {
235                 if (*walk != '%') {
236                         *(outwalk++) = *walk;
237                         continue;
238                 }
239
240                 if (strncmp(walk+1, "status", strlen("status")) == 0) {
241                         outwalk += sprintf(outwalk, "%s", statusbuf);
242                         walk += strlen("status");
243                 } else if (strncmp(walk+1, "percentage", strlen("percentage")) == 0) {
244                         outwalk += sprintf(outwalk, "%s", percentagebuf);
245                         walk += strlen("percentage");
246                 } else if (strncmp(walk+1, "remaining", strlen("remaining")) == 0) {
247                         outwalk += sprintf(outwalk, "%s", remainingbuf);
248                         walk += strlen("remaining");
249                 } else if (strncmp(walk+1, "emptytime", strlen("emptytime")) == 0) {
250                         outwalk += sprintf(outwalk, "%s", emptytimebuf);
251                         walk += strlen("emptytime");
252                 }
253         }
254
255         OUTPUT_FULL_TEXT(buffer);
256 }