]> git.sesse.net Git - mlt/blob - src/modules/jackrack/plugin_mgr.c
Merge ../mlt++
[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
43
44 static gboolean
45 plugin_is_valid (const LADSPA_Descriptor * descriptor)
46 {
47   unsigned long i;
48   unsigned long icount = 0;
49   unsigned long ocount = 0;
50   
51   for (i = 0; i < descriptor->PortCount; i++)
52     {
53       if (!LADSPA_IS_PORT_AUDIO (descriptor->PortDescriptors[i]))
54         continue;
55       
56       if (LADSPA_IS_PORT_INPUT (descriptor->PortDescriptors[i]))
57         icount++;
58       else
59         ocount++;
60     }
61   
62   if (icount == 0 || ocount == 0)
63     return FALSE;
64   
65   return TRUE;
66 }
67
68 static void
69 plugin_mgr_get_object_file_plugins (plugin_mgr_t * plugin_mgr, const char * filename)
70 {
71   const char * dlerr;
72   void * dl_handle;
73   LADSPA_Descriptor_Function get_descriptor;
74   const LADSPA_Descriptor * descriptor;
75   unsigned long plugin_index;
76   plugin_desc_t * desc, * other_desc = NULL;
77   GSList * list;
78   gboolean exists;
79   int err;
80   
81   /* open the object file */
82   dl_handle = dlopen (filename, RTLD_NOW|RTLD_GLOBAL);
83   if (!dl_handle)
84     {
85       fprintf (stderr, "%s: error opening shared object file '%s': %s\n",
86                __FUNCTION__, filename, dlerror());
87       return;
88     }
89   
90   
91   /* get the get_descriptor function */
92   dlerror (); /* clear the error report */
93   
94   get_descriptor = (LADSPA_Descriptor_Function)
95     dlsym (dl_handle, "ladspa_descriptor");
96   
97   dlerr = dlerror();
98   if (dlerr) {
99     fprintf (stderr, "%s: error finding ladspa_descriptor symbol in object file '%s': %s\n",
100              __FUNCTION__, filename, dlerr);
101     dlclose (dl_handle);
102     return;
103   }
104   
105   plugin_index = 0;
106   while ( (descriptor = get_descriptor (plugin_index)) )
107     {
108       if (!plugin_is_valid (descriptor))
109         {
110           plugin_index++;
111           continue;
112         }
113
114       
115       /* check it doesn't already exist */
116       exists = FALSE;
117       for (list = plugin_mgr->all_plugins; list; list = g_slist_next (list))
118         {
119           other_desc = (plugin_desc_t *) list->data;
120           
121           if (other_desc->id == descriptor->UniqueID)
122             {
123               exists = TRUE;
124               break;
125             }
126         }
127       
128       if (exists)
129         {
130           printf ("Plugin %ld exists in both '%s' and '%s'; using version in '%s'\n",
131                   descriptor->UniqueID, other_desc->object_file, filename, other_desc->object_file);
132           plugin_index++;
133           continue;
134         }
135
136       
137       desc = plugin_desc_new_with_descriptor (filename, plugin_index, descriptor);
138       plugin_mgr->all_plugins = g_slist_append (plugin_mgr->all_plugins, desc);
139       plugin_index++;
140       plugin_mgr->plugin_count++;
141       
142       /* print in the splash screen */
143       /* printf ("Loaded plugin '%s'\n", desc->name); */
144     }
145   
146   err = dlclose (dl_handle);
147   if (err)
148     {
149       fprintf (stderr, "%s: error closing object file '%s': %s\n",
150                __FUNCTION__, filename, dlerror ());
151     }
152 }
153
154 static void
155 plugin_mgr_get_dir_plugins (plugin_mgr_t * plugin_mgr, const char * dir)
156 {
157   DIR * dir_stream;
158   struct dirent * dir_entry;
159   char * file_name;
160   int err;
161   size_t dirlen;
162   
163   dir_stream = opendir (dir);
164   if (!dir_stream)
165     {
166 /*      fprintf (stderr, "%s: error opening directory '%s': %s\n",
167                __FUNCTION__, dir, strerror (errno)); */
168       return;
169     }
170   
171   dirlen = strlen (dir);
172   
173   while ( (dir_entry = readdir (dir_stream)) )
174     {
175       struct stat info;
176
177       if (strcmp (dir_entry->d_name, ".") == 0 ||
178           strcmp (dir_entry->d_name, "..") == 0)
179         continue;
180   
181       file_name = g_malloc (dirlen + 1 + strlen (dir_entry->d_name) + 1);
182     
183       strcpy (file_name, dir);
184       if (file_name[dirlen - 1] == '/')
185         strcpy (file_name + dirlen, dir_entry->d_name);
186       else
187         {
188           file_name[dirlen] = '/';
189           strcpy (file_name + dirlen + 1, dir_entry->d_name);
190         }
191     
192       stat (file_name, &info);
193       if (S_ISDIR (info.st_mode))
194         plugin_mgr_get_dir_plugins (plugin_mgr, file_name);
195       else
196         plugin_mgr_get_object_file_plugins (plugin_mgr, file_name);
197       
198       g_free (file_name);
199     }
200
201   err = closedir (dir_stream);
202   if (err)
203     fprintf (stderr, "%s: error closing directory '%s': %s\n",
204              __FUNCTION__, dir, strerror (errno));
205 }
206
207 static void
208 plugin_mgr_get_path_plugins (plugin_mgr_t * plugin_mgr)
209 {
210   char * ladspa_path, * dir;
211   
212   ladspa_path = g_strdup (getenv ("LADSPA_PATH"));
213   if (!ladspa_path)
214     ladspa_path = g_strdup ("/usr/local/lib/ladspa:/usr/lib/ladspa:/usr/lib64/ladspa");
215   
216   dir = strtok (ladspa_path, ":");
217   do
218     plugin_mgr_get_dir_plugins (plugin_mgr, dir);
219   while ((dir = strtok (NULL, ":")));
220
221   g_free (ladspa_path);
222 }
223
224 static gint
225 plugin_mgr_sort (gconstpointer a, gconstpointer b)
226 {
227   const plugin_desc_t * da;
228   const plugin_desc_t * db;
229   da = (const plugin_desc_t *) a;
230   db = (const plugin_desc_t *) b;
231   
232   return strcasecmp (da->name, db->name);
233 }
234
235 plugin_mgr_t *
236 plugin_mgr_new ()
237 {
238   plugin_mgr_t * pm;
239   
240   pm = g_malloc (sizeof (plugin_mgr_t));
241   pm->all_plugins = NULL;  
242   pm->plugins = NULL;
243   pm->plugin_count = 0;
244   
245   plugin_mgr_get_path_plugins (pm);
246   
247   if (!pm->all_plugins)
248     {
249       fprintf (stderr, "No LADSPA plugins were found!\n\nCheck your LADSPA_PATH environment variable.\n");
250       abort ();
251     }
252   
253   pm->all_plugins = g_slist_sort (pm->all_plugins, plugin_mgr_sort);
254   
255   return pm;
256 }
257
258 void
259 plugin_mgr_destroy (plugin_mgr_t * plugin_mgr)
260 {
261   GSList * list;
262   
263   for (list = plugin_mgr->all_plugins; list; list = g_slist_next (list))
264     plugin_desc_destroy ((plugin_desc_t *) list->data);
265   
266   g_slist_free (plugin_mgr->plugins);
267   g_slist_free (plugin_mgr->all_plugins);
268   free (plugin_mgr);
269 }
270
271
272 void
273 plugin_mgr_set_plugins (plugin_mgr_t * plugin_mgr, unsigned long rack_channels)
274 {
275   GSList * list;
276   plugin_desc_t * desc;
277
278   /* clear the current plugins */
279   g_slist_free (plugin_mgr->plugins);
280   plugin_mgr->plugins = NULL;
281   
282   for (list = plugin_mgr->all_plugins; list; list = g_slist_next (list))
283     {
284       desc = (plugin_desc_t *) list->data;
285       
286       if (plugin_desc_get_copies (desc, rack_channels) != 0)
287         plugin_mgr->plugins = g_slist_append (plugin_mgr->plugins, desc);
288     }
289 }
290
291 static plugin_desc_t *
292 plugin_mgr_find_desc (plugin_mgr_t * plugin_mgr, GSList * plugins, unsigned long id)
293 {
294   GSList * list;
295   plugin_desc_t * desc;
296   
297   for (list = plugins; list; list = g_slist_next (list))
298     {
299       desc = (plugin_desc_t *) list->data;
300       
301       if (desc->id == id)
302         return desc;
303     }
304   
305   return NULL;
306 }
307
308 plugin_desc_t *
309 plugin_mgr_get_desc (plugin_mgr_t * plugin_mgr, unsigned long id)
310 {
311   return plugin_mgr_find_desc (plugin_mgr, plugin_mgr->plugins, id);
312 }
313
314 plugin_desc_t *
315 plugin_mgr_get_any_desc (plugin_mgr_t * plugin_mgr, unsigned long id)
316 {
317   return plugin_mgr_find_desc (plugin_mgr, plugin_mgr->all_plugins, id);
318 }
319
320
321 /* EOF */