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