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