Fix various MIDI locking issues.
[ardour.git] / gtk2_ardour / automation_streamview.cc
1 /*
2     Copyright (C) 2001-2007 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 #include <cassert>
20 #include <cmath>
21 #include <list>
22 #include <utility>
23
24 #include <gtkmm.h>
25
26 #include "gtkmm2ext/gtk_ui.h"
27
28 #include "pbd/compose.h"
29 #include "canvas/debug.h"
30
31 #include "ardour/midi_region.h"
32 #include "ardour/midi_source.h"
33
34 #include "ardour_ui.h"
35 #include "automation_region_view.h"
36 #include "automation_streamview.h"
37 #include "automation_time_axis.h"
38 #include "global_signals.h"
39 #include "gui_thread.h"
40 #include "public_editor.h"
41 #include "region_selection.h"
42 #include "region_view.h"
43 #include "rgb_macros.h"
44 #include "selection.h"
45
46 using namespace std;
47 using namespace ARDOUR;
48 using namespace ARDOUR_UI_UTILS;
49 using namespace PBD;
50 using namespace Editing;
51
52 AutomationStreamView::AutomationStreamView (AutomationTimeAxisView& tv)
53         : StreamView (*dynamic_cast<RouteTimeAxisView*>(tv.get_parent()),
54                       tv.canvas_display())
55         , _automation_view(tv)
56         , _pending_automation_state (Off)
57 {
58         CANVAS_DEBUG_NAME (_canvas_group, string_compose ("SV canvas group auto %1", tv.name()));
59         CANVAS_DEBUG_NAME (canvas_rect, string_compose ("SV canvas rectangle auto %1", tv.name()));
60
61         color_handler ();
62
63         ColorsChanged.connect(sigc::mem_fun(*this, &AutomationStreamView::color_handler));
64 }
65
66 AutomationStreamView::~AutomationStreamView ()
67 {
68 }
69
70
71 RegionView*
72 AutomationStreamView::add_region_view_internal (boost::shared_ptr<Region> region, bool wait_for_data, bool /*recording*/)
73 {
74         assert (region);
75
76         if (wait_for_data) {
77                 boost::shared_ptr<MidiRegion> mr = boost::dynamic_pointer_cast<MidiRegion>(region);
78                 if (mr) {
79                         Source::Lock lock(mr->midi_source()->mutex());
80                         mr->midi_source()->load_model(lock);
81                 }
82         }
83
84         const boost::shared_ptr<AutomationControl> control = boost::dynamic_pointer_cast<AutomationControl> (
85                 region->control (_automation_view.parameter(), true)
86                 );
87
88         boost::shared_ptr<AutomationList> list;
89         if (control) {
90                 list = boost::dynamic_pointer_cast<AutomationList>(control->list());
91                 assert(!control->list() || list);
92         }
93
94         AutomationRegionView *region_view;
95         std::list<RegionView *>::iterator i;
96
97         for (i = region_views.begin(); i != region_views.end(); ++i) {
98                 if ((*i)->region() == region) {
99
100                         /* great. we already have an AutomationRegionView for this Region. use it again. */
101                         AutomationRegionView* arv = dynamic_cast<AutomationRegionView*>(*i);;
102
103                         if (arv->line()) {
104                                 arv->line()->set_list (list);
105                         }
106                         (*i)->set_valid (true);
107                         (*i)->enable_display (wait_for_data);
108                         display_region(arv);
109
110                         return 0;
111                 }
112         }
113
114         region_view = new AutomationRegionView (
115                 _canvas_group, _automation_view, region,
116                 _automation_view.parameter (), list,
117                 _samples_per_pixel, region_color
118                 );
119
120         region_view->init (false);
121         region_views.push_front (region_view);
122
123         /* follow global waveform setting */
124
125         if (wait_for_data) {
126                 region_view->enable_display(true);
127                 // region_view->midi_region()->midi_source(0)->load_model();
128         }
129
130         display_region (region_view);
131
132         /* catch regionview going away */
133         region->DropReferences.connect (*this, invalidator (*this), boost::bind (&AutomationStreamView::remove_region_view, this, boost::weak_ptr<Region>(region)), gui_context());
134
135         /* setup automation state for this region */
136         boost::shared_ptr<AutomationLine> line = region_view->line ();
137         if (line && line->the_list()) {
138                 line->the_list()->set_automation_state (automation_state ());
139         }
140
141         RegionViewAdded (region_view);
142
143         return region_view;
144 }
145
146 void
147 AutomationStreamView::display_region(AutomationRegionView* region_view)
148 {
149         region_view->line().reset();
150 }
151
152 void
153 AutomationStreamView::set_automation_state (AutoState state)
154 {
155         /* Setting the automation state for this view sets the state of all regions' lists to the same thing */
156
157         if (region_views.empty()) {
158                 _pending_automation_state = state;
159         } else {
160                 list<boost::shared_ptr<AutomationLine> > lines = get_lines ();
161
162                 for (list<boost::shared_ptr<AutomationLine> >::iterator i = lines.begin(); i != lines.end(); ++i) {
163                         if ((*i)->the_list()) {
164                                 (*i)->the_list()->set_automation_state (state);
165                         }
166                 }
167         }
168 }
169
170 void
171 AutomationStreamView::redisplay_track ()
172 {
173         // Flag region views as invalid and disable drawing
174         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
175                 (*i)->set_valid (false);
176                 (*i)->enable_display(false);
177         }
178
179         // Add and display region views, and flag them as valid
180         if (_trackview.is_track()) {
181                 _trackview.track()->playlist()->foreach_region (
182                         sigc::hide_return (sigc::mem_fun (*this, &StreamView::add_region_view))
183                         );
184         }
185
186         // Stack regions by layer, and remove invalid regions
187         layer_regions();
188 }
189
190
191 void
192 AutomationStreamView::setup_rec_box ()
193 {
194 }
195
196 void
197 AutomationStreamView::color_handler ()
198 {
199         if (_trackview.is_midi_track()) {
200                 canvas_rect->set_fill_color (ARDOUR_UI::config()->color_mod ("midi track base", "midi track base"));
201         } else {
202                 canvas_rect->set_fill_color (ARDOUR_UI::config()->color ("midi bus base"));
203         }
204 }
205
206 AutoState
207 AutomationStreamView::automation_state () const
208 {
209         if (region_views.empty()) {
210                 return _pending_automation_state;
211         }
212
213         boost::shared_ptr<AutomationLine> line = ((AutomationRegionView*) region_views.front())->line ();
214         if (!line || !line->the_list()) {
215                 return Off;
216         }
217
218         return line->the_list()->automation_state ();
219 }
220
221 bool
222 AutomationStreamView::has_automation () const
223 {
224         list<boost::shared_ptr<AutomationLine> > lines = get_lines ();
225
226         for (list<boost::shared_ptr<AutomationLine> >::iterator i = lines.begin(); i != lines.end(); ++i) {
227                 if ((*i)->npoints() > 0) {
228                         return true;
229                 }
230         }
231
232         return false;
233 }
234
235 /** Our parent AutomationTimeAxisView calls this when the user requests a particular
236  *  InterpolationStyle; tell the AutomationLists in our regions.
237  */
238 void
239 AutomationStreamView::set_interpolation (AutomationList::InterpolationStyle s)
240 {
241         list<boost::shared_ptr<AutomationLine> > lines = get_lines ();
242
243         for (list<boost::shared_ptr<AutomationLine> >::iterator i = lines.begin(); i != lines.end(); ++i) {
244                 (*i)->the_list()->set_interpolation (s);
245         }
246 }
247
248 AutomationList::InterpolationStyle
249 AutomationStreamView::interpolation () const
250 {
251         if (region_views.empty()) {
252                 return AutomationList::Linear;
253         }
254
255         AutomationRegionView* v = dynamic_cast<AutomationRegionView*> (region_views.front());
256         if (v) {
257                 return v->line()->the_list()->interpolation ();
258         }
259         return AutomationList::Linear;
260 }
261
262 /** Clear all automation displayed in this view */
263 void
264 AutomationStreamView::clear ()
265 {
266         list<boost::shared_ptr<AutomationLine> > lines = get_lines ();
267
268         for (list<boost::shared_ptr<AutomationLine> >::iterator i = lines.begin(); i != lines.end(); ++i) {
269                 (*i)->clear ();
270         }
271 }
272
273 /** @param start Start position in session frames.
274  *  @param end End position in session frames.
275  *  @param bot Bottom position expressed as a fraction of track height where 0 is the bottom of the track.
276  *  @param top Top position expressed as a fraction of track height where 0 is the bottom of the track.
277  *  NOTE: this y system is different to that for the StreamView method that this overrides, which is a little
278  *  confusing.
279  */
280 void
281 AutomationStreamView::get_selectables (framepos_t start, framepos_t end, double botfrac, double topfrac, list<Selectable*>& results)
282 {
283         if (!_trackview.editor().internal_editing()) {
284                 return;  // TODO: selection of automation regions
285         }
286         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
287                 AutomationRegionView* arv = dynamic_cast<AutomationRegionView*> (*i);
288                 assert (arv);
289                 arv->line()->get_selectables (start, end, botfrac, topfrac, results);
290         }
291 }
292
293 void
294 AutomationStreamView::set_selected_points (PointSelection& ps)
295 {
296         list<boost::shared_ptr<AutomationLine> > lines = get_lines ();
297
298         for (list<boost::shared_ptr<AutomationLine> >::iterator i = lines.begin(); i != lines.end(); ++i) {
299                 (*i)->set_selected_points (ps);
300         }
301 }
302
303 list<boost::shared_ptr<AutomationLine> >
304 AutomationStreamView::get_lines () const
305 {
306         list<boost::shared_ptr<AutomationLine> > lines;
307
308         for (list<RegionView*>::const_iterator i = region_views.begin(); i != region_views.end(); ++i) {
309                 AutomationRegionView* arv = dynamic_cast<AutomationRegionView*> (*i);
310                 assert (arv);
311                 lines.push_back (arv->line());
312         }
313
314         return lines;
315 }
316
317 struct RegionPositionSorter {
318         bool operator() (RegionView* a, RegionView* b) {
319                 return a->region()->position() < b->region()->position();
320         }
321 };
322
323
324 bool
325 AutomationStreamView::paste (framepos_t                                pos,
326                              unsigned                                  paste_count,
327                              float                                     times,
328                              boost::shared_ptr<ARDOUR::AutomationList> alist)
329 {
330         /* XXX: not sure how best to pick this; for now, just use the last region which starts before pos */
331
332         if (region_views.empty()) {
333                 return false;
334         }
335
336         region_views.sort (RegionPositionSorter ());
337
338         list<RegionView*>::const_iterator prev = region_views.begin ();
339
340         for (list<RegionView*>::const_iterator i = region_views.begin(); i != region_views.end(); ++i) {
341                 if ((*i)->region()->position() > pos) {
342                         break;
343                 }
344                 prev = i;
345         }
346
347         boost::shared_ptr<Region> r = (*prev)->region ();
348
349         /* If *prev doesn't cover pos, it's no good */
350         if (r->position() > pos || ((r->position() + r->length()) < pos)) {
351                 return false;
352         }
353
354         AutomationRegionView* arv = dynamic_cast<AutomationRegionView*> (*prev);
355         return arv ? arv->paste(pos, paste_count, times, alist) : false;
356 }