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