Skip to content

Commit

Permalink
Add initial battery_info and apple_battery_info
Browse files Browse the repository at this point in the history
  • Loading branch information
hrubymar10 authored and jyrkive committed Oct 30, 2018
1 parent 9513e43 commit 3cb3dbe
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 0 deletions.
16 changes: 16 additions & 0 deletions projectfiles/Xcode/Wesnoth.xcodeproj/project.pbxproj
Expand Up @@ -8,6 +8,8 @@

/* Begin PBXBuildFile section */
460CA6D52143362800B89741 /* apple_version.mm in Sources */ = {isa = PBXBuildFile; fileRef = 46F54C26211DFB7200374A1C /* apple_version.mm */; };
46181DCF2119F73A00908BC2 /* apple_battery_info.mm in Sources */ = {isa = PBXBuildFile; fileRef = 46181DCE2119F73A00908BC2 /* apple_battery_info.mm */; };
46181DD22119F74D00908BC2 /* battery_info.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 46181DD12119F74C00908BC2 /* battery_info.cpp */; };
461CE967205C3A8700E6E98F /* base64.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 469BDB53205C357400DBF748 /* base64.cpp */; };
463896712034180900075E54 /* deprecation.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 463896702034180900075E54 /* deprecation.cpp */; };
463896722034180900075E54 /* deprecation.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 463896702034180900075E54 /* deprecation.cpp */; };
Expand Down Expand Up @@ -45,6 +47,7 @@
46BDBBEB217C6F6200D2820C /* lua_terrainfilter.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 46BDBBE6217C6F6100D2820C /* lua_terrainfilter.cpp */; };
46BDBBEC217C6F6200D2820C /* lua_terrainmap.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 46BDBBE8217C6F6200D2820C /* lua_terrainmap.cpp */; };
46BDBBED217C6F6200D2820C /* lua_terrainmap.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 46BDBBE8217C6F6200D2820C /* lua_terrainmap.cpp */; };
46BCC905211A1E68003BDB8C /* IOKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 46BCC904211A1E68003BDB8C /* IOKit.framework */; };
46BED4D3205060EA00842FA5 /* crypt_blowfish.c in Sources */ = {isa = PBXBuildFile; fileRef = 46BED4D1205060EA00842FA5 /* crypt_blowfish.c */; };
46BED4D4205060EA00842FA5 /* crypt_blowfish.c in Sources */ = {isa = PBXBuildFile; fileRef = 46BED4D1205060EA00842FA5 /* crypt_blowfish.c */; };
46BED4D52050611600842FA5 /* crypt_blowfish.c in Sources */ = {isa = PBXBuildFile; fileRef = 46BED4D1205060EA00842FA5 /* crypt_blowfish.c */; };
Expand Down Expand Up @@ -1447,6 +1450,10 @@
/* Begin PBXFileReference section */
1058C7A1FEA54F0111CA2CBB /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = System/Library/Frameworks/Cocoa.framework; sourceTree = SDKROOT; };
1C58BBDF21822A930078D25A /* Security.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Security.framework; path = System/Library/Frameworks/Security.framework; sourceTree = SDKROOT; };
46181DCD2119F73A00908BC2 /* apple_battery_info.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = apple_battery_info.hpp; path = ../../src/desktop/apple_battery_info.hpp; sourceTree = "<group>"; };
46181DCE2119F73A00908BC2 /* apple_battery_info.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = apple_battery_info.mm; path = ../../src/desktop/apple_battery_info.mm; sourceTree = "<group>"; };
46181DD02119F74C00908BC2 /* battery_info.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = battery_info.hpp; sourceTree = "<group>"; };
46181DD12119F74C00908BC2 /* battery_info.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = battery_info.cpp; sourceTree = "<group>"; };
4638966F2034180800075E54 /* deprecation.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = deprecation.hpp; sourceTree = "<group>"; };
463896702034180900075E54 /* deprecation.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = deprecation.cpp; sourceTree = "<group>"; };
4649B879202886F000827CFB /* test_irdya_date.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = test_irdya_date.cpp; sourceTree = "<group>"; };
Expand All @@ -1463,6 +1470,7 @@
46BDBBE7217C6F6100D2820C /* lua_terrainfilter.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = lua_terrainfilter.hpp; sourceTree = "<group>"; };
46BDBBE8217C6F6200D2820C /* lua_terrainmap.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = lua_terrainmap.cpp; sourceTree = "<group>"; };
46BDBBE9217C6F6200D2820C /* lua_terrainmap.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = lua_terrainmap.hpp; sourceTree = "<group>"; };
46BCC904211A1E68003BDB8C /* IOKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = IOKit.framework; path = System/Library/Frameworks/IOKit.framework; sourceTree = SDKROOT; };
46BED4D0205060EA00842FA5 /* crypt_blowfish.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = crypt_blowfish.h; sourceTree = "<group>"; };
46BED4D1205060EA00842FA5 /* crypt_blowfish.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = crypt_blowfish.c; sourceTree = "<group>"; };
46BED4D92050710800842FA5 /* scenario-story.cfg */ = {isa = PBXFileReference; lastKnownFileType = text; path = "scenario-story.cfg"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2780,6 +2788,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
46BCC905211A1E68003BDB8C /* IOKit.framework in Frameworks */,
ECA9E7471CA20AA800A947D6 /* libreadline.7.0.dylib in Frameworks */,
8D11072F0486CEB800E47090 /* Cocoa.framework in Frameworks */,
F4D2A99614DAED0E00CAFF31 /* CoreFoundation.framework in Frameworks */,
Expand Down Expand Up @@ -2903,6 +2912,8 @@
080E96DDFE201D6D7F000001 /* Mac-specific */ = {
isa = PBXGroup;
children = (
46181DCD2119F73A00908BC2 /* apple_battery_info.hpp */,
46181DCE2119F73A00908BC2 /* apple_battery_info.mm */,
F40A13BD1A3AA56800C4D071 /* apple_notification.hpp */,
F40A13BB1A3A88BA00C4D071 /* apple_notification.mm */,
46F54C28211DFB9100374A1C /* apple_version.hpp */,
Expand Down Expand Up @@ -3254,6 +3265,7 @@
29B97323FDCFA39411CA2CEA /* Frameworks */ = {
isa = PBXGroup;
children = (
46BCC904211A1E68003BDB8C /* IOKit.framework */,
1058C7A2FEA54F0111CA2CBB /* Included Frameworks */,
46BAF784206672240004711F /* libboost_chrono-mt.dylib */,
ECF9D43F19F4026D00E6C9D9 /* libboost_filesystem-mt.dylib */,
Expand Down Expand Up @@ -4021,6 +4033,8 @@
91B621A41B76A6E900B00E0F /* desktop */ = {
isa = PBXGroup;
children = (
46181DD12119F74C00908BC2 /* battery_info.cpp */,
46181DD02119F74C00908BC2 /* battery_info.hpp */,
EC49A38119F34F1200E78528 /* clipboard.cpp */,
91B621B81B76B2AB00B00E0F /* clipboard.hpp */,
EC74C119197576F500B85A1A /* notifications.cpp */,
Expand Down Expand Up @@ -5343,6 +5357,7 @@
620EC87414FEEE6D006BBD1F /* state.cpp in Sources */,
B5599AFE0EC62181008DD061 /* statistics.cpp in Sources */,
46F92D852174F6A300602C1C /* vertical_list.cpp in Sources */,
46181DD22119F74D00908BC2 /* battery_info.cpp in Sources */,
B5599B080EC62181008DD061 /* string_utils.cpp in Sources */,
F4E4E0B11367241E001C7528 /* suppose_dead.cpp in Sources */,
EC84246C18F30D9100FC1EEF /* synced_checkup.cpp in Sources */,
Expand Down Expand Up @@ -5430,6 +5445,7 @@
EC219454196064C500420969 /* wesnoth.cpp in Sources */,
46F92F1F2174FEC000602C1C /* constants.cpp in Sources */,
B5599AE20EC62181008DD061 /* widget.cpp in Sources */,
46181DCF2119F73A00908BC2 /* apple_battery_info.mm in Sources */,
EC44E383181DDAF90037A62F /* wmi_manager.cpp in Sources */,
B5599AD30EC62181008DD061 /* wml_exception.cpp in Sources */,
EC2F60201A0490FC0018C9D6 /* xbrz.cpp in Sources */,
Expand Down
24 changes: 24 additions & 0 deletions src/desktop/apple_battery_info.hpp
@@ -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
99 changes: 99 additions & 0 deletions src/desktop/apple_battery_info.mm
@@ -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__
45 changes: 45 additions & 0 deletions src/desktop/battery_info.cpp
@@ -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
26 changes: 26 additions & 0 deletions src/desktop/battery_info.hpp
@@ -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

0 comments on commit 3cb3dbe

Please sign in to comment.