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.
38 #include "plugin_mgr.h"
39 #include "plugin_desc.h"
43 plugin_is_valid (const LADSPA_Descriptor
* descriptor
)
46 unsigned long icount
= 0;
47 unsigned long ocount
= 0;
49 for (i
= 0; i
< descriptor
->PortCount
; i
++)
51 if (!LADSPA_IS_PORT_AUDIO (descriptor
->PortDescriptors
[i
]))
54 if (LADSPA_IS_PORT_INPUT (descriptor
->PortDescriptors
[i
]))
60 if (icount
== 0 || ocount
== 0)
67 plugin_mgr_get_object_file_plugins (plugin_mgr_t
* plugin_mgr
, const char * filename
)
71 LADSPA_Descriptor_Function get_descriptor
;
72 const LADSPA_Descriptor
* descriptor
;
73 unsigned long plugin_index
;
74 plugin_desc_t
* desc
, * other_desc
= NULL
;
79 /* open the object file */
80 dl_handle
= dlopen (filename
, RTLD_NOW
|RTLD_GLOBAL
);
83 fprintf (stderr
, "%s: error opening shared object file '%s': %s\n",
84 __FUNCTION__
, filename
, dlerror());
89 /* get the get_descriptor function */
90 dlerror (); /* clear the error report */
92 get_descriptor
= (LADSPA_Descriptor_Function
)
93 dlsym (dl_handle
, "ladspa_descriptor");
97 fprintf (stderr
, "%s: error finding ladspa_descriptor symbol in object file '%s': %s\n",
98 __FUNCTION__
, filename
, dlerr
);
104 while ( (descriptor
= get_descriptor (plugin_index
)) )
106 if (!plugin_is_valid (descriptor
))
113 /* check it doesn't already exist */
115 for (list
= plugin_mgr
->all_plugins
; list
; list
= g_slist_next (list
))
117 other_desc
= (plugin_desc_t
*) list
->data
;
119 if (other_desc
->id
== descriptor
->UniqueID
)
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
);
135 desc
= plugin_desc_new_with_descriptor (filename
, plugin_index
, descriptor
);
136 plugin_mgr
->all_plugins
= g_slist_append (plugin_mgr
->all_plugins
, desc
);
138 plugin_mgr
->plugin_count
++;
140 /* print in the splash screen */
141 /* printf ("Loaded plugin '%s'\n", desc->name); */
144 err
= dlclose (dl_handle
);
147 fprintf (stderr
, "%s: error closing object file '%s': %s\n",
148 __FUNCTION__
, filename
, dlerror ());
153 plugin_mgr_get_dir_plugins (plugin_mgr_t
* plugin_mgr
, const char * dir
)
156 struct dirent
* dir_entry
;
161 dir_stream
= opendir (dir
);
164 /* fprintf (stderr, "%s: error opening directory '%s': %s\n",
165 __FUNCTION__, dir, strerror (errno)); */
169 dirlen
= strlen (dir
);
171 while ( (dir_entry
= readdir (dir_stream
)) )
173 if (strcmp (dir_entry
->d_name
, ".") == 0 ||
174 strcmp (dir_entry
->d_name
, "..") == 0)
177 file_name
= g_malloc (dirlen
+ 1 + strlen (dir_entry
->d_name
) + 1);
179 strcpy (file_name
, dir
);
180 if (file_name
[dirlen
- 1] == '/')
181 strcpy (file_name
+ dirlen
, dir_entry
->d_name
);
184 file_name
[dirlen
] = '/';
185 strcpy (file_name
+ dirlen
+ 1, dir_entry
->d_name
);
188 plugin_mgr_get_object_file_plugins (plugin_mgr
, file_name
);
193 err
= closedir (dir_stream
);
195 fprintf (stderr
, "%s: error closing directory '%s': %s\n",
196 __FUNCTION__
, dir
, strerror (errno
));
200 plugin_mgr_get_path_plugins (plugin_mgr_t
* plugin_mgr
)
202 char * ladspa_path
, * dir
;
204 ladspa_path
= g_strdup (getenv ("LADSPA_PATH"));
206 ladspa_path
= g_strdup ("/usr/local/lib/ladspa:/usr/lib/ladspa");
208 dir
= strtok (ladspa_path
, ":");
210 plugin_mgr_get_dir_plugins (plugin_mgr
, dir
);
211 while ((dir
= strtok (NULL
, ":")));
213 g_free (ladspa_path
);
217 plugin_mgr_sort (gconstpointer a
, gconstpointer b
)
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
;
224 return strcasecmp (da
->name
, db
->name
);
232 pm
= g_malloc (sizeof (plugin_mgr_t
));
233 pm
->all_plugins
= NULL
;
235 pm
->plugin_count
= 0;
237 plugin_mgr_get_path_plugins (pm
);
239 if (!pm
->all_plugins
)
241 fprintf (stderr
, "No LADSPA plugins were found!\n\nCheck your LADSPA_PATH environment variable.\n");
245 pm
->all_plugins
= g_slist_sort (pm
->all_plugins
, plugin_mgr_sort
);
251 plugin_mgr_destroy (plugin_mgr_t
* plugin_mgr
)
255 for (list
= plugin_mgr
->all_plugins
; list
; list
= g_slist_next (list
))
256 plugin_desc_destroy ((plugin_desc_t
*) list
->data
);
258 g_slist_free (plugin_mgr
->plugins
);
259 g_slist_free (plugin_mgr
->all_plugins
);
265 plugin_mgr_set_plugins (plugin_mgr_t
* plugin_mgr
, unsigned long rack_channels
)
268 plugin_desc_t
* desc
;
270 /* clear the current plugins */
271 g_slist_free (plugin_mgr
->plugins
);
272 plugin_mgr
->plugins
= NULL
;
274 for (list
= plugin_mgr
->all_plugins
; list
; list
= g_slist_next (list
))
276 desc
= (plugin_desc_t
*) list
->data
;
278 if (plugin_desc_get_copies (desc
, rack_channels
) != 0)
279 plugin_mgr
->plugins
= g_slist_append (plugin_mgr
->plugins
, desc
);
283 static plugin_desc_t
*
284 plugin_mgr_find_desc (plugin_mgr_t
* plugin_mgr
, GSList
* plugins
, unsigned long id
)
287 plugin_desc_t
* desc
;
289 for (list
= plugins
; list
; list
= g_slist_next (list
))
291 desc
= (plugin_desc_t
*) list
->data
;
301 plugin_mgr_get_desc (plugin_mgr_t
* plugin_mgr
, unsigned long id
)
303 return plugin_mgr_find_desc (plugin_mgr
, plugin_mgr
->plugins
, id
);
307 plugin_mgr_get_any_desc (plugin_mgr_t
* plugin_mgr
, unsigned long id
)
309 return plugin_mgr_find_desc (plugin_mgr
, plugin_mgr
->all_plugins
, id
);