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