faderport: fix long press behaviour, and fix ardour-only behaviour with user button...
[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                         , led_on (false)
226                         , flash (false)
227                 {}
228
229                 void set_action (std::string const& action_name, bool on_press, FaderPort::ButtonState = ButtonState (0));
230                 void set_action (boost::function<void()> function, bool on_press, FaderPort::ButtonState = ButtonState (0));
231                 std::string get_action (bool press, FaderPort::ButtonState bs = ButtonState (0));
232
233                 void set_led_state (boost::shared_ptr<MIDI::Port>, int onoff, bool force = false);
234                 void invoke (ButtonState bs, bool press);
235                 bool uses_flash () const { return flash; }
236                 void set_flash (bool yn) { flash = yn; }
237
238                 XMLNode& get_state () const;
239                 int set_state (XMLNode const&);
240
241                 sigc::connection timeout_connection;
242
243           private:
244                 FaderPort& fp;
245                 std::string name;
246                 ButtonID id;
247                 int out;
248                 bool led_on;
249                 bool flash;
250
251                 struct ToDo {
252                         ActionType type;
253                         /* could be a union if boost::function didn't require a
254                          * constructor
255                          */
256                         std::string action_name;
257                         boost::function<void()> function;
258                 };
259
260                 typedef std::map<FaderPort::ButtonState,ToDo> ToDoMap;
261                 ToDoMap on_press;
262                 ToDoMap on_release;
263         };
264
265         typedef std::map<ButtonID,Button> ButtonMap;
266
267         ButtonMap buttons;
268         Button& get_button (ButtonID) const;
269
270         std::set<ButtonID> buttons_down;
271         std::set<ButtonID> consumed;
272
273         bool button_long_press_timeout (ButtonID id);
274         void start_press_timeout (Button&, ButtonID);
275
276         void all_lights_out ();
277         void close ();
278         void start_midi_handling ();
279         void stop_midi_handling ();
280
281         PBD::ScopedConnectionList session_connections;
282         void connect_session_signals ();
283         void notify_record_state_changed ();
284         void notify_transport_state_changed ();
285
286         sigc::connection blink_connection;
287         typedef std::list<ButtonID> Blinkers;
288         Blinkers blinkers;
289         bool blink_state;
290         bool blink ();
291         void start_blinking (ButtonID);
292         void stop_blinking (ButtonID);
293
294         void set_current_route (boost::shared_ptr<ARDOUR::Route>);
295         void drop_current_route ();
296         void use_master ();
297         void use_monitor ();
298         void gui_track_selection_changed (ARDOUR::RouteNotificationListPtr);
299         PBD::ScopedConnection selection_connection;
300         PBD::ScopedConnectionList route_connections;
301
302         void map_route_state ();
303         void map_solo (bool,void*,bool);
304         void map_listen (void*,bool);
305         void map_mute (void*);
306         void map_recenable ();
307         void map_gain ();
308         void map_cut ();
309         void map_auto ();
310         void parameter_changed (std::string);
311
312         /* operations (defined in operations.cc) */
313
314         void read ();
315         void write ();
316
317         void left ();
318         void right ();
319
320         void touch ();
321         void off ();
322
323         void undo ();
324         void redo ();
325         void solo ();
326         void mute ();
327         void rec_enable ();
328
329         void ardour_pan_azimuth (int);
330         void ardour_pan_width (int);
331         void mixbus_pan (int);
332
333         void punch ();
334 };
335
336 }
337
338 #endif /* ardour_surface_faderport_h */