Xinqi Bao's Git
1 /* See LICENSE file for copyright and license details. */
11 #include <machine/apmvar.h>
17 battery_perc(const char *bat
)
23 snprintf(path
, sizeof(path
), "%s%s%s", "/sys/class/power_supply/", bat
, "/capacity");
24 return (pscanf(path
, "%i", &perc
) == 1) ?
25 bprintf("%d", perc
) : NULL
;
27 struct apm_power_info apm_info
;
30 fd
= open("/dev/apm", O_RDONLY
);
32 warn("Failed to open file /dev/apm");
36 if (ioctl(fd
, APM_IOC_GETPOWER
, &apm_info
) < 0) {
37 warn("Failed to get battery info");
43 return bprintf("%d", apm_info
.battery_life
);
49 battery_power(const char *bat
)
54 snprintf(path
, sizeof(path
), "%s%s%s", "/sys/class/power_supply/", bat
, "/power_now");
55 return (pscanf(path
, "%i", &watts
) == 1) ?
56 bprintf("%d", (watts
+ 500000) / 1000000) : NULL
;
60 battery_state(const char *bat
)
67 { "Discharging", "-" },
72 char path
[PATH_MAX
], state
[12];
74 snprintf(path
, sizeof(path
), "%s%s%s", "/sys/class/power_supply/", bat
, "/status");
75 if (pscanf(path
, "%12s", state
) != 1) {
79 for (i
= 0; i
< LEN(map
); i
++) {
80 if (!strcmp(map
[i
].state
, state
)) {
84 return (i
== LEN(map
)) ? "?" : map
[i
].symbol
;