e81a45b47ab64b3ea93f50f0da215468e77c7e1a
[ardour.git] / libs / surfaces / generic_midi / generic_midi_control_protocol.cc
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     $Id$
19 */
20
21 #include <algorithm>
22
23 #include <pbd/error.h>
24 #include <pbd/failed_constructor.h>
25
26 #include <midi++/port.h>
27 #include <midi++/manager.h>
28 #include <midi++/port_request.h>
29
30 #include <ardour/route.h>
31 #include <ardour/session.h>
32
33 #include "generic_midi_control_protocol.h"
34 #include "midicontrollable.h"
35
36 using namespace ARDOUR;
37 using namespace PBD;
38
39 #include "i18n.h"
40
41 GenericMidiControlProtocol::GenericMidiControlProtocol (Session& s)
42         : ControlProtocol  (s, _("Generic MIDI"))
43 {
44         MIDI::Manager* mm = MIDI::Manager::instance();
45
46         /* XXX it might be nice to run "control" through i18n, but thats a bit tricky because
47            the name is defined in ardour.rc which is likely not internationalized.
48         */
49         
50         _port = mm->port (X_("control"));
51
52         if (_port == 0) {
53                 error << _("no MIDI port named \"control\" exists - generic MIDI control disabled") << endmsg;
54                 throw failed_constructor();
55         }
56
57         do_feedback = false;
58         _feedback_interval = 10000; // microseconds
59         last_feedback_time = 0;
60
61         Controllable::StartLearning.connect (mem_fun (*this, &GenericMidiControlProtocol::start_learning));
62         Controllable::StopLearning.connect (mem_fun (*this, &GenericMidiControlProtocol::stop_learning));
63         Session::SendFeedback.connect (mem_fun (*this, &GenericMidiControlProtocol::send_feedback));
64 }
65
66 GenericMidiControlProtocol::~GenericMidiControlProtocol ()
67 {
68 }
69
70 int
71 GenericMidiControlProtocol::set_active (bool yn)
72 {
73         /* start/stop delivery/outbound thread */
74         return 0;
75 }
76
77 void
78 GenericMidiControlProtocol::set_feedback_interval (microseconds_t ms)
79 {
80         _feedback_interval = ms;
81 }
82
83 void 
84 GenericMidiControlProtocol::send_feedback ()
85 {
86         if (!do_feedback) {
87                 return;
88         }
89
90         microseconds_t now = get_microseconds ();
91
92         if (last_feedback_time != 0) {
93                 if ((now - last_feedback_time) < _feedback_interval) {
94                         return;
95                 }
96         }
97
98         _send_feedback ();
99         
100         last_feedback_time = now;
101 }
102
103 void 
104 GenericMidiControlProtocol::_send_feedback ()
105 {
106         const int32_t bufsize = 16 * 1024; /* XXX too big */
107         MIDI::byte buf[bufsize];
108         int32_t bsize = bufsize;
109         MIDI::byte* end = buf;
110         
111         for (MIDIControllables::iterator r = controllables.begin(); r != controllables.end(); ++r) {
112                 end = (*r)->write_feedback (end, bsize);
113         }
114         
115         if (end == buf) {
116                 return;
117         } 
118         
119         _port->write (buf, (int32_t) (end - buf));
120 }
121
122 bool
123 GenericMidiControlProtocol::start_learning (Controllable* c)
124 {
125         if (c == 0) {
126                 return false;
127         }
128
129         MIDIControllable* mc = 0;
130
131         for (MIDIControllables::iterator i = controllables.begin(); i != controllables.end(); ++i) {
132                 if ((*i)->get_controllable().id() == c->id()) {
133                         mc = *i;
134                         break;
135                 }
136         }
137
138         if (!mc) {
139                 mc = new MIDIControllable (*_port, *c);
140         }
141         
142         {
143                 Glib::Mutex::Lock lm (pending_lock);
144                 std::pair<MIDIControllables::iterator,bool> result;
145                 result = pending_controllables.insert (mc);
146                 if (result.second) {
147                         c->LearningFinished.connect (bind (mem_fun (*this, &GenericMidiControlProtocol::learning_stopped), mc));
148                 }
149         }
150
151         mc->learn_about_external_control ();
152         return true;
153 }
154
155 void
156 GenericMidiControlProtocol::learning_stopped (MIDIControllable* mc)
157 {
158         Glib::Mutex::Lock lm (pending_lock);
159         Glib::Mutex::Lock lm2 (controllables_lock);
160         
161         MIDIControllables::iterator i = find (pending_controllables.begin(), pending_controllables.end(), mc);
162
163         if (i != pending_controllables.end()) {
164                 pending_controllables.erase (i);
165         }
166
167         controllables.insert (mc);
168 }
169
170 void
171 GenericMidiControlProtocol::stop_learning (Controllable* c)
172 {
173         Glib::Mutex::Lock lm (pending_lock);
174
175         /* learning timed out, and we've been told to consider this attempt to learn to be cancelled. find the
176            relevant MIDIControllable and remove it from the pending list.
177         */
178
179         for (MIDIControllables::iterator i = pending_controllables.begin(); i != pending_controllables.end(); ++i) {
180                 if (&(*i)->get_controllable() == c) {
181                         (*i)->stop_learning ();
182                         delete (*i);
183                         pending_controllables.erase (i);
184                         break;
185                 }
186         }
187 }
188
189 XMLNode&
190 GenericMidiControlProtocol::get_state () 
191 {
192         XMLNode* node = new XMLNode ("Protocol"); 
193         char buf[32];
194
195         node->add_property (X_("name"), _name);
196         node->add_property (X_("feedback"), do_feedback ? "1" : "0");
197         snprintf (buf, sizeof (buf), "%" PRIu64, _feedback_interval);
198         node->add_property (X_("feedback_interval"), buf);
199
200         XMLNode* children = new XMLNode (X_("controls"));
201
202         node->add_child_nocopy (*children);
203
204         Glib::Mutex::Lock lm2 (controllables_lock);
205         for (MIDIControllables::iterator i = controllables.begin(); i != controllables.end(); ++i) {
206                 children->add_child_nocopy ((*i)->get_state());
207         }
208
209         return *node;
210 }
211
212 int
213 GenericMidiControlProtocol::set_state (const XMLNode& node)
214 {
215         XMLNodeList nlist;
216         XMLNodeConstIterator niter;
217         const XMLProperty* prop;
218
219         if ((prop = node.property ("feedback")) != 0) {
220                 do_feedback = (bool) atoi (prop->value().c_str());
221         } else {
222                 do_feedback = false;
223         }
224
225         if ((prop = node.property ("feedback_interval")) != 0) {
226                 if (sscanf (prop->value().c_str(), "%" PRIu64, &_feedback_interval) != 1) {
227                         _feedback_interval = 10000;
228                 }
229         } else {
230                 _feedback_interval = 10000;
231         }
232
233         Controllable* c;
234
235         {
236                 Glib::Mutex::Lock lm (pending_lock);
237                 pending_controllables.clear ();
238         }
239
240         Glib::Mutex::Lock lm2 (controllables_lock);
241
242         controllables.clear ();
243
244         nlist = node.children(); // "controls"
245
246         if (nlist.empty()) {
247                 return 0;
248         }
249
250         nlist = nlist.front()->children ();
251
252         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
253
254                 if ((prop = (*niter)->property ("id")) != 0) {
255                         
256                         ID id = prop->value ();
257                         
258                         c = session->controllable_by_id (id);
259                         
260                         if (c) {
261                                 MIDIControllable* mc = new MIDIControllable (*_port, *c);
262                                 if (mc->set_state (**niter) == 0) {
263                                         controllables.insert (mc);
264                                 }
265                                 
266                         } else {
267                                 warning << string_compose (_("Generic MIDI control: controllable %1 not found in session (ignored)"),
268                                                            id)
269                                         << endmsg;
270                         }
271                 }
272         }
273
274         return 0;
275 }
276
277 int
278 GenericMidiControlProtocol::set_feedback (bool yn)
279 {
280         do_feedback = yn;
281         last_feedback_time = 0;
282         return 0;
283 }
284
285 bool
286 GenericMidiControlProtocol::get_feedback () const
287 {
288         return do_feedback;
289 }