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