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