From 08ec8906f27880d13ee48d16245904489e141a95 Mon Sep 17 00:00:00 2001 From: Matteo Ceruti Date: Sat, 26 Aug 2023 01:10:00 +0200 Subject: Disable macOS's AppNap (throttling) while tracking --- opentrack/CMakeLists.txt | 9 +++++++++ opentrack/appnap_mac.mm | 47 +++++++++++++++++++++++++++++++++++++++++++++++ opentrack/main-window.cpp | 15 +++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 opentrack/appnap_mac.mm (limited to 'opentrack') 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 + +/** + * 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 #include + +#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; -- cgit v1.2.3