082f0cc77ece93caa3d8ea87e6291e70d706b3f6
[ardour.git] / libs / surfaces / mackie / surface.h
1 #ifndef mackie_surface_h
2 #define mackie_surface_h
3
4 #include <stdint.h>
5
6 #include <sigc++/trackable.h>
7
8 #include "pbd/signals.h"
9 #include "pbd/xml++.h"
10 #include "midi++/types.h"
11
12 #include "ardour/types.h"
13
14 #include "control_protocol/types.h"
15
16 #include "controls.h"
17 #include "types.h"
18 #include "jog_wheel.h"
19
20 namespace MIDI {
21         class Parser;
22 }
23
24 namespace ARDOUR {
25         class Stripable;
26         class Port;
27 }
28
29 class MidiByteArray;
30
31 namespace ArdourSurface {
32
33 class MackieControlProtocol;
34
35 namespace Mackie
36 {
37
38 class MackieButtonHandler;
39 class SurfacePort;
40 class MackieMidiBuilder;
41 class Button;
42 class Meter;
43 class Fader;
44 class Jog;
45 class Pot;
46 class Led;
47
48 class Surface : public PBD::ScopedConnectionList, public sigc::trackable
49 {
50 public:
51         Surface (MackieControlProtocol&, const std::string& name, uint32_t number, surface_type_t stype);
52         virtual ~Surface();
53
54         surface_type_t type() const { return _stype; }
55         uint32_t number() const { return _number; }
56         const std::string& name() { return _name; }
57
58         void connected ();
59
60         bool active() const { return _active; }
61
62         typedef std::vector<Control*> Controls;
63         Controls controls;
64
65         std::map<int,Fader*> faders;
66         std::map<int,Pot*> pots;
67         std::map<int,Button*> buttons; // index is device-DEPENDENT
68         std::map<int,Led*> leds;
69         std::map<int,Meter*> meters;
70         std::map<int,Control*> controls_by_device_independent_id;
71
72         Mackie::JogWheel* jog_wheel() const { return _jog_wheel; }
73         Fader* master_fader() const { return _master_fader; }
74
75         /// The collection of all numbered strips.
76         typedef std::vector<Strip*> Strips;
77         Strips strips;
78
79         uint32_t n_strips (bool with_locked_strips = true) const;
80         Strip* nth_strip (uint32_t n) const;
81
82         bool stripable_is_locked_to_strip (boost::shared_ptr<ARDOUR::Stripable>) const;
83         bool stripable_is_mapped (boost::shared_ptr<ARDOUR::Stripable>) const;
84
85         /// This collection owns the groups
86         typedef std::map<std::string,Group*> Groups;
87         Groups groups;
88
89         SurfacePort& port() const { return *_port; }
90
91         void map_stripables (const std::vector<boost::shared_ptr<ARDOUR::Stripable> >&);
92
93         void update_strip_selection ();
94
95         const MidiByteArray& sysex_hdr() const;
96
97         void periodic (ARDOUR::microseconds_t now_usecs);
98         void redisplay (ARDOUR::microseconds_t now_usecs, bool force);
99         void hui_heartbeat ();
100
101         void handle_midi_pitchbend_message (MIDI::Parser&, MIDI::pitchbend_t, uint32_t channel_id);
102         void handle_midi_controller_message (MIDI::Parser&, MIDI::EventTwoBytes*);
103         void handle_midi_note_on_message (MIDI::Parser&, MIDI::EventTwoBytes*);
104
105         /// Connect the any signal from the parser to handle_midi_any
106         /// unless it's already connected
107         void connect_to_signals ();
108
109         /// write a sysex message
110         void write_sysex (const MidiByteArray& mba);
111         void write_sysex (MIDI::byte msg);
112         /// proxy write for port
113         void write (const MidiByteArray&);
114
115         /// display an indicator of the first switched-in Route. Do nothing by default.
116         void display_bank_start (uint32_t /*current_bank*/);
117
118         /// called from MackieControlProtocol::zero_all to turn things off
119         void zero_all ();
120         void zero_controls ();
121
122         /// turn off leds around the jog wheel. This is for surfaces that use a pot
123         /// pretending to be a jog wheel.
124         void blank_jog_ring ();
125
126         void display_timecode (const std::string & /*timecode*/, const std::string & /*timecode_last*/);
127
128         /// sends MCP "reset" message to surface
129         void reset ();
130
131         void recalibrate_faders ();
132         void toggle_backlight ();
133         void set_touch_sensitivity (int);
134
135         /**
136                 This is used to calculate the clicks per second that define
137                 a transport speed of 1.0 for the jog wheel. 100.0 is 10 clicks
138                 per second, 50.5 is 5 clicks per second.
139         */
140         float scrub_scaling_factor() const;
141
142         /**
143                 The scaling factor function for speed increase and decrease. At
144                 low transport speeds this should return a small value, for high transport
145                 speeds, this should return an exponentially larger value. This provides
146                 high definition control at low speeds and quick speed changes to/from
147                 higher speeds.
148         */
149         float scaled_delta (float delta, float current_speed);
150
151         // display the first 2 chars of the msg in the 2 char display
152         // . is appended to the previous character, so A.B. would
153         // be two characters
154         void show_two_char_display (const std::string & msg, const std::string & dots = "  ");
155         void show_two_char_display (unsigned int value, const std::string & dots = "  ");
156
157         void update_view_mode_display (bool with_helpful_text);
158         void update_flip_mode_display ();
159
160         void subview_mode_changed ();
161
162         MackieControlProtocol& mcp() const { return _mcp; }
163
164         void next_jog_mode ();
165         void set_jog_mode (Mackie::JogWheel::Mode);
166
167         void notify_metering_state_changed();
168         void turn_it_on ();
169
170         void display_message_for (std::string const& msg, uint64_t msecs);
171
172         bool connection_handler (boost::weak_ptr<ARDOUR::Port>, std::string name1, boost::weak_ptr<ARDOUR::Port>, std::string name2, bool);
173
174         void master_monitor_may_have_changed ();
175
176         XMLNode& get_state ();
177         int set_state (const XMLNode&, int version);
178
179         bool get_qcon_flag() { return is_qcon; }
180
181   private:
182         MackieControlProtocol& _mcp;
183         SurfacePort*           _port;
184         surface_type_t         _stype;
185         uint32_t               _number;
186         std::string            _name;
187         bool                   _active;
188         bool                   _connected;
189         Mackie::JogWheel*      _jog_wheel;
190         Fader*                 _master_fader;
191         float                  _last_master_gain_written;
192         PBD::ScopedConnection   master_connection;
193
194         void handle_midi_sysex (MIDI::Parser&, MIDI::byte *, size_t count);
195         MidiByteArray host_connection_query (MidiByteArray& bytes);
196         MidiByteArray host_connection_confirmation (const MidiByteArray& bytes);
197
198         void say_hello ();
199         void init_controls ();
200         void init_strips (uint32_t n);
201         void setup_master ();
202         void master_gain_changed ();
203
204         enum ConnectionState {
205                 InputConnected = 0x1,
206                 OutputConnected = 0x2
207         };
208
209         int connection_state;
210
211         // QCon Flag
212         bool is_qcon = false;
213
214         MidiByteArray display_line (std::string const& msg, int line_num);
215
216   public:
217         /* IP MIDI devices need to keep a handle on this and destroy it */
218         GSource*    input_source;
219 };
220
221 }
222 }
223
224 #endif