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