9d51efc97d32afeed451fdccf08181b6009e8df2
[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         class ControllableDescriptor;
34 }
35
36 namespace ARDOUR {
37         class Session;
38         class MidiPort;
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         ARDOUR::Port* input_port () const { return ((ARDOUR::Port*) _input_port); }
58         ARDOUR::Port* output_port () const { return ((ARDOUR::Port*) _output_port); }
59         
60         void set_feedback_interval (ARDOUR::microseconds_t);
61
62         int set_feedback (bool yn);
63         bool get_feedback () const;
64
65         boost::shared_ptr<PBD::Controllable> lookup_controllable (const PBD::ControllableDescriptor&) const;
66
67         XMLNode& get_state ();
68         int set_state (const XMLNode&, int version);
69
70         bool has_editor () const { return true; }
71         void* get_gui () const;
72         void  tear_down_gui ();
73
74         int load_bindings (const std::string&);
75         void drop_bindings ();
76
77         void check_used_event (int, int);
78
79         std::string current_binding() const { return _current_binding; }
80
81         struct MapInfo {
82             std::string name;
83             std::string path;
84         };
85
86         std::list<MapInfo> map_info;
87         void reload_maps ();
88
89         void set_current_bank (uint32_t);
90         void next_bank ();
91         void prev_bank ();
92
93         void set_motorised (bool);
94
95         bool motorised () const {
96                 return _motorised;
97         }
98
99         void set_threshold (int);
100
101         int threshold () const {
102                 return _threshold;
103         }
104
105         PBD::Signal0<void> ConnectionChange;
106   private:
107         MIDI::Port* _input_port;
108         MIDI::Port* _output_port;
109         ARDOUR::microseconds_t _feedback_interval;
110         ARDOUR::microseconds_t last_feedback_time;
111
112         bool  do_feedback;
113         void _send_feedback ();
114         void  send_feedback ();
115
116         typedef std::list<MIDIControllable*> MIDIControllables;
117         MIDIControllables controllables;
118
119         typedef std::list<MIDIFunction*> MIDIFunctions;
120         MIDIFunctions functions;
121
122         typedef std::list<MIDIAction*> MIDIActions;
123         MIDIActions actions;
124
125         typedef std::pair<MIDIControllable*,PBD::ScopedConnection> MIDIPendingControllable;
126         typedef std::list<MIDIPendingControllable* > MIDIPendingControllables;
127         MIDIPendingControllables pending_controllables;
128         Glib::Threads::Mutex controllables_lock;
129         Glib::Threads::Mutex pending_lock;
130
131         bool start_learning (PBD::Controllable*);
132         void stop_learning (PBD::Controllable*);
133
134         void learning_stopped (MIDIControllable*);
135
136         void create_binding (PBD::Controllable*, int, int);
137         void delete_binding (PBD::Controllable*);
138
139         MIDIControllable* create_binding (const XMLNode&);
140         MIDIFunction* create_function (const XMLNode&);
141         MIDIAction* create_action (const XMLNode&);
142
143         void reset_controllables ();
144         void drop_all ();
145
146         std::string _current_binding;
147         uint32_t _bank_size;
148         uint32_t _current_bank;
149         /** true if this surface is motorised.  If it is, we assume
150             that the surface's controls are never out of sync with
151             Ardour's state, so we don't have to take steps to avoid
152             values jumping around when things are not in sync.
153         */
154         bool _motorised;
155         int _threshold;
156
157         mutable void *gui;
158         void build_gui ();
159 };
160
161 #endif /* ardour_generic_midi_control_protocol_h */