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.

CarlaNativeExtUI.hpp 6.2KB

6 years ago
6 years ago
10 years ago
10 years ago
10 years ago
6 years ago
6 years ago
6 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * Carla Native Plugin API (C++)
  3. * Copyright (C) 2012-2020 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. #include "CarlaMIDI.h"
  22. /*!
  23. * @defgroup CarlaNativeAPI Carla Native API
  24. * @{
  25. */
  26. // -----------------------------------------------------------------------
  27. // Native Plugin and External UI class
  28. class NativePluginAndUiClass : public NativePluginClass,
  29. public CarlaExternalUI
  30. {
  31. public:
  32. NativePluginAndUiClass(const NativeHostDescriptor* const host, const char* const pathToExternalUI)
  33. : NativePluginClass(host),
  34. CarlaExternalUI(),
  35. fExtUiPath(getResourceDir())
  36. {
  37. fExtUiPath += CARLA_OS_SEP_STR;
  38. fExtUiPath += pathToExternalUI;
  39. #ifdef CARLA_OS_WIN
  40. fExtUiPath += ".exe";
  41. #endif
  42. }
  43. const char* getExtUiPath() const noexcept
  44. {
  45. return fExtUiPath;
  46. }
  47. protected:
  48. // -------------------------------------------------------------------
  49. // Plugin UI calls
  50. void uiShow(const bool show) override
  51. {
  52. if (show)
  53. {
  54. if (isPipeRunning())
  55. {
  56. writeFocusMessage();
  57. return;
  58. }
  59. carla_stdout("Trying to start UI using \"%s\"", fExtUiPath.buffer());
  60. CarlaExternalUI::setData(fExtUiPath, getSampleRate(), getUiName());
  61. if (! CarlaExternalUI::startPipeServer(true))
  62. {
  63. uiClosed();
  64. hostUiUnavailable();
  65. }
  66. }
  67. else
  68. {
  69. CarlaExternalUI::stopPipeServer(2000);
  70. }
  71. }
  72. void uiIdle() override
  73. {
  74. CarlaExternalUI::idlePipe();
  75. switch (CarlaExternalUI::getAndResetUiState())
  76. {
  77. case CarlaExternalUI::UiNone:
  78. case CarlaExternalUI::UiShow:
  79. break;
  80. case CarlaExternalUI::UiCrashed:
  81. uiClosed();
  82. hostUiUnavailable();
  83. break;
  84. case CarlaExternalUI::UiHide:
  85. uiClosed();
  86. CarlaExternalUI::stopPipeServer(1000);
  87. break;
  88. }
  89. }
  90. void uiSetParameterValue(const uint32_t index, const float value) noexcept override
  91. {
  92. CARLA_SAFE_ASSERT_RETURN(index < getParameterCount(),);
  93. writeControlMessage(index, value);
  94. }
  95. void uiSetMidiProgram(const uint8_t channel, const uint32_t bank, const uint32_t program) noexcept override
  96. {
  97. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  98. writeProgramMessage(channel, bank, program);
  99. }
  100. void uiSetCustomData(const char* const key, const char* const value) noexcept override
  101. {
  102. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  103. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  104. writeConfigureMessage(key, value);
  105. }
  106. void uiNameChanged(const char* const uiName) override
  107. {
  108. CARLA_SAFE_ASSERT_RETURN(uiName != nullptr && uiName[0] != '\0',);
  109. const CarlaMutexLocker cml(getPipeLock());
  110. if (! writeMessage("uiTitle\n", 8))
  111. return;
  112. if (! writeAndFixMessage(uiName))
  113. return;
  114. flushMessages();
  115. }
  116. bool uiMIDIEvent(const uint8_t size, const uint8_t data[]) override
  117. {
  118. if (size != 3)
  119. return false;
  120. const uint8_t status = MIDI_GET_STATUS_FROM_DATA(data);
  121. if (! (MIDI_IS_STATUS_NOTE_ON(status) || MIDI_IS_STATUS_NOTE_OFF(status)))
  122. return false;
  123. writeMidiNoteMessage(MIDI_IS_STATUS_NOTE_ON(status),
  124. MIDI_GET_CHANNEL_FROM_DATA(data),
  125. data[1], data[2]);
  126. return true;
  127. }
  128. // -------------------------------------------------------------------
  129. // Pipe Server calls
  130. bool msgReceived(const char* const msg) noexcept override
  131. {
  132. if (CarlaExternalUI::msgReceived(msg))
  133. return true;
  134. if (std::strcmp(msg, "control") == 0)
  135. {
  136. uint32_t param;
  137. float value;
  138. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(param), true);
  139. CARLA_SAFE_ASSERT_RETURN(readNextLineAsFloat(value), true);
  140. try {
  141. uiParameterChanged(param, value);
  142. } CARLA_SAFE_EXCEPTION("uiParameterChanged");
  143. return true;
  144. }
  145. if (std::strcmp(msg, "program") == 0)
  146. {
  147. uint8_t channel;
  148. uint32_t bank, program;
  149. CARLA_SAFE_ASSERT_RETURN(readNextLineAsByte(channel), true);
  150. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(bank), true);
  151. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(program), true);
  152. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS, true);
  153. try {
  154. uiMidiProgramChanged(static_cast<uint8_t>(channel), bank, program);
  155. } CARLA_SAFE_EXCEPTION("uiMidiProgramChanged");
  156. return true;
  157. }
  158. if (std::strcmp(msg, "configure") == 0)
  159. {
  160. const char* key;
  161. const char* value;
  162. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(key, true), true);
  163. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(value, false), true);
  164. try {
  165. uiCustomDataChanged(key, value);
  166. } CARLA_SAFE_EXCEPTION("uiCustomDataChanged");
  167. delete[] key;
  168. return true;
  169. }
  170. return false;
  171. }
  172. private:
  173. CarlaString fExtUiPath;
  174. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NativePluginAndUiClass)
  175. };
  176. /**@}*/
  177. // -----------------------------------------------------------------------
  178. #endif // CARLA_NATIVE_EXTERNAL_UI_HPP_INCLUDED