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.

228 lines
6.3KB

  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. // TODO writeControlMessage and others
  82. char tmpBuf[0xff+1];
  83. tmpBuf[0xff] = '\0';
  84. const CarlaMutexLocker cml(getPipeLock());
  85. const ScopedLocale csl;
  86. writeMessage("control\n", 8);
  87. {
  88. std::snprintf(tmpBuf, 0xff, "%i\n", index);
  89. writeMessage(tmpBuf);
  90. std::snprintf(tmpBuf, 0xff, "%f\n", value);
  91. writeMessage(tmpBuf);
  92. }
  93. flushMessages();
  94. }
  95. void uiSetMidiProgram(const uint8_t channel, const uint32_t bank, const uint32_t program) noexcept override
  96. {
  97. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  98. char tmpBuf[0xff+1];
  99. tmpBuf[0xff] = '\0';
  100. const CarlaMutexLocker cml(getPipeLock());
  101. writeMessage("program\n", 8);
  102. {
  103. std::snprintf(tmpBuf, 0xff, "%i\n", channel);
  104. writeMessage(tmpBuf);
  105. std::snprintf(tmpBuf, 0xff, "%i\n", bank);
  106. writeMessage(tmpBuf);
  107. std::snprintf(tmpBuf, 0xff, "%i\n", program);
  108. writeMessage(tmpBuf);
  109. }
  110. flushMessages();
  111. }
  112. void uiSetCustomData(const char* const key, const char* const value) noexcept override
  113. {
  114. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  115. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  116. const CarlaMutexLocker cml(getPipeLock());
  117. writeMessage("configure\n", 10);
  118. writeAndFixMessage(key);
  119. writeAndFixMessage(value);
  120. flushMessages();
  121. }
  122. // -------------------------------------------------------------------
  123. // Pipe Server calls
  124. bool msgReceived(const char* const msg) noexcept override
  125. {
  126. if (CarlaExternalUI::msgReceived(msg))
  127. return true;
  128. if (std::strcmp(msg, "control") == 0)
  129. {
  130. uint32_t param;
  131. float value;
  132. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(param), true);
  133. CARLA_SAFE_ASSERT_RETURN(readNextLineAsFloat(value), true);
  134. try {
  135. uiParameterChanged(param, value);
  136. } CARLA_SAFE_EXCEPTION("uiParameterChanged");
  137. return true;
  138. }
  139. if (std::strcmp(msg, "program") == 0)
  140. {
  141. uint32_t channel, bank, program;
  142. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(channel), true);
  143. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(bank), true);
  144. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(program), true);
  145. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS, true);
  146. try {
  147. uiMidiProgramChanged(static_cast<uint8_t>(channel), bank, program);
  148. } CARLA_SAFE_EXCEPTION("uiMidiProgramChanged");
  149. return true;
  150. }
  151. if (std::strcmp(msg, "configure") == 0)
  152. {
  153. const char* key;
  154. const char* value;
  155. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(key), true);
  156. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(value), true);
  157. try {
  158. uiCustomDataChanged(key, value);
  159. } CARLA_SAFE_EXCEPTION("uiCustomDataChanged");
  160. delete[] key;
  161. delete[] value;
  162. return true;
  163. }
  164. carla_stderr("NativePluginAndUiClass::msgReceived : %s", msg);
  165. return false;
  166. }
  167. private:
  168. CarlaString fExtUiPath;
  169. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NativePluginAndUiClass)
  170. };
  171. /**@}*/
  172. // -----------------------------------------------------------------------
  173. #endif // CARLA_NATIVE_EXTERNAL_UI_HPP_INCLUDED