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