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