summaryrefslogtreecommitdiff
path: root/libs/ardour/mac_vst_support.cc
blob: e9a7f4895106e60b6706d01a2659880a1d41d7c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
 * Copyright (C) 2016-2017 Robin Gareus <robin@gareus.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <pthread.h>
#include <signal.h>
#include <dlfcn.h>
#include <string.h>
#include <strings.h>
#include <time.h>
#include <unistd.h>
#include <pthread.h>

#include <glib.h>
#include "pbd/gstdio_compat.h"
#include <glibmm/miscutils.h>
#include <glibmm/fileutils.h>

#include "ardour/mac_vst_support.h"
#include "pbd/basename.h"
#include "pbd/error.h"

#include "pbd/i18n.h"

#include <Carbon/Carbon.h>

/*Simple error handler stuff for VSTFX*/

void mac_vst_error (const char *fmt, ...)
{
	va_list ap;
	char buffer[512];

	va_start (ap, fmt);
	vsnprintf (buffer, sizeof (buffer), fmt, ap);
	mac_vst_error_callback (buffer);
	va_end (ap);
}

/*default error handler callback*/

static void default_mac_vst_error_callback (const char *desc)
{
	PBD::error << desc << endmsg;
}

void (*mac_vst_error_callback)(const char *desc) = &default_mac_vst_error_callback;

/* --- */

/*Create and return a pointer to a new VSTFX handle*/

static VSTHandle *
mac_vst_handle_new ()
{
	VSTHandle* mac_vst = (VSTHandle *) calloc (1, sizeof (VSTHandle));
	return mac_vst;
}

/*Create and return a pointer to a new mac_vst instance*/

static VSTState *
mac_vst_new ()
{
	VSTState* mac_vst = (VSTState *) calloc (1, sizeof (VSTState));
	vststate_init (mac_vst);
	return mac_vst;
}

/*This loads up a plugin, given the path to its .vst bundle and
 * finds its main entry point etc */

VSTHandle *
mac_vst_load (const char *path)
{
	VSTHandle* fhandle;

	/*Create a new handle we can use to reference the plugin*/

	fhandle = mac_vst_handle_new ();

	fhandle->dll = NULL;

	CFURLRef url;
	if (!(url = CFURLCreateFromFileSystemRepresentation (0, (const UInt8*)path, (CFIndex) strlen (path), true))) {
		return 0;
	}

	CFBundleRef bundleRef = CFBundleCreate (kCFAllocatorDefault, url);
	CFRelease (url);

	if (bundleRef == 0) {
		return 0;
	}

	if (!CFBundleLoadExecutable (bundleRef)) {
		CFRelease (bundleRef);
		return 0;
	}

	fhandle->name = strdup (path);
	fhandle->dll = (void*) &bundleRef;

	fhandle->main_entry = (main_entry_t)
		CFBundleGetFunctionPointerForName (bundleRef, CFSTR("main_macho"));

	if (!fhandle->main_entry) {
		fhandle->main_entry = (main_entry_t)
			CFBundleGetFunctionPointerForName (bundleRef, CFSTR("VSTPluginMain"));
	}

	if (fhandle->main_entry == 0) {
		mac_vst_unload (fhandle);
		return 0;
	}

	fhandle->res_file_id = CFBundleOpenBundleResourceMap (bundleRef);

	/*return the handle of the plugin*/
	return fhandle;
}

/*This unloads a plugin*/

int
mac_vst_unload (VSTHandle* fhandle)
{
	if (fhandle->plugincnt)
	{
		/*Still have plugin instances - can't unload the library
		- actually dlclose keeps an instance count anyway*/

		return -1;
	}

	/*Valid plugin loaded?*/

	if (fhandle->dll)
	{
		CFBundleRef* bundleRefPtr = (CFBundleRef*) fhandle->dll;
		CFBundleCloseBundleResourceMap (*bundleRefPtr, (CFBundleRefNum)fhandle->res_file_id);
		CFRelease (*bundleRefPtr);
		fhandle->dll = 0;
	}

	if (fhandle->name)
	{
		free (fhandle->name);
	}

	/*Don't need the plugin handle any more*/

	free (fhandle);
	return 0;
}

/*This instantiates a plugin*/

VSTState *
mac_vst_instantiate (VSTHandle* fhandle, audioMasterCallback amc, void* userptr)
{
	VSTState* mac_vst = mac_vst_new ();

	if (fhandle == 0)
	{
		mac_vst_error ( "** ERROR ** VSTFX : The handle was 0\n" );
		free (mac_vst);
		return 0;
	}

	if ((mac_vst->plugin = fhandle->main_entry (amc)) == 0)
	{
		mac_vst_error ("** ERROR ** VSTFX : %s could not be instantiated :(\n", fhandle->name);
		free (mac_vst);
		return 0;
	}

	mac_vst->handle = fhandle;
	mac_vst->plugin->ptr1 = userptr;

	if (mac_vst->plugin->magic != kEffectMagic)
	{
		mac_vst_error ("** ERROR ** VSTFX : %s is not a VST plugin\n", fhandle->name);
		free (mac_vst);
		return 0;
	}

	if (!userptr) {
		/* scanning.. or w/o master-callback userptr == 0, open now.
		 *
		 * Session::vst_callback needs a pointer to the AEffect
		 *     ((VSTPlugin*)userptr)->_plugin = vstfx->plugin
		 * before calling effOpen, because effOpen may call back
		 */
		mac_vst->plugin->dispatcher (mac_vst->plugin, effOpen, 0, 0, 0, 0);
		mac_vst->vst_version = mac_vst->plugin->dispatcher (mac_vst->plugin, effGetVstVersion, 0, 0, 0, 0);

		/* configure plugin to use Cocoa View */
		mac_vst->plugin->dispatcher (mac_vst->plugin, effCanDo, 0, 0, const_cast<char*> ("hasCockosViewAsConfig"), 0.0f);
	}

	mac_vst->handle->plugincnt++;
	mac_vst->wantIdle = 0;

	return mac_vst;
}

/*Close a mac_vst instance*/

void mac_vst_close (VSTState* mac_vst)
{
	// assert that the GUI object is destoyed

	if (mac_vst->plugin)
	{
		mac_vst->plugin->dispatcher (mac_vst->plugin, effMainsChanged, 0, 0, 0, 0);

		/*Calling dispatcher with effClose will cause the plugin's destructor to
		be called, which will also remove the editor if it exists*/

		mac_vst->plugin->dispatcher (mac_vst->plugin, effClose, 0, 0, 0, 0);
	}

	if (mac_vst->handle->plugincnt)
			mac_vst->handle->plugincnt--;

	/*mac_vst_unload will unload the dll if the instance count allows -
	we need to do this because some plugins keep their own instance count
	and (JUCE) manages the plugin UI in its own thread.  When the plugins
	internal instance count reaches zero, JUCE stops the UI thread and won't
	restart it until the next time the library is loaded.  If we don't unload
	the lib JUCE will never restart*/


	if (mac_vst->handle->plugincnt)
	{
		return;
	}

	/*Valid plugin loaded - so we can unload it and 0 the pointer
	to it.  We can't free the handle here because we don't know what else
	might need it.  It should be / is freed when the plugin is deleted*/

	if (mac_vst->handle->dll)
	{
		dlclose (mac_vst->handle->dll); //dlclose keeps its own reference count
		mac_vst->handle->dll = 0;
	}
	free (mac_vst);
}

#if 0 // TODO wrap dispatch
intptr_t
mac_vst_dispatch (VSTState* mac_vst, int op, int idx, intptr_t val, void* ptr, float opt)
{
	const ResFileRefNum old_resources = CurResFile();

	if (mac_vst->handle->res_file_id) {
		UseResFile (mac_vst->handle->res_file_id);
	}

	mac_vst->plugin->dispatcher (mac_vst->plugin, op, idx, val, prt, opt);

	const ResFileRefNum current_res = CurResFile();
	if (current_res != old_resources) {
		mac_vst->handle->res_file_id = current_res;
		UseResFile (old_resources);
	}
}
#endif