Skip to content

Commit

Permalink
MacGui: add an option to disable HBCore automatic sleep prevention be…
Browse files Browse the repository at this point in the history
…haviour.
  • Loading branch information
galad87 committed Jan 5, 2017
1 parent 17ab380 commit 2bf8ccc
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
16 changes: 16 additions & 0 deletions macosx/HBCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,22 @@ typedef void (^HBCoreCompletionHandler)(HBCoreResult result);
*/
@property (nonatomic, readwrite) int logLevel;

/**
* Set whether system sleep will be disable or not during a scan/encode
* Enabled by default.
*/
@property (nonatomic, readwrite) BOOL automaticallyPreventSleep;

/**
* Manually prevent system sleep if automaticallyPreventSleep is set to NO.
*/
- (void)preventSleep;

/**
* Manually allow system sleep if automaticallyPreventSleep is set to NO.
*/
- (void)allowSleep;

/**
* State formatter.
*/
Expand Down
40 changes: 35 additions & 5 deletions macosx/HBCore.m
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,34 @@ - (void)setLogLevel:(int)logLevel
hb_log_level_set(_hb_handle, logLevel);
}

- (void)preventSleep
{
NSAssert(!self.automaticallyPreventSleep, @"[HBCore preventSleep:] called with automaticallyPreventSleep enabled.");
hb_system_sleep_prevent(_hb_handle);
}

- (void)allowSleep
{
NSAssert(!self.automaticallyPreventSleep, @"[HBCore allowSleep:] called with automaticallyPreventSleep enabled.");
hb_system_sleep_allow(_hb_handle);
}

- (void)preventAutoSleep
{
if (self.automaticallyPreventSleep)
{
hb_system_sleep_prevent(_hb_handle);
}
}

- (void)allowAutoSleep
{
if (self.automaticallyPreventSleep)
{
hb_system_sleep_allow(_hb_handle);
}
}

#pragma mark - Scan

- (BOOL)canScan:(NSURL *)url error:(NSError * __autoreleasing *)error
Expand Down Expand Up @@ -229,7 +257,7 @@ - (void)scanURL:(NSURL *)url titleIndex:(NSUInteger)index previews:(NSUInteger)p
[HBUtilities writeToActivityLog:"%s scanning titles with a duration of %d seconds or more", self.name.UTF8String, seconds];
}

hb_system_sleep_prevent(_hb_handle);
[self preventAutoSleep];

hb_scan(_hb_handle, path.fileSystemRepresentation,
(int)index, (int)previewsNum,
Expand Down Expand Up @@ -469,7 +497,8 @@ - (void)encodeJob:(HBJob *)job progressHandler:(HBCoreProgressHandler)progressHa
// Free the job
hb_job_close(&hb_job);

hb_system_sleep_prevent(_hb_handle);
[self preventAutoSleep];

hb_start(_hb_handle);

// Start the timer to handle libhb state changes
Expand Down Expand Up @@ -523,15 +552,15 @@ - (void)cancelEncode
- (void)pause
{
hb_pause(_hb_handle);
hb_system_sleep_allow(_hb_handle);
self.state = HBStatePaused;
[self allowAutoSleep];
}

- (void)resume
{
hb_resume(_hb_handle);
hb_system_sleep_prevent(_hb_handle);
self.state = HBStateWorking;
[self preventAutoSleep];
}

#pragma mark - State updates
Expand Down Expand Up @@ -637,8 +666,9 @@ - (void)handleCompletion

// Set the state to idle, because the update timer won't fire again.
self.state = HBStateIdle;

// Reallow system sleep.
hb_system_sleep_allow(_hb_handle);
[self allowAutoSleep];

// Call the completion block and clean ups the handlers
self.progressHandler = nil;
Expand Down

0 comments on commit 2bf8ccc

Please sign in to comment.