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