Xinqi Bao's Git
e997e4c9a86497272ff7a43269a0f6a338971821
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) ?
20 bprintf("%d", perc
) : NULL
;
24 battery_state(const char *bat
)
31 { "Discharging", "-" },
35 char path
[PATH_MAX
], state
[12];
37 snprintf(path
, sizeof(path
), "%s%s%s", "/sys/class/power_supply/",
39 if (pscanf(path
, "%12s", state
) != 1) {
43 for (i
= 0; i
< LEN(map
); i
++) {
44 if (!strcmp(map
[i
].state
, state
)) {
48 return (i
== LEN(map
)) ? "?" : map
[i
].symbol
;
50 #elif defined(__OpenBSD__)
52 #include <machine/apmvar.h>
53 #include <sys/ioctl.h>
57 battery_perc(const char *null
)
59 struct apm_power_info apm_info
;
62 fd
= open("/dev/apm", O_RDONLY
);
64 fprintf(stderr
, "open '/dev/apm': %s\n", strerror(errno
));
68 if (ioctl(fd
, APM_IOC_GETPOWER
, &apm_info
) < 0) {
69 fprintf(stderr
, "ioctl 'APM_IOC_GETPOWER': %s\n",
76 return bprintf("%d", apm_info
.battery_life
);
80 battery_state(const char *bat
)
84 struct apm_power_info apm_info
;
93 fd
= open("/dev/apm", O_RDONLY
);
95 fprintf(stderr
, "open '/dev/apm': %s\n", strerror(errno
));
99 if (ioctl(fd
, APM_IOC_GETPOWER
, &apm_info
) < 0) {
100 fprintf(stderr
, "ioctl 'APM_IOC_GETPOWER': %s\n",
107 for (i
= 0; i
< LEN(map
); i
++) {
108 if (map
[i
].state
== apm_info
.ac_state
) {
112 return (i
== LEN(map
)) ? "?" : map
[i
].symbol
;