1 /*
2  * DISTRHO Plugin Framework (DPF)
3  * Copyright (C) 2012-2016 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 #include "DistrhoUIInternal.hpp"
18 
19 #ifdef HAVE_DGL
20 # include "src/WidgetPrivateData.hpp"
21 #endif
22 
23 START_NAMESPACE_DISTRHO
24 
25 /* ------------------------------------------------------------------------------------------------------------
26  * Static data, see DistrhoUIInternal.hpp */
27 
28 double      d_lastUiSampleRate = 0.0;
29 void*       d_lastUiDspPtr     = nullptr;
30 #ifdef HAVE_DGL
31 Window*     d_lastUiWindow     = nullptr;
32 #endif
33 uintptr_t   g_nextWindowId     = 0;
34 const char* g_nextBundlePath   = nullptr;
35 
36 /* ------------------------------------------------------------------------------------------------------------
37  * UI */
38 
39 #ifdef HAVE_DGL
40 UI::UI(uint width, uint height)
41     : UIWidget(*d_lastUiWindow),
42       pData(new PrivateData())
43 {
44     ((UIWidget*)this)->pData->needsFullViewport = false;
45 
46     if (width > 0 && height > 0)
47         setSize(width, height);
48 }
49 #else
50 UI::UI(uint width, uint height)
51     : UIWidget(width, height),
52       pData(new PrivateData()) {}
53 #endif
54 
55 UI::~UI()
56 {
57     delete pData;
58 }
59 
60 /* ------------------------------------------------------------------------------------------------------------
61  * Host state */
62 
63 double UI::getSampleRate() const noexcept
64 {
65     return pData->sampleRate;
66 }
67 
68 void UI::editParameter(uint32_t index, bool started)
69 {
70     pData->editParamCallback(index + pData->parameterOffset, started);
71 }
72 
73 void UI::setParameterValue(uint32_t index, float value)
74 {
75     pData->setParamCallback(index + pData->parameterOffset, value);
76 }
77 
78 #if DISTRHO_PLUGIN_WANT_STATE
79 void UI::setState(const char* key, const char* value)
80 {
81     pData->setStateCallback(key, value);
82 }
83 #endif
84 
85 #if DISTRHO_PLUGIN_WANT_MIDI_INPUT
86 void UI::sendNote(uint8_t channel, uint8_t note, uint8_t velocity)
87 {
88     pData->sendNoteCallback(channel, note, velocity);
89 }
90 #endif
91 
92 #if DISTRHO_PLUGIN_WANT_DIRECT_ACCESS
93 /* ------------------------------------------------------------------------------------------------------------
94  * Direct DSP access */
95 
96 void* UI::getPluginInstancePointer() const noexcept
97 {
98     return pData->dspPtr;
99 }
100 #endif
101 
102 #if DISTRHO_PLUGIN_HAS_EXTERNAL_UI
103 /* ------------------------------------------------------------------------------------------------------------
104  * External UI helpers */
105 
106 const char* UI::getNextBundlePath() noexcept
107 {
108     return g_nextBundlePath;
109 }
110 
111 # if DISTRHO_PLUGIN_HAS_EMBED_UI
112 uintptr_t UI::getNextWindowId() noexcept
113 {
114     return g_nextWindowId;
115 }
116 # endif
117 #endif
118 
119 /* ------------------------------------------------------------------------------------------------------------
120  * DSP/Plugin Callbacks (optional) */
121 
122 void UI::sampleRateChanged(double) {}
123 
124 #ifdef HAVE_DGL
125 /* ------------------------------------------------------------------------------------------------------------
126  * UI Callbacks (optional) */
127 
128 #ifndef DGL_FILE_BROWSER_DISABLED
129 void UI::uiFileBrowserSelected(const char*)
130 {
131 }
132 #endif
133 
134 void UI::uiReshape(uint width, uint height)
135 {
136     glEnable(GL_BLEND);
137     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
138     glMatrixMode(GL_PROJECTION);
139     glLoadIdentity();
140     glOrtho(0.0, static_cast<GLdouble>(width), static_cast<GLdouble>(height), 0.0, 0.0, 1.0);
141     glViewport(0, 0, static_cast<GLsizei>(width), static_cast<GLsizei>(height));
142     glMatrixMode(GL_MODELVIEW);
143     glLoadIdentity();
144 }
145 
146 /* ------------------------------------------------------------------------------------------------------------
147  * UI Resize Handling, internal */
148 
149 void UI::onResize(const ResizeEvent& ev)
150 {
151     pData->setSizeCallback(ev.size.getWidth(), ev.size.getHeight());
152 }
153 #endif
154 
155 // -----------------------------------------------------------------------------------------------------------
156 
157 END_NAMESPACE_DISTRHO
158