Setup mini variant UI, move around more code to accomodate this

Signed-off-by: falkTX <falktx@falktx.com>
This commit is contained in:
falkTX 2022-12-26 09:10:52 +00:00
parent cf74324568
commit a85679758c
No known key found for this signature in database
GPG key ID: CDBAA37ABC74FBA0
12 changed files with 534 additions and 334 deletions

View file

@ -38,6 +38,7 @@
#include <settings.hpp>
#include <string.hpp>
#include <system.hpp>
#include <app/Browser.hpp>
#include <app/Scene.hpp>
#include <window/Window.hpp>
@ -65,6 +66,27 @@
# define HEADLESS
#endif
#if CARDINAL_VARIANT_FX
# define CARDINAL_TEMPLATE_NAME "init/fx.vcv"
#elif CARDINAL_VARIANT_NATIVE
# define CARDINAL_TEMPLATE_NAME "init/native.vcv"
#elif CARDINAL_VARIANT_SYNTH
# define CARDINAL_TEMPLATE_NAME "init/synth.vcv"
#else
# define CARDINAL_TEMPLATE_NAME "init/main.vcv"
#endif
namespace rack {
namespace asset {
std::string patchesPath();
void destroy();
}
namespace plugin {
void initStaticPlugins();
void destroyStaticPlugins();
}
}
const std::string CARDINAL_VERSION = "22.12";
START_NAMESPACE_DISTRHO
@ -172,6 +194,284 @@ void CardinalPluginContext::writeMidiMessage(const rack::midi::Message& message,
plugin->writeMidiEvent(event);
}
// -----------------------------------------------------------------------------------------------------------
#ifdef CARDINAL_INIT_OSC_THREAD
static void osc_error_handler(int num, const char* msg, const char* path)
{
d_stderr("Cardinal OSC Error: code: %i, msg: \"%s\", path: \"%s\")", num, msg, path);
}
static int osc_fallback_handler(const char* const path, const char* const types, lo_arg**, int, lo_message, void*)
{
d_stderr("Cardinal OSC unhandled message \"%s\" with types \"%s\"", path, types);
return 0;
}
static int osc_hello_handler(const char*, const char*, lo_arg**, int, const lo_message m, void* const self)
{
d_stdout("osc_hello_handler()");
const lo_address source = lo_message_get_source(m);
lo_send_from(source, static_cast<Initializer*>(self)->oscServer, LO_TT_IMMEDIATE, "/resp", "ss", "hello", "ok");
return 0;
}
static int osc_load_handler(const char*, const char* types, lo_arg** argv, int argc, const lo_message m, void* const self)
{
d_stdout("osc_load_handler()");
DISTRHO_SAFE_ASSERT_RETURN(argc == 1, 0);
DISTRHO_SAFE_ASSERT_RETURN(types != nullptr && types[0] == 'b', 0);
const int32_t size = argv[0]->blob.size;
DISTRHO_SAFE_ASSERT_RETURN(size > 4, 0);
const uint8_t* const blob = (uint8_t*)(&argv[0]->blob.data);
DISTRHO_SAFE_ASSERT_RETURN(blob != nullptr, 0);
bool ok = false;
if (CardinalBasePlugin* const plugin = static_cast<Initializer*>(self)->oscPlugin)
{
CardinalPluginContext* const context = plugin->context;
std::vector<uint8_t> data(size);
std::memcpy(data.data(), blob, size);
rack::contextSet(context);
rack::system::removeRecursively(context->patch->autosavePath);
rack::system::createDirectories(context->patch->autosavePath);
try {
rack::system::unarchiveToDirectory(data, context->patch->autosavePath);
context->patch->loadAutosave();
ok = true;
}
catch (rack::Exception& e) {
WARN("%s", e.what());
}
rack::contextSet(nullptr);
}
const lo_address source = lo_message_get_source(m);
lo_send_from(source, static_cast<Initializer*>(self)->oscServer,
LO_TT_IMMEDIATE, "/resp", "ss", "load", ok ? "ok" : "fail");
return 0;
}
static int osc_screenshot_handler(const char*, const char* types, lo_arg** argv, int argc, const lo_message m, void* const self)
{
d_stdout("osc_screenshot_handler()");
DISTRHO_SAFE_ASSERT_RETURN(argc == 1, 0);
DISTRHO_SAFE_ASSERT_RETURN(types != nullptr && types[0] == 'b', 0);
const int32_t size = argv[0]->blob.size;
DISTRHO_SAFE_ASSERT_RETURN(size > 4, 0);
const uint8_t* const blob = (uint8_t*)(&argv[0]->blob.data);
DISTRHO_SAFE_ASSERT_RETURN(blob != nullptr, 0);
bool ok = false;
if (CardinalBasePlugin* const plugin = static_cast<Initializer*>(self)->oscPlugin)
ok = plugin->updateStateValue("screenshot", String::asBase64(blob, size).buffer());
const lo_address source = lo_message_get_source(m);
lo_send_from(source, static_cast<Initializer*>(self)->oscServer,
LO_TT_IMMEDIATE, "/resp", "ss", "screenshot", ok ? "ok" : "fail");
return 0;
}
#endif
Initializer::Initializer(const CardinalBasePlugin* const plugin, const CardinalBaseUI* const ui)
{
using namespace rack;
#ifdef DISTRHO_OS_WASM
settings::allowCursorLock = true;
#else
settings::allowCursorLock = false;
#endif
settings::autoCheckUpdates = false;
settings::autosaveInterval = 0;
settings::devMode = true;
settings::isPlugin = true;
settings::skipLoadOnLaunch = true;
settings::showTipsOnLaunch = false;
settings::windowPos = math::Vec(0, 0);
#ifdef HEADLESS
settings::headless = true;
#endif
// copied from https://community.vcvrack.com/t/16-colour-cable-palette/15951
settings::cableColors = {
color::fromHexString("#ff5252"),
color::fromHexString("#ff9352"),
color::fromHexString("#ffd452"),
color::fromHexString("#e8ff52"),
color::fromHexString("#a8ff52"),
color::fromHexString("#67ff52"),
color::fromHexString("#52ff7d"),
color::fromHexString("#52ffbe"),
color::fromHexString("#52ffff"),
color::fromHexString("#52beff"),
color::fromHexString("#527dff"),
color::fromHexString("#6752ff"),
color::fromHexString("#a852ff"),
color::fromHexString("#e952ff"),
color::fromHexString("#ff52d4"),
color::fromHexString("#ff5293"),
};
system::init();
logger::init();
random::init();
ui::init();
if (asset::systemDir.empty())
{
if (const char* const bundlePath = (plugin != nullptr ? plugin->getBundlePath() :
ui != nullptr ? ui->getBundlePath() : nullptr))
{
if (const char* const resourcePath = getResourcePath(bundlePath))
{
asset::systemDir = resourcePath;
asset::bundlePath = system::join(asset::systemDir, "PluginManifests");
}
}
if (asset::systemDir.empty() || ! system::exists(asset::systemDir) || ! system::exists(asset::bundlePath))
{
#ifdef CARDINAL_PLUGIN_SOURCE_DIR
// Make system dir point to source code location as fallback
asset::systemDir = CARDINAL_PLUGIN_SOURCE_DIR DISTRHO_OS_SEP_STR "Rack";
asset::bundlePath.clear();
// If source code dir does not exist use install target prefix as system dir
if (!system::exists(system::join(asset::systemDir, "res")))
#endif
{
#if defined(DISTRHO_OS_WASM)
asset::systemDir = "/resources";
#elif defined(ARCH_MAC)
asset::systemDir = "/Library/Application Support/Cardinal";
#elif defined(ARCH_WIN)
const std::string commonprogfiles = getSpecialPath(kSpecialPathCommonProgramFiles);
if (! commonprogfiles.empty())
asset::systemDir = system::join(commonprogfiles, "Cardinal");
#else
asset::systemDir = CARDINAL_PLUGIN_PREFIX "/share/cardinal";
#endif
asset::bundlePath = system::join(asset::systemDir, "PluginManifests");
}
}
asset::userDir = asset::systemDir;
}
const std::string patchesPath = asset::patchesPath();
#ifdef DISTRHO_OS_WASM
templatePath = system::join(patchesPath, CARDINAL_WASM_WELCOME_TEMPLATE_FILENAME);
#else
templatePath = system::join(patchesPath, CARDINAL_TEMPLATE_NAME);
#endif
factoryTemplatePath = system::join(patchesPath, CARDINAL_TEMPLATE_NAME);
// Log environment
INFO("%s %s %s, compatible with Rack version %s", APP_NAME.c_str(), APP_EDITION.c_str(), CARDINAL_VERSION.c_str(), APP_VERSION.c_str());
INFO("%s", system::getOperatingSystemInfo().c_str());
INFO("Binary filename: %s", getBinaryFilename());
if (plugin != nullptr) {
INFO("Bundle path: %s", plugin->getBundlePath());
} else if (ui != nullptr) {
INFO("Bundle path: %s", ui->getBundlePath());
}
INFO("System directory: %s", asset::systemDir.c_str());
INFO("User directory: %s", asset::userDir.c_str());
INFO("Template patch: %s", templatePath.c_str());
INFO("System template patch: %s", factoryTemplatePath.c_str());
// Report to user if something is wrong with the installation
if (asset::systemDir.empty())
{
d_stderr2("Failed to locate Cardinal plugin bundle.\n"
"Install Cardinal with its bundle folder intact and try again.");
}
else if (! system::exists(asset::systemDir))
{
d_stderr2("System directory \"%s\" does not exist.\n"
"Make sure Cardinal was downloaded and installed correctly.", asset::systemDir.c_str());
}
INFO("Initializing plugins");
plugin::initStaticPlugins();
INFO("Initializing plugin browser DB");
app::browserInit();
#ifdef CARDINAL_INIT_OSC_THREAD
INFO("Initializing OSC Remote control");
oscServer = lo_server_new_with_proto(REMOTE_HOST_PORT, LO_UDP, osc_error_handler);
DISTRHO_SAFE_ASSERT_RETURN(oscServer != nullptr,);
lo_server_add_method(oscServer, "/hello", "", osc_hello_handler, this);
lo_server_add_method(oscServer, "/load", "b", osc_load_handler, this);
lo_server_add_method(oscServer, "/screenshot", "b", osc_screenshot_handler, this);
lo_server_add_method(oscServer, nullptr, nullptr, osc_fallback_handler, nullptr);
startThread();
#else
INFO("OSC Remote control is not enabled in this build");
#endif
}
Initializer::~Initializer()
{
using namespace rack;
#ifdef CARDINAL_INIT_OSC_THREAD
if (oscServer != nullptr)
{
stopThread(5000);
lo_server_del_method(oscServer, nullptr, nullptr);
lo_server_free(oscServer);
oscServer = nullptr;
}
#endif
INFO("Clearing asset paths");
asset::bundlePath.clear();
asset::systemDir.clear();
asset::userDir.clear();
INFO("Destroying plugins");
plugin::destroyStaticPlugins();
INFO("Destroying colourized assets");
asset::destroy();
INFO("Destroying settings");
settings::destroy();
INFO("Destroying logger");
logger::destroy();
}
#ifdef CARDINAL_INIT_OSC_THREAD
void Initializer::run()
{
INFO("OSC Thread Listening for remote commands");
while (! shouldThreadExit())
{
d_msleep(200);
while (lo_server_recv_noblock(oscServer, 0) != 0) {}
}
INFO("OSC Thread Closed");
}
#endif
// --------------------------------------------------------------------------------------------------------------------
END_NAMESPACE_DISTRHO
// --------------------------------------------------------------------------------------------------------------------