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;
54 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);
61 OSC::OSC (Session& s, uint32_t port)
62 : ControlProtocol (s, "OSC")
69 _namespace_root = "/ardour";
70 _send_route_changes = true;
72 // "Application Hooks"
74 session->Exported.connect( mem_fun( *this, &OSC::session_exported ) );
76 /* catch up with existing routes */
78 boost::shared_ptr<RouteList> rl = session->get_routes ();
79 route_added (*(rl.get()));
81 // session->RouteAdded.connect (mem_fun (*this, &OSC::route_added));
90 OSC::set_active (bool yn)
100 OSC::get_active () const
102 return _osc_server != 0;
106 OSC::set_feedback (bool yn)
108 _send_route_changes = yn;
113 OSC::get_feedback () const
115 return _send_route_changes;
124 /* already started */
128 for (int j=0; j < 20; ++j) {
129 snprintf(tmpstr, sizeof(tmpstr), "%d", _port);
131 if ((_osc_server = lo_server_new (tmpstr, error_callback))) {
135 cerr << "can't get osc at port: " << _port << endl;
141 #ifdef ARDOUR_OSC_UNIX_SERVER
143 // APPEARS sluggish for now
145 // attempt to create unix socket server too
147 snprintf(tmpstr, sizeof(tmpstr), "/tmp/sooperlooper_XXXXXX");
148 int fd = mkstemp(tmpstr);
154 _osc_unix_server = lo_server_new (tmpstr, error_callback);
156 if (_osc_unix_server) {
157 _osc_unix_socket_path = tmpstr;
162 cerr << "OSC @ " << get_server_url () << endl;
164 PBD::sys::path url_file;
166 if (find_file_in_search_path (ardour_search_path() + system_config_search_path(),
167 "osc_url", url_file)) {
168 _osc_url_file = url_file.to_string();
170 urlfile.open(_osc_url_file.c_str(), ios::trunc);
173 urlfile << get_server_url () << endl;
178 cerr << "Couldn't write '" << _osc_url_file << "'" <<endl;
182 register_callbacks();
184 // lo_server_thread_add_method(_sthread, NULL, NULL, OSC::_dummy_handler, this);
186 if (!init_osc_thread()) {
195 if (_osc_server == 0) {
196 /* already stopped */
200 // stop server thread
201 terminate_osc_thread();
203 lo_server_free (_osc_server);
206 if (!_osc_unix_socket_path.empty()) {
208 unlink(_osc_unix_socket_path.c_str());
211 if (! _osc_url_file.empty() ) {
212 unlink(_osc_url_file.c_str() );
218 OSC::register_callbacks()
223 srvs[0] = _osc_server;
224 srvs[1] = _osc_unix_server;
226 for (size_t i = 0; i < 2; ++i) {
234 /* this is a special catchall handler */
236 lo_server_add_method (serv, 0, 0, _catchall, this);
238 #define REGISTER_CALLBACK(serv,path,types, function) lo_server_add_method (serv, path, types, OSC::_ ## function, this)
240 REGISTER_CALLBACK (serv, "/ardour/add_marker", "", add_marker);
241 REGISTER_CALLBACK (serv, "/ardour/access_action", "s", access_action);
242 REGISTER_CALLBACK (serv, "/ardour/loop_toggle", "", loop_toggle);
243 REGISTER_CALLBACK (serv, "/ardour/goto_start", "", goto_start);
244 REGISTER_CALLBACK (serv, "/ardour/goto_end", "", goto_end);
245 REGISTER_CALLBACK (serv, "/ardour/rewind", "", rewind);
246 REGISTER_CALLBACK (serv, "/ardour/ffwd", "", ffwd);
247 REGISTER_CALLBACK (serv, "/ardour/transport_stop", "", transport_stop);
248 REGISTER_CALLBACK (serv, "/ardour/transport_play", "", transport_play);
249 REGISTER_CALLBACK (serv, "/ardour/set_transport_speed", "f", set_transport_speed);
250 REGISTER_CALLBACK (serv, "/ardour/save_state", "", save_state);
251 REGISTER_CALLBACK (serv, "/ardour/prev_marker", "", prev_marker);
252 REGISTER_CALLBACK (serv, "/ardour/next_marker", "", next_marker);
253 REGISTER_CALLBACK (serv, "/ardour/undo", "", undo);
254 REGISTER_CALLBACK (serv, "/ardour/redo", "", redo);
255 REGISTER_CALLBACK (serv, "/ardour/toggle_punch_in", "", toggle_punch_in);
256 REGISTER_CALLBACK (serv, "/ardour/toggle_punch_out", "", toggle_punch_out);
257 REGISTER_CALLBACK (serv, "/ardour/rec_enable_toggle", "", rec_enable_toggle);
258 REGISTER_CALLBACK (serv, "/ardour/toggle_all_rec_enables", "", toggle_all_rec_enables);
260 REGISTER_CALLBACK (serv, "/ardour/routes/mute", "ii", route_mute);
261 REGISTER_CALLBACK (serv, "/ardour/routes/solo", "ii", route_solo);
262 REGISTER_CALLBACK (serv, "/ardour/routes/recenable", "ii", route_recenable);
263 REGISTER_CALLBACK (serv, "/ardour/routes/gainabs", "if", route_set_gain_abs);
264 REGISTER_CALLBACK (serv, "/ardour/routes/gaindB", "if", route_set_gain_dB);
267 REGISTER_CALLBACK (serv, "/ardour/*/#current_value", "", current_value);
268 REGISTER_CALLBACK (serv, "/ardour/set", "", set);
272 // un/register_update args= s:ctrl s:returl s:retpath
273 lo_server_add_method(serv, "/register_update", "sss", OSC::global_register_update_handler, this);
274 lo_server_add_method(serv, "/unregister_update", "sss", OSC::global_unregister_update_handler, this);
275 lo_server_add_method(serv, "/register_auto_update", "siss", OSC::global_register_auto_update_handler, this);
276 lo_server_add_method(serv, "/unregister_auto_update", "sss", OSC::_global_unregister_auto_update_handler, this);
282 OSC::init_osc_thread ()
284 // create new thread to run server
285 if (pipe (_request_pipe)) {
286 cerr << "Cannot create osc request signal pipe" << strerror (errno) << endl;
290 if (fcntl (_request_pipe[0], F_SETFL, O_NONBLOCK)) {
291 cerr << "osc: cannot set O_NONBLOCK on signal read pipe " << strerror (errno) << endl;
295 if (fcntl (_request_pipe[1], F_SETFL, O_NONBLOCK)) {
296 cerr << "osc: cannot set O_NONBLOCK on signal write pipe " << strerror (errno) << endl;
301 pthread_attr_init(&attr);
302 pthread_attr_setstacksize(&attr, 500000);
304 pthread_create (&_osc_thread, &attr, &OSC::_osc_receiver, this);
308 pthread_attr_destroy(&attr);
310 //pthread_detach (_osc_thread);
315 OSC::terminate_osc_thread ()
323 pthread_join (_osc_thread, &status);
327 OSC::poke_osc_thread ()
331 if (write (_request_pipe[1], &c, 1) != 1) {
332 cerr << "cannot send signal to osc thread! " << strerror (errno) << endl;
337 OSC::get_server_url()
343 urlstr = lo_server_get_url (_osc_server);
352 OSC::get_unix_server_url()
357 if (_osc_unix_server) {
358 urlstr = lo_server_get_url (_osc_unix_server);
370 OSC::_osc_receiver(void * arg)
372 PBD::notify_gui_about_thread_creation (pthread_self(), X_("OSC"));
374 static_cast<OSC*> (arg)->osc_receiver();
381 struct pollfd pfd[3];
388 fds[0] = _request_pipe[0];
391 if (_osc_server && lo_server_get_socket_fd(_osc_server) >= 0) {
392 fds[nfds] = lo_server_get_socket_fd(_osc_server);
393 srvs[nfds] = _osc_server;
397 if (_osc_unix_server && lo_server_get_socket_fd(_osc_unix_server) >= 0) {
398 fds[nfds] = lo_server_get_socket_fd(_osc_unix_server);
399 srvs[nfds] = _osc_unix_server;
406 for (int i=0; i < nfds; ++i) {
408 pfd[i].events = POLLIN|POLLPRI|POLLHUP|POLLERR;
413 //cerr << "poll on " << nfds << " for " << timeout << endl;
414 if ((ret = poll (pfd, nfds, timeout)) < 0) {
415 if (errno == EINTR) {
416 /* gdb at work, perhaps */
420 cerr << "OSC thread poll failed: " << strerror (errno) << endl;
425 //cerr << "poll returned " << ret << " pfd[0].revents = " << pfd[0].revents << " pfd[1].revents = " << pfd[1].revents << endl;
431 if ((pfd[0].revents & ~POLLIN)) {
432 cerr << "OSC: error polling extra port" << endl;
436 for (int i=1; i < nfds; ++i) {
437 if (pfd[i].revents & POLLIN)
439 // this invokes callbacks
440 // cerr << "invoking recv on " << pfd[i].fd << endl;
441 lo_server_recv(srvs[i]);
447 //cerr << "SL engine shutdown" << endl;
450 int fd = lo_server_get_socket_fd(_osc_server);
455 lo_server_free (_osc_server);
459 if (_osc_unix_server) {
460 cerr << "freeing unix server" << endl;
461 lo_server_free (_osc_unix_server);
462 _osc_unix_server = 0;
465 close(_request_pipe[0]);
466 close(_request_pipe[1]);
470 OSC::current_value_query (const char* path, size_t len, lo_arg **argv, int argc, lo_message msg)
474 subpath = (char*) malloc (len-15+1);
475 memcpy (subpath, path, len-15);
476 subpath[len-15] = '\0';
478 send_current_value (subpath, argv, argc, msg);
484 OSC::send_current_value (const char* path, lo_arg** argv, int argc, lo_message msg)
490 lo_message reply = lo_message_new ();
491 boost::shared_ptr<Route> r;
494 lo_message_add_string (reply, path);
497 lo_message_add_string (reply, "bad syntax");
500 r = session->route_by_remote_id (id);
503 lo_message_add_string (reply, "not found");
506 if (strcmp (path, "/routes/state") == 0) {
508 if (boost::dynamic_pointer_cast<AudioTrack>(r)) {
509 lo_message_add_string (reply, "AT");
510 } else if (boost::dynamic_pointer_cast<MidiTrack>(r)) {
511 lo_message_add_string (reply, "MT");
513 lo_message_add_string (reply, "B");
516 lo_message_add_string (reply, r->name().c_str());
517 lo_message_add_int32 (reply, r->n_inputs().n_audio());
518 lo_message_add_int32 (reply, r->n_outputs().n_audio());
519 lo_message_add_int32 (reply, r->muted());
520 lo_message_add_int32 (reply, r->soloed());
522 } else if (strcmp (path, "/routes/mute") == 0) {
524 lo_message_add_int32 (reply, (float) r->muted());
526 } else if (strcmp (path, "/routes/solo") == 0) {
528 lo_message_add_int32 (reply, r->soloed());
533 lo_send_message (lo_message_get_source (msg), "#reply", reply);
534 lo_message_free (reply);
538 OSC::_catchall (const char *path, const char *types, lo_arg **argv, int argc, void *data, void *user_data)
540 return ((OSC*)user_data)->catchall (path, types, argv, argc, data);
544 OSC::catchall (const char *path, const char *types, lo_arg **argv, int argc, lo_message msg)
547 int ret = 1; /* unhandled */
549 cerr << "Received a message, path = " << path << " types = \""
550 << (types ? types : "NULL") << '"' << endl;
552 /* 15 for /#current_value plus 2 for /<path> */
556 if (len >= 17 && !strcmp (&path[len-15], "/#current_value")) {
557 current_value_query (path, len, argv, argc, msg);
560 } else if (strcmp (path, "/routes/listen") == 0) {
562 cerr << "set up listener\n";
564 lo_message reply = lo_message_new ();
567 lo_message_add_string (reply, "syntax error");
569 for (int n = 0; n < argc; ++n) {
571 boost::shared_ptr<Route> r = session->route_by_remote_id (argv[n]->i);
574 lo_message_add_string (reply, "not found");
575 cerr << "no such route\n";
578 cerr << "add listener\n";
579 listen_to_route (r, lo_message_get_source (msg));
580 lo_message_add_int32 (reply, argv[n]->i);
585 lo_send_message (lo_message_get_source (msg), "#reply", reply);
586 lo_message_free (reply);
588 } else if (strcmp (path, "/routes/ignore") == 0) {
590 for (int n = 0; n < argc; ++n) {
592 boost::shared_ptr<Route> r = session->route_by_remote_id (argv[n]->i);
595 end_listen (r, lo_message_get_source (msg));
604 OSC::route_added (RouteList& rl)
609 OSC::listen_to_route (boost::shared_ptr<Route> route, lo_address addr)
611 Controllables::iterator x;
612 bool route_exists = false;
614 cerr << "listen to route\n";
616 /* avoid duplicate listens */
618 for (x = controllables.begin(); x != controllables.end(); ++x) {
620 OSCRouteControllable* rc;
622 if ((rc = dynamic_cast<OSCRouteControllable*>(*x)) != 0) {
624 if (rc->route() == route) {
627 /* XXX NEED lo_address_equal() */
629 if (rc->address() == addr) {
636 cerr << "listener binding to signals\n";
641 path = X_("/route/solo");
642 c = new OSCRouteControllable (addr, path, route->solo_control(), route);
643 controllables.push_back (c);
645 path = X_("/route/mute");
646 c = new OSCRouteControllable (addr, path, route->mute_control(), route);
647 controllables.push_back (c);
649 path = X_("/route/gain");
650 c = new OSCRouteControllable (addr, path, route->gain_control(), route);
651 controllables.push_back (c);
653 cerr << "Now have " << controllables.size() << " controllables\n";
655 /* if there is no existing controllable related to this route, make sure we clean up
656 if it is ever deleted.
660 route->GoingAway.connect (bind (mem_fun (*this, &OSC::drop_route), boost::weak_ptr<Route> (route)));
665 OSC::drop_route (boost::weak_ptr<Route> wr)
667 boost::shared_ptr<Route> r = wr.lock ();
673 for (Controllables::iterator x = controllables.begin(); x != controllables.end();) {
675 OSCRouteControllable* rc;
677 if ((rc = dynamic_cast<OSCRouteControllable*>(*x)) != 0) {
678 if (rc->route() == r) {
680 x = controllables.erase (x);
691 OSC::end_listen (boost::shared_ptr<Route> r, lo_address addr)
693 Controllables::iterator x;
695 for (x = controllables.begin(); x != controllables.end(); ++x) {
697 OSCRouteControllable* rc;
699 if ((rc = dynamic_cast<OSCRouteControllable*>(*x)) != 0) {
701 /* XXX NEED lo_address_equal () */
703 if (rc->route() == r && rc->address() == addr) {
704 controllables.erase (x);
711 // "Application Hook" Handlers //
713 OSC::session_loaded( Session& s ) {
714 lo_address listener = lo_address_new( NULL, "7770" );
715 lo_send( listener, "/session/loaded", "ss", s.path().c_str(), s.name().c_str() );
719 OSC::session_exported( std::string path, std::string name ) {
720 lo_address listener = lo_address_new( NULL, "7770" );
721 lo_send( listener, "/session/exported", "ss", path.c_str(), name.c_str() );
724 // end "Application Hook" Handlers //
729 OSC::current_value (const char *path, const char *types, lo_arg **argv, int argc, void *data, void* user_data)
734 if (argc < 3 || types == 0 || strlen (types) < 3 || types[0] != 's' || types[1] != 's' || types[2] != s) {
738 const char *returl = argv[1]->s;
739 lo_address addr = find_or_cache_addr (returl);
741 const char *retpath = argv[2]->s;
744 if (strcmp (argv[0]->s, "transport_frame") == 0) {
747 lo_send (addr, retpath, "i", session->transport_frame());
750 } else if (strcmp (argv[0]->s, "transport_speed") == 0) {
753 lo_send (addr, retpath, "i", session->transport_frame());
756 } else if (strcmp (argv[0]->s, "transport_locked") == 0) {
759 lo_send (addr, retpath, "i", session->transport_frame());
762 } else if (strcmp (argv[0]->s, "punch_in") == 0) {
765 lo_send (addr, retpath, "i", session->transport_frame());
768 } else if (strcmp (argv[0]->s, "punch_out") == 0) {
771 lo_send (addr, retpath, "i", session->transport_frame());
774 } else if (strcmp (argv[0]->s, "rec_enable") == 0) {
777 lo_send (addr, retpath, "i", session->transport_frame());
789 OSC::route_mute (int rid, int yn)
791 if (!session) return -1;
793 boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
796 r->set_mute (yn, this);
802 OSC::route_solo (int rid, int yn)
804 if (!session) return -1;
806 boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
809 r->set_solo (yn, this);
815 OSC::route_recenable (int rid, int yn)
817 if (!session) return -1;
819 boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
822 r->set_record_enable (yn, this);
828 OSC::route_set_gain_abs (int rid, float level)
830 if (!session) return -1;
832 boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
835 r->set_gain (level, this);
842 OSC::route_set_gain_dB (int rid, float dB)
844 if (!session) return -1;
846 boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
849 r->set_gain (dB_to_coefficient (dB), this);
858 return *(new XMLNode ("OSC"));
862 OSC::set_state (const XMLNode&)