fix crash when copy'ing latent plugins
[ardour.git] / libs / ardour / region_factory.cc
index ddc0451523c356884442f3c8405d14d4e36aac23..16a9e02e378cf0f6835fe81bb973e4b5e8892c34 100644 (file)
@@ -19,6 +19,7 @@
 
 #include <inttypes.h>
 
+#include "pbd/basename.h"
 #include "pbd/error.h"
 
 #include "ardour/audioregion.h"
 #include "ardour/region_factory.h"
 #include "ardour/session.h"
 
-#include "i18n.h"
+#include "pbd/i18n.h"
 
 using namespace ARDOUR;
 using namespace PBD;
 using namespace std;
 
 PBD::Signal1<void,boost::shared_ptr<Region> > RegionFactory::CheckNewRegion;
-Glib::StaticMutex                             RegionFactory::region_map_lock;
+Glib::Threads::Mutex                          RegionFactory::region_map_lock;
 RegionFactory::RegionMap                      RegionFactory::region_map;
 PBD::ScopedConnectionList*                    RegionFactory::region_list_connections = 0;
-Glib::StaticMutex                             RegionFactory::region_name_maps_mutex;
+Glib::Threads::Mutex                          RegionFactory::region_name_maps_mutex;
 std::map<std::string, uint32_t>               RegionFactory::region_name_number_map;
 std::map<std::string, PBD::ID>                RegionFactory::region_name_map;
 RegionFactory::CompoundAssociations           RegionFactory::_compound_associations;
 
 boost::shared_ptr<Region>
-RegionFactory::create (boost::shared_ptr<const Region> region, bool announce)
+RegionFactory::create (boost::shared_ptr<const Region> region, bool announce, const int32_t sub_num)
 {
        boost::shared_ptr<Region> ret;
        boost::shared_ptr<const AudioRegion> ar;
@@ -53,30 +54,41 @@ RegionFactory::create (boost::shared_ptr<const Region> region, bool announce)
 
        if ((ar = boost::dynamic_pointer_cast<const AudioRegion>(region)) != 0) {
 
-               ret = boost::shared_ptr<Region> (new AudioRegion (ar, 0));
+               ret = boost::shared_ptr<Region> (new AudioRegion (ar, 0, sub_num));
 
        } else if ((mr = boost::dynamic_pointer_cast<const MidiRegion>(region)) != 0) {
 
                if (mr->session().config.get_midi_copy_is_fork()) {
-                       ret = mr->clone ();
+                       /* What we really want to do here is what Editor::fork_region()
+                          does via Session::create_midi_source_by_stealing_name(), but we
+                          don't have a Track.  We'll just live with the skipped number,
+                          and store the ancestral name of sources so multiple clones
+                          generates reasonable names that don't have too many suffixes. */
+                       const std::string ancestor_name = mr->sources().front()->ancestor_name();
+                       const std::string base          = PBD::basename_nosuffix(ancestor_name);
+
+                       boost::shared_ptr<MidiSource> source = mr->session().create_midi_source_for_session(base);
+                       source->set_ancestor_name(mr->sources().front()->name());
+                       ret = mr->clone(source);
                } else {
-                       ret = boost::shared_ptr<Region> (new MidiRegion (mr, 0));
+                       ret = boost::shared_ptr<Region> (new MidiRegion (mr, 0, sub_num));
                }
 
        } else {
                fatal << _("programming error: RegionFactory::create() called with unknown Region type")
                      << endmsg;
-               /*NOTREACHED*/
+               abort(); /*NOTREACHED*/
        }
 
        if (ret) {
                ret->set_name (new_region_name(ret->name()));
-               ret->set_position (region->position());
-               
-               if (ret->session().config.get_glue_new_regions_to_bars_and_beats ()) {
+
+               if (ret->session().config.get_glue_new_regions_to_bars_and_beats() && ret->position_lock_style() != MusicTime) {
                        ret->set_position_lock_style (MusicTime);
                }
 
+               ret->set_position (region->position(), sub_num);
+
                /* pure copy constructor - no property list */
                if (announce) {
                        map_add (ret);
@@ -108,17 +120,17 @@ RegionFactory::create (boost::shared_ptr<Region> region, const PropertyList& pli
        } else {
                fatal << _("programming error: RegionFactory::create() called with unknown Region type")
                      << endmsg;
-               /*NOTREACHED*/
+               abort(); /*NOTREACHED*/
                return boost::shared_ptr<Region>();
        }
 
        if (ret) {
                ret->apply_changes (plist);
 
-               if (ret->session().config.get_glue_new_regions_to_bars_and_beats ()) {
+               if (ret->session().config.get_glue_new_regions_to_bars_and_beats() && ret->position_lock_style() != MusicTime) {
                        ret->set_position_lock_style (MusicTime);
                }
-               
+
                if (announce) {
                        map_add (ret);
                        CheckNewRegion (ret);
@@ -132,7 +144,7 @@ RegionFactory::create (boost::shared_ptr<Region> region, const PropertyList& pli
 }
 
 boost::shared_ptr<Region>
-RegionFactory::create (boost::shared_ptr<Region> region, frameoffset_t offset, const PropertyList& plist, bool announce)
+RegionFactory::create (boost::shared_ptr<Region> region, frameoffset_t offset, const PropertyList& plist, bool announce, const int32_t sub_num)
 {
        boost::shared_ptr<Region> ret;
        boost::shared_ptr<const AudioRegion> other_a;
@@ -140,23 +152,23 @@ RegionFactory::create (boost::shared_ptr<Region> region, frameoffset_t offset, c
 
        if ((other_a = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
 
-               ret = boost::shared_ptr<Region> (new AudioRegion (other_a, offset));
+               ret = boost::shared_ptr<Region> (new AudioRegion (other_a, offset, sub_num));
 
        } else if ((other_m = boost::dynamic_pointer_cast<MidiRegion>(region)) != 0) {
 
-               ret = boost::shared_ptr<Region> (new MidiRegion (other_m, offset));
+               ret = boost::shared_ptr<Region> (new MidiRegion (other_m, offset, sub_num));
 
        } else {
                fatal << _("programming error: RegionFactory::create() called with unknown Region type")
                      << endmsg;
-               /*NOTREACHED*/
+               abort(); /*NOTREACHED*/
                return boost::shared_ptr<Region>();
        }
 
        if (ret) {
                ret->apply_changes (plist);
 
-               if (ret->session().config.get_glue_new_regions_to_bars_and_beats ()) {
+               if (ret->session().config.get_glue_new_regions_to_bars_and_beats() && ret->position_lock_style() != MusicTime) {
                        ret->set_position_lock_style (MusicTime);
                }
 
@@ -191,13 +203,13 @@ RegionFactory::create (boost::shared_ptr<Region> region, const SourceList& srcs,
        } else {
                fatal << _("programming error: RegionFactory::create() called with unknown Region type")
                      << endmsg;
-               /*NOTREACHED*/
+               abort(); /*NOTREACHED*/
        }
 
        if (ret) {
                ret->apply_changes (plist);
 
-               if (ret->session().config.get_glue_new_regions_to_bars_and_beats ()) {
+               if (ret->session().config.get_glue_new_regions_to_bars_and_beats() && ret->position_lock_style() != MusicTime) {
                        ret->set_position_lock_style (MusicTime);
                }
 
@@ -241,10 +253,10 @@ RegionFactory::create (const SourceList& srcs, const PropertyList& plist, bool a
        if (ret) {
                ret->apply_changes (plist);
 
-               if (ret->session().config.get_glue_new_regions_to_bars_and_beats ()) {
+               if (ret->session().config.get_glue_new_regions_to_bars_and_beats() && ret->position_lock_style() != MusicTime) {
                        ret->set_position_lock_style (MusicTime);
                }
-               
+
                if (announce) {
                        map_add (ret);
                        CheckNewRegion (ret);
@@ -291,7 +303,7 @@ RegionFactory::create (SourceList& srcs, const XMLNode& node)
                        /* Don't fiddle with position_lock_style here as the region
                           description is coming from XML.
                        */
-                       
+
                        CheckNewRegion (ret);
                }
        }
@@ -310,7 +322,7 @@ RegionFactory::map_add (boost::shared_ptr<Region> r)
        p.second = r;
 
        {
-               Glib::Mutex::Lock lm (region_map_lock);
+               Glib::Threads::Mutex::Lock lm (region_map_lock);
                region_map.insert (p);
        }
 
@@ -331,8 +343,8 @@ RegionFactory::map_remove (boost::weak_ptr<Region> w)
        if (!r) {
                return;
        }
-       
-       Glib::Mutex::Lock lm (region_map_lock);
+
+       Glib::Threads::Mutex::Lock lm (region_map_lock);
        RegionMap::iterator i = region_map.find (r->id());
 
        if (i != region_map.end()) {
@@ -383,7 +395,7 @@ RegionFactory::clear_map ()
        }
 
        {
-               Glib::Mutex::Lock lm (region_map_lock);
+               Glib::Threads::Mutex::Lock lm (region_map_lock);
                region_map.clear ();
                _compound_associations.clear ();
                region_name_map.clear ();
@@ -397,7 +409,7 @@ RegionFactory::delete_all_regions ()
 
        /* copy region list */
        {
-               Glib::Mutex::Lock lm (region_map_lock);
+               Glib::Threads::Mutex::Lock lm (region_map_lock);
                copy = region_map;
        }
 
@@ -417,7 +429,7 @@ RegionFactory::delete_all_regions ()
 uint32_t
 RegionFactory::nregions ()
 {
-       Glib::Mutex::Lock lm (region_map_lock);
+       Glib::Threads::Mutex::Lock lm (region_map_lock);
        return region_map.size ();
 }
 
@@ -427,7 +439,7 @@ RegionFactory::add_to_region_name_maps (boost::shared_ptr<Region> region)
 {
        update_region_name_number_map (region);
 
-       Glib::Mutex::Lock lm (region_name_maps_mutex);
+       Glib::Threads::Mutex::Lock lm (region_name_maps_mutex);
        region_name_map[region->name()] = region->id ();
 }
 
@@ -437,8 +449,8 @@ RegionFactory::rename_in_region_name_maps (boost::shared_ptr<Region> region)
 {
        update_region_name_number_map (region);
 
-       Glib::Mutex::Lock lm (region_name_maps_mutex);
-       
+       Glib::Threads::Mutex::Lock lm (region_name_maps_mutex);
+
        map<string, PBD::ID>::iterator i = region_name_map.begin();
        while (i != region_name_map.end() && i->second != region->id ()) {
                ++i;
@@ -476,7 +488,7 @@ RegionFactory::update_region_name_number_map (boost::shared_ptr<Region> region)
                   which is just fine
                */
 
-               Glib::Mutex::Lock lm (region_name_maps_mutex);
+               Glib::Threads::Mutex::Lock lm (region_name_maps_mutex);
                region_name_number_map[base] = atoi (number.c_str ());
        }
 }
@@ -526,7 +538,7 @@ RegionFactory::region_name (string& result, string base, bool newlevel)
                }
 
                {
-                       Glib::Mutex::Lock lm (region_name_maps_mutex);
+                       Glib::Threads::Mutex::Lock lm (region_name_maps_mutex);
 
                        map<string,uint32_t>::iterator x;
 
@@ -564,7 +576,7 @@ RegionFactory::new_region_name (string old)
        uint32_t number;
        string::size_type len = old.length() + 64;
        string remainder;
-       char buf[len];
+       std::vector<char> buf(len);
 
        if ((last_period = old.find_last_of ('.')) == string::npos) {
 
@@ -603,8 +615,8 @@ RegionFactory::new_region_name (string old)
 
                number++;
 
-               snprintf (buf, len, "%s%" PRIu32 "%s", old.substr (0, last_period + 1).c_str(), number, remainder.c_str());
-               sbuf = buf;
+               snprintf (&buf[0], len, "%s%" PRIu32 "%s", old.substr (0, last_period + 1).c_str(), number, remainder.c_str());
+               sbuf = &buf[0];
 
                if (region_name_map.find (sbuf) == region_name_map.end ()) {
                        break;
@@ -612,7 +624,7 @@ RegionFactory::new_region_name (string old)
        }
 
        if (number != (UINT_MAX-1)) {
-               return buf;
+               return &buf[0];
        }
 
        error << string_compose (_("cannot create new name for region \"%1\""), old) << endmsg;
@@ -622,9 +634,9 @@ RegionFactory::new_region_name (string old)
 void
 RegionFactory::get_regions_using_source (boost::shared_ptr<Source> s, std::set<boost::shared_ptr<Region> >& r)
 {
-       Glib::Mutex::Lock lm (region_map_lock);
+       Glib::Threads::Mutex::Lock lm (region_map_lock);
 
-       for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
+       for (RegionMap::const_iterator i = region_map.begin(); i != region_map.end(); ++i) {
                if (i->second->uses_source (s)) {
                        r.insert (i->second);
                }
@@ -634,14 +646,14 @@ RegionFactory::get_regions_using_source (boost::shared_ptr<Source> s, std::set<b
 void
 RegionFactory::remove_regions_using_source (boost::shared_ptr<Source> src)
 {
-       Glib::Mutex::Lock lm (region_map_lock);
+       Glib::Threads::Mutex::Lock lm (region_map_lock);
 
        RegionMap::iterator i = region_map.begin();
        while (i != region_map.end()) {
 
                RegionMap::iterator j = i;
                ++j;
-               
+
                if (i->second->uses_source (src)) {
                        remove_from_region_name_map (i->second->name ());
                        region_map.erase (i);
@@ -654,6 +666,6 @@ RegionFactory::remove_regions_using_source (boost::shared_ptr<Source> src)
 void
 RegionFactory::add_compound_association (boost::shared_ptr<Region> orig, boost::shared_ptr<Region> copy)
 {
-       Glib::Mutex::Lock lm (region_map_lock);
+       Glib::Threads::Mutex::Lock lm (region_map_lock);
        _compound_associations[copy] = orig;
 }