Add support for PreSonus Faderport2 (2018 model)
[ardour.git] / libs / surfaces / faderport8 / fp8_controls.cc
1 /* Faderport 8 Control Surface
2  * Abstraction of Surface Control Elements.
3  *
4  * Copyright (C) 2017 Robin Gareus <robin@gareus.org>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19  */
20
21 #include "fp8_controls.h"
22
23 using namespace ArdourSurface::FP_NAMESPACE;
24 using namespace ArdourSurface::FP_NAMESPACE::FP8Types;
25
26 bool FP8ButtonInterface::force_change = false;
27
28 #define NEWBUTTON(midi_id, button_id, color)            \
29   do {                                                  \
30   assert (_midimap.end() == _midimap.find (midi_id));   \
31   assert (_ctrlmap.end() == _ctrlmap.find (button_id)); \
32   FP8Button *t = new FP8Button (b, midi_id);            \
33   _midimap[midi_id] = t;                                \
34   _ctrlmap[button_id] = t;                              \
35   } while (0)
36
37
38 #define NEWTYPEBUTTON(TYPE, midi_id, button_id, color)  \
39   do {                                                  \
40   assert (_midimap.end() == _midimap.find (midi_id));   \
41   assert (_ctrlmap.end() == _ctrlmap.find (button_id)); \
42   TYPE *t = new TYPE (b, midi_id);                      \
43   _midimap[midi_id] = t;                                \
44   _ctrlmap[button_id] = t;                              \
45   } while (0)
46
47
48
49 #define NEWSHIFTBUTTON(midi_id, id1, id2, color)        \
50   do {                                                  \
51   assert (_midimap.end() == _midimap.find (midi_id));   \
52   assert (_ctrlmap.end() == _ctrlmap.find (id1));       \
53   assert (_ctrlmap.end() == _ctrlmap.find (id2));       \
54   FP8ShiftSensitiveButton *t =                          \
55     new FP8ShiftSensitiveButton (b, midi_id, color);    \
56   _midimap[midi_id] = t;                                \
57   _ctrlmap[id1] = t->button ();                         \
58   _ctrlmap[id2] = t->button_shift ();                   \
59   } while (0)
60
61
62 FP8Controls::FP8Controls (FP8Base& b)
63         : _fadermode (ModeTrack)
64 #ifdef FADERPORT2
65         , _navmode (NavScroll)
66 #else
67         , _navmode (NavMaster)
68 #endif
69         , _mixmode (MixAll)
70         , _display_timecode (false)
71 {
72         NEWBUTTON (0x56, BtnLoop, false);
73         NEWTYPEBUTTON (FP8RepeatButton, 0x5b, BtnRewind, false);
74         NEWTYPEBUTTON (FP8RepeatButton, 0x5c, BtnFastForward, false);
75         NEWBUTTON (0x5d, BtnStop, false);
76         NEWBUTTON (0x5e, BtnPlay, false);
77         NEWBUTTON (0x5f, BtnRecord, false);
78
79 #ifdef FADERPORT2
80
81         NEWSHIFTBUTTON (0x4a, BtnARead, BtnAOff, true);
82         NEWSHIFTBUTTON (0x4b, BtnAWrite, BtnATrim, true);
83         NEWSHIFTBUTTON (0x4d, BtnATouch, BtnALatch, true);
84
85         NEWSHIFTBUTTON (0x2e, BtnPrev, BtnUndo, false);
86         NEWSHIFTBUTTON (0x2f, BtnNext, BtnRedo, false);
87
88         NEWSHIFTBUTTON (0x2a, BtnPan, BtnFlip, true);  //TODO: Flip Pan knob to fader ...?
89
90         NEWSHIFTBUTTON (0x36, BtnChannel, BtnChanLock, true);
91
92         NEWSHIFTBUTTON (0x38, BtnScroll,  BtnZoom, true);
93
94         NEWSHIFTBUTTON (0x3a, BtnMaster,  BtnF1, false);
95         NEWSHIFTBUTTON (0x3b, BtnClick,   BtnF2, false);
96         NEWSHIFTBUTTON (0x3c, BtnSection, BtnF3, false);
97         NEWSHIFTBUTTON (0x3d, BtnMarker,  BtnF4, false);
98
99         //these buttons do not exist in FP2, but they need to exist in the ctrlmap:
100         NEWBUTTON (0x71, BtnBank, false);
101         NEWBUTTON (0x72, BtnF5, false);
102         NEWBUTTON (0x73, BtnF6, false);
103         NEWBUTTON (0x74, BtnF7, false);
104         NEWBUTTON (0x75, BtnF8, false);
105         NEWBUTTON (0x76, BtnUser1, false);
106         NEWBUTTON (0x77, BtnUser2, false);
107         NEWBUTTON (0x78, BtnUser3, false);
108         NEWBUTTON (0x79, BtnSave, false);
109
110 #else
111         NEWSHIFTBUTTON (0x4a, BtnARead, BtnUser3, true);
112         NEWSHIFTBUTTON (0x4b, BtnAWrite, BtnUser2, true);
113         NEWSHIFTBUTTON (0x4c, BtnATrim, BtnRedo, true);
114         NEWSHIFTBUTTON (0x4d, BtnATouch, BtnUser1, true);
115         NEWSHIFTBUTTON (0x4e, BtnALatch, BtnSave, true);
116         NEWSHIFTBUTTON (0x4f, BtnAOff, BtnUndo, true);
117
118         NEWBUTTON (0x2e, BtnPrev, false);
119         NEWBUTTON (0x2f, BtnNext, false);
120
121         NEWSHIFTBUTTON (0x36, BtnChannel, BtnF1, false);
122         NEWSHIFTBUTTON (0x37, BtnZoom,    BtnF2, false);
123         NEWSHIFTBUTTON (0x38, BtnScroll,  BtnF3, false);
124         NEWSHIFTBUTTON (0x39, BtnBank,    BtnF4, false);
125         NEWSHIFTBUTTON (0x3a, BtnMaster,  BtnF5, false);
126         NEWSHIFTBUTTON (0x3b, BtnClick,   BtnF6, false);
127         NEWSHIFTBUTTON (0x3c, BtnSection, BtnF7, false);
128         NEWSHIFTBUTTON (0x3d, BtnMarker,  BtnF8, false);
129
130         NEWBUTTON (0x2a, BtnPan, false);
131 #endif
132
133         NEWSHIFTBUTTON (0x28, BtnTrack, BtnTimecode, false);
134         NEWBUTTON (0x2b, BtnPlugins, false);
135         NEWBUTTON (0x29, BtnSend, false);
136
137         NEWSHIFTBUTTON (0x00, BtnArm, BtnArmAll, false);
138         NEWBUTTON (0x01, BtnSoloClear, false);
139         NEWBUTTON (0x02, BtnMuteClear, false);
140
141         NEWSHIFTBUTTON (0x03, BtnBypass, BtnBypassAll, true);
142         NEWSHIFTBUTTON (0x04, BtnMacro, BtnOpen, true);
143         NEWSHIFTBUTTON (0x05, BtnLink, BtnLock, true);
144
145         NEWSHIFTBUTTON (0x3e, BtnMAudio, BtnMInputs, true);
146         NEWSHIFTBUTTON (0x3f, BtnMVI, BtnMMIDI, true);
147         NEWSHIFTBUTTON (0x40, BtnMBus, BtnMOutputs, true);
148         NEWSHIFTBUTTON (0x41, BtnMVCA, BtnMFX, true);
149         NEWSHIFTBUTTON (0x42, BtnMAll, BtnMUser, true);
150
151         NEWTYPEBUTTON (FP8ReadOnlyButton, 0x53, BtnEncoder, false);
152         NEWTYPEBUTTON (FP8ReadOnlyButton, 0x20, BtnParam, false);
153         NEWTYPEBUTTON (FP8ReadOnlyButton, 0x66, BtnFootswitch, false);
154
155         /* internal bindings */
156
157 #define BindMethod(ID, CB) \
158         button (ID).released.connect_same_thread (button_connections, boost::bind (&FP8Controls:: CB, this));
159
160         BindMethod (FP8Controls::BtnTimecode, toggle_timecode);
161
162 #define BindNav(BTN, MODE)\
163         button (BTN).released.connect_same_thread (button_connections, boost::bind (&FP8Controls::set_nav_mode, this, MODE))
164
165         BindNav (BtnChannel, NavChannel);
166         BindNav (BtnZoom,    NavZoom);
167         BindNav (BtnScroll,  NavScroll);
168         BindNav (BtnBank,    NavBank);
169         BindNav (BtnMaster,  NavMaster);
170         BindNav (BtnSection, NavSection);
171         BindNav (BtnMarker,  NavMarker);
172 #ifdef FADERPORT2
173         BindNav (BtnPan,     NavPan);
174 #endif
175
176 #define BindFader(BTN, MODE)\
177         button (BTN).released.connect_same_thread (button_connections, boost::bind (&FP8Controls::set_fader_mode, this, MODE))
178
179         BindFader (BtnTrack,   ModeTrack);
180         BindFader (BtnPlugins, ModePlugins);
181         BindFader (BtnSend,    ModeSend);
182 #ifndef FADERPORT2
183         BindFader (BtnPan,     ModePan);
184 #endif
185
186
187 #define BindMix(BTN, MODE)\
188         button (BTN).released.connect_same_thread (button_connections, boost::bind (&FP8Controls::set_mix_mode, this, MODE))
189
190         BindMix (BtnMAudio,   MixAudio);
191         BindMix (BtnMVI,      MixInstrument);
192         BindMix (BtnMBus,     MixBus);
193         BindMix (BtnMVCA,     MixVCA);
194         BindMix (BtnMAll,     MixAll);
195         BindMix (BtnMInputs,  MixInputs);
196         BindMix (BtnMMIDI,    MixMIDI);
197         BindMix (BtnMOutputs, MixOutputs);
198         BindMix (BtnMFX,      MixFX);
199         BindMix (BtnMUser,    MixUser);
200
201         /* create channelstrips */
202         for (uint8_t id = 0; id < N_STRIPS; ++id) {
203                 chanstrip[id] = new FP8Strip (b, id);
204                 _midimap_strip[FP8Strip::midi_ctrl_id (FP8Strip::BtnSolo, id)] = &(chanstrip[id]->solo_button());
205                 _midimap_strip[FP8Strip::midi_ctrl_id (FP8Strip::BtnMute, id)] = &(chanstrip[id]->mute_button());
206                 _midimap_strip[FP8Strip::midi_ctrl_id (FP8Strip::BtnSelect, id)] = &(chanstrip[id]->selrec_button());
207         }
208
209         /* set User button names */
210
211 #define REGISTER_ENUM(ID, NAME) \
212         _user_str_to_enum[#ID] = ID; \
213         _user_enum_to_str[ID]  = #ID; \
214         _user_buttons[ID]      = NAME;
215
216         REGISTER_ENUM (BtnFootswitch, "Footswitch");
217         REGISTER_ENUM (BtnUser1     , "User 1");
218         REGISTER_ENUM (BtnUser2     , "User 2");
219         REGISTER_ENUM (BtnUser3     , "User 3");
220         REGISTER_ENUM (BtnF1        , "F1");
221         REGISTER_ENUM (BtnF2        , "F2");
222         REGISTER_ENUM (BtnF3        , "F3");
223         REGISTER_ENUM (BtnF4        , "F4");
224         REGISTER_ENUM (BtnF5        , "F5");
225         REGISTER_ENUM (BtnF6        , "F6");
226         REGISTER_ENUM (BtnF7        , "F7");
227         REGISTER_ENUM (BtnF8        , "F8");
228 #undef REGISTER_ENUM
229 }
230
231 FP8Controls::~FP8Controls ()
232 {
233         for (MidiButtonMap::const_iterator i = _midimap.begin (); i != _midimap.end (); ++i) {
234                 delete i->second;
235         }
236         for (uint8_t id = 0; id < N_STRIPS; ++id) {
237                 delete chanstrip[id];
238         }
239         _midimap_strip.clear ();
240         _ctrlmap.clear ();
241         _midimap.clear ();
242 }
243
244 bool
245 FP8Controls::button_name_to_enum (std::string const& n, ButtonId& id) const
246 {
247         std::map<std::string, ButtonId>::const_iterator i = _user_str_to_enum.find (n);
248         if (i == _user_str_to_enum.end()) {
249                 return false;
250         }
251         id = i->second;
252         return true;
253 }
254
255 bool
256 FP8Controls::button_enum_to_name (ButtonId id, std::string& n) const
257 {
258         std::map<ButtonId, std::string>::const_iterator i = _user_enum_to_str.find (id);
259         if (i == _user_enum_to_str.end()) {
260                 return false;
261         }
262         n = i->second;
263         return true;
264 }
265
266 void
267 FP8Controls::initialize ()
268 {
269         FP8ButtonInterface::force_change = true;
270         /* set RGB colors */
271         button (BtnUndo).set_color (0x00ff00ff);
272         button (BtnRedo).set_color (0x00ff00ff);
273
274         button (BtnAOff).set_color (0xffffffff);
275         button (BtnATrim).set_color (0x000030ff);
276         button (BtnARead).set_color (0x00ff00ff);
277         button (BtnAWrite).set_color (0xff0000ff);
278         button (BtnATouch).set_color (0xff8800ff);
279         button (BtnALatch).set_color (0xffff00ff);
280
281         button (BtnUser1).set_color (0x0000ffff);
282         button (BtnUser2).set_color (0x0000ffff);
283         button (BtnUser3).set_color (0x0000ffff);
284
285         button (BtnBypass).set_color (0x888888ff);
286         button (BtnBypassAll).set_color (0xffffffff);
287
288         button (BtnMacro).set_color (0x888888ff);
289         button (BtnOpen).set_color (0xffffffff);
290
291         button (BtnLink).set_color (0x888888ff);
292         button (BtnLock).set_color (0xffffffff);
293
294         button (BtnMAudio).set_color (0x0000ffff);
295         button (BtnMVI).set_color (0x0000ffff);
296         button (BtnMBus).set_color (0x0000ffff);
297         button (BtnMVCA).set_color (0x0000ffff);
298         button (BtnMAll).set_color (0x0000ffff);
299
300         button (BtnMInputs).set_color (0x0000ffff);
301         button (BtnMMIDI).set_color (0x0000ffff);
302         button (BtnMOutputs).set_color (0x0000ffff);
303         button (BtnMFX).set_color (0x0000ffff);
304         button (BtnMUser).set_color (0x0000ffff);
305
306 #ifdef FADERPORT2
307         /* encoder mode-switches are orange, to match the Master switch physical color */
308         button (BtnLink).set_color (0x000000ff);
309         button (BtnChannel).set_color (0x0000ffff);
310         button (BtnScroll).set_color (0x0000ffff);
311         button (BtnPan).set_color (0xffffffff);
312 #endif
313
314         for (uint8_t id = 0; id < N_STRIPS; ++id) {
315                 chanstrip[id]->initialize ();
316         }
317
318         /* initally turn all lights off */
319         all_lights_off ();
320
321         /* default modes */
322 #ifdef FADERPORT2
323         button (BtnScroll).set_active (true);
324 #else
325         button (BtnMaster).set_active (true);
326 #endif
327         button (BtnTrack).set_active (true);
328         button (BtnMAll).set_active (true);
329         button (BtnTimecode).set_active (_display_timecode);
330
331         FP8ButtonInterface::force_change = false;
332 }
333
334 void
335 FP8Controls::all_lights_off () const
336 {
337         for (CtrlButtonMap::const_iterator i = _ctrlmap.begin (); i != _ctrlmap.end (); ++i) {
338                 i->second->set_active (false);
339         }
340 }
341
342 FP8ButtonInterface&
343 FP8Controls::button (ButtonId id)
344 {
345         CtrlButtonMap::const_iterator i = _ctrlmap.find (id);
346         if (i == _ctrlmap.end()) {
347                 assert (0);
348                 return _dummy_button;
349         }
350         return *(i->second);
351 }
352
353 FP8Strip&
354 FP8Controls::strip (uint8_t id)
355 {
356         assert (id < N_STRIPS);
357         return *chanstrip[id];
358 }
359
360 /* *****************************************************************************
361  * Delegate MIDI events
362  */
363
364 bool
365 FP8Controls::midi_event (uint8_t id, uint8_t val)
366 {
367         MidiButtonMap::const_iterator i;
368
369         i = _midimap_strip.find (id);
370         if (i != _midimap_strip.end()) {
371                 return i->second->midi_event (val > 0x40);
372         }
373
374         i = _midimap.find (id);
375         if (i != _midimap.end()) {
376                 return i->second->midi_event (val > 0x40);
377         }
378         return false;
379 }
380
381 bool
382 FP8Controls::midi_touch (uint8_t id, uint8_t val)
383 {
384         assert (id < N_STRIPS);
385         return chanstrip[id]->midi_touch (val > 0x40);
386 }
387
388 bool
389 FP8Controls::midi_fader (uint8_t id, unsigned short val)
390 {
391         assert (id < N_STRIPS);
392         return chanstrip[id]->midi_fader ((val >> 4) / 1023.f);
393 }
394
395 /* *****************************************************************************
396  * Internal Model + View for Modes
397  */
398
399 void
400 FP8Controls::set_nav_mode (NavigationMode m)
401 {
402         if (_navmode == m) {
403                 return;
404         }
405         // TODO add special-cases:
406         // - master/monitor (blink when button is held + monitor section present)
407         // - "click" hold -> encoder sets click volume, encoder-press toggle rec-only-metro
408         button (BtnChannel).set_active (m == NavChannel);
409         button (BtnZoom).set_active (m == NavZoom);
410         button (BtnScroll).set_active (m == NavScroll);
411         button (BtnBank).set_active (m == NavBank);
412         button (BtnMaster).set_active (m == NavMaster);
413         button (BtnSection).set_active (m == NavSection);
414         button (BtnMarker).set_active (m == NavMarker);
415 #ifdef FADERPORT2
416         button (BtnPan).set_active (m == NavPan);
417 #endif
418         _navmode = m;
419 }
420
421 void
422 FP8Controls::set_fader_mode (FaderMode m)
423 {
424         if (_fadermode == m) {
425                 if (m == ModePlugins || m == ModeSend) {
426                         /* "Edit Plugins" while editing Plugin-params, returns back
427                          * to plugin selection.
428                          * "Sends" button banks through sends.
429                          */
430                         FaderModeChanged ();
431                 }
432                 return;
433         }
434         // set lights
435         button (BtnTrack).set_active (m == ModeTrack);
436         button (BtnPlugins).set_active (m == ModePlugins);
437         button (BtnSend).set_active (m == ModeSend);
438         button (BtnPan).set_active (m == ModePan);
439         _fadermode = m;
440         FaderModeChanged ();
441 }
442
443 void
444 FP8Controls::set_mix_mode (MixMode m)
445 {
446         if (_mixmode == m) {
447                 if (m == MixUser || m == MixInputs) {
448                         /* always re-assign:
449                          *  - MixUser: depends on selection
450                          *  - MixInputs: depends on rec-arm
451                          */
452                         MixModeChanged ();
453                 }
454                 return;
455         }
456         button (BtnMAudio).set_active (m == MixAudio);
457         button (BtnMVI).set_active (m == MixInstrument);
458         button (BtnMBus).set_active (m == MixBus);
459         button (BtnMVCA).set_active (m == MixVCA);
460         button (BtnMAll).set_active (m == MixAll);
461         button (BtnMInputs).set_active (m == MixInputs);
462         button (BtnMMIDI).set_active (m == MixMIDI);
463         button (BtnMOutputs).set_active (m == MixOutputs);
464         button (BtnMFX).set_active (m == MixFX);
465         button (BtnMUser).set_active (m == MixUser);
466
467         _mixmode = m;
468         MixModeChanged ();
469 }
470
471 void
472 FP8Controls::toggle_timecode ()
473 {
474         _display_timecode = !_display_timecode;
475         button (BtnTimecode).set_active (_display_timecode);
476 }