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