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_DISTRHO
52 
53 /* ------------------------------------------------------------------------------------------------------------
54  * DPF UI */
55 
56 /**
57  @addtogroup MainClasses
58  @{
59  */
60 
61 /**
62  DPF UI class from where UI instances are created.
63 
64  @note You must call setSize during construction,
65  @TODO Detailed information about this class.
66  */
67 class UI : public UIWidget
68 {
69 public:
70  /**
71  UI class constructor.
72  The UI should be initialized to a default state that matches the plugin side.
73  */
74  UI(uint width = 0, uint height = 0);
75 
76  /**
77  Destructor.
78  */
79  virtual ~UI();
80 
81  /* --------------------------------------------------------------------------------------------------------
82  * Host state */
83 
84  /**
85  Get the color used for UI background (i.e. window color) in RGBA format.
86  Returns 0 by default, in case of error or lack of host support.
87 
88  The following example code can be use to extract individual colors:
89  ```
90  const int red = (bgColor >> 24) & 0xff;
91  const int green = (bgColor >> 16) & 0xff;
92  const int blue = (bgColor >> 8) & 0xff;
93  ```
94  */
95  uint getBackgroundColor() const noexcept;
96 
97  /**
98  Get the color used for UI foreground (i.e. text color) in RGBA format.
99  Returns 0xffffffff by default, in case of error or lack of host support.
100 
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  /**
111  Get the current sample rate used in plugin processing.
112  @see sampleRateChanged(double)
113  */
114  double getSampleRate() const noexcept;
115 
116  /**
117  editParameter.
118 
119  Touch/pressed-down event.
120  Lets the host know the user is tweaking a parameter.
121  Required in some hosts to record automation.
122  */
123  void editParameter(uint32_t index, bool started);
124 
125  /**
126  setParameterValue.
127 
128  Change a parameter value in the Plugin.
129  */
130  void setParameterValue(uint32_t index, float value);
131 
132 #if DISTRHO_PLUGIN_WANT_STATE
133  /**
134  setState.
135  @TODO Document this.
136  */
137  void setState(const char* key, const char* value);
138 #endif
139 
140 #if DISTRHO_PLUGIN_WANT_STATEFILES
141  /**
142  Request a new file from the host, matching the properties of a state key.@n
143  This will use the native host file browser if available, otherwise a DPF built-in file browser is used.@n
144  Response will be sent asynchronously to stateChanged, with the matching key and the new file as the value.@n
145  It is not possible to know if the action was cancelled by the user.
146 
147  @return Success if a file-browser was opened, otherwise false.
148  @note You cannot request more than one file at a time.
149  */
150  bool requestStateFile(const char* key);
151 #endif
152 
153 #if DISTRHO_PLUGIN_WANT_MIDI_INPUT
154  /**
155  sendNote.
156  @TODO Document this.
157  @note Work in progress. Implemented for DSSI and LV2 formats.
158  */
159  void sendNote(uint8_t channel, uint8_t note, uint8_t velocity);
160 #endif
161 
162 #if DISTRHO_PLUGIN_WANT_DIRECT_ACCESS
163  /* --------------------------------------------------------------------------------------------------------
164  * Direct DSP access - DO NOT USE THIS UNLESS STRICTLY NECESSARY!! */
165 
166  /**
167  getPluginInstancePointer.
168  @TODO Document this.
169  */
170  void* getPluginInstancePointer() const noexcept;
171 #endif
172 
173 #if DISTRHO_PLUGIN_HAS_EXTERNAL_UI
174  /* --------------------------------------------------------------------------------------------------------
175  * External UI helpers */
176 
177  /**
178  Get the bundle path that will be used for the next UI.
179  @note: This function is only valid during createUI(),
180  it will return null when called from anywhere else.
181  */
182  static const char* getNextBundlePath() noexcept;
183 
184  /**
185  Get the scale factor that will be used for the next UI.
186  @note: This function is only valid during createUI(),
187  it will return 1.0 when called from anywhere else.
188  */
189  static double getNextScaleFactor() noexcept;
190 
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 
201 protected:
202  /* --------------------------------------------------------------------------------------------------------
203  * DSP/Plugin Callbacks */
204 
205  /**
206  A parameter has changed on the plugin side.@n
207  This is called by the host to inform the UI about parameter changes.
208  */
209  virtual void parameterChanged(uint32_t index, float value) = 0;
210 
211 #if DISTRHO_PLUGIN_WANT_PROGRAMS
212  /**
213  A program has been loaded on the plugin side.@n
214  This is called by the host to inform the UI about program changes.
215  */
216  virtual void programLoaded(uint32_t index) = 0;
217 #endif
218 
219 #if DISTRHO_PLUGIN_WANT_STATE
220  /**
221  A state has changed on the plugin side.@n
222  This is called by the host to inform the UI about state changes.
223  */
224  virtual void stateChanged(const char* key, const char* value) = 0;
225 #endif
226 
227  /* --------------------------------------------------------------------------------------------------------
228  * DSP/Plugin Callbacks (optional) */
229 
230  /**
231  Optional callback to inform the UI about a sample rate change on the plugin side.
232  @see getSampleRate()
233  */
234  virtual void sampleRateChanged(double newSampleRate);
235 
236 #if !DISTRHO_PLUGIN_HAS_EXTERNAL_UI
237  /* --------------------------------------------------------------------------------------------------------
238  * UI Callbacks (optional) */
239 
240  /**
241  uiIdle.
242  @TODO Document this.
243  */
244  virtual void uiIdle() {}
245 
246 # ifndef DGL_FILE_BROWSER_DISABLED
247  /**
248  File browser selected function.
249  @see Window::fileBrowserSelected(const char*)
250  */
251  virtual void uiFileBrowserSelected(const char* filename);
252 # endif
253 
254  /**
255  OpenGL window reshape function, called when parent window is resized.
256  You can reimplement this function for a custom OpenGL state.
257  @see Window::onReshape(uint,uint)
258  */
259  virtual void uiReshape(uint width, uint height);
260 
261  /* --------------------------------------------------------------------------------------------------------
262  * UI Resize Handling, internal */
263 
264  /**
265  OpenGL widget resize function, called when the widget is resized.
266  This is overriden here so the host knows when the UI is resized by you.
267  @see Widget::onResize(const ResizeEvent&)
268  */
269  void onResize(const ResizeEvent& ev) override;
270 #endif
271 
272  // -------------------------------------------------------------------------------------------------------
273 
274 private:
275  struct PrivateData;
276  PrivateData* const uiData;
277  friend class UIExporter;
278  friend class UIExporterWindow;
279 
280  DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(UI)
281 };
282 
283 /** @} */
284 
285 /* ------------------------------------------------------------------------------------------------------------
286  * Create UI, entry point */
287 
288 /**
289  @addtogroup EntryPoints
290  @{
291  */
292 
293 /**
294  createUI.
295  @TODO Document this.
296  */
297 extern UI* createUI();
298 
299 /** @} */
300 
301 // -----------------------------------------------------------------------------------------------------------
302 
303 END_NAMESPACE_DISTRHO
304 
305 #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
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
Definition: DistrhoUI.hpp:67
UI::sendNote
void sendNote(uint8_t channel, uint8_t note, uint8_t velocity)
UI::getNextBundlePath
static const char * getNextBundlePath() noexcept
UI::getSampleRate
double getSampleRate() const noexcept
UI::UI
UI(uint width=0, uint height=0)
UI::editParameter
void editParameter(uint32_t index, bool started)
DISTRHO_UI_CUSTOM_WIDGET_TYPE
#define DISTRHO_UI_CUSTOM_WIDGET_TYPE
Definition: DistrhoInfo.hpp:586
UI::sampleRateChanged
virtual void sampleRateChanged(double newSampleRate)
createUI
UI * createUI()
UI::getPluginInstancePointer
void * getPluginInstancePointer() const noexcept
UI::getNextWindowId
static uintptr_t getNextWindowId() noexcept
UI::setState
void setState(const char *key, const char *value)
UI::parameterChanged
virtual void parameterChanged(uint32_t index, float value)=0