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.
30 #include <pbd/pthread_utils.h>
32 #include <ardour/osc.h>
33 #include <ardour/session.h>
34 #include <ardour/route.h>
35 #include <ardour/audio_track.h>
39 using namespace ARDOUR;
43 static void error_callback(int num, const char *m, const char *path)
46 fprintf(stderr, "liblo server error %d in path %s: %s\n", num, path, m);
50 OSC::OSC (uint32_t port)
69 for (int j=0; j < 20; ++j) {
70 snprintf(tmpstr, sizeof(tmpstr), "%d", _port);
72 if ((_osc_server = lo_server_new (tmpstr, error_callback))) {
76 cerr << "can't get osc at port: " << _port << endl;
82 #ifdef ARDOUR_OSC_UNIX_SERVER
84 // APPEARS sluggish for now
86 // attempt to create unix socket server too
88 snprintf(tmpstr, sizeof(tmpstr), "/tmp/sooperlooper_XXXXXX");
89 int fd = mkstemp(tmpstr);
95 _osc_unix_server = lo_server_new (tmpstr, error_callback);
97 if (_osc_unix_server) {
98 _osc_unix_socket_path = tmpstr;
103 cerr << "OSC @ " << get_server_url () << endl;
105 register_callbacks();
107 // lo_server_thread_add_method(_sthread, NULL, NULL, OSC::_dummy_handler, this);
109 if (!init_osc_thread()) {
118 if (_osc_server == 0) {
119 /* already stopped */
123 // stop server thread
124 terminate_osc_thread();
126 lo_server_free (_osc_server);
129 if (!_osc_unix_socket_path.empty()) {
131 unlink(_osc_unix_socket_path.c_str());
143 OSC::register_callbacks()
148 srvs[0] = _osc_server;
149 srvs[1] = _osc_unix_server;
151 for (size_t i = 0; i < 2; ++i) {
159 #define REGISTER_CALLBACK(serv,path,types, function) lo_server_add_method (serv, path, types, OSC::_ ## function, this)
161 REGISTER_CALLBACK (serv, "/ardour/add_marker", "", add_marker);
162 REGISTER_CALLBACK (serv, "/ardour/loop_toggle", "", loop_toggle);
163 REGISTER_CALLBACK (serv, "/ardour/goto_start", "", goto_start);
164 REGISTER_CALLBACK (serv, "/ardour/goto_end", "", goto_end);
165 REGISTER_CALLBACK (serv, "/ardour/rewind", "", rewind);
166 REGISTER_CALLBACK (serv, "/ardour/ffwd", "", ffwd);
167 REGISTER_CALLBACK (serv, "/ardour/transport_stop", "", transport_stop);
168 REGISTER_CALLBACK (serv, "/ardour/transport_play", "", transport_play);
169 REGISTER_CALLBACK (serv, "/ardour/set_transport_speed", "f", set_transport_speed);
170 REGISTER_CALLBACK (serv, "/ardour/save_state", "", save_state);
171 REGISTER_CALLBACK (serv, "/ardour/prev_marker", "", prev_marker);
172 REGISTER_CALLBACK (serv, "/ardour/next_marker", "", next_marker);
173 REGISTER_CALLBACK (serv, "/ardour/undo", "", undo);
174 REGISTER_CALLBACK (serv, "/ardour/redo", "", redo);
175 REGISTER_CALLBACK (serv, "/ardour/toggle_punch_in", "", toggle_punch_in);
176 REGISTER_CALLBACK (serv, "/ardour/toggle_punch_out", "", toggle_punch_out);
177 REGISTER_CALLBACK (serv, "/ardour/rec_enable_toggle", "", rec_enable_toggle);
178 REGISTER_CALLBACK (serv, "/ardour/toggle_all_rec_enables", "", toggle_all_rec_enables);
181 REGISTER_CALLBACK (serv, "/ardour/*/#current_value", "", current_value);
182 REGISTER_CALLBACK (serv, "/ardour/set", "", set);
186 // un/register_update args= s:ctrl s:returl s:retpath
187 lo_server_add_method(serv, "/register_update", "sss", OSC::global_register_update_handler, this);
188 lo_server_add_method(serv, "/unregister_update", "sss", OSC::global_unregister_update_handler, this);
189 lo_server_add_method(serv, "/register_auto_update", "siss", OSC::global_register_auto_update_handler, this);
190 lo_server_add_method(serv, "/unregister_auto_update", "sss", OSC::_global_unregister_auto_update_handler, this);
196 OSC::init_osc_thread ()
198 // create new thread to run server
199 if (pipe (_request_pipe)) {
200 cerr << "Cannot create osc request signal pipe" << strerror (errno) << endl;
204 if (fcntl (_request_pipe[0], F_SETFL, O_NONBLOCK)) {
205 cerr << "osc: cannot set O_NONBLOCK on signal read pipe " << strerror (errno) << endl;
209 if (fcntl (_request_pipe[1], F_SETFL, O_NONBLOCK)) {
210 cerr << "osc: cannot set O_NONBLOCK on signal write pipe " << strerror (errno) << endl;
215 pthread_attr_init(&attr);
216 pthread_attr_setstacksize(&attr, 500000);
218 pthread_create (&_osc_thread, &attr, &OSC::_osc_receiver, this);
222 pthread_attr_destroy(&attr);
224 //pthread_detach (_osc_thread);
229 OSC::terminate_osc_thread ()
237 pthread_join (_osc_thread, &status);
241 OSC::poke_osc_thread ()
245 if (write (_request_pipe[1], &c, 1) != 1) {
246 cerr << "cannot send signal to osc thread! " << strerror (errno) << endl;
251 OSC::get_server_url()
257 urlstr = lo_server_get_url (_osc_server);
266 OSC::get_unix_server_url()
271 if (_osc_unix_server) {
272 urlstr = lo_server_get_url (_osc_unix_server);
284 OSC::_osc_receiver(void * arg)
286 PBD::ThreadCreated (pthread_self(), X_("OSC"));
288 static_cast<OSC*> (arg)->osc_receiver();
295 struct pollfd pfd[3];
302 fds[0] = _request_pipe[0];
305 if (_osc_server && lo_server_get_socket_fd(_osc_server) >= 0) {
306 fds[nfds] = lo_server_get_socket_fd(_osc_server);
307 srvs[nfds] = _osc_server;
311 if (_osc_unix_server && lo_server_get_socket_fd(_osc_unix_server) >= 0) {
312 fds[nfds] = lo_server_get_socket_fd(_osc_unix_server);
313 srvs[nfds] = _osc_unix_server;
320 for (int i=0; i < nfds; ++i) {
322 pfd[i].events = POLLIN|POLLPRI|POLLHUP|POLLERR;
327 //cerr << "poll on " << nfds << " for " << timeout << endl;
328 if ((ret = poll (pfd, nfds, timeout)) < 0) {
329 if (errno == EINTR) {
330 /* gdb at work, perhaps */
334 cerr << "OSC thread poll failed: " << strerror (errno) << endl;
339 //cerr << "poll returned " << ret << " pfd[0].revents = " << pfd[0].revents << " pfd[1].revents = " << pfd[1].revents << endl;
345 if ((pfd[0].revents & ~POLLIN)) {
346 cerr << "OSC: error polling extra port" << endl;
350 for (int i=1; i < nfds; ++i) {
351 if (pfd[i].revents & POLLIN)
353 // this invokes callbacks
354 //cerr << "invoking recv on " << pfd[i].fd << endl;
355 lo_server_recv(srvs[i]);
361 //cerr << "SL engine shutdown" << endl;
364 int fd = lo_server_get_socket_fd(_osc_server);
369 lo_server_free (_osc_server);
373 if (_osc_unix_server) {
374 cerr << "freeing unix server" << endl;
375 lo_server_free (_osc_unix_server);
376 _osc_unix_server = 0;
379 close(_request_pipe[0]);
380 close(_request_pipe[1]);
384 OSC::set_session (Session& s)
387 session->GoingAway.connect (mem_fun (*this, &OSC::session_going_away));
391 OSC::session_going_away ()
399 OSC::current_value (const char *path, const char *types, lo_arg **argv, int argc, void *data, void* user_data)
404 if (argc < 3 || types == 0 || strlen (types) < 3 || types[0] != 's' || types[1] != 's' || types[2] != s) {
408 const char *returl = argv[1]->s;
409 lo_address addr = find_or_cache_addr (returl);
411 const char *retpath = argv[2]->s;
414 if (strcmp (argv[0]->s, "transport_frame")) {
417 lo_send (addr, retpath, "i", session->transport_frame());
420 } else if (strcmp (argv[0]->s, "transport_speed")) {
423 lo_send (addr, retpath, "i", session->transport_frame());
426 } else if (strcmp (argv[0]->s, "transport_locked")) {
429 lo_send (addr, retpath, "i", session->transport_frame());
432 } else if (strcmp (argv[0]->s, "punch_in") {
435 lo_send (addr, retpath, "i", session->transport_frame());
438 } else if (strcmp (argv[0]->s, "punch_out") {
441 lo_send (addr, retpath, "i", session->transport_frame());
444 } else if (strcmp (argv[0]->s, "rec_enable") {
447 lo_send (addr, retpath, "i", session->transport_frame());