a4a51b7f99a5c2114894ee0aff282f15904cfafe
[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         std::string current_binding() const { return _current_binding; }
76
77         struct MapInfo {
78             std::string name;
79             std::string path;
80         };
81
82         std::list<MapInfo> map_info;
83         void reload_maps ();
84
85         void set_current_bank (uint32_t);
86         void next_bank ();
87         void prev_bank ();
88
89         void set_motorised (bool);
90         
91         bool motorised () const {
92                 return _motorised;
93         }
94
95         void set_threshold (int);
96
97         int threshold () const {
98                 return _threshold;
99         }
100
101   private:
102         MIDI::Port* _input_port;
103         MIDI::Port* _output_port;
104         ARDOUR::microseconds_t _feedback_interval;
105         ARDOUR::microseconds_t last_feedback_time;
106
107         bool  do_feedback;
108         void _send_feedback ();
109         void  send_feedback ();
110
111         typedef std::list<MIDIControllable*> MIDIControllables;
112         MIDIControllables controllables;
113
114         typedef std::list<MIDIFunction*> MIDIFunctions;
115         MIDIFunctions functions;
116
117         typedef std::list<MIDIAction*> MIDIActions;
118         MIDIActions actions;
119
120         typedef std::pair<MIDIControllable*,PBD::ScopedConnection> MIDIPendingControllable;
121         typedef std::list<MIDIPendingControllable* > MIDIPendingControllables;
122         MIDIPendingControllables pending_controllables;
123         Glib::Threads::Mutex controllables_lock;
124         Glib::Threads::Mutex pending_lock;
125
126         bool start_learning (PBD::Controllable*);
127         void stop_learning (PBD::Controllable*);
128
129         void learning_stopped (MIDIControllable*);
130
131         void create_binding (PBD::Controllable*, int, int);
132         void delete_binding (PBD::Controllable*);
133
134         MIDIControllable* create_binding (const XMLNode&);
135         MIDIFunction* create_function (const XMLNode&);
136         MIDIAction* create_action (const XMLNode&);
137
138         void reset_controllables ();
139         void drop_all ();
140
141         std::string _current_binding;
142         uint32_t _bank_size;
143         uint32_t _current_bank;
144         /** true if this surface is motorised.  If it is, we assume
145             that the surface's controls are never out of sync with
146             Ardour's state, so we don't have to take steps to avoid
147             values jumping around when things are not in sync.
148         */
149         bool _motorised;
150         int _threshold;
151
152         mutable void *gui;
153         void build_gui ();
154 };
155
156 #endif /* ardour_generic_midi_control_protocol_h */