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