1 // samplv1widget_lv2.cpp
2 //
3 /****************************************************************************
4    Copyright (C) 2012-2021, rncbc aka Rui Nuno Capela. All rights reserved.
5 
6    This program is free software; you can redistribute it and/or
7    modify it under the terms of the GNU General Public License
8    as published by the Free Software Foundation; either version 2
9    of the License, or (at your option) any later version.
10 
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License along
17    with this program; if not, write to the Free Software Foundation, Inc.,
18    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 
20 *****************************************************************************/
21 
22 #include "samplv1widget_lv2.h"
23 
24 #include "samplv1_lv2.h"
25 
26 #include "samplv1widget_palette.h"
27 
28 #include <QApplication>
29 #include <QFileInfo>
30 #include <QDir>
31 
32 #include <QCloseEvent>
33 
34 #include <QStyleFactory>
35 
36 #ifndef CONFIG_LIBDIR
37 #if defined(__x86_64__)
38 #define CONFIG_LIBDIR CONFIG_PREFIX "/lib64"
39 #else
40 #define CONFIG_LIBDIR CONFIG_PREFIX "/lib"
41 #endif
42 #endif
43 
44 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
45 #define CONFIG_PLUGINSDIR CONFIG_LIBDIR "/qt5/plugins"
46 #else
47 #define CONFIG_PLUGINSDIR CONFIG_LIBDIR "/qt6/plugins"
48 #endif
49 
50 
51 //-------------------------------------------------------------------------
52 // samplv1widget_lv2 - impl.
53 //
54 
55 // Constructor.
samplv1widget_lv2(samplv1_lv2 * pSampl,LV2UI_Controller controller,LV2UI_Write_Function write_function)56 samplv1widget_lv2::samplv1widget_lv2 ( samplv1_lv2 *pSampl,
57 	LV2UI_Controller controller, LV2UI_Write_Function write_function )
58 	: samplv1widget()
59 {
60 	// Check whether under a dedicated application instance...
61 	QApplication *pApp = samplv1_lv2::qapp_instance();
62 	if (pApp) {
63 		// Special style paths...
64 		if (QDir(CONFIG_PLUGINSDIR).exists())
65 			pApp->addLibraryPath(CONFIG_PLUGINSDIR);
66 		// Custom color/style themes...
67 		samplv1_config *pConfig = samplv1_config::getInstance();
68 		if (pConfig) {
69 			if (!pConfig->sCustomColorTheme.isEmpty()) {
70 				QPalette pal;
71 				if (samplv1widget_palette::namedPalette(
72 						pConfig, pConfig->sCustomColorTheme, pal))
73 					pApp->setPalette(pal);
74 			}
75 			if (!pConfig->sCustomStyleTheme.isEmpty()) {
76 				pApp->setStyle(
77 					QStyleFactory::create(pConfig->sCustomStyleTheme));
78 			}
79 		}
80 	}
81 
82 	// Initialize (user) interface stuff...
83 	m_pSamplUi = new samplv1_lv2ui(pSampl, controller, write_function);
84 
85 #ifdef CONFIG_LV2_UI_EXTERNAL
86 	m_external_host = nullptr;
87 #endif
88 #ifdef CONFIG_LV2_UI_IDLE
89 	m_bIdleClosed = false;
90 #endif
91 
92 	// Initialise preset stuff...
93 	clearPreset();
94 
95 	// Initial update, always...
96 	updateSample(m_pSamplUi->sample());
97 
98 	//resetParamValues();
99 	resetParamKnobs();
100 
101 	// May initialize the scheduler/work notifier.
102 	openSchedNotifier();
103 }
104 
105 
106 // Destructor.
~samplv1widget_lv2(void)107 samplv1widget_lv2::~samplv1widget_lv2 (void)
108 {
109 	delete m_pSamplUi;
110 }
111 
112 
113 // Synth engine accessor.
ui_instance(void) const114 samplv1_ui *samplv1widget_lv2::ui_instance (void) const
115 {
116 	return m_pSamplUi;
117 }
118 
119 
120 #ifdef CONFIG_LV2_UI_EXTERNAL
121 
setExternalHost(LV2_External_UI_Host * external_host)122 void samplv1widget_lv2::setExternalHost ( LV2_External_UI_Host *external_host )
123 {
124 	m_external_host = external_host;
125 }
126 
externalHost(void) const127 const LV2_External_UI_Host *samplv1widget_lv2::externalHost (void) const
128 {
129 	return m_external_host;
130 }
131 
132 #endif	// CONFIG_LV2_UI_EXTERNAL
133 
134 
135 #ifdef CONFIG_LV2_UI_IDLE
136 
isIdleClosed(void) const137 bool samplv1widget_lv2::isIdleClosed (void) const
138 {
139 	return m_bIdleClosed;
140 }
141 
142 #endif	// CONFIG_LV2_UI_IDLE
143 
144 
145 // Close event handler.
closeEvent(QCloseEvent * pCloseEvent)146 void samplv1widget_lv2::closeEvent ( QCloseEvent *pCloseEvent )
147 {
148 	samplv1widget::closeEvent(pCloseEvent);
149 
150 #ifdef CONFIG_LV2_UI_IDLE
151 	if (pCloseEvent->isAccepted())
152 		m_bIdleClosed = true;
153 #endif
154 #ifdef CONFIG_LV2_UI_EXTERNAL
155 	if (m_external_host && m_external_host->ui_closed) {
156 		if (pCloseEvent->isAccepted())
157 			m_external_host->ui_closed(m_pSamplUi->controller());
158 	}
159 #endif
160 }
161 
162 
163 // LV2 port event dispatcher.
port_event(uint32_t port_index,uint32_t buffer_size,uint32_t format,const void * buffer)164 void samplv1widget_lv2::port_event ( uint32_t port_index,
165 	uint32_t buffer_size, uint32_t format, const void *buffer )
166 {
167 	if (format == 0 && buffer_size == sizeof(float)) {
168 		const samplv1::ParamIndex index
169 			= samplv1::ParamIndex(port_index - samplv1_lv2::ParamBase);
170 		const float fValue = *(float *) buffer;
171 		setParamValue(index, fValue);
172 	}
173 }
174 
175 
176 // Param method.
updateParam(samplv1::ParamIndex index,float fValue) const177 void samplv1widget_lv2::updateParam (
178 	samplv1::ParamIndex index, float fValue ) const
179 {
180 	m_pSamplUi->write_function(index, fValue);
181 }
182 
183 
184 // end of samplv1widget_lv2.cpp
185