diff --git a/src/plugin/ChildProcess.hpp b/src/plugin/ChildProcess.hpp index 22c7b7f..5e6bb5a 100644 --- a/src/plugin/ChildProcess.hpp +++ b/src/plugin/ChildProcess.hpp @@ -24,7 +24,12 @@ START_NAMESPACE_DISTRHO // FIXME -#define P "/home/falktx/Source/MOD/mod-app/build" +#if defined(DISTRHO_OS_MAC) +# define P "/Users/falktx/Source/MOD/mod-app/build/mod-desktop.app/Contents" +#elif defined(DISTRHO_OS_WINDOWS) +#else +# define P "/home/falktx/Source/MOD/mod-app/build" +#endif // ----------------------------------------------------------------------------------------------------------- @@ -32,7 +37,7 @@ class ChildProcess { #ifdef _WIN32 #else - pid_t pid = 0; + pid_t pid = -1; #endif public: @@ -49,13 +54,22 @@ class ChildProcess { // FIXME const char* const envp[] = { + "MOD_DESKTOP=1", + "LANG=en_US.UTF-8", + #if defined(DISTRHO_OS_MAC) + "DYLD_LIBRARY_PATH=" P "/MacOS", + "JACK_DRIVER_DIR=" P "/MacOS/jack", + "MOD_DATA_DIR=/Users/falktx/Documents/MOD Desktop", + "MOD_FACTORY_PEDALBOARDS_DIR=" P "/Resources/pedalboards", + "LV2_PATH=" P "/PlugIns/LV2", // FIXME + #elif defined(DISTRHO_OS_WINDOWS) + #else "LD_LIBRARY_PATH=" P, "JACK_DRIVER_DIR=" P "/jack", "MOD_DATA_DIR=" P "/data", "MOD_FACTORY_PEDALBOARDS_DIR=" P "/pedalboards", - "MOD_DESKTOP=1", - "LANG=en_US.UTF-8", "LV2_PATH=" P "/plugins", + #endif nullptr }; @@ -82,14 +96,14 @@ class ChildProcess void stop(const uint32_t timeoutInMilliseconds = 2000) { - if (pid == 0) + if (pid <= 0) return; const uint32_t timeout = d_gettime_ms() + timeoutInMilliseconds; const pid_t opid = pid; pid_t ret; bool sendTerminate = true; - pid = 0; + pid = -1; for (;;) { diff --git a/src/plugin/DesktopPlugin.cpp b/src/plugin/DesktopPlugin.cpp index d94b2ae..da437bf 100644 --- a/src/plugin/DesktopPlugin.cpp +++ b/src/plugin/DesktopPlugin.cpp @@ -179,7 +179,7 @@ class DesktopPlugin : public Plugin /* -------------------------------------------------------------------------------------------------------- * Process */ - void activate() + void activate() override { shm.reset(); @@ -187,7 +187,7 @@ class DesktopPlugin : public Plugin numFramesInShmBuffer = numFramesInTmpBuffer = 0; } - void deactivate() + void deactivate() override { } diff --git a/src/plugin/Makefile b/src/plugin/Makefile index fcd42ee..7fcc8ee 100644 --- a/src/plugin/Makefile +++ b/src/plugin/Makefile @@ -11,9 +11,7 @@ NAME = MOD-Desktop # Files to build FILES_DSP = \ - DesktopPlugin.cpp \ - zita-resampler/resampler.cc \ - zita-resampler/resampler-table.cc + DesktopPlugin.cpp FILES_UI = \ DesktopUI.cpp diff --git a/src/plugin/SharedMemory.hpp b/src/plugin/SharedMemory.hpp index 85780bf..796d9e4 100644 --- a/src/plugin/SharedMemory.hpp +++ b/src/plugin/SharedMemory.hpp @@ -5,20 +5,25 @@ #include "DistrhoUtils.hpp" -#if defined(DISTRHO_OS_MAC) -#elif defined(DISTRHO_OS_WINDOWS) +#ifdef DISTRHO_OS_WINDOWS #else -# include # include -# include -# include # include -# include +# ifdef DISTRHO_OS_MAC +# include +# include +# include +# else +# include +# include +# include +# include +# endif #endif START_NAMESPACE_DISTRHO -// ----------------------------------------------------------------------------------------------------------- +// -------------------------------------------------------------------------------------------------------------------- class SharedMemory { @@ -34,40 +39,86 @@ class SharedMemory bool init() { - #if defined(DISTRHO_OS_MAC) - #elif defined(DISTRHO_OS_WINDOWS) + #ifdef DISTRHO_OS_WINDOWS #else fd = shm_open("/mod-desktop-test1", O_CREAT|O_EXCL|O_RDWR, 0600); DISTRHO_SAFE_ASSERT_RETURN(fd >= 0, false); { - const int ret = ::ftruncate(fd, static_cast(kDataSize)); + const int ret = ftruncate(fd, static_cast(kDataSize)); DISTRHO_SAFE_ASSERT_RETURN(ret == 0, fail_deinit()); } { - void* const ptr = ::mmap(nullptr, kDataSize, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_LOCKED, fd, 0); + void* ptr; + + #ifdef MAP_LOCKED + ptr = mmap(nullptr, kDataSize, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_LOCKED, fd, 0); + if (ptr == nullptr || ptr == MAP_FAILED) + #endif + { + ptr = mmap(nullptr, kDataSize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); + } + DISTRHO_SAFE_ASSERT_RETURN(ptr != nullptr, fail_deinit()); DISTRHO_SAFE_ASSERT_RETURN(ptr != MAP_FAILED, fail_deinit()); data = static_cast(ptr); + + #ifndef MAP_LOCKED + mlock(data, kDataSize); + #endif } #endif std::memset(data, 0, kDataSize); data->magic = 1337; + #ifdef DISTRHO_OS_MAC + task = mach_task_self(); + + mach_port_t bootport1, bootport2; + DISTRHO_SAFE_ASSERT_RETURN(task_get_bootstrap_port(task, &bootport1) == KERN_SUCCESS, fail_deinit()); + DISTRHO_SAFE_ASSERT_RETURN(task_get_bootstrap_port(task, &bootport2) == KERN_SUCCESS, fail_deinit()); + DISTRHO_SAFE_ASSERT_RETURN(semaphore_create(task, &sem1, SYNC_POLICY_FIFO, 0) == KERN_SUCCESS, fail_deinit()); + DISTRHO_SAFE_ASSERT_RETURN(semaphore_create(task, &sem2, SYNC_POLICY_FIFO, 0) == KERN_SUCCESS, fail_deinit()); + + static int bootcounter = 0; + std::snprintf(data->bootname1, 31, "mdskt_%d_%d_%p", ++bootcounter, getpid(), &sem1); + std::snprintf(data->bootname2, 31, "mdskt_%d_%d_%p", ++bootcounter, getpid(), &sem2); + data->bootname1[31] = data->bootname2[31] = '\0'; + + #pragma clang diagnostic push + #pragma clang diagnostic ignored "-Wdeprecated-declarations" + DISTRHO_SAFE_ASSERT_RETURN(bootstrap_register(bootport1, data->bootname1, sem1) == KERN_SUCCESS, fail_deinit()); + DISTRHO_SAFE_ASSERT_RETURN(bootstrap_register(bootport2, data->bootname2, sem2) == KERN_SUCCESS, fail_deinit()); + #pragma clang diagnostic pop + #endif + return true; } void deinit() { - #if defined(DISTRHO_OS_MAC) - #elif defined(DISTRHO_OS_WINDOWS) + #ifdef DISTRHO_OS_MAC + if (sem1 != MACH_PORT_NULL) + { + semaphore_destroy(task, sem1); + sem1 = MACH_PORT_NULL; + } + + if (sem2 != MACH_PORT_NULL) + { + semaphore_destroy(task, sem2); + sem2 = MACH_PORT_NULL; + } + #endif + + #ifdef DISTRHO_OS_WINDOWS #else if (data != nullptr) { - ::munmap(data, kDataSize); + munmap(data, kDataSize); data = nullptr; } @@ -80,6 +131,8 @@ class SharedMemory #endif } + // ---------------------------------------------------------------------------------------------------------------- + void getAudioData(float* audio[2]) { DISTRHO_SAFE_ASSERT_RETURN(data != nullptr,); @@ -113,13 +166,22 @@ class SharedMemory return true; } + // ---------------------------------------------------------------------------------------------------------------- + private: struct Data { uint32_t magic; + int32_t padding1; + #if defined(DISTRHO_OS_MAC) + char bootname1[32]; + char bootname2[32]; + #elif defined(DISTRHO_OS_WINDOWS) + HANDLE sem1; + HANDLE sem2; + #else int32_t sem1; int32_t sem2; - int32_t padding1; - // uint8_t sem[32]; + #endif uint16_t midiEventCount; uint16_t midiFrames[511]; uint8_t midiData[511 * 4]; @@ -129,32 +191,50 @@ class SharedMemory static constexpr const size_t kDataSize = sizeof(Data) + sizeof(float) * 128 * 2; + // ---------------------------------------------------------------------------------------------------------------- + // shared memory details + + #ifdef DISTRHO_OS_WINDOWS + #else + int fd = -1; + #endif + + #ifdef DISTRHO_OS_MAC + mach_port_t task = MACH_PORT_NULL; + semaphore_t sem1 = MACH_PORT_NULL; + semaphore_t sem2 = MACH_PORT_NULL; + #endif + + // ---------------------------------------------------------------------------------------------------------------- + // semaphore details + #if defined(DISTRHO_OS_MAC) void post() { + semaphore_signal(sem1); } bool wait() { - return false; + const mach_timespec timeout = { 1, 0 }; + return semaphore_timedwait(sem2, timeout) == KERN_SUCCESS; } #elif defined(DISTRHO_OS_WINDOWS) void post() { + ReleaseSemaphore(data->sem1, 1, nullptr); } bool wait() { - return false; + return WaitForSingleObject(data->sem2, 1000) == WAIT_OBJECT_0; } #else - int fd = -1; - void post() { const bool unlocked = __sync_bool_compare_and_swap(&data->sem1, 0, 1); DISTRHO_SAFE_ASSERT_RETURN(unlocked,); - ::syscall(__NR_futex, &data->sem1, FUTEX_WAKE, 1, nullptr, nullptr, 0); + syscall(__NR_futex, &data->sem1, FUTEX_WAKE, 1, nullptr, nullptr, 0); } bool wait() @@ -166,7 +246,7 @@ class SharedMemory if (__sync_bool_compare_and_swap(&data->sem2, 1, 0)) return true; - if (::syscall(__NR_futex, &data->sem2, FUTEX_WAIT, 0, &timeout, nullptr, 0) != 0) + if (syscall(__NR_futex, &data->sem2, FUTEX_WAIT, 0, &timeout, nullptr, 0) != 0) if (errno != EAGAIN && errno != EINTR) return false; } @@ -182,6 +262,6 @@ class SharedMemory DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(SharedMemory) }; -// ----------------------------------------------------------------------------------------------------------- +// -------------------------------------------------------------------------------------------------------------------- END_NAMESPACE_DISTRHO