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