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 remainingbuf[256];
23 const char *walk, *last;
27 charging_status_t status = CS_DISCHARGING;
29 memset(statusbuf, '\0', sizeof(statusbuf));
30 memset(remainingbuf, '\0', sizeof(remainingbuf));
33 static char batpath[512];
34 sprintf(batpath, "/sys/class/power_supply/BAT%d/uevent", number);
35 if (!slurp(batpath, buf, sizeof(buf))) {
40 for (walk = buf, last = buf; (walk-buf) < 1024; walk++) {
49 if (BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_NOW") ||
50 BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_NOW"))
51 remaining = atoi(walk+1);
52 else if (BEGINS_WITH(last, "POWER_SUPPLY_CURRENT_NOW"))
53 present_rate = atoi(walk+1);
54 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Charging"))
56 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Full"))
59 /* The only thing left is the full capacity */
60 if (last_full_capacity) {
61 if (!BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL") &&
62 !BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL"))
65 if (!BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL_DESIGN") &&
66 !BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL_DESIGN"))
70 full_design = atoi(walk+1);
74 if ((full_design == 1) || (remaining == -1))
77 (void)snprintf(statusbuf, sizeof(statusbuf), "%s",
78 (status == CS_CHARGING ? "CHR" :
79 (status == CS_DISCHARGING ? "BAT" : "FULL")));
81 if (present_rate > 0) {
83 int seconds, hours, minutes;
84 if (status == CS_CHARGING)
85 remaining_time = ((float)full_design - (float)remaining) / (float)present_rate;
86 else if (status == CS_DISCHARGING)
87 remaining_time = ((float)remaining / (float)present_rate);
88 else remaining_time = 0;
90 seconds = (int)(remaining_time * 3600.0);
91 hours = seconds / 3600;
92 seconds -= (hours * 3600);
93 minutes = seconds / 60;
94 seconds -= (minutes * 60);
96 (void)snprintf(remainingbuf, sizeof(remainingbuf), "%.02f%% %02d:%02d:%02d",
97 (((float)remaining / (float)full_design) * 100),
98 max(hours, 0), max(minutes, 0), max(seconds, 0));
100 (void)snprintf(remainingbuf, sizeof(remainingbuf), "%.02f%%", (((float)remaining / (float)full_design) * 100));
102 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
105 size_t sysctl_size = sizeof(sysctl_rslt);
107 if (sysctlbyname(BATT_LIFE, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
108 printf("No battery");
112 present_rate = sysctl_rslt;
113 if (sysctlbyname(BATT_TIME, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
114 printf("No battery");
118 remaining = sysctl_rslt;
119 if (sysctlbyname(BATT_STATE, &sysctl_rslt, &sysctl_size, NULL,0) != 0) {
120 printf("No battery");
125 if (state == 0 && present_rate == 100)
127 else if (state == 0 && present_rate < 100)
128 status = CS_CHARGING;
130 status = CS_DISCHARGING;
132 full_design = sysctl_rslt;
134 (void)snprintf(statusbuf, sizeof(statusbuf), "%s",
135 (status == CS_CHARGING ? "CHR" :
136 (status == CS_DISCHARGING ? "BAT" : "FULL")));
141 hours = minutes / 60;
142 minutes -= (hours * 60);
143 (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02d%% %02dh%02d",
145 max(hours, 0), max(minutes, 0));
147 (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02d%%",
152 for (walk = format; *walk != '\0'; walk++) {
158 if (strncmp(walk+1, "status", strlen("status")) == 0) {
159 printf("%s", statusbuf);
160 walk += strlen("status");
161 } else if (strncmp(walk+1, "remaining", strlen("remaining")) == 0) {
162 printf("%s", remainingbuf);
163 walk += strlen("remaining");