2 * Copyright (C) 2006 Paul Davis
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.
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.
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.
31 #include <glibmm/miscutils.h>
33 #include <pbd/pthread_utils.h>
34 #include <pbd/file_utils.h>
35 #include <pbd/filesystem.h>
36 #include <pbd/failed_constructor.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"
46 #include "osc_controllable.h"
49 using namespace ARDOUR;
55 static void error_callback(int num, const char *m, const char *path)
57 fprintf(stderr, "liblo server error %d in path %s: %s\n", num, path, m);
60 static void error_callback(int, const char *, const char *)
66 OSC::OSC (Session& s, uint32_t port)
67 : ControlProtocol (s, "OSC")
74 _namespace_root = "/ardour";
75 _send_route_changes = true;
77 // "Application Hooks"
79 session->Exported.connect( mem_fun( *this, &OSC::session_exported ) );
81 /* catch up with existing routes */
83 boost::shared_ptr<RouteList> rl = session->get_routes ();
84 route_added (*(rl.get()));
86 // session->RouteAdded.connect (mem_fun (*this, &OSC::route_added));
95 OSC::set_active (bool yn)
105 OSC::get_active () const
107 return _osc_server != 0;
111 OSC::set_feedback (bool yn)
113 _send_route_changes = yn;
118 OSC::get_feedback () const
120 return _send_route_changes;
129 /* already started */
133 for (int j=0; j < 20; ++j) {
134 snprintf(tmpstr, sizeof(tmpstr), "%d", _port);
136 if ((_osc_server = lo_server_new (tmpstr, error_callback))) {
140 cerr << "can't get osc at port: " << _port << endl;
146 #ifdef ARDOUR_OSC_UNIX_SERVER
148 // APPEARS sluggish for now
150 // attempt to create unix socket server too
152 snprintf(tmpstr, sizeof(tmpstr), "/tmp/sooperlooper_XXXXXX");
153 int fd = mkstemp(tmpstr);
159 _osc_unix_server = lo_server_new (tmpstr, error_callback);
161 if (_osc_unix_server) {
162 _osc_unix_socket_path = tmpstr;
167 cerr << "OSC @ " << get_server_url () << endl;
169 PBD::sys::path url_file;
171 if (find_file_in_search_path (ardour_search_path() + system_config_search_path(),
172 "osc_url", url_file)) {
173 _osc_url_file = url_file.to_string();
175 urlfile.open(_osc_url_file.c_str(), ios::trunc);
178 urlfile << get_server_url () << endl;
183 cerr << "Couldn't write '" << _osc_url_file << "'" <<endl;
187 register_callbacks();
189 // lo_server_thread_add_method(_sthread, NULL, NULL, OSC::_dummy_handler, this);
191 if (!init_osc_thread()) {
200 if (_osc_server == 0) {
201 /* already stopped */
205 // stop server thread
206 terminate_osc_thread();
208 lo_server_free (_osc_server);
211 if (!_osc_unix_socket_path.empty()) {
213 unlink(_osc_unix_socket_path.c_str());
216 if (! _osc_url_file.empty() ) {
217 unlink(_osc_url_file.c_str() );
223 OSC::register_callbacks()
228 srvs[0] = _osc_server;
229 srvs[1] = _osc_unix_server;
231 for (size_t i = 0; i < 2; ++i) {
239 /* this is a special catchall handler */
241 lo_server_add_method (serv, 0, 0, _catchall, this);
243 #define REGISTER_CALLBACK(serv,path,types, function) lo_server_add_method (serv, path, types, OSC::_ ## function, this)
245 REGISTER_CALLBACK (serv, "/ardour/add_marker", "", add_marker);
246 REGISTER_CALLBACK (serv, "/ardour/access_action", "s", access_action);
247 REGISTER_CALLBACK (serv, "/ardour/loop_toggle", "", loop_toggle);
248 REGISTER_CALLBACK (serv, "/ardour/goto_start", "", goto_start);
249 REGISTER_CALLBACK (serv, "/ardour/goto_end", "", goto_end);
250 REGISTER_CALLBACK (serv, "/ardour/rewind", "", rewind);
251 REGISTER_CALLBACK (serv, "/ardour/ffwd", "", ffwd);
252 REGISTER_CALLBACK (serv, "/ardour/transport_stop", "", transport_stop);
253 REGISTER_CALLBACK (serv, "/ardour/transport_play", "", transport_play);
254 REGISTER_CALLBACK (serv, "/ardour/set_transport_speed", "f", set_transport_speed);
255 REGISTER_CALLBACK (serv, "/ardour/save_state", "", save_state);
256 REGISTER_CALLBACK (serv, "/ardour/prev_marker", "", prev_marker);
257 REGISTER_CALLBACK (serv, "/ardour/next_marker", "", next_marker);
258 REGISTER_CALLBACK (serv, "/ardour/undo", "", undo);
259 REGISTER_CALLBACK (serv, "/ardour/redo", "", redo);
260 REGISTER_CALLBACK (serv, "/ardour/toggle_punch_in", "", toggle_punch_in);
261 REGISTER_CALLBACK (serv, "/ardour/toggle_punch_out", "", toggle_punch_out);
262 REGISTER_CALLBACK (serv, "/ardour/rec_enable_toggle", "", rec_enable_toggle);
263 REGISTER_CALLBACK (serv, "/ardour/toggle_all_rec_enables", "", toggle_all_rec_enables);
265 REGISTER_CALLBACK (serv, "/ardour/routes/mute", "ii", route_mute);
266 REGISTER_CALLBACK (serv, "/ardour/routes/solo", "ii", route_solo);
267 REGISTER_CALLBACK (serv, "/ardour/routes/recenable", "ii", route_recenable);
268 REGISTER_CALLBACK (serv, "/ardour/routes/gainabs", "if", route_set_gain_abs);
269 REGISTER_CALLBACK (serv, "/ardour/routes/gaindB", "if", route_set_gain_dB);
272 REGISTER_CALLBACK (serv, "/ardour/*/#current_value", "", current_value);
273 REGISTER_CALLBACK (serv, "/ardour/set", "", set);
277 // un/register_update args= s:ctrl s:returl s:retpath
278 lo_server_add_method(serv, "/register_update", "sss", OSC::global_register_update_handler, this);
279 lo_server_add_method(serv, "/unregister_update", "sss", OSC::global_unregister_update_handler, this);
280 lo_server_add_method(serv, "/register_auto_update", "siss", OSC::global_register_auto_update_handler, this);
281 lo_server_add_method(serv, "/unregister_auto_update", "sss", OSC::_global_unregister_auto_update_handler, this);
287 OSC::init_osc_thread ()
289 // create new thread to run server
290 if (pipe (_request_pipe)) {
291 cerr << "Cannot create osc request signal pipe" << strerror (errno) << endl;
295 if (fcntl (_request_pipe[0], F_SETFL, O_NONBLOCK)) {
296 cerr << "osc: cannot set O_NONBLOCK on signal read pipe " << strerror (errno) << endl;
300 if (fcntl (_request_pipe[1], F_SETFL, O_NONBLOCK)) {
301 cerr << "osc: cannot set O_NONBLOCK on signal write pipe " << strerror (errno) << endl;
306 pthread_attr_init(&attr);
307 pthread_attr_setstacksize(&attr, 500000);
309 pthread_create_and_store (X_("OSC"), &_osc_thread, &attr, &OSC::_osc_receiver, this);
313 pthread_attr_destroy(&attr);
315 //pthread_detach (_osc_thread);
320 OSC::terminate_osc_thread ()
328 pthread_join (_osc_thread, &status);
332 OSC::poke_osc_thread ()
336 if (write (_request_pipe[1], &c, 1) != 1) {
337 cerr << "cannot send signal to osc thread! " << strerror (errno) << endl;
342 OSC::get_server_url()
348 urlstr = lo_server_get_url (_osc_server);
357 OSC::get_unix_server_url()
362 if (_osc_unix_server) {
363 urlstr = lo_server_get_url (_osc_unix_server);
375 OSC::_osc_receiver(void * arg)
377 static_cast<OSC*>(arg)->register_thread (X_("OSC"));
378 static_cast<OSC*>(arg)->osc_receiver();
385 struct pollfd pfd[3];
392 fds[0] = _request_pipe[0];
395 if (_osc_server && lo_server_get_socket_fd(_osc_server) >= 0) {
396 fds[nfds] = lo_server_get_socket_fd(_osc_server);
397 srvs[nfds] = _osc_server;
401 if (_osc_unix_server && lo_server_get_socket_fd(_osc_unix_server) >= 0) {
402 fds[nfds] = lo_server_get_socket_fd(_osc_unix_server);
403 srvs[nfds] = _osc_unix_server;
410 for (int i=0; i < nfds; ++i) {
412 pfd[i].events = POLLIN|POLLPRI|POLLHUP|POLLERR;
417 //cerr << "poll on " << nfds << " for " << timeout << endl;
418 if ((ret = poll (pfd, nfds, timeout)) < 0) {
419 if (errno == EINTR) {
420 /* gdb at work, perhaps */
424 cerr << "OSC thread poll failed: " << strerror (errno) << endl;
429 //cerr << "poll returned " << ret << " pfd[0].revents = " << pfd[0].revents << " pfd[1].revents = " << pfd[1].revents << endl;
435 if ((pfd[0].revents & ~POLLIN)) {
436 cerr << "OSC: error polling extra port" << endl;
440 for (int i=1; i < nfds; ++i) {
441 if (pfd[i].revents & POLLIN)
443 // this invokes callbacks
444 // cerr << "invoking recv on " << pfd[i].fd << endl;
445 lo_server_recv(srvs[i]);
451 //cerr << "SL engine shutdown" << endl;
454 int fd = lo_server_get_socket_fd(_osc_server);
459 lo_server_free (_osc_server);
463 if (_osc_unix_server) {
464 cerr << "freeing unix server" << endl;
465 lo_server_free (_osc_unix_server);
466 _osc_unix_server = 0;
469 close(_request_pipe[0]);
470 close(_request_pipe[1]);
474 OSC::current_value_query (const char* path, size_t len, lo_arg **argv, int argc, lo_message msg)
478 subpath = (char*) malloc (len-15+1);
479 memcpy (subpath, path, len-15);
480 subpath[len-15] = '\0';
482 send_current_value (subpath, argv, argc, msg);
488 OSC::send_current_value (const char* path, lo_arg** argv, int argc, lo_message msg)
494 lo_message reply = lo_message_new ();
495 boost::shared_ptr<Route> r;
498 lo_message_add_string (reply, path);
501 lo_message_add_string (reply, "bad syntax");
504 r = session->route_by_remote_id (id);
507 lo_message_add_string (reply, "not found");
510 if (strcmp (path, "/routes/state") == 0) {
512 if (boost::dynamic_pointer_cast<AudioTrack>(r)) {
513 lo_message_add_string (reply, "AT");
514 } else if (boost::dynamic_pointer_cast<MidiTrack>(r)) {
515 lo_message_add_string (reply, "MT");
517 lo_message_add_string (reply, "B");
520 lo_message_add_string (reply, r->name().c_str());
521 lo_message_add_int32 (reply, r->n_inputs().n_audio());
522 lo_message_add_int32 (reply, r->n_outputs().n_audio());
523 lo_message_add_int32 (reply, r->muted());
524 lo_message_add_int32 (reply, r->soloed());
526 } else if (strcmp (path, "/routes/mute") == 0) {
528 lo_message_add_int32 (reply, (float) r->muted());
530 } else if (strcmp (path, "/routes/solo") == 0) {
532 lo_message_add_int32 (reply, r->soloed());
537 lo_send_message (lo_message_get_source (msg), "#reply", reply);
538 lo_message_free (reply);
542 OSC::_catchall (const char *path, const char *types, lo_arg **argv, int argc, void *data, void *user_data)
544 return ((OSC*)user_data)->catchall (path, types, argv, argc, data);
548 OSC::catchall (const char *path, const char *types, lo_arg **argv, int argc, lo_message msg)
551 int ret = 1; /* unhandled */
553 cerr << "Received a message, path = " << path << " types = \""
554 << (types ? types : "NULL") << '"' << endl;
556 /* 15 for /#current_value plus 2 for /<path> */
560 if (len >= 17 && !strcmp (&path[len-15], "/#current_value")) {
561 current_value_query (path, len, argv, argc, msg);
564 } else if (strcmp (path, "/routes/listen") == 0) {
566 cerr << "set up listener\n";
568 lo_message reply = lo_message_new ();
571 lo_message_add_string (reply, "syntax error");
573 for (int n = 0; n < argc; ++n) {
575 boost::shared_ptr<Route> r = session->route_by_remote_id (argv[n]->i);
578 lo_message_add_string (reply, "not found");
579 cerr << "no such route\n";
582 cerr << "add listener\n";
583 listen_to_route (r, lo_message_get_source (msg));
584 lo_message_add_int32 (reply, argv[n]->i);
589 lo_send_message (lo_message_get_source (msg), "#reply", reply);
590 lo_message_free (reply);
592 } else if (strcmp (path, "/routes/ignore") == 0) {
594 for (int n = 0; n < argc; ++n) {
596 boost::shared_ptr<Route> r = session->route_by_remote_id (argv[n]->i);
599 end_listen (r, lo_message_get_source (msg));
608 OSC::route_added (RouteList&)
613 OSC::listen_to_route (boost::shared_ptr<Route> route, lo_address addr)
615 Controllables::iterator x;
616 bool route_exists = false;
618 cerr << "listen to route\n";
620 /* avoid duplicate listens */
622 for (x = controllables.begin(); x != controllables.end(); ++x) {
624 OSCRouteControllable* rc;
626 if ((rc = dynamic_cast<OSCRouteControllable*>(*x)) != 0) {
628 if (rc->route() == route) {
631 /* XXX NEED lo_address_equal() */
633 if (rc->address() == addr) {
640 cerr << "listener binding to signals\n";
645 path = X_("/route/solo");
646 c = new OSCRouteControllable (addr, path, route->solo_control(), route);
647 controllables.push_back (c);
649 path = X_("/route/mute");
650 c = new OSCRouteControllable (addr, path, route->mute_control(), route);
651 controllables.push_back (c);
653 path = X_("/route/gain");
654 c = new OSCRouteControllable (addr, path, route->gain_control(), route);
655 controllables.push_back (c);
657 cerr << "Now have " << controllables.size() << " controllables\n";
659 /* if there is no existing controllable related to this route, make sure we clean up
660 if it is ever deleted.
664 route->GoingAway.connect (bind (mem_fun (*this, &OSC::drop_route), boost::weak_ptr<Route> (route)));
669 OSC::drop_route (boost::weak_ptr<Route> wr)
671 boost::shared_ptr<Route> r = wr.lock ();
677 for (Controllables::iterator x = controllables.begin(); x != controllables.end();) {
679 OSCRouteControllable* rc;
681 if ((rc = dynamic_cast<OSCRouteControllable*>(*x)) != 0) {
682 if (rc->route() == r) {
684 x = controllables.erase (x);
695 OSC::end_listen (boost::shared_ptr<Route> r, lo_address addr)
697 Controllables::iterator x;
699 for (x = controllables.begin(); x != controllables.end(); ++x) {
701 OSCRouteControllable* rc;
703 if ((rc = dynamic_cast<OSCRouteControllable*>(*x)) != 0) {
705 /* XXX NEED lo_address_equal () */
707 if (rc->route() == r && rc->address() == addr) {
708 controllables.erase (x);
715 // "Application Hook" Handlers //
717 OSC::session_loaded( Session& s ) {
718 lo_address listener = lo_address_new( NULL, "7770" );
719 lo_send( listener, "/session/loaded", "ss", s.path().c_str(), s.name().c_str() );
723 OSC::session_exported( std::string path, std::string name ) {
724 lo_address listener = lo_address_new( NULL, "7770" );
725 lo_send( listener, "/session/exported", "ss", path.c_str(), name.c_str() );
728 // end "Application Hook" Handlers //
733 OSC::current_value (const char */*path*/, const char */*types*/, lo_arg **/*argv*/, int /*argc*/, void */*data*/, void* /*user_data*/)
738 if (argc < 3 || types == 0 || strlen (types) < 3 || types[0] != 's' || types[1] != 's' || types[2] != s) {
742 const char *returl = argv[1]->s;
743 lo_address addr = find_or_cache_addr (returl);
745 const char *retpath = argv[2]->s;
748 if (strcmp (argv[0]->s, "transport_frame") == 0) {
751 lo_send (addr, retpath, "i", session->transport_frame());
754 } else if (strcmp (argv[0]->s, "transport_speed") == 0) {
757 lo_send (addr, retpath, "i", session->transport_frame());
760 } else if (strcmp (argv[0]->s, "transport_locked") == 0) {
763 lo_send (addr, retpath, "i", session->transport_frame());
766 } else if (strcmp (argv[0]->s, "punch_in") == 0) {
769 lo_send (addr, retpath, "i", session->transport_frame());
772 } else if (strcmp (argv[0]->s, "punch_out") == 0) {
775 lo_send (addr, retpath, "i", session->transport_frame());
778 } else if (strcmp (argv[0]->s, "rec_enable") == 0) {
781 lo_send (addr, retpath, "i", session->transport_frame());
793 OSC::route_mute (int rid, int yn)
795 if (!session) return -1;
797 boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
800 r->set_mute (yn, this);
806 OSC::route_solo (int rid, int yn)
808 if (!session) return -1;
810 boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
813 r->set_solo (yn, this);
819 OSC::route_recenable (int rid, int yn)
821 if (!session) return -1;
823 boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
826 r->set_record_enable (yn, this);
832 OSC::route_set_gain_abs (int rid, float level)
834 if (!session) return -1;
836 boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
839 r->set_gain (level, this);
846 OSC::route_set_gain_dB (int rid, float dB)
848 if (!session) return -1;
850 boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
853 r->set_gain (dB_to_coefficient (dB), this);
862 return *(new XMLNode ("OSC"));
866 OSC::set_state (const XMLNode&, int /*version*/)