DISTRHO Plugin Framework
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.

213 lines
5.8KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2021 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. #include "DistrhoUIPrivateData.hpp"
  17. #include "src/WindowPrivateData.hpp"
  18. #if !DISTRHO_PLUGIN_HAS_EXTERNAL_UI
  19. # include "src/TopLevelWidgetPrivateData.hpp"
  20. #endif
  21. #include "NanoVG.hpp"
  22. START_NAMESPACE_DISTRHO
  23. /* ------------------------------------------------------------------------------------------------------------
  24. * Static data, see DistrhoUIInternal.hpp and DistrhoUIPrivateData.hpp */
  25. double d_lastUiSampleRate = 0.0;
  26. void* d_lastUiDspPtr = nullptr;
  27. #if DISTRHO_PLUGIN_HAS_EXTERNAL_UI
  28. const char* g_nextBundlePath = nullptr;
  29. double g_nextScaleFactor = 1.0;
  30. uintptr_t g_nextWindowId = 0;
  31. #else
  32. Window* d_lastUiWindow = nullptr;
  33. #endif
  34. // -----------------------------------------------------------------------------------------------------------
  35. #if DISTRHO_PLUGIN_HAS_EXTERNAL_UI
  36. UI* createUiWrapper(void* const dspPtr, const uintptr_t winId, const double scaleFactor, const char* const bundlePath)
  37. {
  38. d_lastUiDspPtr = dspPtr;
  39. g_nextWindowId = winId;
  40. g_nextScaleFactor = scaleFactor;
  41. g_nextBundlePath = bundlePath;
  42. UI* const ret = createUI();
  43. d_lastUiDspPtr = nullptr;
  44. g_nextWindowId = 0;
  45. g_nextScaleFactor = 1.0;
  46. g_nextBundlePath = nullptr;
  47. return ret;
  48. }
  49. #else
  50. UI* createUiWrapper(void* const dspPtr, Window* const window)
  51. {
  52. d_lastUiDspPtr = dspPtr;
  53. d_lastUiWindow = window;
  54. UI* const ret = createUI();
  55. d_lastUiDspPtr = nullptr;
  56. d_lastUiWindow = nullptr;
  57. return ret;
  58. }
  59. #endif // DISTRHO_PLUGIN_HAS_EXTERNAL_UI
  60. /* ------------------------------------------------------------------------------------------------------------
  61. * UI */
  62. #if DISTRHO_PLUGIN_HAS_EXTERNAL_UI
  63. UI::UI(uint width, uint height)
  64. : UIWidget(width, height),
  65. uiData(new PrivateData()) {}
  66. #else
  67. UI::UI(uint width, uint height)
  68. : UIWidget(*d_lastUiWindow),
  69. uiData(new PrivateData())
  70. {
  71. if (width > 0 && height > 0)
  72. setSize(width, height);
  73. }
  74. #endif
  75. UI::~UI()
  76. {
  77. delete uiData;
  78. }
  79. /* ------------------------------------------------------------------------------------------------------------
  80. * Host state */
  81. uint UI::getBackgroundColor() const noexcept
  82. {
  83. return uiData->bgColor;
  84. }
  85. uint UI::getForegroundColor() const noexcept
  86. {
  87. return uiData->fgColor;
  88. }
  89. double UI::getSampleRate() const noexcept
  90. {
  91. return uiData->sampleRate;
  92. }
  93. void UI::editParameter(uint32_t index, bool started)
  94. {
  95. uiData->editParamCallback(index + uiData->parameterOffset, started);
  96. }
  97. void UI::setParameterValue(uint32_t index, float value)
  98. {
  99. uiData->setParamCallback(index + uiData->parameterOffset, value);
  100. }
  101. #if DISTRHO_PLUGIN_WANT_STATE
  102. void UI::setState(const char* key, const char* value)
  103. {
  104. uiData->setStateCallback(key, value);
  105. }
  106. #endif
  107. #if DISTRHO_PLUGIN_WANT_STATEFILES
  108. bool UI::requestStateFile(const char* key)
  109. {
  110. return uiData->fileRequestCallback(key);
  111. }
  112. #endif
  113. #if DISTRHO_PLUGIN_WANT_MIDI_INPUT
  114. void UI::sendNote(uint8_t channel, uint8_t note, uint8_t velocity)
  115. {
  116. uiData->sendNoteCallback(channel, note, velocity);
  117. }
  118. #endif
  119. #if DISTRHO_PLUGIN_WANT_DIRECT_ACCESS
  120. /* ------------------------------------------------------------------------------------------------------------
  121. * Direct DSP access */
  122. void* UI::getPluginInstancePointer() const noexcept
  123. {
  124. return uiData->dspPtr;
  125. }
  126. #endif
  127. #if DISTRHO_PLUGIN_HAS_EXTERNAL_UI
  128. /* ------------------------------------------------------------------------------------------------------------
  129. * External UI helpers */
  130. const char* UI::getNextBundlePath() noexcept
  131. {
  132. return g_nextBundlePath;
  133. }
  134. double UI::getNextScaleFactor() noexcept
  135. {
  136. return g_nextScaleFactor;
  137. }
  138. # if DISTRHO_PLUGIN_HAS_EMBED_UI
  139. uintptr_t UI::getNextWindowId() noexcept
  140. {
  141. return g_nextWindowId;
  142. }
  143. # endif
  144. #endif // DISTRHO_PLUGIN_HAS_EXTERNAL_UI
  145. /* ------------------------------------------------------------------------------------------------------------
  146. * DSP/Plugin Callbacks (optional) */
  147. void UI::sampleRateChanged(double) {}
  148. #if !DISTRHO_PLUGIN_HAS_EXTERNAL_UI
  149. /* ------------------------------------------------------------------------------------------------------------
  150. * UI Callbacks (optional) */
  151. # ifndef DGL_FILE_BROWSER_DISABLED
  152. void UI::uiFileBrowserSelected(const char*)
  153. {
  154. }
  155. # endif
  156. void UI::uiReshape(uint, uint)
  157. {
  158. pData->fallbackOnResize();
  159. }
  160. /* ------------------------------------------------------------------------------------------------------------
  161. * UI Resize Handling, internal */
  162. void UI::onResize(const ResizeEvent& ev)
  163. {
  164. if (uiData->resizeInProgress)
  165. return;
  166. uiData->setSizeCallback(ev.size.getWidth(), ev.size.getHeight());
  167. }
  168. #endif // !DISTRHO_PLUGIN_HAS_EXTERNAL_UI
  169. // -----------------------------------------------------------------------------------------------------------
  170. END_NAMESPACE_DISTRHO
  171. // -----------------------------------------------------------------------
  172. // Possible template data types
  173. template class NanoWidget<SubWidget>;
  174. template class NanoWidget<TopLevelWidget>;
  175. template class NanoWidget<StandaloneWindow>;