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