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