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.

170 lines
4.8KB

  1. /*
  2. * Carla Plugin Host
  3. * Copyright (C) 2011-2018 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(timeout);
  44. return fLastReadLine;
  45. }
  46. bool msgReceived(const char* const msg) noexcept override
  47. {
  48. if (fCallbackFunc != nullptr)
  49. {
  50. try {
  51. fCallbackFunc(fCallbackPtr, msg);
  52. } CARLA_SAFE_EXCEPTION("msgReceived");
  53. }
  54. return true;
  55. }
  56. private:
  57. const CarlaPipeCallbackFunc fCallbackFunc;
  58. void* const fCallbackPtr;
  59. const char* fLastReadLine;
  60. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ExposedCarlaPipeClient)
  61. };
  62. CarlaPipeClientHandle carla_pipe_client_new(const char* argv[], CarlaPipeCallbackFunc callbackFunc, void* callbackPtr)
  63. {
  64. carla_debug("carla_pipe_client_new(%p, %p, %p)", argv, callbackFunc, callbackPtr);
  65. ExposedCarlaPipeClient* const pipe = new ExposedCarlaPipeClient(callbackFunc, callbackPtr);
  66. if (! pipe->initPipeClient(argv))
  67. {
  68. delete pipe;
  69. return nullptr;
  70. }
  71. return pipe;
  72. }
  73. void carla_pipe_client_idle(CarlaPipeClientHandle handle)
  74. {
  75. CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
  76. ((ExposedCarlaPipeClient*)handle)->idlePipe();
  77. }
  78. bool carla_pipe_client_is_running(CarlaPipeClientHandle handle)
  79. {
  80. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, false);
  81. return ((ExposedCarlaPipeClient*)handle)->isPipeRunning();
  82. }
  83. void carla_pipe_client_lock(CarlaPipeClientHandle handle)
  84. {
  85. CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
  86. return ((ExposedCarlaPipeClient*)handle)->lockPipe();
  87. }
  88. void carla_pipe_client_unlock(CarlaPipeClientHandle handle)
  89. {
  90. CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
  91. return ((ExposedCarlaPipeClient*)handle)->unlockPipe();
  92. }
  93. const char* carla_pipe_client_readlineblock(CarlaPipeClientHandle handle, uint timeout)
  94. {
  95. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  96. return ((ExposedCarlaPipeClient*)handle)->readlineblock(timeout);
  97. }
  98. bool carla_pipe_client_write_msg(CarlaPipeClientHandle handle, const char* msg)
  99. {
  100. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, false);
  101. return ((ExposedCarlaPipeClient*)handle)->writeMessage(msg);
  102. }
  103. bool carla_pipe_client_write_and_fix_msg(CarlaPipeClientHandle handle, const char* msg)
  104. {
  105. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, false);
  106. return ((ExposedCarlaPipeClient*)handle)->writeAndFixMessage(msg);
  107. }
  108. bool carla_pipe_client_flush(CarlaPipeClientHandle handle)
  109. {
  110. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, false);
  111. return ((ExposedCarlaPipeClient*)handle)->flushMessages();
  112. }
  113. bool carla_pipe_client_flush_and_unlock(CarlaPipeClientHandle handle)
  114. {
  115. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, false);
  116. ExposedCarlaPipeClient* const pipe = (ExposedCarlaPipeClient*)handle;
  117. const bool ret = pipe->flushMessages();
  118. pipe->unlockPipe();
  119. return ret;
  120. }
  121. void carla_pipe_client_destroy(CarlaPipeClientHandle handle)
  122. {
  123. CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
  124. carla_debug("carla_pipe_client_destroy(%p)", handle);
  125. ExposedCarlaPipeClient* const pipe = (ExposedCarlaPipeClient*)handle;
  126. pipe->closePipeClient();
  127. delete pipe;
  128. }
  129. // -------------------------------------------------------------------------------------------------------------------
  130. #include "CarlaPipeUtils.cpp"
  131. // -------------------------------------------------------------------------------------------------------------------