fix compose mess, and a number of 64 bit printf specs
[ardour.git] / libs / ardour / session_state.cc
1 /*
2   Copyright (C) 1999-2002 Paul Davis 
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License as published by
6   the Free Software Foundation; either version 2 of the License, or
7   (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   GNU General Public License for more details.
13
14   You should have received a copy of the GNU General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17     
18   $Id$
19 */
20
21 #include <algorithm>
22 #include <fstream>
23 #include <string>
24 #include <cerrno>
25
26 #include <sigc++/bind.h>
27
28 #include <cstdio> /* snprintf(3) ... grrr */
29 #include <cmath>
30 #include <unistd.h>
31 #include <sys/stat.h>
32 #include <climits>
33 #include <fcntl.h>
34 #include <poll.h>
35 #include <signal.h>
36 #include <sys/mman.h>
37 #include <sys/time.h>
38 #include <dirent.h>
39
40 #ifdef HAVE_SYS_VFS_H
41 #include <sys/vfs.h>
42 #else
43 #include <sys/mount.h>
44 #include <sys/param.h>
45 #endif
46
47 #include <midi++/mmc.h>
48 #include <midi++/port.h>
49 #include <pbd/error.h>
50 #include <pbd/dirname.h>
51 #include <pbd/lockmonitor.h>
52 #include <pbd/pathscanner.h>
53 #include <pbd/pthread_utils.h>
54 #include <pbd/basename.h>
55 #include <pbd/strsplit.h>
56
57 #include <ardour/audioengine.h>
58 #include <ardour/configuration.h>
59 #include <ardour/session.h>
60 #include <ardour/diskstream.h>
61 #include <ardour/utils.h>
62 #include <ardour/audioplaylist.h>
63 #include <ardour/source.h>
64 #include <ardour/filesource.h>
65 #include <ardour/sndfilesource.h>
66 #include <ardour/sndfile_helpers.h>
67 #include <ardour/auditioner.h>
68 #include <ardour/export.h>
69 #include <ardour/redirect.h>
70 #include <ardour/send.h>
71 #include <ardour/insert.h>
72 #include <ardour/connection.h>
73 #include <ardour/slave.h>
74 #include <ardour/tempo.h>
75 #include <ardour/audio_track.h>
76 #include <ardour/cycle_timer.h>
77 #include <ardour/utils.h>
78 #include <ardour/named_selection.h>
79 #include <ardour/version.h>
80 #include <ardour/location.h>
81 #include <ardour/audioregion.h>
82 #include <ardour/crossfade.h>
83
84 #include "i18n.h"
85 #include <locale.h>
86
87 using namespace std;
88 using namespace ARDOUR;
89
90 void
91 Session::first_stage_init (string fullpath, string snapshot_name)
92 {
93         if (fullpath.length() == 0) {
94                 throw failed_constructor();
95         }
96
97         char buf[PATH_MAX+1];
98         if (!realpath(fullpath.c_str(), buf) && (errno != ENOENT)) {
99                 error << string_compose(_("Could not use path %1 (%s)"), buf, strerror(errno)) << endmsg;
100                 throw failed_constructor();
101         }
102         _path = string(buf);
103
104         if (_path[_path.length()-1] != '/') {
105                 _path += '/';
106         }
107
108         /* these two are just provisional settings. set_state()
109            will likely override them.
110         */
111
112         _name = _current_snapshot_name = snapshot_name;
113         setup_raid_path (_path);
114
115         _current_frame_rate = _engine.frame_rate ();
116         _tempo_map = new TempoMap (_current_frame_rate);
117         _tempo_map->StateChanged.connect (mem_fun (*this, &Session::tempo_map_changed));
118
119         atomic_set (&processing_prohibited, 0);
120         send_cnt = 0;
121         insert_cnt = 0;
122         _transport_speed = 0;
123         _last_transport_speed = 0;
124         transport_sub_state = 0;
125         _transport_frame = 0;
126         last_stop_frame = 0;
127         end_location = new Location (0, 0, _("end"), Location::Flags ((Location::IsMark|Location::IsEnd)));
128         _end_location_is_free = true;
129         atomic_set (&_record_status, Disabled);
130         auto_play = false;
131         punch_in = false;
132         punch_out = false;
133         auto_loop = false;
134         seamless_loop = false;
135         loop_changing = false;
136         auto_input = true;
137         crossfades_active = false;
138         all_safe = false;
139         auto_return = false;
140         _last_roll_location = 0;
141         _last_record_location = 0;
142         pending_locate_frame = 0;
143         pending_locate_roll = false;
144         pending_locate_flush = false;
145         dstream_buffer_size = 0;
146         state_tree = 0;
147         state_was_pending = false;
148         set_next_event ();
149         outbound_mtc_smpte_frame = 0;
150         next_quarter_frame_to_send = -1;
151         current_block_size = 0;
152         _solo_latched = true;
153         _solo_model = InverseMute;
154         solo_update_disabled = false;
155         currently_soloing = false;
156         _worst_output_latency = 0;
157         _worst_input_latency = 0;
158         _worst_track_latency = 0;
159         _state_of_the_state = StateOfTheState(CannotSave|InitialConnecting|Loading);
160         _slave = 0;
161         _slave_type = None;
162         butler_mixdown_buffer = 0;
163         butler_gain_buffer = 0;
164         auditioner = 0;
165         mmc_control = false;
166         midi_feedback = false;
167         midi_control = true;
168         mmc = 0;
169         post_transport_work = PostTransportWork (0);
170         atomic_set (&butler_should_do_transport_work, 0);
171         atomic_set (&butler_active, 0);
172         atomic_set (&_playback_load, 100);
173         atomic_set (&_capture_load, 100);
174         atomic_set (&_playback_load_min, 100);
175         atomic_set (&_capture_load_min, 100);
176         pending_audition_region = 0;
177         _edit_mode = Slide;
178         pending_edit_mode = _edit_mode;
179         _play_range = false;
180         align_style = ExistingMaterial;
181         _control_out = 0;
182         _master_out = 0;
183         input_auto_connect = AutoConnectOption (0);
184         output_auto_connect = AutoConnectOption (0);
185         _have_captured = false;
186         waiting_to_start = false;
187         _exporting = false;
188         _gain_automation_buffer = 0;
189         _pan_automation_buffer = 0;
190         _npan_buffers = 0;
191         pending_abort = false;
192         layer_model = MoveAddHigher;
193         xfade_model = ShortCrossfade;
194         
195         /* default short fade = 15ms */
196
197         Crossfade::set_short_xfade_length ((jack_nframes_t) floor ((15.0 * frame_rate()) / 1000.0));
198
199         last_mmc_step.tv_sec = 0;
200         last_mmc_step.tv_usec = 0;
201         step_speed = 0.0;
202
203         preroll.type = AnyTime::Frames;
204         preroll.frames = 0;
205         postroll.type = AnyTime::Frames;
206         postroll.frames = 0;
207
208         /* click sounds are unset by default, which causes us to internal
209            waveforms for clicks.
210         */
211         
212         _click_io = 0;
213         _clicking = false;
214         click_requested = false;
215         click_data = 0;
216         click_emphasis_data = 0;
217         click_length = 0;
218         click_emphasis_length = 0;
219
220         process_function = &Session::process_with_events;
221
222         last_smpte_when = 0;
223         _smpte_offset = 0;
224         _smpte_offset_negative = true;
225         last_smpte_valid = false;
226
227         last_rr_session_dir = session_dirs.begin();
228         refresh_disk_space ();
229
230         // set_default_fade (0.2, 5.0); /* steepness, millisecs */
231
232         /* default configuration */
233
234         recording_plugins = false;
235         over_length_short = 2;
236         over_length_long = 10;
237         send_midi_timecode = false;
238         send_midi_machine_control = false;
239         shuttle_speed_factor = 1.0;
240         shuttle_speed_threshold = 5;
241         rf_speed = 2.0;
242         _meter_hold = 100; // XXX unknown units: number of calls to meter::set()
243         _meter_falloff = 1.5f; // XXX unknown units: refresh_rate
244         max_level = 0;
245         min_level = 0;
246
247         /* slave stuff */
248
249         average_slave_delta = 1800;
250         have_first_delta_accumulator = false;
251         delta_accumulator_cnt = 0;
252         slave_state = Stopped;
253
254         /* default SMPTE type is 30 FPS, non-drop */
255
256         set_smpte_type (30.0, false);
257
258         _engine.GraphReordered.connect (mem_fun (*this, &Session::graph_reordered));
259
260         /* These are all static "per-class" signals */
261
262         Region::CheckNewRegion.connect (mem_fun (*this, &Session::add_region));
263         Source::SourceCreated.connect (mem_fun (*this, &Session::add_source));
264         Playlist::PlaylistCreated.connect (mem_fun (*this, &Session::add_playlist));
265         Redirect::RedirectCreated.connect (mem_fun (*this, &Session::add_redirect));
266         DiskStream::DiskStreamCreated.connect (mem_fun (*this, &Session::add_diskstream));
267         NamedSelection::NamedSelectionCreated.connect (mem_fun (*this, &Session::add_named_selection));
268
269         IO::MoreOutputs.connect (mem_fun (*this, &Session::ensure_passthru_buffers));
270
271         /* stop IO objects from doing stuff until we're ready for them */
272
273         IO::disable_panners ();
274         IO::disable_ports ();
275         IO::disable_connecting ();
276 }
277
278 int
279 Session::second_stage_init (bool new_session)
280 {
281         SndFileSource::set_peak_dir (peak_dir());
282
283         if (!new_session) {
284                 if (load_state (_current_snapshot_name)) {
285                         return -1;
286                 }
287                 remove_empty_sounds ();
288         }
289
290         if (start_butler_thread()) {
291                 return -1;
292         }
293
294         if (start_midi_thread ()) {
295                 return -1;
296         }
297
298         if (init_feedback ()) {
299                 return -1;
300         }
301
302         if (state_tree) {
303                 if (set_state (*state_tree->root())) {
304                         return -1;
305                 }
306         }
307
308         /* we can't save till after ::when_engine_running() is called,
309            because otherwise we save state with no connections made.
310            therefore, we reset _state_of_the_state because ::set_state()
311            will have cleared it.
312
313            we also have to include Loading so that any events that get
314            generated between here and the end of ::when_engine_running()
315            will be processed directly rather than queued.
316         */
317
318         _state_of_the_state = StateOfTheState (_state_of_the_state|CannotSave|Loading);
319
320         // set_auto_input (true);
321         _locations.changed.connect (mem_fun (this, &Session::locations_changed));
322         _locations.added.connect (mem_fun (this, &Session::locations_added));
323         setup_click_sounds (0);
324         setup_midi_control ();
325
326         /* Pay attention ... */
327
328         _engine.Halted.connect (mem_fun (*this, &Session::engine_halted));
329         _engine.Xrun.connect (mem_fun (*this, &Session::xrun_recovery));
330
331         if (_engine.running()) {
332                 when_engine_running();
333         } else {
334                 first_time_running = _engine.Running.connect (mem_fun (*this, &Session::when_engine_running));
335         }
336
337         send_full_time_code ();
338         _engine.transport_locate (0);
339         deliver_mmc (MIDI::MachineControl::cmdMmcReset, 0);
340         deliver_mmc (MIDI::MachineControl::cmdLocate, 0);
341         send_all_midi_feedback();
342
343         if (new_session) {
344                 _end_location_is_free = true;
345         } else {
346                 _end_location_is_free = false;
347         }
348         
349         return 0;
350 }
351
352 string
353 Session::raid_path () const
354 {
355         string path;
356
357         for (vector<space_and_path>::const_iterator i = session_dirs.begin(); i != session_dirs.end(); ++i) {
358                 path += (*i).path;
359                 path += ':';
360         }
361         
362         return path.substr (0, path.length() - 1); // drop final colon
363 }
364
365 void
366 Session::set_raid_path (string path)
367 {
368         /* public-access to setup_raid_path() */
369
370         setup_raid_path (path);
371 }
372
373 void
374 Session::setup_raid_path (string path)
375 {
376         string::size_type colon;
377         string remaining;
378         space_and_path sp;
379         string fspath;
380         string::size_type len = path.length();
381         int colons;
382
383         colons = 0;
384
385         if (path.length() == 0) {
386                 return;
387         }
388
389         session_dirs.clear ();
390
391         for (string::size_type n = 0; n < len; ++n) {
392                 if (path[n] == ':') {
393                         colons++;
394                 }
395         }
396
397         if (colons == 0) {
398
399                 /* no multiple search path, just one directory (common case) */
400
401                 sp.path = path;
402                 sp.blocks = 0;
403                 session_dirs.push_back (sp);
404                 
405                 FileSource::set_search_path (path + sound_dir_name);
406
407                 return;
408         }
409
410         remaining = path;
411
412         while ((colon = remaining.find_first_of (':')) != string::npos) {
413                 
414                 sp.blocks = 0;
415                 sp.path = remaining.substr (0, colon);
416
417                 fspath += sp.path;
418                 if (fspath[fspath.length()-1] != '/') {
419                         fspath += '/';
420                 }
421                 fspath += sound_dir_name;
422                 fspath += ':';
423
424                 session_dirs.push_back (sp);
425
426                 remaining = remaining.substr (colon+1);
427         }
428
429         if (remaining.length()) {
430
431                 sp.blocks = 0;
432                 sp.path = remaining;
433
434                 fspath += sp.path;
435                 if (fspath[fspath.length()-1] != '/') {
436                         fspath += '/';
437                 }
438                 fspath += sound_dir_name;
439
440                 session_dirs.push_back (sp);
441         }
442
443         /* set the FileSource search path */
444
445         FileSource::set_search_path (fspath);
446
447         /* reset the round-robin soundfile path thingie */
448
449         last_rr_session_dir = session_dirs.begin();
450 }
451
452 int
453 Session::create (bool& new_session, string* mix_template, jack_nframes_t initial_length)
454 {
455         string dir;
456         
457         if (mkdir (_path.c_str(), 0755) < 0) {
458                 if (errno == EEXIST) {
459                         new_session = false;
460                 } else {
461                         error << string_compose(_("Session: cannot create session dir \"%1\" (%2)"), _path, strerror (errno)) << endmsg;
462                         return -1;
463                 }
464         } else {
465                 new_session = true;
466         }
467
468         dir = peak_dir ();
469
470         if (mkdir (dir.c_str(), 0755) < 0) {
471                 if (errno != EEXIST) {
472                         error << string_compose(_("Session: cannot create session peakfile dir \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
473                         return -1;
474                 }
475         }
476
477         dir = sound_dir ();
478
479         if (mkdir (dir.c_str(), 0755) < 0) {
480                 if (errno != EEXIST) {
481                         error << string_compose(_("Session: cannot create session sounds dir \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
482                         return -1;
483                 }
484         }
485
486         dir = dead_sound_dir ();
487
488         if (mkdir (dir.c_str(), 0755) < 0) {
489                 if (errno != EEXIST) {
490                         error << string_compose(_("Session: cannot create session dead sounds dir \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
491                         return -1;
492                 }
493         }
494
495         dir = automation_dir ();
496
497         if (mkdir (dir.c_str(), 0755) < 0) {
498                 if (errno != EEXIST) {
499                         error << string_compose(_("Session: cannot create session automation dir \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
500                         return -1;
501                 }
502         }
503
504         
505         /* check new_session so we don't overwrite an existing one */
506         
507         if (mix_template) {
508                 if (new_session){
509
510                         string lookfor = "^";
511                         lookfor += *mix_template;
512                         lookfor += _template_suffix;
513                         lookfor += '$';
514
515                         PathScanner scanner;
516                         string tpath = template_path();
517                         string in_path;
518                         string out_path;
519
520                         vector<string*>* result= scanner (tpath, lookfor, false, true);
521
522                         if (result == 0) {
523                                 error << string_compose (_("Could not find a template called %1 in %2"), *mix_template, tpath)
524                                       << endmsg;
525                                 *mix_template = "";
526                         }
527
528                         if (result->size() == 0) {
529                                 delete result;
530                                 error << string_compose (_("Could not find a template called %1 in %2"), *mix_template, tpath)
531                                       << endmsg;
532                                 *mix_template = "";
533                         }
534
535                         in_path = *(result->front());
536
537                         ifstream in(in_path.c_str());
538                         
539                         if (in){
540                                 string out_path = _path;
541                                 out_path += _name;
542                                 out_path += _statefile_suffix;
543                                 
544                                 ofstream out(out_path.c_str());
545
546                                 if (out){
547                                         out << in.rdbuf();
548                                         
549                                         // okay, session is set up.  Treat like normal saved
550                                         // session from now on.
551                                         
552                                         new_session = false;
553                                         return 0;
554                                         
555                                 } else {
556                                         error << string_compose (_("Could not open %1 for writing mix template"), out_path) 
557                                               << endmsg;
558                                         return -1;
559                                 }
560                                 
561                         } else {
562                                 error << string_compose (_("Could not open mix template %1 for reading"), in_path) 
563                                       << endmsg;
564                                 return -1;
565                         }
566                         
567                         
568                 } else {
569                         warning << _("Session already exists.  Not overwriting") << endmsg;
570                         return -1;
571                 }
572         }
573
574         if (new_session) {
575
576                 /* set an initial end point */
577
578                 end_location->set_end (initial_length);
579                 _locations.add (end_location);
580
581                 _state_of_the_state = Clean;
582
583                 if (save_state (_current_snapshot_name)) {
584                         return -1;
585                 }
586         }
587
588         return 0;
589 }
590
591 int
592 Session::load_diskstreams (const XMLNode& node)
593 {
594         XMLNodeList          clist;
595         XMLNodeConstIterator citer;
596         
597         clist = node.children();
598
599         for (citer = clist.begin(); citer != clist.end(); ++citer) {
600                 
601                 DiskStream* dstream;
602
603                 try {
604                         dstream = new DiskStream (*this, **citer);
605                         /* added automatically by DiskStreamCreated handler */
606                 } 
607                 
608                 catch (failed_constructor& err) {
609                         error << _("Session: could not load diskstream via XML state")                        << endmsg;
610                         return -1;
611                 }
612         }
613
614         return 0;
615 }
616
617 void
618 Session::remove_pending_capture_state ()
619 {
620         string xml_path;
621
622         xml_path = _path;
623         xml_path += _current_snapshot_name;
624         xml_path += _pending_suffix;
625
626         unlink (xml_path.c_str());
627 }
628
629 int
630 Session::save_state (string snapshot_name, bool pending)
631 {
632         XMLTree tree;
633         string xml_path;
634         string bak_path;
635
636         if (_state_of_the_state & CannotSave) {
637                 return 1;
638         }
639
640         tree.set_root (&get_state());
641
642         if (snapshot_name.empty()) {
643                 snapshot_name = _current_snapshot_name;
644         }
645
646         if (!pending) {
647
648                 xml_path = _path;
649                 xml_path += snapshot_name;
650                 xml_path += _statefile_suffix;
651                 bak_path = xml_path;
652                 bak_path += ".bak";
653                 
654                 // Make backup of state file
655                 
656                 if ((access (xml_path.c_str(), F_OK) == 0) &&
657                     (rename(xml_path.c_str(), bak_path.c_str()))) {
658                         error << _("could not backup old state file, current state not saved.") << endmsg;
659                         return -1;
660                 }
661
662         } else {
663
664                 xml_path = _path;
665                 xml_path += snapshot_name;
666                 xml_path += _pending_suffix;
667
668         }
669
670         if (!tree.write (xml_path)) {
671                 error << string_compose (_("state could not be saved to %1"), xml_path) << endmsg;
672
673                 /* don't leave a corrupt file lying around if it is
674                    possible to fix.
675                 */
676
677                 if (unlink (xml_path.c_str())) {
678                         error << string_compose (_("could not remove corrupt state file %1"), xml_path) << endmsg;
679                 } else {
680                         if (!pending) {
681                                 if (rename (bak_path.c_str(), xml_path.c_str())) {
682                                         error << string_compose (_("could not restore state file from backup %1"), bak_path) << endmsg;
683                                 }
684                         }
685                 }
686
687                 return -1;
688         }
689
690         if (!pending) {
691
692                 bool was_dirty = dirty();
693                 
694                 _state_of_the_state = StateOfTheState (_state_of_the_state & ~Dirty);
695                 
696                 if (was_dirty) {
697                         DirtyChanged (); /* EMIT SIGNAL */
698                 }
699                 
700                 StateSaved (snapshot_name); /* EMIT SIGNAL */
701         }
702
703         return 0;
704 }
705
706 int
707 Session::restore_state (string snapshot_name)
708 {
709         if (load_state (snapshot_name) == 0) {
710                 set_state (*state_tree->root());
711         }
712         
713         return 0;
714 }
715
716 int
717 Session::load_state (string snapshot_name)
718 {
719         if (state_tree) {
720                 delete state_tree;
721                 state_tree = 0;
722         }
723
724         string xmlpath;
725         
726         state_was_pending = false;
727
728         /* check for leftover pending state from a crashed capture attempt */
729
730         xmlpath = _path;
731         xmlpath += snapshot_name;
732         xmlpath += _pending_suffix;
733
734         if (!access (xmlpath.c_str(), F_OK)) {
735
736                 /* there is pending state from a crashed capture attempt */
737
738                 if (AskAboutPendingState()) {
739                         state_was_pending = true;
740                 } 
741         } 
742
743         if (!state_was_pending) {
744
745                 xmlpath = _path;
746                 xmlpath += snapshot_name;
747                 xmlpath += _statefile_suffix;
748         }
749
750         if (access (xmlpath.c_str(), F_OK)) {
751                 error << string_compose(_("%1: session state information file \"%2\" doesn't exist!"), _name, xmlpath) << endmsg;
752                 return 1;
753         }
754
755         state_tree = new XMLTree;
756
757         set_dirty();
758
759         if (state_tree->read (xmlpath)) {
760                 return 0;
761         } else {
762                 error << string_compose(_("Could not understand ardour file %1"), xmlpath) << endmsg;
763         }
764
765         delete state_tree;
766         state_tree = 0;
767         return -1;
768 }
769
770 int
771 Session::load_options (const XMLNode& node)
772 {
773         XMLNode* child;
774         XMLProperty* prop;
775         bool have_fade_msecs = false;
776         bool have_fade_steepness = false;
777         float fade_msecs = 0;
778         float fade_steepness = 0;
779         SlaveSource slave_src = None;
780         int x;
781         LocaleGuard lg (X_("POSIX"));
782         
783         if ((child = find_named_node (node, "input-auto-connect")) != 0) {
784                 if ((prop = child->property ("val")) != 0) {
785                         sscanf (prop->value().c_str(), "%x", &x);
786                         input_auto_connect = AutoConnectOption (x);
787                 }
788         }
789
790         if ((child = find_named_node (node, "output-auto-connect")) != 0) {
791                 if ((prop = child->property ("val")) != 0) {
792                         sscanf (prop->value().c_str(), "%x", &x);
793                         output_auto_connect = AutoConnectOption (x);
794                 }
795         }
796                                 
797         if ((child = find_named_node (node, "slave")) != 0) {
798                 if ((prop = child->property ("type")) != 0) {
799                         if (prop->value() == "none") {
800                                 slave_src = None;
801                         } else if (prop->value() == "mtc") {
802                                 slave_src = MTC;
803                         } else if (prop->value() == "jack") {
804                                 slave_src = JACK;
805                         }
806                         set_slave_source (slave_src, 0);
807                 }
808         }
809
810         /* we cannot set edit mode if we are loading a session,
811            because it might destroy the playlist's positioning
812         */
813
814         if ((child = find_named_node (node, "edit-mode")) != 0) {
815                 if ((prop = child->property ("val")) != 0) {
816                         if (prop->value() == "slide") {
817                                 pending_edit_mode = Slide;
818                         } else if (prop->value() == "splice") {
819                                 pending_edit_mode = Splice;
820                         } 
821                 }
822         }
823                                 
824         if ((child = find_named_node (node, "send-midi-timecode")) != 0) {
825                 if ((prop = child->property ("val")) != 0) {
826                         bool x = (prop->value() == "yes");
827                         send_mtc = !x; /* force change in value */
828                         set_send_mtc (x);
829                 }
830         }
831         if ((child = find_named_node (node, "send-midi-machine-control")) != 0) {
832                 if ((prop = child->property ("val")) != 0) {
833                         bool x = (prop->value() == "yes");
834                         send_mmc = !x; /* force change in value */
835                         set_send_mmc (prop->value() == "yes");
836                 }
837         }
838         if ((child = find_named_node (node, "max-level")) != 0) {
839                 if ((prop = child->property ("val")) != 0) {
840                         max_level = atoi (prop->value().c_str());
841                 }
842         }
843         if ((child = find_named_node (node, "min-level")) != 0) {
844                 if ((prop = child->property ("val")) != 0) {
845                         min_level = atoi (prop->value().c_str());
846                 }
847         }
848         if ((child = find_named_node (node, "meter-hold")) != 0) {
849                 if ((prop = child->property ("val")) != 0) {
850                         _meter_hold = atof (prop->value().c_str());
851                 }
852         }
853         if ((child = find_named_node (node, "meter-falloff")) != 0) {
854                 if ((prop = child->property ("val")) != 0) {
855                         _meter_falloff = atof (prop->value().c_str());
856                 }
857         }
858         if ((child = find_named_node (node, "long-over-length")) != 0) {
859                 if ((prop = child->property ("val")) != 0) {
860                         over_length_long = atoi (prop->value().c_str());
861                 }
862         }
863         if ((child = find_named_node (node, "short-over-length")) != 0) {
864                 if ((prop = child->property ("val")) != 0) {
865                         over_length_short = atoi (prop->value().c_str());
866                 }
867         }
868         if ((child = find_named_node (node, "shuttle-speed-factor")) != 0) {
869                 if ((prop = child->property ("val")) != 0) {
870                         shuttle_speed_factor = atof (prop->value().c_str());
871                 }
872         }
873         if ((child = find_named_node (node, "shuttle-speed-threshold")) != 0) {
874                 if ((prop = child->property ("val")) != 0) {
875                         shuttle_speed_threshold = atof (prop->value().c_str());
876                 }
877         }
878         if ((child = find_named_node (node, "rf-speed")) != 0) {
879                 if ((prop = child->property ("val")) != 0) {
880                         rf_speed = atof (prop->value().c_str());
881                 }
882         }
883         if ((child = find_named_node (node, "smpte-frames-per-second")) != 0) {
884                 if ((prop = child->property ("val")) != 0) {
885                         set_smpte_type( atof (prop->value().c_str()), smpte_drop_frames );
886                 }
887         }
888         if ((child = find_named_node (node, "smpte-drop-frames")) != 0) {
889                 if ((prop = child->property ("val")) != 0) {
890                         set_smpte_type( smpte_frames_per_second, (prop->value() == "yes") );
891                 }
892         }
893         if ((child = find_named_node (node, "smpte-offset")) != 0) {
894                 if ((prop = child->property ("val")) != 0) {
895                         set_smpte_offset( atoi (prop->value().c_str()) );
896                 }
897         }
898         if ((child = find_named_node (node, "smpte-offset-negative")) != 0) {
899                 if ((prop = child->property ("val")) != 0) {
900                         set_smpte_offset_negative( (prop->value() == "yes") );
901                 }
902         }
903         if ((child = find_named_node (node, "click-sound")) != 0) {
904                 if ((prop = child->property ("val")) != 0) {
905                         click_sound = prop->value();
906                 }
907         }
908         if ((child = find_named_node (node, "click-emphasis-sound")) != 0) {
909                 if ((prop = child->property ("val")) != 0) {
910                         click_emphasis_sound = prop->value();
911                 }
912         }
913
914         if ((child = find_named_node (node, "solo-model")) != 0) {
915                 if ((prop = child->property ("val")) != 0) {
916                         if (prop->value() == "SoloBus")
917                                 _solo_model = SoloBus;
918                         else
919                                 _solo_model = InverseMute;
920                 }
921         }
922
923         /* BOOLEAN OPTIONS */
924
925         if ((child = find_named_node (node, "auto-play")) != 0) {
926                 if ((prop = child->property ("val")) != 0) {
927                         set_auto_play (prop->value() == "yes");
928                 }
929         }
930         if ((child = find_named_node (node, "auto-input")) != 0) {
931                 if ((prop = child->property ("val")) != 0) {
932                         set_auto_input (prop->value() == "yes");
933                 }
934         }
935         if ((child = find_named_node (node, "seamless-loop")) != 0) {
936                 if ((prop = child->property ("val")) != 0) {
937                         set_seamless_loop (prop->value() == "yes");
938                 }
939         }
940         if ((child = find_named_node (node, "punch-in")) != 0) {
941                 if ((prop = child->property ("val")) != 0) {
942                         set_punch_in (prop->value() == "yes");
943                 }
944         }
945         if ((child = find_named_node (node, "punch-out")) != 0) {
946                 if ((prop = child->property ("val")) != 0) {
947                         set_punch_out (prop->value() == "yes");
948                 }
949         }
950         if ((child = find_named_node (node, "auto-return")) != 0) {
951                 if ((prop = child->property ("val")) != 0) {
952                         set_auto_return (prop->value() == "yes");
953                 }
954         }
955         if ((child = find_named_node (node, "send-mtc")) != 0) {
956                 if ((prop = child->property ("val")) != 0) {
957                         set_send_mtc (prop->value() == "yes");
958                 }
959         }
960         if ((child = find_named_node (node, "mmc-control")) != 0) {
961                 if ((prop = child->property ("val")) != 0) {
962                         set_mmc_control (prop->value() == "yes");
963                 }
964         }
965         if ((child = find_named_node (node, "midi-control")) != 0) {
966                 if ((prop = child->property ("val")) != 0) {
967                         set_midi_control (prop->value() == "yes");
968                 }
969         }
970         if ((child = find_named_node (node, "midi-feedback")) != 0) {
971                 if ((prop = child->property ("val")) != 0) {
972                         set_midi_feedback (prop->value() == "yes");
973                 }
974         }
975         if ((child = find_named_node (node, "recording-plugins")) != 0) {
976                 if ((prop = child->property ("val")) != 0) {
977                         set_recording_plugins (prop->value() == "yes");
978                 }
979         }
980         if ((child = find_named_node (node, "crossfades-active")) != 0) {
981                 if ((prop = child->property ("val")) != 0) {
982                         set_crossfades_active (prop->value() == "yes");
983                 }
984         }
985         if ((child = find_named_node (node, "audible-click")) != 0) {
986                 if ((prop = child->property ("val")) != 0) {
987                         set_clicking (prop->value() == "yes");
988                 }
989         }
990
991         if ((child = find_named_node (node, "align-style")) != 0) {
992                 if ((prop = child->property ("val")) != 0) {
993                         if (prop->value() == "capture") {
994                                 set_align_style (CaptureTime);
995                         } else {
996                                 set_align_style (ExistingMaterial);
997                         }
998                 }
999         }
1000
1001         if ((child = find_named_node (node, "layer-model")) != 0) {
1002                 if ((prop = child->property ("val")) != 0) {
1003                         if (prop->value() == X_("LaterHigher")) {
1004                                 set_layer_model (LaterHigher);
1005                         } else if (prop->value() == X_("AddHigher")) {
1006                                 set_layer_model (AddHigher);
1007                         } else {
1008                                 set_layer_model (MoveAddHigher);
1009                         }
1010                 }
1011         }
1012
1013         if ((child = find_named_node (node, "xfade-model")) != 0) {
1014                 if ((prop = child->property ("val")) != 0) {
1015                         if (prop->value() == X_("Short")) {
1016                                 set_xfade_model (ShortCrossfade);
1017                         } else {
1018                                 set_xfade_model (FullCrossfade);
1019                         }
1020                 }
1021         }
1022
1023         if ((child = find_named_node (node, "short-xfade-length")) != 0) {
1024                 if ((prop = child->property ("val")) != 0) {
1025                         /* value is stored as a fractional seconds */
1026                         float secs = atof (prop->value().c_str());
1027                         Crossfade::set_short_xfade_length ((jack_nframes_t) floor (secs * frame_rate()));
1028                 } 
1029         }
1030
1031         if ((child = find_named_node (node, "full-xfades-unmuted")) != 0) {
1032                 if ((prop = child->property ("val")) != 0) {
1033                         crossfades_active = (prop->value() == "yes");
1034                 }
1035         } 
1036
1037         /* TIED OPTIONS */
1038
1039         if ((child = find_named_node (node, "default-fade-steepness")) != 0) {
1040                 if ((prop = child->property ("val")) != 0) {
1041                         fade_steepness = atof (prop->value().c_str());
1042                         have_fade_steepness = true;
1043                 }
1044         }
1045         if ((child = find_named_node (node, "default-fade-msec")) != 0) {
1046                 if ((prop = child->property ("val")) != 0) {
1047                         fade_msecs = atof (prop->value().c_str());
1048                         have_fade_msecs = true;
1049                 }
1050         }
1051
1052         if (have_fade_steepness || have_fade_msecs) {
1053                 // set_default_fade (fade_steepness, fade_msecs);
1054         }
1055
1056         return 0;
1057 }
1058
1059 XMLNode&
1060 Session::get_options () const
1061 {
1062         XMLNode* opthead;
1063         XMLNode* child;
1064         char buf[32];
1065         LocaleGuard lg (X_("POSIX"));
1066
1067         opthead = new XMLNode ("Options");
1068
1069         SlaveSource src = slave_source ();
1070         string src_string;
1071         switch (src) {
1072         case None:
1073                 src_string = "none";
1074                 break;
1075         case MTC:
1076                 src_string = "mtc";
1077                 break;
1078         case JACK:
1079                 src_string = "jack";
1080                 break;
1081         }
1082         child = opthead->add_child ("slave");
1083         child->add_property ("type", src_string);
1084         
1085         child = opthead->add_child ("send-midi-timecode");
1086         child->add_property ("val", send_midi_timecode?"yes":"no");
1087
1088         child = opthead->add_child ("send-midi-machine-control");
1089         child->add_property ("val", send_midi_machine_control?"yes":"no");
1090
1091         snprintf (buf, sizeof(buf)-1, "%x", (int) input_auto_connect);
1092         child = opthead->add_child ("input-auto-connect");
1093         child->add_property ("val", buf);
1094
1095         snprintf (buf, sizeof(buf)-1, "%x", (int) output_auto_connect);
1096         child = opthead->add_child ("output-auto-connect");
1097         child->add_property ("val", buf);
1098
1099         snprintf (buf, sizeof(buf)-1, "%d", max_level);
1100         child = opthead->add_child ("max-level");
1101         child->add_property ("val", buf);
1102
1103         snprintf (buf, sizeof(buf)-1, "%d", min_level);
1104         child = opthead->add_child ("min-level");
1105         child->add_property ("val", buf);
1106
1107         snprintf (buf, sizeof(buf)-1, "%f", _meter_hold);
1108         child = opthead->add_child ("meter-hold");
1109         child->add_property ("val", buf);
1110
1111         snprintf (buf, sizeof(buf)-1, "%f", _meter_falloff);
1112         child = opthead->add_child ("meter-falloff");
1113         child->add_property ("val", buf);
1114         
1115         snprintf (buf, sizeof(buf)-1, "%u", over_length_long);
1116         child = opthead->add_child ("long-over-length");
1117         child->add_property ("val", buf);
1118
1119         snprintf (buf, sizeof(buf)-1, "%u", over_length_short);
1120         child = opthead->add_child ("short-over-length");
1121         child->add_property ("val", buf);
1122
1123         snprintf (buf, sizeof(buf)-1, "%f", shuttle_speed_factor);
1124         child = opthead->add_child ("shuttle-speed-factor");
1125         child->add_property ("val", buf);
1126
1127         snprintf (buf, sizeof(buf)-1, "%f", shuttle_speed_threshold);
1128         child = opthead->add_child ("shuttle-speed-threshold");
1129         child->add_property ("val", buf);
1130
1131         snprintf (buf, sizeof(buf)-1, "%f", rf_speed);
1132         child = opthead->add_child ("rf-speed");
1133         child->add_property ("val", buf);
1134
1135         snprintf (buf, sizeof(buf)-1, "%.2f", smpte_frames_per_second);
1136         child = opthead->add_child ("smpte-frames-per-second");
1137         child->add_property ("val", buf);
1138         
1139         child = opthead->add_child ("smpte-drop-frames");
1140         child->add_property ("val", smpte_drop_frames ? "yes" : "no");
1141         
1142         snprintf (buf, sizeof(buf)-1, "%u", smpte_offset ());
1143         child = opthead->add_child ("smpte-offset");
1144         child->add_property ("val", buf);
1145         
1146         child = opthead->add_child ("smpte-offset-negative");
1147         child->add_property ("val", smpte_offset_negative () ? "yes" : "no");
1148         
1149         child = opthead->add_child ("edit-mode");
1150         switch (_edit_mode) {
1151         case Splice:
1152                 child->add_property ("val", "splice");
1153                 break;
1154
1155         case Slide:
1156                 child->add_property ("val", "slide");
1157                 break;
1158         }
1159
1160         child = opthead->add_child ("auto-play");
1161         child->add_property ("val", get_auto_play () ? "yes" : "no");
1162         child = opthead->add_child ("auto-input");
1163         child->add_property ("val", get_auto_input () ? "yes" : "no");
1164         child = opthead->add_child ("seamless-loop");
1165         child->add_property ("val", get_seamless_loop () ? "yes" : "no");
1166         child = opthead->add_child ("punch-in");
1167         child->add_property ("val", get_punch_in () ? "yes" : "no");
1168         child = opthead->add_child ("punch-out");
1169         child->add_property ("val", get_punch_out () ? "yes" : "no");
1170         child = opthead->add_child ("all-safe");
1171         child->add_property ("val", get_all_safe () ? "yes" : "no");
1172         child = opthead->add_child ("auto-return");
1173         child->add_property ("val", get_auto_return () ? "yes" : "no");
1174         child = opthead->add_child ("mmc-control");
1175         child->add_property ("val", get_mmc_control () ? "yes" : "no");
1176         child = opthead->add_child ("midi-control");
1177         child->add_property ("val", get_midi_control () ? "yes" : "no");
1178         child = opthead->add_child ("midi-feedback");
1179         child->add_property ("val", get_midi_feedback () ? "yes" : "no");
1180         child = opthead->add_child ("recording-plugins");
1181         child->add_property ("val", get_recording_plugins () ? "yes" : "no");
1182         child = opthead->add_child ("auto-crossfade");
1183         child->add_property ("val", get_crossfades_active () ? "yes" : "no");
1184         child = opthead->add_child ("audible-click");
1185         child->add_property ("val", get_clicking () ? "yes" : "no");
1186
1187         if (click_sound.length()) {
1188                 child = opthead->add_child ("click-sound");
1189                 child->add_property ("val", click_sound);
1190         }
1191
1192         if (click_emphasis_sound.length()) {
1193                 child = opthead->add_child ("click-emphasis-sound");
1194                 child->add_property ("val", click_emphasis_sound);
1195         }
1196
1197         child = opthead->add_child ("solo-model");
1198         child->add_property ("val", _solo_model == SoloBus ? "SoloBus" : "InverseMute");
1199
1200         child = opthead->add_child ("align-style");
1201         child->add_property ("val", (align_style == ExistingMaterial ? "existing" : "capture"));
1202
1203         child = opthead->add_child ("layer-model");
1204         switch (layer_model) {
1205         case LaterHigher:
1206                 child->add_property ("val", X_("LaterHigher"));
1207                 break;
1208         case MoveAddHigher:
1209                 child->add_property ("val", X_("MoveAddHigher"));
1210                 break;
1211         case AddHigher:
1212                 child->add_property ("val", X_("AddHigher"));
1213                 break;
1214         }
1215
1216         child = opthead->add_child ("xfade-model");
1217         switch (xfade_model) {
1218         case FullCrossfade:
1219                 child->add_property ("val", X_("Full"));
1220                 break;
1221         case ShortCrossfade:
1222                 child->add_property ("val", X_("Short"));
1223         }
1224
1225         child = opthead->add_child ("short-xfade-length");
1226         /* store as fractions of a second */
1227         snprintf (buf, sizeof(buf)-1, "%f", 
1228                   (float) Crossfade::short_xfade_length() / frame_rate());
1229         child->add_property ("val", buf);
1230
1231         child = opthead->add_child ("full-xfades-unmuted");
1232         child->add_property ("val", crossfades_active ? "yes" : "no");
1233
1234         return *opthead;
1235 }
1236
1237 XMLNode&
1238 Session::get_state()
1239 {
1240         return state(true);
1241 }
1242
1243 XMLNode&
1244 Session::get_template()
1245 {
1246         /* if we don't disable rec-enable, diskstreams
1247            will believe they need to store their capture
1248            sources in their state node. 
1249         */
1250         
1251         disable_record ();
1252
1253         cerr << "STart get template\n";
1254         return state(false);
1255 }
1256
1257 XMLNode&
1258 Session::state(bool full_state)
1259 {
1260         XMLNode* node = new XMLNode("Session");
1261         XMLNode* child;
1262
1263         // store libardour version, just in case
1264         char buf[16];
1265         snprintf(buf, sizeof(buf)-1, "%d.%d.%d", 
1266                  libardour_major_version, libardour_minor_version, libardour_micro_version);
1267         node->add_property("version", string(buf));
1268                 
1269         /* store configuration settings */
1270
1271         if (full_state) {
1272         
1273                 /* store the name */
1274                 node->add_property ("name", _name);
1275
1276                 if (session_dirs.size() > 1) {
1277
1278                         string p;
1279
1280                         vector<space_and_path>::iterator i = session_dirs.begin();
1281                         vector<space_and_path>::iterator next;
1282
1283                         ++i; /* skip the first one */
1284                         next = i;
1285                         ++next;
1286
1287                         while (i != session_dirs.end()) {
1288
1289                                 p += (*i).path;
1290
1291                                 if (next != session_dirs.end()) {
1292                                         p += ':';
1293                                 } else {
1294                                         break;
1295                                 }
1296
1297                                 ++next;
1298                                 ++i;
1299                         }
1300                         
1301                         child = node->add_child ("Path");
1302                         child->add_content (p);
1303                 }
1304         }
1305
1306         node->add_child_nocopy (get_options());
1307
1308         child = node->add_child ("Sources");
1309
1310         if (full_state) {
1311                 LockMonitor sl (source_lock, __LINE__, __FILE__);
1312
1313                 for (SourceList::iterator siter = sources.begin(); siter != sources.end(); ++siter) {
1314                         
1315                         /* Don't save information about FileSources that are empty */
1316                         
1317                         FileSource* fs;
1318                         
1319                         if ((fs = dynamic_cast<FileSource*> ((*siter).second)) != 0) {
1320                                 if (fs->length() == 0) {
1321                                         continue;
1322                                 }
1323                         }
1324                         
1325                         child->add_child_nocopy ((*siter).second->get_state());
1326                 }
1327         }
1328
1329         child = node->add_child ("Regions");
1330
1331         if (full_state) { 
1332                 LockMonitor rl (region_lock, __LINE__, __FILE__);
1333
1334                 for (AudioRegionList::const_iterator i = audio_regions.begin(); i != audio_regions.end(); ++i) {
1335                         
1336                         /* only store regions not attached to playlists */
1337
1338                         if ((*i).second->playlist() == 0) {
1339                                 child->add_child_nocopy (i->second->state (true));
1340                         }
1341                 }
1342         }
1343
1344         child = node->add_child ("DiskStreams");
1345
1346         { 
1347                 LockMonitor dl (diskstream_lock, __LINE__, __FILE__);
1348                 for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
1349                         if (!(*i)->hidden()) {
1350                                 child->add_child_nocopy ((*i)->get_state());
1351                         }
1352                 }
1353         }
1354
1355         node->add_child_nocopy (_locations.get_state());
1356         
1357         child = node->add_child ("Connections");
1358         {
1359                 LockMonitor lm (connection_lock, __LINE__, __FILE__);
1360                 for (ConnectionList::iterator i = _connections.begin(); i != _connections.end(); ++i) {
1361                         if (!(*i)->system_dependent()) {
1362                                 child->add_child_nocopy ((*i)->get_state());
1363                         }
1364                 }
1365         }
1366
1367         child = node->add_child ("Routes");
1368         {
1369                 LockMonitor lm (route_lock, __LINE__, __FILE__);
1370                 
1371                 RoutePublicOrderSorter cmp;
1372                 RouteList public_order(routes);
1373                 public_order.sort (cmp);
1374                 
1375                 for (RouteList::iterator i = public_order.begin(); i != public_order.end(); ++i) {
1376                         if (!(*i)->hidden()) {
1377                                 if (full_state) {
1378                                         child->add_child_nocopy ((*i)->get_state());
1379                                 } else {
1380                                         child->add_child_nocopy ((*i)->get_template());
1381                                 }
1382                         }
1383                 }
1384         }
1385
1386         
1387         child = node->add_child ("EditGroups");
1388         for (list<RouteGroup *>::iterator i = edit_groups.begin(); i != edit_groups.end(); ++i) {
1389                 child->add_child_nocopy ((*i)->get_state());
1390         }
1391
1392         child = node->add_child ("MixGroups");
1393         for (list<RouteGroup *>::iterator i = mix_groups.begin(); i != mix_groups.end(); ++i) {
1394                 child->add_child_nocopy ((*i)->get_state());
1395         }
1396
1397         child = node->add_child ("Playlists");
1398         for (PlaylistList::iterator i = playlists.begin(); i != playlists.end(); ++i) {
1399                 if (!(*i)->hidden()) {
1400                         if (!(*i)->empty()) {
1401                                 if (full_state) {
1402                                         child->add_child_nocopy ((*i)->get_state());
1403                                 } else {
1404                                         child->add_child_nocopy ((*i)->get_template());
1405                                 }
1406                         }
1407                 }
1408         }
1409
1410         child = node->add_child ("UnusedPlaylists");
1411         for (PlaylistList::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
1412                 if (!(*i)->hidden()) {
1413                         if (!(*i)->empty()) {
1414                                 if (full_state) {
1415                                         child->add_child_nocopy ((*i)->get_state());
1416                                 } else {
1417                                         child->add_child_nocopy ((*i)->get_template());
1418                                 }
1419                         }
1420                 }
1421         }
1422         
1423         
1424         if (_click_io) {
1425                 child = node->add_child ("Click");
1426                 child->add_child_nocopy (_click_io->state (full_state));
1427         }
1428
1429         if (full_state) {
1430                 child = node->add_child ("NamedSelections");
1431                 for (NamedSelectionList::iterator i = named_selections.begin(); i != named_selections.end(); ++i) {
1432                         if (full_state) {
1433                                 child->add_child_nocopy ((*i)->get_state());
1434                         } 
1435                 }
1436         }
1437
1438         node->add_child_nocopy (_tempo_map->get_state());
1439
1440         if (_extra_xml) {
1441                 node->add_child_copy (*_extra_xml);
1442         }
1443
1444         return *node;
1445 }
1446
1447 int
1448 Session::set_state (const XMLNode& node)
1449 {
1450         XMLNodeList nlist;
1451         XMLNode* child;
1452         const XMLProperty* prop;
1453
1454         _state_of_the_state = StateOfTheState (_state_of_the_state|CannotSave);
1455         
1456         if (node.name() != "Session"){
1457                 fatal << _("programming error: Session: incorrect XML node sent to set_state()") << endmsg;
1458                 return -1;
1459         }
1460
1461         if ((prop = node.property ("name")) != 0) {
1462                 _name = prop->value ();
1463         }
1464         
1465         IO::disable_ports ();
1466         IO::disable_connecting ();
1467
1468         /* Object loading order:
1469
1470         MIDI
1471         Path
1472         extra
1473         Options
1474         Sources
1475         AudioRegions
1476         DiskStreams
1477         Connections
1478         Locations
1479         Routes
1480         EditGroups
1481         MixGroups
1482         Click
1483         */
1484
1485         if (use_config_midi_ports ()) {
1486                 return -1;
1487         }
1488
1489         if ((child = find_named_node (node, "Path")) != 0) {
1490                 /* XXX this XML content stuff horrible API design */
1491                 string raid_path = _path + ':' + child->children().front()->content();
1492                 setup_raid_path (raid_path);
1493         } else {
1494                 /* the path is already set */
1495         }
1496
1497         if ((child = find_named_node (node, "extra")) != 0) {
1498                 _extra_xml = new XMLNode (*child);
1499         }
1500
1501         if ((child = find_named_node (node, "Options")) == 0) {
1502                 error << _("Session: XML state has no options section") << endmsg;
1503                 return -1;
1504         } else if (load_options (*child)) {
1505                 return -1;
1506         }
1507
1508         if ((child = find_named_node (node, "Sources")) == 0) {
1509                 error << _("Session: XML state has no sources section") << endmsg;
1510                 return -1;
1511         } else if (load_sources (*child)) {
1512                 return -1;
1513         }
1514
1515         if ((child = find_named_node (node, "Regions")) == 0) {
1516                 error << _("Session: XML state has no Regions section") << endmsg;
1517                 return -1;
1518         } else if (load_regions (*child)) {
1519                 return -1;
1520         }
1521
1522         if ((child = find_named_node (node, "Playlists")) == 0) {
1523                 error << _("Session: XML state has no playlists section") << endmsg;
1524                 return -1;
1525         } else if (load_playlists (*child)) {
1526                 return -1;
1527         }
1528
1529         if ((child = find_named_node (node, "UnusedPlaylists")) == 0) {
1530                 // this is OK
1531         } else if (load_unused_playlists (*child)) {
1532                 return -1;
1533         }
1534         
1535         if ((child = find_named_node (node, "NamedSelections")) != 0) {
1536                 if (load_named_selections (*child)) {
1537                         return -1;
1538                 }
1539         }
1540
1541         if ((child = find_named_node (node, "DiskStreams")) == 0) {
1542                 error << _("Session: XML state has no diskstreams section") << endmsg;
1543                 return -1;
1544         } else if (load_diskstreams (*child)) {
1545                 return -1;
1546         }
1547
1548         if ((child = find_named_node (node, "Connections")) == 0) {
1549                 error << _("Session: XML state has no connections section") << endmsg;
1550                 return -1;
1551         } else if (load_connections (*child)) {
1552                 return -1;
1553         }
1554
1555         if ((child = find_named_node (node, "Locations")) == 0) {
1556                 error << _("Session: XML state has no locations section") << endmsg;
1557                 return -1;
1558         } else if (_locations.set_state (*child)) {
1559                 return -1;
1560         }
1561
1562         Location* location;
1563
1564         if ((location = _locations.auto_loop_location()) != 0) {
1565                 set_auto_loop_location (location);
1566         }
1567
1568         if ((location = _locations.auto_punch_location()) != 0) {
1569                 set_auto_punch_location (location);
1570         }
1571
1572         if ((location = _locations.end_location()) == 0) {
1573                 _locations.add (end_location);
1574         } else {
1575                 end_location = location;
1576         }
1577
1578         _locations.save_state (_("initial state"));
1579
1580         if ((child = find_named_node (node, "EditGroups")) == 0) {
1581                 error << _("Session: XML state has no edit groups section") << endmsg;
1582                 return -1;
1583         } else if (load_edit_groups (*child)) {
1584                 return -1;
1585         }
1586
1587         if ((child = find_named_node (node, "MixGroups")) == 0) {
1588                 error << _("Session: XML state has no mix groups section") << endmsg;
1589                 return -1;
1590         } else if (load_mix_groups (*child)) {
1591                 return -1;
1592         }
1593
1594         if ((child = find_named_node (node, "TempoMap")) == 0) {
1595                 error << _("Session: XML state has no Tempo Map section") << endmsg;
1596                 return -1;
1597         } else if (_tempo_map->set_state (*child)) {
1598                 return -1;
1599         }
1600
1601         if ((child = find_named_node (node, "Routes")) == 0) {
1602                 error << _("Session: XML state has no routes section") << endmsg;
1603                 return -1;
1604         } else if (load_routes (*child)) {
1605                 return -1;
1606         }
1607
1608         if ((child = find_named_node (node, "Click")) == 0) {
1609                 warning << _("Session: XML state has no click section") << endmsg;
1610         } else if (_click_io) {
1611                 _click_io->set_state (*child);
1612         }
1613         
1614         /* OK, now we can set edit mode */
1615
1616         set_edit_mode (pending_edit_mode);
1617
1618         /* here beginneth the second phase ... */
1619
1620         StateReady (); /* EMIT SIGNAL */
1621
1622         _state_of_the_state = Clean;
1623
1624         if (state_was_pending) {
1625                 save_state (_current_snapshot_name);
1626                 remove_pending_capture_state ();
1627                 state_was_pending = false;
1628         }
1629
1630         return 0;
1631 }
1632
1633 int
1634 Session::load_routes (const XMLNode& node)
1635 {
1636         XMLNodeList nlist;
1637         XMLNodeConstIterator niter;
1638         Route *route;
1639
1640         nlist = node.children();
1641
1642         set_dirty();
1643
1644         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1645
1646                 if ((route = XMLRouteFactory (**niter)) == 0) {
1647                         error << _("Session: cannot create Route from XML description.")                              << endmsg;
1648                         return -1;
1649                 }
1650
1651                 add_route (route);
1652         }
1653
1654         return 0;
1655 }
1656
1657 Route *
1658 Session::XMLRouteFactory (const XMLNode& node)
1659 {
1660         if (node.name() != "Route") {
1661                 return 0;
1662         }
1663
1664         if (node.property ("diskstream") != 0 || node.property ("diskstream-id") != 0) {
1665                 return new AudioTrack (*this, node);
1666         } else {
1667                 return new Route (*this, node);
1668         }
1669 }
1670
1671 int
1672 Session::load_regions (const XMLNode& node)
1673 {
1674         XMLNodeList nlist;
1675         XMLNodeConstIterator niter;
1676         AudioRegion* region;
1677
1678         nlist = node.children();
1679
1680         set_dirty();
1681
1682         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1683
1684                 if ((region = XMLRegionFactory (**niter, false)) == 0) {
1685                         error << _("Session: cannot create Region from XML description.") << endmsg;
1686                 }
1687         }
1688
1689         return 0;
1690 }
1691
1692 AudioRegion *
1693 Session::XMLRegionFactory (const XMLNode& node, bool full)
1694 {
1695         const XMLProperty* prop;
1696         id_t s_id;
1697         Source* source;
1698         AudioRegion::SourceList sources;
1699         uint32_t nchans = 1;
1700         char buf[128];
1701         
1702         if (node.name() != X_("Region")) {
1703                 return 0;
1704         }
1705
1706         if ((prop = node.property (X_("channels"))) != 0) {
1707                 nchans = atoi (prop->value().c_str());
1708         }
1709
1710         
1711         if ((prop = node.property (X_("source-0"))) == 0) {
1712                 if ((prop = node.property ("source")) == 0) {
1713                         error << _("Session: XMLNode describing a AudioRegion is incomplete (no source)") << endmsg;
1714                         return 0;
1715                 }
1716         }
1717
1718         sscanf (prop->value().c_str(), "%" PRIu64, &s_id);
1719
1720         if ((source = get_source (s_id)) == 0) {
1721                 error << string_compose(_("Session: XMLNode describing a AudioRegion references an unknown source id =%1"), s_id) << endmsg;
1722                 return 0;
1723         }
1724
1725         sources.push_back(source);
1726
1727         /* pickup other channels */
1728
1729         for (uint32_t n=1; n < nchans; ++n) {
1730                 snprintf (buf, sizeof(buf), X_("source-%d"), n);
1731                 if ((prop = node.property (buf)) != 0) {
1732                         sscanf (prop->value().c_str(), "%" PRIu64, &s_id);
1733                         
1734                         if ((source = get_source (s_id)) == 0) {
1735                                 error << string_compose(_("Session: XMLNode describing a AudioRegion references an unknown source id =%1"), s_id) << endmsg;
1736                                 return 0;
1737                         }
1738                         sources.push_back(source);
1739                 }
1740         }
1741         
1742         
1743         try {
1744                 return new AudioRegion (sources, node);
1745         }
1746
1747         catch (failed_constructor& err) {
1748                 return 0;
1749         }
1750 }
1751
1752 XMLNode&
1753 Session::get_sources_as_xml ()
1754
1755 {
1756         XMLNode* node = new XMLNode (X_("Sources"));
1757         LockMonitor lm (source_lock, __LINE__, __FILE__);
1758
1759         for (SourceList::iterator i = sources.begin(); i != sources.end(); ++i) {
1760                 node->add_child_nocopy ((*i).second->get_state());
1761         }
1762
1763         return *node;
1764 }
1765
1766 string
1767 Session::path_from_region_name (string name, string identifier)
1768 {
1769         char buf[PATH_MAX+1];
1770         uint32_t n;
1771         string dir = discover_best_sound_dir ();
1772
1773         for (n = 0; n < 999999; ++n) {
1774                 if (identifier.length()) {
1775                         snprintf (buf, sizeof(buf), "%s/%s%s%" PRIu32 ".wav", dir.c_str(), name.c_str(), 
1776                                   identifier.c_str(), n);
1777                 } else {
1778                         snprintf (buf, sizeof(buf), "%s/%s-%" PRIu32 ".wav", dir.c_str(), name.c_str(), n);
1779                 }
1780                 if (access (buf, F_OK) != 0) {
1781                         return buf;
1782                 }
1783         }
1784
1785         return "";
1786 }
1787         
1788
1789 int
1790 Session::load_sources (const XMLNode& node)
1791 {
1792         XMLNodeList nlist;
1793         XMLNodeConstIterator niter;
1794         Source* source;
1795
1796         nlist = node.children();
1797
1798         set_dirty();
1799
1800         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1801
1802                 if ((source = XMLSourceFactory (**niter)) == 0) {
1803                         error << _("Session: cannot create Source from XML description.") << endmsg;
1804                 }
1805         }
1806
1807         return 0;
1808 }
1809
1810 Source *
1811 Session::XMLSourceFactory (const XMLNode& node)
1812 {
1813         Source *src;
1814
1815         if (node.name() != "Source") {
1816                 return 0;
1817         }
1818
1819         try {
1820                 src = new FileSource (node, frame_rate());
1821         }
1822         
1823         catch (failed_constructor& err) {
1824
1825                 try {
1826                         src = new SndFileSource (node);
1827                 }
1828
1829                 catch (failed_constructor& err) {
1830                         error << _("Found a sound file that cannot be used by Ardour. See the progammers.") << endmsg;
1831                         return 0;
1832                 } 
1833         }
1834
1835         return src;
1836 }
1837
1838 int
1839 Session::save_template (string template_name)
1840 {
1841         XMLTree tree;
1842         string xml_path, bak_path, template_path;
1843
1844         if (_state_of_the_state & CannotSave) {
1845                 return -1;
1846         }
1847
1848         DIR* dp;
1849         string dir = template_dir();
1850
1851         if ((dp = opendir (dir.c_str()))) {
1852                 closedir (dp);
1853         } else {
1854                 if (mkdir (dir.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)<0) {
1855                         error << string_compose(_("Could not create mix templates directory \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
1856                         return -1;
1857                 }
1858         }
1859
1860         tree.set_root (&get_template());
1861
1862         xml_path = dir;
1863         xml_path += template_name;
1864         xml_path += _template_suffix;
1865
1866         ifstream in(xml_path.c_str());
1867         
1868         if (in) {
1869                 warning << string_compose(_("Template \"%1\" already exists - new version not created"), template_name) << endmsg;
1870                 return -1;
1871         } else {
1872                 in.close();
1873         }
1874
1875         if (!tree.write (xml_path)) {
1876                 error << _("mix template not saved") << endmsg;
1877                 return -1;
1878         }
1879
1880         return 0;
1881 }
1882
1883 int
1884 Session::rename_template (string old_name, string new_name) 
1885 {
1886         string old_path = template_dir() + old_name + _template_suffix;
1887         string new_path = template_dir() + new_name + _template_suffix;
1888
1889         return rename (old_path.c_str(), new_path.c_str());
1890 }
1891
1892 int
1893 Session::delete_template (string name) 
1894 {
1895         string template_path = template_dir();
1896         template_path += name;
1897         template_path += _template_suffix;
1898
1899         return remove (template_path.c_str());
1900 }
1901
1902 void
1903 Session::refresh_disk_space ()
1904 {
1905 #if HAVE_SYS_VFS_H
1906         struct statfs statfsbuf;
1907         vector<space_and_path>::iterator i;
1908         LockMonitor lm (space_lock, __LINE__, __FILE__);
1909         double scale;
1910
1911         /* get freespace on every FS that is part of the session path */
1912
1913         _total_free_4k_blocks = 0;
1914         
1915         for (i = session_dirs.begin(); i != session_dirs.end(); ++i) {
1916                 statfs ((*i).path.c_str(), &statfsbuf);
1917
1918                 scale = statfsbuf.f_bsize/4096.0;
1919
1920                 (*i).blocks = (uint32_t) floor (statfsbuf.f_bavail * scale);
1921                 _total_free_4k_blocks += (*i).blocks;
1922         }
1923 #endif
1924 }
1925
1926 int
1927 Session::ensure_sound_dir (string path, string& result)
1928 {
1929         string dead;
1930         string peak;
1931
1932         /* Ensure that the parent directory exists */
1933         
1934         if (mkdir (path.c_str(), 0775)) {
1935                 if (errno != EEXIST) {
1936                         error << string_compose(_("cannot create session directory \"%1\"; ignored"), path) << endmsg;
1937                         return -1;
1938                 }
1939         }
1940         
1941         /* Ensure that the sounds directory exists */
1942         
1943         result = path;
1944         result += '/';
1945         result += sound_dir_name;
1946         
1947         if (mkdir (result.c_str(), 0775)) {
1948                 if (errno != EEXIST) {
1949                         error << string_compose(_("cannot create sounds directory \"%1\"; ignored"), result) << endmsg;
1950                         return -1;
1951                 }
1952         }
1953
1954         dead = path;
1955         dead += '/';
1956         dead += dead_sound_dir_name;
1957         
1958         if (mkdir (dead.c_str(), 0775)) {
1959                 if (errno != EEXIST) {
1960                         error << string_compose(_("cannot create dead sounds directory \"%1\"; ignored"), dead) << endmsg;
1961                         return -1;
1962                 }
1963         }
1964
1965         peak = path;
1966         peak += '/';
1967         peak += peak_dir_name;
1968         
1969         if (mkdir (peak.c_str(), 0775)) {
1970                 if (errno != EEXIST) {
1971                         error << string_compose(_("cannot create peak file directory \"%1\"; ignored"), peak) << endmsg;
1972                         return -1;
1973                 }
1974         }
1975         
1976         /* callers expect this to be terminated ... */
1977                         
1978         result += '/';
1979         return 0;
1980 }       
1981
1982 string
1983 Session::discover_best_sound_dir ()
1984 {
1985         vector<space_and_path>::iterator i;
1986         string result;
1987
1988         /* handle common case without system calls */
1989
1990         if (session_dirs.size() == 1) {
1991                 return sound_dir();
1992         }
1993
1994         /* OK, here's the algorithm we're following here:
1995
1996         We want to select which directory to use for 
1997         the next file source to be created. Ideally,
1998         we'd like to use a round-robin process so as to
1999         get maximum performance benefits from splitting
2000         the files across multiple disks.
2001
2002         However, in situations without much diskspace, an
2003         RR approach may end up filling up a filesystem
2004         with new files while others still have space.
2005         Its therefore important to pay some attention to
2006         the freespace in the filesystem holding each
2007         directory as well. However, if we did that by
2008         itself, we'd keep creating new files in the file
2009         system with the most space until it was as full
2010         as all others, thus negating any performance
2011         benefits of this RAID-1 like approach.
2012
2013         So, we use a user-configurable space threshold. If
2014         there are at least 2 filesystems with more than this
2015         much space available, we use RR selection between them. 
2016         If not, then we pick the filesystem with the most space.
2017
2018         This gets a good balance between the two
2019         approaches.  
2020         */
2021         
2022         refresh_disk_space ();
2023         
2024         int free_enough = 0;
2025
2026         for (i = session_dirs.begin(); i != session_dirs.end(); ++i) {
2027                 if ((*i).blocks * 4096 >= Config->get_disk_choice_space_threshold()) {
2028                         free_enough++;
2029                 }
2030         }
2031
2032         if (free_enough >= 2) {
2033
2034                 bool found_it = false;
2035
2036                 /* use RR selection process, ensuring that the one
2037                    picked works OK.
2038                 */
2039
2040                 i = last_rr_session_dir;
2041
2042                 do {
2043                         if (++i == session_dirs.end()) {
2044                                 i = session_dirs.begin();
2045                         }
2046
2047                         if ((*i).blocks * 4096 >= Config->get_disk_choice_space_threshold()) {
2048                                 if (ensure_sound_dir ((*i).path, result) == 0) {
2049                                         last_rr_session_dir = i;
2050                                         found_it = true;
2051                                         break;
2052                                 }
2053                         }
2054
2055                 } while (i != last_rr_session_dir);
2056
2057                 if (!found_it) {
2058                         result = sound_dir();
2059                 }
2060
2061         } else {
2062
2063                 /* pick FS with the most freespace (and that
2064                    seems to actually work ...)
2065                 */
2066                 
2067                 vector<space_and_path> sorted;
2068                 space_and_path_ascending_cmp cmp;
2069
2070                 sorted = session_dirs;
2071                 sort (sorted.begin(), sorted.end(), cmp);
2072                 
2073                 for (i = sorted.begin(); i != sorted.end(); ++i) {
2074                         if (ensure_sound_dir ((*i).path, result) == 0) {
2075                                 last_rr_session_dir = i;
2076                                 break;
2077                         }
2078                 }
2079                 
2080                 /* if the above fails, fall back to the most simplistic solution */
2081                 
2082                 if (i == sorted.end()) {
2083                         return sound_dir();
2084                 } 
2085         }
2086
2087         return result;
2088 }
2089
2090 int
2091 Session::load_playlists (const XMLNode& node)
2092 {
2093         XMLNodeList nlist;
2094         XMLNodeConstIterator niter;
2095         Playlist *playlist;
2096
2097         nlist = node.children();
2098
2099         set_dirty();
2100
2101         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2102                 
2103                 if ((playlist = XMLPlaylistFactory (**niter)) == 0) {
2104                         error << _("Session: cannot create Playlist from XML description.") << endmsg;
2105                 }
2106         }
2107
2108         return 0;
2109 }
2110
2111 int
2112 Session::load_unused_playlists (const XMLNode& node)
2113 {
2114         XMLNodeList nlist;
2115         XMLNodeConstIterator niter;
2116         Playlist *playlist;
2117
2118         nlist = node.children();
2119
2120         set_dirty();
2121
2122         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2123                 
2124                 if ((playlist = XMLPlaylistFactory (**niter)) == 0) {
2125                         error << _("Session: cannot create Playlist from XML description.") << endmsg;
2126                         continue;
2127                 }
2128
2129                 // now manually untrack it
2130
2131                 track_playlist (playlist, false);
2132         }
2133
2134         return 0;
2135 }
2136
2137
2138 Playlist *
2139 Session::XMLPlaylistFactory (const XMLNode& node)
2140 {
2141         try {
2142                 return new AudioPlaylist (*this, node);
2143         }
2144
2145         catch (failed_constructor& err) {
2146                 return 0;
2147         }
2148 }
2149
2150 int
2151 Session::load_named_selections (const XMLNode& node)
2152 {
2153         XMLNodeList nlist;
2154         XMLNodeConstIterator niter;
2155         NamedSelection *ns;
2156
2157         nlist = node.children();
2158
2159         set_dirty();
2160
2161         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2162                 
2163                 if ((ns = XMLNamedSelectionFactory (**niter)) == 0) {
2164                         error << _("Session: cannot create Named Selection from XML description.") << endmsg;
2165                 }
2166         }
2167
2168         return 0;
2169 }
2170
2171 NamedSelection *
2172 Session::XMLNamedSelectionFactory (const XMLNode& node)
2173 {
2174         try {
2175                 return new NamedSelection (*this, node);
2176         }
2177
2178         catch (failed_constructor& err) {
2179                 return 0;
2180         }
2181 }
2182
2183 string
2184 Session::dead_sound_dir () const
2185 {
2186         string res = _path;
2187         res += dead_sound_dir_name;
2188         res += '/';
2189         return res;
2190 }
2191
2192 string
2193 Session::sound_dir () const
2194 {
2195         string res = _path;
2196         res += sound_dir_name;
2197         res += '/';
2198         return res;
2199 }
2200
2201 string
2202 Session::peak_dir () const
2203 {
2204         string res = _path;
2205         res += peak_dir_name;
2206         res += '/';
2207         return res;
2208 }
2209         
2210 string
2211 Session::automation_dir () const
2212 {
2213         string res = _path;
2214         res += "automation/";
2215         return res;
2216 }
2217
2218 string
2219 Session::template_dir ()
2220 {
2221         string path = Config->get_user_ardour_path();
2222         path += "templates/";
2223
2224         return path;
2225 }
2226
2227 string
2228 Session::template_path ()
2229 {
2230         string path;
2231
2232         path += Config->get_user_ardour_path();
2233         if (path[path.length()-1] != ':') {
2234                 path += ':';
2235         }
2236         path += Config->get_system_ardour_path();
2237
2238         vector<string> split_path;
2239         
2240         split (path, split_path, ':');
2241         path = "";
2242
2243         for (vector<string>::iterator i = split_path.begin(); i != split_path.end(); ++i) {
2244                 path += *i;
2245                 path += "templates/";
2246                 
2247                 if (distance (i, split_path.end()) != 1) {
2248                         path += ':';
2249                 }
2250         }
2251                 
2252         return path;
2253 }
2254
2255 int
2256 Session::load_connections (const XMLNode& node)
2257 {
2258         XMLNodeList nlist = node.children();
2259         XMLNodeConstIterator niter;
2260
2261         set_dirty();
2262
2263         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2264                 if ((*niter)->name() == "InputConnection") {
2265                         add_connection (new ARDOUR::InputConnection (**niter));
2266                 } else if ((*niter)->name() == "OutputConnection") {
2267                         add_connection (new ARDOUR::OutputConnection (**niter));
2268                 } else {
2269                         error << string_compose(_("Unknown node \"%1\" found in Connections list from state file"), (*niter)->name()) << endmsg;
2270                         return -1;
2271                 }
2272         }
2273
2274         return 0;
2275 }                               
2276
2277 int
2278 Session::load_edit_groups (const XMLNode& node)
2279 {
2280         return load_route_groups (node, true);
2281 }
2282
2283 int
2284 Session::load_mix_groups (const XMLNode& node)
2285 {
2286         return load_route_groups (node, false);
2287 }
2288
2289 int
2290 Session::load_route_groups (const XMLNode& node, bool edit)
2291 {
2292         XMLNodeList nlist = node.children();
2293         XMLNodeConstIterator niter;
2294         RouteGroup* route;
2295
2296         set_dirty();
2297
2298         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2299                 if ((*niter)->name() == "RouteGroup") {
2300                         if (edit) {
2301                                 route = add_edit_group ("");
2302                                 route->set_state (**niter);
2303                         } else {
2304                                 route = add_mix_group ("");
2305                                 route->set_state (**niter);
2306                         }
2307                 }
2308         }
2309         
2310         return 0;
2311 }                               
2312
2313 void
2314 Session::swap_configuration(Configuration** new_config)
2315 {
2316         LockMonitor lm (route_lock, __LINE__, __FILE__);
2317         Configuration* tmp = *new_config;
2318         *new_config = Config;
2319         Config = tmp;
2320         set_dirty();
2321 }
2322
2323 void
2324 Session::copy_configuration(Configuration* new_config)
2325 {
2326         LockMonitor lm (route_lock, __LINE__, __FILE__);
2327         new_config = new Configuration(*Config);
2328 }
2329
2330 static bool
2331 state_file_filter (const string &str, void *arg)
2332 {
2333         return (str.length() > strlen(Session::statefile_suffix()) &&
2334                 str.find (Session::statefile_suffix()) == (str.length() - strlen (Session::statefile_suffix())));
2335 }
2336
2337 struct string_cmp {
2338         bool operator()(const string* a, const string* b) {
2339                 return *a < *b;
2340         }
2341 };
2342
2343 static string*
2344 remove_end(string* state)
2345 {
2346         string statename(*state);
2347         
2348         string::size_type start,end;
2349         if ((start = statename.find_last_of ('/')) != string::npos) {
2350                 statename = statename.substr (start+1);
2351         }
2352                 
2353         if ((end = statename.rfind(".ardour")) < 0) {
2354                 end = statename.length();
2355         }
2356
2357         return new string(statename.substr (0, end));
2358 }
2359
2360 vector<string *> *
2361 Session::possible_states (string path) 
2362 {
2363         PathScanner scanner;
2364         vector<string*>* states = scanner (path, state_file_filter, 0, false, false);
2365         
2366         transform(states->begin(), states->end(), states->begin(), remove_end);
2367         
2368         string_cmp cmp;
2369         sort (states->begin(), states->end(), cmp);
2370         
2371         return states;
2372 }
2373
2374 vector<string *> *
2375 Session::possible_states () const
2376 {
2377         return possible_states(_path);
2378 }
2379
2380 void
2381 Session::auto_save()
2382 {
2383         save_state (_current_snapshot_name);
2384 }
2385
2386 RouteGroup *
2387 Session::add_edit_group (string name)
2388 {
2389         RouteGroup* rg = new RouteGroup (name);
2390         edit_groups.push_back (rg);
2391         edit_group_added (rg); /* EMIT SIGNAL */
2392         set_dirty();
2393         return rg;
2394 }
2395
2396 RouteGroup *
2397 Session::add_mix_group (string name)
2398 {
2399         RouteGroup* rg = new RouteGroup (name, RouteGroup::Relative);
2400         mix_groups.push_back (rg);
2401         mix_group_added (rg); /* EMIT SIGNAL */
2402         set_dirty();
2403         return rg;
2404 }
2405
2406 RouteGroup *
2407 Session::mix_group_by_name (string name)
2408 {
2409         list<RouteGroup *>::iterator i;
2410
2411         for (i = mix_groups.begin(); i != mix_groups.end(); ++i) {
2412                 if ((*i)->name() == name) {
2413                         return* i;
2414                 }
2415         }
2416         return 0;
2417 }
2418
2419 RouteGroup *
2420 Session::edit_group_by_name (string name)
2421 {
2422         list<RouteGroup *>::iterator i;
2423
2424         for (i = edit_groups.begin(); i != edit_groups.end(); ++i) {
2425                 if ((*i)->name() == name) {
2426                         return* i;
2427                 }
2428         }
2429         return 0;
2430 }
2431
2432 void
2433 Session::set_meter_hold (float val)
2434 {
2435         _meter_hold = val;
2436         MeterHoldChanged(); // emit
2437 }
2438
2439 void
2440 Session::set_meter_falloff (float val)
2441 {
2442         _meter_falloff = val;
2443         MeterFalloffChanged(); // emit
2444 }
2445
2446
2447 void
2448 Session::begin_reversible_command (string name, UndoAction* private_undo)
2449 {
2450         current_cmd.clear ();
2451         current_cmd.set_name (name);
2452
2453         if (private_undo) {
2454                 current_cmd.add_undo (*private_undo);
2455         }
2456 }
2457
2458 void
2459 Session::commit_reversible_command (UndoAction* private_redo)
2460 {
2461         struct timeval now;
2462
2463         if (private_redo) {
2464                 current_cmd.add_redo_no_execute (*private_redo);
2465         }
2466
2467         gettimeofday (&now, 0);
2468         current_cmd.set_timestamp (now);
2469
2470         history.add (current_cmd);
2471 }
2472
2473 Session::GlobalRouteBooleanState 
2474 Session::get_global_route_boolean (bool (Route::*method)(void) const)
2475 {
2476         GlobalRouteBooleanState s;
2477         LockMonitor lm (route_lock, __LINE__, __FILE__);
2478
2479         for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
2480                 if (!(*i)->hidden()) {
2481                         RouteBooleanState v;
2482                         
2483                         v.first =* i;
2484                         v.second = ((*i)->*method)();
2485                         
2486                         s.push_back (v);
2487                 }
2488         }
2489
2490         return s;
2491 }
2492
2493 Session::GlobalRouteMeterState
2494 Session::get_global_route_metering ()
2495 {
2496         GlobalRouteMeterState s;
2497         LockMonitor lm (route_lock, __LINE__, __FILE__);
2498
2499         for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
2500                 if (!(*i)->hidden()) {
2501                         RouteMeterState v;
2502                         
2503                         v.first =* i;
2504                         v.second = (*i)->meter_point();
2505                         
2506                         s.push_back (v);
2507                 }
2508         }
2509
2510         return s;
2511 }
2512
2513 void
2514 Session::set_global_route_metering (GlobalRouteMeterState s, void* arg) 
2515 {
2516         for (GlobalRouteMeterState::iterator i = s.begin(); i != s.end(); ++i) {
2517                 i->first->set_meter_point (i->second, arg);
2518         }
2519 }
2520
2521 void
2522 Session::set_global_route_boolean (GlobalRouteBooleanState s, void (Route::*method)(bool, void*), void* arg)
2523 {
2524         for (GlobalRouteBooleanState::iterator i = s.begin(); i != s.end(); ++i) {
2525                 (i->first->*method) (i->second, arg);
2526         }
2527 }
2528
2529 void
2530 Session::set_global_mute (GlobalRouteBooleanState s, void* src)
2531 {
2532         set_global_route_boolean (s, &Route::set_mute, src);
2533 }
2534
2535 void
2536 Session::set_global_solo (GlobalRouteBooleanState s, void* src)
2537 {
2538         set_global_route_boolean (s, &Route::set_solo, src);
2539 }
2540
2541 void
2542 Session::set_global_record_enable (GlobalRouteBooleanState s, void* src)
2543 {
2544         set_global_route_boolean (s, &Route::set_record_enable, src);
2545 }
2546
2547 UndoAction
2548 Session::global_mute_memento (void* src)
2549 {
2550         return sigc::bind (mem_fun (*this, &Session::set_global_mute), get_global_route_boolean (&Route::muted), src);
2551 }
2552
2553 UndoAction
2554 Session::global_metering_memento (void* src)
2555 {
2556         return sigc::bind (mem_fun (*this, &Session::set_global_route_metering), get_global_route_metering (), src);
2557 }
2558
2559 UndoAction
2560 Session::global_solo_memento (void* src)
2561 {
2562         return sigc::bind (mem_fun (*this, &Session::set_global_solo), get_global_route_boolean (&Route::soloed), src);
2563 }
2564
2565 UndoAction
2566 Session::global_record_enable_memento (void* src)
2567 {
2568         return sigc::bind (mem_fun (*this, &Session::set_global_record_enable), get_global_route_boolean (&Route::record_enabled), src);
2569 }
2570
2571 static bool
2572 template_filter (const string &str, void *arg)
2573 {
2574         return (str.length() > strlen(Session::template_suffix()) &&
2575                 str.find (Session::template_suffix()) == (str.length() - strlen (Session::template_suffix())));
2576 }
2577
2578 void
2579 Session::get_template_list (list<string> &template_names)
2580 {
2581         vector<string *> *templates;
2582         PathScanner scanner;
2583         string path;
2584
2585         path = template_path ();
2586
2587         templates = scanner (path, template_filter, 0, false, true);
2588         
2589         vector<string*>::iterator i;
2590         for (i = templates->begin(); i != templates->end(); ++i) {
2591                 string fullpath = *(*i);
2592                 int start, end;
2593
2594                 start = fullpath.find_last_of ('/') + 1;
2595                 if ((end = fullpath.find_last_of ('.')) <0) {
2596                         end = fullpath.length();
2597                 }
2598                 
2599                 template_names.push_back(fullpath.substr(start, (end-start)));
2600         }
2601 }
2602
2603 int
2604 Session::read_favorite_dirs (FavoriteDirs & favs)
2605 {
2606         string path = Config->get_user_ardour_path();
2607         path += "/favorite_dirs";
2608
2609         ifstream fav (path.c_str());
2610
2611         favs.clear();
2612         
2613         if (!fav) {
2614                 if (errno != ENOENT) {
2615                         //error << string_compose (_("cannot open favorite file %1 (%2)"), path, strerror (errno)) << endmsg;
2616                         return -1;
2617                 } else {
2618                         return 1;
2619                 }
2620         }
2621
2622         while (true) {
2623
2624                 string newfav;
2625
2626                 getline(fav, newfav);
2627
2628                 if (!fav.good()) {
2629                         break;
2630                 }
2631
2632                 favs.push_back (newfav);
2633         }
2634
2635         return 0;
2636 }
2637
2638 int
2639 Session::write_favorite_dirs (FavoriteDirs & favs)
2640 {
2641         string path = Config->get_user_ardour_path();
2642         path += "/favorite_dirs";
2643
2644         ofstream fav (path.c_str());
2645
2646         if (!fav) {
2647                 return -1;
2648         }
2649
2650         for (FavoriteDirs::iterator i = favs.begin(); i != favs.end(); ++i) {
2651                 fav << (*i) << endl;
2652         }
2653         
2654         return 0;
2655 }
2656
2657 static bool
2658 accept_all_non_peak_files (const string& path, void *arg)
2659 {
2660         return (path.length() > 5 && path.find (".peak") != (path.length() - 5));
2661 }
2662
2663 static bool
2664 accept_all_state_files (const string& path, void *arg)
2665 {
2666         return (path.length() > 7 && path.find (".ardour") == (path.length() - 7));
2667 }
2668
2669 int 
2670 Session::find_all_sources (string path, set<string>& result)
2671 {
2672         XMLTree tree;
2673         XMLNode* node;
2674
2675         if (!tree.read (path)) {
2676                 return -1;
2677         }
2678
2679         if ((node = find_named_node (*tree.root(), "Sources")) == 0) {
2680                 return -2;
2681         }
2682
2683         XMLNodeList nlist;
2684         XMLNodeConstIterator niter;
2685
2686         nlist = node->children();
2687
2688         set_dirty();
2689
2690         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2691                 
2692                 XMLProperty* prop;
2693
2694                 if ((prop = (*niter)->property (X_("name"))) == 0) {
2695                         continue;
2696                 }
2697
2698                 if (prop->value()[0] == '/') {
2699                         /* external file, ignore */
2700                         continue;
2701                 }
2702
2703                 string path = _path; /* /-terminated */
2704                 path += sound_dir_name;
2705                 path += '/';
2706                 path += prop->value();
2707
2708                 result.insert (path);
2709         }
2710
2711         return 0;
2712 }
2713
2714 int
2715 Session::find_all_sources_across_snapshots (set<string>& result, bool exclude_this_snapshot)
2716 {
2717         PathScanner scanner;
2718         vector<string*>* state_files;
2719         string ripped;
2720         string this_snapshot_path;
2721
2722         result.clear ();
2723
2724         ripped = _path;
2725
2726         if (ripped[ripped.length()-1] == '/') {
2727                 ripped = ripped.substr (0, ripped.length() - 1);
2728         }
2729
2730         state_files = scanner (ripped, accept_all_state_files, (void *) 0, false, true);
2731         
2732         if (state_files == 0) {
2733                 /* impossible! */
2734                 return 0;
2735         }
2736
2737         this_snapshot_path = _path;
2738         this_snapshot_path += _current_snapshot_name;
2739         this_snapshot_path += _statefile_suffix;
2740
2741         for (vector<string*>::iterator i = state_files->begin(); i != state_files->end(); ++i) {
2742
2743                 if (exclude_this_snapshot && **i == this_snapshot_path) {
2744                         continue;
2745                 }
2746
2747                 if (find_all_sources (**i, result) < 0) {
2748                         return -1;
2749                 }
2750         }
2751
2752         return 0;
2753 }
2754
2755 int
2756 Session::cleanup_sources (Session::cleanup_report& rep)
2757 {
2758         vector<Source*> dead_sources;
2759         vector<Playlist*> playlists_tbd;
2760         PathScanner scanner;
2761         string sound_path;
2762         vector<space_and_path>::iterator i;
2763         vector<space_and_path>::iterator nexti;
2764         vector<string*>* soundfiles;
2765         vector<string> unused;
2766         set<string> all_sources;
2767         bool used;
2768         string spath;
2769         int ret = -1;
2770                 
2771         _state_of_the_state = (StateOfTheState) (_state_of_the_state | InCleanup);
2772         
2773         /* step 1: consider deleting all unused playlists */
2774
2775         for (PlaylistList::iterator x = unused_playlists.begin(); x != unused_playlists.end(); ++x) {
2776                 int status;
2777
2778                 status = AskAboutPlaylistDeletion (*x);
2779
2780                 switch (status) {
2781                 case -1:
2782                         ret = 0;
2783                         goto out;
2784                         break;
2785
2786                 case 0:
2787                         playlists_tbd.push_back (*x);
2788                         break;
2789
2790                 default:
2791                         /* leave it alone */
2792                         break;
2793                 }
2794         }
2795
2796         /* now delete any that were marked for deletion */
2797
2798         for (vector<Playlist*>::iterator x = playlists_tbd.begin(); x != playlists_tbd.end(); ++x) {
2799                 PlaylistList::iterator foo;
2800
2801                 if ((foo = unused_playlists.find (*x)) != unused_playlists.end()) {
2802                         unused_playlists.erase (foo);
2803                 }
2804                 delete *x;
2805         }
2806
2807         /* step 2: clear the undo/redo history for all playlists */
2808
2809         for (PlaylistList::iterator x = playlists.begin(); x != playlists.end(); ++x) {
2810                 (*x)->drop_all_states ();
2811         }
2812
2813         /* step 3: find all un-referenced sources */
2814
2815         rep.paths.clear ();
2816         rep.space = 0;
2817
2818         for (SourceList::iterator i = sources.begin(); i != sources.end(); ) {
2819
2820                 SourceList::iterator tmp;
2821
2822                 tmp = i;
2823                 ++tmp;
2824
2825                 /* only remove files that are not in use and have some size
2826                    to them. otherwise we remove the current "nascent"
2827                    capture files.
2828                 */
2829
2830                 if ((*i).second->use_cnt() == 0 && (*i).second->length() > 0) {
2831                         dead_sources.push_back (i->second);
2832
2833                         /* remove this source from our own list to avoid us
2834                            adding it to the list of all sources below
2835                         */
2836
2837                         sources.erase (i);
2838                 }
2839
2840                 i = tmp;
2841         }
2842
2843         /* Step 4: get rid of all regions in the region list that use any dead sources
2844            in case the sources themselves don't go away (they might be referenced in
2845            other snapshots).
2846         */
2847                 
2848         for (vector<Source*>::iterator i = dead_sources.begin(); i != dead_sources.end();++i) {
2849
2850                 for (AudioRegionList::iterator r = audio_regions.begin(); r != audio_regions.end(); ) {
2851                         AudioRegionList::iterator tmp;
2852                         AudioRegion* ar;
2853
2854                         tmp = r;
2855                         ++tmp;
2856                         
2857                         ar = (*r).second;
2858
2859                         for (uint32_t n = 0; n < ar->n_channels(); ++n) {
2860                                 if (&ar->source (n) == (*i)) {
2861                                         /* this region is dead */
2862                                         remove_region (ar);
2863                                 }
2864                         }
2865                         
2866                         r = tmp;
2867                 }
2868         }
2869
2870         /* build a list of all the possible sound directories for the session */
2871
2872         for (i = session_dirs.begin(); i != session_dirs.end(); ) {
2873
2874                 nexti = i;
2875                 ++nexti;
2876
2877                 sound_path += (*i).path;
2878                 sound_path += sound_dir_name;
2879
2880                 if (nexti != session_dirs.end()) {
2881                         sound_path += ':';
2882                 }
2883
2884                 i = nexti;
2885         }
2886         
2887         /* now do the same thing for the files that ended up in the sounds dir(s) 
2888            but are not referenced as sources in any snapshot.
2889         */
2890
2891         soundfiles = scanner (sound_path, accept_all_non_peak_files, (void *) 0, false, true);
2892
2893         if (soundfiles == 0) {
2894                 return 0;
2895         }
2896
2897         /* find all sources, but don't use this snapshot because the
2898            state file on disk still references sources we may have already
2899            dropped.
2900         */
2901
2902         find_all_sources_across_snapshots (all_sources, true);
2903
2904         /* add our current source list
2905          */
2906
2907         for (SourceList::iterator i = sources.begin(); i != sources.end(); ++i) {
2908                 FileSource* fs;
2909                 SndFileSource* sfs;
2910                 
2911                 if ((fs = dynamic_cast<FileSource*> ((*i).second)) != 0) {
2912                         all_sources.insert (fs->path());
2913                 } else if ((sfs = dynamic_cast<SndFileSource*> ((*i).second)) != 0) {
2914                         all_sources.insert (sfs->path());
2915                 } 
2916         }
2917
2918         for (vector<string*>::iterator x = soundfiles->begin(); x != soundfiles->end(); ++x) {
2919
2920                 used = false;
2921                 spath = **x;
2922
2923                 for (set<string>::iterator i = all_sources.begin(); i != all_sources.end(); ++i) {
2924
2925                         if (spath == *i) {
2926                                 used = true;
2927                                 break;
2928                         }
2929
2930                 }
2931
2932                 if (!used) {
2933                         unused.push_back (spath);
2934                 }
2935         }
2936
2937         /* now try to move all unused files into the "dead_sounds" directory(ies) */
2938
2939         for (vector<string>::iterator x = unused.begin(); x != unused.end(); ++x) {
2940                 struct stat statbuf;
2941
2942                 rep.paths.push_back (*x);
2943                 if (stat ((*x).c_str(), &statbuf) == 0) {
2944                         rep.space += statbuf.st_size;
2945                 }
2946
2947                 string newpath;
2948                 
2949                 /* don't move the file across filesystems, just
2950                    stick it in the `dead_sound_dir_name' directory
2951                    on whichever filesystem it was already on.
2952                 */
2953
2954                 newpath = PBD::dirname (*x);
2955                 newpath = PBD::dirname (newpath);
2956
2957                 newpath += '/';
2958                 newpath += dead_sound_dir_name;
2959                 newpath += '/';
2960                 newpath += PBD::basename ((*x));
2961                 
2962                 if (access (newpath.c_str(), F_OK) == 0) {
2963                         
2964                         /* the new path already exists, try versioning */
2965                         
2966                         char buf[PATH_MAX+1];
2967                         int version = 1;
2968                         string newpath_v;
2969                         
2970                         snprintf (buf, sizeof (buf), "%s.%d", newpath.c_str(), version);
2971                         newpath_v = buf;
2972
2973                         while (access (newpath_v.c_str(), F_OK) == 0 && version < 999) {
2974                                 snprintf (buf, sizeof (buf), "%s.%d", newpath.c_str(), ++version);
2975                                 newpath_v = buf;
2976                         }
2977                         
2978                         if (version == 999) {
2979                                 error << string_compose (_("there are already 1000 files with names like %1; versioning discontinued"),
2980                                                   newpath)
2981                                       << endmsg;
2982                         } else {
2983                                 newpath = newpath_v;
2984                         }
2985                         
2986                 } else {
2987                         
2988                         /* it doesn't exist, or we can't read it or something */
2989                         
2990                 }
2991
2992                 if (::rename ((*x).c_str(), newpath.c_str()) != 0) {
2993                         error << string_compose (_("cannot rename audio file source from %1 to %2 (%3)"),
2994                                           (*x), newpath, strerror (errno))
2995                               << endmsg;
2996                         goto out;
2997                 }
2998                 
2999
3000                 /* see if there an easy to find peakfile for this file, and remove it.
3001                  */
3002
3003                 string peakpath = (*x).substr (0, (*x).find_last_of ('.'));
3004                 peakpath += ".peak";
3005
3006                 if (access (peakpath.c_str(), W_OK) == 0) {
3007                         if (::unlink (peakpath.c_str()) != 0) {
3008                                 error << string_compose (_("cannot remove peakfile %1 for %2 (%3)"),
3009                                                   peakpath, _path, strerror (errno))
3010                                       << endmsg;
3011                                 /* try to back out */
3012                                 rename (newpath.c_str(), _path.c_str());
3013                                 goto out;
3014                         }
3015                 }
3016
3017         }
3018
3019         ret = 0;
3020
3021         /* dump the history list */
3022
3023         history.clear ();
3024
3025         /* save state so we don't end up a session file
3026            referring to non-existent sources.
3027         */
3028         
3029         save_state ("");
3030
3031   out:
3032         _state_of_the_state = (StateOfTheState) (_state_of_the_state & ~InCleanup);
3033         return ret;
3034 }
3035
3036 int
3037 Session::cleanup_trash_sources (Session::cleanup_report& rep)
3038 {
3039         vector<space_and_path>::iterator i;
3040         string dead_sound_dir;
3041         struct dirent* dentry;
3042         struct stat statbuf;
3043         DIR* dead;
3044
3045         rep.paths.clear ();
3046         rep.space = 0;
3047
3048         for (i = session_dirs.begin(); i != session_dirs.end(); ++i) {
3049                 
3050                 dead_sound_dir = (*i).path;
3051                 dead_sound_dir += dead_sound_dir_name;
3052
3053                 if ((dead = opendir (dead_sound_dir.c_str())) == 0) {
3054                         continue;
3055                 }
3056
3057                 while ((dentry = readdir (dead)) != 0) {
3058
3059                         /* avoid '.' and '..' */
3060                         
3061                         if ((dentry->d_name[0] == '.' && dentry->d_name[1] == '\0') || 
3062                             (dentry->d_name[2] == '\0' && dentry->d_name[0] == '.' && dentry->d_name[1] == '.')) {
3063                                 continue;
3064                         }
3065
3066                         string fullpath;
3067
3068                         fullpath = dead_sound_dir;
3069                         fullpath += '/';
3070                         fullpath += dentry->d_name;
3071
3072                         if (stat (fullpath.c_str(), &statbuf)) {
3073                                 continue;
3074                         }
3075
3076                         if (!S_ISREG (statbuf.st_mode)) {
3077                                 continue;
3078                         }
3079
3080                         if (unlink (fullpath.c_str())) {
3081                                 error << string_compose (_("cannot remove dead sound file %1 (%2)"),
3082                                                   fullpath, strerror (errno))
3083                                       << endmsg;
3084                         }
3085
3086                         rep.paths.push_back (dentry->d_name);
3087                         rep.space += statbuf.st_size;
3088                 }
3089
3090                 closedir (dead);
3091                 
3092         }
3093
3094         return 0;
3095 }
3096
3097 void
3098 Session::set_dirty ()
3099 {
3100         bool was_dirty = dirty();
3101         
3102         _state_of_the_state = StateOfTheState (_state_of_the_state | Dirty);
3103
3104         if (!was_dirty) {
3105                 DirtyChanged(); /* EMIT SIGNAL */
3106         }
3107 }
3108
3109
3110 void
3111 Session::set_clean ()
3112 {
3113         bool was_dirty = dirty();
3114         
3115         _state_of_the_state = Clean;
3116
3117         if (was_dirty) {
3118                 DirtyChanged(); /* EMIT SIGNAL */
3119         }
3120 }
3121