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