bf52eb1fdbc9f4337a27ffee35b71497b23e614b
[platform/upstream/gst-plugins-good.git] / ext / ladspa / load.c
1 /* load.c
2
3    Free software by Richard W.E. Furse. Do with as you will. No
4    warranty. */
5
6 /*****************************************************************************/
7
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif
11
12 #include <dlfcn.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <glib.h>
17
18 /*****************************************************************************/
19
20 #include "ladspa.h"
21 #include "utils.h"
22
23 /*****************************************************************************/
24
25 /* This function provides a wrapping of dlopen(). When the filename is
26    not an absolute path (i.e. does not begin with / character), this
27    routine will search the LADSPA_PATH for the file. */
28 static void *
29 dlopenLADSPA(const char * pcFilename, int iFlag) {
30
31   char * pcBuffer;
32   const char * pcEnd;
33   const char * pcLADSPAPath;
34   const char * pcStart;
35   int iEndsInSO;
36   int iNeedSlash;
37   size_t iFilenameLength;
38   void * pvResult;
39
40   iFilenameLength = strlen(pcFilename);
41   pvResult = NULL;
42
43   if (pcFilename[0] == '/') {
44
45     /* The filename is absolute. Assume the user knows what he/she is
46        doing and simply dlopen() it. */
47
48     pvResult = dlopen(pcFilename, iFlag);
49     if (pvResult != NULL)
50       return pvResult;
51
52   }
53   else {
54
55     /* If the filename is not absolute then we wish to check along the
56        LADSPA_PATH path to see if we can find the file there. We do
57        NOT call dlopen() directly as this would find plugins on the
58        LD_LIBRARY_PATH, whereas the LADSPA_PATH is the correct place
59        to search. */
60
61     /* thomasvs: I'm sorry, but I'm going to add glib stuff here.
62      * I'm appending logical values for LADSPA_PATH here
63      */
64
65     pcLADSPAPath = g_strdup_printf ("%s:/usr/lib/ladspa:/usr/local/lib/ladspa",
66         getenv("LADSPA_PATH"));
67     
68     if (pcLADSPAPath) {
69
70       pcStart = pcLADSPAPath;
71       while (*pcStart != '\0') {
72         pcEnd = pcStart;
73         while (*pcEnd != ':' && *pcEnd != '\0')
74           pcEnd++;
75         
76         pcBuffer = malloc(iFilenameLength + 2 + (pcEnd - pcStart));
77         if (pcEnd > pcStart)
78           strncpy(pcBuffer, pcStart, pcEnd - pcStart);
79         iNeedSlash = 0;
80         if (pcEnd > pcStart)
81           if (*(pcEnd - 1) != '/') {
82             iNeedSlash = 1;
83             pcBuffer[pcEnd - pcStart] = '/';
84           }
85         strcpy(pcBuffer + iNeedSlash + (pcEnd - pcStart), pcFilename);
86         
87         pvResult = dlopen(pcBuffer, iFlag);
88         
89         free (pcBuffer);
90         if (pvResult != NULL)
91           return pvResult;
92         
93         pcStart = pcEnd;
94         if (*pcStart == ':')
95           pcStart++;
96       }
97     }
98   }
99
100   /* As a last ditch effort, check if filename does not end with
101      ".so". In this case, add this suffix and recurse. */
102   iEndsInSO = 0;
103   if (iFilenameLength > 3)
104     iEndsInSO = (strcmp(pcFilename + iFilenameLength - 3, ".so") == 0);
105   if (!iEndsInSO) {
106     pcBuffer = malloc(iFilenameLength + 4);
107     strcpy(pcBuffer, pcFilename);
108     strcat(pcBuffer, ".so");
109     pvResult = dlopenLADSPA(pcBuffer, iFlag);
110     free(pcBuffer);
111   }
112
113   if (pvResult != NULL)
114     return pvResult;
115
116   /* If nothing has worked, then at least we can make sure we set the
117      correct error message - and this should correspond to a call to
118      dlopen() with the actual filename requested. The dlopen() manual
119      page does not specify whether the first or last error message
120      will be kept when multiple calls are made to dlopen(). We've
121      covered the former case - now we can handle the latter by calling
122      dlopen() again here. */
123   return dlopen(pcFilename, iFlag);
124 }
125
126 /*****************************************************************************/
127
128 void *
129 loadLADSPAPluginLibrary(const char * pcPluginFilename) {
130
131   void * pvPluginHandle;
132
133   pvPluginHandle = dlopenLADSPA(pcPluginFilename, RTLD_NOW);
134   if (!pvPluginHandle) {
135     fprintf(stderr, 
136             "Failed to load plugin \"%s\": %s\n", 
137             pcPluginFilename,
138             dlerror());
139     exit(1);
140   }
141
142   return pvPluginHandle;
143 }
144
145 /*****************************************************************************/
146
147 void 
148 unloadLADSPAPluginLibrary(void * pvLADSPAPluginLibrary) {
149   dlclose(pvLADSPAPluginLibrary);
150 }
151
152 /*****************************************************************************/
153
154 const LADSPA_Descriptor *
155 findLADSPAPluginDescriptor(void * pvLADSPAPluginLibrary,
156                            const char * pcPluginLibraryFilename,
157                            const char * pcPluginLabel) {
158
159   const LADSPA_Descriptor * psDescriptor;
160   LADSPA_Descriptor_Function pfDescriptorFunction;
161   unsigned long lPluginIndex;
162
163   dlerror();
164   pfDescriptorFunction 
165     = (LADSPA_Descriptor_Function)dlsym(pvLADSPAPluginLibrary,
166                                         "ladspa_descriptor");
167   if (!pfDescriptorFunction) {
168     const char * pcError = dlerror();
169     if (pcError) {
170       fprintf(stderr,
171               "Unable to find ladspa_descriptor() function in plugin "
172               "library file \"%s\": %s.\n"
173               "Are you sure this is a LADSPA plugin file?\n", 
174               pcPluginLibraryFilename,
175               pcError);
176       exit(1);
177     }
178   }
179
180   for (lPluginIndex = 0;; lPluginIndex++) {
181     psDescriptor = pfDescriptorFunction(lPluginIndex);
182     if (psDescriptor == NULL) {
183       fprintf(stderr,
184               "Unable to find label \"%s\" in plugin library file \"%s\".\n",
185               pcPluginLabel,
186               pcPluginLibraryFilename);
187       exit(1);      
188     }
189     if (strcmp(psDescriptor->Label, pcPluginLabel) == 0)
190       return psDescriptor;
191   }
192 }
193
194 /*****************************************************************************/
195
196 /* EOF */