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