Xinqi Bao's Git
807a7e62ca18c993f6994e071b40872fcda1bde6
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
, "%i", &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
;
48 #elif defined(__OpenBSD__)
50 #include <machine/apmvar.h>
51 #include <sys/ioctl.h>
55 battery_perc(const char *unused
)
57 struct apm_power_info apm_info
;
60 fd
= open("/dev/apm", O_RDONLY
);
62 warn("open '/dev/apm':");
66 if (ioctl(fd
, APM_IOC_GETPOWER
, &apm_info
) < 0) {
67 warn("ioctl 'APM_IOC_GETPOWER':");
73 return bprintf("%d", apm_info
.battery_life
);
77 battery_state(const char *unused
)
81 struct apm_power_info apm_info
;
90 fd
= open("/dev/apm", O_RDONLY
);
92 warn("open '/dev/apm':");
96 if (ioctl(fd
, APM_IOC_GETPOWER
, &apm_info
) < 0) {
97 warn("ioctl 'APM_IOC_GETPOWER':");
103 for (i
= 0; i
< LEN(map
); i
++) {
104 if (map
[i
].state
== apm_info
.ac_state
) {
108 return (i
== LEN(map
)) ? "?" : map
[i
].symbol
;