correctly forward configure events to plugin windows, and cleanup FST code
[ardour.git] / libs / fst / fstinfofile.c
1
2 #include "fst.h"
3 #include "vst/aeffectx.h"
4
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <unistd.h>
8
9 #include <stddef.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
13
14 #define MAX_STRING_LEN 256
15
16 #define FALSE 0
17 #define TRUE !FALSE
18
19 static char *read_string( FILE *fp ) {
20     char buf[MAX_STRING_LEN];
21
22     fgets( buf, MAX_STRING_LEN, fp );
23     if( strlen( buf ) < MAX_STRING_LEN ) {
24         
25         if( strlen(buf) )
26             buf[strlen(buf)-1] = 0;
27
28         return strdup( buf );
29     } else {
30         return NULL;
31     }
32 }
33
34 static FSTInfo *load_fst_info_file( char *filename ) {
35
36     FSTInfo *info = (FSTInfo *) malloc( sizeof( FSTInfo ) );
37     FILE *fp;
38     int i;
39
40
41     if( info == NULL )
42         return NULL;
43
44     fp = fopen( filename, "r" );
45     
46     if( fp == NULL ) {
47         free( info );
48         return NULL;
49     }
50
51     if( (info->name = read_string( fp )) == NULL ) goto error;
52     if( 1 != fscanf( fp, "%d\n", &info->UniqueID ) ) goto error;
53     if( (info->Category = read_string( fp )) == NULL ) goto error;
54     if( 1 != fscanf( fp, "%d\n", &info->numInputs ) ) goto error;
55     if( 1 != fscanf( fp, "%d\n", &info->numOutputs ) ) goto error;
56     if( 1 != fscanf( fp, "%d\n", &info->numParams ) ) goto error;
57     if( 1 != fscanf( fp, "%d\n", &info->wantMidi ) ) goto error;
58     if( 1 != fscanf( fp, "%d\n", &info->hasEditor ) ) goto error;
59     if( 1 != fscanf( fp, "%d\n", &info->canProcessReplacing ) ) goto error;
60
61     if( (info->ParamNames = (char **) malloc( sizeof( char * ) * info->numParams )) == NULL ) goto error;
62     for( i=0; i<info->numParams; i++ ) {
63         if( (info->ParamNames[i] = read_string( fp )) == NULL ) goto error;
64     }
65     if( (info->ParamLabels = (char **) malloc( sizeof( char * ) * info->numParams )) == NULL ) goto error;
66     for( i=0; i<info->numParams; i++ ) {
67         if( (info->ParamLabels[i] = read_string( fp )) == NULL ) goto error;
68     }
69         
70
71     fclose( fp );
72     return info;
73
74 error:
75     fclose( fp );
76     free( info );
77     return NULL;
78 }
79
80 static int save_fst_info_file( FSTInfo *info, char *filename ) {
81
82     FILE *fp;
83     int i;
84
85
86     if( info == NULL ) {
87         fst_error( "info is NULL\n" );
88         return TRUE;
89     }
90
91     fp = fopen( filename, "w" );
92     
93     if( fp == NULL ) {
94         fst_error( "Cant write info file %s\n", filename );
95         return TRUE;
96     }
97
98     fprintf( fp, "%s\n", info->name );
99     fprintf( fp, "%d\n", info->UniqueID );
100     fprintf( fp, "%s\n", info->Category );
101     fprintf( fp, "%d\n", info->numInputs );
102     fprintf( fp, "%d\n", info->numOutputs );
103     fprintf( fp, "%d\n", info->numParams );
104     fprintf( fp, "%d\n", info->wantMidi );
105     fprintf( fp, "%d\n", info->hasEditor );
106     fprintf( fp, "%d\n", info->canProcessReplacing );
107
108     for( i=0; i<info->numParams; i++ ) {
109         fprintf( fp, "%s\n", info->ParamNames[i] );
110     }
111     for( i=0; i<info->numParams; i++ ) {
112         fprintf( fp, "%s\n", info->ParamLabels[i] );
113     }
114         
115
116     fclose( fp );
117
118     return FALSE;
119 }
120
121 static char *fst_dllpath_to_infopath( char *dllpath ) {
122     char *retval;
123     if( strstr( dllpath, ".dll" ) == NULL ) return NULL;
124     
125     retval = strdup( dllpath );
126     sprintf( retval + strlen(retval) - 4, ".fst" );
127     return retval;
128 }
129
130 static int fst_info_file_is_valid( char *dllpath ) {
131     struct stat dllstat, fststat;
132     char *fstpath = fst_dllpath_to_infopath( dllpath );
133
134     if( !fstpath ) return FALSE;
135     
136     if( stat( dllpath, &dllstat ) ){ fst_error( "dll path %s invalid\n", dllpath );  return TRUE; }
137     if( stat( fstpath, &fststat ) ) return FALSE;
138
139     free( fstpath );
140     if( dllstat.st_mtime > fststat.st_mtime )
141         return FALSE;
142     else 
143         return TRUE;
144 }
145
146 static int fst_can_midi( FST *fst ) {
147         AEffect *plugin = fst->plugin;
148         int vst_version = plugin->dispatcher (plugin, effGetVstVersion, 0, 0, NULL, 0.0f);
149
150         if (vst_version >= 2) {
151                 
152                 /* should we send it VST events (i.e. MIDI) */
153                 
154                 if ((plugin->flags & effFlagsIsSynth) ||
155                     (plugin->dispatcher (plugin, effCanDo, 0, 0,(void*) "receiveVstEvents", 0.0f) > 0))
156                     return TRUE;
157         }
158         return FALSE;
159
160 }
161 static FSTInfo *fst_info_from_plugin( FST *fst ) {
162     FSTInfo *info = (FSTInfo *) malloc( sizeof( FSTInfo ) );
163     AEffect *plugin;
164     int i;
165
166     if( ! fst ) {
167         fst_error( "fst is NULL\n" );
168         return NULL;
169     }
170
171     if( ! info ) return NULL;
172     
173     plugin = fst->plugin;
174     
175
176     info->name = strdup(fst->handle->name ); 
177     info->UniqueID = plugin->uniqueID;
178     info->Category = strdup( "None" );          // FIXME:  
179     info->numInputs = plugin->numInputs;
180     info->numOutputs = plugin->numOutputs;
181     info->numParams = plugin->numParams;
182     info->wantMidi = fst_can_midi( fst ); 
183     info->hasEditor = plugin->flags & effFlagsHasEditor ? TRUE : FALSE;
184     info->canProcessReplacing = plugin->flags & effFlagsCanReplacing ? TRUE : FALSE;
185
186     info->ParamNames = (char **) malloc( sizeof( char * ) * info->numParams );
187     info->ParamLabels = (char **) malloc( sizeof( char * ) * info->numParams );
188     for( i=0; i<info->numParams; i++ ) {
189         char name[20];
190         char label[9];
191                 plugin->dispatcher (plugin,
192                                     effGetParamName,
193                                     i, 0, name, 0);
194                 
195                 plugin->dispatcher (plugin,
196                                     effGetParamLabel,
197                                     i, 0, label, 0);
198
199         info->ParamNames[i] = strdup( name );
200         info->ParamLabels[i] = strdup( label );
201     }
202     return info;
203 }
204
205 // most simple one :) could be sufficient.... 
206 static long simple_master_callback( AEffect *fx, long opcode, long index, long value, void *ptr, float opt ) {
207     if( opcode == audioMasterVersion )
208         return 2;
209     else
210         return 0;
211 }
212
213 FSTInfo *fst_get_info( char *dllpath ) {
214
215     if( fst_info_file_is_valid( dllpath ) ) {
216         FSTInfo *info;
217         char *fstpath = fst_dllpath_to_infopath( dllpath );
218
219         info = load_fst_info_file( fstpath );
220         free( fstpath );
221         return info;
222
223     } else {
224
225         FSTHandle *h;
226         FST *fst;
227         FSTInfo *info;
228         char *fstpath;
229
230         if( !(h = fst_load( dllpath )) ) return NULL;
231         if( !(fst = fst_instantiate( h, simple_master_callback, NULL )) ) {
232             fst_unload( h );
233             fst_error( "instantiate failed\n" );
234             return NULL;
235         }
236         fstpath = fst_dllpath_to_infopath( dllpath );
237         if( !fstpath ) {
238             fst_close( fst );
239             fst_unload( h );
240             fst_error( "get fst filename failed\n" );
241             return NULL;
242         }
243         info = fst_info_from_plugin( fst );
244         save_fst_info_file( info, fstpath );
245
246         free( fstpath );
247         fst_close( fst );
248         fst_unload( h );
249         return info;
250     }
251 }
252
253 void fst_free_info( FSTInfo *info ) {
254
255     int i;
256
257     for( i=0; i<info->numParams; i++ ) {
258         free( info->ParamNames[i] );
259         free( info->ParamLabels[i] );
260     }
261     free( info->name );
262     free( info->Category );
263     free( info );
264 }
265
266