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.

DistrhoUI.hpp 14KB

6 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
9 years ago
9 years ago
9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2022 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_HPP_INCLUDED
  17. #define DISTRHO_UI_HPP_INCLUDED
  18. #include "extra/LeakDetector.hpp"
  19. #include "src/DistrhoPluginChecks.h"
  20. #ifdef DGL_CAIRO
  21. # include "Cairo.hpp"
  22. #endif
  23. #ifdef DGL_OPENGL
  24. # include "OpenGL.hpp"
  25. #endif
  26. #ifdef DGL_VULKAN
  27. # include "Vulkan.hpp"
  28. #endif
  29. #if DISTRHO_PLUGIN_HAS_EXTERNAL_UI
  30. # include "../dgl/Base.hpp"
  31. # include "extra/ExternalWindow.hpp"
  32. typedef DISTRHO_NAMESPACE::ExternalWindow UIWidget;
  33. #elif DISTRHO_UI_USE_CUSTOM
  34. # include DISTRHO_UI_CUSTOM_INCLUDE_PATH
  35. typedef DISTRHO_UI_CUSTOM_WIDGET_TYPE UIWidget;
  36. #elif DISTRHO_UI_USE_CAIRO
  37. # include "../dgl/Cairo.hpp"
  38. typedef DGL_NAMESPACE::CairoTopLevelWidget UIWidget;
  39. #elif DISTRHO_UI_USE_NANOVG
  40. # include "../dgl/NanoVG.hpp"
  41. typedef DGL_NAMESPACE::NanoTopLevelWidget UIWidget;
  42. #else
  43. # include "../dgl/TopLevelWidget.hpp"
  44. typedef DGL_NAMESPACE::TopLevelWidget UIWidget;
  45. #endif
  46. #if DISTRHO_UI_FILE_BROWSER
  47. # include "extra/FileBrowserDialog.hpp"
  48. #endif
  49. START_NAMESPACE_DISTRHO
  50. class PluginWindow;
  51. /* ------------------------------------------------------------------------------------------------------------
  52. * DPF UI */
  53. /**
  54. @addtogroup MainClasses
  55. @{
  56. */
  57. /**
  58. DPF UI class from where UI instances are created.
  59. @note You must call setSize during construction,
  60. @TODO Detailed information about this class.
  61. */
  62. class UI : public UIWidget
  63. {
  64. public:
  65. /**
  66. UI class constructor.
  67. The UI should be initialized to a default state that matches the plugin side.
  68. When @a automaticallyScale is set to true, DPF will automatically scale up the UI
  69. to fit the host/desktop scale factor.@n
  70. It assumes aspect ratio is meant to be kept.
  71. Manually call setGeometryConstraints instead if keeping UI aspect ratio is not required.
  72. */
  73. UI(uint width = 0, uint height = 0, bool automaticallyScaleAndSetAsMinimumSize = false);
  74. /**
  75. Destructor.
  76. */
  77. ~UI() override;
  78. /* --------------------------------------------------------------------------------------------------------
  79. * Host state */
  80. /**
  81. Check if this UI window is resizable (by the user or window manager).
  82. There are situations where an UI supports resizing but the plugin host does not, so this could return false.
  83. You might want to add a resize handle for such cases, so the user is still allowed to resize the window.
  84. (programatically resizing a window is always possible, but the same is not true for the window manager)
  85. */
  86. bool isResizable() const noexcept;
  87. /**
  88. Get the color used for UI background (i.e. window color) in RGBA format.
  89. Returns 0 by default, in case of error or lack of host support.
  90. The following example code can be use to extract individual colors:
  91. ```
  92. const int red = (bgColor >> 24) & 0xff;
  93. const int green = (bgColor >> 16) & 0xff;
  94. const int blue = (bgColor >> 8) & 0xff;
  95. ```
  96. */
  97. uint getBackgroundColor() const noexcept;
  98. /**
  99. Get the color used for UI foreground (i.e. text color) in RGBA format.
  100. Returns 0xffffffff by default, in case of error or lack of host support.
  101. The following example code can be use to extract individual colors:
  102. ```
  103. const int red = (fgColor >> 24) & 0xff;
  104. const int green = (fgColor >> 16) & 0xff;
  105. const int blue = (fgColor >> 8) & 0xff;
  106. ```
  107. */
  108. uint getForegroundColor() const noexcept;
  109. /**
  110. Get the current sample rate used in plugin processing.
  111. @see sampleRateChanged(double)
  112. */
  113. double getSampleRate() const noexcept;
  114. /**
  115. Get the bundle path where the UI resides.@n
  116. Can return null if the UI is not available in a bundle (if it is a single binary).
  117. @see getBinaryFilename
  118. */
  119. const char* getBundlePath() const noexcept;
  120. /**
  121. editParameter.
  122. Touch/pressed-down event.
  123. Lets the host know the user is tweaking a parameter.
  124. Required in some hosts to record automation.
  125. */
  126. void editParameter(uint32_t index, bool started);
  127. /**
  128. setParameterValue.
  129. Change a parameter value in the Plugin.
  130. */
  131. void setParameterValue(uint32_t index, float value);
  132. #if DISTRHO_PLUGIN_WANT_STATE
  133. /**
  134. setState.
  135. @TODO Document this.
  136. */
  137. void setState(const char* key, const char* value);
  138. /**
  139. Request a new file from the host, matching the properties of a state key.@n
  140. This will use the native host file browser if available, otherwise a DPF built-in file browser is used.@n
  141. Response will be sent asynchronously to stateChanged, with the matching key and the new file as the value.@n
  142. It is not possible to know if the action was cancelled by the user.
  143. @return Success if a file-browser was opened, otherwise false.
  144. @note You cannot request more than one file at a time.
  145. */
  146. bool requestStateFile(const char* key);
  147. #endif
  148. #if DISTRHO_PLUGIN_WANT_MIDI_INPUT
  149. /**
  150. Send a single MIDI note from the UI to the plugin DSP side.@n
  151. A note with zero velocity will be sent as note-off (MIDI 0x80), otherwise note-on (MIDI 0x90).
  152. */
  153. void sendNote(uint8_t channel, uint8_t note, uint8_t velocity);
  154. #endif
  155. #if DISTRHO_UI_FILE_BROWSER
  156. /**
  157. Open a file browser dialog with this window as transient parent.@n
  158. A few options can be specified to setup the dialog.
  159. If a path is selected, onFileSelected() will be called with the user chosen path.
  160. If the user cancels or does not pick a file, onFileSelected() will be called with nullptr as filename.
  161. This function does not block the event loop.
  162. @note This is exactly the same API as provided by the Window class,
  163. but redeclared here so that non-embed/DGL based UIs can still use file browser related functions.
  164. */
  165. bool openFileBrowser(const DISTRHO_NAMESPACE::FileBrowserOptions& options = FileBrowserOptions());
  166. #endif
  167. #if DISTRHO_PLUGIN_WANT_DIRECT_ACCESS
  168. /* --------------------------------------------------------------------------------------------------------
  169. * Direct DSP access - DO NOT USE THIS UNLESS STRICTLY NECESSARY!! */
  170. /**
  171. getPluginInstancePointer.
  172. @TODO Document this.
  173. */
  174. void* getPluginInstancePointer() const noexcept;
  175. #endif
  176. #if DISTRHO_PLUGIN_HAS_EXTERNAL_UI
  177. /* --------------------------------------------------------------------------------------------------------
  178. * External UI helpers */
  179. /**
  180. Get the bundle path that will be used for the next UI.
  181. @note: This function is only valid during createUI(),
  182. it will return null when called from anywhere else.
  183. */
  184. static const char* getNextBundlePath() noexcept;
  185. /**
  186. Get the scale factor that will be used for the next UI.
  187. @note: This function is only valid during createUI(),
  188. it will return 1.0 when called from anywhere else.
  189. */
  190. static double getNextScaleFactor() noexcept;
  191. # if DISTRHO_PLUGIN_HAS_EMBED_UI
  192. /**
  193. Get the Window Id that will be used for the next created window.
  194. @note: This function is only valid during createUI(),
  195. it will return 0 when called from anywhere else.
  196. */
  197. static uintptr_t getNextWindowId() noexcept;
  198. # endif
  199. #endif
  200. protected:
  201. /* --------------------------------------------------------------------------------------------------------
  202. * DSP/Plugin Callbacks */
  203. /**
  204. A parameter has changed on the plugin side.@n
  205. This is called by the host to inform the UI about parameter changes.
  206. */
  207. virtual void parameterChanged(uint32_t index, float value) = 0;
  208. #if DISTRHO_PLUGIN_WANT_PROGRAMS
  209. /**
  210. A program has been loaded on the plugin side.@n
  211. This is called by the host to inform the UI about program changes.
  212. */
  213. virtual void programLoaded(uint32_t index) = 0;
  214. #endif
  215. #if DISTRHO_PLUGIN_WANT_STATE
  216. /**
  217. A state has changed on the plugin side.@n
  218. This is called by the host to inform the UI about state changes.
  219. */
  220. virtual void stateChanged(const char* key, const char* value) = 0;
  221. #endif
  222. /* --------------------------------------------------------------------------------------------------------
  223. * DSP/Plugin Callbacks (optional) */
  224. /**
  225. Optional callback to inform the UI about a sample rate change on the plugin side.
  226. @see getSampleRate()
  227. */
  228. virtual void sampleRateChanged(double newSampleRate);
  229. /* --------------------------------------------------------------------------------------------------------
  230. * UI Callbacks (optional) */
  231. /**
  232. UI idle function, called to give idle time to the plugin UI directly from the host.
  233. This is called right after OS event handling and Window idle events (within the same cycle).
  234. There are no guarantees in terms of timing.
  235. @see addIdleCallback(IdleCallback*, uint).
  236. */
  237. virtual void uiIdle() {}
  238. /**
  239. Window scale factor function, called when the scale factor changes.
  240. This function is for plugin UIs to be able to override Window::onScaleFactorChanged(double).
  241. The default implementation does nothing.
  242. WARNING function needs a proper name
  243. */
  244. virtual void uiScaleFactorChanged(double scaleFactor);
  245. #if !DISTRHO_PLUGIN_HAS_EXTERNAL_UI
  246. /**
  247. Get the types available for the data in a clipboard.
  248. Must only be called within the context of uiClipboardDataOffer.
  249. */
  250. std::vector<DGL_NAMESPACE::ClipboardDataOffer> getClipboardDataOfferTypes();
  251. /**
  252. Window clipboard data offer function, called when clipboard has data present, possibly with several datatypes.
  253. While handling this event, the data types can be investigated with getClipboardDataOfferTypes() to decide whether to accept the offer.
  254. Reimplement and return a non-zero id to accept the clipboard data offer for a particular type.
  255. UIs must ignore any type they do not recognize.
  256. The default implementation accepts the "text/plain" mimetype.
  257. */
  258. virtual uint32_t uiClipboardDataOffer();
  259. /**
  260. Windows focus function, called when the window gains or loses the keyboard focus.
  261. This function is for plugin UIs to be able to override Window::onFocus(bool, CrossingMode).
  262. The default implementation does nothing.
  263. */
  264. virtual void uiFocus(bool focus, DGL_NAMESPACE::CrossingMode mode);
  265. /**
  266. Window reshape function, called when the window is resized.
  267. This function is for plugin UIs to be able to override Window::onReshape(uint, uint).
  268. The plugin UI size will be set right after this function.
  269. The default implementation sets up the drawing context where necessary.
  270. You should almost never need to override this function.
  271. The most common exception is custom OpenGL setup, but only really needed for custom OpenGL drawing code.
  272. */
  273. virtual void uiReshape(uint width, uint height);
  274. #endif // !DISTRHO_PLUGIN_HAS_EXTERNAL_UI
  275. #if DISTRHO_UI_FILE_BROWSER
  276. /**
  277. Window file selected function, called when a path is selected by the user, as triggered by openFileBrowser().
  278. This function is for plugin UIs to be able to override Window::onFileSelected(const char*).
  279. This action happens after the user confirms the action, so the file browser dialog will be closed at this point.
  280. The default implementation does nothing.
  281. If you need to use files as plugin state, please setup and use states with kStateIsFilenamePath instead.
  282. */
  283. virtual void uiFileBrowserSelected(const char* filename);
  284. #endif
  285. /* --------------------------------------------------------------------------------------------------------
  286. * UI Resize Handling, internal */
  287. #if DISTRHO_PLUGIN_HAS_EXTERNAL_UI
  288. /**
  289. External Window resize function, called when the window is resized.
  290. This is overriden here so the host knows when the UI is resized by you.
  291. @see ExternalWindow::sizeChanged(uint,uint)
  292. */
  293. void sizeChanged(uint width, uint height) override;
  294. #else
  295. /**
  296. Widget resize function, called when the widget is resized.
  297. This is overriden here so the host knows when the UI is resized by you.
  298. @see Widget::onResize(const ResizeEvent&)
  299. */
  300. void onResize(const ResizeEvent& ev) override;
  301. #endif
  302. // -------------------------------------------------------------------------------------------------------
  303. private:
  304. struct PrivateData;
  305. PrivateData* const uiData;
  306. friend class PluginWindow;
  307. friend class UIExporter;
  308. #if !DISTRHO_PLUGIN_HAS_EXTERNAL_UI
  309. /** @internal */
  310. void requestSizeChange(uint width, uint height) override;
  311. #endif
  312. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(UI)
  313. };
  314. /** @} */
  315. /* ------------------------------------------------------------------------------------------------------------
  316. * Create UI, entry point */
  317. /**
  318. @addtogroup EntryPoints
  319. @{
  320. */
  321. /**
  322. createUI.
  323. @TODO Document this.
  324. */
  325. extern UI* createUI();
  326. /** @} */
  327. // -----------------------------------------------------------------------------------------------------------
  328. END_NAMESPACE_DISTRHO
  329. #endif // DISTRHO_UI_HPP_INCLUDED