MacOS [sic] required changes for Rect in Push 2 code
[ardour.git] / libs / surfaces / push2 / track_mix.cc
1 /*
2   Copyright (C) 2016 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 <cairomm/region.h>
20 #include <pangomm/layout.h>
21
22 #include "pbd/compose.h"
23 #include "pbd/convert.h"
24 #include "pbd/debug.h"
25 #include "pbd/failed_constructor.h"
26 #include "pbd/file_utils.h"
27 #include "pbd/i18n.h"
28 #include "pbd/search_path.h"
29 #include "pbd/enumwriter.h"
30
31 #include "midi++/parser.h"
32 #include "timecode/time.h"
33 #include "timecode/bbt_time.h"
34
35 #include "ardour/async_midi_port.h"
36 #include "ardour/audioengine.h"
37 #include "ardour/debug.h"
38 #include "ardour/dsp_filter.h"
39 #include "ardour/filesystem_paths.h"
40 #include "ardour/midiport_manager.h"
41 #include "ardour/midi_track.h"
42 #include "ardour/midi_port.h"
43 #include "ardour/monitor_control.h"
44 #include "ardour/meter.h"
45 #include "ardour/session.h"
46 #include "ardour/solo_isolate_control.h"
47 #include "ardour/solo_safe_control.h"
48 #include "ardour/tempo.h"
49
50 #include "gtkmm2ext/gui_thread.h"
51 #include "gtkmm2ext/rgb_macros.h"
52
53 #include "canvas/box.h"
54 #include "canvas/line.h"
55 #include "canvas/meter.h"
56 #include "canvas/rectangle.h"
57 #include "canvas/text.h"
58 #include "canvas/types.h"
59
60 #include "canvas.h"
61 #include "knob.h"
62 #include "level_meter.h"
63 #include "menu.h"
64 #include "push2.h"
65 #include "track_mix.h"
66 #include "utils.h"
67
68 #ifdef __APPLE__
69 #define Rect ArdourCanvas::Rect
70 #endif
71
72 using namespace ARDOUR;
73 using namespace std;
74 using namespace PBD;
75 using namespace Glib;
76 using namespace ArdourSurface;
77 using namespace ArdourCanvas;
78
79 TrackMixLayout::TrackMixLayout (Push2& p, Session & s, std::string const & name)
80         : Push2Layout (p, s, name)
81 {
82         Pango::FontDescription fd ("Sans 10");
83
84         bg = new Rectangle (this);
85         bg->set (Rect (0, 0, display_width(), display_height()));
86         bg->set_fill_color (p2.get_color (Push2::DarkBackground));
87
88         upper_line = new Line (this);
89         upper_line->set (Duple (0, 22.5), Duple (display_width(), 22.5));
90         upper_line->set_outline_color (p2.get_color (Push2::LightBackground));
91
92         for (int n = 0; n < 8; ++n) {
93                 Text* t;
94
95                 if (n < 4) {
96                         t = new Text (this);
97                         t->set_font_description (fd);
98                         t->set_color (p2.get_color (Push2::ParameterName));
99                         t->set_position ( Duple (10 + (n*Push2Canvas::inter_button_spacing()), 2));
100                         upper_text.push_back (t);
101                 }
102
103                 t = new Text (this);
104                 t->set_font_description (fd);
105                 t->set_color (p2.get_color (Push2::ParameterName));
106                 t->set_position (Duple (10 + (n*Push2Canvas::inter_button_spacing()), 140));
107
108                 lower_text.push_back (t);
109
110                 switch (n) {
111                 case 0:
112                         upper_text[n]->set (_("Track Volume"));
113                         lower_text[n]->set (_("Mute"));
114                         break;
115                 case 1:
116                         upper_text[n]->set (_("Track Pan"));
117                         lower_text[n]->set (_("Solo"));
118                         break;
119                 case 2:
120                         upper_text[n]->set (_("Track Width"));
121                         lower_text[n]->set (_("Rec-enable"));
122                         break;
123                 case 3:
124                         upper_text[n]->set (_("Track Trim"));
125                         lower_text[n]->set (_("In"));
126                         break;
127                 case 4:
128                         lower_text[n]->set (_("Disk"));
129                         break;
130                 case 5:
131                         lower_text[n]->set (_("Solo Iso"));
132                         break;
133                 case 6:
134                         lower_text[n]->set (_("Solo Lock"));
135                         break;
136                 case 7:
137                         lower_text[n]->set (_(""));
138                         break;
139                 }
140
141                 knobs[n] = new Push2Knob (p2, this);
142                 knobs[n]->set_position (Duple (60 + (Push2Canvas::inter_button_spacing()*n), 95));
143                 knobs[n]->set_radius (25);
144         }
145
146         name_text = new Text (this);
147         name_text->set_font_description (fd);
148         name_text->set_position (Duple (10 + (4*Push2Canvas::inter_button_spacing()), 2));
149
150         meter = new LevelMeter (p2, this, 300, ArdourCanvas::Meter::Horizontal);
151         meter->set_position (Duple (10 + (4 * Push2Canvas::inter_button_spacing()), 30));
152
153         Pango::FontDescription fd2 ("Sans 18");
154         bbt_text = new Text (this);
155         bbt_text->set_font_description (fd2);
156         bbt_text->set_color (p2.get_color (Push2::LightBackground));
157         bbt_text->set_position (Duple (10 + (4 * Push2Canvas::inter_button_spacing()), 60));
158
159         minsec_text = new Text (this);
160         minsec_text->set_font_description (fd2);
161         minsec_text->set_color (p2.get_color (Push2::LightBackground));
162         minsec_text->set_position (Duple (10 + (4 * Push2Canvas::inter_button_spacing()), 90));
163
164         ControlProtocol::StripableSelectionChanged.connect (selection_connection, invalidator (*this), boost::bind (&TrackMixLayout::selection_changed, this), &p2);
165 }
166
167 TrackMixLayout::~TrackMixLayout ()
168 {
169         for (int n = 0; n < 8; ++n) {
170                 delete knobs[n];
171         }
172 }
173
174 void
175 TrackMixLayout::selection_changed ()
176 {
177         boost::shared_ptr<Stripable> s = ControlProtocol::first_selected_stripable();
178
179         if (s) {
180                 set_stripable (s);
181         }
182 }
183
184 void
185 TrackMixLayout::show ()
186 {
187         Push2::ButtonID lower_buttons[] = { Push2::Lower1, Push2::Lower2, Push2::Lower3, Push2::Lower4,
188                                             Push2::Lower5, Push2::Lower6, Push2::Lower7, Push2::Lower8 };
189
190         for (size_t n = 0; n < sizeof (lower_buttons) / sizeof (lower_buttons[0]); ++n) {
191                 Push2::Button* b = p2.button_by_id (lower_buttons[n]);
192                 b->set_color (Push2::LED::DarkGray);
193                 b->set_state (Push2::LED::OneShot24th);
194                 p2.write (b->state_msg());
195         }
196
197         show_state ();
198
199         Container::show ();
200 }
201
202 void
203 TrackMixLayout::hide ()
204 {
205
206 }
207
208 void
209 TrackMixLayout::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
210 {
211         Container::render (area, context);
212 }
213
214 void
215 TrackMixLayout::button_upper (uint32_t n)
216 {
217 }
218
219 void
220 TrackMixLayout::button_lower (uint32_t n)
221 {
222         if (!stripable) {
223                 return;
224         }
225
226         MonitorChoice mc;
227
228         switch (n) {
229         case 0:
230                 if (stripable->mute_control()) {
231                         stripable->mute_control()->set_value (!stripable->mute_control()->get_value(), PBD::Controllable::UseGroup);
232                 }
233                 break;
234         case 1:
235                 if (stripable->solo_control()) {
236                         stripable->solo_control()->set_value (!stripable->solo_control()->get_value(), PBD::Controllable::UseGroup);
237                 }
238                 break;
239         case 2:
240                 if (stripable->rec_enable_control()) {
241                         stripable->rec_enable_control()->set_value (!stripable->rec_enable_control()->get_value(), PBD::Controllable::UseGroup);
242                 }
243                 break;
244         case 3:
245                 if (stripable->monitor_control()) {
246                         mc = stripable->monitoring_control()->monitoring_choice();
247                         switch (mc) {
248                         case MonitorInput:
249                                 stripable->monitoring_control()->set_value (MonitorAuto, PBD::Controllable::UseGroup);
250                                 break;
251                         default:
252                                 stripable->monitoring_control()->set_value (MonitorInput, PBD::Controllable::UseGroup);
253                                 break;
254                         }
255                 }
256                 break;
257         case 4:
258                 mc = stripable->monitoring_control()->monitoring_choice();
259                 switch (mc) {
260                 case MonitorDisk:
261                         stripable->monitoring_control()->set_value (MonitorAuto, PBD::Controllable::UseGroup);
262                         break;
263                 default:
264                         stripable->monitoring_control()->set_value (MonitorDisk, PBD::Controllable::UseGroup);
265                         break;
266                 }
267                 break;
268         case 5:
269                 if (stripable->solo_isolate_control()) {
270                         stripable->solo_isolate_control()->set_value (!stripable->solo_isolate_control()->get_value(), PBD::Controllable::UseGroup);
271                 }
272                 break;
273         case 6:
274                 if (stripable->solo_safe_control()) {
275                         stripable->solo_safe_control()->set_value (!stripable->solo_safe_control()->get_value(), PBD::Controllable::UseGroup);
276                 }
277                 break;
278         case 7:
279                 /* nothing here */
280                 break;
281         }
282 }
283
284 void
285 TrackMixLayout::button_left ()
286 {
287         p2.access_action ("Editor/select-prev-route");
288 }
289
290 void
291 TrackMixLayout::button_right ()
292 {
293         p2.access_action ("Editor/select-next-route");
294 }
295
296 void
297 TrackMixLayout::simple_control_change (boost::shared_ptr<AutomationControl> ac, Push2::ButtonID bid)
298 {
299         if (!ac || !parent()) {
300                 return;
301         }
302
303         Push2::Button* b = p2.button_by_id (bid);
304
305         if (!b) {
306                 return;
307         }
308
309         if (ac->get_value()) {
310                 b->set_color (selection_color);
311         } else {
312                 b->set_color (Push2::LED::DarkGray);
313         }
314         b->set_state (Push2::LED::OneShot24th);
315         p2.write (b->state_msg());
316 }
317
318 void
319 TrackMixLayout::solo_mute_change ()
320 {
321         if (!stripable) {
322                 return;
323         }
324
325         Push2::Button* b = p2.button_by_id (Push2::Lower2);
326
327         if (b) {
328                 boost::shared_ptr<SoloControl> sc = stripable->solo_control();
329
330                 if (sc) {
331                         if (sc->soloed_by_self_or_masters()) {
332                                 b->set_color (selection_color);
333                                 b->set_state (Push2::LED::OneShot24th);
334                         } else if (sc->soloed_by_others_upstream() || sc->soloed_by_others_downstream()) {
335                                 b->set_color (selection_color);
336                                 b->set_state (Push2::LED::Blinking8th);
337                         } else {
338                                 b->set_color (Push2::LED::DarkGray);
339                                 b->set_state (Push2::LED::OneShot24th);
340                         }
341                 } else {
342                         b->set_color (Push2::LED::DarkGray);
343                         b->set_state (Push2::LED::OneShot24th);
344                 }
345
346                 p2.write (b->state_msg());
347         }
348
349         b = p2.button_by_id (Push2::Lower1);
350
351         if (b) {
352                 boost::shared_ptr<MuteControl> mc = stripable->mute_control();
353
354                 if (mc) {
355                         if (mc->muted_by_self_or_masters()) {
356                                 b->set_color (selection_color);
357                                 b->set_state (Push2::LED::OneShot24th);
358                         } else if (mc->muted_by_others_soloing()) {
359                                 b->set_color (selection_color);
360                                 b->set_state (Push2::LED::Blinking8th);
361                         } else {
362                                 b->set_color (Push2::LED::DarkGray);
363                                 b->set_state (Push2::LED::OneShot24th);
364                         }
365
366                 } else {
367                         b->set_color (Push2::LED::DarkGray);
368                         b->set_state (Push2::LED::OneShot24th);
369                 }
370
371                 p2.write (b->state_msg());
372         }
373
374 }
375
376 void
377 TrackMixLayout::rec_enable_change ()
378 {
379         if (!stripable) {
380                 return;
381         }
382
383         simple_control_change (stripable->rec_enable_control(), Push2::Lower3);
384 }
385
386 void
387 TrackMixLayout::solo_iso_change ()
388 {
389         if (!stripable) {
390                 return;
391         }
392
393         simple_control_change (stripable->solo_isolate_control(), Push2::Lower6);
394 }
395 void
396 TrackMixLayout::solo_safe_change ()
397 {
398         if (!stripable) {
399                 return;
400         }
401
402         simple_control_change (stripable->solo_safe_control(), Push2::Lower7);
403 }
404
405 void
406 TrackMixLayout::monitoring_change ()
407 {
408         if (!stripable) {
409                 return;
410         }
411
412         if (!stripable->monitoring_control()) {
413                 return;
414         }
415
416         Push2::Button* b1 = p2.button_by_id (Push2::Lower4);
417         Push2::Button* b2 = p2.button_by_id (Push2::Lower5);
418         uint8_t b1_color;
419         uint8_t b2_color;
420
421         MonitorChoice mc = stripable->monitoring_control()->monitoring_choice ();
422
423         switch (mc) {
424         case MonitorAuto:
425                 b1_color = Push2::LED::DarkGray;
426                 b2_color = Push2::LED::DarkGray;
427                 break;
428         case MonitorInput:
429                 b1_color = selection_color;
430                 b2_color = Push2::LED::DarkGray;
431                 break;
432         case MonitorDisk:
433                 b1_color = Push2::LED::DarkGray;
434                 b2_color = selection_color;
435                 break;
436         case MonitorCue:
437                 b1_color = selection_color;
438                 b2_color = selection_color;
439                 break;
440         }
441
442         b1->set_color (b1_color);
443         b1->set_state (Push2::LED::OneShot24th);
444         p2.write (b1->state_msg());
445
446         b2->set_color (b2_color);
447         b2->set_state (Push2::LED::OneShot24th);
448         p2.write (b2->state_msg());
449 }
450
451 void
452 TrackMixLayout::show_state ()
453 {
454         if (!parent()) {
455                 return;
456         }
457
458         if (stripable) {
459                 name_changed ();
460                 color_changed ();
461                 solo_mute_change ();
462                 rec_enable_change ();
463                 solo_iso_change ();
464                 solo_safe_change ();
465                 monitoring_change ();
466
467                 meter->set_meter (stripable->peak_meter ().get());
468         } else {
469                 meter->set_meter (0);
470         }
471 }
472
473 void
474 TrackMixLayout::set_stripable (boost::shared_ptr<Stripable> s)
475 {
476         stripable_connections.drop_connections ();
477
478         stripable = s;
479
480         if (stripable) {
481
482                 stripable->DropReferences.connect (stripable_connections, invalidator (*this), boost::bind (&TrackMixLayout::drop_stripable, this), &p2);
483
484                 stripable->PropertyChanged.connect (stripable_connections, invalidator (*this), boost::bind (&TrackMixLayout::stripable_property_change, this, _1), &p2);
485                 stripable->presentation_info().PropertyChanged.connect (stripable_connections, invalidator (*this), boost::bind (&TrackMixLayout::stripable_property_change, this, _1), &p2);
486
487                 stripable->solo_control()->Changed.connect (stripable_connections, invalidator (*this), boost::bind (&TrackMixLayout::solo_mute_change, this), &p2);
488                 stripable->mute_control()->Changed.connect (stripable_connections, invalidator (*this), boost::bind (&TrackMixLayout::solo_mute_change, this), &p2);
489                 stripable->solo_isolate_control()->Changed.connect (stripable_connections, invalidator (*this), boost::bind (&TrackMixLayout::solo_iso_change, this), &p2);
490                 stripable->solo_safe_control()->Changed.connect (stripable_connections, invalidator (*this), boost::bind (&TrackMixLayout::solo_safe_change, this), &p2);
491
492                 if (stripable->rec_enable_control()) {
493                         stripable->rec_enable_control()->Changed.connect (stripable_connections, invalidator (*this), boost::bind (&TrackMixLayout::rec_enable_change, this), &p2);
494                 }
495
496                 if (stripable->monitoring_control()) {
497                         stripable->monitoring_control()->Changed.connect (stripable_connections, invalidator (*this), boost::bind (&TrackMixLayout::monitoring_change, this), &p2);
498                 }
499
500                 knobs[0]->set_controllable (stripable->gain_control());
501                 knobs[1]->set_controllable (stripable->pan_azimuth_control());
502                 knobs[1]->add_flag (Push2Knob::ArcToZero);
503                 knobs[2]->set_controllable (stripable->pan_width_control());
504                 knobs[3]->set_controllable (stripable->trim_control());
505                 knobs[3]->add_flag (Push2Knob::ArcToZero);
506                 knobs[4]->set_controllable (boost::shared_ptr<AutomationControl>());
507                 knobs[5]->set_controllable (boost::shared_ptr<AutomationControl>());
508                 knobs[6]->set_controllable (boost::shared_ptr<AutomationControl>());
509                 knobs[7]->set_controllable (boost::shared_ptr<AutomationControl>());
510         }
511
512         show_state ();
513 }
514
515 void
516 TrackMixLayout::drop_stripable ()
517 {
518         stripable_connections.drop_connections ();
519         stripable.reset ();
520 }
521
522 void
523 TrackMixLayout::name_changed ()
524 {
525         if (stripable) {
526
527                 name_text->set (stripable->name());
528
529                 /* right justify */
530
531                 Duple pos;
532                 pos.y = name_text->position().y;
533                 pos.x = display_width() - 10 - name_text->width();
534
535                 name_text->set_position (pos);
536         }
537 }
538
539 void
540 TrackMixLayout::color_changed ()
541 {
542         if (!parent()) {
543                 return;
544         }
545
546         Color rgba = stripable->presentation_info().color();
547         selection_color = p2.get_color_index (rgba);
548
549         name_text->set_color (rgba);
550
551         for (int n = 0; n < 8; ++n) {
552                 knobs[n]->set_text_color (rgba);
553                 knobs[n]->set_arc_start_color (rgba);
554                 knobs[n]->set_arc_end_color (rgba);
555         }
556 }
557
558 void
559 TrackMixLayout::stripable_property_change (PropertyChange const& what_changed)
560 {
561         if (what_changed.contains (Properties::color)) {
562                 color_changed ();
563         }
564         if (what_changed.contains (Properties::name)) {
565                 name_changed ();
566         }
567 }
568
569 void
570 TrackMixLayout::strip_vpot (int n, int delta)
571 {
572         boost::shared_ptr<Controllable> ac = knobs[n]->controllable();
573
574         if (ac) {
575                 ac->set_value (ac->get_value() + ((2.0/64.0) * delta), PBD::Controllable::UseGroup);
576         }
577 }
578
579 void
580 TrackMixLayout::strip_vpot_touch (int n, bool touching)
581 {
582         boost::shared_ptr<AutomationControl> ac = knobs[n]->controllable();
583         if (ac) {
584                 if (touching) {
585                         ac->start_touch (session.audible_frame());
586                 } else {
587                         ac->stop_touch (true, session.audible_frame());
588                 }
589         }
590 }
591
592 void
593 TrackMixLayout::update_meters ()
594 {
595         if (!stripable) {
596                 return;
597         }
598
599         meter->update_meters ();
600 }
601
602 void
603 TrackMixLayout::update_clocks ()
604 {
605         framepos_t pos = session.audible_frame();
606         bool negative = false;
607
608         if (pos < 0) {
609                 pos = -pos;
610                 negative = true;
611         }
612
613         char buf[16];
614         Timecode::BBT_Time BBT = session.tempo_map().bbt_at_frame (pos);
615
616 #define BBT_BAR_CHAR "|"
617
618         if (negative) {
619                 snprintf (buf, sizeof (buf), "-%03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
620                           BBT.bars, BBT.beats, BBT.ticks);
621         } else {
622                 snprintf (buf, sizeof (buf), " %03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
623                           BBT.bars, BBT.beats, BBT.ticks);
624         }
625
626         bbt_text->set (buf);
627
628         framecnt_t left;
629         int hrs;
630         int mins;
631         int secs;
632         int millisecs;
633
634         const double frame_rate = session.frame_rate ();
635
636         left = pos;
637         hrs = (int) floor (left / (frame_rate * 60.0f * 60.0f));
638         left -= (framecnt_t) floor (hrs * frame_rate * 60.0f * 60.0f);
639         mins = (int) floor (left / (frame_rate * 60.0f));
640         left -= (framecnt_t) floor (mins * frame_rate * 60.0f);
641         secs = (int) floor (left / (float) frame_rate);
642         left -= (framecnt_t) floor ((double)(secs * frame_rate));
643         millisecs = floor (left * 1000.0 / (float) frame_rate);
644
645         if (negative) {
646                 snprintf (buf, sizeof (buf), "-%02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
647         } else {
648                 snprintf (buf, sizeof (buf), " %02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
649         }
650
651         minsec_text->set (buf);
652 }