Skip to content

Commit

Permalink
Start of multi-instance details
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 eb09b71 commit 3a1b689
Show file tree
Hide file tree
Showing 16 changed files with 206 additions and 82 deletions.
2 changes: 1 addition & 1 deletion src/DPF
2 changes: 1 addition & 1 deletion src/mod-host
2 changes: 1 addition & 1 deletion src/mod-ui
30 changes: 30 additions & 0 deletions src/plugin/DesktopAudioDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
# include <fcntl.h>
# include <sys/mman.h>
# ifdef __APPLE__
# include <dispatch/dispatch.h>
# include <mach/mach.h>
# include <mach/semaphore.h>
# include <servers/bootstrap.h>
# else
# include <cerrno>
# include <syscall.h>
# include <sys/prctl.h>
# include <sys/time.h>
# include <linux/futex.h>
# endif
Expand All @@ -29,6 +31,33 @@ namespace Jack

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

#ifdef __APPLE__
static void terminateHandler(void*)
{
printf("Desktop driver parent has died, terminating ourselves now\n");
fflush(stdout);
kill(getpid(), SIGTERM);
}
#endif

static void terminateOnParentExit() noexcept
{
#if defined(__APPLE__)
const dispatch_source_t source = dispatch_source_create(DISPATCH_SOURCE_TYPE_PROC,
getppid(),
DISPATCH_PROC_EXIT,
nullptr);

dispatch_source_set_event_handler_f(source, terminateHandler);

dispatch_resume(source);
#elif !defined(_WIN32)
prctl(PR_SET_PDEATHSIG, SIGTERM);
#endif
}

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

class DesktopAudioDriver : public JackAudioDriver
{
struct Data {
Expand Down Expand Up @@ -516,6 +545,7 @@ SERVER_EXPORT Jack::JackDriverClientInterface* driver_initialize(Jack::JackLocke
if (driver->Open(period, rate, true, true, 2, 2, false, "", "", 0, 0) == 0)
{
printf("%03d:%s OK\n", __LINE__, __FUNCTION__);
Jack::terminateOnParentExit();
return driver;
}

Expand Down
14 changes: 9 additions & 5 deletions src/plugin/DesktopPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class DesktopPlugin : public Plugin,
float* tmpBuffers[2] = {};
uint32_t numFramesInShmBuffer = 0;
uint32_t numFramesInTmpBuffer = 0;
uint portBaseNum = 0;

#ifdef DISTRHO_OS_WINDOWS
const WCHAR* envp;
Expand All @@ -40,7 +41,10 @@ class DesktopPlugin : public Plugin,
if (isDummyInstance())
return;

envp = getEvironment();
// TODO check available ports
portBaseNum = 1;

envp = getEvironment(portBaseNum);

if (shm.init() && run())
startRunner(500);
Expand Down Expand Up @@ -91,15 +95,15 @@ class DesktopPlugin : public Plugin,
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");
const String servernameStr("mod-desktop-" + String(portBaseNum));
const String sampleRateStr(static_cast<int>(getSampleRate()));

const char* const jackd_args[] = {
jackdStr.buffer(),
"-R",
"-S",
"-n", "mod-desktop",
"-C",
jacksessionStr.buffer(),
"-n", servernameStr.buffer(),
"-C", jacksessionStr.buffer(),
"-d", "desktop",
"-r", sampleRateStr.buffer(),
nullptr
Expand Down Expand Up @@ -129,7 +133,7 @@ class DesktopPlugin : public Plugin,
return mod_ui.start(mod_ui_args, envp);
}

parameters[kParameterBasePortNumber] = 1;
parameters[kParameterBasePortNumber] = portBaseNum;
return true;
}

Expand Down
27 changes: 20 additions & 7 deletions src/plugin/DesktopUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class DesktopUI : public UI,
Button buttonOpenWebGui;
Button buttonOpenUserFilesDir;
String label;
uint port = 0;
void* webview = nullptr;

public:
Expand Down Expand Up @@ -54,8 +55,6 @@ class DesktopUI : public UI,
label += getPluginFormatName();
label += " v" VERSION;

webview = addWebView(getWindow().getNativeWindowHandle());

if (d_isNotEqual(scaleFactor, 1.0))
{
setGeometryConstraints((DISTRHO_UI_DEFAULT_WIDTH - 100) * scaleFactor,
Expand All @@ -70,7 +69,8 @@ class DesktopUI : public UI,

~DesktopUI() override
{
destroyWebView(webview);
if (webview != nullptr)
destroyWebView(webview);
}

protected:
Expand All @@ -85,8 +85,17 @@ class DesktopUI : public UI,
{
if (index == kParameterBasePortNumber)
{
// TODO set port
reloadWebView(webview);
if (webview != nullptr)
{
destroyWebView(webview);
webview = nullptr;
}

port = d_roundToUnsignedInt(value);
DISTRHO_SAFE_ASSERT_RETURN(port != 0,);

port += kPortNumOffset;
webview = addWebView(getWindow().getNativeWindowHandle(), port);
}
}

Expand Down Expand Up @@ -120,10 +129,11 @@ class DesktopUI : public UI,
switch (widget->getId())
{
case 1:
reloadWebView(webview);
if (webview != nullptr)
reloadWebView(webview);
break;
case 2:
openWebGui();
openWebGui(port);
break;
case 3:
openUserFilesDir();
Expand All @@ -135,6 +145,9 @@ class DesktopUI : public UI,
{
UI::onResize(ev);

if (webview == nullptr)
return;

const double scaleFactor = getScaleFactor();

uint offset = kVerticalOffset * scaleFactor;
Expand Down
1 change: 1 addition & 0 deletions src/plugin/DistrhoPluginInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#define DISTRHO_UI_USER_RESIZABLE 1

static const constexpr unsigned int kVerticalOffset = 30;
static const constexpr unsigned int kPortNumOffset = 18190;

enum Parameters {
kParameterBasePortNumber,
Expand Down
2 changes: 1 addition & 1 deletion src/plugin/WebView.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ START_NAMESPACE_DISTRHO

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

void* addWebView(uintptr_t viewptr);
void* addWebView(uintptr_t parentWinId, uint port);
void destroyWebView(void* webview);
void reloadWebView(void* webview);
void resizeWebView(void* webview, uint offset, uint width, uint height);
Expand Down
49 changes: 35 additions & 14 deletions src/plugin/WebView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,65 @@

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

void* addWebView(const uintptr_t viewptr)
struct WebViewImpl {
NSView* const view;
WKWebView* const webview;
NSURLRequest* const urlreq;
};

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

void* addWebView(const uintptr_t parentWinId, const uint port)
{
NSView* const view = reinterpret_cast<NSView*>(viewptr);
NSView* const view = reinterpret_cast<NSView*>(parentWinId);

const CGRect rect = CGRectMake(0,
kVerticalOffset,
DISTRHO_UI_DEFAULT_WIDTH,
DISTRHO_UI_DEFAULT_HEIGHT - kVerticalOffset);

WKWebView* const webview = [[WKWebView alloc] initWithFrame: rect];

[[[webview configuration] preferences] setValue: @(true) forKey: @"developerExtrasEnabled"];

[view addSubview:webview];

char url[32];
std::snprintf(url, 31, "http://127.0.0.1:%u/", port);
NSString* const nsurl = [[NSString alloc]
initWithBytes:url
length:std::strlen(url)
encoding:NSUTF8StringEncoding];
NSURLRequest* const urlreq = [[NSURLRequest alloc] initWithURL: [NSURL URLWithString: nsurl]];
[nsurl release];

[webview loadRequest: urlreq];
[webview setHidden:NO];

return webview;
return new WebViewImpl{view, webview, urlreq};
}

void destroyWebView(void* const webviewptr)
void destroyWebView(void* const webview)
{
WKWebView* const webview = static_cast<WKWebView*>(webviewptr);
WebViewImpl* const impl = static_cast<WebViewImpl*>(webview);

[impl->webview setHidden:YES];
[impl->webview removeFromSuperview];
[impl->urlreq release];

[webview setHidden:YES];
delete impl;
}

void reloadWebView(void* const webviewptr)
void reloadWebView(void* const webview)
{
WKWebView* const webview = static_cast<WKWebView*>(webviewptr);
WebViewImpl* const impl = static_cast<WebViewImpl*>(webview);

[webview loadRequest: [NSURLRequest requestWithURL: [NSURL URLWithString:@"http://127.0.0.1:18181/"]]];
[impl->webview loadRequest: impl->urlreq];
}

void resizeWebView(void* const webviewptr, const uint offset, const uint width, const uint height)
void resizeWebView(void* const webview, const uint offset, const uint width, const uint height)
{
WKWebView* const webview = static_cast<WKWebView*>(webviewptr);
WebViewImpl* const impl = static_cast<WebViewImpl*>(webview);

[webview setFrame:CGRectMake(0, offset, width, height)];
[impl->webview setFrame:CGRectMake(0, offset, width, height)];
}

// -----------------------------------------------------------------------------------------------------------
Expand Down
Loading

0 comments on commit 3a1b689

Please sign in to comment.