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