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