Xinqi Bao's Git

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