Xinqi Bao's Git

battery: Minor coding style fixes
[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 float 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 timeleft = (float)charge_now / (float)current_now;
111 h = timeleft;
112 m = (timeleft - (float)h) * 60;
113
114 return bprintf("%dh %dm", h, m);
115 }
116
117 return "";
118 }
119 #elif defined(__OpenBSD__)
120 #include <fcntl.h>
121 #include <machine/apmvar.h>
122 #include <sys/ioctl.h>
123 #include <unistd.h>
124
125 static int
126 load_apm_power_info(struct apm_power_info *apm_info)
127 {
128 int fd;
129
130 fd = open("/dev/apm", O_RDONLY);
131 if (fd < 0) {
132 warn("open '/dev/apm':");
133 return 0;
134 }
135
136 memset(apm_info, 0, sizeof(struct apm_power_info));
137 if (ioctl(fd, APM_IOC_GETPOWER, apm_info) < 0) {
138 warn("ioctl 'APM_IOC_GETPOWER':");
139 close(fd);
140 return 0;
141 }
142 return close(fd), 1;
143 }
144
145 const char *
146 battery_perc(const char *unused)
147 {
148 struct apm_power_info apm_info;
149
150 if (load_apm_power_info(&apm_info)) {
151 return bprintf("%d", apm_info.battery_life);
152 }
153
154 return NULL;
155 }
156
157 const char *
158 battery_state(const char *unused)
159 {
160 struct {
161 unsigned int state;
162 char *symbol;
163 } map[] = {
164 { APM_AC_ON, "+" },
165 { APM_AC_OFF, "-" },
166 };
167 struct apm_power_info apm_info;
168 size_t i;
169
170 if (load_apm_power_info(&apm_info)) {
171 for (i = 0; i < LEN(map); i++) {
172 if (map[i].state == apm_info.ac_state) {
173 break;
174 }
175 }
176 return (i == LEN(map)) ? "?" : map[i].symbol;
177 }
178
179 return NULL;
180 }
181
182 const char *
183 battery_remaining(const char *unused)
184 {
185 struct apm_power_info apm_info;
186
187 if (load_apm_power_info(&apm_info)) {
188 if (apm_info.ac_state != APM_AC_ON) {
189 return bprintf("%uh %02um",
190 apm_info.minutes_left / 60,
191 apm_info.minutes_left % 60);
192 } else {
193 return "";
194 }
195 }
196
197 return NULL;
198 }
199 #endif