don't look for panners in symlinks to avoid finding "duplicate" libs
[ardour.git] / libs / ardour / panner_manager.cc
1 /*
2     Copyright (C) 2011 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <unistd.h>
21
22 #include <glibmm/pattern.h>
23
24 #include "pbd/error.h"
25 #include "pbd/compose.h"
26 #include "pbd/file_utils.h"
27 #include "pbd/symlink.h"
28
29 #include "ardour/panner_manager.h"
30 #include "ardour/panner_search_path.h"
31
32 #include "i18n.h"
33
34 using namespace std;
35 using namespace ARDOUR;
36 using namespace PBD;
37
38 PannerManager* PannerManager::_instance = 0;
39
40 PannerManager::PannerManager ()
41 {
42 }
43
44 PannerManager::~PannerManager ()
45 {
46         for (list<PannerInfo*>::iterator p = panner_info.begin(); p != panner_info.end(); ++p) {
47                 delete *p;
48         }
49 }
50
51 PannerManager&
52 PannerManager::instance ()
53 {
54         if (_instance == 0) {
55                 _instance = new PannerManager ();
56         }
57
58         return *_instance;
59 }
60
61 void
62 PannerManager::discover_panners ()
63 {
64         vector<sys::path> panner_modules;
65
66         Glib::PatternSpec so_extension_pattern("*.so");
67         Glib::PatternSpec dylib_extension_pattern("*.dylib");
68
69         find_matching_files_in_search_path (panner_search_path (),
70                                             so_extension_pattern, panner_modules);
71
72         find_matching_files_in_search_path (panner_search_path (),
73                                             dylib_extension_pattern, panner_modules);
74
75         info << string_compose (_("looking for panners in %1"), panner_search_path().to_string()) << endmsg;
76
77         for (vector<sys::path>::iterator i = panner_modules.begin(); i != panner_modules.end(); ++i) {
78                 panner_discover ((*i).to_string());
79         }
80 }
81 int
82 PannerManager::panner_discover (string path)
83 {
84         PannerInfo* pinfo;
85
86         /* don't look in symlinks, because otherwise we find too many versions
87          * of the same library.
88          */
89
90         if (PBD::is_symlink (path)) {
91                 return 0;
92         }
93
94         if ((pinfo = get_descriptor (path)) != 0) {
95                 panner_info.push_back (pinfo);
96                 info << string_compose(_("Panner discovered: \"%1\" in %2"), pinfo->descriptor.name, path) << endmsg;
97         }
98
99         return 0;
100 }
101
102 PannerInfo*
103 PannerManager::get_descriptor (string path)
104 {
105         void *module;
106         PannerInfo* info = 0;
107         PanPluginDescriptor *descriptor = 0;
108         PanPluginDescriptor* (*dfunc)(void);
109         const char *errstr;
110
111         if ((module = dlopen (path.c_str(), RTLD_NOW)) == 0) {
112                 error << string_compose(_("PannerManager: cannot load module \"%1\" (%2)"), path, dlerror()) << endmsg;
113                 return 0;
114         }
115
116         dfunc = (PanPluginDescriptor* (*)(void)) dlsym (module, "panner_descriptor");
117
118         if ((errstr = dlerror()) != 0) {
119                 error << string_compose(_("PannerManager: module \"%1\" has no descriptor function."), path) << endmsg;
120                 error << errstr << endmsg;
121                 dlclose (module);
122                 return 0;
123         }
124
125         descriptor = dfunc();
126         if (descriptor) {
127                 info = new PannerInfo (*descriptor, module);
128         } else {
129                 dlclose (module);
130         }
131
132         return info;
133 }
134
135 PannerInfo*
136 PannerManager::select_panner (ChanCount in, ChanCount out)
137 {
138         PanPluginDescriptor* d;
139         int32_t nin = in.n_audio();
140         int32_t nout = out.n_audio();
141
142         /* look for exact match first */
143
144         for (list<PannerInfo*>::iterator p = panner_info.begin(); p != panner_info.end(); ++p) {
145                 d = &(*p)->descriptor;
146
147                 if (d->in == nin && d->out == nout) {
148                         return *p;
149                 }
150         }
151
152         /* no exact match, look for good fit on inputs and variable on outputs */
153
154         for (list<PannerInfo*>::iterator p = panner_info.begin(); p != panner_info.end(); ++p) {
155                 d = &(*p)->descriptor;
156
157                 if (d->in == nin && d->out == -1) {
158                         return *p;
159                 }
160         }
161
162         /* no exact match, look for good fit on outputs and variable on inputs */
163
164         for (list<PannerInfo*>::iterator p = panner_info.begin(); p != panner_info.end(); ++p) {
165                 d = &(*p)->descriptor;
166
167                 if (d->in == -1 && d->out == nout) {
168                         return *p;
169                 }
170         }
171
172         /* no exact match, look for variable fit on inputs and outputs */
173
174         for (list<PannerInfo*>::iterator p = panner_info.begin(); p != panner_info.end(); ++p) {
175                 d = &(*p)->descriptor;
176
177                 if (d->in == -1 && d->out == -1) {
178                         return *p;
179                 }
180         }
181
182         warning << string_compose (_("no panner discovered for in/out = %1/%2"), nin, nout) << endmsg;
183
184         return 0;
185 }