src/power/SDL_power.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Sun, 27 Mar 2016 22:22:13 +0200
changeset 10123 d2686deec646
parent 9998 f67cf37e9cd4
child 10609 d702ecbd8ba7
permissions -rw-r--r--
Fixed comment in gesture source.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2016 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_internal.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_sys_class_power_supply(SDL_PowerState *, int *, int *);
    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_Haiku(SDL_PowerState *, int *, int *);
    38 SDL_bool SDL_GetPowerInfo_UIKit(SDL_PowerState *, int *, int *);
    39 SDL_bool SDL_GetPowerInfo_Android(SDL_PowerState *, int *, int *);
    40 SDL_bool SDL_GetPowerInfo_PSP(SDL_PowerState *, int *, int *);
    41 SDL_bool SDL_GetPowerInfo_WinRT(SDL_PowerState *, int *, int *);
    42 SDL_bool SDL_GetPowerInfo_Emscripten(SDL_PowerState *, int *, int *);
    43 
    44 #ifndef SDL_POWER_DISABLED
    45 #ifdef SDL_POWER_HARDWIRED
    46 /* This is for things that _never_ have a battery */
    47 static SDL_bool
    48 SDL_GetPowerInfo_Hardwired(SDL_PowerState * state, int *seconds, int *percent)
    49 {
    50     *seconds = -1;
    51     *percent = -1;
    52     *state = SDL_POWERSTATE_NO_BATTERY;
    53     return SDL_TRUE;
    54 }
    55 #endif
    56 #endif
    57 
    58 
    59 static SDL_GetPowerInfo_Impl implementations[] = {
    60 #ifndef SDL_POWER_DISABLED
    61 #ifdef SDL_POWER_LINUX          /* in order of preference. More than could work. */
    62     SDL_GetPowerInfo_Linux_sys_class_power_supply,
    63     SDL_GetPowerInfo_Linux_proc_acpi,
    64     SDL_GetPowerInfo_Linux_proc_apm,
    65 #endif
    66 #ifdef SDL_POWER_WINDOWS        /* handles Win32, Win64, PocketPC. */
    67     SDL_GetPowerInfo_Windows,
    68 #endif
    69 #ifdef SDL_POWER_UIKIT          /* handles iPhone/iPad/etc */
    70     SDL_GetPowerInfo_UIKit,
    71 #endif
    72 #ifdef SDL_POWER_MACOSX         /* handles Mac OS X, Darwin. */
    73     SDL_GetPowerInfo_MacOSX,
    74 #endif
    75 #ifdef SDL_POWER_HAIKU          /* with BeOS euc.jp apm driver. Does this work on Haiku? */
    76     SDL_GetPowerInfo_Haiku,
    77 #endif
    78 #ifdef SDL_POWER_ANDROID        /* handles Android. */
    79     SDL_GetPowerInfo_Android,
    80 #endif
    81 #ifdef SDL_POWER_PSP        /* handles PSP. */
    82     SDL_GetPowerInfo_PSP,
    83 #endif
    84 #ifdef SDL_POWER_WINRT          /* handles WinRT */
    85     SDL_GetPowerInfo_WinRT,
    86 #endif
    87 #ifdef SDL_POWER_EMSCRIPTEN     /* handles Emscripten */
    88     SDL_GetPowerInfo_Emscripten,
    89 #endif
    90 
    91 #ifdef SDL_POWER_HARDWIRED
    92     SDL_GetPowerInfo_Hardwired,
    93 #endif
    94 #endif
    95 };
    96 
    97 SDL_PowerState
    98 SDL_GetPowerInfo(int *seconds, int *percent)
    99 {
   100     const int total = sizeof(implementations) / sizeof(implementations[0]);
   101     int _seconds, _percent;
   102     SDL_PowerState retval = SDL_POWERSTATE_UNKNOWN;
   103     int i;
   104 
   105     /* Make these never NULL for platform-specific implementations. */
   106     if (seconds == NULL) {
   107         seconds = &_seconds;
   108     }
   109 
   110     if (percent == NULL) {
   111         percent = &_percent;
   112     }
   113 
   114     for (i = 0; i < total; i++) {
   115         if (implementations[i](&retval, seconds, percent)) {
   116             return retval;
   117         }
   118     }
   119 
   120     /* nothing was definitive. */
   121     *seconds = -1;
   122     *percent = -1;
   123     return SDL_POWERSTATE_UNKNOWN;
   124 }
   125 
   126 /* vi: set ts=4 sw=4 expandtab: */