Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add BDPowerInfo

  • Loading branch information...
commit 3b3fa0aff15614be75b9520ea6ba967d41941f92 1 parent 883e0ca
@MSch authored
View
12 Battery Doom.xcodeproj/project.pbxproj
@@ -7,6 +7,8 @@
objects = {
/* Begin PBXBuildFile section */
+ C53F47901494F01400C6657A /* BDPowerInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = C53F478F1494F01400C6657A /* BDPowerInfo.m */; };
+ C53F47931494F38F00C6657A /* IOKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C53F47911494F37E00C6657A /* IOKit.framework */; };
C57ACFAC1494BE60003DBA8A /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C57ACFAB1494BE60003DBA8A /* Cocoa.framework */; };
C57ACFB61494BE60003DBA8A /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = C57ACFB41494BE60003DBA8A /* InfoPlist.strings */; };
C57ACFB81494BE60003DBA8A /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = C57ACFB71494BE60003DBA8A /* main.m */; };
@@ -19,6 +21,9 @@
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
+ C53F478E1494F01400C6657A /* BDPowerInfo.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BDPowerInfo.h; sourceTree = "<group>"; };
+ C53F478F1494F01400C6657A /* BDPowerInfo.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BDPowerInfo.m; sourceTree = "<group>"; };
+ C53F47911494F37E00C6657A /* IOKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = IOKit.framework; path = System/Library/Frameworks/IOKit.framework; sourceTree = SDKROOT; };
C57ACFA71494BE60003DBA8A /* Battery Doom.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Battery Doom.app"; sourceTree = BUILT_PRODUCTS_DIR; };
C57ACFAB1494BE60003DBA8A /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = System/Library/Frameworks/Cocoa.framework; sourceTree = SDKROOT; };
C57ACFB31494BE60003DBA8A /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
@@ -41,8 +46,9 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
- C57ACFD31494C826003DBA8A /* Quartz.framework in Frameworks */,
C57ACFAC1494BE60003DBA8A /* Cocoa.framework in Frameworks */,
+ C53F47931494F38F00C6657A /* IOKit.framework in Frameworks */,
+ C57ACFD31494C826003DBA8A /* Quartz.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -70,6 +76,7 @@
isa = PBXGroup;
children = (
C57ACFAB1494BE60003DBA8A /* Cocoa.framework */,
+ C53F47911494F37E00C6657A /* IOKit.framework */,
C57ACFD21494C826003DBA8A /* Quartz.framework */,
);
name = Frameworks;
@@ -80,6 +87,8 @@
children = (
C57ACFBD1494BE60003DBA8A /* BDAppDelegate.h */,
C57ACFBE1494BE60003DBA8A /* BDAppDelegate.m */,
+ C53F478E1494F01400C6657A /* BDPowerInfo.h */,
+ C53F478F1494F01400C6657A /* BDPowerInfo.m */,
C57ACFCF1494C331003DBA8A /* BDView.h */,
C57ACFD01494C331003DBA8A /* BDView.m */,
C57ACFDF1494D5C5003DBA8A /* BDWindow.h */,
@@ -170,6 +179,7 @@
C57ACFBF1494BE60003DBA8A /* BDAppDelegate.m in Sources */,
C57ACFD11494C331003DBA8A /* BDView.m in Sources */,
C57ACFE11494D5C5003DBA8A /* BDWindow.m in Sources */,
+ C53F47901494F01400C6657A /* BDPowerInfo.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
View
6 Sources/BDAppDelegate.m
@@ -1,10 +1,16 @@
#import "BDAppDelegate.h"
+#import "BDPowerInfo.h"
@implementation BDAppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
[BDWindow show];
[BDWindow performSelector:@selector(hide) withObject:nil afterDelay:2];
+
+ BDPowerInfo *powerInfo = [BDPowerInfo sharedPowerInfo];
+ NSLog(@"%ld", powerInfo.remainingCharge);
+ NSLog(@"%d", powerInfo.chargingBattery);
+ NSLog(@"%d", powerInfo.runningOnBattery);
}
@end
View
11 Sources/BDPowerInfo.h
@@ -0,0 +1,11 @@
+#import <Foundation/Foundation.h>
+
+@interface BDPowerInfo : NSObject
+
+@property (nonatomic, readonly) NSInteger remainingCharge;
+@property (nonatomic, readonly, getter=isRunningOnBattery) BOOL runningOnBattery;
+@property (nonatomic, readonly, getter=isChargingBattery) BOOL chargingBattery;
+
++ (id)sharedPowerInfo;
+
+@end
View
56 Sources/BDPowerInfo.m
@@ -0,0 +1,56 @@
+#import <IOKit/ps/IOPowerSources.h>
+#import <IOKit/ps/IOPSKeys.h>
+
+#import "BDPowerInfo.h"
+
+@implementation BDPowerInfo
+
+- (id)IOKitPowerSourceKey:(id)key {
+ id result = nil;
+
+ CFTypeRef info = IOPSCopyPowerSourcesInfo();
+ CFArrayRef list = IOPSCopyPowerSourcesList(info);
+
+ for(int i = 0; i < CFArrayGetCount(list); i++) {
+ CFDictionaryRef description = IOPSGetPowerSourceDescription(info, CFArrayGetValueAtIndex(list, i));
+ if(description != NULL) {
+ result = (__bridge id)CFDictionaryGetValue(description, (__bridge void *)key);
+ if(result)
+ break;
+ }
+ }
+
+ CFRelease(list);
+ CFRelease(info);
+
+ return result;
+}
+
+#pragma mark -
+
+- (NSInteger)remainingCharge {
+ NSInteger currentCharge = [[self IOKitPowerSourceKey:@kIOPSCurrentCapacityKey] integerValue];
+ NSInteger maxCharge = [[self IOKitPowerSourceKey:@kIOPSMaxCapacityKey] integerValue];
+ return (currentCharge * 100) / maxCharge;
+}
+
+- (BOOL)isRunningOnBattery {
+ return [[self IOKitPowerSourceKey:@kIOPSPowerSourceStateKey] isEqualToString:@kIOPSBatteryPowerValue];
+}
+
+- (BOOL)isChargingBattery {
+ return [[self IOKitPowerSourceKey:@kIOPSIsChargingKey] boolValue];
+}
+
+#pragma mark -
+
++ (id)sharedPowerInfo {
+ static dispatch_once_t onceToken;
+ static BDPowerInfo *powerInfo = nil;
+ dispatch_once(&onceToken, ^{
+ powerInfo = [[self alloc] init];
+ });
+ return powerInfo;
+}
+
+@end
Please sign in to comment.
Something went wrong with that request. Please try again.