Initial commit
[resorg.git] / ladspa / search.c
blobc7d750e8e07d2679cecee5cb4c532935b405b756
1 /*
2 * This file is part of Resource Organizer.
4 * Copyright (C) 2014 Nikita Zlobin <nick87720z@gmail.com>
6 * Resource Organizer is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * Resource Organizer is distributed in the hope that it will be
12 * useful, 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.
16 * You should have received a copy of the GNU General Public License
17 * along with Resource Organizer. If not, see
18 * <http://www.gnu.org/licenses/>.
21 #include <dirent.h>
22 #include <dlfcn.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28 #include <ladspa.h>
30 #include <resource.h>
31 #include <search-so.h>
33 static ResourceType rtype = {
34 .label = "ladspa",
35 .name = "LADSPA"
38 static ResourceHandlerFunc res_handler;
40 static void
41 describeLadspaPluginLibrary (const char * fileName)
43 void * dl_Handle;
45 if (strcmp (fileName + strlen (fileName) - strlen (".so"), ".so") != 0) return;
47 // FIXME: Strange leaks in dlopen(), need valgrind self-check
48 dl_Handle = dlopen (fileName, RTLD_NOW | RTLD_LOCAL);
49 if (! dl_Handle) {
50 fprintf (stderr, "dlerror: %s\n", dlerror());
51 return;
54 /* This is a file and the file is a shared library! */
55 LADSPA_Descriptor_Function descFunc;
57 dlerror();
58 descFunc = (LADSPA_Descriptor_Function) dlsym (dl_Handle, "ladspa_descriptor");
59 if (dlerror() == NULL && descFunc) {
60 const LADSPA_Descriptor * plugDesc;
61 long lIndex = 0;
63 Resource res = { .type = & rtype,
64 .file = (char *)fileName,
65 .url = NULL };
66 size_t base_size = strlen (":/") + strlen (res.type->label) + strlen (fileName) + 1;
68 // FIXME: Strange leaks in descFunc(), need valgrind self-check
69 for( ; (plugDesc = descFunc (lIndex)) != NULL; lIndex++ )
71 size_t l_size, old_size = 0;
72 char * url = NULL, * url_label;
74 l_size = strlen (plugDesc->Label);
75 if (l_size > old_size)
76 url = realloc (url, base_size + l_size);
78 if (url != res.url)
80 url_label = (res.url = url) + base_size - 1;
81 sprintf (url, "%s:%s/", res.type->label, fileName);
83 sprintf (url_label, "%s", plugDesc->Label);
85 res.name = (char *)plugDesc->Name;
86 res.author = (char *)plugDesc->Maker;
87 res_handler (& res);
89 free (res.url);
91 dlclose (dl_Handle);
94 void ladspaPathIterate (void)
96 pathIterate ("LADSPA_PATH", describeLadspaPluginLibrary);
99 void ladspaResourceHandler (ResourceHandlerFunc cb_func)
101 res_handler = cb_func;