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