Xinqi Bao's Git
327d5765c66991408f2e668e05934894a22702ed
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_power(const char *bat
)
29 snprintf(path
, sizeof(path
), "%s%s%s", "/sys/class/power_supply/",
31 return (pscanf(path
, "%i", &watts
) == 1) ?
32 bprintf("%d", (watts
+ 500000) / 1000000) : NULL
;
36 battery_state(const char *bat
)
43 { "Discharging", "-" },
48 char path
[PATH_MAX
], state
[12];
50 snprintf(path
, sizeof(path
), "%s%s%s", "/sys/class/power_supply/",
52 if (pscanf(path
, "%12s", state
) != 1) {
56 for (i
= 0; i
< LEN(map
); i
++) {
57 if (!strcmp(map
[i
].state
, state
)) {
61 return (i
== LEN(map
)) ? "?" : map
[i
].symbol
;
63 #elif defined(__OpenBSD__)
65 #include <machine/apmvar.h>
66 #include <sys/ioctl.h>
70 battery_perc(const char *null
)
72 struct apm_power_info apm_info
;
75 fd
= open("/dev/apm", O_RDONLY
);
77 fprintf(stderr
, "open '/dev/apm': %s\n", strerror(errno
));
81 if (ioctl(fd
, APM_IOC_GETPOWER
, &apm_info
) < 0) {
82 fprintf(stderr
, "ioctl 'APM_IOC_GETPOWER': %s\n",
89 return bprintf("%d", apm_info
.battery_life
);
93 battery_state(const char *bat
)
97 struct apm_power_info apm_info
;
104 { APM_AC_UNKNOWN
, "/" },
107 fd
= open("/dev/apm", O_RDONLY
);
109 fprintf(stderr
, "open '/dev/apm': %s\n", strerror(errno
));
113 if (ioctl(fd
, APM_IOC_GETPOWER
, &apm_info
) < 0) {
114 fprintf(stderr
, "ioctl 'APM_IOC_GETPOWER': %s\n",
121 for (i
= 0; i
< LEN(map
); i
++) {
122 if (map
[i
].state
== apm_info
.ac_state
) {
126 return (i
== LEN(map
)) ? "?" : map
[i
].symbol
;