Xinqi Bao's Git
b086a283a654bc21925b09c778daa1eba8016a2f
1 /* See LICENSE file for copyright and license details. */
13 { "Discharging", "-" },
16 #if defined(__linux__)
20 battery_perc(const char *bat
)
25 snprintf(path
, sizeof(path
), "%s%s%s", "/sys/class/power_supply/",
27 return (pscanf(path
, "%d", &perc
) == 1) ? bprintf("%d", perc
) : NULL
;
31 battery_state(const char *bat
)
34 char path
[PATH_MAX
], state
[12];
36 snprintf(path
, sizeof(path
), "%s%s%s", "/sys/class/power_supply/",
38 if (pscanf(path
, "%12s", state
) != 1) {
42 for (i
= 0; i
< LEN(map
); i
++) {
43 if (!strcmp(map
[i
].state
, state
)) {
47 return (i
== LEN(map
)) ? "?" : map
[i
].symbol
;
51 battery_remaining(const char *bat
)
53 int charge_now
, current_now
, m
, h
;
55 char path
[PATH_MAX
], state
[12];
57 snprintf(path
, sizeof(path
), "%s%s%s", "/sys/class/power_supply/",
59 if (pscanf(path
, "%12s", state
) != 1) {
63 if (!strcmp(state
, "Discharging")) {
64 snprintf(path
, sizeof(path
), "%s%s%s", "/sys/class/power_supply/",
66 if (pscanf(path
, "%d", &charge_now
) != 1) {
69 snprintf(path
, sizeof(path
), "%s%s%s", "/sys/class/power_supply/",
71 if (pscanf(path
, "%d", ¤t_now
) != 1) {
75 timeleft
= (float)charge_now
/ (float)current_now
;
77 m
= (timeleft
- (float)h
) * 60;
79 return bprintf("%dh %dm", h
, m
);
84 #elif defined(__OpenBSD__)
86 #include <machine/apmvar.h>
87 #include <sys/ioctl.h>
91 load_apm_power_info(struct apm_power_info
*apm_info
)
95 fd
= open("/dev/apm", O_RDONLY
);
97 warn("open '/dev/apm':");
101 memset(apm_info
, 0, sizeof(struct apm_power_info
));
102 if (ioctl(fd
, APM_IOC_GETPOWER
, apm_info
) < 0) {
103 warn("ioctl 'APM_IOC_GETPOWER':");
111 battery_perc(const char *unused
)
113 struct apm_power_info apm_info
;
115 if (load_apm_power_info(&apm_info
)) {
116 return bprintf("%d", apm_info
.battery_life
);
123 battery_state(const char *unused
)
125 struct apm_power_info apm_info
;
128 if (load_apm_power_info(&apm_info
)) {
129 for (i
= 0; i
< LEN(map
); i
++) {
130 if (map
[i
].state
== apm_info
.ac_state
) {
134 return (i
== LEN(map
)) ? "?" : map
[i
].symbol
;
141 battery_remaining(const char *unused
)
143 struct apm_power_info apm_info
;
145 if (load_apm_power_info(&apm_info
)) {
146 if (apm_info
.ac_state
!= APM_AC_ON
) {
147 return bprintf("%uh %02um", apm_info
.minutes_left
/ 60,
148 apm_info
.minutes_left
% 60);