src/power/SDL_power.c
author Sam Lantinga
Sat, 19 Sep 2009 13:29:40 +0000
changeset 3280 00cace2d9080
parent 3272 e8f63b28947d
child 3697 f7b03b6838cb
permissions -rw-r--r--
Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
     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 #include "SDL_power.h"
    24 
    25 /*
    26  * Returns SDL_TRUE if we have a definitive answer.
    27  * SDL_FALSE to try next implementation.
    28  */
    29 typedef SDL_bool
    30     (*SDL_GetPowerInfo_Impl) (SDL_PowerState * state, int *seconds,
    31                               int *percent);
    32 
    33 SDL_bool SDL_GetPowerInfo_Linux_proc_acpi(SDL_PowerState *, int *, int *);
    34 SDL_bool SDL_GetPowerInfo_Linux_proc_apm(SDL_PowerState *, int *, int *);
    35 SDL_bool SDL_GetPowerInfo_Windows(SDL_PowerState *, int *, int *);
    36 SDL_bool SDL_GetPowerInfo_MacOSX(SDL_PowerState *, int *, int *);
    37 SDL_bool SDL_GetPowerInfo_BeOS(SDL_PowerState *, int *, int *);
    38 SDL_bool SDL_GetPowerInfo_NintendoDS(SDL_PowerState *, int *, int *);
    39 
    40 #ifndef SDL_POWER_DISABLED
    41 #ifdef SDL_POWER_HARDWIRED
    42 /* This is for things that _never_ have a battery */
    43 static SDL_bool
    44 SDL_GetPowerInfo_Hardwired(SDL_PowerState * state, int *seconds, int *percent)
    45 {
    46     *seconds = -1;
    47     *percent = -1;
    48     *state = SDL_POWERSTATE_NO_BATTERY;
    49     return SDL_TRUE;
    50 }
    51 #endif
    52 #endif
    53 
    54 
    55 static SDL_GetPowerInfo_Impl implementations[] = {
    56 #ifndef SDL_POWER_DISABLED
    57 #ifdef SDL_POWER_LINUX          /* in order of preference. More than could work. */
    58     SDL_GetPowerInfo_Linux_proc_acpi,
    59     SDL_GetPowerInfo_Linux_proc_apm,
    60 #endif
    61 #ifdef SDL_POWER_WINDOWS        /* handles Win32, Win64, PocketPC. */
    62     SDL_GetPowerInfo_Windows,
    63 #endif
    64 #ifdef SDL_POWER_MACOSX         /* handles Mac OS X, Darwin, iPhone. */
    65     SDL_GetPowerInfo_MacOSX,
    66 #endif
    67 #ifdef SDL_POWER_NINTENDODS     /* handles Nintendo DS. */
    68     SDL_GetPowerInfo_NintendoDS,
    69 #endif
    70 #ifdef SDL_POWER_BEOS           /* handles BeOS, Zeta, with euc.jp apm driver. */
    71     SDL_GetPowerInfo_BeOS,
    72 #endif
    73 #ifdef SDL_POWER_HARDWIRED
    74     SDL_GetPowerInfo_Hardwired,
    75 #endif
    76 #endif
    77 };
    78 
    79 SDL_PowerState
    80 SDL_GetPowerInfo(int *seconds, int *percent)
    81 {
    82     const int total = sizeof(implementations) / sizeof(implementations[0]);
    83     int _seconds, _percent;
    84     SDL_PowerState retval;
    85     int i;
    86 
    87     /* Make these never NULL for platform-specific implementations. */
    88     if (seconds == NULL) {
    89         seconds = &_seconds;
    90     }
    91 
    92     if (percent == NULL) {
    93         percent = &_percent;
    94     }
    95 
    96     for (i = 0; i < total; i++) {
    97         if (implementations[i] (&retval, seconds, percent)) {
    98             return retval;
    99         }
   100     }
   101 
   102     /* nothing was definitive. */
   103     *seconds = -1;
   104     *percent = -1;
   105     return SDL_POWERSTATE_UNKNOWN;
   106 }
   107 
   108 /* vi: set ts=4 sw=4 expandtab: */