Merge with 2.0-ongoing R2883.
[ardour.git] / libs / ardour / osc.cc
1 /*
2  * Copyright (C) 2006 Paul Davis
3  *  
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.
8  *  
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.
13  *  
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.
17  *  
18  */
19
20 #include <iostream>
21 #include <fstream>
22 #include <cstdio>
23 #include <cstdlib>
24 #include <cerrno>
25 #include <algorithm>
26
27 #include <sys/poll.h>
28 #include <unistd.h>
29 #include <fcntl.h>
30
31 #include <pbd/pthread_utils.h>
32 #include <pbd/file_utils.h>
33
34 #include <ardour/osc.h>
35 #include <ardour/session.h>
36 #include <ardour/route.h>
37 #include <ardour/audio_track.h>
38 #include <ardour/filesystem_paths.h>
39
40 #include "i18n.h"
41
42 using namespace ARDOUR;
43 using namespace sigc;
44 using namespace std;
45
46 static void error_callback(int num, const char *m, const char *path)
47 {
48 #ifdef DEBUG
49         fprintf(stderr, "liblo server error %d in path %s: %s\n", num, path, m);
50 #endif
51 }
52
53 OSC::OSC (uint32_t port)
54         : _port(port)
55 {
56         _shutdown = false;
57         _osc_server = 0;
58         _osc_unix_server = 0;
59         _osc_thread = 0;
60 }
61
62 int
63 OSC::start ()
64 {
65         char tmpstr[255];
66
67         if (_osc_server) {
68                 /* already started */
69                 return 0;
70         }
71         
72         for (int j=0; j < 20; ++j) {
73                 snprintf(tmpstr, sizeof(tmpstr), "%d", _port);
74                 
75                 if ((_osc_server = lo_server_new (tmpstr, error_callback))) {
76                         break;
77                 }
78 #ifdef DEBUG            
79                 cerr << "can't get osc at port: " << _port << endl;
80 #endif
81                 _port++;
82                 continue;
83         }
84         
85 #ifdef ARDOUR_OSC_UNIX_SERVER
86         
87         // APPEARS sluggish for now
88         
89         // attempt to create unix socket server too
90         
91         snprintf(tmpstr, sizeof(tmpstr), "/tmp/sooperlooper_XXXXXX");
92         int fd = mkstemp(tmpstr);
93         
94         if (fd >= 0 ) {
95                 unlink (tmpstr);
96                 close (fd);
97                 
98                 _osc_unix_server = lo_server_new (tmpstr, error_callback);
99                 
100                 if (_osc_unix_server) {
101                         _osc_unix_socket_path = tmpstr;
102                 }
103         }
104 #endif
105         
106         cerr << "OSC @ " << get_server_url () << endl;
107
108         sys::path url_file;
109
110         if (find_file_in_search_path (ardour_search_path() + system_config_search_path(),
111                                 "osc_url", url_file)) {
112                 _osc_url_file = url_file.to_string();
113                 ofstream urlfile;
114                 urlfile.open(_osc_url_file.c_str(), ios::trunc);
115                 if ( urlfile )
116                 {
117                         urlfile << get_server_url () << endl;
118                         urlfile.close();
119                 }
120                 else
121                 {  
122                         cerr << "Couldn't write '" <<  _osc_url_file << "'" <<endl;
123                 }
124         }
125         
126         register_callbacks();
127         
128         // lo_server_thread_add_method(_sthread, NULL, NULL, OSC::_dummy_handler, this);
129                 
130         if (!init_osc_thread()) {
131                 return -1;
132         }
133         return 0;
134 }
135
136 int
137 OSC::stop ()
138 {       
139         if (_osc_server == 0) {
140                 /* already stopped */
141                 return 0;
142         }
143
144         // stop server thread
145         terminate_osc_thread();
146
147         lo_server_free (_osc_server);
148         _osc_server = 0;
149         
150         if (!_osc_unix_socket_path.empty()) {
151                 // unlink it
152                 unlink(_osc_unix_socket_path.c_str());
153         }
154         
155    if (!  _osc_url_file.empty() ) {
156       unlink(_osc_url_file.c_str() );
157    }
158         return 0;
159 }
160
161 OSC::~OSC()
162 {
163         stop ();
164 }
165
166 void
167 OSC::register_callbacks()
168 {
169         lo_server srvs[2];
170         lo_server serv;
171
172         srvs[0] = _osc_server;
173         srvs[1] = _osc_unix_server;
174         
175         for (size_t i = 0; i < 2; ++i) {
176
177                 if (!srvs[i]) {
178                         continue;
179                 }
180
181                 serv = srvs[i];
182
183 #define REGISTER_CALLBACK(serv,path,types, function) lo_server_add_method (serv, path, types, OSC::_ ## function, this)
184                 
185                 REGISTER_CALLBACK (serv, "/ardour/add_marker", "", add_marker);
186                 REGISTER_CALLBACK (serv, "/ardour/loop_toggle", "", loop_toggle);
187                 REGISTER_CALLBACK (serv, "/ardour/goto_start", "", goto_start);
188                 REGISTER_CALLBACK (serv, "/ardour/goto_end", "", goto_end);
189                 REGISTER_CALLBACK (serv, "/ardour/rewind", "", rewind);
190                 REGISTER_CALLBACK (serv, "/ardour/ffwd", "", ffwd);
191                 REGISTER_CALLBACK (serv, "/ardour/transport_stop", "", transport_stop);
192                 REGISTER_CALLBACK (serv, "/ardour/transport_play", "", transport_play);
193                 REGISTER_CALLBACK (serv, "/ardour/set_transport_speed", "f", set_transport_speed);
194                 REGISTER_CALLBACK (serv, "/ardour/save_state", "", save_state);
195                 REGISTER_CALLBACK (serv, "/ardour/prev_marker", "", prev_marker);
196                 REGISTER_CALLBACK (serv, "/ardour/next_marker", "", next_marker);
197                 REGISTER_CALLBACK (serv, "/ardour/undo", "", undo);
198                 REGISTER_CALLBACK (serv, "/ardour/redo", "", redo);
199                 REGISTER_CALLBACK (serv, "/ardour/toggle_punch_in", "", toggle_punch_in);
200                 REGISTER_CALLBACK (serv, "/ardour/toggle_punch_out", "", toggle_punch_out);
201                 REGISTER_CALLBACK (serv, "/ardour/rec_enable_toggle", "", rec_enable_toggle);
202                 REGISTER_CALLBACK (serv, "/ardour/toggle_all_rec_enables", "", toggle_all_rec_enables);
203
204 #if 0
205                 REGISTER_CALLBACK (serv, "/ardour/*/#current_value", "", current_value);
206                 REGISTER_CALLBACK (serv, "/ardour/set", "", set);
207 #endif
208
209 #if 0
210                 // un/register_update args= s:ctrl s:returl s:retpath
211                 lo_server_add_method(serv, "/register_update", "sss", OSC::global_register_update_handler, this);
212                 lo_server_add_method(serv, "/unregister_update", "sss", OSC::global_unregister_update_handler, this);
213                 lo_server_add_method(serv, "/register_auto_update", "siss", OSC::global_register_auto_update_handler, this);
214                 lo_server_add_method(serv, "/unregister_auto_update", "sss", OSC::_global_unregister_auto_update_handler, this);
215 #endif
216         }
217 }
218
219 bool
220 OSC::init_osc_thread ()
221 {
222         // create new thread to run server
223         if (pipe (_request_pipe)) {
224                 cerr << "Cannot create osc request signal pipe" <<  strerror (errno) << endl;
225                 return false;
226         }
227
228         if (fcntl (_request_pipe[0], F_SETFL, O_NONBLOCK)) {
229                 cerr << "osc: cannot set O_NONBLOCK on signal read pipe " << strerror (errno) << endl;
230                 return false;
231         }
232
233         if (fcntl (_request_pipe[1], F_SETFL, O_NONBLOCK)) {
234                 cerr << "osc: cannot set O_NONBLOCK on signal write pipe " << strerror (errno) << endl;
235                 return false;
236         }
237         
238         pthread_attr_t attr;
239         pthread_attr_init(&attr);
240         pthread_attr_setstacksize(&attr, 500000);
241
242         pthread_create (&_osc_thread, &attr, &OSC::_osc_receiver, this);
243         if (!_osc_thread) {
244                 return false;
245         }
246         pthread_attr_destroy(&attr);
247
248         //pthread_detach (_osc_thread);
249         return true;
250 }
251
252 void
253 OSC::terminate_osc_thread ()
254 {
255         void* status;
256
257         _shutdown = true;
258         
259         poke_osc_thread ();
260
261         pthread_join (_osc_thread, &status);
262 }
263
264 void
265 OSC::poke_osc_thread ()
266 {
267         char c;
268
269         if (write (_request_pipe[1], &c, 1) != 1) {
270                 cerr << "cannot send signal to osc thread! " <<  strerror (errno) << endl;
271         }
272 }
273
274 std::string
275 OSC::get_server_url()
276 {
277         string url;
278         char * urlstr;
279
280         if (_osc_server) {
281                 urlstr = lo_server_get_url (_osc_server);
282                 url = urlstr;
283                 free (urlstr);
284         }
285         
286         return url;
287 }
288
289 std::string
290 OSC::get_unix_server_url()
291 {
292         string url;
293         char * urlstr;
294
295         if (_osc_unix_server) {
296                 urlstr = lo_server_get_url (_osc_unix_server);
297                 url = urlstr;
298                 free (urlstr);
299         }
300         
301         return url;
302 }
303
304
305 /* server thread */
306
307 void *
308 OSC::_osc_receiver(void * arg)
309 {
310         PBD::ThreadCreated (pthread_self(), X_("OSC"));
311
312         static_cast<OSC*> (arg)->osc_receiver();
313         return 0;
314 }
315
316 void
317 OSC::osc_receiver()
318 {
319         struct pollfd pfd[3];
320         int fds[3];
321         lo_server srvs[3];
322         int nfds = 0;
323         int timeout = -1;
324         int ret;
325         
326         fds[0] = _request_pipe[0];
327         nfds++;
328         
329         if (_osc_server && lo_server_get_socket_fd(_osc_server) >= 0) {
330                 fds[nfds] = lo_server_get_socket_fd(_osc_server);
331                 srvs[nfds] = _osc_server;
332                 nfds++;
333         }
334
335         if (_osc_unix_server && lo_server_get_socket_fd(_osc_unix_server) >= 0) {
336                 fds[nfds] = lo_server_get_socket_fd(_osc_unix_server);
337                 srvs[nfds] = _osc_unix_server;
338                 nfds++;
339         }
340         
341         
342         while (!_shutdown) {
343
344                 for (int i=0; i < nfds; ++i) {
345                         pfd[i].fd = fds[i];
346                         pfd[i].events = POLLIN|POLLPRI|POLLHUP|POLLERR;
347                         pfd[i].revents = 0;
348                 }
349                 
350         again:
351                 //cerr << "poll on " << nfds << " for " << timeout << endl;
352                 if ((ret = poll (pfd, nfds, timeout)) < 0) {
353                         if (errno == EINTR) {
354                                 /* gdb at work, perhaps */
355                                 goto again;
356                         }
357                         
358                         cerr << "OSC thread poll failed: " <<  strerror (errno) << endl;
359                         
360                         break;
361                 }
362
363                 //cerr << "poll returned " << ret << "  pfd[0].revents = " << pfd[0].revents << "  pfd[1].revents = " << pfd[1].revents << endl;
364                 
365                 if (_shutdown) {
366                         break;
367                 }
368                 
369                 if ((pfd[0].revents & ~POLLIN)) {
370                         cerr << "OSC: error polling extra port" << endl;
371                         break;
372                 }
373                 
374                 for (int i=1; i < nfds; ++i) {
375                         if (pfd[i].revents & POLLIN)
376                         {
377                                 // this invokes callbacks
378                                 //cerr << "invoking recv on " << pfd[i].fd << endl;
379                                 lo_server_recv(srvs[i]);
380                         }
381                 }
382
383         }
384
385         //cerr << "SL engine shutdown" << endl;
386         
387         if (_osc_server) {
388                 int fd = lo_server_get_socket_fd(_osc_server);
389                 if (fd >=0) {
390                                 // hack around
391                         close(fd);
392                 }
393                 lo_server_free (_osc_server);
394                 _osc_server = 0;
395         }
396
397         if (_osc_unix_server) {
398                 cerr << "freeing unix server" << endl;
399                 lo_server_free (_osc_unix_server);
400                 _osc_unix_server = 0;
401         }
402         
403         close(_request_pipe[0]);
404         close(_request_pipe[1]);
405 }
406
407 void
408 OSC::set_session (Session& s)
409 {
410         session = &s;
411         session->GoingAway.connect (mem_fun (*this, &OSC::session_going_away));
412 }
413
414 void
415 OSC::session_going_away ()
416 {
417         session = 0;
418 }
419
420 /* path callbacks */
421
422 int 
423 OSC::current_value (const char *path, const char *types, lo_arg **argv, int argc, void *data, void* user_data) 
424
425 #if 0
426         const char* returl;
427
428         if (argc < 3 || types == 0 || strlen (types) < 3 || types[0] != 's' || types[1] != 's' || types[2] != s) {
429                 return 1;
430         }
431
432         const char *returl = argv[1]->s;
433         lo_address addr = find_or_cache_addr (returl);
434
435         const char *retpath = argv[2]->s;
436
437         
438         if (strcmp (argv[0]->s, "transport_frame")) {
439
440                 if (session) {
441                         lo_send (addr, retpath, "i", session->transport_frame());
442                 }
443
444         } else if (strcmp (argv[0]->s, "transport_speed")) {
445
446                 if (session) {
447                         lo_send (addr, retpath, "i", session->transport_frame());
448                 }
449
450         } else if (strcmp (argv[0]->s, "transport_locked")) {
451
452                 if (session) {
453                         lo_send (addr, retpath, "i", session->transport_frame());
454                 }
455
456         } else if (strcmp (argv[0]->s, "punch_in") {
457
458                 if (session) {
459                         lo_send (addr, retpath, "i", session->transport_frame());
460                 }
461
462         } else if (strcmp (argv[0]->s, "punch_out") {
463
464                 if (session) {
465                         lo_send (addr, retpath, "i", session->transport_frame());
466                 }
467
468         } else if (strcmp (argv[0]->s, "rec_enable") {
469
470                 if (session) {
471                         lo_send (addr, retpath, "i", session->transport_frame());
472                 }
473
474         } else {
475
476                 /* error */
477         }
478 #endif
479         return 0;
480 }