MCP: fix handling of vpot presses
[ardour.git] / libs / surfaces / mackie / mackie_control_protocol.h
1 /*
2     Copyright (C) 2006,2007 John Anderson
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 #ifndef ardour_mackie_control_protocol_h
20 #define ardour_mackie_control_protocol_h
21
22 #include <vector>
23 #include <map>
24 #include <list>
25 #include <set>
26
27 #include <sys/time.h>
28 #include <pthread.h>
29 #include <boost/smart_ptr.hpp>
30
31 #include <glibmm/thread.h>
32
33 #include "pbd/abstract_ui.h"
34 #include "midi++/types.h"
35 #include "ardour/types.h"
36 #include "control_protocol/control_protocol.h"
37
38 #include "types.h"
39 #include "midi_byte_array.h"
40 #include "controls.h"
41 #include "jog_wheel.h"
42 #include "timer.h"
43 #include "device_info.h"
44 #include "device_profile.h"
45
46 namespace ARDOUR {
47         class AutomationControl;
48 }
49
50 namespace MIDI {
51         class Port;
52 }
53
54 namespace Mackie {
55         class Surface;
56         class Control;
57         class SurfacePort;
58         class Button;
59 }
60
61 /**
62         This handles the plugin duties, and the midi encoding and decoding,
63         and the signal callbacks, mostly from ARDOUR::Route.
64
65         The model of the control surface is handled by classes in controls.h
66
67         What happens is that each strip on the control surface has
68         a corresponding route in ControlProtocol::route_table. When
69         an incoming midi message is signaled, the correct route
70         is looked up, and the relevant changes made to it.
71
72         For each route currently in route_table, there's a RouteSignal object
73         which encapsulates the signals that indicate that there are changes
74         to be sent to the surface. The signals are handled by this class.
75
76         Calls to signal handlers pass a Route object which is used to look
77         up the relevant Strip in Surface. Then the state is retrieved from
78         the Route and encoded as the correct midi message.
79 */
80
81 struct MackieControlUIRequest : public BaseUI::BaseRequestObject {
82 public:
83         MackieControlUIRequest () {}
84         ~MackieControlUIRequest () {}
85 };
86
87 class MackieControlProtocol 
88         : public ARDOUR::ControlProtocol
89         , public AbstractUI<MackieControlUIRequest>
90 {
91   public:
92         static const int MODIFIER_OPTION;
93         static const int MODIFIER_CONTROL;
94         static const int MODIFIER_SHIFT;
95         static const int MODIFIER_CMDALT;
96
97         enum ViewMode {
98                 Mixer,
99                 Dynamics,
100                 EQ,
101                 Loop,
102                 AudioTracks,
103                 MidiTracks,
104                 Busses,
105                 Sends,
106                 Plugins,
107         };
108
109         enum FlipMode {
110                 Normal, /* fader controls primary, vpot controls secondary */
111                 Mirror, /* fader + vpot control secondary */
112                 Swap,   /* fader controls secondary, vpot controls primary */
113                 Zero,   /* fader controls primary, but doesn't move, vpot controls secondary */
114         };
115         
116         MackieControlProtocol(ARDOUR::Session &);
117         virtual ~MackieControlProtocol();
118
119         static MackieControlProtocol* instance() { return _instance; }
120         
121         const Mackie::DeviceInfo& device_info() const { return _device_info; }
122         Mackie::DeviceProfile& device_profile() { return _device_profile; }
123
124         int set_active (bool yn);
125         void set_device (const std::string&, bool allow_activation = true);
126         void set_profile (const std::string&);
127
128         bool     flip_mode () const { return _flip_mode; }
129         ViewMode view_mode () const { return _view_mode; }
130         bool zoom_mode () const { return _zoom_mode; }
131
132         void set_view_mode (ViewMode);
133         void set_flip_mode (bool);
134
135         XMLNode& get_state ();
136         int set_state (const XMLNode&, int version);
137   
138         static bool probe();
139         
140         typedef std::list<boost::shared_ptr<Mackie::Surface> > Surfaces;
141         Surfaces surfaces;
142
143         std::list<boost::shared_ptr<ARDOUR::Bundle> > bundles ();
144
145         void set_master_on_surface_strip (uint32_t surface, uint32_t strip);
146         void set_monitor_on_surface_strip (uint32_t surface, uint32_t strip);
147         
148         uint32_t n_strips (bool with_locked_strips = true) const;
149         
150         bool has_editor () const { return true; }
151         void* get_gui () const;
152         void tear_down_gui ();
153
154         void handle_button_event (Mackie::Surface&, Mackie::Button& button, Mackie::ButtonState);
155
156         void notify_route_added (ARDOUR::RouteList &);
157         void notify_remote_id_changed();
158
159         /// rebuild the current bank. Called on route added/removed and
160         /// remote id changed.
161         void refresh_current_bank();
162
163         // button-related signals
164         void notify_record_state_changed();
165         void notify_transport_state_changed();
166         void notify_loop_state_changed();
167         // mainly to pick up punch-in and punch-out
168         void notify_parameter_changed(std::string const &);
169         void notify_solo_active_changed(bool);
170
171         /// Turn timecode on and beats off, or vice versa, depending
172         /// on state of _timecode_type
173         void update_timecode_beats_led();
174   
175         /// this is called to generate the midi to send in response to a button press.
176         void update_led(Mackie::Surface&, Mackie::Button & button, Mackie::LedState);
177   
178         void update_global_button (int id, Mackie::LedState);
179         void update_global_led (int id, Mackie::LedState);
180
181         ARDOUR::Session & get_session() { return *session; }
182         framepos_t transport_frame() const;
183
184         int modifier_state() const { return _modifier_state; }
185
186         typedef std::list<boost::shared_ptr<ARDOUR::AutomationControl> > ControlList;
187
188         void add_down_button (ARDOUR::AutomationType, int surface, int strip);
189         void remove_down_button (ARDOUR::AutomationType, int surface, int strip);
190         ControlList down_controls (ARDOUR::AutomationType);
191         
192         void add_down_select_button (int surface, int strip);
193         void remove_down_select_button (int surface, int strip);
194         void select_range ();
195
196         int16_t ipmidi_base() const { return _ipmidi_base; }
197         void    set_ipmidi_base (int16_t);
198         
199   protected:
200         // shut down the surface
201         void close();
202   
203         // This sets up the notifications and sets the
204         // controls to the correct values
205         void update_surfaces();
206         
207         // connects global (not strip) signals from the Session to here
208         // so the surface can be notified of changes from the other UIs.
209         void connect_session_signals();
210         
211         // set all controls to their zero position
212         void zero_all();
213         
214         /**
215            Fetch the set of routes to be considered for control by the
216            surface. Excluding master, hidden and control routes, and inactive routes
217         */
218         typedef std::vector<boost::shared_ptr<ARDOUR::Route> > Sorted;
219         Sorted get_sorted_routes();
220   
221         // bank switching
222         void switch_banks (uint32_t first_remote_id, bool force = false);
223         void prev_track ();
224         void next_track ();
225   
226         // also called from poll_automation to update timecode display
227         void update_timecode_display();
228
229         std::string format_bbt_timecode (ARDOUR::framepos_t now_frame);
230         std::string format_timecode_timecode (ARDOUR::framepos_t now_frame);
231         
232         void do_request (MackieControlUIRequest*);
233         int stop ();
234
235         void thread_init ();
236         void midi_connectivity_established ();
237
238         bool route_is_locked_to_strip (boost::shared_ptr<ARDOUR::Route>) const;
239
240   private:
241
242         struct ButtonHandlers {
243             Mackie::LedState (MackieControlProtocol::*press) (Mackie::Button&);
244             Mackie::LedState (MackieControlProtocol::*release) (Mackie::Button&);
245             
246             ButtonHandlers (Mackie::LedState (MackieControlProtocol::*p) (Mackie::Button&),
247                             Mackie::LedState (MackieControlProtocol::*r) (Mackie::Button&)) 
248             : press (p)
249             , release (r) {}
250         };
251
252         typedef std::map<Mackie::Button::ID,ButtonHandlers> ButtonMap;
253         typedef std::list<GSource*> PortSources;
254
255         static MackieControlProtocol* _instance;
256         
257         Mackie::DeviceInfo       _device_info;
258         Mackie::DeviceProfile    _device_profile;
259         sigc::connection          periodic_connection;
260         uint32_t                 _current_initial_bank;
261         PBD::ScopedConnectionList audio_engine_connections;
262         PBD::ScopedConnectionList session_connections;
263         PBD::ScopedConnectionList port_connections;
264         PBD::ScopedConnectionList route_connections;
265         PBD::ScopedConnectionList gui_connections;
266         bool _transport_previously_rolling;
267         // timer for two quick marker left presses
268         Mackie::Timer            _frm_left_last;
269         // last written timecode string
270         std::string              _timecode_last;
271         // Which timecode are we displaying? BBT or Timecode
272         ARDOUR::AnyTime::Type    _timecode_type;
273         // Bundle to represent our input ports
274         boost::shared_ptr<ARDOUR::Bundle> _input_bundle;
275         // Bundle to represent our output ports
276         boost::shared_ptr<ARDOUR::Bundle> _output_bundle;
277         void*                    _gui;
278         bool                     _zoom_mode;
279         bool                     _scrub_mode;
280         bool                     _flip_mode;
281         ViewMode                 _view_mode;
282         int                      _current_selected_track;
283         int                      _modifier_state;
284         PortSources               port_sources;
285         ButtonMap                 button_map;
286         int16_t                  _ipmidi_base;
287         bool                      needs_ipmidi_restart;
288         
289         void create_surfaces ();
290         bool periodic();
291         void build_gui ();
292         bool midi_input_handler (Glib::IOCondition ioc, MIDI::Port* port);
293         void clear_ports ();
294         void force_special_route_to_strip (boost::shared_ptr<ARDOUR::Route> r, uint32_t surface, uint32_t strip_number);
295         void build_button_map ();
296         void gui_track_selection_changed (ARDOUR::RouteNotificationListPtr);
297         void ipmidi_restart ();
298         
299         /* BUTTON HANDLING */
300
301         typedef std::set<uint32_t> DownButtonList;
302         typedef std::map<ARDOUR::AutomationType,DownButtonList> DownButtonMap;
303         DownButtonMap  _down_buttons;
304         DownButtonList _down_select_buttons; 
305
306         void pull_route_range (DownButtonList&, ARDOUR::RouteList&);
307
308         /* implemented button handlers */
309         Mackie::LedState frm_left_press(Mackie::Button &);
310         Mackie::LedState frm_left_release(Mackie::Button &);
311         Mackie::LedState frm_right_press(Mackie::Button &);
312         Mackie::LedState frm_right_release(Mackie::Button &);
313         Mackie::LedState stop_press(Mackie::Button &);
314         Mackie::LedState stop_release(Mackie::Button &);
315         Mackie::LedState play_press(Mackie::Button &);
316         Mackie::LedState play_release(Mackie::Button &);
317         Mackie::LedState record_press(Mackie::Button &);
318         Mackie::LedState record_release(Mackie::Button &);
319         Mackie::LedState loop_press(Mackie::Button &);
320         Mackie::LedState loop_release(Mackie::Button &);
321         Mackie::LedState punch_in_press(Mackie::Button &);
322         Mackie::LedState punch_in_release(Mackie::Button &);
323         Mackie::LedState punch_out_press(Mackie::Button &);
324         Mackie::LedState punch_out_release(Mackie::Button &);
325         Mackie::LedState home_press(Mackie::Button &);
326         Mackie::LedState home_release(Mackie::Button &);
327         Mackie::LedState end_press(Mackie::Button &);
328         Mackie::LedState end_release(Mackie::Button &);
329         Mackie::LedState rewind_press(Mackie::Button & button);
330         Mackie::LedState rewind_release(Mackie::Button & button);
331         Mackie::LedState ffwd_press(Mackie::Button & button);
332         Mackie::LedState ffwd_release(Mackie::Button & button);
333         Mackie::LedState cursor_up_press (Mackie::Button &);
334         Mackie::LedState cursor_up_release (Mackie::Button &);
335         Mackie::LedState cursor_down_press (Mackie::Button &);
336         Mackie::LedState cursor_down_release (Mackie::Button &);
337         Mackie::LedState cursor_left_press (Mackie::Button &);
338         Mackie::LedState cursor_left_release (Mackie::Button &);
339         Mackie::LedState cursor_right_press (Mackie::Button &);
340         Mackie::LedState cursor_right_release (Mackie::Button &);
341         Mackie::LedState left_press(Mackie::Button &);
342         Mackie::LedState left_release(Mackie::Button &);
343         Mackie::LedState right_press(Mackie::Button &);
344         Mackie::LedState right_release(Mackie::Button &);
345         Mackie::LedState channel_left_press(Mackie::Button &);
346         Mackie::LedState channel_left_release(Mackie::Button &);
347         Mackie::LedState channel_right_press(Mackie::Button &);
348         Mackie::LedState channel_right_release(Mackie::Button &);
349         Mackie::LedState clicking_press(Mackie::Button &);
350         Mackie::LedState clicking_release(Mackie::Button &);
351         Mackie::LedState global_solo_press(Mackie::Button &);
352         Mackie::LedState global_solo_release(Mackie::Button &);
353         Mackie::LedState marker_press(Mackie::Button &);
354         Mackie::LedState marker_release(Mackie::Button &);
355         Mackie::LedState save_press(Mackie::Button &);
356         Mackie::LedState save_release(Mackie::Button &);
357         Mackie::LedState timecode_beats_press(Mackie::Button &);
358         Mackie::LedState timecode_beats_release(Mackie::Button &);
359         Mackie::LedState zoom_press(Mackie::Button &);
360         Mackie::LedState zoom_release(Mackie::Button &);
361         Mackie::LedState scrub_press(Mackie::Button &);
362         Mackie::LedState scrub_release(Mackie::Button &);
363         Mackie::LedState undo_press (Mackie::Button &);
364         Mackie::LedState undo_release (Mackie::Button &);
365         Mackie::LedState redo_press (Mackie::Button &);
366         Mackie::LedState redo_release (Mackie::Button &);
367         Mackie::LedState shift_press (Mackie::Button &);
368         Mackie::LedState shift_release (Mackie::Button &);
369         Mackie::LedState option_press (Mackie::Button &);
370         Mackie::LedState option_release (Mackie::Button &);
371         Mackie::LedState control_press (Mackie::Button &);
372         Mackie::LedState control_release (Mackie::Button &);
373         Mackie::LedState cmd_alt_press (Mackie::Button &);
374         Mackie::LedState cmd_alt_release (Mackie::Button &);
375
376         Mackie::LedState io_press (Mackie::Button &);
377         Mackie::LedState io_release (Mackie::Button &);
378         Mackie::LedState sends_press (Mackie::Button &);
379         Mackie::LedState sends_release (Mackie::Button &);
380         Mackie::LedState pan_press (Mackie::Button &);
381         Mackie::LedState pan_release (Mackie::Button &);
382         Mackie::LedState plugin_press (Mackie::Button &);
383         Mackie::LedState plugin_release (Mackie::Button &);
384         Mackie::LedState eq_press (Mackie::Button &);
385         Mackie::LedState eq_release (Mackie::Button &);
386         Mackie::LedState dyn_press (Mackie::Button &);
387         Mackie::LedState dyn_release (Mackie::Button &);
388         Mackie::LedState flip_press (Mackie::Button &);
389         Mackie::LedState flip_release (Mackie::Button &);
390         Mackie::LedState edit_press (Mackie::Button &);
391         Mackie::LedState edit_release (Mackie::Button &);
392         Mackie::LedState name_value_press (Mackie::Button &);
393         Mackie::LedState name_value_release (Mackie::Button &);
394         Mackie::LedState F1_press (Mackie::Button &);
395         Mackie::LedState F1_release (Mackie::Button &);
396         Mackie::LedState F2_press (Mackie::Button &);
397         Mackie::LedState F2_release (Mackie::Button &);
398         Mackie::LedState F3_press (Mackie::Button &);
399         Mackie::LedState F3_release (Mackie::Button &);
400         Mackie::LedState F4_press (Mackie::Button &);
401         Mackie::LedState F4_release (Mackie::Button &);
402         Mackie::LedState F5_press (Mackie::Button &);
403         Mackie::LedState F5_release (Mackie::Button &);
404         Mackie::LedState F6_press (Mackie::Button &);
405         Mackie::LedState F6_release (Mackie::Button &);
406         Mackie::LedState F7_press (Mackie::Button &);
407         Mackie::LedState F7_release (Mackie::Button &);
408         Mackie::LedState F8_press (Mackie::Button &);
409         Mackie::LedState F8_release (Mackie::Button &);
410         Mackie::LedState F9_press (Mackie::Button &);
411         Mackie::LedState F9_release (Mackie::Button &);
412         Mackie::LedState F10_press (Mackie::Button &);
413         Mackie::LedState F10_release (Mackie::Button &);
414         Mackie::LedState F11_press (Mackie::Button &);
415         Mackie::LedState F11_release (Mackie::Button &);
416         Mackie::LedState F12_press (Mackie::Button &);
417         Mackie::LedState F12_release (Mackie::Button &);
418         Mackie::LedState F13_press (Mackie::Button &);
419         Mackie::LedState F13_release (Mackie::Button &);
420         Mackie::LedState F14_press (Mackie::Button &);
421         Mackie::LedState F14_release (Mackie::Button &);
422         Mackie::LedState F15_press (Mackie::Button &);
423         Mackie::LedState F15_release (Mackie::Button &);
424         Mackie::LedState F16_press (Mackie::Button &);
425         Mackie::LedState F16_release (Mackie::Button &);
426         Mackie::LedState on_press (Mackie::Button &);
427         Mackie::LedState on_release (Mackie::Button &);
428         Mackie::LedState rec_ready_press (Mackie::Button &);
429         Mackie::LedState rec_ready_release (Mackie::Button &);
430         Mackie::LedState touch_press (Mackie::Button &);
431         Mackie::LedState touch_release (Mackie::Button &);
432         Mackie::LedState enter_press (Mackie::Button &);
433         Mackie::LedState enter_release (Mackie::Button &);
434         Mackie::LedState cancel_press (Mackie::Button &);
435         Mackie::LedState cancel_release (Mackie::Button &);
436         Mackie::LedState mixer_press (Mackie::Button &);
437         Mackie::LedState mixer_release (Mackie::Button &);
438         Mackie::LedState user_a_press (Mackie::Button &);
439         Mackie::LedState user_a_release (Mackie::Button &);
440         Mackie::LedState user_b_press (Mackie::Button &);
441         Mackie::LedState user_b_release (Mackie::Button &);
442         Mackie::LedState fader_touch_press (Mackie::Button &);
443         Mackie::LedState fader_touch_release (Mackie::Button &);
444
445         Mackie::LedState snapshot_press (Mackie::Button&);
446         Mackie::LedState snapshot_release (Mackie::Button&);
447         Mackie::LedState read_press (Mackie::Button&);
448         Mackie::LedState read_release (Mackie::Button&);
449         Mackie::LedState write_press (Mackie::Button&);
450         Mackie::LedState write_release (Mackie::Button&);
451         Mackie::LedState fdrgroup_press (Mackie::Button&);
452         Mackie::LedState fdrgroup_release (Mackie::Button&);
453         Mackie::LedState clearsolo_press (Mackie::Button&);
454         Mackie::LedState clearsolo_release (Mackie::Button&);
455         Mackie::LedState track_press (Mackie::Button&);
456         Mackie::LedState track_release (Mackie::Button&);
457         Mackie::LedState send_press (Mackie::Button&);
458         Mackie::LedState send_release (Mackie::Button&);
459         Mackie::LedState miditracks_press (Mackie::Button&);
460         Mackie::LedState miditracks_release (Mackie::Button&);
461         Mackie::LedState inputs_press (Mackie::Button&);
462         Mackie::LedState inputs_release (Mackie::Button&);
463         Mackie::LedState audiotracks_press (Mackie::Button&);
464         Mackie::LedState audiotracks_release (Mackie::Button&);
465         Mackie::LedState audioinstruments_press (Mackie::Button&);
466         Mackie::LedState audioinstruments_release (Mackie::Button&);
467         Mackie::LedState aux_press (Mackie::Button&);
468         Mackie::LedState aux_release (Mackie::Button&);
469         Mackie::LedState busses_press (Mackie::Button&);
470         Mackie::LedState busses_release (Mackie::Button&);
471         Mackie::LedState outputs_press (Mackie::Button&);
472         Mackie::LedState outputs_release (Mackie::Button&);
473         Mackie::LedState user_press (Mackie::Button&);
474         Mackie::LedState user_release (Mackie::Button&);
475         Mackie::LedState trim_press (Mackie::Button&);
476         Mackie::LedState trim_release (Mackie::Button&);
477         Mackie::LedState latch_press (Mackie::Button&);
478         Mackie::LedState latch_release (Mackie::Button&);
479         Mackie::LedState grp_press (Mackie::Button&);
480         Mackie::LedState grp_release (Mackie::Button&);
481         Mackie::LedState nudge_press (Mackie::Button&);
482         Mackie::LedState nudge_release (Mackie::Button&);
483         Mackie::LedState drop_press (Mackie::Button&);
484         Mackie::LedState drop_release (Mackie::Button&);
485         Mackie::LedState replace_press (Mackie::Button&);
486         Mackie::LedState replace_release (Mackie::Button&);
487         Mackie::LedState click_press (Mackie::Button&);
488         Mackie::LedState click_release (Mackie::Button&);
489         Mackie::LedState view_press (Mackie::Button&);
490         Mackie::LedState view_release (Mackie::Button&);
491 };
492
493
494
495 #endif // ardour_mackie_control_protocol_h