src/power/linux/SDL_syspower.c
changeset 3205 f9a4aa1e1b34
parent 3204 f77f50add24f
child 3206 f735edf338d3
     1.1 --- a/src/power/linux/SDL_syspower.c	Tue Jun 30 05:27:11 2009 +0000
     1.2 +++ b/src/power/linux/SDL_syspower.c	Tue Jun 30 05:44:00 2009 +0000
     1.3 @@ -34,26 +34,28 @@
     1.4  
     1.5  #include "SDL_power.h"
     1.6  
     1.7 -static const char *proc_acpi_path = "/proc/acpi/battery";
     1.8 +static const char *proc_acpi_battery_path = "/proc/acpi/battery";
     1.9 +static const char *proc_acpi_ac_adapter_path = "/proc/acpi/ac_adapter";
    1.10  
    1.11 -static int open_acpi_file(const char *node, const char *key)
    1.12 +static int open_acpi_file(const char *base, const char *node, const char *key)
    1.13  {
    1.14 -    const size_t pathlen = strlen(proc_acpi_path)+strlen(node)+strlen(key)+3;
    1.15 +    const size_t pathlen = strlen(base) + strlen(node) + strlen(key) + 3;
    1.16      char *path = (char *) alloca(pathlen);
    1.17      if (path == NULL) {
    1.18          return -1;  /* oh well. */
    1.19      }
    1.20  
    1.21 -    snprintf(path, pathlen, "%s/%s/%s", proc_acpi_path, node, key);
    1.22 +    snprintf(path, pathlen, "%s/%s/%s", base, node, key);
    1.23      return open(path, O_RDONLY);
    1.24  }
    1.25  
    1.26  
    1.27  static SDL_bool
    1.28 -load_acpi_file(const char *node, const char *key, char *buf, size_t buflen)
    1.29 +load_acpi_file(const char *base, const char *node, const char *key,
    1.30 +               char *buf, size_t buflen)
    1.31  {
    1.32      ssize_t br = 0;
    1.33 -    const int fd = open_acpi_file(node, key);
    1.34 +    const int fd = open_acpi_file(base, node, key);
    1.35      if (fd == -1) {
    1.36          return SDL_FALSE;
    1.37      }
    1.38 @@ -66,8 +68,9 @@
    1.39      return SDL_TRUE;
    1.40  }
    1.41  
    1.42 +
    1.43  static SDL_bool
    1.44 -make_acpi_key_val(char **_ptr, char **_key, char **_val)
    1.45 +make_proc_acpi_key_val(char **_ptr, char **_key, char **_val)
    1.46  {
    1.47      char *ptr = *_ptr;
    1.48  
    1.49 @@ -114,13 +117,12 @@
    1.50  }
    1.51  
    1.52  static void
    1.53 -check_acpi(const char * fname, SDL_bool * have_ac, SDL_bool * have_battery,
    1.54 -           SDL_bool * charging, int *seconds, int *percent)
    1.55 +check_proc_acpi_battery(const char * node, SDL_bool * have_battery,
    1.56 +                        SDL_bool * charging, int *seconds, int *percent)
    1.57  {
    1.58 -    int fd = -1;
    1.59 +    const char *base = proc_acpi_battery_path;
    1.60      char info[1024];
    1.61      char state[1024];
    1.62 -    ssize_t br = 0;
    1.63      char *ptr = NULL;
    1.64      char *key = NULL;
    1.65      char *val = NULL;
    1.66 @@ -132,14 +134,14 @@
    1.67      int secs = -1;
    1.68      int pct = -1;
    1.69  
    1.70 -    if (!load_acpi_file(fname, "state", state, sizeof (state))) {
    1.71 +    if (!load_acpi_file(base, node, "state", state, sizeof (state))) {
    1.72          return;
    1.73 -    } else if (!load_acpi_file(fname, "info", info, sizeof (info))) {
    1.74 +    } else if (!load_acpi_file(base, node, "info", info, sizeof (info))) {
    1.75          return;
    1.76      }
    1.77  
    1.78      ptr = &state[0];
    1.79 -    while (make_acpi_key_val(&ptr, &key, &val)) {
    1.80 +    while (make_proc_acpi_key_val(&ptr, &key, &val)) {
    1.81          if (strcmp(key, "present") == 0) {
    1.82              if (strcmp(val, "yes") == 0) {
    1.83                  *have_battery = SDL_TRUE;
    1.84 @@ -147,15 +149,8 @@
    1.85          } else if (strcmp(key, "charging state") == 0) {
    1.86              /* !!! FIXME: what exactly _does_ charging/discharging mean? */
    1.87              if (strcmp(val, "charging/discharging") == 0) {
    1.88 -                *have_ac = is_ac = SDL_TRUE;
    1.89                  charge = SDL_TRUE;
    1.90              } else if (strcmp(val, "charging") == 0) {
    1.91 -                *have_ac = is_ac = SDL_TRUE;
    1.92 -                charge = SDL_TRUE;
    1.93 -            } else if (strcmp(val, "charged") == 0) {
    1.94 -                /* !!! FIXME: maybe another battery is discharging,
    1.95 -                   !!! FIXME:   instead of AC connection. */
    1.96 -                *have_ac = is_ac = SDL_TRUE;
    1.97                  charge = SDL_TRUE;
    1.98              }
    1.99          } else if (strcmp(key, "remaining capacity") == 0) {
   1.100 @@ -166,9 +161,9 @@
   1.101              }
   1.102          }
   1.103      }
   1.104 -    
   1.105 +
   1.106      ptr = &info[0];
   1.107 -    while (make_acpi_key_val(&ptr, &key, &val)) {
   1.108 +    while (make_proc_acpi_key_val(&ptr, &key, &val)) {
   1.109          if (strcmp(key, "design capacity") == 0) {
   1.110              char *endptr = NULL;
   1.111              const int cvt = (int) strtol(val, &endptr, 10);
   1.112 @@ -209,31 +204,74 @@
   1.113          *percent = pct;
   1.114          *charging = charge;
   1.115      }
   1.116 +}
   1.117  
   1.118 +static void
   1.119 +check_proc_acpi_ac_adapter(const char * node, SDL_bool * have_ac)
   1.120 +{
   1.121 +    const char *base = proc_acpi_ac_adapter_path;
   1.122 +    char state[256];
   1.123 +    char *ptr = NULL;
   1.124 +    char *key = NULL;
   1.125 +    char *val = NULL;
   1.126 +    SDL_bool charge = SDL_FALSE;
   1.127 +    SDL_bool choose = SDL_FALSE;
   1.128 +    SDL_bool is_ac = SDL_FALSE;
   1.129 +    int maximum = -1;
   1.130 +    int remaining = -1;
   1.131 +    int secs = -1;
   1.132 +    int pct = -1;
   1.133 +
   1.134 +    if (!load_acpi_file(base, node, "state", state, sizeof (state))) {
   1.135 +        return;
   1.136 +    }
   1.137 +
   1.138 +    ptr = &state[0];
   1.139 +    while (make_proc_acpi_key_val(&ptr, &key, &val)) {
   1.140 +        if (strcmp(key, "state") == 0) {
   1.141 +            if (strcmp(val, "on-line") == 0) {
   1.142 +                *have_ac = SDL_TRUE;
   1.143 +            }
   1.144 +        }
   1.145 +    }
   1.146  }
   1.147  
   1.148 +
   1.149  SDL_bool
   1.150  SDL_GetPowerInfo_Linux_proc_acpi(SDL_PowerState * state,
   1.151                                   int *seconds, int *percent)
   1.152  {
   1.153      struct dirent *dent = NULL;
   1.154      DIR *dirp = NULL;
   1.155 +    SDL_bool have_battery = SDL_FALSE;
   1.156      SDL_bool have_ac = SDL_FALSE;
   1.157 -    SDL_bool have_battery = SDL_FALSE;
   1.158      SDL_bool charging = SDL_FALSE;
   1.159  
   1.160      *seconds = -1;
   1.161      *percent = -1;
   1.162      *state = SDL_POWERSTATE_UNKNOWN;
   1.163  
   1.164 -    dirp = opendir(proc_acpi_path);
   1.165 +    dirp = opendir(proc_acpi_battery_path);
   1.166      if (dirp == NULL) {
   1.167          return SDL_FALSE;  /* can't use this interface. */
   1.168 +    } else {
   1.169 +        while ((dent = readdir(dirp)) != NULL) {
   1.170 +            const char *node = dent->d_name;
   1.171 +            check_proc_acpi_battery(node, &have_battery, &charging,
   1.172 +                                    seconds, percent);
   1.173 +        }
   1.174 +        closedir(dirp);
   1.175      }
   1.176  
   1.177 -    while ((dent = readdir(dirp)) != NULL) {
   1.178 -        const char *name = dent->d_name;
   1.179 -        check_acpi(name, &have_ac, &have_battery, &charging, seconds, percent);
   1.180 +    dirp = opendir(proc_acpi_ac_adapter_path);
   1.181 +    if (dirp == NULL) {
   1.182 +        return SDL_FALSE;  /* can't use this interface. */
   1.183 +    } else {
   1.184 +        while ((dent = readdir(dirp)) != NULL) {
   1.185 +            const char *node = dent->d_name;
   1.186 +            check_proc_acpi_ac_adapter(node, &have_ac);
   1.187 +        }
   1.188 +        closedir(dirp);
   1.189      }
   1.190  
   1.191      if (!have_battery) {