cscg22-gearboy

CSCG 2022 Challenge 'Gearboy'
git clone https://git.sinitax.com/sinitax/cscg22-gearboy
Log | Files | Refs | sfeed.txt

SDL_power.c (3875B)


      1/*
      2  Simple DirectMedia Layer
      3  Copyright (C) 1997-2014 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 */
     28typedef SDL_bool
     29    (*SDL_GetPowerInfo_Impl) (SDL_PowerState * state, int *seconds,
     30                              int *percent);
     31
     32SDL_bool SDL_GetPowerInfo_Linux_proc_acpi(SDL_PowerState *, int *, int *);
     33SDL_bool SDL_GetPowerInfo_Linux_proc_apm(SDL_PowerState *, int *, int *);
     34SDL_bool SDL_GetPowerInfo_Windows(SDL_PowerState *, int *, int *);
     35SDL_bool SDL_GetPowerInfo_MacOSX(SDL_PowerState *, int *, int *);
     36SDL_bool SDL_GetPowerInfo_Haiku(SDL_PowerState *, int *, int *);
     37SDL_bool SDL_GetPowerInfo_UIKit(SDL_PowerState *, int *, int *);
     38SDL_bool SDL_GetPowerInfo_Android(SDL_PowerState *, int *, int *);
     39SDL_bool SDL_GetPowerInfo_PSP(SDL_PowerState *, int *, int *);
     40SDL_bool SDL_GetPowerInfo_WinRT(SDL_PowerState *, int *, int *);
     41
     42#ifndef SDL_POWER_DISABLED
     43#ifdef SDL_POWER_HARDWIRED
     44/* This is for things that _never_ have a battery */
     45static SDL_bool
     46SDL_GetPowerInfo_Hardwired(SDL_PowerState * state, int *seconds, int *percent)
     47{
     48    *seconds = -1;
     49    *percent = -1;
     50    *state = SDL_POWERSTATE_NO_BATTERY;
     51    return SDL_TRUE;
     52}
     53#endif
     54#endif
     55
     56
     57static SDL_GetPowerInfo_Impl implementations[] = {
     58#ifndef SDL_POWER_DISABLED
     59#ifdef SDL_POWER_LINUX          /* in order of preference. More than could work. */
     60    SDL_GetPowerInfo_Linux_proc_acpi,
     61    SDL_GetPowerInfo_Linux_proc_apm,
     62#endif
     63#ifdef SDL_POWER_WINDOWS        /* handles Win32, Win64, PocketPC. */
     64    SDL_GetPowerInfo_Windows,
     65#endif
     66#ifdef SDL_POWER_UIKIT          /* handles iPhone/iPad/etc */
     67    SDL_GetPowerInfo_UIKit,
     68#endif
     69#ifdef SDL_POWER_MACOSX         /* handles Mac OS X, Darwin. */
     70    SDL_GetPowerInfo_MacOSX,
     71#endif
     72#ifdef SDL_POWER_HAIKU          /* with BeOS euc.jp apm driver. Does this work on Haiku? */
     73    SDL_GetPowerInfo_Haiku,
     74#endif
     75#ifdef SDL_POWER_ANDROID        /* handles Android. */
     76    SDL_GetPowerInfo_Android,
     77#endif
     78#ifdef SDL_POWER_PSP        /* handles PSP. */
     79    SDL_GetPowerInfo_PSP,
     80#endif
     81#ifdef SDL_POWER_WINRT          /* handles WinRT */
     82    SDL_GetPowerInfo_WinRT,
     83#endif
     84
     85#ifdef SDL_POWER_HARDWIRED
     86    SDL_GetPowerInfo_Hardwired,
     87#endif
     88#endif
     89};
     90
     91SDL_PowerState
     92SDL_GetPowerInfo(int *seconds, int *percent)
     93{
     94    const int total = sizeof(implementations) / sizeof(implementations[0]);
     95    int _seconds, _percent;
     96    SDL_PowerState retval = SDL_POWERSTATE_UNKNOWN;
     97    int i;
     98
     99    /* Make these never NULL for platform-specific implementations. */
    100    if (seconds == NULL) {
    101        seconds = &_seconds;
    102    }
    103
    104    if (percent == NULL) {
    105        percent = &_percent;
    106    }
    107
    108    for (i = 0; i < total; i++) {
    109        if (implementations[i](&retval, seconds, percent)) {
    110            return retval;
    111        }
    112    }
    113
    114    /* nothing was definitive. */
    115    *seconds = -1;
    116    *percent = -1;
    117    return SDL_POWERSTATE_UNKNOWN;
    118}
    119
    120/* vi: set ts=4 sw=4 expandtab: */