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