Xinqi Bao's Git

99e09d531f95f5b38f4893b4da82ff3ba2fa983e
[slstatus.git] / components / battery.c
1 /* See LICENSE file for copyright and license details. */
2 #include <stdio.h>
3 #include <string.h>
4
5 #include "../util.h"
6
7 #if defined(__linux__)
8 #include <limits.h>
9 #include <unistd.h>
10
11 #define CHARGE_NOW "/sys/class/power_supply/%s/charge_now"
12 #define ENERGY_NOW "/sys/class/power_supply/%s/energy_now"
13 #define CURRENT_NOW "/sys/class/power_supply/%s/current_now"
14 #define POWER_NOW "/sys/class/power_supply/%s/power_now"
15
16 static const char *
17 pick(const char *bat, const char *f1, const char *f2, char *path,
18 size_t length)
19 {
20 if (esnprintf(path, length, f1, bat) > 0 &&
21 access(path, R_OK) == 0) {
22 return f1;
23 }
24
25 if (esnprintf(path, length, f2, bat) > 0 &&
26 access(path, R_OK) == 0) {
27 return f2;
28 }
29
30 return NULL;
31 }
32
33 const char *
34 battery_perc(const char *bat)
35 {
36 int perc;
37 char path[PATH_MAX];
38
39 if (esnprintf(path, sizeof(path),
40 "/sys/class/power_supply/%s/capacity",
41 bat) < 0) {
42 return NULL;
43 }
44 if (pscanf(path, "%d", &perc) != 1) {
45 return NULL;
46 }
47
48 return bprintf("%d", perc);
49 }
50
51 const char *
52 battery_state(const char *bat)
53 {
54 static struct {
55 char *state;
56 char *symbol;
57 } map[] = {
58 { "Charging", "+" },
59 { "Discharging", "-" },
60 };
61 size_t i;
62 char path[PATH_MAX], state[12];
63
64 if (esnprintf(path, sizeof(path),
65 "/sys/class/power_supply/%s/status",
66 bat) < 0) {
67 return NULL;
68 }
69 if (pscanf(path, "%12s", state) != 1) {
70 return NULL;
71 }
72
73 for (i = 0; i < LEN(map); i++) {
74 if (!strcmp(map[i].state, state)) {
75 break;
76 }
77 }
78 return (i == LEN(map)) ? "?" : map[i].symbol;
79 }
80
81 const char *
82 battery_remaining(const char *bat)
83 {
84 int charge_now, current_now, m, h;
85 double timeleft;
86 char path[PATH_MAX], state[12];
87
88 if (esnprintf(path, sizeof(path),
89 "/sys/class/power_supply/%s/status",
90 bat) < 0) {
91 return NULL;
92 }
93 if (pscanf(path, "%12s", state) != 1) {
94 return NULL;
95 }
96
97 if (!pick(bat, CHARGE_NOW, ENERGY_NOW, path, sizeof(path)) ||
98 pscanf(path, "%d", &charge_now) < 0) {
99 return NULL;
100 }
101
102 if (!strcmp(state, "Discharging")) {
103 if (!pick(bat, CURRENT_NOW, POWER_NOW, path,
104 sizeof(path)) ||
105 pscanf(path, "%d", &current_now) < 0) {
106 return NULL;
107 }
108
109 if (current_now == 0) {
110 return NULL;
111 }
112
113 timeleft = (double)charge_now / (double)current_now;
114 h = timeleft;
115 m = (timeleft - (double)h) * 60;
116
117 return bprintf("%dh %dm", h, m);
118 }
119
120 return "";
121 }
122 #elif defined(__OpenBSD__)
123 #include <fcntl.h>
124 #include <machine/apmvar.h>
125 #include <sys/ioctl.h>
126 #include <unistd.h>
127
128 static int
129 load_apm_power_info(struct apm_power_info *apm_info)
130 {
131 int fd;
132
133 fd = open("/dev/apm", O_RDONLY);
134 if (fd < 0) {
135 warn("open '/dev/apm':");
136 return 0;
137 }
138
139 memset(apm_info, 0, sizeof(struct apm_power_info));
140 if (ioctl(fd, APM_IOC_GETPOWER, apm_info) < 0) {
141 warn("ioctl 'APM_IOC_GETPOWER':");
142 close(fd);
143 return 0;
144 }
145 return close(fd), 1;
146 }
147
148 const char *
149 battery_perc(const char *unused)
150 {
151 struct apm_power_info apm_info;
152
153 if (load_apm_power_info(&apm_info)) {
154 return bprintf("%d", apm_info.battery_life);
155 }
156
157 return NULL;
158 }
159
160 const char *
161 battery_state(const char *unused)
162 {
163 struct {
164 unsigned int state;
165 char *symbol;
166 } map[] = {
167 { APM_AC_ON, "+" },
168 { APM_AC_OFF, "-" },
169 };
170 struct apm_power_info apm_info;
171 size_t i;
172
173 if (load_apm_power_info(&apm_info)) {
174 for (i = 0; i < LEN(map); i++) {
175 if (map[i].state == apm_info.ac_state) {
176 break;
177 }
178 }
179 return (i == LEN(map)) ? "?" : map[i].symbol;
180 }
181
182 return NULL;
183 }
184
185 const char *
186 battery_remaining(const char *unused)
187 {
188 struct apm_power_info apm_info;
189
190 if (load_apm_power_info(&apm_info)) {
191 if (apm_info.ac_state != APM_AC_ON) {
192 return bprintf("%uh %02um",
193 apm_info.minutes_left / 60,
194 apm_info.minutes_left % 60);
195 } else {
196 return "";
197 }
198 }
199
200 return NULL;
201 }
202 #endif