83cbfe4a561e9b339089da28f4f4e38e2d7f6f02
[ardour.git] / libs / ardour / linux_vst_info_file.cc
1 /***********************************************************/
2 /*vstfx infofile - module to manage info files             */
3 /*containing cached information about a plugin. e.g. its   */
4 /*name, creator etc etc                                    */
5 /***********************************************************/
6
7 #include <iostream>
8 #include <cassert>
9
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <unistd.h>
13 #include <errno.h>
14
15 #include <stdlib.h>
16 #include <stddef.h>
17 #include <stdio.h>
18 #include <string.h>
19 #include <libgen.h>
20
21 #include <glib.h>
22 #include <glib/gstdio.h>
23
24 #include "pbd/error.h"
25
26 #include "ardour/linux_vst_support.h"
27
28 #define MAX_STRING_LEN 256
29
30 static char* read_string(FILE *fp)
31 {
32     char buf[MAX_STRING_LEN];
33
34     if (!fgets( buf, MAX_STRING_LEN, fp )) {
35             return 0;
36     }
37     
38     if(strlen(buf) < MAX_STRING_LEN) {
39             if (strlen(buf)) {
40                     buf[strlen(buf)-1] = 0;
41             }
42             return strdup(buf);
43     } else {
44             return 0;
45     }
46 }
47
48 /** Read an integer value from a line in fp into n,
49  *  @return true on success, false on failure.
50  */
51 static bool
52 read_int (FILE* fp, int* n)
53 {
54         char buf[MAX_STRING_LEN];
55
56         char* p = fgets (buf, MAX_STRING_LEN, fp);
57         if (p == 0) {
58                 return false;
59         }
60
61         return (sscanf (p, "%d", n) != 1);
62 }
63
64 static VSTInfo *
65 load_vstfx_info_file (FILE* fp)
66 {
67         VSTInfo *info;
68         
69         if ((info = (VSTInfo*) malloc (sizeof (VSTInfo))) == 0) {
70                 return 0;
71         }
72
73         if ((info->name = read_string(fp)) == 0) goto error;
74         if ((info->creator = read_string(fp)) == 0) goto error;
75         if (read_int (fp, &info->UniqueID)) goto error;
76         if ((info->Category = read_string(fp)) == 0) goto error;
77         if (read_int (fp, &info->numInputs)) goto error;
78         if (read_int (fp, &info->numOutputs)) goto error;
79         if (read_int (fp, &info->numParams)) goto error;
80         if (read_int (fp, &info->wantMidi)) goto error;
81         if (read_int (fp, &info->hasEditor)) goto error;
82         if (read_int (fp, &info->canProcessReplacing)) goto error;
83
84         if ((info->ParamNames = (char **) malloc(sizeof(char*)*info->numParams)) == 0) {
85                 goto error;
86         }
87
88         for (int i = 0; i < info->numParams; ++i) {
89                 if ((info->ParamNames[i] = read_string(fp)) == 0) goto error;
90         }
91
92         if ((info->ParamLabels = (char **) malloc(sizeof(char*)*info->numParams)) == 0) {
93                 goto error;
94         }
95         
96         for (int i = 0; i < info->numParams; ++i) {
97                 if ((info->ParamLabels[i] = read_string(fp)) == 0) goto error;
98         }
99         
100         return info;
101         
102   error:
103         free( info );
104         return 0;
105 }
106
107 static int
108 save_vstfx_info_file (VSTInfo *info, FILE* fp)
109 {
110     if (info == 0) {
111             vstfx_error("** ERROR ** VSTFXinfofile : info ptr is 0\n");
112             return -1;
113     }
114
115     if (fp == 0) {
116             vstfx_error("** ERROR ** VSTFXinfofile : file ptr is 0\n");
117             return -1;
118     }
119     
120     fprintf( fp, "%s\n", info->name );
121     fprintf( fp, "%s\n", info->creator );
122     fprintf( fp, "%d\n", info->UniqueID );
123     fprintf( fp, "%s\n", info->Category );
124     fprintf( fp, "%d\n", info->numInputs );
125     fprintf( fp, "%d\n", info->numOutputs );
126     fprintf( fp, "%d\n", info->numParams );
127     fprintf( fp, "%d\n", info->wantMidi );
128     fprintf( fp, "%d\n", info->hasEditor );
129     fprintf( fp, "%d\n", info->canProcessReplacing );
130
131     for (int i = 0; i < info->numParams; i++) {
132                 fprintf(fp, "%s\n", info->ParamNames[i]);
133     }
134         
135     for (int i = 0; i < info->numParams; i++) {
136                 fprintf(fp, "%s\n", info->ParamLabels[i]);
137     }
138         
139     return 0;
140 }
141
142 static char* vstfx_infofile_stat (char *dllpath, struct stat* statbuf, int personal)
143 {
144         char* path;
145         char* dir_path;
146         char* basename;
147         char* base;
148         size_t blen;
149
150         if (strstr (dllpath, ".so" ) == 0) {
151                 return 0;
152         }
153         
154         if (personal) {
155                 dir_path = g_build_filename (g_get_home_dir(), ".fst", NULL);
156         } else {
157                 dir_path = g_path_get_dirname (dllpath);
158         }
159         
160         base = g_path_get_basename (dllpath);
161         blen = strlen (base) + 2; // null char and '.'
162         basename = (char*) g_malloc (blen);
163         snprintf (basename, blen, ".%s.fsi", base);
164         g_free (base);
165         
166         path = g_build_filename (dir_path, basename, NULL);
167         
168         g_free (dir_path);
169         g_free (basename);
170
171
172         if (g_file_test (path, GFileTest (G_FILE_TEST_EXISTS|G_FILE_TEST_IS_REGULAR))) {
173
174                 /* info file exists in same location as the shared object, so
175                    check if its current and up to date
176                 */
177
178
179                 struct stat dllstat;
180                 
181                 if (stat (dllpath, &dllstat) == 0) {
182                         if (stat(path, statbuf) == 0) {
183                                 if (dllstat.st_mtime <= statbuf->st_mtime) {
184                                         /* plugin is older than info file */
185                                         return path;
186                                 }
187                         }
188                 } 
189         }
190
191         g_free (path);
192
193         return 0;
194 }
195
196
197 static FILE* vstfx_infofile_for_read (char* dllpath)
198 {
199         struct stat own_statbuf;
200         struct stat sys_statbuf;
201         char *own_info;
202         char *sys_info;
203         
204         own_info = vstfx_infofile_stat (dllpath, &own_statbuf, 1);
205         sys_info = vstfx_infofile_stat (dllpath, &sys_statbuf, 0);
206
207         if (own_info) {
208                 if (sys_info) {
209                         if (own_statbuf.st_mtime <= sys_statbuf.st_mtime) {
210                                 /* system info file is newer, use it */
211                                 return fopen (sys_info, "r");
212                         }
213                 } else {
214                         return fopen (own_info, "r");
215                 }
216         }
217
218         return 0;
219 }
220
221 static FILE* vstfx_infofile_create (char* dllpath, int personal)
222 {
223         char* path;
224         char* dir_path;
225         char* basename;
226         char* base;
227         size_t blen;
228
229         if (strstr (dllpath, ".so" ) == 0) {
230                 return 0;
231         }
232         
233         if (personal) {
234                 dir_path = g_build_filename (g_get_home_dir(), ".fst", NULL);
235
236                 /* if the directory doesn't yet exist, try to create it */
237
238                 if (!g_file_test (dir_path, G_FILE_TEST_IS_DIR)) {
239                         if (g_mkdir (dir_path, 0700)) {
240                                 return 0;
241                         }
242                 }
243
244         } else {
245                 dir_path = g_path_get_dirname (dllpath);
246         }
247         
248         base = g_path_get_basename (dllpath);
249         blen = strlen (base) + 2; // null char and '.'
250         basename = (char*) g_malloc (blen);
251         snprintf (basename, blen, ".%s.fsi", base);
252         g_free (base);
253
254         path = g_build_filename (dir_path, basename, NULL);
255
256         g_free (dir_path);
257         g_free (basename);
258
259         FILE* f = fopen (path, "w");
260         g_free (path);
261
262         return f;
263 }
264
265 static FILE* vstfx_infofile_for_write (char* dllpath)
266 {
267         FILE* f;
268
269         if ((f = vstfx_infofile_create (dllpath, 0)) == 0) {
270                 f = vstfx_infofile_create (dllpath, 1);
271         }
272         
273         return f;
274 }
275
276 static
277 int vstfx_can_midi (VSTState* vstfx)
278 {
279         AEffect *plugin = vstfx->plugin;
280         
281         int vst_version = plugin->dispatcher (plugin, effGetVstVersion, 0, 0, 0, 0.0f);
282
283         if (vst_version >= 2)
284         {
285                 /* should we send it VST events (i.e. MIDI) */
286                 
287                 if ((plugin->flags & effFlagsIsSynth) || (plugin->dispatcher (plugin, effCanDo, 0, 0,(void*) "receiveVstEvents", 0.0f) > 0))
288                     return -1;
289         }
290         return false;
291 }
292
293 static VSTInfo *
294 vstfx_info_from_plugin (VSTState* vstfx)
295 {
296         assert (vstfx);
297         
298         VSTInfo* info = (VSTInfo*) malloc (sizeof (VSTInfo));
299         
300         AEffect *plugin;
301         
302         /*We need to init the creator because some plugins
303           fail to implement getVendorString, and so won't stuff the
304           string with any name*/
305         
306         char creator[65] = "Unknown\0";
307         
308         if(!info)
309                 return 0;
310         
311         plugin = vstfx->plugin;
312         
313         info->name = strdup(vstfx->handle->name ); 
314         
315         /*If the plugin doesn't bother to implement GetVendorString we will
316           have pre-stuffed the string with 'Unkown' */
317         
318         plugin->dispatcher (plugin, effGetVendorString, 0, 0, creator, 0);
319         
320         /*Some plugins DO implement GetVendorString, but DON'T put a name in it
321           so if its just a zero length string we replace it with 'Unknown' */
322         
323         if (strlen(creator) == 0) {
324                 info->creator = strdup("Unknown");
325         } else {
326                 info->creator = strdup (creator);
327         }
328         
329         info->UniqueID = plugin->uniqueID;
330         
331         info->Category = strdup("None");          // FIXME:  
332         info->numInputs = plugin->numInputs;
333         info->numOutputs = plugin->numOutputs;
334         info->numParams = plugin->numParams;
335         info->wantMidi = vstfx_can_midi(vstfx); 
336         info->hasEditor = plugin->flags & effFlagsHasEditor ? true : false;
337         info->canProcessReplacing = plugin->flags & effFlagsCanReplacing ? true : false;
338         info->ParamNames = (char **) malloc(sizeof(char*)*info->numParams);
339         info->ParamLabels = (char **) malloc(sizeof(char*)*info->numParams);
340
341         for (int i = 0; i < info->numParams; ++i) {
342                 char name[64];
343                 char label[64];
344                 
345                 /*Not all plugins give parameters labels as well as names*/
346                 
347                 strcpy(name, "No Name");
348                 strcpy(label, "No Label");
349                 
350                 plugin->dispatcher (plugin, effGetParamName, i, 0, name, 0);
351                 info->ParamNames[i] = strdup(name);
352                 
353                 //NOTE: 'effGetParamLabel' is no longer defined in vestige headers
354                 //plugin->dispatcher (plugin, effGetParamLabel, i, 0, label, 0);
355                 info->ParamLabels[i] = strdup(label);
356         }
357         return info;
358 }
359
360 /* A simple 'dummy' audiomaster callback which should be ok,
361 we will only be instantiating the plugin in order to get its info*/
362
363 static intptr_t
364 simple_master_callback (AEffect *, int32_t opcode, int32_t, intptr_t, void *, float)
365 {
366         if (opcode == audioMasterVersion)
367                 return 2;
368         else
369                 return 0;
370 }
371
372 /*Try to get plugin info - first by looking for a .fsi cache of the
373 data, and if that doesn't exist, load the plugin, get its data and
374 then cache it for future ref*/
375
376 VSTInfo *
377 vstfx_get_info (char* dllpath)
378 {
379         FILE* infofile;
380         VSTHandle* h;
381         VSTState* vstfx;
382         VSTInfo* info;
383
384         if ((infofile = vstfx_infofile_for_read (dllpath)) != 0) {
385                 VSTInfo *info;
386                 info = load_vstfx_info_file (infofile);
387                 fclose (infofile);
388                 if (info == 0) {
389                         PBD::warning << "Cannot get LinuxVST information form " << dllpath << ": info file load failed." << endmsg;
390                 }
391                 return info;
392         } 
393         
394         if(!(h = vstfx_load(dllpath))) {
395                 PBD::warning << "Cannot get LinuxVST information from " << dllpath << ": load failed." << endmsg;
396                 return 0;
397         }
398         
399         if(!(vstfx = vstfx_instantiate(h, simple_master_callback, 0))) {
400                 vstfx_unload(h);
401                 PBD::warning << "Cannot get LinuxVST information from " << dllpath << ": instantiation failed." << endmsg;
402                 return 0;
403         }
404         
405         infofile = vstfx_infofile_for_write (dllpath);
406         
407         if(!infofile) {
408                 vstfx_close(vstfx);
409                 vstfx_unload(h);
410                 PBD::warning << "Cannot get LinuxVST information from " << dllpath << ": cannot create new FST info file." << endmsg;
411                 return 0;
412         }
413         
414         info = vstfx_info_from_plugin(vstfx);
415         
416         save_vstfx_info_file(info, infofile);
417         fclose (infofile);
418         
419         vstfx_close(vstfx);
420         vstfx_unload(h);
421         
422         return info;
423 }
424
425 void
426 vstfx_free_info (VSTInfo *info)
427 {
428         for (int i = 0; i < info->numParams; i++) {
429                 free (info->ParamNames[i]);
430                 free (info->ParamLabels[i]);
431         }
432         
433         free (info->name);
434         free (info->creator);
435         free (info->Category);
436         free (info);
437 }
438
439