Merged with trunk R1327.
[ardour.git] / libs / ardour / session_state.cc
index 45a51e813ba3c0eddd64b3c16037b555ed1eb29a..37f7b2c32f6db69d7e08c903bc71cc4bbf8279f4 100644 (file)
@@ -18,6 +18,9 @@
   $Id$
 */
 
+#define __STDC_FORMAT_MACROS 1
+#include <stdint.h>
+
 #include <algorithm>
 #include <fstream>
 #include <string>
 #ifdef HAVE_SYS_VFS_H
 #include <sys/vfs.h>
 #else
-#include <sys/mount.h>
 #include <sys/param.h>
+#include <sys/mount.h>
 #endif
 
+#include <glibmm.h>
+
 #include <midi++/mmc.h>
 #include <midi++/port.h>
 #include <pbd/error.h>
-#include <pbd/dirname.h>
-#include <pbd/lockmonitor.h>
+
+#include <glibmm/thread.h>
 #include <pbd/pathscanner.h>
 #include <pbd/pthread_utils.h>
-#include <pbd/basename.h>
 #include <pbd/strsplit.h>
+#include <pbd/stacktrace.h>
+#include <pbd/copyfile.h>
 
 #include <ardour/audioengine.h>
 #include <ardour/configuration.h>
 #include <ardour/session.h>
-#include <ardour/diskstream.h>
+#include <ardour/buffer.h>
+#include <ardour/audio_diskstream.h>
+#include <ardour/midi_diskstream.h>
 #include <ardour/utils.h>
 #include <ardour/audioplaylist.h>
-#include <ardour/source.h>
-#include <ardour/filesource.h>
-#include <ardour/sndfilesource.h>
+#include <ardour/midi_playlist.h>
+#include <ardour/smf_source.h>
+#include <ardour/audiofilesource.h>
+#include <ardour/destructive_filesource.h>
+#include <ardour/midi_source.h>
 #include <ardour/sndfile_helpers.h>
 #include <ardour/auditioner.h>
 #include <ardour/export.h>
 #include <ardour/slave.h>
 #include <ardour/tempo.h>
 #include <ardour/audio_track.h>
+#include <ardour/midi_track.h>
 #include <ardour/cycle_timer.h>
 #include <ardour/utils.h>
 #include <ardour/named_selection.h>
 #include <ardour/version.h>
 #include <ardour/location.h>
 #include <ardour/audioregion.h>
+#include <ardour/midi_region.h>
 #include <ardour/crossfade.h>
+#include <ardour/control_protocol_manager.h>
+#include <ardour/region_factory.h>
+#include <ardour/source_factory.h>
+#include <ardour/playlist_factory.h>
+
+#include <control_protocol/control_protocol.h>
 
 #include "i18n.h"
 #include <locale.h>
 
 using namespace std;
 using namespace ARDOUR;
+using namespace PBD;
 
 void
 Session::first_stage_init (string fullpath, string snapshot_name)
 {
        if (fullpath.length() == 0) {
+               destroy ();
                throw failed_constructor();
        }
 
        char buf[PATH_MAX+1];
-       if (!realpath(fullpath.c_str(), buf) && (errno != ENOENT)) {
+       if (!realpath (fullpath.c_str(), buf) && (errno != ENOENT)) {
                error << string_compose(_("Could not use path %1 (%s)"), buf, strerror(errno)) << endmsg;
+               destroy ();
                throw failed_constructor();
        }
+
        _path = string(buf);
 
        if (_path[_path.length()-1] != '/') {
@@ -110,14 +132,12 @@ Session::first_stage_init (string fullpath, string snapshot_name)
        */
 
        _name = _current_snapshot_name = snapshot_name;
-       setup_raid_path (_path);
 
        _current_frame_rate = _engine.frame_rate ();
        _tempo_map = new TempoMap (_current_frame_rate);
        _tempo_map->StateChanged.connect (mem_fun (*this, &Session::tempo_map_changed));
 
-       atomic_set (&processing_prohibited, 0);
-       send_cnt = 0;
+       g_atomic_int_set (&processing_prohibited, 0);
        insert_cnt = 0;
        _transport_speed = 0;
        _last_transport_speed = 0;
@@ -125,18 +145,11 @@ Session::first_stage_init (string fullpath, string snapshot_name)
        _transport_frame = 0;
        last_stop_frame = 0;
        end_location = new Location (0, 0, _("end"), Location::Flags ((Location::IsMark|Location::IsEnd)));
+       start_location = new Location (0, 0, _("start"), Location::Flags ((Location::IsMark|Location::IsStart)));
        _end_location_is_free = true;
-       atomic_set (&_record_status, Disabled);
-       auto_play = false;
-       punch_in = false;
-       punch_out = false;
-       auto_loop = false;
-       seamless_loop = false;
+       g_atomic_int_set (&_record_status, Disabled);
        loop_changing = false;
-       auto_input = true;
-       crossfades_active = false;
-       all_safe = false;
-       auto_return = false;
+       play_loop = false;
        _last_roll_location = 0;
        _last_record_location = 0;
        pending_locate_frame = 0;
@@ -149,104 +162,82 @@ Session::first_stage_init (string fullpath, string snapshot_name)
        outbound_mtc_smpte_frame = 0;
        next_quarter_frame_to_send = -1;
        current_block_size = 0;
-       _solo_latched = true;
-       _solo_model = InverseMute;
        solo_update_disabled = false;
        currently_soloing = false;
+       _have_captured = false;
        _worst_output_latency = 0;
        _worst_input_latency = 0;
        _worst_track_latency = 0;
-       _state_of_the_state = StateOfTheState(CannotSave|InitialConnecting|Loading);
+       _state_of_the_state = StateOfTheState(CannotSave|InitialConnecting|Loading|Deletion);
        _slave = 0;
-       _slave_type = None;
        butler_mixdown_buffer = 0;
        butler_gain_buffer = 0;
-       auditioner = 0;
-       mmc_control = false;
-       midi_feedback = false;
-       midi_control = true;
        mmc = 0;
+       session_send_mmc = false;
+       session_send_mtc = false;
        post_transport_work = PostTransportWork (0);
-       atomic_set (&butler_should_do_transport_work, 0);
-       atomic_set (&butler_active, 0);
-       atomic_set (&_playback_load, 100);
-       atomic_set (&_capture_load, 100);
-       atomic_set (&_playback_load_min, 100);
-       atomic_set (&_capture_load_min, 100);
-       pending_audition_region = 0;
-       _edit_mode = Slide;
-       pending_edit_mode = _edit_mode;
+       g_atomic_int_set (&butler_should_do_transport_work, 0);
+       g_atomic_int_set (&butler_active, 0);
+       g_atomic_int_set (&_playback_load, 100);
+       g_atomic_int_set (&_capture_load, 100);
+       g_atomic_int_set (&_playback_load_min, 100);
+       g_atomic_int_set (&_capture_load_min, 100);
        _play_range = false;
-       _control_out = 0;
-       _master_out = 0;
-       input_auto_connect = AutoConnectOption (0);
-       output_auto_connect = AutoConnectOption (0);
-       _have_captured = false;
        waiting_to_start = false;
        _exporting = false;
        _gain_automation_buffer = 0;
        _pan_automation_buffer = 0;
        _npan_buffers = 0;
        pending_abort = false;
-       layer_model = MoveAddHigher;
-       xfade_model = ShortCrossfade;
+       destructive_index = 0;
+       current_trans = 0;
+       first_file_data_format_reset = true;
+       first_file_header_format_reset = true;
+
+       AudioDiskstream::allocate_working_buffers();
 
-       /* allocate conversion buffers */
-       _conversion_buffers[ButlerContext] = new char[DiskStream::disk_io_frames() * 4];
-       _conversion_buffers[TransportContext] = new char[DiskStream::disk_io_frames() * 4];
-       
        /* default short fade = 15ms */
 
-       Crossfade::set_short_xfade_length ((jack_nframes_t) floor ((15.0 * frame_rate()) / 1000.0));
+       Crossfade::set_short_xfade_length ((nframes_t) floor (Config->get_short_xfade_seconds() * frame_rate()));
+       SndFileSource::setup_standard_crossfades (frame_rate());
 
        last_mmc_step.tv_sec = 0;
        last_mmc_step.tv_usec = 0;
        step_speed = 0.0;
 
-       preroll.type = AnyTime::Frames;
-       preroll.frames = 0;
-       postroll.type = AnyTime::Frames;
-       postroll.frames = 0;
-
        /* click sounds are unset by default, which causes us to internal
           waveforms for clicks.
        */
        
-       _click_io = 0;
-       _clicking = false;
-       click_requested = false;
        click_data = 0;
        click_emphasis_data = 0;
        click_length = 0;
        click_emphasis_length = 0;
+       _clicking = false;
 
        process_function = &Session::process_with_events;
 
+       if (Config->get_use_video_sync()) {
+               waiting_for_sync_offset = true;
+       } else {
+               waiting_for_sync_offset = false;
+       }
+
+       _current_frame_rate = 48000;
+       _base_frame_rate = 48000;
+
        last_smpte_when = 0;
        _smpte_offset = 0;
        _smpte_offset_negative = true;
        last_smpte_valid = false;
 
+       sync_time_vars ();
+
        last_rr_session_dir = session_dirs.begin();
        refresh_disk_space ();
 
        // set_default_fade (0.2, 5.0); /* steepness, millisecs */
 
-       /* default configuration */
-
-       do_not_record_plugins = false;
-       over_length_short = 2;
-       over_length_long = 10;
-       send_midi_timecode = false;
-       send_midi_machine_control = false;
-       shuttle_speed_factor = 1.0;
-       shuttle_speed_threshold = 5;
-       rf_speed = 2.0;
-       _meter_hold = 100; // XXX unknown units: number of calls to meter::set()
-       _meter_falloff = 1.5f; // XXX unknown units: refresh_rate
-       max_level = 0;
-       min_level = 0;
-
        /* slave stuff */
 
        average_slave_delta = 1800;
@@ -254,22 +245,20 @@ Session::first_stage_init (string fullpath, string snapshot_name)
        delta_accumulator_cnt = 0;
        slave_state = Stopped;
 
-       /* default SMPTE type is 30 FPS, non-drop */
-
-       set_smpte_type (30.0, false);
-
        _engine.GraphReordered.connect (mem_fun (*this, &Session::graph_reordered));
 
        /* These are all static "per-class" signals */
 
-       Region::CheckNewRegion.connect (mem_fun (*this, &Session::add_region));
-       Source::SourceCreated.connect (mem_fun (*this, &Session::add_source));
-       Playlist::PlaylistCreated.connect (mem_fun (*this, &Session::add_playlist));
+       RegionFactory::CheckNewRegion.connect (mem_fun (*this, &Session::add_region));
+       SourceFactory::SourceCreated.connect (mem_fun (*this, &Session::add_source));
+       PlaylistFactory::PlaylistCreated.connect (mem_fun (*this, &Session::add_playlist));
        Redirect::RedirectCreated.connect (mem_fun (*this, &Session::add_redirect));
-       DiskStream::DiskStreamCreated.connect (mem_fun (*this, &Session::add_diskstream));
        NamedSelection::NamedSelectionCreated.connect (mem_fun (*this, &Session::add_named_selection));
+       AutomationList::AutomationListCreated.connect (mem_fun (*this, &Session::add_automation_list));
 
-       IO::MoreOutputs.connect (mem_fun (*this, &Session::ensure_passthru_buffers));
+       Controllable::Destroyed.connect (mem_fun (*this, &Session::remove_controllable));
+
+       IO::MoreChannels.connect (mem_fun (*this, &Session::ensure_buffers));
 
        /* stop IO objects from doing stuff until we're ready for them */
 
@@ -281,7 +270,7 @@ Session::first_stage_init (string fullpath, string snapshot_name)
 int
 Session::second_stage_init (bool new_session)
 {
-       SndFileSource::set_peak_dir (peak_dir());
+       AudioFileSource::set_peak_dir (peak_dir());
 
        if (!new_session) {
                if (load_state (_current_snapshot_name)) {
@@ -294,18 +283,18 @@ Session::second_stage_init (bool new_session)
                return -1;
        }
 
-       if (start_midi_thread ()) {
+       /*if (start_midi_thread ()) {
                return -1;
-       }
-
-       if (init_feedback ()) {
-               return -1;
-       }
+       }*/
 
+       // set_state() will call setup_raid_path(), but if it's a new session we need
+       // to call setup_raid_path() here.
        if (state_tree) {
                if (set_state (*state_tree->root())) {
                        return -1;
                }
+       } else {
+               setup_raid_path(_path);
        }
 
        /* we can't save till after ::when_engine_running() is called,
@@ -331,17 +320,28 @@ Session::second_stage_init (bool new_session)
        _engine.Halted.connect (mem_fun (*this, &Session::engine_halted));
        _engine.Xrun.connect (mem_fun (*this, &Session::xrun_recovery));
 
-       if (_engine.running()) {
+       try {
                when_engine_running();
-       } else {
-               first_time_running = _engine.Running.connect (mem_fun (*this, &Session::when_engine_running));
        }
 
-       send_full_time_code ();
+       /* handle this one in a different way than all others, so that its clear what happened */
+       
+       catch (AudioEngine::PortRegistrationFailure& err) {
+               error << _("Unable to create all required ports")
+                     << endmsg;
+               return -1;
+       }
+
+       catch (...) {
+               return -1;
+       }
+
+       //send_full_time_code ();
        _engine.transport_locate (0);
        deliver_mmc (MIDI::MachineControl::cmdMmcReset, 0);
        deliver_mmc (MIDI::MachineControl::cmdLocate, 0);
-       send_all_midi_feedback();
+
+       ControlProtocolManager::instance().set_session (*this);
 
        if (new_session) {
                _end_location_is_free = true;
@@ -365,14 +365,6 @@ Session::raid_path () const
        return path.substr (0, path.length() - 1); // drop final colon
 }
 
-void
-Session::set_raid_path (string path)
-{
-       /* public-access to setup_raid_path() */
-
-       setup_raid_path (path);
-}
-
 void
 Session::setup_raid_path (string path)
 {
@@ -399,13 +391,25 @@ Session::setup_raid_path (string path)
 
        if (colons == 0) {
 
-               /* no multiple search path, just one directory (common case) */
+               /* no multiple search path, just one location (common case) */
 
                sp.path = path;
                sp.blocks = 0;
                session_dirs.push_back (sp);
+
+               string fspath;
+
+               /* sounds dir */
+
+               fspath += sp.path;
+               if (fspath[fspath.length()-1] != '/') {
+                       fspath += '/';
+               }
+
+               fspath += sound_dir (false);
                
-               FileSource::set_search_path (path + sound_dir_name);
+               AudioFileSource::set_search_path (fspath);
+               SMFSource::set_search_path (fspath); // FIXME: should be different
 
                return;
        }
@@ -416,16 +420,17 @@ Session::setup_raid_path (string path)
                
                sp.blocks = 0;
                sp.path = remaining.substr (0, colon);
+               session_dirs.push_back (sp);
+
+               /* add sounds to file search path */
 
                fspath += sp.path;
                if (fspath[fspath.length()-1] != '/') {
                        fspath += '/';
                }
-               fspath += sound_dir_name;
+               fspath += sound_dir (false);
                fspath += ':';
 
-               session_dirs.push_back (sp);
-
                remaining = remaining.substr (colon+1);
        }
 
@@ -434,18 +439,21 @@ Session::setup_raid_path (string path)
                sp.blocks = 0;
                sp.path = remaining;
 
+               fspath += ':';
                fspath += sp.path;
                if (fspath[fspath.length()-1] != '/') {
                        fspath += '/';
                }
-               fspath += sound_dir_name;
+               fspath += sound_dir (false);
+               fspath += ':';
 
                session_dirs.push_back (sp);
        }
 
-       /* set the FileSource search path */
+       /* set the AudioFileSource search path */
 
-       FileSource::set_search_path (fspath);
+       AudioFileSource::set_search_path (fspath);
+       SMFSource::set_search_path (fspath); // FIXME: should be different
 
        /* reset the round-robin soundfile path thingie */
 
@@ -453,113 +461,93 @@ Session::setup_raid_path (string path)
 }
 
 int
-Session::create (bool& new_session, string* mix_template, jack_nframes_t initial_length)
+Session::create (bool& new_session, string* mix_template, nframes_t initial_length)
 {
        string dir;
-       
-       if (mkdir (_path.c_str(), 0755) < 0) {
-               if (errno == EEXIST) {
-                       new_session = false;
-               } else {
-                       error << string_compose(_("Session: cannot create session dir \"%1\" (%2)"), _path, strerror (errno)) << endmsg;
-                       return -1;
-               }
-       } else {
-               new_session = true;
+
+       if (g_mkdir_with_parents (_path.c_str(), 0755) < 0) {
+               error << string_compose(_("Session: cannot create session dir \"%1\" (%2)"), _path, strerror (errno)) << endmsg;
+               return -1;
        }
 
        dir = peak_dir ();
 
-       if (mkdir (dir.c_str(), 0755) < 0) {
-               if (errno != EEXIST) {
-                       error << string_compose(_("Session: cannot create session peakfile dir \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
-                       return -1;
-               }
+       if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
+               error << string_compose(_("Session: cannot create session peakfile dir \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
+               return -1;
        }
 
        dir = sound_dir ();
 
-       if (mkdir (dir.c_str(), 0755) < 0) {
-               if (errno != EEXIST) {
-                       error << string_compose(_("Session: cannot create session sounds dir \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
-                       return -1;
-               }
+       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 ();
 
-       if (mkdir (dir.c_str(), 0755) < 0) {
-               if (errno != EEXIST) {
-                       error << string_compose(_("Session: cannot create session dead sounds dir \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
-                       return -1;
-               }
+       if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
+               error << string_compose(_("Session: cannot create session dead sounds dir \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
+               return -1;
        }
 
        dir = automation_dir ();
 
-       if (mkdir (dir.c_str(), 0755) < 0) {
-               if (errno != EEXIST) {
-                       error << string_compose(_("Session: cannot create session automation dir \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
-                       return -1;
-               }
+       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;
        }
 
-       
+
        /* check new_session so we don't overwrite an existing one */
-       
+
        if (mix_template) {
-               if (new_session){
-                       std::string in_path = *mix_template;
+               std::string in_path = *mix_template;
+
+               ifstream in(in_path.c_str());
+
+               if (in){
+                       string out_path = _path;
+                       out_path += _name;
+                       out_path += _statefile_suffix;
+
+                       ofstream out(out_path.c_str());
+
+                       if (out){
+                               out << in.rdbuf();
+
+                               // okay, session is set up.  Treat like normal saved
+                               // session from now on.
+
+                               new_session = false;
+                               return 0;
 
-                       ifstream in(in_path.c_str());
-                       
-                       if (in){
-                               string out_path = _path;
-                               out_path += _name;
-                               out_path += _statefile_suffix;
-                               
-                               ofstream out(out_path.c_str());
-
-                               if (out){
-                                       out << in.rdbuf();
-                                       
-                                       // okay, session is set up.  Treat like normal saved
-                                       // session from now on.
-                                       
-                                       new_session = false;
-                                       return 0;
-                                       
-                               } else {
-                                       error << string_compose (_("Could not open %1 for writing mix template"), out_path) 
-                                             << endmsg;
-                                       return -1;
-                               }
-                               
                        } else {
-                               error << string_compose (_("Could not open mix template %1 for reading"), in_path) 
-                                     << endmsg;
+                               error << string_compose (_("Could not open %1 for writing mix template"), out_path) 
+                                       << endmsg;
                                return -1;
                        }
-                       
-                       
+
                } else {
-                       warning << _("Session already exists.  Not overwriting") << endmsg;
+                       error << string_compose (_("Could not open mix template %1 for reading"), in_path) 
+                               << endmsg;
                        return -1;
                }
+
        }
 
-       if (new_session) {
+       /* set initial start + end point */
 
-               /* set an initial end point */
+       start_location->set_end (0);
+       _locations.add (start_location);
 
-               end_location->set_end (initial_length);
-               _locations.add (end_location);
+       end_location->set_end (initial_length);
+       _locations.add (end_location);
 
-               _state_of_the_state = Clean;
+       _state_of_the_state = Clean;
 
-               if (save_state (_current_snapshot_name)) {
-                       return -1;
-               }
+       if (save_state (_current_snapshot_name)) {
+               return -1;
        }
 
        return 0;
@@ -574,16 +562,22 @@ Session::load_diskstreams (const XMLNode& node)
        clist = node.children();
 
        for (citer = clist.begin(); citer != clist.end(); ++citer) {
-               
-               DiskStream* dstream;
 
                try {
-                       dstream = new DiskStream (*this, **citer);
-                       /* added automatically by DiskStreamCreated handler */
+                       /* diskstreams added automatically by DiskstreamCreated handler */
+                       if ((*citer)->name() == "AudioDiskstream" || (*citer)->name() == "DiskStream") {
+                               boost::shared_ptr<AudioDiskstream> dstream (new AudioDiskstream (*this, **citer));
+                               add_diskstream (dstream);
+                       } else if ((*citer)->name() == "MidiDiskstream") {
+                               boost::shared_ptr<MidiDiskstream> dstream (new MidiDiskstream (*this, **citer));
+                               add_diskstream (dstream);
+                       } else {
+                               error << _("Session: unknown diskstream type in XML") << endmsg;
+                       }
                } 
                
                catch (failed_constructor& err) {
-                       error << _("Session: could not load diskstream via XML state")                        << endmsg;
+                       error << _("Session: could not load diskstream via XML state") << endmsg;
                        return -1;
                }
        }
@@ -625,15 +619,12 @@ Session::save_state (string snapshot_name, bool pending)
                xml_path = _path;
                xml_path += snapshot_name;
                xml_path += _statefile_suffix;
+
                bak_path = xml_path;
                bak_path += ".bak";
                
-               // Make backup of state file
-               
-               if ((access (xml_path.c_str(), F_OK) == 0) &&
-                   (rename(xml_path.c_str(), bak_path.c_str()))) {
-                       error << _("could not backup old state file, current state not saved.") << endmsg;
-                       return -1;
+               if (g_file_test (xml_path.c_str(), G_FILE_TEST_EXISTS)) {
+                       copy_file (xml_path, bak_path);
                }
 
        } else {
@@ -644,30 +635,34 @@ Session::save_state (string snapshot_name, bool pending)
 
        }
 
-       if (!tree.write (xml_path)) {
-               error << string_compose (_("state could not be saved to %1"), xml_path) << endmsg;
+       string tmp_path;
 
-               /* don't leave a corrupt file lying around if it is
-                  possible to fix.
-               */
+       tmp_path = _path;
+       tmp_path += snapshot_name;
+       tmp_path += ".tmp";
 
-               if (unlink (xml_path.c_str())) {
-                       error << string_compose (_("could not remove corrupt state file %1"), xml_path) << endmsg;
-               } else {
-                       if (!pending) {
-                               if (rename (bak_path.c_str(), xml_path.c_str())) {
-                                       error << string_compose (_("could not restore state file from backup %1"), bak_path) << endmsg;
-                               }
-                       }
-               }
+       cerr << "actually writing state\n";
 
+       if (!tree.write (tmp_path)) {
+               error << string_compose (_("state could not be saved to %1"), tmp_path) << endmsg;
+               unlink (tmp_path.c_str());
                return -1;
+
+       } 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;
+                       unlink (tmp_path.c_str());
+                       return -1;
+               }
        }
 
        if (!pending) {
 
+                save_history (snapshot_name);
+
                bool was_dirty = dirty();
-               
+
                _state_of_the_state = StateOfTheState (_state_of_the_state & ~Dirty);
                
                if (was_dirty) {
@@ -733,475 +728,93 @@ Session::load_state (string snapshot_name)
 
        set_dirty();
 
-       if (state_tree->read (xmlpath)) {
-               return 0;
-       } else {
+       if (!state_tree->read (xmlpath)) {
                error << string_compose(_("Could not understand ardour file %1"), xmlpath) << endmsg;
+               delete state_tree;
+               state_tree = 0;
+               return -1;
        }
 
-       delete state_tree;
-       state_tree = 0;
-       return -1;
-}
-
-int
-Session::load_options (const XMLNode& node)
-{
-       XMLNode* child;
-       XMLProperty* prop;
-       bool have_fade_msecs = false;
-       bool have_fade_steepness = false;
-       float fade_msecs = 0;
-       float fade_steepness = 0;
-       SlaveSource slave_src = None;
-       int x;
-       LocaleGuard lg (X_("POSIX"));
+       XMLNode& root (*state_tree->root());
        
-       if ((child = find_named_node (node, "input-auto-connect")) != 0) {
-               if ((prop = child->property ("val")) != 0) {
-                       sscanf (prop->value().c_str(), "%x", &x);
-                       input_auto_connect = AutoConnectOption (x);
-               }
-       }
-
-       if ((child = find_named_node (node, "output-auto-connect")) != 0) {
-               if ((prop = child->property ("val")) != 0) {
-                       sscanf (prop->value().c_str(), "%x", &x);
-                       output_auto_connect = AutoConnectOption (x);
-               }
-       }
-                               
-       if ((child = find_named_node (node, "slave")) != 0) {
-               if ((prop = child->property ("type")) != 0) {
-                       if (prop->value() == "none") {
-                               slave_src = None;
-                       } else if (prop->value() == "mtc") {
-                               slave_src = MTC;
-                       } else if (prop->value() == "jack") {
-                               slave_src = JACK;
-                       }
-                       set_slave_source (slave_src, 0);
-               }
+       if (root.name() != X_("Session")) {
+               error << string_compose (_("Session file %1 is not an Ardour session"), xmlpath) << endmsg;
+               delete state_tree;
+               state_tree = 0;
+               return -1;
        }
 
-       /* we cannot set edit mode if we are loading a session,
-          because it might destroy the playlist's positioning
-       */
+       const XMLProperty* prop;
+       bool is_old = false;
 
-       if ((child = find_named_node (node, "edit-mode")) != 0) {
-               if ((prop = child->property ("val")) != 0) {
-                       if (prop->value() == "slide") {
-                               pending_edit_mode = Slide;
-                       } else if (prop->value() == "splice") {
-                               pending_edit_mode = Splice;
-                       } 
-               }
-       }
-                               
-       if ((child = find_named_node (node, "send-midi-timecode")) != 0) {
-               if ((prop = child->property ("val")) != 0) {
-                       bool x = (prop->value() == "yes");
-                       send_mtc = !x; /* force change in value */
-                       set_send_mtc (x);
-               }
-       }
-       if ((child = find_named_node (node, "send-midi-machine-control")) != 0) {
-               if ((prop = child->property ("val")) != 0) {
-                       bool x = (prop->value() == "yes");
-                       send_mmc = !x; /* force change in value */
-                       set_send_mmc (prop->value() == "yes");
-               }
-       }
-       if ((child = find_named_node (node, "max-level")) != 0) {
-               if ((prop = child->property ("val")) != 0) {
-                       max_level = atoi (prop->value().c_str());
-               }
-       }
-       if ((child = find_named_node (node, "min-level")) != 0) {
-               if ((prop = child->property ("val")) != 0) {
-                       min_level = atoi (prop->value().c_str());
-               }
-       }
-       if ((child = find_named_node (node, "meter-hold")) != 0) {
-               if ((prop = child->property ("val")) != 0) {
-                       _meter_hold = atof (prop->value().c_str());
-               }
-       }
-       if ((child = find_named_node (node, "meter-falloff")) != 0) {
-               if ((prop = child->property ("val")) != 0) {
-                       _meter_falloff = atof (prop->value().c_str());
-               }
-       }
-       if ((child = find_named_node (node, "long-over-length")) != 0) {
-               if ((prop = child->property ("val")) != 0) {
-                       over_length_long = atoi (prop->value().c_str());
-               }
-       }
-       if ((child = find_named_node (node, "short-over-length")) != 0) {
-               if ((prop = child->property ("val")) != 0) {
-                       over_length_short = atoi (prop->value().c_str());
-               }
-       }
-       if ((child = find_named_node (node, "shuttle-speed-factor")) != 0) {
-               if ((prop = child->property ("val")) != 0) {
-                       shuttle_speed_factor = atof (prop->value().c_str());
-               }
-       }
-       if ((child = find_named_node (node, "shuttle-speed-threshold")) != 0) {
-               if ((prop = child->property ("val")) != 0) {
-                       shuttle_speed_threshold = atof (prop->value().c_str());
-               }
-       }
-       if ((child = find_named_node (node, "rf-speed")) != 0) {
-               if ((prop = child->property ("val")) != 0) {
-                       rf_speed = atof (prop->value().c_str());
-               }
-       }
-       if ((child = find_named_node (node, "smpte-frames-per-second")) != 0) {
-               if ((prop = child->property ("val")) != 0) {
-                       set_smpte_type( atof (prop->value().c_str()), smpte_drop_frames );
-               }
-       }
-       if ((child = find_named_node (node, "smpte-drop-frames")) != 0) {
-               if ((prop = child->property ("val")) != 0) {
-                       set_smpte_type( smpte_frames_per_second, (prop->value() == "yes") );
-               }
-       }
-       if ((child = find_named_node (node, "smpte-offset")) != 0) {
-               if ((prop = child->property ("val")) != 0) {
-                       set_smpte_offset( atoi (prop->value().c_str()) );
-               }
-       }
-       if ((child = find_named_node (node, "smpte-offset-negative")) != 0) {
-               if ((prop = child->property ("val")) != 0) {
-                       set_smpte_offset_negative( (prop->value() == "yes") );
-               }
-       }
-       if ((child = find_named_node (node, "click-sound")) != 0) {
-               if ((prop = child->property ("val")) != 0) {
-                       click_sound = prop->value();
-               }
-       }
-       if ((child = find_named_node (node, "click-emphasis-sound")) != 0) {
-               if ((prop = child->property ("val")) != 0) {
-                       click_emphasis_sound = prop->value();
+       if ((prop = root.property ("version")) == 0) {
+               /* no version implies very old version of Ardour */
+               is_old = true;
+       } else {
+               int major_version;
+               major_version = atoi (prop->value()); // grab just the first number before the period
+               if (major_version < 2) {
+                       is_old = true;
                }
        }
 
-       if ((child = find_named_node (node, "solo-model")) != 0) {
-               if ((prop = child->property ("val")) != 0) {
-                       if (prop->value() == "SoloBus")
-                               _solo_model = SoloBus;
-                       else
-                               _solo_model = InverseMute;
-               }
-       }
+       if (is_old) {
+               string backup_path;
 
-       /* BOOLEAN OPTIONS */
+               backup_path = xmlpath;
+               backup_path += ".1";
 
-       if ((child = find_named_node (node, "auto-play")) != 0) {
-               if ((prop = child->property ("val")) != 0) {
-                       set_auto_play (prop->value() == "yes");
-               }
-       }
-       if ((child = find_named_node (node, "auto-input")) != 0) {
-               if ((prop = child->property ("val")) != 0) {
-                       set_auto_input (prop->value() == "yes");
-               }
-       }
-       if ((child = find_named_node (node, "seamless-loop")) != 0) {
-               if ((prop = child->property ("val")) != 0) {
-                       set_seamless_loop (prop->value() == "yes");
-               }
-       }
-       if ((child = find_named_node (node, "punch-in")) != 0) {
-               if ((prop = child->property ("val")) != 0) {
-                       set_punch_in (prop->value() == "yes");
-               }
-       }
-       if ((child = find_named_node (node, "punch-out")) != 0) {
-               if ((prop = child->property ("val")) != 0) {
-                       set_punch_out (prop->value() == "yes");
-               }
-       }
-       if ((child = find_named_node (node, "auto-return")) != 0) {
-               if ((prop = child->property ("val")) != 0) {
-                       set_auto_return (prop->value() == "yes");
-               }
-       }
-       if ((child = find_named_node (node, "send-mtc")) != 0) {
-               if ((prop = child->property ("val")) != 0) {
-                       set_send_mtc (prop->value() == "yes");
-               }
-       }
-       if ((child = find_named_node (node, "mmc-control")) != 0) {
-               if ((prop = child->property ("val")) != 0) {
-                       set_mmc_control (prop->value() == "yes");
-               }
-       }
-       if ((child = find_named_node (node, "midi-control")) != 0) {
-               if ((prop = child->property ("val")) != 0) {
-                       set_midi_control (prop->value() == "yes");
-               }
-       }
-       if ((child = find_named_node (node, "midi-feedback")) != 0) {
-               if ((prop = child->property ("val")) != 0) {
-                       set_midi_feedback (prop->value() == "yes");
-               }
-       }
-       // Legacy support for <recording-plugins>
-       if ((child = find_named_node (node, "recording-plugins")) != 0) {
-               if ((prop = child->property ("val")) != 0) {
-                       set_do_not_record_plugins (prop->value() == "no");
-               }
-       }
-       if ((child = find_named_node (node, "do-not-record-plugins")) != 0) {
-               if ((prop = child->property ("val")) != 0) {
-                       set_do_not_record_plugins (prop->value() == "yes");
-               }
-       }
-       if ((child = find_named_node (node, "crossfades-active")) != 0) {
-               if ((prop = child->property ("val")) != 0) {
-                       set_crossfades_active (prop->value() == "yes");
-               }
-       }
-       if ((child = find_named_node (node, "audible-click")) != 0) {
-               if ((prop = child->property ("val")) != 0) {
-                       set_clicking (prop->value() == "yes");
-               }
-       }
+               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;
 
-       if ((child = find_named_node (node, "layer-model")) != 0) {
-               if ((prop = child->property ("val")) != 0) {
-                       if (prop->value() == X_("LaterHigher")) {
-                               set_layer_model (LaterHigher);
-                       } else if (prop->value() == X_("AddHigher")) {
-                               set_layer_model (AddHigher);
-                       } else {
-                               set_layer_model (MoveAddHigher);
-                       }
-               }
-       }
+               copy_file (xmlpath, backup_path);
 
-       if ((child = find_named_node (node, "xfade-model")) != 0) {
-               if ((prop = child->property ("val")) != 0) {
-                       if (prop->value() == X_("Short")) {
-                               set_xfade_model (ShortCrossfade);
-                       } else {
-                               set_xfade_model (FullCrossfade);
-                       }
-               }
+               /* if it fails, don't worry. right? */
        }
 
-       if ((child = find_named_node (node, "short-xfade-length")) != 0) {
-               if ((prop = child->property ("val")) != 0) {
-                       /* value is stored as a fractional seconds */
-                       float secs = atof (prop->value().c_str());
-                       Crossfade::set_short_xfade_length ((jack_nframes_t) floor (secs * frame_rate()));
-               } 
-       }
+       return 0;
+}
 
-       if ((child = find_named_node (node, "full-xfades-unmuted")) != 0) {
-               if ((prop = child->property ("val")) != 0) {
-                       crossfades_active = (prop->value() == "yes");
-               }
-       } 
+int
+Session::load_options (const XMLNode& node)
+{
+       XMLNode* child;
+       XMLProperty* prop;
+       LocaleGuard lg (X_("POSIX"));
 
-       /* TIED OPTIONS */
+       Config->set_variables (node, ConfigVariableBase::Session);
 
-       if ((child = find_named_node (node, "default-fade-steepness")) != 0) {
-               if ((prop = child->property ("val")) != 0) {
-                       fade_steepness = atof (prop->value().c_str());
-                       have_fade_steepness = true;
-               }
-       }
-       if ((child = find_named_node (node, "default-fade-msec")) != 0) {
+       if ((child = find_named_node (node, "end-marker-is-free")) != 0) {
                if ((prop = child->property ("val")) != 0) {
-                       fade_msecs = atof (prop->value().c_str());
-                       have_fade_msecs = true;
+                       _end_location_is_free = (prop->value() == "yes");
                }
        }
 
-       if (have_fade_steepness || have_fade_msecs) {
-               // set_default_fade (fade_steepness, fade_msecs);
-       }
-
        return 0;
 }
 
+bool
+Session::save_config_options_predicate (ConfigVariableBase::Owner owner) const
+{
+       const ConfigVariableBase::Owner modified_by_session_or_user = (ConfigVariableBase::Owner)
+               (ConfigVariableBase::Session|ConfigVariableBase::Interface);
+
+       return owner & modified_by_session_or_user;
+}
+
 XMLNode&
 Session::get_options () const
 {
-       XMLNode* opthead;
        XMLNode* child;
-       char buf[32];
        LocaleGuard lg (X_("POSIX"));
 
-       opthead = new XMLNode ("Options");
-
-       SlaveSource src = slave_source ();
-       string src_string;
-       switch (src) {
-       case None:
-               src_string = "none";
-               break;
-       case MTC:
-               src_string = "mtc";
-               break;
-       case JACK:
-               src_string = "jack";
-               break;
-       }
-       child = opthead->add_child ("slave");
-       child->add_property ("type", src_string);
-       
-       child = opthead->add_child ("send-midi-timecode");
-       child->add_property ("val", send_midi_timecode?"yes":"no");
-
-       child = opthead->add_child ("send-midi-machine-control");
-       child->add_property ("val", send_midi_machine_control?"yes":"no");
-
-       snprintf (buf, sizeof(buf)-1, "%x", (int) input_auto_connect);
-       child = opthead->add_child ("input-auto-connect");
-       child->add_property ("val", buf);
-
-       snprintf (buf, sizeof(buf)-1, "%x", (int) output_auto_connect);
-       child = opthead->add_child ("output-auto-connect");
-       child->add_property ("val", buf);
-
-       snprintf (buf, sizeof(buf)-1, "%d", max_level);
-       child = opthead->add_child ("max-level");
-       child->add_property ("val", buf);
-
-       snprintf (buf, sizeof(buf)-1, "%d", min_level);
-       child = opthead->add_child ("min-level");
-       child->add_property ("val", buf);
-
-       snprintf (buf, sizeof(buf)-1, "%f", _meter_hold);
-       child = opthead->add_child ("meter-hold");
-       child->add_property ("val", buf);
-
-       snprintf (buf, sizeof(buf)-1, "%f", _meter_falloff);
-       child = opthead->add_child ("meter-falloff");
-       child->add_property ("val", buf);
-       
-       snprintf (buf, sizeof(buf)-1, "%u", over_length_long);
-       child = opthead->add_child ("long-over-length");
-       child->add_property ("val", buf);
-
-       snprintf (buf, sizeof(buf)-1, "%u", over_length_short);
-       child = opthead->add_child ("short-over-length");
-       child->add_property ("val", buf);
-
-       snprintf (buf, sizeof(buf)-1, "%f", shuttle_speed_factor);
-       child = opthead->add_child ("shuttle-speed-factor");
-       child->add_property ("val", buf);
+       XMLNode& option_root = Config->get_variables (mem_fun (*this, &Session::save_config_options_predicate));
 
-       snprintf (buf, sizeof(buf)-1, "%f", shuttle_speed_threshold);
-       child = opthead->add_child ("shuttle-speed-threshold");
-       child->add_property ("val", buf);
+       child = option_root.add_child ("end-marker-is-free");
+       child->add_property ("val", _end_location_is_free ? "yes" : "no");
 
-       snprintf (buf, sizeof(buf)-1, "%f", rf_speed);
-       child = opthead->add_child ("rf-speed");
-       child->add_property ("val", buf);
-
-       snprintf (buf, sizeof(buf)-1, "%.2f", smpte_frames_per_second);
-       child = opthead->add_child ("smpte-frames-per-second");
-       child->add_property ("val", buf);
-       
-       child = opthead->add_child ("smpte-drop-frames");
-       child->add_property ("val", smpte_drop_frames ? "yes" : "no");
-       
-       snprintf (buf, sizeof(buf)-1, "%u", smpte_offset ());
-       child = opthead->add_child ("smpte-offset");
-       child->add_property ("val", buf);
-       
-       child = opthead->add_child ("smpte-offset-negative");
-       child->add_property ("val", smpte_offset_negative () ? "yes" : "no");
-       
-       child = opthead->add_child ("edit-mode");
-       switch (_edit_mode) {
-       case Splice:
-               child->add_property ("val", "splice");
-               break;
-
-       case Slide:
-               child->add_property ("val", "slide");
-               break;
-       }
-
-       child = opthead->add_child ("auto-play");
-       child->add_property ("val", get_auto_play () ? "yes" : "no");
-       child = opthead->add_child ("auto-input");
-       child->add_property ("val", get_auto_input () ? "yes" : "no");
-       child = opthead->add_child ("seamless-loop");
-       child->add_property ("val", get_seamless_loop () ? "yes" : "no");
-       child = opthead->add_child ("punch-in");
-       child->add_property ("val", get_punch_in () ? "yes" : "no");
-       child = opthead->add_child ("punch-out");
-       child->add_property ("val", get_punch_out () ? "yes" : "no");
-       child = opthead->add_child ("all-safe");
-       child->add_property ("val", get_all_safe () ? "yes" : "no");
-       child = opthead->add_child ("auto-return");
-       child->add_property ("val", get_auto_return () ? "yes" : "no");
-       child = opthead->add_child ("mmc-control");
-       child->add_property ("val", get_mmc_control () ? "yes" : "no");
-       child = opthead->add_child ("midi-control");
-       child->add_property ("val", get_midi_control () ? "yes" : "no");
-       child = opthead->add_child ("midi-feedback");
-       child->add_property ("val", get_midi_feedback () ? "yes" : "no");
-       child = opthead->add_child ("do-not-record-plugins");
-       child->add_property ("val", get_do_not_record_plugins () ? "yes" : "no");
-       child = opthead->add_child ("auto-crossfade");
-       child->add_property ("val", get_crossfades_active () ? "yes" : "no");
-       child = opthead->add_child ("audible-click");
-       child->add_property ("val", get_clicking () ? "yes" : "no");
-
-       if (click_sound.length()) {
-               child = opthead->add_child ("click-sound");
-               child->add_property ("val", click_sound);
-       }
-
-       if (click_emphasis_sound.length()) {
-               child = opthead->add_child ("click-emphasis-sound");
-               child->add_property ("val", click_emphasis_sound);
-       }
-
-       child = opthead->add_child ("solo-model");
-       child->add_property ("val", _solo_model == SoloBus ? "SoloBus" : "InverseMute");
-
-       child = opthead->add_child ("layer-model");
-       switch (layer_model) {
-       case LaterHigher:
-               child->add_property ("val", X_("LaterHigher"));
-               break;
-       case MoveAddHigher:
-               child->add_property ("val", X_("MoveAddHigher"));
-               break;
-       case AddHigher:
-               child->add_property ("val", X_("AddHigher"));
-               break;
-       }
-
-       child = opthead->add_child ("xfade-model");
-       switch (xfade_model) {
-       case FullCrossfade:
-               child->add_property ("val", X_("Full"));
-               break;
-       case ShortCrossfade:
-               child->add_property ("val", X_("Short"));
-       }
-
-       child = opthead->add_child ("short-xfade-length");
-       /* store as fractions of a second */
-       snprintf (buf, sizeof(buf)-1, "%f", 
-                 (float) Crossfade::short_xfade_length() / frame_rate());
-       child->add_property ("val", buf);
-
-       child = opthead->add_child ("full-xfades-unmuted");
-       child->add_property ("val", crossfades_active ? "yes" : "no");
-
-       return *opthead;
+       return option_root;
 }
 
 XMLNode&
@@ -1218,7 +831,7 @@ Session::get_template()
           sources in their state node. 
        */
        
-       disable_record ();
+       disable_record (false);
 
        return state(false);
 }
@@ -1272,39 +885,53 @@ Session::state(bool full_state)
                }
        }
 
+       /* save the ID counter */
+       
+       snprintf (buf, sizeof (buf), "%" PRIu64, ID::counter());
+       node->add_property ("id-counter", buf);
+
+       /* various options */
+
        node->add_child_nocopy (get_options());
 
        child = node->add_child ("Sources");
 
        if (full_state) {
-               LockMonitor sl (source_lock, __LINE__, __FILE__);
+               Glib::Mutex::Lock sl (source_lock);
 
-               for (SourceList::iterator siter = sources.begin(); siter != sources.end(); ++siter) {
+               for (SourceMap::iterator siter = sources.begin(); siter != sources.end(); ++siter) {
                        
-                       /* Don't save information about FileSources that are empty */
+                       /* Don't save information about AudioFileSources that are empty */
                        
-                       FileSource* fs;
-                       
-                       if ((fs = dynamic_cast<FileSource*> ((*siter).second)) != 0) {
-                               if (fs->length() == 0) {
-                                       continue;
+                       boost::shared_ptr<AudioFileSource> fs;
+
+                       if ((fs = boost::dynamic_pointer_cast<AudioFileSource> (siter->second)) != 0) {
+
+                               /* Don't save sources that are empty, unless they're destructive (which are OK
+                                  if they are empty, because we will re-use them every time.)
+                               */
+
+                               if (!fs->destructive()) {
+                                       if (fs->length() == 0) {
+                                               continue;
+                                       }
                                }
                        }
                        
-                       child->add_child_nocopy ((*siter).second->get_state());
+                       child->add_child_nocopy (siter->second->get_state());
                }
        }
 
        child = node->add_child ("Regions");
 
        if (full_state) { 
-               LockMonitor rl (region_lock, __LINE__, __FILE__);
+               Glib::Mutex::Lock rl (region_lock);
 
-               for (AudioRegionList::const_iterator i = audio_regions.begin(); i != audio_regions.end(); ++i) {
+               for (RegionList::const_iterator i = regions.begin(); i != regions.end(); ++i) {
                        
                        /* only store regions not attached to playlists */
 
-                       if ((*i).second->playlist() == 0) {
+                       if (i->second->playlist() == 0) {
                                child->add_child_nocopy (i->second->state (true));
                        }
                }
@@ -1313,19 +940,32 @@ Session::state(bool full_state)
        child = node->add_child ("DiskStreams");
 
        { 
-               RWLockMonitor dl (diskstream_lock, false, __LINE__, __FILE__);
-               for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
+               boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
+               for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
                        if (!(*i)->hidden()) {
                                child->add_child_nocopy ((*i)->get_state());
                        }
                }
        }
 
-       node->add_child_nocopy (_locations.get_state());
+       if (full_state) {
+               node->add_child_nocopy (_locations.get_state());
+       } else {
+               // for a template, just create a new Locations, populate it
+               // with the default start and end, and get the state for that.
+               Locations loc;
+               Location* start = new Location(0, 0, _("start"), Location::Flags ((Location::IsMark|Location::IsStart)));
+               Location* end = new Location(0, 0, _("end"), Location::Flags ((Location::IsMark|Location::IsEnd)));
+               start->set_end(0);
+               loc.add (start);
+               end->set_end(compute_initial_length());
+               loc.add (end);
+               node->add_child_nocopy (loc.get_state());
+       }
        
        child = node->add_child ("Connections");
        {
-               LockMonitor lm (connection_lock, __LINE__, __FILE__);
+               Glib::Mutex::Lock lm (connection_lock);
                for (ConnectionList::iterator i = _connections.begin(); i != _connections.end(); ++i) {
                        if (!(*i)->system_dependent()) {
                                child->add_child_nocopy ((*i)->get_state());
@@ -1335,10 +975,10 @@ Session::state(bool full_state)
 
        child = node->add_child ("Routes");
        {
-               RWLockMonitor lm (route_lock, false, __LINE__, __FILE__);
+               boost::shared_ptr<RouteList> r = routes.reader ();
                
                RoutePublicOrderSorter cmp;
-               RouteList public_order(routes);
+               RouteList public_order (*r);
                public_order.sort (cmp);
                
                for (RouteList::iterator i = public_order.begin(); i != public_order.end(); ++i) {
@@ -1406,6 +1046,8 @@ Session::state(bool full_state)
 
        node->add_child_nocopy (_tempo_map->get_state());
 
+       node->add_child_nocopy (get_control_protocol_state());
+
        if (_extra_xml) {
                node->add_child_copy (*_extra_xml);
        }
@@ -1413,6 +1055,25 @@ Session::state(bool full_state)
        return *node;
 }
 
+XMLNode&
+Session::get_control_protocol_state ()
+{
+       ControlProtocolManager& cpm (ControlProtocolManager::instance());
+       XMLNode* node = new XMLNode (X_("ControlProtocols"));
+
+       cpm.foreach_known_protocol (bind (mem_fun (*this, &Session::add_control_protocol), node));
+       
+       return *node;
+}
+
+void
+Session::add_control_protocol (const ControlProtocolInfo* const cpi, XMLNode* node)
+{
+       if (cpi->protocol) {
+               node->add_child_nocopy (cpi->protocol->get_state());
+       }
+}
+
 int
 Session::set_state (const XMLNode& node)
 {
@@ -1423,16 +1084,31 @@ Session::set_state (const XMLNode& node)
 
        _state_of_the_state = StateOfTheState (_state_of_the_state|CannotSave);
        
-       if (node.name() != "Session"){
+       if (node.name() != X_("Session")){
                fatal << _("programming error: Session: incorrect XML node sent to set_state()") << endmsg;
                return -1;
        }
 
-       StateManager::prohibit_save ();
-
        if ((prop = node.property ("name")) != 0) {
                _name = prop->value ();
        }
+
+       setup_raid_path(_path);
+
+       if ((prop = node.property (X_("id-counter"))) != 0) {
+               uint64_t x;
+               sscanf (prop->value().c_str(), "%" PRIu64, &x);
+               ID::init_counter (x);
+       } else {
+               /* old sessions used a timebased counter, so fake
+                  the startup ID counter based on a standard
+                  timestamp.
+               */
+               time_t now;
+               time (&now);
+               ID::init_counter (now);
+       }
+
        
        IO::disable_ports ();
        IO::disable_connecting ();
@@ -1442,38 +1118,67 @@ Session::set_state (const XMLNode& node)
        MIDI
        Path
        extra
-       Options
+       Options/Config
+       Locations
        Sources
        AudioRegions
-       DiskStreams
+       AudioDiskstreams
        Connections
-       Locations
        Routes
        EditGroups
        MixGroups
        Click
+       ControlProtocols
        */
 
        if (use_config_midi_ports ()) {
        }
 
-       if ((child = find_named_node (node, "Path")) != 0) {
-               /* XXX this XML content stuff horrible API design */
-               string raid_path = _path + ':' + child->children().front()->content();
-               setup_raid_path (raid_path);
-       } else {
-               /* the path is already set */
-       }
-
        if ((child = find_named_node (node, "extra")) != 0) {
                _extra_xml = new XMLNode (*child);
        }
 
-       if ((child = find_named_node (node, "Options")) == 0) {
+       if (((child = find_named_node (node, "Options")) != 0)) { /* old style */
+               load_options (*child);
+       } else if ((child = find_named_node (node, "Config")) != 0) { /* new style */
+               load_options (*child);
+       } else {
                error << _("Session: XML state has no options section") << endmsg;
-       } else if (load_options (*child)) {
        }
 
+       if ((child = find_named_node (node, "Locations")) == 0) {
+               error << _("Session: XML state has no locations section") << endmsg;
+               goto out;
+       } else if (_locations.set_state (*child)) {
+               goto out;
+       }
+
+       Location* location;
+
+       if ((location = _locations.auto_loop_location()) != 0) {
+               set_auto_loop_location (location);
+       }
+
+       if ((location = _locations.auto_punch_location()) != 0) {
+               set_auto_punch_location (location);
+       }
+
+       if ((location = _locations.end_location()) == 0) {
+               _locations.add (end_location);
+       } else {
+               delete end_location;
+               end_location = location;
+       }
+
+       if ((location = _locations.start_location()) == 0) {
+               _locations.add (start_location);
+       } else {
+               delete start_location;
+               start_location = location;
+       }
+
+       AudioFileSource::set_header_position_offset (start_location->start());
+
        if ((child = find_named_node (node, "Sources")) == 0) {
                error << _("Session: XML state has no sources section") << endmsg;
                goto out;
@@ -1521,31 +1226,6 @@ Session::set_state (const XMLNode& node)
                goto out;
        }
 
-       if ((child = find_named_node (node, "Locations")) == 0) {
-               error << _("Session: XML state has no locations section") << endmsg;
-               goto out;
-       } else if (_locations.set_state (*child)) {
-               goto out;
-       }
-
-       Location* location;
-
-       if ((location = _locations.auto_loop_location()) != 0) {
-               set_auto_loop_location (location);
-       }
-
-       if ((location = _locations.auto_punch_location()) != 0) {
-               set_auto_punch_location (location);
-       }
-
-       if ((location = _locations.end_location()) == 0) {
-               _locations.add (end_location);
-       } else {
-               end_location = location;
-       }
-
-       _locations.save_state (_("initial state"));
-
        if ((child = find_named_node (node, "EditGroups")) == 0) {
                error << _("Session: XML state has no edit groups section") << endmsg;
                goto out;
@@ -1580,9 +1260,9 @@ Session::set_state (const XMLNode& node)
                _click_io->set_state (*child);
        }
        
-       /* OK, now we can set edit mode */
-
-       set_edit_mode (pending_edit_mode);
+       if ((child = find_named_node (node, "ControlProtocols")) != 0) {
+               ControlProtocolManager::instance().set_protocol_states (*child);
+       }
 
        /* here beginneth the second phase ... */
 
@@ -1590,8 +1270,6 @@ Session::set_state (const XMLNode& node)
 
        _state_of_the_state = Clean;
 
-       StateManager::allow_save (_("initial state"), true);
-
        if (state_was_pending) {
                save_state (_current_snapshot_name);
                remove_pending_capture_state ();
@@ -1601,8 +1279,6 @@ Session::set_state (const XMLNode& node)
        return 0;
 
   out:
-       /* we failed, re-enable state saving but don't actually save internal state */
-       StateManager::allow_save (X_("ignored"), false);
        return ret;
 }
 
@@ -1611,7 +1287,7 @@ Session::load_routes (const XMLNode& node)
 {
        XMLNodeList nlist;
        XMLNodeConstIterator niter;
-       Route *route;
+       RouteList new_routes;
 
        nlist = node.children();
 
@@ -1619,28 +1295,48 @@ Session::load_routes (const XMLNode& node)
 
        for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
 
-               if ((route = XMLRouteFactory (**niter)) == 0) {
+               boost::shared_ptr<Route> route (XMLRouteFactory (**niter));
+
+               if (route == 0) {
                        error << _("Session: cannot create Route from XML description.")                              << endmsg;
                        return -1;
                }
 
-               add_route (route);
+               new_routes.push_back (route);
        }
 
+       add_routes (new_routes);
+
        return 0;
 }
 
-Route *
+boost::shared_ptr<Route>
 Session::XMLRouteFactory (const XMLNode& node)
 {
        if (node.name() != "Route") {
-               return 0;
+               return boost::shared_ptr<Route> ((Route*) 0);
        }
 
-       if (node.property ("diskstream") != 0 || node.property ("diskstream-id") != 0) {
-               return new AudioTrack (*this, node);
+       bool has_diskstream = (node.property ("diskstream") != 0 || node.property ("diskstream-id") != 0);
+       
+       DataType type = DataType::AUDIO;
+       const XMLProperty* prop = node.property("default-type");
+       if (prop)
+               type = DataType(prop->value());
+       
+       assert(type != DataType::NIL);
+
+       if (has_diskstream) {
+               if (type == DataType::AUDIO) {
+                       boost::shared_ptr<Route> ret (new AudioTrack (*this, node));
+                       return ret;
+               } else {
+                       boost::shared_ptr<Route> ret (new MidiTrack (*this, node));
+                       return ret;
+               }
        } else {
-               return new Route (*this, node);
+               boost::shared_ptr<Route> ret (new Route (*this, node));
+               return ret;
        }
 }
 
@@ -1649,14 +1345,13 @@ Session::load_regions (const XMLNode& node)
 {
        XMLNodeList nlist;
        XMLNodeConstIterator niter;
-       AudioRegion* region;
+       boost::shared_ptr<Region> region;
 
        nlist = node.children();
 
        set_dirty();
 
        for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
-
                if ((region = XMLRegionFactory (**niter, false)) == 0) {
                        error << _("Session: cannot create Region from XML description.") << endmsg;
                }
@@ -1665,63 +1360,158 @@ Session::load_regions (const XMLNode& node)
        return 0;
 }
 
-AudioRegion *
+boost::shared_ptr<Region>
 Session::XMLRegionFactory (const XMLNode& node, bool full)
+{
+       const XMLProperty* type = node.property("type");
+
+       try {
+       
+       if ( !type || type->value() == "audio" ) {
+
+               return boost::shared_ptr<Region>(XMLAudioRegionFactory (node, full));
+       
+       } else if (type->value() == "midi") {
+               
+               return boost::shared_ptr<Region>(XMLMidiRegionFactory (node, full));
+
+       }
+       
+       } catch (failed_constructor& err) {
+               return boost::shared_ptr<Region> ();
+       }
+
+       return boost::shared_ptr<Region> ();
+}
+
+boost::shared_ptr<AudioRegion>
+Session::XMLAudioRegionFactory (const XMLNode& node, bool full)
 {
        const XMLProperty* prop;
-       id_t s_id;
-       Source* source;
-       AudioRegion::SourceList sources;
+       boost::shared_ptr<Source> source;
+       boost::shared_ptr<AudioSource> as;
+       SourceList sources;
        uint32_t nchans = 1;
        char buf[128];
        
        if (node.name() != X_("Region")) {
-               return 0;
+               return boost::shared_ptr<AudioRegion>();
        }
 
        if ((prop = node.property (X_("channels"))) != 0) {
                nchans = atoi (prop->value().c_str());
        }
 
+
+       if ((prop = node.property ("name")) == 0) {
+               cerr << "no name for this region\n";
+               abort ();
+       }
        
        if ((prop = node.property (X_("source-0"))) == 0) {
                if ((prop = node.property ("source")) == 0) {
                        error << _("Session: XMLNode describing a AudioRegion is incomplete (no source)") << endmsg;
-                       return 0;
+                       return boost::shared_ptr<AudioRegion>();
                }
        }
 
-       sscanf (prop->value().c_str(), "%" PRIu64, &s_id);
+       PBD::ID s_id (prop->value());
 
-       if ((source = get_source (s_id)) == 0) {
+       if ((source = source_by_id (s_id)) == 0) {
                error << string_compose(_("Session: XMLNode describing a AudioRegion references an unknown source id =%1"), s_id) << endmsg;
-               return 0;
+               return boost::shared_ptr<AudioRegion>();
+       }
+       
+       as = boost::dynamic_pointer_cast<AudioSource>(source);
+       if (!as) {
+               error << string_compose(_("Session: XMLNode describing a AudioRegion references a non-audio source id =%1"), s_id) << endmsg;
+               return boost::shared_ptr<AudioRegion>();
        }
 
-       sources.push_back(source);
+       sources.push_back (as);
 
        /* pickup other channels */
 
        for (uint32_t n=1; n < nchans; ++n) {
                snprintf (buf, sizeof(buf), X_("source-%d"), n);
                if ((prop = node.property (buf)) != 0) {
-                       sscanf (prop->value().c_str(), "%" PRIu64, &s_id);
                        
-                       if ((source = get_source (s_id)) == 0) {
-                               error << string_compose(_("Session: XMLNode describing a AudioRegion references an unknown source id =%1"), s_id) << endmsg;
-                               return 0;
+                       PBD::ID id2 (prop->value());
+                       
+                       if ((source = source_by_id (id2)) == 0) {
+                               error << string_compose(_("Session: XMLNode describing a AudioRegion references an unknown source id =%1"), id2) << endmsg;
+                               return boost::shared_ptr<AudioRegion>();
                        }
-                       sources.push_back(source);
+                       
+                       as = boost::dynamic_pointer_cast<AudioSource>(source);
+                       if (!as) {
+                               error << string_compose(_("Session: XMLNode describing a AudioRegion references a non-audio source id =%1"), id2) << endmsg;
+                               return boost::shared_ptr<AudioRegion>();
+                       }
+                       sources.push_back (as);
                }
        }
        
+       try {
+               boost::shared_ptr<AudioRegion> region (boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (sources, node)));
+               return region;
+                                                      
+       }
+
+       catch (failed_constructor& err) {
+               return boost::shared_ptr<AudioRegion>();
+       }
+}
+
+boost::shared_ptr<MidiRegion>
+Session::XMLMidiRegionFactory (const XMLNode& node, bool full)
+{
+       const XMLProperty* prop;
+       boost::shared_ptr<Source> source;
+       boost::shared_ptr<MidiSource> ms;
+       MidiRegion::SourceList sources;
+       uint32_t nchans = 1;
        
+       if (node.name() != X_("Region")) {
+               return boost::shared_ptr<MidiRegion>();
+       }
+
+       if ((prop = node.property (X_("channels"))) != 0) {
+               nchans = atoi (prop->value().c_str());
+       }
+
+       // Multiple midi channels?  that's just crazy talk
+       assert(nchans == 1);
+
+       if ((prop = node.property (X_("source-0"))) == 0) {
+               if ((prop = node.property ("source")) == 0) {
+                       error << _("Session: XMLNode describing a MidiRegion is incomplete (no source)") << endmsg;
+                       return boost::shared_ptr<MidiRegion>();
+               }
+       }
+
+       PBD::ID s_id (prop->value());
+
+       if ((source = source_by_id (s_id)) == 0) {
+               error << string_compose(_("Session: XMLNode describing a MidiRegion references an unknown source id =%1"), s_id) << endmsg;
+               return boost::shared_ptr<MidiRegion>();
+       }
+
+       ms = boost::dynamic_pointer_cast<MidiSource>(source);
+       if (!ms) {
+               error << string_compose(_("Session: XMLNode describing a MidiRegion references a non-midi source id =%1"), s_id) << endmsg;
+               return boost::shared_ptr<MidiRegion>();
+       }
+
+       sources.push_back (ms);
+
        try {
-               return new AudioRegion (sources, node);
+               boost::shared_ptr<MidiRegion> region (boost::dynamic_pointer_cast<MidiRegion> (RegionFactory::create (sources, node)));
+               return region;
        }
 
        catch (failed_constructor& err) {
-               return 0;
+               return boost::shared_ptr<MidiRegion>();
        }
 }
 
@@ -1730,12 +1520,14 @@ Session::get_sources_as_xml ()
 
 {
        XMLNode* node = new XMLNode (X_("Sources"));
-       LockMonitor lm (source_lock, __LINE__, __FILE__);
+       Glib::Mutex::Lock lm (source_lock);
 
-       for (SourceList::iterator i = sources.begin(); i != sources.end(); ++i) {
-               node->add_child_nocopy ((*i).second->get_state());
+       for (SourceMap::iterator i = sources.begin(); i != sources.end(); ++i) {
+               node->add_child_nocopy (i->second->get_state());
        }
 
+       /* XXX get MIDI and other sources here */
+
        return *node;
 }
 
@@ -1767,7 +1559,7 @@ Session::load_sources (const XMLNode& node)
 {
        XMLNodeList nlist;
        XMLNodeConstIterator niter;
-       Source* source;
+       boost::shared_ptr<Source> source;
 
        nlist = node.children();
 
@@ -1778,37 +1570,27 @@ Session::load_sources (const XMLNode& node)
                if ((source = XMLSourceFactory (**niter)) == 0) {
                        error << _("Session: cannot create Source from XML description.") << endmsg;
                }
+
        }
 
        return 0;
 }
 
-Source *
+boost::shared_ptr<Source>
 Session::XMLSourceFactory (const XMLNode& node)
 {
-       Source *src = 0;
-
        if (node.name() != "Source") {
-               return 0;
+               return boost::shared_ptr<Source>();
        }
 
        try {
-               src = new FileSource (node, frame_rate());
+               return SourceFactory::create (*this, node);
        }
        
        catch (failed_constructor& err) {
-
-               try {
-                       src = new SndFileSource (node);
-               }
-
-               catch (failed_constructor& err) {
-                       error << _("Found a sound file that cannot be used by Ardour. See the progammers.") << endmsg;
-                       return 0;
-               } 
+               error << _("Found a sound file that cannot be used by Ardour. Talk to the progammers.") << endmsg;
+               return boost::shared_ptr<Source>();
        }
-
-       return src;
 }
 
 int
@@ -1827,7 +1609,7 @@ Session::save_template (string template_name)
        if ((dp = opendir (dir.c_str()))) {
                closedir (dp);
        } else {
-               if (mkdir (dir.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)<0) {
+               if (g_mkdir_with_parents (dir.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) {
                        error << string_compose(_("Could not create mix templates directory \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
                        return -1;
                }
@@ -1881,7 +1663,7 @@ Session::refresh_disk_space ()
 #if HAVE_SYS_VFS_H
        struct statfs statfsbuf;
        vector<space_and_path>::iterator i;
-       LockMonitor lm (space_lock, __LINE__, __FILE__);
+       Glib::Mutex::Lock lm (space_lock);
        double scale;
 
        /* get freespace on every FS that is part of the session path */
@@ -1907,11 +1689,9 @@ Session::ensure_sound_dir (string path, string& result)
 
        /* Ensure that the parent directory exists */
        
-       if (mkdir (path.c_str(), 0775)) {
-               if (errno != EEXIST) {
-                       error << string_compose(_("cannot create session directory \"%1\"; ignored"), path) << endmsg;
-                       return -1;
-               }
+       if (g_mkdir_with_parents (path.c_str(), 0775)) {
+               error << string_compose(_("cannot create session directory \"%1\"; ignored"), path) << endmsg;
+               return -1;
        }
        
        /* Ensure that the sounds directory exists */
@@ -1920,33 +1700,27 @@ Session::ensure_sound_dir (string path, string& result)
        result += '/';
        result += sound_dir_name;
        
-       if (mkdir (result.c_str(), 0775)) {
-               if (errno != EEXIST) {
-                       error << string_compose(_("cannot create sounds directory \"%1\"; ignored"), result) << endmsg;
-                       return -1;
-               }
+       if (g_mkdir_with_parents (result.c_str(), 0775)) {
+               error << string_compose(_("cannot create sounds directory \"%1\"; ignored"), result) << endmsg;
+               return -1;
        }
 
        dead = path;
        dead += '/';
        dead += dead_sound_dir_name;
        
-       if (mkdir (dead.c_str(), 0775)) {
-               if (errno != EEXIST) {
-                       error << string_compose(_("cannot create dead sounds directory \"%1\"; ignored"), dead) << endmsg;
-                       return -1;
-               }
+       if (g_mkdir_with_parents (dead.c_str(), 0775)) {
+               error << string_compose(_("cannot create dead sounds directory \"%1\"; ignored"), dead) << endmsg;
+               return -1;
        }
 
        peak = path;
        peak += '/';
        peak += peak_dir_name;
        
-       if (mkdir (peak.c_str(), 0775)) {
-               if (errno != EEXIST) {
-                       error << string_compose(_("cannot create peak file directory \"%1\"; ignored"), peak) << endmsg;
-                       return -1;
-               }
+       if (g_mkdir_with_parents (peak.c_str(), 0775)) {
+               error << string_compose(_("cannot create peak file directory \"%1\"; ignored"), peak) << endmsg;
+               return -1;
        }
        
        /* callers expect this to be terminated ... */
@@ -1956,7 +1730,7 @@ Session::ensure_sound_dir (string path, string& result)
 }      
 
 string
-Session::discover_best_sound_dir ()
+Session::discover_best_sound_dir (bool destructive)
 {
        vector<space_and_path>::iterator i;
        string result;
@@ -1968,7 +1742,7 @@ Session::discover_best_sound_dir ()
        }
 
        /* OK, here's the algorithm we're following here:
-
+          
        We want to select which directory to use for 
        the next file source to be created. Ideally,
        we'd like to use a round-robin process so as to
@@ -2068,7 +1842,7 @@ Session::load_playlists (const XMLNode& node)
 {
        XMLNodeList nlist;
        XMLNodeConstIterator niter;
-       Playlist *playlist;
+       boost::shared_ptr<Playlist> playlist;
 
        nlist = node.children();
 
@@ -2089,7 +1863,7 @@ Session::load_unused_playlists (const XMLNode& node)
 {
        XMLNodeList nlist;
        XMLNodeConstIterator niter;
-       Playlist *playlist;
+       boost::shared_ptr<Playlist> playlist;
 
        nlist = node.children();
 
@@ -2104,22 +1878,21 @@ Session::load_unused_playlists (const XMLNode& node)
 
                // now manually untrack it
 
-               track_playlist (playlist, false);
+               track_playlist (false, boost::weak_ptr<Playlist> (playlist));
        }
 
        return 0;
 }
 
-
-Playlist *
+boost::shared_ptr<Playlist>
 Session::XMLPlaylistFactory (const XMLNode& node)
 {
        try {
-               return new AudioPlaylist (*this, node);
+               return PlaylistFactory::create (*this, node);
        }
 
        catch (failed_constructor& err) {
-               return 0;
+               return boost::shared_ptr<Playlist>();
        }
 }
 
@@ -2165,12 +1938,40 @@ Session::dead_sound_dir () const
        return res;
 }
 
-string
-Session::sound_dir () const
-{
-       string res = _path;
-       res += sound_dir_name;
+string
+Session::sound_dir (bool with_path) const
+{
+       /* support old session structure */
+
+       struct stat statbuf;
+       string old_nopath;
+       string old_withpath;
+
+       old_nopath += old_sound_dir_name;
+       old_nopath += '/';
+       
+       old_withpath = _path;
+       old_withpath += old_sound_dir_name;
+
+       if (stat (old_withpath.c_str(), &statbuf) == 0) {
+               if (with_path)
+                       return old_withpath;
+               
+               return old_nopath;
+       }
+
+       string res;
+
+       if (with_path) {
+               res = _path;
+       }
+
+       res += interchange_dir_name;
        res += '/';
+       res += legalize_for_path (_name);
+       res += '/';
+       res += sound_dir_name;
+
        return res;
 }
 
@@ -2194,22 +1995,27 @@ Session::automation_dir () const
 string
 Session::template_dir ()
 {
-       string path = Config->get_user_ardour_path();
+       string path = get_user_ardour_path();
        path += "templates/";
 
        return path;
 }
 
 string
-Session::template_path ()
+Session::suffixed_search_path (string suffix, bool data)
 {
        string path;
 
-       path += Config->get_user_ardour_path();
+       path += get_user_ardour_path();
        if (path[path.length()-1] != ':') {
                path += ':';
        }
-       path += Config->get_system_ardour_path();
+
+       if (data) {
+               path += get_system_data_path();
+       } else {
+               path += get_system_module_path();
+       }
 
        vector<string> split_path;
        
@@ -2218,7 +2024,8 @@ Session::template_path ()
 
        for (vector<string>::iterator i = split_path.begin(); i != split_path.end(); ++i) {
                path += *i;
-               path += "templates/";
+               path += suffix;
+               path += '/';
                
                if (distance (i, split_path.end()) != 1) {
                        path += ':';
@@ -2228,6 +2035,18 @@ Session::template_path ()
        return path;
 }
 
+string
+Session::template_path ()
+{
+       return suffixed_search_path (X_("templates"), true);
+}
+
+string
+Session::control_protocol_path ()
+{
+       return suffixed_search_path (X_("surfaces"), false);
+}
+
 int
 Session::load_connections (const XMLNode& node)
 {
@@ -2267,18 +2086,18 @@ Session::load_route_groups (const XMLNode& node, bool edit)
 {
        XMLNodeList nlist = node.children();
        XMLNodeConstIterator niter;
-       RouteGroup* route;
+       RouteGroup* rg;
 
        set_dirty();
 
        for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
                if ((*niter)->name() == "RouteGroup") {
                        if (edit) {
-                               route = add_edit_group ("");
-                               route->set_state (**niter);
+                               rg = add_edit_group ("");
+                               rg->set_state (**niter);
                        } else {
-                               route = add_mix_group ("");
-                               route->set_state (**niter);
+                               rg = add_mix_group ("");
+                               rg->set_state (**niter);
                        }
                }
        }
@@ -2286,23 +2105,6 @@ Session::load_route_groups (const XMLNode& node, bool edit)
        return 0;
 }                              
 
-void
-Session::swap_configuration(Configuration** new_config)
-{
-       RWLockMonitor lm (route_lock, true, __LINE__, __FILE__); // jlc - WHY?
-       Configuration* tmp = *new_config;
-       *new_config = Config;
-       Config = tmp;
-       set_dirty();
-}
-
-void
-Session::copy_configuration(Configuration* new_config)
-{
-       RWLockMonitor lm (route_lock, true, __LINE__, __FILE__);
-       new_config = new Configuration(*Config);
-}
-
 static bool
 state_file_filter (const string &str, void *arg)
 {
@@ -2326,7 +2128,7 @@ remove_end(string* state)
                statename = statename.substr (start+1);
        }
                
-       if ((end = statename.rfind(".ardour")) < 0) {
+       if ((end = statename.rfind(".ardour")) == string::npos) {
                end = statename.length();
        }
 
@@ -2362,7 +2164,7 @@ Session::auto_save()
 RouteGroup *
 Session::add_edit_group (string name)
 {
-       RouteGroup* rg = new RouteGroup (name);
+       RouteGroup* rg = new RouteGroup (*this, name);
        edit_groups.push_back (rg);
        edit_group_added (rg); /* EMIT SIGNAL */
        set_dirty();
@@ -2372,13 +2174,41 @@ Session::add_edit_group (string name)
 RouteGroup *
 Session::add_mix_group (string name)
 {
-       RouteGroup* rg = new RouteGroup (name, RouteGroup::Relative);
+       RouteGroup* rg = new RouteGroup (*this, name, RouteGroup::Relative);
        mix_groups.push_back (rg);
        mix_group_added (rg); /* EMIT SIGNAL */
        set_dirty();
        return rg;
 }
 
+void
+Session::remove_edit_group (RouteGroup& rg)
+{
+       list<RouteGroup*>::iterator i;
+
+       if ((i = find (edit_groups.begin(), edit_groups.end(), &rg)) != edit_groups.end()) {
+               (*i)->apply (&Route::drop_edit_group, this);
+               edit_groups.erase (i);
+               edit_group_removed (); /* EMIT SIGNAL */
+       }
+
+       delete &rg;
+}
+
+void
+Session::remove_mix_group (RouteGroup& rg)
+{
+       list<RouteGroup*>::iterator i;
+
+       if ((i = find (mix_groups.begin(), mix_groups.end(), &rg)) != mix_groups.end()) {
+               (*i)->apply (&Route::drop_mix_group, this);
+               mix_groups.erase (i);
+               mix_group_removed (); /* EMIT SIGNAL */
+       }
+
+       delete &rg;
+}
+
 RouteGroup *
 Session::mix_group_by_name (string name)
 {
@@ -2406,58 +2236,40 @@ Session::edit_group_by_name (string name)
 }
 
 void
-Session::set_meter_hold (float val)
+Session::begin_reversible_command (string name)
 {
-       _meter_hold = val;
-       MeterHoldChanged(); // emit
+       current_trans = new UndoTransaction;
+       current_trans->set_name (name);
 }
 
 void
-Session::set_meter_falloff (float val)
-{
-       _meter_falloff = val;
-       MeterFalloffChanged(); // emit
-}
-
-
-void
-Session::begin_reversible_command (string name, UndoAction* private_undo)
-{
-       current_cmd.clear ();
-       current_cmd.set_name (name);
-
-       if (private_undo) {
-               current_cmd.add_undo (*private_undo);
-       }
-}
-
-void
-Session::commit_reversible_command (UndoAction* private_redo)
+Session::commit_reversible_command (Command *cmd)
 {
        struct timeval now;
 
-       if (private_redo) {
-               current_cmd.add_redo_no_execute (*private_redo);
+       if (cmd) {
+               current_trans->add_command (cmd);
        }
 
        gettimeofday (&now, 0);
-       current_cmd.set_timestamp (now);
+       current_trans->set_timestamp (now);
 
-       history.add (current_cmd);
+       _history.add (current_trans);
 }
 
 Session::GlobalRouteBooleanState 
 Session::get_global_route_boolean (bool (Route::*method)(void) const)
 {
        GlobalRouteBooleanState s;
-       RWLockMonitor lm (route_lock, false, __LINE__, __FILE__);
+       boost::shared_ptr<RouteList> r = routes.reader ();
 
-       for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
+       for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
                if (!(*i)->hidden()) {
                        RouteBooleanState v;
                        
                        v.first =* i;
-                       v.second = ((*i)->*method)();
+                       Route* r = (*i).get();
+                       v.second = (r->*method)();
                        
                        s.push_back (v);
                }
@@ -2470,9 +2282,9 @@ Session::GlobalRouteMeterState
 Session::get_global_route_metering ()
 {
        GlobalRouteMeterState s;
-       RWLockMonitor lm (route_lock, false, __LINE__, __FILE__);
+       boost::shared_ptr<RouteList> r = routes.reader ();
 
-       for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
+       for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
                if (!(*i)->hidden()) {
                        RouteMeterState v;
                        
@@ -2490,7 +2302,12 @@ void
 Session::set_global_route_metering (GlobalRouteMeterState s, void* arg) 
 {
        for (GlobalRouteMeterState::iterator i = s.begin(); i != s.end(); ++i) {
-               i->first->set_meter_point (i->second, arg);
+
+               boost::shared_ptr<Route> r = (i->first.lock());
+
+               if (r) {
+                       r->set_meter_point (i->second, arg);
+               }
        }
 }
 
@@ -2498,7 +2315,13 @@ void
 Session::set_global_route_boolean (GlobalRouteBooleanState s, void (Route::*method)(bool, void*), void* arg)
 {
        for (GlobalRouteBooleanState::iterator i = s.begin(); i != s.end(); ++i) {
-               (i->first->*method) (i->second, arg);
+
+               boost::shared_ptr<Route> r = (i->first.lock());
+
+               if (r) {
+                       Route* rp = r.get();
+                       (rp->*method) (i->second, arg);
+               }
        }
 }
 
@@ -2520,6 +2343,7 @@ Session::set_global_record_enable (GlobalRouteBooleanState s, void* src)
        set_global_route_boolean (s, &Route::set_record_enable, src);
 }
 
+#if 0
 UndoAction
 Session::global_mute_memento (void* src)
 {
@@ -2543,6 +2367,7 @@ Session::global_record_enable_memento (void* src)
 {
        return sigc::bind (mem_fun (*this, &Session::set_global_record_enable), get_global_route_boolean (&Route::record_enabled), src);
 }
+#endif
 
 static bool
 template_filter (const string &str, void *arg)
@@ -2579,7 +2404,7 @@ Session::get_template_list (list<string> &template_names)
 int
 Session::read_favorite_dirs (FavoriteDirs & favs)
 {
-       string path = Config->get_user_ardour_path();
+       string path = get_user_ardour_path();
        path += "/favorite_dirs";
 
        ifstream fav (path.c_str());
@@ -2614,7 +2439,7 @@ Session::read_favorite_dirs (FavoriteDirs & favs)
 int
 Session::write_favorite_dirs (FavoriteDirs & favs)
 {
-       string path = Config->get_user_ardour_path();
+       string path = get_user_ardour_path();
        path += "/favorite_dirs";
 
        ofstream fav (path.c_str());
@@ -2728,11 +2553,20 @@ Session::find_all_sources_across_snapshots (set<string>& result, bool exclude_th
        return 0;
 }
 
+struct RegionCounter {
+    typedef std::map<PBD::ID,boost::shared_ptr<AudioSource> > AudioSourceList;
+    AudioSourceList::iterator iter;
+    boost::shared_ptr<Region> region;
+    uint32_t count;
+    
+    RegionCounter() : count (0) {}
+};
+
 int
 Session::cleanup_sources (Session::cleanup_report& rep)
 {
-       vector<Source*> dead_sources;
-       vector<Playlist*> playlists_tbd;
+       vector<boost::shared_ptr<Source> > dead_sources;
+       vector<boost::shared_ptr<Playlist> > playlists_tbd;
        PathScanner scanner;
        string sound_path;
        vector<space_and_path>::iterator i;
@@ -2771,78 +2605,36 @@ Session::cleanup_sources (Session::cleanup_report& rep)
 
        /* now delete any that were marked for deletion */
 
-       for (vector<Playlist*>::iterator x = playlists_tbd.begin(); x != playlists_tbd.end(); ++x) {
-               PlaylistList::iterator foo;
-
-               if ((foo = unused_playlists.find (*x)) != unused_playlists.end()) {
-                       unused_playlists.erase (foo);
-               }
-               delete *x;
+       for (vector<boost::shared_ptr<Playlist> >::iterator x = playlists_tbd.begin(); x != playlists_tbd.end(); ++x) {
+               (*x)->drop_references ();
        }
 
-       /* step 2: clear the undo/redo history for all playlists */
-
-       for (PlaylistList::iterator x = playlists.begin(); x != playlists.end(); ++x) {
-               (*x)->drop_all_states ();
-       }
+       playlists_tbd.clear ();
 
-       /* step 3: find all un-referenced sources */
+       /* step 2: find all un-used sources */
 
        rep.paths.clear ();
        rep.space = 0;
 
-       for (SourceList::iterator i = sources.begin(); i != sources.end(); ) {
-
-               SourceList::iterator tmp;
+       for (SourceMap::iterator i = sources.begin(); i != sources.end(); ) {
+               
+               SourceMap::iterator tmp;
 
                tmp = i;
                ++tmp;
 
-               /* only remove files that are not in use and have some size
-                  to them. otherwise we remove the current "nascent"
+               /* do not bother with files that are zero size, otherwise we remove the current "nascent"
                   capture files.
                */
 
-               if ((*i).second->use_cnt() == 0 && (*i).second->length() > 0) {
+               if (!i->second->used() && i->second->length() > 0) {
                        dead_sources.push_back (i->second);
-
-                       /* remove this source from our own list to avoid us
-                          adding it to the list of all sources below
-                       */
-
-                       sources.erase (i);
-               }
+                       i->second->GoingAway();
+               } 
 
                i = tmp;
        }
 
-       /* Step 4: get rid of all regions in the region list that use any dead sources
-          in case the sources themselves don't go away (they might be referenced in
-          other snapshots).
-       */
-               
-       for (vector<Source*>::iterator i = dead_sources.begin(); i != dead_sources.end();++i) {
-
-               for (AudioRegionList::iterator r = audio_regions.begin(); r != audio_regions.end(); ) {
-                       AudioRegionList::iterator tmp;
-                       AudioRegion* ar;
-
-                       tmp = r;
-                       ++tmp;
-                       
-                       ar = (*r).second;
-
-                       for (uint32_t n = 0; n < ar->n_channels(); ++n) {
-                               if (&ar->source (n) == (*i)) {
-                                       /* this region is dead */
-                                       remove_region (ar);
-                               }
-                       }
-                       
-                       r = tmp;
-               }
-       }
-
        /* build a list of all the possible sound directories for the session */
 
        for (i = session_dirs.begin(); i != session_dirs.end(); ) {
@@ -2851,7 +2643,7 @@ Session::cleanup_sources (Session::cleanup_report& rep)
                ++nexti;
 
                sound_path += (*i).path;
-               sound_path += sound_dir_name;
+               sound_path += sound_dir (false);
 
                if (nexti != session_dirs.end()) {
                        sound_path += ':';
@@ -2859,7 +2651,7 @@ Session::cleanup_sources (Session::cleanup_report& rep)
 
                i = nexti;
        }
-       
+
        /* now do the same thing for the files that ended up in the sounds dir(s) 
           but are not referenced as sources in any snapshot.
        */
@@ -2874,20 +2666,17 @@ Session::cleanup_sources (Session::cleanup_report& rep)
           state file on disk still references sources we may have already
           dropped.
        */
-
+       
        find_all_sources_across_snapshots (all_sources, true);
 
-       /* add our current source list
+       /*  add our current source list
         */
-
-       for (SourceList::iterator i = sources.begin(); i != sources.end(); ++i) {
-               FileSource* fs;
-               SndFileSource* sfs;
+       
+       for (SourceMap::iterator i = sources.begin(); i != sources.end(); ++i) {
+               boost::shared_ptr<AudioFileSource> fs;
                
-               if ((fs = dynamic_cast<FileSource*> ((*i).second)) != 0) {
+               if ((fs = boost::dynamic_pointer_cast<AudioFileSource> (i->second)) != 0) {
                        all_sources.insert (fs->path());
-               } else if ((sfs = dynamic_cast<SndFileSource*> ((*i).second)) != 0) {
-                       all_sources.insert (sfs->path());
                } 
        }
 
@@ -2902,7 +2691,6 @@ Session::cleanup_sources (Session::cleanup_report& rep)
                                used = true;
                                break;
                        }
-
                }
 
                if (!used) {
@@ -2927,13 +2715,33 @@ Session::cleanup_sources (Session::cleanup_report& rep)
                   on whichever filesystem it was already on.
                */
 
-               newpath = PBD::dirname (*x);
-               newpath = PBD::dirname (newpath);
+               if (_path.find ("/sounds/")) {
+
+                       /* old school, go up 1 level */
+
+                       newpath = Glib::path_get_dirname (*x);      // "sounds" 
+                       newpath = Glib::path_get_dirname (newpath); // "session-name"
+
+               } else {
+
+                       /* new school, go up 4 levels */
+                       
+                       newpath = Glib::path_get_dirname (*x);      // "audiofiles" 
+                       newpath = Glib::path_get_dirname (newpath); // "session-name"
+                       newpath = Glib::path_get_dirname (newpath); // "interchange"
+                       newpath = Glib::path_get_dirname (newpath); // "session-dir"
+               }
 
                newpath += '/';
                newpath += dead_sound_dir_name;
+
+               if (g_mkdir_with_parents (newpath.c_str(), 0755) < 0) {
+                       error << string_compose(_("Session: cannot create session peakfile dir \"%1\" (%2)"), newpath, strerror (errno)) << endmsg;
+                       return -1;
+               }
+
                newpath += '/';
-               newpath += PBD::basename ((*x));
+               newpath += Glib::path_get_basename ((*x));
                
                if (access (newpath.c_str(), F_OK) == 0) {
                        
@@ -2971,7 +2779,6 @@ Session::cleanup_sources (Session::cleanup_report& rep)
                              << endmsg;
                        goto out;
                }
-               
 
                /* see if there an easy to find peakfile for this file, and remove it.
                 */
@@ -2989,14 +2796,13 @@ Session::cleanup_sources (Session::cleanup_report& rep)
                                goto out;
                        }
                }
-
        }
 
        ret = 0;
 
        /* dump the history list */
 
-       history.clear ();
+       _history.clear ();
 
        /* save state so we don't end up a session file
           referring to non-existent sources.
@@ -3074,7 +2880,7 @@ void
 Session::set_dirty ()
 {
        bool was_dirty = dirty();
-       
+
        _state_of_the_state = StateOfTheState (_state_of_the_state | Dirty);
 
        if (!was_dirty) {
@@ -3095,3 +2901,354 @@ Session::set_clean ()
        }
 }
 
+void
+Session::set_deletion_in_progress ()
+{
+       _state_of_the_state = StateOfTheState (_state_of_the_state | Deletion);
+}
+
+void
+Session::add_controllable (Controllable* c)
+{
+       Glib::Mutex::Lock lm (controllables_lock);
+       controllables.insert (c);
+}
+
+void
+Session::remove_controllable (Controllable* c)
+{
+       if (_state_of_the_state | Deletion) {
+               return;
+       }
+
+       Glib::Mutex::Lock lm (controllables_lock);
+
+       Controllables::iterator x = controllables.find (c);
+
+       if (x != controllables.end()) {
+               controllables.erase (x);
+       }
+}      
+
+Controllable*
+Session::controllable_by_id (const PBD::ID& id)
+{
+       Glib::Mutex::Lock lm (controllables_lock);
+       
+       for (Controllables::iterator i = controllables.begin(); i != controllables.end(); ++i) {
+               if ((*i)->id() == id) {
+                       return *i;
+               }
+       }
+
+       return 0;
+}
+
+void 
+Session::add_instant_xml (XMLNode& node, const std::string& dir)
+{
+       Stateful::add_instant_xml (node, dir);
+       Config->add_instant_xml (node, get_user_ardour_path());
+}
+
+
+int 
+Session::save_history (string snapshot_name)
+{
+    XMLTree tree;
+    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;
+    }
+
+    xml_path = _path + snapshot_name + ".history"; 
+
+    bak_path = xml_path + ".bak";
+
+    if ((access (xml_path.c_str(), F_OK) == 0) &&
+        (rename (xml_path.c_str(), bak_path.c_str())))
+    {
+        error << _("could not backup old history file, current history not saved.") << endmsg;
+        return -1;
+    }
+
+    if (!tree.write (xml_path))
+    {
+        error << string_compose (_("history could not be saved to %1"), xml_path) << endmsg;
+
+        /* don't leave a corrupt file lying around if it is
+         * possible to fix.
+         */
+
+        if (unlink (xml_path.c_str())) {
+               error << string_compose (_("could not remove corrupt history file %1"), xml_path) << endmsg;
+        } else {
+               if (rename (bak_path.c_str(), xml_path.c_str())) 
+               {
+                       error << string_compose (_("could not restore history file from backup %1"), bak_path) << endmsg;
+               }
+        }
+
+        return -1;
+    }
+
+    return 0;
+}
+
+int
+Session::restore_history (string snapshot_name)
+{
+    XMLTree tree;
+    string xmlpath;
+
+    if (snapshot_name.empty()) {
+           snapshot_name = _current_snapshot_name;
+    }
+
+    /* read xml */
+    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;
+           return 1;
+    }
+
+    if (!tree.read (xmlpath)) {
+           error << string_compose (_("Could not understand session history file \"%1\""), xmlpath) << endmsg;
+           return -1;
+    }
+
+    /* replace history */
+    _history.clear();
+
+    for (XMLNodeConstIterator it  = tree.root()->children().begin(); it != tree.root()->children().end(); it++) {
+           
+           XMLNode *t = *it;
+           UndoTransaction* ut = new UndoTransaction ();
+           struct timeval tv;
+           
+           ut->set_name(t->property("name")->value());
+           stringstream ss(t->property("tv_sec")->value());
+           ss >> tv.tv_sec;
+           ss.str(t->property("tv_usec")->value());
+           ss >> tv.tv_usec;
+           ut->set_timestamp(tv);
+           
+           for (XMLNodeConstIterator child_it  = t->children().begin();
+                child_it != t->children().end();
+                child_it++)
+           {
+                   XMLNode *n = *child_it;
+                   Command *c;
+       
+                   if (n->name() == "MementoCommand" ||
+                       n->name() == "MementoUndoCommand" ||
+                       n->name() == "MementoRedoCommand") {
+
+                           if ((c = memento_command_factory(n))) {
+                                   ut->add_command(c);
+                           }
+                           
+                   } else if (n->name() == X_("GlobalRouteStateCommand")) {
+
+                           if ((c = global_state_command_factory (*n))) {
+                                   ut->add_command (c);
+                           }
+                           
+                   } else {
+
+                           error << string_compose(_("Couldn't figure out how to make a Command out of a %1 XMLNode."), n->name()) << endmsg;
+                   }
+           }
+
+           _history.add (ut);
+    }
+
+    return 0;
+}
+
+void
+Session::config_changed (const char* parameter_name)
+{
+#define PARAM_IS(x) (!strcmp (parameter_name, (x)))
+
+       if (PARAM_IS ("seamless-loop")) {
+               
+       } else if (PARAM_IS ("rf-speed")) {
+               
+       } else if (PARAM_IS ("auto-loop")) {
+               
+       } else if (PARAM_IS ("auto-input")) {
+
+               if (Config->get_monitoring_model() == HardwareMonitoring && transport_rolling()) {
+                       /* auto-input only makes a difference if we're rolling */
+                       
+                       boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
+                       
+                       for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
+                               if ((*i)->record_enabled ()) {
+                                       (*i)->monitor_input (!Config->get_auto_input());
+                               }
+                       }
+               }
+
+       } else if (PARAM_IS ("punch-in")) {
+
+               Location* location;
+               
+               if ((location = _locations.auto_punch_location()) != 0) {
+                       
+                       if (Config->get_punch_in ()) {
+                               replace_event (Event::PunchIn, location->start());
+                       } else {
+                               remove_event (location->start(), Event::PunchIn);
+                       }
+               }
+               
+       } else if (PARAM_IS ("punch-out")) {
+
+               Location* location;
+               
+               if ((location = _locations.auto_punch_location()) != 0) {
+                       
+                       if (Config->get_punch_out()) {
+                               replace_event (Event::PunchOut, location->end());
+                       } else {
+                               clear_events (Event::PunchOut);
+                       }
+               }
+
+       } else if (PARAM_IS ("edit-mode")) {
+
+               Glib::Mutex::Lock lm (playlist_lock);
+               
+               for (PlaylistList::iterator i = playlists.begin(); i != playlists.end(); ++i) {
+                       (*i)->set_edit_mode (Config->get_edit_mode ());
+               }
+
+       } else if (PARAM_IS ("use-video-sync")) {
+
+               if (transport_stopped()) {
+                       if (Config->get_use_video_sync()) {
+                               waiting_for_sync_offset = true;
+                       }
+               }
+
+       } else if (PARAM_IS ("mmc-control")) {
+
+               //poke_midi_thread ();
+
+       } else if (PARAM_IS ("midi-control")) {
+               
+               //poke_midi_thread ();
+
+       } else if (PARAM_IS ("raid-path")) {
+
+               setup_raid_path (Config->get_raid_path());
+
+       } else if (PARAM_IS ("smpte-format")) {
+
+               sync_time_vars ();
+
+       } else if (PARAM_IS ("video-pullup")) {
+
+               sync_time_vars ();
+
+       } else if (PARAM_IS ("seamless-loop")) {
+
+               if (play_loop && transport_rolling()) {
+                       // to reset diskstreams etc
+                       request_play_loop (true);
+               }
+
+       } else if (PARAM_IS ("rf-speed")) {
+
+               cumulative_rf_motion = 0;
+               reset_rf_scale (0);
+
+       } else if (PARAM_IS ("click-sound")) {
+
+               setup_click_sounds (1);
+
+       } else if (PARAM_IS ("click-emphasis-sound")) {
+
+               setup_click_sounds (-1);
+
+       } else if (PARAM_IS ("clicking")) {
+
+               if (Config->get_clicking()) {
+                       if (_click_io && click_data) { // don't require emphasis data
+                               _clicking = true;
+                       }
+               } else {
+                       _clicking = false;
+               }
+
+       } else if (PARAM_IS ("send-mtc")) {
+               
+               /* only set the internal flag if we have
+                  a port.
+               */
+               
+               if (_mtc_port != 0) {
+                       session_send_mtc = Config->get_send_mtc();
+                       if (session_send_mtc) {
+                               /* mark us ready to send */
+                               next_quarter_frame_to_send = 0;
+                       }
+               }
+
+       } else if (PARAM_IS ("send-mmc")) {
+               
+               /* only set the internal flag if we have
+                  a port.
+               */
+               
+               if (_mmc_port != 0) {
+                       session_send_mmc = Config->get_send_mmc();
+               }
+
+       } else if (PARAM_IS ("midi-feedback")) {
+               
+               /* only set the internal flag if we have
+                  a port.
+               */
+               
+               if (_mtc_port != 0) {
+                       session_midi_feedback = Config->get_midi_feedback();
+               }
+
+       } else if (PARAM_IS ("jack-time-master")) {
+
+               engine().reset_timebase ();
+
+       } else if (PARAM_IS ("native-file-header-format")) {
+
+               if (!first_file_header_format_reset) {
+                       reset_native_file_format ();
+               }
+
+               first_file_header_format_reset = false;
+
+       } else if (PARAM_IS ("native-file-data-format")) {
+
+               if (!first_file_data_format_reset) {
+                       reset_native_file_format ();
+               }
+
+               first_file_data_format_reset = false;
+
+       } else if (PARAM_IS ("slave-source")) {
+               set_slave_source (Config->get_slave_source());
+       }
+
+       set_dirty ();
+                  
+#undef PARAM_IS
+
+}