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