Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
438 lines (370 loc) · 11.3 KB

SDL_syspower.c

File metadata and controls

438 lines (370 loc) · 11.3 KB
 
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
/*
SDL - Simple DirectMedia Layer
Copyright (C) 1997-2009 Sam Lantinga
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Sam Lantinga
slouken@libsdl.org
*/
#include "SDL_config.h"
#ifndef SDL_POWER_DISABLED
#ifdef SDL_POWER_LINUX
#include <stdio.h>
#include <unistd.h>
Jun 8, 2009
Jun 8, 2009
30
31
#include <sys/types.h>
#include <sys/stat.h>
Jun 30, 2009
Jun 30, 2009
32
#include <dirent.h>
Jun 8, 2009
Jun 8, 2009
33
34
#include <fcntl.h>
35
36
#include "SDL_power.h"
Jun 30, 2009
Jun 30, 2009
37
static const char *proc_apm_path = "/proc/apm";
Jun 30, 2009
Jun 30, 2009
38
39
static const char *proc_acpi_battery_path = "/proc/acpi/battery";
static const char *proc_acpi_ac_adapter_path = "/proc/acpi/ac_adapter";
Jun 30, 2009
Jun 30, 2009
40
Jun 30, 2009
Jun 30, 2009
41
static int open_acpi_file(const char *base, const char *node, const char *key)
Jun 30, 2009
Jun 30, 2009
42
{
Jun 30, 2009
Jun 30, 2009
43
const size_t pathlen = strlen(base) + strlen(node) + strlen(key) + 3;
Jun 30, 2009
Jun 30, 2009
44
45
46
47
48
char *path = (char *) alloca(pathlen);
if (path == NULL) {
return -1; /* oh well. */
}
Jun 30, 2009
Jun 30, 2009
49
snprintf(path, pathlen, "%s/%s/%s", base, node, key);
Jun 30, 2009
Jun 30, 2009
50
51
52
53
54
return open(path, O_RDONLY);
}
static SDL_bool
Jun 30, 2009
Jun 30, 2009
55
56
load_acpi_file(const char *base, const char *node, const char *key,
char *buf, size_t buflen)
Jun 30, 2009
Jun 30, 2009
57
58
{
ssize_t br = 0;
Jun 30, 2009
Jun 30, 2009
59
const int fd = open_acpi_file(base, node, key);
60
if (fd == -1) {
Jun 30, 2009
Jun 30, 2009
61
62
63
64
65
66
67
68
69
70
71
return SDL_FALSE;
}
br = read(fd, buf, buflen-1);
close(fd);
if (br < 0) {
return SDL_FALSE;
}
buf[br] = '\0'; // null-terminate the string.
return SDL_TRUE;
}
Jun 30, 2009
Jun 30, 2009
72
Jun 30, 2009
Jun 30, 2009
73
static SDL_bool
Jun 30, 2009
Jun 30, 2009
74
make_proc_acpi_key_val(char **_ptr, char **_key, char **_val)
Jun 30, 2009
Jun 30, 2009
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
{
char *ptr = *_ptr;
while (*ptr == ' ') {
ptr++; /* skip whitespace. */
}
if (*ptr == '\0') {
return SDL_FALSE; /* EOF. */
}
*_key = ptr;
while ((*ptr != ':') && (*ptr != '\0')) {
ptr++;
}
if (*ptr == '\0') {
return SDL_FALSE; /* (unexpected) EOF. */
}
*(ptr++) = '\0'; /* terminate the key. */
while ((*ptr == ' ') && (*ptr != '\0')) {
ptr++; /* skip whitespace. */
}
if (*ptr == '\0') {
return SDL_FALSE; /* (unexpected) EOF. */
}
*_val = ptr;
while ((*ptr != '\n') && (*ptr != '\0')) {
ptr++;
}
if (*ptr != '\0') {
*(ptr++) = '\0'; /* terminate the value. */
Jun 30, 2009
Jun 30, 2009
115
116
*_ptr = ptr; /* store for next time. */
117
return SDL_TRUE;
Jun 30, 2009
Jun 30, 2009
118
119
120
}
static void
Jun 30, 2009
Jun 30, 2009
121
122
check_proc_acpi_battery(const char * node, SDL_bool * have_battery,
SDL_bool * charging, int *seconds, int *percent)
Jun 30, 2009
Jun 30, 2009
123
{
Jun 30, 2009
Jun 30, 2009
124
const char *base = proc_acpi_battery_path;
Jun 30, 2009
Jun 30, 2009
125
126
127
128
129
130
131
132
133
134
135
136
137
char info[1024];
char state[1024];
char *ptr = NULL;
char *key = NULL;
char *val = NULL;
SDL_bool charge = SDL_FALSE;
SDL_bool choose = SDL_FALSE;
SDL_bool is_ac = SDL_FALSE;
int maximum = -1;
int remaining = -1;
int secs = -1;
int pct = -1;
Jun 30, 2009
Jun 30, 2009
138
if (!load_acpi_file(base, node, "state", state, sizeof (state))) {
Jun 30, 2009
Jun 30, 2009
139
return;
Jun 30, 2009
Jun 30, 2009
140
} else if (!load_acpi_file(base, node, "info", info, sizeof (info))) {
Jun 30, 2009
Jun 30, 2009
141
142
143
144
return;
}
ptr = &state[0];
Jun 30, 2009
Jun 30, 2009
145
while (make_proc_acpi_key_val(&ptr, &key, &val)) {
Jun 30, 2009
Jun 30, 2009
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
if (strcmp(key, "present") == 0) {
if (strcmp(val, "yes") == 0) {
*have_battery = SDL_TRUE;
}
} else if (strcmp(key, "charging state") == 0) {
/* !!! FIXME: what exactly _does_ charging/discharging mean? */
if (strcmp(val, "charging/discharging") == 0) {
charge = SDL_TRUE;
} else if (strcmp(val, "charging") == 0) {
charge = SDL_TRUE;
}
} else if (strcmp(key, "remaining capacity") == 0) {
char *endptr = NULL;
const int cvt = (int) strtol(val, &endptr, 10);
if (*endptr == ' ') {
remaining = cvt;
}
}
}
Jun 30, 2009
Jun 30, 2009
165
Jun 30, 2009
Jun 30, 2009
166
ptr = &info[0];
Jun 30, 2009
Jun 30, 2009
167
while (make_proc_acpi_key_val(&ptr, &key, &val)) {
Jun 30, 2009
Jun 30, 2009
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
if (strcmp(key, "design capacity") == 0) {
char *endptr = NULL;
const int cvt = (int) strtol(val, &endptr, 10);
if (*endptr == ' ') {
maximum = cvt;
}
}
}
if ((maximum >= 0) && (remaining >= 0)) {
pct = (int) ((((float) remaining) / ((float) maximum)) * 100.0f);
if (pct < 0) {
pct = 0;
} else if (pct > 100) {
pct = 100;
}
}
/* !!! FIXME: calculate (secs). */
/*
* We pick the battery that claims to have the most minutes left.
* (failing a report of minutes, we'll take the highest percent.)
*/
if ((secs < 0) && (*seconds < 0)) {
if ((pct < 0) && (*percent < 0)) {
choose = SDL_TRUE; /* at least we know there's a battery. */
}
if (pct > *percent) {
choose = SDL_TRUE;
}
} else if (secs > *seconds) {
choose = SDL_TRUE;
}
if (choose) {
*seconds = secs;
*percent = pct;
*charging = charge;
}
Jun 30, 2009
Jun 30, 2009
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
}
static void
check_proc_acpi_ac_adapter(const char * node, SDL_bool * have_ac)
{
const char *base = proc_acpi_ac_adapter_path;
char state[256];
char *ptr = NULL;
char *key = NULL;
char *val = NULL;
SDL_bool charge = SDL_FALSE;
SDL_bool choose = SDL_FALSE;
SDL_bool is_ac = SDL_FALSE;
int maximum = -1;
int remaining = -1;
int secs = -1;
int pct = -1;
if (!load_acpi_file(base, node, "state", state, sizeof (state))) {
return;
}
Jun 30, 2009
Jun 30, 2009
229
Jun 30, 2009
Jun 30, 2009
230
231
232
233
234
235
236
237
ptr = &state[0];
while (make_proc_acpi_key_val(&ptr, &key, &val)) {
if (strcmp(key, "state") == 0) {
if (strcmp(val, "on-line") == 0) {
*have_ac = SDL_TRUE;
}
}
}
238
239
}
Jun 30, 2009
Jun 30, 2009
240
241
SDL_bool
Jun 10, 2009
Jun 10, 2009
242
243
SDL_GetPowerInfo_Linux_proc_acpi(SDL_PowerState * state,
int *seconds, int *percent)
Jun 30, 2009
Jun 30, 2009
245
246
247
struct dirent *dent = NULL;
DIR *dirp = NULL;
SDL_bool have_battery = SDL_FALSE;
Jun 30, 2009
Jun 30, 2009
248
SDL_bool have_ac = SDL_FALSE;
Jun 30, 2009
Jun 30, 2009
249
250
251
252
253
254
SDL_bool charging = SDL_FALSE;
*seconds = -1;
*percent = -1;
*state = SDL_POWERSTATE_UNKNOWN;
Jun 30, 2009
Jun 30, 2009
255
dirp = opendir(proc_acpi_battery_path);
Jun 30, 2009
Jun 30, 2009
256
257
if (dirp == NULL) {
return SDL_FALSE; /* can't use this interface. */
Jun 30, 2009
Jun 30, 2009
258
259
260
261
262
263
264
} else {
while ((dent = readdir(dirp)) != NULL) {
const char *node = dent->d_name;
check_proc_acpi_battery(node, &have_battery, &charging,
seconds, percent);
}
closedir(dirp);
Jun 30, 2009
Jun 30, 2009
266
Jun 30, 2009
Jun 30, 2009
267
268
269
270
271
272
273
274
275
dirp = opendir(proc_acpi_ac_adapter_path);
if (dirp == NULL) {
return SDL_FALSE; /* can't use this interface. */
} else {
while ((dent = readdir(dirp)) != NULL) {
const char *node = dent->d_name;
check_proc_acpi_ac_adapter(node, &have_ac);
}
closedir(dirp);
Jun 30, 2009
Jun 30, 2009
276
277
278
279
280
281
282
283
284
285
286
287
288
}
if (!have_battery) {
*state = SDL_POWERSTATE_NO_BATTERY;
} else if (charging) {
*state = SDL_POWERSTATE_CHARGING;
} else if (have_ac) {
*state = SDL_POWERSTATE_CHARGED;
} else {
*state = SDL_POWERSTATE_ON_BATTERY;
}
return SDL_TRUE; /* definitive answer. */
289
290
}
Jun 30, 2009
Jun 30, 2009
291
292
293
294
295
296
297
static SDL_bool
next_string(char **_ptr, char **_str)
{
char *ptr = *_ptr;
char *str = *_str;
Jun 10, 2009
Jun 10, 2009
298
while (*ptr == ' ') { /* skip any spaces... */
299
300
301
302
303
304
305
306
ptr++;
}
if (*ptr == '\0') {
return SDL_FALSE;
}
str = ptr;
Jun 30, 2009
Jun 30, 2009
307
while ((*ptr != ' ') && (*ptr != '\n') && (*ptr != '\0'))
308
309
310
311
312
313
314
315
316
317
318
319
320
321
ptr++;
if (*ptr != '\0')
*(ptr++) = '\0';
*_str = str;
*_ptr = ptr;
return SDL_TRUE;
}
static SDL_bool
int_string(char *str, int *val)
{
char *endptr = NULL;
Jun 30, 2009
Jun 30, 2009
322
*val = (int) strtol(str, &endptr, 0);
323
324
325
326
327
return ((*str != '\0') && (*endptr == '\0'));
}
/* http://lxr.linux.no/linux+v2.6.29/drivers/char/apm-emulation.c */
SDL_bool
Jun 10, 2009
Jun 10, 2009
328
329
SDL_GetPowerInfo_Linux_proc_apm(SDL_PowerState * state,
int *seconds, int *percent)
330
331
332
333
334
335
336
{
SDL_bool need_details = SDL_FALSE;
int ac_status = 0;
int battery_status = 0;
int battery_flag = 0;
int battery_percent = 0;
int battery_time = 0;
Jun 30, 2009
Jun 30, 2009
337
const int fd = open(proc_apm_path, O_RDONLY);
338
339
340
341
342
343
char buf[128];
char *ptr = &buf[0];
char *str = NULL;
ssize_t br;
if (fd == -1) {
Jun 10, 2009
Jun 10, 2009
344
return SDL_FALSE; /* can't use this interface. */
345
346
}
Jun 30, 2009
Jun 30, 2009
347
br = read(fd, buf, sizeof (buf) - 1);
348
349
350
351
352
353
close(fd);
if (br < 0) {
return SDL_FALSE;
}
Jun 10, 2009
Jun 10, 2009
354
355
buf[br] = '\0'; // null-terminate the string.
if (!next_string(&ptr, &str)) { /* driver version */
356
357
return SDL_FALSE;
}
Jun 10, 2009
Jun 10, 2009
358
if (!next_string(&ptr, &str)) { /* BIOS version */
359
360
return SDL_FALSE;
}
Jun 10, 2009
Jun 10, 2009
361
if (!next_string(&ptr, &str)) { /* APM flags */
362
363
364
return SDL_FALSE;
}
Jun 10, 2009
Jun 10, 2009
365
if (!next_string(&ptr, &str)) { /* AC line status */
366
367
368
369
370
return SDL_FALSE;
} else if (!int_string(str, &ac_status)) {
return SDL_FALSE;
}
Jun 10, 2009
Jun 10, 2009
371
if (!next_string(&ptr, &str)) { /* battery status */
372
373
374
375
return SDL_FALSE;
} else if (!int_string(str, &battery_status)) {
return SDL_FALSE;
}
Jun 10, 2009
Jun 10, 2009
376
if (!next_string(&ptr, &str)) { /* battery flag */
377
378
379
380
return SDL_FALSE;
} else if (!int_string(str, &battery_flag)) {
return SDL_FALSE;
}
Jun 10, 2009
Jun 10, 2009
381
if (!next_string(&ptr, &str)) { /* remaining battery life percent */
382
383
384
385
386
387
388
389
390
return SDL_FALSE;
}
if (str[strlen(str) - 1] == '%') {
str[strlen(str) - 1] = '\0';
}
if (!int_string(str, &battery_percent)) {
return SDL_FALSE;
}
Jun 10, 2009
Jun 10, 2009
391
if (!next_string(&ptr, &str)) { /* remaining battery life time */
392
393
394
395
396
return SDL_FALSE;
} else if (!int_string(str, &battery_time)) {
return SDL_FALSE;
}
Jun 10, 2009
Jun 10, 2009
397
if (!next_string(&ptr, &str)) { /* remaining battery life time units */
398
399
400
401
402
return SDL_FALSE;
} else if (strcmp(str, "min") == 0) {
battery_time *= 60;
}
Jun 10, 2009
Jun 10, 2009
403
if (battery_flag == 0xFF) { /* unknown state */
404
*state = SDL_POWERSTATE_UNKNOWN;
Jun 10, 2009
Jun 10, 2009
405
} else if (battery_flag & (1 << 7)) { /* no battery */
406
*state = SDL_POWERSTATE_NO_BATTERY;
Jun 10, 2009
Jun 10, 2009
407
} else if (battery_flag & (1 << 3)) { /* charging */
408
409
410
*state = SDL_POWERSTATE_CHARGING;
need_details = SDL_TRUE;
} else if (ac_status == 1) {
Jun 10, 2009
Jun 10, 2009
411
*state = SDL_POWERSTATE_CHARGED; /* on AC, not charging. */
412
413
414
415
416
417
418
419
420
421
422
423
need_details = SDL_TRUE;
} else {
*state = SDL_POWERSTATE_ON_BATTERY;
need_details = SDL_TRUE;
}
*percent = -1;
*seconds = -1;
if (need_details) {
const int pct = battery_percent;
const int secs = battery_time;
Jun 10, 2009
Jun 10, 2009
424
425
if (pct >= 0) { /* -1 == unknown */
*percent = (pct > 100) ? 100 : pct; /* clamp between 0%, 100% */
Jun 10, 2009
Jun 10, 2009
427
if (secs >= 0) { /* -1 == unknown */
428
429
430
431
432
433
434
435
436
437
438
*seconds = secs;
}
}
return SDL_TRUE;
}
#endif /* SDL_POWER_LINUX */
#endif /* SDL_POWER_DISABLED */
/* vi: set ts=4 sw=4 expandtab: */