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