Skip to content

Commit 11cf222

Browse files
committed
Move OSC code to CardinalOSC.{cpp,hpp}
1 parent 68c0599 commit 11cf222

13 files changed

Lines changed: 369 additions & 294 deletions

File tree

src/Cardinal/CardinalOSC.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../CardinalOSC.cpp

src/CardinalCommon.cpp

Lines changed: 2 additions & 274 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,6 @@
3636
# error wrong build
3737
#endif
3838

39-
#if (defined(STATIC_BUILD) && !defined(__MOD_DEVICES__)) || CARDINAL_VARIANT_MINI
40-
# undef CARDINAL_INIT_OSC_THREAD
41-
#endif
42-
4339
#ifdef NDEBUG
4440
# undef DEBUG
4541
#endif
@@ -56,10 +52,6 @@
5652
# include <fstream>
5753
#endif
5854

59-
#ifdef HAVE_LIBLO
60-
# include <lo/lo.h>
61-
#endif
62-
6355
#ifdef DISTRHO_OS_WASM
6456
# include <emscripten/emscripten.h>
6557
#endif
@@ -345,176 +337,6 @@ START_NAMESPACE_DISTRHO
345337

346338
// -----------------------------------------------------------------------------------------------------------
347339

348-
#ifdef HAVE_LIBLO
349-
static void osc_error_handler(int num, const char* msg, const char* path)
350-
{
351-
d_stderr("Cardinal OSC Error: code: %i, msg: \"%s\", path: \"%s\")", num, msg, path);
352-
}
353-
354-
static int osc_fallback_handler(const char* const path, const char* const types, lo_arg**, int, lo_message, void*)
355-
{
356-
d_stderr("Cardinal OSC unhandled message \"%s\" with types \"%s\"", path, types);
357-
return 0;
358-
}
359-
360-
static int osc_hello_handler(const char*, const char*, lo_arg**, int, const lo_message m, void* const self)
361-
{
362-
d_stdout("Hello received from OSC, saying hello back to them o/");
363-
const lo_address source = lo_message_get_source(m);
364-
const lo_server server = static_cast<Initializer*>(self)->oscServer;
365-
366-
// send list of features first
367-
#ifdef CARDINAL_INIT_OSC_THREAD
368-
lo_send_from(source, server, LO_TT_IMMEDIATE, "/resp", "ss", "features", ":screenshot:");
369-
#else
370-
lo_send_from(source, server, LO_TT_IMMEDIATE, "/resp", "ss", "features", "");
371-
#endif
372-
373-
// then finally hello reply
374-
lo_send_from(source, server, LO_TT_IMMEDIATE, "/resp", "ss", "hello", "ok");
375-
return 0;
376-
}
377-
378-
static int osc_load_handler(const char*, const char* types, lo_arg** argv, int argc, const lo_message m, void* const self)
379-
{
380-
d_debug("osc_load_handler()");
381-
DISTRHO_SAFE_ASSERT_RETURN(argc == 1, 0);
382-
DISTRHO_SAFE_ASSERT_RETURN(types != nullptr && types[0] == 'b', 0);
383-
384-
const int32_t size = argv[0]->blob.size;
385-
DISTRHO_SAFE_ASSERT_RETURN(size > 4, 0);
386-
387-
const uint8_t* const blob = (uint8_t*)(&argv[0]->blob.data);
388-
DISTRHO_SAFE_ASSERT_RETURN(blob != nullptr, 0);
389-
390-
bool ok = false;
391-
392-
if (CardinalBasePlugin* const plugin = static_cast<Initializer*>(self)->remotePluginInstance)
393-
{
394-
CardinalPluginContext* const context = plugin->context;
395-
std::vector<uint8_t> data(size);
396-
std::memcpy(data.data(), blob, size);
397-
398-
#ifdef CARDINAL_INIT_OSC_THREAD
399-
rack::contextSet(context);
400-
#endif
401-
402-
rack::system::removeRecursively(context->patch->autosavePath);
403-
rack::system::createDirectories(context->patch->autosavePath);
404-
try {
405-
rack::system::unarchiveToDirectory(data, context->patch->autosavePath);
406-
context->patch->loadAutosave();
407-
ok = true;
408-
}
409-
catch (rack::Exception& e) {
410-
WARN("%s", e.what());
411-
}
412-
413-
#ifdef CARDINAL_INIT_OSC_THREAD
414-
rack::contextSet(nullptr);
415-
#endif
416-
}
417-
418-
const lo_address source = lo_message_get_source(m);
419-
const lo_server server = static_cast<Initializer*>(self)->oscServer;
420-
lo_send_from(source, server, LO_TT_IMMEDIATE, "/resp", "ss", "load", ok ? "ok" : "fail");
421-
return 0;
422-
}
423-
424-
static int osc_param_handler(const char*, const char* types, lo_arg** argv, int argc, const lo_message m, void* const self)
425-
{
426-
d_debug("osc_param_handler()");
427-
DISTRHO_SAFE_ASSERT_RETURN(argc == 3, 0);
428-
DISTRHO_SAFE_ASSERT_RETURN(types != nullptr, 0);
429-
DISTRHO_SAFE_ASSERT_RETURN(types[0] == 'h', 0);
430-
DISTRHO_SAFE_ASSERT_RETURN(types[1] == 'i', 0);
431-
DISTRHO_SAFE_ASSERT_RETURN(types[2] == 'f', 0);
432-
433-
if (CardinalBasePlugin* const plugin = static_cast<Initializer*>(self)->remotePluginInstance)
434-
{
435-
CardinalPluginContext* const context = plugin->context;
436-
437-
const int64_t moduleId = argv[0]->h;
438-
const int paramId = argv[1]->i;
439-
const float paramValue = argv[2]->f;
440-
441-
#ifdef CARDINAL_INIT_OSC_THREAD
442-
rack::contextSet(context);
443-
#endif
444-
445-
rack::engine::Module* const module = context->engine->getModule(moduleId);
446-
DISTRHO_SAFE_ASSERT_RETURN(module != nullptr, 0);
447-
448-
context->engine->setParamValue(module, paramId, paramValue);
449-
450-
#ifdef CARDINAL_INIT_OSC_THREAD
451-
rack::contextSet(nullptr);
452-
#endif
453-
}
454-
455-
return 0;
456-
}
457-
458-
static int osc_host_param_handler(const char*, const char* types, lo_arg** argv, int argc, const lo_message m, void* const self)
459-
{
460-
d_debug("osc_host_param_handler()");
461-
DISTRHO_SAFE_ASSERT_RETURN(argc == 2, 0);
462-
DISTRHO_SAFE_ASSERT_RETURN(types != nullptr, 0);
463-
DISTRHO_SAFE_ASSERT_RETURN(types[0] == 'i', 0);
464-
DISTRHO_SAFE_ASSERT_RETURN(types[1] == 'f', 0);
465-
466-
if (CardinalBasePlugin* const plugin = static_cast<Initializer*>(self)->remotePluginInstance)
467-
{
468-
CardinalPluginContext* const context = plugin->context;
469-
470-
const int paramId = argv[0]->i;
471-
DISTRHO_SAFE_ASSERT_RETURN(paramId >= 0, 0);
472-
473-
const uint uparamId = static_cast<uint>(paramId);
474-
DISTRHO_SAFE_ASSERT_UINT2_RETURN(uparamId < kModuleParameterCount, uparamId, kModuleParameterCount, 0);
475-
476-
const float paramValue = argv[1]->f;
477-
478-
context->parameters[uparamId] = paramValue;
479-
}
480-
481-
return 0;
482-
}
483-
484-
# ifdef CARDINAL_INIT_OSC_THREAD
485-
static int osc_screenshot_handler(const char*, const char* types, lo_arg** argv, int argc, const lo_message m, void* const self)
486-
{
487-
d_debug("osc_screenshot_handler()");
488-
DISTRHO_SAFE_ASSERT_RETURN(argc == 1, 0);
489-
DISTRHO_SAFE_ASSERT_RETURN(types != nullptr && types[0] == 'b', 0);
490-
491-
const int32_t size = argv[0]->blob.size;
492-
DISTRHO_SAFE_ASSERT_RETURN(size > 4, 0);
493-
494-
const uint8_t* const blob = (uint8_t*)(&argv[0]->blob.data);
495-
DISTRHO_SAFE_ASSERT_RETURN(blob != nullptr, 0);
496-
497-
bool ok = false;
498-
499-
if (CardinalBasePlugin* const plugin = static_cast<Initializer*>(self)->remotePluginInstance)
500-
{
501-
if (char* const screenshot = String::asBase64(blob, size).getAndReleaseBuffer())
502-
{
503-
ok = plugin->updateStateValue("screenshot", screenshot);
504-
std::free(screenshot);
505-
}
506-
}
507-
508-
const lo_address source = lo_message_get_source(m);
509-
const lo_server server = static_cast<Initializer*>(self)->oscServer;
510-
lo_send_from(source, server, LO_TT_IMMEDIATE, "/resp", "ss", "screenshot", ok ? "ok" : "fail");
511-
return 0;
512-
}
513-
# endif
514-
#endif
515-
516-
// -----------------------------------------------------------------------------------------------------------
517-
518340
#if defined(DISTRHO_OS_WASM) && !defined(CARDINAL_COMMON_UI_ONLY)
519341
static void WebBrowserDataLoaded(void* const data)
520342
{
@@ -698,32 +520,14 @@ Initializer::Initializer(const CardinalBasePlugin* const plugin, const CardinalB
698520

699521
loadSettings(isRealInstance);
700522

701-
#if defined(CARDINAL_INIT_OSC_THREAD)
702-
INFO("Initializing OSC Remote control");
703-
const char* port;
704-
if (const char* const portEnv = std::getenv("CARDINAL_REMOTE_HOST_PORT"))
705-
port = portEnv;
706-
else
707-
port = CARDINAL_DEFAULT_REMOTE_PORT;
708-
startRemoteServer(port);
709-
#elif defined(HAVE_LIBLO)
710-
if (isStandalone()) {
711-
INFO("OSC Remote control is available on request");
712-
} else {
713-
INFO("OSC Remote control is not available on plugin variants");
714-
}
715-
#else
716-
INFO("OSC Remote control is not enabled in this build");
717-
#endif
523+
oscInitState(this);
718524
}
719525

720526
Initializer::~Initializer()
721527
{
722528
using namespace rack;
723529

724-
#ifdef HAVE_LIBLO
725-
stopRemoteServer();
726-
#endif
530+
oscShutdownState(this);
727531

728532
if (shouldSaveSettings)
729533
{
@@ -784,82 +588,6 @@ void Initializer::loadSettings(const bool isRealInstance)
784588
switchDarkMode(settings::uiTheme == "dark");
785589
}
786590

787-
#ifdef HAVE_LIBLO
788-
bool Initializer::startRemoteServer(const char* const port)
789-
{
790-
#ifdef CARDINAL_INIT_OSC_THREAD
791-
if (oscServerThread != nullptr)
792-
return true;
793-
794-
if ((oscServerThread = lo_server_thread_new_with_proto(port, LO_UDP, osc_error_handler)) == nullptr)
795-
return false;
796-
797-
oscServer = lo_server_thread_get_server(oscServerThread);
798-
799-
lo_server_thread_add_method(oscServerThread, "/hello", "", osc_hello_handler, this);
800-
lo_server_thread_add_method(oscServerThread, "/host-param", "if", osc_host_param_handler, this);
801-
lo_server_thread_add_method(oscServerThread, "/load", "b", osc_load_handler, this);
802-
lo_server_thread_add_method(oscServerThread, "/param", "hif", osc_param_handler, this);
803-
lo_server_thread_add_method(oscServerThread, "/screenshot", "b", osc_screenshot_handler, this);
804-
lo_server_thread_add_method(oscServerThread, nullptr, nullptr, osc_fallback_handler, nullptr);
805-
lo_server_thread_start(oscServerThread);
806-
#else
807-
if (oscServer != nullptr)
808-
return true;
809-
810-
if ((oscServer = lo_server_new_with_proto(port, LO_UDP, osc_error_handler)) == nullptr)
811-
return false;
812-
813-
lo_server_add_method(oscServer, "/hello", "", osc_hello_handler, this);
814-
lo_server_add_method(oscServer, "/host-param", "if", osc_host_param_handler, this);
815-
lo_server_add_method(oscServer, "/load", "b", osc_load_handler, this);
816-
lo_server_add_method(oscServer, "/param", "hif", osc_param_handler, this);
817-
lo_server_add_method(oscServer, nullptr, nullptr, osc_fallback_handler, nullptr);
818-
#endif
819-
820-
return true;
821-
}
822-
823-
void Initializer::stopRemoteServer()
824-
{
825-
DISTRHO_SAFE_ASSERT(remotePluginInstance == nullptr);
826-
827-
#ifdef CARDINAL_INIT_OSC_THREAD
828-
if (oscServerThread != nullptr)
829-
{
830-
lo_server_thread_stop(oscServerThread);
831-
lo_server_thread_del_method(oscServerThread, nullptr, nullptr);
832-
lo_server_thread_free(oscServerThread);
833-
oscServerThread = nullptr;
834-
oscServer = nullptr;
835-
}
836-
#else
837-
if (oscServer != nullptr)
838-
{
839-
lo_server_del_method(oscServer, nullptr, nullptr);
840-
lo_server_free(oscServer);
841-
oscServer = nullptr;
842-
}
843-
#endif
844-
}
845-
846-
void Initializer::stepRemoteServer()
847-
{
848-
DISTRHO_SAFE_ASSERT_RETURN(oscServer != nullptr,);
849-
DISTRHO_SAFE_ASSERT_RETURN(remotePluginInstance != nullptr,);
850-
851-
#ifndef CARDINAL_INIT_OSC_THREAD
852-
for (;;)
853-
{
854-
try {
855-
if (lo_server_recv_noblock(oscServer, 0) == 0)
856-
break;
857-
} DISTRHO_SAFE_EXCEPTION_CONTINUE("stepRemoteServer")
858-
}
859-
#endif
860-
}
861-
#endif // HAVE_LIBLO
862-
863591
// --------------------------------------------------------------------------------------------------------------------
864592

865593
END_NAMESPACE_DISTRHO

src/CardinalCommon.hpp

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#pragma once
88

99
#include "DistrhoUtils.hpp"
10+
#include "CardinalOSC.hpp"
1011

1112
#include <string>
1213

@@ -74,14 +75,6 @@ void openBrowser(const std::string& url);
7475

7576
// -----------------------------------------------------------------------------------------------------------
7677

77-
#if defined(HAVE_LIBLO) && defined(HEADLESS)
78-
# define CARDINAL_INIT_OSC_THREAD
79-
#endif
80-
81-
#ifdef HAVE_LIBLO
82-
# include <lo/lo_types.h>
83-
#endif
84-
8578
START_NAMESPACE_DISTRHO
8679

8780
class CardinalBasePlugin;
@@ -92,22 +85,12 @@ struct Initializer
9285
std::string templatePath;
9386
std::string factoryTemplatePath;
9487
bool shouldSaveSettings = false;
88+
CARDINAL_OSC_FIELDS
9589

9690
Initializer(const CardinalBasePlugin* plugin, const CardinalBaseUI* ui);
9791
~Initializer();
9892
void loadSettings(bool isRealInstance);
99-
100-
#ifdef HAVE_LIBLO
101-
lo_server oscServer = nullptr;
102-
#ifdef CARDINAL_INIT_OSC_THREAD
103-
lo_server_thread oscServerThread = nullptr;
104-
#endif
105-
CardinalBasePlugin* remotePluginInstance = nullptr;
106-
107-
bool startRemoteServer(const char* port);
108-
void stopRemoteServer();
109-
void stepRemoteServer();
110-
#endif
93+
CARDINAL_OSC_METHODS
11194
};
11295

11396
#ifndef HEADLESS

src/CardinalFX/CardinalOSC.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../CardinalOSC.cpp

src/CardinalMini/CardinalOSC.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../CardinalOSC.cpp
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../CardinalOSC.cpp

src/CardinalNative/CardinalOSC.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../CardinalOSC.cpp

0 commit comments

Comments
 (0)