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