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 *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));
40 static char batpath[512];
41 sprintf(batpath, "/sys/class/power_supply/BAT%d/uevent", number);
42 if (!slurp(batpath, buf, sizeof(buf))) {
47 for (walk = buf, last = buf; (walk-buf) < 1024; walk++) {
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"))
63 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Full"))
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"))
72 if (!BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL_DESIGN") &&
73 !BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL_DESIGN"))
77 full_design = atoi(walk+1);
81 if ((full_design == 1) || (remaining == -1))
84 (void)snprintf(statusbuf, sizeof(statusbuf), "%s",
85 (status == CS_CHARGING ? "CHR" :
86 (status == CS_DISCHARGING ? "BAT" : "FULL")));
88 (void)snprintf(percentagebuf, sizeof(percentagebuf), "%.02f%%",
89 (((float)remaining / (float)full_design) * 100));
91 if (present_rate > 0) {
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;
100 seconds_remaining = (int)(remaining_time * 3600.0);
102 hours = seconds_remaining / 3600;
103 seconds = seconds_remaining - (hours * 3600);
104 minutes = seconds / 60;
105 seconds -= (minutes * 60);
107 (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02d:%02d:%02d",
108 max(hours, 0), max(minutes, 0), max(seconds, 0));
110 empty_time = time(NULL);
111 empty_time += seconds_remaining;
112 empty_tm = localtime(&empty_time);
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));
117 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
120 size_t sysctl_size = sizeof(sysctl_rslt);
122 if (sysctlbyname(BATT_LIFE, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
123 printf("No battery");
127 present_rate = sysctl_rslt;
128 if (sysctlbyname(BATT_TIME, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
129 printf("No battery");
133 remaining = sysctl_rslt;
134 if (sysctlbyname(BATT_STATE, &sysctl_rslt, &sysctl_size, NULL,0) != 0) {
135 printf("No battery");
140 if (state == 0 && present_rate == 100)
142 else if (state == 0 && present_rate < 100)
143 status = CS_CHARGING;
145 status = CS_DISCHARGING;
147 full_design = sysctl_rslt;
149 (void)snprintf(statusbuf, sizeof(statusbuf), "%s",
150 (status == CS_CHARGING ? "CHR" :
151 (status == CS_DISCHARGING ? "BAT" : "FULL")));
153 (void)snprintf(percentagebuf, sizeof(percentagebuf), "%02d%%",
159 hours = minutes / 60;
160 minutes -= (hours * 60);
161 (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02dh%02d",
162 max(hours, 0), max(minutes, 0));
166 for (walk = format; *walk != '\0'; walk++) {
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");