summaryrefslogtreecommitdiff
path: root/sysutils/xbattbar/patches/patch-ab
blob: 141a20192ffc1ab4d2c8b1ec60fea6b11158d2ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
$NetBSD: patch-ab,v 1.10 2018/05/06 14:32:06 jmcneill Exp $

--- xbattbar.c.orig	2001-02-02 05:25:29.000000000 +0000
+++ xbattbar.c
@@ -27,6 +27,16 @@ static char *ReleaseVersion="1.4.2";
 
 #include <sys/types.h>
 #include <sys/time.h>
+
+#ifdef __NetBSD__
+#define ENVSYSUNITNAMES
+#include <sys/param.h>
+#include <sys/envsys.h>
+#include <paths.h>
+#include <stdlib.h>
+#include <string.h>
+#endif /* __NetBSD__ */
+
 #include <signal.h>
 #include <stdio.h>
 #include <unistd.h>
@@ -577,33 +587,190 @@ void battery_check(void)
 
 #ifdef __NetBSD__
 
+#ifndef _NO_APM
 #include <machine/apmvar.h>
+#else
+#define APM_AC_OFF	0x00
+#define APM_AC_ON	0x01
+#endif
 
 #define _PATH_APM_SOCKET       "/var/run/apmdev"
 #define _PATH_APM_CTLDEV       "/dev/apmctl"
 #define _PATH_APM_NORMAL       "/dev/apm"
 
+/*
+ * pre:  fd contains a valid file descriptor of an envsys(4) supporting device
+ *       && ns is the number of sensors
+ *       && etds and ebis are arrays of sufficient size
+ * post: returns 0 and etds and ebis arrays are filled with sensor info
+ *       or returns -1 on failure
+ */
+static int
+fillsensors(int fd, envsys_tre_data_t *etds, envsys_basic_info_t *ebis,
+    size_t ns)
+{
+	int i;
+
+	for (i = 0; i < ns; ++i) {
+		ebis[i].sensor = i;
+		if (ioctl(fd, ENVSYS_GTREINFO, &ebis[i]) == -1) {
+			warn("Can't get sensor info for sensor %d", i);
+			return 0;
+		}
+
+		etds[i].sensor = i;
+		if (ioctl(fd, ENVSYS_GTREDATA, &etds[i]) == -1) {
+			warn("Can't get sensor data for sensor %d", i);
+			return 0;
+		}
+	}
+	return 1;
+}
+
+/*
+ * pre:  fd contains a valid file descriptor of an envsys(4) supporting device
+ * post: returns the number of valid sensors provided by the device
+ *       or -1 on error
+ */
+static size_t
+numsensors(int fd)
+{
+	int count = 0, valid = 1;
+	envsys_tre_data_t etd;
+	etd.sensor = 0;
+
+	while (valid) {
+		if (ioctl(fd, ENVSYS_GTREDATA, &etd) == -1)
+			err(1, "Can't get sensor data");
+
+		valid = etd.validflags & ENVSYS_FVALID;
+		if (valid)
+			++count;
+
+		++etd.sensor;
+	}
+
+	return count;
+}
+
+static envsys_tre_data_t *etds;
+static envsys_basic_info_t *ebis;
+static int *cetds;
+
+#if defined(_PATH_SYSMON) && __NetBSD_Version__ >= 106110000
+#define HAVE_NETBSD_ACPI
+#endif
+
 int first = 1;
 void battery_check(void)
 {
        int fd, r, p;
+#ifndef _NO_APM
        struct apm_power_info info;
+#endif
+       int acpi;
+       size_t ns;
+       size_t cc;
+       char *apmdev;
+       int i;
+
+       acpi = 0;
+       apmdev = _PATH_APM_NORMAL;
+       if ((fd = open(apmdev, O_RDONLY)) == -1) {
+#ifdef HAVE_NETBSD_ACPI
+	       apmdev = _PATH_SYSMON;
+	       fd = open(apmdev, O_RDONLY);
+	       acpi = 1;
+#endif
+       }
+       if (fd < 0) {
+               fprintf(stderr, "xbattbar: cannot open %s device\n", apmdev);
+               exit(1);
+       }
 
-       if ((fd = open(_PATH_APM_NORMAL, O_RDONLY)) == -1) {
-               fprintf(stderr, "xbattbar: cannot open apm device\n");
+       if (acpi) {
+#ifdef HAVE_NETBSD_ACPI
+		if ((ns = numsensors(fd)) == 0) {
+		       fprintf(stderr, "xbattbar: no sensors found\n");
                exit(1);
        }
+		if (first) {
+			cetds = (int *)malloc(ns * sizeof(int));
+			etds = (envsys_tre_data_t *)malloc(ns * sizeof(envsys_tre_data_t));
+			ebis = (envsys_basic_info_t *)malloc(ns * sizeof(envsys_basic_info_t));
+
+			if ((cetds == NULL) || (etds == NULL) || (ebis == NULL)) {
+				err(1, "Out of memory");
+			}
+		}
+
+		fillsensors(fd, etds, ebis, ns);
+
+#endif
+#ifndef _NO_APM
+       } else {
 
+	       memset(&info, 0, sizeof(info));
        if (ioctl(fd, APM_IOC_GETPOWER, &info) != 0) {
                fprintf(stderr, "xbattbar: ioctl APM_IOC_GETPOWER failed\n");
                exit(1);
        }
+#endif
+       }
 
        close(fd);
 
        ++elapsed_time;
 
-       /* get current remoain */
+       if (acpi) {
+#ifdef HAVE_NETBSD_ACPI
+		int32_t rtot = 0, maxtot = 0;
+		int have_pct = 0;
+		p = APM_AC_ON;
+		for (i = 0 ; i < ns ; i++) {
+			if ((etds[i].validflags & ENVSYS_FCURVALID) == 0)
+				continue;
+			cc = strlen(ebis[i].desc);
+			if (strncmp(ebis[i].desc, "acpibat", 7) == 0 &&
+			    (strcmp(&ebis[i].desc[cc - 7], " charge") == 0 ||
+			     strcmp(&ebis[i].desc[cc - 7], " energy") == 0)) {
+				rtot += etds[i].cur.data_s;
+				maxtot += etds[i].max.data_s;
+			}
+			/*
+			 * XXX: We should use acpiacad driver and look for
+			 * " connected", but that's broken on some machines
+			 * and we want this to work everywhere.  With this
+			 * we will occasionally catch a machine conditioning
+			 * a battery while connected, while other machines take
+			 * 10-15 seconds to switch from "charging" to
+			 * "discharging" and vice versa, but this is the best
+			 * compromise.
+			 */
+			if ((ebis[i].units == ENVSYS_SWATTS || ebis[i].units == ENVSYS_SAMPS) &&
+			    etds[i].cur.data_s &&
+			    strncmp(ebis[i].desc, "acpibat", 7) == 0 &&
+			    strcmp(&ebis[i].desc[cc - 14], "discharge rate") == 0) {
+				p = APM_AC_OFF;
+			}
+
+			if (ebis[i].units == ENVSYS_INTEGER &&
+			    strcmp(ebis[i].desc, "battery percent") == 0) {
+				have_pct = 1;
+				r = etds[i].cur.data_s;
+			}
+			if (ebis[i].units == ENVSYS_INDICATOR &&
+			    strcmp(ebis[i].desc, "ACIN present") == 0 &&
+			    etds[i].cur.data_s == 0) {
+				p = APM_AC_OFF;
+			}
+		}
+		if (!have_pct)
+			r = (rtot * 100.0) / maxtot;
+#endif
+#ifndef _NO_APM
+       } else {
+	       /* get current remain */
        if (info.battery_life > 100) {
                /* some APM BIOSes return values slightly > 100 */
                r = 100;
@@ -617,6 +784,8 @@ void battery_check(void)
        } else {
                p = APM_AC_OFF;
        }
+#endif
+       }
 
        if (first || ac_line != p || battery_level != r) {
                first = 0;