Power: First shot at Linux /proc/acpi/battery support.
authorRyan C. Gordon <icculus@icculus.org>
Tue, 30 Jun 2009 03:50:30 +0000
changeset 3203790cbbda6429
parent 3202 3aa519a5c676
child 3204 f77f50add24f
Power: First shot at Linux /proc/acpi/battery support.

Untested, not even tried to compile yet.
src/power/linux/SDL_syspower.c
     1.1 --- a/src/power/linux/SDL_syspower.c	Mon Jun 29 19:54:43 2009 +0000
     1.2 +++ b/src/power/linux/SDL_syspower.c	Tue Jun 30 03:50:30 2009 +0000
     1.3 @@ -29,6 +29,7 @@
     1.4  
     1.5  #include <sys/types.h>
     1.6  #include <sys/stat.h>
     1.7 +#include <dirent.h>
     1.8  #include <fcntl.h>
     1.9  
    1.10  #include "SDL_power.h"
    1.11 @@ -38,29 +39,225 @@
    1.12                                   int *seconds, int *percent)
    1.13  {
    1.14      return SDL_FALSE;           /* !!! FIXME: write me. */
    1.15 -#if 0
    1.16 -    const int fd = open("/sys/power", O_RDONLY);
    1.17 +}
    1.18 +
    1.19 +
    1.20 +static const char *proc_acpi_path = "/proc/acpi/battery";
    1.21 +
    1.22 +static int open_acpi_file(const char *node, const char *key)
    1.23 +{
    1.24 +    const size_t pathlen = strlen(proc_acpi_path)+strlen(node)+strlen(key)+3;
    1.25 +    char *path = (char *) alloca(pathlen);
    1.26 +    if (path == NULL) {
    1.27 +        return -1;  /* oh well. */
    1.28 +    }
    1.29 +
    1.30 +    snprintf(path, pathlen, "%s/%s/%s", proc_acpi_path, node, key);
    1.31 +    return open(path, O_RDONLY);
    1.32 +}
    1.33 +
    1.34 +
    1.35 +static SDL_bool
    1.36 +load_acpi_file(const char *node, const char *key, char *buf, size_t buflen)
    1.37 +{
    1.38 +    ssize_t br = 0;
    1.39 +    const int fd = open_acpi_file(node, key);
    1.40      if (fd == -1) {
    1.41 -        return SDL_FALSE;       /* can't use this interface. */
    1.42 +        return SDL_FALSE;
    1.43      }
    1.44 +    br = read(fd, buf, buflen-1);
    1.45 +    close(fd);
    1.46 +    if (br < 0) {
    1.47 +        return SDL_FALSE;
    1.48 +    }
    1.49 +    buf[br] = '\0';             // null-terminate the string.
    1.50      return SDL_TRUE;
    1.51 -#endif
    1.52 +}
    1.53 +
    1.54 +static SDL_bool
    1.55 +make_acpi_key_val(char **_ptr, char **_key, char **_val)
    1.56 +{
    1.57 +    char *ptr = *_ptr;
    1.58 +
    1.59 +    while (*ptr == ' ') {
    1.60 +        ptr++;  /* skip whitespace. */
    1.61 +    }
    1.62 +
    1.63 +    if (*ptr == '\0') {
    1.64 +        return SDL_FALSE;  /* EOF. */
    1.65 +    }
    1.66 +
    1.67 +    *_key = ptr;
    1.68 +
    1.69 +    while ((*ptr != ':') && (*ptr != '\0')) {
    1.70 +        ptr++;
    1.71 +    }
    1.72 +
    1.73 +    if (*ptr == '\0') {
    1.74 +        return SDL_FALSE;  /* (unexpected) EOF. */
    1.75 +    }
    1.76 +
    1.77 +    *(ptr++) = '\0';  /* terminate the key. */
    1.78 +
    1.79 +    while ((*ptr == ' ') && (*ptr != '\0')) {
    1.80 +        ptr++;  /* skip whitespace. */
    1.81 +    }
    1.82 +
    1.83 +    if (*ptr == '\0') {
    1.84 +        return SDL_FALSE;  /* (unexpected) EOF. */
    1.85 +    }
    1.86 +
    1.87 +    *_val = ptr;
    1.88 +
    1.89 +    while ((*ptr != '\n') && (*ptr != '\0')) {
    1.90 +        ptr++;
    1.91 +    }
    1.92 +
    1.93 +    if (*ptr != '\0') {
    1.94 +        *(ptr++) = '\0';  /* terminate the value. */
    1.95 +    }
    1.96 +
    1.97 +    *_ptr = ptr;  /* store for next time. */
    1.98 +    return SDL_TRUE;
    1.99 +}
   1.100 +
   1.101 +static void
   1.102 +check_acpi(const char * fname, SDL_bool * have_ac, SDL_bool * have_battery,
   1.103 +           SDL_bool * charging, int *seconds, int *percent)
   1.104 +{
   1.105 +    int fd = -1;
   1.106 +    char info[1024];
   1.107 +    char state[1024];
   1.108 +    ssize_t br = 0;
   1.109 +    char *ptr = NULL;
   1.110 +    char *key = NULL;
   1.111 +    char *val = NULL;
   1.112 +    SDL_bool charge = SDL_FALSE;
   1.113 +    SDL_bool choose = SDL_FALSE;
   1.114 +    SDL_bool is_ac = SDL_FALSE;
   1.115 +    int maximum = -1;
   1.116 +    int remaining = -1;
   1.117 +    int secs = -1;
   1.118 +    int pct = -1;
   1.119 +
   1.120 +    if (!load_acpi_file(fname, "state", state, sizeof (state))) {
   1.121 +        return;
   1.122 +    } else if (!load_acpi_file(fname, "info", info, sizeof (info))) {
   1.123 +        return;
   1.124 +    }
   1.125 +
   1.126 +    ptr = &state[0];
   1.127 +    while (make_acpi_key_val(&ptr, &key, &val)) {
   1.128 +        if (strcmp(key, "present") == 0) {
   1.129 +            if (strcmp(val, "yes") == 0) {
   1.130 +                *have_battery = SDL_TRUE;
   1.131 +            }
   1.132 +        } else if (strcmp(key, "charging state") == 0) {
   1.133 +            /* !!! FIXME: what exactly _does_ charging/discharging mean? */
   1.134 +            if (strcmp(val, "charging/discharging") == 0) {
   1.135 +                *have_ac = is_ac = SDL_TRUE;
   1.136 +                charge = SDL_TRUE;
   1.137 +            } else if (strcmp(val, "charging") == 0) {
   1.138 +                *have_ac = is_ac = SDL_TRUE;
   1.139 +                charge = SDL_TRUE;
   1.140 +            } else if (strcmp(val, "charged") == 0) {
   1.141 +                /* !!! FIXME: maybe another battery is discharging,
   1.142 +                   !!! FIXME:   instead of AC connection. */
   1.143 +                *have_ac = is_ac = SDL_TRUE;
   1.144 +                charge = SDL_TRUE;
   1.145 +            }
   1.146 +        } else if (strcmp(key, "remaining capacity") == 0) {
   1.147 +            char *endptr = NULL;
   1.148 +            const int cvt = (int) strtol(val, &endptr, 10);
   1.149 +            if (*endptr == ' ') {
   1.150 +                remaining = cvt;
   1.151 +            }
   1.152 +        }
   1.153 +    }
   1.154 +    
   1.155 +    ptr = &info[0];
   1.156 +    while (make_acpi_key_val(&ptr, &key, &val)) {
   1.157 +        if (strcmp(key, "design capacity") == 0) {
   1.158 +            char *endptr = NULL;
   1.159 +            const int cvt = (int) strtol(val, &endptr, 10);
   1.160 +            if (*endptr == ' ') {
   1.161 +                maximum = cvt;
   1.162 +            }
   1.163 +        }
   1.164 +    }
   1.165 +
   1.166 +    if ((maximum >= 0) && (remaining >= 0)) {
   1.167 +        pct = (int) ((((float) remaining) / ((float) maximum)) * 100.0f);
   1.168 +        if (pct < 0) {
   1.169 +            pct = 0;
   1.170 +        } else if (pct > 100) {
   1.171 +            pct = 100;
   1.172 +        }
   1.173 +    }
   1.174 +
   1.175 +    /* !!! FIXME: calculate (secs). */
   1.176 +
   1.177 +    /*
   1.178 +     * We pick the battery that claims to have the most minutes left.
   1.179 +     *  (failing a report of minutes, we'll take the highest percent.)
   1.180 +     */
   1.181 +    if ((secs < 0) && (*seconds < 0)) {
   1.182 +        if ((pct < 0) && (*percent < 0)) {
   1.183 +            choose = SDL_TRUE;  /* at least we know there's a battery. */
   1.184 +        }
   1.185 +        if (pct > *percent) {
   1.186 +            choose = SDL_TRUE;
   1.187 +        }
   1.188 +    } else if (secs > *seconds) {
   1.189 +        choose = SDL_TRUE;
   1.190 +    }
   1.191 +
   1.192 +    if (choose) {
   1.193 +        *seconds = secs;
   1.194 +        *percent = pct;
   1.195 +        *charging = charge;
   1.196 +    }
   1.197 +
   1.198  }
   1.199  
   1.200  SDL_bool
   1.201  SDL_GetPowerInfo_Linux_proc_acpi(SDL_PowerState * state,
   1.202                                   int *seconds, int *percent)
   1.203  {
   1.204 -    return SDL_FALSE;           /* !!! FIXME: write me. */
   1.205 -#if 0
   1.206 -    const int fd = open("/proc/acpi", O_RDONLY);
   1.207 -    if (fd == -1) {
   1.208 -        return SDL_FALSE;       /* can't use this interface. */
   1.209 +    struct dirent *dent = NULL;
   1.210 +    DIR *dirp = NULL;
   1.211 +    SDL_bool have_ac = SDL_FALSE;
   1.212 +    SDL_bool have_battery = SDL_FALSE;
   1.213 +    SDL_bool charging = SDL_FALSE;
   1.214 +
   1.215 +    *seconds = -1;
   1.216 +    *percent = -1;
   1.217 +    *state = SDL_POWERSTATE_UNKNOWN;
   1.218 +
   1.219 +    dirp = opendir(proc_acpi_path);
   1.220 +    if (dirp == NULL) {
   1.221 +        return SDL_FALSE;  /* can't use this interface. */
   1.222      }
   1.223 -    return SDL_TRUE;
   1.224 -#endif
   1.225 +
   1.226 +    while ((dent = readdir(dirp)) != NULL) {
   1.227 +        const char *name = dent->d_name;
   1.228 +        check_acpi(name, &have_ac, &have_battery, &charging, seconds, percent);
   1.229 +    }
   1.230 +
   1.231 +    if (!have_battery) {
   1.232 +        *state = SDL_POWERSTATE_NO_BATTERY;
   1.233 +    } else if (charging) {
   1.234 +        *state = SDL_POWERSTATE_CHARGING;
   1.235 +    } else if (have_ac) {
   1.236 +        *state = SDL_POWERSTATE_CHARGED;
   1.237 +    } else {
   1.238 +        *state = SDL_POWERSTATE_ON_BATTERY;
   1.239 +    }
   1.240 +
   1.241 +    return SDL_TRUE;   /* definitive answer. */
   1.242  }
   1.243  
   1.244 +
   1.245  static SDL_bool
   1.246  next_string(char **_ptr, char **_str)
   1.247  {
   1.248 @@ -76,7 +273,7 @@
   1.249      }
   1.250  
   1.251      str = ptr;
   1.252 -    while ((*ptr != ' ') && (*ptr != '\0'))
   1.253 +    while ((*ptr != ' ') && (*ptr != '\n') && (*ptr != '\0'))
   1.254          ptr++;
   1.255  
   1.256      if (*ptr != '\0')
   1.257 @@ -116,7 +313,7 @@
   1.258          return SDL_FALSE;       /* can't use this interface. */
   1.259      }
   1.260  
   1.261 -    br = read(fd, buf, sizeof(buf) - 1);
   1.262 +    br = read(fd, buf, sizeof (buf) - 1);
   1.263      close(fd);
   1.264  
   1.265      if (br < 0) {