1 /*
2  * JACK Rack
3  *
4  * Original:
5  * Copyright (C) Robert Ham 2002, 2003 (node@users.sourceforge.net)
6  *
7  * Modifications for MLT:
8  * Copyright (C) 2004-2016 Meltytech, LLC
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24 
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdio.h>
28 #include <dirent.h>
29 #include <errno.h>
30 #include <sys/stat.h>
31 #include <dlfcn.h>
32 #include <math.h>
33 #include <strings.h>
34 #include <ctype.h>
35 #include <ladspa.h>
36 #include <sys/types.h>
37 #include <unistd.h>
38 
39 #include "plugin_mgr.h"
40 #include "plugin_desc.h"
41 #include "framework/mlt_log.h"
42 #include "framework/mlt_factory.h"
43 
44 static gboolean
plugin_is_valid(const LADSPA_Descriptor * descriptor)45 plugin_is_valid (const LADSPA_Descriptor * descriptor)
46 {
47   unsigned long i;
48   unsigned long icount = 0;
49   unsigned long ocount = 0;
50 
51   for (i = 0; i < descriptor->PortCount; i++)
52     {
53       if (!LADSPA_IS_PORT_AUDIO (descriptor->PortDescriptors[i]))
54         continue;
55 
56       if (LADSPA_IS_PORT_INPUT (descriptor->PortDescriptors[i]))
57         icount++;
58       else
59         ocount++;
60     }
61 
62   if (ocount == 0)
63     return FALSE;
64 
65   return TRUE;
66 }
67 
68 static void
plugin_mgr_get_object_file_plugins(plugin_mgr_t * plugin_mgr,const char * filename)69 plugin_mgr_get_object_file_plugins (plugin_mgr_t * plugin_mgr, const char * filename)
70 {
71   const char * dlerr;
72   void * dl_handle;
73   LADSPA_Descriptor_Function get_descriptor;
74   const LADSPA_Descriptor * descriptor;
75   unsigned long plugin_index;
76   plugin_desc_t * desc, * other_desc = NULL;
77   GSList * list;
78   gboolean exists;
79   int err;
80 
81   /* open the object file */
82   dl_handle = dlopen (filename, RTLD_LAZY);
83   if (!dl_handle)
84     {
85       mlt_log_info( NULL, "%s: error opening shared object file '%s': %s\n",
86                __FUNCTION__, filename, dlerror());
87       return;
88     }
89 
90 
91   /* get the get_descriptor function */
92   dlerror (); /* clear the error report */
93 
94   get_descriptor = (LADSPA_Descriptor_Function)
95     dlsym (dl_handle, "ladspa_descriptor");
96 
97   dlerr = dlerror();
98   if (dlerr) {
99     mlt_log_info( NULL, "%s: error finding ladspa_descriptor symbol in object file '%s': %s\n",
100              __FUNCTION__, filename, dlerr);
101     dlclose (dl_handle);
102     return;
103   }
104 
105 #ifdef __APPLE__
106   if (!get_descriptor (0)) {
107     void (*constructor)(void) = dlsym (dl_handle, "_init");
108     if (constructor) constructor();
109   }
110 #endif
111 
112   plugin_index = 0;
113   while ( (descriptor = get_descriptor (plugin_index)) )
114     {
115       if (!plugin_is_valid (descriptor))
116         {
117           plugin_index++;
118           continue;
119         }
120 
121 
122       /* check it doesn't already exist */
123       exists = FALSE;
124       for (list = plugin_mgr->all_plugins; list; list = g_slist_next (list))
125         {
126           other_desc = (plugin_desc_t *) list->data;
127 
128           if (other_desc->id == descriptor->UniqueID)
129             {
130               exists = TRUE;
131               break;
132             }
133         }
134 
135       if (exists)
136         {
137           mlt_log_info( NULL, "Plugin %ld exists in both '%s' and '%s'; using version in '%s'\n",
138                   descriptor->UniqueID, other_desc->object_file, filename, other_desc->object_file);
139           plugin_index++;
140           continue;
141         }
142 
143 
144       desc = plugin_desc_new_with_descriptor (filename, plugin_index, descriptor);
145       plugin_mgr->all_plugins = g_slist_append (plugin_mgr->all_plugins, desc);
146       plugin_index++;
147       plugin_mgr->plugin_count++;
148 
149       /* print in the splash screen */
150       /* mlt_log_verbose( NULL, "Loaded plugin '%s'\n", desc->name); */
151     }
152 
153   err = dlclose (dl_handle);
154   if (err)
155     {
156       mlt_log_warning( NULL, "%s: error closing object file '%s': %s\n",
157                __FUNCTION__, filename, dlerror ());
158     }
159 }
160 
161 static void
plugin_mgr_get_dir_plugins(plugin_mgr_t * plugin_mgr,const char * dir)162 plugin_mgr_get_dir_plugins (plugin_mgr_t * plugin_mgr, const char * dir)
163 {
164   DIR * dir_stream;
165   struct dirent * dir_entry;
166   char * file_name;
167   int err;
168   size_t dirlen;
169 
170   dir_stream = opendir (dir);
171   if (!dir_stream)
172     {
173 /*      mlt_log_warning( NULL, "%s: error opening directory '%s': %s\n",
174                __FUNCTION__, dir, strerror (errno)); */
175       return;
176     }
177 
178   dirlen = strlen (dir);
179 
180   while ( (dir_entry = readdir (dir_stream)) )
181     {
182       struct stat info;
183 
184       if (strcmp (dir_entry->d_name, ".") == 0 ||
185           mlt_properties_get (plugin_mgr->blacklist, dir_entry->d_name) ||
186           strcmp (dir_entry->d_name, "..") == 0)
187         continue;
188 
189       file_name = g_malloc (dirlen + 1 + strlen (dir_entry->d_name) + 1);
190 
191       strcpy (file_name, dir);
192       if (file_name[dirlen - 1] == '/')
193         strcpy (file_name + dirlen, dir_entry->d_name);
194       else
195         {
196           file_name[dirlen] = '/';
197           strcpy (file_name + dirlen + 1, dir_entry->d_name);
198         }
199 
200       stat (file_name, &info);
201       if (S_ISDIR (info.st_mode))
202         plugin_mgr_get_dir_plugins (plugin_mgr, file_name);
203       else
204         plugin_mgr_get_object_file_plugins (plugin_mgr, file_name);
205 
206       g_free (file_name);
207     }
208 
209   err = closedir (dir_stream);
210   if (err)
211     mlt_log_warning( NULL, "%s: error closing directory '%s': %s\n",
212              __FUNCTION__, dir, strerror (errno));
213 }
214 
215 static void
plugin_mgr_get_path_plugins(plugin_mgr_t * plugin_mgr)216 plugin_mgr_get_path_plugins (plugin_mgr_t * plugin_mgr)
217 {
218   char * ladspa_path, * dir;
219 
220   ladspa_path = g_strdup (getenv ("LADSPA_PATH"));
221 #ifdef _WIN32
222   if (!ladspa_path)
223   {
224     ladspa_path = malloc (strlen (mlt_environment("MLT_APPDIR")) + strlen ("\\lib\\ladspa") + 1);
225     strcpy (ladspa_path, mlt_environment("MLT_APPDIR"));
226     strcat (ladspa_path, "\\lib\\ladspa");
227   }
228 #elif defined(__APPLE__) && defined(RELOCATABLE)
229   {
230     ladspa_path = malloc( strlen (mlt_environment ("MLT_APPDIR")) + strlen ("/PlugIns/ladspa") + 1 );
231     strcpy (ladspa_path,  mlt_environment ("MLT_APPDIR"));
232     strcat (ladspa_path, "/PlugIns/ladspa" );
233   }
234 #else
235   if (!ladspa_path)
236     ladspa_path = g_strdup ("/usr/local/lib/ladspa:/usr/lib/ladspa:/usr/lib64/ladspa");
237 #endif
238 
239   for (dir = strtok (ladspa_path, MLT_DIRLIST_DELIMITER); dir; dir = strtok (NULL, MLT_DIRLIST_DELIMITER))
240     plugin_mgr_get_dir_plugins (plugin_mgr, dir);
241 
242   g_free (ladspa_path);
243 }
244 
245 static gint
plugin_mgr_sort(gconstpointer a,gconstpointer b)246 plugin_mgr_sort (gconstpointer a, gconstpointer b)
247 {
248   const plugin_desc_t * da;
249   const plugin_desc_t * db;
250   da = (const plugin_desc_t *) a;
251   db = (const plugin_desc_t *) b;
252 
253   return strcasecmp (da->name, db->name);
254 }
255 
256 plugin_mgr_t *
plugin_mgr_new()257 plugin_mgr_new ()
258 {
259   plugin_mgr_t * pm;
260   char dirname[PATH_MAX];
261 
262   pm = g_malloc (sizeof (plugin_mgr_t));
263   pm->all_plugins = NULL;
264   pm->plugins = NULL;
265   pm->plugin_count = 0;
266 
267   snprintf (dirname, PATH_MAX, "%s/jackrack/blacklist.txt", mlt_environment ("MLT_DATA"));
268   pm->blacklist = mlt_properties_load (dirname);
269   plugin_mgr_get_path_plugins (pm);
270 
271   if (!pm->all_plugins)
272     mlt_log_warning( NULL, "No LADSPA plugins were found!\n\nCheck your LADSPA_PATH environment variable.\n");
273   else
274     pm->all_plugins = g_slist_sort (pm->all_plugins, plugin_mgr_sort);
275 
276   return pm;
277 }
278 
279 void
plugin_mgr_destroy(plugin_mgr_t * plugin_mgr)280 plugin_mgr_destroy (plugin_mgr_t * plugin_mgr)
281 {
282   GSList * list;
283 
284   for (list = plugin_mgr->all_plugins; list; list = g_slist_next (list))
285     plugin_desc_destroy ((plugin_desc_t *) list->data);
286 
287   g_slist_free (plugin_mgr->plugins);
288   g_slist_free (plugin_mgr->all_plugins);
289   mlt_properties_close(plugin_mgr->blacklist);
290   free (plugin_mgr);
291 }
292 
293 
294 void
plugin_mgr_set_plugins(plugin_mgr_t * plugin_mgr,unsigned long rack_channels)295 plugin_mgr_set_plugins (plugin_mgr_t * plugin_mgr, unsigned long rack_channels)
296 {
297   GSList * list;
298   plugin_desc_t * desc;
299 
300   /* clear the current plugins */
301   g_slist_free (plugin_mgr->plugins);
302   plugin_mgr->plugins = NULL;
303 
304   for (list = plugin_mgr->all_plugins; list; list = g_slist_next (list))
305     {
306       desc = (plugin_desc_t *) list->data;
307 
308       if (plugin_desc_get_copies (desc, rack_channels) != 0)
309         plugin_mgr->plugins = g_slist_append (plugin_mgr->plugins, desc);
310     }
311 }
312 
313 static plugin_desc_t *
plugin_mgr_find_desc(plugin_mgr_t * plugin_mgr,GSList * plugins,unsigned long id)314 plugin_mgr_find_desc (plugin_mgr_t * plugin_mgr, GSList * plugins, unsigned long id)
315 {
316   GSList * list;
317   plugin_desc_t * desc;
318 
319   for (list = plugins; list; list = g_slist_next (list))
320     {
321       desc = (plugin_desc_t *) list->data;
322 
323       if (desc->id == id)
324         return desc;
325     }
326 
327   return NULL;
328 }
329 
330 plugin_desc_t *
plugin_mgr_get_desc(plugin_mgr_t * plugin_mgr,unsigned long id)331 plugin_mgr_get_desc (plugin_mgr_t * plugin_mgr, unsigned long id)
332 {
333   return plugin_mgr_find_desc (plugin_mgr, plugin_mgr->plugins, id);
334 }
335 
336 plugin_desc_t *
plugin_mgr_get_any_desc(plugin_mgr_t * plugin_mgr,unsigned long id)337 plugin_mgr_get_any_desc (plugin_mgr_t * plugin_mgr, unsigned long id)
338 {
339   return plugin_mgr_find_desc (plugin_mgr, plugin_mgr->all_plugins, id);
340 }
341 
342 
343 /* EOF */
344