Xinqi Bao's Git
1 /* See LICENSE file for copyright and license details. */
6 #elif defined(__OpenBSD__)
10 #include <machine/apmvar.h>
16 battery_perc(const char *bat
)
18 #if defined(__linux__)
22 snprintf(path
, sizeof(path
), "%s%s%s", "/sys/class/power_supply/", bat
, "/capacity");
23 return (pscanf(path
, "%i", &perc
) == 1) ?
24 bprintf("%d", perc
) : NULL
;
25 #elif defined(__OpenBSD__)
26 struct apm_power_info apm_info
;
29 fd
= open("/dev/apm", O_RDONLY
);
31 fprintf(stderr
, "Failed to open file /dev/apm");
35 if (ioctl(fd
, APM_IOC_GETPOWER
, &apm_info
) < 0) {
36 fprintf(stderr
, "Failed to get battery info");
42 return bprintf("%d", apm_info
.battery_life
);
46 #if defined(__linux__)
48 battery_power(const char *bat
)
53 snprintf(path
, sizeof(path
), "%s%s%s", "/sys/class/power_supply/", bat
, "/power_now");
54 return (pscanf(path
, "%i", &watts
) == 1) ?
55 bprintf("%d", (watts
+ 500000) / 1000000) : NULL
;
59 battery_state(const char *bat
)
66 { "Discharging", "-" },
71 char path
[PATH_MAX
], state
[12];
73 snprintf(path
, sizeof(path
), "%s%s%s", "/sys/class/power_supply/", bat
, "/status");
74 if (pscanf(path
, "%12s", state
) != 1) {
78 for (i
= 0; i
< LEN(map
); i
++) {
79 if (!strcmp(map
[i
].state
, state
)) {
83 return (i
== LEN(map
)) ? "?" : map
[i
].symbol
;