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 <ardour/osc.h>
32 #include <ardour/session.h>
33 #include <ardour/route.h>
34 #include <ardour/audio_track.h>
38 using namespace ARDOUR;
42 static void error_callback(int num, const char *m, const char *path)
45 fprintf(stderr, "liblo server error %d in path %s: %s\n", num, path, m);
49 OSC::OSC (uint32_t port)
63 for (int j=0; j < 20; ++j) {
64 snprintf(tmpstr, sizeof(tmpstr), "%d", _port);
66 if ((_osc_server = lo_server_new (tmpstr, error_callback))) {
70 cerr << "can't get osc at port: " << _port << endl;
76 #ifdef ARDOUR_OSC_UNIX_SERVER
78 // APPEARS sluggish for now
80 // attempt to create unix socket server too
82 snprintf(tmpstr, sizeof(tmpstr), "/tmp/sooperlooper_XXXXXX");
83 int fd = mkstemp(tmpstr);
89 _osc_unix_server = lo_server_new (tmpstr, error_callback);
91 if (_osc_unix_server) {
92 _osc_unix_socket_path = tmpstr;
97 cerr << "OSC @ " << get_server_url () << endl;
101 // lo_server_thread_add_method(_sthread, NULL, NULL, OSC::_dummy_handler, this);
103 if (!init_osc_thread()) {
112 lo_server_free (_osc_server);
114 if (!_osc_unix_socket_path.empty()) {
116 unlink(_osc_unix_socket_path.c_str());
119 // stop server thread
120 terminate_osc_thread();
131 OSC::register_callbacks()
136 srvs[0] = _osc_server;
137 srvs[1] = _osc_unix_server;
139 for (size_t i = 0; i < 2; ++i) {
147 #define REGISTER_CALLBACK(serv,path,types, function) lo_server_add_method (serv, path, types, OSC::_ ## function, this)
149 REGISTER_CALLBACK (serv, "/ardour/add_marker", "", add_marker);
150 REGISTER_CALLBACK (serv, "/ardour/loop_toggle", "", loop_toggle);
151 REGISTER_CALLBACK (serv, "/ardour/goto_start", "", goto_start);
152 REGISTER_CALLBACK (serv, "/ardour/goto_end", "", goto_end);
153 REGISTER_CALLBACK (serv, "/ardour/rewind", "", rewind);
154 REGISTER_CALLBACK (serv, "/ardour/ffwd", "", ffwd);
155 REGISTER_CALLBACK (serv, "/ardour/transport_stop", "", transport_stop);
156 REGISTER_CALLBACK (serv, "/ardour/transport_play", "", transport_play);
157 REGISTER_CALLBACK (serv, "/ardour/set_transport_speed", "f", set_transport_speed);
158 REGISTER_CALLBACK (serv, "/ardour/save_state", "", save_state);
159 REGISTER_CALLBACK (serv, "/ardour/prev_marker", "", prev_marker);
160 REGISTER_CALLBACK (serv, "/ardour/next_marker", "", next_marker);
161 REGISTER_CALLBACK (serv, "/ardour/undo", "", undo);
162 REGISTER_CALLBACK (serv, "/ardour/redo", "", redo);
163 REGISTER_CALLBACK (serv, "/ardour/toggle_punch_in", "", toggle_punch_in);
164 REGISTER_CALLBACK (serv, "/ardour/toggle_punch_out", "", toggle_punch_out);
165 REGISTER_CALLBACK (serv, "/ardour/rec_enable_toggle", "", rec_enable_toggle);
166 REGISTER_CALLBACK (serv, "/ardour/toggle_all_rec_enables", "", toggle_all_rec_enables);
169 REGISTER_CALLBACK (serv, "/ardour/*/#current_value", "", current_value);
170 REGISTER_CALLBACK (serv, "/ardour/set", "", set);
174 // un/register_update args= s:ctrl s:returl s:retpath
175 lo_server_add_method(serv, "/register_update", "sss", OSC::global_register_update_handler, this);
176 lo_server_add_method(serv, "/unregister_update", "sss", OSC::global_unregister_update_handler, this);
177 lo_server_add_method(serv, "/register_auto_update", "siss", OSC::global_register_auto_update_handler, this);
178 lo_server_add_method(serv, "/unregister_auto_update", "sss", OSC::_global_unregister_auto_update_handler, this);
184 OSC::init_osc_thread ()
186 // create new thread to run server
187 if (pipe (_request_pipe)) {
188 cerr << "Cannot create osc request signal pipe" << strerror (errno) << endl;
192 if (fcntl (_request_pipe[0], F_SETFL, O_NONBLOCK)) {
193 cerr << "osc: cannot set O_NONBLOCK on signal read pipe " << strerror (errno) << endl;
197 if (fcntl (_request_pipe[1], F_SETFL, O_NONBLOCK)) {
198 cerr << "osc: cannot set O_NONBLOCK on signal write pipe " << strerror (errno) << endl;
202 pthread_create (&_osc_thread, NULL, &OSC::_osc_receiver, this);
207 //pthread_detach (_osc_thread);
212 OSC::terminate_osc_thread ()
220 pthread_join (_osc_thread, &status);
224 OSC::poke_osc_thread ()
228 if (write (_request_pipe[1], &c, 1) != 1) {
229 cerr << "cannot send signal to osc thread! " << strerror (errno) << endl;
234 OSC::get_server_url()
240 urlstr = lo_server_get_url (_osc_server);
249 OSC::get_unix_server_url()
254 if (_osc_unix_server) {
255 urlstr = lo_server_get_url (_osc_unix_server);
267 OSC::_osc_receiver(void * arg)
269 static_cast<OSC*> (arg)->osc_receiver();
276 struct pollfd pfd[3];
283 fds[0] = _request_pipe[0];
286 if (_osc_server && lo_server_get_socket_fd(_osc_server) >= 0) {
287 fds[nfds] = lo_server_get_socket_fd(_osc_server);
288 srvs[nfds] = _osc_server;
292 if (_osc_unix_server && lo_server_get_socket_fd(_osc_unix_server) >= 0) {
293 fds[nfds] = lo_server_get_socket_fd(_osc_unix_server);
294 srvs[nfds] = _osc_unix_server;
301 for (int i=0; i < nfds; ++i) {
303 pfd[i].events = POLLIN|POLLPRI|POLLHUP|POLLERR;
308 //cerr << "poll on " << nfds << " for " << timeout << endl;
309 if ((ret = poll (pfd, nfds, timeout)) < 0) {
310 if (errno == EINTR) {
311 /* gdb at work, perhaps */
315 cerr << "OSC thread poll failed: " << strerror (errno) << endl;
320 //cerr << "poll returned " << ret << " pfd[0].revents = " << pfd[0].revents << " pfd[1].revents = " << pfd[1].revents << endl;
326 if ((pfd[0].revents & ~POLLIN)) {
327 cerr << "OSC: error polling extra port" << endl;
331 for (int i=1; i < nfds; ++i) {
332 if (pfd[i].revents & POLLIN)
334 // this invokes callbacks
335 //cerr << "invoking recv on " << pfd[i].fd << endl;
336 lo_server_recv(srvs[i]);
342 //cerr << "SL engine shutdown" << endl;
345 int fd = lo_server_get_socket_fd(_osc_server);
350 lo_server_free (_osc_server);
354 if (_osc_unix_server) {
355 cerr << "freeing unix server" << endl;
356 lo_server_free (_osc_unix_server);
357 _osc_unix_server = 0;
360 close(_request_pipe[0]);
361 close(_request_pipe[1]);
365 OSC::set_session (Session& s)
368 session->GoingAway.connect (mem_fun (*this, &OSC::session_going_away));
372 OSC::session_going_away ()
380 OSC::current_value (const char *path, const char *types, lo_arg **argv, int argc, void *data, void* user_data)
385 if (argc < 3 || types == 0 || strlen (types) < 3 || types[0] != 's' || types[1] != 's' || types[2] != s) {
389 const char *returl = argv[1]->s;
390 lo_address addr = find_or_cache_addr (returl);
392 const char *retpath = argv[2]->s;
395 if (strcmp (argv[0]->s, "transport_frame")) {
398 lo_send (addr, retpath, "i", session->transport_frame());
401 } else if (strcmp (argv[0]->s, "transport_speed")) {
404 lo_send (addr, retpath, "i", session->transport_frame());
407 } else if (strcmp (argv[0]->s, "transport_locked")) {
410 lo_send (addr, retpath, "i", session->transport_frame());
413 } else if (strcmp (argv[0]->s, "punch_in") {
416 lo_send (addr, retpath, "i", session->transport_frame());
419 } else if (strcmp (argv[0]->s, "punch_out") {
422 lo_send (addr, retpath, "i", session->transport_frame());
425 } else if (strcmp (argv[0]->s, "rec_enable") {
428 lo_send (addr, retpath, "i", session->transport_frame());