Fix finding panner modules on windows by looking for files with *.dll extension
[ardour.git] / libs / ardour / panner_manager.cc
index 8644ae2ccea97dff2a6ab57fd8c9c8785f5bae2d..4e4ad7fe4d43106597c8df94e67b3a210ff9817e 100644 (file)
@@ -1,10 +1,38 @@
+/*
+    Copyright (C) 2011 Paul Davis
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+*/
+
+#include <unistd.h>
+
 #include <glibmm/pattern.h>
+#include <glibmm/fileutils.h>
 
 #include "pbd/error.h"
 #include "pbd/compose.h"
 #include "pbd/file_utils.h"
 
+#include "ardour/debug.h"
 #include "ardour/panner_manager.h"
+
+#ifdef SearchPath
+#undef SearchPath
+#endif
+
 #include "ardour/panner_search_path.h"
 
 #include "i18n.h"
@@ -21,50 +49,65 @@ PannerManager::PannerManager ()
 
 PannerManager::~PannerManager ()
 {
-        for (list<PannerInfo*>::iterator p = panner_info.begin(); p != panner_info.end(); ++p) {
-                delete *p;
-        }
+       for (list<PannerInfo*>::iterator p = panner_info.begin(); p != panner_info.end(); ++p) {
+               delete *p;
+       }
 }
 
 PannerManager&
 PannerManager::instance ()
 {
-        if (_instance == 0) {
-                _instance = new PannerManager ();
-        }
+       if (_instance == 0) {
+               _instance = new PannerManager ();
+       }
 
-        return *_instance;
+       return *_instance;
 }
 
 void
 PannerManager::discover_panners ()
 {
-       vector<sys::path> panner_modules;
+       vector<std::string> panner_modules;
 
        Glib::PatternSpec so_extension_pattern("*.so");
        Glib::PatternSpec dylib_extension_pattern("*.dylib");
+       Glib::PatternSpec dll_extension_pattern("*.dll");
+
+       find_matching_files_in_search_path (panner_search_path (),
+                                           so_extension_pattern, panner_modules);
 
        find_matching_files_in_search_path (panner_search_path (),
-                       so_extension_pattern, panner_modules);
+                                           dylib_extension_pattern, panner_modules);
 
        find_matching_files_in_search_path (panner_search_path (),
-                       dylib_extension_pattern, panner_modules);
+                                           dll_extension_pattern, panner_modules);
 
-       info << string_compose (_("looking for panners in %1"), panner_search_path().to_string()) << endmsg;
+       DEBUG_TRACE (DEBUG::Panning, string_compose (_("looking for panners in %1"), panner_search_path().to_string()));
 
-       for (vector<sys::path>::iterator i = panner_modules.begin(); i != panner_modules.end(); ++i) {
-               panner_discover ((*i).to_string());
+       for (vector<std::string>::iterator i = panner_modules.begin(); i != panner_modules.end(); ++i) {
+               panner_discover (*i);
        }
 }
 int
 PannerManager::panner_discover (string path)
 {
-        PannerInfo* pinfo;
+       PannerInfo* pinfo;
 
        if ((pinfo = get_descriptor (path)) != 0) {
-                panner_info.push_back (pinfo);
-                info << string_compose(_("Panner discovered: \"%1\""), pinfo->descriptor.name) << endmsg;
-        }
+
+               list<PannerInfo*>::iterator i;
+
+               for (i = panner_info.begin(); i != panner_info.end(); ++i) {
+                       if (pinfo->descriptor.name == (*i)->descriptor.name) {
+                               break;
+                       }
+               }
+
+               if (i == panner_info.end()) {
+                       panner_info.push_back (pinfo);
+                       DEBUG_TRACE (DEBUG::Panning, string_compose(_("Panner discovered: \"%1\" in %2"), pinfo->descriptor.name, path));
+               }
+       }
 
        return 0;
 }
@@ -72,31 +115,33 @@ PannerManager::panner_discover (string path)
 PannerInfo*
 PannerManager::get_descriptor (string path)
 {
-       void *module;
-        PannerInfo* info = 0;
+       Glib::Module* module = new Glib::Module(path);
+       PannerInfo* info = 0;
        PanPluginDescriptor *descriptor = 0;
        PanPluginDescriptor* (*dfunc)(void);
-       const char *errstr;
+       void* func = 0;
 
-       if ((module = dlopen (path.c_str(), RTLD_NOW)) == 0) {
-               error << string_compose(_("PannerManager: cannot load module \"%1\" (%2)"), path, dlerror()) << endmsg;
+       if (!module) {
+               error << string_compose(_("PannerManager: cannot load module \"%1\" (%2)"), path,
+                               Glib::Module::get_last_error()) << endmsg;
+               delete module;
                return 0;
        }
 
-       dfunc = (PanPluginDescriptor* (*)(void)) dlsym (module, "panner_descriptor");
-
-       if ((errstr = dlerror()) != 0) {
+       if (!module->get_symbol("panner_descriptor", func)) {
                error << string_compose(_("PannerManager: module \"%1\" has no descriptor function."), path) << endmsg;
-               error << errstr << endmsg;
-               dlclose (module);
+               error << Glib::Module::get_last_error() << endmsg;
+               delete module;
                return 0;
        }
 
+       dfunc = (PanPluginDescriptor* (*)(void))func;
        descriptor = dfunc();
+
        if (descriptor) {
-                info = new PannerInfo (*descriptor, module);
+               info = new PannerInfo (*descriptor, module);
        } else {
-               dlclose (module);
+               delete module;
        }
 
        return info;
@@ -105,51 +150,51 @@ PannerManager::get_descriptor (string path)
 PannerInfo*
 PannerManager::select_panner (ChanCount in, ChanCount out)
 {
-        PanPluginDescriptor* d;
-        int32_t nin = in.n_audio();
-        int32_t nout = out.n_audio();
+       PanPluginDescriptor* d;
+       int32_t nin = in.n_audio();
+       int32_t nout = out.n_audio();
 
-        /* look for exact match first */
+       /* look for exact match first */
 
-        for (list<PannerInfo*>::iterator p = panner_info.begin(); p != panner_info.end(); ++p) {
-                d = &(*p)->descriptor;
+       for (list<PannerInfo*>::iterator p = panner_info.begin(); p != panner_info.end(); ++p) {
+               d = &(*p)->descriptor;
 
-                if (d->in == nin && d->out == nout) {
-                        return *p;
-                }
-        }
+               if (d->in == nin && d->out == nout) {
+                       return *p;
+               }
+       }
 
-        /* no exact match, look for good fit on inputs and variable on outputs */
+       /* no exact match, look for good fit on inputs and variable on outputs */
 
-        for (list<PannerInfo*>::iterator p = panner_info.begin(); p != panner_info.end(); ++p) {
-                d = &(*p)->descriptor;
+       for (list<PannerInfo*>::iterator p = panner_info.begin(); p != panner_info.end(); ++p) {
+               d = &(*p)->descriptor;
 
-                if (d->in == nin && d->out == -1) {
-                        return *p;
-                }
-        }
+               if (d->in == nin && d->out == -1) {
+                       return *p;
+               }
+       }
 
-        /* no exact match, look for good fit on outputs and variable on inputs */
+       /* no exact match, look for good fit on outputs and variable on inputs */
 
-        for (list<PannerInfo*>::iterator p = panner_info.begin(); p != panner_info.end(); ++p) {
-                d = &(*p)->descriptor;
+       for (list<PannerInfo*>::iterator p = panner_info.begin(); p != panner_info.end(); ++p) {
+               d = &(*p)->descriptor;
 
-                if (d->in == -1 && d->out == nout) {
-                        return *p;
-                }
-        }
+               if (d->in == -1 && d->out == nout) {
+                       return *p;
+               }
+       }
 
-        /* no exact match, look for variable fit on inputs and outputs */
+       /* no exact match, look for variable fit on inputs and outputs */
 
-        for (list<PannerInfo*>::iterator p = panner_info.begin(); p != panner_info.end(); ++p) {
-                d = &(*p)->descriptor;
+       for (list<PannerInfo*>::iterator p = panner_info.begin(); p != panner_info.end(); ++p) {
+               d = &(*p)->descriptor;
 
-                if (d->in == -1 && d->out == -1) {
-                        return *p;
-                }
-        }
+               if (d->in == -1 && d->out == -1) {
+                       return *p;
+               }
+       }
 
-        warning << string_compose (_("no panner discovered for in/out = %1/%2"), nin, nout) << endmsg;
+       warning << string_compose (_("no panner discovered for in/out = %1/%2"), nin, nout) << endmsg;
 
-        return 0;
+       return 0;
 }