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