src/power/uikit/SDL_syspower.m
author Sam Lantinga <slouken@libsdl.org>
Sun, 02 Feb 2014 00:53:27 -0800
changeset 8149 681eb46b8ac4
parent 8093 b43765095a6f
child 8222 01a7b69c4fb7
permissions -rw-r--r--
Fixed bug 2374 - Update copyright for 2014...

Is it that time already??
icculus@4444
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@8149
     3
  Copyright (C) 1997-2014 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@8093
    21
#include "../../SDL_internal.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
slouken@7191
    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) {
slouken@7857
    41
        if (SDL_TICKS_PASSED(SDL_GetTicks(), SDL_UIKitLastPowerInfoQuery + BATTERY_MONITORING_TIMEOUT)) {
icculus@4444
    42
            UIDevice *uidev = [UIDevice currentDevice];
icculus@4444
    43
            SDL_assert([uidev isBatteryMonitoringEnabled] == YES);
icculus@4444
    44
            [uidev setBatteryMonitoringEnabled:NO];
icculus@4444
    45
            SDL_UIKitLastPowerInfoQuery = 0;
icculus@4444
    46
        }
icculus@4444
    47
    }
icculus@4444
    48
}
icculus@4444
    49
icculus@4444
    50
SDL_bool
icculus@4444
    51
SDL_GetPowerInfo_UIKit(SDL_PowerState * state, int *seconds, int *percent)
icculus@4444
    52
{
icculus@4444
    53
    UIDevice *uidev = [UIDevice currentDevice];
icculus@4444
    54
icculus@4444
    55
    if (!SDL_UIKitLastPowerInfoQuery) {
icculus@4444
    56
        SDL_assert([uidev isBatteryMonitoringEnabled] == NO);
icculus@4444
    57
        [uidev setBatteryMonitoringEnabled:YES];
icculus@4444
    58
    }
icculus@4444
    59
slouken@7191
    60
    /* UIKit_GL_SwapWindow() (etc) will check this and disable the battery
slouken@7191
    61
     *  monitoring if the app hasn't queried it in the last X seconds.
slouken@7191
    62
     *  Apparently monitoring the battery burns battery life.  :)
slouken@7191
    63
     *  Apple's docs say not to monitor the battery unless you need it.
slouken@7191
    64
     */
icculus@4444
    65
    SDL_UIKitLastPowerInfoQuery = SDL_GetTicks();
icculus@4444
    66
slouken@7191
    67
    *seconds = -1;   /* no API to estimate this in UIKit. */
icculus@4444
    68
icculus@4444
    69
    switch ([uidev batteryState])
icculus@4444
    70
    {
icculus@4444
    71
        case UIDeviceBatteryStateCharging:
icculus@4444
    72
            *state = SDL_POWERSTATE_CHARGING;
icculus@4444
    73
            break;
icculus@4444
    74
icculus@4444
    75
        case UIDeviceBatteryStateFull:
icculus@4444
    76
            *state = SDL_POWERSTATE_CHARGED;
icculus@4444
    77
            break;
icculus@4444
    78
icculus@4444
    79
        case UIDeviceBatteryStateUnplugged:
icculus@4444
    80
            *state = SDL_POWERSTATE_ON_BATTERY;
icculus@4444
    81
            break;
icculus@4444
    82
icculus@4444
    83
        case UIDeviceBatteryStateUnknown:
icculus@4444
    84
        default:
icculus@4444
    85
            *state = SDL_POWERSTATE_UNKNOWN;
icculus@4444
    86
            break;
icculus@4444
    87
    }
icculus@4444
    88
icculus@4444
    89
    const float level = [uidev batteryLevel];
icculus@4444
    90
    *percent = ( (level < 0.0f) ? -1 : (((int) (level + 0.5f)) * 100) );
icculus@4444
    91
    return SDL_TRUE;            /* always the definitive answer on iPhoneOS. */
icculus@4444
    92
}
icculus@4444
    93
icculus@4444
    94
#endif /* SDL_POWER_UIKIT */
icculus@4444
    95
#endif /* SDL_POWER_DISABLED */
icculus@4444
    96
icculus@4444
    97
/* vi: set ts=4 sw=4 expandtab: */