Xinqi Bao's Git
1 /* See LICENSE file for copyright and license details. */
8 #elif defined(__OpenBSD__)
12 #include <machine/apmvar.h>
17 #if defined(__linux__)
19 battery_perc(const char *bat
)
24 snprintf(path
, sizeof(path
), "%s%s%s", "/sys/class/power_supply/", bat
, "/capacity");
25 return (pscanf(path
, "%i", &perc
) == 1) ?
26 bprintf("%d", perc
) : NULL
;
28 #elif defined(__OpenBSD__)
30 battery_perc(const char *null
)
32 struct apm_power_info apm_info
;
35 fd
= open("/dev/apm", O_RDONLY
);
37 fprintf(stderr
, "open '/dev/apm': %s\n", strerror(errno
));
41 if (ioctl(fd
, APM_IOC_GETPOWER
, &apm_info
) < 0) {
42 fprintf(stderr
, "ioctl 'APM_IOC_GETPOWER': %s\n", strerror(errno
));
48 return bprintf("%d", apm_info
.battery_life
);
52 #if defined(__linux__)
54 battery_power(const char *bat
)
59 snprintf(path
, sizeof(path
), "%s%s%s", "/sys/class/power_supply/", bat
, "/power_now");
60 return (pscanf(path
, "%i", &watts
) == 1) ?
61 bprintf("%d", (watts
+ 500000) / 1000000) : NULL
;
65 battery_state(const char *bat
)
72 { "Discharging", "-" },
77 char path
[PATH_MAX
], state
[12];
79 snprintf(path
, sizeof(path
), "%s%s%s", "/sys/class/power_supply/", bat
, "/status");
80 if (pscanf(path
, "%12s", state
) != 1) {
84 for (i
= 0; i
< LEN(map
); i
++) {
85 if (!strcmp(map
[i
].state
, state
)) {
89 return (i
== LEN(map
)) ? "?" : map
[i
].symbol
;