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