fix ordering of cut/copied regions when pasting; ctrl-click now does the right thing...
[ardour.git] / libs / ardour / session.cc
index 932db17909ed4e3f05eb70be4a6a7d4dd21f9d2b..eba090665a173559c4a672cac964e0e160e170f7 100644 (file)
@@ -281,12 +281,13 @@ Session::Session (AudioEngine &eng,
        if (new_session) {
                if (create (new_session, mix_template, compute_initial_length())) {
                        cerr << "create failed\n";
+                       destroy ();
                        throw failed_constructor ();
                }
        }
        
        if (second_stage_init (new_session)) {
-               cerr << "2nd state failed\n";
+               destroy ();
                throw failed_constructor ();
        }
        
@@ -346,33 +347,45 @@ Session::Session (AudioEngine &eng,
 
        if (new_session) {
                if (create (new_session, 0, initial_length)) {
+                       destroy ();
                        throw failed_constructor ();
                }
        }
 
-       if (control_out_channels) {
-               shared_ptr<Route> r (new Route (*this, _("monitor"), -1, control_out_channels, -1, control_out_channels, Route::ControlOut));
-               RouteList rl;
-               rl.push_back (r);
-               add_routes (rl);
-               _control_out = r;
-       }
-
-       if (master_out_channels) {
-               shared_ptr<Route> r (new Route (*this, _("master"), -1, master_out_channels, -1, master_out_channels, Route::MasterOut));
+       {
+               /* set up Master Out and Control Out if necessary */
+               
                RouteList rl;
-               rl.push_back (r);
-               add_routes (rl);
-               _master_out = r;
-       } else {
-               /* prohibit auto-connect to master, because there isn't one */
-               output_ac = AutoConnectOption (output_ac & ~AutoConnectMaster);
+               int control_id = 1;
+               
+               if (control_out_channels) {
+                       shared_ptr<Route> r (new Route (*this, _("monitor"), -1, control_out_channels, -1, control_out_channels, Route::ControlOut));
+                       r->set_remote_control_id (control_id++);
+                       
+                       rl.push_back (r);
+               }
+               
+               if (master_out_channels) {
+                       shared_ptr<Route> r (new Route (*this, _("master"), -1, master_out_channels, -1, master_out_channels, Route::MasterOut));
+                       r->set_remote_control_id (control_id);
+                        
+                       rl.push_back (r);
+               } else {
+                       /* prohibit auto-connect to master, because there isn't one */
+                       output_ac = AutoConnectOption (output_ac & ~AutoConnectMaster);
+               }
+               
+               if (!rl.empty()) {
+                       add_routes (rl);
+               }
+               
        }
 
        Config->set_input_auto_connect (input_ac);
        Config->set_output_auto_connect (output_ac);
 
        if (second_stage_init (new_session)) {
+               destroy ();
                throw failed_constructor ();
        }
        
@@ -382,12 +395,20 @@ Session::Session (AudioEngine &eng,
 
        _state_of_the_state = StateOfTheState (_state_of_the_state & ~Dirty);
 
+       Config->ParameterChanged.connect (mem_fun (*this, &Session::config_changed));
+
        if (was_dirty) {
                DirtyChanged (); /* EMIT SIGNAL */
        }
 }
 
 Session::~Session ()
+{
+       destroy ();
+}
+
+void
+Session::destroy ()
 {
        /* if we got to here, leaving pending capture state around
           is a mistake.
@@ -1606,6 +1627,7 @@ Session::new_audio_track (int input_channels, int output_channels, TrackMode mod
        string port;
        RouteList new_routes;
        list<boost::shared_ptr<AudioTrack> > ret;
+       uint32_t control_id;
 
        /* count existing audio tracks */
 
@@ -1629,6 +1651,7 @@ Session::new_audio_track (int input_channels, int output_channels, TrackMode mod
 
        _engine.get_physical_outputs (physoutputs);
        _engine.get_physical_inputs (physinputs);
+       control_id = ntracks() + nbusses() + 1;
 
        while (how_many) {
 
@@ -1717,7 +1740,8 @@ Session::new_audio_track (int input_channels, int output_channels, TrackMode mod
                        }
                        
                        track->DiskstreamChanged.connect (mem_fun (this, &Session::resort_routes));
-                       track->set_remote_control_id (ntracks());
+                       track->set_remote_control_id (control_id);
+                       ++control_id;
 
                        new_routes.push_back (track);
                        ret.push_back (track);
@@ -1749,6 +1773,7 @@ Session::new_audio_route (int input_channels, int output_channels, uint32_t how_
        uint32_t n = 0;
        string port;
        RouteList ret;
+       uint32_t control_id;
 
        /* count existing audio busses */
 
@@ -1769,6 +1794,7 @@ Session::new_audio_route (int input_channels, int output_channels, uint32_t how_
 
        _engine.get_physical_outputs (physoutputs);
        _engine.get_physical_inputs (physinputs);
+       control_id = ntracks() + nbusses() + 1;
 
        while (how_many) {
 
@@ -1832,6 +1858,9 @@ Session::new_audio_route (int input_channels, int output_channels, uint32_t how_
                                bus->set_control_outs (cports);
                        }
 
+                       bus->set_remote_control_id (control_id);
+                       ++control_id;
+
                        ret.push_back (bus);
                }
        
@@ -2682,6 +2711,7 @@ Session::add_source (boost::shared_ptr<Source> source)
                        source->GoingAway.connect (sigc::bind (mem_fun (this, &Session::remove_source), boost::weak_ptr<Source> (source)));
                        set_dirty();
                }
+
        } 
 }
 
@@ -2929,6 +2959,7 @@ Session::audio_path_from_name (string name, uint32_t nchan, uint32_t chan, bool
 
                if (cnt > limit) {
                        error << string_compose(_("There are already %1 recordings for %2, which I consider too many."), limit, name) << endmsg;
+                       destroy ();
                        throw failed_constructor();
                }
        }
@@ -3069,19 +3100,16 @@ Session::remove_playlist (boost::weak_ptr<Playlist> weak_playlist)
 
        { 
                Glib::Mutex::Lock lm (playlist_lock);
-               cerr << "removing playlist: " << playlist->name() << endl;
 
                PlaylistList::iterator i;
 
                i = find (playlists.begin(), playlists.end(), playlist);
                if (i != playlists.end()) {
-                       cerr << "\tfound it in used playlist\n";
                        playlists.erase (i);
                }
 
                i = find (unused_playlists.begin(), unused_playlists.end(), playlist);
                if (i != unused_playlists.end()) {
-                       cerr << "\tfound it in unused playlist\n";
                        unused_playlists.erase (i);
                }
                
@@ -3336,10 +3364,14 @@ Session::remove_redirect (Redirect* redirect)
        Insert* insert;
        PortInsert* port_insert;
        PluginInsert* plugin_insert;
-
+       
        if ((insert = dynamic_cast<Insert *> (redirect)) != 0) {
                if ((port_insert = dynamic_cast<PortInsert *> (insert)) != 0) {
-                       _port_inserts.remove (port_insert);
+                       list<PortInsert*>::iterator x = find (_port_inserts.begin(), _port_inserts.end(), port_insert);
+                       if (x != _port_inserts.end()) {
+                               insert_bitset[port_insert->bit_slot()] = false;
+                               _port_inserts.erase (x);
+                       }
                } else if ((plugin_insert = dynamic_cast<PluginInsert *> (insert)) != 0) {
                        _plugin_inserts.remove (plugin_insert);
                } else {
@@ -3349,7 +3381,11 @@ Session::remove_redirect (Redirect* redirect)
                        /*NOTREACHED*/
                }
        } else if ((send = dynamic_cast<Send *> (redirect)) != 0) {
-               _sends.remove (send);
+               list<Send*>::iterator x = find (_sends.begin(), _sends.end(), send);
+               if (x != _sends.end()) {
+                       send_bitset[send->bit_slot()] = false;
+                       _sends.erase (x);
+               }
        } else {
                fatal << _("programming error: unknown type of Redirect deleted!") << endmsg;
                /*NOTREACHED*/
@@ -3481,26 +3517,70 @@ Session::ensure_passthru_buffers (uint32_t howmany)
        allocate_pan_automation_buffers (current_block_size, howmany, false);
 }
 
-string
-Session::next_send_name ()
+uint32_t
+Session::next_insert_id ()
 {
-       uint32_t cnt = 0;
-       
-       shared_ptr<RouteList> r = routes.reader ();
-       
-       for (RouteList::const_iterator i = r->begin(); i != r->end(); ++i) {
-               cnt += (*i)->count_sends ();
+       /* this doesn't really loop forever. just think about it */
+
+       while (true) {
+               for (boost::dynamic_bitset<uint32_t>::size_type n = 0; n < insert_bitset.size(); ++n) {
+                       if (!insert_bitset[n]) {
+                               insert_bitset[n] = true;
+                               cerr << "Returning " << n << " as insert ID\n";
+                               return n;
+                               
+                       }
+               }
+               
+               /* none available, so resize and try again */
+
+               insert_bitset.resize (insert_bitset.size() + 16, false);
+       }
+}
+
+uint32_t
+Session::next_send_id ()
+{
+       /* this doesn't really loop forever. just think about it */
+
+       while (true) {
+               for (boost::dynamic_bitset<uint32_t>::size_type n = 0; n < send_bitset.size(); ++n) {
+                       if (!send_bitset[n]) {
+                               send_bitset[n] = true;
+                               cerr << "Returning " << n << " as send ID\n";
+                               return n;
+                               
+                       }
+               }
+               
+               /* none available, so resize and try again */
+
+               send_bitset.resize (send_bitset.size() + 16, false);
        }
+}
 
-       return string_compose (_("send %1"), ++cnt);
+void
+Session::mark_send_id (uint32_t id)
+{
+       if (id >= send_bitset.size()) {
+               send_bitset.resize (id+16, false);
+       }
+       if (send_bitset[id]) {
+               warning << string_compose (_("send ID %1 appears to be in use already"), id) << endmsg;
+       }
+       send_bitset[id] = true;
 }
 
-string
-Session::next_insert_name ()
+void
+Session::mark_insert_id (uint32_t id)
 {
-       char buf[32];
-       snprintf (buf, sizeof (buf), "insert %" PRIu32, ++insert_cnt);
-       return buf;
+       if (id >= insert_bitset.size()) {
+               insert_bitset.resize (id+16, false);
+       }
+       if (insert_bitset[id]) {
+               warning << string_compose (_("insert ID %1 appears to be in use already"), id) << endmsg;
+       }
+       insert_bitset[id] = true;
 }
 
 /* Named Selection management */
@@ -3525,9 +3605,13 @@ Session::add_named_selection (NamedSelection* named_selection)
                named_selections.insert (named_selections.begin(), named_selection);
        }
 
+       for (list<boost::shared_ptr<Playlist> >::iterator i = named_selection->playlists.begin(); i != named_selection->playlists.end(); ++i) {
+               add_playlist (*i);
+       }
+
        set_dirty();
 
-        NamedSelectionAdded (); /* EMIT SIGNAL */
+       NamedSelectionAdded (); /* EMIT SIGNAL */
 }
 
 void
@@ -3577,12 +3661,6 @@ Session::route_name_unique (string n) const
        return true;
 }
 
-int
-Session::cleanup_audio_file_source (boost::shared_ptr<AudioFileSource> fs)
-{
-       return fs->move_to_trash (dead_sound_dir_name);
-}
-
 uint32_t
 Session::n_playlists () const
 {
@@ -3768,7 +3846,7 @@ Session::write_one_audio_track (AudioTrack& track, nframes_t start, nframes_t le
                /* construct a region to represent the bounced material */
 
                boost::shared_ptr<Region> aregion = RegionFactory::create (srcs, 0, srcs.front()->length(), 
-                                                                          region_name_from_path (srcs.front()->name()));
+                                                                          region_name_from_path (srcs.front()->name(), true));
 
                ret = 0;
        }