comparison src/power/linux/SDL_syspower.c @ 3186:51750b7a966f

indent
author Sam Lantinga <slouken@libsdl.org>
date Wed, 10 Jun 2009 13:34:20 +0000
parents c8b9c6d27476
children 790cbbda6429
comparison
equal deleted inserted replaced
3185:44d5474c2c8a 3186:51750b7a966f
32 #include <fcntl.h> 32 #include <fcntl.h>
33 33
34 #include "SDL_power.h" 34 #include "SDL_power.h"
35 35
36 SDL_bool 36 SDL_bool
37 SDL_GetPowerInfo_Linux_sys_power(SDL_PowerState *state, 37 SDL_GetPowerInfo_Linux_sys_power(SDL_PowerState * state,
38 int *seconds, int *percent) 38 int *seconds, int *percent)
39 { 39 {
40 return SDL_FALSE; /* !!! FIXME: write me. */ 40 return SDL_FALSE; /* !!! FIXME: write me. */
41 #if 0 41 #if 0
42 const int fd = open("/sys/power", O_RDONLY); 42 const int fd = open("/sys/power", O_RDONLY);
43 if (fd == -1) { 43 if (fd == -1) {
44 return SDL_FALSE; /* can't use this interface. */ 44 return SDL_FALSE; /* can't use this interface. */
45 } 45 }
46 return SDL_TRUE; 46 return SDL_TRUE;
47 #endif 47 #endif
48 } 48 }
49 49
50 SDL_bool 50 SDL_bool
51 SDL_GetPowerInfo_Linux_proc_acpi(SDL_PowerState *state, 51 SDL_GetPowerInfo_Linux_proc_acpi(SDL_PowerState * state,
52 int *seconds, int *percent) 52 int *seconds, int *percent)
53 { 53 {
54 return SDL_FALSE; /* !!! FIXME: write me. */ 54 return SDL_FALSE; /* !!! FIXME: write me. */
55 #if 0 55 #if 0
56 const int fd = open("/proc/acpi", O_RDONLY); 56 const int fd = open("/proc/acpi", O_RDONLY);
57 if (fd == -1) { 57 if (fd == -1) {
58 return SDL_FALSE; /* can't use this interface. */ 58 return SDL_FALSE; /* can't use this interface. */
59 } 59 }
60 return SDL_TRUE; 60 return SDL_TRUE;
61 #endif 61 #endif
62 } 62 }
63 63
65 next_string(char **_ptr, char **_str) 65 next_string(char **_ptr, char **_str)
66 { 66 {
67 char *ptr = *_ptr; 67 char *ptr = *_ptr;
68 char *str = *_str; 68 char *str = *_str;
69 69
70 while (*ptr == ' ') { /* skip any spaces... */ 70 while (*ptr == ' ') { /* skip any spaces... */
71 ptr++; 71 ptr++;
72 } 72 }
73 73
74 if (*ptr == '\0') { 74 if (*ptr == '\0') {
75 return SDL_FALSE; 75 return SDL_FALSE;
89 89
90 static SDL_bool 90 static SDL_bool
91 int_string(char *str, int *val) 91 int_string(char *str, int *val)
92 { 92 {
93 char *endptr = NULL; 93 char *endptr = NULL;
94 *val = (int) strtol(str+2, &endptr, 16); 94 *val = (int) strtol(str + 2, &endptr, 16);
95 return ((*str != '\0') && (*endptr == '\0')); 95 return ((*str != '\0') && (*endptr == '\0'));
96 } 96 }
97 97
98 /* http://lxr.linux.no/linux+v2.6.29/drivers/char/apm-emulation.c */ 98 /* http://lxr.linux.no/linux+v2.6.29/drivers/char/apm-emulation.c */
99 SDL_bool 99 SDL_bool
100 SDL_GetPowerInfo_Linux_proc_apm(SDL_PowerState *state, 100 SDL_GetPowerInfo_Linux_proc_apm(SDL_PowerState * state,
101 int *seconds, int *percent) 101 int *seconds, int *percent)
102 { 102 {
103 SDL_bool need_details = SDL_FALSE; 103 SDL_bool need_details = SDL_FALSE;
104 int ac_status = 0; 104 int ac_status = 0;
105 int battery_status = 0; 105 int battery_status = 0;
106 int battery_flag = 0; 106 int battery_flag = 0;
111 char *ptr = &buf[0]; 111 char *ptr = &buf[0];
112 char *str = NULL; 112 char *str = NULL;
113 ssize_t br; 113 ssize_t br;
114 114
115 if (fd == -1) { 115 if (fd == -1) {
116 return SDL_FALSE; /* can't use this interface. */ 116 return SDL_FALSE; /* can't use this interface. */
117 } 117 }
118 118
119 br = read(fd, buf, sizeof (buf) - 1); 119 br = read(fd, buf, sizeof(buf) - 1);
120 close(fd); 120 close(fd);
121 121
122 if (br < 0) { 122 if (br < 0) {
123 return SDL_FALSE; 123 return SDL_FALSE;
124 } 124 }
125 125
126 buf[br] = '\0'; // null-terminate the string. 126 buf[br] = '\0'; // null-terminate the string.
127 if (!next_string(&ptr, &str)) { /* driver version */ 127 if (!next_string(&ptr, &str)) { /* driver version */
128 return SDL_FALSE; 128 return SDL_FALSE;
129 } 129 }
130 if (!next_string(&ptr, &str)) { /* BIOS version */ 130 if (!next_string(&ptr, &str)) { /* BIOS version */
131 return SDL_FALSE; 131 return SDL_FALSE;
132 } 132 }
133 if (!next_string(&ptr, &str)) { /* APM flags */ 133 if (!next_string(&ptr, &str)) { /* APM flags */
134 return SDL_FALSE; 134 return SDL_FALSE;
135 } 135 }
136 136
137 if (!next_string(&ptr, &str)) { /* AC line status */ 137 if (!next_string(&ptr, &str)) { /* AC line status */
138 return SDL_FALSE; 138 return SDL_FALSE;
139 } else if (!int_string(str, &ac_status)) { 139 } else if (!int_string(str, &ac_status)) {
140 return SDL_FALSE; 140 return SDL_FALSE;
141 } 141 }
142 142
143 if (!next_string(&ptr, &str)) { /* battery status */ 143 if (!next_string(&ptr, &str)) { /* battery status */
144 return SDL_FALSE; 144 return SDL_FALSE;
145 } else if (!int_string(str, &battery_status)) { 145 } else if (!int_string(str, &battery_status)) {
146 return SDL_FALSE; 146 return SDL_FALSE;
147 } 147 }
148 if (!next_string(&ptr, &str)) { /* battery flag */ 148 if (!next_string(&ptr, &str)) { /* battery flag */
149 return SDL_FALSE; 149 return SDL_FALSE;
150 } else if (!int_string(str, &battery_flag)) { 150 } else if (!int_string(str, &battery_flag)) {
151 return SDL_FALSE; 151 return SDL_FALSE;
152 } 152 }
153 if (!next_string(&ptr, &str)) { /* remaining battery life percent */ 153 if (!next_string(&ptr, &str)) { /* remaining battery life percent */
154 return SDL_FALSE; 154 return SDL_FALSE;
155 } 155 }
156 if (str[strlen(str) - 1] == '%') { 156 if (str[strlen(str) - 1] == '%') {
157 str[strlen(str) - 1] = '\0'; 157 str[strlen(str) - 1] = '\0';
158 } 158 }
159 if (!int_string(str, &battery_percent)) { 159 if (!int_string(str, &battery_percent)) {
160 return SDL_FALSE; 160 return SDL_FALSE;
161 } 161 }
162 162
163 if (!next_string(&ptr, &str)) { /* remaining battery life time */ 163 if (!next_string(&ptr, &str)) { /* remaining battery life time */
164 return SDL_FALSE; 164 return SDL_FALSE;
165 } else if (!int_string(str, &battery_time)) { 165 } else if (!int_string(str, &battery_time)) {
166 return SDL_FALSE; 166 return SDL_FALSE;
167 } 167 }
168 168
169 if (!next_string(&ptr, &str)) { /* remaining battery life time units */ 169 if (!next_string(&ptr, &str)) { /* remaining battery life time units */
170 return SDL_FALSE; 170 return SDL_FALSE;
171 } else if (strcmp(str, "min") == 0) { 171 } else if (strcmp(str, "min") == 0) {
172 battery_time *= 60; 172 battery_time *= 60;
173 } 173 }
174 174
175 if (battery_flag == 0xFF) { /* unknown state */ 175 if (battery_flag == 0xFF) { /* unknown state */
176 *state = SDL_POWERSTATE_UNKNOWN; 176 *state = SDL_POWERSTATE_UNKNOWN;
177 } else if (battery_flag & (1 << 7)) { /* no battery */ 177 } else if (battery_flag & (1 << 7)) { /* no battery */
178 *state = SDL_POWERSTATE_NO_BATTERY; 178 *state = SDL_POWERSTATE_NO_BATTERY;
179 } else if (battery_flag & (1 << 3)) { /* charging */ 179 } else if (battery_flag & (1 << 3)) { /* charging */
180 *state = SDL_POWERSTATE_CHARGING; 180 *state = SDL_POWERSTATE_CHARGING;
181 need_details = SDL_TRUE; 181 need_details = SDL_TRUE;
182 } else if (ac_status == 1) { 182 } else if (ac_status == 1) {
183 *state = SDL_POWERSTATE_CHARGED; /* on AC, not charging. */ 183 *state = SDL_POWERSTATE_CHARGED; /* on AC, not charging. */
184 need_details = SDL_TRUE; 184 need_details = SDL_TRUE;
185 } else { 185 } else {
186 *state = SDL_POWERSTATE_ON_BATTERY; 186 *state = SDL_POWERSTATE_ON_BATTERY;
187 need_details = SDL_TRUE; 187 need_details = SDL_TRUE;
188 } 188 }
191 *seconds = -1; 191 *seconds = -1;
192 if (need_details) { 192 if (need_details) {
193 const int pct = battery_percent; 193 const int pct = battery_percent;
194 const int secs = battery_time; 194 const int secs = battery_time;
195 195
196 if (pct >= 0) { /* -1 == unknown */ 196 if (pct >= 0) { /* -1 == unknown */
197 *percent = (pct > 100) ? 100 : pct; /* clamp between 0%, 100% */ 197 *percent = (pct > 100) ? 100 : pct; /* clamp between 0%, 100% */
198 } 198 }
199 if (secs >= 0) { /* -1 == unknown */ 199 if (secs >= 0) { /* -1 == unknown */
200 *seconds = secs; 200 *seconds = secs;
201 } 201 }
202 } 202 }
203 203
204 return SDL_TRUE; 204 return SDL_TRUE;
206 206
207 #endif /* SDL_POWER_LINUX */ 207 #endif /* SDL_POWER_LINUX */
208 #endif /* SDL_POWER_DISABLED */ 208 #endif /* SDL_POWER_DISABLED */
209 209
210 /* vi: set ts=4 sw=4 expandtab: */ 210 /* vi: set ts=4 sw=4 expandtab: */
211