From 74e17f2b6695720ca5c1982cff19c87aed4c0510 Mon Sep 17 00:00:00 2001
From: Damiano Galassi <damiog@gmail.com>
Date: Thu, 5 Jan 2017 10:12:30 +0100
Subject: [PATCH] MacGui: add an option to disable HBCore automatic sleep
 prevention behaviour.

(cherry picked from commit 2bf8ccc610d4df61660cd3efafd6b81fa5936c43)
---
 macosx/HBCore.h | 16 ++++++++++++++++
 macosx/HBCore.m | 40 +++++++++++++++++++++++++++++++++++-----
 2 files changed, 51 insertions(+), 5 deletions(-)

diff --git a/macosx/HBCore.h b/macosx/HBCore.h
index e94b5ac12..26b74073c 100644
--- a/macosx/HBCore.h
+++ b/macosx/HBCore.h
@@ -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.
  */
diff --git a/macosx/HBCore.m b/macosx/HBCore.m
index 31f293448..baabcf75d 100644
--- a/macosx/HBCore.m
+++ b/macosx/HBCore.m
@@ -139,6 +139,34 @@ static void hb_error_handler(const char *errmsg)
     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
@@ -229,7 +257,7 @@ static void hb_error_handler(const char *errmsg)
         [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,
@@ -469,7 +497,8 @@ static void hb_error_handler(const char *errmsg)
     // 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
@@ -523,15 +552,15 @@ static void hb_error_handler(const char *errmsg)
 - (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
@@ -637,8 +666,9 @@ static void hb_error_handler(const char *errmsg)
 
     // 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;
-- 
2.40.0