fix bypassing plugins with sidechain i/o
[ardour.git] / libs / ardour / session_state.cc
1 /*
2   Copyright (C) 1999-2013 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 <string>
29 #include <cerrno>
30 #include <cstdio> /* snprintf(3) ... grrr */
31 #include <cmath>
32
33 #include <unistd.h>
34 #include <climits>
35 #include <signal.h>
36 #include <sys/time.h>
37
38 #ifdef HAVE_SYS_VFS_H
39 #include <sys/vfs.h>
40 #endif
41
42 #if defined(__APPLE__) || defined(__FreeBSD__)
43 #include <sys/param.h>
44 #include <sys/mount.h>
45 #endif
46
47 #ifdef HAVE_SYS_STATVFS_H
48 #include <sys/statvfs.h>
49 #endif
50
51 #include <glib.h>
52 #include "pbd/gstdio_compat.h"
53
54 #include <glibmm.h>
55 #include <glibmm/threads.h>
56 #include <glibmm/fileutils.h>
57
58 #include <boost/algorithm/string.hpp>
59
60 #include "midi++/mmc.h"
61 #include "midi++/port.h"
62
63 #include "evoral/SMF.hpp"
64
65 #include "pbd/basename.h"
66 #include "pbd/debug.h"
67 #include "pbd/enumwriter.h"
68 #include "pbd/error.h"
69 #include "pbd/file_utils.h"
70 #include "pbd/pathexpand.h"
71 #include "pbd/pthread_utils.h"
72 #include "pbd/stacktrace.h"
73 #include "pbd/convert.h"
74 #include "pbd/localtime_r.h"
75 #include "pbd/unwind.h"
76
77 #include "ardour/amp.h"
78 #include "ardour/async_midi_port.h"
79 #include "ardour/audio_diskstream.h"
80 #include "ardour/audio_track.h"
81 #include "ardour/audioengine.h"
82 #include "ardour/audiofilesource.h"
83 #include "ardour/audioregion.h"
84 #include "ardour/auditioner.h"
85 #include "ardour/automation_control.h"
86 #include "ardour/boost_debug.h"
87 #include "ardour/butler.h"
88 #include "ardour/controllable_descriptor.h"
89 #include "ardour/control_protocol_manager.h"
90 #include "ardour/directory_names.h"
91 #include "ardour/filename_extensions.h"
92 #include "ardour/graph.h"
93 #include "ardour/location.h"
94 #include "ardour/midi_model.h"
95 #include "ardour/midi_patch_manager.h"
96 #include "ardour/midi_region.h"
97 #include "ardour/midi_scene_changer.h"
98 #include "ardour/midi_source.h"
99 #include "ardour/midi_track.h"
100 #include "ardour/pannable.h"
101 #include "ardour/playlist_factory.h"
102 #include "ardour/playlist_source.h"
103 #include "ardour/port.h"
104 #include "ardour/processor.h"
105 #include "ardour/profile.h"
106 #include "ardour/proxy_controllable.h"
107 #include "ardour/recent_sessions.h"
108 #include "ardour/region_factory.h"
109 #include "ardour/revision.h"
110 #include "ardour/route_group.h"
111 #include "ardour/send.h"
112 #include "ardour/session.h"
113 #include "ardour/session_directory.h"
114 #include "ardour/session_metadata.h"
115 #include "ardour/session_playlists.h"
116 #include "ardour/session_state_utils.h"
117 #include "ardour/silentfilesource.h"
118 #include "ardour/sndfilesource.h"
119 #include "ardour/source_factory.h"
120 #include "ardour/speakers.h"
121 #include "ardour/template_utils.h"
122 #include "ardour/tempo.h"
123 #include "ardour/ticker.h"
124 #include "ardour/user_bundle.h"
125 #include "ardour/vca.h"
126 #include "ardour/vca_manager.h"
127
128 #include "control_protocol/control_protocol.h"
129
130 #include "LuaBridge/LuaBridge.h"
131
132 #include "i18n.h"
133 #include <locale.h>
134
135 using namespace std;
136 using namespace ARDOUR;
137 using namespace PBD;
138
139 #define DEBUG_UNDO_HISTORY(msg) DEBUG_TRACE (PBD::DEBUG::UndoHistory, string_compose ("%1: %2\n", __LINE__, msg));
140
141 void
142 Session::pre_engine_init (string fullpath)
143 {
144         if (fullpath.empty()) {
145                 destroy ();
146                 throw failed_constructor();
147         }
148
149         /* discover canonical fullpath */
150
151         _path = canonical_path(fullpath);
152
153         /* is it new ? */
154         if (Profile->get_trx() ) {
155                 // Waves TracksLive has a usecase of session replacement with a new one.
156                 // We should check session state file (<session_name>.ardour) existance
157                 // to determine if the session is new or not
158
159                 string full_session_name = Glib::build_filename( fullpath, _name );
160                 full_session_name += statefile_suffix;
161
162                 _is_new = !Glib::file_test (full_session_name, Glib::FileTest (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR));
163         } else {
164                 _is_new = !Glib::file_test (_path, Glib::FileTest (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR));
165         }
166
167         /* finish initialization that can't be done in a normal C++ constructor
168            definition.
169         */
170
171         timerclear (&last_mmc_step);
172         g_atomic_int_set (&processing_prohibited, 0);
173         g_atomic_int_set (&_record_status, Disabled);
174         g_atomic_int_set (&_playback_load, 100);
175         g_atomic_int_set (&_capture_load, 100);
176         set_next_event ();
177         _all_route_group->set_active (true, this);
178         interpolation.add_channel_to (0, 0);
179
180         if (config.get_use_video_sync()) {
181                 waiting_for_sync_offset = true;
182         } else {
183                 waiting_for_sync_offset = false;
184         }
185
186         last_rr_session_dir = session_dirs.begin();
187
188         set_history_depth (Config->get_history_depth());
189
190         /* default: assume simple stereo speaker configuration */
191
192         _speakers->setup_default_speakers (2);
193
194         _solo_cut_control.reset (new ProxyControllable (_("solo cut control (dB)"), PBD::Controllable::GainLike,
195                                                         boost::bind (&RCConfiguration::set_solo_mute_gain, Config, _1),
196                                                         boost::bind (&RCConfiguration::get_solo_mute_gain, Config)));
197         add_controllable (_solo_cut_control);
198
199         /* These are all static "per-class" signals */
200
201         SourceFactory::SourceCreated.connect_same_thread (*this, boost::bind (&Session::add_source, this, _1));
202         PlaylistFactory::PlaylistCreated.connect_same_thread (*this, boost::bind (&Session::add_playlist, this, _1, _2));
203         AutomationList::AutomationListCreated.connect_same_thread (*this, boost::bind (&Session::add_automation_list, this, _1));
204         Controllable::Destroyed.connect_same_thread (*this, boost::bind (&Session::remove_controllable, this, _1));
205         IO::PortCountChanged.connect_same_thread (*this, boost::bind (&Session::ensure_buffers, this, _1));
206
207         /* stop IO objects from doing stuff until we're ready for them */
208
209         Delivery::disable_panners ();
210         IO::disable_connecting ();
211 }
212
213 int
214 Session::post_engine_init ()
215 {
216         BootMessage (_("Set block size and sample rate"));
217
218         set_block_size (_engine.samples_per_cycle());
219         set_frame_rate (_engine.sample_rate());
220
221         BootMessage (_("Using configuration"));
222
223         _midi_ports = new MidiPortManager;
224
225         MIDISceneChanger* msc;
226
227         _scene_changer = msc = new MIDISceneChanger (*this);
228         msc->set_input_port (boost::dynamic_pointer_cast<MidiPort>(scene_input_port()));
229         msc->set_output_port (boost::dynamic_pointer_cast<MidiPort>(scene_output_port()));
230
231         boost::function<framecnt_t(void)> timer_func (boost::bind (&Session::audible_frame, this));
232         boost::dynamic_pointer_cast<AsyncMIDIPort>(scene_input_port())->set_timer (timer_func);
233
234         setup_midi_machine_control ();
235
236         if (_butler->start_thread()) {
237                 return -1;
238         }
239
240         if (start_midi_thread ()) {
241                 return -1;
242         }
243
244         setup_click_sounds (0);
245         setup_midi_control ();
246
247         _engine.Halted.connect_same_thread (*this, boost::bind (&Session::engine_halted, this));
248         _engine.Xrun.connect_same_thread (*this, boost::bind (&Session::xrun_recovery, this));
249
250         try {
251                 /* tempo map requires sample rate knowledge */
252
253                 delete _tempo_map;
254                 _tempo_map = new TempoMap (_current_frame_rate);
255                 _tempo_map->PropertyChanged.connect_same_thread (*this, boost::bind (&Session::tempo_map_changed, this, _1));
256                 _tempo_map->MetricPositionChanged.connect_same_thread (*this, boost::bind (&Session::gui_tempo_map_changed, this));
257
258                 /* MidiClock requires a tempo map */
259
260                 delete midi_clock;
261                 midi_clock = new MidiClockTicker ();
262                 midi_clock->set_session (this);
263
264                 /* crossfades require sample rate knowledge */
265
266                 SndFileSource::setup_standard_crossfades (*this, frame_rate());
267                 _engine.GraphReordered.connect_same_thread (*this, boost::bind (&Session::graph_reordered, this));
268
269                 AudioDiskstream::allocate_working_buffers();
270                 refresh_disk_space ();
271
272                 /* we're finally ready to call set_state() ... all objects have
273                  * been created, the engine is running.
274                  */
275
276                 if (state_tree) {
277                         if (set_state (*state_tree->root(), Stateful::loading_state_version)) {
278                                 return -1;
279                         }
280                 } else {
281                         // set_state() will call setup_raid_path(), but if it's a new session we need
282                         // to call setup_raid_path() here.
283                         setup_raid_path (_path);
284                 }
285
286                 /* ENGINE */
287
288                 boost::function<void (std::string)> ff (boost::bind (&Session::config_changed, this, _1, false));
289                 boost::function<void (std::string)> ft (boost::bind (&Session::config_changed, this, _1, true));
290
291                 Config->map_parameters (ff);
292                 config.map_parameters (ft);
293                 _butler->map_parameters ();
294
295                 /* Reset all panners */
296
297                 Delivery::reset_panners ();
298
299                 /* this will cause the CPM to instantiate any protocols that are in use
300                  * (or mandatory), which will pass it this Session, and then call
301                  * set_state() on each instantiated protocol to match stored state.
302                  */
303
304                 ControlProtocolManager::instance().set_session (this);
305
306                 /* This must be done after the ControlProtocolManager set_session above,
307                    as it will set states for ports which the ControlProtocolManager creates.
308                 */
309
310                 // XXX set state of MIDI::Port's
311                 // MidiPortManager::instance()->set_port_states (Config->midi_port_states ());
312
313                 /* And this must be done after the MIDI::Manager::set_port_states as
314                  * it will try to make connections whose details are loaded by set_port_states.
315                  */
316
317                 hookup_io ();
318
319                 /* Let control protocols know that we are now all connected, so they
320                  * could start talking to surfaces if they want to.
321                  */
322
323                 ControlProtocolManager::instance().midi_connectivity_established ();
324
325                 if (_is_new && !no_auto_connect()) {
326                         Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock());
327                         auto_connect_master_bus ();
328                 }
329
330                 _state_of_the_state = StateOfTheState (_state_of_the_state & ~(CannotSave|Dirty));
331
332                 /* update latencies */
333
334                 initialize_latencies ();
335
336                 _locations->added.connect_same_thread (*this, boost::bind (&Session::location_added, this, _1));
337                 _locations->removed.connect_same_thread (*this, boost::bind (&Session::location_removed, this, _1));
338                 _locations->changed.connect_same_thread (*this, boost::bind (&Session::locations_changed, this));
339
340         } catch (AudioEngine::PortRegistrationFailure& err) {
341                 /* handle this one in a different way than all others, so that its clear what happened */
342                 error << err.what() << endmsg;
343                 return -1;
344         } catch (...) {
345                 return -1;
346         }
347
348         BootMessage (_("Reset Remote Controls"));
349
350         // send_full_time_code (0);
351         _engine.transport_locate (0);
352
353         send_immediate_mmc (MIDI::MachineControlCommand (MIDI::MachineControl::cmdMmcReset));
354         send_immediate_mmc (MIDI::MachineControlCommand (Timecode::Time ()));
355
356         MIDI::Name::MidiPatchManager::instance().add_search_path (session_directory().midi_patch_path() );
357
358         ltc_tx_initialize();
359         /* initial program change will be delivered later; see ::config_changed() */
360
361         _state_of_the_state = Clean;
362
363         Port::set_connecting_blocked (false);
364
365         DirtyChanged (); /* EMIT SIGNAL */
366
367         if (_is_new) {
368                 save_state ("");
369         } else if (state_was_pending) {
370                 save_state ("");
371                 remove_pending_capture_state ();
372                 state_was_pending = false;
373         }
374
375         /* Now, finally, we can fill the playback buffers */
376
377         BootMessage (_("Filling playback buffers"));
378
379         boost::shared_ptr<RouteList> rl = routes.reader();
380         for (RouteList::iterator r = rl->begin(); r != rl->end(); ++r) {
381                 boost::shared_ptr<Track> trk = boost::dynamic_pointer_cast<Track> (*r);
382                 if (trk && !trk->hidden()) {
383                         trk->seek (_transport_frame, true);
384                 }
385         }
386
387         return 0;
388 }
389
390 void
391 Session::session_loaded ()
392 {
393         SessionLoaded();
394
395         _state_of_the_state = Clean;
396
397         DirtyChanged (); /* EMIT SIGNAL */
398
399         if (_is_new) {
400                 save_state ("");
401         } else if (state_was_pending) {
402                 save_state ("");
403                 remove_pending_capture_state ();
404                 state_was_pending = false;
405         }
406
407         /* Now, finally, we can fill the playback buffers */
408
409         BootMessage (_("Filling playback buffers"));
410         force_locate (_transport_frame, false);
411 }
412
413 string
414 Session::raid_path () const
415 {
416         Searchpath raid_search_path;
417
418         for (vector<space_and_path>::const_iterator i = session_dirs.begin(); i != session_dirs.end(); ++i) {
419                 raid_search_path += (*i).path;
420         }
421
422         return raid_search_path.to_string ();
423 }
424
425 void
426 Session::setup_raid_path (string path)
427 {
428         if (path.empty()) {
429                 return;
430         }
431
432         space_and_path sp;
433         string fspath;
434
435         session_dirs.clear ();
436
437         Searchpath search_path(path);
438         Searchpath sound_search_path;
439         Searchpath midi_search_path;
440
441         for (Searchpath::const_iterator i = search_path.begin(); i != search_path.end(); ++i) {
442                 sp.path = *i;
443                 sp.blocks = 0; // not needed
444                 session_dirs.push_back (sp);
445
446                 SessionDirectory sdir(sp.path);
447
448                 sound_search_path += sdir.sound_path ();
449                 midi_search_path += sdir.midi_path ();
450         }
451
452         // reset the round-robin soundfile path thingie
453         last_rr_session_dir = session_dirs.begin();
454 }
455
456 bool
457 Session::path_is_within_session (const std::string& path)
458 {
459         for (vector<space_and_path>::const_iterator i = session_dirs.begin(); i != session_dirs.end(); ++i) {
460                 if (PBD::path_is_within (i->path, path)) {
461                         return true;
462                 }
463         }
464         return false;
465 }
466
467 int
468 Session::ensure_subdirs ()
469 {
470         string dir;
471
472         dir = session_directory().peak_path();
473
474         if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
475                 error << string_compose(_("Session: cannot create session peakfile folder \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
476                 return -1;
477         }
478
479         dir = session_directory().sound_path();
480
481         if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
482                 error << string_compose(_("Session: cannot create session sounds dir \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
483                 return -1;
484         }
485
486         dir = session_directory().midi_path();
487
488         if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
489                 error << string_compose(_("Session: cannot create session midi dir \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
490                 return -1;
491         }
492
493         dir = session_directory().dead_path();
494
495         if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
496                 error << string_compose(_("Session: cannot create session dead sounds folder \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
497                 return -1;
498         }
499
500         dir = session_directory().export_path();
501
502         if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
503                 error << string_compose(_("Session: cannot create session export folder \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
504                 return -1;
505         }
506
507         dir = analysis_dir ();
508
509         if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
510                 error << string_compose(_("Session: cannot create session analysis folder \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
511                 return -1;
512         }
513
514         dir = plugins_dir ();
515
516         if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
517                 error << string_compose(_("Session: cannot create session plugins folder \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
518                 return -1;
519         }
520
521         dir = externals_dir ();
522
523         if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
524                 error << string_compose(_("Session: cannot create session externals folder \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
525                 return -1;
526         }
527
528         return 0;
529 }
530
531 /** @param session_template directory containing session template, or empty.
532  *  Caller must not hold process lock.
533  */
534 int
535 Session::create (const string& session_template, BusProfile* bus_profile)
536 {
537         if (g_mkdir_with_parents (_path.c_str(), 0755) < 0) {
538                 error << string_compose(_("Session: cannot create session folder \"%1\" (%2)"), _path, strerror (errno)) << endmsg;
539                 return -1;
540         }
541
542         if (ensure_subdirs ()) {
543                 return -1;
544         }
545
546         _writable = exists_and_writable (_path);
547
548         if (!session_template.empty()) {
549                 string in_path = (ARDOUR::Profile->get_trx () ? session_template : session_template_dir_to_file (session_template));
550
551                 FILE* in = g_fopen (in_path.c_str(), "rb");
552
553                 if (in) {
554                         /* no need to call legalize_for_path() since the string
555                          * in session_template is already a legal path name
556                          */
557                         string out_path = Glib::build_filename (_session_dir->root_path(), _name + statefile_suffix);
558
559                         FILE* out = g_fopen (out_path.c_str(), "wb");
560
561                         if (out) {
562                                 char buf[1024];
563                                 stringstream new_session;
564
565                                 while (!feof (in)) {
566                                         size_t charsRead = fread (buf, sizeof(char), 1024, in);
567
568                                         if (ferror (in)) {
569                                                 error << string_compose (_("Error reading session template file %1 (%2)"), in_path, strerror (errno)) << endmsg;
570                                                 fclose (in);
571                                                 fclose (out);
572                                                 return -1;
573                                         }
574                                         if (charsRead == 0) {
575                                                 break;
576                                         }
577                                         new_session.write (buf, charsRead);
578                                 }
579                                 fclose (in);
580
581                                 string file_contents = new_session.str();
582                                 size_t writeSize = file_contents.length();
583                                 if (fwrite (file_contents.c_str(), sizeof(char), writeSize, out) != writeSize) {
584                                         error << string_compose (_("Error writing session template file %1 (%2)"), out_path, strerror (errno)) << endmsg;
585                                         fclose (out);
586                                         return -1;
587                                 }
588                                 fclose (out);
589
590                                 _is_new = false;
591
592                                 if (!ARDOUR::Profile->get_trx()) {
593                                         /* Copy plugin state files from template to new session */
594                                         std::string template_plugins = Glib::build_filename (session_template, X_("plugins"));
595                                         copy_recurse (template_plugins, plugins_dir ());
596                                 }
597
598                                 return 0;
599
600                         } else {
601                                 error << string_compose (_("Could not open %1 for writing session template"), out_path)
602                                         << endmsg;
603                                 fclose(in);
604                                 return -1;
605                         }
606
607                 } else {
608                         error << string_compose (_("Could not open session template %1 for reading"), in_path)
609                                 << endmsg;
610                         return -1;
611                 }
612
613         }
614
615         if (Profile->get_trx()) {
616
617                 /* set initial start + end point : ARDOUR::Session::session_end_shift long.
618                    Remember that this is a brand new session. Sessions
619                    loaded from saved state will get this range from the saved state.
620                 */
621
622                 set_session_range_location (0, 0);
623
624                 /* Initial loop location, from absolute zero, length 10 seconds  */
625
626                 Location* loc = new Location (*this, 0, 10.0 * _engine.sample_rate(), _("Loop"),  Location::IsAutoLoop);
627                 _locations->add (loc, true);
628                 set_auto_loop_location (loc);
629         }
630
631         _state_of_the_state = Clean;
632
633         /* set up Master Out and Monitor Out if necessary */
634
635         if (bus_profile) {
636
637                 RouteList rl;
638                 ChanCount count(DataType::AUDIO, bus_profile->master_out_channels);
639
640                 // Waves Tracks: always create master bus for Tracks
641                 if (ARDOUR::Profile->get_trx() || bus_profile->master_out_channels) {
642                         boost::shared_ptr<Route> r (new Route (*this, _("Master"), PresentationInfo::MasterOut, DataType::AUDIO));
643                         if (r->init ()) {
644                                 return -1;
645                         }
646
647                         BOOST_MARK_ROUTE(r);
648
649                         {
650                                 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
651                                 r->input()->ensure_io (count, false, this);
652                                 r->output()->ensure_io (count, false, this);
653                         }
654
655                         rl.push_back (r);
656
657                 } else {
658                         /* prohibit auto-connect to master, because there isn't one */
659                         bus_profile->output_ac = AutoConnectOption (bus_profile->output_ac & ~AutoConnectMaster);
660                 }
661
662                 if (!rl.empty()) {
663                         add_routes (rl, false, false, false, PresentationInfo::max_order);
664                 }
665
666                 // Waves Tracks: Skip this. Always use autoconnection for Tracks
667                 if (!ARDOUR::Profile->get_trx()) {
668
669                         /* this allows the user to override settings with an environment variable.
670                          */
671
672                         if (no_auto_connect()) {
673                                 bus_profile->input_ac = AutoConnectOption (0);
674                                 bus_profile->output_ac = AutoConnectOption (0);
675                         }
676
677                         Config->set_input_auto_connect (bus_profile->input_ac);
678                         Config->set_output_auto_connect (bus_profile->output_ac);
679                 }
680         }
681
682         if (Config->get_use_monitor_bus() && bus_profile) {
683                 add_monitor_section ();
684         }
685
686         return 0;
687 }
688
689 void
690 Session::maybe_write_autosave()
691 {
692         if (dirty() && record_status() != Recording) {
693                 save_state("", true);
694         }
695 }
696
697 void
698 Session::remove_pending_capture_state ()
699 {
700         std::string pending_state_file_path(_session_dir->root_path());
701
702         pending_state_file_path = Glib::build_filename (pending_state_file_path, legalize_for_path (_current_snapshot_name) + pending_suffix);
703
704         if (!Glib::file_test (pending_state_file_path, Glib::FILE_TEST_EXISTS)) return;
705
706         if (g_remove (pending_state_file_path.c_str()) != 0) {
707                 error << string_compose(_("Could not remove pending capture state at path \"%1\" (%2)"),
708                                 pending_state_file_path, g_strerror (errno)) << endmsg;
709         }
710 }
711
712 /** Rename a state file.
713  *  @param old_name Old snapshot name.
714  *  @param new_name New snapshot name.
715  */
716 void
717 Session::rename_state (string old_name, string new_name)
718 {
719         if (old_name == _current_snapshot_name || old_name == _name) {
720                 /* refuse to rename the current snapshot or the "main" one */
721                 return;
722         }
723
724         const string old_xml_filename = legalize_for_path (old_name) + statefile_suffix;
725         const string new_xml_filename = legalize_for_path (new_name) + statefile_suffix;
726
727         const std::string old_xml_path(Glib::build_filename (_session_dir->root_path(), old_xml_filename));
728         const std::string new_xml_path(Glib::build_filename (_session_dir->root_path(), new_xml_filename));
729
730         if (::g_rename (old_xml_path.c_str(), new_xml_path.c_str()) != 0) {
731                 error << string_compose(_("could not rename snapshot %1 to %2 (%3)"),
732                                 old_name, new_name, g_strerror(errno)) << endmsg;
733         }
734 }
735
736 /** Remove a state file.
737  *  @param snapshot_name Snapshot name.
738  */
739 void
740 Session::remove_state (string snapshot_name)
741 {
742         if (!_writable || snapshot_name == _current_snapshot_name || snapshot_name == _name) {
743                 // refuse to remove the current snapshot or the "main" one
744                 return;
745         }
746
747         std::string xml_path(_session_dir->root_path());
748
749         xml_path = Glib::build_filename (xml_path, legalize_for_path (snapshot_name) + statefile_suffix);
750
751         if (!create_backup_file (xml_path)) {
752                 // don't remove it if a backup can't be made
753                 // create_backup_file will log the error.
754                 return;
755         }
756
757         // and delete it
758         if (g_remove (xml_path.c_str()) != 0) {
759                 error << string_compose(_("Could not remove session file at path \"%1\" (%2)"),
760                                 xml_path, g_strerror (errno)) << endmsg;
761         }
762 }
763
764 /** @param snapshot_name Name to save under, without .ardour / .pending prefix */
765 int
766 Session::save_state (string snapshot_name, bool pending, bool switch_to_snapshot, bool template_only)
767 {
768         DEBUG_TRACE (DEBUG::Locale, string_compose ("Session::save_state locale '%1'\n", setlocale (LC_NUMERIC, NULL)));
769
770         XMLTree tree;
771         std::string xml_path(_session_dir->root_path());
772
773         /* prevent concurrent saves from different threads */
774
775         Glib::Threads::Mutex::Lock lm (save_state_lock);
776
777         if (!_writable || (_state_of_the_state & CannotSave)) {
778                 return 1;
779         }
780
781         if (g_atomic_int_get(&_suspend_save)) {
782                 _save_queued = true;
783                 return 1;
784         }
785         _save_queued = false;
786
787         if (!_engine.connected ()) {
788                 error << string_compose (_("the %1 audio engine is not connected and state saving would lose all I/O connections. Session not saved"),
789                                          PROGRAM_NAME)
790                       << endmsg;
791                 return 1;
792         }
793
794 #ifndef NDEBUG
795         const int64_t save_start_time = g_get_monotonic_time();
796 #endif
797
798         /* tell sources we're saving first, in case they write out to a new file
799          * which should be saved with the state rather than the old one */
800         for (SourceMap::const_iterator i = sources.begin(); i != sources.end(); ++i) {
801                 try {
802                         i->second->session_saved();
803                 } catch (Evoral::SMF::FileError& e) {
804                         error << string_compose ("Could not write to MIDI file %1; MIDI data not saved.", e.file_name ()) << endmsg;
805                 }
806         }
807
808         SessionSaveUnderway (); /* EMIT SIGNAL */
809
810         bool mark_as_clean = true;
811
812         if (!snapshot_name.empty() && !switch_to_snapshot) {
813                 mark_as_clean = false;
814         }
815
816         if (template_only) {
817                 mark_as_clean = false;
818                 tree.set_root (&get_template());
819         } else {
820                 tree.set_root (&get_state());
821         }
822
823         if (snapshot_name.empty()) {
824                 snapshot_name = _current_snapshot_name;
825         } else if (switch_to_snapshot) {
826                 set_snapshot_name (snapshot_name);
827         }
828
829         assert (!snapshot_name.empty());
830
831         if (!pending) {
832
833                 /* proper save: use statefile_suffix (.ardour in English) */
834
835                 xml_path = Glib::build_filename (xml_path, legalize_for_path (snapshot_name) + statefile_suffix);
836
837                 /* make a backup copy of the old file */
838
839                 if (Glib::file_test (xml_path, Glib::FILE_TEST_EXISTS) && !create_backup_file (xml_path)) {
840                         // create_backup_file will log the error
841                         return -1;
842                 }
843
844         } else {
845
846                 /* pending save: use pending_suffix (.pending in English) */
847                 xml_path = Glib::build_filename (xml_path, legalize_for_path (snapshot_name) + pending_suffix);
848         }
849
850         std::string tmp_path(_session_dir->root_path());
851         tmp_path = Glib::build_filename (tmp_path, legalize_for_path (snapshot_name) + temp_suffix);
852
853         cerr << "actually writing state to " << tmp_path << endl;
854
855         if (!tree.write (tmp_path)) {
856                 error << string_compose (_("state could not be saved to %1"), tmp_path) << endmsg;
857                 if (g_remove (tmp_path.c_str()) != 0) {
858                         error << string_compose(_("Could not remove temporary session file at path \"%1\" (%2)"),
859                                         tmp_path, g_strerror (errno)) << endmsg;
860                 }
861                 return -1;
862
863         } else {
864
865                 cerr << "renaming state to " << xml_path << endl;
866
867                 if (::g_rename (tmp_path.c_str(), xml_path.c_str()) != 0) {
868                         error << string_compose (_("could not rename temporary session file %1 to %2 (%3)"),
869                                         tmp_path, xml_path, g_strerror(errno)) << endmsg;
870                         if (g_remove (tmp_path.c_str()) != 0) {
871                                 error << string_compose(_("Could not remove temporary session file at path \"%1\" (%2)"),
872                                                 tmp_path, g_strerror (errno)) << endmsg;
873                         }
874                         return -1;
875                 }
876         }
877
878         if (!pending) {
879
880                 save_history (snapshot_name);
881
882                 if (mark_as_clean) {
883                         bool was_dirty = dirty();
884
885                         _state_of_the_state = StateOfTheState (_state_of_the_state & ~Dirty);
886
887                         if (was_dirty) {
888                                 DirtyChanged (); /* EMIT SIGNAL */
889                         }
890                 }
891
892                 StateSaved (snapshot_name); /* EMIT SIGNAL */
893         }
894
895 #ifndef NDEBUG
896         const int64_t elapsed_time_us = g_get_monotonic_time() - save_start_time;
897         cerr << "saved state in " << fixed << setprecision (1) << elapsed_time_us / 1000. << " ms\n";
898 #endif
899         return 0;
900 }
901
902 int
903 Session::restore_state (string snapshot_name)
904 {
905         if (load_state (snapshot_name) == 0) {
906                 set_state (*state_tree->root(), Stateful::loading_state_version);
907         }
908
909         return 0;
910 }
911
912 int
913 Session::load_state (string snapshot_name)
914 {
915         delete state_tree;
916         state_tree = 0;
917
918         state_was_pending = false;
919
920         /* check for leftover pending state from a crashed capture attempt */
921
922         std::string xmlpath(_session_dir->root_path());
923         xmlpath = Glib::build_filename (xmlpath, legalize_for_path (snapshot_name) + pending_suffix);
924
925         if (Glib::file_test (xmlpath, Glib::FILE_TEST_EXISTS)) {
926
927                 /* there is pending state from a crashed capture attempt */
928
929                 boost::optional<int> r = AskAboutPendingState();
930                 if (r.get_value_or (1)) {
931                         state_was_pending = true;
932                 }
933         }
934
935         if (!state_was_pending) {
936                 xmlpath = Glib::build_filename (_session_dir->root_path(), snapshot_name);
937         }
938
939         if (!Glib::file_test (xmlpath, Glib::FILE_TEST_EXISTS)) {
940                 xmlpath = Glib::build_filename (_session_dir->root_path(), legalize_for_path (snapshot_name) + statefile_suffix);
941                 if (!Glib::file_test (xmlpath, Glib::FILE_TEST_EXISTS)) {
942                         error << string_compose(_("%1: session file \"%2\" doesn't exist!"), _name, xmlpath) << endmsg;
943                         return 1;
944                 }
945         }
946
947         state_tree = new XMLTree;
948
949         set_dirty();
950
951         _writable = exists_and_writable (xmlpath) && exists_and_writable(Glib::path_get_dirname(xmlpath));
952
953         if (!state_tree->read (xmlpath)) {
954                 error << string_compose(_("Could not understand session file %1"), xmlpath) << endmsg;
955                 delete state_tree;
956                 state_tree = 0;
957                 return -1;
958         }
959
960         XMLNode const & root (*state_tree->root());
961
962         if (root.name() != X_("Session")) {
963                 error << string_compose (_("Session file %1 is not a session"), xmlpath) << endmsg;
964                 delete state_tree;
965                 state_tree = 0;
966                 return -1;
967         }
968
969         XMLProperty const * prop;
970
971         if ((prop = root.property ("version")) == 0) {
972                 /* no version implies very old version of Ardour */
973                 Stateful::loading_state_version = 1000;
974         } else {
975                 if (prop->value().find ('.') != string::npos) {
976                         /* old school version format */
977                         if (prop->value()[0] == '2') {
978                                 Stateful::loading_state_version = 2000;
979                         } else {
980                                 Stateful::loading_state_version = 3000;
981                         }
982                 } else {
983                         Stateful::loading_state_version = atoi (prop->value());
984                 }
985         }
986
987         if (Stateful::loading_state_version < CURRENT_SESSION_FILE_VERSION && _writable) {
988
989                 std::string backup_path(_session_dir->root_path());
990                 std::string backup_filename = string_compose ("%1-%2%3", legalize_for_path (snapshot_name), Stateful::loading_state_version, statefile_suffix);
991                 backup_path = Glib::build_filename (backup_path, backup_filename);
992
993                 // only create a backup for a given statefile version once
994
995                 if (!Glib::file_test (backup_path, Glib::FILE_TEST_EXISTS)) {
996
997                         VersionMismatch (xmlpath, backup_path);
998
999                         if (!copy_file (xmlpath, backup_path)) {;
1000                                 return -1;
1001                         }
1002                 }
1003         }
1004
1005         save_snapshot_name (snapshot_name);
1006
1007         return 0;
1008 }
1009
1010 int
1011 Session::load_options (const XMLNode& node)
1012 {
1013         LocaleGuard lg;
1014         config.set_variables (node);
1015         return 0;
1016 }
1017
1018 bool
1019 Session::save_default_options ()
1020 {
1021         return config.save_state();
1022 }
1023
1024 XMLNode&
1025 Session::get_state()
1026 {
1027         return state(true);
1028 }
1029
1030 XMLNode&
1031 Session::get_template()
1032 {
1033         /* if we don't disable rec-enable, diskstreams
1034            will believe they need to store their capture
1035            sources in their state node.
1036         */
1037
1038         disable_record (false);
1039
1040         return state(false);
1041 }
1042
1043 XMLNode&
1044 Session::state (bool full_state)
1045 {
1046         LocaleGuard lg;
1047         XMLNode* node = new XMLNode("Session");
1048         XMLNode* child;
1049
1050         char buf[16];
1051         snprintf(buf, sizeof(buf), "%d", CURRENT_SESSION_FILE_VERSION);
1052         node->add_property("version", buf);
1053
1054         child = node->add_child ("ProgramVersion");
1055         child->add_property("created-with", created_with);
1056
1057         std::string modified_with = string_compose ("%1 %2", PROGRAM_NAME, revision);
1058         child->add_property("modified-with", modified_with);
1059
1060         /* store configuration settings */
1061
1062         if (full_state) {
1063
1064                 node->add_property ("name", _name);
1065                 snprintf (buf, sizeof (buf), "%" PRId64, _base_frame_rate);
1066                 node->add_property ("sample-rate", buf);
1067
1068                 if (session_dirs.size() > 1) {
1069
1070                         string p;
1071
1072                         vector<space_and_path>::iterator i = session_dirs.begin();
1073                         vector<space_and_path>::iterator next;
1074
1075                         ++i; /* skip the first one */
1076                         next = i;
1077                         ++next;
1078
1079                         while (i != session_dirs.end()) {
1080
1081                                 p += (*i).path;
1082
1083                                 if (next != session_dirs.end()) {
1084                                         p += G_SEARCHPATH_SEPARATOR;
1085                                 } else {
1086                                         break;
1087                                 }
1088
1089                                 ++next;
1090                                 ++i;
1091                         }
1092
1093                         child = node->add_child ("Path");
1094                         child->add_content (p);
1095                 }
1096         }
1097
1098         /* save the ID counter */
1099
1100         snprintf (buf, sizeof (buf), "%" PRIu64, ID::counter());
1101         node->add_property ("id-counter", buf);
1102
1103         snprintf (buf, sizeof (buf), "%u", name_id_counter ());
1104         node->add_property ("name-counter", buf);
1105
1106         /* save the event ID counter */
1107
1108         snprintf (buf, sizeof (buf), "%d", Evoral::event_id_counter());
1109         node->add_property ("event-counter", buf);
1110
1111         /* save the VCA counter */
1112
1113         snprintf (buf, sizeof (buf), "%" PRIu32, VCA::get_next_vca_number());
1114         node->add_property ("vca-counter", buf);
1115
1116         /* various options */
1117
1118         list<XMLNode*> midi_port_nodes = _midi_ports->get_midi_port_states();
1119         if (!midi_port_nodes.empty()) {
1120                 XMLNode* midi_port_stuff = new XMLNode ("MIDIPorts");
1121                 for (list<XMLNode*>::const_iterator n = midi_port_nodes.begin(); n != midi_port_nodes.end(); ++n) {
1122                         midi_port_stuff->add_child_nocopy (**n);
1123                 }
1124                 node->add_child_nocopy (*midi_port_stuff);
1125         }
1126
1127         node->add_child_nocopy (config.get_variables ());
1128
1129         node->add_child_nocopy (ARDOUR::SessionMetadata::Metadata()->get_state());
1130
1131         child = node->add_child ("Sources");
1132
1133         if (full_state) {
1134                 Glib::Threads::Mutex::Lock sl (source_lock);
1135
1136                 for (SourceMap::iterator siter = sources.begin(); siter != sources.end(); ++siter) {
1137
1138                         /* Don't save information about non-file Sources, or
1139                          * about non-destructive file sources that are empty
1140                          * and unused by any regions.
1141                         */
1142
1143                         boost::shared_ptr<FileSource> fs;
1144
1145                         if ((fs = boost::dynamic_pointer_cast<FileSource> (siter->second)) != 0) {
1146
1147                                 if (!fs->destructive()) {
1148                                         if (fs->empty() && !fs->used()) {
1149                                                 continue;
1150                                         }
1151                                 }
1152
1153                                 child->add_child_nocopy (siter->second->get_state());
1154                         }
1155                 }
1156         }
1157
1158         child = node->add_child ("Regions");
1159
1160         if (full_state) {
1161                 Glib::Threads::Mutex::Lock rl (region_lock);
1162                 const RegionFactory::RegionMap& region_map (RegionFactory::all_regions());
1163                 for (RegionFactory::RegionMap::const_iterator i = region_map.begin(); i != region_map.end(); ++i) {
1164                         boost::shared_ptr<Region> r = i->second;
1165                         /* only store regions not attached to playlists */
1166                         if (r->playlist() == 0) {
1167                                 if (boost::dynamic_pointer_cast<AudioRegion>(r)) {
1168                                         child->add_child_nocopy ((boost::dynamic_pointer_cast<AudioRegion>(r))->get_basic_state ());
1169                                 } else {
1170                                         child->add_child_nocopy (r->get_state ());
1171                                 }
1172                         }
1173                 }
1174
1175                 RegionFactory::CompoundAssociations& cassocs (RegionFactory::compound_associations());
1176
1177                 if (!cassocs.empty()) {
1178                         XMLNode* ca = node->add_child (X_("CompoundAssociations"));
1179
1180                         for (RegionFactory::CompoundAssociations::iterator i = cassocs.begin(); i != cassocs.end(); ++i) {
1181                                 char buf[64];
1182                                 XMLNode* can = new XMLNode (X_("CompoundAssociation"));
1183                                 i->first->id().print (buf, sizeof (buf));
1184                                 can->add_property (X_("copy"), buf);
1185                                 i->second->id().print (buf, sizeof (buf));
1186                                 can->add_property (X_("original"), buf);
1187                                 ca->add_child_nocopy (*can);
1188                         }
1189                 }
1190         }
1191
1192
1193
1194         if (full_state) {
1195
1196                 if (_locations) {
1197                         node->add_child_nocopy (_locations->get_state());
1198                 }
1199         } else {
1200                 Locations loc (*this);
1201                 // for a template, just create a new Locations, populate it
1202                 // with the default start and end, and get the state for that.
1203                 Location* range = new Location (*this, 0, 0, _("session"), Location::IsSessionRange);
1204                 range->set (max_framepos, 0);
1205                 loc.add (range);
1206                 XMLNode& locations_state = loc.get_state();
1207
1208                 if (ARDOUR::Profile->get_trx() && _locations) {
1209                         // For tracks we need stored the Auto Loop Range and all MIDI markers.
1210                         for (Locations::LocationList::const_iterator i = _locations->list ().begin (); i != _locations->list ().end (); ++i) {
1211                                 if ((*i)->is_mark () || (*i)->is_auto_loop ()) {
1212                                         locations_state.add_child_nocopy ((*i)->get_state ());
1213                                 }
1214                         }
1215                 }
1216                 node->add_child_nocopy (locations_state);
1217         }
1218
1219         child = node->add_child ("Bundles");
1220         {
1221                 boost::shared_ptr<BundleList> bundles = _bundles.reader ();
1222                 for (BundleList::iterator i = bundles->begin(); i != bundles->end(); ++i) {
1223                         boost::shared_ptr<UserBundle> b = boost::dynamic_pointer_cast<UserBundle> (*i);
1224                         if (b) {
1225                                 child->add_child_nocopy (b->get_state());
1226                         }
1227                 }
1228         }
1229
1230         node->add_child_nocopy (_vca_manager->get_state());
1231
1232         child = node->add_child ("Routes");
1233         {
1234                 boost::shared_ptr<RouteList> r = routes.reader ();
1235
1236                 RoutePublicOrderSorter cmp;
1237                 RouteList public_order (*r);
1238                 public_order.sort (cmp);
1239
1240                 /* the sort should have put the monitor out first */
1241
1242                 if (_monitor_out) {
1243                         assert (_monitor_out == public_order.front());
1244                 }
1245
1246                 for (RouteList::iterator i = public_order.begin(); i != public_order.end(); ++i) {
1247                         if (!(*i)->is_auditioner()) {
1248                                 if (full_state) {
1249                                         child->add_child_nocopy ((*i)->get_state());
1250                                 } else {
1251                                         child->add_child_nocopy ((*i)->get_template());
1252                                 }
1253                         }
1254                 }
1255         }
1256
1257         playlists->add_state (node, full_state);
1258
1259         child = node->add_child ("RouteGroups");
1260         for (list<RouteGroup *>::iterator i = _route_groups.begin(); i != _route_groups.end(); ++i) {
1261                 child->add_child_nocopy ((*i)->get_state());
1262         }
1263
1264         if (_click_io) {
1265                 XMLNode* gain_child = node->add_child ("Click");
1266                 gain_child->add_child_nocopy (_click_io->state (full_state));
1267                 gain_child->add_child_nocopy (_click_gain->state (full_state));
1268         }
1269
1270         if (_ltc_input) {
1271                 XMLNode* ltc_input_child = node->add_child ("LTC-In");
1272                 ltc_input_child->add_child_nocopy (_ltc_input->state (full_state));
1273         }
1274
1275         if (_ltc_input) {
1276                 XMLNode* ltc_output_child = node->add_child ("LTC-Out");
1277                 ltc_output_child->add_child_nocopy (_ltc_output->state (full_state));
1278         }
1279
1280         node->add_child_nocopy (_speakers->get_state());
1281         node->add_child_nocopy (_tempo_map->get_state());
1282         node->add_child_nocopy (get_control_protocol_state());
1283
1284         if (_extra_xml) {
1285                 node->add_child_copy (*_extra_xml);
1286         }
1287
1288         {
1289                 Glib::Threads::Mutex::Lock lm (lua_lock);
1290                 std::string saved;
1291                 {
1292                         luabridge::LuaRef savedstate ((*_lua_save)());
1293                         saved = savedstate.cast<std::string>();
1294                 }
1295                 lua.collect_garbage ();
1296                 lm.release ();
1297
1298                 gchar* b64 = g_base64_encode ((const guchar*)saved.c_str (), saved.size ());
1299                 std::string b64s (b64);
1300                 g_free (b64);
1301
1302                 XMLNode* script_node = new XMLNode (X_("Script"));
1303                 script_node->add_property (X_("lua"), LUA_VERSION);
1304                 script_node->add_content (b64s);
1305                 node->add_child_nocopy (*script_node);
1306         }
1307
1308         return *node;
1309 }
1310
1311 XMLNode&
1312 Session::get_control_protocol_state ()
1313 {
1314         ControlProtocolManager& cpm (ControlProtocolManager::instance());
1315         return cpm.get_state();
1316 }
1317
1318 int
1319 Session::set_state (const XMLNode& node, int version)
1320 {
1321         LocaleGuard lg;
1322         XMLNodeList nlist;
1323         XMLNode* child;
1324         XMLProperty const * prop;
1325         int ret = -1;
1326
1327         _state_of_the_state = StateOfTheState (_state_of_the_state|CannotSave);
1328
1329         if (node.name() != X_("Session")) {
1330                 fatal << _("programming error: Session: incorrect XML node sent to set_state()") << endmsg;
1331                 goto out;
1332         }
1333
1334         if ((prop = node.property ("name")) != 0) {
1335                 _name = prop->value ();
1336         }
1337
1338         if ((prop = node.property (X_("sample-rate"))) != 0) {
1339
1340                 _base_frame_rate = atoi (prop->value());
1341                 _nominal_frame_rate = _base_frame_rate;
1342
1343                 assert (AudioEngine::instance()->running ());
1344                 if (_base_frame_rate != AudioEngine::instance()->sample_rate ()) {
1345                         boost::optional<int> r = AskAboutSampleRateMismatch (_base_frame_rate, _current_frame_rate);
1346                         if (r.get_value_or (0)) {
1347                                 goto out;
1348                         }
1349                 }
1350         }
1351
1352         created_with = "unknown";
1353         if ((child = find_named_node (node, "ProgramVersion")) != 0) {
1354                 if ((prop = child->property (X_("created-with"))) != 0) {
1355                         created_with = prop->value ();
1356                 }
1357         }
1358
1359         setup_raid_path(_session_dir->root_path());
1360
1361         if ((prop = node.property (X_("id-counter"))) != 0) {
1362                 uint64_t x;
1363                 sscanf (prop->value().c_str(), "%" PRIu64, &x);
1364                 ID::init_counter (x);
1365         } else {
1366                 /* old sessions used a timebased counter, so fake
1367                    the startup ID counter based on a standard
1368                    timestamp.
1369                 */
1370                 time_t now;
1371                 time (&now);
1372                 ID::init_counter (now);
1373         }
1374
1375         if ((prop = node.property (X_("name-counter"))) != 0) {
1376                 init_name_id_counter (atoi (prop->value()));
1377         }
1378
1379         if ((prop = node.property (X_("event-counter"))) != 0) {
1380                 Evoral::init_event_id_counter (atoi (prop->value()));
1381         }
1382
1383         if ((prop = node.property (X_("vca-counter"))) != 0) {
1384                 uint32_t x;
1385                 sscanf (prop->value().c_str(), "%" PRIu32, &x);
1386                 VCA::set_next_vca_number (x);
1387         } else {
1388                 VCA::set_next_vca_number (1);
1389         }
1390
1391         if ((child = find_named_node (node, "MIDIPorts")) != 0) {
1392                 _midi_ports->set_midi_port_states (child->children());
1393         }
1394
1395         IO::disable_connecting ();
1396
1397         Stateful::save_extra_xml (node);
1398
1399         if (((child = find_named_node (node, "Options")) != 0)) { /* old style */
1400                 load_options (*child);
1401         } else if ((child = find_named_node (node, "Config")) != 0) { /* new style */
1402                 load_options (*child);
1403         } else {
1404                 error << _("Session: XML state has no options section") << endmsg;
1405         }
1406
1407         if (version >= 3000) {
1408                 if ((child = find_named_node (node, "Metadata")) == 0) {
1409                         warning << _("Session: XML state has no metadata section") << endmsg;
1410                 } else if ( ARDOUR::SessionMetadata::Metadata()->set_state (*child, version) ) {
1411                         goto out;
1412                 }
1413         }
1414
1415         if ((child = find_named_node (node, X_("Speakers"))) != 0) {
1416                 _speakers->set_state (*child, version);
1417         }
1418
1419         if ((child = find_named_node (node, "Sources")) == 0) {
1420                 error << _("Session: XML state has no sources section") << endmsg;
1421                 goto out;
1422         } else if (load_sources (*child)) {
1423                 goto out;
1424         }
1425
1426         if ((child = find_named_node (node, "TempoMap")) == 0) {
1427                 error << _("Session: XML state has no Tempo Map section") << endmsg;
1428                 goto out;
1429         } else if (_tempo_map->set_state (*child, version)) {
1430                 goto out;
1431         }
1432
1433         if ((child = find_named_node (node, "Locations")) == 0) {
1434                 error << _("Session: XML state has no locations section") << endmsg;
1435                 goto out;
1436         } else if (_locations->set_state (*child, version)) {
1437                 goto out;
1438         }
1439
1440         locations_changed ();
1441
1442         if (_session_range_location) {
1443                 AudioFileSource::set_header_position_offset (_session_range_location->start());
1444         }
1445
1446         if ((child = find_named_node (node, "Regions")) == 0) {
1447                 error << _("Session: XML state has no Regions section") << endmsg;
1448                 goto out;
1449         } else if (load_regions (*child)) {
1450                 goto out;
1451         }
1452
1453         if ((child = find_named_node (node, "Playlists")) == 0) {
1454                 error << _("Session: XML state has no playlists section") << endmsg;
1455                 goto out;
1456         } else if (playlists->load (*this, *child)) {
1457                 goto out;
1458         }
1459
1460         if ((child = find_named_node (node, "UnusedPlaylists")) == 0) {
1461                 // this is OK
1462         } else if (playlists->load_unused (*this, *child)) {
1463                 goto out;
1464         }
1465
1466         if ((child = find_named_node (node, "CompoundAssociations")) != 0) {
1467                 if (load_compounds (*child)) {
1468                         goto out;
1469                 }
1470         }
1471
1472         if (version >= 3000) {
1473                 if ((child = find_named_node (node, "Bundles")) == 0) {
1474                         warning << _("Session: XML state has no bundles section") << endmsg;
1475                         //goto out;
1476                 } else {
1477                         /* We can't load Bundles yet as they need to be able
1478                            to convert from port names to Port objects, which can't happen until
1479                            later */
1480                         _bundle_xml_node = new XMLNode (*child);
1481                 }
1482         }
1483
1484         if (version < 3000) {
1485                 if ((child = find_named_node (node, X_("DiskStreams"))) == 0) {
1486                         error << _("Session: XML state has no diskstreams section") << endmsg;
1487                         goto out;
1488                 } else if (load_diskstreams_2X (*child, version)) {
1489                         goto out;
1490                 }
1491         }
1492
1493         if ((child = find_named_node (node, VCAManager::xml_node_name)) != 0) {
1494                 _vca_manager->set_state (*child, version);
1495         }
1496
1497         if ((child = find_named_node (node, "Routes")) == 0) {
1498                 error << _("Session: XML state has no routes section") << endmsg;
1499                 goto out;
1500         } else if (load_routes (*child, version)) {
1501                 goto out;
1502         }
1503
1504         /* Now that we have Routes and masters loaded, connect them if appropriate */
1505
1506         Slavable::Assign (_vca_manager); /* EMIT SIGNAL */
1507
1508         /* our diskstreams list is no longer needed as they are now all owned by their Route */
1509         _diskstreams_2X.clear ();
1510
1511         if (version >= 3000) {
1512
1513                 if ((child = find_named_node (node, "RouteGroups")) == 0) {
1514                         error << _("Session: XML state has no route groups section") << endmsg;
1515                         goto out;
1516                 } else if (load_route_groups (*child, version)) {
1517                         goto out;
1518                 }
1519
1520         } else if (version < 3000) {
1521
1522                 if ((child = find_named_node (node, "EditGroups")) == 0) {
1523                         error << _("Session: XML state has no edit groups section") << endmsg;
1524                         goto out;
1525                 } else if (load_route_groups (*child, version)) {
1526                         goto out;
1527                 }
1528
1529                 if ((child = find_named_node (node, "MixGroups")) == 0) {
1530                         error << _("Session: XML state has no mix groups section") << endmsg;
1531                         goto out;
1532                 } else if (load_route_groups (*child, version)) {
1533                         goto out;
1534                 }
1535         }
1536
1537         if ((child = find_named_node (node, "Click")) == 0) {
1538                 warning << _("Session: XML state has no click section") << endmsg;
1539         } else if (_click_io) {
1540                 setup_click_state (&node);
1541         }
1542
1543         if ((child = find_named_node (node, ControlProtocolManager::state_node_name)) != 0) {
1544                 ControlProtocolManager::instance().set_state (*child, version);
1545         }
1546
1547         if ((child = find_named_node (node, "Script"))) {
1548                 for (XMLNodeList::const_iterator n = child->children ().begin (); n != child->children ().end (); ++n) {
1549                         if (!(*n)->is_content ()) { continue; }
1550                         gsize size;
1551                         guchar* buf = g_base64_decode ((*n)->content ().c_str (), &size);
1552                         try {
1553                                 Glib::Threads::Mutex::Lock lm (lua_lock);
1554                                 (*_lua_load)(std::string ((const char*)buf, size));
1555                         } catch (luabridge::LuaException const& e) {
1556                                 cerr << "LuaException:" << e.what () << endl;
1557                         }
1558                         g_free (buf);
1559                 }
1560         }
1561
1562         update_route_record_state ();
1563
1564         /* here beginneth the second phase ... */
1565         set_snapshot_name (_current_snapshot_name);
1566
1567         StateReady (); /* EMIT SIGNAL */
1568
1569         delete state_tree;
1570         state_tree = 0;
1571         return 0;
1572
1573   out:
1574         delete state_tree;
1575         state_tree = 0;
1576         return ret;
1577 }
1578
1579 int
1580 Session::load_routes (const XMLNode& node, int version)
1581 {
1582         XMLNodeList nlist;
1583         XMLNodeConstIterator niter;
1584         RouteList new_routes;
1585
1586         nlist = node.children();
1587
1588         set_dirty();
1589
1590         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1591
1592                 boost::shared_ptr<Route> route;
1593                 if (version < 3000) {
1594                         route = XMLRouteFactory_2X (**niter, version);
1595                 } else {
1596                         route = XMLRouteFactory (**niter, version);
1597                 }
1598
1599                 if (route == 0) {
1600                         error << _("Session: cannot create Route from XML description.") << endmsg;
1601                         return -1;
1602                 }
1603
1604                 BootMessage (string_compose (_("Loaded track/bus %1"), route->name()));
1605
1606                 new_routes.push_back (route);
1607         }
1608
1609         BootMessage (_("Tracks/busses loaded;  Adding to Session"));
1610
1611         add_routes (new_routes, false, false, false, PresentationInfo::max_order);
1612
1613         BootMessage (_("Finished adding tracks/busses"));
1614
1615         return 0;
1616 }
1617
1618 boost::shared_ptr<Route>
1619 Session::XMLRouteFactory (const XMLNode& node, int version)
1620 {
1621         boost::shared_ptr<Route> ret;
1622
1623         if (node.name() != "Route") {
1624                 return ret;
1625         }
1626
1627         XMLNode* ds_child = find_named_node (node, X_("Diskstream"));
1628
1629         DataType type = DataType::AUDIO;
1630         XMLProperty const * prop = node.property("default-type");
1631
1632         if (prop) {
1633                 type = DataType (prop->value());
1634         }
1635
1636         assert (type != DataType::NIL);
1637
1638         if (ds_child) {
1639
1640                 boost::shared_ptr<Track> track;
1641
1642                 if (type == DataType::AUDIO) {
1643                         track.reset (new AudioTrack (*this, X_("toBeResetFroXML")));
1644                 } else {
1645                         track.reset (new MidiTrack (*this, X_("toBeResetFroXML")));
1646                 }
1647
1648                 if (track->init()) {
1649                         return ret;
1650                 }
1651
1652                 if (track->set_state (node, version)) {
1653                         return ret;
1654                 }
1655
1656                 BOOST_MARK_TRACK (track);
1657                 ret = track;
1658
1659         } else {
1660                 PresentationInfo::Flag flags = PresentationInfo::get_flags (node);
1661                 boost::shared_ptr<Route> r (new Route (*this, X_("toBeResetFroXML"), flags));
1662
1663                 if (r->init () == 0 && r->set_state (node, version) == 0) {
1664                         BOOST_MARK_ROUTE (r);
1665                         ret = r;
1666                 }
1667         }
1668
1669         return ret;
1670 }
1671
1672 boost::shared_ptr<Route>
1673 Session::XMLRouteFactory_2X (const XMLNode& node, int version)
1674 {
1675         boost::shared_ptr<Route> ret;
1676
1677         if (node.name() != "Route") {
1678                 return ret;
1679         }
1680
1681         XMLProperty const * ds_prop = node.property (X_("diskstream-id"));
1682         if (!ds_prop) {
1683                 ds_prop = node.property (X_("diskstream"));
1684         }
1685
1686         DataType type = DataType::AUDIO;
1687         XMLProperty const * prop = node.property("default-type");
1688
1689         if (prop) {
1690                 type = DataType (prop->value());
1691         }
1692
1693         assert (type != DataType::NIL);
1694
1695         if (ds_prop) {
1696
1697                 list<boost::shared_ptr<Diskstream> >::iterator i = _diskstreams_2X.begin ();
1698                 while (i != _diskstreams_2X.end() && (*i)->id() != ds_prop->value()) {
1699                         ++i;
1700                 }
1701
1702                 if (i == _diskstreams_2X.end()) {
1703                         error << _("Could not find diskstream for route") << endmsg;
1704                         return boost::shared_ptr<Route> ();
1705                 }
1706
1707                 boost::shared_ptr<Track> track;
1708
1709                 if (type == DataType::AUDIO) {
1710                         track.reset (new AudioTrack (*this, X_("toBeResetFroXML")));
1711                 } else {
1712                         track.reset (new MidiTrack (*this, X_("toBeResetFroXML")));
1713                 }
1714
1715                 if (track->init()) {
1716                         return ret;
1717                 }
1718
1719                 if (track->set_state (node, version)) {
1720                         return ret;
1721                 }
1722
1723                 track->set_diskstream (*i);
1724
1725                 BOOST_MARK_TRACK (track);
1726                 ret = track;
1727
1728         } else {
1729                 PresentationInfo::Flag flags = PresentationInfo::get_flags (node);
1730                 boost::shared_ptr<Route> r (new Route (*this, X_("toBeResetFroXML"), flags));
1731
1732                 if (r->init () == 0 && r->set_state (node, version) == 0) {
1733                         BOOST_MARK_ROUTE (r);
1734                         ret = r;
1735                 }
1736         }
1737
1738         return ret;
1739 }
1740
1741 int
1742 Session::load_regions (const XMLNode& node)
1743 {
1744         XMLNodeList nlist;
1745         XMLNodeConstIterator niter;
1746         boost::shared_ptr<Region> region;
1747
1748         nlist = node.children();
1749
1750         set_dirty();
1751
1752         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1753                 if ((region = XMLRegionFactory (**niter, false)) == 0) {
1754                         error << _("Session: cannot create Region from XML description.");
1755                         XMLProperty const * name = (**niter).property("name");
1756
1757                         if (name) {
1758                                 error << " " << string_compose (_("Can not load state for region '%1'"), name->value());
1759                         }
1760
1761                         error << endmsg;
1762                 }
1763         }
1764
1765         return 0;
1766 }
1767
1768 int
1769 Session::load_compounds (const XMLNode& node)
1770 {
1771         XMLNodeList calist = node.children();
1772         XMLNodeConstIterator caiter;
1773         XMLProperty const * caprop;
1774
1775         for (caiter = calist.begin(); caiter != calist.end(); ++caiter) {
1776                 XMLNode* ca = *caiter;
1777                 ID orig_id;
1778                 ID copy_id;
1779
1780                 if ((caprop = ca->property (X_("original"))) == 0) {
1781                         continue;
1782                 }
1783                 orig_id = caprop->value();
1784
1785                 if ((caprop = ca->property (X_("copy"))) == 0) {
1786                         continue;
1787                 }
1788                 copy_id = caprop->value();
1789
1790                 boost::shared_ptr<Region> orig = RegionFactory::region_by_id (orig_id);
1791                 boost::shared_ptr<Region> copy = RegionFactory::region_by_id (copy_id);
1792
1793                 if (!orig || !copy) {
1794                         warning << string_compose (_("Regions in compound description not found (ID's %1 and %2): ignored"),
1795                                                    orig_id, copy_id)
1796                                 << endmsg;
1797                         continue;
1798                 }
1799
1800                 RegionFactory::add_compound_association (orig, copy);
1801         }
1802
1803         return 0;
1804 }
1805
1806 void
1807 Session::load_nested_sources (const XMLNode& node)
1808 {
1809         XMLNodeList nlist;
1810         XMLNodeConstIterator niter;
1811
1812         nlist = node.children();
1813
1814         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1815                 if ((*niter)->name() == "Source") {
1816
1817                         /* it may already exist, so don't recreate it unnecessarily
1818                          */
1819
1820                         XMLProperty const * prop = (*niter)->property (X_("id"));
1821                         if (!prop) {
1822                                 error << _("Nested source has no ID info in session file! (ignored)") << endmsg;
1823                                 continue;
1824                         }
1825
1826                         ID source_id (prop->value());
1827
1828                         if (!source_by_id (source_id)) {
1829
1830                                 try {
1831                                         SourceFactory::create (*this, **niter, true);
1832                                 }
1833                                 catch (failed_constructor& err) {
1834                                         error << string_compose (_("Cannot reconstruct nested source for region %1"), name()) << endmsg;
1835                                 }
1836                         }
1837                 }
1838         }
1839 }
1840
1841 boost::shared_ptr<Region>
1842 Session::XMLRegionFactory (const XMLNode& node, bool full)
1843 {
1844         XMLProperty const * type = node.property("type");
1845
1846         try {
1847
1848                 const XMLNodeList& nlist = node.children();
1849
1850                 for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter) {
1851                         XMLNode *child = (*niter);
1852                         if (child->name() == "NestedSource") {
1853                                 load_nested_sources (*child);
1854                         }
1855                 }
1856
1857                 if (!type || type->value() == "audio") {
1858                         return boost::shared_ptr<Region>(XMLAudioRegionFactory (node, full));
1859                 } else if (type->value() == "midi") {
1860                         return boost::shared_ptr<Region>(XMLMidiRegionFactory (node, full));
1861                 }
1862
1863         } catch (failed_constructor& err) {
1864                 return boost::shared_ptr<Region> ();
1865         }
1866
1867         return boost::shared_ptr<Region> ();
1868 }
1869
1870 boost::shared_ptr<AudioRegion>
1871 Session::XMLAudioRegionFactory (const XMLNode& node, bool /*full*/)
1872 {
1873         XMLProperty const * prop;
1874         boost::shared_ptr<Source> source;
1875         boost::shared_ptr<AudioSource> as;
1876         SourceList sources;
1877         SourceList master_sources;
1878         uint32_t nchans = 1;
1879         char buf[128];
1880
1881         if (node.name() != X_("Region")) {
1882                 return boost::shared_ptr<AudioRegion>();
1883         }
1884
1885         if ((prop = node.property (X_("channels"))) != 0) {
1886                 nchans = atoi (prop->value().c_str());
1887         }
1888
1889         if ((prop = node.property ("name")) == 0) {
1890                 cerr << "no name for this region\n";
1891                 abort ();
1892         }
1893
1894         if ((prop = node.property (X_("source-0"))) == 0) {
1895                 if ((prop = node.property ("source")) == 0) {
1896                         error << _("Session: XMLNode describing a AudioRegion is incomplete (no source)") << endmsg;
1897                         return boost::shared_ptr<AudioRegion>();
1898                 }
1899         }
1900
1901         PBD::ID s_id (prop->value());
1902
1903         if ((source = source_by_id (s_id)) == 0) {
1904                 error << string_compose(_("Session: XMLNode describing a AudioRegion references an unknown source id =%1"), s_id) << endmsg;
1905                 return boost::shared_ptr<AudioRegion>();
1906         }
1907
1908         as = boost::dynamic_pointer_cast<AudioSource>(source);
1909         if (!as) {
1910                 error << string_compose(_("Session: XMLNode describing a AudioRegion references a non-audio source id =%1"), s_id) << endmsg;
1911                 return boost::shared_ptr<AudioRegion>();
1912         }
1913
1914         sources.push_back (as);
1915
1916         /* pickup other channels */
1917
1918         for (uint32_t n=1; n < nchans; ++n) {
1919                 snprintf (buf, sizeof(buf), X_("source-%d"), n);
1920                 if ((prop = node.property (buf)) != 0) {
1921
1922                         PBD::ID id2 (prop->value());
1923
1924                         if ((source = source_by_id (id2)) == 0) {
1925                                 error << string_compose(_("Session: XMLNode describing a AudioRegion references an unknown source id =%1"), id2) << endmsg;
1926                                 return boost::shared_ptr<AudioRegion>();
1927                         }
1928
1929                         as = boost::dynamic_pointer_cast<AudioSource>(source);
1930                         if (!as) {
1931                                 error << string_compose(_("Session: XMLNode describing a AudioRegion references a non-audio source id =%1"), id2) << endmsg;
1932                                 return boost::shared_ptr<AudioRegion>();
1933                         }
1934                         sources.push_back (as);
1935                 }
1936         }
1937
1938         for (uint32_t n = 0; n < nchans; ++n) {
1939                 snprintf (buf, sizeof(buf), X_("master-source-%d"), n);
1940                 if ((prop = node.property (buf)) != 0) {
1941
1942                         PBD::ID id2 (prop->value());
1943
1944                         if ((source = source_by_id (id2)) == 0) {
1945                                 error << string_compose(_("Session: XMLNode describing a AudioRegion references an unknown source id =%1"), id2) << endmsg;
1946                                 return boost::shared_ptr<AudioRegion>();
1947                         }
1948
1949                         as = boost::dynamic_pointer_cast<AudioSource>(source);
1950                         if (!as) {
1951                                 error << string_compose(_("Session: XMLNode describing a AudioRegion references a non-audio source id =%1"), id2) << endmsg;
1952                                 return boost::shared_ptr<AudioRegion>();
1953                         }
1954                         master_sources.push_back (as);
1955                 }
1956         }
1957
1958         try {
1959                 boost::shared_ptr<AudioRegion> region (boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (sources, node)));
1960
1961                 /* a final detail: this is the one and only place that we know how long missing files are */
1962
1963                 if (region->whole_file()) {
1964                         for (SourceList::iterator sx = sources.begin(); sx != sources.end(); ++sx) {
1965                                 boost::shared_ptr<SilentFileSource> sfp = boost::dynamic_pointer_cast<SilentFileSource> (*sx);
1966                                 if (sfp) {
1967                                         sfp->set_length (region->length());
1968                                 }
1969                         }
1970                 }
1971
1972                 if (!master_sources.empty()) {
1973                         if (master_sources.size() != nchans) {
1974                                 error << _("Session: XMLNode describing an AudioRegion is missing some master sources; ignored") << endmsg;
1975                         } else {
1976                                 region->set_master_sources (master_sources);
1977                         }
1978                 }
1979
1980                 return region;
1981
1982         }
1983
1984         catch (failed_constructor& err) {
1985                 return boost::shared_ptr<AudioRegion>();
1986         }
1987 }
1988
1989 boost::shared_ptr<MidiRegion>
1990 Session::XMLMidiRegionFactory (const XMLNode& node, bool /*full*/)
1991 {
1992         XMLProperty const * prop;
1993         boost::shared_ptr<Source> source;
1994         boost::shared_ptr<MidiSource> ms;
1995         SourceList sources;
1996
1997         if (node.name() != X_("Region")) {
1998                 return boost::shared_ptr<MidiRegion>();
1999         }
2000
2001         if ((prop = node.property ("name")) == 0) {
2002                 cerr << "no name for this region\n";
2003                 abort ();
2004         }
2005
2006         if ((prop = node.property (X_("source-0"))) == 0) {
2007                 if ((prop = node.property ("source")) == 0) {
2008                         error << _("Session: XMLNode describing a MidiRegion is incomplete (no source)") << endmsg;
2009                         return boost::shared_ptr<MidiRegion>();
2010                 }
2011         }
2012
2013         PBD::ID s_id (prop->value());
2014
2015         if ((source = source_by_id (s_id)) == 0) {
2016                 error << string_compose(_("Session: XMLNode describing a MidiRegion references an unknown source id =%1"), s_id) << endmsg;
2017                 return boost::shared_ptr<MidiRegion>();
2018         }
2019
2020         ms = boost::dynamic_pointer_cast<MidiSource>(source);
2021         if (!ms) {
2022                 error << string_compose(_("Session: XMLNode describing a MidiRegion references a non-midi source id =%1"), s_id) << endmsg;
2023                 return boost::shared_ptr<MidiRegion>();
2024         }
2025
2026         sources.push_back (ms);
2027
2028         try {
2029                 boost::shared_ptr<MidiRegion> region (boost::dynamic_pointer_cast<MidiRegion> (RegionFactory::create (sources, node)));
2030                 /* a final detail: this is the one and only place that we know how long missing files are */
2031
2032                 if (region->whole_file()) {
2033                         for (SourceList::iterator sx = sources.begin(); sx != sources.end(); ++sx) {
2034                                 boost::shared_ptr<SilentFileSource> sfp = boost::dynamic_pointer_cast<SilentFileSource> (*sx);
2035                                 if (sfp) {
2036                                         sfp->set_length (region->length());
2037                                 }
2038                         }
2039                 }
2040
2041                 return region;
2042         }
2043
2044         catch (failed_constructor& err) {
2045                 return boost::shared_ptr<MidiRegion>();
2046         }
2047 }
2048
2049 XMLNode&
2050 Session::get_sources_as_xml ()
2051
2052 {
2053         XMLNode* node = new XMLNode (X_("Sources"));
2054         Glib::Threads::Mutex::Lock lm (source_lock);
2055
2056         for (SourceMap::iterator i = sources.begin(); i != sources.end(); ++i) {
2057                 node->add_child_nocopy (i->second->get_state());
2058         }
2059
2060         return *node;
2061 }
2062
2063 void
2064 Session::reset_write_sources (bool mark_write_complete, bool force)
2065 {
2066         boost::shared_ptr<RouteList> rl = routes.reader();
2067         for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
2068                 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
2069                 if (tr) {
2070                         _state_of_the_state = StateOfTheState (_state_of_the_state|InCleanup);
2071                         tr->reset_write_sources(mark_write_complete, force);
2072                         _state_of_the_state = StateOfTheState (_state_of_the_state & ~InCleanup);
2073                 }
2074         }
2075 }
2076
2077 int
2078 Session::load_sources (const XMLNode& node)
2079 {
2080         XMLNodeList nlist;
2081         XMLNodeConstIterator niter;
2082         boost::shared_ptr<Source> source; /* don't need this but it stops some
2083                                            * versions of gcc complaining about
2084                                            * discarded return values.
2085                                            */
2086
2087         nlist = node.children();
2088
2089         set_dirty();
2090
2091         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2092           retry:
2093                 try {
2094                         if ((source = XMLSourceFactory (**niter)) == 0) {
2095                                 error << _("Session: cannot create Source from XML description.") << endmsg;
2096                         }
2097
2098                 } catch (MissingSource& err) {
2099
2100                         int user_choice;
2101
2102                         if (err.type == DataType::MIDI && Glib::path_is_absolute (err.path)) {
2103                                 error << string_compose (_("A external MIDI file is missing. %1 cannot currently recover from missing external MIDI files"),
2104                                                          PROGRAM_NAME) << endmsg;
2105                                 return -1;
2106                         }
2107
2108                         if (!no_questions_about_missing_files) {
2109                                 user_choice = MissingFile (this, err.path, err.type).get_value_or (-1);
2110                         } else {
2111                                 user_choice = -2;
2112                         }
2113
2114                         switch (user_choice) {
2115                         case 0:
2116                                 /* user added a new search location, so try again */
2117                                 goto retry;
2118
2119
2120                         case 1:
2121                                 /* user asked to quit the entire session load
2122                                  */
2123                                 return -1;
2124
2125                         case 2:
2126                                 no_questions_about_missing_files = true;
2127                                 goto retry;
2128
2129                         case 3:
2130                                 no_questions_about_missing_files = true;
2131                                 /* fallthru */
2132
2133                         case -1:
2134                         default:
2135                                 switch (err.type) {
2136
2137                                 case DataType::AUDIO:
2138                                         source = SourceFactory::createSilent (*this, **niter, max_framecnt, _current_frame_rate);
2139                                         break;
2140
2141                                 case DataType::MIDI:
2142                                         /* The MIDI file is actually missing so
2143                                          * just create a new one in the same
2144                                          * location. Do not announce its
2145                                          */
2146                                         string fullpath;
2147
2148                                         if (!Glib::path_is_absolute (err.path)) {
2149                                                 fullpath = Glib::build_filename (source_search_path (DataType::MIDI).front(), err.path);
2150                                         } else {
2151                                                 /* this should be an unrecoverable error: we would be creating a MIDI file outside
2152                                                    the session tree.
2153                                                 */
2154                                                 return -1;
2155                                         }
2156                                         /* Note that we do not announce the source just yet - we need to reset its ID before we do that */
2157                                         source = SourceFactory::createWritable (DataType::MIDI, *this, fullpath, false, _current_frame_rate, false, false);
2158                                         /* reset ID to match the missing one */
2159                                         source->set_id (**niter);
2160                                         /* Now we can announce it */
2161                                         SourceFactory::SourceCreated (source);
2162                                         break;
2163                                 }
2164                                 break;
2165                         }
2166                 }
2167         }
2168
2169         return 0;
2170 }
2171
2172 boost::shared_ptr<Source>
2173 Session::XMLSourceFactory (const XMLNode& node)
2174 {
2175         if (node.name() != "Source") {
2176                 return boost::shared_ptr<Source>();
2177         }
2178
2179         try {
2180                 /* note: do peak building in another thread when loading session state */
2181                 return SourceFactory::create (*this, node, true);
2182         }
2183
2184         catch (failed_constructor& err) {
2185                 error << string_compose (_("Found a sound file that cannot be used by %1. Talk to the programmers."), PROGRAM_NAME) << endmsg;
2186                 return boost::shared_ptr<Source>();
2187         }
2188 }
2189
2190 int
2191 Session::save_template (string template_name, bool replace_existing)
2192 {
2193         if ((_state_of_the_state & CannotSave) || template_name.empty ()) {
2194                 return -1;
2195         }
2196
2197         bool absolute_path = Glib::path_is_absolute (template_name);
2198
2199         /* directory to put the template in */
2200         std::string template_dir_path;
2201
2202         if (!absolute_path) {
2203                 std::string user_template_dir(user_template_directory());
2204
2205                 if (g_mkdir_with_parents (user_template_dir.c_str(), 0755) != 0) {
2206                         error << string_compose(_("Could not create templates directory \"%1\" (%2)"),
2207                                         user_template_dir, g_strerror (errno)) << endmsg;
2208                         return -1;
2209                 }
2210
2211                 template_dir_path = Glib::build_filename (user_template_dir, template_name);
2212         } else {
2213                 template_dir_path = template_name;
2214         }
2215
2216         if (!ARDOUR::Profile->get_trx()) {
2217                 if (!replace_existing && Glib::file_test (template_dir_path, Glib::FILE_TEST_EXISTS)) {
2218                         warning << string_compose(_("Template \"%1\" already exists - new version not created"),
2219                                                                           template_dir_path) << endmsg;
2220                         return -2;
2221                 }
2222
2223                 if (g_mkdir_with_parents (template_dir_path.c_str(), 0755) != 0) {
2224                         error << string_compose(_("Could not create directory for Session template\"%1\" (%2)"),
2225                                                                         template_dir_path, g_strerror (errno)) << endmsg;
2226                         return -1;
2227                 }
2228         }
2229
2230         /* file to write */
2231         std::string template_file_path;
2232
2233         if (ARDOUR::Profile->get_trx()) {
2234                 template_file_path = template_name;
2235         } else {
2236                 if (absolute_path) {
2237                         template_file_path = Glib::build_filename (template_dir_path, Glib::path_get_basename (template_dir_path) + template_suffix);
2238                 } else {
2239                         template_file_path = Glib::build_filename (template_dir_path, template_name + template_suffix);
2240                 }
2241         }
2242
2243         SessionSaveUnderway (); /* EMIT SIGNAL */
2244
2245         XMLTree tree;
2246
2247         {
2248                 PBD::Unwinder<std::string> uw (_template_state_dir, template_dir_path);
2249                 tree.set_root (&get_template());
2250         }
2251
2252         if (!tree.write (template_file_path)) {
2253                 error << _("template not saved") << endmsg;
2254                 return -1;
2255         }
2256
2257         store_recent_templates (template_file_path);
2258
2259         return 0;
2260 }
2261
2262 void
2263 Session::refresh_disk_space ()
2264 {
2265 #if __APPLE__ || __FreeBSD__ || (HAVE_SYS_VFS_H && HAVE_SYS_STATVFS_H)
2266
2267         Glib::Threads::Mutex::Lock lm (space_lock);
2268
2269         /* get freespace on every FS that is part of the session path */
2270
2271         _total_free_4k_blocks = 0;
2272         _total_free_4k_blocks_uncertain = false;
2273
2274         for (vector<space_and_path>::iterator i = session_dirs.begin(); i != session_dirs.end(); ++i) {
2275
2276                 struct statfs statfsbuf;
2277                 statfs (i->path.c_str(), &statfsbuf);
2278
2279                 double const scale = statfsbuf.f_bsize / 4096.0;
2280
2281                 /* See if this filesystem is read-only */
2282                 struct statvfs statvfsbuf;
2283                 statvfs (i->path.c_str(), &statvfsbuf);
2284
2285                 /* f_bavail can be 0 if it is undefined for whatever
2286                    filesystem we are looking at; Samba shares mounted
2287                    via GVFS are an example of this.
2288                 */
2289                 if (statfsbuf.f_bavail == 0) {
2290                         /* block count unknown */
2291                         i->blocks = 0;
2292                         i->blocks_unknown = true;
2293                 } else if (statvfsbuf.f_flag & ST_RDONLY) {
2294                         /* read-only filesystem */
2295                         i->blocks = 0;
2296                         i->blocks_unknown = false;
2297                 } else {
2298                         /* read/write filesystem with known space */
2299                         i->blocks = (uint32_t) floor (statfsbuf.f_bavail * scale);
2300                         i->blocks_unknown = false;
2301                 }
2302
2303                 _total_free_4k_blocks += i->blocks;
2304                 if (i->blocks_unknown) {
2305                         _total_free_4k_blocks_uncertain = true;
2306                 }
2307         }
2308 #elif defined PLATFORM_WINDOWS
2309         vector<string> scanned_volumes;
2310         vector<string>::iterator j;
2311         vector<space_and_path>::iterator i;
2312     DWORD nSectorsPerCluster, nBytesPerSector,
2313           nFreeClusters, nTotalClusters;
2314     char disk_drive[4];
2315         bool volume_found;
2316
2317         _total_free_4k_blocks = 0;
2318
2319         for (i = session_dirs.begin(); i != session_dirs.end(); i++) {
2320                 strncpy (disk_drive, (*i).path.c_str(), 3);
2321                 disk_drive[3] = 0;
2322                 strupr(disk_drive);
2323
2324                 volume_found = false;
2325                 if (0 != (GetDiskFreeSpace(disk_drive, &nSectorsPerCluster, &nBytesPerSector, &nFreeClusters, &nTotalClusters)))
2326                 {
2327                         int64_t nBytesPerCluster = nBytesPerSector * nSectorsPerCluster;
2328                         int64_t nFreeBytes = nBytesPerCluster * (int64_t)nFreeClusters;
2329                         i->blocks = (uint32_t)(nFreeBytes / 4096);
2330
2331                         for (j = scanned_volumes.begin(); j != scanned_volumes.end(); j++) {
2332                                 if (0 == j->compare(disk_drive)) {
2333                                         volume_found = true;
2334                                         break;
2335                                 }
2336                         }
2337
2338                         if (!volume_found) {
2339                                 scanned_volumes.push_back(disk_drive);
2340                                 _total_free_4k_blocks += i->blocks;
2341                         }
2342                 }
2343         }
2344
2345         if (0 == _total_free_4k_blocks) {
2346                 strncpy (disk_drive, path().c_str(), 3);
2347                 disk_drive[3] = 0;
2348
2349                 if (0 != (GetDiskFreeSpace(disk_drive, &nSectorsPerCluster, &nBytesPerSector, &nFreeClusters, &nTotalClusters)))
2350                 {
2351                         int64_t nBytesPerCluster = nBytesPerSector * nSectorsPerCluster;
2352                         int64_t nFreeBytes = nBytesPerCluster * (int64_t)nFreeClusters;
2353                         _total_free_4k_blocks = (uint32_t)(nFreeBytes / 4096);
2354                 }
2355         }
2356 #endif
2357 }
2358
2359 string
2360 Session::get_best_session_directory_for_new_audio ()
2361 {
2362         vector<space_and_path>::iterator i;
2363         string result = _session_dir->root_path();
2364
2365         /* handle common case without system calls */
2366
2367         if (session_dirs.size() == 1) {
2368                 return result;
2369         }
2370
2371         /* OK, here's the algorithm we're following here:
2372
2373         We want to select which directory to use for
2374         the next file source to be created. Ideally,
2375         we'd like to use a round-robin process so as to
2376         get maximum performance benefits from splitting
2377         the files across multiple disks.
2378
2379         However, in situations without much diskspace, an
2380         RR approach may end up filling up a filesystem
2381         with new files while others still have space.
2382         Its therefore important to pay some attention to
2383         the freespace in the filesystem holding each
2384         directory as well. However, if we did that by
2385         itself, we'd keep creating new files in the file
2386         system with the most space until it was as full
2387         as all others, thus negating any performance
2388         benefits of this RAID-1 like approach.
2389
2390         So, we use a user-configurable space threshold. If
2391         there are at least 2 filesystems with more than this
2392         much space available, we use RR selection between them.
2393         If not, then we pick the filesystem with the most space.
2394
2395         This gets a good balance between the two
2396         approaches.
2397         */
2398
2399         refresh_disk_space ();
2400
2401         int free_enough = 0;
2402
2403         for (i = session_dirs.begin(); i != session_dirs.end(); ++i) {
2404                 if ((*i).blocks * 4096 >= Config->get_disk_choice_space_threshold()) {
2405                         free_enough++;
2406                 }
2407         }
2408
2409         if (free_enough >= 2) {
2410                 /* use RR selection process, ensuring that the one
2411                    picked works OK.
2412                 */
2413
2414                 i = last_rr_session_dir;
2415
2416                 do {
2417                         if (++i == session_dirs.end()) {
2418                                 i = session_dirs.begin();
2419                         }
2420
2421                         if ((*i).blocks * 4096 >= Config->get_disk_choice_space_threshold()) {
2422                                 SessionDirectory sdir(i->path);
2423                                 if (sdir.create ()) {
2424                                         result = (*i).path;
2425                                         last_rr_session_dir = i;
2426                                         return result;
2427                                 }
2428                         }
2429
2430                 } while (i != last_rr_session_dir);
2431
2432         } else {
2433
2434                 /* pick FS with the most freespace (and that
2435                    seems to actually work ...)
2436                 */
2437
2438                 vector<space_and_path> sorted;
2439                 space_and_path_ascending_cmp cmp;
2440
2441                 sorted = session_dirs;
2442                 sort (sorted.begin(), sorted.end(), cmp);
2443
2444                 for (i = sorted.begin(); i != sorted.end(); ++i) {
2445                         SessionDirectory sdir(i->path);
2446                         if (sdir.create ()) {
2447                                 result = (*i).path;
2448                                 last_rr_session_dir = i;
2449                                 return result;
2450                         }
2451                 }
2452         }
2453
2454         return result;
2455 }
2456
2457 string
2458 Session::automation_dir () const
2459 {
2460         return Glib::build_filename (_path, automation_dir_name);
2461 }
2462
2463 string
2464 Session::analysis_dir () const
2465 {
2466         return Glib::build_filename (_path, analysis_dir_name);
2467 }
2468
2469 string
2470 Session::plugins_dir () const
2471 {
2472         return Glib::build_filename (_path, plugins_dir_name);
2473 }
2474
2475 string
2476 Session::externals_dir () const
2477 {
2478         return Glib::build_filename (_path, externals_dir_name);
2479 }
2480
2481 int
2482 Session::load_bundles (XMLNode const & node)
2483 {
2484         XMLNodeList nlist = node.children();
2485         XMLNodeConstIterator niter;
2486
2487         set_dirty();
2488
2489         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2490                 if ((*niter)->name() == "InputBundle") {
2491                         add_bundle (boost::shared_ptr<UserBundle> (new UserBundle (**niter, true)));
2492                 } else if ((*niter)->name() == "OutputBundle") {
2493                         add_bundle (boost::shared_ptr<UserBundle> (new UserBundle (**niter, false)));
2494                 } else {
2495                         error << string_compose(_("Unknown node \"%1\" found in Bundles list from session file"), (*niter)->name()) << endmsg;
2496                         return -1;
2497                 }
2498         }
2499
2500         return 0;
2501 }
2502
2503 int
2504 Session::load_route_groups (const XMLNode& node, int version)
2505 {
2506         XMLNodeList nlist = node.children();
2507         XMLNodeConstIterator niter;
2508
2509         set_dirty ();
2510
2511         if (version >= 3000) {
2512
2513                 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2514                         if ((*niter)->name() == "RouteGroup") {
2515                                 RouteGroup* rg = new RouteGroup (*this, "");
2516                                 add_route_group (rg);
2517                                 rg->set_state (**niter, version);
2518                         }
2519                 }
2520
2521         } else if (version < 3000) {
2522
2523                 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2524                         if ((*niter)->name() == "EditGroup" || (*niter)->name() == "MixGroup") {
2525                                 RouteGroup* rg = new RouteGroup (*this, "");
2526                                 add_route_group (rg);
2527                                 rg->set_state (**niter, version);
2528                         }
2529                 }
2530         }
2531
2532         return 0;
2533 }
2534
2535 static bool
2536 state_file_filter (const string &str, void* /*arg*/)
2537 {
2538         return (str.length() > strlen(statefile_suffix) &&
2539                 str.find (statefile_suffix) == (str.length() - strlen (statefile_suffix)));
2540 }
2541
2542 static string
2543 remove_end(string state)
2544 {
2545         string statename(state);
2546
2547         string::size_type start,end;
2548         if ((start = statename.find_last_of (G_DIR_SEPARATOR)) != string::npos) {
2549                 statename = statename.substr (start+1);
2550         }
2551
2552         if ((end = statename.rfind(statefile_suffix)) == string::npos) {
2553                 end = statename.length();
2554         }
2555
2556         return string(statename.substr (0, end));
2557 }
2558
2559 vector<string>
2560 Session::possible_states (string path)
2561 {
2562         vector<string> states;
2563         find_files_matching_filter (states, path, state_file_filter, 0, false, false);
2564
2565         transform(states.begin(), states.end(), states.begin(), remove_end);
2566
2567         sort (states.begin(), states.end());
2568
2569         return states;
2570 }
2571
2572 vector<string>
2573 Session::possible_states () const
2574 {
2575         return possible_states(_path);
2576 }
2577
2578 void
2579 Session::add_route_group (RouteGroup* g)
2580 {
2581         _route_groups.push_back (g);
2582         route_group_added (g); /* EMIT SIGNAL */
2583
2584         g->RouteAdded.connect_same_thread (*this, boost::bind (&Session::route_added_to_route_group, this, _1, _2));
2585         g->RouteRemoved.connect_same_thread (*this, boost::bind (&Session::route_removed_from_route_group, this, _1, _2));
2586         g->PropertyChanged.connect_same_thread (*this, boost::bind (&Session::route_group_property_changed, this, g));
2587
2588         set_dirty ();
2589 }
2590
2591 void
2592 Session::remove_route_group (RouteGroup& rg)
2593 {
2594         list<RouteGroup*>::iterator i;
2595
2596         if ((i = find (_route_groups.begin(), _route_groups.end(), &rg)) != _route_groups.end()) {
2597                 _route_groups.erase (i);
2598                 delete &rg;
2599
2600                 route_group_removed (); /* EMIT SIGNAL */
2601         }
2602 }
2603
2604 /** Set a new order for our route groups, without adding or removing any.
2605  *  @param groups Route group list in the new order.
2606  */
2607 void
2608 Session::reorder_route_groups (list<RouteGroup*> groups)
2609 {
2610         _route_groups = groups;
2611
2612         route_groups_reordered (); /* EMIT SIGNAL */
2613         set_dirty ();
2614 }
2615
2616
2617 RouteGroup *
2618 Session::route_group_by_name (string name)
2619 {
2620         list<RouteGroup *>::iterator i;
2621
2622         for (i = _route_groups.begin(); i != _route_groups.end(); ++i) {
2623                 if ((*i)->name() == name) {
2624                         return* i;
2625                 }
2626         }
2627         return 0;
2628 }
2629
2630 RouteGroup&
2631 Session::all_route_group() const
2632 {
2633         return *_all_route_group;
2634 }
2635
2636 void
2637 Session::add_commands (vector<Command*> const & cmds)
2638 {
2639         for (vector<Command*>::const_iterator i = cmds.begin(); i != cmds.end(); ++i) {
2640                 add_command (*i);
2641         }
2642 }
2643
2644 void
2645 Session::add_command (Command* const cmd)
2646 {
2647         assert (_current_trans);
2648         DEBUG_UNDO_HISTORY (
2649             string_compose ("Current Undo Transaction %1, adding command: %2",
2650                             _current_trans->name (),
2651                             cmd->name ()));
2652         _current_trans->add_command (cmd);
2653 }
2654
2655 PBD::StatefulDiffCommand*
2656 Session::add_stateful_diff_command (boost::shared_ptr<PBD::StatefulDestructible> sfd)
2657 {
2658         PBD::StatefulDiffCommand* cmd = new PBD::StatefulDiffCommand (sfd);
2659         add_command (cmd);
2660         return cmd;
2661 }
2662
2663 void
2664 Session::begin_reversible_command (const string& name)
2665 {
2666         begin_reversible_command (g_quark_from_string (name.c_str ()));
2667 }
2668
2669 /** Begin a reversible command using a GQuark to identify it.
2670  *  begin_reversible_command() and commit_reversible_command() calls may be nested,
2671  *  but there must be as many begin...()s as there are commit...()s.
2672  */
2673 void
2674 Session::begin_reversible_command (GQuark q)
2675 {
2676         /* If nested begin/commit pairs are used, we create just one UndoTransaction
2677            to hold all the commands that are committed.  This keeps the order of
2678            commands correct in the history.
2679         */
2680
2681         if (_current_trans == 0) {
2682                 DEBUG_UNDO_HISTORY (string_compose (
2683                     "Begin Reversible Command, new transaction: %1", g_quark_to_string (q)));
2684
2685                 /* start a new transaction */
2686                 assert (_current_trans_quarks.empty ());
2687                 _current_trans = new UndoTransaction();
2688                 _current_trans->set_name (g_quark_to_string (q));
2689         } else {
2690                 DEBUG_UNDO_HISTORY (
2691                     string_compose ("Begin Reversible Command, current transaction: %1",
2692                                     _current_trans->name ()));
2693         }
2694
2695         _current_trans_quarks.push_front (q);
2696 }
2697
2698 void
2699 Session::abort_reversible_command ()
2700 {
2701         if (_current_trans != 0) {
2702                 DEBUG_UNDO_HISTORY (
2703                     string_compose ("Abort Reversible Command: %1", _current_trans->name ()));
2704                 _current_trans->clear();
2705                 delete _current_trans;
2706                 _current_trans = 0;
2707                 _current_trans_quarks.clear();
2708         }
2709 }
2710
2711 void
2712 Session::commit_reversible_command (Command *cmd)
2713 {
2714         assert (_current_trans);
2715         assert (!_current_trans_quarks.empty ());
2716
2717         struct timeval now;
2718
2719         if (cmd) {
2720                 DEBUG_UNDO_HISTORY (
2721                     string_compose ("Current Undo Transaction %1, adding command: %2",
2722                                     _current_trans->name (),
2723                                     cmd->name ()));
2724                 _current_trans->add_command (cmd);
2725         }
2726
2727         DEBUG_UNDO_HISTORY (
2728             string_compose ("Commit Reversible Command, current transaction: %1",
2729                             _current_trans->name ()));
2730
2731         _current_trans_quarks.pop_front ();
2732
2733         if (!_current_trans_quarks.empty ()) {
2734                 DEBUG_UNDO_HISTORY (
2735                     string_compose ("Commit Reversible Command, transaction is not "
2736                                     "top-level, current transaction: %1",
2737                                     _current_trans->name ()));
2738                 /* the transaction we're committing is not the top-level one */
2739                 return;
2740         }
2741
2742         if (_current_trans->empty()) {
2743                 /* no commands were added to the transaction, so just get rid of it */
2744                 DEBUG_UNDO_HISTORY (
2745                     string_compose ("Commit Reversible Command, No commands were "
2746                                     "added to current transaction: %1",
2747                                     _current_trans->name ()));
2748                 delete _current_trans;
2749                 _current_trans = 0;
2750                 return;
2751         }
2752
2753         gettimeofday (&now, 0);
2754         _current_trans->set_timestamp (now);
2755
2756         _history.add (_current_trans);
2757         _current_trans = 0;
2758 }
2759
2760 static bool
2761 accept_all_audio_files (const string& path, void* /*arg*/)
2762 {
2763         if (!Glib::file_test (path, Glib::FILE_TEST_IS_REGULAR)) {
2764                 return false;
2765         }
2766
2767         if (!AudioFileSource::safe_audio_file_extension (path)) {
2768                 return false;
2769         }
2770
2771         return true;
2772 }
2773
2774 static bool
2775 accept_all_midi_files (const string& path, void* /*arg*/)
2776 {
2777         if (!Glib::file_test (path, Glib::FILE_TEST_IS_REGULAR)) {
2778                 return false;
2779         }
2780
2781         return ((path.length() > 4 && path.find (".mid") != (path.length() - 4)) ||
2782                 (path.length() > 4 && path.find (".smf") != (path.length() - 4)) ||
2783                 (path.length() > 5 && path.find (".midi") != (path.length() - 5)));
2784 }
2785
2786 static bool
2787 accept_all_state_files (const string& path, void* /*arg*/)
2788 {
2789         if (!Glib::file_test (path, Glib::FILE_TEST_IS_REGULAR)) {
2790                 return false;
2791         }
2792
2793         std::string const statefile_ext (statefile_suffix);
2794         if (path.length() >= statefile_ext.length()) {
2795                 return (0 == path.compare (path.length() - statefile_ext.length(), statefile_ext.length(), statefile_ext));
2796         } else {
2797                 return false;
2798         }
2799 }
2800
2801 int
2802 Session::find_all_sources (string path, set<string>& result)
2803 {
2804         XMLTree tree;
2805         XMLNode* node;
2806
2807         if (!tree.read (path)) {
2808                 return -1;
2809         }
2810
2811         if ((node = find_named_node (*tree.root(), "Sources")) == 0) {
2812                 return -2;
2813         }
2814
2815         XMLNodeList nlist;
2816         XMLNodeConstIterator niter;
2817
2818         nlist = node->children();
2819
2820         set_dirty();
2821
2822         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2823
2824                 XMLProperty const * prop;
2825
2826                 if ((prop = (*niter)->property (X_("type"))) == 0) {
2827                         continue;
2828                 }
2829
2830                 DataType type (prop->value());
2831
2832                 if ((prop = (*niter)->property (X_("name"))) == 0) {
2833                         continue;
2834                 }
2835
2836                 if (Glib::path_is_absolute (prop->value())) {
2837                         /* external file, ignore */
2838                         continue;
2839                 }
2840
2841                 string found_path;
2842                 bool is_new;
2843                 uint16_t chan;
2844
2845                 if (FileSource::find (*this, type, prop->value(), true, is_new, chan, found_path)) {
2846                         result.insert (found_path);
2847                 }
2848         }
2849
2850         return 0;
2851 }
2852
2853 int
2854 Session::find_all_sources_across_snapshots (set<string>& result, bool exclude_this_snapshot)
2855 {
2856         vector<string> state_files;
2857         string ripped;
2858         string this_snapshot_path;
2859
2860         result.clear ();
2861
2862         ripped = _path;
2863
2864         if (ripped[ripped.length()-1] == G_DIR_SEPARATOR) {
2865                 ripped = ripped.substr (0, ripped.length() - 1);
2866         }
2867
2868         find_files_matching_filter (state_files, ripped, accept_all_state_files, (void *) 0, true, true);
2869
2870         if (state_files.empty()) {
2871                 /* impossible! */
2872                 return 0;
2873         }
2874
2875         this_snapshot_path = Glib::build_filename (_path, legalize_for_path (_current_snapshot_name));
2876         this_snapshot_path += statefile_suffix;
2877
2878         for (vector<string>::iterator i = state_files.begin(); i != state_files.end(); ++i) {
2879
2880                 cerr << "Looking at snapshot " << (*i) << " ( with this = [" << this_snapshot_path << "])\n";
2881
2882                 if (exclude_this_snapshot && *i == this_snapshot_path) {
2883                         cerr << "\texcluded\n";
2884                         continue;
2885
2886                 }
2887
2888                 if (find_all_sources (*i, result) < 0) {
2889                         return -1;
2890                 }
2891         }
2892
2893         return 0;
2894 }
2895
2896 struct RegionCounter {
2897     typedef std::map<PBD::ID,boost::shared_ptr<AudioSource> > AudioSourceList;
2898     AudioSourceList::iterator iter;
2899     boost::shared_ptr<Region> region;
2900     uint32_t count;
2901
2902     RegionCounter() : count (0) {}
2903 };
2904
2905 int
2906 Session::ask_about_playlist_deletion (boost::shared_ptr<Playlist> p)
2907 {
2908         boost::optional<int> r = AskAboutPlaylistDeletion (p);
2909         return r.get_value_or (1);
2910 }
2911
2912 void
2913 Session::cleanup_regions ()
2914 {
2915         bool removed = false;
2916         const RegionFactory::RegionMap& regions (RegionFactory::regions());
2917
2918         for (RegionFactory::RegionMap::const_iterator i = regions.begin(); i != regions.end();) {
2919
2920                 uint32_t used = playlists->region_use_count (i->second);
2921
2922                 if (used == 0 && !i->second->automatic ()) {
2923                         boost::weak_ptr<Region> w = i->second;
2924                         ++i;
2925                         removed = true;
2926                         RegionFactory::map_remove (w);
2927                 } else {
2928                         ++i;
2929                 }
2930         }
2931
2932         if (removed) {
2933                 // re-check to remove parent references of compound regions
2934                 for (RegionFactory::RegionMap::const_iterator i = regions.begin(); i != regions.end();) {
2935                         if (!(i->second->whole_file() && i->second->max_source_level() > 0)) {
2936                                 ++i;
2937                                 continue;
2938                         }
2939                         assert(boost::dynamic_pointer_cast<PlaylistSource>(i->second->source (0)) != 0);
2940                         if (0 == playlists->region_use_count (i->second)) {
2941                                 boost::weak_ptr<Region> w = i->second;
2942                                 ++i;
2943                                 RegionFactory::map_remove (w);
2944                         } else {
2945                                 ++i;
2946                         }
2947                 }
2948         }
2949
2950         /* dump the history list */
2951         _history.clear ();
2952
2953         save_state ("");
2954 }
2955
2956 bool
2957 Session::can_cleanup_peakfiles () const
2958 {
2959         if (deletion_in_progress()) {
2960                 return false;
2961         }
2962         if (!_writable || (_state_of_the_state & CannotSave)) {
2963                 warning << _("Cannot cleanup peak-files for read-only session.") << endmsg;
2964                 return false;
2965         }
2966         if (record_status() == Recording) {
2967                 error << _("Cannot cleanup peak-files while recording") << endmsg;
2968                 return false;
2969         }
2970         return true;
2971 }
2972
2973 int
2974 Session::cleanup_peakfiles ()
2975 {
2976         Glib::Threads::Mutex::Lock lm (peak_cleanup_lock, Glib::Threads::TRY_LOCK);
2977         if (!lm.locked()) {
2978                 return -1;
2979         }
2980
2981         assert (can_cleanup_peakfiles ());
2982         assert (!peaks_cleanup_in_progres());
2983
2984         _state_of_the_state = StateOfTheState (_state_of_the_state | PeakCleanup);
2985
2986         int timeout = 5000; // 5 seconds
2987         while (!SourceFactory::files_with_peaks.empty()) {
2988                 Glib::usleep (1000);
2989                 if (--timeout < 0) {
2990                         warning << _("Timeout waiting for peak-file creation to terminate before cleanup, please try again later.") << endmsg;
2991                         _state_of_the_state = StateOfTheState (_state_of_the_state & (~PeakCleanup));
2992                         return -1;
2993                 }
2994         }
2995
2996         for (SourceMap::iterator i = sources.begin(); i != sources.end(); ++i) {
2997                 boost::shared_ptr<AudioSource> as;
2998                 if ((as = boost::dynamic_pointer_cast<AudioSource> (i->second)) != 0) {
2999                         as->close_peakfile();
3000                 }
3001         }
3002
3003         PBD::clear_directory (session_directory().peak_path());
3004
3005         _state_of_the_state = StateOfTheState (_state_of_the_state & (~PeakCleanup));
3006
3007         for (SourceMap::iterator i = sources.begin(); i != sources.end(); ++i) {
3008                 boost::shared_ptr<AudioSource> as;
3009                 if ((as = boost::dynamic_pointer_cast<AudioSource> (i->second)) != 0) {
3010                         SourceFactory::setup_peakfile(as, true);
3011                 }
3012         }
3013         return 0;
3014 }
3015
3016 static void
3017 merge_all_sources (boost::shared_ptr<const Playlist> pl, std::set<boost::shared_ptr<Source> >* all_sources)
3018 {
3019         pl->deep_sources (*all_sources);
3020 }
3021
3022 int
3023 Session::cleanup_sources (CleanupReport& rep)
3024 {
3025         // FIXME: needs adaptation to midi
3026
3027         vector<boost::shared_ptr<Source> > dead_sources;
3028         string audio_path;
3029         string midi_path;
3030         vector<string> candidates;
3031         vector<string> unused;
3032         set<string> sources_used_by_all_snapshots;
3033         string spath;
3034         int ret = -1;
3035         string tmppath1;
3036         string tmppath2;
3037         Searchpath asp;
3038         Searchpath msp;
3039         set<boost::shared_ptr<Source> > sources_used_by_this_snapshot;
3040
3041         _state_of_the_state = (StateOfTheState) (_state_of_the_state | InCleanup);
3042
3043         /* this is mostly for windows which doesn't allow file
3044          * renaming if the file is in use. But we don't special
3045          * case it because we need to know if this causes
3046          * problems, and the easiest way to notice that is to
3047          * keep it in place for all platforms.
3048          */
3049
3050         request_stop (false);
3051         _butler->summon ();
3052         _butler->wait_until_finished ();
3053
3054         /* consider deleting all unused playlists */
3055
3056         if (playlists->maybe_delete_unused (boost::bind (Session::ask_about_playlist_deletion, _1))) {
3057                 ret = 0;
3058                 goto out;
3059         }
3060
3061         /* sync the "all regions" property of each playlist with its current state
3062          */
3063
3064         playlists->sync_all_regions_with_regions ();
3065
3066         /* find all un-used sources */
3067
3068         rep.paths.clear ();
3069         rep.space = 0;
3070
3071         for (SourceMap::iterator i = sources.begin(); i != sources.end(); ) {
3072
3073                 SourceMap::iterator tmp;
3074
3075                 tmp = i;
3076                 ++tmp;
3077
3078                 /* do not bother with files that are zero size, otherwise we remove the current "nascent"
3079                    capture files.
3080                 */
3081
3082                 if (!i->second->used() && (i->second->length(i->second->timeline_position() > 0))) {
3083                         dead_sources.push_back (i->second);
3084                         i->second->drop_references ();
3085                 }
3086
3087                 i = tmp;
3088         }
3089
3090         /* build a list of all the possible audio directories for the session */
3091
3092         for (vector<space_and_path>::const_iterator i = session_dirs.begin(); i != session_dirs.end(); ++i) {
3093                 SessionDirectory sdir ((*i).path);
3094                 asp += sdir.sound_path();
3095         }
3096         audio_path += asp.to_string();
3097
3098
3099         /* build a list of all the possible midi directories for the session */
3100
3101         for (vector<space_and_path>::const_iterator i = session_dirs.begin(); i != session_dirs.end(); ++i) {
3102                 SessionDirectory sdir ((*i).path);
3103                 msp += sdir.midi_path();
3104         }
3105         midi_path += msp.to_string();
3106
3107         find_files_matching_filter (candidates, audio_path, accept_all_audio_files, (void *) 0, true, true);
3108         find_files_matching_filter (candidates, midi_path, accept_all_midi_files, (void *) 0, true, true);
3109
3110         /* add sources from all other snapshots as "used", but don't use this
3111            snapshot because the state file on disk still references sources we
3112            may have already dropped.
3113         */
3114
3115         find_all_sources_across_snapshots (sources_used_by_all_snapshots, true);
3116
3117         /* Although the region factory has a list of all regions ever created
3118          * for this session, we're only interested in regions actually in
3119          * playlists right now. So merge all playlist regions lists together.
3120          *
3121          * This will include the playlists used within compound regions.
3122          */
3123
3124         playlists->foreach (boost::bind (merge_all_sources, _1, &sources_used_by_this_snapshot));
3125
3126         /*  add our current source list
3127          */
3128
3129         for (SourceMap::iterator i = sources.begin(); i != sources.end(); ) {
3130                 boost::shared_ptr<FileSource> fs;
3131                 SourceMap::iterator tmp = i;
3132                 ++tmp;
3133
3134                 if ((fs = boost::dynamic_pointer_cast<FileSource> (i->second)) == 0) {
3135                         /* not a file */
3136                         i = tmp;
3137                         continue;
3138                 }
3139
3140                 /* this is mostly for windows which doesn't allow file
3141                  * renaming if the file is in use. But we do not special
3142                  * case it because we need to know if this causes
3143                  * problems, and the easiest way to notice that is to
3144                  * keep it in place for all platforms.
3145                  */
3146
3147                 fs->close ();
3148
3149                 if (!fs->is_stub()) {
3150
3151                         /* Note that we're checking a list of all
3152                          * sources across all snapshots with the list
3153                          * of sources used by this snapshot.
3154                          */
3155
3156                         if (sources_used_by_this_snapshot.find (i->second) != sources_used_by_this_snapshot.end()) {
3157                                 /* this source is in use by this snapshot */
3158                                 sources_used_by_all_snapshots.insert (fs->path());
3159                                 cerr << "Source from source list found in used_by_this_snapshot (" << fs->path() << ")\n";
3160                         } else {
3161                                 cerr << "Source from source list NOT found in used_by_this_snapshot (" << fs->path() << ")\n";
3162                                 /* this source is NOT in use by this snapshot
3163                                  */
3164
3165                                 /* remove all related regions from RegionFactory master list
3166                                  */
3167
3168                                 RegionFactory::remove_regions_using_source (i->second);
3169
3170                                 /* remove from our current source list
3171                                  * also. We may not remove it from
3172                                  * disk, because it may be used by
3173                                  * other snapshots, but it isn't used inside this
3174                                  * snapshot anymore, so we don't need a
3175                                  * reference to it.
3176                                  */
3177
3178                                 sources.erase (i);
3179                         }
3180                 }
3181
3182                 i = tmp;
3183         }
3184
3185         /* now check each candidate source to see if it exists in the list of
3186            sources_used_by_all_snapshots. If it doesn't, put it into "unused".
3187         */
3188
3189         cerr << "Candidates: " << candidates.size() << endl;
3190         cerr << "Used by others: " << sources_used_by_all_snapshots.size() << endl;
3191
3192         for (vector<string>::iterator x = candidates.begin(); x != candidates.end(); ++x) {
3193
3194                 bool used = false;
3195                 spath = *x;
3196
3197                 for (set<string>::iterator i = sources_used_by_all_snapshots.begin(); i != sources_used_by_all_snapshots.end(); ++i) {
3198
3199                         tmppath1 = canonical_path (spath);
3200                         tmppath2 = canonical_path ((*i));
3201
3202                         cerr << "\t => " << tmppath2 << endl;
3203
3204                         if (tmppath1 == tmppath2) {
3205                                 used = true;
3206                                 break;
3207                         }
3208                 }
3209
3210                 if (!used) {
3211                         unused.push_back (spath);
3212                 }
3213         }
3214
3215         cerr << "Actually unused: " << unused.size() << endl;
3216
3217         if (unused.empty()) {
3218                 /* Nothing to do */
3219                 ret = 0;
3220                 goto out;
3221         }
3222
3223         /* now try to move all unused files into the "dead" directory(ies) */
3224
3225         for (vector<string>::iterator x = unused.begin(); x != unused.end(); ++x) {
3226                 GStatBuf statbuf;
3227
3228                 string newpath;
3229
3230                 /* don't move the file across filesystems, just
3231                    stick it in the `dead_dir_name' directory
3232                    on whichever filesystem it was already on.
3233                 */
3234
3235                 if ((*x).find ("/sounds/") != string::npos) {
3236
3237                         /* old school, go up 1 level */
3238
3239                         newpath = Glib::path_get_dirname (*x);      // "sounds"
3240                         newpath = Glib::path_get_dirname (newpath); // "session-name"
3241
3242                 } else {
3243
3244                         /* new school, go up 4 levels */
3245
3246                         newpath = Glib::path_get_dirname (*x);      // "audiofiles" or "midifiles"
3247                         newpath = Glib::path_get_dirname (newpath); // "session-name"
3248                         newpath = Glib::path_get_dirname (newpath); // "interchange"
3249                         newpath = Glib::path_get_dirname (newpath); // "session-dir"
3250                 }
3251
3252                 newpath = Glib::build_filename (newpath, dead_dir_name);
3253
3254                 if (g_mkdir_with_parents (newpath.c_str(), 0755) < 0) {
3255                         error << string_compose(_("Session: cannot create dead file folder \"%1\" (%2)"), newpath, strerror (errno)) << endmsg;
3256                         return -1;
3257                 }
3258
3259                 newpath = Glib::build_filename (newpath, Glib::path_get_basename ((*x)));
3260
3261                 if (Glib::file_test (newpath, Glib::FILE_TEST_EXISTS)) {
3262
3263                         /* the new path already exists, try versioning */
3264
3265                         char buf[PATH_MAX+1];
3266                         int version = 1;
3267                         string newpath_v;
3268
3269                         snprintf (buf, sizeof (buf), "%s.%d", newpath.c_str(), version);
3270                         newpath_v = buf;
3271
3272                         while (Glib::file_test (newpath_v.c_str(), Glib::FILE_TEST_EXISTS) && version < 999) {
3273                                 snprintf (buf, sizeof (buf), "%s.%d", newpath.c_str(), ++version);
3274                                 newpath_v = buf;
3275                         }
3276
3277                         if (version == 999) {
3278                                 error << string_compose (_("there are already 1000 files with names like %1; versioning discontinued"),
3279                                                   newpath)
3280                                       << endmsg;
3281                         } else {
3282                                 newpath = newpath_v;
3283                         }
3284
3285                 }
3286
3287                 g_stat ((*x).c_str(), &statbuf);
3288
3289                 if (::rename ((*x).c_str(), newpath.c_str()) != 0) {
3290                         error << string_compose (_("cannot rename unused file source from %1 to %2 (%3)"), (*x), newpath, strerror (errno)) << endmsg;
3291                         continue;
3292                 }
3293
3294                 /* see if there an easy to find peakfile for this file, and remove it.
3295                  */
3296
3297                 string base = Glib::path_get_basename (*x);
3298                 base += "%A"; /* this is what we add for the channel suffix of all native files,
3299                                  or for the first channel of embedded files. it will miss
3300                                  some peakfiles for other channels
3301                               */
3302                 string peakpath = construct_peak_filepath (base);
3303
3304                 if (Glib::file_test (peakpath.c_str(), Glib::FILE_TEST_EXISTS)) {
3305                         if (::g_unlink (peakpath.c_str()) != 0) {
3306                                 error << string_compose (_("cannot remove peakfile %1 for %2 (%3)"),
3307                                                          peakpath, _path, strerror (errno))
3308                                       << endmsg;
3309                                 /* try to back out */
3310                                 ::rename (newpath.c_str(), _path.c_str());
3311                                 goto out;
3312                         }
3313                 }
3314
3315                 rep.paths.push_back (*x);
3316                 rep.space += statbuf.st_size;
3317         }
3318
3319         /* dump the history list */
3320
3321         _history.clear ();
3322
3323         /* save state so we don't end up a session file
3324            referring to non-existent sources.
3325         */
3326
3327         save_state ("");
3328         ret = 0;
3329
3330   out:
3331         _state_of_the_state = (StateOfTheState) (_state_of_the_state & ~InCleanup);
3332
3333         return ret;
3334 }
3335
3336 int
3337 Session::cleanup_trash_sources (CleanupReport& rep)
3338 {
3339         // FIXME: needs adaptation for MIDI
3340
3341         vector<space_and_path>::iterator i;
3342         string dead_dir;
3343
3344         rep.paths.clear ();
3345         rep.space = 0;
3346
3347         for (i = session_dirs.begin(); i != session_dirs.end(); ++i) {
3348
3349                 dead_dir = Glib::build_filename ((*i).path, dead_dir_name);
3350
3351                 clear_directory (dead_dir, &rep.space, &rep.paths);
3352         }
3353
3354         return 0;
3355 }
3356
3357 void
3358 Session::set_dirty ()
3359 {
3360         /* never mark session dirty during loading */
3361
3362         if (_state_of_the_state & Loading) {
3363                 return;
3364         }
3365
3366         bool was_dirty = dirty();
3367
3368         _state_of_the_state = StateOfTheState (_state_of_the_state | Dirty);
3369
3370
3371         if (!was_dirty) {
3372                 DirtyChanged(); /* EMIT SIGNAL */
3373         }
3374 }
3375
3376
3377 void
3378 Session::set_clean ()
3379 {
3380         bool was_dirty = dirty();
3381
3382         _state_of_the_state = Clean;
3383
3384
3385         if (was_dirty) {
3386                 DirtyChanged(); /* EMIT SIGNAL */
3387         }
3388 }
3389
3390 void
3391 Session::set_deletion_in_progress ()
3392 {
3393         _state_of_the_state = StateOfTheState (_state_of_the_state | Deletion);
3394 }
3395
3396 void
3397 Session::clear_deletion_in_progress ()
3398 {
3399         _state_of_the_state = StateOfTheState (_state_of_the_state & (~Deletion));
3400 }
3401
3402 void
3403 Session::add_controllable (boost::shared_ptr<Controllable> c)
3404 {
3405         /* this adds a controllable to the list managed by the Session.
3406            this is a subset of those managed by the Controllable class
3407            itself, and represents the only ones whose state will be saved
3408            as part of the session.
3409         */
3410
3411         Glib::Threads::Mutex::Lock lm (controllables_lock);
3412         controllables.insert (c);
3413 }
3414
3415 struct null_deleter { void operator()(void const *) const {} };
3416
3417 void
3418 Session::remove_controllable (Controllable* c)
3419 {
3420         if (_state_of_the_state & Deletion) {
3421                 return;
3422         }
3423
3424         Glib::Threads::Mutex::Lock lm (controllables_lock);
3425
3426         Controllables::iterator x = controllables.find (boost::shared_ptr<Controllable>(c, null_deleter()));
3427
3428         if (x != controllables.end()) {
3429                 controllables.erase (x);
3430         }
3431 }
3432
3433 boost::shared_ptr<Controllable>
3434 Session::controllable_by_id (const PBD::ID& id)
3435 {
3436         Glib::Threads::Mutex::Lock lm (controllables_lock);
3437
3438         for (Controllables::iterator i = controllables.begin(); i != controllables.end(); ++i) {
3439                 if ((*i)->id() == id) {
3440                         return *i;
3441                 }
3442         }
3443
3444         return boost::shared_ptr<Controllable>();
3445 }
3446
3447 boost::shared_ptr<Controllable>
3448 Session::controllable_by_descriptor (const ControllableDescriptor& desc)
3449 {
3450         boost::shared_ptr<Controllable> c;
3451         boost::shared_ptr<Stripable> s;
3452         boost::shared_ptr<Route> r;
3453
3454         switch (desc.top_level_type()) {
3455         case ControllableDescriptor::NamedRoute:
3456         {
3457                 std::string str = desc.top_level_name();
3458
3459                 if (str == "Master" || str == "master") {
3460                         s = _master_out;
3461                 } else if (str == "control" || str == "listen" || str == "monitor" || str == "Monitor") {
3462                         s = _monitor_out;
3463                 } else if (str == "auditioner") {
3464                         s = auditioner;
3465                 } else {
3466                         s = route_by_name (desc.top_level_name());
3467                 }
3468
3469                 break;
3470         }
3471
3472         case ControllableDescriptor::PresentationOrderRoute:
3473                 s = get_remote_nth_stripable (desc.presentation_order(), PresentationInfo::Route);
3474                 break;
3475
3476         case ControllableDescriptor::PresentationOrderTrack:
3477                 s = get_remote_nth_stripable (desc.presentation_order(), PresentationInfo::Track);
3478                 break;
3479
3480         case ControllableDescriptor::PresentationOrderBus:
3481                 s = get_remote_nth_stripable (desc.presentation_order(), PresentationInfo::Bus);
3482                 break;
3483
3484         case ControllableDescriptor::PresentationOrderVCA:
3485                 s = get_remote_nth_stripable (desc.presentation_order(), PresentationInfo::VCA);
3486                 break;
3487
3488         case ControllableDescriptor::SelectionCount:
3489                 s = route_by_selected_count (desc.selection_id());
3490                 break;
3491         }
3492
3493         if (!s) {
3494                 return c;
3495         }
3496
3497         r = boost::dynamic_pointer_cast<Route> (s);
3498
3499         switch (desc.subtype()) {
3500         case ControllableDescriptor::Gain:
3501                 c = s->gain_control ();
3502                 break;
3503
3504         case ControllableDescriptor::Trim:
3505                 c = s->trim_control ();
3506                 break;
3507
3508         case ControllableDescriptor::Solo:
3509                 c = s->solo_control();
3510                 break;
3511
3512         case ControllableDescriptor::Mute:
3513                 c = s->mute_control();
3514                 break;
3515
3516         case ControllableDescriptor::Recenable:
3517                 c = s->rec_enable_control ();
3518                 break;
3519
3520         case ControllableDescriptor::PanDirection:
3521                 c = s->pan_azimuth_control();
3522                 break;
3523
3524         case ControllableDescriptor::PanWidth:
3525                 c = s->pan_width_control();
3526                 break;
3527
3528         case ControllableDescriptor::PanElevation:
3529                 c = s->pan_elevation_control();
3530                 break;
3531
3532         case ControllableDescriptor::Balance:
3533                 /* XXX simple pan control */
3534                 break;
3535
3536         case ControllableDescriptor::PluginParameter:
3537         {
3538                 uint32_t plugin = desc.target (0);
3539                 uint32_t parameter_index = desc.target (1);
3540
3541                 /* revert to zero based counting */
3542
3543                 if (plugin > 0) {
3544                         --plugin;
3545                 }
3546
3547                 if (parameter_index > 0) {
3548                         --parameter_index;
3549                 }
3550
3551                 if (!r) {
3552                         return c;
3553                 }
3554
3555                 boost::shared_ptr<Processor> p = r->nth_plugin (plugin);
3556
3557                 if (p) {
3558                         c = boost::dynamic_pointer_cast<ARDOUR::AutomationControl>(
3559                                 p->control(Evoral::Parameter(PluginAutomation, 0, parameter_index)));
3560                 }
3561                 break;
3562         }
3563
3564         case ControllableDescriptor::SendGain: {
3565                 uint32_t send = desc.target (0);
3566                 if (send > 0) {
3567                         --send;
3568                 }
3569                 if (!r) {
3570                         return c;
3571                 }
3572                 c = r->send_level_controllable (send);
3573                 break;
3574         }
3575
3576         default:
3577                 /* relax and return a null pointer */
3578                 break;
3579         }
3580
3581         return c;
3582 }
3583
3584 void
3585 Session::add_instant_xml (XMLNode& node, bool write_to_config)
3586 {
3587         if (_writable) {
3588                 Stateful::add_instant_xml (node, _path);
3589         }
3590
3591         if (write_to_config) {
3592                 Config->add_instant_xml (node);
3593         }
3594 }
3595
3596 XMLNode*
3597 Session::instant_xml (const string& node_name)
3598 {
3599         return Stateful::instant_xml (node_name, _path);
3600 }
3601
3602 int
3603 Session::save_history (string snapshot_name)
3604 {
3605         XMLTree tree;
3606
3607         if (!_writable) {
3608                 return 0;
3609         }
3610
3611         if (!Config->get_save_history() || Config->get_saved_history_depth() < 0 ||
3612             (_history.undo_depth() == 0 && _history.redo_depth() == 0)) {
3613                 return 0;
3614         }
3615
3616         if (snapshot_name.empty()) {
3617                 snapshot_name = _current_snapshot_name;
3618         }
3619
3620         const string history_filename = legalize_for_path (snapshot_name) + history_suffix;
3621         const string backup_filename = history_filename + backup_suffix;
3622         const std::string xml_path(Glib::build_filename (_session_dir->root_path(), history_filename));
3623         const std::string backup_path(Glib::build_filename (_session_dir->root_path(), backup_filename));
3624
3625         if (Glib::file_test (xml_path, Glib::FILE_TEST_EXISTS)) {
3626                 if (::g_rename (xml_path.c_str(), backup_path.c_str()) != 0) {
3627                         error << _("could not backup old history file, current history not saved") << endmsg;
3628                         return -1;
3629                 }
3630         }
3631
3632         tree.set_root (&_history.get_state (Config->get_saved_history_depth()));
3633
3634         if (!tree.write (xml_path))
3635         {
3636                 error << string_compose (_("history could not be saved to %1"), xml_path) << endmsg;
3637
3638                 if (g_remove (xml_path.c_str()) != 0) {
3639                         error << string_compose(_("Could not remove history file at path \"%1\" (%2)"),
3640                                         xml_path, g_strerror (errno)) << endmsg;
3641                 }
3642                 if (::g_rename (backup_path.c_str(), xml_path.c_str()) != 0) {
3643                         error << string_compose (_("could not restore history file from backup %1 (%2)"),
3644                                         backup_path, g_strerror (errno)) << endmsg;
3645                 }
3646
3647                 return -1;
3648         }
3649
3650         return 0;
3651 }
3652
3653 int
3654 Session::restore_history (string snapshot_name)
3655 {
3656         XMLTree tree;
3657
3658         if (snapshot_name.empty()) {
3659                 snapshot_name = _current_snapshot_name;
3660         }
3661
3662         const std::string xml_filename = legalize_for_path (snapshot_name) + history_suffix;
3663         const std::string xml_path(Glib::build_filename (_session_dir->root_path(), xml_filename));
3664
3665         info << "Loading history from " << xml_path << endmsg;
3666
3667         if (!Glib::file_test (xml_path, Glib::FILE_TEST_EXISTS)) {
3668                 info << string_compose (_("%1: no history file \"%2\" for this session."),
3669                                 _name, xml_path) << endmsg;
3670                 return 1;
3671         }
3672
3673         if (!tree.read (xml_path)) {
3674                 error << string_compose (_("Could not understand session history file \"%1\""),
3675                                 xml_path) << endmsg;
3676                 return -1;
3677         }
3678
3679         // replace history
3680         _history.clear();
3681
3682         for (XMLNodeConstIterator it  = tree.root()->children().begin(); it != tree.root()->children().end(); it++) {
3683
3684                 XMLNode *t = *it;
3685                 UndoTransaction* ut = new UndoTransaction ();
3686                 struct timeval tv;
3687
3688                 ut->set_name(t->property("name")->value());
3689                 stringstream ss(t->property("tv-sec")->value());
3690                 ss >> tv.tv_sec;
3691                 ss.str(t->property("tv-usec")->value());
3692                 ss >> tv.tv_usec;
3693                 ut->set_timestamp(tv);
3694
3695                 for (XMLNodeConstIterator child_it  = t->children().begin();
3696                                 child_it != t->children().end(); child_it++)
3697                 {
3698                         XMLNode *n = *child_it;
3699                         Command *c;
3700
3701                         if (n->name() == "MementoCommand" ||
3702                                         n->name() == "MementoUndoCommand" ||
3703                                         n->name() == "MementoRedoCommand") {
3704
3705                                 if ((c = memento_command_factory(n))) {
3706                                         ut->add_command(c);
3707                                 }
3708
3709                         } else if (n->name() == "NoteDiffCommand") {
3710                                 PBD::ID id (n->property("midi-source")->value());
3711                                 boost::shared_ptr<MidiSource> midi_source =
3712                                         boost::dynamic_pointer_cast<MidiSource, Source>(source_by_id(id));
3713                                 if (midi_source) {
3714                                         ut->add_command (new MidiModel::NoteDiffCommand(midi_source->model(), *n));
3715                                 } else {
3716                                         error << _("Failed to downcast MidiSource for NoteDiffCommand") << endmsg;
3717                                 }
3718
3719                         } else if (n->name() == "SysExDiffCommand") {
3720
3721                                 PBD::ID id (n->property("midi-source")->value());
3722                                 boost::shared_ptr<MidiSource> midi_source =
3723                                         boost::dynamic_pointer_cast<MidiSource, Source>(source_by_id(id));
3724                                 if (midi_source) {
3725                                         ut->add_command (new MidiModel::SysExDiffCommand (midi_source->model(), *n));
3726                                 } else {
3727                                         error << _("Failed to downcast MidiSource for SysExDiffCommand") << endmsg;
3728                                 }
3729
3730                         } else if (n->name() == "PatchChangeDiffCommand") {
3731
3732                                 PBD::ID id (n->property("midi-source")->value());
3733                                 boost::shared_ptr<MidiSource> midi_source =
3734                                         boost::dynamic_pointer_cast<MidiSource, Source>(source_by_id(id));
3735                                 if (midi_source) {
3736                                         ut->add_command (new MidiModel::PatchChangeDiffCommand (midi_source->model(), *n));
3737                                 } else {
3738                                         error << _("Failed to downcast MidiSource for PatchChangeDiffCommand") << endmsg;
3739                                 }
3740
3741                         } else if (n->name() == "StatefulDiffCommand") {
3742                                 if ((c = stateful_diff_command_factory (n))) {
3743                                         ut->add_command (c);
3744                                 }
3745                         } else {
3746                                 error << string_compose(_("Couldn't figure out how to make a Command out of a %1 XMLNode."), n->name()) << endmsg;
3747                         }
3748                 }
3749
3750                 _history.add (ut);
3751         }
3752
3753         return 0;
3754 }
3755
3756 void
3757 Session::config_changed (std::string p, bool ours)
3758 {
3759         if (ours) {
3760                 set_dirty ();
3761         }
3762
3763         if (p == "seamless-loop") {
3764
3765         } else if (p == "rf-speed") {
3766
3767         } else if (p == "auto-loop") {
3768
3769         } else if (p == "auto-input") {
3770
3771                 if (Config->get_monitoring_model() == HardwareMonitoring && transport_rolling()) {
3772                         /* auto-input only makes a difference if we're rolling */
3773                         set_track_monitor_input_status (!config.get_auto_input());
3774                 }
3775
3776         } else if (p == "punch-in") {
3777
3778                 Location* location;
3779
3780                 if ((location = _locations->auto_punch_location()) != 0) {
3781
3782                         if (config.get_punch_in ()) {
3783                                 replace_event (SessionEvent::PunchIn, location->start());
3784                         } else {
3785                                 remove_event (location->start(), SessionEvent::PunchIn);
3786                         }
3787                 }
3788
3789         } else if (p == "punch-out") {
3790
3791                 Location* location;
3792
3793                 if ((location = _locations->auto_punch_location()) != 0) {
3794
3795                         if (config.get_punch_out()) {
3796                                 replace_event (SessionEvent::PunchOut, location->end());
3797                         } else {
3798                                 clear_events (SessionEvent::PunchOut);
3799                         }
3800                 }
3801
3802         } else if (p == "edit-mode") {
3803
3804                 Glib::Threads::Mutex::Lock lm (playlists->lock);
3805
3806                 for (SessionPlaylists::List::iterator i = playlists->playlists.begin(); i != playlists->playlists.end(); ++i) {
3807                         (*i)->set_edit_mode (Config->get_edit_mode ());
3808                 }
3809
3810         } else if (p == "use-video-sync") {
3811
3812                 waiting_for_sync_offset = config.get_use_video_sync();
3813
3814         } else if (p == "mmc-control") {
3815
3816                 //poke_midi_thread ();
3817
3818         } else if (p == "mmc-device-id" || p == "mmc-receive-id" || p == "mmc-receive-device-id") {
3819
3820                 _mmc->set_receive_device_id (Config->get_mmc_receive_device_id());
3821
3822         } else if (p == "mmc-send-id" || p == "mmc-send-device-id") {
3823
3824                 _mmc->set_send_device_id (Config->get_mmc_send_device_id());
3825
3826         } else if (p == "midi-control") {
3827
3828                 //poke_midi_thread ();
3829
3830         } else if (p == "raid-path") {
3831
3832                 setup_raid_path (config.get_raid_path());
3833
3834         } else if (p == "timecode-format") {
3835
3836                 sync_time_vars ();
3837
3838         } else if (p == "video-pullup") {
3839
3840                 sync_time_vars ();
3841
3842         } else if (p == "seamless-loop") {
3843
3844                 if (play_loop && transport_rolling()) {
3845                         // to reset diskstreams etc
3846                         request_play_loop (true);
3847                 }
3848
3849         } else if (p == "rf-speed") {
3850
3851                 cumulative_rf_motion = 0;
3852                 reset_rf_scale (0);
3853
3854         } else if (p == "click-sound") {
3855
3856                 setup_click_sounds (1);
3857
3858         } else if (p == "click-emphasis-sound") {
3859
3860                 setup_click_sounds (-1);
3861
3862         } else if (p == "clicking") {
3863
3864                 if (Config->get_clicking()) {
3865                         if (_click_io && click_data) { // don't require emphasis data
3866                                 _clicking = true;
3867                         }
3868                 } else {
3869                         _clicking = false;
3870                 }
3871
3872         } else if (p == "click-gain") {
3873
3874                 if (_click_gain) {
3875                         _click_gain->gain_control()->set_value (Config->get_click_gain(), Controllable::NoGroup);
3876                 }
3877
3878         } else if (p == "send-mtc") {
3879
3880                 if (Config->get_send_mtc ()) {
3881                         /* mark us ready to send */
3882                         next_quarter_frame_to_send = 0;
3883                 }
3884
3885         } else if (p == "send-mmc") {
3886
3887                 _mmc->enable_send (Config->get_send_mmc ());
3888
3889         } else if (p == "midi-feedback") {
3890
3891                 session_midi_feedback = Config->get_midi_feedback();
3892
3893         } else if (p == "jack-time-master") {
3894
3895                 engine().reset_timebase ();
3896
3897         } else if (p == "native-file-header-format") {
3898
3899                 if (!first_file_header_format_reset) {
3900                         reset_native_file_format ();
3901                 }
3902
3903                 first_file_header_format_reset = false;
3904
3905         } else if (p == "native-file-data-format") {
3906
3907                 if (!first_file_data_format_reset) {
3908                         reset_native_file_format ();
3909                 }
3910
3911                 first_file_data_format_reset = false;
3912
3913         } else if (p == "external-sync") {
3914                 if (!config.get_external_sync()) {
3915                         drop_sync_source ();
3916                 } else {
3917                         switch_to_sync_source (Config->get_sync_source());
3918                 }
3919         }  else if (p == "denormal-model") {
3920                 setup_fpu ();
3921         } else if (p == "history-depth") {
3922                 set_history_depth (Config->get_history_depth());
3923         } else if (p == "remote-model") {
3924                 /* XXX DO SOMETHING HERE TO TELL THE GUI THAT WE NEED
3925                    TO SET REMOTE ID'S
3926                 */
3927         } else if (p == "initial-program-change") {
3928
3929                 if (_mmc->output_port() && Config->get_initial_program_change() >= 0) {
3930                         MIDI::byte buf[2];
3931
3932                         buf[0] = MIDI::program; // channel zero by default
3933                         buf[1] = (Config->get_initial_program_change() & 0x7f);
3934
3935                         _mmc->output_port()->midimsg (buf, sizeof (buf), 0);
3936                 }
3937         } else if (p == "solo-mute-override") {
3938                 // catch_up_on_solo_mute_override ();
3939         } else if (p == "listen-position" || p == "pfl-position") {
3940                 listen_position_changed ();
3941         } else if (p == "solo-control-is-listen-control") {
3942                 solo_control_mode_changed ();
3943         } else if (p == "solo-mute-gain") {
3944                 _solo_cut_control->Changed (true, Controllable::NoGroup);
3945         } else if (p == "timecode-offset" || p == "timecode-offset-negative") {
3946                 last_timecode_valid = false;
3947         } else if (p == "playback-buffer-seconds") {
3948                 AudioSource::allocate_working_buffers (frame_rate());
3949         } else if (p == "ltc-source-port") {
3950                 reconnect_ltc_input ();
3951         } else if (p == "ltc-sink-port") {
3952                 reconnect_ltc_output ();
3953         } else if (p == "timecode-generator-offset") {
3954                 ltc_tx_parse_offset();
3955         } else if (p == "auto-return-target-list") {
3956                 follow_playhead_priority ();
3957         }
3958
3959         set_dirty ();
3960 }
3961
3962 void
3963 Session::set_history_depth (uint32_t d)
3964 {
3965         _history.set_depth (d);
3966 }
3967
3968 int
3969 Session::load_diskstreams_2X (XMLNode const & node, int)
3970 {
3971         XMLNodeList          clist;
3972         XMLNodeConstIterator citer;
3973
3974         clist = node.children();
3975
3976         for (citer = clist.begin(); citer != clist.end(); ++citer) {
3977
3978                 try {
3979                         /* diskstreams added automatically by DiskstreamCreated handler */
3980                         if ((*citer)->name() == "AudioDiskstream" || (*citer)->name() == "DiskStream") {
3981                                 boost::shared_ptr<AudioDiskstream> dsp (new AudioDiskstream (*this, **citer));
3982                                 _diskstreams_2X.push_back (dsp);
3983                         } else {
3984                                 error << _("Session: unknown diskstream type in XML") << endmsg;
3985                         }
3986                 }
3987
3988                 catch (failed_constructor& err) {
3989                         error << _("Session: could not load diskstream via XML state") << endmsg;
3990                         return -1;
3991                 }
3992         }
3993
3994         return 0;
3995 }
3996
3997 /** Connect things to the MMC object */
3998 void
3999 Session::setup_midi_machine_control ()
4000 {
4001         _mmc = new MIDI::MachineControl;
4002
4003         boost::shared_ptr<AsyncMIDIPort> async_in = boost::dynamic_pointer_cast<AsyncMIDIPort> (_midi_ports->mmc_input_port());
4004         boost::shared_ptr<AsyncMIDIPort> async_out = boost::dynamic_pointer_cast<AsyncMIDIPort> (_midi_ports->mmc_output_port());
4005
4006         if (!async_out || !async_out) {
4007                 return;
4008         }
4009
4010         /* XXXX argh, passing raw pointers back into libmidi++ */
4011
4012         MIDI::Port* mmc_in = async_in.get();
4013         MIDI::Port* mmc_out = async_out.get();
4014
4015         _mmc->set_ports (mmc_in, mmc_out);
4016
4017         _mmc->Play.connect_same_thread (*this, boost::bind (&Session::mmc_deferred_play, this, _1));
4018         _mmc->DeferredPlay.connect_same_thread (*this, boost::bind (&Session::mmc_deferred_play, this, _1));
4019         _mmc->Stop.connect_same_thread (*this, boost::bind (&Session::mmc_stop, this, _1));
4020         _mmc->FastForward.connect_same_thread (*this, boost::bind (&Session::mmc_fast_forward, this, _1));
4021         _mmc->Rewind.connect_same_thread (*this, boost::bind (&Session::mmc_rewind, this, _1));
4022         _mmc->Pause.connect_same_thread (*this, boost::bind (&Session::mmc_pause, this, _1));
4023         _mmc->RecordPause.connect_same_thread (*this, boost::bind (&Session::mmc_record_pause, this, _1));
4024         _mmc->RecordStrobe.connect_same_thread (*this, boost::bind (&Session::mmc_record_strobe, this, _1));
4025         _mmc->RecordExit.connect_same_thread (*this, boost::bind (&Session::mmc_record_exit, this, _1));
4026         _mmc->Locate.connect_same_thread (*this, boost::bind (&Session::mmc_locate, this, _1, _2));
4027         _mmc->Step.connect_same_thread (*this, boost::bind (&Session::mmc_step, this, _1, _2));
4028         _mmc->Shuttle.connect_same_thread (*this, boost::bind (&Session::mmc_shuttle, this, _1, _2, _3));
4029         _mmc->TrackRecordStatusChange.connect_same_thread (*this, boost::bind (&Session::mmc_record_enable, this, _1, _2, _3));
4030
4031         /* also handle MIDI SPP because its so common */
4032
4033         _mmc->SPPStart.connect_same_thread (*this, boost::bind (&Session::spp_start, this));
4034         _mmc->SPPContinue.connect_same_thread (*this, boost::bind (&Session::spp_continue, this));
4035         _mmc->SPPStop.connect_same_thread (*this, boost::bind (&Session::spp_stop, this));
4036 }
4037
4038 boost::shared_ptr<Controllable>
4039 Session::solo_cut_control() const
4040 {
4041         /* the solo cut control is a bit of an anomaly, at least as of Febrary 2011. There are no other
4042            controls in Ardour that currently get presented to the user in the GUI that require
4043            access as a Controllable and are also NOT owned by some SessionObject (e.g. Route, or MonitorProcessor).
4044
4045            its actually an RCConfiguration parameter, so we use a ProxyControllable to wrap
4046            it up as a Controllable. Changes to the Controllable will just map back to the RCConfiguration
4047            parameter.
4048         */
4049
4050         return _solo_cut_control;
4051 }
4052
4053 void
4054 Session::save_snapshot_name (const std::string & n)
4055 {
4056         /* assure Stateful::_instant_xml is loaded
4057          * add_instant_xml() only adds to existing data and defaults
4058          * to use an empty Tree otherwise
4059          */
4060         instant_xml ("LastUsedSnapshot");
4061
4062         XMLNode* last_used_snapshot = new XMLNode ("LastUsedSnapshot");
4063         last_used_snapshot->add_property ("name", string(n));
4064         add_instant_xml (*last_used_snapshot, false);
4065 }
4066
4067 void
4068 Session::set_snapshot_name (const std::string & n)
4069 {
4070         _current_snapshot_name = n;
4071         save_snapshot_name (n);
4072 }
4073
4074 int
4075 Session::rename (const std::string& new_name)
4076 {
4077         string legal_name = legalize_for_path (new_name);
4078         string new_path;
4079         string oldstr;
4080         string newstr;
4081         bool first = true;
4082
4083         string const old_sources_root = _session_dir->sources_root();
4084
4085         if (!_writable || (_state_of_the_state & CannotSave)) {
4086                 error << _("Cannot rename read-only session.") << endmsg;
4087                 return 0; // don't show "messed up" warning
4088         }
4089         if (record_status() == Recording) {
4090                 error << _("Cannot rename session while recording") << endmsg;
4091                 return 0; // don't show "messed up" warning
4092         }
4093
4094         StateProtector stp (this);
4095
4096         /* Rename:
4097
4098          * session directory
4099          * interchange subdirectory
4100          * session file
4101          * session history
4102
4103          * Backup files are left unchanged and not renamed.
4104          */
4105
4106         /* Windows requires that we close all files before attempting the
4107          * rename. This works on other platforms, but isn't necessary there.
4108          * Leave it in place for all platforms though, since it may help
4109          * catch issues that could arise if the way Source files work ever
4110          * change (since most developers are not using Windows).
4111          */
4112
4113         for (SourceMap::const_iterator i = sources.begin(); i != sources.end(); ++i) {
4114                 boost::shared_ptr<FileSource> fs = boost::dynamic_pointer_cast<FileSource> (i->second);
4115                 if (fs) {
4116                         fs->close ();
4117                 }
4118         }
4119
4120         /* pass one: not 100% safe check that the new directory names don't
4121          * already exist ...
4122          */
4123
4124         for (vector<space_and_path>::const_iterator i = session_dirs.begin(); i != session_dirs.end(); ++i) {
4125
4126                 oldstr = (*i).path;
4127
4128                 /* this is a stupid hack because Glib::path_get_dirname() is
4129                  * lexical-only, and so passing it /a/b/c/ gives a different
4130                  * result than passing it /a/b/c ...
4131                  */
4132
4133                 if (oldstr[oldstr.length()-1] == G_DIR_SEPARATOR) {
4134                         oldstr = oldstr.substr (0, oldstr.length() - 1);
4135                 }
4136
4137                 string base = Glib::path_get_dirname (oldstr);
4138
4139                 newstr = Glib::build_filename (base, legal_name);
4140
4141                 cerr << "Looking for " << newstr << endl;
4142
4143                 if (Glib::file_test (newstr, Glib::FILE_TEST_EXISTS)) {
4144                         cerr << " exists\n";
4145                         return -1;
4146                 }
4147         }
4148
4149         /* Session dirs */
4150
4151         first = true;
4152
4153         for (vector<space_and_path>::iterator i = session_dirs.begin(); i != session_dirs.end(); ++i) {
4154
4155                 vector<string> v;
4156
4157                 oldstr = (*i).path;
4158
4159                 /* this is a stupid hack because Glib::path_get_dirname() is
4160                  * lexical-only, and so passing it /a/b/c/ gives a different
4161                  * result than passing it /a/b/c ...
4162                  */
4163
4164                 if (oldstr[oldstr.length()-1] == G_DIR_SEPARATOR) {
4165                         oldstr = oldstr.substr (0, oldstr.length() - 1);
4166                 }
4167
4168                 string base = Glib::path_get_dirname (oldstr);
4169                 newstr = Glib::build_filename (base, legal_name);
4170
4171                 cerr << "for " << oldstr << " new dir = " << newstr << endl;
4172
4173                 cerr << "Rename " << oldstr << " => " << newstr << endl;
4174                 if (::g_rename (oldstr.c_str(), newstr.c_str()) != 0) {
4175                         cerr << string_compose (_("renaming %s as %2 failed (%3)"), oldstr, newstr, g_strerror (errno)) << endl;
4176                         error << string_compose (_("renaming %s as %2 failed (%3)"), oldstr, newstr, g_strerror (errno)) << endmsg;
4177                         return 1;
4178                 }
4179
4180                 /* Reset path in "session dirs" */
4181
4182                 (*i).path = newstr;
4183                 (*i).blocks = 0;
4184
4185                 /* reset primary SessionDirectory object */
4186
4187                 if (first) {
4188                         (*_session_dir) = newstr;
4189                         new_path = newstr;
4190                         first = false;
4191                 }
4192
4193                 /* now rename directory below session_dir/interchange */
4194
4195                 string old_interchange_dir;
4196                 string new_interchange_dir;
4197
4198                 /* use newstr here because we renamed the path
4199                  * (folder/directory) that used to be oldstr to newstr above
4200                  */
4201
4202                 v.push_back (newstr);
4203                 v.push_back (interchange_dir_name);
4204                 v.push_back (Glib::path_get_basename (oldstr));
4205
4206                 old_interchange_dir = Glib::build_filename (v);
4207
4208                 v.clear ();
4209                 v.push_back (newstr);
4210                 v.push_back (interchange_dir_name);
4211                 v.push_back (legal_name);
4212
4213                 new_interchange_dir = Glib::build_filename (v);
4214
4215                 cerr << "Rename " << old_interchange_dir << " => " << new_interchange_dir << endl;
4216
4217                 if (::g_rename (old_interchange_dir.c_str(), new_interchange_dir.c_str()) != 0) {
4218                         cerr << string_compose (_("renaming %s as %2 failed (%3)"),
4219                                                  old_interchange_dir, new_interchange_dir,
4220                                                  g_strerror (errno))
4221                               << endl;
4222                         error << string_compose (_("renaming %s as %2 failed (%3)"),
4223                                                  old_interchange_dir, new_interchange_dir,
4224                                                  g_strerror (errno))
4225                               << endmsg;
4226                         return 1;
4227                 }
4228         }
4229
4230         /* state file */
4231
4232         oldstr = Glib::build_filename (new_path, _current_snapshot_name + statefile_suffix);
4233         newstr= Glib::build_filename (new_path, legal_name + statefile_suffix);
4234
4235         cerr << "Rename " << oldstr << " => " << newstr << endl;
4236
4237         if (::g_rename (oldstr.c_str(), newstr.c_str()) != 0) {
4238                 cerr << string_compose (_("renaming %1 as %2 failed (%3)"), oldstr, newstr, g_strerror (errno)) << endl;
4239                 error << string_compose (_("renaming %1 as %2 failed (%3)"), oldstr, newstr, g_strerror (errno)) << endmsg;
4240                 return 1;
4241         }
4242
4243         /* history file */
4244
4245         oldstr = Glib::build_filename (new_path, _current_snapshot_name) + history_suffix;
4246
4247         if (Glib::file_test (oldstr, Glib::FILE_TEST_EXISTS))  {
4248                 newstr = Glib::build_filename (new_path, legal_name) + history_suffix;
4249
4250                 cerr << "Rename " << oldstr << " => " << newstr << endl;
4251
4252                 if (::g_rename (oldstr.c_str(), newstr.c_str()) != 0) {
4253                         cerr << string_compose (_("renaming %1 as %2 failed (%3)"), oldstr, newstr, g_strerror (errno)) << endl;
4254                         error << string_compose (_("renaming %1 as %2 failed (%3)"), oldstr, newstr, g_strerror (errno)) << endmsg;
4255                         return 1;
4256                 }
4257         }
4258
4259         /* remove old name from recent sessions */
4260         remove_recent_sessions (_path);
4261         _path = new_path;
4262
4263         /* update file source paths */
4264
4265         for (SourceMap::iterator i = sources.begin(); i != sources.end(); ++i) {
4266                 boost::shared_ptr<FileSource> fs = boost::dynamic_pointer_cast<FileSource> (i->second);
4267                 if (fs) {
4268                         string p = fs->path ();
4269                         boost::replace_all (p, old_sources_root, _session_dir->sources_root());
4270                         fs->set_path (p);
4271                         SourceFactory::setup_peakfile(i->second, true);
4272                 }
4273         }
4274
4275         set_snapshot_name (new_name);
4276         _name = new_name;
4277
4278         set_dirty ();
4279
4280         /* save state again to get everything just right */
4281
4282         save_state (_current_snapshot_name);
4283
4284         /* add to recent sessions */
4285
4286         store_recent_sessions (new_name, _path);
4287
4288         return 0;
4289 }
4290
4291 int
4292 Session::get_session_info_from_path (XMLTree& tree, const string& xmlpath)
4293 {
4294         if (!Glib::file_test (xmlpath, Glib::FILE_TEST_EXISTS)) {
4295                 return -1;
4296         }
4297
4298         if (!tree.read (xmlpath)) {
4299                 return -1;
4300         }
4301
4302         return 0;
4303 }
4304
4305 int
4306 Session::get_info_from_path (const string& xmlpath, float& sample_rate, SampleFormat& data_format)
4307 {
4308         XMLTree tree;
4309         bool found_sr = false;
4310         bool found_data_format = false;
4311
4312         if (get_session_info_from_path (tree, xmlpath)) {
4313                 return -1;
4314         }
4315
4316         /* sample rate */
4317
4318         XMLProperty const * prop;
4319         XMLNode const * root (tree.root());
4320
4321         if ((prop = root->property (X_("sample-rate"))) != 0) {
4322                 sample_rate = atoi (prop->value());
4323                 found_sr = true;
4324         }
4325
4326         const XMLNodeList& children (root->children());
4327         for (XMLNodeList::const_iterator c = children.begin(); c != children.end(); ++c) {
4328                 const XMLNode* child = *c;
4329                 if (child->name() == "Config") {
4330                         const XMLNodeList& options (child->children());
4331                         for (XMLNodeList::const_iterator oc = options.begin(); oc != options.end(); ++oc) {
4332                                 XMLNode const * option = *oc;
4333                                 XMLProperty const * name = option->property("name");
4334
4335                                 if (!name) {
4336                                         continue;
4337                                 }
4338
4339                                 if (name->value() == "native-file-data-format") {
4340                                         XMLProperty const * value = option->property ("value");
4341                                         if (value) {
4342                                                 SampleFormat fmt = (SampleFormat) string_2_enum (option->property ("value")->value(), fmt);
4343                                                 data_format = fmt;
4344                                                 found_data_format = true;
4345                                                 break;
4346                                         }
4347                                 }
4348                         }
4349                 }
4350                 if (found_data_format) {
4351                         break;
4352                 }
4353         }
4354
4355         return !(found_sr && found_data_format); // zero if they are both found
4356 }
4357
4358 std::string
4359 Session::get_snapshot_from_instant (const std::string& session_dir)
4360 {
4361         std::string instant_xml_path = Glib::build_filename (session_dir, "instant.xml");
4362
4363         if (!Glib::file_test (instant_xml_path, Glib::FILE_TEST_EXISTS)) {
4364                 return "";
4365         }
4366
4367         XMLTree tree;
4368         if (!tree.read (instant_xml_path)) {
4369                 return "";
4370         }
4371
4372         XMLProperty const * prop;
4373         XMLNode *last_used_snapshot = tree.root()->child("LastUsedSnapshot");
4374         if (last_used_snapshot && (prop = last_used_snapshot->property ("name")) != 0) {
4375                 return prop->value();
4376         }
4377
4378         return "";
4379 }
4380
4381 typedef std::vector<boost::shared_ptr<FileSource> > SeveralFileSources;
4382 typedef std::map<std::string,SeveralFileSources> SourcePathMap;
4383
4384 int
4385 Session::bring_all_sources_into_session (boost::function<void(uint32_t,uint32_t,string)> callback)
4386 {
4387         uint32_t total = 0;
4388         uint32_t n = 0;
4389         SourcePathMap source_path_map;
4390         string new_path;
4391         boost::shared_ptr<AudioFileSource> afs;
4392         int ret = 0;
4393
4394         {
4395
4396                 Glib::Threads::Mutex::Lock lm (source_lock);
4397
4398                 cerr << " total sources = " << sources.size();
4399
4400                 for (SourceMap::const_iterator i = sources.begin(); i != sources.end(); ++i) {
4401                         boost::shared_ptr<FileSource> fs = boost::dynamic_pointer_cast<FileSource> (i->second);
4402
4403                         if (!fs) {
4404                                 continue;
4405                         }
4406
4407                         if (fs->within_session()) {
4408                                 continue;
4409                         }
4410
4411                         if (source_path_map.find (fs->path()) != source_path_map.end()) {
4412                                 source_path_map[fs->path()].push_back (fs);
4413                         } else {
4414                                 SeveralFileSources v;
4415                                 v.push_back (fs);
4416                                 source_path_map.insert (make_pair (fs->path(), v));
4417                         }
4418
4419                         total++;
4420                 }
4421
4422                 cerr << " fsources = " << total << endl;
4423
4424                 for (SourcePathMap::iterator i = source_path_map.begin(); i != source_path_map.end(); ++i) {
4425
4426                         /* tell caller where we are */
4427
4428                         string old_path = i->first;
4429
4430                         callback (n, total, old_path);
4431
4432                         cerr << old_path << endl;
4433
4434                         new_path.clear ();
4435
4436                         switch (i->second.front()->type()) {
4437                         case DataType::AUDIO:
4438                                 new_path = new_audio_source_path_for_embedded (old_path);
4439                                 break;
4440
4441                         case DataType::MIDI:
4442                                 /* XXX not implemented yet */
4443                                 break;
4444                         }
4445
4446                         if (new_path.empty()) {
4447                                 continue;
4448                         }
4449
4450                         cerr << "Move " << old_path << " => " << new_path << endl;
4451
4452                         if (!copy_file (old_path, new_path)) {
4453                                 cerr << "failed !\n";
4454                                 ret = -1;
4455                         }
4456
4457                         /* make sure we stop looking in the external
4458                            dir/folder. Remember, this is an all-or-nothing
4459                            operations, it doesn't merge just some files.
4460                         */
4461                         remove_dir_from_search_path (Glib::path_get_dirname (old_path), i->second.front()->type());
4462
4463                         for (SeveralFileSources::iterator f = i->second.begin(); f != i->second.end(); ++f) {
4464                                 (*f)->set_path (new_path);
4465                         }
4466                 }
4467         }
4468
4469         save_state ("", false, false);
4470
4471         return ret;
4472 }
4473
4474 static
4475 bool accept_all_files (string const &, void *)
4476 {
4477         return true;
4478 }
4479
4480 void
4481 Session::save_as_bring_callback (uint32_t,uint32_t,string)
4482 {
4483         /* It would be good if this did something useful vis-a-vis save-as, but the arguments doesn't provide the correct information right now to do this.
4484         */
4485 }
4486
4487 static string
4488 make_new_media_path (string old_path, string new_session_folder, string new_session_path)
4489 {
4490         /* typedir is the "midifiles" or "audiofiles" etc. part of the path. */
4491
4492         string typedir = Glib::path_get_basename (Glib::path_get_dirname (old_path));
4493         vector<string> v;
4494         v.push_back (new_session_folder); /* full path */
4495         v.push_back (interchange_dir_name);
4496         v.push_back (new_session_path);   /* just one directory/folder */
4497         v.push_back (typedir);
4498         v.push_back (Glib::path_get_basename (old_path));
4499
4500         return Glib::build_filename (v);
4501 }
4502
4503 int
4504 Session::save_as (SaveAs& saveas)
4505 {
4506         vector<string> files;
4507         string current_folder = Glib::path_get_dirname (_path);
4508         string new_folder = legalize_for_path (saveas.new_name);
4509         string to_dir = Glib::build_filename (saveas.new_parent_folder, new_folder);
4510         int64_t total_bytes = 0;
4511         int64_t copied = 0;
4512         int64_t cnt = 0;
4513         int64_t all = 0;
4514         int32_t internal_file_cnt = 0;
4515
4516         vector<string> do_not_copy_extensions;
4517         do_not_copy_extensions.push_back (statefile_suffix);
4518         do_not_copy_extensions.push_back (pending_suffix);
4519         do_not_copy_extensions.push_back (backup_suffix);
4520         do_not_copy_extensions.push_back (temp_suffix);
4521         do_not_copy_extensions.push_back (history_suffix);
4522
4523         /* get total size */
4524
4525         for (vector<space_and_path>::const_iterator sd = session_dirs.begin(); sd != session_dirs.end(); ++sd) {
4526
4527                 /* need to clear this because
4528                  * find_files_matching_filter() is cumulative
4529                  */
4530
4531                 files.clear ();
4532
4533                 find_files_matching_filter (files, (*sd).path, accept_all_files, 0, false, true, true);
4534
4535                 all += files.size();
4536
4537                 for (vector<string>::iterator i = files.begin(); i != files.end(); ++i) {
4538                         GStatBuf gsb;
4539                         g_stat ((*i).c_str(), &gsb);
4540                         total_bytes += gsb.st_size;
4541                 }
4542         }
4543
4544         /* save old values so we can switch back if we are not switching to the new session */
4545
4546         string old_path = _path;
4547         string old_name = _name;
4548         string old_snapshot = _current_snapshot_name;
4549         string old_sd = _session_dir->root_path();
4550         vector<string> old_search_path[DataType::num_types];
4551         string old_config_search_path[DataType::num_types];
4552
4553         old_search_path[DataType::AUDIO] = source_search_path (DataType::AUDIO);
4554         old_search_path[DataType::MIDI] = source_search_path (DataType::MIDI);
4555         old_config_search_path[DataType::AUDIO]  = config.get_audio_search_path ();
4556         old_config_search_path[DataType::MIDI]  = config.get_midi_search_path ();
4557
4558         /* switch session directory */
4559
4560         (*_session_dir) = to_dir;
4561
4562         /* create new tree */
4563
4564         if (!_session_dir->create()) {
4565                 saveas.failure_message = string_compose (_("Cannot create new session folder %1"), to_dir);
4566                 return -1;
4567         }
4568
4569         try {
4570                 /* copy all relevant files. Find each location in session_dirs,
4571                  * and copy files from there to target.
4572                  */
4573
4574                 for (vector<space_and_path>::const_iterator sd = session_dirs.begin(); sd != session_dirs.end(); ++sd) {
4575
4576                         /* need to clear this because
4577                          * find_files_matching_filter() is cumulative
4578                          */
4579
4580                         files.clear ();
4581
4582                         const size_t prefix_len = (*sd).path.size();
4583
4584                         /* Work just on the files within this session dir */
4585
4586                         find_files_matching_filter (files, (*sd).path, accept_all_files, 0, false, true, true);
4587
4588                         /* add dir separator to protect against collisions with
4589                          * track names (e.g. track named "audiofiles" or
4590                          * "analysis".
4591                          */
4592
4593                         static const std::string audiofile_dir_string = string (sound_dir_name) + G_DIR_SEPARATOR;
4594                         static const std::string midifile_dir_string = string (midi_dir_name) + G_DIR_SEPARATOR;
4595                         static const std::string analysis_dir_string = analysis_dir() + G_DIR_SEPARATOR;
4596
4597                         /* copy all the files. Handling is different for media files
4598                            than others because of the *silly* subtree we have below the interchange
4599                            folder. That really was a bad idea, but I'm not fixing it as part of
4600                            implementing ::save_as().
4601                         */
4602
4603                         for (vector<string>::iterator i = files.begin(); i != files.end(); ++i) {
4604
4605                                 std::string from = *i;
4606
4607 #ifdef __APPLE__
4608                                 string filename = Glib::path_get_basename (from);
4609                                 std::transform (filename.begin(), filename.end(), filename.begin(), ::toupper);
4610                                 if (filename == ".DS_STORE") {
4611                                         continue;
4612                                 }
4613 #endif
4614
4615                                 if (from.find (audiofile_dir_string) != string::npos) {
4616
4617                                         /* audio file: only copy if asked */
4618
4619                                         if (saveas.include_media && saveas.copy_media) {
4620
4621                                                 string to = make_new_media_path (*i, to_dir, new_folder);
4622
4623                                                 info << "media file copying from " << from << " to " << to << endmsg;
4624
4625                                                 if (!copy_file (from, to)) {
4626                                                         throw Glib::FileError (Glib::FileError::IO_ERROR,
4627                                                                                                    string_compose(_("\ncopying \"%1\" failed !"), from));
4628                                                 }
4629                                         }
4630
4631                                         /* we found media files inside the session folder */
4632
4633                                         internal_file_cnt++;
4634
4635                                 } else if (from.find (midifile_dir_string) != string::npos) {
4636
4637                                         /* midi file: always copy unless
4638                                          * creating an empty new session
4639                                          */
4640
4641                                         if (saveas.include_media) {
4642
4643                                                 string to = make_new_media_path (*i, to_dir, new_folder);
4644
4645                                                 info << "media file copying from " << from << " to " << to << endmsg;
4646
4647                                                 if (!copy_file (from, to)) {
4648                                                         throw Glib::FileError (Glib::FileError::IO_ERROR, "copy failed");
4649                                                 }
4650                                         }
4651
4652                                         /* we found media files inside the session folder */
4653
4654                                         internal_file_cnt++;
4655
4656                                 } else if (from.find (analysis_dir_string) != string::npos) {
4657
4658                                         /*  make sure analysis dir exists in
4659                                          *  new session folder, but we're not
4660                                          *  copying analysis files here, see
4661                                          *  below
4662                                          */
4663
4664                                         (void) g_mkdir_with_parents (analysis_dir().c_str(), 775);
4665                                         continue;
4666
4667                                 } else {
4668
4669                                         /* normal non-media file. Don't copy state, history, etc.
4670                                          */
4671
4672                                         bool do_copy = true;
4673
4674                                         for (vector<string>::iterator v = do_not_copy_extensions.begin(); v != do_not_copy_extensions.end(); ++v) {
4675                                                 if ((from.length() > (*v).length()) && (from.find (*v) == from.length() - (*v).length())) {
4676                                                         /* end of filename matches extension, do not copy file */
4677                                                         do_copy = false;
4678                                                         break;
4679                                                 }
4680                                         }
4681
4682                                         if (!saveas.copy_media && from.find (peakfile_suffix) != string::npos) {
4683                                                 /* don't copy peakfiles if
4684                                                  * we're not copying media
4685                                                  */
4686                                                 do_copy = false;
4687                                         }
4688
4689                                         if (do_copy) {
4690                                                 string to = Glib::build_filename (to_dir, from.substr (prefix_len));
4691
4692                                                 info << "attempting to make directory/folder " << to << endmsg;
4693
4694                                                 if (g_mkdir_with_parents (Glib::path_get_dirname (to).c_str(), 0755)) {
4695                                                         throw Glib::FileError (Glib::FileError::IO_ERROR, "cannot create required directory");
4696                                                 }
4697
4698                                                 info << "attempting to copy " << from << " to " << to << endmsg;
4699
4700                                                 if (!copy_file (from, to)) {
4701                                                         throw Glib::FileError (Glib::FileError::IO_ERROR,
4702                                                                                                    string_compose(_("\ncopying \"%1\" failed !"), from));
4703                                                 }
4704                                         }
4705                                 }
4706
4707                                 /* measure file size even if we're not going to copy so that our Progress
4708                                    signals are correct, since we included these do-not-copy files
4709                                    in the computation of the total size and file count.
4710                                 */
4711
4712                                 GStatBuf gsb;
4713                                 g_stat (from.c_str(), &gsb);
4714                                 copied += gsb.st_size;
4715                                 cnt++;
4716
4717                                 double fraction = (double) copied / total_bytes;
4718
4719                                 bool keep_going = true;
4720
4721                                 if (saveas.copy_media) {
4722
4723                                         /* no need or expectation of this if
4724                                          * media is not being copied, because
4725                                          * it will be fast(ish).
4726                                          */
4727
4728                                         /* tell someone "X percent, file M of N"; M is one-based */
4729
4730                                         boost::optional<bool> res = saveas.Progress (fraction, cnt, all);
4731
4732                                         if (res) {
4733                                                 keep_going = *res;
4734                                         }
4735                                 }
4736
4737                                 if (!keep_going) {
4738                                         throw Glib::FileError (Glib::FileError::FAILED, "copy cancelled");
4739                                 }
4740                         }
4741
4742                 }
4743
4744                 /* copy optional folders, if any */
4745
4746                 string old = plugins_dir ();
4747                 if (Glib::file_test (old, Glib::FILE_TEST_EXISTS)) {
4748                         string newdir = Glib::build_filename (to_dir, Glib::path_get_basename (old));
4749                         copy_files (old, newdir);
4750                 }
4751
4752                 old = externals_dir ();
4753                 if (Glib::file_test (old, Glib::FILE_TEST_EXISTS)) {
4754                         string newdir = Glib::build_filename (to_dir, Glib::path_get_basename (old));
4755                         copy_files (old, newdir);
4756                 }
4757
4758                 old = automation_dir ();
4759                 if (Glib::file_test (old, Glib::FILE_TEST_EXISTS)) {
4760                         string newdir = Glib::build_filename (to_dir, Glib::path_get_basename (old));
4761                         copy_files (old, newdir);
4762                 }
4763
4764                 if (saveas.include_media) {
4765
4766                         if (saveas.copy_media) {
4767 #ifndef PLATFORM_WINDOWS
4768                                 /* There are problems with analysis files on
4769                                  * Windows, because they used a colon in their
4770                                  * names as late as 4.0. Colons are not legal
4771                                  * under Windows even if NTFS allows them.
4772                                  *
4773                                  * This is a tricky problem to solve so for
4774                                  * just don't copy these files. They will be
4775                                  * regenerated as-needed anyway, subject to the
4776                                  * existing issue that the filenames will be
4777                                  * rejected by Windows, which is a separate
4778                                  * problem (though related).
4779                                  */
4780
4781                                 /* only needed if we are copying media, since the
4782                                  * analysis data refers to media data
4783                                  */
4784
4785                                 old = analysis_dir ();
4786                                 if (Glib::file_test (old, Glib::FILE_TEST_EXISTS)) {
4787                                         string newdir = Glib::build_filename (to_dir, "analysis");
4788                                         copy_files (old, newdir);
4789                                 }
4790 #endif /* PLATFORM_WINDOWS */
4791                         }
4792                 }
4793
4794
4795                 _path = to_dir;
4796                 set_snapshot_name (saveas.new_name);
4797                 _name = saveas.new_name;
4798
4799                 if (saveas.include_media && !saveas.copy_media) {
4800
4801                         /* reset search paths of the new session (which we're pretending to be right now) to
4802                            include the original session search path, so we can still find all audio.
4803                         */
4804
4805                         if (internal_file_cnt) {
4806                                 for (vector<string>::iterator s = old_search_path[DataType::AUDIO].begin(); s != old_search_path[DataType::AUDIO].end(); ++s) {
4807                                         ensure_search_path_includes (*s, DataType::AUDIO);
4808                                         cerr << "be sure to include " << *s << "  for audio" << endl;
4809                                 }
4810
4811                                 /* we do not do this for MIDI because we copy
4812                                    all MIDI files if saveas.include_media is
4813                                    true
4814                                 */
4815                         }
4816                 }
4817
4818                 bool was_dirty = dirty ();
4819
4820                 save_state ("", false, false, !saveas.include_media);
4821                 save_default_options ();
4822
4823                 if (saveas.copy_media && saveas.copy_external) {
4824                         if (bring_all_sources_into_session (boost::bind (&Session::save_as_bring_callback, this, _1, _2, _3))) {
4825                                 throw Glib::FileError (Glib::FileError::NO_SPACE_LEFT, "consolidate failed");
4826                         }
4827                 }
4828
4829                 saveas.final_session_folder_name = _path;
4830
4831                 store_recent_sessions (_name, _path);
4832
4833                 if (!saveas.switch_to) {
4834
4835                         /* switch back to the way things were */
4836
4837                         _path = old_path;
4838                         _name = old_name;
4839                         set_snapshot_name (old_snapshot);
4840
4841                         (*_session_dir) = old_sd;
4842
4843                         if (was_dirty) {
4844                                 set_dirty ();
4845                         }
4846
4847                         if (internal_file_cnt) {
4848                                 /* reset these to their original values */
4849                                 config.set_audio_search_path (old_config_search_path[DataType::AUDIO]);
4850                                 config.set_midi_search_path (old_config_search_path[DataType::MIDI]);
4851                         }
4852
4853                 } else {
4854
4855                         /* prune session dirs, and update disk space statistics
4856                          */
4857
4858                         space_and_path sp;
4859                         sp.path = _path;
4860                         session_dirs.clear ();
4861                         session_dirs.push_back (sp);
4862                         refresh_disk_space ();
4863
4864                         /* ensure that all existing tracks reset their current capture source paths
4865                          */
4866                         reset_write_sources (true, true);
4867
4868                         /* the copying above was based on actually discovering files, not just iterating over the sources list.
4869                            But if we're going to switch to the new (copied) session, we need to change the paths in the sources also.
4870                         */
4871
4872                         for (SourceMap::const_iterator i = sources.begin(); i != sources.end(); ++i) {
4873                                 boost::shared_ptr<FileSource> fs = boost::dynamic_pointer_cast<FileSource> (i->second);
4874
4875                                 if (!fs) {
4876                                         continue;
4877                                 }
4878
4879                                 if (fs->within_session()) {
4880                                         string newpath = make_new_media_path (fs->path(), to_dir, new_folder);
4881                                         fs->set_path (newpath);
4882                                 }
4883                         }
4884                 }
4885
4886         } catch (Glib::FileError& e) {
4887
4888                 saveas.failure_message = e.what();
4889
4890                 /* recursively remove all the directories */
4891
4892                 remove_directory (to_dir);
4893
4894                 /* return error */
4895
4896                 return -1;
4897
4898         } catch (...) {
4899
4900                 saveas.failure_message = _("unknown reason");
4901
4902                 /* recursively remove all the directories */
4903
4904                 remove_directory (to_dir);
4905
4906                 /* return error */
4907
4908                 return -1;
4909         }
4910
4911         return 0;
4912 }