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));
40 static char batpath[512];
41 sprintf(batpath, path, 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_POWER_NOW"))
62 present_rate = atoi(walk+1);
63 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Charging"))
65 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Full"))
68 /* The only thing left is the full capacity */
69 if (last_full_capacity) {
70 if (!BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL") &&
71 !BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL"))
74 if (!BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL_DESIGN") &&
75 !BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL_DESIGN"))
79 full_design = atoi(walk+1);
83 if ((full_design == 1) || (remaining == -1))
86 (void)snprintf(statusbuf, sizeof(statusbuf), "%s",
87 (status == CS_CHARGING ? "CHR" :
88 (status == CS_DISCHARGING ? "BAT" : "FULL")));
90 (void)snprintf(percentagebuf, sizeof(percentagebuf), "%.02f%%",
91 (((float)remaining / (float)full_design) * 100));
93 if (present_rate > 0) {
95 int seconds, hours, minutes, seconds_remaining;
96 if (status == CS_CHARGING)
97 remaining_time = ((float)full_design - (float)remaining) / (float)present_rate;
98 else if (status == CS_DISCHARGING)
99 remaining_time = ((float)remaining / (float)present_rate);
100 else remaining_time = 0;
102 seconds_remaining = (int)(remaining_time * 3600.0);
104 hours = seconds_remaining / 3600;
105 seconds = seconds_remaining - (hours * 3600);
106 minutes = seconds / 60;
107 seconds -= (minutes * 60);
109 (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02d:%02d:%02d",
110 max(hours, 0), max(minutes, 0), max(seconds, 0));
112 empty_time = time(NULL);
113 empty_time += seconds_remaining;
114 empty_tm = localtime(&empty_time);
116 (void)snprintf(emptytimebuf, sizeof(emptytimebuf), "%02d:%02d:%02d",
117 max(empty_tm->tm_hour, 0), max(empty_tm->tm_min, 0), max(empty_tm->tm_sec, 0));
119 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
122 size_t sysctl_size = sizeof(sysctl_rslt);
124 if (sysctlbyname(BATT_LIFE, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
125 printf("No battery");
129 present_rate = sysctl_rslt;
130 if (sysctlbyname(BATT_TIME, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
131 printf("No battery");
135 remaining = sysctl_rslt;
136 if (sysctlbyname(BATT_STATE, &sysctl_rslt, &sysctl_size, NULL,0) != 0) {
137 printf("No battery");
142 if (state == 0 && present_rate == 100)
144 else if (state == 0 && present_rate < 100)
145 status = CS_CHARGING;
147 status = CS_DISCHARGING;
149 full_design = sysctl_rslt;
151 (void)snprintf(statusbuf, sizeof(statusbuf), "%s",
152 (status == CS_CHARGING ? "CHR" :
153 (status == CS_DISCHARGING ? "BAT" : "FULL")));
155 (void)snprintf(percentagebuf, sizeof(percentagebuf), "%02d%%",
161 hours = minutes / 60;
162 minutes -= (hours * 60);
163 (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02dh%02d",
164 max(hours, 0), max(minutes, 0));
168 for (walk = format; *walk != '\0'; walk++) {
174 if (strncmp(walk+1, "status", strlen("status")) == 0) {
175 printf("%s", statusbuf);
176 walk += strlen("status");
177 } else if (strncmp(walk+1, "percentage", strlen("percentage")) == 0) {
178 printf("%s", percentagebuf);
179 walk += strlen("percentage");
180 } else if (strncmp(walk+1, "remaining", strlen("remaining")) == 0) {
181 printf("%s", remainingbuf);
182 walk += strlen("remaining");
183 } else if (strncmp(walk+1, "emptytime", strlen("emptytime")) == 0) {
184 printf("%s", emptytimebuf);
185 walk += strlen("emptytime");