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