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.

207 lines
5.8KB

  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 (isRunning())
  44. {
  45. writeMsg("focus\n", 6);
  46. flush();
  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::start();
  53. }
  54. else
  55. {
  56. CarlaExternalUI::stop(5000);
  57. }
  58. }
  59. void uiIdle() override
  60. {
  61. CarlaExternalUI::idle();
  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::stop(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. const CarlaMutexLocker cml(getLock());
  81. writeMsg("control\n", 8);
  82. std::sprintf(tmpBuf, "%i\n", index);
  83. writeMsg(tmpBuf);
  84. std::sprintf(tmpBuf, "%f\n", value);
  85. writeMsg(tmpBuf);
  86. flush();
  87. }
  88. void uiSetMidiProgram(const uint8_t channel, const uint32_t bank, const uint32_t program) override
  89. {
  90. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  91. char tmpBuf[0xff+1];
  92. const CarlaMutexLocker cml(getLock());
  93. writeMsg("program\n", 8);
  94. std::sprintf(tmpBuf, "%i\n", channel);
  95. writeMsg(tmpBuf);
  96. std::sprintf(tmpBuf, "%i\n", bank);
  97. writeMsg(tmpBuf);
  98. std::sprintf(tmpBuf, "%i\n", program);
  99. writeMsg(tmpBuf);
  100. flush();
  101. }
  102. void uiSetCustomData(const char* const key, const char* const value) override
  103. {
  104. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  105. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  106. const CarlaMutexLocker cml(getLock());
  107. writeMsg("configure\n", 10);
  108. writeAndFixMsg(key);
  109. writeAndFixMsg(value);
  110. flush();
  111. }
  112. // -------------------------------------------------------------------
  113. // Pipe Server calls
  114. bool msgReceived(const char* const msg) noexcept override
  115. {
  116. if (CarlaExternalUI::msgReceived(msg))
  117. return true;
  118. if (std::strcmp(msg, "control") == 0)
  119. {
  120. uint32_t param;
  121. float value;
  122. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(param), true);
  123. CARLA_SAFE_ASSERT_RETURN(readNextLineAsFloat(value), true);
  124. try {
  125. uiParameterChanged(param, value);
  126. } CARLA_SAFE_EXCEPTION("uiParameterChanged");
  127. return true;
  128. }
  129. if (std::strcmp(msg, "program") == 0)
  130. {
  131. uint32_t channel, bank, program;
  132. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(channel), true);
  133. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(bank), true);
  134. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(program), true);
  135. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS, true);
  136. try {
  137. uiMidiProgramChanged(channel, bank, program);
  138. } CARLA_SAFE_EXCEPTION("uiMidiProgramChanged");
  139. return true;
  140. }
  141. if (std::strcmp(msg, "configure") == 0)
  142. {
  143. const char* key;
  144. const char* value;
  145. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(key), true);
  146. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(value), true);
  147. try {
  148. uiCustomDataChanged(key, value);
  149. } CARLA_SAFE_EXCEPTION("uiCustomDataChanged");
  150. delete[] key;
  151. delete[] value;
  152. return true;
  153. }
  154. carla_stderr("NativePluginAndUiClass::msgReceived : %s", msg);
  155. return false;
  156. }
  157. private:
  158. CarlaString fExtUiPath;
  159. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NativePluginAndUiClass)
  160. };
  161. /**@}*/
  162. // -----------------------------------------------------------------------
  163. #endif // CARLA_NATIVE_EXTERNAL_UI_HPP_INCLUDED