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.

223 lines
6.1KB

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