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