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