src/power/SDL_power.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 19 Sep 2009 06:43:45 +0000
changeset 3269 a67a961e2171
parent 3204 f77f50add24f
child 3272 e8f63b28947d
permissions -rw-r--r--
Removed outdated OS/2 support
icculus@3170
     1
/*
icculus@3170
     2
    SDL - Simple DirectMedia Layer
icculus@3170
     3
    Copyright (C) 1997-2009 Sam Lantinga
icculus@3170
     4
icculus@3170
     5
    This library is free software; you can redistribute it and/or
icculus@3170
     6
    modify it under the terms of the GNU Lesser General Public
icculus@3170
     7
    License as published by the Free Software Foundation; either
icculus@3170
     8
    version 2.1 of the License, or (at your option) any later version.
icculus@3170
     9
icculus@3170
    10
    This library is distributed in the hope that it will be useful,
icculus@3170
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
icculus@3170
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
icculus@3170
    13
    Lesser General Public License for more details.
icculus@3170
    14
icculus@3170
    15
    You should have received a copy of the GNU Lesser General Public
icculus@3170
    16
    License along with this library; if not, write to the Free Software
icculus@3170
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
icculus@3170
    18
icculus@3170
    19
    Sam Lantinga
icculus@3170
    20
    slouken@libsdl.org
icculus@3170
    21
*/
icculus@3170
    22
#include "SDL_config.h"
icculus@3170
    23
#include "SDL_power.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
slouken@3186
    33
SDL_bool SDL_GetPowerInfo_Linux_proc_acpi(SDL_PowerState *, int *, int *);
slouken@3186
    34
SDL_bool SDL_GetPowerInfo_Linux_proc_apm(SDL_PowerState *, int *, int *);
slouken@3186
    35
SDL_bool SDL_GetPowerInfo_Windows(SDL_PowerState *, int *, int *);
slouken@3186
    36
SDL_bool SDL_GetPowerInfo_MacOSX(SDL_PowerState *, int *, int *);
slouken@3186
    37
SDL_bool SDL_GetPowerInfo_BeOS(SDL_PowerState *, int *, int *);
slouken@3186
    38
SDL_bool SDL_GetPowerInfo_NintendoDS(SDL_PowerState *, int *, int *);
icculus@3170
    39
icculus@3170
    40
#ifndef SDL_POWER_DISABLED
icculus@3170
    41
#ifdef SDL_POWER_HARDWIRED
icculus@3170
    42
/* This is for things that _never_ have a battery, like the Dreamcast, etc. */
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
slouken@3186
    64
#ifdef SDL_POWER_MACOSX         /* handles Mac OS X, Darwin, iPhone. */
icculus@3170
    65
    SDL_GetPowerInfo_MacOSX,
icculus@3170
    66
#endif
slouken@3186
    67
#ifdef SDL_POWER_NINTENDODS     /* handles Nintendo DS. */
icculus@3170
    68
    SDL_GetPowerInfo_NintendoDS,
icculus@3170
    69
#endif
slouken@3186
    70
#ifdef SDL_POWER_BEOS           /* handles BeOS, Zeta, with euc.jp apm driver. */
icculus@3173
    71
    SDL_GetPowerInfo_BeOS,
icculus@3173
    72
#endif
icculus@3170
    73
#ifdef SDL_POWER_HARDWIRED
icculus@3170
    74
    SDL_GetPowerInfo_Hardwired,
icculus@3170
    75
#endif
icculus@3170
    76
#endif
icculus@3170
    77
};
icculus@3170
    78
icculus@3170
    79
SDL_PowerState
icculus@3170
    80
SDL_GetPowerInfo(int *seconds, int *percent)
icculus@3170
    81
{
slouken@3186
    82
    const int total = sizeof(implementations) / sizeof(implementations[0]);
icculus@3170
    83
    int _seconds, _percent;
icculus@3170
    84
    SDL_PowerState retval;
icculus@3170
    85
    int i;
icculus@3170
    86
icculus@3170
    87
    /* Make these never NULL for platform-specific implementations. */
icculus@3170
    88
    if (seconds == NULL) {
icculus@3170
    89
        seconds = &_seconds;
icculus@3170
    90
    }
icculus@3170
    91
icculus@3170
    92
    if (percent == NULL) {
icculus@3170
    93
        percent = &_percent;
icculus@3170
    94
    }
icculus@3170
    95
icculus@3170
    96
    for (i = 0; i < total; i++) {
slouken@3186
    97
        if (implementations[i] (&retval, seconds, percent)) {
icculus@3170
    98
            return retval;
icculus@3170
    99
        }
icculus@3170
   100
    }
icculus@3170
   101
icculus@3170
   102
    /* nothing was definitive. */
icculus@3170
   103
    *seconds = -1;
icculus@3170
   104
    *percent = -1;
icculus@3170
   105
    return SDL_POWERSTATE_UNKNOWN;
icculus@3170
   106
}
icculus@3170
   107
icculus@3170
   108
/* vi: set ts=4 sw=4 expandtab: */