23 #ifndef SDL_POWER_DISABLED
36 static const char *proc_apm_path =
"/proc/apm";
37 static const char *proc_acpi_battery_path =
"/proc/acpi/battery";
38 static const char *proc_acpi_ac_adapter_path =
"/proc/acpi/ac_adapter";
40 static int open_acpi_file(
const char *base,
const char *node,
const char *key)
42 const size_t pathlen = strlen(base) + strlen(node) + strlen(key) + 3;
43 char *
path = (
char *) alloca(pathlen);
48 snprintf(path, pathlen,
"%s/%s/%s", base, node, key);
49 return open(path, O_RDONLY);
54 load_acpi_file(
const char *base,
const char *node,
const char *key,
55 char *
buf,
size_t buflen)
58 const int fd = open_acpi_file(base, node, key);
62 br = read(fd, buf, buflen-1);
73 make_proc_acpi_key_val(
char **_ptr,
char **_key,
char **_val)
87 while ((*ptr !=
':') && (*ptr !=
'\0')) {
97 while ((*ptr ==
' ') && (*ptr !=
'\0')) {
107 while ((*ptr !=
'\n') && (*ptr !=
'\0')) {
120 check_proc_acpi_battery(
const char * node,
SDL_bool * have_battery,
121 SDL_bool * charging,
int *seconds,
int *percent)
123 const char *base = proc_acpi_battery_path;
136 if (!load_acpi_file(base, node,
"state", state,
sizeof (state))) {
138 }
else if (!load_acpi_file(base, node,
"info", info,
sizeof (info))) {
143 while (make_proc_acpi_key_val(&ptr, &key, &val)) {
144 if (strcmp(key,
"present") == 0) {
145 if (strcmp(val,
"yes") == 0) {
148 }
else if (strcmp(key,
"charging state") == 0) {
150 if (strcmp(val,
"charging/discharging") == 0) {
152 }
else if (strcmp(val,
"charging") == 0) {
155 }
else if (strcmp(key,
"remaining capacity") == 0) {
157 const int cvt = (
int) strtol(val, &endptr, 10);
158 if (*endptr ==
' ') {
165 while (make_proc_acpi_key_val(&ptr, &key, &val)) {
166 if (strcmp(key,
"design capacity") == 0) {
168 const int cvt = (
int) strtol(val, &endptr, 10);
169 if (*endptr ==
' ') {
175 if ((maximum >= 0) && (remaining >= 0)) {
176 pct = (
int) ((((
float) remaining) / ((
float) maximum)) * 100.0f);
179 }
else if (pct > 100) {
190 if ((secs < 0) && (*seconds < 0)) {
191 if ((pct < 0) && (*percent < 0)) {
194 if (pct > *percent) {
197 }
else if (secs > *seconds) {
209 check_proc_acpi_ac_adapter(
const char * node,
SDL_bool * have_ac)
211 const char *base = proc_acpi_ac_adapter_path;
217 if (!load_acpi_file(base, node,
"state", state,
sizeof (state))) {
222 while (make_proc_acpi_key_val(&ptr, &key, &val)) {
223 if (strcmp(key,
"state") == 0) {
224 if (strcmp(val,
"on-line") == 0) {
234 int *seconds,
int *percent)
236 struct dirent *dent =
NULL;
246 dirp = opendir(proc_acpi_battery_path);
250 while ((dent = readdir(dirp)) !=
NULL) {
251 const char *node = dent->d_name;
252 check_proc_acpi_battery(node, &have_battery, &charging,
258 dirp = opendir(proc_acpi_ac_adapter_path);
262 while ((dent = readdir(dirp)) !=
NULL) {
263 const char *node = dent->d_name;
264 check_proc_acpi_ac_adapter(node, &have_ac);
271 }
else if (charging) {
273 }
else if (have_ac) {
284 next_string(
char **_ptr,
char **_str)
289 while (*ptr ==
' ') {
298 while ((*ptr !=
' ') && (*ptr !=
'\n') && (*ptr !=
'\0'))
310 int_string(
char *str,
int *val)
313 *val = (
int) strtol(str, &endptr, 0);
314 return ((*str !=
'\0') && (*endptr ==
'\0'));
320 int *seconds,
int *percent)
324 int battery_status = 0;
325 int battery_flag = 0;
326 int battery_percent = 0;
327 int battery_time = 0;
328 const int fd = open(proc_apm_path, O_RDONLY);
338 br = read(fd, buf,
sizeof (buf) - 1);
346 if (!next_string(&ptr, &str)) {
349 if (!next_string(&ptr, &str)) {
352 if (!next_string(&ptr, &str)) {
356 if (!next_string(&ptr, &str)) {
358 }
else if (!int_string(str, &ac_status)) {
362 if (!next_string(&ptr, &str)) {
364 }
else if (!int_string(str, &battery_status)) {
367 if (!next_string(&ptr, &str)) {
369 }
else if (!int_string(str, &battery_flag)) {
372 if (!next_string(&ptr, &str)) {
375 if (str[strlen(str) - 1] ==
'%') {
376 str[strlen(str) - 1] =
'\0';
378 if (!int_string(str, &battery_percent)) {
382 if (!next_string(&ptr, &str)) {
384 }
else if (!int_string(str, &battery_time)) {
388 if (!next_string(&ptr, &str)) {
390 }
else if (strcmp(str,
"min") == 0) {
394 if (battery_flag == 0xFF) {
396 }
else if (battery_flag & (1 << 7)) {
398 }
else if (battery_flag & (1 << 3)) {
401 }
else if (ac_status == 1) {
412 const int pct = battery_percent;
413 const int secs = battery_time;
416 *percent = (pct > 100) ? 100 : pct;
GLuint const GLfloat * val
SDL_bool SDL_GetPowerInfo_Linux_proc_apm(SDL_PowerState *, int *, int *)
GLsizei const GLchar *const * path
SDL_PowerState
The basic state for the system's power supply.
GLenum GLuint GLsizei const GLchar * buf
SDL_bool SDL_GetPowerInfo_Linux_proc_acpi(SDL_PowerState *, int *, int *)