various fixes for moving markers, fixes a crash reported by tim blechmann and also...
[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 */
19
20 #include <algorithm>
21 #include <set>
22 #include <cstdio> /* for sprintf */
23 #include <unistd.h>
24 #include <cerrno>
25 #include <ctime>
26 #include <list>
27
28 #include "pbd/stl_delete.h"
29 #include "pbd/xml++.h"
30 #include "pbd/enumwriter.h"
31
32 #include "ardour/location.h"
33 #include "ardour/session.h"
34 #include "ardour/audiofilesource.h"
35 #include "ardour/tempo.h"
36
37 #include "i18n.h"
38
39 #define SUFFIX_MAX 32
40
41 using namespace std;
42 using namespace ARDOUR;
43 using namespace PBD;
44
45 Location::Location (Session& s)
46         : SessionHandleRef (s)
47         , _start (0)
48         , _end (0)
49         , _flags (Flags (0))
50         , _locked (false)
51         , _position_lock_style (AudioTime)
52 {
53         assert (_start >= 0);
54         assert (_end >= 0);
55 }
56
57 /** Construct a new Location, giving it the position lock style determined by glue-new-markers-to-bars-and-beats */
58 Location::Location (Session& s, framepos_t sample_start, framepos_t sample_end, const std::string &name, Flags bits)
59         : SessionHandleRef (s)
60         , _name (name)
61         , _start (sample_start)
62         , _end (sample_end)
63         , _flags (bits)
64         , _locked (false)
65         , _position_lock_style (s.config.get_glue_new_markers_to_bars_and_beats() ? MusicTime : AudioTime)
66 {
67         recompute_bbt_from_frames ();
68
69         assert (_start >= 0);
70         assert (_end >= 0);
71 }
72
73 Location::Location (const Location& other)
74         : SessionHandleRef (other._session)
75         , StatefulDestructible()
76         , _name (other._name)
77         , _start (other._start)
78         , _bbt_start (other._bbt_start)
79         , _end (other._end)
80         , _bbt_end (other._bbt_end)
81         , _flags (other._flags)
82         , _position_lock_style (other._position_lock_style)
83 {
84         /* copy is not locked even if original was */
85
86         _locked = false;
87
88         assert (_start >= 0);
89         assert (_end >= 0);
90 }
91
92 Location::Location (Session& s, const XMLNode& node)
93         : SessionHandleRef (s)
94         , _position_lock_style (AudioTime)
95 {
96         /* Note: _position_lock_style is initialised above in case set_state doesn't set it
97            (for 2.X session file compatibility).
98         */
99
100         if (set_state (node, Stateful::loading_state_version)) {
101                 throw failed_constructor ();
102         }
103
104         assert (_start >= 0);
105         assert (_end >= 0);
106 }
107
108 bool
109 Location::operator== (const Location& other)
110 {
111         if (_name != other._name ||
112             _start != other._start ||
113             _end != other._end ||
114             _bbt_start != other._bbt_start ||
115             _bbt_end != other._bbt_end ||
116             _flags != other._flags ||
117             _position_lock_style != other._position_lock_style) {
118                 return false;
119         }
120         return true;
121 }
122
123 Location*
124 Location::operator= (const Location& other)
125 {
126         if (this == &other) {
127                 return this;
128         }
129
130         _name = other._name;
131         _start = other._start;
132         _bbt_start = other._bbt_start;
133         _end = other._end;
134         _bbt_end = other._bbt_end;
135         _flags = other._flags;
136         _position_lock_style = other._position_lock_style;
137
138         /* copy is not locked even if original was */
139
140         _locked = false;
141
142         /* "changed" not emitted on purpose */
143
144         assert (_start >= 0);
145         assert (_end >= 0);
146
147         return this;
148 }
149
150 /** Set start position.
151  *  @param s New start.
152  *  @param force true to force setting, even if the given new start is after the current end.
153  *  @param allow_bbt_recompute True to recompute BBT start time from the new given start time.
154  */
155 int
156 Location::set_start (framepos_t s, bool force, bool allow_bbt_recompute)
157 {
158         if (s < 0) {
159                 return -1;
160         }
161
162         if (_locked) {
163                 return -1;
164         }
165
166         if (!force) {
167                 if (((is_auto_punch() || is_auto_loop()) && s >= _end) || (!is_mark() && s > _end)) {
168                         return -1;
169                 }
170         }
171
172         if (is_mark()) {
173                 if (_start != s) {
174                         _start = s;
175                         _end = s;
176                         if (allow_bbt_recompute) {
177                                 recompute_bbt_from_frames ();
178                         }
179
180                         start_changed (this); /* EMIT SIGNAL */
181                         end_changed (this); /* EMIT SIGNAL */
182                 }
183
184                 assert (_start >= 0);
185                 assert (_end >= 0);
186
187                 return 0;
188         }
189
190         if (s != _start) {
191
192                 framepos_t const old = _start;
193
194                 _start = s;
195                 if (allow_bbt_recompute) {
196                         recompute_bbt_from_frames ();
197                 }
198                 start_changed (this); /* EMIT SIGNAL */
199                 if (is_session_range ()) {
200                         Session::StartTimeChanged (old); /* EMIT SIGNAL */
201                         AudioFileSource::set_header_position_offset (s);
202                 }
203         }
204
205         assert (_start >= 0);
206
207         return 0;
208 }
209
210 /** Set end position.
211  *  @param s New end.
212  *  @param force true to force setting, even if the given new start is after the current end.
213  *  @param allow_bbt_recompute True to recompute BBT end time from the new given end time.
214  */
215 int
216 Location::set_end (framepos_t e, bool force, bool allow_bbt_recompute)
217 {
218         if (e < 0) {
219                 return -1;
220         }
221
222         if (_locked) {
223                 return -1;
224         }
225
226         if (!force) {
227                 if (((is_auto_punch() || is_auto_loop()) && e <= _start) || e < _start) {
228                         return -1;
229                 }
230         }
231
232         if (is_mark()) {
233                 if (_start != e) {
234                         _start = e;
235                         _end = e;
236                         if (allow_bbt_recompute) {
237                                 recompute_bbt_from_frames ();
238                         }
239                         start_changed (this); /* EMIT SIGNAL */
240                         end_changed (this); /* EMIT SIGNAL */
241                 }
242
243                 assert (_start >= 0);
244                 assert (_end >= 0);
245
246                 return 0;
247         }
248
249         if (e != _end) {
250
251                 framepos_t const old = _end;
252
253                 _end = e;
254                 if (allow_bbt_recompute) {
255                         recompute_bbt_from_frames ();
256                 }
257                 end_changed(this); /* EMIT SIGNAL */
258
259                 if (is_session_range()) {
260                         Session::EndTimeChanged (old); /* EMIT SIGNAL */
261                 }
262         }
263
264         assert (_end >= 0);
265
266         return 0;
267 }
268
269 int
270 Location::set (framepos_t start, framepos_t end, bool allow_bbt_recompute)
271 {
272         if (start < 0 || end < 0) {
273                 return -1;
274         }
275
276         /* check validity */
277         if (((is_auto_punch() || is_auto_loop()) && start >= end) || (!is_mark() && start > end)) {
278                 return -1;
279         }
280
281         /* now we know these values are ok, so force-set them */
282         int const s = set_start (start, true, allow_bbt_recompute);
283         int const e = set_end (end, true, allow_bbt_recompute);
284
285         return (s == 0 && e == 0) ? 0 : -1;
286 }
287
288 int
289 Location::move_to (framepos_t pos)
290 {
291         if (pos < 0) {
292                 return -1;
293         }
294
295         if (_locked) {
296                 return -1;
297         }
298
299         if (_start != pos) {
300                 _start = pos;
301                 _end = _start + length();
302                 recompute_bbt_from_frames ();
303
304                 changed (this); /* EMIT SIGNAL */
305         }
306
307         assert (_start >= 0);
308         assert (_end >= 0);
309
310         return 0;
311 }
312
313 void
314 Location::set_hidden (bool yn, void *src)
315 {
316         if (set_flag_internal (yn, IsHidden)) {
317                  FlagsChanged (this, src); /* EMIT SIGNAL */
318         }
319 }
320
321 void
322 Location::set_cd (bool yn, void *src)
323 {
324         // XXX this really needs to be session start
325         // but its not available here - leave to GUI
326
327         if (_start == 0) {
328                 error << _("You cannot put a CD marker at this position") << endmsg;
329                 return;
330         }
331
332         if (set_flag_internal (yn, IsCDMarker)) {
333                  FlagsChanged (this, src); /* EMIT SIGNAL */
334         }
335 }
336
337 void
338 Location::set_is_range_marker (bool yn, void *src)
339 {
340        if (set_flag_internal (yn, IsRangeMarker)) {
341                 FlagsChanged (this, src); /* EMIT SIGNAL */
342        }
343 }
344
345 void
346 Location::set_auto_punch (bool yn, void *src)
347 {
348         if (is_mark() || _start == _end) {
349                 return;
350         }
351
352         if (set_flag_internal (yn, IsAutoPunch)) {
353                  FlagsChanged (this, src); /* EMIT SIGNAL */
354         }
355 }
356
357 void
358 Location::set_auto_loop (bool yn, void *src)
359 {
360         if (is_mark() || _start == _end) {
361                 return;
362         }
363
364         if (set_flag_internal (yn, IsAutoLoop)) {
365                  FlagsChanged (this, src); /* EMIT SIGNAL */
366         }
367 }
368
369 bool
370 Location::set_flag_internal (bool yn, Flags flag)
371 {
372         if (yn) {
373                 if (!(_flags & flag)) {
374                         _flags = Flags (_flags | flag);
375                         return true;
376                 }
377         } else {
378                 if (_flags & flag) {
379                         _flags = Flags (_flags & ~flag);
380                         return true;
381                 }
382         }
383         return false;
384 }
385
386 void
387 Location::set_mark (bool yn)
388 {
389         /* This function is private, and so does not emit signals */
390
391         if (_start != _end) {
392                 return;
393         }
394
395         set_flag_internal (yn, IsMark);
396 }
397
398
399 XMLNode&
400 Location::cd_info_node(const string & name, const string & value)
401 {
402         XMLNode* root = new XMLNode("CD-Info");
403
404         root->add_property("name", name);
405         root->add_property("value", value);
406
407         return *root;
408 }
409
410
411 XMLNode&
412 Location::get_state ()
413 {
414         XMLNode *node = new XMLNode ("Location");
415         char buf[64];
416
417         typedef map<string, string>::const_iterator CI;
418
419         for(CI m = cd_info.begin(); m != cd_info.end(); ++m){
420                 node->add_child_nocopy(cd_info_node(m->first, m->second));
421         }
422
423         id().print (buf, sizeof (buf));
424         node->add_property("id", buf);
425         node->add_property ("name", name());
426         snprintf (buf, sizeof (buf), "%" PRId64, start());
427         node->add_property ("start", buf);
428         snprintf (buf, sizeof (buf), "%" PRId64, end());
429         node->add_property ("end", buf);
430         node->add_property ("flags", enum_2_string (_flags));
431         node->add_property ("locked", (_locked ? "yes" : "no"));
432         node->add_property ("position-lock-style", enum_2_string (_position_lock_style));
433
434         return *node;
435 }
436
437 int
438 Location::set_state (const XMLNode& node, int /*version*/)
439 {
440         const XMLProperty *prop;
441
442         XMLNodeList cd_list = node.children();
443         XMLNodeConstIterator cd_iter;
444         XMLNode *cd_node;
445
446         string cd_name;
447         string cd_value;
448
449         if (node.name() != "Location") {
450                 error << _("incorrect XML node passed to Location::set_state") << endmsg;
451                 return -1;
452         }
453
454         if (!set_id (node)) {
455                 warning << _("XML node for Location has no ID information") << endmsg;
456         }
457
458         if ((prop = node.property ("name")) == 0) {
459                 error << _("XML node for Location has no name information") << endmsg;
460                 return -1;
461         }
462
463         set_name (prop->value());
464
465         if ((prop = node.property ("start")) == 0) {
466                 error << _("XML node for Location has no start information") << endmsg;
467                 return -1;
468         }
469
470                 /* can't use set_start() here, because _end
471                    may make the value of _start illegal.
472                 */
473
474         sscanf (prop->value().c_str(), "%" PRId64, &_start);
475
476         if ((prop = node.property ("end")) == 0) {
477                   error << _("XML node for Location has no end information") << endmsg;
478                   return -1;
479         }
480
481         sscanf (prop->value().c_str(), "%" PRId64, &_end);
482
483         if ((prop = node.property ("flags")) == 0) {
484                   error << _("XML node for Location has no flags information") << endmsg;
485                   return -1;
486         }
487
488         _flags = Flags (string_2_enum (prop->value(), _flags));
489
490         if ((prop = node.property ("locked")) != 0) {
491                 _locked = string_is_affirmative (prop->value());
492         } else {
493                 _locked = false;
494         }
495
496         for (cd_iter = cd_list.begin(); cd_iter != cd_list.end(); ++cd_iter) {
497
498                   cd_node = *cd_iter;
499
500                   if (cd_node->name() != "CD-Info") {
501                           continue;
502                   }
503
504                   if ((prop = cd_node->property ("name")) != 0) {
505                           cd_name = prop->value();
506                   } else {
507                           throw failed_constructor ();
508                   }
509
510                   if ((prop = cd_node->property ("value")) != 0) {
511                           cd_value = prop->value();
512                   } else {
513                           throw failed_constructor ();
514                   }
515
516
517                   cd_info[cd_name] = cd_value;
518         }
519
520         if ((prop = node.property ("position-lock-style")) != 0) {
521                 _position_lock_style = PositionLockStyle (string_2_enum (prop->value(), _position_lock_style));
522         }
523
524         recompute_bbt_from_frames ();
525
526         changed (this); /* EMIT SIGNAL */
527
528         assert (_start >= 0);
529         assert (_end >= 0);
530
531         return 0;
532 }
533
534 void
535 Location::set_position_lock_style (PositionLockStyle ps)
536 {
537         if (_position_lock_style == ps) {
538                 return;
539         }
540
541         _position_lock_style = ps;
542
543         recompute_bbt_from_frames ();
544
545         PositionLockStyleChanged (this); /* EMIT SIGNAL */
546 }
547
548 void
549 Location::recompute_bbt_from_frames ()
550 {
551         if (_position_lock_style != MusicTime) {
552                 return;
553         }
554
555         _session.bbt_time (_start, _bbt_start);
556         _session.bbt_time (_end, _bbt_end);
557 }
558
559 void
560 Location::recompute_frames_from_bbt ()
561 {
562         if (_position_lock_style != MusicTime) {
563                 return;
564         }
565
566         TempoMap& map (_session.tempo_map());
567         set (map.frame_time (_bbt_start), map.frame_time (_bbt_end), false);
568 }
569
570 void
571 Location::lock ()
572 {
573         _locked = true;
574         LockChanged (this);
575 }
576
577 void
578 Location::unlock ()
579 {
580         _locked = false;
581         LockChanged (this);
582 }
583
584 /*---------------------------------------------------------------------- */
585
586 Locations::Locations (Session& s)
587         : SessionHandleRef (s)
588 {
589         current_location = 0;
590 }
591
592 Locations::~Locations ()
593 {
594         for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
595                 LocationList::iterator tmp = i;
596                 ++tmp;
597                 delete *i;
598                 i = tmp;
599         }
600 }
601
602 int
603 Locations::set_current (Location *loc, bool want_lock)
604 {
605         int ret;
606
607         if (want_lock) {
608                 Glib::Threads::Mutex::Lock lm (lock);
609                 ret = set_current_unlocked (loc);
610         } else {
611                 ret = set_current_unlocked (loc);
612         }
613
614         if (ret == 0) {
615                  current_changed (current_location); /* EMIT SIGNAL */
616         }
617         return ret;
618 }
619
620 int
621 Locations::next_available_name(string& result,string base)
622 {
623         LocationList::iterator i;
624         Location* location;
625         string temp;
626         string::size_type l;
627         int suffix;
628         char buf[32];
629         bool available[SUFFIX_MAX+1];
630
631         result = base;
632         for (int k=1; k<SUFFIX_MAX; k++) {
633                 available[k] = true;
634         }
635         l = base.length();
636         for (i = locations.begin(); i != locations.end(); ++i) {
637                 location =* i;
638                 temp = location->name();
639                 if (l && !temp.find(base,0)) {
640                         suffix = atoi(temp.substr(l,3).c_str());
641                         if (suffix) available[suffix] = false;
642                 }
643         }
644         for (int k=1; k<=SUFFIX_MAX; k++) {
645                 if (available[k]) {
646                         snprintf (buf, 31, "%d", k);
647                         result += buf;
648                         return 1;
649                 }
650         }
651         return 0;
652 }
653
654 int
655 Locations::set_current_unlocked (Location *loc)
656 {
657         if (find (locations.begin(), locations.end(), loc) == locations.end()) {
658                 error << _("Locations: attempt to use unknown location as selected location") << endmsg;
659                 return -1;
660         }
661
662         current_location = loc;
663         return 0;
664 }
665
666 void
667 Locations::clear ()
668 {
669         {
670                 Glib::Threads::Mutex::Lock lm (lock);
671
672                 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
673
674                         LocationList::iterator tmp = i;
675                         ++tmp;
676
677                         if (!(*i)->is_session_range()) {
678                                 locations.erase (i);
679                         }
680
681                         i = tmp;
682                 }
683
684                 current_location = 0;
685         }
686
687         changed (OTHER); /* EMIT SIGNAL */
688         current_changed (0); /* EMIT SIGNAL */
689 }
690
691 void
692 Locations::clear_markers ()
693 {
694         {
695                 Glib::Threads::Mutex::Lock lm (lock);
696                 LocationList::iterator tmp;
697
698                 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
699                         tmp = i;
700                         ++tmp;
701
702                         if ((*i)->is_mark() && !(*i)->is_session_range()) {
703                                 locations.erase (i);
704                         }
705
706                         i = tmp;
707                 }
708         }
709
710         changed (OTHER); /* EMIT SIGNAL */
711 }
712
713 void
714 Locations::clear_ranges ()
715 {
716         {
717                 Glib::Threads::Mutex::Lock lm (lock);
718                 LocationList::iterator tmp;
719
720                 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
721
722                         tmp = i;
723                         ++tmp;
724
725                         if (!(*i)->is_mark()) {
726                                 locations.erase (i);
727
728                         }
729
730                         i = tmp;
731                 }
732
733                 current_location = 0;
734         }
735
736         changed (OTHER); /* EMIT SIGNAL */
737         current_changed (0); /* EMIT SIGNAL */
738 }
739
740 void
741 Locations::add (Location *loc, bool make_current)
742 {
743         assert (loc);
744
745         {
746                 Glib::Threads::Mutex::Lock lm (lock);
747                 locations.push_back (loc);
748
749                 if (make_current) {
750                         current_location = loc;
751                 }
752         }
753
754         added (loc); /* EMIT SIGNAL */
755
756         if (make_current) {
757                  current_changed (current_location); /* EMIT SIGNAL */
758         }
759
760         if (loc->is_session_range()) {
761                 Session::StartTimeChanged (0);
762                 Session::EndTimeChanged (1);
763         }
764 }
765
766 void
767 Locations::remove (Location *loc)
768 {
769         bool was_removed = false;
770         bool was_current = false;
771         LocationList::iterator i;
772
773         if (loc->is_session_range()) {
774                 return;
775         }
776
777         {
778                 Glib::Threads::Mutex::Lock lm (lock);
779
780                 for (i = locations.begin(); i != locations.end(); ++i) {
781                         if ((*i) == loc) {
782                                 locations.erase (i);
783                                 was_removed = true;
784                                 if (current_location == loc) {
785                                         current_location = 0;
786                                         was_current = true;
787                                 }
788                                 break;
789                         }
790                 }
791         }
792
793         if (was_removed) {
794
795                 removed (loc); /* EMIT SIGNAL */
796
797                 if (was_current) {
798                          current_changed (0); /* EMIT SIGNAL */
799                 }
800
801                 changed (REMOVAL); /* EMIT_SIGNAL */
802         }
803 }
804
805 void
806 Locations::location_changed (Location* /*loc*/)
807 {
808         changed (OTHER); /* EMIT SIGNAL */
809 }
810
811 XMLNode&
812 Locations::get_state ()
813 {
814         XMLNode *node = new XMLNode ("Locations");
815         LocationList::iterator iter;
816         Glib::Threads::Mutex::Lock lm (lock);
817
818         for (iter = locations.begin(); iter != locations.end(); ++iter) {
819                 node->add_child_nocopy ((*iter)->get_state ());
820         }
821
822         return *node;
823 }
824
825 int
826 Locations::set_state (const XMLNode& node, int version)
827 {
828         if (node.name() != "Locations") {
829                 error << _("incorrect XML mode passed to Locations::set_state") << endmsg;
830                 return -1;
831         }
832
833         XMLNodeList nlist = node.children();
834
835         /* build up a new locations list in here */
836         LocationList new_locations;
837
838         current_location = 0;
839
840         Location* session_range_location = 0;
841         if (version < 3000) {
842                 session_range_location = new Location (_session, 0, 0, _("session"), Location::IsSessionRange);
843                 new_locations.push_back (session_range_location);
844         }
845
846         {
847                 Glib::Threads::Mutex::Lock lm (lock);
848
849                 XMLNodeConstIterator niter;
850                 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
851
852                         try {
853
854                                 XMLProperty const * prop_id = (*niter)->property ("id");
855                                 assert (prop_id);
856                                 PBD::ID id (prop_id->value ());
857
858                                 LocationList::const_iterator i = locations.begin();
859                                 while (i != locations.end () && (*i)->id() != id) {
860                                         ++i;
861                                 }
862
863                                 Location* loc;
864                                 if (i != locations.end()) {
865                                         /* we can re-use an old Location object */
866                                         loc = *i;
867                                         loc->set_state (**niter, version);
868                                 } else {
869                                         loc = new Location (_session, **niter);
870                                 }
871
872                                 bool add = true;
873
874                                 if (version < 3000) {
875                                         /* look for old-style IsStart / IsEnd properties in this location;
876                                            if they are present, update the session_range_location accordingly
877                                         */
878                                         XMLProperty const * prop = (*niter)->property ("flags");
879                                         if (prop) {
880                                                 string v = prop->value ();
881                                                 while (1) {
882                                                         string::size_type const c = v.find_first_of (',');
883                                                         string const s = v.substr (0, c);
884                                                         if (s == X_("IsStart")) {
885                                                                 session_range_location->set_start (loc->start(), true);
886                                                                 add = false;
887                                                         } else if (s == X_("IsEnd")) {
888                                                                 session_range_location->set_end (loc->start(), true);
889                                                                 add = false;
890                                                         }
891
892                                                         if (c == string::npos) {
893                                                                 break;
894                                                         }
895
896                                                         v = v.substr (c + 1);
897                                                 }
898                                         }
899                                 }
900
901                                 if (add) {
902                                         new_locations.push_back (loc);
903                                 }
904                         }
905
906                         catch (failed_constructor& err) {
907                                 error << _("could not load location from session file - ignored") << endmsg;
908                         }
909                 }
910
911                 locations = new_locations;
912
913                 if (locations.size()) {
914                         current_location = locations.front();
915                 } else {
916                         current_location = 0;
917                 }
918         }
919
920         changed (OTHER); /* EMIT SIGNAL */
921
922         return 0;
923 }
924
925 struct LocationStartEarlierComparison
926 {
927     bool operator() (Location *a, Location *b) {
928         return a->start() < b->start();
929     }
930 };
931
932 struct LocationStartLaterComparison
933 {
934     bool operator() (Location *a, Location *b) {
935         return a->start() > b->start();
936     }
937 };
938
939 Location *
940 Locations::first_location_before (framepos_t frame, bool include_special_ranges)
941 {
942         LocationList locs;
943
944         {
945                 Glib::Threads::Mutex::Lock lm (lock);
946                 locs = locations;
947         }
948
949         LocationStartLaterComparison cmp;
950         locs.sort (cmp);
951
952         /* locs is now sorted latest..earliest */
953
954         for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
955                 if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
956                         continue;
957                 }
958                 if (!(*i)->is_hidden() && (*i)->start() < frame) {
959                         return (*i);
960                 }
961         }
962
963         return 0;
964 }
965
966 Location *
967 Locations::first_location_after (framepos_t frame, bool include_special_ranges)
968 {
969         LocationList locs;
970
971         {
972                 Glib::Threads::Mutex::Lock lm (lock);
973                 locs = locations;
974         }
975
976         LocationStartEarlierComparison cmp;
977         locs.sort (cmp);
978
979         /* locs is now sorted earliest..latest */
980
981         for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
982                 if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
983                         continue;
984                 }
985                 if (!(*i)->is_hidden() && (*i)->start() > frame) {
986                         return (*i);
987                 }
988         }
989
990         return 0;
991 }
992
993 /** Look for the `marks' (either locations which are marks, or start/end points of range markers) either
994  *  side of a frame.  Note that if frame is exactly on a `mark', that mark will not be considered for returning
995  *  as before/after.
996  *  @param frame Frame to look for.
997  *  @param before Filled in with the position of the last `mark' before `frame' (or max_framepos if none exists)
998  *  @param after Filled in with the position of the next `mark' after `frame' (or max_framepos if none exists)
999  */
1000 void
1001 Locations::marks_either_side (framepos_t const frame, framepos_t& before, framepos_t& after) const
1002 {
1003         before = after = max_framepos;
1004
1005         LocationList locs;
1006
1007         {
1008                 Glib::Threads::Mutex::Lock lm (lock);
1009                 locs = locations;
1010         }
1011
1012         /* Get a list of positions; don't store any that are exactly on our requested position */
1013
1014         std::list<framepos_t> positions;
1015
1016         for (LocationList::const_iterator i = locs.begin(); i != locs.end(); ++i) {
1017                 if (((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
1018                         continue;
1019                 }
1020
1021                 if (!(*i)->is_hidden()) {
1022                         if ((*i)->is_mark ()) {
1023                                 if ((*i)->start() != frame) {
1024                                         positions.push_back ((*i)->start ());
1025                                 }
1026                         } else {
1027                                 if ((*i)->start() != frame) {
1028                                         positions.push_back ((*i)->start ());
1029                                 }
1030                                 if ((*i)->end() != frame) {
1031                                         positions.push_back ((*i)->end ());
1032                                 }
1033                         }
1034                 }
1035         }
1036
1037         if (positions.empty ()) {
1038                 return;
1039         }
1040
1041         positions.sort ();
1042
1043         std::list<framepos_t>::iterator i = positions.begin ();
1044         while (i != positions.end () && *i < frame) {
1045                 ++i;
1046         }
1047
1048         if (i == positions.end ()) {
1049                 /* run out of marks */
1050                 before = positions.back ();
1051                 return;
1052         }
1053
1054         after = *i;
1055
1056         if (i == positions.begin ()) {
1057                 /* none before */
1058                 return;
1059         }
1060
1061         --i;
1062         before = *i;
1063 }
1064
1065 Location*
1066 Locations::session_range_location () const
1067 {
1068         for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
1069                 if ((*i)->is_session_range()) {
1070                         return const_cast<Location*> (*i);
1071                 }
1072         }
1073         return 0;
1074 }
1075
1076 Location*
1077 Locations::auto_loop_location () const
1078 {
1079         for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
1080                 if ((*i)->is_auto_loop()) {
1081                         return const_cast<Location*> (*i);
1082                 }
1083         }
1084         return 0;
1085 }
1086
1087 Location*
1088 Locations::auto_punch_location () const
1089 {
1090         for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
1091                 if ((*i)->is_auto_punch()) {
1092                         return const_cast<Location*> (*i);
1093                 }
1094         }
1095        return 0;
1096 }
1097
1098 uint32_t
1099 Locations::num_range_markers () const
1100 {
1101         uint32_t cnt = 0;
1102         Glib::Threads::Mutex::Lock lm (lock);
1103         for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
1104                 if ((*i)->is_range_marker()) {
1105                         ++cnt;
1106                 }
1107         }
1108         return cnt;
1109 }
1110
1111 Location *
1112 Locations::get_location_by_id(PBD::ID id)
1113 {
1114     LocationList::iterator it;
1115     for (it  = locations.begin(); it != locations.end(); ++it)
1116         if (id == (*it)->id())
1117             return *it;
1118
1119     return 0;
1120 }
1121
1122 void
1123 Locations::find_all_between (framepos_t start, framepos_t end, LocationList& ll, Location::Flags flags)
1124 {
1125         Glib::Threads::Mutex::Lock lm (lock);
1126
1127         for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
1128                 if ((flags == 0 || (*i)->matches (flags)) &&
1129                     ((*i)->start() >= start && (*i)->end() < end)) {
1130                         ll.push_back (*i);
1131                 }
1132         }
1133 }