summaryrefslogtreecommitdiff
path: root/libs/ardour/linux_vst_info_file.cc
blob: 7d4df1dcc8d1dc14731f187b5326c7ba066be7d9 (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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
    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.

*/

/** @file libs/ardour/vst_info_file.cc
 *  @brief Code to manage info files containing cached information about a plugin.
 *  e.g. its name, creator etc.
 */

#include <iostream>
#include <cassert>

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>

#include <stdlib.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <libgen.h>

#include <glib.h>
#include <glib/gstdio.h>
#include <glibmm.h>

#include "pbd/error.h"

#include "ardour/linux_vst_support.h"

#define MAX_STRING_LEN 256

using namespace std;

static char *
read_string (FILE *fp)
{
	char buf[MAX_STRING_LEN];

	if (!fgets (buf, MAX_STRING_LEN, fp)) {
		return 0;
	}
	
	if (strlen(buf) < MAX_STRING_LEN) {
		if (strlen (buf)) {
			buf[strlen(buf)-1] = 0;
		}
		return strdup (buf);
	} else {
		return 0;
	}
}

/** Read an integer value from a line in fp into n,
 *  @return true on failure, false on success.
 */
static bool
read_int (FILE* fp, int* n)
{
	char buf[MAX_STRING_LEN];

	char* p = fgets (buf, MAX_STRING_LEN, fp);
	if (p == 0) {
		return true;
	}

	return (sscanf (p, "%d", n) != 1);
}

static VSTInfo *
load_vstfx_info_file (FILE* fp)
{
	VSTInfo *info;
	
	if ((info = (VSTInfo*) malloc (sizeof (VSTInfo))) == 0) {
		return 0;
	}

	if ((info->name = read_string(fp)) == 0) goto error;
	if ((info->creator = read_string(fp)) == 0) goto error;
	if (read_int (fp, &info->UniqueID)) goto error;
	if ((info->Category = read_string(fp)) == 0) goto error;
	if (read_int (fp, &info->numInputs)) goto error;
	if (read_int (fp, &info->numOutputs)) goto error;
	if (read_int (fp, &info->numParams)) goto error;
	if (read_int (fp, &info->wantMidi)) goto error;
	if (read_int (fp, &info->hasEditor)) goto error;
	if (read_int (fp, &info->canProcessReplacing)) goto error;

	if ((info->ParamNames = (char **) malloc(sizeof(char*)*info->numParams)) == 0) {
		goto error;
	}

	for (int i = 0; i < info->numParams; ++i) {
		if ((info->ParamNames[i] = read_string(fp)) == 0) goto error;
	}

	if ((info->ParamLabels = (char **) malloc(sizeof(char*)*info->numParams)) == 0) {
		goto error;
	}
	
	for (int i = 0; i < info->numParams; ++i) {
		if ((info->ParamLabels[i] = read_string(fp)) == 0) goto error;
	}
	
	return info;
	
  error:
	free (info);
	return 0;
}

static int
save_vstfx_info_file (VSTInfo *info, FILE* fp)
{
	assert (info);
	assert (fp);
    
	fprintf (fp, "%s\n", info->name);
	fprintf (fp, "%s\n", info->creator);
	fprintf (fp, "%d\n", info->UniqueID);
	fprintf (fp, "%s\n", info->Category);
	fprintf (fp, "%d\n", info->numInputs);
	fprintf (fp, "%d\n", info->numOutputs);
	fprintf (fp, "%d\n", info->numParams);
	fprintf (fp, "%d\n", info->wantMidi);
	fprintf (fp, "%d\n", info->hasEditor);
	fprintf (fp, "%d\n", info->canProcessReplacing);

	for (int i = 0; i < info->numParams; i++) {
		fprintf (fp, "%s\n", info->ParamNames[i]);
	}
	
	for (int i = 0; i < info->numParams; i++) {
		fprintf (fp, "%s\n", info->ParamLabels[i]);
	}
	
    return 0;
}

static string
vstfx_infofile_path (char* dllpath, int personal)
{
	string dir;
	if (personal) {
		dir = Glib::build_filename (Glib::get_home_dir (), ".fst");

		/* If the directory doesn't exist, try to create it */
		if (!Glib::file_test (dir, Glib::FILE_TEST_IS_DIR)) {
			if (g_mkdir (dir.c_str (), 0700)) {
				return 0;
			}
		}
		
	} else {
		dir = Glib::path_get_dirname (dllpath);
	}

	stringstream s;
	s << "." << Glib::path_get_basename (dllpath) << ".fsi";
	return Glib::build_filename (dir, s.str ());
}

static char *
vstfx_infofile_stat (char *dllpath, struct stat* statbuf, int personal)
{
	if (strstr (dllpath, ".so" ) == 0) {
		return 0;
	}

	string const path = vstfx_infofile_path (dllpath, personal);

	if (Glib::file_test (path, Glib::FileTest (Glib::FILE_TEST_EXISTS | Glib::FILE_TEST_IS_REGULAR))) {

		/* info file exists in same location as the shared object, so
		   check if its current and up to date
		*/


		struct stat dllstat;
		
		if (stat (dllpath, &dllstat) == 0) {
			if (stat (path.c_str(), statbuf) == 0) {
				if (dllstat.st_mtime <= statbuf->st_mtime) {
					/* plugin is older than info file */
					return strdup (path.c_str ());
				}
			}
		} 
	}

	return 0;
}


static FILE *
vstfx_infofile_for_read (char* dllpath)
{
	struct stat own_statbuf;
	struct stat sys_statbuf;
	
	char* own_info = vstfx_infofile_stat (dllpath, &own_statbuf, 1);
	char* sys_info = vstfx_infofile_stat (dllpath, &sys_statbuf, 0);

	if (own_info) {
		if (sys_info) {
			if (own_statbuf.st_mtime <= sys_statbuf.st_mtime) {
				/* system info file is newer, use it */
				return g_fopen (sys_info, "rb");
			}
		} else {
			return g_fopen (own_info, "rb");
		}
	}

	return 0;
}

static FILE *
vstfx_infofile_create (char* dllpath, int personal)
{
	if (strstr (dllpath, ".so" ) == 0) {
		return 0;
	}

	string const path = vstfx_infofile_path (dllpath, personal);
	return fopen (path.c_str(), "w");
}

static FILE *
vstfx_infofile_for_write (char* dllpath)
{
	FILE* f;

	if ((f = vstfx_infofile_create (dllpath, 0)) == 0) {
		f = vstfx_infofile_create (dllpath, 1);
	}
	
	return f;
}

static
int vstfx_can_midi (VSTState* vstfx)
{
	AEffect* plugin = vstfx->plugin;
	
	int const vst_version = plugin->dispatcher (plugin, effGetVstVersion, 0, 0, 0, 0.0f);

	if (vst_version >= 2) {
		/* should we send it VST events (i.e. MIDI) */
		
		if ((plugin->flags & effFlagsIsSynth) || (plugin->dispatcher (plugin, effCanDo, 0, 0,(void*) "receiveVstEvents", 0.0f) > 0)) {
			return -1;
		}
	}
	
	return false;
}

static VSTInfo *
vstfx_info_from_plugin (VSTState* vstfx)
{
	assert (vstfx);
	
	VSTInfo* info = (VSTInfo*) malloc (sizeof (VSTInfo));
	if (!info) {
		return 0;
	}
	
	/*We need to init the creator because some plugins
	  fail to implement getVendorString, and so won't stuff the
	  string with any name*/
	
	char creator[65] = "Unknown\0";
	
	AEffect* plugin = vstfx->plugin;
	
	info->name = strdup (vstfx->handle->name); 
	
	/*If the plugin doesn't bother to implement GetVendorString we will
	  have pre-stuffed the string with 'Unkown' */
	
	plugin->dispatcher (plugin, effGetVendorString, 0, 0, creator, 0);
	
	/*Some plugins DO implement GetVendorString, but DON'T put a name in it
	  so if its just a zero length string we replace it with 'Unknown' */
	
	if (strlen(creator) == 0) {
		info->creator = strdup ("Unknown");
	} else {
		info->creator = strdup (creator);
	}
	
	info->UniqueID = plugin->uniqueID;
	
	info->Category = strdup("None"); /* XXX */
	info->numInputs = plugin->numInputs;
	info->numOutputs = plugin->numOutputs;
	info->numParams = plugin->numParams;
	info->wantMidi = vstfx_can_midi(vstfx); 
	info->hasEditor = plugin->flags & effFlagsHasEditor ? true : false;
	info->canProcessReplacing = plugin->flags & effFlagsCanReplacing ? true : false;
	info->ParamNames = (char **) malloc(sizeof(char*)*info->numParams);
	info->ParamLabels = (char **) malloc(sizeof(char*)*info->numParams);

	for (int i = 0; i < info->numParams; ++i) {
		char name[64];
		char label[64];
		
		/* Not all plugins give parameters labels as well as names */
		
		strcpy (name, "No Name");
		strcpy (label, "No Label");
		
		plugin->dispatcher (plugin, effGetParamName, i, 0, name, 0);
		info->ParamNames[i] = strdup(name);
		
		//NOTE: 'effGetParamLabel' is no longer defined in vestige headers
		//plugin->dispatcher (plugin, effGetParamLabel, i, 0, label, 0);
		info->ParamLabels[i] = strdup(label);
	}
	return info;
}

/* A simple 'dummy' audiomaster callback which should be ok,
   we will only be instantiating the plugin in order to get its info
*/

static intptr_t
simple_master_callback (AEffect *, int32_t opcode, int32_t, intptr_t, void *, float)
{
	if (opcode == audioMasterVersion) {
		return 2;
	} else {
		return 0;
	}
}

/** Try to get plugin info - first by looking for a .fsi cache of the
    data, and if that doesn't exist, load the plugin, get its data and
    then cache it for future ref
*/

VSTInfo *
vstfx_get_info (char* dllpath)
{
	FILE* infofile;
	VSTHandle* h;
	VSTState* vstfx;

	if ((infofile = vstfx_infofile_for_read (dllpath)) != 0) {
		VSTInfo *info;
		info = load_vstfx_info_file (infofile);
		fclose (infofile);
		if (info == 0) {
			PBD::warning << "Cannot get LinuxVST information form " << dllpath << ": info file load failed." << endmsg;
		}
		return info;
	} 
	
	if (!(h = vstfx_load(dllpath))) {
		PBD::warning << "Cannot get LinuxVST information from " << dllpath << ": load failed." << endmsg;
		return 0;
	}
	
	if (!(vstfx = vstfx_instantiate(h, simple_master_callback, 0))) {
	    	vstfx_unload(h);
		PBD::warning << "Cannot get LinuxVST information from " << dllpath << ": instantiation failed." << endmsg;
	    	return 0;
	}
	
	infofile = vstfx_infofile_for_write (dllpath);
	
	if (!infofile) {
		vstfx_close(vstfx);
		vstfx_unload(h);
		PBD::warning << "Cannot get LinuxVST information from " << dllpath << ": cannot create new FST info file." << endmsg;
		return 0;
	}
	
	VSTInfo* info = vstfx_info_from_plugin (vstfx);
	
	save_vstfx_info_file (info, infofile);
	fclose (infofile);
	
	vstfx_close (vstfx);
	vstfx_unload (h);
	
	return info;
}

void
vstfx_free_info (VSTInfo *info)
{
	for (int i = 0; i < info->numParams; i++) {
		free (info->ParamNames[i]);
		free (info->ParamLabels[i]);
	}
	
	free (info->name);
	free (info->creator);
	free (info->Category);
	free (info);
}