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