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.

217 lines
6.1KB

  1. /*
  2. * Carla Plugin Host
  3. * Copyright (C) 2011-2019 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 = CarlaBackend;
  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. CarlaPipeClientHandle carla_pipe_client_new(const char* argv[], CarlaPipeCallbackFunc callbackFunc, void* callbackPtr)
  81. {
  82. carla_debug("carla_pipe_client_new(%p, %p, %p)", argv, callbackFunc, callbackPtr);
  83. ExposedCarlaPipeClient* const pipe = new ExposedCarlaPipeClient(callbackFunc, callbackPtr);
  84. if (! pipe->initPipeClient(argv))
  85. {
  86. delete pipe;
  87. return nullptr;
  88. }
  89. return pipe;
  90. }
  91. void carla_pipe_client_idle(CarlaPipeClientHandle handle)
  92. {
  93. CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
  94. ((ExposedCarlaPipeClient*)handle)->idlePipe();
  95. }
  96. bool carla_pipe_client_is_running(CarlaPipeClientHandle handle)
  97. {
  98. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, false);
  99. return ((ExposedCarlaPipeClient*)handle)->isPipeRunning();
  100. }
  101. void carla_pipe_client_lock(CarlaPipeClientHandle handle)
  102. {
  103. CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
  104. return ((ExposedCarlaPipeClient*)handle)->lockPipe();
  105. }
  106. void carla_pipe_client_unlock(CarlaPipeClientHandle handle)
  107. {
  108. CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
  109. return ((ExposedCarlaPipeClient*)handle)->unlockPipe();
  110. }
  111. const char* carla_pipe_client_readlineblock(CarlaPipeClientHandle handle, uint timeout)
  112. {
  113. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  114. return ((ExposedCarlaPipeClient*)handle)->readlineblock(timeout);
  115. }
  116. bool carla_pipe_client_readlineblock_bool(CarlaPipeClientHandle handle, uint timeout)
  117. {
  118. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, false);
  119. return ((ExposedCarlaPipeClient*)handle)->readlineblock_bool(timeout);
  120. }
  121. int carla_pipe_client_readlineblock_int(CarlaPipeClientHandle handle, uint timeout)
  122. {
  123. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, 0);
  124. return ((ExposedCarlaPipeClient*)handle)->readlineblock_int(timeout);
  125. }
  126. double carla_pipe_client_readlineblock_float(CarlaPipeClientHandle handle, uint timeout)
  127. {
  128. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, 0.0);
  129. return ((ExposedCarlaPipeClient*)handle)->readlineblock_float(timeout);
  130. }
  131. bool carla_pipe_client_write_msg(CarlaPipeClientHandle handle, const char* msg)
  132. {
  133. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, false);
  134. return ((ExposedCarlaPipeClient*)handle)->writeMessage(msg);
  135. }
  136. bool carla_pipe_client_write_and_fix_msg(CarlaPipeClientHandle handle, const char* msg)
  137. {
  138. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, false);
  139. return ((ExposedCarlaPipeClient*)handle)->writeAndFixMessage(msg);
  140. }
  141. bool carla_pipe_client_flush(CarlaPipeClientHandle handle)
  142. {
  143. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, false);
  144. return ((ExposedCarlaPipeClient*)handle)->flushMessages();
  145. }
  146. bool carla_pipe_client_flush_and_unlock(CarlaPipeClientHandle handle)
  147. {
  148. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, false);
  149. ExposedCarlaPipeClient* const pipe = (ExposedCarlaPipeClient*)handle;
  150. const bool ret = pipe->flushMessages();
  151. pipe->unlockPipe();
  152. return ret;
  153. }
  154. void carla_pipe_client_destroy(CarlaPipeClientHandle handle)
  155. {
  156. CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
  157. carla_debug("carla_pipe_client_destroy(%p)", handle);
  158. ExposedCarlaPipeClient* const pipe = (ExposedCarlaPipeClient*)handle;
  159. pipe->closePipeClient();
  160. delete pipe;
  161. }
  162. // -------------------------------------------------------------------------------------------------------------------
  163. #ifndef CARLA_PLUGIN_EXPORT
  164. # include "CarlaPipeUtils.cpp"
  165. #endif
  166. // -------------------------------------------------------------------------------------------------------------------