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