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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
--- epplets/E-Power.c.orig 2011-11-26 11:38:40.000000000 +0100
+++ epplets/E-Power.c 2012-10-10 15:01:17.000000000 +0200
@@ -1,9 +1,22 @@
+/*-
+ * Copyright 2008, Pietro Cerutti <gahr@FreeBSD.org> (FreeBSD adaptation)
+ */
+
#define _GNU_SOURCE
#include "epplet.h"
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
+#ifdef __FreeBSD__
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <dev/acpica/acpiio.h>
+#ifdef __i386__
+#include <machine/apm_bios.h>
+#endif
+#endif
+
/* Modified by Attila ZIMLER <hijaszu@hlfslinux.hu>, 2003/11/16
Added ACPI power management support.
*/
@@ -11,6 +24,20 @@
/* Length of explain strings in /proc/acpi/battery/BAT0 data files */
#define DATA_EXPLAIN_STR_LEN 25
+/* Power management modes */
+#define MODE_NONE 0x0
+#define MODE_APM 0x1
+#define MODE_ACPI 0x2
+#define MODE_SYS 0x3
+static unsigned mode = MODE_NONE;
+
+#ifdef __FreeBSD__
+#define ACPI_DEV "/dev/acpi"
+#define APM_DEV "/dev/apm"
+static int apm_fd = -1,
+ acpi_fd = -1;
+#endif
+
int prev_bat_val = 110;
int bat_val = 0;
int time_val = 0;
@@ -33,14 +60,37 @@
static void
cb_timer(void *data)
{
+#ifdef linux
struct stat st;
if ((stat("/proc/apm", &st) > -1) && S_ISREG(st.st_mode))
- cb_timer_apm(data);
+ mode = MODE_APM;
else if ((stat("/proc/acpi/battery", &st) > -1) && S_ISDIR(st.st_mode))
- cb_timer_acpi(data);
+ mode = MODE_ACPI;
else if ((stat("/sys/class/power_supply", &st) > -1) && S_ISDIR(st.st_mode))
- cb_timer_sys(data);
+ mode = MODE_SYS;
+ else
+ mode = MODE_NONE;
+#elif defined (__FreeBSD__)
+ /*
+ * Try ACPI first, if it doesn't work, revert to APM
+ */
+ if (acpi_fd != 1 || ((acpi_fd = open(ACPI_DEV, O_RDONLY)) != -1))
+ mode = MODE_ACPI;
+ else if (apm_fd != 1 || (apm_fd = open(APM_DEV, O_RDONLY)) != -1)
+ mode = MODE_APM;
+ else
+ mode = MODE_NONE;
+#else
+ mode = MODE_NONE;
+#endif
+
+ if (mode & MODE_APM)
+ cb_timer_apm(data);
+ else if (mode & MODE_ACPI)
+ cb_timer_acpi(data);
+ else if (mode & MODE_SYS)
+ tb_timer_sys(data);
}
static void
@@ -61,9 +111,8 @@
int bat_level = 0;
int bat_drain = 1;
- int bat_val = 0;
-
char current_status[256];
+
char *line = 0;
size_t lsize = 0;
int discharging = 0;
@@ -77,6 +126,8 @@
int hours, minutes;
+#ifdef linux
+
/* Read some information on first run. */
dirp = opendir("/proc/acpi/battery");
if (dirp)
@@ -207,27 +258,74 @@
else
snprintf(current_status, sizeof(current_status), "Full");
+ /* Final steps before ending the status update. */
+ data = NULL;
+ if (lsize)
+ free(line);
+
+#elif defined(__FreeBSD__)
+ union acpi_battery_ioctl_arg batt;
+ int c;
+ batt.unit = 0;
+ if(ioctl(acpi_fd, ACPIIO_BATT_GET_BATTINFO, &batt) == -1)
+ return;
+
+ /*
+ * Get percent
+ */
+ if(batt.battinfo.cap == -1) {
+ c = snprintf(current_status, sizeof(current_status), "???");
+ bat_val = 0;
+ }
+ else {
+ c = snprintf(current_status, sizeof(current_status), "%d%%",
+ batt.battinfo.cap);
+ bat_val = batt.battinfo.cap;
+ }
+
+ /*
+ * Get online status
+ */
+ if(batt.battinfo.state == ACPI_BATT_STAT_NOT_PRESENT)
+ c += snprintf(¤t_status[c], sizeof(current_status) - c, " NONE\n");
+ else if(batt.battinfo.state == ACPI_BATT_STAT_DISCHARG || batt.battinfo.state == ACPI_BATT_STAT_CRITICAL)
+ c += snprintf(¤t_status[c], sizeof(current_status) - c, " OFF\n");
+ else
+ c += snprintf(¤t_status[c], sizeof(current_status) - c, " ON\n");
+
+ /*
+ * Get remaining time
+ */
+ if(batt.battinfo.min == -1) {
+ if(batt.battinfo.state == ACPI_BATT_STAT_DISCHARG ||
+ batt.battinfo.state == ACPI_BATT_STAT_CRITICAL ||
+ batt.battinfo.state == ACPI_BATT_STAT_NOT_PRESENT)
+ snprintf(¤t_status[c], sizeof(current_status) - c, "?:??");
+ }
+ else {
+ snprintf(¤t_status[c], sizeof(current_status) - c, "%d:%2d",
+ batt.battinfo.min / 60, batt.battinfo.min % 60);
+ }
+
+#endif
/* Display current status */
Epplet_change_label(label, current_status);
sprintf(current_status, "E-Power-Bat-%i.png", ((bat_val + 5) / 10) * 10);
Epplet_change_image(image, 44, 24, current_status);
Epplet_timer(cb_timer, NULL, 5.0, "TIMER");
-
- /* Final steps before ending the status update. */
- data = NULL;
- if (lsize)
- free(line);
}
static void
cb_timer_apm(void *data)
{
+ char s[256];
+#ifdef linux
static FILE *f;
f = fopen("/proc/apm", "r");
if (f)
{
- char s[256], s1[32], s2[32], s3[32];
+ char s1[32], s2[32], s3[32];
int apm_flags, ac_stat, bat_stat, bat_flags;
int i, hours, minutes, up, up2;
char *s_ptr;
@@ -300,14 +398,57 @@
else
s_ptr += sprintf(s_ptr, "%i:%02i", hours, minutes);
}
- Epplet_change_label(label, s);
+ prev_bat_val = bat_val;
+#elif defined(__FreeBSD__) && defined(__i386__)
+ struct apm_info apm;
+ int c;
+ if(ioctl(apm_fd, APMIO_GETINFO, &apm) == -1)
+ return;
+
+ /*
+ * Get percent
+ */
+ if(apm.ai_batt_life == 0xff) {
+ c = snprintf(s, sizeof(s), "???");
+ bat_val = 0;
+ }
+ else {
+ c = snprintf(s, sizeof(s), "%d%%", apm.ai_batt_life);
+ bat_val = apm.ai_batt_life;
+ }
+
+ /*
+ * Get online status
+ */
+ if(apm.ai_acline == 0xff)
+ c += snprintf(&s[c], sizeof(s) - c, " NONE\n");
+ else if(apm.ai_acline)
+ c += snprintf(&s[c], sizeof(s) - c, " ON\n");
+ else
+ c += snprintf(&s[c], sizeof(s) - c , " OFF\n");
+
+ /*
+ * Get remaining time
+ */
+ if(apm.ai_batt_time == -1) {
+ if(apm.ai_acline == 0xff || !apm.ai_acline)
+ snprintf(&s[c], sizeof(s) - c, "?:??");
+ }
+ else
+ snprintf(&s[c], sizeof(s) - c, "%d:%2d",
+ apm.ai_batt_time / 3600, apm.ai_batt_time / 60 % 60);
+#endif
+ /* Display current status */
+ Epplet_change_label(label, s);
sprintf(s, "E-Power-Bat-%i.png", ((bat_val + 5) / 10) * 10);
Epplet_change_image(image, 44, 24, s);
+#ifdef linux
Epplet_timer(cb_timer, NULL, 30.0, "TIMER");
+#else
+ Epplet_timer(cb_timer, NULL, 5.0, "TIMER");
+#endif
- prev_bat_val = bat_val;
- }
data = NULL;
}
@@ -530,7 +671,14 @@
static void
cb_suspend(void *data)
{
+#ifdef __FreeBSD__
+ if(mode & MODE_ACPI)
+ system("/usr/sbin/acpiconf -s 5");
+ else if(mode & MODE_APM)
+ system("/usr/sbin/apm -z");
+#else
system("/usr/bin/apm -s");
+#endif
return;
data = NULL;
}
@@ -538,7 +686,14 @@
static void
cb_sleep(void *data)
{
+#ifdef __FreeBSD__
+ if(mode & MODE_ACPI)
+ system("/usr/sbin/acpiconf -s 1");
+ else if(mode & MODE_APM)
+ system("/usr/sbin/amp -z");
+#else
system("/usr/bin/apm -S");
+#endif
return;
data = NULL;
}
|