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) ? 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 *null
)
57 struct apm_power_info apm_info
;
60 fd
= open("/dev/apm", O_RDONLY
);
62 fprintf(stderr
, "open '/dev/apm': %s\n", strerror(errno
));
66 if (ioctl(fd
, APM_IOC_GETPOWER
, &apm_info
) < 0) {
67 fprintf(stderr
, "ioctl 'APM_IOC_GETPOWER': %s\n",
74 return bprintf("%d", apm_info
.battery_life
);
78 battery_state(const char *bat
)
82 struct apm_power_info apm_info
;
91 fd
= open("/dev/apm", O_RDONLY
);
93 fprintf(stderr
, "open '/dev/apm': %s\n", strerror(errno
));
97 if (ioctl(fd
, APM_IOC_GETPOWER
, &apm_info
) < 0) {
98 fprintf(stderr
, "ioctl 'APM_IOC_GETPOWER': %s\n",
105 for (i
= 0; i
< LEN(map
); i
++) {
106 if (map
[i
].state
== apm_info
.ac_state
) {
110 return (i
== LEN(map
)) ? "?" : map
[i
].symbol
;