Fix crash when X11 is not available for VST UIs
[ardour.git] / gtk2_ardour / streamview.cc
1 /*
2  * Copyright (C) 2005-2019 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2005 Taybin Rutkin <taybin@taybin.com>
4  * Copyright (C) 2006-2007 Jesse Chappell <jesse@essej.net>
5  * Copyright (C) 2006-2015 David Robillard <d@drobilla.net>
6  * Copyright (C) 2007-2012 Carl Hetherington <carl@carlh.net>
7  * Copyright (C) 2007 Doug McLain <doug@nostar.net>
8  * Copyright (C) 2014-2015 Tim Mayberry <mojofunk@gmail.com>
9  * Copyright (C) 2014-2019 Robin Gareus <robin@gareus.org>
10  * Copyright (C) 2016 Nick Mainsbridge <mainsbridge@gmail.com>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License along
23  * with this program; if not, write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25  */
26
27 #include <cmath>
28
29 #include <gtkmm.h>
30
31 #include <gtkmm2ext/gtk_ui.h>
32 #include <gtkmm2ext/utils.h>
33
34 #include "ardour/playlist.h"
35 #include "ardour/region.h"
36 #include "ardour/track.h"
37 #include "ardour/session.h"
38
39 #include "pbd/compose.h"
40
41 #include "canvas/rectangle.h"
42 #include "canvas/debug.h"
43
44 #include "streamview.h"
45 #include "region_view.h"
46 #include "route_time_axis.h"
47 #include "region_selection.h"
48 #include "selection.h"
49 #include "public_editor.h"
50 #include "timers.h"
51 #include "rgb_macros.h"
52 #include "gui_thread.h"
53 #include "ui_config.h"
54 #include "utils.h"
55
56 #include "pbd/i18n.h"
57
58 using namespace std;
59 using namespace ARDOUR;
60 using namespace ARDOUR_UI_UTILS;
61 using namespace PBD;
62 using namespace Editing;
63
64 StreamView::StreamView (RouteTimeAxisView& tv, ArdourCanvas::Container* canvas_group)
65         : _trackview (tv)
66         , _canvas_group (canvas_group ? canvas_group : new ArdourCanvas::Container (_trackview.canvas_display()))
67         , _samples_per_pixel (_trackview.editor().get_current_zoom ())
68         , rec_updating(false)
69         , rec_active(false)
70         , stream_base_color(0xFFFFFFFF)
71         , _layers (1)
72         , _layer_display (Overlaid)
73         , height (tv.height)
74         , last_rec_data_sample(0)
75 {
76         CANVAS_DEBUG_NAME (_canvas_group, string_compose ("SV canvas group %1", _trackview.name()));
77
78         /* set_position() will position the group */
79
80         canvas_rect = new ArdourCanvas::Rectangle (_canvas_group);
81         CANVAS_DEBUG_NAME (canvas_rect, string_compose ("SV canvas rectangle %1", _trackview.name()));
82         canvas_rect->set (ArdourCanvas::Rect (0, 0, ArdourCanvas::COORD_MAX, tv.current_height ()));
83         canvas_rect->set_outline (false);
84         canvas_rect->set_fill (true);
85         canvas_rect->Event.connect (sigc::bind (sigc::mem_fun (_trackview.editor(), &PublicEditor::canvas_stream_view_event), canvas_rect, &_trackview));
86
87         if (_trackview.is_track()) {
88                 _trackview.track()->rec_enable_control()->Changed.connect (*this, invalidator (*this), boost::bind (&StreamView::rec_enable_changed, this), gui_context());
89
90                 _trackview.session()->TransportStateChange.connect (*this, invalidator (*this), boost::bind (&StreamView::transport_changed, this), gui_context());
91                 _trackview.session()->TransportLooped.connect (*this, invalidator (*this), boost::bind (&StreamView::transport_looped, this), gui_context());
92                 _trackview.session()->RecordStateChanged.connect (*this, invalidator (*this), boost::bind (&StreamView::sess_rec_enable_changed, this), gui_context());
93         }
94
95         UIConfiguration::instance().ColorsChanged.connect (sigc::mem_fun (*this, &StreamView::color_handler));
96 }
97
98 StreamView::~StreamView ()
99 {
100         undisplay_track ();
101
102         delete canvas_rect;
103 }
104
105 void
106 StreamView::attach ()
107 {
108         if (_trackview.is_track()) {
109                 display_track (_trackview.track ());
110         }
111 }
112
113 int
114 StreamView::set_position (gdouble x, gdouble y)
115 {
116         _canvas_group->set_position (ArdourCanvas::Duple (x, y));
117         return 0;
118 }
119
120 int
121 StreamView::set_height (double h)
122 {
123         /* limit the values to something sane-ish */
124
125         if (h < 10.0 || h > 1000.0) {
126                 return -1;
127         }
128
129         if (height == h) {
130                 return 0;
131         }
132
133         height = h;
134         canvas_rect->set_y1 (height);
135         update_contents_height ();
136
137         return 0;
138 }
139
140 int
141 StreamView::set_samples_per_pixel (double fpp)
142 {
143         RegionViewList::iterator i;
144
145         if (fpp < 1.0) {
146                 return -1;
147         }
148
149         if (fpp == _samples_per_pixel) {
150                 return 0;
151         }
152
153         _samples_per_pixel = fpp;
154
155         for (i = region_views.begin(); i != region_views.end(); ++i) {
156                 (*i)->set_samples_per_pixel (fpp);
157         }
158
159         for (vector<RecBoxInfo>::iterator xi = rec_rects.begin(); xi != rec_rects.end(); ++xi) {
160                 RecBoxInfo &recbox = (*xi);
161
162                 ArdourCanvas::Coord const xstart = _trackview.editor().sample_to_pixel (recbox.start);
163                 ArdourCanvas::Coord const xend = _trackview.editor().sample_to_pixel (recbox.start + recbox.length);
164
165                 recbox.rectangle->set_x0 (xstart);
166                 recbox.rectangle->set_x1 (xend);
167         }
168
169         update_coverage_frame ();
170
171         return 0;
172 }
173
174 void
175 StreamView::add_region_view (boost::weak_ptr<Region> wr)
176 {
177         boost::shared_ptr<Region> r (wr.lock());
178         if (!r) {
179                 return;
180         }
181
182         add_region_view_internal (r, true);
183
184         if (_layer_display == Stacked || _layer_display == Expanded) {
185                 update_contents_height ();
186         }
187 }
188
189 void
190 StreamView::remove_region_view (boost::weak_ptr<Region> weak_r)
191 {
192         ENSURE_GUI_THREAD (*this, &StreamView::remove_region_view, weak_r)
193
194         boost::shared_ptr<Region> r (weak_r.lock());
195
196         if (!r) {
197                 return;
198         }
199
200         for (list<RegionView *>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
201                 if (((*i)->region()) == r) {
202                         RegionView* rv = *i;
203                         region_views.erase (i);
204                         delete rv;
205                         break;
206                 }
207         }
208
209         RegionViewRemoved (); /* EMIT SIGNAL */
210 }
211
212 void
213 StreamView::undisplay_track ()
214 {
215         for (RegionViewList::iterator i = region_views.begin(); i != region_views.end() ; ) {
216                 RegionViewList::iterator next = i;
217                 ++next;
218                 delete *i;
219                 i = next;
220         }
221
222         region_views.clear();
223 }
224
225 void
226 StreamView::display_track (boost::shared_ptr<Track> tr)
227 {
228         playlist_switched_connection.disconnect();
229         playlist_switched (tr);
230         tr->PlaylistChanged.connect (playlist_switched_connection, invalidator (*this), boost::bind (&StreamView::playlist_switched, this, boost::weak_ptr<Track> (tr)), gui_context());
231 }
232
233 void
234 StreamView::layer_regions()
235 {
236         // In one traversal of the region view list:
237         // - Build a list of region views sorted by layer
238         // - Remove invalid views from the actual region view list
239         RegionViewList copy;
240         list<RegionView*>::iterator i, tmp;
241         for (i = region_views.begin(); i != region_views.end(); ) {
242                 tmp = i;
243                 tmp++;
244
245                 if (!(*i)->is_valid()) {
246                         delete *i;
247                         region_views.erase (i);
248                         i = tmp;
249                         continue;
250                 } else {
251                         (*i)->enable_display(true);
252                 }
253
254                 if (copy.size() == 0) {
255                         copy.push_front((*i));
256                         i = tmp;
257                         continue;
258                 }
259
260                 RegionViewList::iterator k = copy.begin();
261                 RegionViewList::iterator l = copy.end();
262                 l--;
263
264                 if ((*i)->region()->layer() <= (*k)->region()->layer()) {
265                         copy.push_front((*i));
266                         i = tmp;
267                         continue;
268                 } else if ((*i)->region()->layer() >= (*l)->region()->layer()) {
269                         copy.push_back((*i));
270                         i = tmp;
271                         continue;
272                 }
273
274                 for (RegionViewList::iterator j = copy.begin(); j != copy.end(); ++j) {
275                         if ((*j)->region()->layer() >= (*i)->region()->layer()) {
276                                 copy.insert(j, (*i));
277                                 break;
278                         }
279                 }
280
281                 i = tmp;
282         }
283
284         // Fix canvas layering by raising each to the top in the sorted order.
285         for (RegionViewList::iterator i = copy.begin(); i != copy.end(); ++i) {
286                 (*i)->get_canvas_group()->raise_to_top ();
287         }
288 }
289
290 void
291 StreamView::playlist_layered (boost::weak_ptr<Track> wtr)
292 {
293         boost::shared_ptr<Track> tr (wtr.lock());
294
295         if (!tr) {
296                 return;
297         }
298
299         /* update layers count and the y positions and heights of our regions */
300         if (tr->playlist()) {
301                 _layers = tr->playlist()->top_layer() + 1;
302         }
303
304         if (_layer_display == Stacked) {
305                 update_contents_height ();
306                 /* tricky. playlist_changed() does this as well, and its really inefficient. */
307                 update_coverage_frame ();
308         } else {
309                 /* layering has probably been modified. reflect this in the canvas. */
310                 layer_regions();
311         }
312 }
313
314 void
315 StreamView::playlist_switched (boost::weak_ptr<Track> wtr)
316 {
317         boost::shared_ptr<Track> tr (wtr.lock());
318
319         if (!tr) {
320                 return;
321         }
322
323         /* disconnect from old playlist */
324
325         playlist_connections.drop_connections ();
326         //undisplay_track ();
327
328         /* draw it */
329         tr->playlist()->freeze();
330         redisplay_track ();
331         tr->playlist()->thaw();
332         /* update layers count and the y positions and heights of our regions */
333         _layers = tr->playlist()->top_layer() + 1;
334         update_contents_height ();
335         update_coverage_frame ();
336
337         /* catch changes */
338
339         tr->playlist()->LayeringChanged.connect (playlist_connections, invalidator (*this), boost::bind (&StreamView::playlist_layered, this, boost::weak_ptr<Track> (tr)), gui_context());
340         tr->playlist()->RegionAdded.connect (playlist_connections, invalidator (*this), boost::bind (&StreamView::add_region_view, this, _1), gui_context());
341         tr->playlist()->RegionRemoved.connect (playlist_connections, invalidator (*this), boost::bind (&StreamView::remove_region_view, this, _1), gui_context());
342         tr->playlist()->ContentsChanged.connect (playlist_connections, invalidator (*this), boost::bind (&StreamView::update_coverage_frame, this), gui_context());
343 }
344
345
346 void
347 StreamView::diskstream_changed ()
348 {
349         boost::shared_ptr<Track> t;
350
351         if ((t = _trackview.track()) != 0) {
352                 Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&StreamView::display_track, this, t));
353         } else {
354                 Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&StreamView::undisplay_track, this));
355         }
356 }
357
358 void
359 StreamView::apply_color (Gdk::Color const& c, ColorTarget target)
360 {
361         return apply_color (gdk_color_to_rgba (c), target);
362 }
363
364 void
365 StreamView::apply_color (uint32_t color, ColorTarget target)
366 {
367         list<RegionView *>::iterator i;
368
369         switch (target) {
370         case RegionColor:
371                 region_color = color;
372                 for (i = region_views.begin(); i != region_views.end(); ++i) {
373                         (*i)->set_color (region_color);
374                 }
375                 break;
376
377         case StreamBaseColor:
378                 stream_base_color = color;
379                 canvas_rect->set_fill_color (stream_base_color);
380                 break;
381         }
382 }
383
384 void
385 StreamView::region_layered (RegionView* rv)
386 {
387         /* don't ever leave it at the bottom, since then it doesn't
388            get events - the  parent group does instead ...
389         */
390         rv->get_canvas_group()->raise (rv->region()->layer());
391 }
392
393 void
394 StreamView::rec_enable_changed ()
395 {
396         setup_rec_box ();
397 }
398
399 void
400 StreamView::sess_rec_enable_changed ()
401 {
402         setup_rec_box ();
403 }
404
405 void
406 StreamView::transport_changed()
407 {
408         setup_rec_box ();
409 }
410
411 void
412 StreamView::transport_looped()
413 {
414         // to force a new rec region
415         rec_active = false;
416         Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&StreamView::setup_rec_box, this));
417 }
418
419 void
420 StreamView::create_rec_box(samplepos_t sample_pos, double width)
421 {
422         const double   xstart     = _trackview.editor().sample_to_pixel(sample_pos);
423         const double   xend       = xstart + width;
424         const uint32_t fill_color = UIConfiguration::instance().color_mod("recording rect", "recording_rect");
425
426         ArdourCanvas::Rectangle* rec_rect = new ArdourCanvas::Rectangle(_canvas_group);
427         rec_rect->set_x0(xstart);
428         rec_rect->set_y0(0);
429         rec_rect->set_x1(xend);
430         rec_rect->set_y1(child_height ());
431         rec_rect->set_outline_what(ArdourCanvas::Rectangle::What(0));
432         rec_rect->set_outline_color(UIConfiguration::instance().color("recording rect"));
433         rec_rect->set_fill_color(fill_color);
434         rec_rect->lower_to_bottom();
435
436         RecBoxInfo recbox;
437         recbox.rectangle = rec_rect;
438         recbox.length    = 0;
439
440         if (rec_rects.empty()) {
441                 recbox.start = _trackview.session()->record_location ();
442         } else {
443                 recbox.start = _trackview.session()->transport_sample ();
444         }
445
446         rec_rects.push_back (recbox);
447
448         screen_update_connection.disconnect();
449         screen_update_connection = Timers::rapid_connect (sigc::mem_fun(*this, &StreamView::update_rec_box));
450
451         rec_updating = true;
452         rec_active = true;
453 }
454
455 void
456 StreamView::update_rec_box ()
457 {
458         if (rec_active && rec_rects.size() > 0) {
459                 /* only update the last box */
460                 RecBoxInfo & rect = rec_rects.back();
461                 samplepos_t const at = _trackview.track()->current_capture_end ();
462                 double xstart;
463                 double xend;
464
465                 switch (_trackview.track()->mode()) {
466
467                 case NonLayered:
468                 case Normal:
469                         rect.length = at - rect.start;
470                         xstart = _trackview.editor().sample_to_pixel (rect.start);
471                         xend = _trackview.editor().sample_to_pixel (at);
472                         break;
473
474                 case Destructive:
475                         rect.length = 2;
476                         xstart = _trackview.editor().sample_to_pixel (_trackview.track()->current_capture_start());
477                         xend = _trackview.editor().sample_to_pixel (at);
478                         break;
479
480                 default:
481                         fatal << string_compose (_("programming error: %1"), "illegal track mode") << endmsg;
482                         abort(); /*NOTREACHED*/
483                         return;
484                 }
485
486                 rect.rectangle->set_x0 (xstart);
487                 rect.rectangle->set_x1 (xend);
488         }
489 }
490
491 RegionView*
492 StreamView::find_view (boost::shared_ptr<const Region> region)
493 {
494         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
495
496                 if ((*i)->region() == region) {
497                         return *i;
498                 }
499         }
500         return 0;
501 }
502
503 uint32_t
504 StreamView::num_selected_regionviews () const
505 {
506         uint32_t cnt = 0;
507
508         for (list<RegionView*>::const_iterator i = region_views.begin(); i != region_views.end(); ++i) {
509                 if ((*i)->selected()) {
510                         ++cnt;
511                 }
512         }
513         return cnt;
514 }
515
516 void
517 StreamView::foreach_regionview (sigc::slot<void,RegionView*> slot)
518 {
519         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
520                 slot (*i);
521         }
522 }
523
524 void
525 StreamView::foreach_selected_regionview (sigc::slot<void,RegionView*> slot)
526 {
527         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
528                 if ((*i)->selected()) {
529                         slot (*i);
530                 }
531         }
532 }
533
534 void
535 StreamView::set_selected_regionviews (RegionSelection& regions)
536 {
537         bool selected;
538
539         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
540
541                 selected = false;
542
543                 for (RegionSelection::iterator ii = regions.begin(); ii != regions.end(); ++ii) {
544                         if (*i == *ii) {
545                                 selected = true;
546                                 break;
547                         }
548                 }
549
550                 (*i)->set_selected (selected);
551         }
552 }
553
554 /** Get selectable things within a given range.
555  *  @param start Start time in session samples.
556  *  @param end End time in session samples.
557  *  @param top Top y range, in trackview coordinates (ie 0 is the top of the track view)
558  *  @param bot Bottom y range, in trackview coordinates (ie 0 is the top of the track view)
559  *  @param result Filled in with selectable things.
560  */
561 void
562 StreamView::get_selectables (samplepos_t start, samplepos_t end, double top, double bottom, list<Selectable*>& results, bool within)
563 {
564         if (_trackview.editor().internal_editing()) {
565                 return;  // Don't select regions with an internal tool
566         }
567
568         layer_t min_layer = 0;
569         layer_t max_layer = 0;
570
571         if (_layer_display == Stacked) {
572                 double const c = child_height ();
573
574                 int const mi = _layers - ((bottom - _trackview.y_position()) / c);
575                 if (mi < 0) {
576                         min_layer = 0;
577                 } else {
578                         min_layer = mi;
579                 }
580
581                 int const ma = _layers - ((top - _trackview.y_position()) / c);
582                 if (ma > (int) _layers) {
583                         max_layer = _layers - 1;
584                 } else {
585                         max_layer = ma;
586                 }
587
588         }
589
590         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
591
592                 bool layer_ok = true;
593
594                 if (_layer_display == Stacked) {
595                         layer_t const l = (*i)->region()->layer ();
596                         layer_ok = (min_layer <= l && l <= max_layer);
597                 }
598
599                 if (within) {
600                         if ((*i)->region()->coverage (start, end) == Evoral::OverlapExternal && layer_ok) {
601                                 results.push_back (*i);
602                         }
603                 } else {
604                         if ((*i)->region()->coverage (start, end) != Evoral::OverlapNone && layer_ok) {
605                                 results.push_back (*i);
606                         }
607                 }
608
609         }
610 }
611
612 void
613 StreamView::get_inverted_selectables (Selection& sel, list<Selectable*>& results)
614 {
615         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
616                 if (!sel.regions.contains (*i)) {
617                         results.push_back (*i);
618                 }
619         }
620 }
621
622 /** @return height of a child region view, depending on stacked / overlaid mode */
623 double
624 StreamView::child_height () const
625 {
626         switch (_layer_display) {
627         case Overlaid:
628                 return height;
629         case Stacked:
630                 return height / _layers;
631         case Expanded:
632                 return height / (_layers * 2 + 1);
633         }
634
635         abort(); /* NOTREACHED */
636         return height;
637 }
638
639 void
640 StreamView::update_contents_height ()
641 {
642         const double h = child_height ();
643
644         for (RegionViewList::iterator i = region_views.begin(); i != region_views.end(); ++i) {
645                 switch (_layer_display) {
646                 case Overlaid:
647                         (*i)->set_y (0);
648                         break;
649                 case Stacked:
650                         (*i)->set_y (height - ((*i)->region()->layer() + 1) * h);
651                         break;
652                 case Expanded:
653                         (*i)->set_y (height - ((*i)->region()->layer() + 1) * 2 * h);
654                         break;
655                 }
656
657                 (*i)->set_height (h);
658         }
659
660         for (vector<RecBoxInfo>::iterator i = rec_rects.begin(); i != rec_rects.end(); ++i) {
661                 switch (_layer_display) {
662                 case Overlaid:
663                         i->rectangle->set_y1 (height);
664                         break;
665                 case Stacked:
666                 case Expanded:
667                         /* In stacked displays, the recregion is always at the top */
668                         i->rectangle->set_y0 (0);
669                         i->rectangle->set_y1 (h);
670                         break;
671                 }
672         }
673
674         ContentsHeightChanged (); /* EMIT SIGNAL */
675 }
676
677 void
678 StreamView::set_layer_display (LayerDisplay d)
679 {
680         _layer_display = d;
681
682         if (_layer_display == Overlaid) {
683                 layer_regions ();
684         }
685
686         update_contents_height ();
687         update_coverage_frame ();
688 }
689
690 void
691 StreamView::update_coverage_frame ()
692 {
693         for (RegionViewList::iterator i = region_views.begin (); i != region_views.end (); ++i) {
694                 (*i)->update_coverage_frame (_layer_display);
695         }
696 }
697
698 void
699 StreamView::check_record_layers (boost::shared_ptr<Region> region, samplepos_t to)
700 {
701         if (_new_rec_layer_time < to) {
702                 /* The region being recorded has overlapped the start of a top-layered region, so
703                    `fake' a new visual layer for the recording.  This is only a visual thing for now,
704                    as the proper layering will get sorted out when the recorded region is added to
705                    its playlist.
706                 */
707
708                 /* Stop this happening again */
709                 _new_rec_layer_time = max_samplepos;
710
711                 /* Make space in the view for the new layer */
712                 ++_layers;
713
714                 /* Set the temporary region to the correct layer so that it gets drawn correctly */
715                 region->set_layer (_layers - 1);
716
717                 /* and reset the view */
718                 update_contents_height ();
719         }
720 }
721
722 void
723 StreamView::setup_new_rec_layer_time (boost::shared_ptr<Region> region)
724 {
725         /* If we are in Stacked mode, we may need to (visually) create a new layer to put the
726            recorded region in.  To work out where this needs to happen, find the start of the next
727            top-layered region after the start of the region we are recording and make a note of it.
728         */
729         if (_layer_display == Stacked) {
730                 _new_rec_layer_time = _trackview.track()->playlist()->find_next_top_layer_position (region->start());
731         } else {
732                 _new_rec_layer_time = max_samplepos;
733         }
734 }
735
736 void
737 StreamView::parameter_changed (string const & what)
738 {
739         if (what == "show-region-name") {
740                 for (RegionViewList::iterator i = region_views.begin (); i != region_views.end (); ++i) {
741                         (*i)->update_visibility ();
742                 }
743         }
744 }