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