src/power/SDL_power.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 08 Apr 2011 13:03:26 -0700
changeset 5535 96594ac5fd1a
parent 5262 b530ef003506
child 6138 4c64952a58fb
permissions -rw-r--r--
SDL 1.3 is now under the zlib license.
icculus@3170
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@5535
     3
  Copyright (C) 1997-2011 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 *);
icculus@3170
    39
icculus@3170
    40
#ifndef SDL_POWER_DISABLED
icculus@3170
    41
#ifdef SDL_POWER_HARDWIRED
slouken@3272
    42
/* This is for things that _never_ have a battery */
icculus@3170
    43
static SDL_bool
slouken@3186
    44
SDL_GetPowerInfo_Hardwired(SDL_PowerState * state, int *seconds, int *percent)
icculus@3170
    45
{
icculus@3170
    46
    *seconds = -1;
icculus@3170
    47
    *percent = -1;
icculus@3170
    48
    *state = SDL_POWERSTATE_NO_BATTERY;
icculus@3170
    49
    return SDL_TRUE;
icculus@3170
    50
}
icculus@3170
    51
#endif
icculus@3170
    52
#endif
icculus@3170
    53
icculus@3170
    54
icculus@3170
    55
static SDL_GetPowerInfo_Impl implementations[] = {
icculus@3170
    56
#ifndef SDL_POWER_DISABLED
slouken@3186
    57
#ifdef SDL_POWER_LINUX          /* in order of preference. More than could work. */
bob@3174
    58
    SDL_GetPowerInfo_Linux_proc_acpi,
icculus@3170
    59
    SDL_GetPowerInfo_Linux_proc_apm,
icculus@3170
    60
#endif
slouken@3186
    61
#ifdef SDL_POWER_WINDOWS        /* handles Win32, Win64, PocketPC. */
icculus@3170
    62
    SDL_GetPowerInfo_Windows,
icculus@3170
    63
#endif
icculus@4444
    64
#ifdef SDL_POWER_UIKIT          /* handles iPhone/iPad/etc */
icculus@4444
    65
    SDL_GetPowerInfo_UIKit,
icculus@4444
    66
#endif
icculus@4444
    67
#ifdef SDL_POWER_MACOSX         /* handles Mac OS X, Darwin. */
icculus@3170
    68
    SDL_GetPowerInfo_MacOSX,
icculus@3170
    69
#endif
slouken@3186
    70
#ifdef SDL_POWER_NINTENDODS     /* handles Nintendo DS. */
icculus@3170
    71
    SDL_GetPowerInfo_NintendoDS,
icculus@3170
    72
#endif
slouken@3186
    73
#ifdef SDL_POWER_BEOS           /* handles BeOS, Zeta, with euc.jp apm driver. */
icculus@3173
    74
    SDL_GetPowerInfo_BeOS,
icculus@3173
    75
#endif
icculus@3170
    76
#ifdef SDL_POWER_HARDWIRED
icculus@3170
    77
    SDL_GetPowerInfo_Hardwired,
icculus@3170
    78
#endif
icculus@3170
    79
#endif
icculus@3170
    80
};
icculus@3170
    81
icculus@3170
    82
SDL_PowerState
icculus@3170
    83
SDL_GetPowerInfo(int *seconds, int *percent)
icculus@3170
    84
{
slouken@3186
    85
    const int total = sizeof(implementations) / sizeof(implementations[0]);
icculus@3170
    86
    int _seconds, _percent;
icculus@3170
    87
    SDL_PowerState retval;
icculus@3170
    88
    int i;
icculus@3170
    89
icculus@3170
    90
    /* Make these never NULL for platform-specific implementations. */
icculus@3170
    91
    if (seconds == NULL) {
icculus@3170
    92
        seconds = &_seconds;
icculus@3170
    93
    }
icculus@3170
    94
icculus@3170
    95
    if (percent == NULL) {
icculus@3170
    96
        percent = &_percent;
icculus@3170
    97
    }
icculus@3170
    98
icculus@3170
    99
    for (i = 0; i < total; i++) {
slouken@3186
   100
        if (implementations[i] (&retval, seconds, percent)) {
icculus@3170
   101
            return retval;
icculus@3170
   102
        }
icculus@3170
   103
    }
icculus@3170
   104
icculus@3170
   105
    /* nothing was definitive. */
icculus@3170
   106
    *seconds = -1;
icculus@3170
   107
    *percent = -1;
icculus@3170
   108
    return SDL_POWERSTATE_UNKNOWN;
icculus@3170
   109
}
icculus@3170
   110
icculus@3170
   111
/* vi: set ts=4 sw=4 expandtab: */