annotate src/power/linux/SDL_syspower.c @ 4976:3811132c584f

Added information about starting a command shell on the device
author Sam Lantinga <slouken@libsdl.org>
date Wed, 12 Jan 2011 12:40:11 -0800
parents f7b03b6838cb
children b530ef003506
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
3697
f7b03b6838cb Fixed bug #926
Sam Lantinga <slouken@libsdl.org>
parents: 3207
diff changeset
3 Copyright (C) 1997-2010 Sam Lantinga
3170
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
3207
65fdb0961d7c Moved hardcoding of "/proc/apm" elsewhere.
Ryan C. Gordon <icculus@icculus.org>
parents: 3206
diff changeset
37 static const char *proc_apm_path = "/proc/apm";
3205
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_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
39 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
40
3205
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
41 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
42 {
3205
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
43 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
44 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
45 if (path == NULL) {
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
46 return -1; /* oh well. */
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
47 }
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
48
3205
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
49 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
50 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
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
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
54 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
55 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
56 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
57 {
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
58 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
59 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
60 if (fd == -1) {
3203
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
61 return SDL_FALSE;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
62 }
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
63 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
64 close(fd);
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
65 if (br < 0) {
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
66 return SDL_FALSE;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
67 }
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
68 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
69 return SDL_TRUE;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
70 }
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
71
3205
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
72
3203
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
73 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
74 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
75 {
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
76 char *ptr = *_ptr;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
77
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
78 while (*ptr == ' ') {
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
79 ptr++; /* skip whitespace. */
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
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
82 if (*ptr == '\0') {
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
83 return SDL_FALSE; /* EOF. */
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
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
86 *_key = ptr;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
87
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
88 while ((*ptr != ':') && (*ptr != '\0')) {
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
89 ptr++;
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
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
92 if (*ptr == '\0') {
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
93 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
94 }
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
95
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
96 *(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
97
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
98 while ((*ptr == ' ') && (*ptr != '\0')) {
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
99 ptr++; /* skip whitespace. */
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
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
102 if (*ptr == '\0') {
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
103 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
104 }
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
105
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
106 *_val = ptr;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
107
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
108 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
109 ptr++;
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
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
112 if (*ptr != '\0') {
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
113 *(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
114 }
3203
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
115
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
116 *_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
117 return SDL_TRUE;
3203
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
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
120 static void
3205
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
121 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
122 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
123 {
3205
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
124 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
125 char info[1024];
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
126 char state[1024];
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
127 char *ptr = NULL;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
128 char *key = NULL;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
129 char *val = NULL;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
130 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
131 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
132 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
133 int maximum = -1;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
134 int remaining = -1;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
135 int secs = -1;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
136 int pct = -1;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
137
3205
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
138 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
139 return;
3205
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
140 } 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
141 return;
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
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
144 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
145 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
146 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
147 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
148 *have_battery = SDL_TRUE;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
149 }
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
150 } 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
151 /* !!! 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
152 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
153 charge = SDL_TRUE;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
154 } 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
155 charge = SDL_TRUE;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
156 }
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
157 } 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
158 char *endptr = NULL;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
159 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
160 if (*endptr == ' ') {
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
161 remaining = cvt;
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 }
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
164 }
3205
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
165
3203
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
166 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
167 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
168 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
169 char *endptr = NULL;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
170 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
171 if (*endptr == ' ') {
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
172 maximum = cvt;
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
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
177 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
178 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
179 if (pct < 0) {
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
180 pct = 0;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
181 } else if (pct > 100) {
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
182 pct = 100;
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
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
186 /* !!! FIXME: calculate (secs). */
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 /*
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
189 * 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
190 * (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
191 */
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
192 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
193 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
194 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
195 }
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
196 if (pct > *percent) {
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
197 choose = SDL_TRUE;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
198 }
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
199 } else if (secs > *seconds) {
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
200 choose = SDL_TRUE;
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
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
203 if (choose) {
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
204 *seconds = secs;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
205 *percent = pct;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
206 *charging = charge;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
207 }
3205
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
208 }
3203
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
209
3205
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
210 static void
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
211 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
212 {
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
213 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
214 char state[256];
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
215 char *ptr = NULL;
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
216 char *key = NULL;
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
217 char *val = NULL;
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
218 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
219 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
220 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
221 int maximum = -1;
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
222 int remaining = -1;
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
223 int secs = -1;
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
224 int pct = -1;
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
225
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
226 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
227 return;
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
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
230 ptr = &state[0];
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
231 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
232 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
233 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
234 *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
235 }
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
236 }
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
237 }
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
238 }
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
239
3205
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
240
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
241 SDL_bool
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
242 SDL_GetPowerInfo_Linux_proc_acpi(SDL_PowerState * state,
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
243 int *seconds, int *percent)
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
244 {
3203
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
245 struct dirent *dent = NULL;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
246 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
247 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
248 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
249 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
250
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
251 *seconds = -1;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
252 *percent = -1;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
253 *state = SDL_POWERSTATE_UNKNOWN;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
254
3205
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
255 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
256 if (dirp == NULL) {
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
257 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
258 } else {
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
259 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
260 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
261 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
262 seconds, percent);
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
263 }
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
264 closedir(dirp);
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
265 }
3203
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
266
3205
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
267 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
268 if (dirp == NULL) {
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
269 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
270 } else {
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
271 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
272 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
273 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
274 }
f9a4aa1e1b34 Linux /proc/acpi: check for ac_adapter states, other cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 3204
diff changeset
275 closedir(dirp);
3203
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
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
278 if (!have_battery) {
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
279 *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
280 } else if (charging) {
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
281 *state = SDL_POWERSTATE_CHARGING;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
282 } else if (have_ac) {
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
283 *state = SDL_POWERSTATE_CHARGED;
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
284 } else {
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
285 *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
286 }
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
287
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
288 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
289 }
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
290
3203
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
291
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
292 static SDL_bool
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
293 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
294 {
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
295 char *ptr = *_ptr;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
296 char *str = *_str;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
297
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
298 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
299 ptr++;
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
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
302 if (*ptr == '\0') {
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
303 return SDL_FALSE;
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
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
306 str = ptr;
3203
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
307 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
308 ptr++;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
309
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
310 if (*ptr != '\0')
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
311 *(ptr++) = '\0';
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
312
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
313 *_str = str;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
314 *_ptr = ptr;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
315 return SDL_TRUE;
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
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
318 static SDL_bool
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
319 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
320 {
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
321 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
322 *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
323 return ((*str != '\0') && (*endptr == '\0'));
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
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
326 /* 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
327 SDL_bool
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
328 SDL_GetPowerInfo_Linux_proc_apm(SDL_PowerState * state,
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
329 int *seconds, int *percent)
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
330 {
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
331 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
332 int ac_status = 0;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
333 int battery_status = 0;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
334 int battery_flag = 0;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
335 int battery_percent = 0;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
336 int battery_time = 0;
3207
65fdb0961d7c Moved hardcoding of "/proc/apm" elsewhere.
Ryan C. Gordon <icculus@icculus.org>
parents: 3206
diff changeset
337 const int fd = open(proc_apm_path, O_RDONLY);
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
338 char buf[128];
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
339 char *ptr = &buf[0];
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
340 char *str = NULL;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
341 ssize_t br;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
342
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
343 if (fd == -1) {
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
344 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
345 }
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
346
3203
790cbbda6429 Power: First shot at Linux /proc/acpi/battery support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3186
diff changeset
347 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
348 close(fd);
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
349
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
350 if (br < 0) {
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
351 return SDL_FALSE;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
352 }
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
353
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
354 buf[br] = '\0'; // null-terminate the string.
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
355 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
356 return SDL_FALSE;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
357 }
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
358 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
359 return SDL_FALSE;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
360 }
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
361 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
362 return SDL_FALSE;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
363 }
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
364
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
365 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
366 return SDL_FALSE;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
367 } 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
368 return SDL_FALSE;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
369 }
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
370
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
371 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
372 return SDL_FALSE;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
373 } 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
374 return SDL_FALSE;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
375 }
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
376 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
377 return SDL_FALSE;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
378 } 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
379 return SDL_FALSE;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
380 }
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
381 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
382 return SDL_FALSE;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
383 }
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
384 if (str[strlen(str) - 1] == '%') {
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
385 str[strlen(str) - 1] = '\0';
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
386 }
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
387 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
388 return SDL_FALSE;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
389 }
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
390
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
391 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
392 return SDL_FALSE;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
393 } 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
394 return SDL_FALSE;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
395 }
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
396
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
397 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
398 return SDL_FALSE;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
399 } 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
400 battery_time *= 60;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
401 }
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
402
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
403 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
404 *state = SDL_POWERSTATE_UNKNOWN;
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
405 } 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
406 *state = SDL_POWERSTATE_NO_BATTERY;
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
407 } 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
408 *state = SDL_POWERSTATE_CHARGING;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
409 need_details = SDL_TRUE;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
410 } else if (ac_status == 1) {
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
411 *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
412 need_details = SDL_TRUE;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
413 } else {
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
414 *state = SDL_POWERSTATE_ON_BATTERY;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
415 need_details = SDL_TRUE;
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
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
418 *percent = -1;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
419 *seconds = -1;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
420 if (need_details) {
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
421 const int pct = battery_percent;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
422 const int secs = battery_time;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
423
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
424 if (pct >= 0) { /* -1 == unknown */
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
425 *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
426 }
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
427 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
428 *seconds = secs;
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
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
432 return SDL_TRUE;
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
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
435 #endif /* SDL_POWER_LINUX */
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
436 #endif /* SDL_POWER_DISABLED */
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
437
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
438 /* vi: set ts=4 sw=4 expandtab: */