Replace code for finding ControlProtocols/Surface plugins with a portable equivalent.
authorTim Mayberry <mojofunk@gmail.com>
Wed, 27 Jun 2007 12:12:18 +0000 (12:12 +0000)
committerTim Mayberry <mojofunk@gmail.com>
Wed, 27 Jun 2007 12:12:18 +0000 (12:12 +0000)
Remove Session::control_protocol_path and the supporting non-portable
Session::suffixed_search_path from Session.

Add ARDOUR::control_protocol_search_path which is used in place of
Session::control_protocol_path

Replace ARDOUR::get_system_module_path with ARDOUR::system_module_directory which
is used by ARDOUR::control_protocol_search_path

Export ARDOUR_SURFACES_PATH in gtk2_ardour/ardev_common.sh which is returned
by ARDOUR::control_protocol_search_path if defined. This means the control surfaces
in the libs/surfaces/* directories can now be used without installing them.

Add pbd/file_utils.h/cc containing functions for finding files matching a certain
pattern

Update documentation in SearchPath and add another constructor that takes
a sys::path

git-svn-id: svn://localhost/ardour2/trunk@2049 d708f5d6-7413-0410-9779-e7cbd77b26cf

17 files changed:
gtk2_ardour/ardev_common.sh.in
libs/ardour/SConscript
libs/ardour/ardour/ardour.h
libs/ardour/ardour/control_protocol_manager.h
libs/ardour/ardour/control_protocol_search_path.h [new file with mode: 0644]
libs/ardour/ardour/filesystem_paths.h
libs/ardour/ardour/session.h
libs/ardour/control_protocol_manager.cc
libs/ardour/control_protocol_search_path.cc [new file with mode: 0644]
libs/ardour/filesystem_paths.cc
libs/ardour/globals.cc
libs/ardour/session_state.cc
libs/pbd/SConscript
libs/pbd/file_utils.cc [new file with mode: 0644]
libs/pbd/pbd/file_utils.h [new file with mode: 0644]
libs/pbd/pbd/search_path.h
libs/pbd/search_path.cc

index 235607163ce73950b897264feb2c8fafa7e78d25..8e8f73f6b88da23203e94243800ac7e3c2f9d1ba 100644 (file)
@@ -5,6 +5,7 @@ cd `dirname "$0"`/..
 export ARDOUR_PATH=.:gtk2_ardour/icons:gtk2_ardour/pixmaps:gtk2_ardour
 export GTK_PATH=libs/clearlooks
 
+export ARDOUR_SURFACES_PATH=libs/surfaces/frontier:libs/surfaces/generic_midi:libs/surfaces/mackie:libs/surfaces/tranzport
 
 export LD_LIBRARY_PATH=libs/surfaces/control_protocol:libs/ardour:libs/midi++2:libs/pbd:libs/soundtouch:libs/gtkmm2ext:libs/sigc++2:libs/glibmm2:libs/gtkmm2/atk:libs/gtkmm2/pango:libs/gtkmm2/gdk:libs/gtkmm2/gtk:libs/libgnomecanvasmm:libs/libsndfile:libs/appleutility:$LD_LIBRARY_PATH
 
index 1d66b1f6be3e1a033a392e7e3e01f8236d4bbe88..e2e84c386c0bc940bae3e53b092888b73ea51772 100644 (file)
@@ -66,6 +66,7 @@ automation.cc
 automation_event.cc
 configuration.cc
 control_protocol_manager.cc
+control_protocol_search_path.cc
 crossfade.cc
 curve.cc
 cycle_timer.cc
index c7bf7edcf8848345aec355bb5884fd61d2e3e559..e76c984ba42a89f70c1d6dcc74a76ef314e5e28f 100644 (file)
@@ -53,7 +53,6 @@ namespace ARDOUR {
        
        std::string get_user_ardour_path ();
        std::string get_system_data_path ();
-       std::string get_system_module_path ();
 
        std::string find_config_file (std::string name);
        std::string find_data_file (std::string name, std::string subdir = "" );
index e8dc0bf22c2a75a0593391ed298632cf80b8b388..c61513e117becf2da996baa9007b0d0d90a317f1 100644 (file)
@@ -58,7 +58,7 @@ struct ControlProtocolInfo {
        static ControlProtocolManager& instance() { return *_instance; }
 
        void set_session (Session&);
-       void discover_control_protocols (std::string search_path);
+       void discover_control_protocols ();
        void foreach_known_protocol (sigc::slot<void,const ControlProtocolInfo*>);
        void load_mandatory_protocols ();
 
diff --git a/libs/ardour/ardour/control_protocol_search_path.h b/libs/ardour/ardour/control_protocol_search_path.h
new file mode 100644 (file)
index 0000000..f9a8103
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+    Copyright (C) 2007 Tim Mayberry 
+
+    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.
+
+*/
+
+#ifndef ARDOUR_CONTROL_PROTOCOL_SEARCH_PATH_INCLUDED
+#define ARDOUR_CONTROL_PROTOCOL_SEARCH_PATH_INCLUDED
+
+#include <pbd/search_path.h>
+
+namespace ARDOUR {
+
+       using PBD::SearchPath;
+
+       /**
+        * return a SearchPath containing directories in which to look for 
+        * control surface plugins.
+        *
+        * If ARDOUR_SURFACES_PATH is defined then the SearchPath returned
+        * will contain only those directories specified in it, otherwise it will
+        * contain the user and system directories which may contain control
+        * surface plugins.
+        */
+       SearchPath control_protocol_search_path ();
+
+} // namespace ARDOUR
+
+#endif
index dc0d2576baa5f3beb1e447a986570960ebbd50dd..0e17327fa5b5dbb35af7d558afc81f0905213acc 100644 (file)
@@ -32,6 +32,12 @@ namespace ARDOUR {
         */
        sys::path user_config_directory ();
 
+       /**
+        * @return the path to the directory that contains the system wide ardour
+        * modules.
+        */
+       sys::path system_module_directory ();
+
 } // namespace ARDOUR
 
 #endif
index f0a175c624df433ddd8475e0366282d8c4474cbb..9001196b60d2afa6cfc2e2476b75a948c312fb54 100644 (file)
@@ -261,9 +261,6 @@ class Session : public PBD::StatefulDestructible
 
        std::string automation_dir () const;
 
-       static string suffixed_search_path (std::string suffix, bool data);
-       static string control_protocol_path ();
-       
        static string change_audio_path_by_name (string oldpath, string oldname, string newname, bool destructive);
        static string change_midi_path_by_name (string oldpath, string oldname, string newname, bool destructive);
        
index 00c05b280039fef867b74767bd9f1700f65ebee1..84791ab2c77fa5a6eac9597f07613af7f9dc27f8 100644 (file)
 #include <dlfcn.h>
 
 #include <pbd/compose.h>
+#include <pbd/file_utils.h>
 #include <pbd/error.h>
-#include <pbd/pathscanner.h>
 
 #include <control_protocol/control_protocol.h>
 
 #include <ardour/session.h>
 #include <ardour/control_protocol_manager.h>
+#include <ardour/control_protocol_search_path.h>
 
 using namespace ARDOUR;
 using namespace std;
@@ -168,15 +169,6 @@ ControlProtocolManager::teardown (ControlProtocolInfo& cpi)
        return 0;
 }
 
-static bool protocol_filter (const string& str, void *arg)
-{
-       /* Not a dotfile, has a prefix before a period, suffix is "so", or "dylib" */
-       
-       return str[0] != '.' 
-         && ((str.length() > 3 && str.find (".so") == (str.length() - 3))
-             || (str.length() > 6 && str.find (".dylib") == (str.length() - 6)));
-}
-
 void
 ControlProtocolManager::load_mandatory_protocols ()
 {
@@ -193,21 +185,24 @@ ControlProtocolManager::load_mandatory_protocols ()
 }
 
 void
-ControlProtocolManager::discover_control_protocols (string path)
+ControlProtocolManager::discover_control_protocols ()
 {
-       vector<string *> *found;
-       PathScanner scanner;
+       vector<sys::path> cp_modules;
 
-       info << string_compose (_("looking for control protocols in %1"), path) << endmsg;
+       Glib::PatternSpec so_extension_pattern("*.so");
+       Glib::PatternSpec dylib_extension_pattern("*.dylib");
 
-       found = scanner (path, protocol_filter, 0, false, true);
+       find_matching_files_in_search_path (control_protocol_search_path (),
+                       so_extension_pattern, cp_modules);
 
-       for (vector<string*>::iterator i = found->begin(); i != found->end(); ++i) {
-               control_protocol_discover (**i);
-               delete *i;
-       }
+       find_matching_files_in_search_path (control_protocol_search_path (),
+                       dylib_extension_pattern, cp_modules);
+
+       info << string_compose (_("looking for control protocols in %1"), control_protocol_search_path().get_string()) << endmsg;
 
-       delete found;
+       for (vector<sys::path>::iterator i = cp_modules.begin(); i != cp_modules.end(); ++i) {
+               control_protocol_discover ((*i).to_string());
+       }
 }
 
 int
diff --git a/libs/ardour/control_protocol_search_path.cc b/libs/ardour/control_protocol_search_path.cc
new file mode 100644 (file)
index 0000000..aac83c6
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+    Copyright (C) 2007 Tim Mayberry 
+
+    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 <glibmm/miscutils.h>
+
+#include <ardour/control_protocol_search_path.h>
+#include <ardour/directory_names.h>
+#include <ardour/filesystem_paths.h>
+
+namespace {
+       const char * const surfaces_env_variable_name = "ARDOUR_SURFACES_PATH";
+} // anonymous
+
+namespace ARDOUR {
+
+SearchPath
+control_protocol_search_path ()
+{
+       bool surfaces_path_defined = false;
+       SearchPath spath_env(Glib::getenv(surfaces_env_variable_name, surfaces_path_defined));
+       
+       if (surfaces_path_defined)
+       {
+               return spath_env;
+       }
+
+       SearchPath spath(user_config_directory ());
+
+       spath += system_module_directory ();
+
+       spath.add_subdirectory_to_paths(surfaces_dir_name);
+
+       return spath;
+}
+
+} // namespace ARDOUR
index 55a8a9983f6c4a16b16b9ceaa0f7399c64843084..465d07bddc86d93f9783334a00315e44fc1e86b8 100644 (file)
@@ -49,4 +49,12 @@ user_config_directory ()
        return p;
 }
 
+sys::path
+system_module_directory ()
+{
+       sys::path module_directory(MODULE_DIR);
+       module_directory /= "ardour2";
+       return module_directory;
+}
+
 } // namespace ARDOUR
index 1a5e2bb5b5cf8511ec61494c83bc0e1d37cdd0c5..c51d2339eaca61ce6a0ceee5a5be5f9346c777cf 100644 (file)
@@ -307,7 +307,7 @@ ARDOUR::init (bool use_vst, bool try_optimization)
        
        /* singleton - first object is "it" */
        new ControlProtocolManager ();
-       ControlProtocolManager::instance().discover_control_protocols (Session::control_protocol_path());
+       ControlProtocolManager::instance().discover_control_protocols ();
 
        XMLNode* node;
        if ((node = Config->control_protocol_state()) != 0) {
@@ -405,22 +405,6 @@ ARDOUR::get_system_data_path ()
        return path;
 }
 
-string
-ARDOUR::get_system_module_path ()
-{
-       string path;
-       char *envvar;
-
-       if ((envvar = getenv ("ARDOUR_MODULE_PATH")) != 0) {
-               path = envvar;
-       } else {
-               path += MODULE_DIR;
-               path += "/ardour2/";
-       }
-       
-       return path;
-}
-
 static string
 find_file (string name, string dir, string subdir = "")
 {
index b1a9b87536f2298591ed5937afb7219f9ab129c5..446743796d381fb3f45062e6aeeeb2bb418645eb 100644 (file)
@@ -56,7 +56,6 @@
 #include <pbd/pathscanner.h>
 #include <pbd/pthread_utils.h>
 #include <pbd/search_path.h>
-#include <pbd/strsplit.h>
 #include <pbd/stacktrace.h>
 #include <pbd/copyfile.h>
 
@@ -1839,46 +1838,6 @@ Session::automation_dir () const
        return res;
 }
 
-string
-Session::suffixed_search_path (string suffix, bool data)
-{
-       string path;
-
-       path += get_user_ardour_path();
-       if (path[path.length()-1] != ':') {
-               path += ':';
-       }
-
-       if (data) {
-               path += get_system_data_path();
-       } else {
-               path += get_system_module_path();
-       }
-
-       vector<string> split_path;
-       
-       split (path, split_path, ':');
-       path = "";
-
-       for (vector<string>::iterator i = split_path.begin(); i != split_path.end(); ++i) {
-               path += *i;
-               path += suffix;
-               path += '/';
-               
-               if (distance (i, split_path.end()) != 1) {
-                       path += ':';
-               }
-       }
-               
-       return path;
-}
-
-string
-Session::control_protocol_path ()
-{
-       return suffixed_search_path (surfaces_dir_name, false);
-}
-
 int
 Session::load_bundles (const XMLNode& node)
 {
index 3dee4183988b558d9016fffa1caad85f0e977d97..bb1f56e6c7b5dd8412beb457c5fe74f2835a9091 100644 (file)
@@ -28,6 +28,7 @@ enumwriter.cc
 dmalloc.cc
 error.cc
 filesystem.cc
+file_utils.cc
 fpu.cc
 id.cc
 mountpoint.cc
diff --git a/libs/pbd/file_utils.cc b/libs/pbd/file_utils.cc
new file mode 100644 (file)
index 0000000..30ecbc0
--- /dev/null
@@ -0,0 +1,132 @@
+/*
+    Copyright (C) 2007 Tim Mayberry 
+
+    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 <algorithm>
+
+#include <glibmm/fileutils.h>
+#include <glibmm/pattern.h>
+
+#include <pbd/compose.h>
+#include <pbd/file_utils.h>
+
+#include <pbd/error.h>
+
+namespace PBD {
+
+void
+get_files_in_directory (const sys::path& directory_path, vector<string>& result)
+{
+       if (!is_directory(directory_path)) return;
+
+       try
+       {
+               Glib::Dir dir(directory_path.to_string());
+               std::copy(dir.begin(), dir.end(), std::back_inserter(result));
+       }
+       catch (Glib::FileError& err)
+       {
+               warning << err.what();
+       }
+}
+
+void
+find_matching_files_in_directory (const sys::path& directory,
+                                  const Glib::PatternSpec& pattern,
+                                  vector<sys::path>& result)
+{
+       vector<string> tmp_files;
+
+       get_files_in_directory (directory, tmp_files);
+
+       for (vector<string>::iterator file_iter = tmp_files.begin();
+                       file_iter != tmp_files.end();
+                       ++file_iter)
+       {
+               if (!pattern.match(*file_iter)) continue;
+
+               sys::path full_path(directory);
+               full_path /= *file_iter;
+
+               result.push_back(full_path);
+       }
+}
+
+void
+find_matching_files_in_directories (const vector<sys::path>& paths,
+                                    const Glib::PatternSpec& pattern,
+                                    vector<sys::path>& result)
+{
+       for (vector<sys::path>::const_iterator path_iter = paths.begin();
+                       path_iter != paths.end();
+                       ++path_iter)
+       {
+               find_matching_files_in_directory (*path_iter, pattern, result);
+       }               
+}
+
+void
+find_matching_files_in_search_path (const SearchPath& search_path,
+                                    const Glib::PatternSpec& pattern,
+                                    vector<sys::path>& result)
+{
+       vector<sys::path> dirs;
+       std::copy(search_path.begin(), search_path.end(), std::back_inserter(dirs));
+       find_matching_files_in_directories (dirs, pattern, result);    
+}
+
+bool
+find_file_in_search_path(const SearchPath& search_path,
+                         const string& filename,
+                         sys::path& result)
+{
+       vector<sys::path> tmp;
+       Glib::PatternSpec tmp_pattern(filename);
+
+       find_matching_files_in_search_path (search_path, tmp_pattern, tmp);
+
+       if (tmp.size() == 0)
+       {
+               info << string_compose
+                       (
+                        "Found no file named %1 in search path %2",
+                        filename,
+                        search_path.get_string ()
+                       )
+                       << endmsg;
+
+               return false;
+       }
+
+       if (tmp.size() != 1)
+       {
+               info << string_compose
+                       (
+                        "Found more than one file matching %1 in search path %2",
+                        filename,
+                        search_path.get_string ()
+                       )
+                       << endmsg;
+       }
+
+       result = tmp.front();
+
+       return true;
+}
+
+} // namespace PBD
diff --git a/libs/pbd/pbd/file_utils.h b/libs/pbd/pbd/file_utils.h
new file mode 100644 (file)
index 0000000..a9832ce
--- /dev/null
@@ -0,0 +1,99 @@
+/*
+    Copyright (C) 2007 Tim Mayberry 
+
+    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.
+
+*/
+
+#ifndef PBD_FILE_UTILS_INCLUDED
+#define PBD_FILE_UTILS_INCLUDED
+
+#include <string>
+#include <vector>
+
+#include <glibmm/pattern.h>
+
+#include <pbd/search_path.h>
+
+namespace PBD {
+
+using std::string;
+using std::vector;
+
+/**
+ * Get a list of files in a directory.
+ * @note You must join path with result to get the absolute path
+ * to the file.
+ *
+ * @param path An Absolute path to a directory
+ * @param result A vector of filenames.
+ */
+void
+get_files_in_directory (const sys::path& path,
+                        vector<string>& result);
+
+/**
+ * Takes a directory path and returns all the files in the directory
+ * matching a particular pattern.
+ *
+ * @param directory A directory path
+ * @param pattern A Glib::PatternSpec used to match the files.
+ * @param result A vector in which to place the resulting matches.
+ */
+void
+find_matching_files_in_directory (const sys::path& directory,
+                                  const Glib::PatternSpec& pattern,
+                                  vector<sys::path>& result);
+
+/**
+ * Takes a number of directory paths and returns all the files matching
+ * a particular pattern.
+ *
+ * @param paths A vector containing the Absolute paths
+ * @param pattern A Glib::PatternSpec used to match the files
+ * @param result A vector in which to place the resulting matches.
+ */
+void
+find_matching_files_in_directories (const vector<sys::path>& directory_paths,
+                                    const Glib::PatternSpec& pattern,
+                                    vector<sys::path>& result);
+
+/**
+ * Takes a SearchPath and puts a list of all the files in the search path
+ * that match pattern into the result vector.
+ *
+ * @param search_path A SearchPath
+ * @param pattern A Glib::PatternSpec used to match the files
+ * @param result A vector in which to place the resulting matches.
+ */
+void
+find_matching_files_in_search_path (const SearchPath& search_path,
+                                    const Glib::PatternSpec& pattern,
+                                    vector<sys::path>& result);
+
+/**
+ * Takes a search path and a file name and place the full path
+ * to that file in result if it is found within the search path.
+ *
+ * @return true If file is found within the search path.
+ */
+bool
+find_file_in_search_path (const SearchPath& search_path,
+                          const string& filename,
+                          sys::path& result);
+                       
+} // namespace PBD
+
+#endif
index 18f05ef0142a8d2bb10604affa5799229cae713c..e51d5de4ad5014404212b734de1f686973f662e2 100644 (file)
@@ -53,13 +53,25 @@ public:
        SearchPath ();
 
        /**
-        * Initialize SearchPath from a string each path may or may not
-        * exist.
+        * Initialize SearchPath from a string where the string contains
+        * one or more absolute paths to directories which are delimited 
+        * by a path separation character. The path delimeter is a 
+        * colon(:) on unix and a semi-colon(;) on windows.
+        *
+        * Each path contained in the search path may or may not resolve to
+        * an existing directory in the filesystem.
         * 
         * @param search_path A path string.
         */
        SearchPath (const string& search_path);
 
+       /**
+        * Initialize SearchPath from a sys::path.
+        *
+        * @param directory_path A directory path.
+        */
+       SearchPath (const sys::path& directory_path);
+
        /**
         * Initialize SearchPath from a vector of paths that may or may
         * not exist.
index 146fd7eaf08b6d811a91c3f6e99ef7b75a0dc658..b3590ba7bdcc01aa4bad32e5034eae954174ce8a 100644 (file)
@@ -42,14 +42,15 @@ SearchPath::SearchPath (const string& path)
 {
        vector<sys::path> tmp;
 
-       if(!tokenize ( path, string(path_delimiter), std::back_inserter (tmp)))
+       if(tokenize (path, string(path_delimiter), std::back_inserter (tmp)))
        {
-               // log warning(info perhaps?) that the path is empty 
-               warning << "SearchPath contains no tokens" << endmsg;
-
+               add_directories (tmp);
        }
+}
 
-       add_directories (tmp);
+SearchPath::SearchPath (const sys::path& directory_path)
+{
+       add_directory (directory_path);
 }
 
 SearchPath::SearchPath (const vector<sys::path>& paths)