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.

250 lines
7.1KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2014 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. * or without fee is hereby granted, provided that the above copyright notice and this
  7. * permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  10. * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  11. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  12. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  13. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #ifndef DISTRHO_UI_INTERNAL_HPP_INCLUDED
  17. #define DISTRHO_UI_INTERNAL_HPP_INCLUDED
  18. #include "../DistrhoUI.hpp"
  19. #include "../../dgl/App.hpp"
  20. #include "../../dgl/Window.hpp"
  21. START_NAMESPACE_DISTRHO
  22. // -----------------------------------------------------------------------
  23. // Static data, see DistrhoUI.cpp
  24. extern double d_lastUiSampleRate;
  25. // -----------------------------------------------------------------------
  26. // UI callbacks
  27. typedef void (*editParamFunc) (void* ptr, uint32_t rindex, bool started);
  28. typedef void (*setParamFunc) (void* ptr, uint32_t rindex, float value);
  29. typedef void (*setStateFunc) (void* ptr, const char* key, const char* value);
  30. typedef void (*sendNoteFunc) (void* ptr, uint8_t channel, uint8_t note, uint8_t velo);
  31. typedef void (*uiResizeFunc) (void* ptr, unsigned int width, unsigned int height);
  32. // -----------------------------------------------------------------------
  33. // UI private data
  34. struct UI::PrivateData {
  35. // DSP
  36. double sampleRate;
  37. uint32_t parameterOffset;
  38. // Callbacks
  39. editParamFunc editParamCallbackFunc;
  40. setParamFunc setParamCallbackFunc;
  41. setStateFunc setStateCallbackFunc;
  42. sendNoteFunc sendNoteCallbackFunc;
  43. uiResizeFunc uiResizeCallbackFunc;
  44. void* ptr;
  45. PrivateData() noexcept
  46. : sampleRate(d_lastUiSampleRate),
  47. parameterOffset(0),
  48. editParamCallbackFunc(nullptr),
  49. setParamCallbackFunc(nullptr),
  50. setStateCallbackFunc(nullptr),
  51. sendNoteCallbackFunc(nullptr),
  52. uiResizeCallbackFunc(nullptr),
  53. ptr(nullptr)
  54. {
  55. assert(sampleRate != 0.0);
  56. #if defined(DISTRHO_PLUGIN_TARGET_DSSI) || defined(DISTRHO_PLUGIN_TARGET_LV2)
  57. parameterOffset += DISTRHO_PLUGIN_NUM_INPUTS + DISTRHO_PLUGIN_NUM_OUTPUTS;
  58. # if DISTRHO_PLUGIN_WANT_LATENCY
  59. parameterOffset += 1;
  60. # endif
  61. #endif
  62. #ifdef DISTRHO_PLUGIN_TARGET_LV2
  63. # if (DISTRHO_PLUGIN_IS_SYNTH || DISTRHO_PLUGIN_WANT_TIMEPOS || DISTRHO_PLUGIN_WANT_STATE)
  64. parameterOffset += 1;
  65. # if DISTRHO_PLUGIN_WANT_STATE
  66. parameterOffset += 1;
  67. # endif
  68. # endif
  69. #endif
  70. }
  71. void editParamCallback(const uint32_t rindex, const bool started)
  72. {
  73. if (editParamCallbackFunc != nullptr)
  74. editParamCallbackFunc(ptr, rindex, started);
  75. }
  76. void setParamCallback(const uint32_t rindex, const float value)
  77. {
  78. if (setParamCallbackFunc != nullptr)
  79. setParamCallbackFunc(ptr, rindex, value);
  80. }
  81. void setStateCallback(const char* const key, const char* const value)
  82. {
  83. if (setStateCallbackFunc != nullptr)
  84. setStateCallbackFunc(ptr, key, value);
  85. }
  86. void sendNoteCallback(const uint8_t channel, const uint8_t note, const uint8_t velocity)
  87. {
  88. if (sendNoteCallbackFunc != nullptr)
  89. sendNoteCallbackFunc(ptr, channel, note, velocity);
  90. }
  91. void uiResizeCallback(const unsigned int width, const unsigned int height)
  92. {
  93. if (uiResizeCallbackFunc != nullptr)
  94. uiResizeCallbackFunc(ptr, width, height);
  95. }
  96. };
  97. // -----------------------------------------------------------------------
  98. // UI exporter class
  99. class UIExporter
  100. {
  101. public:
  102. UIExporter(void* const ptr, const intptr_t winId,
  103. const editParamFunc editParamCall, const setParamFunc setParamCall, const setStateFunc setStateCall, const sendNoteFunc sendNoteCall, const uiResizeFunc uiResizeCall)
  104. : glApp(),
  105. glWindow(glApp, winId),
  106. fUi(createUI()),
  107. fData((fUi != nullptr) ? fUi->pData : nullptr)
  108. {
  109. assert(fUi != nullptr);
  110. if (fUi == nullptr)
  111. return;
  112. fData->ptr = ptr;
  113. fData->editParamCallbackFunc = editParamCall;
  114. fData->setParamCallbackFunc = setParamCall;
  115. fData->setStateCallbackFunc = setStateCall;
  116. fData->sendNoteCallbackFunc = sendNoteCall;
  117. fData->uiResizeCallbackFunc = uiResizeCall;
  118. glWindow.setSize(fUi->d_getWidth(), fUi->d_getHeight());
  119. glWindow.setResizable(false);
  120. }
  121. ~UIExporter()
  122. {
  123. delete fUi;
  124. }
  125. // -------------------------------------------------------------------
  126. const char* getName() const noexcept
  127. {
  128. return (fUi != nullptr) ? fUi->d_getName() : "";
  129. }
  130. unsigned int getWidth() const noexcept
  131. {
  132. return (fUi != nullptr) ? fUi->d_getWidth() : 0;
  133. }
  134. unsigned int getHeight() const noexcept
  135. {
  136. return (fUi != nullptr) ? fUi->d_getHeight() : 0;
  137. }
  138. // -------------------------------------------------------------------
  139. uint32_t getParameterOffset() const noexcept
  140. {
  141. return (fData != nullptr) ? fData->parameterOffset : 0;
  142. }
  143. // -------------------------------------------------------------------
  144. void parameterChanged(const uint32_t index, const float value)
  145. {
  146. if (fUi != nullptr)
  147. fUi->d_parameterChanged(index, value);
  148. }
  149. #if DISTRHO_PLUGIN_WANT_PROGRAMS
  150. void programChanged(const uint32_t index)
  151. {
  152. if (fUi != nullptr)
  153. fUi->d_programChanged(index);
  154. }
  155. #endif
  156. #if DISTRHO_PLUGIN_WANT_STATE
  157. void stateChanged(const char* const key, const char* const value)
  158. {
  159. if (fUi != nullptr)
  160. fUi->d_stateChanged(key, value);
  161. }
  162. #endif
  163. // -------------------------------------------------------------------
  164. bool idle()
  165. {
  166. if (fUi != nullptr)
  167. fUi->d_uiIdle();
  168. glApp.idle();
  169. return ! glApp.isQuiting();
  170. }
  171. void quit()
  172. {
  173. glWindow.close();
  174. glApp.quit();
  175. }
  176. void setSize(const unsigned int width, const unsigned int height)
  177. {
  178. glWindow.setSize(width, height);
  179. }
  180. void setTitle(const char* const uiTitle)
  181. {
  182. glWindow.setTitle(uiTitle);
  183. }
  184. void setVisible(const bool yesNo)
  185. {
  186. glWindow.setVisible(yesNo);
  187. }
  188. private:
  189. // -------------------------------------------------------------------
  190. // DGL Application and Window for this plugin
  191. DGL::App glApp;
  192. DGL::Window glWindow;
  193. // -------------------------------------------------------------------
  194. // private members accessed by DistrhoPlugin class
  195. UI* const fUi;
  196. UI::PrivateData* const fData;
  197. };
  198. // -----------------------------------------------------------------------
  199. END_NAMESPACE_DISTRHO
  200. #endif // DISTRHO_UI_INTERNAL_HPP_INCLUDED