The Battle for Wesnoth  1.15.1+dev
apple_battery_info.mm
Go to the documentation of this file.
1 /*
2  Copyright (C) 2018 by Martin HrubĂ˝ <hrubymar10@gmail.com>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13  */
14 
15 #ifdef __APPLE__
16 
17 #include "apple_battery_info.hpp"
18 
19 #if defined(__APPLE__) && defined(__MACH__) && defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__)
20 #define __IPHONEOS__ (__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__*1000)
21 #endif
22 
23 #if defined(__IPHONEOS__)
24 
25 #import <UIKit/UIDevice.h>
26 
27 #else
28 
29 #import <Foundation/Foundation.h>
30 #import <IOKit/ps/IOPowerSources.h>
31 #import <IOKit/ps/IOPSKeys.h>
32 #import <IOKit/pwr_mgt/IOPM.h>
33 
34 #endif
35 
36 namespace desktop {
37 namespace battery_info {
38 namespace apple {
39 
41 #if defined(__IPHONEOS__)
42  UIDevice.currentDevice.batteryMonitoringEnabled = YES;
43  if (UIDevice.currentDevice.batteryState == UIDeviceBatteryStateUnknown) {
44  return false;
45  } else {
46  return true;
47  }
48 #else
49  if (get_battery_percentage() == -1) {
50  return false;
51  } else {
52  return true;
53  }
54 #endif
55 }
56 
57 #if !defined(__IPHONEOS__)
58 inline NSDictionary* get_iops_battery_info() {
59  //Code taken from https://github.com/Hammerspoon/hammerspoon/blob/master/extensions/battery/internal.m
60  CFTypeRef info = IOPSCopyPowerSourcesInfo();
61 
62  if (info == NULL)
63  return NULL;
64 
65 
66  CFArrayRef list = IOPSCopyPowerSourcesList(info);
67 
68  if (list == NULL || !CFArrayGetCount(list)) {
69  if (list)
70  CFRelease(list);
71 
72  CFRelease(info);
73  return NULL;
74  }
75 
76  CFDictionaryRef battery = CFDictionaryCreateCopy(NULL, IOPSGetPowerSourceDescription(info, CFArrayGetValueAtIndex(list, 0)));
77 
78  CFRelease(list);
79  CFRelease(info);
80 
81  return static_cast<NSDictionary*>(battery);
82 }
83 #endif
84 
85 double get_battery_percentage() {
86 #if defined(__IPHONEOS__)
87  return UIDevice.currentDevice.batteryLevel * 100;
88 #else
89  //Code taken from https://github.com/Hammerspoon/hammerspoon/blob/master/extensions/battery/internal.m
90  NSDictionary* battery = get_iops_battery_info();
91 
92  NSNumber *maxCapacity = [battery objectForKey:@kIOPSMaxCapacityKey];
93  NSNumber *currentCapacity = [battery objectForKey:@kIOPSCurrentCapacityKey];
94 
95  if (maxCapacity && currentCapacity) {
96  return ([currentCapacity doubleValue] / [maxCapacity doubleValue]) * 100;
97  } else {
98  return -1;
99  }
100 #endif
101 }
102 
103 } // end namespace apple
104 } // end namespace battery_info
105 } // end namespace desktop
106 
107 #endif //end __APPLE__
logger & info()
Definition: log.cpp:90