Skip to content

Commit

Permalink
Dynamic path handling on Linux too
Browse files Browse the repository at this point in the history
Signed-off-by: falkTX <[email protected]>
  • Loading branch information
falkTX committed Mar 14, 2024
1 parent 3a1b689 commit faaa01a
Show file tree
Hide file tree
Showing 9 changed files with 217 additions and 71 deletions.
4 changes: 2 additions & 2 deletions src/plugin/ChildProcess.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,10 @@ class ChildProcess
#else
if (pid <= 0)
return false;

const pid_t ret = ::waitpid(pid, nullptr, WNOHANG);

if (ret == -1 && errno == ECHILD)
if (ret == pid || (ret == -1 && errno == ECHILD))
{
pid = 0;
return false;
Expand Down
48 changes: 43 additions & 5 deletions src/plugin/DesktopPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class DesktopPlugin : public Plugin,
ChildProcess jackd;
ChildProcess mod_ui;
SharedMemory shm;
bool startingJackd = false;
bool startingModUI = false;
bool processing = false;
bool firstTimeProcessing = true;
float parameters[kParameterCount] = {};
Expand All @@ -42,9 +44,17 @@ class DesktopPlugin : public Plugin,
return;

// TODO check available ports
portBaseNum = 1;
int availablePortNum = 1;

envp = getEvironment(portBaseNum);
envp = getEvironment(availablePortNum);

if (envp == nullptr)
{
parameters[kParameterBasePortNumber] = -kErrorAppDirNotFound;
return;
}

portBaseNum = availablePortNum;

if (shm.init() && run())
startRunner(500);
Expand Down Expand Up @@ -92,6 +102,13 @@ class DesktopPlugin : public Plugin,

if (! jackd.isRunning())
{
if (startingJackd)
{
startingJackd = false;
parameters[kParameterBasePortNumber] = -kErrorJackdExecFailed;
return false;
}

const String appDir(getAppDir());
const String jackdStr(appDir + DISTRHO_OS_SEP_STR "jackd" APP_EXT);
const String jacksessionStr(appDir + DISTRHO_OS_SEP_STR "jack" DISTRHO_OS_SEP_STR "jack-session.conf");
Expand All @@ -109,9 +126,16 @@ class DesktopPlugin : public Plugin,
nullptr
};

return jackd.start(jackd_args, envp);
startingJackd = true;
if (jackd.start(jackd_args, envp))
return true;

parameters[kParameterBasePortNumber] = -kErrorJackdExecFailed;
return false;
}

startingJackd = false;

if (! processing)
{
if (shm.sync())
Expand All @@ -122,6 +146,13 @@ class DesktopPlugin : public Plugin,

if (! mod_ui.isRunning())
{
if (startingModUI)
{
startingModUI = false;
parameters[kParameterBasePortNumber] = -kErrorModUiExecFailed;
return false;
}

const String appDir(getAppDir());
const String moduiStr(appDir + DISTRHO_OS_SEP_STR "mod-ui" APP_EXT);

Expand All @@ -130,9 +161,16 @@ class DesktopPlugin : public Plugin,
nullptr
};

return mod_ui.start(mod_ui_args, envp);
startingModUI = true;
if (mod_ui.start(mod_ui_args, envp))
return true;

parameters[kParameterBasePortNumber] = -kErrorModUiExecFailed;
return false;
}

startingModUI = false;

parameters[kParameterBasePortNumber] = portBaseNum;
return true;
}
Expand Down Expand Up @@ -218,7 +256,7 @@ class DesktopPlugin : public Plugin,
parameter.hints = kParameterIsOutput | kParameterIsInteger;
parameter.name = "base port number";
parameter.symbol = "base_port_num";
parameter.ranges.min = 0.f;
parameter.ranges.min = -kErrorUndefined;
parameter.ranges.max = 512.f;
parameter.ranges.def = 0.f;
break;
Expand Down
51 changes: 50 additions & 1 deletion src/plugin/DesktopUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class DesktopUI : public UI,
Button buttonOpenWebGui;
Button buttonOpenUserFilesDir;
String label;
String error;
String errorDetail;
uint port = 0;
void* webview = nullptr;

Expand Down Expand Up @@ -85,6 +87,41 @@ class DesktopUI : public UI,
{
if (index == kParameterBasePortNumber)
{
if (d_isZero(value))
return;

if (value < 0.f)
{
switch (-d_roundToIntNegative(value))
{
case kErrorAppDirNotFound:
error = "Error: MOD Desktop application directory not found";
errorDetail = "Make sure to install the standalone and run it at least once";
break;
case kErrorJackdExecFailed:
error = "Error: Failed to start jackd";
errorDetail = "";
break;
case kErrorModUiExecFailed:
error = "Error: Failed to start mod-ui";
errorDetail = "";
break;
case kErrorUndefined:
error = "Error initializing MOD Desktop plugin";
errorDetail = "";
break;
}
repaint();
return;
}

if (error.isNotEmpty())
{
error.clear();
errorDetail.clear();
repaint();
}

if (webview != nullptr)
{
destroyWebView(webview);
Expand Down Expand Up @@ -119,9 +156,21 @@ class DesktopUI : public UI,
const double scaleFactor = getScaleFactor();

fillColor(255, 255, 255, 255);
fontSize(14 * scaleFactor);
fontSize(18 * scaleFactor);
textAlign(ALIGN_CENTER | ALIGN_MIDDLE);
text(getWidth() / 2, kVerticalOffset * scaleFactor / 2, label, nullptr);

if (error.isNotEmpty())
{
fontSize(36 * scaleFactor);
text(getWidth() / 2, getHeight() / 2 - 18 * scaleFactor, error, nullptr);

if (errorDetail.isNotEmpty())
{
fontSize(18 * scaleFactor);
text(getWidth() / 2, getHeight() / 2 + 18 * scaleFactor, errorDetail, nullptr);
}
}
}

void buttonClicked(SubWidget* const widget, int button) override
Expand Down
7 changes: 7 additions & 0 deletions src/plugin/DistrhoPluginInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@
static const constexpr unsigned int kVerticalOffset = 30;
static const constexpr unsigned int kPortNumOffset = 18190;

enum Error {
kErrorAppDirNotFound = 1,
kErrorJackdExecFailed,
kErrorModUiExecFailed,
kErrorUndefined
};

enum Parameters {
kParameterBasePortNumber,
kParameterCount
Expand Down
6 changes: 5 additions & 1 deletion src/plugin/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#!/usr/bin/make -f

export DISTRHO_NAMESPACE = DesktopDISTRHO
export DGL_NAMESPACE = DesktopDGL
export NVG_FONT_TEXTURE_FLAGS = NVG_IMAGE_NEAREST

include ../DPF/Makefile.base.mk

# ---------------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -50,7 +54,7 @@ $(TARGET_DIR)/MOD-Desktop-WebView: $(BUILD_DIR)/WebViewQt.cpp.o

$(BUILD_DIR)/WebViewQt.cpp.o: BUILD_CXX_FLAGS += -std=gnu++14 $(shell $(PKG_CONFIG) --cflags Qt5WebEngineWidgets)

include $(BUILD_DIR)/WebViewQt.cpp.d
-include $(BUILD_DIR)/WebViewQt.cpp.d

endif

Expand Down
4 changes: 2 additions & 2 deletions src/plugin/WebViewQt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ int main(int argc, char* argv[])

printf("'%s' '%s'\n", argv[1], argv[2]);

if (argc == 3 && std::strcmp(argv[1], "-xembed") == 0)
if (argc == 4 && std::strcmp(argv[1], "-xembed") == 0)
{
const uintptr_t parentId = std::atoll(argv[2]);
QWindow* const parentWindow = QWindow::fromWinId(parentId);
Expand All @@ -46,7 +46,7 @@ int main(int argc, char* argv[])
webview.setFixedSize(DISTRHO_UI_DEFAULT_WIDTH, DISTRHO_UI_DEFAULT_HEIGHT - kVerticalOffset);
webview.winId();
webview.windowHandle()->setParent(parentWindow);
webview.setUrl(QUrl("http://127.0.0.1:18181/"));
webview.setUrl(QUrl(QString::fromLocal8Bit("http://127.0.0.1:") + QString::fromLocal8Bit(argv[3])));
webview.show();
return app.exec();
}
Expand Down
17 changes: 12 additions & 5 deletions src/plugin/WebViewX11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct WebViewIPC {

// -----------------------------------------------------------------------------------------------------------

void* addWebView(uintptr_t viewptr)
void* addWebView(const uintptr_t parentWinId, const uint port)
{
char webviewTool[PATH_MAX] = {};
{
Expand Down Expand Up @@ -60,10 +60,17 @@ void* addWebView(uintptr_t viewptr)
WebViewIPC* const ipc = new WebViewIPC();
ipc->display = display;
ipc->childWindow = 0;
ipc->ourWindow = viewptr;

const String viewStr(viewptr);
const char* const args[] = { webviewTool, "-platform", "xcb", "-xembed", viewStr.buffer(), nullptr };
ipc->ourWindow = parentWinId;

const String embedIdStr(parentWinId);
const String portStr(port);
const char* const args[] = {
webviewTool,
"-platform", "xcb",
"-xembed", embedIdStr.buffer(),
portStr.buffer(),
nullptr
};
ipc->p.start(args);

return ipc;
Expand Down
Loading

0 comments on commit faaa01a

Please sign in to comment.