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.

215 lines
5.6KB

  1. /*
  2. * JackBridge (Part 2, Semaphore + Shared memory and other misc functions)
  3. * Copyright (C) 2013-2023 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. # include "CarlaProcessUtils.hpp"
  21. # include "CarlaSemUtils.hpp"
  22. # include "CarlaShmUtils.hpp"
  23. # include "CarlaTimeUtils.hpp"
  24. # ifdef __WINE__
  25. # include "utils/PipeClient.cpp"
  26. # endif
  27. #endif // ! JACKBRIDGE_DUMMY
  28. // --------------------------------------------------------------------------------------------------------------------
  29. bool jackbridge_sem_init(void* sem) noexcept
  30. {
  31. CARLA_SAFE_ASSERT_RETURN(sem != nullptr, false);
  32. #ifndef JACKBRIDGE_DUMMY
  33. return carla_sem_create2(*(carla_sem_t*)sem, true);
  34. #endif
  35. }
  36. void jackbridge_sem_destroy(void* sem) noexcept
  37. {
  38. CARLA_SAFE_ASSERT_RETURN(sem != nullptr,);
  39. #ifndef JACKBRIDGE_DUMMY
  40. carla_sem_destroy2(*(carla_sem_t*)sem);
  41. #endif
  42. }
  43. bool jackbridge_sem_connect(void* sem) noexcept
  44. {
  45. CARLA_SAFE_ASSERT_RETURN(sem != nullptr, false);
  46. #ifdef JACKBRIDGE_DUMMY
  47. return false;
  48. #else
  49. return carla_sem_connect(*(carla_sem_t*)sem);
  50. #endif
  51. }
  52. void jackbridge_sem_post(void* sem, bool server) noexcept
  53. {
  54. CARLA_SAFE_ASSERT_RETURN(sem != nullptr,);
  55. #ifndef JACKBRIDGE_DUMMY
  56. carla_sem_post(*(carla_sem_t*)sem, server);
  57. #endif
  58. }
  59. #ifndef CARLA_OS_WASM
  60. bool jackbridge_sem_timedwait(void* sem, uint msecs, bool server) noexcept
  61. {
  62. CARLA_SAFE_ASSERT_RETURN(sem != nullptr, false);
  63. #ifdef JACKBRIDGE_DUMMY
  64. return false;
  65. #else
  66. return carla_sem_timedwait(*(carla_sem_t*)sem, msecs, server);
  67. #endif
  68. }
  69. #endif
  70. // --------------------------------------------------------------------------------------------------------------------
  71. bool jackbridge_shm_is_valid(const void* shm) noexcept
  72. {
  73. CARLA_SAFE_ASSERT_RETURN(shm != nullptr, false);
  74. #ifdef JACKBRIDGE_DUMMY
  75. return false;
  76. #else
  77. return carla_is_shm_valid(*(const carla_shm_t*)shm);
  78. #endif
  79. }
  80. void jackbridge_shm_init(void* shm) noexcept
  81. {
  82. CARLA_SAFE_ASSERT_RETURN(shm != nullptr,);
  83. #ifndef JACKBRIDGE_DUMMY
  84. carla_shm_init(*(carla_shm_t*)shm);
  85. #endif
  86. }
  87. void jackbridge_shm_attach(void* shm, const char* name) noexcept
  88. {
  89. CARLA_SAFE_ASSERT_RETURN(shm != nullptr,);
  90. #ifndef JACKBRIDGE_DUMMY
  91. *(carla_shm_t*)shm = carla_shm_attach(name);
  92. #endif
  93. }
  94. void jackbridge_shm_close(void* shm) noexcept
  95. {
  96. CARLA_SAFE_ASSERT_RETURN(shm != nullptr,);
  97. #ifndef JACKBRIDGE_DUMMY
  98. carla_shm_close(*(carla_shm_t*)shm);
  99. #endif
  100. }
  101. void* jackbridge_shm_map(void* shm, uint64_t size) noexcept
  102. {
  103. CARLA_SAFE_ASSERT_RETURN(shm != nullptr, nullptr);
  104. #ifdef JACKBRIDGE_DUMMY
  105. return nullptr;
  106. #else
  107. return carla_shm_map(*(carla_shm_t*)shm, static_cast<std::size_t>(size));
  108. #endif
  109. }
  110. void jackbridge_shm_unmap(void* shm, void* ptr) noexcept
  111. {
  112. CARLA_SAFE_ASSERT_RETURN(shm != nullptr,);
  113. #ifndef JACKBRIDGE_DUMMY
  114. return carla_shm_unmap(*(carla_shm_t*)shm, ptr);
  115. #endif
  116. }
  117. // --------------------------------------------------------------------------------------------------------------------
  118. #if !defined(JACKBRIDGE_DUMMY) && defined(__WINE__)
  119. static void discovery_pipe_callback(void*, const char* const msg) noexcept
  120. {
  121. carla_stdout("discovery msgReceived %s", msg);
  122. }
  123. #endif
  124. void* jackbridge_discovery_pipe_create(const char* argv[])
  125. {
  126. #if defined(JACKBRIDGE_DUMMY) || !defined(__WINE__)
  127. return nullptr;
  128. // unused
  129. (void)argv;
  130. #else
  131. return carla_pipe_client_new(argv, discovery_pipe_callback, nullptr);
  132. #endif
  133. }
  134. void jackbridge_discovery_pipe_message(void* pipe, const char* key, const char* value)
  135. {
  136. #if defined(JACKBRIDGE_DUMMY) || !defined(__WINE__)
  137. // unused
  138. (void)pipe;
  139. (void)key;
  140. (void)value;
  141. #else
  142. carla_pipe_client_lock(pipe);
  143. carla_pipe_client_write_and_fix_msg(pipe, key);
  144. carla_pipe_client_write_and_fix_msg(pipe, value);
  145. carla_pipe_client_flush_and_unlock(pipe);
  146. #endif
  147. }
  148. void jackbridge_discovery_pipe_destroy(void* pipe)
  149. {
  150. #if defined(JACKBRIDGE_DUMMY) || !defined(__WINE__)
  151. // unused
  152. (void)pipe;
  153. #else
  154. carla_pipe_client_lock(pipe);
  155. carla_pipe_client_write_msg(pipe, "exiting\n");
  156. carla_pipe_client_flush_and_unlock(pipe);
  157. // NOTE: no more messages are handled after this point
  158. // pData->clientClosingDown = true;
  159. for (int i=0; i < 100 && carla_pipe_client_is_running(pipe); ++i)
  160. {
  161. carla_msleep(50);
  162. carla_pipe_client_idle(pipe);
  163. }
  164. if (carla_pipe_client_is_running(pipe))
  165. carla_stderr2("jackbridge_discovery_pipe_destroy: pipe is still running!");
  166. carla_pipe_client_destroy(pipe);
  167. #endif
  168. }
  169. // --------------------------------------------------------------------------------------------------------------------
  170. void jackbridge_parent_deathsig(bool kill) noexcept
  171. {
  172. #ifndef JACKBRIDGE_DUMMY
  173. carla_terminateProcessOnParentExit(kill);
  174. #endif
  175. }
  176. // --------------------------------------------------------------------------------------------------------------------