Xinqi Bao's Git

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