Factor out sequencing related things into an independant new library: "evoral".
[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
36 #include <ardour/osc.h>
37 #include <ardour/session.h>
38 #include <ardour/route.h>
39 #include <ardour/audio_track.h>
40 #include <ardour/filesystem_paths.h>
41
42 #include "i18n.h"
43
44 using namespace ARDOUR;
45 using namespace sigc;
46 using namespace std;
47
48 static void error_callback(int num, const char *m, const char *path)
49 {
50 #ifdef DEBUG
51         fprintf(stderr, "liblo server error %d in path %s: %s\n", num, path, m);
52 #endif
53 }
54
55 OSC::OSC (uint32_t port)
56         : _port(port)
57 {
58         _shutdown = false;
59         _osc_server = 0;
60         _osc_unix_server = 0;
61         _osc_thread = 0;
62 }
63
64 int
65 OSC::start ()
66 {
67         char tmpstr[255];
68
69         if (_osc_server) {
70                 /* already started */
71                 return 0;
72         }
73         
74         for (int j=0; j < 20; ++j) {
75                 snprintf(tmpstr, sizeof(tmpstr), "%d", _port);
76                 
77                 if ((_osc_server = lo_server_new (tmpstr, error_callback))) {
78                         break;
79                 }
80 #ifdef DEBUG            
81                 cerr << "can't get osc at port: " << _port << endl;
82 #endif
83                 _port++;
84                 continue;
85         }
86         
87 #ifdef ARDOUR_OSC_UNIX_SERVER
88         
89         // APPEARS sluggish for now
90         
91         // attempt to create unix socket server too
92         
93         snprintf(tmpstr, sizeof(tmpstr), "/tmp/sooperlooper_XXXXXX");
94         int fd = mkstemp(tmpstr);
95         
96         if (fd >= 0 ) {
97                 unlink (tmpstr);
98                 close (fd);
99                 
100                 _osc_unix_server = lo_server_new (tmpstr, error_callback);
101                 
102                 if (_osc_unix_server) {
103                         _osc_unix_socket_path = tmpstr;
104                 }
105         }
106 #endif
107         
108         cerr << "OSC @ " << get_server_url () << endl;
109
110         sys::path url_file;
111
112         if (find_file_in_search_path (ardour_search_path() + system_config_search_path(),
113                                 "osc_url", url_file)) {
114                 _osc_url_file = url_file.to_string();
115                 ofstream urlfile;
116                 urlfile.open(_osc_url_file.c_str(), ios::trunc);
117                 if ( urlfile )
118                 {
119                         urlfile << get_server_url () << endl;
120                         urlfile.close();
121                 }
122                 else
123                 {  
124                         cerr << "Couldn't write '" <<  _osc_url_file << "'" <<endl;
125                 }
126         }
127         
128         register_callbacks();
129         
130         // lo_server_thread_add_method(_sthread, NULL, NULL, OSC::_dummy_handler, this);
131                 
132         if (!init_osc_thread()) {
133                 return -1;
134         }
135         return 0;
136 }
137
138 int
139 OSC::stop ()
140 {       
141         if (_osc_server == 0) {
142                 /* already stopped */
143                 return 0;
144         }
145
146         // stop server thread
147         terminate_osc_thread();
148
149         lo_server_free (_osc_server);
150         _osc_server = 0;
151         
152         if (!_osc_unix_socket_path.empty()) {
153                 // unlink it
154                 unlink(_osc_unix_socket_path.c_str());
155         }
156         
157    if (!  _osc_url_file.empty() ) {
158       unlink(_osc_url_file.c_str() );
159    }
160         return 0;
161 }
162
163 OSC::~OSC()
164 {
165         stop ();
166 }
167
168 void
169 OSC::register_callbacks()
170 {
171         lo_server srvs[2];
172         lo_server serv;
173
174         srvs[0] = _osc_server;
175         srvs[1] = _osc_unix_server;
176         
177         for (size_t i = 0; i < 2; ++i) {
178
179                 if (!srvs[i]) {
180                         continue;
181                 }
182
183                 serv = srvs[i];
184
185 #define REGISTER_CALLBACK(serv,path,types, function) lo_server_add_method (serv, path, types, OSC::_ ## function, this)
186                 
187                 REGISTER_CALLBACK (serv, "/ardour/add_marker", "", add_marker);
188                 REGISTER_CALLBACK (serv, "/ardour/access_action", "s", access_action);
189                 REGISTER_CALLBACK (serv, "/ardour/loop_toggle", "", loop_toggle);
190                 REGISTER_CALLBACK (serv, "/ardour/goto_start", "", goto_start);
191                 REGISTER_CALLBACK (serv, "/ardour/goto_end", "", goto_end);
192                 REGISTER_CALLBACK (serv, "/ardour/rewind", "", rewind);
193                 REGISTER_CALLBACK (serv, "/ardour/ffwd", "", ffwd);
194                 REGISTER_CALLBACK (serv, "/ardour/transport_stop", "", transport_stop);
195                 REGISTER_CALLBACK (serv, "/ardour/transport_play", "", transport_play);
196                 REGISTER_CALLBACK (serv, "/ardour/set_transport_speed", "f", set_transport_speed);
197                 REGISTER_CALLBACK (serv, "/ardour/save_state", "", save_state);
198                 REGISTER_CALLBACK (serv, "/ardour/prev_marker", "", prev_marker);
199                 REGISTER_CALLBACK (serv, "/ardour/next_marker", "", next_marker);
200                 REGISTER_CALLBACK (serv, "/ardour/undo", "", undo);
201                 REGISTER_CALLBACK (serv, "/ardour/redo", "", redo);
202                 REGISTER_CALLBACK (serv, "/ardour/toggle_punch_in", "", toggle_punch_in);
203                 REGISTER_CALLBACK (serv, "/ardour/toggle_punch_out", "", toggle_punch_out);
204                 REGISTER_CALLBACK (serv, "/ardour/rec_enable_toggle", "", rec_enable_toggle);
205                 REGISTER_CALLBACK (serv, "/ardour/toggle_all_rec_enables", "", toggle_all_rec_enables);
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 (&_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::ThreadCreated (pthread_self(), X_("OSC"));
314
315         static_cast<OSC*> (arg)->osc_receiver();
316         return 0;
317 }
318
319 void
320 OSC::osc_receiver()
321 {
322         struct pollfd pfd[3];
323         int fds[3];
324         lo_server srvs[3];
325         int nfds = 0;
326         int timeout = -1;
327         int ret;
328         
329         fds[0] = _request_pipe[0];
330         nfds++;
331         
332         if (_osc_server && lo_server_get_socket_fd(_osc_server) >= 0) {
333                 fds[nfds] = lo_server_get_socket_fd(_osc_server);
334                 srvs[nfds] = _osc_server;
335                 nfds++;
336         }
337
338         if (_osc_unix_server && lo_server_get_socket_fd(_osc_unix_server) >= 0) {
339                 fds[nfds] = lo_server_get_socket_fd(_osc_unix_server);
340                 srvs[nfds] = _osc_unix_server;
341                 nfds++;
342         }
343         
344         
345         while (!_shutdown) {
346
347                 for (int i=0; i < nfds; ++i) {
348                         pfd[i].fd = fds[i];
349                         pfd[i].events = POLLIN|POLLPRI|POLLHUP|POLLERR;
350                         pfd[i].revents = 0;
351                 }
352                 
353         again:
354                 //cerr << "poll on " << nfds << " for " << timeout << endl;
355                 if ((ret = poll (pfd, nfds, timeout)) < 0) {
356                         if (errno == EINTR) {
357                                 /* gdb at work, perhaps */
358                                 goto again;
359                         }
360                         
361                         cerr << "OSC thread poll failed: " <<  strerror (errno) << endl;
362                         
363                         break;
364                 }
365
366                 //cerr << "poll returned " << ret << "  pfd[0].revents = " << pfd[0].revents << "  pfd[1].revents = " << pfd[1].revents << endl;
367                 
368                 if (_shutdown) {
369                         break;
370                 }
371                 
372                 if ((pfd[0].revents & ~POLLIN)) {
373                         cerr << "OSC: error polling extra port" << endl;
374                         break;
375                 }
376                 
377                 for (int i=1; i < nfds; ++i) {
378                         if (pfd[i].revents & POLLIN)
379                         {
380                                 // this invokes callbacks
381                                 //cerr << "invoking recv on " << pfd[i].fd << endl;
382                                 lo_server_recv(srvs[i]);
383                         }
384                 }
385
386         }
387
388         //cerr << "SL engine shutdown" << endl;
389         
390         if (_osc_server) {
391                 int fd = lo_server_get_socket_fd(_osc_server);
392                 if (fd >=0) {
393                                 // hack around
394                         close(fd);
395                 }
396                 lo_server_free (_osc_server);
397                 _osc_server = 0;
398         }
399
400         if (_osc_unix_server) {
401                 cerr << "freeing unix server" << endl;
402                 lo_server_free (_osc_unix_server);
403                 _osc_unix_server = 0;
404         }
405         
406         close(_request_pipe[0]);
407         close(_request_pipe[1]);
408 }
409
410 void
411 OSC::set_session (Session& s)
412 {
413         session = &s;
414         session->GoingAway.connect (mem_fun (*this, &OSC::session_going_away));
415
416         // "Application Hooks"
417         session_loaded( s );
418         session->Exported.connect( mem_fun( *this, &OSC::session_exported ) );
419 }
420
421 void
422 OSC::session_going_away ()
423 {
424         session = 0;
425 }
426
427 // "Application Hook" Handlers //
428 void
429 OSC::session_loaded( Session& s ) {
430         lo_address listener = lo_address_new( NULL, "7770" );
431         lo_send( listener, "/session/loaded", "ss", s.path().c_str(), s.name().c_str() );
432 }
433
434 void
435 OSC::session_exported( std::string path, std::string name ) {
436         lo_address listener = lo_address_new( NULL, "7770" );
437         lo_send( listener, "/session/exported", "ss", path.c_str(), name.c_str() );
438 }
439
440 // end "Application Hook" Handlers //
441
442 /* path callbacks */
443
444 int 
445 OSC::current_value (const char *path, const char *types, lo_arg **argv, int argc, void *data, void* user_data) 
446
447 #if 0
448         const char* returl;
449
450         if (argc < 3 || types == 0 || strlen (types) < 3 || types[0] != 's' || types[1] != 's' || types[2] != s) {
451                 return 1;
452         }
453
454         const char *returl = argv[1]->s;
455         lo_address addr = find_or_cache_addr (returl);
456
457         const char *retpath = argv[2]->s;
458
459         
460         if (strcmp (argv[0]->s, "transport_frame")) {
461
462                 if (session) {
463                         lo_send (addr, retpath, "i", session->transport_frame());
464                 }
465
466         } else if (strcmp (argv[0]->s, "transport_speed")) {
467
468                 if (session) {
469                         lo_send (addr, retpath, "i", session->transport_frame());
470                 }
471
472         } else if (strcmp (argv[0]->s, "transport_locked")) {
473
474                 if (session) {
475                         lo_send (addr, retpath, "i", session->transport_frame());
476                 }
477
478         } else if (strcmp (argv[0]->s, "punch_in") {
479
480                 if (session) {
481                         lo_send (addr, retpath, "i", session->transport_frame());
482                 }
483
484         } else if (strcmp (argv[0]->s, "punch_out") {
485
486                 if (session) {
487                         lo_send (addr, retpath, "i", session->transport_frame());
488                 }
489
490         } else if (strcmp (argv[0]->s, "rec_enable") {
491
492                 if (session) {
493                         lo_send (addr, retpath, "i", session->transport_frame());
494                 }
495
496         } else {
497
498                 /* error */
499         }
500 #endif
501         return 0;
502 }