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