annotate src/power/linux/SDL_syspower.c @ 3206:f735edf338d3

Linux /proc/apm: handle hex and decimal (and octal!) values.
author Ryan C. Gordon <icculus@icculus.org>
date Tue, 30 Jun 2009 06:26:02 +0000
parents f9a4aa1e1b34
children 65fdb0961d7c
rev   line source
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
1 /*
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
2 SDL - Simple DirectMedia Layer
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
3 Copyright (C) 1997-2009 Sam Lantinga
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
4
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
5 This library is free software; you can redistribute it and/or
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
6 modify it under the terms of the GNU Lesser General Public
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
7 License as published by the Free Software Foundation; either
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
8 version 2.1 of the License, or (at your option) any later version.
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
9
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
10 This library is distributed in the hope that it will be useful,
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
13 Lesser General Public License for more details.
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
14
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
15 You should have received a copy of the GNU Lesser General Public
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
16 License along with this library; if not, write to the Free Software
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
18
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
19 Sam Lantinga
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
20 slouken@libsdl.org
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
21 */
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
22 #include "SDL_config.h"
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
23
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
24 #ifndef SDL_POWER_DISABLED
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
25 #ifdef SDL_POWER_LINUX
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
26
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
27 #include <stdio.h>
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
28 #include <unistd.h>
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
29
3174
c8b9c6d27476 Fixed some typos and added a couple of includes. All were need to get SDL_power to compile on Linux.
Bob Pendleton <bob@pendleton.com>
parents: 3170
diff changeset
30 #include <sys/types.h>
c8b9c6d27476 Fixed some typos and added a couple of includes. All were need to get SDL_power to compile on Linux.
Bob Pendleton <bob@pendleton.com>
parents: 3170
diff changeset
31 #include <sys/stat.h>
3203
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
32 #include <dirent.h>
3174
c8b9c6d27476 Fixed some typos and added a couple of includes. All were need to get SDL_power to compile on Linux.
Bob Pendleton <bob@pendleton.com>
parents: 3170
diff changeset
33 #include <fcntl.h>
c8b9c6d27476 Fixed some typos and added a couple of includes. All were need to get SDL_power to compile on Linux.
Bob Pendleton <bob@pendleton.com>
parents: 3170
diff changeset
34
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
35 #include "SDL_power.h"
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
36
3205
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
37 static const char *proc_acpi_battery_path = "/proc/acpi/battery";
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
38 static const char *proc_acpi_ac_adapter_path = "/proc/acpi/ac_adapter";
3203
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
39
3205
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
40 static int open_acpi_file(const char *base, const char *node, const char *key)
3203
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
41 {
3205
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
42 const size_t pathlen = strlen(base) + strlen(node) + strlen(key) + 3;
3203
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
43 char *path = (char *) alloca(pathlen);
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
44 if (path == NULL) {
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
45 return -1; /* oh well. */
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
46 }
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
47
3205
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
48 snprintf(path, pathlen, "%s/%s/%s", base, node, key);
3203
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
49 return open(path, O_RDONLY);
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
50 }
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
51
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
52
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
53 static SDL_bool
3205
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
54 load_acpi_file(const char *base, const char *node, const char *key,
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
55 char *buf, size_t buflen)
3203
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
56 {
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
57 ssize_t br = 0;
3205
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
58 const int fd = open_acpi_file(base, node, key);
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
59 if (fd == -1) {
3203
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
60 return SDL_FALSE;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
61 }
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
62 br = read(fd, buf, buflen-1);
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
63 close(fd);
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
64 if (br < 0) {
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
65 return SDL_FALSE;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
66 }
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
67 buf[br] = '\0'; // null-terminate the string.
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
68 return SDL_TRUE;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
69 }
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
70
3205
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
71
3203
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
72 static SDL_bool
3205
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
73 make_proc_acpi_key_val(char **_ptr, char **_key, char **_val)
3203
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
74 {
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
75 char *ptr = *_ptr;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
76
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
77 while (*ptr == ' ') {
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
78 ptr++; /* skip whitespace. */
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
79 }
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
80
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
81 if (*ptr == '\0') {
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
82 return SDL_FALSE; /* EOF. */
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
83 }
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
84
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
85 *_key = ptr;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
86
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
87 while ((*ptr != ':') && (*ptr != '\0')) {
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
88 ptr++;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
89 }
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
90
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
91 if (*ptr == '\0') {
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
92 return SDL_FALSE; /* (unexpected) EOF. */
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
93 }
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
94
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
95 *(ptr++) = '\0'; /* terminate the key. */
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
96
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
97 while ((*ptr == ' ') && (*ptr != '\0')) {
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
98 ptr++; /* skip whitespace. */
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
99 }
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
100
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
101 if (*ptr == '\0') {
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
102 return SDL_FALSE; /* (unexpected) EOF. */
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
103 }
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
104
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
105 *_val = ptr;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
106
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
107 while ((*ptr != '\n') && (*ptr != '\0')) {
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
108 ptr++;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
109 }
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
110
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
111 if (*ptr != '\0') {
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
112 *(ptr++) = '\0'; /* terminate the value. */
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
113 }
3203
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
114
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
115 *_ptr = ptr; /* store for next time. */
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
116 return SDL_TRUE;
3203
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
117 }
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
118
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
119 static void
3205
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
120 check_proc_acpi_battery(const char * node, SDL_bool * have_battery,
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
121 SDL_bool * charging, int *seconds, int *percent)
3203
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
122 {
3205
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
123 const char *base = proc_acpi_battery_path;
3203
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
124 char info[1024];
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
125 char state[1024];
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
126 char *ptr = NULL;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
127 char *key = NULL;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
128 char *val = NULL;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
129 SDL_bool charge = SDL_FALSE;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
130 SDL_bool choose = SDL_FALSE;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
131 SDL_bool is_ac = SDL_FALSE;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
132 int maximum = -1;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
133 int remaining = -1;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
134 int secs = -1;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
135 int pct = -1;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
136
3205
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
137 if (!load_acpi_file(base, node, "state", state, sizeof (state))) {
3203
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
138 return;
3205
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
139 } else if (!load_acpi_file(base, node, "info", info, sizeof (info))) {
3203
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
140 return;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
141 }
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
142
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
143 ptr = &state[0];
3205
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
144 while (make_proc_acpi_key_val(&ptr, &key, &val)) {
3203
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
145 if (strcmp(key, "present") == 0) {
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
146 if (strcmp(val, "yes") == 0) {
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
147 *have_battery = SDL_TRUE;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
148 }
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
149 } else if (strcmp(key, "charging state") == 0) {
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
150 /* !!! FIXME: what exactly _does_ charging/discharging mean? */
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
151 if (strcmp(val, "charging/discharging") == 0) {
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
152 charge = SDL_TRUE;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
153 } else if (strcmp(val, "charging") == 0) {
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
154 charge = SDL_TRUE;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
155 }
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
156 } else if (strcmp(key, "remaining capacity") == 0) {
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
157 char *endptr = NULL;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
158 const int cvt = (int) strtol(val, &endptr, 10);
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
159 if (*endptr == ' ') {
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
160 remaining = cvt;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
161 }
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
162 }
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
163 }
3205
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
164
3203
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
165 ptr = &info[0];
3205
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
166 while (make_proc_acpi_key_val(&ptr, &key, &val)) {
3203
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
167 if (strcmp(key, "design capacity") == 0) {
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
168 char *endptr = NULL;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
169 const int cvt = (int) strtol(val, &endptr, 10);
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
170 if (*endptr == ' ') {
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
171 maximum = cvt;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
172 }
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
173 }
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
174 }
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
175
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
176 if ((maximum >= 0) && (remaining >= 0)) {
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
177 pct = (int) ((((float) remaining) / ((float) maximum)) * 100.0f);
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
178 if (pct < 0) {
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
179 pct = 0;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
180 } else if (pct > 100) {
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
181 pct = 100;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
182 }
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
183 }
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
184
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
185 /* !!! FIXME: calculate (secs). */
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
186
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
187 /*
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
188 * We pick the battery that claims to have the most minutes left.
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
189 * (failing a report of minutes, we'll take the highest percent.)
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
190 */
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
191 if ((secs < 0) && (*seconds < 0)) {
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
192 if ((pct < 0) && (*percent < 0)) {
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
193 choose = SDL_TRUE; /* at least we know there's a battery. */
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
194 }
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
195 if (pct > *percent) {
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
196 choose = SDL_TRUE;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
197 }
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
198 } else if (secs > *seconds) {
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
199 choose = SDL_TRUE;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
200 }
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
201
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
202 if (choose) {
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
203 *seconds = secs;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
204 *percent = pct;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
205 *charging = charge;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
206 }
3205
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
207 }
3203
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
208
3205
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
209 static void
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
210 check_proc_acpi_ac_adapter(const char * node, SDL_bool * have_ac)
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
211 {
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
212 const char *base = proc_acpi_ac_adapter_path;
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
213 char state[256];
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
214 char *ptr = NULL;
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
215 char *key = NULL;
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
216 char *val = NULL;
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
217 SDL_bool charge = SDL_FALSE;
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
218 SDL_bool choose = SDL_FALSE;
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
219 SDL_bool is_ac = SDL_FALSE;
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
220 int maximum = -1;
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
221 int remaining = -1;
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
222 int secs = -1;
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
223 int pct = -1;
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
224
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
225 if (!load_acpi_file(base, node, "state", state, sizeof (state))) {
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
226 return;
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
227 }
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
228
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
229 ptr = &state[0];
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
230 while (make_proc_acpi_key_val(&ptr, &key, &val)) {
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
231 if (strcmp(key, "state") == 0) {
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
232 if (strcmp(val, "on-line") == 0) {
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
233 *have_ac = SDL_TRUE;
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
234 }
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
235 }
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
236 }
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
237 }
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
238
3205
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
239
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
240 SDL_bool
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
241 SDL_GetPowerInfo_Linux_proc_acpi(SDL_PowerState * state,
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
242 int *seconds, int *percent)
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
243 {
3203
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
244 struct dirent *dent = NULL;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
245 DIR *dirp = NULL;
3205
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
246 SDL_bool have_battery = SDL_FALSE;
3203
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
247 SDL_bool have_ac = SDL_FALSE;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
248 SDL_bool charging = SDL_FALSE;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
249
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
250 *seconds = -1;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
251 *percent = -1;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
252 *state = SDL_POWERSTATE_UNKNOWN;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
253
3205
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
254 dirp = opendir(proc_acpi_battery_path);
3203
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
255 if (dirp == NULL) {
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
256 return SDL_FALSE; /* can't use this interface. */
3205
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
257 } else {
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
258 while ((dent = readdir(dirp)) != NULL) {
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
259 const char *node = dent->d_name;
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
260 check_proc_acpi_battery(node, &have_battery, &charging,
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
261 seconds, percent);
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
262 }
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
263 closedir(dirp);
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
264 }
3203
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
265
3205
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
266 dirp = opendir(proc_acpi_ac_adapter_path);
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
267 if (dirp == NULL) {
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
268 return SDL_FALSE; /* can't use this interface. */
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
269 } else {
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
270 while ((dent = readdir(dirp)) != NULL) {
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
271 const char *node = dent->d_name;
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
272 check_proc_acpi_ac_adapter(node, &have_ac);
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
273 }
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
274 closedir(dirp);
3203
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
275 }
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
276
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
277 if (!have_battery) {
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
278 *state = SDL_POWERSTATE_NO_BATTERY;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
279 } else if (charging) {
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
280 *state = SDL_POWERSTATE_CHARGING;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
281 } else if (have_ac) {
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
282 *state = SDL_POWERSTATE_CHARGED;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
283 } else {
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
284 *state = SDL_POWERSTATE_ON_BATTERY;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
285 }
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
286
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
287 return SDL_TRUE; /* definitive answer. */
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
288 }
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
289
3203
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
290
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
291 static SDL_bool
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
292 next_string(char **_ptr, char **_str)
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
293 {
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
294 char *ptr = *_ptr;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
295 char *str = *_str;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
296
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
297 while (*ptr == ' ') { /* skip any spaces... */
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
298 ptr++;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
299 }
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
300
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
301 if (*ptr == '\0') {
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
302 return SDL_FALSE;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
303 }
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
304
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
305 str = ptr;
3203
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
306 while ((*ptr != ' ') && (*ptr != '\n') && (*ptr != '\0'))
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
307 ptr++;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
308
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
309 if (*ptr != '\0')
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
310 *(ptr++) = '\0';
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
311
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
312 *_str = str;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
313 *_ptr = ptr;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
314 return SDL_TRUE;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
315 }
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
316
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
317 static SDL_bool
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
318 int_string(char *str, int *val)
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
319 {
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
320 char *endptr = NULL;
3206
f735edf338d3 Linux /proc/apm: handle hex and decimal (and octal!) values.
Ryan C. Gordon <icculus@icculus.org>
parents: 3205
diff changeset
321 *val = (int) strtol(str, &endptr, 0);
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
322 return ((*str != '\0') && (*endptr == '\0'));
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
323 }
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
324
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
325 /* http://lxr.linux.no/linux+v2.6.29/drivers/char/apm-emulation.c */
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
326 SDL_bool
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
327 SDL_GetPowerInfo_Linux_proc_apm(SDL_PowerState * state,
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
328 int *seconds, int *percent)
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
329 {
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
330 SDL_bool need_details = SDL_FALSE;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
331 int ac_status = 0;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
332 int battery_status = 0;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
333 int battery_flag = 0;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
334 int battery_percent = 0;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
335 int battery_time = 0;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
336 const int fd = open("/proc/apm", O_RDONLY);
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
337 char buf[128];
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
338 char *ptr = &buf[0];
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
339 char *str = NULL;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
340 ssize_t br;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
341
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
342 if (fd == -1) {
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
343 return SDL_FALSE; /* can't use this interface. */
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
344 }
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
345
3203
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
346 br = read(fd, buf, sizeof (buf) - 1);
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
347 close(fd);
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
348
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
349 if (br < 0) {
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
350 return SDL_FALSE;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
351 }
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
352
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
353 buf[br] = '\0'; // null-terminate the string.
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
354 if (!next_string(&ptr, &str)) { /* driver version */
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
355 return SDL_FALSE;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
356 }
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
357 if (!next_string(&ptr, &str)) { /* BIOS version */
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
358 return SDL_FALSE;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
359 }
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
360 if (!next_string(&ptr, &str)) { /* APM flags */
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
361 return SDL_FALSE;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
362 }
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
363
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
364 if (!next_string(&ptr, &str)) { /* AC line status */
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
365 return SDL_FALSE;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
366 } else if (!int_string(str, &ac_status)) {
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
367 return SDL_FALSE;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
368 }
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
369
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
370 if (!next_string(&ptr, &str)) { /* battery status */
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
371 return SDL_FALSE;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
372 } else if (!int_string(str, &battery_status)) {
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
373 return SDL_FALSE;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
374 }
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
375 if (!next_string(&ptr, &str)) { /* battery flag */
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
376 return SDL_FALSE;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
377 } else if (!int_string(str, &battery_flag)) {
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
378 return SDL_FALSE;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
379 }
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
380 if (!next_string(&ptr, &str)) { /* remaining battery life percent */
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
381 return SDL_FALSE;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
382 }
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
383 if (str[strlen(str) - 1] == '%') {
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
384 str[strlen(str) - 1] = '\0';
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
385 }
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
386 if (!int_string(str, &battery_percent)) {
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
387 return SDL_FALSE;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
388 }
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
389
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
390 if (!next_string(&ptr, &str)) { /* remaining battery life time */
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
391 return SDL_FALSE;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
392 } else if (!int_string(str, &battery_time)) {
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
393 return SDL_FALSE;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
394 }
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
395
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
396 if (!next_string(&ptr, &str)) { /* remaining battery life time units */
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
397 return SDL_FALSE;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
398 } else if (strcmp(str, "min") == 0) {
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
399 battery_time *= 60;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
400 }
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
401
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
402 if (battery_flag == 0xFF) { /* unknown state */
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
403 *state = SDL_POWERSTATE_UNKNOWN;
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
404 } else if (battery_flag & (1 << 7)) { /* no battery */
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
405 *state = SDL_POWERSTATE_NO_BATTERY;
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
406 } else if (battery_flag & (1 << 3)) { /* charging */
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
407 *state = SDL_POWERSTATE_CHARGING;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
408 need_details = SDL_TRUE;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
409 } else if (ac_status == 1) {
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
410 *state = SDL_POWERSTATE_CHARGED; /* on AC, not charging. */
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
411 need_details = SDL_TRUE;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
412 } else {
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
413 *state = SDL_POWERSTATE_ON_BATTERY;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
414 need_details = SDL_TRUE;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
415 }
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
416
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
417 *percent = -1;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
418 *seconds = -1;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
419 if (need_details) {
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
420 const int pct = battery_percent;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
421 const int secs = battery_time;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
422
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
423 if (pct >= 0) { /* -1 == unknown */
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
424 *percent = (pct > 100) ? 100 : pct; /* clamp between 0%, 100% */
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
425 }
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
426 if (secs >= 0) { /* -1 == unknown */
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
427 *seconds = secs;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
428 }
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
429 }
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
430
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
431 return SDL_TRUE;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
432 }
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
433
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
434 #endif /* SDL_POWER_LINUX */
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
435 #endif /* SDL_POWER_DISABLED */
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
436
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
437 /* vi: set ts=4 sw=4 expandtab: */