summaryrefslogtreecommitdiff
path: root/libs/ardour/linux_vst_support.cc
blob: 498cbc8339c49076a9f8289d06e6a69e63cedfa4 (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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
    Copyright (C) 2012 Paul Davis

    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., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <pthread.h>
#include <signal.h>
#include <dlfcn.h>
#include <string.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/linux_vst_support.h"
#include "pbd/basename.h"
#include "pbd/error.h"

#include "pbd/i18n.h"

/***********************************************************/
/* VSTFX - A set of modules for managing linux VST plugins */
/* vstfx.cc, vstfxwin.cc and vstfxinfofile.cc              */
/***********************************************************/

/*Simple error handler stuff for VSTFX*/

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

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

/*default error handler callback*/

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

void (*vstfx_error_callback)(const char *desc) = &default_vstfx_error_callback;

/* --- */

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

static VSTHandle *
vstfx_handle_new ()
{
	VSTHandle* vstfx = (VSTHandle *) calloc (1, sizeof (VSTHandle));
	return vstfx;
}

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

static VSTState *
vstfx_new ()
{
	VSTState* vstfx = (VSTState *) calloc (1, sizeof (VSTState));
	vststate_init (vstfx);
	return vstfx;
}

/*This loads the plugin shared library*/

static void* vstfx_load_vst_library(const char* path)
{
	void* dll;
	char* full_path = NULL;
	char* envdup;
	char* lxvst_path;
	size_t len1;
	size_t len2;

	/*Try and load the shared library pointed to by the path -
	NOTE: You have to give RTLD_LAZY or RTLD_NOW to dlopen or
	you get some occasional failures to load - dlerror reports
	invalid arguments*/

	if ((dll = dlopen (path, RTLD_LOCAL | RTLD_LAZY)) != 0) {
		return dll;
	}

	if (Glib::file_test (path, Glib::FILE_TEST_EXISTS)) {
		PBD::error << string_compose (_("Could not open existing LXVST plugin: %1"), dlerror()) << endmsg;
		return 0;
	}

	/*We didn't find the library so try and get the path specified in the
	env variable LXVST_PATH*/

	envdup = getenv ("LXVST_PATH");

	/*Path not specified - not much more we can do*/

	if (envdup == 0)
		return 0;

	/*Copy the path into envdup*/

	envdup = strdup (envdup);

	if (envdup == 0)
		return 0;

	len2 = strlen(path);

	/*Try all the possibilities in the path - deliminated by : */
	char *saveptr;
	lxvst_path = strtok_r (envdup, ":", &saveptr);

	while (lxvst_path != 0)
	{
		vstfx_error ("\"%s\"", lxvst_path);
		len1 = strlen(lxvst_path);

		if (full_path) free(full_path);
		full_path = (char*)malloc(len1 + 1 + len2 + 1);
		memcpy(full_path, lxvst_path, len1);
		full_path[len1] = '/';
		memcpy(full_path + len1 + 1, path, len2);
		full_path[len1 + 1 + len2] = '\0';

		/*Try and load the library*/

		if ((dll = dlopen(full_path, RTLD_LOCAL | RTLD_LAZY)) != 0)
		{
			/*Succeeded */
			break;
		}

		/*Try again*/

		lxvst_path = strtok_r (0, ":", &saveptr);
	}

	/*Free the path*/
	if (full_path) free(full_path);
	free(envdup);

	return dll;
}

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

VSTHandle *
vstfx_load (const char *path)
{
	char* buf = 0;
	VSTHandle* fhandle;

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

	fhandle = vstfx_handle_new();

	/*See if we have .so appended to the path - if not we need to make sure it is added*/

	if (strstr (path, ".so") == 0)
	{

		/*Append the .so to the path - Make sure the path has enough space*/

		buf = (char *)malloc(strlen(path) + 4); //The .so and a terminating zero

		sprintf (buf, "%s.so", path);

	}
	else
	{
		/*We already have .so appened to the filename*/

		buf = strdup(path);
	}

	/* get a name for the plugin based on the path: ye old VST problem where
	   we don't know anything about its name until we load and instantiate the plugin
	   which we don't want to do at this point
	*/

	fhandle->name = strdup (PBD::basename_nosuffix (path).c_str());

	/*call load_vstfx_library to actually load the .so into memory*/

	if ((fhandle->dll = vstfx_load_vst_library (buf)) == 0)
	{
		vstfx_unload (fhandle);

		free(buf);

		return 0;
	}

	/*Find the main entry point into the plugin*/

	fhandle->main_entry = (main_entry_t) dlsym(fhandle->dll, "VSTPluginMain");

	if (fhandle->main_entry == 0) {
		fhandle->main_entry = (main_entry_t) dlsym(fhandle->dll, "main");
	}

	if (fhandle->main_entry == 0)
	{
		/*If it can't be found, unload the plugin and return a 0 handle*/

		vstfx_unload (fhandle);

		free(buf);

		return 0;
	}

	free(buf);

	/*return the handle of the plugin*/

	return fhandle;
}

/*This unloads a plugin*/

int
vstfx_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)
	{
		dlclose(fhandle->dll);
		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 *
vstfx_instantiate (VSTHandle* fhandle, audioMasterCallback amc, void* userptr)
{
	VSTState* vstfx = vstfx_new ();

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

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

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

	if (vstfx->plugin->magic != kEffectMagic)
	{
		vstfx_error ("** ERROR ** VSTFX : %s is not a VST plugin\n", fhandle->name);
		free (vstfx);
		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
		 */
		vstfx->plugin->dispatcher (vstfx->plugin, effOpen, 0, 0, 0, 0);
		vstfx->vst_version = vstfx->plugin->dispatcher (vstfx->plugin, effGetVstVersion, 0, 0, 0, 0);
	}

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

	return vstfx;
}

/*Close a vstfx instance*/

void vstfx_close (VSTState* vstfx)
{
	vstfx_destroy_editor(vstfx);

	if(vstfx->plugin)
	{
		vstfx->plugin->dispatcher (vstfx->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*/

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

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

	/*vstfx_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 (vstfx->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 (vstfx->handle->dll)
	{
		dlclose(vstfx->handle->dll); //dlclose keeps its own reference count
		vstfx->handle->dll = 0;
	}
	free(vstfx);
}