Fixed crash in slowmotion producer
[melted] / src / modules / jackrack / plugin_mgr.c
1 /*
2 * jack-ladspa-host
3 *
4 * Copyright (C) Robert Ham 2002, 2003 (node@users.sourceforge.net)
5 *
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.
10 *
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.
15 *
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.
19 */
20
21 #include <stdlib.h>
22 #include <string.h>
23 #include <stdio.h>
24 #include <dirent.h>
25 #include <errno.h>
26 #include <sys/stat.h>
27 #include <dlfcn.h>
28 #include <math.h>
29 #include <strings.h>
30 #include <ctype.h>
31 #include <ladspa.h>
32
33 #include "plugin_mgr.h"
34 #include "plugin_desc.h"
35
36
37 static gboolean
38 plugin_is_valid (const LADSPA_Descriptor * descriptor)
39 {
40 unsigned long i;
41 unsigned long icount = 0;
42 unsigned long ocount = 0;
43
44 for (i = 0; i < descriptor->PortCount; i++)
45 {
46 if (!LADSPA_IS_PORT_AUDIO (descriptor->PortDescriptors[i]))
47 continue;
48
49 if (LADSPA_IS_PORT_INPUT (descriptor->PortDescriptors[i]))
50 icount++;
51 else
52 ocount++;
53 }
54
55 if (icount == 0 || ocount == 0)
56 return FALSE;
57
58 return TRUE;
59 }
60
61 static void
62 plugin_mgr_get_object_file_plugins (plugin_mgr_t * plugin_mgr, const char * filename)
63 {
64 const char * dlerr;
65 void * dl_handle;
66 LADSPA_Descriptor_Function get_descriptor;
67 const LADSPA_Descriptor * descriptor;
68 unsigned long plugin_index;
69 plugin_desc_t * desc, * other_desc = NULL;
70 GSList * list;
71 gboolean exists;
72 int err;
73
74 /* open the object file */
75 dl_handle = dlopen (filename, RTLD_NOW|RTLD_GLOBAL);
76 if (!dl_handle)
77 {
78 fprintf (stderr, "%s: error opening shared object file '%s': %s\n",
79 __FUNCTION__, filename, dlerror());
80 return;
81 }
82
83
84 /* get the get_descriptor function */
85 dlerror (); /* clear the error report */
86
87 get_descriptor = (LADSPA_Descriptor_Function)
88 dlsym (dl_handle, "ladspa_descriptor");
89
90 dlerr = dlerror();
91 if (dlerr) {
92 fprintf (stderr, "%s: error finding ladspa_descriptor symbol in object file '%s': %s\n",
93 __FUNCTION__, filename, dlerr);
94 dlclose (dl_handle);
95 return;
96 }
97
98 plugin_index = 0;
99 while ( (descriptor = get_descriptor (plugin_index)) )
100 {
101 if (!plugin_is_valid (descriptor))
102 {
103 plugin_index++;
104 continue;
105 }
106
107
108 /* check it doesn't already exist */
109 exists = FALSE;
110 for (list = plugin_mgr->all_plugins; list; list = g_slist_next (list))
111 {
112 other_desc = (plugin_desc_t *) list->data;
113
114 if (other_desc->id == descriptor->UniqueID)
115 {
116 exists = TRUE;
117 break;
118 }
119 }
120
121 if (exists)
122 {
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);
125 plugin_index++;
126 continue;
127 }
128
129
130 desc = plugin_desc_new_with_descriptor (filename, plugin_index, descriptor);
131 plugin_mgr->all_plugins = g_slist_append (plugin_mgr->all_plugins, desc);
132 plugin_index++;
133 plugin_mgr->plugin_count++;
134
135 /* print in the splash screen */
136 /* printf ("Loaded plugin '%s'\n", desc->name); */
137 }
138
139 err = dlclose (dl_handle);
140 if (err)
141 {
142 fprintf (stderr, "%s: error closing object file '%s': %s\n",
143 __FUNCTION__, filename, dlerror ());
144 }
145 }
146
147 static void
148 plugin_mgr_get_dir_plugins (plugin_mgr_t * plugin_mgr, const char * dir)
149 {
150 DIR * dir_stream;
151 struct dirent * dir_entry;
152 char * file_name;
153 int err;
154 size_t dirlen;
155
156 dir_stream = opendir (dir);
157 if (!dir_stream)
158 {
159 /* fprintf (stderr, "%s: error opening directory '%s': %s\n",
160 __FUNCTION__, dir, strerror (errno)); */
161 return;
162 }
163
164 dirlen = strlen (dir);
165
166 while ( (dir_entry = readdir (dir_stream)) )
167 {
168 if (strcmp (dir_entry->d_name, ".") == 0 ||
169 strcmp (dir_entry->d_name, "..") == 0)
170 continue;
171
172 file_name = g_malloc (dirlen + 1 + strlen (dir_entry->d_name) + 1);
173
174 strcpy (file_name, dir);
175 if (file_name[dirlen - 1] == '/')
176 strcpy (file_name + dirlen, dir_entry->d_name);
177 else
178 {
179 file_name[dirlen] = '/';
180 strcpy (file_name + dirlen + 1, dir_entry->d_name);
181 }
182
183 plugin_mgr_get_object_file_plugins (plugin_mgr, file_name);
184
185 g_free (file_name);
186 }
187
188 err = closedir (dir_stream);
189 if (err)
190 fprintf (stderr, "%s: error closing directory '%s': %s\n",
191 __FUNCTION__, dir, strerror (errno));
192 }
193
194 static void
195 plugin_mgr_get_path_plugins (plugin_mgr_t * plugin_mgr)
196 {
197 char * ladspa_path, * dir;
198
199 ladspa_path = g_strdup (getenv ("LADSPA_PATH"));
200 if (!ladspa_path)
201 ladspa_path = g_strdup ("/usr/local/lib/ladspa:/usr/lib/ladspa");
202
203 dir = strtok (ladspa_path, ":");
204 do
205 plugin_mgr_get_dir_plugins (plugin_mgr, dir);
206 while ((dir = strtok (NULL, ":")));
207
208 g_free (ladspa_path);
209 }
210
211 static gint
212 plugin_mgr_sort (gconstpointer a, gconstpointer b)
213 {
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;
218
219 return strcasecmp (da->name, db->name);
220 }
221
222 plugin_mgr_t *
223 plugin_mgr_new ()
224 {
225 plugin_mgr_t * pm;
226
227 pm = g_malloc (sizeof (plugin_mgr_t));
228 pm->all_plugins = NULL;
229 pm->plugins = NULL;
230 pm->plugin_count = 0;
231
232 plugin_mgr_get_path_plugins (pm);
233
234 if (!pm->all_plugins)
235 {
236 fprintf (stderr, "No LADSPA plugins were found!\n\nCheck your LADSPA_PATH environment variable.\n");
237 abort ();
238 }
239
240 pm->all_plugins = g_slist_sort (pm->all_plugins, plugin_mgr_sort);
241
242 return pm;
243 }
244
245 void
246 plugin_mgr_destroy (plugin_mgr_t * plugin_mgr)
247 {
248 GSList * list;
249
250 for (list = plugin_mgr->all_plugins; list; list = g_slist_next (list))
251 plugin_desc_destroy ((plugin_desc_t *) list->data);
252
253 g_slist_free (plugin_mgr->plugins);
254 g_slist_free (plugin_mgr->all_plugins);
255 free (plugin_mgr);
256 }
257
258
259 void
260 plugin_mgr_set_plugins (plugin_mgr_t * plugin_mgr, unsigned long rack_channels)
261 {
262 GSList * list;
263 plugin_desc_t * desc;
264
265 /* clear the current plugins */
266 g_slist_free (plugin_mgr->plugins);
267 plugin_mgr->plugins = NULL;
268
269 for (list = plugin_mgr->all_plugins; list; list = g_slist_next (list))
270 {
271 desc = (plugin_desc_t *) list->data;
272
273 if (plugin_desc_get_copies (desc, rack_channels) != 0)
274 plugin_mgr->plugins = g_slist_append (plugin_mgr->plugins, desc);
275 }
276 }
277
278 static plugin_desc_t *
279 plugin_mgr_find_desc (plugin_mgr_t * plugin_mgr, GSList * plugins, unsigned long id)
280 {
281 GSList * list;
282 plugin_desc_t * desc;
283
284 for (list = plugins; list; list = g_slist_next (list))
285 {
286 desc = (plugin_desc_t *) list->data;
287
288 if (desc->id == id)
289 return desc;
290 }
291
292 return NULL;
293 }
294
295 plugin_desc_t *
296 plugin_mgr_get_desc (plugin_mgr_t * plugin_mgr, unsigned long id)
297 {
298 return plugin_mgr_find_desc (plugin_mgr, plugin_mgr->plugins, id);
299 }
300
301 plugin_desc_t *
302 plugin_mgr_get_any_desc (plugin_mgr_t * plugin_mgr, unsigned long id)
303 {
304 return plugin_mgr_find_desc (plugin_mgr, plugin_mgr->all_plugins, id);
305 }
306
307
308 /* EOF */