d758e29f03d93c6fcfef9816aa9cf2d05a0e551d
[ardour.git] / libs / surfaces / osc / 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 <cstdio>
21 #include <cstdlib>
22 #include <cerrno>
23 #include <algorithm>
24
25 #include <unistd.h>
26 #include <fcntl.h>
27
28 #include "pbd/gstdio_compat.h"
29 #include <glibmm/miscutils.h>
30
31 #include <pbd/convert.h>
32 #include <pbd/pthread_utils.h>
33 #include <pbd/file_utils.h>
34 #include <pbd/failed_constructor.h>
35
36 #include "ardour/amp.h"
37 #include "ardour/session.h"
38 #include "ardour/route.h"
39 #include "ardour/audio_track.h"
40 #include "ardour/midi_track.h"
41 #include "ardour/dB.h"
42 #include "ardour/filesystem_paths.h"
43 #include "ardour/panner.h"
44 #include "ardour/plugin.h"
45 #include "ardour/plugin_insert.h"
46 #include "ardour/send.h"
47
48 #include "osc.h"
49 #include "osc_controllable.h"
50 #include "osc_route_observer.h"
51 #include "i18n.h"
52
53 using namespace ARDOUR;
54 using namespace std;
55 using namespace Glib;
56 using namespace ArdourSurface;
57
58 #include "pbd/abstract_ui.cc" // instantiate template
59
60 OSC* OSC::_instance = 0;
61
62 #ifdef DEBUG
63 static void error_callback(int num, const char *m, const char *path)
64 {
65         fprintf(stderr, "liblo server error %d in path %s: %s\n", num, path, m);
66 }
67 #else
68 static void error_callback(int, const char *, const char *)
69 {
70
71 }
72 #endif
73
74 OSC::OSC (Session& s, uint32_t port)
75         : ControlProtocol (s, X_("Open Sound Control (OSC)"))
76         , AbstractUI<OSCUIRequest> (name())
77         , local_server (0)
78         , remote_server (0)
79         , _port(port)
80         , _ok (true)
81         , _shutdown (false)
82         , _osc_server (0)
83         , _osc_unix_server (0)
84         , _send_route_changes (true)
85         , _debugmode (Off)
86         , gui (0)
87 {
88         _instance = this;
89
90         session->Exported.connect (*this, MISSING_INVALIDATOR, boost::bind (&OSC::session_exported, this, _1, _2), this);
91 }
92
93 OSC::~OSC()
94 {
95         stop ();
96         _instance = 0;
97 }
98
99 void*
100 OSC::request_factory (uint32_t num_requests)
101 {
102         /* AbstractUI<T>::request_buffer_factory() is a template method only
103            instantiated in this source module. To provide something visible for
104            use in the interface/descriptor, we have this static method that is
105            template-free.
106         */
107         return request_buffer_factory (num_requests);
108 }
109
110 void
111 OSC::do_request (OSCUIRequest* req)
112 {
113         if (req->type == CallSlot) {
114
115                 call_slot (MISSING_INVALIDATOR, req->the_slot);
116
117         } else if (req->type == Quit) {
118
119                 stop ();
120         }
121 }
122
123 int
124 OSC::set_active (bool yn)
125 {
126         if (yn != active()) {
127
128                 if (yn) {
129                         if (start ()) {
130                                 return -1;
131                         }
132                 } else {
133                         if (stop ()) {
134                                 return -1;
135                         }
136                 }
137
138         }
139
140         return ControlProtocol::set_active (yn);
141 }
142
143 bool
144 OSC::get_active () const
145 {
146         return _osc_server != 0;
147 }
148
149 int
150 OSC::set_feedback (bool yn)
151 {
152         _send_route_changes = yn;
153         return 0;
154 }
155
156 bool
157 OSC::get_feedback () const
158 {
159         return _send_route_changes;
160 }
161
162 int
163 OSC::start ()
164 {
165         char tmpstr[255];
166
167         if (_osc_server) {
168                 /* already started */
169                 return 0;
170         }
171
172         for (int j=0; j < 20; ++j) {
173                 snprintf(tmpstr, sizeof(tmpstr), "%d", _port);
174
175                 //if ((_osc_server = lo_server_new_with_proto (tmpstr, LO_TCP, error_callback))) {
176                 //      break;
177                 //}
178
179                 if ((_osc_server = lo_server_new (tmpstr, error_callback))) {
180                         break;
181                 }
182
183 #ifdef DEBUG
184                 cerr << "can't get osc at port: " << _port << endl;
185 #endif
186                 _port++;
187                 continue;
188         }
189
190         if (!_osc_server) {
191                 return 1;
192         }
193
194 #ifdef ARDOUR_OSC_UNIX_SERVER
195
196         // APPEARS sluggish for now
197
198         // attempt to create unix socket server too
199
200         snprintf(tmpstr, sizeof(tmpstr), "/tmp/sooperlooper_XXXXXX");
201         int fd = mkstemp(tmpstr);
202
203         if (fd >= 0 ) {
204                 ::g_unlink (tmpstr);
205                 close (fd);
206
207                 _osc_unix_server = lo_server_new (tmpstr, error_callback);
208
209                 if (_osc_unix_server) {
210                         _osc_unix_socket_path = tmpstr;
211                 }
212         }
213 #endif
214
215         PBD::info << "OSC @ " << get_server_url () << endmsg;
216
217         std::string url_file;
218
219         if (find_file (ardour_config_search_path(), "osc_url", url_file)) {
220                 _osc_url_file = url_file;
221                 if (g_file_set_contents (_osc_url_file.c_str(), get_server_url().c_str(), -1, NULL)) {
222                         cerr << "Couldn't write '" <<  _osc_url_file << "'" <<endl;
223                 }
224         }
225
226         register_callbacks();
227
228         session_loaded (*session);
229
230         // lo_server_thread_add_method(_sthread, NULL, NULL, OSC::_dummy_handler, this);
231
232         /* startup the event loop thread */
233
234         BaseUI::run ();
235
236         return 0;
237 }
238
239 void
240 OSC::thread_init ()
241 {
242         pthread_set_name (event_loop_name().c_str());
243
244         if (_osc_unix_server) {
245                 Glib::RefPtr<IOSource> src = IOSource::create (lo_server_get_socket_fd (_osc_unix_server), IO_IN|IO_HUP|IO_ERR);
246                 src->connect (sigc::bind (sigc::mem_fun (*this, &OSC::osc_input_handler), _osc_unix_server));
247                 src->attach (_main_loop->get_context());
248                 local_server = src->gobj();
249                 g_source_ref (local_server);
250         }
251
252         if (_osc_server) {
253 #ifdef PLATFORM_WINDOWS
254                 Glib::RefPtr<IOChannel> chan = Glib::IOChannel::create_from_win32_socket (lo_server_get_socket_fd (_osc_server));
255                 Glib::RefPtr<IOSource> src  = IOSource::create (chan, IO_IN|IO_HUP|IO_ERR);
256 #else
257                 Glib::RefPtr<IOSource> src  = IOSource::create (lo_server_get_socket_fd (_osc_server), IO_IN|IO_HUP|IO_ERR);
258 #endif
259                 src->connect (sigc::bind (sigc::mem_fun (*this, &OSC::osc_input_handler), _osc_server));
260                 src->attach (_main_loop->get_context());
261                 remote_server = src->gobj();
262                 g_source_ref (remote_server);
263         }
264
265         PBD::notify_event_loops_about_thread_creation (pthread_self(), event_loop_name(), 2048);
266         SessionEvent::create_per_thread_pool (event_loop_name(), 128);
267 }
268
269 int
270 OSC::stop ()
271 {
272         /* stop main loop */
273
274         if (local_server) {
275                 g_source_destroy (local_server);
276                 g_source_unref (local_server);
277                 local_server = 0;
278         }
279
280         if (remote_server) {
281                 g_source_destroy (remote_server);
282                 g_source_unref (remote_server);
283                 remote_server = 0;
284         }
285
286         BaseUI::quit ();
287
288         if (_osc_server) {
289                 lo_server_free (_osc_server);
290                 _osc_server = 0;
291         }
292
293         if (_osc_unix_server) {
294                 lo_server_free (_osc_unix_server);
295                 _osc_unix_server = 0;
296         }
297
298         if (!_osc_unix_socket_path.empty()) {
299                 ::g_unlink (_osc_unix_socket_path.c_str());
300         }
301
302         if (!_osc_url_file.empty() ) {
303                 ::g_unlink (_osc_url_file.c_str() );
304         }
305
306         // Delete any active route observers
307         for (RouteObservers::iterator x = route_observers.begin(); x != route_observers.end();) {
308
309                 OSCRouteObserver* rc;
310
311                 if ((rc = dynamic_cast<OSCRouteObserver*>(*x)) != 0) {
312                         delete *x;
313                         x = route_observers.erase (x);
314                 } else {
315                         ++x;
316                 }
317         }
318
319         return 0;
320 }
321
322 void
323 OSC::register_callbacks()
324 {
325         lo_server srvs[2];
326         lo_server serv;
327
328         srvs[0] = _osc_server;
329         srvs[1] = _osc_unix_server;
330
331         for (size_t i = 0; i < 2; ++i) {
332
333                 if (!srvs[i]) {
334                         continue;
335                 }
336
337                 serv = srvs[i];
338
339
340 #define REGISTER_CALLBACK(serv,path,types, function) lo_server_add_method (serv, path, types, OSC::_ ## function, this)
341
342                 REGISTER_CALLBACK (serv, "/routes/list", "", routes_list);
343                 REGISTER_CALLBACK (serv, "/ardour/add_marker", "", add_marker);
344                 REGISTER_CALLBACK (serv, "/ardour/access_action", "s", access_action);
345                 REGISTER_CALLBACK (serv, "/ardour/loop_toggle", "", loop_toggle);
346                 REGISTER_CALLBACK (serv, "/ardour/loop_location", "ii", loop_location);
347                 REGISTER_CALLBACK (serv, "/ardour/goto_start", "", goto_start);
348                 REGISTER_CALLBACK (serv, "/ardour/goto_end", "", goto_end);
349                 REGISTER_CALLBACK (serv, "/ardour/rewind", "", rewind);
350                 REGISTER_CALLBACK (serv, "/ardour/ffwd", "", ffwd);
351                 REGISTER_CALLBACK (serv, "/ardour/transport_stop", "", transport_stop);
352                 REGISTER_CALLBACK (serv, "/ardour/transport_play", "", transport_play);
353                 REGISTER_CALLBACK (serv, "/ardour/transport_frame", "", transport_frame);
354                 REGISTER_CALLBACK (serv, "/ardour/transport_speed", "", transport_speed);
355                 REGISTER_CALLBACK (serv, "/ardour/record_enabled", "", record_enabled);
356                 REGISTER_CALLBACK (serv, "/ardour/set_transport_speed", "f", set_transport_speed);
357                 REGISTER_CALLBACK (serv, "/ardour/locate", "ii", locate);
358                 REGISTER_CALLBACK (serv, "/ardour/save_state", "", save_state);
359                 REGISTER_CALLBACK (serv, "/ardour/prev_marker", "", prev_marker);
360                 REGISTER_CALLBACK (serv, "/ardour/next_marker", "", next_marker);
361                 REGISTER_CALLBACK (serv, "/ardour/undo", "", undo);
362                 REGISTER_CALLBACK (serv, "/ardour/redo", "", redo);
363                 REGISTER_CALLBACK (serv, "/ardour/toggle_punch_in", "", toggle_punch_in);
364                 REGISTER_CALLBACK (serv, "/ardour/toggle_punch_out", "", toggle_punch_out);
365                 REGISTER_CALLBACK (serv, "/ardour/rec_enable_toggle", "", rec_enable_toggle);
366                 REGISTER_CALLBACK (serv, "/ardour/toggle_all_rec_enables", "", toggle_all_rec_enables);
367                 REGISTER_CALLBACK (serv, "/ardour/all_tracks_rec_in", "f", all_tracks_rec_in);
368                 REGISTER_CALLBACK (serv, "/ardour/all_tracks_rec_out", "f", all_tracks_rec_out);
369                 REGISTER_CALLBACK (serv, "/ardour/remove_marker", "", remove_marker_at_playhead);
370                 REGISTER_CALLBACK (serv, "/ardour/jump_bars", "f", jump_by_bars);
371                 REGISTER_CALLBACK (serv, "/ardour/jump_seconds", "f", jump_by_seconds);
372                 REGISTER_CALLBACK (serv, "/ardour/mark_in", "", mark_in);
373                 REGISTER_CALLBACK (serv, "/ardour/mark_out", "", mark_out);
374                 REGISTER_CALLBACK (serv, "/ardour/toggle_click", "", toggle_click);
375                 REGISTER_CALLBACK (serv, "/ardour/midi_panic", "", midi_panic);
376                 REGISTER_CALLBACK (serv, "/ardour/toggle_roll", "", toggle_roll);
377                 REGISTER_CALLBACK (serv, "/ardour/stop_forget", "", stop_forget);
378                 REGISTER_CALLBACK (serv, "/ardour/set_punch_range", "", set_punch_range);
379                 REGISTER_CALLBACK (serv, "/ardour/set_loop_range", "", set_loop_range);
380                 REGISTER_CALLBACK (serv, "/ardour/set_session_range", "", set_session_range);
381                 REGISTER_CALLBACK (serv, "/ardour/toggle_monitor_mute", "", toggle_monitor_mute);
382                 REGISTER_CALLBACK (serv, "/ardour/toggle_monitor_dim", "", toggle_monitor_dim);
383                 REGISTER_CALLBACK (serv, "/ardour/toggle_monitor_mono", "", toggle_monitor_mono);
384                 REGISTER_CALLBACK (serv, "/ardour/quick_snapshot_switch", "", quick_snapshot_switch);
385                 REGISTER_CALLBACK (serv, "/ardour/quick_snapshot_stay", "", quick_snapshot_stay);
386                 REGISTER_CALLBACK (serv, "/ardour/fit_1_track", "", fit_1_track);
387                 REGISTER_CALLBACK (serv, "/ardour/fit_2_tracks", "", fit_2_tracks);
388                 REGISTER_CALLBACK (serv, "/ardour/fit_4_tracks", "", fit_4_tracks);
389                 REGISTER_CALLBACK (serv, "/ardour/fit_8_tracks", "", fit_8_tracks);
390                 REGISTER_CALLBACK (serv, "/ardour/fit_16_tracks", "", fit_16_tracks);
391                 REGISTER_CALLBACK (serv, "/ardour/fit_32_tracks", "", fit_32_tracks);
392                 REGISTER_CALLBACK (serv, "/ardour/fit_all_tracks", "", fit_all_tracks);
393                 REGISTER_CALLBACK (serv, "/ardour/zoom_100_ms", "", zoom_100_ms);
394                 REGISTER_CALLBACK (serv, "/ardour/zoom_1_sec", "", zoom_1_sec);
395                 REGISTER_CALLBACK (serv, "/ardour/zoom_10_sec", "", zoom_10_sec);
396                 REGISTER_CALLBACK (serv, "/ardour/zoom_1_min", "", zoom_1_min);
397                 REGISTER_CALLBACK (serv, "/ardour/zoom_5_min", "", zoom_5_min);
398                 REGISTER_CALLBACK (serv, "/ardour/zoom_10_min", "", zoom_10_min);
399                 REGISTER_CALLBACK (serv, "/ardour/zoom_to_session", "", zoom_to_session);
400                 REGISTER_CALLBACK (serv, "/ardour/temporal_zoom_in", "f", temporal_zoom_in);
401                 REGISTER_CALLBACK (serv, "/ardour/temporal_zoom_out", "f", temporal_zoom_out);
402                 REGISTER_CALLBACK (serv, "/ardour/scroll_up_1_track", "f", scroll_up_1_track);
403                 REGISTER_CALLBACK (serv, "/ardour/scroll_dn_1_track", "f", scroll_dn_1_track);
404                 REGISTER_CALLBACK (serv, "/ardour/scroll_up_1_page", "f", scroll_up_1_page);
405                 REGISTER_CALLBACK (serv, "/ardour/scroll_dn_1_page", "f", scroll_dn_1_page);
406
407
408                 /*
409                  * NOTE: these messages are provided for (arguably broken) apps
410                  *   that MUST send float args ( TouchOSC and Lemur ).
411                  * Normally these ardour transport messages don't require an argument,
412                  * so we're providing redundant calls with vestigial "float" args.
413                  *
414                  * These controls are active on 1.0 only (to prevent duplicate action on
415                  * press "/button 1", and release "/button 0")
416                  * http://hexler.net/docs/touchosc-controls-reference
417                  */
418                 REGISTER_CALLBACK (serv, "/ardour/pushbutton/loop_toggle", "f", loop_toggle);
419                 REGISTER_CALLBACK (serv, "/ardour/pushbutton/add_marker", "f", add_marker);
420                 REGISTER_CALLBACK (serv, "/ardour/pushbutton/goto_start", "f", goto_start);
421                 REGISTER_CALLBACK (serv, "/ardour/pushbutton/goto_end", "f", goto_end);
422                 REGISTER_CALLBACK (serv, "/ardour/pushbutton/rewind", "f", rewind);
423                 REGISTER_CALLBACK (serv, "/ardour/pushbutton/ffwd", "f", ffwd);
424                 REGISTER_CALLBACK (serv, "/ardour/pushbutton/transport_stop", "f", transport_stop);
425                 REGISTER_CALLBACK (serv, "/ardour/pushbutton/transport_play", "f", transport_play);
426                 REGISTER_CALLBACK (serv, "/ardour/pushbutton/save_state", "f", save_state);
427                 REGISTER_CALLBACK (serv, "/ardour/pushbutton/prev_marker", "f", prev_marker);
428                 REGISTER_CALLBACK (serv, "/ardour/pushbutton/next_marker", "f", next_marker);
429                 REGISTER_CALLBACK (serv, "/ardour/pushbutton/undo", "f", undo);
430                 REGISTER_CALLBACK (serv, "/ardour/pushbutton/redo", "f", redo);
431                 REGISTER_CALLBACK (serv, "/ardour/pushbutton/toggle_punch_in", "f", toggle_punch_in);
432                 REGISTER_CALLBACK (serv, "/ardour/pushbutton/toggle_punch_out", "f", toggle_punch_out);
433                 REGISTER_CALLBACK (serv, "/ardour/pushbutton/rec_enable_toggle", "f", rec_enable_toggle);
434                 REGISTER_CALLBACK (serv, "/ardour/pushbutton/toggle_all_rec_enables", "f", toggle_all_rec_enables);
435                 REGISTER_CALLBACK (serv, "/ardour/pushbutton/all_tracks_rec_in", "f", all_tracks_rec_in);
436                 REGISTER_CALLBACK (serv, "/ardour/pushbutton/all_tracks_rec_out", "f", all_tracks_rec_out);
437                 REGISTER_CALLBACK (serv, "/ardour/pushbutton/remove_marker", "f", remove_marker_at_playhead);
438                 REGISTER_CALLBACK (serv, "/ardour/pushbutton/mark_in", "f", mark_in);
439                 REGISTER_CALLBACK (serv, "/ardour/pushbutton/mark_out", "f", mark_out);
440                 REGISTER_CALLBACK (serv, "/ardour/pushbutton/toggle_click", "f", toggle_click);
441                 REGISTER_CALLBACK (serv, "/ardour/pushbutton/midi_panic", "f", midi_panic);
442                 REGISTER_CALLBACK (serv, "/ardour/pushbutton/toggle_roll", "f", toggle_roll);
443                 REGISTER_CALLBACK (serv, "/ardour/pushbutton/stop_forget", "f", stop_forget);
444                 REGISTER_CALLBACK (serv, "/ardour/pushbutton/set_punch_range", "f", set_punch_range);
445                 REGISTER_CALLBACK (serv, "/ardour/pushbutton/set_loop_range", "f", set_loop_range);
446                 REGISTER_CALLBACK (serv, "/ardour/pushbutton/set_session_range", "f", set_session_range);
447                 REGISTER_CALLBACK (serv, "/ardour/pushbutton/toggle_monitor_mute", "f", toggle_monitor_mute);
448                 REGISTER_CALLBACK (serv, "/ardour/pushbutton/toggle_monitor_dim", "f", toggle_monitor_dim);
449                 REGISTER_CALLBACK (serv, "/ardour/pushbutton/toggle_monitor_mono", "f", toggle_monitor_mono);
450                 REGISTER_CALLBACK (serv, "/ardour/pushbutton/quick_snapshot_switch", "f", quick_snapshot_switch);
451                 REGISTER_CALLBACK (serv, "/ardour/pushbutton/quick_snapshot_stay", "f", quick_snapshot_stay);
452                 REGISTER_CALLBACK (serv, "/ardour/pushbutton/fit_1_track", "f", fit_1_track);
453                 REGISTER_CALLBACK (serv, "/ardour/pushbutton/fit_2_tracks", "f", fit_2_tracks);
454                 REGISTER_CALLBACK (serv, "/ardour/pushbutton/fit_4_tracks", "f", fit_4_tracks);
455                 REGISTER_CALLBACK (serv, "/ardour/pushbutton/fit_8_tracks", "f", fit_8_tracks);
456                 REGISTER_CALLBACK (serv, "/ardour/pushbutton/fit_16_tracks", "f", fit_16_tracks);
457                 REGISTER_CALLBACK (serv, "/ardour/pushbutton/fit_32_tracks", "f", fit_32_tracks);
458                 REGISTER_CALLBACK (serv, "/ardour/pushbutton/fit_all_tracks", "f", fit_all_tracks);
459                 REGISTER_CALLBACK (serv, "/ardour/pushbutton/zoom_100_ms", "f", zoom_100_ms);
460                 REGISTER_CALLBACK (serv, "/ardour/pushbutton/zoom_1_sec", "f", zoom_1_sec);
461                 REGISTER_CALLBACK (serv, "/ardour/pushbutton/zoom_10_sec", "f", zoom_10_sec);
462                 REGISTER_CALLBACK (serv, "/ardour/pushbutton/zoom_1_min", "f", zoom_1_min);
463                 REGISTER_CALLBACK (serv, "/ardour/pushbutton/zoom_5_min", "f", zoom_5_min);
464                 REGISTER_CALLBACK (serv, "/ardour/pushbutton/zoom_10_min", "f", zoom_10_min);
465                 REGISTER_CALLBACK (serv, "/ardour/pushbutton/zoom_to_session", "f", zoom_to_session);
466                 REGISTER_CALLBACK (serv, "/ardour/pushbutton/temporal_zoom_in", "f", temporal_zoom_in);
467                 REGISTER_CALLBACK (serv, "/ardour/pushbutton/temporal_zoom_out", "f", temporal_zoom_out);
468                 REGISTER_CALLBACK (serv, "/ardour/pushbutton/scroll_up_1_track", "f", scroll_up_1_track);
469                 REGISTER_CALLBACK (serv, "/ardour/pushbutton/scroll_dn_1_track", "f", scroll_dn_1_track);
470                 REGISTER_CALLBACK (serv, "/ardour/pushbutton/scroll_up_1_page", "f", scroll_up_1_page);
471                 REGISTER_CALLBACK (serv, "/ardour/pushbutton/scroll_dn_1_page", "f", scroll_dn_1_page);
472
473                 /* These commands require the route index in addition to the arg; TouchOSC (et al) can't use these  */ 
474                 REGISTER_CALLBACK (serv, "/ardour/routes/mute", "ii", route_mute);
475                 REGISTER_CALLBACK (serv, "/ardour/routes/solo", "ii", route_solo);
476                 REGISTER_CALLBACK (serv, "/ardour/routes/recenable", "ii", route_recenable);
477                 REGISTER_CALLBACK (serv, "/ardour/routes/gainabs", "if", route_set_gain_abs);
478                 REGISTER_CALLBACK (serv, "/ardour/routes/gaindB", "if", route_set_gain_dB);
479                 REGISTER_CALLBACK (serv, "/ardour/routes/gainfader", "if", route_set_gain_fader);
480                 REGISTER_CALLBACK (serv, "/ardour/routes/trimabs", "if", route_set_trim_abs);
481                 REGISTER_CALLBACK (serv, "/ardour/routes/trimdB", "if", route_set_trim_dB);
482                 REGISTER_CALLBACK (serv, "/ardour/routes/pan_stereo_position", "if", route_set_pan_stereo_position);
483                 REGISTER_CALLBACK (serv, "/ardour/routes/pan_stereo_width", "if", route_set_pan_stereo_width);
484                 REGISTER_CALLBACK (serv, "/ardour/routes/plugin/parameter", "iiif", route_plugin_parameter);
485                 REGISTER_CALLBACK (serv, "/ardour/routes/plugin/parameter/print", "iii", route_plugin_parameter_print);
486                 REGISTER_CALLBACK (serv, "/ardour/routes/send/gainabs", "iif", route_set_send_gain_abs);
487                 REGISTER_CALLBACK (serv, "/ardour/routes/send/gaindB", "iif", route_set_send_gain_dB);
488
489                 /* still not-really-standardized query interface */
490                 //REGISTER_CALLBACK (serv, "/ardour/*/#current_value", "", current_value);
491                 //REGISTER_CALLBACK (serv, "/ardour/set", "", set);
492
493                 // un/register_update args= s:ctrl s:returl s:retpath
494                 //lo_server_add_method(serv, "/register_update", "sss", OSC::global_register_update_handler, this);
495                 //lo_server_add_method(serv, "/unregister_update", "sss", OSC::global_unregister_update_handler, this);
496                 //lo_server_add_method(serv, "/register_auto_update", "siss", OSC::global_register_auto_update_handler, this);
497                 //lo_server_add_method(serv, "/unregister_auto_update", "sss", OSC::_global_unregister_auto_update_handler, this);
498
499                 /* this is a special catchall handler,
500                  * register at the end so this is only called if no
501                  * other handler matches (used for debug) */
502                 lo_server_add_method (serv, 0, 0, _catchall, this);
503         }
504 }
505
506 bool
507 OSC::osc_input_handler (IOCondition ioc, lo_server srv)
508 {
509         if (ioc & ~IO_IN) {
510                 return false;
511         }
512
513         if (ioc & IO_IN) {
514                 lo_server_recv (srv);
515         }
516
517         return true;
518 }
519
520 std::string
521 OSC::get_server_url()
522 {
523         string url;
524         char * urlstr;
525
526         if (_osc_server) {
527                 urlstr = lo_server_get_url (_osc_server);
528                 url = urlstr;
529                 free (urlstr);
530         }
531
532         return url;
533 }
534
535 std::string
536 OSC::get_unix_server_url()
537 {
538         string url;
539         char * urlstr;
540
541         if (_osc_unix_server) {
542                 urlstr = lo_server_get_url (_osc_unix_server);
543                 url = urlstr;
544                 free (urlstr);
545         }
546
547         return url;
548 }
549
550 void
551 OSC::listen_to_route (boost::shared_ptr<Route> route, lo_address addr)
552 {
553         /* avoid duplicate listens */
554
555         for (RouteObservers::iterator x = route_observers.begin(); x != route_observers.end(); ++x) {
556
557                 OSCRouteObserver* ro;
558
559                 if ((ro = dynamic_cast<OSCRouteObserver*>(*x)) != 0) {
560
561                         int res = strcmp(lo_address_get_hostname(ro->address()), lo_address_get_hostname(addr));
562
563                         if (ro->route() == route && res == 0) {
564                                 return;
565                         }
566                 }
567         }
568
569         OSCRouteObserver* o = new OSCRouteObserver (route, addr);
570         route_observers.push_back (o);
571
572         route->DropReferences.connect (*this, MISSING_INVALIDATOR, boost::bind (&OSC::drop_route, this, boost::weak_ptr<Route> (route)), this);
573 }
574
575 void
576 OSC::drop_route (boost::weak_ptr<Route> wr)
577 {
578         boost::shared_ptr<Route> r = wr.lock ();
579
580         if (!r) {
581                 return;
582         }
583
584         for (RouteObservers::iterator x = route_observers.begin(); x != route_observers.end();) {
585
586                 OSCRouteObserver* rc;
587
588                 if ((rc = dynamic_cast<OSCRouteObserver*>(*x)) != 0) {
589
590                         if (rc->route() == r) {
591                                 delete *x;
592                                 x = route_observers.erase (x);
593                         } else {
594                                 ++x;
595                         }
596                 } else {
597                         ++x;
598                 }
599         }
600 }
601
602 void
603 OSC::end_listen (boost::shared_ptr<Route> r, lo_address addr)
604 {
605         RouteObservers::iterator x;
606
607         // Remove the route observers
608         for (x = route_observers.begin(); x != route_observers.end();) {
609
610                 OSCRouteObserver* ro;
611
612                 if ((ro = dynamic_cast<OSCRouteObserver*>(*x)) != 0) {
613
614                         int res = strcmp(lo_address_get_hostname(ro->address()), lo_address_get_hostname(addr));
615
616                         if (ro->route() == r && res == 0) {
617                                 delete *x;
618                                 x = route_observers.erase (x);
619                         }
620                         else {
621                                 ++x;
622                         }
623                 }
624                 else {
625                         ++x;
626                 }
627         }
628 }
629
630 void
631 OSC::current_value_query (const char* path, size_t len, lo_arg **argv, int argc, lo_message msg)
632 {
633         char* subpath;
634
635         subpath = (char*) malloc (len-15+1);
636         memcpy (subpath, path, len-15);
637         subpath[len-15] = '\0';
638
639         send_current_value (subpath, argv, argc, msg);
640
641         free (subpath);
642 }
643
644 void
645 OSC::send_current_value (const char* path, lo_arg** argv, int argc, lo_message msg)
646 {
647         if (!session) {
648                 return;
649         }
650
651         lo_message reply = lo_message_new ();
652         boost::shared_ptr<Route> r;
653         int id;
654
655         lo_message_add_string (reply, path);
656
657         if (argc == 0) {
658                 lo_message_add_string (reply, "bad syntax");
659         } else {
660                 id = argv[0]->i;
661                 r = session->route_by_remote_id (id);
662
663                 if (!r) {
664                         lo_message_add_string (reply, "not found");
665                 } else {
666
667                         if (strcmp (path, "/routes/state") == 0) {
668
669                                 if (boost::dynamic_pointer_cast<AudioTrack>(r)) {
670                                         lo_message_add_string (reply, "AT");
671                                 } else if (boost::dynamic_pointer_cast<MidiTrack>(r)) {
672                                         lo_message_add_string (reply, "MT");
673                                 } else {
674                                         lo_message_add_string (reply, "B");
675                                 }
676
677                                 lo_message_add_string (reply, r->name().c_str());
678                                 lo_message_add_int32 (reply, r->n_inputs().n_audio());
679                                 lo_message_add_int32 (reply, r->n_outputs().n_audio());
680                                 lo_message_add_int32 (reply, r->muted());
681                                 lo_message_add_int32 (reply, r->soloed());
682
683                         } else if (strcmp (path, "/routes/mute") == 0) {
684
685                                 lo_message_add_int32 (reply, (float) r->muted());
686
687                         } else if (strcmp (path, "/routes/solo") == 0) {
688
689                                 lo_message_add_int32 (reply, r->soloed());
690                         }
691                 }
692         }
693
694         lo_send_message (lo_message_get_source (msg), "#reply", reply);
695         lo_message_free (reply);
696 }
697
698 int
699 OSC::_catchall (const char *path, const char *types, lo_arg **argv, int argc, void *data, void *user_data)
700 {
701         return ((OSC*)user_data)->catchall (path, types, argv, argc, data);
702 }
703
704 int
705 OSC::catchall (const char *path, const char* types, lo_arg **argv, int argc, lo_message msg)
706 {
707         size_t len;
708         int ret = 1; /* unhandled */
709
710         //cerr << "Received a message, path = " << path << " types = \""
711         //     << (types ? types : "NULL") << '"' << endl;
712
713         /* 15 for /#current_value plus 2 for /<path> */
714
715         len = strlen (path);
716
717         if (len >= 17 && !strcmp (&path[len-15], "/#current_value")) {
718                 current_value_query (path, len, argv, argc, msg);
719                 ret = 0;
720
721         } else if (strcmp (path, "/routes/listen") == 0) {
722
723                 cerr << "set up listener\n";
724
725                 lo_message reply = lo_message_new ();
726
727                 if (argc <= 0) {
728                         lo_message_add_string (reply, "syntax error");
729                 } else {
730                         for (int n = 0; n < argc; ++n) {
731
732                                 boost::shared_ptr<Route> r = session->route_by_remote_id (argv[n]->i);
733
734                                 if (!r) {
735                                         lo_message_add_string (reply, "not found");
736                                         cerr << "no such route\n";
737                                         break;
738                                 } else {
739                                         cerr << "add listener\n";
740                                         listen_to_route (r, lo_message_get_source (msg));
741                                         lo_message_add_int32 (reply, argv[n]->i);
742                                 }
743                         }
744                 }
745
746                 lo_send_message (lo_message_get_source (msg), "#reply", reply);
747                 lo_message_free (reply);
748
749                 ret = 0;
750
751         } else if (strcmp (path, "/routes/ignore") == 0) {
752
753                 for (int n = 0; n < argc; ++n) {
754
755                         boost::shared_ptr<Route> r = session->route_by_remote_id (argv[n]->i);
756
757                         if (r) {
758                                 end_listen (r, lo_message_get_source (msg));
759                         }
760                 }
761
762                 ret = 0;
763         } else if (argc == 1 && types[0] == 'f') { // single float -- probably TouchOSC
764                 if (!strncmp (path, "/ardour/routes/gainabs/", 23) && strlen (path) > 23) {
765                         int rid = atoi (&path[23]);
766                         // use some power-scale mapping??
767                         route_set_gain_abs (rid, argv[0]->f);
768                         ret = 0;
769                 }
770                 else if (!strncmp (path, "/ardour/routes/trimabs/", 23) && strlen (path) > 23) {
771                         int rid = atoi (&path[23]);
772                         // normalize 0..1 ?
773                         route_set_trim_abs (rid, argv[0]->f);
774                         ret = 0;
775                 }
776                 else if (!strncmp (path, "/ardour/routes/mute/", 20) && strlen (path) > 20) {
777                         int rid = atoi (&path[20]);
778                         route_mute (rid, argv[0]->f == 1.0);
779                         ret = 0;
780                 }
781                 else if (!strncmp (path, "/ardour/routes/solo/", 20) && strlen (path) > 20) {
782                         int rid = atoi (&path[20]);
783                         route_solo (rid, argv[0]->f == 1.0);
784                         ret = 0;
785                 }
786                 else if (!strncmp (path, "/ardour/routes/recenable/", 25) && strlen (path) > 25) {
787                         int rid = atoi (&path[25]);
788                         route_recenable (rid, argv[0]->f == 1.0);
789                         ret = 0;
790                 }
791         }
792
793         if ((ret && _debugmode == Unhandled)) {
794                 debugmsg (_("Unhandled OSC message"), path, types, argv, argc);
795         }
796
797         return ret;
798 }
799
800 void
801 OSC::debugmsg (const char *prefix, const char *path, const char* types, lo_arg **argv, int argc)
802 {
803         std::stringstream ss;
804         for (int i = 0; i < argc; ++i) {
805                 lo_type type = (lo_type)types[i];
806                         ss << " ";
807                 switch (type) {
808                         case LO_INT32:
809                                 ss << "i:" << argv[i]->i;
810                                 break;
811                         case LO_FLOAT:
812                                 ss << "f:" << argv[i]->f;
813                                 break;
814                         case LO_DOUBLE:
815                                 ss << "d:" << argv[i]->d;
816                                 break;
817                         case LO_STRING:
818                                 ss << "s:" << &argv[i]->s;
819                                 break;
820                         case LO_INT64:
821                                 ss << "h:" << argv[i]->h;
822                                 break;
823                         case LO_CHAR:
824                                 ss << "c:" << argv[i]->s;
825                                 break;
826                         case LO_TIMETAG:
827                                 ss << "<Timetag>";
828                                 break;
829                         case LO_BLOB:
830                                 ss << "<BLOB>";
831                                 break;
832                         case LO_TRUE:
833                                 ss << "#T";
834                                 break;
835                         case LO_FALSE:
836                                 ss << "#F";
837                                 break;
838                         case LO_NIL:
839                                 ss << "NIL";
840                                 break;
841                         case LO_INFINITUM:
842                                 ss << "#inf";
843                                 break;
844                         case LO_MIDI:
845                                 ss << "<MIDI>";
846                                 break;
847                         case LO_SYMBOL:
848                                 ss << "<SYMBOL>";
849                                 break;
850                         default:
851                                 ss << "< ?? >";
852                                 break;
853                 }
854         }
855         PBD::info << prefix << ": " << path << ss.str() << endmsg;
856 }
857
858 void
859 OSC::update_clock ()
860 {
861
862 }
863
864 // "Application Hook" Handlers //
865 void
866 OSC::session_loaded (Session& s)
867 {
868 //      lo_address listener = lo_address_new (NULL, "7770");
869 //      lo_send (listener, "/session/loaded", "ss", s.path().c_str(), s.name().c_str());
870 }
871
872 void
873 OSC::session_exported (std::string path, std::string name)
874 {
875         lo_address listener = lo_address_new (NULL, "7770");
876         lo_send (listener, "/session/exported", "ss", path.c_str(), name.c_str());
877         lo_address_free (listener);
878 }
879
880 // end "Application Hook" Handlers //
881
882 /* path callbacks */
883
884 int
885 OSC::current_value (const char */*path*/, const char */*types*/, lo_arg **/*argv*/, int /*argc*/, void */*data*/, void* /*user_data*/)
886 {
887 #if 0
888         const char* returl;
889
890         if (argc < 3 || types == 0 || strlen (types) < 3 || types[0] != 's' || types[1] != 's' || types[2] != s) {
891                 return 1;
892         }
893
894         const char *returl = argv[1]->s;
895         lo_address addr = find_or_cache_addr (returl);
896
897         const char *retpath = argv[2]->s;
898
899
900         if (strcmp (argv[0]->s, "transport_frame") == 0) {
901
902                 if (session) {
903                         lo_send (addr, retpath, "i", session->transport_frame());
904                 }
905
906         } else if (strcmp (argv[0]->s, "transport_speed") == 0) {
907
908                 if (session) {
909                         lo_send (addr, retpath, "i", session->transport_frame());
910                 }
911
912         } else if (strcmp (argv[0]->s, "transport_locked") == 0) {
913
914                 if (session) {
915                         lo_send (addr, retpath, "i", session->transport_frame());
916                 }
917
918         } else if (strcmp (argv[0]->s, "punch_in") == 0) {
919
920                 if (session) {
921                         lo_send (addr, retpath, "i", session->transport_frame());
922                 }
923
924         } else if (strcmp (argv[0]->s, "punch_out") == 0) {
925
926                 if (session) {
927                         lo_send (addr, retpath, "i", session->transport_frame());
928                 }
929
930         } else if (strcmp (argv[0]->s, "rec_enable") == 0) {
931
932                 if (session) {
933                         lo_send (addr, retpath, "i", session->transport_frame());
934                 }
935
936         } else {
937
938                 /* error */
939         }
940 #endif
941         return 0;
942 }
943
944 void
945 OSC::routes_list (lo_message msg)
946 {
947         if (!session) {
948                 return;
949         }
950         for (int n = 0; n < (int) session->nroutes(); ++n) {
951
952                 boost::shared_ptr<Route> r = session->route_by_remote_id (n);
953
954                 if (r) {
955
956                         lo_message reply = lo_message_new ();
957
958                         if (boost::dynamic_pointer_cast<AudioTrack>(r)) {
959                                 lo_message_add_string (reply, "AT");
960                         } else if (boost::dynamic_pointer_cast<MidiTrack>(r)) {
961                                 lo_message_add_string (reply, "MT");
962                         } else {
963                                 lo_message_add_string (reply, "B");
964                         }
965
966                         lo_message_add_string (reply, r->name().c_str());
967                         lo_message_add_int32 (reply, r->n_inputs().n_audio());
968                         lo_message_add_int32 (reply, r->n_outputs().n_audio());
969                         lo_message_add_int32 (reply, r->muted());
970                         lo_message_add_int32 (reply, r->soloed());
971                         lo_message_add_int32 (reply, r->remote_control_id());
972
973                         if (boost::dynamic_pointer_cast<AudioTrack>(r)
974                                         || boost::dynamic_pointer_cast<MidiTrack>(r)) {
975
976                                 boost::shared_ptr<Track> t = boost::dynamic_pointer_cast<Track>(r);
977                                 lo_message_add_int32 (reply, (int32_t) t->rec_enable_control()->get_value());
978                         }
979
980                         //Automatically listen to routes listed
981                         listen_to_route(r, lo_message_get_source (msg));
982
983                         lo_send_message (lo_message_get_source (msg), "#reply", reply);
984                         lo_message_free (reply);
985                 }
986         }
987
988         // Send end of listing message
989         lo_message reply = lo_message_new ();
990
991         lo_message_add_string (reply, "end_route_list");
992         lo_message_add_int64 (reply, session->frame_rate());
993         lo_message_add_int64 (reply, session->current_end_frame());
994
995         lo_send_message (lo_message_get_source (msg), "#reply", reply);
996
997         lo_message_free (reply);
998 }
999
1000 void
1001 OSC::transport_frame (lo_message msg)
1002 {
1003         if (!session) {
1004                 return;
1005         }
1006         framepos_t pos = session->transport_frame ();
1007
1008         lo_message reply = lo_message_new ();
1009         lo_message_add_int64 (reply, pos);
1010
1011         lo_send_message (lo_message_get_source (msg), "/ardour/transport_frame", reply);
1012
1013         lo_message_free (reply);
1014 }
1015
1016 void
1017 OSC::transport_speed (lo_message msg)
1018 {
1019         if (!session) {
1020                 return;
1021         }
1022         double ts = session->transport_speed ();
1023
1024         lo_message reply = lo_message_new ();
1025         lo_message_add_double (reply, ts);
1026
1027         lo_send_message (lo_message_get_source (msg), "/ardour/transport_speed", reply);
1028
1029         lo_message_free (reply);
1030 }
1031
1032 void
1033 OSC::record_enabled (lo_message msg)
1034 {
1035         if (!session) {
1036                 return;
1037         }
1038         int re = (int)session->get_record_enabled ();
1039
1040         lo_message reply = lo_message_new ();
1041         lo_message_add_int32 (reply, re);
1042
1043         lo_send_message (lo_message_get_source (msg), "/ardour/record_enabled", reply);
1044
1045         lo_message_free (reply);
1046 }
1047
1048
1049 int
1050 OSC::route_mute (int rid, int yn)
1051 {
1052         if (!session) return -1;
1053
1054         boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
1055
1056         if (r) {
1057                 r->mute_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
1058         }
1059
1060         return 0;
1061 }
1062
1063 int
1064 OSC::route_solo (int rid, int yn)
1065 {
1066         if (!session) return -1;
1067
1068         boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
1069
1070         if (r) {
1071                 r->solo_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
1072         }
1073
1074         return 0;
1075 }
1076
1077 int
1078 OSC::route_recenable (int rid, int yn)
1079 {
1080         if (!session) return -1;
1081
1082         boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
1083
1084         if (r) {
1085                 boost::shared_ptr<Track> trk = boost::dynamic_pointer_cast<Track> (r);
1086                 if (trk) {
1087                         trk->rec_enable_control()->set_value (yn, PBD::Controllable::UseGroup);
1088                 }
1089         }
1090
1091         return 0;
1092 }
1093
1094 int
1095 OSC::route_set_gain_abs (int rid, float level)
1096 {
1097         if (!session) return -1;
1098
1099         boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
1100
1101         if (r) {
1102                 r->gain_control()->set_value (level, PBD::Controllable::NoGroup);
1103         }
1104
1105         return 0;
1106 }
1107
1108 int
1109 OSC::route_set_gain_dB (int rid, float dB)
1110 {
1111         if (dB < -192) {
1112                 return route_set_gain_abs (rid, 0.0);
1113         }
1114         return route_set_gain_abs (rid, dB_to_coefficient (dB));
1115 }
1116
1117 int
1118 OSC::route_set_gain_fader (int rid, float pos)
1119 {
1120         return route_set_gain_abs (rid, slider_position_to_gain_with_max (pos, 2.0));
1121 }
1122
1123
1124 int
1125 OSC::route_set_trim_abs (int rid, float level)
1126 {
1127         if (!session) return -1;
1128
1129         boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
1130
1131         if (r) {
1132                 r->set_trim (level, PBD::Controllable::NoGroup);
1133         }
1134
1135         return 0;
1136 }
1137
1138 int
1139 OSC::route_set_trim_dB (int rid, float dB)
1140 {
1141         return route_set_trim_abs(rid, dB_to_coefficient (dB));
1142 }
1143
1144
1145 int
1146 OSC::route_set_pan_stereo_position (int rid, float pos)
1147 {
1148         if (!session) return -1;
1149
1150         boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
1151
1152         if (r) {
1153                 boost::shared_ptr<Panner> panner = r->panner();
1154                 if (panner) {
1155                         panner->set_position (pos);
1156                 }
1157         }
1158
1159         return 0;
1160
1161 }
1162
1163 int
1164 OSC::route_set_pan_stereo_width (int rid, float pos)
1165 {
1166         if (!session) return -1;
1167
1168         boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
1169
1170         if (r) {
1171                 boost::shared_ptr<Panner> panner = r->panner();
1172                 if (panner) {
1173                         panner->set_width (pos);
1174                 }
1175         }
1176
1177         return 0;
1178
1179 }
1180
1181 int
1182 OSC::route_set_send_gain_abs (int rid, int sid, float val)
1183 {
1184         if (!session) {
1185                 return -1;
1186         }
1187
1188         boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
1189
1190         if (!r) {
1191                 return -1;
1192         }
1193
1194         /* revert to zero-based counting */
1195
1196         if (sid > 0) {
1197                 --sid;
1198         }
1199
1200         boost::shared_ptr<Processor> p = r->nth_send (sid);
1201
1202         if (p) {
1203                 boost::shared_ptr<Send> s = boost::dynamic_pointer_cast<Send>(p);
1204                 boost::shared_ptr<Amp> a = s->amp();
1205
1206                 if (a) {
1207                         a->gain_control()->set_value (val, PBD::Controllable::NoGroup);
1208                 }
1209         }
1210         return 0;
1211 }
1212
1213 int
1214 OSC::route_set_send_gain_dB (int rid, int sid, float val)
1215 {
1216         if (!session) {
1217                 return -1;
1218         }
1219
1220         boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
1221
1222         if (!r) {
1223                 return -1;
1224         }
1225
1226         /* revert to zero-based counting */
1227
1228         if (sid > 0) {
1229                 --sid;
1230         }
1231
1232         boost::shared_ptr<Processor> p = r->nth_send (sid);
1233
1234         if (p) {
1235                 boost::shared_ptr<Send> s = boost::dynamic_pointer_cast<Send>(p);
1236                 boost::shared_ptr<Amp> a = s->amp();
1237
1238                 if (a) {
1239                         a->gain_control()->set_value (dB_to_coefficient (val), PBD::Controllable::NoGroup);
1240                 }
1241         }
1242         return 0;
1243 }
1244
1245 int
1246 OSC::route_plugin_parameter (int rid, int piid, int par, float val)
1247 {
1248         if (!session)
1249                 return -1;
1250
1251         boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
1252
1253         if (!r) {
1254                 PBD::error << "OSC: Invalid Remote Control ID '" << rid << "'" << endmsg;
1255                 return -1;
1256         }
1257
1258         boost::shared_ptr<Processor> redi=r->nth_plugin (piid);
1259
1260         if (!redi) {
1261                 PBD::error << "OSC: cannot find plugin # " << piid << " for RID '" << rid << "'" << endmsg;
1262                 return -1;
1263         }
1264
1265         boost::shared_ptr<PluginInsert> pi;
1266
1267         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
1268                 PBD::error << "OSC: given processor # " << piid << " on RID '" << rid << "' is not a Plugin." << endmsg;
1269                 return -1;
1270         }
1271
1272         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
1273         bool ok=false;
1274
1275         uint32_t controlid = pip->nth_parameter (par,ok);
1276
1277         if (!ok) {
1278                 PBD::error << "OSC: Cannot find parameter # " << par <<  " for plugin # " << piid << " on RID '" << rid << "'" << endmsg;
1279                 return -1;
1280         }
1281
1282         if (!pip->parameter_is_input(controlid)) {
1283                 PBD::error << "OSC: Parameter # " << par <<  " for plugin # " << piid << " on RID '" << rid << "' is not a control input" << endmsg;
1284                 return -1;
1285         }
1286
1287         ParameterDescriptor pd;
1288         pi->plugin()->get_parameter_descriptor (controlid,pd);
1289
1290         if (val >= pd.lower && val <= pd.upper) {
1291
1292                 boost::shared_ptr<AutomationControl> c = pi->automation_control (Evoral::Parameter(PluginAutomation, 0, controlid));
1293                 // cerr << "parameter:" << redi->describe_parameter(controlid) << " val:" << val << "\n";
1294                 c->set_value (val, PBD::Controllable::NoGroup);
1295         } else {
1296                 PBD::warning << "OSC: Parameter # " << par <<  " for plugin # " << piid << " on RID '" << rid << "' is out of range" << endmsg;
1297                 PBD::info << "OSC: Valid range min=" << pd.lower << " max=" << pd.upper << endmsg;
1298         }
1299
1300         return 0;
1301 }
1302
1303 int
1304 OSC::route_plugin_parameter_print (int rid, int piid, int par)
1305 {
1306         if (!session) {
1307                 return -1;
1308         }
1309
1310         boost::shared_ptr<Route> r = session->route_by_remote_id (rid);
1311
1312         if (!r) {
1313                 return -1;
1314         }
1315
1316         boost::shared_ptr<Processor> redi=r->nth_processor (piid);
1317
1318         if (!redi) {
1319                 return -1;
1320         }
1321
1322         boost::shared_ptr<PluginInsert> pi;
1323
1324         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
1325                 return -1;
1326         }
1327
1328         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
1329         bool ok=false;
1330
1331         uint32_t controlid = pip->nth_parameter (par,ok);
1332
1333         if (!ok) {
1334                 return -1;
1335         }
1336
1337         ParameterDescriptor pd;
1338
1339         if (pi->plugin()->get_parameter_descriptor (controlid, pd) == 0) {
1340                 boost::shared_ptr<AutomationControl> c = pi->automation_control (Evoral::Parameter(PluginAutomation, 0, controlid));
1341
1342                 cerr << "parameter:     " << redi->describe_parameter(controlid)  << "\n";
1343                 cerr << "current value: " << c->get_value ();
1344                 cerr << "lower value:   " << pd.lower << "\n";
1345                 cerr << "upper value:   " << pd.upper << "\n";
1346         }
1347
1348         return 0;
1349 }
1350
1351 XMLNode&
1352 OSC::get_state ()
1353 {
1354         XMLNode& node (ControlProtocol::get_state());
1355         node.add_property("debugmode", (int) _debugmode); // TODO: enum2str
1356         return node;
1357 }
1358
1359 int
1360 OSC::set_state (const XMLNode& node, int version)
1361 {
1362         if (ControlProtocol::set_state (node, version)) {
1363                 return -1;
1364         }
1365         XMLProperty const * p = node.property (X_("debugmode"));
1366         if (p) {
1367                 _debugmode = OSCDebugMode (PBD::atoi(p->value ()));
1368         }
1369
1370         return 0;
1371 }