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"
38 plugin_is_valid (const LADSPA_Descriptor
* descriptor
)
41 unsigned long icount
= 0;
42 unsigned long ocount
= 0;
44 for (i
= 0; i
< descriptor
->PortCount
; i
++)
46 if (!LADSPA_IS_PORT_AUDIO (descriptor
->PortDescriptors
[i
]))
49 if (LADSPA_IS_PORT_INPUT (descriptor
->PortDescriptors
[i
]))
55 if (icount
== 0 || ocount
== 0)
62 plugin_mgr_get_object_file_plugins (plugin_mgr_t
* plugin_mgr
, const char * filename
)
66 LADSPA_Descriptor_Function get_descriptor
;
67 const LADSPA_Descriptor
* descriptor
;
68 unsigned long plugin_index
;
69 plugin_desc_t
* desc
, * other_desc
= NULL
;
74 /* open the object file */
75 dl_handle
= dlopen (filename
, RTLD_NOW
|RTLD_GLOBAL
);
78 fprintf (stderr
, "%s: error opening shared object file '%s': %s\n",
79 __FUNCTION__
, filename
, dlerror());
84 /* get the get_descriptor function */
85 dlerror (); /* clear the error report */
87 get_descriptor
= (LADSPA_Descriptor_Function
)
88 dlsym (dl_handle
, "ladspa_descriptor");
92 fprintf (stderr
, "%s: error finding ladspa_descriptor symbol in object file '%s': %s\n",
93 __FUNCTION__
, filename
, dlerr
);
99 while ( (descriptor
= get_descriptor (plugin_index
)) )
101 if (!plugin_is_valid (descriptor
))
108 /* check it doesn't already exist */
110 for (list
= plugin_mgr
->all_plugins
; list
; list
= g_slist_next (list
))
112 other_desc
= (plugin_desc_t
*) list
->data
;
114 if (other_desc
->id
== descriptor
->UniqueID
)
123 printf ("Plugin %ld exists in both '%s' and '%s'; using version in '%s'\n",
124 descriptor
->UniqueID
, other_desc
->object_file
, filename
, other_desc
->object_file
);
130 desc
= plugin_desc_new_with_descriptor (filename
, plugin_index
, descriptor
);
131 plugin_mgr
->all_plugins
= g_slist_append (plugin_mgr
->all_plugins
, desc
);
133 plugin_mgr
->plugin_count
++;
135 /* print in the splash screen */
136 /* printf ("Loaded plugin '%s'\n", desc->name); */
139 err
= dlclose (dl_handle
);
142 fprintf (stderr
, "%s: error closing object file '%s': %s\n",
143 __FUNCTION__
, filename
, dlerror ());
148 plugin_mgr_get_dir_plugins (plugin_mgr_t
* plugin_mgr
, const char * dir
)
151 struct dirent
* dir_entry
;
156 dir_stream
= opendir (dir
);
159 /* fprintf (stderr, "%s: error opening directory '%s': %s\n",
160 __FUNCTION__, dir, strerror (errno)); */
164 dirlen
= strlen (dir
);
166 while ( (dir_entry
= readdir (dir_stream
)) )
168 if (strcmp (dir_entry
->d_name
, ".") == 0 ||
169 strcmp (dir_entry
->d_name
, "..") == 0)
172 file_name
= g_malloc (dirlen
+ 1 + strlen (dir_entry
->d_name
) + 1);
174 strcpy (file_name
, dir
);
175 if (file_name
[dirlen
- 1] == '/')
176 strcpy (file_name
+ dirlen
, dir_entry
->d_name
);
179 file_name
[dirlen
] = '/';
180 strcpy (file_name
+ dirlen
+ 1, dir_entry
->d_name
);
183 plugin_mgr_get_object_file_plugins (plugin_mgr
, file_name
);
188 err
= closedir (dir_stream
);
190 fprintf (stderr
, "%s: error closing directory '%s': %s\n",
191 __FUNCTION__
, dir
, strerror (errno
));
195 plugin_mgr_get_path_plugins (plugin_mgr_t
* plugin_mgr
)
197 char * ladspa_path
, * dir
;
199 ladspa_path
= g_strdup (getenv ("LADSPA_PATH"));
201 ladspa_path
= g_strdup ("/usr/local/lib/ladspa:/usr/lib/ladspa");
203 dir
= strtok (ladspa_path
, ":");
205 plugin_mgr_get_dir_plugins (plugin_mgr
, dir
);
206 while ((dir
= strtok (NULL
, ":")));
208 g_free (ladspa_path
);
212 plugin_mgr_sort (gconstpointer a
, gconstpointer b
)
214 const plugin_desc_t
* da
;
215 const plugin_desc_t
* db
;
216 da
= (const plugin_desc_t
*) a
;
217 db
= (const plugin_desc_t
*) b
;
219 return strcasecmp (da
->name
, db
->name
);
227 pm
= g_malloc (sizeof (plugin_mgr_t
));
228 pm
->all_plugins
= NULL
;
230 pm
->plugin_count
= 0;
232 plugin_mgr_get_path_plugins (pm
);
234 if (!pm
->all_plugins
)
236 fprintf (stderr
, "No LADSPA plugins were found!\n\nCheck your LADSPA_PATH environment variable.\n");
240 pm
->all_plugins
= g_slist_sort (pm
->all_plugins
, plugin_mgr_sort
);
246 plugin_mgr_destroy (plugin_mgr_t
* plugin_mgr
)
250 for (list
= plugin_mgr
->all_plugins
; list
; list
= g_slist_next (list
))
251 plugin_desc_destroy ((plugin_desc_t
*) list
->data
);
253 g_slist_free (plugin_mgr
->plugins
);
254 g_slist_free (plugin_mgr
->all_plugins
);
260 plugin_mgr_set_plugins (plugin_mgr_t
* plugin_mgr
, unsigned long rack_channels
)
263 plugin_desc_t
* desc
;
265 /* clear the current plugins */
266 g_slist_free (plugin_mgr
->plugins
);
267 plugin_mgr
->plugins
= NULL
;
269 for (list
= plugin_mgr
->all_plugins
; list
; list
= g_slist_next (list
))
271 desc
= (plugin_desc_t
*) list
->data
;
273 if (plugin_desc_get_copies (desc
, rack_channels
) != 0)
274 plugin_mgr
->plugins
= g_slist_append (plugin_mgr
->plugins
, desc
);
278 static plugin_desc_t
*
279 plugin_mgr_find_desc (plugin_mgr_t
* plugin_mgr
, GSList
* plugins
, unsigned long id
)
282 plugin_desc_t
* desc
;
284 for (list
= plugins
; list
; list
= g_slist_next (list
))
286 desc
= (plugin_desc_t
*) list
->data
;
296 plugin_mgr_get_desc (plugin_mgr_t
* plugin_mgr
, unsigned long id
)
298 return plugin_mgr_find_desc (plugin_mgr
, plugin_mgr
->plugins
, id
);
302 plugin_mgr_get_any_desc (plugin_mgr_t
* plugin_mgr
, unsigned long id
)
304 return plugin_mgr_find_desc (plugin_mgr
, plugin_mgr
->all_plugins
, id
);