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.

251 lines
6.8KB

  1. /*
  2. * Carla Native Plugin API (C++)
  3. * Copyright (C) 2012-2014 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. #ifndef CARLA_NATIVE_EXTERNAL_UI_HPP_INCLUDED
  18. #define CARLA_NATIVE_EXTERNAL_UI_HPP_INCLUDED
  19. #include "CarlaNative.hpp"
  20. #include "CarlaExternalUI.hpp"
  21. /*!
  22. * @defgroup CarlaNativeAPI Carla Native API
  23. * @{
  24. */
  25. // -----------------------------------------------------------------------
  26. // Native Plugin and External UI class
  27. class NativePluginAndUiClass : public NativePluginClass,
  28. public CarlaExternalUI
  29. {
  30. public:
  31. NativePluginAndUiClass(const NativeHostDescriptor* const host, const char* const extUiPath)
  32. : NativePluginClass(host),
  33. CarlaExternalUI(),
  34. fExtUiPath(getResourceDir()),
  35. leakDetector_NativePluginAndUiClass()
  36. {
  37. fExtUiPath += CARLA_OS_SEP_STR;
  38. fExtUiPath += extUiPath;
  39. }
  40. const char* getExtUiPath() const noexcept
  41. {
  42. return fExtUiPath;
  43. }
  44. protected:
  45. // -------------------------------------------------------------------
  46. // Plugin UI calls
  47. void uiShow(const bool show) override
  48. {
  49. if (show)
  50. {
  51. if (isPipeRunning())
  52. {
  53. const CarlaMutexLocker cml(getPipeLock());
  54. writeMessage("focus\n", 6);
  55. flushMessages();
  56. return;
  57. }
  58. carla_stdout("Trying to start UI using \"%s\"", fExtUiPath.buffer());
  59. CarlaExternalUI::setData(fExtUiPath, getSampleRate(), getUiName());
  60. if (! CarlaExternalUI::startPipeServer(true))
  61. {
  62. uiClosed();
  63. hostUiUnavailable();
  64. }
  65. }
  66. else
  67. {
  68. CarlaExternalUI::stopPipeServer(2000);
  69. }
  70. }
  71. void uiIdle() override
  72. {
  73. CarlaExternalUI::idlePipe();
  74. switch (CarlaExternalUI::getAndResetUiState())
  75. {
  76. case CarlaExternalUI::UiNone:
  77. case CarlaExternalUI::UiShow:
  78. break;
  79. case CarlaExternalUI::UiCrashed:
  80. uiClosed();
  81. hostUiUnavailable();
  82. break;
  83. case CarlaExternalUI::UiHide:
  84. uiClosed();
  85. CarlaExternalUI::stopPipeServer(1000);
  86. break;
  87. }
  88. }
  89. void uiSetParameterValue(const uint32_t index, const float value) noexcept override
  90. {
  91. CARLA_SAFE_ASSERT_RETURN(index < getParameterCount(),);
  92. // TODO writeControlMessage and others
  93. char tmpBuf[0xff+1];
  94. tmpBuf[0xff] = '\0';
  95. const CarlaMutexLocker cml(getPipeLock());
  96. const ScopedLocale csl;
  97. writeMessage("control\n", 8);
  98. {
  99. std::snprintf(tmpBuf, 0xff, "%i\n", index);
  100. writeMessage(tmpBuf);
  101. std::snprintf(tmpBuf, 0xff, "%f\n", value);
  102. writeMessage(tmpBuf);
  103. }
  104. flushMessages();
  105. }
  106. void uiSetMidiProgram(const uint8_t channel, const uint32_t bank, const uint32_t program) noexcept override
  107. {
  108. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  109. char tmpBuf[0xff+1];
  110. tmpBuf[0xff] = '\0';
  111. const CarlaMutexLocker cml(getPipeLock());
  112. writeMessage("program\n", 8);
  113. {
  114. std::snprintf(tmpBuf, 0xff, "%i\n", channel);
  115. writeMessage(tmpBuf);
  116. std::snprintf(tmpBuf, 0xff, "%i\n", bank);
  117. writeMessage(tmpBuf);
  118. std::snprintf(tmpBuf, 0xff, "%i\n", program);
  119. writeMessage(tmpBuf);
  120. }
  121. flushMessages();
  122. }
  123. void uiSetCustomData(const char* const key, const char* const value) noexcept override
  124. {
  125. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  126. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  127. const CarlaMutexLocker cml(getPipeLock());
  128. writeMessage("configure\n", 10);
  129. writeAndFixMessage(key);
  130. writeAndFixMessage(value);
  131. flushMessages();
  132. }
  133. void uiNameChanged(const char* const uiName) override
  134. {
  135. CARLA_SAFE_ASSERT_RETURN(uiName != nullptr && uiName[0] != '\0',);
  136. const CarlaMutexLocker cml(getPipeLock());
  137. writeMessage("uiTitle\n", 10);
  138. writeAndFixMessage(uiName);
  139. flushMessages();
  140. }
  141. // -------------------------------------------------------------------
  142. // Pipe Server calls
  143. bool msgReceived(const char* const msg) noexcept override
  144. {
  145. if (CarlaExternalUI::msgReceived(msg))
  146. return true;
  147. if (std::strcmp(msg, "control") == 0)
  148. {
  149. uint32_t param;
  150. float value;
  151. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(param), true);
  152. CARLA_SAFE_ASSERT_RETURN(readNextLineAsFloat(value), true);
  153. try {
  154. uiParameterChanged(param, value);
  155. } CARLA_SAFE_EXCEPTION("uiParameterChanged");
  156. return true;
  157. }
  158. if (std::strcmp(msg, "program") == 0)
  159. {
  160. uint32_t channel, bank, program;
  161. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(channel), true);
  162. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(bank), true);
  163. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(program), true);
  164. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS, true);
  165. try {
  166. uiMidiProgramChanged(static_cast<uint8_t>(channel), bank, program);
  167. } CARLA_SAFE_EXCEPTION("uiMidiProgramChanged");
  168. return true;
  169. }
  170. if (std::strcmp(msg, "configure") == 0)
  171. {
  172. const char* key;
  173. const char* value;
  174. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(key), true);
  175. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(value), true);
  176. try {
  177. uiCustomDataChanged(key, value);
  178. } CARLA_SAFE_EXCEPTION("uiCustomDataChanged");
  179. delete[] key;
  180. delete[] value;
  181. return true;
  182. }
  183. carla_stderr("NativePluginAndUiClass::msgReceived : %s", msg);
  184. return false;
  185. }
  186. private:
  187. CarlaString fExtUiPath;
  188. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NativePluginAndUiClass)
  189. };
  190. /**@}*/
  191. // -----------------------------------------------------------------------
  192. #endif // CARLA_NATIVE_EXTERNAL_UI_HPP_INCLUDED