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.

203 lines
5.6KB

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