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