4 * Copyright (C) Robert Ham 2002, 2003 (node@users.sourceforge.net)
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33 #include "plugin_mgr.h"
34 #include "plugin_desc.h"
39 plugin_is_valid (const LADSPA_Descriptor * descriptor)
42 unsigned long icount = 0;
43 unsigned long ocount = 0;
45 for (i = 0; i < descriptor->PortCount; i++)
47 if (!LADSPA_IS_PORT_AUDIO (descriptor->PortDescriptors[i]))
50 if (LADSPA_IS_PORT_INPUT (descriptor->PortDescriptors[i]))
56 if (icount == 0 || ocount == 0)
63 plugin_mgr_get_object_file_plugins (ui_t * ui, plugin_mgr_t * plugin_mgr, const char * filename)
67 LADSPA_Descriptor_Function get_descriptor;
68 const LADSPA_Descriptor * descriptor;
69 unsigned long plugin_index;
70 plugin_desc_t * desc, * other_desc = NULL;
75 /* open the object file */
76 dl_handle = dlopen (filename, RTLD_NOW|RTLD_GLOBAL);
79 fprintf (stderr, "%s: error opening shared object file '%s': %s\n",
80 __FUNCTION__, filename, dlerror());
85 /* get the get_descriptor function */
86 dlerror (); /* clear the error report */
88 get_descriptor = (LADSPA_Descriptor_Function)
89 dlsym (dl_handle, "ladspa_descriptor");
93 fprintf (stderr, "%s: error finding ladspa_descriptor symbol in object file '%s': %s\n",
94 __FUNCTION__, filename, dlerr);
100 while ( (descriptor = get_descriptor (plugin_index)) )
102 if (!plugin_is_valid (descriptor))
109 /* check it doesn't already exist */
111 for (list = plugin_mgr->all_plugins; list; list = g_slist_next (list))
113 other_desc = (plugin_desc_t *) list->data;
115 if (other_desc->id == descriptor->UniqueID)
124 printf ("Plugin %ld exists in both '%s' and '%s'; using version in '%s'\n",
125 descriptor->UniqueID, other_desc->object_file, filename, other_desc->object_file);
131 desc = plugin_desc_new_with_descriptor (filename, plugin_index, descriptor);
132 plugin_mgr->all_plugins = g_slist_append (plugin_mgr->all_plugins, desc);
134 plugin_mgr->plugin_count++;
136 /* print in the splash screen */
137 /* printf ("Loaded plugin '%s'\n", desc->name); */
140 err = dlclose (dl_handle);
143 fprintf (stderr, "%s: error closing object file '%s': %s\n",
144 __FUNCTION__, filename, dlerror ());
149 plugin_mgr_get_dir_plugins (ui_t * ui, plugin_mgr_t * plugin_mgr, const char * dir)
152 struct dirent * dir_entry;
157 dir_stream = opendir (dir);
160 /* fprintf (stderr, "%s: error opening directory '%s': %s\n",
161 __FUNCTION__, dir, strerror (errno)); */
165 dirlen = strlen (dir);
167 while ( (dir_entry = readdir (dir_stream)) )
169 if (strcmp (dir_entry->d_name, ".") == 0 ||
170 strcmp (dir_entry->d_name, "..") == 0)
173 file_name = g_malloc (dirlen + 1 + strlen (dir_entry->d_name) + 1);
175 strcpy (file_name, dir);
176 if (file_name[dirlen - 1] == '/')
177 strcpy (file_name + dirlen, dir_entry->d_name);
180 file_name[dirlen] = '/';
181 strcpy (file_name + dirlen + 1, dir_entry->d_name);
184 plugin_mgr_get_object_file_plugins (ui, plugin_mgr, file_name);
189 err = closedir (dir_stream);
191 fprintf (stderr, "%s: error closing directory '%s': %s\n",
192 __FUNCTION__, dir, strerror (errno));
196 plugin_mgr_get_path_plugins (ui_t * ui, plugin_mgr_t * plugin_mgr)
198 char * ladspa_path, * dir;
200 ladspa_path = g_strdup (getenv ("LADSPA_PATH"));
202 ladspa_path = g_strdup ("/usr/local/lib/ladspa:/usr/lib/ladspa");
204 dir = strtok (ladspa_path, ":");
206 plugin_mgr_get_dir_plugins (ui, plugin_mgr, dir);
207 while ((dir = strtok (NULL, ":")));
209 g_free (ladspa_path);
213 plugin_mgr_sort (gconstpointer a, gconstpointer b)
215 const plugin_desc_t * da;
216 const plugin_desc_t * db;
217 da = (const plugin_desc_t *) a;
218 db = (const plugin_desc_t *) b;
220 return strcasecmp (da->name, db->name);
224 plugin_mgr_new (ui_t * ui)
228 pm = g_malloc (sizeof (plugin_mgr_t));
229 pm->all_plugins = NULL;
231 pm->plugin_count = 0;
233 plugin_mgr_get_path_plugins (ui, pm);
235 if (!pm->all_plugins)
237 fprintf (stderr, "No LADSPA plugins were found!\n\nCheck your LADSPA_PATH environment variable.\n");
241 pm->all_plugins = g_slist_sort (pm->all_plugins, plugin_mgr_sort);
247 plugin_mgr_destroy (plugin_mgr_t * plugin_mgr)
251 for (list = plugin_mgr->all_plugins; list; list = g_slist_next (list))
252 plugin_desc_destroy ((plugin_desc_t *) list->data);
254 g_slist_free (plugin_mgr->plugins);
255 g_slist_free (plugin_mgr->all_plugins);
261 plugin_mgr_set_plugins (plugin_mgr_t * plugin_mgr, unsigned long rack_channels)
264 plugin_desc_t * desc;
266 /* clear the current plugins */
267 g_slist_free (plugin_mgr->plugins);
268 plugin_mgr->plugins = NULL;
270 for (list = plugin_mgr->all_plugins; list; list = g_slist_next (list))
272 desc = (plugin_desc_t *) list->data;
274 if (plugin_desc_get_copies (desc, rack_channels) != 0)
275 plugin_mgr->plugins = g_slist_append (plugin_mgr->plugins, desc);
279 static plugin_desc_t *
280 plugin_mgr_find_desc (plugin_mgr_t * plugin_mgr, GSList * plugins, unsigned long id)
283 plugin_desc_t * desc;
285 for (list = plugins; list; list = g_slist_next (list))
287 desc = (plugin_desc_t *) list->data;
297 plugin_mgr_get_desc (plugin_mgr_t * plugin_mgr, unsigned long id)
299 return plugin_mgr_find_desc (plugin_mgr, plugin_mgr->plugins, id);
303 plugin_mgr_get_any_desc (plugin_mgr_t * plugin_mgr, unsigned long id)
305 return plugin_mgr_find_desc (plugin_mgr, plugin_mgr->all_plugins, id);