src/power/SDL_power.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 15 Feb 2013 08:47:44 -0800
changeset 6885 700f1b25f77f
parent 6448 64a6297a8b93
child 7009 161b7b6a5303
permissions -rw-r--r--
Happy New Year!
icculus@3170
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 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@3170
    21
#include "SDL_config.h"
icculus@3170
    22
#include "SDL_power.h"
icculus@3170
    23
icculus@3170
    24
/*
icculus@3170
    25
 * Returns SDL_TRUE if we have a definitive answer.
icculus@3170
    26
 * SDL_FALSE to try next implementation.
icculus@3170
    27
 */
icculus@3170
    28
typedef SDL_bool
slouken@3186
    29
    (*SDL_GetPowerInfo_Impl) (SDL_PowerState * state, int *seconds,
slouken@3186
    30
                              int *percent);
icculus@3170
    31
slouken@3186
    32
SDL_bool SDL_GetPowerInfo_Linux_proc_acpi(SDL_PowerState *, int *, int *);
slouken@3186
    33
SDL_bool SDL_GetPowerInfo_Linux_proc_apm(SDL_PowerState *, int *, int *);
slouken@3186
    34
SDL_bool SDL_GetPowerInfo_Windows(SDL_PowerState *, int *, int *);
slouken@3186
    35
SDL_bool SDL_GetPowerInfo_MacOSX(SDL_PowerState *, int *, int *);
slouken@3186
    36
SDL_bool SDL_GetPowerInfo_BeOS(SDL_PowerState *, int *, int *);
slouken@3186
    37
SDL_bool SDL_GetPowerInfo_NintendoDS(SDL_PowerState *, int *, int *);
icculus@4444
    38
SDL_bool SDL_GetPowerInfo_UIKit(SDL_PowerState *, int *, int *);
slouken@6448
    39
SDL_bool SDL_GetPowerInfo_Android(SDL_PowerState *, int *, int *);
icculus@3170
    40
icculus@3170
    41
#ifndef SDL_POWER_DISABLED
icculus@3170
    42
#ifdef SDL_POWER_HARDWIRED
slouken@3272
    43
/* This is for things that _never_ have a battery */
icculus@3170
    44
static SDL_bool
slouken@3186
    45
SDL_GetPowerInfo_Hardwired(SDL_PowerState * state, int *seconds, int *percent)
icculus@3170
    46
{
icculus@3170
    47
    *seconds = -1;
icculus@3170
    48
    *percent = -1;
icculus@3170
    49
    *state = SDL_POWERSTATE_NO_BATTERY;
icculus@3170
    50
    return SDL_TRUE;
icculus@3170
    51
}
icculus@3170
    52
#endif
icculus@3170
    53
#endif
icculus@3170
    54
icculus@3170
    55
icculus@3170
    56
static SDL_GetPowerInfo_Impl implementations[] = {
icculus@3170
    57
#ifndef SDL_POWER_DISABLED
slouken@3186
    58
#ifdef SDL_POWER_LINUX          /* in order of preference. More than could work. */
bob@3174
    59
    SDL_GetPowerInfo_Linux_proc_acpi,
icculus@3170
    60
    SDL_GetPowerInfo_Linux_proc_apm,
icculus@3170
    61
#endif
slouken@3186
    62
#ifdef SDL_POWER_WINDOWS        /* handles Win32, Win64, PocketPC. */
icculus@3170
    63
    SDL_GetPowerInfo_Windows,
icculus@3170
    64
#endif
icculus@4444
    65
#ifdef SDL_POWER_UIKIT          /* handles iPhone/iPad/etc */
icculus@4444
    66
    SDL_GetPowerInfo_UIKit,
icculus@4444
    67
#endif
icculus@4444
    68
#ifdef SDL_POWER_MACOSX         /* handles Mac OS X, Darwin. */
icculus@3170
    69
    SDL_GetPowerInfo_MacOSX,
icculus@3170
    70
#endif
slouken@3186
    71
#ifdef SDL_POWER_NINTENDODS     /* handles Nintendo DS. */
icculus@3170
    72
    SDL_GetPowerInfo_NintendoDS,
icculus@3170
    73
#endif
slouken@3186
    74
#ifdef SDL_POWER_BEOS           /* handles BeOS, Zeta, with euc.jp apm driver. */
icculus@3173
    75
    SDL_GetPowerInfo_BeOS,
icculus@3173
    76
#endif
slouken@6448
    77
#ifdef SDL_POWER_ANDROID        /* handles Android. */
slouken@6448
    78
    SDL_GetPowerInfo_Android,
slouken@6448
    79
#endif
icculus@3170
    80
#ifdef SDL_POWER_HARDWIRED
icculus@3170
    81
    SDL_GetPowerInfo_Hardwired,
icculus@3170
    82
#endif
icculus@3170
    83
#endif
icculus@3170
    84
};
icculus@3170
    85
icculus@3170
    86
SDL_PowerState
icculus@3170
    87
SDL_GetPowerInfo(int *seconds, int *percent)
icculus@3170
    88
{
slouken@3186
    89
    const int total = sizeof(implementations) / sizeof(implementations[0]);
icculus@3170
    90
    int _seconds, _percent;
icculus@3170
    91
    SDL_PowerState retval;
icculus@3170
    92
    int i;
icculus@3170
    93
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
icculus@3170
    99
    if (percent == NULL) {
icculus@3170
   100
        percent = &_percent;
icculus@3170
   101
    }
icculus@3170
   102
icculus@3170
   103
    for (i = 0; i < total; i++) {
slouken@3186
   104
        if (implementations[i] (&retval, seconds, percent)) {
icculus@3170
   105
            return retval;
icculus@3170
   106
        }
icculus@3170
   107
    }
icculus@3170
   108
icculus@3170
   109
    /* nothing was definitive. */
icculus@3170
   110
    *seconds = -1;
icculus@3170
   111
    *percent = -1;
icculus@3170
   112
    return SDL_POWERSTATE_UNKNOWN;
icculus@3170
   113
}
icculus@3170
   114
icculus@3170
   115
/* vi: set ts=4 sw=4 expandtab: */