Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

218 lines
5.6KB

  1. /*
  2. * JackBridge (Part 2, Semaphore + Shared memory and other misc functions)
  3. * Copyright (C) 2013-2025 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. * or without fee is hereby granted, provided that the above copyright notice and this
  7. * permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  10. * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  11. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  12. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  13. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include "JackBridge.hpp"
  17. #ifdef JACKBRIDGE_DUMMY
  18. # include "CarlaUtils.hpp"
  19. #else
  20. # ifdef __WINE__
  21. # define NOMINMAX
  22. # include <winsock2.h>
  23. # endif
  24. # include "CarlaProcessUtils.hpp"
  25. # include "CarlaSemUtils.hpp"
  26. # include "CarlaShmUtils.hpp"
  27. # ifdef __WINE__
  28. # include "utils/PipeClient.cpp"
  29. # endif
  30. #endif // ! JACKBRIDGE_DUMMY
  31. // --------------------------------------------------------------------------------------------------------------------
  32. bool jackbridge_sem_init(void* sem) noexcept
  33. {
  34. CARLA_SAFE_ASSERT_RETURN(sem != nullptr, false);
  35. #ifndef JACKBRIDGE_DUMMY
  36. return carla_sem_create2(*(carla_sem_t*)sem, true);
  37. #endif
  38. }
  39. void jackbridge_sem_destroy(void* sem) noexcept
  40. {
  41. CARLA_SAFE_ASSERT_RETURN(sem != nullptr,);
  42. #ifndef JACKBRIDGE_DUMMY
  43. carla_sem_destroy2(*(carla_sem_t*)sem);
  44. #endif
  45. }
  46. bool jackbridge_sem_connect(void* sem) noexcept
  47. {
  48. CARLA_SAFE_ASSERT_RETURN(sem != nullptr, false);
  49. #ifdef JACKBRIDGE_DUMMY
  50. return false;
  51. #else
  52. return true;
  53. #endif
  54. }
  55. void jackbridge_sem_post(void* sem, bool) noexcept
  56. {
  57. CARLA_SAFE_ASSERT_RETURN(sem != nullptr,);
  58. #ifndef JACKBRIDGE_DUMMY
  59. carla_sem_post(*(carla_sem_t*)sem);
  60. #endif
  61. }
  62. #ifndef CARLA_OS_WASM
  63. bool jackbridge_sem_timedwait(void* sem, uint msecs, bool) noexcept
  64. {
  65. CARLA_SAFE_ASSERT_RETURN(sem != nullptr, false);
  66. #ifdef JACKBRIDGE_DUMMY
  67. return false;
  68. #else
  69. return carla_sem_timedwait(*(carla_sem_t*)sem, msecs);
  70. #endif
  71. }
  72. #endif
  73. // --------------------------------------------------------------------------------------------------------------------
  74. bool jackbridge_shm_is_valid(const void* shm) noexcept
  75. {
  76. CARLA_SAFE_ASSERT_RETURN(shm != nullptr, false);
  77. #ifdef JACKBRIDGE_DUMMY
  78. return false;
  79. #else
  80. return carla_is_shm_valid(*(const carla_shm_t*)shm);
  81. #endif
  82. }
  83. void jackbridge_shm_init(void* shm) noexcept
  84. {
  85. CARLA_SAFE_ASSERT_RETURN(shm != nullptr,);
  86. #ifndef JACKBRIDGE_DUMMY
  87. carla_shm_init(*(carla_shm_t*)shm);
  88. #endif
  89. }
  90. void jackbridge_shm_attach(void* shm, const char* name) noexcept
  91. {
  92. CARLA_SAFE_ASSERT_RETURN(shm != nullptr,);
  93. #ifndef JACKBRIDGE_DUMMY
  94. *(carla_shm_t*)shm = carla_shm_attach(name);
  95. #endif
  96. }
  97. void jackbridge_shm_close(void* shm) noexcept
  98. {
  99. CARLA_SAFE_ASSERT_RETURN(shm != nullptr,);
  100. #ifndef JACKBRIDGE_DUMMY
  101. carla_shm_close(*(carla_shm_t*)shm);
  102. #endif
  103. }
  104. void* jackbridge_shm_map(void* shm, uint64_t size) noexcept
  105. {
  106. CARLA_SAFE_ASSERT_RETURN(shm != nullptr, nullptr);
  107. #ifdef JACKBRIDGE_DUMMY
  108. return nullptr;
  109. #else
  110. return carla_shm_map(*(carla_shm_t*)shm, static_cast<std::size_t>(size));
  111. #endif
  112. }
  113. void jackbridge_shm_unmap(void* shm, void* ptr) noexcept
  114. {
  115. CARLA_SAFE_ASSERT_RETURN(shm != nullptr,);
  116. #ifndef JACKBRIDGE_DUMMY
  117. return carla_shm_unmap(*(carla_shm_t*)shm, ptr);
  118. #endif
  119. }
  120. // --------------------------------------------------------------------------------------------------------------------
  121. #if !defined(JACKBRIDGE_DUMMY) && defined(__WINE__)
  122. static void discovery_pipe_callback(void*, const char* const msg) noexcept
  123. {
  124. carla_stdout("discovery msgReceived %s", msg);
  125. }
  126. #endif
  127. void* jackbridge_discovery_pipe_create(const char* argv[])
  128. {
  129. #if defined(JACKBRIDGE_DUMMY) || !defined(__WINE__)
  130. return nullptr;
  131. // unused
  132. (void)argv;
  133. #else
  134. return carla_pipe_client_new(argv, discovery_pipe_callback, nullptr);
  135. #endif
  136. }
  137. void jackbridge_discovery_pipe_message(void* pipe, const char* key, const char* value)
  138. {
  139. #if defined(JACKBRIDGE_DUMMY) || !defined(__WINE__)
  140. // unused
  141. (void)pipe;
  142. (void)key;
  143. (void)value;
  144. #else
  145. carla_pipe_client_lock(pipe);
  146. carla_pipe_client_write_and_fix_msg(pipe, key);
  147. carla_pipe_client_write_and_fix_msg(pipe, value);
  148. carla_pipe_client_flush_and_unlock(pipe);
  149. #endif
  150. }
  151. void jackbridge_discovery_pipe_destroy(void* pipe)
  152. {
  153. #if defined(JACKBRIDGE_DUMMY) || !defined(__WINE__)
  154. // unused
  155. (void)pipe;
  156. #else
  157. carla_pipe_client_lock(pipe);
  158. carla_pipe_client_write_msg(pipe, "exiting\n");
  159. carla_pipe_client_flush_and_unlock(pipe);
  160. // NOTE: no more messages are handled after this point
  161. // pData->clientClosingDown = true;
  162. for (int i=0; i < 100 && carla_pipe_client_is_running(pipe); ++i)
  163. {
  164. d_msleep(50);
  165. carla_pipe_client_idle(pipe);
  166. }
  167. if (carla_pipe_client_is_running(pipe))
  168. carla_stderr2("jackbridge_discovery_pipe_destroy: pipe is still running!");
  169. carla_pipe_client_destroy(pipe);
  170. #endif
  171. }
  172. // --------------------------------------------------------------------------------------------------------------------
  173. void jackbridge_parent_deathsig(bool kill) noexcept
  174. {
  175. #ifndef JACKBRIDGE_DUMMY
  176. carla_terminateProcessOnParentExit(kill);
  177. #endif
  178. }
  179. // --------------------------------------------------------------------------------------------------------------------