Xinqi Bao's Git
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", "-" },
36 char path
[PATH_MAX
], state
[12];
38 snprintf(path
, sizeof(path
), "%s%s%s", "/sys/class/power_supply/",
40 if (pscanf(path
, "%12s", state
) != 1) {
44 for (i
= 0; i
< LEN(map
); i
++) {
45 if (!strcmp(map
[i
].state
, state
)) {
49 return (i
== LEN(map
)) ? "?" : map
[i
].symbol
;
51 #elif defined(__OpenBSD__)
53 #include <machine/apmvar.h>
54 #include <sys/ioctl.h>
58 battery_perc(const char *null
)
60 struct apm_power_info apm_info
;
63 fd
= open("/dev/apm", O_RDONLY
);
65 fprintf(stderr
, "open '/dev/apm': %s\n", strerror(errno
));
69 if (ioctl(fd
, APM_IOC_GETPOWER
, &apm_info
) < 0) {
70 fprintf(stderr
, "ioctl 'APM_IOC_GETPOWER': %s\n",
77 return bprintf("%d", apm_info
.battery_life
);
81 battery_state(const char *bat
)
85 struct apm_power_info apm_info
;
92 { APM_AC_UNKNOWN
, "/" },
95 fd
= open("/dev/apm", O_RDONLY
);
97 fprintf(stderr
, "open '/dev/apm': %s\n", strerror(errno
));
101 if (ioctl(fd
, APM_IOC_GETPOWER
, &apm_info
) < 0) {
102 fprintf(stderr
, "ioctl 'APM_IOC_GETPOWER': %s\n",
109 for (i
= 0; i
< LEN(map
); i
++) {
110 if (map
[i
].state
== apm_info
.ac_state
) {
114 return (i
== LEN(map
)) ? "?" : map
[i
].symbol
;