merge with master.
[ardour.git] / libs / ardour / session_state.cc
index c985f8810a6bf10dbd73cd1d6f52c70acd142b68..eaf9f08b257693702e57771d618fcc9720b17cfb 100644 (file)
 #include <unistd.h>
 #include <sys/stat.h>
 #include <climits>
-#include <fcntl.h>
-#include <poll.h>
 #include <signal.h>
-#include <sys/mman.h>
 #include <sys/time.h>
 
 #ifdef HAVE_SYS_VFS_H
 #include <sys/vfs.h>
-#else
+#endif
+
+#ifdef __APPLE__
 #include <sys/param.h>
 #include <sys/mount.h>
 #endif
 #include "pbd/enumwriter.h"
 #include "pbd/error.h"
 #include "pbd/file_utils.h"
-#include "pbd/pathscanner.h"
+#include "pbd/pathexpand.h"
 #include "pbd/pthread_utils.h"
 #include "pbd/stacktrace.h"
 #include "pbd/convert.h"
-#include "pbd/clear_dir.h"
+#include "pbd/localtime_r.h"
 
 #include "ardour/amp.h"
+#include "ardour/async_midi_port.h"
 #include "ardour/audio_diskstream.h"
 #include "ardour/audio_track.h"
 #include "ardour/audioengine.h"
@@ -91,6 +91,7 @@
 #include "ardour/midi_model.h"
 #include "ardour/midi_patch_manager.h"
 #include "ardour/midi_region.h"
+#include "ardour/midi_scene_changer.h"
 #include "ardour/midi_source.h"
 #include "ardour/midi_track.h"
 #include "ardour/pannable.h"
@@ -135,23 +136,8 @@ Session::pre_engine_init (string fullpath)
 
        /* discover canonical fullpath */
 
-       char buf[PATH_MAX+1];
+       _path = canonical_path(fullpath);
 
-       if (!realpath (fullpath.c_str(), buf)) {
-               if (errno == ENOENT) {
-                       /* fullpath does not exist yet, so realpath() returned
-                        * ENOENT. Just use it as-is
-                        */
-                       _path = fullpath;
-               } else {
-                       error << string_compose(_("Could not use path %1 (%2)"), buf, strerror(errno)) << endmsg;
-                       destroy ();
-                       throw failed_constructor();
-               }
-       } else {
-               _path = string(buf);
-       }
-       
        /* we require _path to end with a dir separator */
 
        if (_path[_path.length()-1] != G_DIR_SEPARATOR) {
@@ -221,6 +207,16 @@ Session::post_engine_init ()
        BootMessage (_("Using configuration"));
 
        _midi_ports = new MidiPortManager;
+       
+       MIDISceneChanger* msc;
+
+       _scene_changer = msc = new MIDISceneChanger (*this);
+       msc->set_input_port (scene_input_port());
+       msc->set_output_port (scene_out());
+
+       boost::function<framecnt_t(void)> timer_func (boost::bind (&Session::audible_frame, this));
+       boost::dynamic_pointer_cast<AsyncMIDIPort>(scene_in())->set_timer (timer_func);
+
        setup_midi_machine_control ();
        
        if (_butler->start_thread()) {
@@ -363,7 +359,7 @@ Session::post_engine_init ()
 string
 Session::raid_path () const
 {
-       SearchPath raid_search_path;
+       Searchpath raid_search_path;
 
        for (vector<space_and_path>::const_iterator i = session_dirs.begin(); i != session_dirs.end(); ++i) {
                raid_search_path += (*i).path;
@@ -384,11 +380,11 @@ Session::setup_raid_path (string path)
 
        session_dirs.clear ();
 
-       SearchPath search_path(path);
-       SearchPath sound_search_path;
-       SearchPath midi_search_path;
+       Searchpath search_path(path);
+       Searchpath sound_search_path;
+       Searchpath midi_search_path;
 
-       for (SearchPath::const_iterator i = search_path.begin(); i != search_path.end(); ++i) {
+       for (Searchpath::const_iterator i = search_path.begin(); i != search_path.end(); ++i) {
                sp.path = *i;
                sp.blocks = 0; // not needed
                session_dirs.push_back (sp);
@@ -673,6 +669,12 @@ Session::save_state (string snapshot_name, bool pending, bool switch_to_snapshot
                return 1;
        }
 
+       if (g_atomic_int_get(&_suspend_save)) {
+               _save_queued = true;
+               return 1;
+       }
+       _save_queued = false;
+
        if (!_engine.connected ()) {
                error << string_compose (_("the %1 audio engine is not connected and state saving would lose all I/O connections. Session not saved"),
                                          PROGRAM_NAME)
@@ -734,9 +736,9 @@ Session::save_state (string snapshot_name, bool pending, bool switch_to_snapshot
 
        } else {
 
-               if (::rename (tmp_path.c_str(), xml_path.c_str()) != 0) {
-                       error << string_compose (_("could not rename temporary session file %1 to %2"),
-                                       tmp_path, xml_path) << endmsg;
+               if (::g_rename (tmp_path.c_str(), xml_path.c_str()) != 0) {
+                       error << string_compose (_("could not rename temporary session file %1 to %2 (%3)"),
+                                       tmp_path, xml_path, g_strerror(errno)) << endmsg;
                        if (g_remove (tmp_path.c_str()) != 0) {
                                error << string_compose(_("Could not remove temporary session file at path \"%1\" (%2)"),
                                                tmp_path, g_strerror (errno)) << endmsg;
@@ -812,7 +814,7 @@ Session::load_state (string snapshot_name)
 
        set_dirty();
 
-       _writable = exists_and_writable (xmlpath);
+       _writable = exists_and_writable (xmlpath) && exists_and_writable(Glib::path_get_dirname(xmlpath));
 
        if (!state_tree->read (xmlpath)) {
                error << string_compose(_("Could not understand session file %1"), xmlpath) << endmsg;
@@ -877,6 +879,12 @@ Session::load_options (const XMLNode& node)
        return 0;
 }
 
+bool
+Session::save_default_options ()
+{
+       return config.save_state();
+}
+
 XMLNode&
 Session::get_state()
 {
@@ -930,7 +938,7 @@ Session::state (bool full_state)
                                p += (*i).path;
 
                                if (next != session_dirs.end()) {
-                                       p += ':';
+                                       p += G_SEARCHPATH_SEPARATOR;
                                } else {
                                        break;
                                }
@@ -1423,7 +1431,13 @@ Session::XMLRouteFactory (const XMLNode& node, int version)
                 ret = track;
 
        } else {
-               boost::shared_ptr<Route> r (new Route (*this, X_("toBeResetFroXML")));
+               enum Route::Flag flags = Route::Flag(0);
+               const XMLProperty* prop = node.property("flags");
+               if (prop) {
+                       flags = Route::Flag (string_2_enum (prop->value(), flags));
+               }
+
+               boost::shared_ptr<Route> r (new Route (*this, X_("toBeResetFroXML"), flags));
 
                 if (r->init () == 0 && r->set_state (node, version) == 0) {
 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
@@ -1495,7 +1509,13 @@ Session::XMLRouteFactory_2X (const XMLNode& node, int version)
                 ret = track;
 
        } else {
-               boost::shared_ptr<Route> r (new Route (*this, X_("toBeResetFroXML")));
+               enum Route::Flag flags = Route::Flag(0);
+               const XMLProperty* prop = node.property("flags");
+               if (prop) {
+                       flags = Route::Flag (string_2_enum (prop->value(), flags));
+               }
+
+               boost::shared_ptr<Route> r (new Route (*this, X_("toBeResetFroXML"), flags));
 
                 if (r->init () == 0 && r->set_state (node, version) == 0) {
 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
@@ -2014,6 +2034,54 @@ Session::refresh_disk_space ()
                        _total_free_4k_blocks_uncertain = true;
                }
        }
+#elif defined (COMPILER_MSVC)
+       vector<string> scanned_volumes;
+       vector<string>::iterator j;
+       vector<space_and_path>::iterator i;
+    DWORD nSectorsPerCluster, nBytesPerSector,
+          nFreeClusters, nTotalClusters;
+    char disk_drive[4];
+       bool volume_found;
+
+       _total_free_4k_blocks = 0;
+
+       for (i = session_dirs.begin(); i != session_dirs.end(); i++) {
+               strncpy (disk_drive, (*i).path.c_str(), 3);
+               disk_drive[3] = 0;
+               strupr(disk_drive);
+
+               volume_found = false;
+               if (0 != (GetDiskFreeSpace(disk_drive, &nSectorsPerCluster, &nBytesPerSector, &nFreeClusters, &nTotalClusters)))
+               {
+                       int64_t nBytesPerCluster = nBytesPerSector * nSectorsPerCluster;
+                       int64_t nFreeBytes = nBytesPerCluster * (int64_t)nFreeClusters;
+                       i->blocks = (uint32_t)(nFreeBytes / 4096);
+
+                       for (j = scanned_volumes.begin(); j != scanned_volumes.end(); j++) {
+                               if (0 == j->compare(disk_drive)) {
+                                       volume_found = true;
+                                       break;
+                               }
+                       }
+
+                       if (!volume_found) {
+                               scanned_volumes.push_back(disk_drive);
+                               _total_free_4k_blocks += i->blocks;
+                       }
+               }
+       }
+
+       if (0 == _total_free_4k_blocks) {
+               strncpy (disk_drive, path().c_str(), 3);
+               disk_drive[3] = 0;
+
+               if (0 != (GetDiskFreeSpace(disk_drive, &nSectorsPerCluster, &nBytesPerSector, &nFreeClusters, &nTotalClusters)))
+               {
+                       int64_t nBytesPerCluster = nBytesPerSector * nSectorsPerCluster;
+                       int64_t nFreeBytes = nBytesPerCluster * (int64_t)nFreeClusters;
+                       _total_free_4k_blocks = (uint32_t)(nFreeBytes / 4096);
+               }
+       }
 #endif
 }
 
@@ -2200,22 +2268,16 @@ Session::auto_save()
 }
 
 static bool
-state_file_filter (const string &str, void */*arg*/)
+state_file_filter (const string &str, void/*arg*/)
 {
        return (str.length() > strlen(statefile_suffix) &&
                str.find (statefile_suffix) == (str.length() - strlen (statefile_suffix)));
 }
 
-struct string_cmp {
-       bool operator()(const string* a, const string* b) {
-               return *a < *b;
-       }
-};
-
-static string*
-remove_end(string* state)
+static string
+remove_end(string state)
 {
-       string statename(*state);
+       string statename(state);
 
        string::size_type start,end;
        if ((start = statename.find_last_of (G_DIR_SEPARATOR)) != string::npos) {
@@ -2226,24 +2288,23 @@ remove_end(string* state)
                end = statename.length();
        }
 
-       return new string(statename.substr (0, end));
+       return string(statename.substr (0, end));
 }
 
-vector<string *> *
+vector<string>
 Session::possible_states (string path)
 {
-       PathScanner scanner;
-       vector<string*>* states = scanner (path, state_file_filter, 0, false, false);
+       vector<string> states;
+       find_files_matching_filter (states, path, state_file_filter, 0, false, false);
 
-       transform(states->begin(), states->end(), states->begin(), remove_end);
+       transform(states.begin(), states.end(), states.begin(), remove_end);
 
-       string_cmp cmp;
-       sort (states->begin(), states->end(), cmp);
+       sort (states.begin(), states.end());
 
        return states;
 }
 
-vector<string *> *
+vector<string>
 Session::possible_states () const
 {
        return possible_states(_path);
@@ -2377,7 +2438,7 @@ Session::commit_reversible_command (Command *cmd)
 }
 
 static bool
-accept_all_audio_files (const string& path, void */*arg*/)
+accept_all_audio_files (const string& path, void/*arg*/)
 {
         if (!Glib::file_test (path, Glib::FILE_TEST_IS_REGULAR)) {
                 return false;
@@ -2391,7 +2452,7 @@ accept_all_audio_files (const string& path, void */*arg*/)
 }
 
 static bool
-accept_all_midi_files (const string& path, void */*arg*/)
+accept_all_midi_files (const string& path, void/*arg*/)
 {
         if (!Glib::file_test (path, Glib::FILE_TEST_IS_REGULAR)) {
                 return false;
@@ -2403,7 +2464,7 @@ accept_all_midi_files (const string& path, void */*arg*/)
 }
 
 static bool
-accept_all_state_files (const string& path, void */*arg*/)
+accept_all_state_files (const string& path, void/*arg*/)
 {
         if (!Glib::file_test (path, Glib::FILE_TEST_IS_REGULAR)) {
                 return false;
@@ -2467,8 +2528,7 @@ Session::find_all_sources (string path, set<string>& result)
 int
 Session::find_all_sources_across_snapshots (set<string>& result, bool exclude_this_snapshot)
 {
-       PathScanner scanner;
-       vector<string*>* state_files;
+       vector<string> state_files;
        string ripped;
        string this_snapshot_path;
 
@@ -2480,9 +2540,9 @@ Session::find_all_sources_across_snapshots (set<string>& result, bool exclude_th
                ripped = ripped.substr (0, ripped.length() - 1);
        }
 
-       state_files = scanner (ripped, accept_all_state_files, (void *) 0, true, true);
+       find_files_matching_filter (state_files, ripped, accept_all_state_files, (void *) 0, true, true);
 
-       if (state_files == 0) {
+       if (state_files.empty()) {
                /* impossible! */
                return 0;
        }
@@ -2491,13 +2551,13 @@ Session::find_all_sources_across_snapshots (set<string>& result, bool exclude_th
        this_snapshot_path += legalize_for_path (_current_snapshot_name);
        this_snapshot_path += statefile_suffix;
 
-       for (vector<string*>::iterator i = state_files->begin(); i != state_files->end(); ++i) {
+       for (vector<string>::iterator i = state_files.begin(); i != state_files.end(); ++i) {
 
-               if (exclude_this_snapshot && **i == this_snapshot_path) {
+               if (exclude_this_snapshot && *i == this_snapshot_path) {
                        continue;
                }
 
-               if (find_all_sources (**i, result) < 0) {
+               if (find_all_sources (*i, result) < 0) {
                        return -1;
                }
        }
@@ -2547,18 +2607,18 @@ Session::cleanup_sources (CleanupReport& rep)
        // FIXME: needs adaptation to midi
 
        vector<boost::shared_ptr<Source> > dead_sources;
-       PathScanner scanner;
        string audio_path;
        string midi_path;
-       vector<space_and_path>::iterator i;
-       vector<space_and_path>::iterator nexti;
-       vector<string*>* candidates;
-       vector<string*>* candidates2;
+       vector<string> candidates;
        vector<string> unused;
        set<string> all_sources;
        bool used;
        string spath;
        int ret = -1;
+       string tmppath1;
+       string tmppath2;
+       Searchpath asp;
+       Searchpath msp;
 
        _state_of_the_state = (StateOfTheState) (_state_of_the_state | InCleanup);
 
@@ -2600,54 +2660,23 @@ Session::cleanup_sources (CleanupReport& rep)
 
        /* build a list of all the possible audio directories for the session */
 
-       for (i = session_dirs.begin(); i != session_dirs.end(); ) {
-
-               nexti = i;
-               ++nexti;
-
+       for (vector<space_and_path>::const_iterator i = session_dirs.begin(); i != session_dirs.end(); ++i) {
                SessionDirectory sdir ((*i).path);
-               audio_path += sdir.sound_path();
-
-               if (nexti != session_dirs.end()) {
-                       audio_path += ':';
-               }
-
-               i = nexti;
+               asp += sdir.sound_path();
        }
+       audio_path += asp.to_string();
 
 
        /* build a list of all the possible midi directories for the session */
 
-       for (i = session_dirs.begin(); i != session_dirs.end(); ) {
-
-               nexti = i;
-               ++nexti;
-
+       for (vector<space_and_path>::const_iterator i = session_dirs.begin(); i != session_dirs.end(); ++i) {
                SessionDirectory sdir ((*i).path);
-               midi_path += sdir.midi_path();
-
-               if (nexti != session_dirs.end()) {
-                       midi_path += ':';
-               }
-
-               i = nexti;
+               msp += sdir.midi_path();
        }
+       midi_path += msp.to_string();
 
-       candidates = scanner (audio_path, accept_all_audio_files, (void *) 0, true, true);
-       candidates2 = scanner (midi_path, accept_all_midi_files, (void *) 0, true, true);
-
-        /* merge them */
-
-        if (candidates) {
-                if (candidates2) {
-                        for (vector<string*>::iterator i = candidates2->begin(); i != candidates2->end(); ++i) {
-                                candidates->push_back (*i);
-                        }
-                        delete candidates2;
-                }
-        } else {
-                candidates = candidates2; // might still be null
-        }
+       find_files_matching_filter (candidates, audio_path, accept_all_audio_files, (void *) 0, true, true);
+       find_files_matching_filter (candidates, midi_path, accept_all_midi_files, (void *) 0, true, true);
 
        /* find all sources, but don't use this snapshot because the
           state file on disk still references sources we may have already
@@ -2687,44 +2716,26 @@ Session::cleanup_sources (CleanupReport& rep)
                 i = tmp;
        }
 
-       char tmppath1[PATH_MAX+1];
-       char tmppath2[PATH_MAX+1];
-
-        if (candidates) {
-                for (vector<string*>::iterator x = candidates->begin(); x != candidates->end(); ++x) {
-
-                        used = false;
-                        spath = **x;
-
-                        for (set<string>::iterator i = all_sources.begin(); i != all_sources.end(); ++i) {
+       for (vector<string>::iterator x = candidates.begin(); x != candidates.end(); ++x) {
 
-                                if (realpath(spath.c_str(), tmppath1) == 0) {
-                                        error << string_compose (_("Cannot expand path %1 (%2)"),
-                                                                 spath, strerror (errno)) << endmsg;
-                                        continue;
-                                }
+               used = false;
+               spath = *x;
 
-                                if (realpath((*i).c_str(),  tmppath2) == 0) {
-                                        error << string_compose (_("Cannot expand path %1 (%2)"),
-                                                                 (*i), strerror (errno)) << endmsg;
-                                        continue;
-                                }
+               for (set<string>::iterator i = all_sources.begin(); i != all_sources.end(); ++i) {
 
-                                if (strcmp(tmppath1, tmppath2) == 0) {
-                                        used = true;
-                                        break;
-                                }
-                        }
-
-                        if (!used) {
-                                unused.push_back (spath);
-                        }
+                       tmppath1 = canonical_path (spath);
+                       tmppath2 = canonical_path ((*i));
 
-                        delete *x;
-                }
+                       if (tmppath1 == tmppath2) {
+                               used = true;
+                               break;
+                       }
+               }
 
-                delete candidates;
-        }
+               if (!used) {
+                       unused.push_back (spath);
+               }
+       }
 
        /* now try to move all unused files into the "dead" directory(ies) */
 
@@ -2814,7 +2825,7 @@ Session::cleanup_sources (CleanupReport& rep)
                string peakpath = peak_path (base);
 
                if (Glib::file_test (peakpath.c_str(), Glib::FILE_TEST_EXISTS)) {
-                       if (::unlink (peakpath.c_str()) != 0) {
+                       if (::g_unlink (peakpath.c_str()) != 0) {
                                error << string_compose (_("cannot remove peakfile %1 for %2 (%3)"),
                                                          peakpath, _path, strerror (errno))
                                      << endmsg;
@@ -3543,8 +3554,6 @@ Session::rename (const std::string& new_name)
 
        string const old_sources_root = _session_dir->sources_root();
 
-#define RENAME ::rename
-
        /* Rename:
 
         * session directory
@@ -3606,7 +3615,8 @@ Session::rename (const std::string& new_name)
 
                cerr << "Rename " << oldstr << " => " << newstr << endl;                
 
-               if (RENAME (oldstr.c_str(), newstr.c_str()) != 0) {
+               if (::g_rename (oldstr.c_str(), newstr.c_str()) != 0) {
+                       error << string_compose (_("renaming %s as %2 failed (%3)"), oldstr, newstr, g_strerror (errno)) << endmsg;
                        return 1;
                }
 
@@ -3633,7 +3643,8 @@ Session::rename (const std::string& new_name)
                
                cerr << "Rename " << oldstr << " => " << newstr << endl;
                
-               if (RENAME (oldstr.c_str(), newstr.c_str()) != 0) {
+               if (::g_rename (oldstr.c_str(), newstr.c_str()) != 0) {
+                       error << string_compose (_("renaming %s as %2 failed (%3)"), oldstr, newstr, g_strerror (errno)) << endmsg;
                        return 1;
                }
        }
@@ -3645,7 +3656,8 @@ Session::rename (const std::string& new_name)
        
        cerr << "Rename " << oldstr << " => " << newstr << endl;                
 
-       if (RENAME (oldstr.c_str(), newstr.c_str()) != 0) {
+       if (::g_rename (oldstr.c_str(), newstr.c_str()) != 0) {
+               error << string_compose (_("renaming %s as %2 failed (%3)"), oldstr, newstr, g_strerror (errno)) << endmsg;
                return 1;
        }
 
@@ -3659,7 +3671,8 @@ Session::rename (const std::string& new_name)
                
                cerr << "Rename " << oldstr << " => " << newstr << endl;                
                
-               if (RENAME (oldstr.c_str(), newstr.c_str()) != 0) {
+               if (::g_rename (oldstr.c_str(), newstr.c_str()) != 0) {
+                       error << string_compose (_("renaming %s as %2 failed (%3)"), oldstr, newstr, g_strerror (errno)) << endmsg;
                        return 1;
                }
        }
@@ -3683,6 +3696,11 @@ Session::rename (const std::string& new_name)
        _current_snapshot_name = new_name;
        _name = new_name;
 
+       /* re-add directory separator - reverse hack to oldstr above */
+       if (_path[_path.length()-1] != G_DIR_SEPARATOR) {
+               _path += G_DIR_SEPARATOR;
+       }
+
        set_dirty ();
 
        /* save state again to get everything just right */
@@ -3695,8 +3713,6 @@ Session::rename (const std::string& new_name)
        store_recent_sessions (new_name, _path);
 
        return 0;
-
-#undef RENAME
 }
 
 int