src/power/SDL_power.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 01 May 2015 01:12:48 -0400
changeset 9580 d37ef6990bf9
parent 9278 8900afb78a19
child 9619 b94b6d0bff0f
permissions -rw-r--r--
checkkeys: Readded the KEYUP event test.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     1
/*
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     2
  Simple DirectMedia Layer
8149
681eb46b8ac4 Fixed bug 2374 - Update copyright for 2014...
Sam Lantinga <slouken@libsdl.org>
parents: 8093
diff changeset
     3
  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     4
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     5
  This software is provided 'as-is', without any express or implied
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     6
  warranty.  In no event will the authors be held liable for any damages
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     7
  arising from the use of this software.
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     9
  Permission is granted to anyone to use this software for any purpose,
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    10
  including commercial applications, and to alter it and redistribute it
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    11
  freely, subject to the following restrictions:
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    12
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    13
  1. The origin of this software must not be misrepresented; you must not
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    14
     claim that you wrote the original software. If you use this software
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    15
     in a product, an acknowledgment in the product documentation would be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    16
     appreciated but is not required.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    17
  2. Altered source versions must be plainly marked as such, and must not be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    18
     misrepresented as being the original software.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    20
*/
8093
b43765095a6f Make internal SDL sources include SDL_internal.h instead of SDL_config.h
Ryan C. Gordon <icculus@icculus.org>
parents: 7981
diff changeset
    21
#include "../SDL_internal.h"
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    22
#include "SDL_power.h"
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    23
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    24
/*
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    25
 * Returns SDL_TRUE if we have a definitive answer.
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    26
 * SDL_FALSE to try next implementation.
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    27
 */
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    28
typedef SDL_bool
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
    29
    (*SDL_GetPowerInfo_Impl) (SDL_PowerState * state, int *seconds,
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
    30
                              int *percent);
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    31
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
    32
SDL_bool SDL_GetPowerInfo_Linux_proc_acpi(SDL_PowerState *, int *, int *);
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
    33
SDL_bool SDL_GetPowerInfo_Linux_proc_apm(SDL_PowerState *, int *, int *);
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
    34
SDL_bool SDL_GetPowerInfo_Windows(SDL_PowerState *, int *, int *);
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
    35
SDL_bool SDL_GetPowerInfo_MacOSX(SDL_PowerState *, int *, int *);
7981
6d538bc1b395 Renamed things named after BeOS to be named after Haiku instead.
Ryan C. Gordon <icculus@icculus.org>
parents: 7011
diff changeset
    36
SDL_bool SDL_GetPowerInfo_Haiku(SDL_PowerState *, int *, int *);
4444
42e1092225f9 Implemented SDL power APIs for iPhoneOS.
Ryan C. Gordon <icculus@icculus.org>
parents: 3697
diff changeset
    37
SDL_bool SDL_GetPowerInfo_UIKit(SDL_PowerState *, int *, int *);
6448
64a6297a8b93 Fixed bug 1606 - SDL does not implement SDL_GetPowerInfo() for Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
    38
SDL_bool SDL_GetPowerInfo_Android(SDL_PowerState *, int *, int *);
7009
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents: 6885
diff changeset
    39
SDL_bool SDL_GetPowerInfo_PSP(SDL_PowerState *, int *, int *);
8611
a762dfc67e1a Fixed various build and runtime errors when using WinRT with VS2012.
David Ludwig <dludwig@pobox.com>
parents: 8149
diff changeset
    40
SDL_bool SDL_GetPowerInfo_WinRT(SDL_PowerState *, int *, int *);
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8820
diff changeset
    41
SDL_bool SDL_GetPowerInfo_Emscripten(SDL_PowerState *, int *, int *);
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    42
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    43
#ifndef SDL_POWER_DISABLED
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    44
#ifdef SDL_POWER_HARDWIRED
3272
e8f63b28947d Removed outdated Dreamcast support
Sam Lantinga <slouken@libsdl.org>
parents: 3269
diff changeset
    45
/* This is for things that _never_ have a battery */
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    46
static SDL_bool
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
    47
SDL_GetPowerInfo_Hardwired(SDL_PowerState * state, int *seconds, int *percent)
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    48
{
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    49
    *seconds = -1;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    50
    *percent = -1;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
    *state = SDL_POWERSTATE_NO_BATTERY;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    52
    return SDL_TRUE;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    53
}
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    54
#endif
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    55
#endif
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    56
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    57
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    58
static SDL_GetPowerInfo_Impl implementations[] = {
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
#ifndef SDL_POWER_DISABLED
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
    60
#ifdef SDL_POWER_LINUX          /* in order of preference. More than could work. */
3174
c8b9c6d27476 Fixed some typos and added a couple of includes. All were need to get SDL_power to compile on Linux.
Bob Pendleton <bob@pendleton.com>
parents: 3173
diff changeset
    61
    SDL_GetPowerInfo_Linux_proc_acpi,
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    62
    SDL_GetPowerInfo_Linux_proc_apm,
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
#endif
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
    64
#ifdef SDL_POWER_WINDOWS        /* handles Win32, Win64, PocketPC. */
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    65
    SDL_GetPowerInfo_Windows,
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    66
#endif
4444
42e1092225f9 Implemented SDL power APIs for iPhoneOS.
Ryan C. Gordon <icculus@icculus.org>
parents: 3697
diff changeset
    67
#ifdef SDL_POWER_UIKIT          /* handles iPhone/iPad/etc */
42e1092225f9 Implemented SDL power APIs for iPhoneOS.
Ryan C. Gordon <icculus@icculus.org>
parents: 3697
diff changeset
    68
    SDL_GetPowerInfo_UIKit,
42e1092225f9 Implemented SDL power APIs for iPhoneOS.
Ryan C. Gordon <icculus@icculus.org>
parents: 3697
diff changeset
    69
#endif
42e1092225f9 Implemented SDL power APIs for iPhoneOS.
Ryan C. Gordon <icculus@icculus.org>
parents: 3697
diff changeset
    70
#ifdef SDL_POWER_MACOSX         /* handles Mac OS X, Darwin. */
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    71
    SDL_GetPowerInfo_MacOSX,
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    72
#endif
7981
6d538bc1b395 Renamed things named after BeOS to be named after Haiku instead.
Ryan C. Gordon <icculus@icculus.org>
parents: 7011
diff changeset
    73
#ifdef SDL_POWER_HAIKU          /* with BeOS euc.jp apm driver. Does this work on Haiku? */
6d538bc1b395 Renamed things named after BeOS to be named after Haiku instead.
Ryan C. Gordon <icculus@icculus.org>
parents: 7011
diff changeset
    74
    SDL_GetPowerInfo_Haiku,
3173
510e3f36c04a BeOS support for power subsystem.
Ryan C. Gordon <icculus@icculus.org>
parents: 3170
diff changeset
    75
#endif
6448
64a6297a8b93 Fixed bug 1606 - SDL does not implement SDL_GetPowerInfo() for Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
    76
#ifdef SDL_POWER_ANDROID        /* handles Android. */
64a6297a8b93 Fixed bug 1606 - SDL does not implement SDL_GetPowerInfo() for Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
    77
    SDL_GetPowerInfo_Android,
64a6297a8b93 Fixed bug 1606 - SDL does not implement SDL_GetPowerInfo() for Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
    78
#endif
7009
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents: 6885
diff changeset
    79
#ifdef SDL_POWER_PSP        /* handles PSP. */
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents: 6885
diff changeset
    80
    SDL_GetPowerInfo_PSP,
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents: 6885
diff changeset
    81
#endif
8611
a762dfc67e1a Fixed various build and runtime errors when using WinRT with VS2012.
David Ludwig <dludwig@pobox.com>
parents: 8149
diff changeset
    82
#ifdef SDL_POWER_WINRT          /* handles WinRT */
a762dfc67e1a Fixed various build and runtime errors when using WinRT with VS2012.
David Ludwig <dludwig@pobox.com>
parents: 8149
diff changeset
    83
    SDL_GetPowerInfo_WinRT,
a762dfc67e1a Fixed various build and runtime errors when using WinRT with VS2012.
David Ludwig <dludwig@pobox.com>
parents: 8149
diff changeset
    84
#endif
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8820
diff changeset
    85
#ifdef SDL_POWER_EMSCRIPTEN     /* handles Emscripten */
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8820
diff changeset
    86
    SDL_GetPowerInfo_Emscripten,
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8820
diff changeset
    87
#endif
7009
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents: 6885
diff changeset
    88
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    89
#ifdef SDL_POWER_HARDWIRED
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    90
    SDL_GetPowerInfo_Hardwired,
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    91
#endif
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    92
#endif
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    93
};
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    94
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    95
SDL_PowerState
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    96
SDL_GetPowerInfo(int *seconds, int *percent)
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    97
{
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
    98
    const int total = sizeof(implementations) / sizeof(implementations[0]);
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    99
    int _seconds, _percent;
8820
0e935d5b193a Added annotations to help code analysis tools
Sam Lantinga <slouken@libsdl.org>
parents: 8611
diff changeset
   100
    SDL_PowerState retval = SDL_POWERSTATE_UNKNOWN;
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   101
    int i;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   102
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   103
    /* Make these never NULL for platform-specific implementations. */
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   104
    if (seconds == NULL) {
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   105
        seconds = &_seconds;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   106
    }
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   107
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   108
    if (percent == NULL) {
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   109
        percent = &_percent;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   110
    }
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   111
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   112
    for (i = 0; i < total; i++) {
8820
0e935d5b193a Added annotations to help code analysis tools
Sam Lantinga <slouken@libsdl.org>
parents: 8611
diff changeset
   113
        if (implementations[i](&retval, seconds, percent)) {
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   114
            return retval;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   115
        }
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   116
    }
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   117
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   118
    /* nothing was definitive. */
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   119
    *seconds = -1;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   120
    *percent = -1;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   121
    return SDL_POWERSTATE_UNKNOWN;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   122
}
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   123
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   124
/* vi: set ts=4 sw=4 expandtab: */