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