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