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