9 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
10 #include <sys/types.h>
11 #include <sys/sysctl.h>
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.
20 void print_battery_info(int number, const char *path, const char *format, bool last_full_capacity) {
25 char percentagebuf[16];
26 char remainingbuf[256];
27 char emptytimebuf[256];
28 const char *walk, *last;
32 charging_status_t status = CS_DISCHARGING;
34 memset(statusbuf, '\0', sizeof(statusbuf));
35 memset(percentagebuf, '\0', sizeof(percentagebuf));
36 memset(remainingbuf, '\0', sizeof(remainingbuf));
37 memset(emptytimebuf, '\0', sizeof(emptytimebuf));
39 if (output_format == O_I3BAR)
40 printf("{\"name\":\"battery\", \"instance\": \"%s\", \"full_text\":\"", path);
43 static char batpath[512];
44 sprintf(batpath, path, number);
45 if (!slurp(batpath, buf, sizeof(buf))) {
50 for (walk = buf, last = buf; (walk-buf) < 1024; walk++) {
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"))
68 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Full"))
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"))
77 if (!BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL_DESIGN") &&
78 !BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL_DESIGN"))
82 full_design = atoi(walk+1);
86 if ((full_design == 1) || (remaining == -1))
89 (void)snprintf(statusbuf, sizeof(statusbuf), "%s",
90 (status == CS_CHARGING ? "CHR" :
91 (status == CS_DISCHARGING ? "BAT" : "FULL")));
93 (void)snprintf(percentagebuf, sizeof(percentagebuf), "%.02f%%",
94 (((float)remaining / (float)full_design) * 100));
96 if (present_rate > 0) {
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;
105 seconds_remaining = (int)(remaining_time * 3600.0);
107 hours = seconds_remaining / 3600;
108 seconds = seconds_remaining - (hours * 3600);
109 minutes = seconds / 60;
110 seconds -= (minutes * 60);
112 (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02d:%02d:%02d",
113 max(hours, 0), max(minutes, 0), max(seconds, 0));
115 empty_time = time(NULL);
116 empty_time += seconds_remaining;
117 empty_tm = localtime(&empty_time);
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));
122 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
125 size_t sysctl_size = sizeof(sysctl_rslt);
127 if (sysctlbyname(BATT_LIFE, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
128 printf("No battery");
132 present_rate = sysctl_rslt;
133 if (sysctlbyname(BATT_TIME, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
134 printf("No battery");
138 remaining = sysctl_rslt;
139 if (sysctlbyname(BATT_STATE, &sysctl_rslt, &sysctl_size, NULL,0) != 0) {
140 printf("No battery");
145 if (state == 0 && present_rate == 100)
147 else if (state == 0 && present_rate < 100)
148 status = CS_CHARGING;
150 status = CS_DISCHARGING;
152 full_design = sysctl_rslt;
154 (void)snprintf(statusbuf, sizeof(statusbuf), "%s",
155 (status == CS_CHARGING ? "CHR" :
156 (status == CS_DISCHARGING ? "BAT" : "FULL")));
158 (void)snprintf(percentagebuf, sizeof(percentagebuf), "%02d%%",
164 hours = minutes / 60;
165 minutes -= (hours * 60);
166 (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02dh%02d",
167 max(hours, 0), max(minutes, 0));
171 for (walk = format; *walk != '\0'; walk++) {
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");
192 if (output_format == O_I3BAR)