gst-indent
[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
32   char *pcBuffer;
33   const char *pcEnd;
34   const char *pcLADSPAPath;
35   const char *pcStart;
36   int iEndsInSO;
37   int iNeedSlash;
38   size_t iFilenameLength;
39   void *pvResult;
40
41   iFilenameLength = strlen (pcFilename);
42   pvResult = NULL;
43
44   if (pcFilename[0] == '/') {
45
46     /* The filename is absolute. Assume the user knows what he/she is
47        doing and simply dlopen() it. */
48
49     pvResult = dlopen (pcFilename, iFlag);
50     if (pvResult != NULL)
51       return pvResult;
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
132   void *pvPluginHandle;
133
134   pvPluginHandle = dlopenLADSPA (pcPluginFilename, RTLD_NOW);
135   if (!pvPluginHandle) {
136     fprintf (stderr,
137         "Failed to load plugin \"%s\": %s\n", pcPluginFilename, dlerror ());
138     exit (1);
139   }
140
141   return pvPluginHandle;
142 }
143
144 /*****************************************************************************/
145
146 void
147 unloadLADSPAPluginLibrary (void *pvLADSPAPluginLibrary)
148 {
149   dlclose (pvLADSPAPluginLibrary);
150 }
151
152 /*****************************************************************************/
153
154 const LADSPA_Descriptor *
155 findLADSPAPluginDescriptor (void *pvLADSPAPluginLibrary,
156     const char *pcPluginLibraryFilename, const char *pcPluginLabel)
157 {
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
170     if (pcError) {
171       fprintf (stderr,
172           "Unable to find ladspa_descriptor() function in plugin "
173           "library file \"%s\": %s.\n"
174           "Are you sure this is a LADSPA plugin file?\n",
175           pcPluginLibraryFilename, 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, pcPluginLibraryFilename);
186       exit (1);
187     }
188     if (strcmp (psDescriptor->Label, pcPluginLabel) == 0)
189       return psDescriptor;
190   }
191 }
192
193 /*****************************************************************************/
194
195 /* EOF */