Xinqi Bao's Git
152777ef46da27dcbeb8594623cbfb2d74b1c34b
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
;
50 battery_remaining(const char *bat
)
55 #elif defined(__OpenBSD__)
57 #include <machine/apmvar.h>
58 #include <sys/ioctl.h>
62 load_apm_power_info(struct apm_power_info
*apm_info
)
66 fd
= open("/dev/apm", O_RDONLY
);
68 warn("open '/dev/apm':");
72 memset(apm_info
, 0, sizeof(struct apm_power_info
));
73 if (ioctl(fd
, APM_IOC_GETPOWER
, apm_info
) < 0) {
74 warn("ioctl 'APM_IOC_GETPOWER':");
82 battery_perc(const char *unused
)
84 struct apm_power_info apm_info
;
86 if (load_apm_power_info(&apm_info
)) {
87 return bprintf("%d", apm_info
.battery_life
);
94 battery_state(const char *unused
)
96 struct apm_power_info apm_info
;
106 if (load_apm_power_info(&apm_info
)) {
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
;
119 battery_remaining(const char *unused
)
121 struct apm_power_info apm_info
;
123 if (load_apm_power_info(&apm_info
)) {
124 return bprintf("%u:%02u", apm_info
.minutes_left
/ 60,
125 apm_info
.minutes_left
% 60);