diff options
author | Damiano Galassi <[email protected]> | 2019-12-29 11:14:35 +0100 |
---|---|---|
committer | Damiano Galassi <[email protected]> | 2020-01-02 10:35:22 +0100 |
commit | e36c675d5f8701dadb7556b399d74b43240a1f23 (patch) | |
tree | 1b14c761430c7865ad1858f27c91f7ef0a3f41be /macosx | |
parent | 4e989255c177c2dfd876e5782f35bafe5937086b (diff) |
MacGui: move sleep prevention to the main process, so we keep the sleep assertion if the XPC service crashes.
(cherry picked from commit f7228c0e67aafe557fc91d49ef547ebcf7734674)
Diffstat (limited to 'macosx')
-rw-r--r-- | macosx/HBCore.m | 2 | ||||
-rw-r--r-- | macosx/HBRemoteCore.m | 57 | ||||
-rw-r--r-- | macosx/HandBrakeXPCService/HBRemoteCoreProtocol.h | 5 | ||||
-rw-r--r-- | macosx/HandBrakeXPCService/HandBrakeXPCService.m | 22 |
4 files changed, 53 insertions, 33 deletions
diff --git a/macosx/HBCore.m b/macosx/HBCore.m index 4fd832b8c..49b73ebe7 100644 --- a/macosx/HBCore.m +++ b/macosx/HBCore.m @@ -154,14 +154,12 @@ typedef void (^HBCoreCleanupHandler)(void); { NSAssert(!self.automaticallyPreventSleep, @"[HBCore preventSleep:] called with automaticallyPreventSleep enabled."); hb_system_sleep_prevent(_hb_handle); - [HBUtilities writeToActivityLog:"%s prevented sleep", self.name.UTF8String]; } - (void)allowSleep { NSAssert(!self.automaticallyPreventSleep, @"[HBCore allowSleep:] called with automaticallyPreventSleep enabled."); hb_system_sleep_allow(_hb_handle); - [HBUtilities writeToActivityLog:"%s stopped preventing sleep", self.name.UTF8String]; } - (void)preventAutoSleep diff --git a/macosx/HBRemoteCore.m b/macosx/HBRemoteCore.m index 0cc410e79..7206310d6 100644 --- a/macosx/HBRemoteCore.m +++ b/macosx/HBRemoteCore.m @@ -5,6 +5,7 @@ #import "HBRemoteCore.h" #import "HBRemoteCoreProtocol.h" #import "HBPreferencesKeys.h" +#import <IOKit/pwr_mgt/IOPMLib.h> @import HandBrakeKit; @@ -21,6 +22,8 @@ @property (nonatomic, readwrite, copy) HBCoreProgressHandler progressHandler; @property (nonatomic, readwrite, copy) HBCoreCompletionHandler completionHandler; +@property (nonatomic, readwrite) IOPMAssertionID assertionID; + @end @implementation HBRemoteCore @@ -33,6 +36,7 @@ _state = HBStateIdle; _stdoutRedirect = HBRedirect.stdoutRedirect; _stderrRedirect = HBRedirect.stderrRedirect; + _assertionID = -1; [self connect]; } @@ -124,23 +128,60 @@ [_proxy setLogLevel:logLevel]; } -- (void)setAutomaticallyPreventSleep:(BOOL)automaticallyPreventSleep +- (void)preventSleep { - [_proxy setAutomaticallyPreventSleep:automaticallyPreventSleep]; + if (_assertionID != -1) + { + // nothing to do + return; + } + + CFStringRef reasonForActivity= CFSTR("HandBrake is currently scanning and/or encoding"); + + IOReturn success = IOPMAssertionCreateWithName(kIOPMAssertPreventUserIdleSystemSleep, + kIOPMAssertionLevelOn, reasonForActivity, &_assertionID); + + if (success != kIOReturnSuccess) + { + [HBUtilities writeToActivityLog:"HBRemoteCore: failed to prevent system sleep"]; + } } - (void)allowSleep { - [_proxy allowSleep]; + if (_assertionID == -1) + { + // nothing to do + return; + } + + IOReturn success = IOPMAssertionRelease(_assertionID); + + if (success == kIOReturnSuccess) + { + _assertionID = -1; + } +} + +- (void)preventAutoSleep +{ + if (self.automaticallyPreventSleep) + { + [self preventSleep]; + } } -- (void)preventSleep +- (void)allowAutoSleep { - [_proxy preventSleep]; + if (self.automaticallyPreventSleep) + { + [self allowSleep]; + } } - (void)scanURL:(NSURL *)url titleIndex:(NSUInteger)index previews:(NSUInteger)previewsNum minDuration:(NSUInteger)seconds keepPreviews:(BOOL)keepPreviews progressHandler:(nonnull HBCoreProgressHandler)progressHandler completionHandler:(nonnull HBCoreCompletionHandler)completionHandler { + [self preventAutoSleep]; #ifdef __SANDBOX_ENABLED__ __block HBSecurityAccessToken *token = [HBSecurityAccessToken tokenWithObject:url]; @@ -167,6 +208,7 @@ #ifdef __SANDBOX_ENABLED__ token = nil; #endif + [weakSelf allowAutoSleep]; handler(result); }); }]; @@ -179,6 +221,8 @@ - (void)encodeJob:(HBJob *)job progressHandler:(HBCoreProgressHandler)progressHandler completionHandler:(HBCoreCompletionHandler)completionHandler { + [self preventAutoSleep]; + #ifdef __SANDBOX_ENABLED__ __block HBSecurityAccessToken *token = [HBSecurityAccessToken tokenWithObject:job]; @@ -221,6 +265,7 @@ #ifdef __SANDBOX_ENABLED__ token = nil; #endif + [weakSelf allowAutoSleep]; handler(result); }); }]; @@ -257,11 +302,13 @@ - (void)pause { [_proxy pauseEncode]; + [self allowAutoSleep]; } - (void)resume { [_proxy resumeEncode]; + [self preventAutoSleep]; } @end diff --git a/macosx/HandBrakeXPCService/HBRemoteCoreProtocol.h b/macosx/HandBrakeXPCService/HBRemoteCoreProtocol.h index 97c8870ef..37fc73e2d 100644 --- a/macosx/HandBrakeXPCService/HBRemoteCoreProtocol.h +++ b/macosx/HandBrakeXPCService/HBRemoteCoreProtocol.h @@ -19,11 +19,6 @@ NS_ASSUME_NONNULL_BEGIN - (void)provideResourceAccessWithBookmarks:(NSArray<NSData *> *)bookmarks; -- (void)setAutomaticallyPreventSleep:(BOOL)automaticallyPreventSleep; - -- (void)preventSleep; -- (void)allowSleep; - - (void)scanURL:(NSURL *)url titleIndex:(NSUInteger)index previews:(NSUInteger)previewsNum minDuration:(NSUInteger)seconds keepPreviews:(BOOL)keepPreviews withReply:(void (^)(HBCoreResult))reply; - (void)cancelScan; diff --git a/macosx/HandBrakeXPCService/HandBrakeXPCService.m b/macosx/HandBrakeXPCService/HandBrakeXPCService.m index facf01268..34d3c04cb 100644 --- a/macosx/HandBrakeXPCService/HandBrakeXPCService.m +++ b/macosx/HandBrakeXPCService/HandBrakeXPCService.m @@ -57,6 +57,7 @@ static void *HandBrakeXPCServiceContext = &HandBrakeXPCServiceContext; _core = [[HBCore alloc] initWithLogLevel:level queue:_queue]; _core.name = name; + _core.automaticallyPreventSleep = NO; // Completion handler void (^completionHandler)(HBCoreResult result) = ^(HBCoreResult result) @@ -105,13 +106,6 @@ static void *HandBrakeXPCServiceContext = &HandBrakeXPCServiceContext; }); } -- (void)setAutomaticallyPreventSleep:(BOOL)automaticallyPreventSleep -{ - dispatch_sync(_queue, ^{ - self.core.automaticallyPreventSleep = automaticallyPreventSleep; - }); -} - - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if (context == HandBrakeXPCServiceContext) @@ -124,20 +118,6 @@ static void *HandBrakeXPCServiceContext = &HandBrakeXPCServiceContext; } } -- (void)preventSleep -{ - dispatch_sync(_queue, ^{ - [self.core preventSleep]; - }); -} - -- (void)allowSleep -{ - dispatch_sync(_queue, ^{ - [self.core allowSleep]; - }); -} - - (void)scanURL:(NSURL *)url titleIndex:(NSUInteger)index previews:(NSUInteger)previewsNum minDuration:(NSUInteger)seconds keepPreviews:(BOOL)keepPreviews withReply:(void (^)(HBCoreResult))reply { dispatch_sync(_queue, ^{ |