]> git.sesse.net Git - mlt/blob - src/modules/jackrack/plugin_mgr.c
a5842105c565dd6ab490e09e765703c5a79df8ea
[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);
84   if (!dl_handle)
85     {
86       mlt_log_info( 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_info( 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 #ifdef __DARWIN__
107   if (!get_descriptor (0)) {
108     void (*constructor)(void) = dlsym (dl_handle, "_init");
109     if (constructor) constructor();
110   }
111 #endif
112
113   plugin_index = 0;
114   while ( (descriptor = get_descriptor (plugin_index)) )
115     {
116       if (!plugin_is_valid (descriptor))
117         {
118           plugin_index++;
119           continue;
120         }
121
122       
123       /* check it doesn't already exist */
124       exists = FALSE;
125       for (list = plugin_mgr->all_plugins; list; list = g_slist_next (list))
126         {
127           other_desc = (plugin_desc_t *) list->data;
128           
129           if (other_desc->id == descriptor->UniqueID)
130             {
131               exists = TRUE;
132               break;
133             }
134         }
135       
136       if (exists)
137         {
138           mlt_log_info( NULL, "Plugin %ld exists in both '%s' and '%s'; using version in '%s'\n",
139                   descriptor->UniqueID, other_desc->object_file, filename, other_desc->object_file);
140           plugin_index++;
141           continue;
142         }
143
144       
145       desc = plugin_desc_new_with_descriptor (filename, plugin_index, descriptor);
146       plugin_mgr->all_plugins = g_slist_append (plugin_mgr->all_plugins, desc);
147       plugin_index++;
148       plugin_mgr->plugin_count++;
149       
150       /* print in the splash screen */
151       /* mlt_log_verbose( NULL, "Loaded plugin '%s'\n", desc->name); */
152     }
153   
154   err = dlclose (dl_handle);
155   if (err)
156     {
157       mlt_log_warning( NULL, "%s: error closing object file '%s': %s\n",
158                __FUNCTION__, filename, dlerror ());
159     }
160 }
161
162 static void
163 plugin_mgr_get_dir_plugins (plugin_mgr_t * plugin_mgr, const char * dir)
164 {
165   DIR * dir_stream;
166   struct dirent * dir_entry;
167   char * file_name;
168   int err;
169   size_t dirlen;
170   
171   dir_stream = opendir (dir);
172   if (!dir_stream)
173     {
174 /*      mlt_log_warning( NULL, "%s: error opening directory '%s': %s\n",
175                __FUNCTION__, dir, strerror (errno)); */
176       return;
177     }
178   
179   dirlen = strlen (dir);
180   
181   while ( (dir_entry = readdir (dir_stream)) )
182     {
183       struct stat info;
184
185       if (strcmp (dir_entry->d_name, ".") == 0 ||
186           mlt_properties_get (plugin_mgr->blacklist, dir_entry->d_name) ||
187           strcmp (dir_entry->d_name, "..") == 0)
188         continue;
189   
190       file_name = g_malloc (dirlen + 1 + strlen (dir_entry->d_name) + 1);
191     
192       strcpy (file_name, dir);
193       if (file_name[dirlen - 1] == '/')
194         strcpy (file_name + dirlen, dir_entry->d_name);
195       else
196         {
197           file_name[dirlen] = '/';
198           strcpy (file_name + dirlen + 1, dir_entry->d_name);
199         }
200     
201       stat (file_name, &info);
202       if (S_ISDIR (info.st_mode))
203         plugin_mgr_get_dir_plugins (plugin_mgr, file_name);
204       else
205         plugin_mgr_get_object_file_plugins (plugin_mgr, file_name);
206       
207       g_free (file_name);
208     }
209
210   err = closedir (dir_stream);
211   if (err)
212     mlt_log_warning( NULL, "%s: error closing directory '%s': %s\n",
213              __FUNCTION__, dir, strerror (errno));
214 }
215
216 static void
217 plugin_mgr_get_path_plugins (plugin_mgr_t * plugin_mgr)
218 {
219   char * ladspa_path, * dir;
220   
221   ladspa_path = g_strdup (getenv ("LADSPA_PATH"));
222 #ifdef WIN32
223   if (!ladspa_path)
224   {
225     ladspa_path = malloc (strlen (mlt_environment("MLT_APPDIR")) + strlen ("\\lib\\ladspa") + 1);
226     strcpy (ladspa_path, mlt_environment("MLT_APPDIR"));
227     strcat (ladspa_path, "\\lib\\ladspa");
228   }
229 #elif defined(__DARWIN__) && defined(RELOCATABLE)
230   {
231     ladspa_path = malloc( strlen (mlt_environment ("MLT_APPDIR")) + strlen ("/lib/ladspa") + 1 );
232     strcpy (ladspa_path,  mlt_environment ("MLT_APPDIR"));
233     strcat (ladspa_path, "/lib/ladspa" );
234   }
235 #else
236   if (!ladspa_path)
237     ladspa_path = g_strdup ("/usr/local/lib/ladspa:/usr/lib/ladspa:/usr/lib64/ladspa");
238 #endif
239   
240   dir = strtok (ladspa_path, ":");
241   do
242     plugin_mgr_get_dir_plugins (plugin_mgr, dir);
243   while ((dir = strtok (NULL, ":")));
244
245   g_free (ladspa_path);
246 }
247
248 static gint
249 plugin_mgr_sort (gconstpointer a, gconstpointer b)
250 {
251   const plugin_desc_t * da;
252   const plugin_desc_t * db;
253   da = (const plugin_desc_t *) a;
254   db = (const plugin_desc_t *) b;
255   
256   return strcasecmp (da->name, db->name);
257 }
258
259 plugin_mgr_t *
260 plugin_mgr_new ()
261 {
262   plugin_mgr_t * pm;
263   char dirname[PATH_MAX];
264
265   pm = g_malloc (sizeof (plugin_mgr_t));
266   pm->all_plugins = NULL;  
267   pm->plugins = NULL;
268   pm->plugin_count = 0;
269
270   snprintf (dirname, PATH_MAX, "%s/jackrack/blacklist.txt", mlt_environment ("MLT_DATA"));
271   pm->blacklist = mlt_properties_load (dirname);
272   plugin_mgr_get_path_plugins (pm);
273   
274   if (!pm->all_plugins)
275     mlt_log_warning( NULL, "No LADSPA plugins were found!\n\nCheck your LADSPA_PATH environment variable.\n");
276   else
277     pm->all_plugins = g_slist_sort (pm->all_plugins, plugin_mgr_sort);
278   
279   return pm;
280 }
281
282 void
283 plugin_mgr_destroy (plugin_mgr_t * plugin_mgr)
284 {
285   GSList * list;
286   
287   for (list = plugin_mgr->all_plugins; list; list = g_slist_next (list))
288     plugin_desc_destroy ((plugin_desc_t *) list->data);
289   
290   g_slist_free (plugin_mgr->plugins);
291   g_slist_free (plugin_mgr->all_plugins);
292   free (plugin_mgr);
293 }
294
295
296 void
297 plugin_mgr_set_plugins (plugin_mgr_t * plugin_mgr, unsigned long rack_channels)
298 {
299   GSList * list;
300   plugin_desc_t * desc;
301
302   /* clear the current plugins */
303   g_slist_free (plugin_mgr->plugins);
304   plugin_mgr->plugins = NULL;
305   
306   for (list = plugin_mgr->all_plugins; list; list = g_slist_next (list))
307     {
308       desc = (plugin_desc_t *) list->data;
309       
310       if (plugin_desc_get_copies (desc, rack_channels) != 0)
311         plugin_mgr->plugins = g_slist_append (plugin_mgr->plugins, desc);
312     }
313 }
314
315 static plugin_desc_t *
316 plugin_mgr_find_desc (plugin_mgr_t * plugin_mgr, GSList * plugins, unsigned long id)
317 {
318   GSList * list;
319   plugin_desc_t * desc;
320   
321   for (list = plugins; list; list = g_slist_next (list))
322     {
323       desc = (plugin_desc_t *) list->data;
324       
325       if (desc->id == id)
326         return desc;
327     }
328   
329   return NULL;
330 }
331
332 plugin_desc_t *
333 plugin_mgr_get_desc (plugin_mgr_t * plugin_mgr, unsigned long id)
334 {
335   return plugin_mgr_find_desc (plugin_mgr, plugin_mgr->plugins, id);
336 }
337
338 plugin_desc_t *
339 plugin_mgr_get_any_desc (plugin_mgr_t * plugin_mgr, unsigned long id)
340 {
341   return plugin_mgr_find_desc (plugin_mgr, plugin_mgr->all_plugins, id);
342 }
343
344
345 /* EOF */