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