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