Xinqi Bao's Git
3965e8f8974d26ed9419594cf0add3b2254ea318
1 /* See LICENSE file for copyright and license details. */
12 battery_perc(const char *bat
)
17 snprintf(path
, sizeof(path
), "%s%s%s", "/sys/class/power_supply/",
19 return (pscanf(path
, "%d", &perc
) == 1) ? bprintf("%d", perc
) : NULL
;
23 battery_state(const char *bat
)
30 { "Discharging", "-" },
33 char path
[PATH_MAX
], state
[12];
35 snprintf(path
, sizeof(path
), "%s%s%s", "/sys/class/power_supply/",
37 if (pscanf(path
, "%12s", state
) != 1) {
41 for (i
= 0; i
< LEN(map
); i
++) {
42 if (!strcmp(map
[i
].state
, state
)) {
46 return (i
== LEN(map
)) ? "?" : map
[i
].symbol
;
50 battery_remaining(const char *bat
)
52 int charge_now
, current_now
, m
, h
;
54 char path
[PATH_MAX
], state
[12];
56 snprintf(path
, sizeof(path
), "%s%s%s", "/sys/class/power_supply/",
58 if (pscanf(path
, "%12s", state
) != 1) {
62 if (!strcmp(state
, "Discharging")) {
63 snprintf(path
, sizeof(path
), "%s%s%s", "/sys/class/power_supply/",
65 if (pscanf(path
, "%d", &charge_now
) != 1) {
68 snprintf(path
, sizeof(path
), "%s%s%s", "/sys/class/power_supply/",
70 if (pscanf(path
, "%d", ¤t_now
) != 1) {
74 timeleft
= (float)charge_now
/ (float)current_now
;
76 m
= (timeleft
- (float)h
) * 60;
78 return bprintf("%dh %dm", h
, m
);
83 #elif defined(__OpenBSD__)
85 #include <machine/apmvar.h>
86 #include <sys/ioctl.h>
90 load_apm_power_info(struct apm_power_info
*apm_info
)
94 fd
= open("/dev/apm", O_RDONLY
);
96 warn("open '/dev/apm':");
100 memset(apm_info
, 0, sizeof(struct apm_power_info
));
101 if (ioctl(fd
, APM_IOC_GETPOWER
, apm_info
) < 0) {
102 warn("ioctl 'APM_IOC_GETPOWER':");
110 battery_perc(const char *unused
)
112 struct apm_power_info apm_info
;
114 if (load_apm_power_info(&apm_info
)) {
115 return bprintf("%d", apm_info
.battery_life
);
122 battery_state(const char *unused
)
124 struct apm_power_info apm_info
;
134 if (load_apm_power_info(&apm_info
)) {
135 for (i
= 0; i
< LEN(map
); i
++) {
136 if (map
[i
].state
== apm_info
.ac_state
) {
140 return (i
== LEN(map
)) ? "?" : map
[i
].symbol
;
147 battery_remaining(const char *unused
)
149 struct apm_power_info apm_info
;
151 if (load_apm_power_info(&apm_info
)) {
152 if (apm_info
.ac_state
!= APM_AC_ON
) {
153 return bprintf("%uh %02um", apm_info
.minutes_left
/ 60,
154 apm_info
.minutes_left
% 60);