Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial battery_info and apple_battery_info
- Loading branch information
1 parent
9513e43
commit 3cb3dbe
Showing
5 changed files
with
210 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
Copyright (C) 2018 by Martin Hrubý <hrubymar10@gmail.com> | ||
Part of the Battle for Wesnoth Project https://www.wesnoth.org/ | ||
This program is free software; you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation; either version 2 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY. | ||
See the COPYING file for more details. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include "battery_info.hpp" | ||
|
||
namespace apple_battery_info { | ||
|
||
bool does_device_have_battery(); | ||
double get_battery_percentage(); | ||
|
||
} // end namespace battery_info |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
Copyright (C) 2018 by Martin Hrubý <hrubymar10@gmail.com> | ||
Part of the Battle for Wesnoth Project https://www.wesnoth.org/ | ||
This program is free software; you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation; either version 2 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY. | ||
See the COPYING file for more details. | ||
*/ | ||
|
||
#ifdef __APPLE__ | ||
|
||
#include "apple_battery_info.hpp" | ||
|
||
#if defined(__IPHONEOS__) | ||
|
||
#import <UIKit/UIDevice.h> | ||
|
||
#else | ||
|
||
#import <Foundation/Foundation.h> | ||
#import <IOKit/ps/IOPowerSources.h> | ||
#import <IOKit/ps/IOPSKeys.h> | ||
#import <IOKit/pwr_mgt/IOPM.h> | ||
|
||
#endif | ||
|
||
namespace apple_battery_info { | ||
|
||
bool does_device_have_battery() { | ||
#if defined(__IPHONEOS__) | ||
UIDevice.currentDevice.batteryMonitoringEnabled = YES; | ||
if (UIDevice.currentDevice.batteryState == UIDeviceBatteryStateUnknown) { | ||
return false; | ||
} else { | ||
return true; | ||
} | ||
#else | ||
if (get_battery_percentage() == -1) { | ||
return false; | ||
} else { | ||
return true; | ||
} | ||
#endif | ||
} | ||
|
||
#if !defined(__IPHONEOS__) | ||
inline NSDictionary* get_iops_battery_info() { | ||
//Code taken from https://github.com/Hammerspoon/hammerspoon/blob/master/extensions/battery/internal.m | ||
CFTypeRef info = IOPSCopyPowerSourcesInfo(); | ||
|
||
if (info == NULL) | ||
return NULL; | ||
|
||
|
||
CFArrayRef list = IOPSCopyPowerSourcesList(info); | ||
|
||
if (list == NULL || !CFArrayGetCount(list)) { | ||
if (list) | ||
CFRelease(list); | ||
|
||
CFRelease(info); | ||
return NULL; | ||
} | ||
|
||
CFDictionaryRef battery = CFDictionaryCreateCopy(NULL, IOPSGetPowerSourceDescription(info, CFArrayGetValueAtIndex(list, 0))); | ||
|
||
CFRelease(list); | ||
CFRelease(info); | ||
|
||
return (NSDictionary*) battery; | ||
} | ||
#endif | ||
|
||
double get_battery_percentage() { | ||
#if defined(__IPHONEOS__) | ||
return UIDevice.currentDevice.batteryLevel * 100; | ||
#else | ||
//Code taken from https://github.com/Hammerspoon/hammerspoon/blob/master/extensions/battery/internal.m | ||
NSDictionary* battery = get_iops_battery_info(); | ||
|
||
NSNumber *maxCapacity = [battery objectForKey:@kIOPSMaxCapacityKey]; | ||
NSNumber *currentCapacity = [battery objectForKey:@kIOPSCurrentCapacityKey]; | ||
|
||
if (maxCapacity && currentCapacity) { | ||
return ([currentCapacity doubleValue] / [maxCapacity doubleValue]) * 100; | ||
} else { | ||
return -1; | ||
} | ||
#endif | ||
} | ||
|
||
} // end namespace apple_battery_info | ||
|
||
#endif //end __APPLE__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
Copyright (C) 2018 by Martin Hrubý <hrubymar10@gmail.com> | ||
Part of the Battle for Wesnoth Project https://www.wesnoth.org/ | ||
This program is free software; you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation; either version 2 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY. | ||
See the COPYING file for more details. | ||
*/ | ||
|
||
#include "battery_info.hpp" | ||
|
||
#ifdef __APPLE__ | ||
#include "apple_battery_info.hpp" | ||
#endif | ||
|
||
namespace desktop { | ||
|
||
namespace battery_info { | ||
|
||
bool does_device_have_battery() | ||
{ | ||
#if defined(__APPLE__) | ||
return apple_battery_info::does_device_have_battery(); | ||
#else | ||
return false; | ||
#endif | ||
} | ||
|
||
double get_battery_percentage() | ||
{ | ||
#if defined(__APPLE__) | ||
return apple_battery_info::get_battery_percentage(); | ||
#else | ||
return -1; | ||
#endif | ||
} | ||
|
||
} // end namespace battery_info | ||
|
||
} // end namespace desktop |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
Copyright (C) 2018 by Martin Hrubý <hrubymar10@gmail.com> | ||
Part of the Battle for Wesnoth Project https://www.wesnoth.org/ | ||
This program is free software; you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation; either version 2 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY. | ||
See the COPYING file for more details. | ||
*/ | ||
|
||
#pragma once | ||
|
||
namespace desktop { | ||
|
||
namespace battery_info { | ||
|
||
bool does_device_have_battery(); | ||
double get_battery_percentage(); | ||
|
||
} // end namespace battery_info | ||
|
||
} // end namespace desktop |