Merge branch 'cairocanvas' of git.ardour.org:ardour/ardour into cairocanvas
[ardour.git] / libs / ardour / location.cc
index e909957e0d9daaf27e1b67cd34504e6fec4f111c..90265af4e4262410fa4bfd37b09d5b117eee89a7 100644 (file)
@@ -30,6 +30,7 @@
 #include "pbd/enumwriter.h"
 
 #include "ardour/location.h"
+#include "ardour/midi_scene_change.h"
 #include "ardour/session.h"
 #include "ardour/audiofilesource.h"
 #include "ardour/tempo.h"
@@ -42,6 +43,8 @@ using namespace std;
 using namespace ARDOUR;
 using namespace PBD;
 
+PBD::Signal0<void> Location::scene_changed;
+
 Location::Location (Session& s)
        : SessionHandleRef (s)
        , _start (0)
@@ -87,6 +90,8 @@ Location::Location (const Location& other)
 
        assert (_start >= 0);
        assert (_end >= 0);
+
+       /* scene change is NOT COPIED */
 }
 
 Location::Location (Session& s, const XMLNode& node)
@@ -105,6 +110,21 @@ Location::Location (Session& s, const XMLNode& node)
        assert (_end >= 0);
 }
 
+bool
+Location::operator== (const Location& other)
+{
+       if (_name != other._name ||
+           _start != other._start ||
+           _end != other._end ||
+           _bbt_start != other._bbt_start ||
+           _bbt_end != other._bbt_end ||
+           _flags != other._flags ||
+           _position_lock_style != other._position_lock_style) {
+               return false;
+       }
+       return true;
+}
+
 Location*
 Location::operator= (const Location& other)
 {
@@ -119,6 +139,8 @@ Location::operator= (const Location& other)
        _bbt_end = other._bbt_end;
        _flags = other._flags;
        _position_lock_style = other._position_lock_style;
+       
+       /* XXX need to copy scene change */
 
        /* copy is not locked even if original was */
 
@@ -140,6 +162,10 @@ Location::operator= (const Location& other)
 int
 Location::set_start (framepos_t s, bool force, bool allow_bbt_recompute)
 {
+       if (s < 0) {
+               return -1;
+       }
+
        if (_locked) {
                return -1;
        }
@@ -196,6 +222,10 @@ Location::set_start (framepos_t s, bool force, bool allow_bbt_recompute)
 int
 Location::set_end (framepos_t e, bool force, bool allow_bbt_recompute)
 {
+       if (e < 0) {
+               return -1;
+       }
+
        if (_locked) {
                return -1;
        }
@@ -224,6 +254,7 @@ Location::set_end (framepos_t e, bool force, bool allow_bbt_recompute)
        }
 
        if (e != _end) {
+
                framepos_t const old = _end;
 
                _end = e;
@@ -245,6 +276,10 @@ Location::set_end (framepos_t e, bool force, bool allow_bbt_recompute)
 int
 Location::set (framepos_t start, framepos_t end, bool allow_bbt_recompute)
 {
+       if (start < 0 || end < 0) {
+               return -1;
+       }
+
        /* check validity */
        if (((is_auto_punch() || is_auto_loop()) && start >= end) || (!is_mark() && start > end)) {
                return -1;
@@ -260,6 +295,10 @@ Location::set (framepos_t start, framepos_t end, bool allow_bbt_recompute)
 int
 Location::move_to (framepos_t pos)
 {
+       if (pos < 0) {
+               return -1;
+       }
+
        if (_locked) {
                return -1;
        }
@@ -399,11 +438,15 @@ Location::get_state ()
        node->add_property ("locked", (_locked ? "yes" : "no"));
        node->add_property ("position-lock-style", enum_2_string (_position_lock_style));
 
+       if (_scene_change) {
+               node->add_child_nocopy (_scene_change->get_state());
+       }
+
        return *node;
 }
 
 int
-Location::set_state (const XMLNode& node, int /*version*/)
+Location::set_state (const XMLNode& node, int version)
 {
        const XMLProperty *prop;
 
@@ -489,6 +532,16 @@ Location::set_state (const XMLNode& node, int /*version*/)
                _position_lock_style = PositionLockStyle (string_2_enum (prop->value(), _position_lock_style));
        }
 
+       XMLNode* scene_child = find_named_node (node, SceneChange::xml_node_name);
+       
+       if (scene_child) {
+               _scene_change = SceneChange::factory (*scene_child, version);
+
+               if (_scene_change) {
+                       _scene_change->set_time (_start);
+               }
+       }
+
        recompute_bbt_from_frames ();
 
        changed (this); /* EMIT SIGNAL */
@@ -549,6 +602,14 @@ Location::unlock ()
        LockChanged (this);
 }
 
+void
+Location::set_scene_change (boost::shared_ptr<SceneChange>  sc)
+{
+       _scene_change = sc;
+
+       scene_changed (); /* EMIT SIGNAL */
+}
+
 /*---------------------------------------------------------------------- */
 
 Locations::Locations (Session& s)
@@ -573,7 +634,7 @@ Locations::set_current (Location *loc, bool want_lock)
        int ret;
 
        if (want_lock) {
-               Glib::Mutex::Lock lm (lock);
+               Glib::Threads::Mutex::Lock lm (lock);
                ret = set_current_unlocked (loc);
        } else {
                ret = set_current_unlocked (loc);
@@ -635,7 +696,7 @@ void
 Locations::clear ()
 {
        {
-               Glib::Mutex::Lock lm (lock);
+               Glib::Threads::Mutex::Lock lm (lock);
 
                for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
 
@@ -643,6 +704,7 @@ Locations::clear ()
                        ++tmp;
 
                        if (!(*i)->is_session_range()) {
+                               delete *i;
                                locations.erase (i);
                        }
 
@@ -660,7 +722,7 @@ void
 Locations::clear_markers ()
 {
        {
-               Glib::Mutex::Lock lm (lock);
+               Glib::Threads::Mutex::Lock lm (lock);
                LocationList::iterator tmp;
 
                for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
@@ -668,6 +730,7 @@ Locations::clear_markers ()
                        ++tmp;
 
                        if ((*i)->is_mark() && !(*i)->is_session_range()) {
+                               delete *i;
                                locations.erase (i);
                        }
 
@@ -682,7 +745,7 @@ void
 Locations::clear_ranges ()
 {
        {
-               Glib::Mutex::Lock lm (lock);
+               Glib::Threads::Mutex::Lock lm (lock);
                LocationList::iterator tmp;
 
                for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
@@ -691,6 +754,7 @@ Locations::clear_ranges ()
                        ++tmp;
 
                        if (!(*i)->is_mark()) {
+                               delete *i;
                                locations.erase (i);
 
                        }
@@ -711,7 +775,7 @@ Locations::add (Location *loc, bool make_current)
        assert (loc);
 
        {
-               Glib::Mutex::Lock lm (lock);
+               Glib::Threads::Mutex::Lock lm (lock);
                locations.push_back (loc);
 
                if (make_current) {
@@ -743,10 +807,11 @@ Locations::remove (Location *loc)
        }
 
        {
-               Glib::Mutex::Lock lm (lock);
+               Glib::Threads::Mutex::Lock lm (lock);
 
                for (i = locations.begin(); i != locations.end(); ++i) {
                        if ((*i) == loc) {
+                               delete *i;
                                locations.erase (i);
                                was_removed = true;
                                if (current_location == loc) {
@@ -781,7 +846,7 @@ Locations::get_state ()
 {
        XMLNode *node = new XMLNode ("Locations");
        LocationList::iterator iter;
-       Glib::Mutex::Lock lm (lock);
+       Glib::Threads::Mutex::Lock lm (lock);
 
        for (iter = locations.begin(); iter != locations.end(); ++iter) {
                node->add_child_nocopy ((*iter)->get_state ());
@@ -812,7 +877,7 @@ Locations::set_state (const XMLNode& node, int version)
        }
 
        {
-               Glib::Mutex::Lock lm (lock);
+               Glib::Threads::Mutex::Lock lm (lock);
 
                XMLNodeConstIterator niter;
                for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
@@ -890,72 +955,125 @@ Locations::set_state (const XMLNode& node, int version)
        return 0;
 }
 
+
+typedef std::pair<framepos_t,Location*> LocationPair;
+
 struct LocationStartEarlierComparison
 {
-    bool operator() (Location *a, Location *b) {
-       return a->start() < b->start();
+    bool operator() (LocationPair a, LocationPair b) {
+           return a.first < b.first;
     }
 };
 
 struct LocationStartLaterComparison
 {
-    bool operator() (Location *a, Location *b) {
-       return a->start() > b->start();
+    bool operator() (LocationPair a, LocationPair b) {
+           return a.first > b.first;
     }
 };
 
-Location *
-Locations::first_location_before (framepos_t frame, bool include_special_ranges)
+framepos_t
+Locations::first_mark_before (framepos_t frame, bool include_special_ranges)
 {
-       LocationList locs;
-
-       {
-               Glib::Mutex::Lock lm (lock);
-               locs = locations;
+       Glib::Threads::Mutex::Lock lm (lock);
+       vector<LocationPair> locs;
+       
+       for (LocationList::iterator i = locations.begin(); i != locations.end(); ++i) {
+               locs.push_back (make_pair ((*i)->start(), (*i)));
+               if (!(*i)->is_mark()) {
+                       locs.push_back (make_pair ((*i)->end(), (*i)));
+               }
        }
 
        LocationStartLaterComparison cmp;
-       locs.sort (cmp);
+       sort (locs.begin(), locs.end(), cmp);
 
-       /* locs is now sorted latest..earliest */
+       /* locs is sorted in ascending order */
 
-       for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
-               if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
+       for (vector<LocationPair>::iterator i = locs.begin(); i != locs.end(); ++i) {
+               if ((*i).second->is_hidden()) {
                        continue;
                }
-               if (!(*i)->is_hidden() && (*i)->start() < frame) {
-                       return (*i);
+               if (!include_special_ranges && ((*i).second->is_auto_loop() || (*i).second->is_auto_punch())) {
+                       continue;
+               }
+               if ((*i).first < frame) {
+                       return (*i).first;
                }
        }
 
-       return 0;
+       return -1;
 }
 
-Location *
-Locations::first_location_after (framepos_t frame, bool include_special_ranges)
+Location*
+Locations::mark_at (framepos_t pos, framecnt_t slop) const
 {
-       LocationList locs;
+       Glib::Threads::Mutex::Lock lm (lock);
+       Location* closest = 0;
+       frameoffset_t mindelta = max_framepos;
+       frameoffset_t delta;
 
-       {
-               Glib::Mutex::Lock lm (lock);
-               locs = locations;
+       /* locations are not necessarily stored in linear time order so we have
+        * to iterate across all of them to find the one closest to a give point.
+        */
+
+       for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
+
+               if ((*i)->is_mark()) {
+                       if (pos > (*i)->start()) { 
+                               delta = pos - (*i)->start();
+                       } else {
+                               delta = (*i)->start() - pos;
+                       }
+                       
+                       if (slop == 0 && delta == 0) {
+                               /* special case: no slop, and direct hit for position */
+                               return *i;
+                       }
+
+                       if (delta <= slop) {
+                               if (delta < mindelta) {
+                                       closest = *i;
+                                       mindelta = delta;
+                               }
+                       }
+               }
        }
 
-       LocationStartEarlierComparison cmp;
-       locs.sort (cmp);
+       return closest;
+}
 
-       /* locs is now sorted earliest..latest */
+framepos_t
+Locations::first_mark_after (framepos_t frame, bool include_special_ranges)
+{
+       Glib::Threads::Mutex::Lock lm (lock);
+       vector<LocationPair> locs;
 
-       for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
-               if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
+       for (LocationList::iterator i = locations.begin(); i != locations.end(); ++i) {
+               locs.push_back (make_pair ((*i)->start(), (*i)));
+               if (!(*i)->is_mark()) {
+                       locs.push_back (make_pair ((*i)->end(), (*i)));
+               }
+       }
+
+       LocationStartEarlierComparison cmp;
+       sort (locs.begin(), locs.end(), cmp);
+       
+       /* locs is sorted in reverse order */
+
+       for (vector<LocationPair>::iterator i = locs.begin(); i != locs.end(); ++i) {
+               if ((*i).second->is_hidden()) {
                        continue;
                }
-               if (!(*i)->is_hidden() && (*i)->start() > frame) {
-                       return (*i);
+               if (!include_special_ranges && ((*i).second->is_auto_loop() || (*i).second->is_auto_punch())) {
+                       continue;
+               }
+               if ((*i).first > frame) {
+                       return (*i).first;
                }
        }
 
-       return 0;
+       return -1;
 }
 
 /** Look for the `marks' (either locations which are marks, or start/end points of range markers) either
@@ -973,7 +1091,7 @@ Locations::marks_either_side (framepos_t const frame, framepos_t& before, framep
        LocationList locs;
 
        {
-               Glib::Mutex::Lock lm (lock);
+               Glib::Threads::Mutex::Lock lm (lock);
                locs = locations;
        }
 
@@ -1067,7 +1185,7 @@ uint32_t
 Locations::num_range_markers () const
 {
        uint32_t cnt = 0;
-       Glib::Mutex::Lock lm (lock);
+       Glib::Threads::Mutex::Lock lm (lock);
        for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
                if ((*i)->is_range_marker()) {
                        ++cnt;
@@ -1090,7 +1208,7 @@ Locations::get_location_by_id(PBD::ID id)
 void
 Locations::find_all_between (framepos_t start, framepos_t end, LocationList& ll, Location::Flags flags)
 {
-       Glib::Mutex::Lock lm (lock);
+       Glib::Threads::Mutex::Lock lm (lock);
 
        for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
                if ((flags == 0 || (*i)->matches (flags)) &&
@@ -1099,3 +1217,4 @@ Locations::find_all_between (framepos_t start, framepos_t end, LocationList& ll,
                }
        }
 }
+