diff options
author | Matteo Ceruti <matteo.ceruti@gmail.com> | 2023-08-26 01:10:00 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2023-08-26 01:39:47 +0200 |
commit | 08ec8906f27880d13ee48d16245904489e141a95 (patch) | |
tree | edccc08f4349eb13ef8150b8d41ca51db72e69b6 /opentrack | |
parent | 39bcce3786ac9f08f0ff80b7c51bd4b2e7a96106 (diff) |
Disable macOS's AppNap (throttling) while tracking
Diffstat (limited to 'opentrack')
-rw-r--r-- | opentrack/CMakeLists.txt | 9 | ||||
-rw-r--r-- | opentrack/appnap_mac.mm | 47 | ||||
-rw-r--r-- | opentrack/main-window.cpp | 15 |
3 files changed, 71 insertions, 0 deletions
diff --git a/opentrack/CMakeLists.txt b/opentrack/CMakeLists.txt index 9bf5cb9b..0fd72475 100644 --- a/opentrack/CMakeLists.txt +++ b/opentrack/CMakeLists.txt @@ -13,4 +13,13 @@ set_target_properties(opentrack-executable PROPERTIES set_source_files_properties(resources.rc OBJECT_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/opentrack.ico") +if(APPLE) + set_source_files_properties(appnap_mac.mm PROPERTIES COMPILE_FLAGS "-fno-objc-arc") + target_sources(${self} PRIVATE appnap_mac.mm) +endif() + target_link_libraries(${self} opentrack-user-interface opentrack-version) + +if(APPLE) + target_link_libraries(${self} "-framework Foundation") +endif() diff --git a/opentrack/appnap_mac.mm b/opentrack/appnap_mac.mm new file mode 100644 index 00000000..3e0bea73 --- /dev/null +++ b/opentrack/appnap_mac.mm @@ -0,0 +1,47 @@ +#ifdef __APPLE__ + +#import <Foundation/Foundation.h> + +/** + * Used to prevent macOS from throttling the opentrack process. + */ + +id token = nil; + +void disable_appnap_start(); +void disable_appnap_stop(); + +void disable_appnap_start() { + + if(token){ + NSLog(@"disable_appnap_start: already started"); + return; + } + + + NSLog(@"disable_appnap_start"); + token = [[NSProcessInfo processInfo] + beginActivityWithOptions: NSActivityUserInitiatedAllowingIdleSystemSleep + reason: @"Disable AppNap"]; + [token retain]; +} + +void disable_appnap_stop() { + if(!token){ + NSLog(@"disable_appnap_start: not started"); + return; + } + + NSLog(@"disable_appnap_stop"); + [[NSProcessInfo processInfo] endActivity:token]; + [token release]; + token = nil; +} + + + +#endif + + + + diff --git a/opentrack/main-window.cpp b/opentrack/main-window.cpp index f449845b..20506d81 100644 --- a/opentrack/main-window.cpp +++ b/opentrack/main-window.cpp @@ -32,6 +32,12 @@ #include <QDir> #include <QDateTime> + +#ifdef __APPLE__ +void disable_appnap_start(); +void disable_appnap_stop(); +#endif + extern "C" const char* const opentrack_version; using namespace options::globals; @@ -436,6 +442,11 @@ void main_window::start_tracker_() if (work) return; +#ifdef __APPLE__ + disable_appnap_start(); +#endif + + #ifndef UI_NO_VIDEO_FEED auto* frame = ui.video_frame; #else @@ -486,6 +497,10 @@ void main_window::stop_tracker_() if (!work) return; +#ifdef __APPLE__ + disable_appnap_stop(); +#endif + force_is_visible(true); with_tracker_teardown sentinel; |