sdl

FORK: Simple Directmedia Layer
git clone https://git.neptards.moe/neptards/sdl.git
Log | Files | Refs

SDL_syspower.m (3555B)


      1 /*
      2   Simple DirectMedia Layer
      3   Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
      4 
      5   This software is provided 'as-is', without any express or implied
      6   warranty.  In no event will the authors be held liable for any damages
      7   arising from the use of this software.
      8 
      9   Permission is granted to anyone to use this software for any purpose,
     10   including commercial applications, and to alter it and redistribute it
     11   freely, subject to the following restrictions:
     12 
     13   1. The origin of this software must not be misrepresented; you must not
     14      claim that you wrote the original software. If you use this software
     15      in a product, an acknowledgment in the product documentation would be
     16      appreciated but is not required.
     17   2. Altered source versions must be plainly marked as such, and must not be
     18      misrepresented as being the original software.
     19   3. This notice may not be removed or altered from any source distribution.
     20 */
     21 #include "../../SDL_internal.h"
     22 
     23 #ifndef SDL_POWER_DISABLED
     24 #if SDL_POWER_UIKIT
     25 
     26 #import <UIKit/UIKit.h>
     27 
     28 #include "SDL_power.h"
     29 #include "SDL_timer.h"
     30 #include "SDL_syspower.h"
     31 
     32 #if !TARGET_OS_TV
     33 /* turn off the battery monitor if it's been more than X ms since last check. */
     34 static const int BATTERY_MONITORING_TIMEOUT = 3000;
     35 static Uint32 SDL_UIKitLastPowerInfoQuery = 0;
     36 
     37 void
     38 SDL_UIKit_UpdateBatteryMonitoring(void)
     39 {
     40     if (SDL_UIKitLastPowerInfoQuery) {
     41         if (SDL_TICKS_PASSED(SDL_GetTicks(), SDL_UIKitLastPowerInfoQuery + BATTERY_MONITORING_TIMEOUT)) {
     42             UIDevice *uidev = [UIDevice currentDevice];
     43             SDL_assert([uidev isBatteryMonitoringEnabled] == YES);
     44             [uidev setBatteryMonitoringEnabled:NO];
     45             SDL_UIKitLastPowerInfoQuery = 0;
     46         }
     47     }
     48 }
     49 #else
     50 void
     51 SDL_UIKit_UpdateBatteryMonitoring(void)
     52 {
     53     /* Do nothing. */
     54 }
     55 #endif /* !TARGET_OS_TV */
     56 
     57 SDL_bool
     58 SDL_GetPowerInfo_UIKit(SDL_PowerState * state, int *seconds, int *percent)
     59 {
     60 #if TARGET_OS_TV
     61     *state = SDL_POWERSTATE_NO_BATTERY;
     62     *seconds = -1;
     63     *percent = -1;
     64 #else /* TARGET_OS_TV */
     65     @autoreleasepool {
     66         UIDevice *uidev = [UIDevice currentDevice];
     67 
     68         if (!SDL_UIKitLastPowerInfoQuery) {
     69             SDL_assert(uidev.isBatteryMonitoringEnabled == NO);
     70             uidev.batteryMonitoringEnabled = YES;
     71         }
     72 
     73         /* UIKit_GL_SwapWindow() (etc) will check this and disable the battery
     74          *  monitoring if the app hasn't queried it in the last X seconds.
     75          *  Apparently monitoring the battery burns battery life.  :)
     76          *  Apple's docs say not to monitor the battery unless you need it.
     77          */
     78         SDL_UIKitLastPowerInfoQuery = SDL_GetTicks();
     79 
     80         *seconds = -1;   /* no API to estimate this in UIKit. */
     81 
     82         switch (uidev.batteryState) {
     83         case UIDeviceBatteryStateCharging:
     84             *state = SDL_POWERSTATE_CHARGING;
     85             break;
     86 
     87         case UIDeviceBatteryStateFull:
     88             *state = SDL_POWERSTATE_CHARGED;
     89             break;
     90 
     91         case UIDeviceBatteryStateUnplugged:
     92             *state = SDL_POWERSTATE_ON_BATTERY;
     93             break;
     94 
     95         case UIDeviceBatteryStateUnknown:
     96         default:
     97             *state = SDL_POWERSTATE_UNKNOWN;
     98             break;
     99         }
    100 
    101         const float level = uidev.batteryLevel;
    102         *percent = ( (level < 0.0f) ? -1 : ((int) ((level * 100) + 0.5f)) );
    103     }
    104 #endif /* TARGET_OS_TV */
    105 
    106     return SDL_TRUE; /* always the definitive answer on iOS. */
    107 }
    108 
    109 #endif /* SDL_POWER_UIKIT */
    110 #endif /* SDL_POWER_DISABLED */
    111 
    112 /* vi: set ts=4 sw=4 expandtab: */