silly me
[ardour.git] / libs / ardour / session_state.cc
index b62f4f3896c4c20165b2b528dfee6a786c7495a5..f704bbad5fc6aa0f58b82001f5d9f0e58a0a985a 100644 (file)
@@ -15,7 +15,6 @@
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
     
-  $Id$
 */
 
 #include <algorithm>
@@ -65,7 +64,7 @@
 #include <ardour/audioplaylist.h>
 #include <ardour/audiofilesource.h>
 #include <ardour/silentfilesource.h>
-#include <ardour/destructive_filesource.h>
+#include <ardour/sndfilesource.h>
 #include <ardour/sndfile_helpers.h>
 #include <ardour/auditioner.h>
 #include <ardour/export.h>
@@ -118,6 +117,9 @@ Session::first_stage_init (string fullpath, string snapshot_name)
                _path += '/';
        }
 
+       set_history_depth (Config->get_history_depth());
+       
+
        /* these two are just provisional settings. set_state()
           will likely override them.
        */
@@ -132,6 +134,7 @@ Session::first_stage_init (string fullpath, string snapshot_name)
        insert_cnt = 0;
        _transport_speed = 0;
        _last_transport_speed = 0;
+       auto_play_legal = false;
        transport_sub_state = 0;
        _transport_frame = 0;
        last_stop_frame = 0;
@@ -184,6 +187,8 @@ Session::first_stage_init (string fullpath, string snapshot_name)
        current_trans = 0;
        first_file_data_format_reset = true;
        first_file_header_format_reset = true;
+       butler_thread = (pthread_t) 0;
+       midi_thread = (pthread_t) 0;
 
        AudioDiskstream::allocate_working_buffers();
        
@@ -466,11 +471,16 @@ Session::create (bool& new_session, string* mix_template, nframes_t initial_leng
                return -1;
        }
 
-       dir = sound_dir ();
+       /* if this is is an existing session with an old "sounds" directory, just use it. see Session::sound_dir() for more details */
 
-       if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
-               error << string_compose(_("Session: cannot create session sounds dir \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
-               return -1;
+       if (!Glib::file_test (old_sound_dir(), Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_DIR)) {
+
+               dir = sound_dir ();
+               
+               if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
+                       error << string_compose(_("Session: cannot create session sounds dir \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
+                       return -1;
+               }
        }
 
        dir = dead_sound_dir ();
@@ -480,13 +490,6 @@ Session::create (bool& new_session, string* mix_template, nframes_t initial_leng
                return -1;
        }
 
-       dir = automation_dir ();
-
-       if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
-               error << string_compose(_("Session: cannot create session automation dir \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
-               return -1;
-       }
-
        dir = export_dir ();
 
        if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
@@ -574,6 +577,14 @@ Session::load_diskstreams (const XMLNode& node)
        return 0;
 }
 
+void
+Session::maybe_write_autosave()
+{
+        if (dirty() && record_status() != Recording) {
+                save_state("", true);
+        }
+}
+
 void
 Session::remove_pending_capture_state ()
 {
@@ -586,6 +597,48 @@ Session::remove_pending_capture_state ()
        unlink (xml_path.c_str());
 }
 
+/** Rename a state file.
+ * @param snapshot_name Snapshot name.
+ */
+void
+Session::rename_state (string old_name, string new_name)
+{
+       if (old_name == _current_snapshot_name || old_name == _name) {
+               /* refuse to rename the current snapshot or the "main" one */
+               return;
+       }
+       
+       const string old_xml_path = _path + old_name + _statefile_suffix;
+       const string new_xml_path = _path + new_name + _statefile_suffix;
+
+       if (rename (old_xml_path.c_str(), new_xml_path.c_str()) != 0) {
+               error << string_compose(_("could not rename snapshot %1 to %2"), old_name, new_name) << endmsg;
+       }
+}
+
+/** Remove a state file.
+ * @param snapshot_name Snapshot name.
+ */
+void
+Session::remove_state (string snapshot_name)
+{
+       if (snapshot_name == _current_snapshot_name || snapshot_name == _name) {
+               /* refuse to remove the current snapshot or the "main" one */
+               return;
+       }
+       
+       const string xml_path = _path + snapshot_name + _statefile_suffix;
+
+       /* make a backup copy of the state file */
+       const string bak_path = xml_path + ".bak";
+       if (g_file_test (xml_path.c_str(), G_FILE_TEST_EXISTS)) {
+               copy_file (xml_path, bak_path);
+       }
+
+       /* and delete it */
+       unlink (xml_path.c_str());
+}
+
 int
 Session::save_state (string snapshot_name, bool pending)
 {
@@ -611,10 +664,12 @@ Session::save_state (string snapshot_name, bool pending)
 
        if (!pending) {
 
+               /* proper save: use _statefile_suffix (.ardour in English) */
                xml_path = _path;
                xml_path += snapshot_name;
                xml_path += _statefile_suffix;
 
+               /* make a backup copy of the old file */
                bak_path = xml_path;
                bak_path += ".bak";
                
@@ -624,6 +679,7 @@ Session::save_state (string snapshot_name, bool pending)
 
        } else {
 
+               /* pending save: use _pending_suffix (.pending in English) */
                xml_path = _path;
                xml_path += snapshot_name;
                xml_path += _pending_suffix;
@@ -636,7 +692,7 @@ Session::save_state (string snapshot_name, bool pending)
        tmp_path += snapshot_name;
        tmp_path += ".tmp";
 
-       cerr << "actually writing state\n";
+       // cerr << "actually writing state to " << xml_path << endl;
 
        if (!tree.write (tmp_path)) {
                error << string_compose (_("state could not be saved to %1"), tmp_path) << endmsg;
@@ -698,7 +754,7 @@ Session::load_state (string snapshot_name)
        xmlpath += snapshot_name;
        xmlpath += _pending_suffix;
 
-       if (!access (xmlpath.c_str(), F_OK)) {
+       if (Glib::file_test (xmlpath, Glib::FILE_TEST_EXISTS)) {
 
                /* there is pending state from a crashed capture attempt */
 
@@ -713,8 +769,8 @@ Session::load_state (string snapshot_name)
                xmlpath += snapshot_name;
                xmlpath += _statefile_suffix;
        }
-
-       if (access (xmlpath.c_str(), F_OK)) {
+       
+       if (!Glib::file_test (xmlpath, Glib::FILE_TEST_EXISTS)) {
                error << string_compose(_("%1: session state information file \"%2\" doesn't exist!"), _name, xmlpath) << endmsg;
                return 1;
        }
@@ -756,16 +812,22 @@ Session::load_state (string snapshot_name)
        if (is_old) {
                string backup_path;
 
-               backup_path = xmlpath;
-               backup_path += ".1";
+               backup_path = _path;
+               backup_path += snapshot_name;
+               backup_path += "-1";
+               backup_path += _statefile_suffix;
 
-               info << string_compose (_("Copying old session file %1 to %2\nUse %2 with Ardour versions before 2.0 from now on"),
-                                       xmlpath, backup_path) 
-                    << endmsg;
+               /* don't make another copy if it already exists */
 
-               copy_file (xmlpath, backup_path);
-
-               /* if it fails, don't worry. right? */
+               if (!Glib::file_test (backup_path, Glib::FILE_TEST_EXISTS)) {
+                       info << string_compose (_("Copying old session file %1 to %2\nUse %2 with Ardour versions before 2.0 from now on"),
+                                               xmlpath, backup_path) 
+                            << endmsg;
+                       
+                       copy_file (xmlpath, backup_path);
+                       
+                       /* if it fails, don't worry. right? */
+               }
        }
 
        return 0;
@@ -840,7 +902,7 @@ Session::state(bool full_state)
        // store libardour version, just in case
        char buf[16];
        snprintf(buf, sizeof(buf)-1, "%d.%d.%d", 
-                libardour_major_version, libardour_minor_version, libardour_micro_version);
+                libardour2_major_version, libardour2_minor_version, libardour2_micro_version);
        node->add_property("version", string(buf));
                
        /* store configuration settings */
@@ -1451,11 +1513,16 @@ Session::path_from_region_name (string name, string identifier)
                } else {
                        snprintf (buf, sizeof(buf), "%s/%s-%" PRIu32 ".wav", dir.c_str(), name.c_str(), n);
                }
-               if (access (buf, F_OK) != 0) {
+
+               if (!Glib::file_test (buf, Glib::FILE_TEST_EXISTS)) {
                        return buf;
                }
        }
 
+       error << string_compose (_("cannot create new file from region name \"%1\" with ident = \"%2\": too many existing files with similar names"),
+                                name, identifier)
+             << endmsg;
+
        return "";
 }
        
@@ -1847,36 +1914,34 @@ Session::dead_sound_dir () const
 {
        string res = _path;
        res += dead_sound_dir_name;
-       res += '/';
+
        return res;
 }
 
 string
-Session::sound_dir (bool with_path) const
+Session::old_sound_dir (bool with_path) const
 {
-       /* support old session structure */
+       string res;
 
-       struct stat statbuf;
-       string old_nopath;
-       string old_withpath;
+       if (with_path) {
+               res = _path;
+       }
 
-       old_nopath += old_sound_dir_name;
-       old_nopath += '/';
-       
-       old_withpath = _path;
-       old_withpath += old_sound_dir_name;
+       res += old_sound_dir_name;
 
-       if (stat (old_withpath.c_str(), &statbuf) == 0) {
-               if (with_path)
-                       return old_withpath;
-               
-               return old_nopath;
-       }
+       return res;
+}
 
+string
+Session::sound_dir (bool with_path) const
+{
        string res;
+       string full;
 
        if (with_path) {
                res = _path;
+       } else {
+               full = _path;
        }
 
        res += interchange_dir_name;
@@ -1885,6 +1950,38 @@ Session::sound_dir (bool with_path) const
        res += '/';
        res += sound_dir_name;
 
+       if (with_path) {
+               full = res;
+       } else {
+               full += res;
+       }
+       
+       /* if this already exists, don't check for the old session sound directory */
+
+       if (Glib::file_test (full, Glib::FILE_TEST_IS_DIR|Glib::FILE_TEST_EXISTS)) {
+               return res;
+       }
+               
+       /* possibly support old session structure */
+
+       string old_nopath;
+       string old_withpath;
+
+       old_nopath += old_sound_dir_name;
+       old_nopath += '/';
+       
+       old_withpath = _path;
+       old_withpath += old_sound_dir_name;
+       
+       if (Glib::file_test (old_withpath.c_str(), Glib::FILE_TEST_IS_DIR|Glib::FILE_TEST_EXISTS)) {
+               if (with_path)
+                       return old_withpath;
+               
+               return old_nopath;
+       }
+       
+       /* ok, old "sounds" directory isn't there, return the new path */
+
        return res;
 }
 
@@ -2602,6 +2699,9 @@ Session::cleanup_sources (Session::cleanup_report& rep)
                } 
        }
 
+       char tmppath1[PATH_MAX+1];
+       char tmppath2[PATH_MAX+1];
+       
        for (vector<string*>::iterator x = soundfiles->begin(); x != soundfiles->end(); ++x) {
 
                used = false;
@@ -2609,7 +2709,10 @@ Session::cleanup_sources (Session::cleanup_report& rep)
 
                for (set<string>::iterator i = all_sources.begin(); i != all_sources.end(); ++i) {
 
-                       if (spath == *i) {
+                       realpath(spath.c_str(), tmppath1);
+                       realpath((*i).c_str(),  tmppath2);
+
+                       if (strcmp(tmppath1, tmppath2) == 0) {
                                used = true;
                                break;
                        }
@@ -2637,7 +2740,7 @@ Session::cleanup_sources (Session::cleanup_report& rep)
                   on whichever filesystem it was already on.
                */
 
-               if (_path.find ("/sounds/")) {
+               if ((*x).find ("/sounds/") != string::npos) {
 
                        /* old school, go up 1 level */
 
@@ -2832,6 +2935,12 @@ Session::set_deletion_in_progress ()
 void
 Session::add_controllable (Controllable* c)
 {
+       /* this adds a controllable to the list managed by the Session.
+          this is a subset of those managed by the Controllable class
+          itself, and represents the only ones whose state will be saved
+          as part of the session.
+       */
+
        Glib::Mutex::Lock lm (controllables_lock);
        controllables.insert (c);
 }
@@ -2873,7 +2982,6 @@ Session::add_instant_xml (XMLNode& node, const std::string& dir)
        Config->add_instant_xml (node, get_user_ardour_path());
 }
 
-
 int 
 Session::save_history (string snapshot_name)
 {
@@ -2881,8 +2989,6 @@ Session::save_history (string snapshot_name)
     string xml_path;
     string bak_path;
 
-    tree.set_root (&_history.get_state (Config->get_saved_history_depth()));
-
     if (snapshot_name.empty()) {
        snapshot_name = _current_snapshot_name;
     }
@@ -2891,13 +2997,17 @@ Session::save_history (string snapshot_name)
 
     bak_path = xml_path + ".bak";
 
-    if ((access (xml_path.c_str(), F_OK) == 0) &&
-        (rename (xml_path.c_str(), bak_path.c_str())))
-    {
+    if (Glib::file_test (xml_path, Glib::FILE_TEST_EXISTS) && ::rename (xml_path.c_str(), bak_path.c_str())) {
         error << _("could not backup old history file, current history not saved.") << endmsg;
         return -1;
     }
 
+    if (!Config->get_save_history() || Config->get_saved_history_depth() < 0) {
+           return 0;
+    }
+
+    tree.set_root (&_history.get_state (Config->get_saved_history_depth()));
+
     if (!tree.write (xml_path))
     {
         error << string_compose (_("history could not be saved to %1"), xml_path) << endmsg;
@@ -2935,8 +3045,7 @@ Session::restore_history (string snapshot_name)
     xmlpath = _path + snapshot_name + ".history";
     cerr << string_compose(_("Loading history from '%1'."), xmlpath) << endmsg;
 
-    if (access (xmlpath.c_str(), F_OK)) {
-           info << string_compose (_("%1: no history file \"%2\" for this session."), _name, xmlpath) << endmsg;
+    if (!Glib::file_test (xmlpath, Glib::FILE_TEST_EXISTS)) {
            return 1;
     }
 
@@ -3055,16 +3164,20 @@ Session::config_changed (const char* parameter_name)
 
        } else if (PARAM_IS ("use-video-sync")) {
 
-               if (transport_stopped()) {
-                       if (Config->get_use_video_sync()) {
-                               waiting_for_sync_offset = true;
-                       }
-               }
+               waiting_for_sync_offset = Config->get_use_video_sync();
 
        } else if (PARAM_IS ("mmc-control")) {
 
                poke_midi_thread ();
 
+       } else if (PARAM_IS ("mmc-device-id") || PARAM_IS ("mmc-receive-device-id")) {
+
+               set_mmc_receive_device_id (Config->get_mmc_receive_device_id());
+
+       } else if (PARAM_IS ("mmc-send-device-id")) {
+
+               set_mmc_send_device_id (Config->get_mmc_send_device_id());
+
        } else if (PARAM_IS ("midi-control")) {
                
                poke_midi_thread ();
@@ -3123,6 +3236,8 @@ Session::config_changed (const char* parameter_name)
                                /* mark us ready to send */
                                next_quarter_frame_to_send = 0;
                        }
+               } else {
+                       session_send_mtc = false;
                }
 
        } else if (PARAM_IS ("send-mmc")) {
@@ -3133,6 +3248,9 @@ Session::config_changed (const char* parameter_name)
                
                if (_mmc_port != 0) {
                        session_send_mmc = Config->get_send_mmc();
+               } else {
+                       mmc = 0;
+                       session_send_mmc = false; 
                }
 
        } else if (PARAM_IS ("midi-feedback")) {
@@ -3167,6 +3285,14 @@ Session::config_changed (const char* parameter_name)
 
        } else if (PARAM_IS ("slave-source")) {
                set_slave_source (Config->get_slave_source());
+       } else if (PARAM_IS ("remote-model")) {
+               set_remote_control_ids ();
+       } else if (PARAM_IS ("denormal-model")) {
+               setup_fpu ();
+       } else if (PARAM_IS ("history-depth")) {
+               set_history_depth (Config->get_history_depth());
+       } else if (PARAM_IS ("sync-all-route-ordering")) {
+               sync_order_keys ();
        }
 
        set_dirty ();
@@ -3174,3 +3300,9 @@ Session::config_changed (const char* parameter_name)
 #undef PARAM_IS
 
 }
+
+void
+Session::set_history_depth (uint32_t d)
+{
+       _history.set_depth (d);
+}