Import all relevant code to remote tool

Signed-off-by: falkTX <falktx@falktx.com>
This commit is contained in:
falkTX 2022-12-25 01:23:31 +00:00
parent 058ad891d2
commit c0fc6cd78b
No known key found for this signature in database
GPG key ID: CDBAA37ABC74FBA0
4 changed files with 310 additions and 107 deletions

View file

@ -30,13 +30,24 @@
#include <engine/Engine.hpp>
#include <ui/common.hpp>
#include "PluginContext.hpp"
#include "extra/ScopedValueSetter.hpp"
#define CARDINAL_TEMPLATE_NAME "init/main.vcv"
extern const std::string CARDINAL_VERSION;
namespace rack {
namespace asset {
std::string patchesPath();
void destroy();
}
namespace engine {
void Engine_setAboutToClose(Engine*);
}
namespace plugin {
void initStaticPlugins();
void destroyStaticPlugins();
void initStaticPlugins();
void destroyStaticPlugins();
}
}
@ -54,17 +65,32 @@ bool requestBufferSizeChange(uint) { return false; }
bool requestMIDI() { return false; }
const char* getPluginFormatName() noexcept { return "Remote"; }
FileBrowserHandle fileBrowserCreate(bool, ulong, double, const FileBrowserOptions&) { return nullptr; }
uint32_t Plugin::getBufferSize() const noexcept { return 128; }
double Plugin::getSampleRate() const noexcept { return 48000; }
bool Plugin::writeMidiEvent(const MidiEvent&) noexcept { return false; }
void UI::editParameter(uint, bool) {}
void UI::setParameterValue(uint, float) {}
void UI::setState(const char*, const char*) {}
bool UI::openFileBrowser(const FileBrowserOptions&) { return false; }
END_NAMESPACE_DISTRHO
int main(const int argc, const char* argv[])
{
using namespace rack;
std::string templatePath, factoryTemplatePath;
// --------------------------------------------------------------------------
#ifdef DISTRHO_OS_WASM
settings::allowCursorLock = true;
#else
settings::allowCursorLock = false;
#endif
settings::autoCheckUpdates = false;
settings::autosaveInterval = 0;
settings::devMode = true;
@ -98,20 +124,18 @@ int main(const int argc, const char* argv[])
random::init();
ui::init();
std::string templatePath;
#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 (system::exists(system::join(asset::systemDir, "res")))
{
templatePath = CARDINAL_PLUGIN_SOURCE_DIR DISTRHO_OS_SEP_STR "template.vcv";
}
// If source code dir does not exist use install target prefix as system dir
else
if (!system::exists(system::join(asset::systemDir, "res")))
#endif
{
#if defined(ARCH_MAC)
#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);
@ -121,33 +145,37 @@ int main(const int argc, const char* argv[])
asset::systemDir = CARDINAL_PLUGIN_PREFIX "/share/cardinal";
#endif
if (! asset::systemDir.empty())
{
asset::bundlePath = system::join(asset::systemDir, "PluginManifests");
templatePath = system::join(asset::systemDir, "template.vcv");
}
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 version %s", APP_NAME.c_str(), APP_EDITION.c_str(), APP_VERSION.c_str());
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());
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.");
"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());
"Make sure Cardinal was downloaded and installed correctly.", asset::systemDir.c_str());
}
INFO("Initializing plugins");
@ -156,8 +184,10 @@ int main(const int argc, const char* argv[])
INFO("Initializing plugin browser DB");
app::browserInit();
// --------------------------------------------------------------------------
// create unique temporary path for this instance
std::string autosavePath;
std::string fAutosavePath;
try {
char uidBuf[24];
@ -165,58 +195,92 @@ int main(const int argc, const char* argv[])
for (int i=1;; ++i)
{
std::snprintf(uidBuf, sizeof(uidBuf), "CardinalRemote.%04d", i);
std::snprintf(uidBuf, sizeof(uidBuf), "Cardinal.%04d", i);
const std::string trypath = rack::system::join(tmp, uidBuf);
if (! rack::system::exists(trypath))
{
if (rack::system::createDirectories(trypath))
autosavePath = trypath;
fAutosavePath = trypath;
break;
}
}
} DISTRHO_SAFE_EXCEPTION("create unique temporary path");
CardinalPluginContext context(nullptr);
rack::contextSet(&context);
CardinalPluginContext* const context = new CardinalPluginContext(nullptr);
rack::contextSet(context);
context.bufferSize = 512;
rack::settings::sampleRate = context.sampleRate = 48000;
const float sampleRate = 48000;
rack::settings::sampleRate = sampleRate;
context.engine = new rack::engine::Engine;
context.engine->setSampleRate(context.sampleRate);
context->bufferSize = 512;
context->sampleRate = sampleRate;
context.history = new rack::history::State;
context.patch = new rack::patch::Manager;
context.patch->autosavePath = autosavePath;
context.patch->templatePath = templatePath;
context->engine = new rack::engine::Engine;
context->engine->setSampleRate(sampleRate);
context.event = new rack::widget::EventState;
context.scene = new rack::app::Scene;
context.event->rootWidget = context.scene;
context.window = new rack::window::Window;
context->history = new rack::history::State;
context->patch = new rack::patch::Manager;
context->patch->autosavePath = fAutosavePath;
context->patch->templatePath = templatePath;
context->patch->factoryTemplatePath = factoryTemplatePath;
context.patch->loadTemplate();
context.scene->rackScroll->reset();
context->event = new rack::widget::EventState;
context->scene = new rack::app::Scene;
context->event->rootWidget = context->scene;
context->window = new rack::window::Window;
context->patch->loadTemplate();
context->scene->rackScroll->reset();
// swap to factory template after first load
context->patch->templatePath = context->patch->factoryTemplatePath;
// --------------------------------------------------------------------------
Application app;
Window win(app);
win.setResizable(true);
win.setTitle("CardinalRemote");
Window window(app);
window.setIgnoringKeyRepeat(true);
window.setResizable(true);
window.setTitle("CardinalRemote");
// --------------------------------------------------------------------------
const double scaleFactor = window.getScaleFactor();
window.setGeometryConstraints(648 * scaleFactor, 538 * scaleFactor);
if (scaleFactor != 1.0)
window.setSize(DISTRHO_UI_DEFAULT_WIDTH * scaleFactor,
DISTRHO_UI_DEFAULT_HEIGHT * scaleFactor);
// --------------------------------------------------------------------------
ScopedPointer<CardinalRemoteUI> remoteUI;
{
const Window::ScopedGraphicsContext sgc(win);
remoteUI = new CardinalRemoteUI(win, templatePath);
const Window::ScopedGraphicsContext sgc(window);
remoteUI = new CardinalRemoteUI(window, templatePath);
}
win.show();
window.show();
app.exec();
context.patch->clear();
// --------------------------------------------------------------------------
if (! autosavePath.empty())
rack::system::removeRecursively(autosavePath);
{
context->patch->clear();
// do a little dance to prevent context scene deletion from saving to temp dir
const ScopedValueSetter<bool> svs(rack::settings::headless, true);
Engine_setAboutToClose(context->engine);
delete context;
}
if (! fAutosavePath.empty())
rack::system::removeRecursively(fAutosavePath);
// --------------------------------------------------------------------------
INFO("Clearing asset paths");
asset::bundlePath.clear();
@ -226,11 +290,16 @@ int main(const int argc, const char* argv[])
INFO("Destroying plugins");
plugin::destroyStaticPlugins();
INFO("Destroying colourized assets");
asset::destroy();
INFO("Destroying settings");
settings::destroy();
INFO("Destroying logger");
logger::destroy();
// --------------------------------------------------------------------------
return 0;
}