da4bb619a3e6c046656fec954a89abd4e2eaf84c
[ardour.git] / generic_midi_control_protocol.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_generic_midi_control_protocol_h
21 #define ardour_generic_midi_control_protocol_h
22
23 #include <list>
24 #include <glibmm/threads.h>
25
26 #include "ardour/types.h"
27 #include "ardour/port.h"
28
29 #include "control_protocol/control_protocol.h"
30
31 namespace PBD {
32         class Controllable;
33 }
34
35 namespace ARDOUR {
36         class AsyncMIDIPort;
37         class MidiPort;
38         class Session;
39 }
40
41 namespace MIDI {
42     class Port;
43 }
44
45 class MIDIControllable;
46 class MIDIFunction;
47 class MIDIAction;
48
49 class GenericMidiControlProtocol : public ARDOUR::ControlProtocol {
50   public:
51         GenericMidiControlProtocol (ARDOUR::Session&);
52         virtual ~GenericMidiControlProtocol();
53
54         int set_active (bool yn);
55         static bool probe() { return true; }
56
57         void stripable_selection_changed () {}
58
59         std::list<boost::shared_ptr<ARDOUR::Bundle> > bundles ();
60
61         boost::shared_ptr<ARDOUR::Port> input_port () const;
62         boost::shared_ptr<ARDOUR::Port> output_port () const;
63
64         void set_feedback_interval (ARDOUR::microseconds_t);
65
66         int set_feedback (bool yn);
67         bool get_feedback () const;
68
69         boost::shared_ptr<PBD::Controllable> lookup_controllable (std::string const &) const;
70
71         void maybe_start_touch (PBD::Controllable*);
72
73         XMLNode& get_state ();
74         int set_state (const XMLNode&, int version);
75
76         bool has_editor () const { return true; }
77         void* get_gui () const;
78         void  tear_down_gui ();
79
80         int load_bindings (const std::string&);
81         void drop_bindings ();
82
83         void check_used_event (int, int);
84
85         std::string current_binding() const { return _current_binding; }
86
87         struct MapInfo {
88             std::string name;
89             std::string path;
90         };
91
92         std::list<MapInfo> map_info;
93         void reload_maps ();
94
95         void set_current_bank (uint32_t);
96         void next_bank ();
97         void prev_bank ();
98
99         void set_motorised (bool);
100
101         bool motorised () const {
102                 return _motorised;
103         }
104
105         void set_threshold (int);
106
107         int threshold () const {
108                 return _threshold;
109         }
110
111         PBD::Signal0<void> ConnectionChange;
112
113   private:
114         boost::shared_ptr<ARDOUR::Bundle> _input_bundle;
115         boost::shared_ptr<ARDOUR::Bundle> _output_bundle;
116         boost::shared_ptr<ARDOUR::AsyncMIDIPort> _input_port;
117         boost::shared_ptr<ARDOUR::AsyncMIDIPort> _output_port;
118
119         ARDOUR::microseconds_t _feedback_interval;
120         ARDOUR::microseconds_t last_feedback_time;
121
122         bool  do_feedback;
123         void _send_feedback ();
124         void  send_feedback ();
125
126         typedef std::list<MIDIControllable*> MIDIControllables;
127         MIDIControllables controllables;
128
129         typedef std::list<MIDIFunction*> MIDIFunctions;
130         MIDIFunctions functions;
131
132         typedef std::list<MIDIAction*> MIDIActions;
133         MIDIActions actions;
134
135         struct MIDIPendingControllable {
136                 MIDIControllable* mc;
137                 bool own_mc;
138                 PBD::ScopedConnection connection;
139
140                 MIDIPendingControllable (MIDIControllable* c, bool omc)
141                         : mc (c)
142                         , own_mc (omc)
143                 {}
144         };
145         typedef std::list<MIDIPendingControllable* > MIDIPendingControllables;
146         MIDIPendingControllables pending_controllables;
147         Glib::Threads::Mutex controllables_lock;
148         Glib::Threads::Mutex pending_lock;
149
150         bool start_learning (PBD::Controllable*);
151         void stop_learning (PBD::Controllable*);
152
153         void learning_stopped (MIDIControllable*);
154
155         void create_binding (PBD::Controllable*, int, int);
156         void delete_binding (PBD::Controllable*);
157
158         MIDIControllable* create_binding (const XMLNode&);
159         MIDIFunction* create_function (const XMLNode&);
160         MIDIAction* create_action (const XMLNode&);
161
162         void reset_controllables ();
163         void drop_all ();
164
165         enum ConnectionState {
166                 InputConnected = 0x1,
167                 OutputConnected = 0x2
168         };
169
170         int connection_state;
171         bool connection_handler (boost::weak_ptr<ARDOUR::Port>, std::string name1, boost::weak_ptr<ARDOUR::Port>, std::string name2, bool yn);
172         PBD::ScopedConnection port_connection;
173         void connected();
174
175         std::string _current_binding;
176         uint32_t _bank_size;
177         uint32_t _current_bank;
178         /** true if this surface is motorised.  If it is, we assume
179             that the surface's controls are never out of sync with
180             Ardour's state, so we don't have to take steps to avoid
181             values jumping around when things are not in sync.
182         */
183         bool _motorised;
184         int _threshold;
185
186         mutable void *gui;
187         void build_gui ();
188
189
190 };
191
192 #endif /* ardour_generic_midi_control_protocol_h */