5 * Copyright (C) Robert Ham 2002, 2003 (node@users.sourceforge.net)
7 * Modification for MLT:
8 * Copyright (C) 2004 Ushodaya Enterprises Limited
9 * Author: Dan Dennedy <dan@dennedy.org>
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.
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.
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.
37 #include <sys/types.h>
40 #include "plugin_mgr.h"
41 #include "plugin_desc.h"
42 #include "framework/mlt_log.h"
43 #include "framework/mlt_factory.h"
46 plugin_is_valid (const LADSPA_Descriptor * descriptor)
49 unsigned long icount = 0;
50 unsigned long ocount = 0;
52 for (i = 0; i < descriptor->PortCount; i++)
54 if (!LADSPA_IS_PORT_AUDIO (descriptor->PortDescriptors[i]))
57 if (LADSPA_IS_PORT_INPUT (descriptor->PortDescriptors[i]))
70 plugin_mgr_get_object_file_plugins (plugin_mgr_t * plugin_mgr, const char * filename)
74 LADSPA_Descriptor_Function get_descriptor;
75 const LADSPA_Descriptor * descriptor;
76 unsigned long plugin_index;
77 plugin_desc_t * desc, * other_desc = NULL;
82 /* open the object file */
83 dl_handle = dlopen (filename, RTLD_NOW);
86 mlt_log_info( NULL, "%s: error opening shared object file '%s': %s\n",
87 __FUNCTION__, filename, dlerror());
92 /* get the get_descriptor function */
93 dlerror (); /* clear the error report */
95 get_descriptor = (LADSPA_Descriptor_Function)
96 dlsym (dl_handle, "ladspa_descriptor");
100 mlt_log_info( NULL, "%s: error finding ladspa_descriptor symbol in object file '%s': %s\n",
101 __FUNCTION__, filename, dlerr);
107 if (!get_descriptor (0)) {
108 void (*constructor)(void) = dlsym (dl_handle, "_init");
109 if (constructor) constructor();
114 while ( (descriptor = get_descriptor (plugin_index)) )
116 if (!plugin_is_valid (descriptor))
123 /* check it doesn't already exist */
125 for (list = plugin_mgr->all_plugins; list; list = g_slist_next (list))
127 other_desc = (plugin_desc_t *) list->data;
129 if (other_desc->id == descriptor->UniqueID)
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);
145 desc = plugin_desc_new_with_descriptor (filename, plugin_index, descriptor);
146 plugin_mgr->all_plugins = g_slist_append (plugin_mgr->all_plugins, desc);
148 plugin_mgr->plugin_count++;
150 /* print in the splash screen */
151 /* mlt_log_verbose( NULL, "Loaded plugin '%s'\n", desc->name); */
154 err = dlclose (dl_handle);
157 mlt_log_warning( NULL, "%s: error closing object file '%s': %s\n",
158 __FUNCTION__, filename, dlerror ());
163 plugin_mgr_get_dir_plugins (plugin_mgr_t * plugin_mgr, const char * dir)
166 struct dirent * dir_entry;
171 dir_stream = opendir (dir);
174 /* mlt_log_warning( NULL, "%s: error opening directory '%s': %s\n",
175 __FUNCTION__, dir, strerror (errno)); */
179 dirlen = strlen (dir);
181 while ( (dir_entry = readdir (dir_stream)) )
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)
190 file_name = g_malloc (dirlen + 1 + strlen (dir_entry->d_name) + 1);
192 strcpy (file_name, dir);
193 if (file_name[dirlen - 1] == '/')
194 strcpy (file_name + dirlen, dir_entry->d_name);
197 file_name[dirlen] = '/';
198 strcpy (file_name + dirlen + 1, dir_entry->d_name);
201 stat (file_name, &info);
202 if (S_ISDIR (info.st_mode))
203 plugin_mgr_get_dir_plugins (plugin_mgr, file_name);
205 plugin_mgr_get_object_file_plugins (plugin_mgr, file_name);
210 err = closedir (dir_stream);
212 mlt_log_warning( NULL, "%s: error closing directory '%s': %s\n",
213 __FUNCTION__, dir, strerror (errno));
217 plugin_mgr_get_path_plugins (plugin_mgr_t * plugin_mgr)
219 char * ladspa_path, * dir;
221 ladspa_path = g_strdup (getenv ("LADSPA_PATH"));
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");
229 #elif defined(__DARWIN__) && defined(RELOCATABLE)
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" );
237 ladspa_path = g_strdup ("/usr/local/lib/ladspa:/usr/lib/ladspa:/usr/lib64/ladspa");
240 for (dir = strtok (ladspa_path, ":"); dir; dir = strtok (NULL, ":"))
241 plugin_mgr_get_dir_plugins (plugin_mgr, dir);
243 g_free (ladspa_path);
247 plugin_mgr_sort (gconstpointer a, gconstpointer b)
249 const plugin_desc_t * da;
250 const plugin_desc_t * db;
251 da = (const plugin_desc_t *) a;
252 db = (const plugin_desc_t *) b;
254 return strcasecmp (da->name, db->name);
261 char dirname[PATH_MAX];
263 pm = g_malloc (sizeof (plugin_mgr_t));
264 pm->all_plugins = NULL;
266 pm->plugin_count = 0;
268 snprintf (dirname, PATH_MAX, "%s/jackrack/blacklist.txt", mlt_environment ("MLT_DATA"));
269 pm->blacklist = mlt_properties_load (dirname);
270 plugin_mgr_get_path_plugins (pm);
272 if (!pm->all_plugins)
273 mlt_log_warning( NULL, "No LADSPA plugins were found!\n\nCheck your LADSPA_PATH environment variable.\n");
275 pm->all_plugins = g_slist_sort (pm->all_plugins, plugin_mgr_sort);
281 plugin_mgr_destroy (plugin_mgr_t * plugin_mgr)
285 for (list = plugin_mgr->all_plugins; list; list = g_slist_next (list))
286 plugin_desc_destroy ((plugin_desc_t *) list->data);
288 g_slist_free (plugin_mgr->plugins);
289 g_slist_free (plugin_mgr->all_plugins);
295 plugin_mgr_set_plugins (plugin_mgr_t * plugin_mgr, unsigned long rack_channels)
298 plugin_desc_t * desc;
300 /* clear the current plugins */
301 g_slist_free (plugin_mgr->plugins);
302 plugin_mgr->plugins = NULL;
304 for (list = plugin_mgr->all_plugins; list; list = g_slist_next (list))
306 desc = (plugin_desc_t *) list->data;
308 if (plugin_desc_get_copies (desc, rack_channels) != 0)
309 plugin_mgr->plugins = g_slist_append (plugin_mgr->plugins, desc);
313 static plugin_desc_t *
314 plugin_mgr_find_desc (plugin_mgr_t * plugin_mgr, GSList * plugins, unsigned long id)
317 plugin_desc_t * desc;
319 for (list = plugins; list; list = g_slist_next (list))
321 desc = (plugin_desc_t *) list->data;
331 plugin_mgr_get_desc (plugin_mgr_t * plugin_mgr, unsigned long id)
333 return plugin_mgr_find_desc (plugin_mgr, plugin_mgr->plugins, id);
337 plugin_mgr_get_any_desc (plugin_mgr_t * plugin_mgr, unsigned long id)
339 return plugin_mgr_find_desc (plugin_mgr, plugin_mgr->all_plugins, id);