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"
45 plugin_is_valid (const LADSPA_Descriptor * descriptor)
48 unsigned long icount = 0;
49 unsigned long ocount = 0;
51 for (i = 0; i < descriptor->PortCount; i++)
53 if (!LADSPA_IS_PORT_AUDIO (descriptor->PortDescriptors[i]))
56 if (LADSPA_IS_PORT_INPUT (descriptor->PortDescriptors[i]))
62 if (icount == 0 || ocount == 0)
69 plugin_mgr_get_object_file_plugins (plugin_mgr_t * plugin_mgr, const char * filename)
73 LADSPA_Descriptor_Function get_descriptor;
74 const LADSPA_Descriptor * descriptor;
75 unsigned long plugin_index;
76 plugin_desc_t * desc, * other_desc = NULL;
81 /* open the object file */
82 dl_handle = dlopen (filename, RTLD_NOW|RTLD_GLOBAL);
85 fprintf (stderr, "%s: error opening shared object file '%s': %s\n",
86 __FUNCTION__, filename, dlerror());
91 /* get the get_descriptor function */
92 dlerror (); /* clear the error report */
94 get_descriptor = (LADSPA_Descriptor_Function)
95 dlsym (dl_handle, "ladspa_descriptor");
99 fprintf (stderr, "%s: error finding ladspa_descriptor symbol in object file '%s': %s\n",
100 __FUNCTION__, filename, dlerr);
106 while ( (descriptor = get_descriptor (plugin_index)) )
108 if (!plugin_is_valid (descriptor))
115 /* check it doesn't already exist */
117 for (list = plugin_mgr->all_plugins; list; list = g_slist_next (list))
119 other_desc = (plugin_desc_t *) list->data;
121 if (other_desc->id == descriptor->UniqueID)
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);
137 desc = plugin_desc_new_with_descriptor (filename, plugin_index, descriptor);
138 plugin_mgr->all_plugins = g_slist_append (plugin_mgr->all_plugins, desc);
140 plugin_mgr->plugin_count++;
142 /* print in the splash screen */
143 /* printf ("Loaded plugin '%s'\n", desc->name); */
146 err = dlclose (dl_handle);
149 fprintf (stderr, "%s: error closing object file '%s': %s\n",
150 __FUNCTION__, filename, dlerror ());
155 plugin_mgr_get_dir_plugins (plugin_mgr_t * plugin_mgr, const char * dir)
158 struct dirent * dir_entry;
163 dir_stream = opendir (dir);
166 /* fprintf (stderr, "%s: error opening directory '%s': %s\n",
167 __FUNCTION__, dir, strerror (errno)); */
171 dirlen = strlen (dir);
173 while ( (dir_entry = readdir (dir_stream)) )
177 if (strcmp (dir_entry->d_name, ".") == 0 ||
178 strcmp (dir_entry->d_name, "..") == 0)
181 file_name = g_malloc (dirlen + 1 + strlen (dir_entry->d_name) + 1);
183 strcpy (file_name, dir);
184 if (file_name[dirlen - 1] == '/')
185 strcpy (file_name + dirlen, dir_entry->d_name);
188 file_name[dirlen] = '/';
189 strcpy (file_name + dirlen + 1, dir_entry->d_name);
192 stat (file_name, &info);
193 if (S_ISDIR (info.st_mode))
194 plugin_mgr_get_dir_plugins (plugin_mgr, file_name);
196 plugin_mgr_get_object_file_plugins (plugin_mgr, file_name);
201 err = closedir (dir_stream);
203 fprintf (stderr, "%s: error closing directory '%s': %s\n",
204 __FUNCTION__, dir, strerror (errno));
208 plugin_mgr_get_path_plugins (plugin_mgr_t * plugin_mgr)
210 char * ladspa_path, * dir;
212 ladspa_path = g_strdup (getenv ("LADSPA_PATH"));
214 ladspa_path = g_strdup ("/usr/local/lib/ladspa:/usr/lib/ladspa:/usr/lib64/ladspa");
216 dir = strtok (ladspa_path, ":");
218 plugin_mgr_get_dir_plugins (plugin_mgr, dir);
219 while ((dir = strtok (NULL, ":")));
221 g_free (ladspa_path);
225 plugin_mgr_sort (gconstpointer a, gconstpointer b)
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;
232 return strcasecmp (da->name, db->name);
240 pm = g_malloc (sizeof (plugin_mgr_t));
241 pm->all_plugins = NULL;
243 pm->plugin_count = 0;
245 plugin_mgr_get_path_plugins (pm);
247 if (!pm->all_plugins)
249 fprintf (stderr, "No LADSPA plugins were found!\n\nCheck your LADSPA_PATH environment variable.\n");
253 pm->all_plugins = g_slist_sort (pm->all_plugins, plugin_mgr_sort);
259 plugin_mgr_destroy (plugin_mgr_t * plugin_mgr)
263 for (list = plugin_mgr->all_plugins; list; list = g_slist_next (list))
264 plugin_desc_destroy ((plugin_desc_t *) list->data);
266 g_slist_free (plugin_mgr->plugins);
267 g_slist_free (plugin_mgr->all_plugins);
273 plugin_mgr_set_plugins (plugin_mgr_t * plugin_mgr, unsigned long rack_channels)
276 plugin_desc_t * desc;
278 /* clear the current plugins */
279 g_slist_free (plugin_mgr->plugins);
280 plugin_mgr->plugins = NULL;
282 for (list = plugin_mgr->all_plugins; list; list = g_slist_next (list))
284 desc = (plugin_desc_t *) list->data;
286 if (plugin_desc_get_copies (desc, rack_channels) != 0)
287 plugin_mgr->plugins = g_slist_append (plugin_mgr->plugins, desc);
291 static plugin_desc_t *
292 plugin_mgr_find_desc (plugin_mgr_t * plugin_mgr, GSList * plugins, unsigned long id)
295 plugin_desc_t * desc;
297 for (list = plugins; list; list = g_slist_next (list))
299 desc = (plugin_desc_t *) list->data;
309 plugin_mgr_get_desc (plugin_mgr_t * plugin_mgr, unsigned long id)
311 return plugin_mgr_find_desc (plugin_mgr, plugin_mgr->plugins, id);
315 plugin_mgr_get_any_desc (plugin_mgr_t * plugin_mgr, unsigned long id)
317 return plugin_mgr_find_desc (plugin_mgr, plugin_mgr->all_plugins, id);