Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added functionality to activate keeping the mac awake when connected to AC power #187

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions KeepingYouAwake/KYAAppController/KYAAppController.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
#import "KYABatteryCapacityThreshold.h"
#import "KYAActivationDurationsMenuController.h"
#import "KYAActivationUserNotification.h"
#import <IOKit/ps/IOPowerSources.h>

#define KYA_POWER_SOURCE_STATE "Power Source State"
#define KYA_AC_POWER "AC Power"
#define KYA_CHECK_POWER_SOURCE_CHANGE 10.0

// Deprecated!
#define KYA_MINUTES(m) (m * 60.0f)
Expand Down Expand Up @@ -57,6 +62,13 @@ - (instancetype)init
selector:@selector(batteryCapacityThresholdDidChange:)
name:kKYABatteryCapacityThresholdDidChangeNotification
object:nil];
// TODO search for a better way to check for power source change
[NSTimer scheduledTimerWithTimeInterval:KYA_CHECK_POWER_SOURCE_CHANGE
target:self
selector:@selector(checkPowerSource)
userInfo:nil
repeats:YES];

}
return self;
}
Expand Down Expand Up @@ -176,6 +188,20 @@ - (void)configureUserNotificationCenter

#pragma mark - Device Power Monitoring

- (void)checkPowerSource
{
CFTypeRef powerSourceInfo = IOPSCopyPowerSourcesInfo();
CFArrayRef powerSources = IOPSCopyPowerSourcesList(powerSourceInfo);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implementation-wise this is certainly possible to solve it like that.

I would prefer to not add IOPowerSources code directly here in KYAAppController. This would probably be a better fit for KYABatteryMonitor. There is some pre-existing IOPowerSources code already, that could be extended, e.g. by adding concrete notifications for this connect/disconnect scenario.


NSString *powerSourceState = [(NSDictionary *)CFBridgingRelease(IOPSGetPowerSourceDescription(powerSourceInfo, CFArrayGetValueAtIndex(powerSources, 0))) objectForKey:@KYA_POWER_SOURCE_STATE];
if([powerSourceState isEqualToString:@KYA_AC_POWER] && [NSUserDefaults.standardUserDefaults kya_isActivateOnPowerEnabled])
{
[self activateTimerWithTimeInterval:KYASleepWakeTimeIntervalIndefinite];
} else {
[self terminateTimer];
}
}

- (void)checkAndEnableBatteryOverride
{
Auto batteryMonitor = KYADevice.currentDevice.batteryMonitor;
Expand Down
1 change: 1 addition & 0 deletions KeepingYouAwake/KYALocalizedStrings.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
#define KYA_L10N_DISABLE_MENU_BAR_ICON_HIGHLIGHT_COLOR NSLocalizedString(@"Disable menu bar icon highlight color", @"Disable menu bar icon highlight color")
#define KYA_L10N_QUIT_ON_TIMER_EXPIRATION NSLocalizedString(@"Quit when activation duration is over", @"Quit when activation duration is over")
#define KYA_L10N_ALLOW_DISPLAY_SLEEP NSLocalizedString(@"Allow the display to sleep", @"Allow the display to sleep")
#define KYA_L10N_ACTIVATE_ON_POWER NSLocalizedString(@"Activate when power source is connected", @"Activate when power source is connected")

#define KYA_L10N_DURATIONS_ALERT_REALLY_RESET_TITLE NSLocalizedString(@"Reset Activation Durations", @"Reset Activation Durations")
#define KYA_L10N_DURATIONS_ALERT_REALLY_RESET_MESSAGE NSLocalizedString(@"Do you really want to reset the activation durations to the default values?", @"Do you really want to reset the activation durations to the default values?")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ - (void)configureAdvancedPreferences
[preferences addObject:[[KYAPreference alloc] initWithTitle:KYA_L10N_ALLOW_DISPLAY_SLEEP
defaultsKey:KYAUserDefaultsKeyAllowDisplaySleep
]];
[preferences addObject:[[KYAPreference alloc] initWithTitle:KYA_L10N_ACTIVATE_ON_POWER
defaultsKey:KYAUserDefaultsKeyisActivateOnPowerEnabled
]];

self.preferences = [preferences copy];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// User Default Keys
NSString * const KYAUserDefaultsKeyActivateOnLaunch = @"info.marcel-dierkes.KeepingYouAwake.ActivateOnLaunch";
NSString * const KYAUserDefaultsKeyAllowDisplaySleep = @"info.marcel-dierkes.KeepingYouAwake.AllowDisplaySleep";
NSString * const KYAUserDefaultsKeyisActivateOnPowerEnabled = @"info.marcel-dierkes.KeepingYouAwake.ActivateOnPower";
NSString * const KYAUserDefaultsKeyMenuBarIconHighlightDisabled = @"info.marcel-dierkes.KeepingYouAwake.MenuBarIconHighlightDisabled";
NSString * const KYAUserDefaultsKeyIsQuitOnTimerExpirationEnabled = @"info.marcel-dierkes.KeepingYouAwake.QuitOnTimerExpirationEnabled";

Expand All @@ -23,8 +24,10 @@
@implementation NSUserDefaults (Keys)
@dynamic kya_activateOnLaunch;
@dynamic kya_allowDisplaySleep;
@dynamic kya_activateOnPowerEnabled;
@dynamic kya_menuBarIconHighlightDisabled;
@dynamic kya_batteryCapacityThresholdEnabled, kya_batteryCapacityThreshold;
@dynamic kya_batteryCapacityThresholdEnabled;
@dynamic kya_batteryCapacityThreshold;
@dynamic kya_lowPowerModeMonitoringEnabled;
@dynamic kya_preReleaseUpdatesEnabled;
@dynamic kya_quitOnTimerExpirationEnabled;
Expand Down Expand Up @@ -125,4 +128,16 @@ - (void)setKya_quitOnTimerExpirationEnabled:(BOOL)quitOnTimerExpirationEnabled
[self setBool:quitOnTimerExpirationEnabled forKey:KYAUserDefaultsKeyIsQuitOnTimerExpirationEnabled];
}

#pragma mark - Activate on AC Power Enabled

- (BOOL)kya_isActivateOnPowerEnabled
{
return [self boolForKey:KYAUserDefaultsKeyisActivateOnPowerEnabled];
}

- (void)setKya_activateOnPowerEnabled:(BOOL)activateOnPowerEnabled
{
[self setBool:activateOnPowerEnabled forKey:KYAUserDefaultsKeyisActivateOnPowerEnabled];
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ NS_ASSUME_NONNULL_BEGIN
// User Default Keys
FOUNDATION_EXPORT NSString * const KYAUserDefaultsKeyActivateOnLaunch;
FOUNDATION_EXPORT NSString * const KYAUserDefaultsKeyAllowDisplaySleep;
FOUNDATION_EXPORT NSString * const KYAUserDefaultsKeyisActivateOnPowerEnabled;
FOUNDATION_EXPORT NSString * const KYAUserDefaultsKeyMenuBarIconHighlightDisabled;
FOUNDATION_EXPORT NSString * const KYAUserDefaultsKeyIsQuitOnTimerExpirationEnabled;
FOUNDATION_EXPORT NSString * const KYAUserDefaultsKeyBatteryCapacityThresholdEnabled;
Expand Down Expand Up @@ -57,6 +58,10 @@ FOUNDATION_EXPORT NSString * const KYAUserDefaultsKeyPreReleaseUpdatesEnabled;
*/
@property (nonatomic, getter=kya_isQuitOnTimerExpirationEnabled) BOOL kya_quitOnTimerExpirationEnabled;

/// Returns YES if the app should activate when AC power is connected

@property (nonatomic, getter=kya_isActivateOnPowerEnabled) BOOL kya_activateOnPowerEnabled;

@end

NS_ASSUME_NONNULL_END