src/power/linux/SDL_syspower.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 10 Jun 2009 13:34:20 +0000
changeset 3186 51750b7a966f
parent 3174 c8b9c6d27476
child 3203 790cbbda6429
permissions -rw-r--r--
indent
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2009 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 #ifndef SDL_POWER_DISABLED
    25 #ifdef SDL_POWER_LINUX
    26 
    27 #include <stdio.h>
    28 #include <unistd.h>
    29 
    30 #include <sys/types.h>
    31 #include <sys/stat.h>
    32 #include <fcntl.h>
    33 
    34 #include "SDL_power.h"
    35 
    36 SDL_bool
    37 SDL_GetPowerInfo_Linux_sys_power(SDL_PowerState * state,
    38                                  int *seconds, int *percent)
    39 {
    40     return SDL_FALSE;           /* !!! FIXME: write me. */
    41 #if 0
    42     const int fd = open("/sys/power", O_RDONLY);
    43     if (fd == -1) {
    44         return SDL_FALSE;       /* can't use this interface. */
    45     }
    46     return SDL_TRUE;
    47 #endif
    48 }
    49 
    50 SDL_bool
    51 SDL_GetPowerInfo_Linux_proc_acpi(SDL_PowerState * state,
    52                                  int *seconds, int *percent)
    53 {
    54     return SDL_FALSE;           /* !!! FIXME: write me. */
    55 #if 0
    56     const int fd = open("/proc/acpi", O_RDONLY);
    57     if (fd == -1) {
    58         return SDL_FALSE;       /* can't use this interface. */
    59     }
    60     return SDL_TRUE;
    61 #endif
    62 }
    63 
    64 static SDL_bool
    65 next_string(char **_ptr, char **_str)
    66 {
    67     char *ptr = *_ptr;
    68     char *str = *_str;
    69 
    70     while (*ptr == ' ') {       /* skip any spaces... */
    71         ptr++;
    72     }
    73 
    74     if (*ptr == '\0') {
    75         return SDL_FALSE;
    76     }
    77 
    78     str = ptr;
    79     while ((*ptr != ' ') && (*ptr != '\0'))
    80         ptr++;
    81 
    82     if (*ptr != '\0')
    83         *(ptr++) = '\0';
    84 
    85     *_str = str;
    86     *_ptr = ptr;
    87     return SDL_TRUE;
    88 }
    89 
    90 static SDL_bool
    91 int_string(char *str, int *val)
    92 {
    93     char *endptr = NULL;
    94     *val = (int) strtol(str + 2, &endptr, 16);
    95     return ((*str != '\0') && (*endptr == '\0'));
    96 }
    97 
    98 /* http://lxr.linux.no/linux+v2.6.29/drivers/char/apm-emulation.c */
    99 SDL_bool
   100 SDL_GetPowerInfo_Linux_proc_apm(SDL_PowerState * state,
   101                                 int *seconds, int *percent)
   102 {
   103     SDL_bool need_details = SDL_FALSE;
   104     int ac_status = 0;
   105     int battery_status = 0;
   106     int battery_flag = 0;
   107     int battery_percent = 0;
   108     int battery_time = 0;
   109     const int fd = open("/proc/apm", O_RDONLY);
   110     char buf[128];
   111     char *ptr = &buf[0];
   112     char *str = NULL;
   113     ssize_t br;
   114 
   115     if (fd == -1) {
   116         return SDL_FALSE;       /* can't use this interface. */
   117     }
   118 
   119     br = read(fd, buf, sizeof(buf) - 1);
   120     close(fd);
   121 
   122     if (br < 0) {
   123         return SDL_FALSE;
   124     }
   125 
   126     buf[br] = '\0';             // null-terminate the string.
   127     if (!next_string(&ptr, &str)) {     /* driver version */
   128         return SDL_FALSE;
   129     }
   130     if (!next_string(&ptr, &str)) {     /* BIOS version */
   131         return SDL_FALSE;
   132     }
   133     if (!next_string(&ptr, &str)) {     /* APM flags */
   134         return SDL_FALSE;
   135     }
   136 
   137     if (!next_string(&ptr, &str)) {     /* AC line status */
   138         return SDL_FALSE;
   139     } else if (!int_string(str, &ac_status)) {
   140         return SDL_FALSE;
   141     }
   142 
   143     if (!next_string(&ptr, &str)) {     /* battery status */
   144         return SDL_FALSE;
   145     } else if (!int_string(str, &battery_status)) {
   146         return SDL_FALSE;
   147     }
   148     if (!next_string(&ptr, &str)) {     /* battery flag */
   149         return SDL_FALSE;
   150     } else if (!int_string(str, &battery_flag)) {
   151         return SDL_FALSE;
   152     }
   153     if (!next_string(&ptr, &str)) {     /* remaining battery life percent */
   154         return SDL_FALSE;
   155     }
   156     if (str[strlen(str) - 1] == '%') {
   157         str[strlen(str) - 1] = '\0';
   158     }
   159     if (!int_string(str, &battery_percent)) {
   160         return SDL_FALSE;
   161     }
   162 
   163     if (!next_string(&ptr, &str)) {     /* remaining battery life time */
   164         return SDL_FALSE;
   165     } else if (!int_string(str, &battery_time)) {
   166         return SDL_FALSE;
   167     }
   168 
   169     if (!next_string(&ptr, &str)) {     /* remaining battery life time units */
   170         return SDL_FALSE;
   171     } else if (strcmp(str, "min") == 0) {
   172         battery_time *= 60;
   173     }
   174 
   175     if (battery_flag == 0xFF) { /* unknown state */
   176         *state = SDL_POWERSTATE_UNKNOWN;
   177     } else if (battery_flag & (1 << 7)) {       /* no battery */
   178         *state = SDL_POWERSTATE_NO_BATTERY;
   179     } else if (battery_flag & (1 << 3)) {       /* charging */
   180         *state = SDL_POWERSTATE_CHARGING;
   181         need_details = SDL_TRUE;
   182     } else if (ac_status == 1) {
   183         *state = SDL_POWERSTATE_CHARGED;        /* on AC, not charging. */
   184         need_details = SDL_TRUE;
   185     } else {
   186         *state = SDL_POWERSTATE_ON_BATTERY;
   187         need_details = SDL_TRUE;
   188     }
   189 
   190     *percent = -1;
   191     *seconds = -1;
   192     if (need_details) {
   193         const int pct = battery_percent;
   194         const int secs = battery_time;
   195 
   196         if (pct >= 0) {         /* -1 == unknown */
   197             *percent = (pct > 100) ? 100 : pct; /* clamp between 0%, 100% */
   198         }
   199         if (secs >= 0) {        /* -1 == unknown */
   200             *seconds = secs;
   201         }
   202     }
   203 
   204     return SDL_TRUE;
   205 }
   206 
   207 #endif /* SDL_POWER_LINUX */
   208 #endif /* SDL_POWER_DISABLED */
   209 
   210 /* vi: set ts=4 sw=4 expandtab: */