part of lincoln's patches for OSC/ardroid
[ardour.git] / libs / surfaces / osc / osc.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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  *  
18  */
19
20 #include <iostream>
21 #include <fstream>
22 #include <cstdio>
23 #include <cstdlib>
24 #include <cerrno>
25 #include <algorithm>
26
27 #include <sys/poll.h>
28 #include <unistd.h>
29 #include <fcntl.h>
30
31 #include <glibmm/miscutils.h>
32 #include <boost/signals2.hpp>
33
34 #include <pbd/pthread_utils.h>
35 #include <pbd/file_utils.h>
36 #include <pbd/filesystem.h>
37 #include <pbd/failed_constructor.h>
38
39 #include "ardour/session.h"
40 #include "ardour/route.h"
41 #include "ardour/audio_track.h"
42 #include "ardour/midi_track.h"
43 #include "ardour/dB.h"
44 #include "ardour/filesystem_paths.h"
45 #include "ardour/panner.h"
46 #include "ardour/plugin.h"
47
48 #include "osc.h"
49 #include "osc_controllable.h"
50 #include "osc_route_observer.h"
51 #include "i18n.h"
52
53 using namespace ARDOUR;
54 using namespace std;
55 using namespace Glib;
56
57 #include "pbd/abstract_ui.cc" // instantiate template
58
59 #define ui_bind(f, ...) boost::protect (boost::bind (f, __VA_ARGS__))
60
61 OSC* OSC::_instance = 0;
62
63 #ifdef DEBUG
64 static void error_callback(int num, const char *m, const char *path)
65 {
66         fprintf(stderr, "liblo server error %d in path %s: %s\n", num, path, m);
67 }
68 #else
69 static void error_callback(int, const char *, const char *)
70 {
71
72 }
73 #endif
74
75 OSC::OSC (Session& s, uint32_t port)
76         : ControlProtocol (s, "OSC", this)
77         , AbstractUI<OSCUIRequest> ("osc")
78         , _port(port)
79 {
80         _instance = this;
81         _shutdown = false;
82         _osc_server = 0;
83         _osc_unix_server = 0;
84         _namespace_root = "/ardour";
85         _send_route_changes = true;
86
87         /* glibmm hack */
88         local_server = 0;
89         remote_server = 0;
90
91         // "Application Hooks"
92         session_loaded (s);
93         session->Exported.connect (*this, MISSING_INVALIDATOR, ui_bind (&OSC::session_exported, this, _1, _2), this);
94 }
95
96 OSC::~OSC()
97 {
98         stop ();
99         _instance = 0;
100 }
101
102 void
103 OSC::do_request (OSCUIRequest* req)
104 {
105         if (req->type == CallSlot) {
106
107                 call_slot (MISSING_INVALIDATOR, req->the_slot);
108
109         } else if (req->type == Quit) {
110
111                 stop ();
112         }
113 }
114
115 int
116 OSC::set_active (bool yn)
117 {
118         if (yn) {
119                 return start ();
120         } else {
121                 return stop ();
122         }
123 }
124
125 bool
126 OSC::get_active () const
127 {
128         return _osc_server != 0;
129 }
130
131 int 
132 OSC::set_feedback (bool yn)
133 {
134         _send_route_changes = yn;
135         return 0;
136 }
137
138 bool
139 OSC::get_feedback () const
140 {
141         return _send_route_changes;
142 }
143
144 int
145 OSC::start ()
146 {
147         char tmpstr[255];
148
149         if (_osc_server) {
150                 /* already started */
151                 return 0;
152         }
153         
154         for (int j=0; j < 20; ++j) {
155                 snprintf(tmpstr, sizeof(tmpstr), "%d", _port);
156                 
157                 //if ((_osc_server = lo_server_new_with_proto (tmpstr, LO_TCP, error_callback))) {
158                 //      break;
159                 //}
160                 
161                 if ((_osc_server = lo_server_new (tmpstr, error_callback))) {
162                         break;
163                 }
164
165 #ifdef DEBUG            
166                 cerr << "can't get osc at port: " << _port << endl;
167 #endif
168                 _port++;
169                 continue;
170         }
171         
172 #ifdef ARDOUR_OSC_UNIX_SERVER
173         
174         // APPEARS sluggish for now
175         
176         // attempt to create unix socket server too
177         
178         snprintf(tmpstr, sizeof(tmpstr), "/tmp/sooperlooper_XXXXXX");
179         int fd = mkstemp(tmpstr);
180         
181         if (fd >= 0 ) {
182                 unlink (tmpstr);
183                 close (fd);
184                 
185                 _osc_unix_server = lo_server_new (tmpstr, error_callback);
186                 
187                 if (_osc_unix_server) {
188                         _osc_unix_socket_path = tmpstr;
189                 }
190         }
191 #endif
192         
193         cerr << "OSC @ " << get_server_url () << endl;
194
195         PBD::sys::path url_file;
196
197         if (find_file_in_search_path (ardour_search_path() + system_config_search_path(),
198                                       "osc_url", url_file)) {
199                 
200                 _osc_url_file = url_file.to_string();
201                 ofstream urlfile;
202                 urlfile.open(_osc_url_file.c_str(), ios::trunc);
203                 
204                 if ( urlfile ){
205                         urlfile << get_server_url () << endl;
206                         urlfile.close();
207                 }
208                 else {  
209                         cerr << "Couldn't write '" <<  _osc_url_file << "'" <<endl;
210                 }
211         }
212         
213         register_callbacks();
214         
215         // lo_server_thread_add_method(_sthread, NULL, NULL, OSC::_dummy_handler, this);
216
217         /* startup the event loop thread */
218
219         BaseUI::run ();
220
221         return 0;
222 }
223
224 void
225 OSC::thread_init ()
226 {
227         char* c = new char[4];
228         strcpy (c, X_("OSC"));
229         pthread_set_name (c);
230
231         if (_osc_unix_server) {
232                 Glib::RefPtr<IOSource> src = IOSource::create (lo_server_get_socket_fd (_osc_unix_server), IO_IN|IO_HUP|IO_ERR);
233                 src->connect (sigc::bind (sigc::mem_fun (*this, &OSC::osc_input_handler), _osc_unix_server));
234                 src->attach (_main_loop->get_context());
235                 local_server = src->gobj();
236                 g_source_ref (local_server);
237         }
238
239         if (_osc_server) {
240                 Glib::RefPtr<IOSource> src  = IOSource::create (lo_server_get_socket_fd (_osc_server), IO_IN|IO_HUP|IO_ERR);
241                 src->connect (sigc::bind (sigc::mem_fun (*this, &OSC::osc_input_handler), _osc_server));
242                 src->attach (_main_loop->get_context());
243                 remote_server = src->gobj();
244                 g_source_ref (remote_server);
245         }
246
247         PBD::notify_gui_about_thread_creation (X_("gui"), pthread_self(), X_("OSC"), 2048);
248         SessionEvent::create_per_thread_pool (X_("OSC"), 128);
249 }
250
251 int
252 OSC::stop ()
253 {       
254         /* stop main loop */
255
256         if (local_server) {
257                 g_source_destroy (local_server);
258                 g_source_unref (local_server);
259                 local_server = 0;
260         }
261
262         if (remote_server) {
263                 g_source_destroy (remote_server);
264                 g_source_unref (remote_server);
265                 remote_server = 0;
266         }
267
268         BaseUI::quit ();
269
270         if (_osc_server) {
271                 int fd = lo_server_get_socket_fd(_osc_server);
272                 if (fd >=0) {
273                         close(fd);
274                 }
275                 lo_server_free (_osc_server);
276                 _osc_server = 0;
277         }
278
279         if (_osc_unix_server) {
280                 int fd = lo_server_get_socket_fd(_osc_unix_server);
281                 if (fd >=0) {
282                         close(fd);
283                 }
284                 lo_server_free (_osc_unix_server);
285                 _osc_unix_server = 0;
286         }
287         
288         if (!_osc_unix_socket_path.empty()) {
289                 unlink (_osc_unix_socket_path.c_str());
290         }
291         
292         if (!_osc_url_file.empty() ) {
293                 unlink (_osc_url_file.c_str() );
294         }
295
296         return 0;
297 }
298
299 void
300 OSC::register_callbacks()
301 {
302         lo_server srvs[2];
303         lo_server serv;
304
305         srvs[0] = _osc_server;
306         srvs[1] = _osc_unix_server;
307         
308         for (size_t i = 0; i < 2; ++i) {
309
310                 if (!srvs[i]) {
311                         continue;
312                 }
313
314                 serv = srvs[i];
315                 
316                 /* this is a special catchall handler */
317                 
318                 lo_server_add_method (serv, 0, 0, _catchall, this);
319
320 #define REGISTER_CALLBACK(serv,path,types, function) lo_server_add_method (serv, path, types, OSC::_ ## function, this)
321                 
322                 REGISTER_CALLBACK (serv, "/ardour/add_marker", "", add_marker);
323                 REGISTER_CALLBACK (serv, "/ardour/access_action", "s", access_action);
324                 REGISTER_CALLBACK (serv, "/ardour/loop_toggle", "", loop_toggle);
325                 REGISTER_CALLBACK (serv, "/ardour/goto_start", "", goto_start);
326                 REGISTER_CALLBACK (serv, "/ardour/goto_end", "", goto_end);
327                 REGISTER_CALLBACK (serv, "/ardour/rewind", "", rewind);
328                 REGISTER_CALLBACK (serv, "/ardour/ffwd", "", ffwd);
329                 REGISTER_CALLBACK (serv, "/ardour/transport_stop", "", transport_stop);
330                 REGISTER_CALLBACK (serv, "/ardour/transport_play", "", transport_play);
331                 //REGISTER_CALLBACK (serv, "/ardour/transport_frame", "", transport_frame);
332                 REGISTER_CALLBACK (serv, "/ardour/set_transport_speed", "f", set_transport_speed);
333                 REGISTER_CALLBACK (serv, "/ardour/locate", "ii", locate);
334                 REGISTER_CALLBACK (serv, "/ardour/save_state", "", save_state);
335                 REGISTER_CALLBACK (serv, "/ardour/prev_marker", "", prev_marker);
336                 REGISTER_CALLBACK (serv, "/ardour/next_marker", "", next_marker);
337                 REGISTER_CALLBACK (serv, "/ardour/undo", "", undo);
338                 REGISTER_CALLBACK (serv, "/ardour/redo", "", redo);
339                 REGISTER_CALLBACK (serv, "/ardour/toggle_punch_in", "", toggle_punch_in);
340                 REGISTER_CALLBACK (serv, "/ardour/toggle_punch_out", "", toggle_punch_out);
341                 REGISTER_CALLBACK (serv, "/ardour/rec_enable_toggle", "", rec_enable_toggle);
342                 REGISTER_CALLBACK (serv, "/ardour/toggle_all_rec_enables", "", toggle_all_rec_enables);
343
344                 REGISTER_CALLBACK (serv, "/ardour/routes/mute", "ii", route_mute);
345                 REGISTER_CALLBACK (serv, "/ardour/routes/solo", "ii", route_solo);
346                 REGISTER_CALLBACK (serv, "/ardour/routes/recenable", "ii", route_recenable);
347                 REGISTER_CALLBACK (serv, "/ardour/routes/gainabs", "if", route_set_gain_abs);
348                 REGISTER_CALLBACK (serv, "/ardour/routes/gaindB", "if", route_set_gain_dB);
349                 REGISTER_CALLBACK (serv, "/ardour/routes/pan_stereo_position", "if", route_set_pan_stereo_position);
350                 REGISTER_CALLBACK (serv, "/ardour/routes/pan_stereo_width", "if", route_set_pan_stereo_width);
351                 REGISTER_CALLBACK (serv, "/ardour/routes/plugin/parameter", "iiif", route_plugin_parameter);
352                 REGISTER_CALLBACK (serv, "/ardour/routes/plugin/parameter/print", "iii", route_plugin_parameter_print);
353
354                 
355
356                 /* still not-really-standardized query interface */
357                 //REGISTER_CALLBACK (serv, "/ardour/*/#current_value", "", current_value);
358                 //REGISTER_CALLBACK (serv, "/ardour/set", "", set);
359
360                 // un/register_update args= s:ctrl s:returl s:retpath
361                 //lo_server_add_method(serv, "/register_update", "sss", OSC::global_register_update_handler, this);
362                 //lo_server_add_method(serv, "/unregister_update", "sss", OSC::global_unregister_update_handler, this);
363                 //lo_server_add_method(serv, "/register_auto_update", "siss", OSC::global_register_auto_update_handler, this);
364                 //lo_server_add_method(serv, "/unregister_auto_update", "sss", OSC::_global_unregister_auto_update_handler, this);
365
366         }
367 }
368
369 bool
370 OSC::osc_input_handler (IOCondition ioc, lo_server srv)
371 {
372         if (ioc & ~IO_IN) {
373                 return false;
374         }
375
376         if (ioc & IO_IN) {
377                 lo_server_recv (srv);
378         }
379
380         return true;
381 }
382
383 std::string
384 OSC::get_server_url()
385 {
386         string url;
387         char * urlstr;
388
389         if (_osc_server) {
390                 urlstr = lo_server_get_url (_osc_server);
391                 url = urlstr;
392                 free (urlstr);
393         }
394         
395         return url;
396 }
397
398 std::string
399 OSC::get_unix_server_url()
400 {
401         string url;
402         char * urlstr;
403
404         if (_osc_unix_server) {
405                 urlstr = lo_server_get_url (_osc_unix_server);
406                 url = urlstr;
407                 free (urlstr);
408         }
409         
410         return url;
411 }
412
413
414 void
415 OSC::current_value_query (const char* path, size_t len, lo_arg **argv, int argc, lo_message msg)
416 {
417         char* subpath;
418         
419         subpath = (char*) malloc (len-15+1);
420         memcpy (subpath, path, len-15);
421         subpath[len-15] = '\0';
422         
423         send_current_value (subpath, argv, argc, msg);
424         
425         free (subpath);
426 }
427
428 void
429 OSC::send_current_value (const char* path, lo_arg** argv, int argc, lo_message msg)
430 {
431         if (!session) {
432                 return;
433         }
434
435         lo_message reply = lo_message_new ();
436         boost::shared_ptr<Route> r;
437         int id;
438
439         lo_message_add_string (reply, path);
440         
441         if (argc == 0) {
442                 lo_message_add_string (reply, "bad syntax");
443         } else {
444                 id = argv[0]->i;
445                 r = session->route_by_remote_id (id);
446
447                 if (!r) {
448                         lo_message_add_string (reply, "not found");
449                 } else {
450
451                         if (strcmp (path, "/routes/state") == 0) {
452                                 
453                                 if (boost::dynamic_pointer_cast<AudioTrack>(r)) {
454                                         lo_message_add_string (reply, "AT");
455                                 } else if (boost::dynamic_pointer_cast<MidiTrack>(r)) {
456                                         lo_message_add_string (reply, "MT");
457                                 } else {
458                                         lo_message_add_string (reply, "B");
459                                 }
460                                 
461                                 lo_message_add_string (reply, r->name().c_str());
462                                 lo_message_add_int32 (reply, r->n_inputs().n_audio());
463                                 lo_message_add_int32 (reply, r->n_outputs().n_audio());
464                                 lo_message_add_int32 (reply, r->muted());
465                                 lo_message_add_int32 (reply, r->soloed());
466                                 
467                         } else if (strcmp (path, "/routes/mute") == 0) {
468                                 
469                                 lo_message_add_int32 (reply, (float) r->muted());
470                                 
471                         } else if (strcmp (path, "/routes/solo") == 0) {
472                                 
473                                 lo_message_add_int32 (reply, r->soloed());
474                         }
475                 }
476         }
477
478         lo_send_message (lo_message_get_source (msg), "#reply", reply);
479         lo_message_free (reply);
480 }
481         
482 int
483 OSC::_catchall (const char *path, const char *types, lo_arg **argv, int argc, void *data, void *user_data) 
484 {
485         return ((OSC*)user_data)->catchall (path, types, argv, argc, data);
486 }
487
488 int
489 OSC::catchall (const char *path, const char *types, lo_arg **argv, int argc, lo_message msg) 
490 {
491         size_t len;
492         int ret = 1; /* unhandled */
493
494         //cerr << "Received a message, path = " << path << " types = \"" 
495         //     << (types ? types : "NULL") << '"' << endl;
496
497         /* 15 for /#current_value plus 2 for /<path> */
498
499         len = strlen (path);
500
501         if (len >= 17 && !strcmp (&path[len-15], "/#current_value")) {
502                 current_value_query (path, len, argv, argc, msg);
503                 ret = 0;
504         } else if (strcmp (path, "/ardour/transport_frame") == 0) {
505                 
506                 framepos_t pos = transport_frame ();
507                 
508                 lo_message reply = lo_message_new ();
509                 lo_message_add_int64 (reply, pos);
510                 
511                 lo_send_message (lo_message_get_source (msg), "/ardour/transport_frame", reply);
512                 
513                 lo_message_free (reply);
514                 
515                 ret = 0;
516                 
517         } else if (strcmp (path, "/routes/listen") == 0) {
518                 
519                 cerr << "set up listener\n";
520
521                 lo_message reply = lo_message_new ();
522
523                 if (argc <= 0) {
524                         lo_message_add_string (reply, "syntax error");
525                 } else {
526                         for (int n = 0; n < argc; ++n) {
527
528                                 boost::shared_ptr<Route> r = session->route_by_remote_id (argv[n]->i);
529                                 
530                                 if (!r) {
531                                         lo_message_add_string (reply, "not found");
532                                         cerr << "no such route\n";
533                                         break;
534                                 } else {                        
535                                         cerr << "add listener\n";
536                                         listen_to_route (r, lo_message_get_source (msg));
537                                         lo_message_add_int32 (reply, argv[n]->i);
538                                 }
539                         }
540                 }
541
542                 lo_send_message (lo_message_get_source (msg), "#reply", reply);
543                 lo_message_free (reply);
544                 
545                 ret = 0;
546
547         } else if (strcmp (path, "/routes/ignore") == 0) {
548
549                 for (int n = 0; n < argc; ++n) {
550
551                         boost::shared_ptr<Route> r = session->route_by_remote_id (argv[n]->i);
552                         
553                         if (r) {
554                                 end_listen (r, lo_message_get_source (msg));
555                         }
556                 }
557                 
558                 ret = 0;
559                 
560         } else if (strcmp (path, "/routes/list") == 0) {
561
562                 for (int n = 0; n < (int) session->nroutes(); ++n) {
563
564                         boost::shared_ptr<Route> r = session->route_by_remote_id (n);
565                         
566                         if (r) {
567
568                                 lo_message reply = lo_message_new ();
569                         
570                                 if (boost::dynamic_pointer_cast<AudioTrack>(r)) {
571                                         lo_message_add_string (reply, "AT");
572                                 } else if (boost::dynamic_pointer_cast<MidiTrack>(r)) {
573                                         lo_message_add_string (reply, "MT");
574                                 } else {
575                                         lo_message_add_string (reply, "B");
576                                 }
577                                 
578                                 lo_message_add_string (reply, r->name().c_str());
579                                 lo_message_add_int32 (reply, r->n_inputs().n_audio());
580                                 lo_message_add_int32 (reply, r->n_outputs().n_audio());
581                                 lo_message_add_int32 (reply, r->muted());
582                                 lo_message_add_int32 (reply, r->soloed());
583                                 lo_message_add_int32 (reply, r->remote_control_id());
584                                 
585                                 if (boost::dynamic_pointer_cast<AudioTrack>(r) 
586                                         || boost::dynamic_pointer_cast<MidiTrack>(r)) {
587                 
588                                         boost::shared_ptr<Track> t = boost::dynamic_pointer_cast<Track>(r);
589                                         lo_message_add_int32 (reply, t->record_enabled());
590                                 }
591                                 
592                                 lo_send_message (lo_message_get_source (msg), "#reply", reply);
593                                 lo_message_free (reply);
594                                 
595                                 //Automatically listen to routes listed
596                                 listen_to_route(r, lo_message_get_source (msg));
597                         }
598                 }
599         
600                 // Send end of listing message
601                 lo_message reply = lo_message_new ();
602
603                 lo_message_add_string (reply, "end_route_list");
604                 lo_message_add_int64 (reply, session->frame_rate());
605                 lo_message_add_int64 (reply, session->current_end_frame());
606                 
607                 lo_send_message (lo_message_get_source (msg), "#reply", reply);
608                 
609                 lo_message_free (reply);
610                 
611                 ret = 0;
612         }
613
614         return ret;
615 }
616
617 void
618 OSC::update_clock (){
619   
620 }
621
622 void
623 OSC::listen_to_route (boost::shared_ptr<Route> route, lo_address addr)
624 {
625         Controllables::iterator x;
626         bool route_exists = false;
627
628         cerr << "listen to route\n";
629
630         /* avoid duplicate listens */
631         
632         for (x = controllables.begin(); x != controllables.end(); ++x) {
633                 
634                 OSCRouteControllable* rc;
635
636                 if ((rc = dynamic_cast<OSCRouteControllable*>(*x)) != 0) {
637                         
638                         if (rc->route() == route) {
639                                 route_exists = true;
640                                 
641                                 /* XXX NEED lo_address_equal() */
642                                 int res = strcmp(lo_address_get_hostname(rc->address()), lo_address_get_hostname(addr));
643                                 
644                                 if (res == 0) {
645                                         cerr << "already listening to route" << endl;
646                                         return;
647                                 }
648                         }
649                 }
650         }
651
652         cerr << "listener binding to signals\n";
653
654         OSCControllable* c;
655         string path;
656         
657         if (boost::dynamic_pointer_cast<AudioTrack>(route) || boost::dynamic_pointer_cast<MidiTrack>(route)) {
658                 
659                 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track>(route);
660                 
661                 path = X_("/route/rec");
662                 c = new OSCRouteControllable (addr, path, track->rec_enable_control(), track);
663                 controllables.push_back (c);
664         }
665
666         path = X_("/route/solo");
667         c = new OSCRouteControllable (addr, path, route->solo_control(), route);
668         controllables.push_back (c);
669
670         path = X_("/route/mute");
671         c = new OSCRouteControllable (addr, path, route->mute_control(), route);
672         controllables.push_back (c);
673
674         path = X_("/route/gain");
675         c = new OSCRouteControllable (addr, path, route->gain_control(), route);
676         controllables.push_back (c);
677         
678         cerr << "Now have " << controllables.size() << " controllables\n";
679
680         OSCRouteObserver* o;
681         
682         o = new OSCRouteObserver (addr, path, route);
683         observables.push_back (o);
684         
685         /* if there is no existing controllable related to this route, make sure we clean up
686            if it is ever deleted.
687         */
688         
689         if (!route_exists) {
690                 route->DropReferences.connect (*this, MISSING_INVALIDATOR, boost::bind (&OSC::drop_route, this, boost::weak_ptr<Route> (route)), this);
691         }
692 }
693
694 void
695 OSC::drop_route (boost::weak_ptr<Route> wr)
696 {
697         boost::shared_ptr<Route> r = wr.lock ();
698
699         if (!r) {
700                 return;
701         }
702
703         for (Controllables::iterator x = controllables.begin(); x != controllables.end();) {
704
705                 OSCRouteControllable* rc;
706                 
707                 if ((rc = dynamic_cast<OSCRouteControllable*>(*x)) != 0) {
708                         if (rc->route() == r) {
709                                 delete *x;
710                                 x = controllables.erase (x);
711                         } else {
712                                 ++x;
713                         }
714                 } else {
715                         ++x;
716                 }
717         }
718 }
719
720 void
721 OSC::end_listen (boost::shared_ptr<Route> r, lo_address addr)
722 {
723         Controllables::iterator x;
724
725         for (x = controllables.begin(); x != controllables.end();) {
726
727                 OSCRouteControllable* rc;
728                 
729                 if ((rc = dynamic_cast<OSCRouteControllable*>(*x)) != 0) {
730
731                         /* XXX NEED lo_address_equal () */
732                         if (rc->route() == r && (0 == strcmp(lo_address_get_hostname(rc->address()), lo_address_get_hostname(addr)))){
733                                 delete *x;
734                                 x = controllables.erase (x);
735                         }
736                         else {
737                                 ++x;
738                         }
739                 }
740                 else {
741                         ++x;
742                 }
743         }
744         
745         Observables::iterator z;
746         
747         // Remove the route observers
748         for (z = observables.begin(); z != observables.end();) {
749
750                 OSCRouteObserver* ro;
751                 
752                 if ((ro = dynamic_cast<OSCRouteObserver*>(*z)) != 0) {
753
754                         /* XXX NEED lo_address_equal () */
755                         if (ro->route() == r){
756                                 delete *z;
757                                 z = observables.erase (z);
758                         }
759                         else {
760                                 ++z;
761                         }
762                 }
763                 else {
764                         ++z;
765                 }
766         }
767 }
768
769
770 // "Application Hook" Handlers //
771 void
772 OSC::session_loaded( Session& s ) {
773         lo_address listener = lo_address_new( NULL, "7770" );
774         lo_send( listener, "/session/loaded", "ss", s.path().c_str(), s.name().c_str() );
775 }
776
777 void
778 OSC::session_exported( std::string path, std::string name ) {
779         lo_address listener = lo_address_new( NULL, "7770" );
780         lo_send( listener, "/session/exported", "ss", path.c_str(), name.c_str() );
781 }
782
783 // end "Application Hook" Handlers //
784
785 /* path callbacks */
786
787 int 
788 OSC::current_value (const char */*path*/, const char */*types*/, lo_arg **/*argv*/, int /*argc*/, void */*data*/, void* /*user_data*/) 
789
790 #if 0
791         const char* returl;
792
793         if (argc < 3 || types == 0 || strlen (types) < 3 || types[0] != 's' || types[1] != 's' || types[2] != s) {
794                 return 1;
795         }
796
797         const char *returl = argv[1]->s;
798         lo_address addr = find_or_cache_addr (returl);
799
800         const char *retpath = argv[2]->s;
801
802         
803         if (strcmp (argv[0]->s, "transport_frame") == 0) {
804
805                 if (session) {
806                         lo_send (addr, retpath, "i", session->transport_frame());
807                 }
808
809         } else if (strcmp (argv[0]->s, "transport_speed") == 0) {
810                 
811                 if (session) {
812                         lo_send (addr, retpath, "i", session->transport_frame());
813                 }
814                 
815         } else if (strcmp (argv[0]->s, "transport_locked") == 0) {
816                 
817                 if (session) {
818                         lo_send (addr, retpath, "i", session->transport_frame());
819                 }
820                 
821         } else if (strcmp (argv[0]->s, "punch_in") == 0) {
822                 
823                 if (session) {
824                         lo_send (addr, retpath, "i", session->transport_frame());
825                 }
826                 
827         } else if (strcmp (argv[0]->s, "punch_out") == 0) {
828
829                 if (session) {
830                         lo_send (addr, retpath, "i", session->transport_frame());
831                 }
832                 
833         } else if (strcmp (argv[0]->s, "rec_enable") == 0) {
834                         
835                 if (session) {
836                         lo_send (addr, retpath, "i", session->transport_frame());
837                 }
838
839         } else {
840
841                 /* error */
842         }
843 #endif
844         return 0;
845 }
846
847 int
848 OSC::route_mute (int rid, int yn)
849 {
850         if (!session) return -1;
851
852         boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
853
854         if (r) {
855                 r->set_mute (yn, this);
856         }
857         return 0;
858 }
859
860 int
861 OSC::route_solo (int rid, int yn)
862 {
863         if (!session) return -1;
864
865         boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
866
867         if (r) {
868                 r->set_solo (yn, this);
869         }
870         return 0;
871 }
872
873 int
874 OSC::route_recenable (int rid, int yn)
875 {
876         if (!session) return -1;
877
878         boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
879
880         if (r) {
881                 r->set_record_enabled (yn, this);
882         }
883         return 0;
884 }
885
886 int
887 OSC::route_set_gain_abs (int rid, float level)
888 {
889         if (!session) return -1;
890
891         boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
892
893         if (r) {
894                 r->set_gain (level, this);
895         }
896
897         return 0;
898 }
899
900 int
901 OSC::route_set_gain_dB (int rid, float dB)
902 {
903         if (!session) return -1;
904
905         boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
906
907         if (r) {
908                 r->set_gain (dB_to_coefficient (dB), this);
909         }
910         
911         return 0;
912 }
913
914 int
915 OSC::route_set_pan_stereo_position (int rid, float pos)
916 {
917         if (!session) return -1;
918
919         boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
920
921         if (r) {
922                 boost::shared_ptr<Panner> panner = r->panner();
923                 if (panner) {
924                         panner->set_stereo_position (pos);
925                 }
926         }
927         
928         return 0;
929
930 }
931
932 int
933 OSC::route_set_pan_stereo_width (int rid, float pos)
934 {
935         if (!session) return -1;
936
937         boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
938
939         if (r) {
940                 boost::shared_ptr<Panner> panner = r->panner();
941                 if (panner) {
942                         panner->set_stereo_width (pos);
943                 }
944         }
945         
946         return 0;
947
948 }
949
950 int
951 OSC::route_plugin_parameter (int rid,int piid,int par, float val)
952 {
953         if (!session) { 
954                 return -1;
955         }
956
957         boost::shared_ptr<Route> r = session->route_by_remote_id (rid);  
958
959         if (!r) {
960                 return -1;
961         }
962
963         boost::shared_ptr<Processor> redi=r->nth_processor (piid);
964         
965         if (!redi) {
966                 return -1;
967         }
968
969         boost::shared_ptr<PluginInsert> pi;
970         
971         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
972                 return -1;
973         }
974
975         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
976         bool ok=false;
977         
978         uint32_t controlid = pip->nth_parameter (par,ok);
979         
980         if (!ok) {
981                 return -1;
982         }
983
984         Plugin::ParameterDescriptor pd;
985         pi->plugin()->get_parameter_descriptor (controlid,pd);
986
987         if (val >= pd.lower && val < pd.upper) {
988                 
989                 boost::shared_ptr<AutomationControl> c = pi->automation_control (Evoral::Parameter(PluginAutomation, 0, controlid));;
990                 cerr << "parameter:" << redi->describe_parameter(controlid) << " val:" << val << "\n";
991                 c->set_value (val);
992         }  
993
994         return 0;
995 }
996
997 int
998 OSC::route_plugin_parameter_print (int rid,int piid,int par)
999 {
1000         if (!session) { 
1001                 return -1;
1002         }
1003
1004         boost::shared_ptr<Route> r = session->route_by_remote_id (rid);  
1005
1006         if (!r) {
1007                 return -1;
1008         }
1009
1010         boost::shared_ptr<Processor> redi=r->nth_processor (piid);
1011         
1012         if (!redi) {
1013                 return -1;
1014         }
1015
1016         boost::shared_ptr<PluginInsert> pi;
1017         
1018         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
1019                 return -1;
1020         }
1021         boost::shared_ptr<ARDOUR::Plugin> pip=pi->plugin();
1022         bool ok=false;
1023         
1024         uint32_t controlid = pip->nth_parameter (par,ok);
1025         
1026         if (!ok) {
1027                 return -1;
1028         }
1029
1030         Plugin::ParameterDescriptor pd;
1031
1032         if (pi->plugin()->get_parameter_descriptor (controlid, pd) == 0) {
1033                 boost::shared_ptr<AutomationControl> c = pi->automation_control (Evoral::Parameter(PluginAutomation, 0, controlid));
1034                 
1035                 cerr << "parameter:     " << redi->describe_parameter(controlid)  << "\n";
1036                 cerr << "current value: " << c->get_value ();
1037                 cerr << "lower value:   " << pd.lower << "\n";
1038                 cerr << "upper value:   " << pd.upper << "\n";
1039         }
1040
1041         return 0;
1042 }
1043
1044 XMLNode& 
1045 OSC::get_state () 
1046 {
1047         return *(new XMLNode ("OSC"));
1048 }
1049                 
1050 int 
1051 OSC::set_state (const XMLNode&, int /*version*/)
1052 {
1053         return 0;
1054 }