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.

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