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