src/power/SDL_power.c
changeset 3170 b7a48f533966
child 3173 510e3f36c04a
equal deleted inserted replaced
3169:f294338ca6eb 3170:b7a48f533966
       
     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, int *percent);
       
    31 
       
    32 SDL_bool SDL_GetPowerInfo_Linux_sys_power(SDL_PowerState*, int*, int*);
       
    33 SDL_bool SDL_GetPowerInfo_Linux_proc_apci(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_OS2(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, like the Dreamcast, etc. */
       
    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_sys_power,
       
    59     SDL_GetPowerInfo_Linux_proc_apci,
       
    60     SDL_GetPowerInfo_Linux_proc_apm,
       
    61 #endif
       
    62 #ifdef SDL_POWER_WINDOWS /* handles Win32, Win64, PocketPC. */
       
    63     SDL_GetPowerInfo_Windows,
       
    64 #endif
       
    65 #ifdef SDL_POWER_MACOSX  /* handles Mac OS X, Darwin, iPhone. */
       
    66     SDL_GetPowerInfo_MacOSX,
       
    67 #endif
       
    68 #ifdef SDL_POWER_OS2  /* handles OS/2, Warp, eComStation. */
       
    69     SDL_GetPowerInfo_OS2,
       
    70 #endif
       
    71 #ifdef SDL_POWER_NINTENDODS  /* handles Nintendo DS. */
       
    72     SDL_GetPowerInfo_NintendoDS,
       
    73 #endif
       
    74 #ifdef SDL_POWER_HARDWIRED
       
    75     SDL_GetPowerInfo_Hardwired,
       
    76 #endif
       
    77 #endif
       
    78 };
       
    79 
       
    80 SDL_PowerState
       
    81 SDL_GetPowerInfo(int *seconds, int *percent)
       
    82 {
       
    83     const int total = sizeof (implementations) / sizeof (implementations[0]);
       
    84     int _seconds, _percent;
       
    85     SDL_PowerState retval;
       
    86     int i;
       
    87 
       
    88     /* Make these never NULL for platform-specific implementations. */
       
    89     if (seconds == NULL) {
       
    90         seconds = &_seconds;
       
    91     }
       
    92 
       
    93     if (percent == NULL) {
       
    94         percent = &_percent;
       
    95     }
       
    96 
       
    97     for (i = 0; i < total; i++) {
       
    98         if (implementations[i](&retval, seconds, percent)) {
       
    99             return retval;
       
   100         }
       
   101     }
       
   102 
       
   103     /* nothing was definitive. */
       
   104     *seconds = -1;
       
   105     *percent = -1;
       
   106     return SDL_POWERSTATE_UNKNOWN;
       
   107 }
       
   108 
       
   109 /* vi: set ts=4 sw=4 expandtab: */
       
   110