src/power/SDL_power.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 13 Nov 2019 21:53:01 -0800
changeset 13244 50987b0b4533
parent 12503 806492103856
permissions -rw-r--r--
Added a utility function to simplify the hint handling logic
icculus@3170
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@12503
     3
  Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
icculus@3170
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
icculus@3170
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
icculus@3170
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
icculus@3170
    20
*/
icculus@8093
    21
#include "../SDL_internal.h"
icculus@3170
    22
#include "SDL_power.h"
slouken@10609
    23
#include "SDL_syspower.h"
icculus@3170
    24
icculus@3170
    25
/*
icculus@3170
    26
 * Returns SDL_TRUE if we have a definitive answer.
icculus@3170
    27
 * SDL_FALSE to try next implementation.
icculus@3170
    28
 */
icculus@3170
    29
typedef SDL_bool
slouken@3186
    30
    (*SDL_GetPowerInfo_Impl) (SDL_PowerState * state, int *seconds,
slouken@3186
    31
                              int *percent);
icculus@3170
    32
icculus@3170
    33
#ifndef SDL_POWER_DISABLED
icculus@3170
    34
#ifdef SDL_POWER_HARDWIRED
slouken@3272
    35
/* This is for things that _never_ have a battery */
icculus@3170
    36
static SDL_bool
slouken@3186
    37
SDL_GetPowerInfo_Hardwired(SDL_PowerState * state, int *seconds, int *percent)
icculus@3170
    38
{
icculus@3170
    39
    *seconds = -1;
icculus@3170
    40
    *percent = -1;
icculus@3170
    41
    *state = SDL_POWERSTATE_NO_BATTERY;
icculus@3170
    42
    return SDL_TRUE;
icculus@3170
    43
}
icculus@3170
    44
#endif
icculus@3170
    45
icculus@3170
    46
static SDL_GetPowerInfo_Impl implementations[] = {
slouken@3186
    47
#ifdef SDL_POWER_LINUX          /* in order of preference. More than could work. */
icculus@11043
    48
    SDL_GetPowerInfo_Linux_org_freedesktop_upower,
icculus@9700
    49
    SDL_GetPowerInfo_Linux_sys_class_power_supply,
bob@3174
    50
    SDL_GetPowerInfo_Linux_proc_acpi,
icculus@3170
    51
    SDL_GetPowerInfo_Linux_proc_apm,
icculus@3170
    52
#endif
slouken@3186
    53
#ifdef SDL_POWER_WINDOWS        /* handles Win32, Win64, PocketPC. */
icculus@3170
    54
    SDL_GetPowerInfo_Windows,
icculus@3170
    55
#endif
icculus@4444
    56
#ifdef SDL_POWER_UIKIT          /* handles iPhone/iPad/etc */
icculus@4444
    57
    SDL_GetPowerInfo_UIKit,
icculus@4444
    58
#endif
icculus@4444
    59
#ifdef SDL_POWER_MACOSX         /* handles Mac OS X, Darwin. */
icculus@3170
    60
    SDL_GetPowerInfo_MacOSX,
icculus@3170
    61
#endif
icculus@7981
    62
#ifdef SDL_POWER_HAIKU          /* with BeOS euc.jp apm driver. Does this work on Haiku? */
icculus@7981
    63
    SDL_GetPowerInfo_Haiku,
icculus@3173
    64
#endif
slouken@6448
    65
#ifdef SDL_POWER_ANDROID        /* handles Android. */
slouken@6448
    66
    SDL_GetPowerInfo_Android,
slouken@6448
    67
#endif
kimonline@7009
    68
#ifdef SDL_POWER_PSP        /* handles PSP. */
kimonline@7009
    69
    SDL_GetPowerInfo_PSP,
kimonline@7009
    70
#endif
dludwig@8611
    71
#ifdef SDL_POWER_WINRT          /* handles WinRT */
dludwig@8611
    72
    SDL_GetPowerInfo_WinRT,
dludwig@8611
    73
#endif
icculus@9278
    74
#ifdef SDL_POWER_EMSCRIPTEN     /* handles Emscripten */
icculus@9278
    75
    SDL_GetPowerInfo_Emscripten,
icculus@9278
    76
#endif
kimonline@7009
    77
icculus@3170
    78
#ifdef SDL_POWER_HARDWIRED
icculus@3170
    79
    SDL_GetPowerInfo_Hardwired,
icculus@3170
    80
#endif
sezeroz@12331
    81
};
icculus@3170
    82
#endif
icculus@3170
    83
icculus@3170
    84
SDL_PowerState
icculus@3170
    85
SDL_GetPowerInfo(int *seconds, int *percent)
icculus@3170
    86
{
sezeroz@12331
    87
#ifndef SDL_POWER_DISABLED
slouken@3186
    88
    const int total = sizeof(implementations) / sizeof(implementations[0]);
slouken@8820
    89
    SDL_PowerState retval = SDL_POWERSTATE_UNKNOWN;
icculus@3170
    90
    int i;
sezeroz@12331
    91
#endif
icculus@3170
    92
sezeroz@12331
    93
    int _seconds, _percent;
icculus@3170
    94
    /* Make these never NULL for platform-specific implementations. */
icculus@3170
    95
    if (seconds == NULL) {
icculus@3170
    96
        seconds = &_seconds;
icculus@3170
    97
    }
icculus@3170
    98
    if (percent == NULL) {
icculus@3170
    99
        percent = &_percent;
icculus@3170
   100
    }
icculus@3170
   101
sezeroz@12331
   102
#ifndef SDL_POWER_DISABLED
icculus@3170
   103
    for (i = 0; i < total; i++) {
slouken@8820
   104
        if (implementations[i](&retval, seconds, percent)) {
icculus@3170
   105
            return retval;
icculus@3170
   106
        }
icculus@3170
   107
    }
sezeroz@12331
   108
#endif
icculus@3170
   109
icculus@3170
   110
    /* nothing was definitive. */
icculus@3170
   111
    *seconds = -1;
icculus@3170
   112
    *percent = -1;
icculus@3170
   113
    return SDL_POWERSTATE_UNKNOWN;
icculus@3170
   114
}
icculus@3170
   115
icculus@3170
   116
/* vi: set ts=4 sw=4 expandtab: */