src/power/uikit/SDL_syspower.m
author Ryan C. Gordon
Wed, 18 Jan 2017 02:11:56 -0500
changeset 10817 efc103e60c5b
parent 10737 3406a0f8b041
child 11811 5d94cb6b24d3
permissions -rw-r--r--
audio: Several fixes to "simple" resampler (thanks, Vitaly!).

Fixes Bugzilla #3551.
icculus@4444
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@10737
     3
  Copyright (C) 1997-2017 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
slime73@10340
    33
#if !TARGET_OS_TV
slouken@7191
    34
/* turn off the battery monitor if it's been more than X ms since last check. */
icculus@4444
    35
static const int BATTERY_MONITORING_TIMEOUT = 3000;
icculus@4444
    36
static Uint32 SDL_UIKitLastPowerInfoQuery = 0;
icculus@4444
    37
icculus@4444
    38
void
icculus@4444
    39
SDL_UIKit_UpdateBatteryMonitoring(void)
icculus@4444
    40
{
icculus@4444
    41
    if (SDL_UIKitLastPowerInfoQuery) {
slouken@7857
    42
        if (SDL_TICKS_PASSED(SDL_GetTicks(), SDL_UIKitLastPowerInfoQuery + BATTERY_MONITORING_TIMEOUT)) {
icculus@4444
    43
            UIDevice *uidev = [UIDevice currentDevice];
icculus@4444
    44
            SDL_assert([uidev isBatteryMonitoringEnabled] == YES);
icculus@4444
    45
            [uidev setBatteryMonitoringEnabled:NO];
icculus@4444
    46
            SDL_UIKitLastPowerInfoQuery = 0;
icculus@4444
    47
        }
icculus@4444
    48
    }
icculus@4444
    49
}
slime73@10340
    50
#else
slime73@10340
    51
void
slime73@10340
    52
SDL_UIKit_UpdateBatteryMonitoring(void)
slime73@10340
    53
{
slime73@10340
    54
    /* Do nothing. */
slime73@10340
    55
}
slime73@10340
    56
#endif /* !TARGET_OS_TV */
icculus@4444
    57
icculus@4444
    58
SDL_bool
icculus@4444
    59
SDL_GetPowerInfo_UIKit(SDL_PowerState * state, int *seconds, int *percent)
icculus@4444
    60
{
slime73@10340
    61
#if TARGET_OS_TV
slime73@10340
    62
    *state = SDL_POWERSTATE_NO_BATTERY;
slime73@10340
    63
    *seconds = -1;
slime73@10340
    64
    *percent = -1;
slime73@10340
    65
#else /* TARGET_OS_TV */
slime73@9506
    66
    @autoreleasepool {
slime73@9506
    67
        UIDevice *uidev = [UIDevice currentDevice];
icculus@4444
    68
slime73@9506
    69
        if (!SDL_UIKitLastPowerInfoQuery) {
slime73@9506
    70
            SDL_assert(uidev.isBatteryMonitoringEnabled == NO);
slime73@9506
    71
            uidev.batteryMonitoringEnabled = YES;
slime73@9506
    72
        }
icculus@4444
    73
slime73@9506
    74
        /* UIKit_GL_SwapWindow() (etc) will check this and disable the battery
slime73@9506
    75
         *  monitoring if the app hasn't queried it in the last X seconds.
slime73@9506
    76
         *  Apparently monitoring the battery burns battery life.  :)
slime73@9506
    77
         *  Apple's docs say not to monitor the battery unless you need it.
slime73@9506
    78
         */
slime73@9506
    79
        SDL_UIKitLastPowerInfoQuery = SDL_GetTicks();
icculus@4444
    80
slime73@9506
    81
        *seconds = -1;   /* no API to estimate this in UIKit. */
icculus@4444
    82
slime73@9506
    83
        switch (uidev.batteryState) {
icculus@4444
    84
        case UIDeviceBatteryStateCharging:
icculus@4444
    85
            *state = SDL_POWERSTATE_CHARGING;
icculus@4444
    86
            break;
icculus@4444
    87
icculus@4444
    88
        case UIDeviceBatteryStateFull:
icculus@4444
    89
            *state = SDL_POWERSTATE_CHARGED;
icculus@4444
    90
            break;
icculus@4444
    91
icculus@4444
    92
        case UIDeviceBatteryStateUnplugged:
icculus@4444
    93
            *state = SDL_POWERSTATE_ON_BATTERY;
icculus@4444
    94
            break;
icculus@4444
    95
icculus@4444
    96
        case UIDeviceBatteryStateUnknown:
icculus@4444
    97
        default:
icculus@4444
    98
            *state = SDL_POWERSTATE_UNKNOWN;
icculus@4444
    99
            break;
slime73@9506
   100
        }
icculus@4444
   101
slime73@9506
   102
        const float level = uidev.batteryLevel;
slime73@9506
   103
        *percent = ( (level < 0.0f) ? -1 : ((int) ((level * 100) + 0.5f)) );
slime73@9506
   104
    }
slime73@10340
   105
#endif /* TARGET_OS_TV */
slime73@10340
   106
slime73@10340
   107
    return SDL_TRUE; /* always the definitive answer on iOS. */
icculus@4444
   108
}
icculus@4444
   109
icculus@4444
   110
#endif /* SDL_POWER_UIKIT */
icculus@4444
   111
#endif /* SDL_POWER_DISABLED */
icculus@4444
   112
icculus@4444
   113
/* vi: set ts=4 sw=4 expandtab: */