From e27292f71a5f30884421f4931a84412586e69219 Mon Sep 17 00:00:00 2001 From: Sander Vocke Date: Thu, 4 Aug 2022 11:39:31 +0000 Subject: [PATCH] Add SIGUSR2 handler in bridge plugin to re-load state. --- source/bridges-plugin/CarlaBridgePlugin.cpp | 59 +++++++++++++++------ 1 file changed, 42 insertions(+), 17 deletions(-) diff --git a/source/bridges-plugin/CarlaBridgePlugin.cpp b/source/bridges-plugin/CarlaBridgePlugin.cpp index 9085eb815..1ffbdd302 100644 --- a/source/bridges-plugin/CarlaBridgePlugin.cpp +++ b/source/bridges-plugin/CarlaBridgePlugin.cpp @@ -74,6 +74,7 @@ using water::String; static bool gIsInitiated = false; static volatile bool gCloseNow = false; static volatile bool gSaveNow = false; +static volatile bool gLoadNow = false; #if defined(CARLA_OS_UNIX) static void closeSignalHandler(int) noexcept @@ -84,6 +85,10 @@ static void saveSignalHandler(int) noexcept { gSaveNow = true; } +static void loadSignalHandler(int) noexcept +{ + gLoadNow = true; +} #elif defined(CARLA_OS_WIN) static BOOL WINAPI winSignalHandler(DWORD dwCtrlType) noexcept { @@ -112,6 +117,11 @@ static void initSignalHandler() sig.sa_flags = SA_RESTART; sigemptyset(&sig.sa_mask); sigaction(SIGUSR1, &sig, nullptr); + + sig.sa_handler = loadSignalHandler; + sig.sa_flags = SA_RESTART; + sigemptyset(&sig.sa_mask); + sigaction(SIGUSR2, &sig, nullptr); #elif defined(CARLA_OS_WIN) SetConsoleCtrlHandler(winSignalHandler, TRUE); #endif @@ -122,6 +132,31 @@ static void initSignalHandler() static String gProjectFilename; static CarlaHostHandle gHostHandle; +static void gLoadPluginState() +{ + if (File(gProjectFilename).existsAsFile()) + { + if (carla_load_plugin_state(gHostHandle, 0, gProjectFilename.toRawUTF8())) + carla_stdout("Plugin state loaded successfully"); + else + carla_stderr("Plugin state load failed, error was:\n%s", carla_get_last_error(gHostHandle)); + } + else + { + carla_stdout("Previous plugin state in '%s' is non-existent, will use default state", + gProjectFilename.toRawUTF8()); + } +} + +static void gSavePluginState() +{ + if (gProjectFilename.isNotEmpty()) + { + if (! carla_save_plugin_state(gHostHandle, 0, gProjectFilename.toRawUTF8())) + carla_stderr("Plugin preset save failed, error was:\n%s", carla_get_last_error(gHostHandle)); + } +} + static void gIdle() { carla_engine_idle(gHostHandle); @@ -129,12 +164,13 @@ static void gIdle() if (gSaveNow) { gSaveNow = false; + gSavePluginState(); + } - if (gProjectFilename.isNotEmpty()) - { - if (! carla_save_plugin_state(gHostHandle, 0, gProjectFilename.toRawUTF8())) - carla_stderr("Plugin preset save failed, error was:\n%s", carla_get_last_error(gHostHandle)); - } + if (gLoadNow) + { + gLoadNow = false; + gLoadPluginState(); } } @@ -212,18 +248,7 @@ public: if (! File::isAbsolutePath(gProjectFilename)) gProjectFilename = File::getCurrentWorkingDirectory().getChildFile(gProjectFilename).getFullPathName(); - if (File(gProjectFilename).existsAsFile()) - { - if (carla_load_plugin_state(gHostHandle, 0, gProjectFilename.toRawUTF8())) - carla_stdout("Plugin state loaded successfully"); - else - carla_stderr("Plugin state load failed, error was:\n%s", carla_get_last_error(gHostHandle)); - } - else - { - carla_stdout("Previous plugin state in '%s' is non-existent, will start from default state", - gProjectFilename.toRawUTF8()); - } + gLoadPluginState(); } gIsInitiated = true;