src/power/uikit/SDL_syspower.m
author Sam Lantinga <slouken@libsdl.org>
Fri, 08 Apr 2011 13:03:26 -0700
changeset 5535 96594ac5fd1a
parent 5262 b530ef003506
child 6044 35448a5ea044
permissions -rw-r--r--
SDL 1.3 is now under the zlib license.
icculus@4444
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@5535
     3
  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
icculus@4444
     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@4444
     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@4444
    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@4444
    20
*/
icculus@4444
    21
#include "SDL_config.h"
icculus@4444
    22
icculus@4444
    23
#ifndef SDL_POWER_DISABLED
icculus@4444
    24
#ifdef SDL_POWER_UIKIT
icculus@4444
    25
icculus@4444
    26
#import <UIKit/UIKit.h>
icculus@4444
    27
icculus@4444
    28
#include "SDL_power.h"
icculus@4444
    29
#include "SDL_timer.h"
icculus@4444
    30
#include "SDL_assert.h"
icculus@4444
    31
icculus@4444
    32
// turn off the battery monitor if it's been more than X ms since last check.
icculus@4444
    33
static const int BATTERY_MONITORING_TIMEOUT = 3000;
icculus@4444
    34
static Uint32 SDL_UIKitLastPowerInfoQuery = 0;
icculus@4444
    35
icculus@4444
    36
void
icculus@4444
    37
SDL_UIKit_UpdateBatteryMonitoring(void)
icculus@4444
    38
{
icculus@4444
    39
    if (SDL_UIKitLastPowerInfoQuery) {
icculus@4444
    40
        const Uint32 prev = SDL_UIKitLastPowerInfoQuery;
icculus@4444
    41
        const UInt32 now = SDL_GetTicks();
icculus@4444
    42
        const UInt32 ticks = now - prev;
icculus@4444
    43
        // if timer wrapped (now < prev), shut down, too.
icculus@4444
    44
        if ((now < prev) || (ticks >= BATTERY_MONITORING_TIMEOUT)) {
icculus@4444
    45
            UIDevice *uidev = [UIDevice currentDevice];
icculus@4444
    46
            SDL_assert([uidev isBatteryMonitoringEnabled] == YES);
icculus@4444
    47
            [uidev setBatteryMonitoringEnabled:NO];
icculus@4444
    48
            SDL_UIKitLastPowerInfoQuery = 0;
icculus@4444
    49
        }
icculus@4444
    50
    }
icculus@4444
    51
}
icculus@4444
    52
icculus@4444
    53
SDL_bool
icculus@4444
    54
SDL_GetPowerInfo_UIKit(SDL_PowerState * state, int *seconds, int *percent)
icculus@4444
    55
{
icculus@4444
    56
    UIDevice *uidev = [UIDevice currentDevice];
icculus@4444
    57
icculus@4444
    58
    if (!SDL_UIKitLastPowerInfoQuery) {
icculus@4444
    59
        SDL_assert([uidev isBatteryMonitoringEnabled] == NO);
icculus@4444
    60
        [uidev setBatteryMonitoringEnabled:YES];
icculus@4444
    61
    }
icculus@4444
    62
icculus@4444
    63
    // UIKit_GL_SwapWindow() (etc) will check this and disable the battery
icculus@4444
    64
    //  monitoring if the app hasn't queried it in the last X seconds.
icculus@4444
    65
    //  Apparently monitoring the battery burns battery life.  :)
icculus@4444
    66
    //  Apple's docs say not to monitor the battery unless you need it.
icculus@4444
    67
    SDL_UIKitLastPowerInfoQuery = SDL_GetTicks();
icculus@4444
    68
icculus@4444
    69
    *seconds = -1;   // no API to estimate this in UIKit.
icculus@4444
    70
icculus@4444
    71
    switch ([uidev batteryState])
icculus@4444
    72
    {
icculus@4444
    73
        case UIDeviceBatteryStateCharging:
icculus@4444
    74
            *state = SDL_POWERSTATE_CHARGING;
icculus@4444
    75
            break;
icculus@4444
    76
icculus@4444
    77
        case UIDeviceBatteryStateFull:
icculus@4444
    78
            *state = SDL_POWERSTATE_CHARGED;
icculus@4444
    79
            break;
icculus@4444
    80
icculus@4444
    81
        case UIDeviceBatteryStateUnplugged:
icculus@4444
    82
            *state = SDL_POWERSTATE_ON_BATTERY;
icculus@4444
    83
            break;
icculus@4444
    84
icculus@4444
    85
        case UIDeviceBatteryStateUnknown:
icculus@4444
    86
        default:
icculus@4444
    87
            *state = SDL_POWERSTATE_UNKNOWN;
icculus@4444
    88
            break;
icculus@4444
    89
    }
icculus@4444
    90
icculus@4444
    91
    const float level = [uidev batteryLevel];
icculus@4444
    92
    *percent = ( (level < 0.0f) ? -1 : (((int) (level + 0.5f)) * 100) );
icculus@4444
    93
    return SDL_TRUE;            /* always the definitive answer on iPhoneOS. */
icculus@4444
    94
}
icculus@4444
    95
icculus@4444
    96
#endif /* SDL_POWER_UIKIT */
icculus@4444
    97
#endif /* SDL_POWER_DISABLED */
icculus@4444
    98
icculus@4444
    99
/* vi: set ts=4 sw=4 expandtab: */