src/power/linux/SDL_syspower.c
changeset 3170 b7a48f533966
child 3174 c8b9c6d27476
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/power/linux/SDL_syspower.c	Sun Jun 07 06:06:35 2009 +0000
     1.3 @@ -0,0 +1,207 @@
     1.4 +/*
     1.5 +    SDL - Simple DirectMedia Layer
     1.6 +    Copyright (C) 1997-2009 Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Lesser General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2.1 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Lesser General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Lesser General Public
    1.19 +    License along with this library; if not, write to the Free Software
    1.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    slouken@libsdl.org
    1.24 +*/
    1.25 +#include "SDL_config.h"
    1.26 +
    1.27 +#ifndef SDL_POWER_DISABLED
    1.28 +#ifdef SDL_POWER_LINUX
    1.29 +
    1.30 +#include <stdio.h>
    1.31 +#include <unistd.h>
    1.32 +
    1.33 +#include "SDL_power.h"
    1.34 +
    1.35 +SDL_bool
    1.36 +SDL_GetPowerInfo_Linux_sys_power(SDL_PowerState *state,
    1.37 +                                 int *seconds, int *percent)
    1.38 +{
    1.39 +    return SDL_FALSE;  /* !!! FIXME: write me. */
    1.40 +#if 0
    1.41 +    const int fd = open("/sys/power", O_RDONLY);
    1.42 +    if (fd == -1) {
    1.43 +        return SDL_FALSE;  /* can't use this interface. */
    1.44 +    }
    1.45 +    return SDL_TRUE;
    1.46 +#endif
    1.47 +}
    1.48 +
    1.49 +SDL_bool
    1.50 +SDL_GetPowerInfo_Linux_sys_proc_acpi(SDL_PowerState *state,
    1.51 +                                     int *seconds, int *percent)
    1.52 +{
    1.53 +    return SDL_FALSE;  /* !!! FIXME: write me. */
    1.54 +#if 0
    1.55 +    const int fd = open("/proc/acpi", O_RDONLY);
    1.56 +    if (fd == -1) {
    1.57 +        return SDL_FALSE;  /* can't use this interface. */
    1.58 +    }
    1.59 +    return SDL_TRUE;
    1.60 +#endif
    1.61 +}
    1.62 +
    1.63 +static SDL_bool
    1.64 +next_string(char **_ptr, char **_str)
    1.65 +{
    1.66 +    char *ptr = *_ptr;
    1.67 +    char *str = *_str;
    1.68 +
    1.69 +    while (*ptr == ' ') {  /* skip any spaces... */
    1.70 +        ptr++;
    1.71 +    }
    1.72 +
    1.73 +    if (*ptr == '\0') {
    1.74 +        return SDL_FALSE;
    1.75 +    }
    1.76 +
    1.77 +    str = ptr;
    1.78 +    while ((*ptr != ' ') && (*ptr != '\0'))
    1.79 +        ptr++;
    1.80 +
    1.81 +    if (*ptr != '\0')
    1.82 +        *(ptr++) = '\0';
    1.83 +
    1.84 +    *_str = str;
    1.85 +    *_ptr = ptr;
    1.86 +    return SDL_TRUE;
    1.87 +}
    1.88 +
    1.89 +static SDL_bool
    1.90 +int_string(char *str, int *val)
    1.91 +{
    1.92 +    char *endptr = NULL;
    1.93 +    *val = (int) strtol(str+2, &endptr, 16);
    1.94 +    return ((*str != '\0') && (*endptr == '\0'));
    1.95 +}
    1.96 +
    1.97 +/* http://lxr.linux.no/linux+v2.6.29/drivers/char/apm-emulation.c */
    1.98 +SDL_bool
    1.99 +SDL_GetPowerInfo_Linux_sys_proc_apm(SDL_PowerState *state,
   1.100 +                                    int *seconds, int *percent)
   1.101 +{
   1.102 +    SDL_bool need_details = SDL_FALSE;
   1.103 +    int ac_status = 0;
   1.104 +    int battery_status = 0;
   1.105 +    int battery_flag = 0;
   1.106 +    int battery_percent = 0;
   1.107 +    int battery_time = 0;
   1.108 +    const int fd = open("/proc/apm", O_RDONLY);
   1.109 +    char buf[128];
   1.110 +    char *ptr = &buf[0];
   1.111 +    char *str = NULL;
   1.112 +    ssize_t br;
   1.113 +
   1.114 +    if (fd == -1) {
   1.115 +        return SDL_FALSE;  /* can't use this interface. */
   1.116 +    }
   1.117 +
   1.118 +    br = read(fd, buf, sizeof (buf) - 1);
   1.119 +    close(fd);
   1.120 +
   1.121 +    if (br < 0) {
   1.122 +        return SDL_FALSE;
   1.123 +    }
   1.124 +
   1.125 +    buf[br] = '\0';  // null-terminate the string.
   1.126 +    if (!next_string(&ptr, &str)) {  /* driver version */
   1.127 +        return SDL_FALSE;
   1.128 +    }
   1.129 +    if (!next_string(&ptr, &str)) {  /* BIOS version */
   1.130 +        return SDL_FALSE;
   1.131 +    }
   1.132 +    if (!next_string(&ptr, &str)) {  /* APM flags */
   1.133 +        return SDL_FALSE;
   1.134 +    }
   1.135 +
   1.136 +    if (!next_string(&ptr, &str)) {  /* AC line status */
   1.137 +        return SDL_FALSE;
   1.138 +    } else if (!int_string(str, &ac_status)) {
   1.139 +        return SDL_FALSE;
   1.140 +    }
   1.141 +
   1.142 +    if (!next_string(&ptr, &str)) {  /* battery status */
   1.143 +        return SDL_FALSE;
   1.144 +    } else if (!int_string(str, &battery_status)) {
   1.145 +        return SDL_FALSE;
   1.146 +    }
   1.147 +    if (!next_string(&ptr, &str)) {  /* battery flag */
   1.148 +        return SDL_FALSE;
   1.149 +    } else if (!int_string(str, &battery_flag)) {
   1.150 +        return SDL_FALSE;
   1.151 +    }
   1.152 +    if (!next_string(&ptr, &str)) {  /* remaining battery life percent */
   1.153 +        return SDL_FALSE;
   1.154 +    }
   1.155 +    if (str[strlen(str) - 1] == '%') {
   1.156 +        str[strlen(str) - 1] = '\0';
   1.157 +    }
   1.158 +    if (!int_string(str, &battery_percent)) {
   1.159 +        return SDL_FALSE;
   1.160 +    }
   1.161 +
   1.162 +    if (!next_string(&ptr, &str)) {  /* remaining battery life time */
   1.163 +        return SDL_FALSE;
   1.164 +    } else if (!int_string(str, &battery_time)) {
   1.165 +        return SDL_FALSE;
   1.166 +    }
   1.167 +
   1.168 +    if (!next_string(&ptr, &str)) {  /* remaining battery life time units */
   1.169 +        return SDL_FALSE;
   1.170 +    } else if (strcmp(str, "min") == 0) {
   1.171 +        battery_time *= 60;
   1.172 +    }
   1.173 +
   1.174 +    if (battery_flag == 0xFF) {  /* unknown state */
   1.175 +        *state = SDL_POWERSTATE_UNKNOWN;
   1.176 +    } else if (battery_flag & (1 << 7)) {  /* no battery */
   1.177 +        *state = SDL_POWERSTATE_NO_BATTERY;
   1.178 +    } else if (battery_flag & (1 << 3)) {  /* charging */
   1.179 +        *state = SDL_POWERSTATE_CHARGING;
   1.180 +        need_details = SDL_TRUE;
   1.181 +    } else if (ac_status == 1) {
   1.182 +        *state = SDL_POWERSTATE_CHARGED;  /* on AC, not charging. */
   1.183 +        need_details = SDL_TRUE;
   1.184 +    } else {
   1.185 +        *state = SDL_POWERSTATE_ON_BATTERY;
   1.186 +        need_details = SDL_TRUE;
   1.187 +    }
   1.188 +
   1.189 +    *percent = -1;
   1.190 +    *seconds = -1;
   1.191 +    if (need_details) {
   1.192 +        const int pct = battery_percent;
   1.193 +        const int secs = battery_time;
   1.194 +
   1.195 +        if (pct >= 0) {  /* -1 == unknown */
   1.196 +            *percent = (pct > 100) ? 100 : pct;  /* clamp between 0%, 100% */
   1.197 +        }
   1.198 +        if (secs >= 0) {  /* -1 == unknown */
   1.199 +            *seconds = secs;
   1.200 +        }
   1.201 +    }
   1.202 +
   1.203 +    return SDL_TRUE;
   1.204 +}
   1.205 +
   1.206 +#endif /* SDL_POWER_LINUX */
   1.207 +#endif /* SDL_POWER_DISABLED */
   1.208 +
   1.209 +/* vi: set ts=4 sw=4 expandtab: */
   1.210 +