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