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>
37 #include <ardour/osc.h>
38 #include <ardour/session.h>
39 #include <ardour/route.h>
40 #include <ardour/audio_track.h>
41 #include <ardour/dB.h>
42 #include <ardour/filesystem_paths.h>
46 using namespace ARDOUR;
50 static void error_callback(int num, const char *m, const char *path)
53 fprintf(stderr, "liblo server error %d in path %s: %s\n", num, path, m);
57 OSC::OSC (uint32_t port)
76 for (int j=0; j < 20; ++j) {
77 snprintf(tmpstr, sizeof(tmpstr), "%d", _port);
79 if ((_osc_server = lo_server_new (tmpstr, error_callback))) {
83 cerr << "can't get osc at port: " << _port << endl;
89 #ifdef ARDOUR_OSC_UNIX_SERVER
91 // APPEARS sluggish for now
93 // attempt to create unix socket server too
95 snprintf(tmpstr, sizeof(tmpstr), "/tmp/sooperlooper_XXXXXX");
96 int fd = mkstemp(tmpstr);
102 _osc_unix_server = lo_server_new (tmpstr, error_callback);
104 if (_osc_unix_server) {
105 _osc_unix_socket_path = tmpstr;
110 cerr << "OSC @ " << get_server_url () << endl;
112 PBD::sys::path url_file;
114 if (find_file_in_search_path (ardour_search_path() + system_config_search_path(),
115 "osc_url", url_file)) {
116 _osc_url_file = url_file.to_string();
118 urlfile.open(_osc_url_file.c_str(), ios::trunc);
121 urlfile << get_server_url () << endl;
126 cerr << "Couldn't write '" << _osc_url_file << "'" <<endl;
130 register_callbacks();
132 // lo_server_thread_add_method(_sthread, NULL, NULL, OSC::_dummy_handler, this);
134 if (!init_osc_thread()) {
143 if (_osc_server == 0) {
144 /* already stopped */
148 // stop server thread
149 terminate_osc_thread();
151 lo_server_free (_osc_server);
154 if (!_osc_unix_socket_path.empty()) {
156 unlink(_osc_unix_socket_path.c_str());
159 if (! _osc_url_file.empty() ) {
160 unlink(_osc_url_file.c_str() );
171 OSC::register_callbacks()
176 srvs[0] = _osc_server;
177 srvs[1] = _osc_unix_server;
179 for (size_t i = 0; i < 2; ++i) {
187 #define REGISTER_CALLBACK(serv,path,types, function) lo_server_add_method (serv, path, types, OSC::_ ## function, this)
189 REGISTER_CALLBACK (serv, "/ardour/add_marker", "", add_marker);
190 REGISTER_CALLBACK (serv, "/ardour/access_action", "s", access_action);
191 REGISTER_CALLBACK (serv, "/ardour/loop_toggle", "", loop_toggle);
192 REGISTER_CALLBACK (serv, "/ardour/goto_start", "", goto_start);
193 REGISTER_CALLBACK (serv, "/ardour/goto_end", "", goto_end);
194 REGISTER_CALLBACK (serv, "/ardour/rewind", "", rewind);
195 REGISTER_CALLBACK (serv, "/ardour/ffwd", "", ffwd);
196 REGISTER_CALLBACK (serv, "/ardour/transport_stop", "", transport_stop);
197 REGISTER_CALLBACK (serv, "/ardour/transport_play", "", transport_play);
198 REGISTER_CALLBACK (serv, "/ardour/set_transport_speed", "f", set_transport_speed);
199 REGISTER_CALLBACK (serv, "/ardour/save_state", "", save_state);
200 REGISTER_CALLBACK (serv, "/ardour/prev_marker", "", prev_marker);
201 REGISTER_CALLBACK (serv, "/ardour/next_marker", "", next_marker);
202 REGISTER_CALLBACK (serv, "/ardour/undo", "", undo);
203 REGISTER_CALLBACK (serv, "/ardour/redo", "", redo);
204 REGISTER_CALLBACK (serv, "/ardour/toggle_punch_in", "", toggle_punch_in);
205 REGISTER_CALLBACK (serv, "/ardour/toggle_punch_out", "", toggle_punch_out);
206 REGISTER_CALLBACK (serv, "/ardour/rec_enable_toggle", "", rec_enable_toggle);
207 REGISTER_CALLBACK (serv, "/ardour/toggle_all_rec_enables", "", toggle_all_rec_enables);
209 REGISTER_CALLBACK (serv, "/ardour/routes/mute", "ii", route_mute);
210 REGISTER_CALLBACK (serv, "/ardour/routes/solo", "ii", route_solo);
211 REGISTER_CALLBACK (serv, "/ardour/routes/recenable", "ii", route_recenable);
212 REGISTER_CALLBACK (serv, "/ardour/routes/gainabs", "if", route_set_gain_abs);
213 REGISTER_CALLBACK (serv, "/ardour/routes/gaindB", "if", route_set_gain_dB);
216 REGISTER_CALLBACK (serv, "/ardour/*/#current_value", "", current_value);
217 REGISTER_CALLBACK (serv, "/ardour/set", "", set);
221 // un/register_update args= s:ctrl s:returl s:retpath
222 lo_server_add_method(serv, "/register_update", "sss", OSC::global_register_update_handler, this);
223 lo_server_add_method(serv, "/unregister_update", "sss", OSC::global_unregister_update_handler, this);
224 lo_server_add_method(serv, "/register_auto_update", "siss", OSC::global_register_auto_update_handler, this);
225 lo_server_add_method(serv, "/unregister_auto_update", "sss", OSC::_global_unregister_auto_update_handler, this);
231 OSC::init_osc_thread ()
233 // create new thread to run server
234 if (pipe (_request_pipe)) {
235 cerr << "Cannot create osc request signal pipe" << strerror (errno) << endl;
239 if (fcntl (_request_pipe[0], F_SETFL, O_NONBLOCK)) {
240 cerr << "osc: cannot set O_NONBLOCK on signal read pipe " << strerror (errno) << endl;
244 if (fcntl (_request_pipe[1], F_SETFL, O_NONBLOCK)) {
245 cerr << "osc: cannot set O_NONBLOCK on signal write pipe " << strerror (errno) << endl;
250 pthread_attr_init(&attr);
251 pthread_attr_setstacksize(&attr, 500000);
253 pthread_create (&_osc_thread, &attr, &OSC::_osc_receiver, this);
257 pthread_attr_destroy(&attr);
259 //pthread_detach (_osc_thread);
264 OSC::terminate_osc_thread ()
272 pthread_join (_osc_thread, &status);
276 OSC::poke_osc_thread ()
280 if (write (_request_pipe[1], &c, 1) != 1) {
281 cerr << "cannot send signal to osc thread! " << strerror (errno) << endl;
286 OSC::get_server_url()
292 urlstr = lo_server_get_url (_osc_server);
301 OSC::get_unix_server_url()
306 if (_osc_unix_server) {
307 urlstr = lo_server_get_url (_osc_unix_server);
319 OSC::_osc_receiver(void * arg)
321 PBD::notify_gui_about_thread_creation (pthread_self(), X_("OSC"));
323 static_cast<OSC*> (arg)->osc_receiver();
330 struct pollfd pfd[3];
337 fds[0] = _request_pipe[0];
340 if (_osc_server && lo_server_get_socket_fd(_osc_server) >= 0) {
341 fds[nfds] = lo_server_get_socket_fd(_osc_server);
342 srvs[nfds] = _osc_server;
346 if (_osc_unix_server && lo_server_get_socket_fd(_osc_unix_server) >= 0) {
347 fds[nfds] = lo_server_get_socket_fd(_osc_unix_server);
348 srvs[nfds] = _osc_unix_server;
355 for (int i=0; i < nfds; ++i) {
357 pfd[i].events = POLLIN|POLLPRI|POLLHUP|POLLERR;
362 //cerr << "poll on " << nfds << " for " << timeout << endl;
363 if ((ret = poll (pfd, nfds, timeout)) < 0) {
364 if (errno == EINTR) {
365 /* gdb at work, perhaps */
369 cerr << "OSC thread poll failed: " << strerror (errno) << endl;
374 //cerr << "poll returned " << ret << " pfd[0].revents = " << pfd[0].revents << " pfd[1].revents = " << pfd[1].revents << endl;
380 if ((pfd[0].revents & ~POLLIN)) {
381 cerr << "OSC: error polling extra port" << endl;
385 for (int i=1; i < nfds; ++i) {
386 if (pfd[i].revents & POLLIN)
388 // this invokes callbacks
389 // cerr << "invoking recv on " << pfd[i].fd << endl;
390 lo_server_recv(srvs[i]);
396 //cerr << "SL engine shutdown" << endl;
399 int fd = lo_server_get_socket_fd(_osc_server);
404 lo_server_free (_osc_server);
408 if (_osc_unix_server) {
409 cerr << "freeing unix server" << endl;
410 lo_server_free (_osc_unix_server);
411 _osc_unix_server = 0;
414 close(_request_pipe[0]);
415 close(_request_pipe[1]);
419 OSC::set_session (Session& s)
422 session->GoingAway.connect (mem_fun (*this, &OSC::session_going_away));
424 // "Application Hooks"
426 session->Exported.connect( mem_fun( *this, &OSC::session_exported ) );
430 OSC::session_going_away ()
435 // "Application Hook" Handlers //
437 OSC::session_loaded( Session& s ) {
438 lo_address listener = lo_address_new( NULL, "7770" );
439 lo_send( listener, "/session/loaded", "ss", s.path().c_str(), s.name().c_str() );
443 OSC::session_exported( std::string path, std::string name ) {
444 lo_address listener = lo_address_new( NULL, "7770" );
445 lo_send( listener, "/session/exported", "ss", path.c_str(), name.c_str() );
448 // end "Application Hook" Handlers //
453 OSC::current_value (const char *path, const char *types, lo_arg **argv, int argc, void *data, void* user_data)
458 if (argc < 3 || types == 0 || strlen (types) < 3 || types[0] != 's' || types[1] != 's' || types[2] != s) {
462 const char *returl = argv[1]->s;
463 lo_address addr = find_or_cache_addr (returl);
465 const char *retpath = argv[2]->s;
468 if (strcmp (argv[0]->s, "transport_frame")) {
471 lo_send (addr, retpath, "i", session->transport_frame());
474 } else if (strcmp (argv[0]->s, "transport_speed")) {
477 lo_send (addr, retpath, "i", session->transport_frame());
480 } else if (strcmp (argv[0]->s, "transport_locked")) {
483 lo_send (addr, retpath, "i", session->transport_frame());
486 } else if (strcmp (argv[0]->s, "punch_in") {
489 lo_send (addr, retpath, "i", session->transport_frame());
492 } else if (strcmp (argv[0]->s, "punch_out") {
495 lo_send (addr, retpath, "i", session->transport_frame());
498 } else if (strcmp (argv[0]->s, "rec_enable") {
501 lo_send (addr, retpath, "i", session->transport_frame());
513 OSC::route_mute (int rid, int yn)
515 if (!session) return -1;
517 boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
520 r->set_mute (yn, this);
526 OSC::route_solo (int rid, int yn)
528 if (!session) return -1;
530 boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
533 r->set_solo (yn, this);
539 OSC::route_recenable (int rid, int yn)
541 if (!session) return -1;
543 boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
546 r->set_record_enable (yn, this);
552 OSC::route_set_gain_abs (int rid, float level)
554 if (!session) return -1;
556 boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
559 r->set_gain (level, this);
566 OSC::route_set_gain_dB (int rid, float dB)
568 if (!session) return -1;
570 boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
573 r->set_gain (dB_to_coefficient (dB), this);