src/power/SDL_power.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 12 Aug 2016 19:59:00 -0400
changeset 10266 c09f06c4e8c8
parent 9998 f67cf37e9cd4
permissions -rw-r--r--
emscripten: send fake mouse events for touches, like other targets do. (This really should be handled at the higher level and not in the individual targets, but this fixes the immediate bug.)
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
9998
f67cf37e9cd4 Updated copyright to 2016
Sam Lantinga <slouken@libsdl.org>
parents: 9700
diff changeset
     3
  Copyright (C) 1997-2016 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
9700
d8cc85e50561 Linux: Implemented sysfs-based version of SDL_GetPowerInfo().
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
    32
SDL_bool SDL_GetPowerInfo_Linux_sys_class_power_supply(SDL_PowerState *, int *, int *);
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
    33
SDL_bool SDL_GetPowerInfo_Linux_proc_acpi(SDL_PowerState *, int *, int *);
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
    34
SDL_bool SDL_GetPowerInfo_Linux_proc_apm(SDL_PowerState *, int *, int *);
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
    35
SDL_bool SDL_GetPowerInfo_Windows(SDL_PowerState *, int *, int *);
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
    36
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
    37
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
    38
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
    39
SDL_bool SDL_GetPowerInfo_Android(SDL_PowerState *, int *, int *);
7009
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents: 6885
diff changeset
    40
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
    41
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
    42
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
    43
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    44
#ifndef SDL_POWER_DISABLED
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    45
#ifdef SDL_POWER_HARDWIRED
3272
e8f63b28947d Removed outdated Dreamcast support
Sam Lantinga <slouken@libsdl.org>
parents: 3269
diff changeset
    46
/* 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
    47
static SDL_bool
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
    48
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
    49
{
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    50
    *seconds = -1;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
    *percent = -1;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    52
    *state = SDL_POWERSTATE_NO_BATTERY;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    53
    return SDL_TRUE;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    54
}
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
#endif
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
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
static SDL_GetPowerInfo_Impl implementations[] = {
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    60
#ifndef SDL_POWER_DISABLED
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
    61
#ifdef SDL_POWER_LINUX          /* in order of preference. More than could work. */
9700
d8cc85e50561 Linux: Implemented sysfs-based version of SDL_GetPowerInfo().
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
    62
    SDL_GetPowerInfo_Linux_sys_class_power_supply,
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
    63
    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
    64
    SDL_GetPowerInfo_Linux_proc_apm,
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    65
#endif
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
    66
#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
    67
    SDL_GetPowerInfo_Windows,
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    68
#endif
4444
42e1092225f9 Implemented SDL power APIs for iPhoneOS.
Ryan C. Gordon <icculus@icculus.org>
parents: 3697
diff changeset
    69
#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
    70
    SDL_GetPowerInfo_UIKit,
42e1092225f9 Implemented SDL power APIs for iPhoneOS.
Ryan C. Gordon <icculus@icculus.org>
parents: 3697
diff changeset
    71
#endif
42e1092225f9 Implemented SDL power APIs for iPhoneOS.
Ryan C. Gordon <icculus@icculus.org>
parents: 3697
diff changeset
    72
#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
    73
    SDL_GetPowerInfo_MacOSX,
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    74
#endif
7981
6d538bc1b395 Renamed things named after BeOS to be named after Haiku instead.
Ryan C. Gordon <icculus@icculus.org>
parents: 7011
diff changeset
    75
#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
    76
    SDL_GetPowerInfo_Haiku,
3173
510e3f36c04a BeOS support for power subsystem.
Ryan C. Gordon <icculus@icculus.org>
parents: 3170
diff changeset
    77
#endif
6448
64a6297a8b93 Fixed bug 1606 - SDL does not implement SDL_GetPowerInfo() for Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
    78
#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
    79
    SDL_GetPowerInfo_Android,
64a6297a8b93 Fixed bug 1606 - SDL does not implement SDL_GetPowerInfo() for Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
    80
#endif
7009
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents: 6885
diff changeset
    81
#ifdef SDL_POWER_PSP        /* handles PSP. */
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents: 6885
diff changeset
    82
    SDL_GetPowerInfo_PSP,
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents: 6885
diff changeset
    83
#endif
8611
a762dfc67e1a Fixed various build and runtime errors when using WinRT with VS2012.
David Ludwig <dludwig@pobox.com>
parents: 8149
diff changeset
    84
#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
    85
    SDL_GetPowerInfo_WinRT,
a762dfc67e1a Fixed various build and runtime errors when using WinRT with VS2012.
David Ludwig <dludwig@pobox.com>
parents: 8149
diff changeset
    86
#endif
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8820
diff changeset
    87
#ifdef SDL_POWER_EMSCRIPTEN     /* handles Emscripten */
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8820
diff changeset
    88
    SDL_GetPowerInfo_Emscripten,
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8820
diff changeset
    89
#endif
7009
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents: 6885
diff changeset
    90
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    91
#ifdef SDL_POWER_HARDWIRED
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    92
    SDL_GetPowerInfo_Hardwired,
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    93
#endif
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    94
#endif
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    95
};
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    96
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    97
SDL_PowerState
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    98
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
    99
{
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3174
diff changeset
   100
    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
   101
    int _seconds, _percent;
8820
0e935d5b193a Added annotations to help code analysis tools
Sam Lantinga <slouken@libsdl.org>
parents: 8611
diff changeset
   102
    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
   103
    int i;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   104
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   105
    /* 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
   106
    if (seconds == NULL) {
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   107
        seconds = &_seconds;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   108
    }
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   109
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   110
    if (percent == NULL) {
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   111
        percent = &_percent;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   112
    }
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   113
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   114
    for (i = 0; i < total; i++) {
8820
0e935d5b193a Added annotations to help code analysis tools
Sam Lantinga <slouken@libsdl.org>
parents: 8611
diff changeset
   115
        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
   116
            return retval;
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
    }
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   119
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   120
    /* nothing was definitive. */
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   121
    *seconds = -1;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   122
    *percent = -1;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   123
    return SDL_POWERSTATE_UNKNOWN;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   124
}
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   125
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   126
/* vi: set ts=4 sw=4 expandtab: */