implement AU host callbacks, to some extent
[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 <glibmm/miscutils.h>
32
33 #include <pbd/pthread_utils.h>
34
35 #include <ardour/osc.h>
36 #include <ardour/session.h>
37 #include <ardour/route.h>
38 #include <ardour/audio_track.h>
39 #include <ardour/dB.h>
40
41 #include "i18n.h"
42
43 using namespace ARDOUR;
44 using namespace sigc;
45 using namespace std;
46
47 static void error_callback(int num, const char *m, const char *path)
48 {
49 #ifdef DEBUG
50         fprintf(stderr, "liblo server error %d in path %s: %s\n", num, path, m);
51 #endif
52 }
53
54 OSC::OSC (uint32_t port)
55         : _port(port)
56 {
57         _shutdown = false;
58         _osc_server = 0;
59         _osc_unix_server = 0;
60         _osc_thread = 0;
61 }
62
63 int
64 OSC::start ()
65 {
66         char tmpstr[255];
67
68         if (_osc_server) {
69                 /* already started */
70                 return 0;
71         }
72         
73         for (int j=0; j < 20; ++j) {
74                 snprintf(tmpstr, sizeof(tmpstr), "%d", _port);
75                 
76                 if ((_osc_server = lo_server_new (tmpstr, error_callback))) {
77                         break;
78                 }
79 #ifdef DEBUG            
80                 cerr << "can't get osc at port: " << _port << endl;
81 #endif
82                 _port++;
83                 continue;
84         }
85         
86 #ifdef ARDOUR_OSC_UNIX_SERVER
87         
88         // APPEARS sluggish for now
89         
90         // attempt to create unix socket server too
91         
92         snprintf(tmpstr, sizeof(tmpstr), "/tmp/sooperlooper_XXXXXX");
93         int fd = mkstemp(tmpstr);
94         
95         if (fd >= 0 ) {
96                 unlink (tmpstr);
97                 close (fd);
98                 
99                 _osc_unix_server = lo_server_new (tmpstr, error_callback);
100                 
101                 if (_osc_unix_server) {
102                         _osc_unix_socket_path = tmpstr;
103                 }
104         }
105 #endif
106         
107         cerr << "OSC @ " << get_server_url () << endl;
108
109         _osc_url_file = Glib::build_filename (get_user_ardour_path (), "osc_url");
110
111         ofstream urlfile;
112         urlfile.open(_osc_url_file.c_str(),ios::trunc);
113
114         if (urlfile) {
115                 urlfile << get_server_url () << endl;
116                 urlfile.close();
117         } else {  
118                 cerr << "Couldn't write '" <<  _osc_url_file << "'" <<endl;
119         }
120         
121         register_callbacks();
122         
123         // lo_server_thread_add_method(_sthread, NULL, NULL, OSC::_dummy_handler, this);
124                 
125         if (!init_osc_thread()) {
126                 return -1;
127         }
128         return 0;
129 }
130
131 int
132 OSC::stop ()
133 {       
134         if (_osc_server == 0) {
135                 /* already stopped */
136                 return 0;
137         }
138
139         // stop server thread
140         terminate_osc_thread();
141
142         lo_server_free (_osc_server);
143         _osc_server = 0;
144         
145         if (!_osc_unix_socket_path.empty()) {
146                 // unlink it
147                 unlink(_osc_unix_socket_path.c_str());
148         }
149         
150         if (!  _osc_url_file.empty() ) {
151                 unlink(_osc_url_file.c_str() );
152         }
153         return 0;
154 }
155
156 OSC::~OSC()
157 {
158         stop ();
159 }
160
161 void
162 OSC::register_callbacks()
163 {
164         lo_server srvs[2];
165         lo_server serv;
166
167         srvs[0] = _osc_server;
168         srvs[1] = _osc_unix_server;
169         
170         for (size_t i = 0; i < 2; ++i) {
171
172                 if (!srvs[i]) {
173                         continue;
174                 }
175
176                 serv = srvs[i];
177
178 #define REGISTER_CALLBACK(serv,path,types, function) lo_server_add_method (serv, path, types, OSC::_ ## function, this)
179                 
180                 REGISTER_CALLBACK (serv, "/ardour/add_marker", "", add_marker);
181                 REGISTER_CALLBACK (serv, "/ardour/access_action", "s", access_action);
182                 REGISTER_CALLBACK (serv, "/ardour/loop_toggle", "", loop_toggle);
183                 REGISTER_CALLBACK (serv, "/ardour/goto_start", "", goto_start);
184                 REGISTER_CALLBACK (serv, "/ardour/goto_end", "", goto_end);
185                 REGISTER_CALLBACK (serv, "/ardour/rewind", "", rewind);
186                 REGISTER_CALLBACK (serv, "/ardour/ffwd", "", ffwd);
187                 REGISTER_CALLBACK (serv, "/ardour/transport_stop", "", transport_stop);
188                 REGISTER_CALLBACK (serv, "/ardour/transport_play", "", transport_play);
189                 REGISTER_CALLBACK (serv, "/ardour/set_transport_speed", "f", set_transport_speed);
190                 REGISTER_CALLBACK (serv, "/ardour/locate", "ii", locate);
191                 REGISTER_CALLBACK (serv, "/ardour/save_state", "", save_state);
192                 REGISTER_CALLBACK (serv, "/ardour/prev_marker", "", prev_marker);
193                 REGISTER_CALLBACK (serv, "/ardour/next_marker", "", next_marker);
194                 REGISTER_CALLBACK (serv, "/ardour/undo", "", undo);
195                 REGISTER_CALLBACK (serv, "/ardour/redo", "", redo);
196                 REGISTER_CALLBACK (serv, "/ardour/toggle_punch_in", "", toggle_punch_in);
197                 REGISTER_CALLBACK (serv, "/ardour/toggle_punch_out", "", toggle_punch_out);
198                 REGISTER_CALLBACK (serv, "/ardour/rec_enable_toggle", "", rec_enable_toggle);
199                 REGISTER_CALLBACK (serv, "/ardour/toggle_all_rec_enables", "", toggle_all_rec_enables);
200
201                 REGISTER_CALLBACK (serv, "/ardour/routes/mute", "ii", route_mute);
202                 REGISTER_CALLBACK (serv, "/ardour/routes/solo", "ii", route_solo);
203                 REGISTER_CALLBACK (serv, "/ardour/routes/recenable", "ii", route_recenable);
204                 REGISTER_CALLBACK (serv, "/ardour/routes/gainabs", "if", route_set_gain_abs);
205                 REGISTER_CALLBACK (serv, "/ardour/routes/gaindB", "if", route_set_gain_dB);
206
207 #if 0
208                 REGISTER_CALLBACK (serv, "/ardour/*/#current_value", "", current_value);
209                 REGISTER_CALLBACK (serv, "/ardour/set", "", set);
210 #endif
211
212 #if 0
213                 // un/register_update args= s:ctrl s:returl s:retpath
214                 lo_server_add_method(serv, "/register_update", "sss", OSC::global_register_update_handler, this);
215                 lo_server_add_method(serv, "/unregister_update", "sss", OSC::global_unregister_update_handler, this);
216                 lo_server_add_method(serv, "/register_auto_update", "siss", OSC::global_register_auto_update_handler, this);
217                 lo_server_add_method(serv, "/unregister_auto_update", "sss", OSC::_global_unregister_auto_update_handler, this);
218 #endif
219         }
220 }
221
222 bool
223 OSC::init_osc_thread ()
224 {
225         // create new thread to run server
226         if (pipe (_request_pipe)) {
227                 cerr << "Cannot create osc request signal pipe" <<  strerror (errno) << endl;
228                 return false;
229         }
230
231         if (fcntl (_request_pipe[0], F_SETFL, O_NONBLOCK)) {
232                 cerr << "osc: cannot set O_NONBLOCK on signal read pipe " << strerror (errno) << endl;
233                 return false;
234         }
235
236         if (fcntl (_request_pipe[1], F_SETFL, O_NONBLOCK)) {
237                 cerr << "osc: cannot set O_NONBLOCK on signal write pipe " << strerror (errno) << endl;
238                 return false;
239         }
240         
241         pthread_attr_t attr;
242         pthread_attr_init(&attr);
243         pthread_attr_setstacksize(&attr, 500000);
244
245         pthread_create_and_store (X_("OSC"), &_osc_thread, &attr, &OSC::_osc_receiver, this);
246         if (!_osc_thread) {
247                 return false;
248         }
249         pthread_attr_destroy(&attr);
250
251         //pthread_detach (_osc_thread);
252         return true;
253 }
254
255 void
256 OSC::terminate_osc_thread ()
257 {
258         void* status;
259
260         _shutdown = true;
261         
262         poke_osc_thread ();
263
264         pthread_join (_osc_thread, &status);
265 }
266
267 void
268 OSC::poke_osc_thread ()
269 {
270         char c;
271
272         if (write (_request_pipe[1], &c, 1) != 1) {
273                 cerr << "cannot send signal to osc thread! " <<  strerror (errno) << endl;
274         }
275 }
276
277 std::string
278 OSC::get_server_url()
279 {
280         string url;
281         char * urlstr;
282
283         if (_osc_server) {
284                 urlstr = lo_server_get_url (_osc_server);
285                 url = urlstr;
286                 free (urlstr);
287         }
288         
289         return url;
290 }
291
292 std::string
293 OSC::get_unix_server_url()
294 {
295         string url;
296         char * urlstr;
297
298         if (_osc_unix_server) {
299                 urlstr = lo_server_get_url (_osc_unix_server);
300                 url = urlstr;
301                 free (urlstr);
302         }
303         
304         return url;
305 }
306
307
308 /* server thread */
309
310 void *
311 OSC::_osc_receiver(void * arg)
312 {
313         PBD::notify_gui_about_thread_creation (pthread_self(), X_("OSC"));
314         static_cast<OSC*> (arg)->osc_receiver();
315         return 0;
316 }
317
318 void
319 OSC::osc_receiver()
320 {
321         struct pollfd pfd[3];
322         int fds[3];
323         lo_server srvs[3];
324         int nfds = 0;
325         int timeout = -1;
326         int ret;
327         
328         fds[0] = _request_pipe[0];
329         nfds++;
330         
331         if (_osc_server && lo_server_get_socket_fd(_osc_server) >= 0) {
332                 fds[nfds] = lo_server_get_socket_fd(_osc_server);
333                 srvs[nfds] = _osc_server;
334                 nfds++;
335         }
336
337         if (_osc_unix_server && lo_server_get_socket_fd(_osc_unix_server) >= 0) {
338                 fds[nfds] = lo_server_get_socket_fd(_osc_unix_server);
339                 srvs[nfds] = _osc_unix_server;
340                 nfds++;
341         }
342         
343         
344         while (!_shutdown) {
345
346                 for (int i=0; i < nfds; ++i) {
347                         pfd[i].fd = fds[i];
348                         pfd[i].events = POLLIN|POLLPRI|POLLHUP|POLLERR;
349                         pfd[i].revents = 0;
350                 }
351                 
352         again:
353                 //cerr << "poll on " << nfds << " for " << timeout << endl;
354                 if ((ret = poll (pfd, nfds, timeout)) < 0) {
355                         if (errno == EINTR) {
356                                 /* gdb at work, perhaps */
357                                 goto again;
358                         }
359                         
360                         cerr << "OSC thread poll failed: " <<  strerror (errno) << endl;
361                         
362                         break;
363                 }
364
365                 //cerr << "poll returned " << ret << "  pfd[0].revents = " << pfd[0].revents << "  pfd[1].revents = " << pfd[1].revents << endl;
366                 
367                 if (_shutdown) {
368                         break;
369                 }
370                 
371                 if ((pfd[0].revents & ~POLLIN)) {
372                         cerr << "OSC: error polling extra port" << endl;
373                         break;
374                 }
375                 
376                 for (int i=1; i < nfds; ++i) {
377                         if (pfd[i].revents & POLLIN)
378                         {
379                                 // this invokes callbacks
380                                 // cerr << "invoking recv on " << pfd[i].fd << endl;
381                                 lo_server_recv(srvs[i]);
382                         }
383                 }
384
385         }
386
387         //cerr << "SL engine shutdown" << endl;
388         
389         if (_osc_server) {
390                 int fd = lo_server_get_socket_fd(_osc_server);
391                 if (fd >=0) {
392                         // hack around
393                         close(fd);
394                 }
395                 lo_server_free (_osc_server);
396                 _osc_server = 0;
397         }
398
399         if (_osc_unix_server) {
400                 cerr << "freeing unix server" << endl;
401                 lo_server_free (_osc_unix_server);
402                 _osc_unix_server = 0;
403         }
404         
405         close(_request_pipe[0]);
406         close(_request_pipe[1]);
407 }
408
409 void
410 OSC::set_session (Session& s)
411 {
412         session = &s;
413         session->GoingAway.connect (mem_fun (*this, &OSC::session_going_away));
414
415         // "Application Hooks"
416         session_loaded( s );
417         session->Exported.connect( mem_fun( *this, &OSC::session_exported ) );
418 }
419
420 void
421 OSC::session_going_away ()
422 {
423         session = 0;
424 }
425
426 // "Application Hook" Handlers //
427 void
428 OSC::session_loaded( Session& s ) {
429         lo_address listener = lo_address_new( NULL, "7770" );
430         lo_send( listener, "/session/loaded", "ss", s.path().c_str(), s.name().c_str() );
431 }
432
433 void
434 OSC::session_exported( std::string path, std::string name ) {
435         lo_address listener = lo_address_new( NULL, "7770" );
436         lo_send( listener, "/session/exported", "ss", path.c_str(), name.c_str() );
437 }
438
439 // end "Application Hook" Handlers //
440
441 /* path callbacks */
442
443 int 
444 OSC::current_value (const char *path, const char *types, lo_arg **argv, int argc, void *data, void* user_data) 
445
446 #if 0
447         const char* returl;
448
449         if (argc < 3 || types == 0 || strlen (types) < 3 || types[0] != 's' || types[1] != 's' || types[2] != s) {
450                 return 1;
451         }
452
453         const char *returl = argv[1]->s;
454         lo_address addr = find_or_cache_addr (returl);
455
456         const char *retpath = argv[2]->s;
457
458         
459         if (strcmp (argv[0]->s, "transport_frame")) {
460
461                 if (session) {
462                         lo_send (addr, retpath, "i", session->transport_frame());
463                 }
464
465         } else if (strcmp (argv[0]->s, "transport_speed")) {
466
467                 if (session) {
468                         lo_send (addr, retpath, "i", session->transport_frame());
469                 }
470
471         } else if (strcmp (argv[0]->s, "transport_locked")) {
472
473                 if (session) {
474                         lo_send (addr, retpath, "i", session->transport_frame());
475                 }
476
477         } else if (strcmp (argv[0]->s, "punch_in") {
478
479                 if (session) {
480                         lo_send (addr, retpath, "i", session->transport_frame());
481                 }
482
483         } else if (strcmp (argv[0]->s, "punch_out") {
484
485                 if (session) {
486                         lo_send (addr, retpath, "i", session->transport_frame());
487                 }
488
489         } else if (strcmp (argv[0]->s, "rec_enable") {
490
491                 if (session) {
492                         lo_send (addr, retpath, "i", session->transport_frame());
493                 }
494
495         } else {
496
497                 /* error */
498         }
499 #endif
500         return 0;
501 }
502
503 int
504 OSC::route_mute (int rid, int yn)
505 {
506         if (!session) return -1;
507
508         boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
509
510         if (r) {
511                 r->set_mute (yn, this);
512         }
513         return 0;
514 }
515
516 int
517 OSC::route_solo (int rid, int yn)
518 {
519         if (!session) return -1;
520
521         boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
522
523         if (r) {
524                 r->set_solo (yn, this);
525         }
526         return 0;
527 }
528
529 int
530 OSC::route_recenable (int rid, int yn)
531 {
532         if (!session) return -1;
533
534         boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
535
536         if (r) {
537                 r->set_record_enable (yn, this);
538         }
539         return 0;
540 }
541
542 int
543 OSC::route_set_gain_abs (int rid, float level)
544 {
545         if (!session) return -1;
546
547         boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
548
549         if (r) {
550                 r->set_gain (level, this);
551         }
552
553         return 0;
554 }
555
556 int
557 OSC::route_set_gain_dB (int rid, float dB)
558 {
559         if (!session) return -1;
560
561         boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
562
563         if (r) {
564                 r->set_gain (dB_to_coefficient (dB), this);
565         }
566
567         return 0;
568 }