8 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
10 #include <sys/sysctl.h>
14 * Get battery information from /sys. Note that it uses the design capacity to
15 * calculate the percentage, not the last full capacity, so you can see how
16 * worn off your battery is.
19 void print_battery_info(int number, const char *format, bool last_full_capacity) {
22 char percentagebuf[16];
23 char remainingbuf[256];
24 const char *walk, *last;
28 charging_status_t status = CS_DISCHARGING;
30 memset(statusbuf, '\0', sizeof(statusbuf));
31 memset(percentagebuf, '\0', sizeof(percentagebuf));
32 memset(remainingbuf, '\0', sizeof(remainingbuf));
35 static char batpath[512];
36 sprintf(batpath, "/sys/class/power_supply/BAT%d/uevent", number);
37 if (!slurp(batpath, buf, sizeof(buf))) {
42 for (walk = buf, last = buf; (walk-buf) < 1024; walk++) {
51 if (BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_NOW") ||
52 BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_NOW"))
53 remaining = atoi(walk+1);
54 else if (BEGINS_WITH(last, "POWER_SUPPLY_CURRENT_NOW"))
55 present_rate = atoi(walk+1);
56 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Charging"))
58 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Full"))
61 /* The only thing left is the full capacity */
62 if (last_full_capacity) {
63 if (!BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL") &&
64 !BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL"))
67 if (!BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL_DESIGN") &&
68 !BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL_DESIGN"))
72 full_design = atoi(walk+1);
76 if ((full_design == 1) || (remaining == -1))
79 (void)snprintf(statusbuf, sizeof(statusbuf), "%s",
80 (status == CS_CHARGING ? "CHR" :
81 (status == CS_DISCHARGING ? "BAT" : "FULL")));
83 (void)snprintf(percentagebuf, sizeof(percentagebuf), "%.02f%%",
84 (((float)remaining / (float)full_design) * 100));
86 if (present_rate > 0) {
88 int seconds, hours, minutes;
89 if (status == CS_CHARGING)
90 remaining_time = ((float)full_design - (float)remaining) / (float)present_rate;
91 else if (status == CS_DISCHARGING)
92 remaining_time = ((float)remaining / (float)present_rate);
93 else remaining_time = 0;
95 seconds = (int)(remaining_time * 3600.0);
96 hours = seconds / 3600;
97 seconds -= (hours * 3600);
98 minutes = seconds / 60;
99 seconds -= (minutes * 60);
101 (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02d:%02d:%02d",
102 max(hours, 0), max(minutes, 0), max(seconds, 0));
104 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
107 size_t sysctl_size = sizeof(sysctl_rslt);
109 if (sysctlbyname(BATT_LIFE, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
110 printf("No battery");
114 present_rate = sysctl_rslt;
115 if (sysctlbyname(BATT_TIME, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
116 printf("No battery");
120 remaining = sysctl_rslt;
121 if (sysctlbyname(BATT_STATE, &sysctl_rslt, &sysctl_size, NULL,0) != 0) {
122 printf("No battery");
127 if (state == 0 && present_rate == 100)
129 else if (state == 0 && present_rate < 100)
130 status = CS_CHARGING;
132 status = CS_DISCHARGING;
134 full_design = sysctl_rslt;
136 (void)snprintf(statusbuf, sizeof(statusbuf), "%s",
137 (status == CS_CHARGING ? "CHR" :
138 (status == CS_DISCHARGING ? "BAT" : "FULL")));
140 (void)snprintf(percentagebuf, sizeof(percentagebuf), "%02d%%",
146 hours = minutes / 60;
147 minutes -= (hours * 60);
148 (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02dh%02d",
149 max(hours, 0), max(minutes, 0));
153 for (walk = format; *walk != '\0'; walk++) {
159 if (strncmp(walk+1, "status", strlen("status")) == 0) {
160 printf("%s", statusbuf);
161 walk += strlen("status");
162 } else if (strncmp(walk+1, "percentage", strlen("percentage")) == 0) {
163 printf("%s", percentagebuf);
164 walk += strlen("percentage");
165 } else if (strncmp(walk+1, "remaining", strlen("remaining")) == 0) {
166 printf("%s", remainingbuf);
167 walk += strlen("remaining");