src/power/SDL_power.c
author Ryan C. Gordon
Thu, 13 Sep 2012 15:24:04 -0400
changeset 6423 339c0ccf6250
parent 6138 4c64952a58fb
child 6448 64a6297a8b93
permissions -rwxr-xr-x
Make the X11 SetBordered event tapdancing more robust, restore focus, etc.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 #include "SDL_config.h"
    22 #include "SDL_power.h"
    23 
    24 /*
    25  * Returns SDL_TRUE if we have a definitive answer.
    26  * SDL_FALSE to try next implementation.
    27  */
    28 typedef SDL_bool
    29     (*SDL_GetPowerInfo_Impl) (SDL_PowerState * state, int *seconds,
    30                               int *percent);
    31 
    32 SDL_bool SDL_GetPowerInfo_Linux_proc_acpi(SDL_PowerState *, int *, int *);
    33 SDL_bool SDL_GetPowerInfo_Linux_proc_apm(SDL_PowerState *, int *, int *);
    34 SDL_bool SDL_GetPowerInfo_Windows(SDL_PowerState *, int *, int *);
    35 SDL_bool SDL_GetPowerInfo_MacOSX(SDL_PowerState *, int *, int *);
    36 SDL_bool SDL_GetPowerInfo_BeOS(SDL_PowerState *, int *, int *);
    37 SDL_bool SDL_GetPowerInfo_NintendoDS(SDL_PowerState *, int *, int *);
    38 SDL_bool SDL_GetPowerInfo_UIKit(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_UIKIT          /* handles iPhone/iPad/etc */
    65     SDL_GetPowerInfo_UIKit,
    66 #endif
    67 #ifdef SDL_POWER_MACOSX         /* handles Mac OS X, Darwin. */
    68     SDL_GetPowerInfo_MacOSX,
    69 #endif
    70 #ifdef SDL_POWER_NINTENDODS     /* handles Nintendo DS. */
    71     SDL_GetPowerInfo_NintendoDS,
    72 #endif
    73 #ifdef SDL_POWER_BEOS           /* handles BeOS, Zeta, with euc.jp apm driver. */
    74     SDL_GetPowerInfo_BeOS,
    75 #endif
    76 #ifdef SDL_POWER_HARDWIRED
    77     SDL_GetPowerInfo_Hardwired,
    78 #endif
    79 #endif
    80 };
    81 
    82 SDL_PowerState
    83 SDL_GetPowerInfo(int *seconds, int *percent)
    84 {
    85     const int total = sizeof(implementations) / sizeof(implementations[0]);
    86     int _seconds, _percent;
    87     SDL_PowerState retval;
    88     int i;
    89 
    90     /* Make these never NULL for platform-specific implementations. */
    91     if (seconds == NULL) {
    92         seconds = &_seconds;
    93     }
    94 
    95     if (percent == NULL) {
    96         percent = &_percent;
    97     }
    98 
    99     for (i = 0; i < total; i++) {
   100         if (implementations[i] (&retval, seconds, percent)) {
   101             return retval;
   102         }
   103     }
   104 
   105     /* nothing was definitive. */
   106     *seconds = -1;
   107     *percent = -1;
   108     return SDL_POWERSTATE_UNKNOWN;
   109 }
   110 
   111 /* vi: set ts=4 sw=4 expandtab: */