Use Stripable::Sorter in libs
[ardour.git] / libs / surfaces / faderport8 / faderport8.h
1 /*
2  * Copyright (C) 2017 Robin Gareus <robin@gareus.org>
3  * Copyright (C) 2015 Paul Davis
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18  */
19
20 #ifndef ardour_surface_faderport8_h
21 #define ardour_surface_faderport8_h
22
23 // allow to undo "mute clear", "solo clear"
24 // eventually this should use some libardour mixer history/undo
25 #define FP8_MUTESOLO_UNDO
26
27 #include <list>
28 #include <map>
29 #include <glibmm/threads.h>
30
31 #define ABSTRACT_UI_EXPORTS
32 #include "pbd/abstract_ui.h"
33 #include "pbd/properties.h"
34
35 #include "ardour/types.h"
36 #include "ardour/async_midi_port.h"
37 #include "ardour/midi_port.h"
38
39 #include "control_protocol/control_protocol.h"
40
41 #include "fp8_base.h"
42 #include "fp8_controls.h"
43
44 namespace MIDI {
45         class Parser;
46 }
47
48 namespace ARDOUR {
49         class Bundle;
50         class Session;
51         class Processor;
52 }
53
54 namespace ArdourSurface {
55
56 struct FaderPort8Request : public BaseUI::BaseRequestObject
57 {
58         public:
59                 FaderPort8Request () {}
60                 ~FaderPort8Request () {}
61 };
62
63 class FaderPort8 : public FP8Base, public ARDOUR::ControlProtocol, public AbstractUI<FaderPort8Request>
64 {
65 public:
66         FaderPort8 (ARDOUR::Session&);
67         virtual ~FaderPort8();
68
69         int set_active (bool yn);
70
71         /* we probe for a device when our ports are connected. Before that,
72          * there's no way to know if the device exists or not.
73          */
74         static bool  probe() { return true; }
75         static void* request_factory (uint32_t);
76
77         XMLNode& get_state ();
78         int set_state (const XMLNode&, int version);
79
80         /* configuration GUI */
81         bool  has_editor () const { return true; }
82         void* get_gui () const;
83         void  tear_down_gui ();
84         PBD::Signal0<void> ConnectionChange;
85
86         void set_button_action (FP8Controls::ButtonId, bool, std::string const&);
87         std::string get_button_action (FP8Controls::ButtonId, bool);
88         FP8Controls const& control () const { return  _ctrls; }
89
90         int stop ();
91         void do_request (FaderPort8Request*);
92         void thread_init ();
93
94         boost::shared_ptr<ARDOUR::Port> input_port() const { return _input_port; }
95         boost::shared_ptr<ARDOUR::Port> output_port() const { return _output_port; }
96         std::list<boost::shared_ptr<ARDOUR::Bundle> > bundles ();
97
98         size_t tx_midi (std::vector<uint8_t> const&) const;
99
100 private:
101         void close ();
102
103         void start_midi_handling ();
104         void stop_midi_handling ();
105
106         /* I/O Ports */
107         PBD::ScopedConnectionList port_connections;
108         boost::shared_ptr<ARDOUR::AsyncMIDIPort> _input_port;
109         boost::shared_ptr<ARDOUR::AsyncMIDIPort> _output_port;
110         boost::shared_ptr<ARDOUR::Bundle>        _input_bundle;
111         boost::shared_ptr<ARDOUR::Bundle>        _output_bundle;
112
113         bool midi_input_handler (Glib::IOCondition ioc, boost::weak_ptr<ARDOUR::AsyncMIDIPort> port);
114
115         bool connection_handler (std::string name1, std::string name2);
116         void engine_reset ();
117
118         enum ConnectionState {
119                 InputConnected = 0x1,
120                 OutputConnected = 0x2
121         };
122
123         void connected ();
124         void disconnected ();
125         int  _connection_state;
126         bool _device_active;
127
128         /* MIDI input message handling */
129         void sysex_handler (MIDI::Parser &p, MIDI::byte *, size_t);
130         void polypressure_handler (MIDI::Parser &, MIDI::EventTwoBytes* tb);
131         void pitchbend_handler (MIDI::Parser &, uint8_t chan, MIDI::pitchbend_t pb);
132         void controller_handler (MIDI::Parser &, MIDI::EventTwoBytes* tb);
133         void note_on_handler (MIDI::Parser &, MIDI::EventTwoBytes* tb);
134         void note_off_handler (MIDI::Parser &, MIDI::EventTwoBytes* tb);
135         PBD::ScopedConnectionList midi_connections;
136
137         /* ***************************************************************************
138          * Control Elements
139          */
140         FP8Controls _ctrls;
141         void notify_stripable_added_or_removed ();
142         void notify_fader_mode_changed ();
143         void filter_stripables (ARDOUR::StripableList& strips) const;
144         void assign_stripables (bool select_only = false);
145         void set_periodic_display_mode (FP8Strip::DisplayMode);
146
147         void assign_strips (bool reset_bank);
148         void bank (bool down, bool page);
149         void move_selected_into_view ();
150         void select_prev_next (bool next);
151
152         void assign_sends ();
153         void spill_plugins ();
154         void assign_processor_ctrls ();
155         void build_well_known_processor_ctrls (boost::shared_ptr<ARDOUR::Stripable>, bool);
156         void select_plugin (int num);
157
158         void bank_param (bool down, bool page);
159         /* bank offsets */
160         int _channel_off;
161         int _plugin_off;
162         int _parameter_off;
163
164         /* plugin + send mode stripable
165          *
166          * This is used when parameters of one strip are assigned to
167          * individual FP8Strip controls (Edit Send, Edit Plugins).
168          *
169          * When there's one stripable per FP8Strip, FP8Strip itself keeps
170          * track of the object lifetime and these are NULL.
171          */
172         PBD::ScopedConnectionList processor_connections;
173
174         PBD::ScopedConnectionList assigned_stripable_connections;
175         typedef std::map<boost::shared_ptr<ARDOUR::Stripable>, uint8_t> StripAssignmentMap;
176         StripAssignmentMap _assigned_strips;
177
178         void drop_ctrl_connections ();
179
180         void select_strip (boost::weak_ptr<ARDOUR::Stripable>);
181
182         void notify_pi_property_changed (const PBD::PropertyChange&);
183         void notify_stripable_property_changed (boost::weak_ptr<ARDOUR::Stripable>, const PBD::PropertyChange&);
184         void stripable_selection_changed ();
185
186         PBD::ScopedConnection selection_connection;
187         PBD::ScopedConnectionList automation_state_connections;
188         PBD::ScopedConnectionList modechange_connections;
189         /* **************************************************************************/
190         struct ProcessorCtrl {
191                 ProcessorCtrl (std::string const &n, boost::shared_ptr<ARDOUR::AutomationControl> c)
192                  : name (n)
193                  , ac (c)
194                 {}
195                 std::string name;
196                 boost::shared_ptr<ARDOUR::AutomationControl> ac;
197         };
198         std::list <ProcessorCtrl> _proc_params;
199         int _showing_well_known;
200         /* **************************************************************************/
201
202         /* periodic updates, parameter poll */
203         sigc::connection _periodic_connection;
204         bool periodic ();
205         std::string _timecode;
206         std::string const& timecode () const { return _timecode; }
207
208         /* sync button blink -- the FP's blink mode does not work */
209         sigc::connection _blink_connection;
210         bool _blink_onoff;
211         bool blink_it ();
212
213         /* shift key */
214         sigc::connection _shift_connection;
215         bool _shift_lock;
216         int _shift_pressed;
217         bool shift_timeout () { _shift_lock = true; return false; }
218         bool shift_mod () const { return _shift_lock || (_shift_pressed > 0); }
219
220         /* GUI */
221         void build_gui ();
222         mutable void *gui;
223
224         /* setup callbacks & actions */
225         void connect_session_signals ();
226         void setup_actions ();
227         void send_session_state ();
228
229         /* callbacks */
230         PBD::ScopedConnectionList session_connections;
231         void notify_parameter_changed (std::string);
232         void notify_record_state_changed ();
233         void notify_transport_state_changed ();
234         void notify_loop_state_changed ();
235         void notify_snap_change ();
236         void notify_session_dirty_changed ();
237         void notify_history_changed ();
238         void notify_solo_changed ();
239         void notify_mute_changed ();
240         void notify_automation_mode_changed ();
241
242         /* actions */
243         PBD::ScopedConnectionList button_connections;
244         void button_play ();
245         void button_stop ();
246         void button_record ();
247         void button_loop ();
248         void button_metronom ();
249         void button_varispeed (bool);
250 #ifdef FP8_MUTESOLO_UNDO
251         void button_solo_clear ();
252 #endif
253         void button_mute_clear ();
254         void button_arm (bool);
255         void button_arm_all ();
256         void button_automation (ARDOUR::AutoState);
257         void button_prev_next (bool);
258         void button_action (const std::string& group, const std::string& item);
259
260         void button_encoder ();
261         void button_parameter ();
262         void encoder_navigate (bool, int);
263         void encoder_parameter (bool, int);
264
265         /* mute undo history */
266 #ifdef FP8_MUTESOLO_UNDO
267         std::vector <boost::weak_ptr<ARDOUR::AutomationControl> > _mute_state;
268         std::vector <boost::weak_ptr<ARDOUR::AutomationControl> > _solo_state;
269 #endif
270
271         /* user bound actions */
272         void button_user (bool, FP8Controls::ButtonId);
273
274         enum ActionType {
275                 Unset,
276                 NamedAction,
277                 // InternalFunction, // unused
278         };
279
280         struct UserAction {
281                 UserAction () : _type (Unset) {}
282
283                 ActionType _type;
284                 std::string _action_name;
285                 //boost::function<void()> function; // unused
286
287                 void clear ()
288                 {
289                         _type = Unset;
290                         _action_name.clear();
291                 }
292
293                 void assign_action (std::string const& action_name)
294                 {
295                         if (action_name.empty ()) {
296                                 _type = Unset;
297                                 _action_name.clear();
298                         } else {
299                                 _type = NamedAction;
300                                 _action_name = action_name;
301                         }
302                 }
303
304                 bool empty () const
305                 {
306                         return _type == Unset;
307                 }
308
309                 void call (FaderPort8& _base) const
310                 {
311                         switch (_type) {
312                                 case NamedAction:
313                                         _base.access_action (_action_name);
314                                         break;
315                                 default:
316                                         break;
317                         }
318                 }
319         };
320
321         struct ButtonAction {
322                 UserAction on_press;
323                 UserAction on_release;
324
325                 UserAction& action (bool press)
326                 {
327                         return press ? on_press : on_release;
328                 }
329
330                 UserAction const& action (bool press) const
331                 {
332                         return press ? on_press : on_release;
333                 }
334
335                 void call (FaderPort8& _base, bool press) const
336                 {
337                         action (press).call (_base);
338                 }
339                 bool empty () const
340                 {
341                         return on_press.empty () && on_release.empty();
342                 }
343         };
344
345         typedef std::map<FP8Controls::ButtonId, ButtonAction> UserActionMap;
346         UserActionMap _user_action_map;
347 };
348
349 } /* namespace */
350
351 #endif /* ardour_surface_faderport8_h */