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