push2: numerous changes, including long press actions
[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, 56);
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 (OctaveUp, 55);
201         MAKE_WHITE_BUTTON (PageRight, 63);
202         MAKE_WHITE_BUTTON (OctaveDown, 54);
203         MAKE_WHITE_BUTTON (PageLeft, 62);
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_right ()
247 {
248         ScrollTimeline (0.75);
249 }
250
251 void
252 Push2::button_left ()
253 {
254         ScrollTimeline (-0.75);
255 }
256
257 void
258 Push2::button_repeat ()
259 {
260         loop_toggle ();
261 }
262
263 void
264 Push2::button_metronome ()
265 {
266         toggle_click ();
267 }
268
269 void
270 Push2::button_solo ()
271 {
272         cancel_all_solo ();
273 }
274
275 void
276 Push2::button_new ()
277 {
278         access_action ("Editor/start-range-from-playhead");
279
280         id_button_map[New]->set_color (LED::White);
281         id_button_map[New]->set_state (LED::NoTransition);
282         write (id_button_map[New]->state_msg());
283
284         /* blink the button for the other half of this operation */
285
286         id_button_map[FixedLength]->set_color (LED::White);
287         id_button_map[FixedLength]->set_state (LED::Blinking4th);
288         write (id_button_map[FixedLength]->state_msg());
289 }
290
291
292 void
293 Push2::button_fixed_length ()
294 {
295         access_action ("Editor/finish-range-from-playhead");
296
297         /* turn off both buttons for this operation */
298
299         id_button_map[New]->set_color (LED::Black);
300         id_button_map[New]->set_state (LED::NoTransition);
301         write (id_button_map[New]->state_msg());
302         id_button_map[FixedLength]->set_color (LED::Black);
303         id_button_map[FixedLength]->set_state (LED::NoTransition);
304         write (id_button_map[FixedLength]->state_msg());
305 }
306
307 void
308 Push2::button_browse ()
309 {
310         if (modifier_state & ModShift) {
311                 access_action ("Editor/addExistingAudioFiles");
312         } else {
313                 switch_bank (max (0, bank_start - 8));
314         }
315 }
316
317 void
318 Push2::button_clip ()
319 {
320         switch_bank (max (0, bank_start + 8));
321 }
322
323 void
324 Push2::button_upper (uint32_t n)
325 {
326         if (!stripable[n]) {
327                 return;
328         }
329
330         if (modifier_state & ModShift) {
331                 boost::shared_ptr<AutomationControl> sc = stripable[n]->rec_enable_control ();
332                 if (sc) {
333                         sc->set_value (!sc->get_value(), PBD::Controllable::UseGroup);
334                 }
335         } else {
336                 boost::shared_ptr<SoloControl> sc = stripable[n]->solo_control ();
337                 if (sc) {
338                         sc->set_value (!sc->get_value(), PBD::Controllable::UseGroup);
339                 }
340         }
341 }
342
343 void
344 Push2::button_lower (uint32_t n)
345 {
346         if (!stripable[n]) {
347                 return;
348         }
349
350         if (modifier_state & ModSelect) {
351                 stripable[n]->presentation_info().set_selected (!stripable[n]->presentation_info().selected());
352         } else {
353                 cerr << "select not set\n";
354                 boost::shared_ptr<MuteControl> mc = stripable[n]->mute_control ();
355
356                 if (mc) {
357                         mc->set_value (!mc->get_value(), PBD::Controllable::UseGroup);
358                 }
359         }
360 }
361
362 void
363 Push2::button_undo ()
364 {
365         if (modifier_state & ModShift) {
366                 ControlProtocol::Redo ();
367         } else {
368                 ControlProtocol::Undo ();
369         }
370 }
371
372 void
373 Push2::button_fwd32t ()
374 {
375         const int n = (modifier_state & ModShift) ? 8 : 0;
376         goto_nth_marker (0+n);
377 }
378
379 void
380 Push2::button_fwd32 ()
381 {
382         const int n = (modifier_state & ModShift) ? 8 : 0;
383         goto_nth_marker (1+n);
384 }
385
386 void
387 Push2::button_fwd16t ()
388 {
389         const int n = (modifier_state & ModShift) ? 8 : 0;
390         goto_nth_marker (2+n);
391 }
392
393 void
394 Push2::button_fwd16 ()
395 {
396         const int n = (modifier_state & ModShift) ? 8 : 0;
397         goto_nth_marker (3+n);
398 }
399
400 void
401 Push2::button_fwd8t ()
402 {
403         const int n = (modifier_state & ModShift) ? 8 : 0;
404         goto_nth_marker (4+n);
405 }
406
407 void
408 Push2::button_fwd8 ()
409 {
410         const int n = (modifier_state & ModShift) ? 8 : 0;
411         goto_nth_marker (5+n);
412 }
413
414 void
415 Push2::button_fwd4t ()
416 {
417         const int n = (modifier_state & ModShift) ? 8 : 0;
418         goto_nth_marker (6+n);
419 }
420
421 void
422 Push2::button_fwd4 ()
423 {
424         const int n = (modifier_state & ModShift) ? 8 : 0;
425         goto_nth_marker (7+n);
426 }
427
428 void
429 Push2::button_add_track ()
430 {
431         access_action ("Main/AddTrackBus");
432 }
433
434 void
435 Push2::button_stop ()
436 {
437         /* close current window */
438         access_action ("Main/close-current-dialog");
439 }
440
441 void
442 Push2::button_shift_press ()
443 {
444         start_shift ();
445 }
446
447 void
448 Push2::button_shift_release ()
449 {
450         end_shift ();
451 }
452
453 void
454 Push2::button_shift_long_press ()
455 {
456         access_action ("Main/close-current-dialog");
457 }
458
459 void
460 Push2::button_select_press ()
461 {
462         start_select ();
463 }
464
465 void
466 Push2::button_select_release ()
467 {
468         if (!(modifier_state & ModSelect)) {
469                 /* somebody else used us as a modifier */
470                 return;
471         }
472
473         end_select ();
474
475         int selected = -1;
476
477         for (int n = 0; n < 8; ++n) {
478                 if (stripable[n]) {
479                         if (stripable[n]->presentation_info().selected()) {
480                                         selected = n;
481                                         break;
482                         }
483                 }
484         }
485
486         if (selected < 0) {
487
488                 /* no visible track selected, select first (if any) */
489
490                 if (stripable[0]) {
491                         stripable[0]->presentation_info().set_selected (true);
492                 }
493
494         } else {
495
496                 if (modifier_state & ModShift) {
497                         std::cerr << "select prev\n";
498                         /* select prev */
499
500                         if (selected == 0) {
501                                 /* current selected is leftmost ... cancel selection,
502                                    switch banks by one, and select leftmost
503                                 */
504                                 if (bank_start != 0) {
505                                         stripable[selected]->presentation_info().set_selected (false);
506                                         switch_bank (bank_start-1);
507                                         if (stripable[0]) {
508                                                 stripable[0]->presentation_info().set_selected (true);
509                                         }
510                                 }
511                         } else {
512                                 /* select prev, if any */
513                                 int n = selected - 1;
514                                 while (n >= 0 && !stripable[n]) {
515                                         --n;
516                                 }
517                                 if (n >= 0) {
518                                         stripable[selected]->presentation_info().set_selected (false);
519                                         stripable[n]->presentation_info().set_selected (true);
520                                 }
521                         }
522
523                 } else {
524
525                         std::cerr << "select next\n";
526                         /* select next */
527
528                         if (selected == 7) {
529                                 /* current selected is rightmost ... cancel selection,
530                                    switch banks by one, and select righmost
531                                 */
532                                 stripable[selected]->presentation_info().set_selected (false);
533                                 switch_bank (bank_start+1);
534                                 if (stripable[7]) {
535                                         stripable[7]->presentation_info().set_selected (true);
536                                 }
537                         } else {
538                                 /* select next, if any */
539                                 int n = selected + 1;
540                                 while (n < 8 && !stripable[n]) {
541                                         ++n;
542                                 }
543
544                                 if (n != 8) {
545                                         stripable[selected]->presentation_info().set_selected (false);
546                                         stripable[n]->presentation_info().set_selected (true);
547                                 }
548                         }
549                 }
550         }
551 }
552
553 void
554 Push2::button_select_long_press ()
555 {
556         access_action ("Main/Escape");
557 }
558
559 bool
560 Push2::button_long_press_timeout (ButtonID id)
561 {
562         if (buttons_down.find (id) != buttons_down.end()) {
563                 DEBUG_TRACE (DEBUG::Push2, string_compose ("long press timeout for %1, invoking method\n", id));
564                 Button* button = id_button_map[id];
565                 (this->*button->long_press_method) ();
566         } else {
567                 DEBUG_TRACE (DEBUG::Push2, string_compose ("long press timeout for %1, expired/cancelled\n", id));
568                 /* release happened and somehow we were not cancelled */
569         }
570
571         /* whichever button this was, we've used it ... don't invoke the
572            release action.
573         */
574         consumed.insert (id);
575
576         return false; /* don't get called again */
577 }
578
579 void
580 Push2::start_press_timeout (Button& button, ButtonID id)
581 {
582         Glib::RefPtr<Glib::TimeoutSource> timeout = Glib::TimeoutSource::create (500); // milliseconds
583         button.timeout_connection = timeout->connect (sigc::bind (sigc::mem_fun (*this, &Push2::button_long_press_timeout), id));
584         timeout->attach (main_loop()->get_context());
585 }