faderport: stop trying to cache LED state (fixes various bugs); blink mute for muted...
[ardour.git] / libs / surfaces / faderport / faderport.h
1 /*
2     Copyright (C) 2006 Paul Davis
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
20 #ifndef ardour_surface_faderport_h
21 #define ardour_surface_faderport_h
22
23 #include <list>
24 #include <map>
25 #include <set>
26 #include <glibmm/threads.h>
27
28 #define ABSTRACT_UI_EXPORTS
29 #include "pbd/abstract_ui.h"
30
31 #include "ardour/types.h"
32
33 #include "control_protocol/control_protocol.h"
34
35 namespace PBD {
36         class Controllable;
37         class ControllableDescriptor;
38 }
39
40 #include <midi++/types.h>
41
42 //#include "pbd/signals.h"
43
44
45 //#include "midi_byte_array.h"
46 #include "types.h"
47
48 #include "glibmm/main.h"
49
50 namespace MIDI {
51         class Parser;
52         class Port;
53 }
54
55
56 namespace ARDOUR {
57         class AsyncMIDIPort;
58         class Bundle;
59         class Port;
60         class Session;
61         class MidiPort;
62 }
63
64
65 class MIDIControllable;
66 class MIDIFunction;
67 class MIDIAction;
68
69 namespace ArdourSurface {
70
71 struct FaderPortRequest : public BaseUI::BaseRequestObject {
72 public:
73         FaderPortRequest () {}
74         ~FaderPortRequest () {}
75 };
76
77 class FaderPort : public ARDOUR::ControlProtocol, public AbstractUI<FaderPortRequest> {
78   public:
79         FaderPort (ARDOUR::Session&);
80         virtual ~FaderPort();
81
82         int set_active (bool yn);
83
84         /* we probe for a device when our ports are connected. Before that,
85            there's no way to know if the device exists or not.
86          */
87         static bool probe() { return true; }
88
89         XMLNode& get_state ();
90         int set_state (const XMLNode&, int version);
91
92         bool has_editor () const { return true; }
93         void* get_gui () const;
94         void  tear_down_gui ();
95
96         /* Note: because the FaderPort speaks an inherently duplex protocol,
97            we do not implement get/set_feedback() since this aspect of
98            support for the protocol is not optional.
99         */
100
101         void do_request (FaderPortRequest*);
102         int stop ();
103
104         void thread_init ();
105
106         PBD::Signal0<void> ConnectionChange;
107
108         boost::shared_ptr<ARDOUR::Port> input_port();
109         boost::shared_ptr<ARDOUR::Port> output_port();
110
111         /* In a feat of engineering brilliance, the Presonus Faderport sends
112          * one button identifier when the button is pressed/released, but
113          * responds to another button identifier as a command to light the LED
114          * corresponding to the button. These ID's define what is sent
115          * for press/release; a separate data structure contains information
116          * on what to send to turn the LED on/off.
117          *
118          * One can only conclude that Presonus just didn't want to fix this
119          * issue because it contradicts their own documentation and is more or
120          * less the first thing you discover when programming the device.
121          */
122
123         enum ButtonID {
124                 Mute = 18,
125                 Solo = 17,
126                 Rec = 16,
127                 Left = 19,
128                 Bank = 20,
129                 Right = 21,
130                 Output = 22,
131                 FP_Read = 10,
132                 FP_Write = 9,
133                 FP_Touch = 8,
134                 FP_Off = 23,
135                 Mix = 11,
136                 Proj = 12,
137                 Trns = 13,
138                 Undo = 14,
139                 Shift = 2,
140                 Punch = 1,
141                 User = 0,
142                 Loop = 15,
143                 Rewind = 3,
144                 Ffwd = 4,
145                 Stop = 5,
146                 Play = 6,
147                 RecEnable = 7,
148                 FaderTouch = 127,
149         };
150
151         enum ButtonState {
152                 ShiftDown = 0x1,
153                 RewindDown = 0x2,
154                 StopDown = 0x4,
155                 UserDown = 0x8,
156                 LongPress = 0x10
157         };
158
159         void set_action (ButtonID, std::string const& action_name, bool on_press, FaderPort::ButtonState = ButtonState (0));
160         std::string get_action (ButtonID, bool on_press, FaderPort::ButtonState = ButtonState (0));
161
162         std::list<boost::shared_ptr<ARDOUR::Bundle> > bundles ();
163         
164   private:
165         boost::shared_ptr<ARDOUR::Route> _current_route;
166         boost::weak_ptr<ARDOUR::Route> pre_master_route;
167         boost::weak_ptr<ARDOUR::Route> pre_monitor_route;
168
169         boost::shared_ptr<ARDOUR::AsyncMIDIPort> _input_port;
170         boost::shared_ptr<ARDOUR::AsyncMIDIPort> _output_port;
171
172         // Bundle to represent our input ports
173         boost::shared_ptr<ARDOUR::Bundle> _input_bundle;
174         // Bundle to represent our output ports
175         boost::shared_ptr<ARDOUR::Bundle> _output_bundle;
176
177         PBD::ScopedConnectionList midi_connections;
178
179         bool midi_input_handler (Glib::IOCondition ioc, boost::shared_ptr<ARDOUR::AsyncMIDIPort> port);
180
181         mutable void *gui;
182         void build_gui ();
183
184         bool connection_handler (boost::weak_ptr<ARDOUR::Port>, std::string name1, boost::weak_ptr<ARDOUR::Port>, std::string name2, bool yn);
185         PBD::ScopedConnection port_connection;
186
187         enum ConnectionState {
188                 InputConnected = 0x1,
189                 OutputConnected = 0x2
190         };
191
192         int connection_state;
193         void connected ();
194         bool _device_active;
195         int fader_msb;
196         int fader_lsb;
197         bool fader_is_touched;
198
199         ARDOUR::microseconds_t last_encoder_time;
200         int last_good_encoder_delta;
201         int last_encoder_delta, last_last_encoder_delta;
202
203         void sysex_handler (MIDI::Parser &p, MIDI::byte *, size_t);
204         void button_handler (MIDI::Parser &, MIDI::EventTwoBytes* tb);
205         void encoder_handler (MIDI::Parser &, MIDI::pitchbend_t pb);
206         void fader_handler (MIDI::Parser &, MIDI::EventTwoBytes* tb);
207
208         ButtonState button_state;
209
210         friend class Button;
211
212         class Button {
213           public:
214
215                 enum ActionType {
216                         NamedAction,
217                         InternalFunction,
218                 };
219
220                 Button (FaderPort& f, std::string const& str, ButtonID i, int o)
221                         : fp (f)
222                         , name (str)
223                         , id (i)
224                         , out (o)
225                         , flash (false)
226                 {}
227
228                 void set_action (std::string const& action_name, bool on_press, FaderPort::ButtonState = ButtonState (0));
229                 void set_action (boost::function<void()> function, bool on_press, FaderPort::ButtonState = ButtonState (0));
230                 std::string get_action (bool press, FaderPort::ButtonState bs = ButtonState (0));
231
232                 void set_led_state (boost::shared_ptr<MIDI::Port>, bool onoff);
233                 void invoke (ButtonState bs, bool press);
234                 bool uses_flash () const { return flash; }
235                 void set_flash (bool yn) { flash = yn; }
236
237                 XMLNode& get_state () const;
238                 int set_state (XMLNode const&);
239
240                 sigc::connection timeout_connection;
241
242           private:
243                 FaderPort& fp;
244                 std::string name;
245                 ButtonID id;
246                 int out;
247                 bool flash;
248
249                 struct ToDo {
250                         ActionType type;
251                         /* could be a union if boost::function didn't require a
252                          * constructor
253                          */
254                         std::string action_name;
255                         boost::function<void()> function;
256                 };
257
258                 typedef std::map<FaderPort::ButtonState,ToDo> ToDoMap;
259                 ToDoMap on_press;
260                 ToDoMap on_release;
261         };
262
263         typedef std::map<ButtonID,Button> ButtonMap;
264
265         ButtonMap buttons;
266         Button& get_button (ButtonID) const;
267
268         std::set<ButtonID> buttons_down;
269         std::set<ButtonID> consumed;
270
271         bool button_long_press_timeout (ButtonID id);
272         void start_press_timeout (Button&, ButtonID);
273
274         void all_lights_out ();
275         void close ();
276         void start_midi_handling ();
277         void stop_midi_handling ();
278
279         PBD::ScopedConnectionList session_connections;
280         void connect_session_signals ();
281         void map_recenable_state ();
282         void map_transport_state ();
283
284         sigc::connection blink_connection;
285         typedef std::list<ButtonID> Blinkers;
286         Blinkers blinkers;
287         bool blink_state;
288         bool blink ();
289         void start_blinking (ButtonID);
290         void stop_blinking (ButtonID);
291
292         void set_current_route (boost::shared_ptr<ARDOUR::Route>);
293         void drop_current_route ();
294         void use_master ();
295         void use_monitor ();
296         void gui_track_selection_changed (ARDOUR::RouteNotificationListPtr);
297         PBD::ScopedConnection selection_connection;
298         PBD::ScopedConnectionList route_connections;
299
300         void map_route_state ();
301         void map_solo (bool,void*,bool);
302         void map_listen (void*,bool);
303         void map_mute (void*);
304         void map_recenable ();
305         void map_gain ();
306         void map_cut ();
307         void map_auto ();
308         void parameter_changed (std::string);
309
310         /* operations (defined in operations.cc) */
311
312         void read ();
313         void write ();
314
315         void left ();
316         void right ();
317
318         void touch ();
319         void off ();
320
321         void undo ();
322         void redo ();
323         void solo ();
324         void mute ();
325         void rec_enable ();
326
327         void ardour_pan_azimuth (int);
328         void ardour_pan_width (int);
329         void mixbus_pan (int);
330
331         void punch ();
332 };
333
334 }
335
336 #endif /* ardour_surface_faderport_h */