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.

201 lines
5.4KB

  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. fExtUiPath(extUiPath)
  34. {
  35. }
  36. ~NativePluginAndUiClass() override
  37. {
  38. }
  39. protected:
  40. // -------------------------------------------------------------------
  41. // Plugin UI calls
  42. void uiShow(const bool show) override
  43. {
  44. if (show)
  45. {
  46. if (isOk())
  47. return;
  48. CarlaString path(getResourceDir() + fExtUiPath);
  49. carla_stdout("Trying to start UI using \"%s\"", path.buffer());
  50. CarlaExternalUI::setData(path, getSampleRate(), getUiName());
  51. CarlaExternalUI::start();
  52. }
  53. else
  54. {
  55. CarlaExternalUI::stop();
  56. }
  57. }
  58. void uiIdle() override
  59. {
  60. CarlaExternalUI::idle();
  61. if (! CarlaExternalUI::isOk())
  62. return;
  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. stop();
  74. break;
  75. }
  76. }
  77. void uiSetParameterValue(const uint32_t index, const float value) override
  78. {
  79. CARLA_SAFE_ASSERT_RETURN(index < getParameterCount(),);
  80. char tmpBuf[0xff+1];
  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. }
  87. void uiSetMidiProgram(const uint8_t channel, const uint32_t bank, const uint32_t program) override
  88. {
  89. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  90. char tmpBuf[0xff+1];
  91. writeMsg("program\n", 8);
  92. std::sprintf(tmpBuf, "%i\n", channel);
  93. writeMsg(tmpBuf);
  94. std::sprintf(tmpBuf, "%i\n", bank);
  95. writeMsg(tmpBuf);
  96. std::sprintf(tmpBuf, "%i\n", program);
  97. writeMsg(tmpBuf);
  98. }
  99. void uiSetCustomData(const char* const key, const char* const value) override
  100. {
  101. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  102. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  103. writeMsg("configure\n", 10);
  104. writeAndFixMsg(key);
  105. writeAndFixMsg(value);
  106. }
  107. // -------------------------------------------------------------------
  108. // Pipe Server calls
  109. bool msgReceived(const char* const msg) noexcept override
  110. {
  111. if (CarlaExternalUI::msgReceived(msg))
  112. return true;
  113. if (std::strcmp(msg, "control") == 0)
  114. {
  115. uint32_t param;
  116. float value;
  117. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(param), true);
  118. CARLA_SAFE_ASSERT_RETURN(readNextLineAsFloat(value), true);
  119. try {
  120. uiParameterChanged(param, value);
  121. } catch(...) {}
  122. return true;
  123. }
  124. if (std::strcmp(msg, "program") == 0)
  125. {
  126. uint32_t channel, bank, program;
  127. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(channel), true);
  128. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(bank), true);
  129. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(program), true);
  130. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS, true);
  131. try {
  132. uiMidiProgramChanged(channel, bank, program);
  133. } catch(...) {}
  134. return true;
  135. }
  136. if (std::strcmp(msg, "configure") == 0)
  137. {
  138. const char* key;
  139. const char* value;
  140. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(key), true);
  141. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(value), true);
  142. try {
  143. uiCustomDataChanged(key, value);
  144. } catch(...) {}
  145. delete[] key;
  146. delete[] value;
  147. return true;
  148. }
  149. carla_stderr("msgReceived : %s", msg);
  150. return false;
  151. }
  152. private:
  153. CarlaString fExtUiPath;
  154. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NativePluginAndUiClass)
  155. };
  156. /**@}*/
  157. // -----------------------------------------------------------------------
  158. #endif // CARLA_NATIVE_EXTERNAL_UI_HPP_INCLUDED