Skip to content

Commit

Permalink
plugin: dynamically find new instances
Browse files Browse the repository at this point in the history
Signed-off-by: falkTX <falktx@falktx.com>
  • Loading branch information
falkTX committed Mar 17, 2024
1 parent 4dc3548 commit 0466d9f
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/PawPaw
Submodule PawPaw updated 1 files
+1 −1 bootstrap-mod.sh
19 changes: 15 additions & 4 deletions src/plugin/DesktopPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,21 @@ class DesktopPlugin : public Plugin,
return;
}

// TODO check available ports
static int port = 1;
int availablePortNum = port;
port += 4;
int availablePortNum = 0;
for (int i = 1; i < 999; ++i)
{
if (shm.canInit(i))
{
availablePortNum = i;
break;
}
}

if (availablePortNum == 0)
{
parameters[kParameterBasePortNumber] = portBaseNum = -kErrorShmSetupFailed;
return;
}

envp = getEvironment(availablePortNum);

Expand Down
69 changes: 51 additions & 18 deletions src/plugin/SharedMemory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ class SharedMemory
float audio[];
}* data = nullptr;

#ifndef DISTRHO_OS_WINDOWS
uint port = 0;
#endif

SharedMemory()
{
}
Expand All @@ -109,37 +113,55 @@ class SharedMemory
deinit();
}

static bool canInit(const uint portBaseNum)
{
char shmName[32] = {};

#ifdef DISTRHO_OS_WINDOWS
std::snprintf(shmName, 31, "\\Global\\mod-desktop-shm-%d", portBaseNum);
const HANDLE shm = OpenFileMappingA(FILE_MAP_ALL_ACCESS, FALSE, shmName);

if (shm == nullptr)
return true;

CloseHandle(shm);
#else
std::snprintf(shmName, 31, "/mod-desktop-shm-%d", portBaseNum);
const int fd = shm_open(shmName, O_RDWR, 0);

if (fd < 0)
return true;

close(fd);
shm_unlink(shmName);
#endif

return false;
}

bool init(const uint portBaseNum)
{
void* ptr;
char semName[32] = {};
char shmName[32] = {};

#ifdef DISTRHO_OS_WINDOWS
SECURITY_ATTRIBUTES sa = {};
sa.nLength = sizeof(sa);
sa.bInheritHandle = TRUE;

std::snprintf(semName, 31, "\\Global\\mod-desktop-shm-%d", portBaseNum);
std::snprintf(shmName, 31, "Local\\mod-desktop-shm-%d", portBaseNum);

shm = CreateFileMappingA(INVALID_HANDLE_VALUE,
&sa,
PAGE_READWRITE|SEC_COMMIT,
0,
static_cast<DWORD>(kDataSize),
"/mod-desktop-test1");
shm = CreateFileMappingA(INVALID_HANDLE_VALUE, &sa, PAGE_READWRITE|SEC_COMMIT, 0, static_cast<DWORD>(kDataSize), shmName);
DISTRHO_SAFE_ASSERT_RETURN(shm != nullptr, false);

ptr = MapViewOfFile(shm, FILE_MAP_ALL_ACCESS, 0, 0, kDataSize);
DISTRHO_SAFE_ASSERT_RETURN(ptr != nullptr, fail_deinit());

VirtualLock(ptr, kDataSize);
#else
std::snprintf(semName, 31, "/mod-desktop-shm-%d", portBaseNum);

// FIXME
shm_unlink(semName);
std::snprintf(shmName, 31, "/mod-desktop-shm-%d", portBaseNum);

shmfd = shm_open(semName, O_CREAT|O_EXCL|O_RDWR, 0600);
shmfd = shm_open(shmName, O_CREAT|O_EXCL|O_RDWR, 0600);
DISTRHO_SAFE_ASSERT_RETURN(shmfd >= 0, false);

DISTRHO_SAFE_ASSERT_RETURN(ftruncate(shmfd, static_cast<off_t>(kDataSize)) == 0, fail_deinit());
Expand All @@ -164,7 +186,7 @@ class SharedMemory
std::memset(data, 0, kDataSize);
data->magic = 1337;

#if defined(DISTRHO_OS_MAC)
#ifdef DISTRHO_OS_MAC
task = mach_task_self();

mach_port_t bootport;
Expand All @@ -190,20 +212,24 @@ class SharedMemory

semServer1 = new SemaphoreServerRunner(port1, sem1);
semServer2 = new SemaphoreServerRunner(port2, sem2);
#elif defined(DISTRHO_OS_WINDOWS)
#endif

#ifdef DISTRHO_OS_WINDOWS
data->sem1 = CreateSemaphoreA(&sa, 0, 1, nullptr);
DISTRHO_SAFE_ASSERT_RETURN(data->sem1 != nullptr, fail_deinit());

data->sem2 = CreateSemaphoreA(&sa, 0, 1, nullptr);
DISTRHO_SAFE_ASSERT_RETURN(data->sem2 != nullptr, fail_deinit());
#endif
#else
port = portBaseNum;
#endif

return true;
}

void deinit()
{
#if defined(DISTRHO_OS_MAC)
#ifdef DISTRHO_OS_MAC
if (semServer1 != nullptr)
{
semServer1->invalidate();
Expand Down Expand Up @@ -279,9 +305,16 @@ class SharedMemory
if (shmfd >= 0)
{
close(shmfd);
shm_unlink("/mod-desktop-test1");
shmfd = -1;
}

if (port != 0)
{
char shmName[32] = {};
std::snprintf(shmName, 31, "/mod-desktop-shm-%d", port);
shm_unlink(shmName);
port = 0;
}
#endif
}

Expand Down
4 changes: 2 additions & 2 deletions src/plugin/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ char* const* getEvironment(const uint portBaseNum)
std::snwprintf(path, PATH_MAX - 1, L"MOD_DESKTOP_SERVER_NAME=mod-desktop-%u", portBaseNum);
set_envp_value(envpl, path);

std::snwprintf(path, PATH_MAX - 1, L"MOD_DEVICE_HOST_PORT=%u", kPortNumOffset + portBaseNum + 1);
std::snwprintf(path, PATH_MAX - 1, L"MOD_DEVICE_HOST_PORT=%u", kPortNumOffset + portBaseNum * 2);
set_envp_value(envpl, path);

std::snwprintf(path, PATH_MAX - 1, L"MOD_DEVICE_WEBSERVER_PORT=%u", kPortNumOffset + portBaseNum);
Expand All @@ -490,7 +490,7 @@ char* const* getEvironment(const uint portBaseNum)
std::snprintf(path, PATH_MAX - 1, "MOD_DESKTOP_SERVER_NAME=mod-desktop-%u", portBaseNum);
set_envp_value(envp, path);

std::snprintf(path, PATH_MAX - 1, "MOD_DEVICE_HOST_PORT=%u", kPortNumOffset + portBaseNum + 1);
std::snprintf(path, PATH_MAX - 1, "MOD_DEVICE_HOST_PORT=%u", kPortNumOffset + portBaseNum * 2);
set_envp_value(envp, path);

std::snprintf(path, PATH_MAX - 1, "MOD_DEVICE_WEBSERVER_PORT=%u", kPortNumOffset + portBaseNum);
Expand Down

0 comments on commit 0466d9f

Please sign in to comment.