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