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.

226 lines
6.2KB

  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(extUiPath),
  35. leakDetector_NativePluginAndUiClass() {}
  36. //~NativePluginAndUiClass() noexcept override {}
  37. protected:
  38. // -------------------------------------------------------------------
  39. // Plugin UI calls
  40. void uiShow(const bool show) override
  41. {
  42. if (show)
  43. {
  44. if (isPipeRunning())
  45. {
  46. const CarlaMutexLocker cml(getPipeLock());
  47. writeMessage("focus\n", 6);
  48. flushMessages();
  49. return;
  50. }
  51. CarlaString path(getResourceDir() + fExtUiPath);
  52. carla_stdout("Trying to start UI using \"%s\"", path.buffer());
  53. CarlaExternalUI::setData(path, getSampleRate(), getUiName());
  54. CarlaExternalUI::startPipeServer(true);
  55. }
  56. else
  57. {
  58. CarlaExternalUI::stopPipeServer(5000);
  59. }
  60. }
  61. void uiIdle() override
  62. {
  63. CarlaExternalUI::idlePipe();
  64. switch (CarlaExternalUI::getAndResetUiState())
  65. {
  66. case CarlaExternalUI::UiNone:
  67. case CarlaExternalUI::UiShow:
  68. break;
  69. case CarlaExternalUI::UiCrashed:
  70. hostUiUnavailable();
  71. break;
  72. case CarlaExternalUI::UiHide:
  73. uiClosed();
  74. CarlaExternalUI::stopPipeServer(2000);
  75. break;
  76. }
  77. }
  78. void uiSetParameterValue(const uint32_t index, const float value) noexcept override
  79. {
  80. CARLA_SAFE_ASSERT_RETURN(index < getParameterCount(),);
  81. char tmpBuf[0xff+1];
  82. tmpBuf[0xff] = '\0';
  83. const CarlaMutexLocker cml(getPipeLock());
  84. const ScopedLocale csl;
  85. writeMessage("control\n", 8);
  86. {
  87. std::snprintf(tmpBuf, 0xff, "%i\n", index);
  88. writeMessage(tmpBuf);
  89. std::snprintf(tmpBuf, 0xff, "%f\n", value);
  90. writeMessage(tmpBuf);
  91. }
  92. flushMessages();
  93. }
  94. void uiSetMidiProgram(const uint8_t channel, const uint32_t bank, const uint32_t program) noexcept override
  95. {
  96. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  97. char tmpBuf[0xff+1];
  98. tmpBuf[0xff] = '\0';
  99. const CarlaMutexLocker cml(getPipeLock());
  100. writeMessage("program\n", 8);
  101. {
  102. std::snprintf(tmpBuf, 0xff, "%i\n", channel);
  103. writeMessage(tmpBuf);
  104. std::snprintf(tmpBuf, 0xff, "%i\n", bank);
  105. writeMessage(tmpBuf);
  106. std::snprintf(tmpBuf, 0xff, "%i\n", program);
  107. writeMessage(tmpBuf);
  108. }
  109. flushMessages();
  110. }
  111. void uiSetCustomData(const char* const key, const char* const value) noexcept override
  112. {
  113. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  114. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  115. const CarlaMutexLocker cml(getPipeLock());
  116. writeMessage("configure\n", 10);
  117. writeAndFixMessage(key);
  118. writeAndFixMessage(value);
  119. flushMessages();
  120. }
  121. // -------------------------------------------------------------------
  122. // Pipe Server calls
  123. bool msgReceived(const char* const msg) noexcept override
  124. {
  125. if (CarlaExternalUI::msgReceived(msg))
  126. return true;
  127. if (std::strcmp(msg, "control") == 0)
  128. {
  129. uint32_t param;
  130. float value;
  131. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(param), true);
  132. CARLA_SAFE_ASSERT_RETURN(readNextLineAsFloat(value), true);
  133. try {
  134. uiParameterChanged(param, value);
  135. } CARLA_SAFE_EXCEPTION("uiParameterChanged");
  136. return true;
  137. }
  138. if (std::strcmp(msg, "program") == 0)
  139. {
  140. uint32_t channel, bank, program;
  141. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(channel), true);
  142. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(bank), true);
  143. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(program), true);
  144. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS, true);
  145. try {
  146. uiMidiProgramChanged(static_cast<uint8_t>(channel), bank, program);
  147. } CARLA_SAFE_EXCEPTION("uiMidiProgramChanged");
  148. return true;
  149. }
  150. if (std::strcmp(msg, "configure") == 0)
  151. {
  152. const char* key;
  153. const char* value;
  154. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(key), true);
  155. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(value), true);
  156. try {
  157. uiCustomDataChanged(key, value);
  158. } CARLA_SAFE_EXCEPTION("uiCustomDataChanged");
  159. delete[] key;
  160. delete[] value;
  161. return true;
  162. }
  163. carla_stderr("NativePluginAndUiClass::msgReceived : %s", msg);
  164. return false;
  165. }
  166. private:
  167. CarlaString fExtUiPath;
  168. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NativePluginAndUiClass)
  169. };
  170. /**@}*/
  171. // -----------------------------------------------------------------------
  172. #endif // CARLA_NATIVE_EXTERNAL_UI_HPP_INCLUDED