Xinqi Bao's Git
1 /* See LICENSE file for copyright and license details. */
8 #elif defined(__OpenBSD__)
12 #include <machine/apmvar.h>
18 battery_perc(const char *bat
)
20 #if defined(__linux__)
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
;
27 #elif defined(__OpenBSD__)
28 struct apm_power_info apm_info
;
31 fd
= open("/dev/apm", O_RDONLY
);
33 fprintf(stderr
, "open '/dev/apm': %s\n", strerror(errno
));
37 if (ioctl(fd
, APM_IOC_GETPOWER
, &apm_info
) < 0) {
38 fprintf(stderr
, "ioctl 'APM_IOC_GETPOWER': %s\n", strerror(errno
));
44 return bprintf("%d", apm_info
.battery_life
);
48 #if defined(__linux__)
50 battery_power(const char *bat
)
55 snprintf(path
, sizeof(path
), "%s%s%s", "/sys/class/power_supply/", bat
, "/power_now");
56 return (pscanf(path
, "%i", &watts
) == 1) ?
57 bprintf("%d", (watts
+ 500000) / 1000000) : NULL
;
61 battery_state(const char *bat
)
68 { "Discharging", "-" },
73 char path
[PATH_MAX
], state
[12];
75 snprintf(path
, sizeof(path
), "%s%s%s", "/sys/class/power_supply/", bat
, "/status");
76 if (pscanf(path
, "%12s", state
) != 1) {
80 for (i
= 0; i
< LEN(map
); i
++) {
81 if (!strcmp(map
[i
].state
, state
)) {
85 return (i
== LEN(map
)) ? "?" : map
[i
].symbol
;