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.

219 lines
6.3KB

  1. /*
  2. * Carla Plugin Host
  3. * Copyright (C) 2011-2022 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #include "CarlaUtils.h"
  18. #include "CarlaPipeUtils.hpp"
  19. namespace CB = CARLA_BACKEND_NAMESPACE;
  20. // --------------------------------------------------------------------------------------------------------------------
  21. class ExposedCarlaPipeClient : public CarlaPipeClient
  22. {
  23. public:
  24. ExposedCarlaPipeClient(const CarlaPipeCallbackFunc callbackFunc, void* const callbackPtr) noexcept
  25. : CarlaPipeClient(),
  26. fCallbackFunc(callbackFunc),
  27. fCallbackPtr(callbackPtr),
  28. fLastReadLine(nullptr)
  29. {
  30. CARLA_SAFE_ASSERT(fCallbackFunc != nullptr);
  31. }
  32. ~ExposedCarlaPipeClient() override
  33. {
  34. if (fLastReadLine != nullptr)
  35. {
  36. delete[] fLastReadLine;
  37. fLastReadLine = nullptr;
  38. }
  39. }
  40. const char* readlineblock(const uint timeout) noexcept
  41. {
  42. delete[] fLastReadLine;
  43. fLastReadLine = CarlaPipeClient::_readlineblock(true, 0, timeout);
  44. return fLastReadLine;
  45. }
  46. bool readlineblock_bool(const uint timeout) noexcept
  47. {
  48. if (const char* const line = CarlaPipeClient::_readlineblock(false, 0, timeout))
  49. return std::strcmp(line, "true") == 0;
  50. return false;
  51. }
  52. int readlineblock_int(const uint timeout) noexcept
  53. {
  54. if (const char* const line = CarlaPipeClient::_readlineblock(false, 0, timeout))
  55. return std::atoi(line);
  56. return 0;
  57. }
  58. double readlineblock_float(const uint timeout) noexcept
  59. {
  60. if (const char* const line = CarlaPipeClient::_readlineblock(false, 0, timeout))
  61. return std::atof(line);
  62. return 0.0;
  63. }
  64. bool msgReceived(const char* const msg) noexcept override
  65. {
  66. if (fCallbackFunc != nullptr)
  67. {
  68. try {
  69. fCallbackFunc(fCallbackPtr, msg);
  70. } CARLA_SAFE_EXCEPTION("msgReceived");
  71. }
  72. return true;
  73. }
  74. private:
  75. const CarlaPipeCallbackFunc fCallbackFunc;
  76. void* const fCallbackPtr;
  77. const char* fLastReadLine;
  78. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ExposedCarlaPipeClient)
  79. };
  80. // --------------------------------------------------------------------------------------------------------------------
  81. CarlaPipeClientHandle carla_pipe_client_new(const char* argv[], CarlaPipeCallbackFunc callbackFunc, void* callbackPtr)
  82. {
  83. carla_debug("carla_pipe_client_new(%p, %p, %p)", argv, callbackFunc, callbackPtr);
  84. ExposedCarlaPipeClient* const pipe = new ExposedCarlaPipeClient(callbackFunc, callbackPtr);
  85. if (! pipe->initPipeClient(argv))
  86. {
  87. delete pipe;
  88. return nullptr;
  89. }
  90. return pipe;
  91. }
  92. void carla_pipe_client_idle(CarlaPipeClientHandle handle)
  93. {
  94. CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
  95. ((ExposedCarlaPipeClient*)handle)->idlePipe();
  96. }
  97. bool carla_pipe_client_is_running(CarlaPipeClientHandle handle)
  98. {
  99. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, false);
  100. return ((ExposedCarlaPipeClient*)handle)->isPipeRunning();
  101. }
  102. void carla_pipe_client_lock(CarlaPipeClientHandle handle)
  103. {
  104. CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
  105. return ((ExposedCarlaPipeClient*)handle)->lockPipe();
  106. }
  107. void carla_pipe_client_unlock(CarlaPipeClientHandle handle)
  108. {
  109. CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
  110. return ((ExposedCarlaPipeClient*)handle)->unlockPipe();
  111. }
  112. const char* carla_pipe_client_readlineblock(CarlaPipeClientHandle handle, uint timeout)
  113. {
  114. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  115. return ((ExposedCarlaPipeClient*)handle)->readlineblock(timeout);
  116. }
  117. bool carla_pipe_client_readlineblock_bool(CarlaPipeClientHandle handle, uint timeout)
  118. {
  119. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, false);
  120. return ((ExposedCarlaPipeClient*)handle)->readlineblock_bool(timeout);
  121. }
  122. int carla_pipe_client_readlineblock_int(CarlaPipeClientHandle handle, uint timeout)
  123. {
  124. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, 0);
  125. return ((ExposedCarlaPipeClient*)handle)->readlineblock_int(timeout);
  126. }
  127. double carla_pipe_client_readlineblock_float(CarlaPipeClientHandle handle, uint timeout)
  128. {
  129. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, 0.0);
  130. return ((ExposedCarlaPipeClient*)handle)->readlineblock_float(timeout);
  131. }
  132. bool carla_pipe_client_write_msg(CarlaPipeClientHandle handle, const char* msg)
  133. {
  134. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, false);
  135. return ((ExposedCarlaPipeClient*)handle)->writeMessage(msg);
  136. }
  137. bool carla_pipe_client_write_and_fix_msg(CarlaPipeClientHandle handle, const char* msg)
  138. {
  139. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, false);
  140. return ((ExposedCarlaPipeClient*)handle)->writeAndFixMessage(msg);
  141. }
  142. bool carla_pipe_client_flush(CarlaPipeClientHandle handle)
  143. {
  144. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, false);
  145. return ((ExposedCarlaPipeClient*)handle)->flushMessages();
  146. }
  147. bool carla_pipe_client_flush_and_unlock(CarlaPipeClientHandle handle)
  148. {
  149. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, false);
  150. ExposedCarlaPipeClient* const pipe = (ExposedCarlaPipeClient*)handle;
  151. const bool ret = pipe->flushMessages();
  152. pipe->unlockPipe();
  153. return ret;
  154. }
  155. void carla_pipe_client_destroy(CarlaPipeClientHandle handle)
  156. {
  157. CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
  158. carla_debug("carla_pipe_client_destroy(%p)", handle);
  159. ExposedCarlaPipeClient* const pipe = (ExposedCarlaPipeClient*)handle;
  160. pipe->closePipeClient();
  161. delete pipe;
  162. }
  163. // --------------------------------------------------------------------------------------------------------------------
  164. #ifndef CARLA_PLUGIN_BUILD
  165. # include "CarlaPipeUtils.cpp"
  166. #endif
  167. // --------------------------------------------------------------------------------------------------------------------