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.

258 lines
6.8KB

  1. /*
  2. * Carla Native Plugin API (C++)
  3. * Copyright (C) 2012-2016 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. {
  36. fExtUiPath += CARLA_OS_SEP_STR;
  37. fExtUiPath += extUiPath;
  38. #ifdef CARLA_OS_WIN
  39. fExtUiPath += ".exe";
  40. #endif
  41. }
  42. const char* getExtUiPath() const noexcept
  43. {
  44. return fExtUiPath;
  45. }
  46. protected:
  47. // -------------------------------------------------------------------
  48. // Plugin UI calls
  49. void uiShow(const bool show) override
  50. {
  51. if (show)
  52. {
  53. if (isPipeRunning())
  54. {
  55. writeFocusMessage();
  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. if (! writeMessage("control\n", 8))
  98. return;
  99. std::snprintf(tmpBuf, 0xff, "%i\n", index);
  100. if (! writeMessage(tmpBuf))
  101. return;
  102. std::snprintf(tmpBuf, 0xff, "%f\n", value);
  103. if (! writeMessage(tmpBuf))
  104. return;
  105. flushMessages();
  106. }
  107. void uiSetMidiProgram(const uint8_t channel, const uint32_t bank, const uint32_t program) noexcept override
  108. {
  109. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  110. char tmpBuf[0xff+1];
  111. tmpBuf[0xff] = '\0';
  112. const CarlaMutexLocker cml(getPipeLock());
  113. if (! writeMessage("program\n", 8))
  114. return;
  115. std::snprintf(tmpBuf, 0xff, "%i\n", channel);
  116. if (! writeMessage(tmpBuf))
  117. return;
  118. std::snprintf(tmpBuf, 0xff, "%i\n", bank);
  119. if (! writeMessage(tmpBuf))
  120. return;
  121. std::snprintf(tmpBuf, 0xff, "%i\n", program);
  122. if (! writeMessage(tmpBuf))
  123. return;
  124. flushMessages();
  125. }
  126. void uiSetCustomData(const char* const key, const char* const value) noexcept override
  127. {
  128. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  129. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  130. const CarlaMutexLocker cml(getPipeLock());
  131. if (! writeMessage("configure\n", 10))
  132. return;
  133. if (! writeAndFixMessage(key))
  134. return;
  135. if (! writeAndFixMessage(value))
  136. return;
  137. flushMessages();
  138. }
  139. void uiNameChanged(const char* const uiName) override
  140. {
  141. CARLA_SAFE_ASSERT_RETURN(uiName != nullptr && uiName[0] != '\0',);
  142. const CarlaMutexLocker cml(getPipeLock());
  143. if (! writeMessage("uiTitle\n", 8))
  144. return;
  145. if (! writeAndFixMessage(uiName))
  146. return;
  147. flushMessages();
  148. }
  149. // -------------------------------------------------------------------
  150. // Pipe Server calls
  151. bool msgReceived(const char* const msg) noexcept override
  152. {
  153. if (CarlaExternalUI::msgReceived(msg))
  154. return true;
  155. if (std::strcmp(msg, "control") == 0)
  156. {
  157. uint32_t param;
  158. float value;
  159. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(param), true);
  160. CARLA_SAFE_ASSERT_RETURN(readNextLineAsFloat(value), true);
  161. try {
  162. uiParameterChanged(param, value);
  163. } CARLA_SAFE_EXCEPTION("uiParameterChanged");
  164. return true;
  165. }
  166. if (std::strcmp(msg, "program") == 0)
  167. {
  168. uint32_t channel, bank, program;
  169. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(channel), true);
  170. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(bank), true);
  171. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(program), true);
  172. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS, true);
  173. try {
  174. uiMidiProgramChanged(static_cast<uint8_t>(channel), bank, program);
  175. } CARLA_SAFE_EXCEPTION("uiMidiProgramChanged");
  176. return true;
  177. }
  178. if (std::strcmp(msg, "configure") == 0)
  179. {
  180. const char* key;
  181. const char* value;
  182. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(key), true);
  183. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(value), true);
  184. try {
  185. uiCustomDataChanged(key, value);
  186. } CARLA_SAFE_EXCEPTION("uiCustomDataChanged");
  187. delete[] key;
  188. delete[] value;
  189. return true;
  190. }
  191. return false;
  192. }
  193. private:
  194. CarlaString fExtUiPath;
  195. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NativePluginAndUiClass)
  196. };
  197. /**@}*/
  198. // -----------------------------------------------------------------------
  199. #endif // CARLA_NATIVE_EXTERNAL_UI_HPP_INCLUDED