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.

PipeClient.cpp 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. #ifdef CARLA_OS_HAIKU
  20. # include "CarlaStringList.hpp"
  21. # define CARLA_PIPE_WITHOUT_CALLBACK
  22. #endif
  23. namespace CB = CarlaBackend;
  24. // -------------------------------------------------------------------------------------------------------------------
  25. class ExposedCarlaPipeClient : public CarlaPipeClient
  26. {
  27. public:
  28. ExposedCarlaPipeClient(const CarlaPipeCallbackFunc callbackFunc, void* const callbackPtr) noexcept
  29. : CarlaPipeClient(),
  30. fCallbackFunc(callbackFunc),
  31. fCallbackPtr(callbackPtr),
  32. #ifdef CARLA_PIPE_WITHOUT_CALLBACK
  33. fMsgsReceived(),
  34. fLastMsgReceived(nullptr),
  35. #endif
  36. fLastReadLine(nullptr)
  37. {
  38. CARLA_SAFE_ASSERT(fCallbackFunc != nullptr);
  39. }
  40. ~ExposedCarlaPipeClient() override
  41. {
  42. if (fLastReadLine != nullptr)
  43. {
  44. delete[] fLastReadLine;
  45. fLastReadLine = nullptr;
  46. }
  47. #ifdef CARLA_PIPE_WITHOUT_CALLBACK
  48. if (fLastMsgReceived != nullptr)
  49. {
  50. delete[] fLastMsgReceived;
  51. fLastMsgReceived = nullptr;
  52. }
  53. #endif
  54. }
  55. const char* idlePipeAndReturnMessage()
  56. {
  57. CarlaPipeClient::idlePipe();
  58. #ifdef CARLA_PIPE_WITHOUT_CALLBACK
  59. if (fMsgsReceived.count() == 0)
  60. return nullptr;
  61. delete[] fLastMsgReceived;
  62. fLastMsgReceived = fMsgsReceived.getAndRemoveFirst();
  63. return fLastMsgReceived;
  64. #else
  65. return nullptr;
  66. #endif
  67. }
  68. const char* readlineblock(const uint timeout) noexcept
  69. {
  70. #ifdef CARLA_PIPE_WITHOUT_CALLBACK
  71. if (fMsgsReceived.count() != 0)
  72. {
  73. delete[] fLastMsgReceived;
  74. fLastMsgReceived = fMsgsReceived.getAndRemoveFirst();
  75. return fLastMsgReceived;
  76. }
  77. #endif
  78. delete[] fLastReadLine;
  79. fLastReadLine = CarlaPipeClient::_readlineblock(timeout);
  80. return fLastReadLine;
  81. }
  82. bool msgReceived(const char* const msg) noexcept override
  83. {
  84. #ifdef CARLA_PIPE_WITHOUT_CALLBACK
  85. fMsgsReceived.append(msg);
  86. #else
  87. if (fCallbackFunc != nullptr)
  88. {
  89. try {
  90. fCallbackFunc(fCallbackPtr, msg);
  91. } CARLA_SAFE_EXCEPTION("msgReceived");
  92. }
  93. #endif
  94. return true;
  95. }
  96. private:
  97. const CarlaPipeCallbackFunc fCallbackFunc;
  98. void* const fCallbackPtr;
  99. #ifdef CARLA_PIPE_WITHOUT_CALLBACK
  100. CarlaStringList fMsgsReceived;
  101. const char* fLastMsgReceived;
  102. #endif
  103. const char* fLastReadLine;
  104. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ExposedCarlaPipeClient)
  105. };
  106. CarlaPipeClientHandle carla_pipe_client_new(const char* argv[], CarlaPipeCallbackFunc callbackFunc, void* callbackPtr)
  107. {
  108. carla_debug("carla_pipe_client_new(%p, %p, %p)", argv, callbackFunc, callbackPtr);
  109. ExposedCarlaPipeClient* const pipe = new ExposedCarlaPipeClient(callbackFunc, callbackPtr);
  110. if (! pipe->initPipeClient(argv))
  111. {
  112. delete pipe;
  113. return nullptr;
  114. }
  115. return pipe;
  116. }
  117. const char* carla_pipe_client_idle(CarlaPipeClientHandle handle)
  118. {
  119. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  120. return ((ExposedCarlaPipeClient*)handle)->idlePipeAndReturnMessage();
  121. }
  122. bool carla_pipe_client_is_running(CarlaPipeClientHandle handle)
  123. {
  124. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, false);
  125. return ((ExposedCarlaPipeClient*)handle)->isPipeRunning();
  126. }
  127. void carla_pipe_client_lock(CarlaPipeClientHandle handle)
  128. {
  129. CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
  130. return ((ExposedCarlaPipeClient*)handle)->lockPipe();
  131. }
  132. void carla_pipe_client_unlock(CarlaPipeClientHandle handle)
  133. {
  134. CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
  135. return ((ExposedCarlaPipeClient*)handle)->unlockPipe();
  136. }
  137. const char* carla_pipe_client_readlineblock(CarlaPipeClientHandle handle, uint timeout)
  138. {
  139. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  140. return ((ExposedCarlaPipeClient*)handle)->readlineblock(timeout);
  141. }
  142. bool carla_pipe_client_write_msg(CarlaPipeClientHandle handle, const char* msg)
  143. {
  144. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, false);
  145. return ((ExposedCarlaPipeClient*)handle)->writeMessage(msg);
  146. }
  147. bool carla_pipe_client_write_and_fix_msg(CarlaPipeClientHandle handle, const char* msg)
  148. {
  149. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, false);
  150. return ((ExposedCarlaPipeClient*)handle)->writeAndFixMessage(msg);
  151. }
  152. bool carla_pipe_client_flush(CarlaPipeClientHandle handle)
  153. {
  154. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, false);
  155. return ((ExposedCarlaPipeClient*)handle)->flushMessages();
  156. }
  157. bool carla_pipe_client_flush_and_unlock(CarlaPipeClientHandle handle)
  158. {
  159. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, false);
  160. ExposedCarlaPipeClient* const pipe = (ExposedCarlaPipeClient*)handle;
  161. const bool ret = pipe->flushMessages();
  162. pipe->unlockPipe();
  163. return ret;
  164. }
  165. void carla_pipe_client_destroy(CarlaPipeClientHandle handle)
  166. {
  167. CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
  168. carla_debug("carla_pipe_client_destroy(%p)", handle);
  169. ExposedCarlaPipeClient* const pipe = (ExposedCarlaPipeClient*)handle;
  170. pipe->closePipeClient();
  171. delete pipe;
  172. }
  173. // -------------------------------------------------------------------------------------------------------------------
  174. #include "CarlaPipeUtils.cpp"
  175. // -------------------------------------------------------------------------------------------------------------------