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.

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