]> git.sesse.net Git - mlt/blob - src/modules/jackrack/plugin_mgr.c
Load frei0r and ladspa plugins relative to exe on win32
[mlt] / src / modules / jackrack / plugin_mgr.c
1 /*
2  * JACK Rack
3  *
4  * Original:
5  * Copyright (C) Robert Ham 2002, 2003 (node@users.sourceforge.net)
6  *
7  * Modification for MLT:
8  * Copyright (C) 2004 Ushodaya Enterprises Limited
9  * Author: Dan Dennedy <dan@dennedy.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  */
25
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include <dirent.h>
30 #include <errno.h>
31 #include <sys/stat.h>
32 #include <dlfcn.h>
33 #include <math.h>
34 #include <strings.h>
35 #include <ctype.h>
36 #include <ladspa.h>
37 #include <sys/types.h>
38 #include <unistd.h>
39
40 #include "plugin_mgr.h"
41 #include "plugin_desc.h"
42 #include "framework/mlt_log.h"
43 #include "framework/mlt_factory.h"
44
45 static gboolean
46 plugin_is_valid (const LADSPA_Descriptor * descriptor)
47 {
48   unsigned long i;
49   unsigned long icount = 0;
50   unsigned long ocount = 0;
51   
52   for (i = 0; i < descriptor->PortCount; i++)
53     {
54       if (!LADSPA_IS_PORT_AUDIO (descriptor->PortDescriptors[i]))
55         continue;
56       
57       if (LADSPA_IS_PORT_INPUT (descriptor->PortDescriptors[i]))
58         icount++;
59       else
60         ocount++;
61     }
62   
63   if (icount == 0 || ocount == 0)
64     return FALSE;
65   
66   return TRUE;
67 }
68
69 static void
70 plugin_mgr_get_object_file_plugins (plugin_mgr_t * plugin_mgr, const char * filename)
71 {
72   const char * dlerr;
73   void * dl_handle;
74   LADSPA_Descriptor_Function get_descriptor;
75   const LADSPA_Descriptor * descriptor;
76   unsigned long plugin_index;
77   plugin_desc_t * desc, * other_desc = NULL;
78   GSList * list;
79   gboolean exists;
80   int err;
81   
82   /* open the object file */
83   dl_handle = dlopen (filename, RTLD_NOW|RTLD_GLOBAL);
84   if (!dl_handle)
85     {
86       mlt_log_warning( NULL, "%s: error opening shared object file '%s': %s\n",
87                __FUNCTION__, filename, dlerror());
88       return;
89     }
90   
91   
92   /* get the get_descriptor function */
93   dlerror (); /* clear the error report */
94   
95   get_descriptor = (LADSPA_Descriptor_Function)
96     dlsym (dl_handle, "ladspa_descriptor");
97   
98   dlerr = dlerror();
99   if (dlerr) {
100     mlt_log_warning( NULL, "%s: error finding ladspa_descriptor symbol in object file '%s': %s\n",
101              __FUNCTION__, filename, dlerr);
102     dlclose (dl_handle);
103     return;
104   }
105   
106   plugin_index = 0;
107   while ( (descriptor = get_descriptor (plugin_index)) )
108     {
109       if (!plugin_is_valid (descriptor))
110         {
111           plugin_index++;
112           continue;
113         }
114
115       
116       /* check it doesn't already exist */
117       exists = FALSE;
118       for (list = plugin_mgr->all_plugins; list; list = g_slist_next (list))
119         {
120           other_desc = (plugin_desc_t *) list->data;
121           
122           if (other_desc->id == descriptor->UniqueID)
123             {
124               exists = TRUE;
125               break;
126             }
127         }
128       
129       if (exists)
130         {
131           mlt_log_info( NULL, "Plugin %ld exists in both '%s' and '%s'; using version in '%s'\n",
132                   descriptor->UniqueID, other_desc->object_file, filename, other_desc->object_file);
133           plugin_index++;
134           continue;
135         }
136
137       
138       desc = plugin_desc_new_with_descriptor (filename, plugin_index, descriptor);
139       plugin_mgr->all_plugins = g_slist_append (plugin_mgr->all_plugins, desc);
140       plugin_index++;
141       plugin_mgr->plugin_count++;
142       
143       /* print in the splash screen */
144       /* mlt_log_verbose( NULL, "Loaded plugin '%s'\n", desc->name); */
145     }
146   
147   err = dlclose (dl_handle);
148   if (err)
149     {
150       mlt_log_warning( NULL, "%s: error closing object file '%s': %s\n",
151                __FUNCTION__, filename, dlerror ());
152     }
153 }
154
155 static void
156 plugin_mgr_get_dir_plugins (plugin_mgr_t * plugin_mgr, const char * dir)
157 {
158   DIR * dir_stream;
159   struct dirent * dir_entry;
160   char * file_name;
161   int err;
162   size_t dirlen;
163   
164   dir_stream = opendir (dir);
165   if (!dir_stream)
166     {
167 /*      mlt_log_warning( NULL, "%s: error opening directory '%s': %s\n",
168                __FUNCTION__, dir, strerror (errno)); */
169       return;
170     }
171   
172   dirlen = strlen (dir);
173   
174   while ( (dir_entry = readdir (dir_stream)) )
175     {
176       struct stat info;
177
178       if (strcmp (dir_entry->d_name, ".") == 0 ||
179           mlt_properties_get (plugin_mgr->blacklist, dir_entry->d_name) ||
180           strcmp (dir_entry->d_name, "..") == 0)
181         continue;
182   
183       file_name = g_malloc (dirlen + 1 + strlen (dir_entry->d_name) + 1);
184     
185       strcpy (file_name, dir);
186       if (file_name[dirlen - 1] == '/')
187         strcpy (file_name + dirlen, dir_entry->d_name);
188       else
189         {
190           file_name[dirlen] = '/';
191           strcpy (file_name + dirlen + 1, dir_entry->d_name);
192         }
193     
194       stat (file_name, &info);
195       if (S_ISDIR (info.st_mode))
196         plugin_mgr_get_dir_plugins (plugin_mgr, file_name);
197       else
198         plugin_mgr_get_object_file_plugins (plugin_mgr, file_name);
199       
200       g_free (file_name);
201     }
202
203   err = closedir (dir_stream);
204   if (err)
205     mlt_log_warning( NULL, "%s: error closing directory '%s': %s\n",
206              __FUNCTION__, dir, strerror (errno));
207 }
208
209 static void
210 plugin_mgr_get_path_plugins (plugin_mgr_t * plugin_mgr)
211 {
212   char * ladspa_path, * dir;
213   
214   ladspa_path = g_strdup (getenv ("LADSPA_PATH"));
215   if (!ladspa_path)
216 #ifdef WIN32
217   {
218     ladspa_path = malloc (strlen (mlt_environment("MLT_DATA")) + strlen ("\\..\\..\\lib\\ladspa") + 1);
219     strcpy (ladspa_path, mlt_environment("MLT_DATA"));
220     strcat (ladspa_path, "\\..\\..\\lib\\ladspa");
221     printf("LADSPA_PATH=%s\n", ladspa_path);
222   }
223 #else
224     ladspa_path = g_strdup ("lib/ladspa:/usr/local/lib/ladspa:/usr/lib/ladspa:/usr/lib64/ladspa");
225 #endif
226   
227   dir = strtok (ladspa_path, ":");
228   do
229     plugin_mgr_get_dir_plugins (plugin_mgr, dir);
230   while ((dir = strtok (NULL, ":")));
231
232   g_free (ladspa_path);
233 }
234
235 static gint
236 plugin_mgr_sort (gconstpointer a, gconstpointer b)
237 {
238   const plugin_desc_t * da;
239   const plugin_desc_t * db;
240   da = (const plugin_desc_t *) a;
241   db = (const plugin_desc_t *) b;
242   
243   return strcasecmp (da->name, db->name);
244 }
245
246 plugin_mgr_t *
247 plugin_mgr_new ()
248 {
249   plugin_mgr_t * pm;
250   char dirname[PATH_MAX];
251
252   pm = g_malloc (sizeof (plugin_mgr_t));
253   pm->all_plugins = NULL;  
254   pm->plugins = NULL;
255   pm->plugin_count = 0;
256
257   snprintf (dirname, PATH_MAX, "%s/jackrack/blacklist.txt", mlt_environment ("MLT_DATA"));
258   pm->blacklist = mlt_properties_load (dirname);
259   plugin_mgr_get_path_plugins (pm);
260   
261   if (!pm->all_plugins)
262     mlt_log_warning( NULL, "No LADSPA plugins were found!\n\nCheck your LADSPA_PATH environment variable.\n");
263   else
264     pm->all_plugins = g_slist_sort (pm->all_plugins, plugin_mgr_sort);
265   
266   return pm;
267 }
268
269 void
270 plugin_mgr_destroy (plugin_mgr_t * plugin_mgr)
271 {
272   GSList * list;
273   
274   for (list = plugin_mgr->all_plugins; list; list = g_slist_next (list))
275     plugin_desc_destroy ((plugin_desc_t *) list->data);
276   
277   g_slist_free (plugin_mgr->plugins);
278   g_slist_free (plugin_mgr->all_plugins);
279   free (plugin_mgr);
280 }
281
282
283 void
284 plugin_mgr_set_plugins (plugin_mgr_t * plugin_mgr, unsigned long rack_channels)
285 {
286   GSList * list;
287   plugin_desc_t * desc;
288
289   /* clear the current plugins */
290   g_slist_free (plugin_mgr->plugins);
291   plugin_mgr->plugins = NULL;
292   
293   for (list = plugin_mgr->all_plugins; list; list = g_slist_next (list))
294     {
295       desc = (plugin_desc_t *) list->data;
296       
297       if (plugin_desc_get_copies (desc, rack_channels) != 0)
298         plugin_mgr->plugins = g_slist_append (plugin_mgr->plugins, desc);
299     }
300 }
301
302 static plugin_desc_t *
303 plugin_mgr_find_desc (plugin_mgr_t * plugin_mgr, GSList * plugins, unsigned long id)
304 {
305   GSList * list;
306   plugin_desc_t * desc;
307   
308   for (list = plugins; list; list = g_slist_next (list))
309     {
310       desc = (plugin_desc_t *) list->data;
311       
312       if (desc->id == id)
313         return desc;
314     }
315   
316   return NULL;
317 }
318
319 plugin_desc_t *
320 plugin_mgr_get_desc (plugin_mgr_t * plugin_mgr, unsigned long id)
321 {
322   return plugin_mgr_find_desc (plugin_mgr, plugin_mgr->plugins, id);
323 }
324
325 plugin_desc_t *
326 plugin_mgr_get_any_desc (plugin_mgr_t * plugin_mgr, unsigned long id)
327 {
328   return plugin_mgr_find_desc (plugin_mgr, plugin_mgr->all_plugins, id);
329 }
330
331
332 /* EOF */