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