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