3 Free software by Richard W.E. Furse. Do with as you will. No
6 /*****************************************************************************/
18 /*****************************************************************************/
23 /*****************************************************************************/
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. */
29 dlopenLADSPA(const char * pcFilename, int iFlag) {
33 const char * pcLADSPAPath;
37 size_t iFilenameLength;
40 iFilenameLength = strlen(pcFilename);
43 if (pcFilename[0] == '/') {
45 /* The filename is absolute. Assume the user knows what he/she is
46 doing and simply dlopen() it. */
48 pvResult = dlopen(pcFilename, iFlag);
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
61 /* thomasvs: I'm sorry, but I'm going to add glib stuff here.
62 * I'm appending logical values for LADSPA_PATH here
65 pcLADSPAPath = g_strdup_printf ("%s:/usr/lib/ladspa:/usr/local/lib/ladspa",
66 getenv("LADSPA_PATH"));
70 pcStart = pcLADSPAPath;
71 while (*pcStart != '\0') {
73 while (*pcEnd != ':' && *pcEnd != '\0')
76 pcBuffer = malloc(iFilenameLength + 2 + (pcEnd - pcStart));
78 strncpy(pcBuffer, pcStart, pcEnd - pcStart);
81 if (*(pcEnd - 1) != '/') {
83 pcBuffer[pcEnd - pcStart] = '/';
85 strcpy(pcBuffer + iNeedSlash + (pcEnd - pcStart), pcFilename);
87 pvResult = dlopen(pcBuffer, iFlag);
100 /* As a last ditch effort, check if filename does not end with
101 ".so". In this case, add this suffix and recurse. */
103 if (iFilenameLength > 3)
104 iEndsInSO = (strcmp(pcFilename + iFilenameLength - 3, ".so") == 0);
106 pcBuffer = malloc(iFilenameLength + 4);
107 strcpy(pcBuffer, pcFilename);
108 strcat(pcBuffer, ".so");
109 pvResult = dlopenLADSPA(pcBuffer, iFlag);
113 if (pvResult != NULL)
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);
126 /*****************************************************************************/
129 loadLADSPAPluginLibrary(const char * pcPluginFilename) {
131 void * pvPluginHandle;
133 pvPluginHandle = dlopenLADSPA(pcPluginFilename, RTLD_NOW);
134 if (!pvPluginHandle) {
136 "Failed to load plugin \"%s\": %s\n",
142 return pvPluginHandle;
145 /*****************************************************************************/
148 unloadLADSPAPluginLibrary(void * pvLADSPAPluginLibrary) {
149 dlclose(pvLADSPAPluginLibrary);
152 /*****************************************************************************/
154 const LADSPA_Descriptor *
155 findLADSPAPluginDescriptor(void * pvLADSPAPluginLibrary,
156 const char * pcPluginLibraryFilename,
157 const char * pcPluginLabel) {
159 const LADSPA_Descriptor * psDescriptor;
160 LADSPA_Descriptor_Function pfDescriptorFunction;
161 unsigned long lPluginIndex;
165 = (LADSPA_Descriptor_Function)dlsym(pvLADSPAPluginLibrary,
166 "ladspa_descriptor");
167 if (!pfDescriptorFunction) {
168 const char * pcError = dlerror();
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,
180 for (lPluginIndex = 0;; lPluginIndex++) {
181 psDescriptor = pfDescriptorFunction(lPluginIndex);
182 if (psDescriptor == NULL) {
184 "Unable to find label \"%s\" in plugin library file \"%s\".\n",
186 pcPluginLibraryFilename);
189 if (strcmp(psDescriptor->Label, pcPluginLabel) == 0)
194 /*****************************************************************************/