From a07613130301331e18fff76e6f9bc62066aee9c7 Mon Sep 17 00:00:00 2001 From: Luciano Iam Date: Wed, 12 Jan 2022 00:55:45 +0100 Subject: [PATCH] Idle timer for external UI (macOS) --- distrho/DistrhoUIMain.cpp | 7 +++++ distrho/src/DistrhoUIVST3.cpp | 52 +++++++++++++++++++++++++++++++++-- 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/distrho/DistrhoUIMain.cpp b/distrho/DistrhoUIMain.cpp index aeeade58..13480408 100644 --- a/distrho/DistrhoUIMain.cpp +++ b/distrho/DistrhoUIMain.cpp @@ -14,6 +14,13 @@ * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#include "src/DistrhoDefines.h" +#if defined(DISTRHO_OS_MAC) +// Allow referencing CoreFoundation from DistrhoUIVST3.cpp. Include must appear +// before DGL headers otherwise compiler fails on ambiguous 'Point' definition. +# include +#endif + #include "src/DistrhoUI.cpp" #if defined(DISTRHO_PLUGIN_TARGET_CARLA) diff --git a/distrho/src/DistrhoUIVST3.cpp b/distrho/src/DistrhoUIVST3.cpp index d0e8422c..ec0ad2e8 100644 --- a/distrho/src/DistrhoUIVST3.cpp +++ b/distrho/src/DistrhoUIVST3.cpp @@ -142,8 +142,12 @@ public: ~UIVst3() { -#if !DISTRHO_PLUGIN_HAS_EXTERNAL_UI && (defined(DISTRHO_OS_MAC) || defined(DISTRHO_OS_WINDOWS)) +#if defined(DISTRHO_OS_MAC) || defined(DISTRHO_OS_WINDOWS) +# if DISTRHO_PLUGIN_HAS_EXTERNAL_UI + nativeIdleTimerDestroy(); +# else fUI.removeIdleCallbackForVST3(this); +# endif #endif if (fConnection != nullptr) disconnect(); @@ -168,8 +172,12 @@ public: if (fConnection != nullptr) connect(fConnection); -#if !DISTRHO_PLUGIN_HAS_EXTERNAL_UI && (defined(DISTRHO_OS_MAC) || defined(DISTRHO_OS_WINDOWS)) +#if defined(DISTRHO_OS_MAC) || defined(DISTRHO_OS_WINDOWS) +# if DISTRHO_PLUGIN_HAS_EXTERNAL_UI + nativeIdleTimerCreate(DPF_VST3_TIMER_INTERVAL); +# else fUI.addIdleCallbackForVST3(this, DPF_VST3_TIMER_INTERVAL); +# endif #endif } @@ -541,6 +549,11 @@ private: // Plugin UI (after VST3 stuff so the UI can call into us during its constructor) UIExporter fUI; + // Native timer support +#if defined(DISTRHO_OS_MAC) + CFRunLoopTimerRef fTimer; +#endif + // ---------------------------------------------------------------------------------------------------------------- // helper functions called during message passing @@ -712,6 +725,41 @@ private: ((UIVst3*)ptr)->setState(key, value); } #endif + +#if DISTRHO_PLUGIN_HAS_EXTERNAL_UI +# if defined(DISTRHO_OS_MAC) + void nativeIdleTimerCreate(const uint timerFrequencyInMs) + { + const CFTimeInterval t = static_cast(timerFrequencyInMs) / 1000.0; + CFRunLoopTimerContext ctx = {}; + ctx.info = this; + fTimer = CFRunLoopTimerCreate(kCFAllocatorDefault, CFAbsoluteTimeGetCurrent() + t, t, 0, 0, + UIVst3::nativeIdleTimerCallback, &ctx); + CFRunLoopAddTimer(CFRunLoopGetMain(), fTimer, kCFRunLoopCommonModes); + } + + void nativeIdleTimerDestroy() + { + CFRunLoopRemoveTimer(CFRunLoopGetMain(), fTimer, kCFRunLoopCommonModes); + CFRelease(fTimer); + } + + static void nativeIdleTimerCallback(CFRunLoopTimerRef timer, void *info) + { + reinterpret_cast(info)->onTimer(); + } +# elif defined(DISTRHO_OS_WINDOWS) + void nativeIdleTimerCreate(const uint timerFrequencyInMs) + { + // TODO + } + + void nativeIdleTimerDestroy() + { + // TODO + } +# endif +#endif }; // --------------------------------------------------------------------------------------------------------------------