Xinqi Bao's Git

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