src/power/uikit/SDL_syspower.m
changeset 4444 42e1092225f9
child 5262 b530ef003506
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/power/uikit/SDL_syspower.m	Fri Apr 30 00:39:31 2010 -0400
     1.3 @@ -0,0 +1,100 @@
     1.4 +/*
     1.5 +    SDL - Simple DirectMedia Layer
     1.6 +    Copyright (C) 1997-2010 Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Lesser General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2.1 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Lesser General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Lesser General Public
    1.19 +    License along with this library; if not, write to the Free Software
    1.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    slouken@libsdl.org
    1.24 +*/
    1.25 +#include "SDL_config.h"
    1.26 +
    1.27 +#ifndef SDL_POWER_DISABLED
    1.28 +#ifdef SDL_POWER_UIKIT
    1.29 +
    1.30 +#import <UIKit/UIKit.h>
    1.31 +
    1.32 +#include "SDL_power.h"
    1.33 +#include "SDL_timer.h"
    1.34 +#include "SDL_assert.h"
    1.35 +
    1.36 +// turn off the battery monitor if it's been more than X ms since last check.
    1.37 +static const int BATTERY_MONITORING_TIMEOUT = 3000;
    1.38 +static Uint32 SDL_UIKitLastPowerInfoQuery = 0;
    1.39 +
    1.40 +void
    1.41 +SDL_UIKit_UpdateBatteryMonitoring(void)
    1.42 +{
    1.43 +    if (SDL_UIKitLastPowerInfoQuery) {
    1.44 +        const Uint32 prev = SDL_UIKitLastPowerInfoQuery;
    1.45 +        const UInt32 now = SDL_GetTicks();
    1.46 +        const UInt32 ticks = now - prev;
    1.47 +        // if timer wrapped (now < prev), shut down, too.
    1.48 +        if ((now < prev) || (ticks >= BATTERY_MONITORING_TIMEOUT)) {
    1.49 +            UIDevice *uidev = [UIDevice currentDevice];
    1.50 +            SDL_assert([uidev isBatteryMonitoringEnabled] == YES);
    1.51 +            [uidev setBatteryMonitoringEnabled:NO];
    1.52 +            SDL_UIKitLastPowerInfoQuery = 0;
    1.53 +        }
    1.54 +    }
    1.55 +}
    1.56 +
    1.57 +SDL_bool
    1.58 +SDL_GetPowerInfo_UIKit(SDL_PowerState * state, int *seconds, int *percent)
    1.59 +{
    1.60 +    UIDevice *uidev = [UIDevice currentDevice];
    1.61 +
    1.62 +    if (!SDL_UIKitLastPowerInfoQuery) {
    1.63 +        SDL_assert([uidev isBatteryMonitoringEnabled] == NO);
    1.64 +        [uidev setBatteryMonitoringEnabled:YES];
    1.65 +    }
    1.66 +
    1.67 +    // UIKit_GL_SwapWindow() (etc) will check this and disable the battery
    1.68 +    //  monitoring if the app hasn't queried it in the last X seconds.
    1.69 +    //  Apparently monitoring the battery burns battery life.  :)
    1.70 +    //  Apple's docs say not to monitor the battery unless you need it.
    1.71 +    SDL_UIKitLastPowerInfoQuery = SDL_GetTicks();
    1.72 +
    1.73 +    *seconds = -1;   // no API to estimate this in UIKit.
    1.74 +
    1.75 +    switch ([uidev batteryState])
    1.76 +    {
    1.77 +        case UIDeviceBatteryStateCharging:
    1.78 +            *state = SDL_POWERSTATE_CHARGING;
    1.79 +            break;
    1.80 +
    1.81 +        case UIDeviceBatteryStateFull:
    1.82 +            *state = SDL_POWERSTATE_CHARGED;
    1.83 +            break;
    1.84 +
    1.85 +        case UIDeviceBatteryStateUnplugged:
    1.86 +            *state = SDL_POWERSTATE_ON_BATTERY;
    1.87 +            break;
    1.88 +
    1.89 +        case UIDeviceBatteryStateUnknown:
    1.90 +        default:
    1.91 +            *state = SDL_POWERSTATE_UNKNOWN;
    1.92 +            break;
    1.93 +    }
    1.94 +
    1.95 +    const float level = [uidev batteryLevel];
    1.96 +    *percent = ( (level < 0.0f) ? -1 : (((int) (level + 0.5f)) * 100) );
    1.97 +    return SDL_TRUE;            /* always the definitive answer on iPhoneOS. */
    1.98 +}
    1.99 +
   1.100 +#endif /* SDL_POWER_UIKIT */
   1.101 +#endif /* SDL_POWER_DISABLED */
   1.102 +
   1.103 +/* vi: set ts=4 sw=4 expandtab: */