push2: update GUI pad display when octave shift is used, or pad map is changed
[ardour.git] / libs / surfaces / push2 / buttons.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 <algorithm>
20
21 #include "ardour/debug.h"
22 #include "ardour/mute_control.h"
23 #include "ardour/session.h"
24 #include "ardour/solo_control.h"
25
26 #include "push2.h"
27
28 using namespace ArdourSurface;
29 using namespace ARDOUR;
30 using namespace PBD;
31 using std::cerr;
32
33 void
34 Push2::build_maps ()
35 {
36         /* Pads */
37
38         Pad* pad;
39
40 #define MAKE_PAD(x,y,nn) \
41         pad = new Pad ((x), (y), (nn)); \
42         nn_pad_map.insert (std::make_pair (pad->extra(), pad)); \
43         coord_pad_map.insert (std::make_pair (pad->coord(), pad));
44
45         MAKE_PAD (0, 1, 93);
46         MAKE_PAD (0, 2, 94);
47         MAKE_PAD (0, 3, 95);
48         MAKE_PAD (0, 4, 96);
49         MAKE_PAD (0, 5, 97);
50         MAKE_PAD (0, 6, 98);
51         MAKE_PAD (0, 7, 90);
52         MAKE_PAD (1, 0, 84);
53         MAKE_PAD (1, 1, 85);
54         MAKE_PAD (1, 2, 86);
55         MAKE_PAD (1, 3, 87);
56         MAKE_PAD (1, 4, 88);
57         MAKE_PAD (1, 5, 89);
58         MAKE_PAD (1, 6, 90);
59         MAKE_PAD (1, 7, 91);
60         MAKE_PAD (2, 0, 76);
61         MAKE_PAD (2, 1, 77);
62         MAKE_PAD (2, 2, 78);
63         MAKE_PAD (2, 3, 79);
64         MAKE_PAD (2, 4, 80);
65         MAKE_PAD (2, 5, 81);
66         MAKE_PAD (2, 6, 82);
67         MAKE_PAD (2, 7, 83);
68         MAKE_PAD (3, 0, 68);
69         MAKE_PAD (3, 1, 69);
70         MAKE_PAD (3, 2, 70);
71         MAKE_PAD (3, 3, 71);
72         MAKE_PAD (3, 4, 72);
73         MAKE_PAD (3, 5, 73);
74         MAKE_PAD (3, 6, 74);
75         MAKE_PAD (3, 7, 75);
76         MAKE_PAD (4, 0, 60);
77         MAKE_PAD (4, 1, 61);
78         MAKE_PAD (4, 2, 62);
79         MAKE_PAD (4, 3, 63);
80         MAKE_PAD (4, 4, 64);
81         MAKE_PAD (4, 5, 65);
82         MAKE_PAD (4, 6, 66);
83         MAKE_PAD (4, 7, 67);
84         MAKE_PAD (5, 0, 52);
85         MAKE_PAD (5, 1, 53);
86         MAKE_PAD (5, 2, 54);
87         MAKE_PAD (5, 3, 55);
88         MAKE_PAD (5, 4, 56);
89         MAKE_PAD (5, 5, 57);
90         MAKE_PAD (5, 6, 58);
91         MAKE_PAD (5, 7, 59);
92         MAKE_PAD (6, 0, 44);
93         MAKE_PAD (6, 1, 45);
94         MAKE_PAD (6, 2, 46);
95         MAKE_PAD (6, 3, 47);
96         MAKE_PAD (6, 4, 48);
97         MAKE_PAD (6, 5, 49);
98         MAKE_PAD (6, 6, 50);
99         MAKE_PAD (6, 7, 51);
100         MAKE_PAD (7, 0, 36);
101         MAKE_PAD (7, 1, 37);
102         MAKE_PAD (7, 2, 38);
103         MAKE_PAD (7, 3, 39);
104         MAKE_PAD (7, 4, 40);
105         MAKE_PAD (7, 5, 41);
106         MAKE_PAD (7, 6, 42);
107         MAKE_PAD (7, 7, 43);
108
109         /* Now color buttons */
110
111         Button *button;
112
113 #define MAKE_COLOR_BUTTON(i,cc) \
114         button = new ColorButton ((i), (cc)); \
115         cc_button_map.insert (std::make_pair (button->controller_number(), button)); \
116         id_button_map.insert (std::make_pair (button->id, button));
117 #define MAKE_COLOR_BUTTON_PRESS(i,cc,p)\
118         button = new ColorButton ((i), (cc), (p)); \
119         cc_button_map.insert (std::make_pair (button->controller_number(), button)); \
120         id_button_map.insert (std::make_pair (button->id, button))
121
122         MAKE_COLOR_BUTTON_PRESS (Upper1, 102, &Push2::button_upper_1);
123         MAKE_COLOR_BUTTON_PRESS (Upper2, 103, &Push2::button_upper_2);
124         MAKE_COLOR_BUTTON_PRESS (Upper3, 104, &Push2::button_upper_3);
125         MAKE_COLOR_BUTTON_PRESS (Upper4, 105, &Push2::button_upper_4);
126         MAKE_COLOR_BUTTON_PRESS (Upper5, 106, &Push2::button_upper_5);
127         MAKE_COLOR_BUTTON_PRESS (Upper6, 107, &Push2::button_upper_6);
128         MAKE_COLOR_BUTTON_PRESS (Upper7, 108, &Push2::button_upper_7);
129         MAKE_COLOR_BUTTON_PRESS (Upper8, 109, &Push2::button_upper_8);
130         MAKE_COLOR_BUTTON_PRESS (Lower1, 20, &Push2::button_lower_1);
131         MAKE_COLOR_BUTTON_PRESS (Lower2, 21, &Push2::button_lower_2);
132         MAKE_COLOR_BUTTON_PRESS (Lower3, 22, &Push2::button_lower_3);
133         MAKE_COLOR_BUTTON_PRESS (Lower4, 23, &Push2::button_lower_4);
134         MAKE_COLOR_BUTTON_PRESS (Lower5, 24, &Push2::button_lower_5);
135         MAKE_COLOR_BUTTON_PRESS (Lower6, 25, &Push2::button_lower_6);
136         MAKE_COLOR_BUTTON_PRESS (Lower7, 26, &Push2::button_lower_7);
137         MAKE_COLOR_BUTTON_PRESS (Lower8, 27, &Push2::button_lower_8);
138         MAKE_COLOR_BUTTON (Master, 28);
139         MAKE_COLOR_BUTTON (Mute, 60);
140         MAKE_COLOR_BUTTON_PRESS (Solo, 61, &Push2::button_solo);
141         MAKE_COLOR_BUTTON_PRESS (Stop, 29, &Push2::button_stop);
142         MAKE_COLOR_BUTTON_PRESS (Fwd32ndT, 43, &Push2::button_fwd32t);
143         MAKE_COLOR_BUTTON_PRESS (Fwd32nd,42 , &Push2::button_fwd32);
144         MAKE_COLOR_BUTTON_PRESS (Fwd16thT, 41, &Push2::button_fwd16t);
145         MAKE_COLOR_BUTTON_PRESS (Fwd16th, 40, &Push2::button_fwd16);
146         MAKE_COLOR_BUTTON_PRESS (Fwd8thT, 39 , &Push2::button_fwd8t);
147         MAKE_COLOR_BUTTON_PRESS (Fwd8th, 38, &Push2::button_fwd8);
148         MAKE_COLOR_BUTTON_PRESS (Fwd4trT, 37, &Push2::button_fwd4t);
149         MAKE_COLOR_BUTTON_PRESS (Fwd4tr, 36, &Push2::button_fwd4);
150         MAKE_COLOR_BUTTON (Automate, 89);
151         MAKE_COLOR_BUTTON_PRESS (RecordEnable, 86, &Push2::button_recenable);
152         MAKE_COLOR_BUTTON_PRESS (Play, 85, &Push2::button_play);
153
154 #define MAKE_WHITE_BUTTON(i,cc)\
155         button = new WhiteButton ((i), (cc)); \
156         cc_button_map.insert (std::make_pair (button->controller_number(), button)); \
157         id_button_map.insert (std::make_pair (button->id, button))
158 #define MAKE_WHITE_BUTTON_PRESS(i,cc,p)\
159         button = new WhiteButton ((i), (cc), (p)); \
160         cc_button_map.insert (std::make_pair (button->controller_number(), button)); \
161         id_button_map.insert (std::make_pair (button->id, button))
162 #define MAKE_WHITE_BUTTON_PRESS_RELEASE(i,cc,p,r)                                \
163         button = new WhiteButton ((i), (cc), (p), (r)); \
164         cc_button_map.insert (std::make_pair (button->controller_number(), button)); \
165         id_button_map.insert (std::make_pair (button->id, button))
166 #define MAKE_WHITE_BUTTON_PRESS_RELEASE_LONG(i,cc,p,r,l)                      \
167         button = new WhiteButton ((i), (cc), (p), (r), (l)); \
168         cc_button_map.insert (std::make_pair (button->controller_number(), button)); \
169         id_button_map.insert (std::make_pair (button->id, button))
170
171         MAKE_WHITE_BUTTON (TapTempo, 3);
172         MAKE_WHITE_BUTTON_PRESS (Metronome, 9, &Push2::button_metronome);
173         MAKE_WHITE_BUTTON (Setup, 30);
174         MAKE_WHITE_BUTTON (User, 59);
175         MAKE_WHITE_BUTTON (Delete, 118);
176         MAKE_WHITE_BUTTON (AddDevice, 52);
177         MAKE_WHITE_BUTTON (Device, 110);
178         MAKE_WHITE_BUTTON (Mix, 112);
179         MAKE_WHITE_BUTTON_PRESS (Undo, 119, &Push2::button_undo);
180         MAKE_WHITE_BUTTON_PRESS (AddTrack, 53, &Push2::button_add_track);
181         MAKE_WHITE_BUTTON_PRESS (Browse, 111, &Push2::button_browse);
182         MAKE_WHITE_BUTTON_PRESS (Clip, 113, &Push2::button_clip);
183         MAKE_WHITE_BUTTON (Convert, 35);
184         MAKE_WHITE_BUTTON (DoubleLoop, 117);
185         MAKE_WHITE_BUTTON (Quantize, 116);
186         MAKE_WHITE_BUTTON (Duplicate, 88);
187         MAKE_WHITE_BUTTON_PRESS (New, 87, &Push2::button_new);
188         MAKE_WHITE_BUTTON_PRESS (FixedLength, 90, &Push2::button_fixed_length);
189         MAKE_WHITE_BUTTON_PRESS (Up, 46, &Push2::button_up);
190         MAKE_WHITE_BUTTON_PRESS (Right, 45, &Push2::button_right);
191         MAKE_WHITE_BUTTON_PRESS (Down, 47, &Push2::button_down);
192         MAKE_WHITE_BUTTON_PRESS (Left, 44, &Push2::button_left);
193         MAKE_WHITE_BUTTON_PRESS (Repeat, 56, &Push2::button_repeat);
194         MAKE_WHITE_BUTTON (Accent, 57);
195         MAKE_WHITE_BUTTON (Scale, 58);
196         MAKE_WHITE_BUTTON (Layout, 31);
197         MAKE_WHITE_BUTTON (Note, 50);
198         MAKE_WHITE_BUTTON (Session, 51);
199         MAKE_WHITE_BUTTON (Layout, 31);
200         MAKE_WHITE_BUTTON_PRESS (OctaveUp, 55, &Push2::button_octave_up);
201         MAKE_WHITE_BUTTON_PRESS (PageRight, 63, &Push2::button_page_right);
202         MAKE_WHITE_BUTTON_PRESS (OctaveDown, 54, &Push2::button_octave_down);
203         MAKE_WHITE_BUTTON_PRESS (PageLeft, 62, &Push2::button_page_left);
204         MAKE_WHITE_BUTTON_PRESS_RELEASE_LONG (Shift, 49, &Push2::button_shift_press, &Push2::button_shift_release, &Push2::button_shift_long_press);
205         MAKE_WHITE_BUTTON_PRESS_RELEASE_LONG (Select, 48, &Push2::button_select_press, &Push2::button_select_release, &Push2::button_select_long_press);
206 }
207
208 void
209 Push2::button_play ()
210 {
211         if (!session) {
212                 return;
213         }
214
215         if (modifier_state & ModShift) {
216                 goto_start (session->transport_rolling());
217                 return;
218         }
219
220         if (session->transport_rolling ()) {
221                 transport_stop ();
222         } else {
223                 transport_play ();
224         }
225 }
226
227 void
228 Push2::button_recenable ()
229 {
230         rec_enable_toggle ();
231 }
232
233 void
234 Push2::button_up ()
235 {
236         scroll_up_1_track ();
237 }
238
239 void
240 Push2::button_down ()
241 {
242         scroll_dn_1_track ();
243 }
244
245 void
246 Push2::button_page_right ()
247 {
248         ScrollTimeline (0.75);
249 }
250
251 void
252 Push2::button_page_left ()
253 {
254         ScrollTimeline (-0.75);
255 }
256
257 void
258 Push2::button_right ()
259 {
260         switch_bank (max (0, bank_start + 8));
261 }
262
263 void
264 Push2::button_left ()
265 {
266         switch_bank (max (0, bank_start - 8));
267 }
268
269 void
270 Push2::button_repeat ()
271 {
272         loop_toggle ();
273 }
274
275 void
276 Push2::button_metronome ()
277 {
278         toggle_click ();
279 }
280
281 void
282 Push2::button_solo ()
283 {
284         cancel_all_solo ();
285 }
286
287 void
288 Push2::button_new ()
289 {
290         access_action ("Editor/start-range-from-playhead");
291
292         id_button_map[New]->set_color (LED::White);
293         id_button_map[New]->set_state (LED::NoTransition);
294         write (id_button_map[New]->state_msg());
295
296         /* blink the button for the other half of this operation */
297
298         id_button_map[FixedLength]->set_color (LED::White);
299         id_button_map[FixedLength]->set_state (LED::Blinking4th);
300         write (id_button_map[FixedLength]->state_msg());
301 }
302
303
304 void
305 Push2::button_fixed_length ()
306 {
307         access_action ("Editor/finish-range-from-playhead");
308
309         /* turn off both buttons for this operation */
310
311         id_button_map[New]->set_color (LED::Black);
312         id_button_map[New]->set_state (LED::NoTransition);
313         write (id_button_map[New]->state_msg());
314         id_button_map[FixedLength]->set_color (LED::Black);
315         id_button_map[FixedLength]->set_state (LED::NoTransition);
316         write (id_button_map[FixedLength]->state_msg());
317 }
318
319 void
320 Push2::button_browse ()
321 {
322         access_action ("Editor/addExistingAudioFiles");
323 }
324
325 void
326 Push2::button_clip ()
327 {
328 }
329
330 void
331 Push2::button_upper (uint32_t n)
332 {
333         if (!stripable[n]) {
334                 return;
335         }
336
337         if (modifier_state & ModShift) {
338                 boost::shared_ptr<AutomationControl> sc = stripable[n]->rec_enable_control ();
339                 if (sc) {
340                         sc->set_value (!sc->get_value(), PBD::Controllable::UseGroup);
341                 }
342         } else {
343                 boost::shared_ptr<SoloControl> sc = stripable[n]->solo_control ();
344                 if (sc) {
345                         sc->set_value (!sc->self_soloed(), PBD::Controllable::UseGroup);
346                 }
347         }
348 }
349
350 void
351 Push2::button_lower (uint32_t n)
352 {
353         if (!stripable[n]) {
354                 return;
355         }
356
357         if (modifier_state & ModSelect) {
358                 stripable[n]->presentation_info().set_selected (!stripable[n]->presentation_info().selected());
359         } else {
360                 boost::shared_ptr<MuteControl> mc = stripable[n]->mute_control ();
361
362                 if (mc) {
363                         mc->set_value (!mc->muted_by_self(), PBD::Controllable::UseGroup);
364                 }
365         }
366 }
367
368 void
369 Push2::button_undo ()
370 {
371         if (modifier_state & ModShift) {
372                 ControlProtocol::Redo ();
373         } else {
374                 ControlProtocol::Undo ();
375         }
376 }
377
378 void
379 Push2::button_fwd32t ()
380 {
381         const int n = (modifier_state & ModShift) ? 8 : 0;
382         goto_nth_marker (0+n);
383 }
384
385 void
386 Push2::button_fwd32 ()
387 {
388         const int n = (modifier_state & ModShift) ? 8 : 0;
389         goto_nth_marker (1+n);
390 }
391
392 void
393 Push2::button_fwd16t ()
394 {
395         const int n = (modifier_state & ModShift) ? 8 : 0;
396         goto_nth_marker (2+n);
397 }
398
399 void
400 Push2::button_fwd16 ()
401 {
402         const int n = (modifier_state & ModShift) ? 8 : 0;
403         goto_nth_marker (3+n);
404 }
405
406 void
407 Push2::button_fwd8t ()
408 {
409         const int n = (modifier_state & ModShift) ? 8 : 0;
410         goto_nth_marker (4+n);
411 }
412
413 void
414 Push2::button_fwd8 ()
415 {
416         const int n = (modifier_state & ModShift) ? 8 : 0;
417         goto_nth_marker (5+n);
418 }
419
420 void
421 Push2::button_fwd4t ()
422 {
423         const int n = (modifier_state & ModShift) ? 8 : 0;
424         goto_nth_marker (6+n);
425 }
426
427 void
428 Push2::button_fwd4 ()
429 {
430         const int n = (modifier_state & ModShift) ? 8 : 0;
431         goto_nth_marker (7+n);
432 }
433
434 void
435 Push2::button_add_track ()
436 {
437         access_action ("Main/AddTrackBus");
438 }
439
440 void
441 Push2::button_stop ()
442 {
443         /* close current window */
444         access_action ("Main/close-current-dialog");
445 }
446
447 void
448 Push2::button_shift_press ()
449 {
450         start_shift ();
451 }
452
453 void
454 Push2::button_shift_release ()
455 {
456         end_shift ();
457 }
458
459 void
460 Push2::button_shift_long_press ()
461 {
462         access_action ("Main/close-current-dialog");
463 }
464
465 void
466 Push2::button_select_press ()
467 {
468         start_select ();
469 }
470
471 void
472 Push2::button_select_release ()
473 {
474         if (!(modifier_state & ModSelect)) {
475                 /* somebody else used us as a modifier */
476                 return;
477         }
478
479         end_select ();
480
481         int selected = -1;
482
483         for (int n = 0; n < 8; ++n) {
484                 if (stripable[n]) {
485                         if (stripable[n]->presentation_info().selected()) {
486                                         selected = n;
487                                         break;
488                         }
489                 }
490         }
491
492         if (selected < 0) {
493
494                 /* no visible track selected, select first (if any) */
495
496                 if (stripable[0]) {
497                         stripable[0]->presentation_info().set_selected (true);
498                 }
499
500         } else {
501
502                 if (modifier_state & ModShift) {
503                         std::cerr << "select prev\n";
504                         /* select prev */
505
506                         if (selected == 0) {
507                                 /* current selected is leftmost ... cancel selection,
508                                    switch banks by one, and select leftmost
509                                 */
510                                 if (bank_start != 0) {
511                                         stripable[selected]->presentation_info().set_selected (false);
512                                         switch_bank (bank_start-1);
513                                         if (stripable[0]) {
514                                                 stripable[0]->presentation_info().set_selected (true);
515                                         }
516                                 }
517                         } else {
518                                 /* select prev, if any */
519                                 int n = selected - 1;
520                                 while (n >= 0 && !stripable[n]) {
521                                         --n;
522                                 }
523                                 if (n >= 0) {
524                                         stripable[selected]->presentation_info().set_selected (false);
525                                         stripable[n]->presentation_info().set_selected (true);
526                                 }
527                         }
528
529                 } else {
530
531                         std::cerr << "select next\n";
532                         /* select next */
533
534                         if (selected == 7) {
535                                 /* current selected is rightmost ... cancel selection,
536                                    switch banks by one, and select righmost
537                                 */
538                                 stripable[selected]->presentation_info().set_selected (false);
539                                 switch_bank (bank_start+1);
540                                 if (stripable[7]) {
541                                         stripable[7]->presentation_info().set_selected (true);
542                                 }
543                         } else {
544                                 /* select next, if any */
545                                 int n = selected + 1;
546                                 while (n < 8 && !stripable[n]) {
547                                         ++n;
548                                 }
549
550                                 if (n != 8) {
551                                         stripable[selected]->presentation_info().set_selected (false);
552                                         stripable[n]->presentation_info().set_selected (true);
553                                 }
554                         }
555                 }
556         }
557 }
558
559 void
560 Push2::button_select_long_press ()
561 {
562         access_action ("Main/Escape");
563 }
564
565 bool
566 Push2::button_long_press_timeout (ButtonID id)
567 {
568         if (buttons_down.find (id) != buttons_down.end()) {
569                 DEBUG_TRACE (DEBUG::Push2, string_compose ("long press timeout for %1, invoking method\n", id));
570                 Button* button = id_button_map[id];
571                 (this->*button->long_press_method) ();
572         } else {
573                 DEBUG_TRACE (DEBUG::Push2, string_compose ("long press timeout for %1, expired/cancelled\n", id));
574                 /* release happened and somehow we were not cancelled */
575         }
576
577         /* whichever button this was, we've used it ... don't invoke the
578            release action.
579         */
580         consumed.insert (id);
581
582         return false; /* don't get called again */
583 }
584
585 void
586 Push2::start_press_timeout (Button& button, ButtonID id)
587 {
588         Glib::RefPtr<Glib::TimeoutSource> timeout = Glib::TimeoutSource::create (500); // milliseconds
589         button.timeout_connection = timeout->connect (sigc::bind (sigc::mem_fun (*this, &Push2::button_long_press_timeout), id));
590         timeout->attach (main_loop()->get_context());
591 }
592
593 void
594 Push2::button_octave_down ()
595 {
596         int os = (max (-4, octave_shift - 1));
597         if (os != octave_shift) {
598                 octave_shift = os;
599                 build_pad_table ();
600         }
601 }
602
603 void
604 Push2::button_octave_up ()
605 {
606         int os = (min (4, octave_shift + 1));
607         if (os != octave_shift) {
608                 octave_shift = os;
609                 build_pad_table ();
610         }
611 }