add global region fade toggle; fixup (?) reload of MIDI config state (both ported...
[ardour.git] / libs / ardour / session_state.cc
1 /*
2   Copyright (C) 1999-2002 Paul Davis 
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License as published by
6   the Free Software Foundation; either version 2 of the License, or
7   (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   GNU General Public License for more details.
13
14   You should have received a copy of the GNU General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17     
18 */
19
20 #define __STDC_FORMAT_MACROS 1
21 #include <stdint.h>
22
23 #include <algorithm>
24 #include <fstream>
25 #include <string>
26 #include <cerrno>
27
28 #include <sigc++/bind.h>
29
30 #include <cstdio> /* snprintf(3) ... grrr */
31 #include <cmath>
32 #include <unistd.h>
33 #include <sys/stat.h>
34 #include <climits>
35 #include <fcntl.h>
36 #include <poll.h>
37 #include <signal.h>
38 #include <sys/mman.h>
39 #include <sys/time.h>
40 #include <dirent.h>
41
42 #ifdef HAVE_SYS_VFS_H
43 #include <sys/vfs.h>
44 #else
45 #include <sys/param.h>
46 #include <sys/mount.h>
47 #endif
48
49 #include <glibmm.h>
50 #include <glibmm/thread.h>
51
52 #include <midi++/mmc.h>
53 #include <midi++/port.h>
54
55 #include <pbd/error.h>
56 #include <pbd/pathscanner.h>
57 #include <pbd/pthread_utils.h>
58 #include <pbd/search_path.h>
59 #include <pbd/stacktrace.h>
60
61 #include <ardour/audioengine.h>
62 #include <ardour/configuration.h>
63 #include <ardour/session.h>
64 #include <ardour/session_directory.h>
65 #include <ardour/session_utils.h>
66 #include <ardour/session_state_utils.h>
67 #include <ardour/session_metadata.h>
68 #include <ardour/buffer.h>
69 #include <ardour/audio_diskstream.h>
70 #include <ardour/midi_diskstream.h>
71 #include <ardour/utils.h>
72 #include <ardour/audioplaylist.h>
73 #include <ardour/midi_playlist.h>
74 #include <ardour/smf_source.h>
75 #include <ardour/audiofilesource.h>
76 #include <ardour/silentfilesource.h>
77 #include <ardour/sndfilesource.h>
78 #include <ardour/midi_source.h>
79 #include <ardour/sndfile_helpers.h>
80 #include <ardour/auditioner.h>
81 #include <ardour/io_processor.h>
82 #include <ardour/send.h>
83 #include <ardour/processor.h>
84 #include <ardour/bundle.h>
85 #include <ardour/slave.h>
86 #include <ardour/tempo.h>
87 #include <ardour/audio_track.h>
88 #include <ardour/midi_track.h>
89 #include <ardour/cycle_timer.h>
90 #include <ardour/utils.h>
91 #include <ardour/named_selection.h>
92 #include <ardour/version.h>
93 #include <ardour/location.h>
94 #include <ardour/audioregion.h>
95 #include <ardour/midi_region.h>
96 #include <ardour/crossfade.h>
97 #include <ardour/control_protocol_manager.h>
98 #include <ardour/region_factory.h>
99 #include <ardour/source_factory.h>
100 #include <ardour/playlist_factory.h>
101 #include <ardour/filename_extensions.h>
102 #include <ardour/directory_names.h>
103 #include <ardour/template_utils.h>
104
105 #include <control_protocol/control_protocol.h>
106
107 #include "i18n.h"
108 #include <locale.h>
109
110 using namespace std;
111 using namespace ARDOUR;
112 using namespace PBD;
113
114 void
115 Session::first_stage_init (string fullpath, string snapshot_name)
116 {
117         if (fullpath.length() == 0) {
118                 destroy ();
119                 throw failed_constructor();
120         }
121
122         char buf[PATH_MAX+1];
123         if (!realpath (fullpath.c_str(), buf) && (errno != ENOENT)) {
124                 error << string_compose(_("Could not use path %1 (%s)"), buf, strerror(errno)) << endmsg;
125                 destroy ();
126                 throw failed_constructor();
127         }
128
129         _path = string(buf);
130
131         if (_path[_path.length()-1] != '/') {
132                 _path += '/';
133         }
134
135         /* these two are just provisional settings. set_state()
136            will likely override them.
137         */
138
139         _name = _current_snapshot_name = snapshot_name;
140
141         set_history_depth (Config->get_history_depth());
142
143         _current_frame_rate = _engine.frame_rate ();
144         _nominal_frame_rate = _current_frame_rate;
145         _base_frame_rate = _current_frame_rate;
146
147         _tempo_map = new TempoMap (_current_frame_rate);
148         _tempo_map->StateChanged.connect (mem_fun (*this, &Session::tempo_map_changed));
149
150
151
152         g_atomic_int_set (&processing_prohibited, 0);
153         insert_cnt = 0;
154         _transport_speed = 0;
155         _last_transport_speed = 0;
156         auto_play_legal = false;
157         transport_sub_state = 0;
158         _transport_frame = 0;
159         last_stop_frame = 0;
160         end_location = new Location (0, 0, _("end"), Location::Flags ((Location::IsMark|Location::IsEnd)));
161         start_location = new Location (0, 0, _("start"), Location::Flags ((Location::IsMark|Location::IsStart)));
162         _end_location_is_free = true;
163         g_atomic_int_set (&_record_status, Disabled);
164         loop_changing = false;
165         play_loop = false;
166         _last_roll_location = 0;
167         _last_record_location = 0;
168         pending_locate_frame = 0;
169         pending_locate_roll = false;
170         pending_locate_flush = false;
171         audio_dstream_buffer_size = 0;
172         midi_dstream_buffer_size = 0;
173         state_tree = 0;
174         state_was_pending = false;
175         set_next_event ();
176         outbound_mtc_smpte_frame = 0;
177         next_quarter_frame_to_send = -1;
178         current_block_size = 0;
179         solo_update_disabled = false;
180         currently_soloing = false;
181         _have_captured = false;
182         _worst_output_latency = 0;
183         _worst_input_latency = 0;
184         _worst_track_latency = 0;
185         _state_of_the_state = StateOfTheState(CannotSave|InitialConnecting|Loading);
186
187         _slave = 0;
188         butler_mixdown_buffer = 0;
189         butler_gain_buffer = 0;
190         mmc = 0;
191         session_send_mmc = false;
192         session_send_mtc = false;
193         post_transport_work = PostTransportWork (0);
194         g_atomic_int_set (&butler_should_do_transport_work, 0);
195         g_atomic_int_set (&butler_active, 0);
196         g_atomic_int_set (&_playback_load, 100);
197         g_atomic_int_set (&_capture_load, 100);
198         g_atomic_int_set (&_playback_load_min, 100);
199         g_atomic_int_set (&_capture_load_min, 100);
200         _play_range = false;
201         _exporting = false;
202         _exporting_realtime = false;
203         _gain_automation_buffer = 0;
204         _pan_automation_buffer = 0;
205         _npan_buffers = 0;
206         pending_abort = false;
207         destructive_index = 0;
208         current_trans = 0;
209         first_file_data_format_reset = true;
210         first_file_header_format_reset = true;
211         butler_thread = (pthread_t) 0;
212         //midi_thread = (pthread_t) 0;
213
214         AudioDiskstream::allocate_working_buffers();
215
216         /* default short fade = 15ms */
217
218         Crossfade::set_short_xfade_length ((nframes_t) floor (Config->get_short_xfade_seconds() * frame_rate()));
219         SndFileSource::setup_standard_crossfades (frame_rate());
220
221         last_mmc_step.tv_sec = 0;
222         last_mmc_step.tv_usec = 0;
223         step_speed = 0.0;
224
225         /* click sounds are unset by default, which causes us to internal
226            waveforms for clicks.
227         */
228         
229         click_data = 0;
230         click_emphasis_data = 0;
231         click_length = 0;
232         click_emphasis_length = 0;
233         _clicking = false;
234
235         process_function = &Session::process_with_events;
236
237         if (Config->get_use_video_sync()) {
238                 waiting_for_sync_offset = true;
239         } else {
240                 waiting_for_sync_offset = false;
241         }
242
243         last_smpte_when = 0;
244         _smpte_offset = 0;
245         _smpte_offset_negative = true;
246         last_smpte_valid = false;
247
248         sync_time_vars ();
249
250         last_rr_session_dir = session_dirs.begin();
251         refresh_disk_space ();
252
253         // set_default_fade (0.2, 5.0); /* steepness, millisecs */
254
255         /* slave stuff */
256
257         average_slave_delta = 1800;
258         have_first_delta_accumulator = false;
259         delta_accumulator_cnt = 0;
260         slave_state = Stopped;
261
262         _engine.GraphReordered.connect (mem_fun (*this, &Session::graph_reordered));
263
264         /* These are all static "per-class" signals */
265
266         RegionFactory::CheckNewRegion.connect (mem_fun (*this, &Session::add_region));
267         SourceFactory::SourceCreated.connect (mem_fun (*this, &Session::add_source));
268         PlaylistFactory::PlaylistCreated.connect (mem_fun (*this, &Session::add_playlist));
269         Processor::ProcessorCreated.connect (mem_fun (*this, &Session::add_processor));
270         NamedSelection::NamedSelectionCreated.connect (mem_fun (*this, &Session::add_named_selection));
271         AutomationList::AutomationListCreated.connect (mem_fun (*this, &Session::add_automation_list));
272
273         Controllable::Destroyed.connect (mem_fun (*this, &Session::remove_controllable));
274
275         IO::PortCountChanged.connect (mem_fun (*this, &Session::ensure_buffers));
276
277         /* stop IO objects from doing stuff until we're ready for them */
278
279         IO::disable_panners ();
280         IO::disable_ports ();
281         IO::disable_connecting ();
282 }
283
284 int
285 Session::second_stage_init (bool new_session)
286 {
287         AudioFileSource::set_peak_dir (_session_dir->peak_path().to_string());
288
289         if (!new_session) {
290                 if (load_state (_current_snapshot_name)) {
291                         return -1;
292                 }
293                 remove_empty_sounds ();
294         }
295
296         if (start_butler_thread()) {
297                 return -1;
298         }
299
300         if (start_midi_thread ()) {
301                 return -1;
302         }
303
304         // set_state() will call setup_raid_path(), but if it's a new session we need
305         // to call setup_raid_path() here.
306
307         if (state_tree) {
308                 if (set_state (*state_tree->root())) {
309                         return -1;
310                 }
311         } else {
312                 setup_raid_path(_path);
313         }
314
315         /* we can't save till after ::when_engine_running() is called,
316            because otherwise we save state with no connections made.
317            therefore, we reset _state_of_the_state because ::set_state()
318            will have cleared it.
319
320            we also have to include Loading so that any events that get
321            generated between here and the end of ::when_engine_running()
322            will be processed directly rather than queued.
323         */
324
325         _state_of_the_state = StateOfTheState (_state_of_the_state|CannotSave|Loading);
326
327
328         _locations.changed.connect (mem_fun (this, &Session::locations_changed));
329         _locations.added.connect (mem_fun (this, &Session::locations_added));
330         setup_click_sounds (0);
331         setup_midi_control ();
332
333         /* Pay attention ... */
334
335         _engine.Halted.connect (mem_fun (*this, &Session::engine_halted));
336         _engine.Xrun.connect (mem_fun (*this, &Session::xrun_recovery));
337
338         try {
339                 when_engine_running();
340         }
341
342         /* handle this one in a different way than all others, so that its clear what happened */
343         
344         catch (AudioEngine::PortRegistrationFailure& err) {
345                 error << _("Unable to create all required ports")
346                       << endmsg;
347                 return -1;
348         }
349
350         catch (...) {
351                 return -1;
352         }
353
354         BootMessage (_("Reset Remote Controls"));
355
356         send_full_time_code (0);
357         _engine.transport_locate (0);
358         deliver_mmc (MIDI::MachineControl::cmdMmcReset, 0);
359         deliver_mmc (MIDI::MachineControl::cmdLocate, 0);
360
361         BootMessage (_("Reset Control Protocols"));
362
363         ControlProtocolManager::instance().set_session (*this);
364
365         if (new_session) {
366                 _end_location_is_free = true;
367         } else {
368                 _end_location_is_free = false;
369         }
370
371         _state_of_the_state = Clean;
372         
373         DirtyChanged (); /* EMIT SIGNAL */
374
375         if (state_was_pending) {
376                 save_state (_current_snapshot_name);
377                 remove_pending_capture_state ();
378                 state_was_pending = false;
379         }
380         
381         BootMessage (_("Session loading complete"));
382
383         return 0;
384 }
385
386 string
387 Session::raid_path () const
388 {
389         SearchPath raid_search_path;
390
391         for (vector<space_and_path>::const_iterator i = session_dirs.begin(); i != session_dirs.end(); ++i) {
392                 raid_search_path += sys::path((*i).path);
393         }
394         
395         return raid_search_path.to_string ();
396 }
397
398 void
399 Session::setup_raid_path (string path)
400 {
401         if (path.empty()) {
402                 return;
403         }
404         
405         space_and_path sp;
406         string fspath;
407
408         session_dirs.clear ();
409
410         SearchPath search_path(path);
411         SearchPath sound_search_path;
412         SearchPath midi_search_path;
413
414         for (
415                         SearchPath::const_iterator i = search_path.begin();
416                         i != search_path.end();
417                         ++i
418                 )
419         {
420                 sp.path = (*i).to_string ();
421                 sp.blocks = 0; // not needed
422                 session_dirs.push_back (sp);
423
424                 SessionDirectory sdir(sp.path);
425
426                 sound_search_path += sdir.sound_path ();
427                 midi_search_path += sdir.midi_path ();
428         }
429
430         // set the AudioFileSource and SMFSource search path
431
432         AudioFileSource::set_search_path (sound_search_path.to_string ());
433         SMFSource::set_search_path (midi_search_path.to_string ());
434
435
436         // reset the round-robin soundfile path thingie
437
438         last_rr_session_dir = session_dirs.begin();
439 }
440
441 int
442 Session::ensure_subdirs ()
443 {
444         string dir;
445
446         dir = session_directory().peak_path().to_string();
447
448         if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
449                 error << string_compose(_("Session: cannot create session peakfile folder \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
450                 return -1;
451         }
452
453         dir = session_directory().sound_path().to_string();
454
455         if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
456                 error << string_compose(_("Session: cannot create session sounds dir \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
457                 return -1;
458         }
459         
460         dir = session_directory().midi_path().to_string();
461
462         if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
463                 error << string_compose(_("Session: cannot create session midi dir \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
464                 return -1;
465         }
466
467         dir = session_directory().dead_sound_path().to_string();
468
469         if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
470                 error << string_compose(_("Session: cannot create session dead sounds folder \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
471                 return -1;
472         }
473
474         dir = session_directory().export_path().to_string();
475
476         if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
477                 error << string_compose(_("Session: cannot create session export folder \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
478                 return -1;
479         }
480
481         dir = analysis_dir ();
482
483         if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
484                 error << string_compose(_("Session: cannot create session analysis folder \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
485                 return -1;
486         }
487
488         return 0;
489 }
490
491 int
492 Session::create (bool& new_session, const string& mix_template, nframes_t initial_length)
493 {
494
495         if (g_mkdir_with_parents (_path.c_str(), 0755) < 0) {
496                 error << string_compose(_("Session: cannot create session folder \"%1\" (%2)"), _path, strerror (errno)) << endmsg;
497                 return -1;
498         }
499
500         if (ensure_subdirs ()) {
501                 return -1;
502         }
503
504         /* check new_session so we don't overwrite an existing one */
505
506         if (!mix_template.empty()) {
507                 std::string in_path = mix_template;
508
509                 ifstream in(in_path.c_str());
510
511                 if (in){
512                         string out_path = _path;
513                         out_path += _name;
514                         out_path += statefile_suffix;
515
516                         ofstream out(out_path.c_str());
517
518                         if (out){
519                                 out << in.rdbuf();
520
521                                 // okay, session is set up.  Treat like normal saved
522                                 // session from now on.
523
524                                 new_session = false;
525                                 return 0;
526
527                         } else {
528                                 error << string_compose (_("Could not open %1 for writing mix template"), out_path) 
529                                         << endmsg;
530                                 return -1;
531                         }
532
533                 } else {
534                         error << string_compose (_("Could not open mix template %1 for reading"), in_path) 
535                                 << endmsg;
536                         return -1;
537                 }
538
539         }
540         
541         /* Instantiate metadata */
542         
543         _metadata = new SessionMetadata ();
544
545         /* set initial start + end point */
546
547         start_location->set_end (0);
548         _locations.add (start_location);
549
550         end_location->set_end (initial_length);
551         _locations.add (end_location);
552
553         _state_of_the_state = Clean;
554
555         save_state ("");
556
557         return 0;
558 }
559
560
561 int
562 Session::load_diskstreams (const XMLNode& node)
563 {
564         XMLNodeList          clist;
565         XMLNodeConstIterator citer;
566         
567         clist = node.children();
568
569         for (citer = clist.begin(); citer != clist.end(); ++citer) {
570
571                 try {
572                         /* diskstreams added automatically by DiskstreamCreated handler */
573                         if ((*citer)->name() == "AudioDiskstream" || (*citer)->name() == "DiskStream") {
574                                 boost::shared_ptr<AudioDiskstream> dstream (new AudioDiskstream (*this, **citer));
575                                 add_diskstream (dstream);
576                         } else if ((*citer)->name() == "MidiDiskstream") {
577                                 boost::shared_ptr<MidiDiskstream> dstream (new MidiDiskstream (*this, **citer));
578                                 add_diskstream (dstream);
579                         } else {
580                                 error << _("Session: unknown diskstream type in XML") << endmsg;
581                         }
582                 } 
583                 
584                 catch (failed_constructor& err) {
585                         error << _("Session: could not load diskstream via XML state") << endmsg;
586                         return -1;
587                 }
588         }
589
590         return 0;
591 }
592
593 void
594 Session::maybe_write_autosave()
595 {
596         if (dirty() && record_status() != Recording) {
597                 save_state("", true);
598         }
599 }
600
601 void
602 Session::remove_pending_capture_state ()
603 {
604         sys::path pending_state_file_path(_session_dir->root_path());
605         
606         pending_state_file_path /= _current_snapshot_name + pending_suffix;
607
608         try
609         {
610                 sys::remove (pending_state_file_path);
611         }
612         catch(sys::filesystem_error& ex)
613         {
614                 error << string_compose(_("Could remove pending capture state at path \"%1\" (%2)"),
615                                 pending_state_file_path.to_string(), ex.what()) << endmsg;
616         }
617 }
618
619 /** Rename a state file.
620  * @param snapshot_name Snapshot name.
621  */
622 void
623 Session::rename_state (string old_name, string new_name)
624 {
625         if (old_name == _current_snapshot_name || old_name == _name) {
626                 /* refuse to rename the current snapshot or the "main" one */
627                 return;
628         }
629
630         const string old_xml_filename = old_name + statefile_suffix;
631         const string new_xml_filename = new_name + statefile_suffix;
632
633         const sys::path old_xml_path = _session_dir->root_path() / old_xml_filename;
634         const sys::path new_xml_path = _session_dir->root_path() / new_xml_filename;
635
636         try
637         {
638                 sys::rename (old_xml_path, new_xml_path);
639         }
640         catch (const sys::filesystem_error& err)
641         {
642                 error << string_compose(_("could not rename snapshot %1 to %2 (%3)"),
643                                 old_name, new_name, err.what()) << endmsg;
644         }
645 }
646
647 /** Remove a state file.
648  * @param snapshot_name Snapshot name.
649  */
650 void
651 Session::remove_state (string snapshot_name)
652 {
653         if (snapshot_name == _current_snapshot_name || snapshot_name == _name) {
654                 // refuse to remove the current snapshot or the "main" one
655                 return;
656         }
657
658         sys::path xml_path(_session_dir->root_path());
659
660         xml_path /= snapshot_name + statefile_suffix;
661
662         if (!create_backup_file (xml_path)) {
663                 // don't remove it if a backup can't be made
664                 // create_backup_file will log the error.
665                 return;
666         }
667
668         // and delete it
669         sys::remove (xml_path);
670 }
671
672 int
673 Session::save_state (string snapshot_name, bool pending)
674 {
675         XMLTree tree;
676         sys::path xml_path(_session_dir->root_path());
677
678         if (_state_of_the_state & CannotSave) {
679                 return 1;
680         }
681
682         if (!_engine.connected ()) {
683                 error << _("Ardour's audio engine is not connected and state saving would lose all I/O connections. Session not saved")
684                       << endmsg;
685                 return 1;
686         }
687
688         /* tell sources we're saving first, in case they write out to a new file
689          * which should be saved with the state rather than the old one */
690         for (SourceMap::const_iterator i = sources.begin(); i != sources.end(); ++i)
691                 i->second->session_saved();
692
693         tree.set_root (&get_state());
694
695         if (snapshot_name.empty()) {
696                 snapshot_name = _current_snapshot_name;
697         }
698
699         if (!pending) {
700
701                 /* proper save: use statefile_suffix (.ardour in English) */
702                 
703                 xml_path /= snapshot_name + statefile_suffix;
704
705                 /* make a backup copy of the old file */
706
707                 if (sys::exists(xml_path) && !create_backup_file (xml_path)) {
708                         // create_backup_file will log the error
709                         return -1;
710                 }
711
712         } else {
713
714                 /* pending save: use pending_suffix (.pending in English) */
715                 xml_path /= snapshot_name + pending_suffix;
716         }
717
718         sys::path tmp_path(_session_dir->root_path());
719
720         tmp_path /= snapshot_name + temp_suffix;
721
722         // cerr << "actually writing state to " << xml_path.to_string() << endl;
723
724         if (!tree.write (tmp_path.to_string())) {
725                 error << string_compose (_("state could not be saved to %1"), tmp_path.to_string()) << endmsg;
726                 sys::remove (tmp_path);
727                 return -1;
728
729         } else {
730
731                 if (rename (tmp_path.to_string().c_str(), xml_path.to_string().c_str()) != 0) {
732                         error << string_compose (_("could not rename temporary session file %1 to %2"),
733                                         tmp_path.to_string(), xml_path.to_string()) << endmsg;
734                         sys::remove (tmp_path);
735                         return -1;
736                 }
737         }
738
739         if (!pending) {
740
741                 save_history (snapshot_name);
742
743                 bool was_dirty = dirty();
744
745                 _state_of_the_state = StateOfTheState (_state_of_the_state & ~Dirty);
746                 
747                 if (was_dirty) {
748                         DirtyChanged (); /* EMIT SIGNAL */
749                 }
750
751                 StateSaved (snapshot_name); /* EMIT SIGNAL */
752         }
753
754         return 0;
755 }
756
757 int
758 Session::restore_state (string snapshot_name)
759 {
760         if (load_state (snapshot_name) == 0) {
761                 set_state (*state_tree->root());
762         }
763         
764         return 0;
765 }
766
767 int
768 Session::load_state (string snapshot_name)
769 {
770         if (state_tree) {
771                 delete state_tree;
772                 state_tree = 0;
773         }
774
775         state_was_pending = false;
776
777         /* check for leftover pending state from a crashed capture attempt */
778
779         sys::path xmlpath(_session_dir->root_path());
780         xmlpath /= snapshot_name + pending_suffix;
781
782         if (sys::exists (xmlpath)) {
783
784                 /* there is pending state from a crashed capture attempt */
785
786                 if (AskAboutPendingState()) {
787                         state_was_pending = true;
788                 } 
789         } 
790
791         if (!state_was_pending) {
792                 xmlpath = _session_dir->root_path();
793                 xmlpath /= snapshot_name + statefile_suffix;
794         }
795         
796         if (!sys::exists (xmlpath)) {
797                 error << string_compose(_("%1: session state information file \"%2\" doesn't exist!"), _name, xmlpath.to_string()) << endmsg;
798                 return 1;
799         }
800
801         state_tree = new XMLTree;
802
803         set_dirty();
804
805         if (!state_tree->read (xmlpath.to_string())) {
806                 error << string_compose(_("Could not understand ardour file %1"), xmlpath.to_string()) << endmsg;
807                 delete state_tree;
808                 state_tree = 0;
809                 return -1;
810         }
811
812         XMLNode& root (*state_tree->root());
813         
814         if (root.name() != X_("Session")) {
815                 error << string_compose (_("Session file %1 is not an Ardour session"), xmlpath.to_string()) << endmsg;
816                 delete state_tree;
817                 state_tree = 0;
818                 return -1;
819         }
820
821         const XMLProperty* prop;
822         bool is_old = false; // session is _very_ old (pre-2.0)
823
824         if ((prop = root.property ("version")) == 0) {
825                 /* no version implies very old version of Ardour */
826                 is_old = true;
827         } else {
828                 int major_version;
829                 major_version = atoi (prop->value().c_str()); // grab just the first number before the period
830                 if (major_version < 2) {
831                         is_old = true;
832                 }
833         }
834
835         if (is_old) {
836
837                 sys::path backup_path(_session_dir->root_path());
838
839                 backup_path /= snapshot_name + "-1" + statefile_suffix;
840
841                 // only create a backup once
842                 if (sys::exists (backup_path)) {
843                         return 0;
844                 }
845
846                 info << string_compose (_("Copying old session file %1 to %2\nUse %2 with Ardour versions before 2.0 from now on"),
847                                         xmlpath.to_string(), backup_path.to_string()) 
848                      << endmsg;
849
850                 try
851                 {
852                         sys::copy_file (xmlpath, backup_path);
853                 }
854                 catch(sys::filesystem_error& ex)
855                 {
856                         error << string_compose (_("Unable to make backup of state file %1 (%2)"),
857                                         xmlpath.to_string(), ex.what())
858                                 << endmsg;
859                         return -1;
860                 }
861         }
862
863         return 0;
864 }
865
866 int
867 Session::load_options (const XMLNode& node)
868 {
869         XMLNode* child;
870         XMLProperty* prop;
871         LocaleGuard lg (X_("POSIX"));
872
873         Config->set_variables (node, ConfigVariableBase::Session);
874
875         /* now reset MIDI ports because the session can have its own 
876            MIDI configuration.
877         */
878
879         setup_midi ();
880
881         if ((child = find_named_node (node, "end-marker-is-free")) != 0) {
882                 if ((prop = child->property ("val")) != 0) {
883                         _end_location_is_free = (prop->value() == "yes");
884                 }
885         }
886
887         return 0;
888 }
889
890 bool
891 Session::save_config_options_predicate (ConfigVariableBase::Owner owner) const
892 {
893         const ConfigVariableBase::Owner modified_by_session_or_user = (ConfigVariableBase::Owner)
894                 (ConfigVariableBase::Session|ConfigVariableBase::Interface);
895
896         return owner & modified_by_session_or_user;
897 }
898
899 XMLNode&
900 Session::get_options () const
901 {
902         XMLNode* child;
903         LocaleGuard lg (X_("POSIX"));
904
905         XMLNode& option_root = Config->get_variables (mem_fun (*this, &Session::save_config_options_predicate));
906
907         child = option_root.add_child ("end-marker-is-free");
908         child->add_property ("val", _end_location_is_free ? "yes" : "no");
909
910         return option_root;
911 }
912
913 XMLNode&
914 Session::get_state()
915 {
916         return state(true);
917 }
918
919 XMLNode&
920 Session::get_template()
921 {
922         /* if we don't disable rec-enable, diskstreams
923            will believe they need to store their capture
924            sources in their state node. 
925         */
926         
927         disable_record (false);
928
929         return state(false);
930 }
931
932 XMLNode&
933 Session::state(bool full_state)
934 {
935         XMLNode* node = new XMLNode("Session");
936         XMLNode* child;
937
938         // store libardour version, just in case
939         char buf[16];
940         snprintf(buf, sizeof(buf), "%d.%d.%d", libardour3_major_version, libardour3_minor_version, libardour3_micro_version);
941         node->add_property("version", string(buf));
942                 
943         /* store configuration settings */
944
945         if (full_state) {
946         
947                 node->add_property ("name", _name);
948                 snprintf (buf, sizeof (buf), "%" PRId32, _nominal_frame_rate);
949                 node->add_property ("sample-rate", buf);
950
951                 if (session_dirs.size() > 1) {
952
953                         string p;
954
955                         vector<space_and_path>::iterator i = session_dirs.begin();
956                         vector<space_and_path>::iterator next;
957
958                         ++i; /* skip the first one */
959                         next = i;
960                         ++next;
961
962                         while (i != session_dirs.end()) {
963
964                                 p += (*i).path;
965
966                                 if (next != session_dirs.end()) {
967                                         p += ':';
968                                 } else {
969                                         break;
970                                 }
971
972                                 ++next;
973                                 ++i;
974                         }
975                         
976                         child = node->add_child ("Path");
977                         child->add_content (p);
978                 }
979         }
980
981         /* save the ID counter */
982         
983         snprintf (buf, sizeof (buf), "%" PRIu64, ID::counter());
984         node->add_property ("id-counter", buf);
985
986         /* various options */
987
988         node->add_child_nocopy (get_options());
989
990         node->add_child_nocopy (_metadata->get_state());
991
992         child = node->add_child ("Sources");
993
994         if (full_state) {
995                 Glib::Mutex::Lock sl (source_lock);
996
997                 for (SourceMap::iterator siter = sources.begin(); siter != sources.end(); ++siter) {
998                         
999                         /* Don't save information about AudioFileSources that are empty */
1000                         
1001                         boost::shared_ptr<AudioFileSource> fs;
1002
1003                         if ((fs = boost::dynamic_pointer_cast<AudioFileSource> (siter->second)) != 0) {
1004
1005                                 /* Don't save sources that are empty, unless they're destructive (which are OK
1006                                    if they are empty, because we will re-use them every time.)
1007                                 */
1008
1009                                 if (!fs->destructive()) {
1010                                         if (fs->length() == 0) {
1011                                                 continue;
1012                                         }
1013                                 }
1014                         }
1015                         
1016                         child->add_child_nocopy (siter->second->get_state());
1017                 }
1018         }
1019
1020         child = node->add_child ("Regions");
1021
1022         if (full_state) { 
1023                 Glib::Mutex::Lock rl (region_lock);
1024
1025                 for (RegionList::const_iterator i = regions.begin(); i != regions.end(); ++i) {
1026                         
1027                         /* only store regions not attached to playlists */
1028
1029                         if (i->second->playlist() == 0) {
1030                                 child->add_child_nocopy (i->second->state (true));
1031                         }
1032                 }
1033         }
1034
1035         child = node->add_child ("DiskStreams");
1036
1037         { 
1038                 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
1039                 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
1040                         if (!(*i)->hidden()) {
1041                                 child->add_child_nocopy ((*i)->get_state());
1042                         }
1043                 }
1044         }
1045
1046         if (full_state) {
1047                 node->add_child_nocopy (_locations.get_state());
1048         } else {
1049                 // for a template, just create a new Locations, populate it
1050                 // with the default start and end, and get the state for that.
1051                 Locations loc;
1052                 Location* start = new Location(0, 0, _("start"), Location::Flags ((Location::IsMark|Location::IsStart)));
1053                 Location* end = new Location(0, 0, _("end"), Location::Flags ((Location::IsMark|Location::IsEnd)));
1054                 start->set_end(0);
1055                 loc.add (start);
1056                 end->set_end(compute_initial_length());
1057                 loc.add (end);
1058                 node->add_child_nocopy (loc.get_state());
1059         }
1060         
1061         child = node->add_child ("Bundles");
1062         {
1063                 Glib::Mutex::Lock lm (bundle_lock);
1064                 for (BundleList::iterator i = _bundles.begin(); i != _bundles.end(); ++i) {
1065                         boost::shared_ptr<UserBundle> b = boost::dynamic_pointer_cast<UserBundle> (*i);
1066                         if (b) {
1067                                 child->add_child_nocopy (b->get_state());
1068                         }
1069                 }
1070         }
1071
1072         child = node->add_child ("Routes");
1073         {
1074                 boost::shared_ptr<RouteList> r = routes.reader ();
1075                 
1076                 RoutePublicOrderSorter cmp;
1077                 RouteList public_order (*r);
1078                 public_order.sort (cmp);
1079                 
1080                 for (RouteList::iterator i = public_order.begin(); i != public_order.end(); ++i) {
1081                         if (!(*i)->is_hidden()) {
1082                                 if (full_state) {
1083                                         child->add_child_nocopy ((*i)->get_state());
1084                                 } else {
1085                                         child->add_child_nocopy ((*i)->get_template());
1086                                 }
1087                         }
1088                 }
1089         }
1090
1091         
1092         child = node->add_child ("EditGroups");
1093         for (list<RouteGroup *>::iterator i = edit_groups.begin(); i != edit_groups.end(); ++i) {
1094                 child->add_child_nocopy ((*i)->get_state());
1095         }
1096
1097         child = node->add_child ("MixGroups");
1098         for (list<RouteGroup *>::iterator i = mix_groups.begin(); i != mix_groups.end(); ++i) {
1099                 child->add_child_nocopy ((*i)->get_state());
1100         }
1101
1102         child = node->add_child ("Playlists");
1103         for (PlaylistList::iterator i = playlists.begin(); i != playlists.end(); ++i) {
1104                 if (!(*i)->hidden()) {
1105                         if (!(*i)->empty()) {
1106                                 if (full_state) {
1107                                         child->add_child_nocopy ((*i)->get_state());
1108                                 } else {
1109                                         child->add_child_nocopy ((*i)->get_template());
1110                                 }
1111                         }
1112                 }
1113         }
1114
1115         child = node->add_child ("UnusedPlaylists");
1116         for (PlaylistList::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
1117                 if (!(*i)->hidden()) {
1118                         if (!(*i)->empty()) {
1119                                 if (full_state) {
1120                                         child->add_child_nocopy ((*i)->get_state());
1121                                 } else {
1122                                         child->add_child_nocopy ((*i)->get_template());
1123                                 }
1124                         }
1125                 }
1126         }
1127         
1128         
1129         if (_click_io) {
1130                 child = node->add_child ("Click");
1131                 child->add_child_nocopy (_click_io->state (full_state));
1132         }
1133
1134         if (full_state) {
1135                 child = node->add_child ("NamedSelections");
1136                 for (NamedSelectionList::iterator i = named_selections.begin(); i != named_selections.end(); ++i) {
1137                         if (full_state) {
1138                                 child->add_child_nocopy ((*i)->get_state());
1139                         } 
1140                 }
1141         }
1142
1143         node->add_child_nocopy (_tempo_map->get_state());
1144
1145         node->add_child_nocopy (get_control_protocol_state());
1146
1147         if (_extra_xml) {
1148                 node->add_child_copy (*_extra_xml);
1149         }
1150
1151         return *node;
1152 }
1153
1154 XMLNode&
1155 Session::get_control_protocol_state ()
1156 {
1157         ControlProtocolManager& cpm (ControlProtocolManager::instance());
1158         return cpm.get_state();
1159 }
1160
1161 int
1162 Session::set_state (const XMLNode& node)
1163 {
1164         XMLNodeList nlist;
1165         XMLNode* child;
1166         const XMLProperty* prop;
1167         int ret = -1;
1168
1169         _state_of_the_state = StateOfTheState (_state_of_the_state|CannotSave);
1170         
1171         if (node.name() != X_("Session")){
1172                 fatal << _("programming error: Session: incorrect XML node sent to set_state()") << endmsg;
1173                 return -1;
1174         }
1175
1176         if ((prop = node.property ("name")) != 0) {
1177                 _name = prop->value ();
1178         }
1179
1180         if ((prop = node.property (X_("sample-rate"))) != 0) {
1181
1182                 _nominal_frame_rate = atoi (prop->value());
1183
1184                 if (_nominal_frame_rate != _current_frame_rate) {
1185                         if (AskAboutSampleRateMismatch (_nominal_frame_rate, _current_frame_rate)) {
1186                                 return -1;
1187                         }
1188                 }
1189         }
1190
1191         setup_raid_path(_session_dir->root_path().to_string());
1192
1193         if ((prop = node.property (X_("id-counter"))) != 0) {
1194                 uint64_t x;
1195                 sscanf (prop->value().c_str(), "%" PRIu64, &x);
1196                 ID::init_counter (x);
1197         } else {
1198                 /* old sessions used a timebased counter, so fake
1199                    the startup ID counter based on a standard
1200                    timestamp.
1201                 */
1202                 time_t now;
1203                 time (&now);
1204                 ID::init_counter (now);
1205         }
1206
1207         
1208         IO::disable_ports ();
1209         IO::disable_connecting ();
1210
1211         /* Object loading order:
1212
1213         Path
1214         extra
1215         Options/Config
1216         MIDI Control // relies on data from Options/Config
1217         Metadata
1218         Locations
1219         Sources
1220         AudioRegions
1221         AudioDiskstreams
1222         Connections
1223         Routes
1224         EditGroups
1225         MixGroups
1226         Click
1227         ControlProtocols
1228         */
1229
1230         if ((child = find_named_node (node, "extra")) != 0) {
1231                 _extra_xml = new XMLNode (*child);
1232         }
1233
1234         if (((child = find_named_node (node, "Options")) != 0)) { /* old style */
1235                 load_options (*child);
1236         } else if ((child = find_named_node (node, "Config")) != 0) { /* new style */
1237                 load_options (*child);
1238         } else {
1239                 error << _("Session: XML state has no options section") << endmsg;
1240         }
1241
1242         if (use_config_midi_ports ()) {
1243         }
1244
1245         if ((child = find_named_node (node, "Metadata")) == 0) {
1246                 warning << _("Session: XML state has no metadata section (2.0 session?)") << endmsg;
1247         } else if (_metadata->set_state (*child)) {
1248                 goto out;
1249         }
1250
1251         if ((child = find_named_node (node, "Locations")) == 0) {
1252                 error << _("Session: XML state has no locations section") << endmsg;
1253                 goto out;
1254         } else if (_locations.set_state (*child)) {
1255                 goto out;
1256         }
1257
1258         Location* location;
1259
1260         if ((location = _locations.auto_loop_location()) != 0) {
1261                 set_auto_loop_location (location);
1262         }
1263
1264         if ((location = _locations.auto_punch_location()) != 0) {
1265                 set_auto_punch_location (location);
1266         }
1267
1268         if ((location = _locations.end_location()) == 0) {
1269                 _locations.add (end_location);
1270         } else {
1271                 delete end_location;
1272                 end_location = location;
1273         }
1274
1275         if ((location = _locations.start_location()) == 0) {
1276                 _locations.add (start_location);
1277         } else {
1278                 delete start_location;
1279                 start_location = location;
1280         }
1281
1282         AudioFileSource::set_header_position_offset (start_location->start());
1283
1284         if ((child = find_named_node (node, "Sources")) == 0) {
1285                 error << _("Session: XML state has no sources section") << endmsg;
1286                 goto out;
1287         } else if (load_sources (*child)) {
1288                 goto out;
1289         }
1290
1291         if ((child = find_named_node (node, "Regions")) == 0) {
1292                 error << _("Session: XML state has no Regions section") << endmsg;
1293                 goto out;
1294         } else if (load_regions (*child)) {
1295                 goto out;
1296         }
1297
1298         if ((child = find_named_node (node, "Playlists")) == 0) {
1299                 error << _("Session: XML state has no playlists section") << endmsg;
1300                 goto out;
1301         } else if (load_playlists (*child)) {
1302                 goto out;
1303         }
1304
1305         if ((child = find_named_node (node, "UnusedPlaylists")) == 0) {
1306                 // this is OK
1307         } else if (load_unused_playlists (*child)) {
1308                 goto out;
1309         }
1310         
1311         if ((child = find_named_node (node, "NamedSelections")) != 0) {
1312                 if (load_named_selections (*child)) {
1313                         goto out;
1314                 }
1315         }
1316
1317         if ((child = find_named_node (node, "DiskStreams")) == 0) {
1318                 error << _("Session: XML state has no diskstreams section") << endmsg;
1319                 goto out;
1320         } else if (load_diskstreams (*child)) {
1321                 goto out;
1322         }
1323
1324         if ((child = find_named_node (node, "Bundles")) == 0) {
1325                 warning << _("Session: XML state has no bundles section (2.0 session?)") << endmsg;
1326                 //goto out;
1327         } else {
1328                 /* We can't load Bundles yet as they need to be able
1329                    to convert from port names to Port objects, which can't happen until
1330                    later */
1331                 _bundle_xml_node = new XMLNode (*child);
1332         }
1333         
1334         if ((child = find_named_node (node, "EditGroups")) == 0) {
1335                 error << _("Session: XML state has no edit groups section") << endmsg;
1336                 goto out;
1337         } else if (load_edit_groups (*child)) {
1338                 goto out;
1339         }
1340
1341         if ((child = find_named_node (node, "MixGroups")) == 0) {
1342                 error << _("Session: XML state has no mix groups section") << endmsg;
1343                 goto out;
1344         } else if (load_mix_groups (*child)) {
1345                 goto out;
1346         }
1347
1348         if ((child = find_named_node (node, "TempoMap")) == 0) {
1349                 error << _("Session: XML state has no Tempo Map section") << endmsg;
1350                 goto out;
1351         } else if (_tempo_map->set_state (*child)) {
1352                 goto out;
1353         }
1354
1355         if ((child = find_named_node (node, "Routes")) == 0) {
1356                 error << _("Session: XML state has no routes section") << endmsg;
1357                 goto out;
1358         } else if (load_routes (*child)) {
1359                 goto out;
1360         }
1361
1362         if ((child = find_named_node (node, "Click")) == 0) {
1363                 warning << _("Session: XML state has no click section") << endmsg;
1364         } else if (_click_io) {
1365                 _click_io->set_state (*child);
1366         }
1367
1368         if ((child = find_named_node (node, "ControlProtocols")) != 0) {
1369                 ControlProtocolManager::instance().set_protocol_states (*child);
1370         }
1371
1372         /* here beginneth the second phase ... */
1373
1374         StateReady (); /* EMIT SIGNAL */
1375
1376         return 0;
1377
1378   out:
1379         return ret;
1380 }
1381
1382 int
1383 Session::load_routes (const XMLNode& node)
1384 {
1385         XMLNodeList nlist;
1386         XMLNodeConstIterator niter;
1387         RouteList new_routes;
1388
1389         nlist = node.children();
1390
1391         set_dirty();
1392
1393         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1394
1395                 boost::shared_ptr<Route> route (XMLRouteFactory (**niter));
1396
1397                 if (route == 0) {
1398                         error << _("Session: cannot create Route from XML description.")                              << endmsg;
1399                         return -1;
1400                 }
1401
1402                 BootMessage (string_compose (_("Loaded track/bus %1"), route->name()));
1403
1404                 new_routes.push_back (route);
1405         }
1406
1407         add_routes (new_routes, false);
1408
1409         return 0;
1410 }
1411
1412 boost::shared_ptr<Route>
1413 Session::XMLRouteFactory (const XMLNode& node)
1414 {
1415         if (node.name() != "Route") {
1416                 return boost::shared_ptr<Route> ((Route*) 0);
1417         }
1418
1419         bool has_diskstream = (node.property ("diskstream") != 0 || node.property ("diskstream-id") != 0);
1420         
1421         DataType type = DataType::AUDIO;
1422         const XMLProperty* prop = node.property("default-type");
1423         if (prop)
1424                 type = DataType(prop->value());
1425         
1426         assert(type != DataType::NIL);
1427
1428         if (has_diskstream) {
1429                 if (type == DataType::AUDIO) {
1430                         boost::shared_ptr<Route> ret (new AudioTrack (*this, node));
1431                         return ret;
1432                 } else {
1433                         boost::shared_ptr<Route> ret (new MidiTrack (*this, node));
1434                         return ret;
1435                 }
1436         } else {
1437                 boost::shared_ptr<Route> ret (new Route (*this, node));
1438                 return ret;
1439         }
1440 }
1441
1442 int
1443 Session::load_regions (const XMLNode& node)
1444 {
1445         XMLNodeList nlist;
1446         XMLNodeConstIterator niter;
1447         boost::shared_ptr<Region> region;
1448
1449         nlist = node.children();
1450
1451         set_dirty();
1452
1453         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1454                 if ((region = XMLRegionFactory (**niter, false)) == 0) {
1455                         error << _("Session: cannot create Region from XML description.");
1456                         const XMLProperty *name = (**niter).property("name");
1457
1458                         if (name) {
1459                                 error << " " << string_compose (_("Can not load state for region '%1'"), name->value());
1460                         }
1461
1462                         error << endmsg;
1463                 }
1464         }
1465
1466         return 0;
1467 }
1468
1469 boost::shared_ptr<Region>
1470 Session::XMLRegionFactory (const XMLNode& node, bool full)
1471 {
1472         const XMLProperty* type = node.property("type");
1473
1474         try {
1475         
1476         if ( !type || type->value() == "audio" ) {
1477
1478                 return boost::shared_ptr<Region>(XMLAudioRegionFactory (node, full));
1479         
1480         } else if (type->value() == "midi") {
1481                 
1482                 return boost::shared_ptr<Region>(XMLMidiRegionFactory (node, full));
1483
1484         }
1485         
1486         } catch (failed_constructor& err) {
1487                 return boost::shared_ptr<Region> ();
1488         }
1489
1490         return boost::shared_ptr<Region> ();
1491 }
1492
1493 boost::shared_ptr<AudioRegion>
1494 Session::XMLAudioRegionFactory (const XMLNode& node, bool full)
1495 {
1496         const XMLProperty* prop;
1497         boost::shared_ptr<Source> source;
1498         boost::shared_ptr<AudioSource> as;
1499         SourceList sources;
1500         SourceList master_sources;
1501         uint32_t nchans = 1;
1502         char buf[128];
1503         
1504         if (node.name() != X_("Region")) {
1505                 return boost::shared_ptr<AudioRegion>();
1506         }
1507
1508         if ((prop = node.property (X_("channels"))) != 0) {
1509                 nchans = atoi (prop->value().c_str());
1510         }
1511
1512         if ((prop = node.property ("name")) == 0) {
1513                 cerr << "no name for this region\n";
1514                 abort ();
1515         }
1516         
1517         if ((prop = node.property (X_("source-0"))) == 0) {
1518                 if ((prop = node.property ("source")) == 0) {
1519                         error << _("Session: XMLNode describing a AudioRegion is incomplete (no source)") << endmsg;
1520                         return boost::shared_ptr<AudioRegion>();
1521                 }
1522         }
1523
1524         PBD::ID s_id (prop->value());
1525
1526         if ((source = source_by_id (s_id)) == 0) {
1527                 error << string_compose(_("Session: XMLNode describing a AudioRegion references an unknown source id =%1"), s_id) << endmsg;
1528                 return boost::shared_ptr<AudioRegion>();
1529         }
1530         
1531         as = boost::dynamic_pointer_cast<AudioSource>(source);
1532         if (!as) {
1533                 error << string_compose(_("Session: XMLNode describing a AudioRegion references a non-audio source id =%1"), s_id) << endmsg;
1534                 return boost::shared_ptr<AudioRegion>();
1535         }
1536
1537         sources.push_back (as);
1538
1539         /* pickup other channels */
1540
1541         for (uint32_t n=1; n < nchans; ++n) {
1542                 snprintf (buf, sizeof(buf), X_("source-%d"), n);
1543                 if ((prop = node.property (buf)) != 0) {
1544                         
1545                         PBD::ID id2 (prop->value());
1546                         
1547                         if ((source = source_by_id (id2)) == 0) {
1548                                 error << string_compose(_("Session: XMLNode describing a AudioRegion references an unknown source id =%1"), id2) << endmsg;
1549                                 return boost::shared_ptr<AudioRegion>();
1550                         }
1551                         
1552                         as = boost::dynamic_pointer_cast<AudioSource>(source);
1553                         if (!as) {
1554                                 error << string_compose(_("Session: XMLNode describing a AudioRegion references a non-audio source id =%1"), id2) << endmsg;
1555                                 return boost::shared_ptr<AudioRegion>();
1556                         }
1557                         sources.push_back (as);
1558                 }
1559         }
1560
1561         for (uint32_t n=1; n < nchans; ++n) {
1562                 snprintf (buf, sizeof(buf), X_("master-source-%d"), n);
1563                 if ((prop = node.property (buf)) != 0) {
1564                         
1565                         PBD::ID id2 (prop->value());
1566                         
1567                         if ((source = source_by_id (id2)) == 0) {
1568                                 error << string_compose(_("Session: XMLNode describing a AudioRegion references an unknown source id =%1"), id2) << endmsg;
1569                                 return boost::shared_ptr<AudioRegion>();
1570                         }
1571                         
1572                         as = boost::dynamic_pointer_cast<AudioSource>(source);
1573                         if (!as) {
1574                                 error << string_compose(_("Session: XMLNode describing a AudioRegion references a non-audio source id =%1"), id2) << endmsg;
1575                                 return boost::shared_ptr<AudioRegion>();
1576                         }
1577                         master_sources.push_back (as);
1578                 }
1579         }
1580
1581         try {
1582                 boost::shared_ptr<AudioRegion> region (boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (sources, node)));
1583
1584                 /* a final detail: this is the one and only place that we know how long missing files are */
1585
1586                 if (region->whole_file()) {
1587                         for (SourceList::iterator sx = sources.begin(); sx != sources.end(); ++sx) {
1588                                 boost::shared_ptr<SilentFileSource> sfp = boost::dynamic_pointer_cast<SilentFileSource> (*sx);
1589                                 if (sfp) {
1590                                         sfp->set_length (region->length());
1591                                 }
1592                         }
1593                 }
1594
1595                 if (!master_sources.empty()) {
1596                         if (master_sources.size() == nchans) {
1597                                 error << _("Session: XMLNode describing an AudioRegion is missing some master sources; ignored") << endmsg;
1598                         } else {
1599                                 region->set_master_sources (master_sources);
1600                         }
1601                 }
1602
1603                 return region;
1604                                                        
1605         }
1606
1607         catch (failed_constructor& err) {
1608                 return boost::shared_ptr<AudioRegion>();
1609         }
1610 }
1611
1612 boost::shared_ptr<MidiRegion>
1613 Session::XMLMidiRegionFactory (const XMLNode& node, bool full)
1614 {
1615         const XMLProperty* prop;
1616         boost::shared_ptr<Source> source;
1617         boost::shared_ptr<MidiSource> ms;
1618         SourceList sources;
1619         uint32_t nchans = 1;
1620         
1621         if (node.name() != X_("Region")) {
1622                 return boost::shared_ptr<MidiRegion>();
1623         }
1624
1625         if ((prop = node.property (X_("channels"))) != 0) {
1626                 nchans = atoi (prop->value().c_str());
1627         }
1628         
1629         if ((prop = node.property ("name")) == 0) {
1630                 cerr << "no name for this region\n";
1631                 abort ();
1632         }
1633
1634         // Multiple midi channels?  that's just crazy talk
1635         assert(nchans == 1);
1636
1637         if ((prop = node.property (X_("source-0"))) == 0) {
1638                 if ((prop = node.property ("source")) == 0) {
1639                         error << _("Session: XMLNode describing a MidiRegion is incomplete (no source)") << endmsg;
1640                         return boost::shared_ptr<MidiRegion>();
1641                 }
1642         }
1643
1644         PBD::ID s_id (prop->value());
1645
1646         if ((source = source_by_id (s_id)) == 0) {
1647                 error << string_compose(_("Session: XMLNode describing a MidiRegion references an unknown source id =%1"), s_id) << endmsg;
1648                 return boost::shared_ptr<MidiRegion>();
1649         }
1650
1651         ms = boost::dynamic_pointer_cast<MidiSource>(source);
1652         if (!ms) {
1653                 error << string_compose(_("Session: XMLNode describing a MidiRegion references a non-midi source id =%1"), s_id) << endmsg;
1654                 return boost::shared_ptr<MidiRegion>();
1655         }
1656
1657         sources.push_back (ms);
1658
1659         try {
1660                 boost::shared_ptr<MidiRegion> region (boost::dynamic_pointer_cast<MidiRegion> (RegionFactory::create (sources, node)));
1661                 /* a final detail: this is the one and only place that we know how long missing files are */
1662
1663                 if (region->whole_file()) {
1664                         for (SourceList::iterator sx = sources.begin(); sx != sources.end(); ++sx) {
1665                                 boost::shared_ptr<SilentFileSource> sfp = boost::dynamic_pointer_cast<SilentFileSource> (*sx);
1666                                 if (sfp) {
1667                                         sfp->set_length (region->length());
1668                                 }
1669                         }
1670                 }
1671
1672                 return region;
1673         }
1674
1675         catch (failed_constructor& err) {
1676                 return boost::shared_ptr<MidiRegion>();
1677         }
1678 }
1679
1680 XMLNode&
1681 Session::get_sources_as_xml ()
1682
1683 {
1684         XMLNode* node = new XMLNode (X_("Sources"));
1685         Glib::Mutex::Lock lm (source_lock);
1686
1687         for (SourceMap::iterator i = sources.begin(); i != sources.end(); ++i) {
1688                 node->add_child_nocopy (i->second->get_state());
1689         }
1690
1691         return *node;
1692 }
1693
1694 string
1695 Session::path_from_region_name (DataType type, string name, string identifier)
1696 {
1697         char buf[PATH_MAX+1];
1698         uint32_t n;
1699         SessionDirectory sdir(get_best_session_directory_for_new_source());
1700         sys::path source_dir = ((type == DataType::AUDIO)
1701                 ? sdir.sound_path() : sdir.midi_path());
1702
1703         string ext = ((type == DataType::AUDIO) ? ".wav" : ".mid");
1704
1705         for (n = 0; n < 999999; ++n) {
1706                 if (identifier.length()) {
1707                         snprintf (buf, sizeof(buf), "%s%s%" PRIu32 "%s", name.c_str(), 
1708                                   identifier.c_str(), n, ext.c_str());
1709                 } else {
1710                         snprintf (buf, sizeof(buf), "%s-%" PRIu32 "%s", name.c_str(),
1711                                         n, ext.c_str());
1712                 }
1713
1714                 sys::path source_path = source_dir / buf;
1715
1716                 if (!sys::exists (source_path)) {
1717                         return source_path.to_string();
1718                 }
1719         }
1720
1721         error << string_compose (_("cannot create new file from region name \"%1\" with ident = \"%2\": too many existing files with similar names"),
1722                                  name, identifier)
1723               << endmsg;
1724
1725         return "";
1726 }
1727         
1728
1729 int
1730 Session::load_sources (const XMLNode& node)
1731 {
1732         XMLNodeList nlist;
1733         XMLNodeConstIterator niter;
1734         boost::shared_ptr<Source> source;
1735
1736         nlist = node.children();
1737
1738         set_dirty();
1739
1740         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1741
1742                 try {
1743                         if ((source = XMLSourceFactory (**niter)) == 0) {
1744                                 error << _("Session: cannot create Source from XML description.") << endmsg;
1745                         }
1746                 }
1747
1748                 catch (non_existent_source& err) {
1749                         warning << _("A sound file is missing. It will be replaced by silence.") << endmsg;
1750                         source = SourceFactory::createSilent (*this, **niter, max_frames, _current_frame_rate);
1751                 }
1752         }
1753
1754         return 0;
1755 }
1756
1757 boost::shared_ptr<Source>
1758 Session::XMLSourceFactory (const XMLNode& node)
1759 {
1760         if (node.name() != "Source") {
1761                 return boost::shared_ptr<Source>();
1762         }
1763
1764         try {
1765                 /* note: do peak building in another thread when loading session state */
1766                 return SourceFactory::create (*this, node, true);
1767         }
1768
1769         catch (failed_constructor& err) {
1770                 error << _("Found a sound file that cannot be used by Ardour. Talk to the progammers.") << endmsg;
1771                 return boost::shared_ptr<Source>();
1772         }
1773 }
1774
1775 int
1776 Session::save_template (string template_name)
1777 {
1778         XMLTree tree;
1779
1780         if (_state_of_the_state & CannotSave) {
1781                 return -1;
1782         }
1783
1784         sys::path user_template_dir(user_template_directory());
1785
1786         try
1787         {
1788                 sys::create_directories (user_template_dir);
1789         }
1790         catch(sys::filesystem_error& ex)
1791         {
1792                 error << string_compose(_("Could not create mix templates directory \"%1\" (%2)"),
1793                                 user_template_dir.to_string(), ex.what()) << endmsg;
1794                 return -1;
1795         }
1796
1797         tree.set_root (&get_template());
1798
1799         sys::path template_file_path(user_template_dir);
1800         template_file_path /= template_name + template_suffix;
1801
1802         if (sys::exists (template_file_path))
1803         {
1804                 warning << string_compose(_("Template \"%1\" already exists - new version not created"),
1805                                 template_file_path.to_string()) << endmsg;
1806                 return -1;
1807         }
1808
1809         if (!tree.write (template_file_path.to_string())) {
1810                 error << _("mix template not saved") << endmsg;
1811                 return -1;
1812         }
1813
1814         return 0;
1815 }
1816
1817 int
1818 Session::rename_template (string old_name, string new_name) 
1819 {
1820         sys::path old_path (user_template_directory());
1821         old_path /= old_name + template_suffix;
1822
1823         sys::path new_path(user_template_directory());
1824         new_path /= new_name + template_suffix;
1825
1826         if (sys::exists (new_path)) {
1827                 warning << string_compose(_("Template \"%1\" already exists - template not renamed"),
1828                                           new_path.to_string()) << endmsg;
1829                 return -1;
1830         }
1831
1832         try {
1833                 sys::rename (old_path, new_path);
1834                 return 0;
1835         } catch (...) {
1836                 return -1;
1837         }
1838 }
1839
1840 int
1841 Session::delete_template (string name) 
1842 {
1843         sys::path path = user_template_directory();
1844         path /= name + template_suffix;
1845
1846         try {
1847                 sys::remove (path);
1848                 return 0;
1849         } catch (...) {
1850                 return -1;
1851         }
1852 }
1853
1854 void
1855 Session::refresh_disk_space ()
1856 {
1857 #if HAVE_SYS_VFS_H
1858         struct statfs statfsbuf;
1859         vector<space_and_path>::iterator i;
1860         Glib::Mutex::Lock lm (space_lock);
1861         double scale;
1862
1863         /* get freespace on every FS that is part of the session path */
1864
1865         _total_free_4k_blocks = 0;
1866         
1867         for (i = session_dirs.begin(); i != session_dirs.end(); ++i) {
1868                 statfs ((*i).path.c_str(), &statfsbuf);
1869
1870                 scale = statfsbuf.f_bsize/4096.0;
1871
1872                 (*i).blocks = (uint32_t) floor (statfsbuf.f_bavail * scale);
1873                 _total_free_4k_blocks += (*i).blocks;
1874         }
1875 #endif
1876 }
1877
1878 string
1879 Session::get_best_session_directory_for_new_source ()
1880 {
1881         vector<space_and_path>::iterator i;
1882         string result = _session_dir->root_path().to_string();
1883
1884         /* handle common case without system calls */
1885
1886         if (session_dirs.size() == 1) {
1887                 return result;
1888         }
1889
1890         /* OK, here's the algorithm we're following here:
1891            
1892         We want to select which directory to use for 
1893         the next file source to be created. Ideally,
1894         we'd like to use a round-robin process so as to
1895         get maximum performance benefits from splitting
1896         the files across multiple disks.
1897
1898         However, in situations without much diskspace, an
1899         RR approach may end up filling up a filesystem
1900         with new files while others still have space.
1901         Its therefore important to pay some attention to
1902         the freespace in the filesystem holding each
1903         directory as well. However, if we did that by
1904         itself, we'd keep creating new files in the file
1905         system with the most space until it was as full
1906         as all others, thus negating any performance
1907         benefits of this RAID-1 like approach.
1908
1909         So, we use a user-configurable space threshold. If
1910         there are at least 2 filesystems with more than this
1911         much space available, we use RR selection between them. 
1912         If not, then we pick the filesystem with the most space.
1913
1914         This gets a good balance between the two
1915         approaches.  
1916         */
1917         
1918         refresh_disk_space ();
1919         
1920         int free_enough = 0;
1921
1922         for (i = session_dirs.begin(); i != session_dirs.end(); ++i) {
1923                 if ((*i).blocks * 4096 >= Config->get_disk_choice_space_threshold()) {
1924                         free_enough++;
1925                 }
1926         }
1927
1928         if (free_enough >= 2) {
1929                 /* use RR selection process, ensuring that the one
1930                    picked works OK.
1931                 */
1932
1933                 i = last_rr_session_dir;
1934
1935                 do {
1936                         if (++i == session_dirs.end()) {
1937                                 i = session_dirs.begin();
1938                         }
1939
1940                         if ((*i).blocks * 4096 >= Config->get_disk_choice_space_threshold()) {
1941                                 if (create_session_directory ((*i).path)) {
1942                                         result = (*i).path;
1943                                         last_rr_session_dir = i;
1944                                         return result;
1945                                 }
1946                         }
1947
1948                 } while (i != last_rr_session_dir);
1949
1950         } else {
1951
1952                 /* pick FS with the most freespace (and that
1953                    seems to actually work ...)
1954                 */
1955                 
1956                 vector<space_and_path> sorted;
1957                 space_and_path_ascending_cmp cmp;
1958
1959                 sorted = session_dirs;
1960                 sort (sorted.begin(), sorted.end(), cmp);
1961                 
1962                 for (i = sorted.begin(); i != sorted.end(); ++i) {
1963                         if (create_session_directory ((*i).path)) {
1964                                 result = (*i).path;
1965                                 last_rr_session_dir = i;
1966                                 return result;
1967                         }
1968                 }
1969         }
1970
1971         return result;
1972 }
1973
1974 int
1975 Session::load_playlists (const XMLNode& node)
1976 {
1977         XMLNodeList nlist;
1978         XMLNodeConstIterator niter;
1979         boost::shared_ptr<Playlist> playlist;
1980
1981         nlist = node.children();
1982
1983         set_dirty();
1984
1985         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1986                 
1987                 if ((playlist = XMLPlaylistFactory (**niter)) == 0) {
1988                         error << _("Session: cannot create Playlist from XML description.") << endmsg;
1989                 }
1990         }
1991
1992         return 0;
1993 }
1994
1995 int
1996 Session::load_unused_playlists (const XMLNode& node)
1997 {
1998         XMLNodeList nlist;
1999         XMLNodeConstIterator niter;
2000         boost::shared_ptr<Playlist> playlist;
2001
2002         nlist = node.children();
2003
2004         set_dirty();
2005
2006         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2007                 
2008                 if ((playlist = XMLPlaylistFactory (**niter)) == 0) {
2009                         error << _("Session: cannot create Playlist from XML description.") << endmsg;
2010                         continue;
2011                 }
2012
2013                 // now manually untrack it
2014
2015                 track_playlist (false, boost::weak_ptr<Playlist> (playlist));
2016         }
2017
2018         return 0;
2019 }
2020
2021 boost::shared_ptr<Playlist>
2022 Session::XMLPlaylistFactory (const XMLNode& node)
2023 {
2024         try {
2025                 return PlaylistFactory::create (*this, node);
2026         }
2027
2028         catch (failed_constructor& err) {
2029                 return boost::shared_ptr<Playlist>();
2030         }
2031 }
2032
2033 int
2034 Session::load_named_selections (const XMLNode& node)
2035 {
2036         XMLNodeList nlist;
2037         XMLNodeConstIterator niter;
2038         NamedSelection *ns;
2039
2040         nlist = node.children();
2041
2042         set_dirty();
2043
2044         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2045                 
2046                 if ((ns = XMLNamedSelectionFactory (**niter)) == 0) {
2047                         error << _("Session: cannot create Named Selection from XML description.") << endmsg;
2048                 }
2049         }
2050
2051         return 0;
2052 }
2053
2054 NamedSelection *
2055 Session::XMLNamedSelectionFactory (const XMLNode& node)
2056 {
2057         try {
2058                 return new NamedSelection (*this, node);
2059         }
2060
2061         catch (failed_constructor& err) {
2062                 return 0;
2063         }
2064 }
2065
2066 string
2067 Session::automation_dir () const
2068 {
2069         return Glib::build_filename (_path, "automation");
2070 }
2071
2072 string
2073 Session::analysis_dir () const
2074 {
2075         return Glib::build_filename (_path, "analysis");
2076 }
2077
2078 int
2079 Session::load_bundles (XMLNode const & node)
2080 {
2081         XMLNodeList nlist = node.children();
2082         XMLNodeConstIterator niter;
2083
2084         set_dirty();
2085
2086         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2087                 if ((*niter)->name() == "InputBundle") {
2088                         add_bundle (boost::shared_ptr<UserBundle> (new UserBundle (**niter, true)));
2089                 } else if ((*niter)->name() == "OutputBundle") {
2090                         add_bundle (boost::shared_ptr<UserBundle> (new UserBundle (**niter, false)));
2091                 } else {
2092                         error << string_compose(_("Unknown node \"%1\" found in Bundles list from state file"), (*niter)->name()) << endmsg;
2093                         return -1;
2094                 }
2095         }
2096
2097         return 0;
2098 }                               
2099
2100 int
2101 Session::load_edit_groups (const XMLNode& node)
2102 {
2103         return load_route_groups (node, true);
2104 }
2105
2106 int
2107 Session::load_mix_groups (const XMLNode& node)
2108 {
2109         return load_route_groups (node, false);
2110 }
2111
2112 int
2113 Session::load_route_groups (const XMLNode& node, bool edit)
2114 {
2115         XMLNodeList nlist = node.children();
2116         XMLNodeConstIterator niter;
2117         RouteGroup* rg;
2118
2119         set_dirty();
2120
2121         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2122                 if ((*niter)->name() == "RouteGroup") {
2123                         if (edit) {
2124                                 rg = add_edit_group ("");
2125                                 rg->set_state (**niter);
2126                         } else {
2127                                 rg = add_mix_group ("");
2128                                 rg->set_state (**niter);
2129                         }
2130                 }
2131         }
2132         
2133         return 0;
2134 }                               
2135
2136 void
2137 Session::auto_save()
2138 {
2139         save_state (_current_snapshot_name);
2140 }
2141
2142 static bool
2143 state_file_filter (const string &str, void *arg)
2144 {
2145         return (str.length() > strlen(statefile_suffix) &&
2146                 str.find (statefile_suffix) == (str.length() - strlen (statefile_suffix)));
2147 }
2148
2149 struct string_cmp {
2150         bool operator()(const string* a, const string* b) {
2151                 return *a < *b;
2152         }
2153 };
2154
2155 static string*
2156 remove_end(string* state)
2157 {
2158         string statename(*state);
2159         
2160         string::size_type start,end;
2161         if ((start = statename.find_last_of ('/')) != string::npos) {
2162                 statename = statename.substr (start+1);
2163         }
2164                 
2165         if ((end = statename.rfind(".ardour")) == string::npos) {
2166                 end = statename.length();
2167         }
2168
2169         return new string(statename.substr (0, end));
2170 }
2171
2172 vector<string *> *
2173 Session::possible_states (string path) 
2174 {
2175         PathScanner scanner;
2176         vector<string*>* states = scanner (path, state_file_filter, 0, false, false);
2177         
2178         transform(states->begin(), states->end(), states->begin(), remove_end);
2179         
2180         string_cmp cmp;
2181         sort (states->begin(), states->end(), cmp);
2182         
2183         return states;
2184 }
2185
2186 vector<string *> *
2187 Session::possible_states () const
2188 {
2189         return possible_states(_path);
2190 }
2191
2192 RouteGroup *
2193 Session::add_edit_group (string name)
2194 {
2195         RouteGroup* rg = new RouteGroup (*this, name);
2196         edit_groups.push_back (rg);
2197         edit_group_added (rg); /* EMIT SIGNAL */
2198         set_dirty();
2199         return rg;
2200 }
2201
2202 RouteGroup *
2203 Session::add_mix_group (string name)
2204 {
2205         RouteGroup* rg = new RouteGroup (*this, name, RouteGroup::Relative);
2206         mix_groups.push_back (rg);
2207         mix_group_added (rg); /* EMIT SIGNAL */
2208         set_dirty();
2209         return rg;
2210 }
2211
2212 void
2213 Session::remove_edit_group (RouteGroup& rg)
2214 {
2215         list<RouteGroup*>::iterator i;
2216
2217         if ((i = find (edit_groups.begin(), edit_groups.end(), &rg)) != edit_groups.end()) {
2218                 (*i)->apply (&Route::drop_edit_group, this);
2219                 edit_groups.erase (i);
2220                 edit_group_removed (); /* EMIT SIGNAL */
2221         }
2222
2223         delete &rg;
2224 }
2225
2226 void
2227 Session::remove_mix_group (RouteGroup& rg)
2228 {
2229         list<RouteGroup*>::iterator i;
2230
2231         if ((i = find (mix_groups.begin(), mix_groups.end(), &rg)) != mix_groups.end()) {
2232                 (*i)->apply (&Route::drop_mix_group, this);
2233                 mix_groups.erase (i);
2234                 mix_group_removed (); /* EMIT SIGNAL */
2235         }
2236
2237         delete &rg;
2238 }
2239
2240 RouteGroup *
2241 Session::mix_group_by_name (string name)
2242 {
2243         list<RouteGroup *>::iterator i;
2244
2245         for (i = mix_groups.begin(); i != mix_groups.end(); ++i) {
2246                 if ((*i)->name() == name) {
2247                         return* i;
2248                 }
2249         }
2250         return 0;
2251 }
2252
2253 RouteGroup *
2254 Session::edit_group_by_name (string name)
2255 {
2256         list<RouteGroup *>::iterator i;
2257
2258         for (i = edit_groups.begin(); i != edit_groups.end(); ++i) {
2259                 if ((*i)->name() == name) {
2260                         return* i;
2261                 }
2262         }
2263         return 0;
2264 }
2265
2266 void
2267 Session::begin_reversible_command (const string& name)
2268 {
2269         current_trans = new UndoTransaction;
2270         current_trans->set_name (name);
2271 }
2272
2273 void
2274 Session::commit_reversible_command (Command *cmd)
2275 {
2276         struct timeval now;
2277
2278         if (cmd) {
2279                 current_trans->add_command (cmd);
2280         }
2281
2282         if (current_trans->empty()) {
2283                 return;
2284         }
2285
2286         gettimeofday (&now, 0);
2287         current_trans->set_timestamp (now);
2288
2289         _history.add (current_trans);
2290 }
2291
2292 Session::GlobalRouteBooleanState 
2293 Session::get_global_route_boolean (bool (Route::*method)(void) const)
2294 {
2295         GlobalRouteBooleanState s;
2296         boost::shared_ptr<RouteList> r = routes.reader ();
2297
2298         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
2299                 if (!(*i)->is_hidden()) {
2300                         RouteBooleanState v;
2301                         
2302                         v.first =* i;
2303                         Route* r = (*i).get();
2304                         v.second = (r->*method)();
2305                         
2306                         s.push_back (v);
2307                 }
2308         }
2309
2310         return s;
2311 }
2312
2313 Session::GlobalRouteMeterState
2314 Session::get_global_route_metering ()
2315 {
2316         GlobalRouteMeterState s;
2317         boost::shared_ptr<RouteList> r = routes.reader ();
2318
2319         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
2320                 if (!(*i)->is_hidden()) {
2321                         RouteMeterState v;
2322                         
2323                         v.first =* i;
2324                         v.second = (*i)->meter_point();
2325                         
2326                         s.push_back (v);
2327                 }
2328         }
2329
2330         return s;
2331 }
2332
2333 void
2334 Session::set_global_route_metering (GlobalRouteMeterState s, void* arg) 
2335 {
2336         for (GlobalRouteMeterState::iterator i = s.begin(); i != s.end(); ++i) {
2337
2338                 boost::shared_ptr<Route> r = (i->first.lock());
2339
2340                 if (r) {
2341                         r->set_meter_point (i->second, arg);
2342                 }
2343         }
2344 }
2345
2346 void
2347 Session::set_global_route_boolean (GlobalRouteBooleanState s, void (Route::*method)(bool, void*), void* arg)
2348 {
2349         for (GlobalRouteBooleanState::iterator i = s.begin(); i != s.end(); ++i) {
2350
2351                 boost::shared_ptr<Route> r = (i->first.lock());
2352
2353                 if (r) {
2354                         Route* rp = r.get();
2355                         (rp->*method) (i->second, arg);
2356                 }
2357         }
2358 }
2359
2360 void
2361 Session::set_global_mute (GlobalRouteBooleanState s, void* src)
2362 {
2363         set_global_route_boolean (s, &Route::set_mute, src);
2364 }
2365
2366 void
2367 Session::set_global_solo (GlobalRouteBooleanState s, void* src)
2368 {
2369         set_global_route_boolean (s, &Route::set_solo, src);
2370 }
2371
2372 void
2373 Session::set_global_record_enable (GlobalRouteBooleanState s, void* src)
2374 {
2375         set_global_route_boolean (s, &Route::set_record_enable, src);
2376 }
2377
2378 #if 0
2379 UndoAction
2380 Session::global_mute_memento (void* src)
2381 {
2382         return sigc::bind (mem_fun (*this, &Session::set_global_mute), get_global_route_boolean (&Route::muted), src);
2383 }
2384
2385 UndoAction
2386 Session::global_metering_memento (void* src)
2387 {
2388         return sigc::bind (mem_fun (*this, &Session::set_global_route_metering), get_global_route_metering (), src);
2389 }
2390
2391 UndoAction
2392 Session::global_solo_memento (void* src)
2393 {
2394         return sigc::bind (mem_fun (*this, &Session::set_global_solo), get_global_route_boolean (&Route::soloed), src);
2395 }
2396
2397 UndoAction
2398 Session::global_record_enable_memento (void* src)
2399 {
2400         return sigc::bind (mem_fun (*this, &Session::set_global_record_enable), get_global_route_boolean (&Route::record_enabled), src);
2401 }
2402 #endif
2403
2404 static bool
2405 accept_all_non_peak_files (const string& path, void *arg)
2406 {
2407         return (path.length() > 5 && path.find (peakfile_suffix) != (path.length() - 5));
2408 }
2409
2410 static bool
2411 accept_all_state_files (const string& path, void *arg)
2412 {
2413         return (path.length() > 7 && path.find (".ardour") == (path.length() - 7));
2414 }
2415
2416 int 
2417 Session::find_all_sources (string path, set<string>& result)
2418 {
2419         XMLTree tree;
2420         XMLNode* node;
2421
2422         if (!tree.read (path)) {
2423                 return -1;
2424         }
2425
2426         if ((node = find_named_node (*tree.root(), "Sources")) == 0) {
2427                 return -2;
2428         }
2429
2430         XMLNodeList nlist;
2431         XMLNodeConstIterator niter;
2432
2433         nlist = node->children();
2434
2435         set_dirty();
2436
2437         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2438                 
2439                 XMLProperty* prop;
2440
2441                 if ((prop = (*niter)->property (X_("name"))) == 0) {
2442                         continue;
2443                 }
2444
2445                 if (prop->value()[0] == '/') {
2446                         /* external file, ignore */
2447                         continue;
2448                 }
2449
2450                 sys::path source_path = _session_dir->sound_path ();
2451
2452                 source_path /= prop->value ();
2453
2454                 result.insert (source_path.to_string ());
2455         }
2456
2457         return 0;
2458 }
2459
2460 int
2461 Session::find_all_sources_across_snapshots (set<string>& result, bool exclude_this_snapshot)
2462 {
2463         PathScanner scanner;
2464         vector<string*>* state_files;
2465         string ripped;
2466         string this_snapshot_path;
2467
2468         result.clear ();
2469
2470         ripped = _path;
2471
2472         if (ripped[ripped.length()-1] == '/') {
2473                 ripped = ripped.substr (0, ripped.length() - 1);
2474         }
2475
2476         state_files = scanner (ripped, accept_all_state_files, (void *) 0, false, true);
2477         
2478         if (state_files == 0) {
2479                 /* impossible! */
2480                 return 0;
2481         }
2482
2483         this_snapshot_path = _path;
2484         this_snapshot_path += _current_snapshot_name;
2485         this_snapshot_path += statefile_suffix;
2486
2487         for (vector<string*>::iterator i = state_files->begin(); i != state_files->end(); ++i) {
2488
2489                 if (exclude_this_snapshot && **i == this_snapshot_path) {
2490                         continue;
2491                 }
2492
2493                 if (find_all_sources (**i, result) < 0) {
2494                         return -1;
2495                 }
2496         }
2497
2498         return 0;
2499 }
2500
2501 struct RegionCounter {
2502     typedef std::map<PBD::ID,boost::shared_ptr<AudioSource> > AudioSourceList;
2503     AudioSourceList::iterator iter;
2504     boost::shared_ptr<Region> region;
2505     uint32_t count;
2506     
2507     RegionCounter() : count (0) {}
2508 };
2509
2510 int
2511 Session::cleanup_sources (Session::cleanup_report& rep)
2512 {
2513         // FIXME: needs adaptation to midi
2514         
2515         vector<boost::shared_ptr<Source> > dead_sources;
2516         vector<boost::shared_ptr<Playlist> > playlists_tbd;
2517         PathScanner scanner;
2518         string sound_path;
2519         vector<space_and_path>::iterator i;
2520         vector<space_and_path>::iterator nexti;
2521         vector<string*>* soundfiles;
2522         vector<string> unused;
2523         set<string> all_sources;
2524         bool used;
2525         string spath;
2526         int ret = -1;
2527                 
2528         _state_of_the_state = (StateOfTheState) (_state_of_the_state | InCleanup);
2529
2530         
2531         /* step 1: consider deleting all unused playlists */
2532
2533         for (PlaylistList::iterator x = unused_playlists.begin(); x != unused_playlists.end(); ++x) {
2534                 int status;
2535
2536                 status = AskAboutPlaylistDeletion (*x);
2537
2538                 switch (status) {
2539                 case -1:
2540                         ret = 0;
2541                         goto out;
2542                         break;
2543
2544                 case 0:
2545                         playlists_tbd.push_back (*x);
2546                         break;
2547
2548                 default:
2549                         /* leave it alone */
2550                         break;
2551                 }
2552         }
2553
2554         /* now delete any that were marked for deletion */
2555
2556         for (vector<boost::shared_ptr<Playlist> >::iterator x = playlists_tbd.begin(); x != playlists_tbd.end(); ++x) {
2557                 (*x)->drop_references ();
2558         }
2559
2560         playlists_tbd.clear ();
2561
2562         /* step 2: find all un-used sources */
2563
2564         rep.paths.clear ();
2565         rep.space = 0;
2566
2567         for (SourceMap::iterator i = sources.begin(); i != sources.end(); ) {
2568                 
2569                 SourceMap::iterator tmp;
2570
2571                 tmp = i;
2572                 ++tmp;
2573
2574                 /* do not bother with files that are zero size, otherwise we remove the current "nascent"
2575                    capture files.
2576                 */
2577
2578                 if (!i->second->used() && i->second->length() > 0) {
2579                         dead_sources.push_back (i->second);
2580                         i->second->GoingAway();
2581                 } 
2582
2583                 i = tmp;
2584         }
2585
2586         /* build a list of all the possible sound directories for the session */
2587
2588         for (i = session_dirs.begin(); i != session_dirs.end(); ) {
2589
2590                 nexti = i;
2591                 ++nexti;
2592
2593                 SessionDirectory sdir ((*i).path);
2594                 sound_path += sdir.sound_path().to_string();
2595
2596                 if (nexti != session_dirs.end()) {
2597                         sound_path += ':';
2598                 }
2599
2600                 i = nexti;
2601         }
2602
2603         /* now do the same thing for the files that ended up in the sounds dir(s) 
2604            but are not referenced as sources in any snapshot.
2605         */
2606
2607         soundfiles = scanner (sound_path, accept_all_non_peak_files, (void *) 0, false, true);
2608
2609         if (soundfiles == 0) {
2610                 return 0;
2611         }
2612
2613         /* find all sources, but don't use this snapshot because the
2614            state file on disk still references sources we may have already
2615            dropped.
2616         */
2617         
2618         find_all_sources_across_snapshots (all_sources, true);
2619
2620         /*  add our current source list
2621          */
2622         
2623         for (SourceMap::iterator i = sources.begin(); i != sources.end(); ++i) {
2624                 boost::shared_ptr<AudioFileSource> fs;
2625                 
2626                 if ((fs = boost::dynamic_pointer_cast<AudioFileSource> (i->second)) != 0) {
2627                         all_sources.insert (fs->path());
2628                 } 
2629         }
2630
2631         char tmppath1[PATH_MAX+1];
2632         char tmppath2[PATH_MAX+1];
2633         
2634         for (vector<string*>::iterator x = soundfiles->begin(); x != soundfiles->end(); ++x) {
2635
2636                 used = false;
2637                 spath = **x;
2638
2639                 for (set<string>::iterator i = all_sources.begin(); i != all_sources.end(); ++i) {
2640
2641                         realpath(spath.c_str(), tmppath1);
2642                         realpath((*i).c_str(),  tmppath2);
2643
2644                         if (strcmp(tmppath1, tmppath2) == 0) {
2645                                 used = true;
2646                                 break;
2647                         }
2648                 }
2649
2650                 if (!used) {
2651                         unused.push_back (spath);
2652                 }
2653         }
2654
2655         /* now try to move all unused files into the "dead_sounds" directory(ies) */
2656
2657         for (vector<string>::iterator x = unused.begin(); x != unused.end(); ++x) {
2658                 struct stat statbuf;
2659
2660                 rep.paths.push_back (*x);
2661                 if (stat ((*x).c_str(), &statbuf) == 0) {
2662                         rep.space += statbuf.st_size;
2663                 }
2664
2665                 string newpath;
2666                 
2667                 /* don't move the file across filesystems, just
2668                    stick it in the `dead_sound_dir_name' directory
2669                    on whichever filesystem it was already on.
2670                 */
2671
2672                 if ((*x).find ("/sounds/") != string::npos) {
2673
2674                         /* old school, go up 1 level */
2675
2676                         newpath = Glib::path_get_dirname (*x);      // "sounds" 
2677                         newpath = Glib::path_get_dirname (newpath); // "session-name"
2678
2679                 } else {
2680
2681                         /* new school, go up 4 levels */
2682                         
2683                         newpath = Glib::path_get_dirname (*x);      // "audiofiles" 
2684                         newpath = Glib::path_get_dirname (newpath); // "session-name"
2685                         newpath = Glib::path_get_dirname (newpath); // "interchange"
2686                         newpath = Glib::path_get_dirname (newpath); // "session-dir"
2687                 }
2688
2689                 newpath += '/';
2690                 newpath += dead_sound_dir_name;
2691
2692                 if (g_mkdir_with_parents (newpath.c_str(), 0755) < 0) {
2693                         error << string_compose(_("Session: cannot create session peakfile folder \"%1\" (%2)"), newpath, strerror (errno)) << endmsg;
2694                         return -1;
2695                 }
2696
2697                 newpath += '/';
2698                 newpath += Glib::path_get_basename ((*x));
2699                 
2700                 if (access (newpath.c_str(), F_OK) == 0) {
2701                         
2702                         /* the new path already exists, try versioning */
2703                         
2704                         char buf[PATH_MAX+1];
2705                         int version = 1;
2706                         string newpath_v;
2707                         
2708                         snprintf (buf, sizeof (buf), "%s.%d", newpath.c_str(), version);
2709                         newpath_v = buf;
2710
2711                         while (access (newpath_v.c_str(), F_OK) == 0 && version < 999) {
2712                                 snprintf (buf, sizeof (buf), "%s.%d", newpath.c_str(), ++version);
2713                                 newpath_v = buf;
2714                         }
2715                         
2716                         if (version == 999) {
2717                                 error << string_compose (_("there are already 1000 files with names like %1; versioning discontinued"),
2718                                                   newpath)
2719                                       << endmsg;
2720                         } else {
2721                                 newpath = newpath_v;
2722                         }
2723                         
2724                 } else {
2725                         
2726                         /* it doesn't exist, or we can't read it or something */
2727                         
2728                 }
2729
2730                 if (::rename ((*x).c_str(), newpath.c_str()) != 0) {
2731                         error << string_compose (_("cannot rename audio file source from %1 to %2 (%3)"),
2732                                           (*x), newpath, strerror (errno))
2733                               << endmsg;
2734                         goto out;
2735                 }
2736
2737                 /* see if there an easy to find peakfile for this file, and remove it.
2738                  */
2739
2740                 string peakpath = (*x).substr (0, (*x).find_last_of ('.'));
2741                 peakpath += peakfile_suffix;
2742
2743                 if (access (peakpath.c_str(), W_OK) == 0) {
2744                         if (::unlink (peakpath.c_str()) != 0) {
2745                                 error << string_compose (_("cannot remove peakfile %1 for %2 (%3)"),
2746                                                   peakpath, _path, strerror (errno))
2747                                       << endmsg;
2748                                 /* try to back out */
2749                                 rename (newpath.c_str(), _path.c_str());
2750                                 goto out;
2751                         }
2752                 }
2753         }
2754
2755         ret = 0;
2756
2757         /* dump the history list */
2758
2759         _history.clear ();
2760
2761         /* save state so we don't end up a session file
2762            referring to non-existent sources.
2763         */
2764         
2765         save_state ("");
2766
2767   out:
2768         _state_of_the_state = (StateOfTheState) (_state_of_the_state & ~InCleanup);
2769
2770         return ret;
2771 }
2772
2773 int
2774 Session::cleanup_trash_sources (Session::cleanup_report& rep)
2775 {
2776         // FIXME: needs adaptation for MIDI
2777         
2778         vector<space_and_path>::iterator i;
2779         string dead_sound_dir;
2780         struct dirent* dentry;
2781         struct stat statbuf;
2782         DIR* dead;
2783
2784         rep.paths.clear ();
2785         rep.space = 0;
2786
2787         for (i = session_dirs.begin(); i != session_dirs.end(); ++i) {
2788                 
2789                 dead_sound_dir = (*i).path;
2790                 dead_sound_dir += dead_sound_dir_name;
2791
2792                 if ((dead = opendir (dead_sound_dir.c_str())) == 0) {
2793                         continue;
2794                 }
2795
2796                 while ((dentry = readdir (dead)) != 0) {
2797
2798                         /* avoid '.' and '..' */
2799                         
2800                         if ((dentry->d_name[0] == '.' && dentry->d_name[1] == '\0') || 
2801                             (dentry->d_name[2] == '\0' && dentry->d_name[0] == '.' && dentry->d_name[1] == '.')) {
2802                                 continue;
2803                         }
2804
2805                         string fullpath;
2806
2807                         fullpath = dead_sound_dir;
2808                         fullpath += '/';
2809                         fullpath += dentry->d_name;
2810
2811                         if (stat (fullpath.c_str(), &statbuf)) {
2812                                 continue;
2813                         }
2814
2815                         if (!S_ISREG (statbuf.st_mode)) {
2816                                 continue;
2817                         }
2818
2819                         if (unlink (fullpath.c_str())) {
2820                                 error << string_compose (_("cannot remove dead sound file %1 (%2)"),
2821                                                   fullpath, strerror (errno))
2822                                       << endmsg;
2823                         }
2824
2825                         rep.paths.push_back (dentry->d_name);
2826                         rep.space += statbuf.st_size;
2827                 }
2828
2829                 closedir (dead);
2830                 
2831         }
2832
2833         return 0;
2834 }
2835
2836 void
2837 Session::set_dirty ()
2838 {
2839         bool was_dirty = dirty();
2840
2841         _state_of_the_state = StateOfTheState (_state_of_the_state | Dirty);
2842
2843
2844         if (!was_dirty) {
2845                 DirtyChanged(); /* EMIT SIGNAL */
2846         }
2847 }
2848
2849
2850 void
2851 Session::set_clean ()
2852 {
2853         bool was_dirty = dirty();
2854         
2855         _state_of_the_state = Clean;
2856
2857
2858         if (was_dirty) {
2859                 DirtyChanged(); /* EMIT SIGNAL */
2860         }
2861 }
2862
2863 void
2864 Session::set_deletion_in_progress ()
2865 {
2866         _state_of_the_state = StateOfTheState (_state_of_the_state | Deletion);
2867
2868 }
2869
2870 void
2871 Session::add_controllable (boost::shared_ptr<Controllable> c)
2872 {
2873         /* this adds a controllable to the list managed by the Session.
2874            this is a subset of those managed by the Controllable class
2875            itself, and represents the only ones whose state will be saved
2876            as part of the session.
2877         */
2878
2879         Glib::Mutex::Lock lm (controllables_lock);
2880         controllables.insert (c);
2881 }
2882         
2883 struct null_deleter { void operator()(void const *) const {} };
2884
2885 void
2886 Session::remove_controllable (Controllable* c)
2887 {
2888         if (_state_of_the_state | Deletion) {
2889                 return;
2890         }
2891
2892         Glib::Mutex::Lock lm (controllables_lock);
2893
2894         Controllables::iterator x = controllables.find(
2895                  boost::shared_ptr<Controllable>(c, null_deleter()));
2896
2897         if (x != controllables.end()) {
2898                 controllables.erase (x);
2899         }
2900 }       
2901
2902 boost::shared_ptr<Controllable>
2903 Session::controllable_by_id (const PBD::ID& id)
2904 {
2905         Glib::Mutex::Lock lm (controllables_lock);
2906         
2907         for (Controllables::iterator i = controllables.begin(); i != controllables.end(); ++i) {
2908                 if ((*i)->id() == id) {
2909                         return *i;
2910                 }
2911         }
2912
2913         return boost::shared_ptr<Controllable>();
2914 }
2915
2916 void 
2917 Session::add_instant_xml (XMLNode& node, bool write_to_config)
2918 {
2919         Stateful::add_instant_xml (node, _path);
2920         if (write_to_config) {
2921                 Config->add_instant_xml (node);
2922         }
2923 }
2924
2925 XMLNode*
2926 Session::instant_xml (const string& node_name)
2927 {
2928         return Stateful::instant_xml (node_name, _path);
2929 }
2930
2931 int 
2932 Session::save_history (string snapshot_name)
2933 {
2934         XMLTree tree;
2935         
2936         if (snapshot_name.empty()) {
2937                 snapshot_name = _current_snapshot_name;
2938         }
2939   
2940         const string history_filename = snapshot_name + history_suffix;
2941         const string backup_filename = history_filename + backup_suffix;
2942         const sys::path xml_path = _session_dir->root_path() / history_filename;
2943         const sys::path backup_path = _session_dir->root_path() / backup_filename;
2944
2945         if (sys::exists (xml_path)) {
2946                 try
2947                 {
2948                         sys::rename (xml_path, backup_path);
2949                 }
2950                 catch (const sys::filesystem_error& err)
2951                 {
2952                         error << _("could not backup old history file, current history not saved") << endmsg;
2953                         return -1;
2954                 }
2955         }
2956
2957
2958         if (!Config->get_save_history() || Config->get_saved_history_depth() < 0) {
2959                 return 0;
2960         }
2961
2962         tree.set_root (&_history.get_state (Config->get_saved_history_depth()));
2963
2964         if (!tree.write (xml_path.to_string()))
2965         {
2966                 error << string_compose (_("history could not be saved to %1"), xml_path.to_string()) << endmsg;
2967
2968                 try
2969                 {
2970                         sys::remove (xml_path);
2971                         sys::rename (backup_path, xml_path);
2972                 }
2973                 catch (const sys::filesystem_error& err)
2974                 {
2975                         error << string_compose (_("could not restore history file from backup %1 (%2)"),
2976                                         backup_path.to_string(), err.what()) << endmsg;
2977                 }
2978
2979                 return -1;
2980         }
2981
2982         return 0;
2983 }
2984
2985 int
2986 Session::restore_history (string snapshot_name)
2987 {
2988         XMLTree tree;
2989
2990         if (snapshot_name.empty()) {
2991                 snapshot_name = _current_snapshot_name;
2992         }
2993
2994         const string xml_filename = snapshot_name + history_suffix;
2995         const sys::path xml_path = _session_dir->root_path() / xml_filename;
2996
2997     cerr << "Loading history from " << xml_path.to_string() << endmsg;
2998
2999         if (!sys::exists (xml_path)) {
3000                 info << string_compose (_("%1: no history file \"%2\" for this session."),
3001                                 _name, xml_path.to_string()) << endmsg;
3002                 return 1;
3003         }
3004
3005         if (!tree.read (xml_path.to_string())) {
3006                 error << string_compose (_("Could not understand session history file \"%1\""),
3007                                 xml_path.to_string()) << endmsg;
3008                 return -1;
3009         }
3010
3011         // replace history
3012         _history.clear();
3013
3014     for (XMLNodeConstIterator it  = tree.root()->children().begin(); it != tree.root()->children().end(); it++) {
3015             
3016             XMLNode *t = *it;
3017             UndoTransaction* ut = new UndoTransaction ();
3018             struct timeval tv;
3019             
3020             ut->set_name(t->property("name")->value());
3021             stringstream ss(t->property("tv-sec")->value());
3022             ss >> tv.tv_sec;
3023             ss.str(t->property("tv-usec")->value());
3024             ss >> tv.tv_usec;
3025             ut->set_timestamp(tv);
3026             
3027             for (XMLNodeConstIterator child_it  = t->children().begin();
3028                                 child_it != t->children().end(); child_it++)
3029             {
3030                     XMLNode *n = *child_it;
3031                     Command *c;
3032         
3033                     if (n->name() == "MementoCommand" ||
3034                                         n->name() == "MementoUndoCommand" ||
3035                                         n->name() == "MementoRedoCommand") {
3036
3037                             if ((c = memento_command_factory(n))) {
3038                                     ut->add_command(c);
3039                             }
3040                             
3041                     } else if (n->name() == X_("GlobalRouteStateCommand")) {
3042
3043                             if ((c = global_state_command_factory (*n))) {
3044                                     ut->add_command (c);
3045                             }
3046                             
3047                     } else if (n->name() == "DeltaCommand") {
3048                          PBD::ID  id(n->property("midi-source")->value());
3049                          boost::shared_ptr<MidiSource> midi_source = 
3050                                  boost::dynamic_pointer_cast<MidiSource, Source>(source_by_id(id));
3051                          if(midi_source) {
3052                                  ut->add_command(new MidiModel::DeltaCommand(midi_source->model(), *n));                                 
3053                          } else {
3054                                  error << "FIXME: Failed to downcast MidiSource for DeltaCommand" << endmsg;
3055                          }
3056                     } else {
3057                             error << string_compose(_("Couldn't figure out how to make a Command out of a %1 XMLNode."), n->name()) << endmsg;
3058                     }
3059             }
3060
3061             _history.add (ut);
3062     }
3063
3064     return 0;
3065 }
3066
3067 void
3068 Session::config_changed (const char* parameter_name)
3069 {
3070 #define PARAM_IS(x) (!strcmp (parameter_name, (x)))
3071
3072         if (PARAM_IS ("seamless-loop")) {
3073                 
3074         } else if (PARAM_IS ("rf-speed")) {
3075                 
3076         } else if (PARAM_IS ("auto-loop")) {
3077                 
3078         } else if (PARAM_IS ("auto-input")) {
3079
3080                 if (Config->get_monitoring_model() == HardwareMonitoring && transport_rolling()) {
3081                         /* auto-input only makes a difference if we're rolling */
3082                         
3083                         boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
3084                         
3085                         for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
3086                                 if ((*i)->record_enabled ()) {
3087                                         (*i)->monitor_input (!Config->get_auto_input());
3088                                 }
3089                         }
3090                 }
3091
3092         } else if (PARAM_IS ("punch-in")) {
3093
3094                 Location* location;
3095                 
3096                 if ((location = _locations.auto_punch_location()) != 0) {
3097                         
3098                         if (Config->get_punch_in ()) {
3099                                 replace_event (Event::PunchIn, location->start());
3100                         } else {
3101                                 remove_event (location->start(), Event::PunchIn);
3102                         }
3103                 }
3104                 
3105         } else if (PARAM_IS ("punch-out")) {
3106
3107                 Location* location;
3108                 
3109                 if ((location = _locations.auto_punch_location()) != 0) {
3110                         
3111                         if (Config->get_punch_out()) {
3112                                 replace_event (Event::PunchOut, location->end());
3113                         } else {
3114                                 clear_events (Event::PunchOut);
3115                         }
3116                 }
3117
3118         } else if (PARAM_IS ("edit-mode")) {
3119
3120                 Glib::Mutex::Lock lm (playlist_lock);
3121                 
3122                 for (PlaylistList::iterator i = playlists.begin(); i != playlists.end(); ++i) {
3123                         (*i)->set_edit_mode (Config->get_edit_mode ());
3124                 }
3125
3126         } else if (PARAM_IS ("use-video-sync")) {
3127
3128                 waiting_for_sync_offset = Config->get_use_video_sync();
3129
3130         } else if (PARAM_IS ("mmc-control")) {
3131
3132                 //poke_midi_thread ();
3133
3134         } else if (PARAM_IS ("mmc-device-id") || PARAM_IS ("mmc-receive-id")) {
3135
3136                 if (mmc) {
3137                         mmc->set_receive_device_id (Config->get_mmc_receive_device_id());
3138                 }
3139
3140         } else if (PARAM_IS ("mmc-send-id")) {
3141
3142                 if (mmc) {
3143                         mmc->set_send_device_id (Config->get_mmc_send_device_id());
3144                 }
3145
3146         } else if (PARAM_IS ("midi-control")) {
3147                 
3148                 //poke_midi_thread ();
3149
3150         } else if (PARAM_IS ("raid-path")) {
3151
3152                 setup_raid_path (Config->get_raid_path());
3153
3154         } else if (PARAM_IS ("smpte-format")) {
3155
3156                 sync_time_vars ();
3157
3158         } else if (PARAM_IS ("video-pullup")) {
3159
3160                 sync_time_vars ();
3161
3162         } else if (PARAM_IS ("seamless-loop")) {
3163
3164                 if (play_loop && transport_rolling()) {
3165                         // to reset diskstreams etc
3166                         request_play_loop (true);
3167                 }
3168
3169         } else if (PARAM_IS ("rf-speed")) {
3170
3171                 cumulative_rf_motion = 0;
3172                 reset_rf_scale (0);
3173
3174         } else if (PARAM_IS ("click-sound")) {
3175
3176                 setup_click_sounds (1);
3177
3178         } else if (PARAM_IS ("click-emphasis-sound")) {
3179
3180                 setup_click_sounds (-1);
3181
3182         } else if (PARAM_IS ("clicking")) {
3183
3184                 if (Config->get_clicking()) {
3185                         if (_click_io && click_data) { // don't require emphasis data
3186                                 _clicking = true;
3187                         }
3188                 } else {
3189                         _clicking = false;
3190                 }
3191
3192         } else if (PARAM_IS ("send-mtc")) {
3193                 
3194                 /* only set the internal flag if we have
3195                    a port.
3196                 */
3197                 
3198                 if (_mtc_port != 0) {
3199                         session_send_mtc = Config->get_send_mtc();
3200                         if (session_send_mtc) {
3201                                 /* mark us ready to send */
3202                                 next_quarter_frame_to_send = 0;
3203                         }
3204                 } else {
3205                         session_send_mtc = false;
3206                 }
3207
3208         } else if (PARAM_IS ("send-mmc")) {
3209                 
3210                 /* only set the internal flag if we have
3211                    a port.
3212                 */
3213                 
3214                 if (_mmc_port != 0) {
3215                         session_send_mmc = Config->get_send_mmc();
3216                 } else {
3217                         mmc = 0;
3218                         session_send_mmc = false; 
3219                 }
3220
3221         } else if (PARAM_IS ("midi-feedback")) {
3222                 
3223                 /* only set the internal flag if we have
3224                    a port.
3225                 */
3226                 
3227                 if (_mtc_port != 0) {
3228                         session_midi_feedback = Config->get_midi_feedback();
3229                 }
3230
3231         } else if (PARAM_IS ("jack-time-master")) {
3232
3233                 engine().reset_timebase ();
3234
3235         } else if (PARAM_IS ("native-file-header-format")) {
3236
3237                 if (!first_file_header_format_reset) {
3238                         reset_native_file_format ();
3239                 }
3240
3241                 first_file_header_format_reset = false;
3242
3243         } else if (PARAM_IS ("native-file-data-format")) {
3244
3245                 if (!first_file_data_format_reset) {
3246                         reset_native_file_format ();
3247                 }
3248
3249                 first_file_data_format_reset = false;
3250
3251         } else if (PARAM_IS ("slave-source")) {
3252                 set_slave_source (Config->get_slave_source());
3253         } else if (PARAM_IS ("remote-model")) {
3254                 set_remote_control_ids ();
3255         }  else if (PARAM_IS ("denormal-model")) {
3256                 setup_fpu ();
3257         } else if (PARAM_IS ("history-depth")) {
3258                 set_history_depth (Config->get_history_depth());
3259         } else if (PARAM_IS ("sync-all-route-ordering")) {
3260                 sync_order_keys ();
3261         } else if (PARAM_IS ("initial-program-change")) {
3262
3263                 if (_mmc_port && Config->get_initial_program_change() >= 0) {
3264                         MIDI::byte buf[2];
3265                         
3266                         buf[0] = MIDI::program; // channel zero by default
3267                         buf[1] = (Config->get_initial_program_change() & 0x7f);
3268
3269                         _mmc_port->midimsg (buf, sizeof (buf), 0);
3270                 }
3271         }
3272
3273         set_dirty ();
3274                    
3275 #undef PARAM_IS
3276
3277 }
3278
3279 void
3280 Session::set_history_depth (uint32_t d)
3281 {
3282         _history.set_depth (d);
3283 }