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