merge changes from harrison branch back into trunk, by hand
[ardour.git] / libs / ardour / location.cc
1 /*
2     Copyright (C) 2000 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 <set>
23 #include <cstdio> /* for sprintf */
24 #include <unistd.h>
25 #include <cerrno>
26 #include <ctime>
27 #include <sigc++/bind.h>
28
29 #include <pbd/stl_delete.h>
30 #include <pbd/xml++.h>
31
32 #include <ardour/location.h>
33 #include <ardour/session.h>
34 #include <ardour/audiofilesource.h>
35
36 #include "i18n.h"
37
38 using namespace std;
39 using namespace ARDOUR;
40 using namespace sigc;
41 using namespace PBD;
42
43 Location::Location (const Location& other)
44         : _name (other._name),
45           _start (other._start),
46           _end (other._end),
47           _flags (other._flags)
48 {
49         /* start and end flags can never be copied, because there can only ever be one of each */
50
51         _flags = Flags (_flags & ~IsStart);
52         _flags = Flags (_flags & ~IsEnd);
53 }
54
55 Location::Location (const XMLNode& node)
56 {
57         if (set_state (node)) {
58                 throw failed_constructor ();
59         }
60 }
61
62 Location*
63 Location::operator= (const Location& other)
64 {
65         if (this == &other) {
66                 return this;
67         }
68
69         _name = other._name;
70         _start = other._start;
71         _end = other._end;
72         _flags = other._flags;
73
74         /* "changed" not emitted on purpose */
75         
76         return this;
77 }
78
79 int
80 Location::set_start (jack_nframes_t s)
81 {
82         if (is_mark()) {
83                 if (_start != s) {
84                         _start = s;
85                         _end = s;
86                         start_changed(this); /* EMIT SIGNAL */
87                         if ( is_start() ) {
88                                 Session::StartTimeChanged (); /* EMIT SIGNAL */
89                                 AudioFileSource::set_header_position_offset ( s );
90                         }
91                         if ( is_end() ) {
92                                 Session::EndTimeChanged (); /* EMIT SIGNAL */
93                         }
94                 }
95                 return 0;
96         }
97
98         if (((is_auto_punch() || is_auto_loop()) && s >= _end) || s > _end) {
99                 return -1;
100         }
101
102         if (s != _start) {
103                 _start = s; 
104                 start_changed(this); /* EMIT SIGNAL */
105         }
106
107         return 0;
108 }
109
110 int
111 Location::set_end (jack_nframes_t e)
112 {
113         if (is_mark()) {
114                 if (_start != e) {
115                         _start = e;
116                         _end = e;
117                         end_changed(this); /* EMIT SIGNAL */
118                 }
119                 return 0;
120         }
121
122         if (((is_auto_punch() || is_auto_loop()) && e <= _start) || e < _start) {
123                 return -1;
124         }
125
126         if (e != _end) {
127                 _end = e; 
128                  end_changed(this); /* EMIT SIGNAL */
129         }
130         return 0;
131 }
132
133 int
134 Location::set (jack_nframes_t start, jack_nframes_t end)
135 {
136         if (is_mark() && start != end) {
137                 return -1;
138         } else if (((is_auto_punch() || is_auto_loop()) && start >= end) || (start > end)) {
139                 return -1;
140         }
141         
142         if (_start != start) {
143                 _start = start;
144                 start_changed(this); /* EMIT SIGNAL */
145         }
146
147         if (_end != end) {
148                 _end = end;
149                 end_changed(this); /* EMIT SIGNAL */
150         }
151         return 0;
152 }
153
154 void
155 Location::set_hidden (bool yn, void *src)
156 {
157         if (set_flag_internal (yn, IsHidden)) {
158                  FlagsChanged (this, src); /* EMIT SIGNAL */
159         }
160 }
161
162 void
163 Location::set_cd (bool yn, void *src)
164 {
165         if (set_flag_internal (yn, IsCDMarker)) {
166                  FlagsChanged (this, src); /* EMIT SIGNAL */
167         }
168 }
169
170 void
171 Location::set_is_end (bool yn, void *src)
172 {
173         if (set_flag_internal (yn, IsEnd)) {
174                  FlagsChanged (this, src); /* EMIT SIGNAL */
175         }
176 }
177
178 void
179 Location::set_is_start (bool yn, void *src)
180 {
181         if (set_flag_internal (yn, IsStart)) {
182                  FlagsChanged (this, src); /* EMIT SIGNAL */
183         }
184 }
185
186 void
187 Location::set_auto_punch (bool yn, void *src) 
188 {
189         if (is_mark() || _start == _end) {
190                 return;
191         }
192
193         if (set_flag_internal (yn, IsAutoPunch)) {
194                  FlagsChanged (this, src); /* EMIT SIGNAL */
195         }
196 }
197
198 void
199 Location::set_auto_loop (bool yn, void *src) 
200 {
201         if (is_mark() || _start == _end) {
202                 return;
203         }
204
205         if (set_flag_internal (yn, IsAutoLoop)) {
206                  FlagsChanged (this, src); /* EMIT SIGNAL */
207         }
208 }
209
210 bool
211 Location::set_flag_internal (bool yn, Flags flag)
212 {
213         if (yn) {
214                 if (!(_flags & flag)) {
215                         _flags |= flag;
216                         return true;
217                 }
218         } else {
219                 if (_flags & flag) {
220                         _flags &= ~flag;
221                         return true;
222                 }
223         }
224         return false;
225 }
226
227 void
228 Location::set_mark (bool yn)
229 {
230         /* This function is private, and so does not emit signals */
231
232         if (_start != _end) {
233                 return;
234         }
235         
236         set_flag_internal (yn, IsMark);
237 }
238
239
240 XMLNode&
241 Location::cd_info_node(const string & name, const string & value)
242 {
243         XMLNode* root = new XMLNode("CD-Info");
244
245         root->add_property("name", name);
246         root->add_property("value", value);
247         
248         return *root;
249 }
250
251  
252 XMLNode&
253 Location::get_state (void)
254 {
255         XMLNode *node = new XMLNode ("Location");
256         char buf[64];
257
258         typedef map<string, string>::const_iterator CI;
259
260         for(CI m = cd_info.begin(); m != cd_info.end(); ++m){
261                 node->add_child_nocopy(cd_info_node(m->first, m->second));
262         }
263
264         id().print (buf);
265         node->add_property("id", buf);
266         node->add_property ("name", name());
267         snprintf (buf, sizeof (buf), "%u", start());
268         node->add_property ("start", buf);
269         snprintf (buf, sizeof (buf), "%u", end());
270         node->add_property ("end", buf);
271         snprintf (buf, sizeof (buf), "%" PRIu32, (uint32_t) _flags);
272         node->add_property ("flags", buf);
273
274         return *node;
275 }
276
277 int
278 Location::set_state (const XMLNode& node)
279 {
280         const XMLProperty *prop;
281
282         XMLNodeList cd_list = node.children();
283         XMLNodeConstIterator cd_iter;
284         XMLNode *cd_node;
285         
286         string cd_name;
287         string cd_value;
288
289         if (node.name() != "Location") {
290                 error << _("incorrect XML node passed to Location::set_state") << endmsg;
291                 return -1;
292         }
293
294         if ((prop = node.property ("id")) == 0) {
295                 warning << _("XML node for Location has no ID information") << endmsg;
296         } else {
297                 _id = prop->value ();
298         }
299
300         if ((prop = node.property ("name")) == 0) {
301                 error << _("XML node for Location has no name information") << endmsg;
302                 return -1;
303         }
304                 
305         set_name (prop->value());
306                 
307         if ((prop = node.property ("start")) == 0) {
308                 error << _("XML node for Location has no start information") << endmsg; 
309                 return -1;
310         }
311                 
312                 /* can't use set_start() here, because _end
313                    may make the value of _start illegal.
314                 */
315                 
316         _start = atoi (prop->value().c_str());
317                 
318         if ((prop = node.property ("end")) == 0) {
319                   error << _("XML node for Location has no end information") << endmsg; 
320                   return -1;
321         }
322                 
323         _end = atoi (prop->value().c_str());
324                 
325         _flags = 0;
326                 
327         if ((prop = node.property ("flags")) == 0) {
328                   error << _("XML node for Location has no flags information") << endmsg; 
329                   return -1;
330         }
331                 
332         _flags = Flags (atoi (prop->value().c_str()));
333
334         for (cd_iter = cd_list.begin(); cd_iter != cd_list.end(); ++cd_iter) {
335                   
336                   cd_node = *cd_iter;
337                   
338                   if (cd_node->name() != "CD-Info") {
339                     continue;
340                   }
341                   
342                   if ((prop = cd_node->property ("name")) != 0) {
343                     cd_name = prop->value();
344                   } else {
345                     throw failed_constructor ();
346                   }
347                   
348                   if ((prop = cd_node->property ("value")) != 0) {
349                     cd_value = prop->value();
350                   } else {
351                     throw failed_constructor ();
352                   }
353                   
354                   
355                   cd_info[cd_name] = cd_value;
356                   
357         }
358
359         changed(this); /* EMIT SIGNAL */
360                 
361         return 0;
362 }
363
364 /*---------------------------------------------------------------------- */
365
366 Locations::Locations ()
367
368 {
369         current_location = 0;
370         save_state (_("initial"));
371 }
372
373 Locations::~Locations () 
374 {
375         std::set<Location*> all_locations;
376         
377         for (StateMap::iterator siter = states.begin(); siter != states.end(); ++siter) {
378
379                 State* lstate = dynamic_cast<State*> (*siter);
380
381                 for (LocationList::iterator liter = lstate->locations.begin(); liter != lstate->locations.end(); ++liter) {
382                         all_locations.insert (*liter);
383                 }
384
385                 for (LocationList::iterator siter = lstate->states.begin(); siter != lstate->states.end(); ++siter) {
386                         all_locations.insert (*siter);
387                 }
388         }
389
390         set_delete (&all_locations);
391 }
392
393 int
394 Locations::set_current (Location *loc, bool want_lock)
395
396 {
397         int ret;
398
399         if (want_lock) {
400                 Glib::Mutex::Lock lm (lock);
401                 ret = set_current_unlocked (loc);
402         } else {
403                 ret = set_current_unlocked (loc);
404         }
405
406         if (ret == 0) {
407                  current_changed (current_location); /* EMIT SIGNAL */
408         }
409         return ret;
410 }
411
412 int
413 Locations::set_current_unlocked (Location *loc)
414 {
415         if (find (locations.begin(), locations.end(), loc) == locations.end()) {
416                 error << _("Locations: attempt to use unknown location as selected location") << endmsg;
417                 return -1;
418         }
419         
420         current_location = loc;
421         return 0;
422 }
423
424 void
425 Locations::clear ()
426 {
427         {
428                 Glib::Mutex::Lock lm (lock);
429                 LocationList::iterator tmp;
430                 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
431                         tmp = i;
432                         ++tmp;
433                         if (!(*i)->is_end() && !(*i)->is_start()) {
434                                 locations.erase (i);
435                         }
436                         i = tmp;
437                 }
438
439                 locations.clear ();
440                 current_location = 0;
441         }
442
443         save_state (_("clear"));
444         
445         changed (); /* EMIT SIGNAL */
446         current_changed (0); /* EMIT SIGNAL */
447 }       
448
449 void
450 Locations::clear_markers ()
451 {
452         {
453                 Glib::Mutex::Lock lm (lock);
454                 LocationList::iterator tmp;
455
456                 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
457                         tmp = i;
458                         ++tmp;
459
460                         if ((*i)->is_mark() && !(*i)->is_end() && !(*i)->is_start()) {
461                                 locations.erase (i);
462                         }
463
464                         i = tmp;
465                 }
466         }
467
468         save_state (_("clear markers"));
469         
470         changed (); /* EMIT SIGNAL */
471 }       
472
473 void
474 Locations::clear_ranges ()
475 {
476         {
477                 Glib::Mutex::Lock lm (lock);
478                 LocationList::iterator tmp;
479                 
480                 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
481
482                         tmp = i;
483                         ++tmp;
484
485                         if (!(*i)->is_mark()) {
486                                 locations.erase (i);
487
488                         }
489
490                         i = tmp;
491                 }
492
493                 current_location = 0;
494         }
495
496         save_state (_("clear ranges"));
497
498         changed (); /* EMIT SIGNAL */
499         current_changed (0); /* EMIT SIGNAL */
500 }       
501
502 void
503 Locations::add (Location *loc, bool make_current)
504 {
505         {
506                 Glib::Mutex::Lock lm (lock);
507                 locations.push_back (loc);
508
509                 if (make_current) {
510                         current_location = loc;
511                 }
512         }
513         
514         save_state (_("add"));
515
516         added (loc); /* EMIT SIGNAL */
517
518         if (make_current) {
519                  current_changed (current_location); /* EMIT SIGNAL */
520         } 
521 }
522
523 void
524 Locations::remove (Location *loc)
525
526 {
527         bool was_removed = false;
528         bool was_current = false;
529         LocationList::iterator i;
530
531         if (loc->is_end() || loc->is_start()) {
532                 return;
533         }
534
535         {
536                 Glib::Mutex::Lock lm (lock);
537
538                 for (i = locations.begin(); i != locations.end(); ++i) {
539                         if ((*i) == loc) {
540                                 locations.erase (i);
541                                 was_removed = true;
542                                 if (current_location == loc) {
543                                         current_location = 0;
544                                         was_current = true;
545                                 }
546                                 break;
547                         }
548                 }
549         }
550         
551         if (was_removed) {
552                 save_state (_("remove"));
553
554                  removed (loc); /* EMIT SIGNAL */
555
556                 if (was_current) {
557                          current_changed (0); /* EMIT SIGNAL */
558                 }
559
560                 changed (); /* EMIT_SIGNAL */
561         }
562 }
563
564 void
565 Locations::location_changed (Location* loc)
566 {
567         save_state (X_("location changed"));
568         changed (); /* EMIT SIGNAL */
569 }
570
571 XMLNode&
572 Locations::get_state ()
573 {
574         XMLNode *node = new XMLNode ("Locations");
575         LocationList::iterator iter;
576         Glib::Mutex::Lock lm (lock);
577        
578         for (iter  = locations.begin(); iter != locations.end(); ++iter) {
579                 node->add_child_nocopy ((*iter)->get_state ());
580         }
581
582         return *node;
583 }       
584
585 int
586 Locations::set_state (const XMLNode& node)
587 {
588         XMLNodeList nlist;
589         XMLNodeConstIterator niter;
590
591         if (node.name() != "Locations") {
592                 error << _("incorrect XML mode passed to Locations::set_state") << endmsg;
593                 return -1;
594         }
595         
596         nlist = node.children();
597         
598         {
599                 Glib::Mutex::Lock lm (lock);
600
601                 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
602                         
603                         try {
604
605                                 Location *loc = new Location (**niter);
606                                 locations.push_back (loc);
607                         }
608
609                         catch (failed_constructor& err) {
610                                 error << _("could not load location from session file - ignored") << endmsg;
611                         }
612                 }
613                 
614                 if (locations.size()) {
615
616                         current_location = locations.front();
617                 } else {
618                         current_location = 0;
619                 }
620         }
621
622         changed (); /* EMIT SIGNAL */
623          
624         return 0;
625 }       
626
627 struct LocationStartEarlierComparison 
628 {
629     bool operator() (Location *a, Location *b) {
630         return a->start() < b->start();
631     }
632 };
633
634 struct LocationStartLaterComparison 
635 {
636     bool operator() (Location *a, Location *b) {
637         return a->start() > b->start();
638     }
639 };
640
641 Location *
642 Locations::first_location_before (jack_nframes_t frame)
643 {
644         LocationList locs;
645
646         {
647                 Glib::Mutex::Lock lm (lock);
648                 locs = locations;
649         }
650
651         LocationStartLaterComparison cmp;
652         locs.sort (cmp);
653
654         /* locs is now sorted latest..earliest */
655         
656         for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
657                 if (!(*i)->is_hidden() && (*i)->start() < frame) {
658                         return (*i);
659                 }
660         }
661
662         return 0;
663 }
664
665 Location *
666 Locations::first_location_after (jack_nframes_t frame)
667 {
668         LocationList locs;
669
670         {
671                 Glib::Mutex::Lock lm (lock);
672                 locs = locations;
673         }
674
675         LocationStartEarlierComparison cmp;
676         locs.sort (cmp);
677
678         /* locs is now sorted earliest..latest */
679         
680         for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
681                 if (!(*i)->is_hidden() && (*i)->start() > frame) {
682                         return (*i);
683                 }
684         }
685
686         return 0;
687 }
688
689 jack_nframes_t
690 Locations::first_mark_before (jack_nframes_t frame)
691 {
692         LocationList locs;
693
694         {
695         Glib::Mutex::Lock lm (lock);
696                 locs = locations;
697         }
698
699         LocationStartLaterComparison cmp;
700         locs.sort (cmp);
701
702         /* locs is now sorted latest..earliest */
703         
704         for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
705                 if (!(*i)->is_hidden()) {
706                         if ((*i)->is_mark()) {
707                                 /* MARK: start == end */
708                                 if ((*i)->start() < frame) {
709                                         return (*i)->start();
710                                 }
711                         } else {
712                                 /* RANGE: start != end, compare start and end */
713                                 if ((*i)->end() < frame) {
714                                         return (*i)->end();
715                                 }
716                                 if ((*i)->start () < frame) {
717                                         return (*i)->start();
718                                 }
719                         }
720                 }
721         }
722
723         return 0;
724 }
725
726 jack_nframes_t
727 Locations::first_mark_after (jack_nframes_t frame)
728 {
729         LocationList locs;
730
731         {
732         Glib::Mutex::Lock lm (lock);
733                 locs = locations;
734         }
735
736         LocationStartEarlierComparison cmp;
737         locs.sort (cmp);
738
739         /* locs is now sorted earliest..latest */
740         
741         for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
742                 if (!(*i)->is_hidden()) {
743                         if ((*i)->is_mark()) {
744                                 /* MARK, start == end so just compare start */
745                                 if ((*i)->start() > frame) {
746                                         return (*i)->start();
747                                 }
748                         } else {
749                                 /* RANGE, start != end, compare start and end */
750                                 if ((*i)->start() > frame ) {
751                                         return (*i)->start ();
752                                 }
753                                 if ((*i)->end() > frame) {
754                                         return (*i)->end ();
755                                 }
756                         }
757                 }
758         }
759
760         return max_frames;
761 }
762
763 Location*
764 Locations::end_location () const
765 {
766         for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
767                 if ((*i)->is_end()) {
768                         return const_cast<Location*> (*i);
769                 }
770         }
771         return 0;
772 }       
773
774 Location*
775 Locations::start_location () const
776 {
777         for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
778                 if ((*i)->is_start()) {
779                         return const_cast<Location*> (*i);
780                 }
781         }
782         return 0;
783 }       
784
785 Location*
786 Locations::auto_loop_location () const
787 {
788         for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
789                 if ((*i)->is_auto_loop()) {
790                         return const_cast<Location*> (*i);
791                 }
792         }
793         return 0;
794 }       
795
796 Location*
797 Locations::auto_punch_location () const
798 {
799         for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
800                 if ((*i)->is_auto_punch()) {
801                         return const_cast<Location*> (*i);
802                 }
803         }
804        return 0;
805 }       
806
807 StateManager::State*
808 Locations::state_factory (std::string why) const
809 {
810         State* state = new State (why);
811
812         state->locations = locations;
813         
814         for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
815                 state->states.push_back (new Location (**i));
816         }
817
818         return state;
819 }
820
821 Change
822 Locations::restore_state (StateManager::State& state) 
823 {
824         {
825                 Glib::Mutex::Lock lm (lock);
826                 State* lstate = dynamic_cast<State*> (&state);
827
828                 locations = lstate->locations;
829                 LocationList& states = lstate->states;
830                 LocationList::iterator l, s;
831
832                 for (l = locations.begin(), s = states.begin(); s != states.end(); ++s, ++l) {
833                         (*l) = (*s);
834                 }
835         }
836
837         return Change (0);
838 }
839
840 UndoAction
841 Locations::get_memento () const
842 {
843   return sigc::bind (mem_fun (*(const_cast<Locations*> (this)), &StateManager::use_state), _current_state_id);
844 }
845
846 uint32_t
847 Locations::num_range_markers () const
848 {
849         uint32_t cnt = 0;
850         Glib::Mutex::Lock lm (lock);
851         for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
852                 if ((*i)->is_range_marker()) {
853                         ++cnt;
854                 }
855         }
856         return cnt;
857 }
858
859 Location *
860 Locations::get_location_by_id(PBD::ID id)
861 {
862     LocationList::iterator it;
863     for (it  = locations.begin(); it != locations.end(); it++)
864         if (id == (*it)->id())
865             return *it;
866
867     return 0;
868 }