2553499ad15c9c55efc8486895cd943f450a11c4
[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.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/monitor_control.h"
42 #include "ardour/dB.h"
43 #include "ardour/filesystem_paths.h"
44 #include "ardour/panner.h"
45 #include "ardour/plugin.h"
46 #include "ardour/plugin_insert.h"
47 #include "ardour/presentation_info.h"
48 #include "ardour/send.h"
49 #include "ardour/internal_send.h"
50 #include "ardour/phase_control.h"
51 #include "ardour/solo_isolate_control.h"
52 #include "ardour/solo_safe_control.h"
53 #include "ardour/vca_manager.h"
54
55 #include "osc_select_observer.h"
56 #include "osc.h"
57 #include "osc_controllable.h"
58 #include "osc_route_observer.h"
59 #include "osc_global_observer.h"
60 #include "osc_cue_observer.h"
61 #include "pbd/i18n.h"
62
63 using namespace ARDOUR;
64 using namespace std;
65 using namespace Glib;
66 using namespace ArdourSurface;
67
68 #include "pbd/abstract_ui.cc" // instantiate template
69
70 OSC* OSC::_instance = 0;
71
72 #ifdef DEBUG
73 static void error_callback(int num, const char *m, const char *path)
74 {
75         fprintf(stderr, "liblo server error %d in path %s: %s\n", num, path, m);
76 }
77 #else
78 static void error_callback(int, const char *, const char *)
79 {
80
81 }
82 #endif
83
84 OSC::OSC (Session& s, uint32_t port)
85         : ControlProtocol (s, X_("Open Sound Control (OSC)"))
86         , AbstractUI<OSCUIRequest> (name())
87         , local_server (0)
88         , remote_server (0)
89         , _port(port)
90         , _ok (true)
91         , _shutdown (false)
92         , _osc_server (0)
93         , _osc_unix_server (0)
94         , _debugmode (Off)
95         , address_only (false)
96         , remote_port ("8000")
97         , default_banksize (0)
98         , default_strip (159)
99         , default_feedback (0)
100         , default_gainmode (0)
101         , tick (true)
102         , bank_dirty (false)
103         , scrub_speed (0)
104         , gui (0)
105 {
106         _instance = this;
107
108         session->Exported.connect (*this, MISSING_INVALIDATOR, boost::bind (&OSC::session_exported, this, _1, _2), this);
109 }
110
111 OSC::~OSC()
112 {
113         stop ();
114         tear_down_gui ();
115         _instance = 0;
116 }
117
118 void*
119 OSC::request_factory (uint32_t num_requests)
120 {
121         /* AbstractUI<T>::request_buffer_factory() is a template method only
122            instantiated in this source module. To provide something visible for
123            use in the interface/descriptor, we have this static method that is
124            template-free.
125         */
126         return request_buffer_factory (num_requests);
127 }
128
129 void
130 OSC::do_request (OSCUIRequest* req)
131 {
132         if (req->type == CallSlot) {
133
134                 call_slot (MISSING_INVALIDATOR, req->the_slot);
135
136         } else if (req->type == Quit) {
137
138                 stop ();
139         }
140 }
141
142 int
143 OSC::set_active (bool yn)
144 {
145         if (yn != active()) {
146
147                 if (yn) {
148                         if (start ()) {
149                                 return -1;
150                         }
151                 } else {
152                         if (stop ()) {
153                                 return -1;
154                         }
155                 }
156
157         }
158
159         return ControlProtocol::set_active (yn);
160 }
161
162 bool
163 OSC::get_active () const
164 {
165         return _osc_server != 0;
166 }
167
168 int
169 OSC::start ()
170 {
171         char tmpstr[255];
172
173         if (_osc_server) {
174                 /* already started */
175                 return 0;
176         }
177
178         for (int j=0; j < 20; ++j) {
179                 snprintf(tmpstr, sizeof(tmpstr), "%d", _port);
180
181                 //if ((_osc_server = lo_server_new_with_proto (tmpstr, LO_TCP, error_callback))) {
182                 //      break;
183                 //}
184
185                 if ((_osc_server = lo_server_new (tmpstr, error_callback))) {
186                         break;
187                 }
188
189 #ifdef DEBUG
190                 cerr << "can't get osc at port: " << _port << endl;
191 #endif
192                 _port++;
193                 continue;
194         }
195
196         if (!_osc_server) {
197                 return 1;
198         }
199
200 #ifdef ARDOUR_OSC_UNIX_SERVER
201
202         // APPEARS sluggish for now
203
204         // attempt to create unix socket server too
205
206         snprintf(tmpstr, sizeof(tmpstr), "/tmp/sooperlooper_XXXXXX");
207         int fd = mkstemp(tmpstr);
208
209         if (fd >= 0 ) {
210                 ::g_unlink (tmpstr);
211                 close (fd);
212
213                 _osc_unix_server = lo_server_new (tmpstr, error_callback);
214
215                 if (_osc_unix_server) {
216                         _osc_unix_socket_path = tmpstr;
217                 }
218         }
219 #endif
220
221         PBD::info << "OSC @ " << get_server_url () << endmsg;
222
223         std::string url_file;
224
225         if (find_file (ardour_config_search_path(), "osc_url", url_file)) {
226                 _osc_url_file = url_file;
227                 if (g_file_set_contents (_osc_url_file.c_str(), get_server_url().c_str(), -1, NULL)) {
228                         cerr << "Couldn't write '" <<  _osc_url_file << "'" <<endl;
229                 }
230         }
231
232         register_callbacks();
233
234         session_loaded (*session);
235
236         // lo_server_thread_add_method(_sthread, NULL, NULL, OSC::_dummy_handler, this);
237
238         /* startup the event loop thread */
239
240         BaseUI::run ();
241
242         // start timers for metering, timecode and heartbeat.
243         // timecode and metering run at 100
244         Glib::RefPtr<Glib::TimeoutSource> periodic_timeout = Glib::TimeoutSource::create (100); // milliseconds
245         periodic_connection = periodic_timeout->connect (sigc::mem_fun (*this, &OSC::periodic));
246         periodic_timeout->attach (main_loop()->get_context());
247
248         // catch track reordering
249         // receive routes added
250         session->RouteAdded.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&OSC::notify_routes_added, this, _1), this);
251         // receive VCAs added
252         session->vca_manager().VCAAdded.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&OSC::notify_vca_added, this, _1), this);
253         // order changed
254         PresentationInfo::Change.connect (session_connections, MISSING_INVALIDATOR, boost::bind (&OSC::recalcbanks, this), this);
255
256         _select = boost::shared_ptr<Stripable>();
257
258         return 0;
259 }
260
261 void
262 OSC::thread_init ()
263 {
264         pthread_set_name (event_loop_name().c_str());
265
266         if (_osc_unix_server) {
267                 Glib::RefPtr<IOSource> src = IOSource::create (lo_server_get_socket_fd (_osc_unix_server), IO_IN|IO_HUP|IO_ERR);
268                 src->connect (sigc::bind (sigc::mem_fun (*this, &OSC::osc_input_handler), _osc_unix_server));
269                 src->attach (_main_loop->get_context());
270                 local_server = src->gobj();
271                 g_source_ref (local_server);
272         }
273
274         if (_osc_server) {
275 #ifdef PLATFORM_WINDOWS
276                 Glib::RefPtr<IOChannel> chan = Glib::IOChannel::create_from_win32_socket (lo_server_get_socket_fd (_osc_server));
277                 Glib::RefPtr<IOSource> src  = IOSource::create (chan, IO_IN|IO_HUP|IO_ERR);
278 #else
279                 Glib::RefPtr<IOSource> src  = IOSource::create (lo_server_get_socket_fd (_osc_server), IO_IN|IO_HUP|IO_ERR);
280 #endif
281                 src->connect (sigc::bind (sigc::mem_fun (*this, &OSC::osc_input_handler), _osc_server));
282                 src->attach (_main_loop->get_context());
283                 remote_server = src->gobj();
284                 g_source_ref (remote_server);
285         }
286
287         PBD::notify_event_loops_about_thread_creation (pthread_self(), event_loop_name(), 2048);
288         SessionEvent::create_per_thread_pool (event_loop_name(), 128);
289 }
290
291 int
292 OSC::stop ()
293 {
294         /* stop main loop */
295
296         if (local_server) {
297                 g_source_destroy (local_server);
298                 g_source_unref (local_server);
299                 local_server = 0;
300         }
301
302         if (remote_server) {
303                 g_source_destroy (remote_server);
304                 g_source_unref (remote_server);
305                 remote_server = 0;
306         }
307
308         BaseUI::quit ();
309
310         if (_osc_server) {
311                 lo_server_free (_osc_server);
312                 _osc_server = 0;
313         }
314
315         if (_osc_unix_server) {
316                 lo_server_free (_osc_unix_server);
317                 _osc_unix_server = 0;
318         }
319
320         if (!_osc_unix_socket_path.empty()) {
321                 ::g_unlink (_osc_unix_socket_path.c_str());
322         }
323
324         if (!_osc_url_file.empty() ) {
325                 ::g_unlink (_osc_url_file.c_str() );
326         }
327
328         periodic_connection.disconnect ();
329         session_connections.drop_connections ();
330         cueobserver_connections.drop_connections ();
331         // Delete any active route observers
332         for (RouteObservers::iterator x = route_observers.begin(); x != route_observers.end();) {
333
334                 OSCRouteObserver* rc;
335
336                 if ((rc = dynamic_cast<OSCRouteObserver*>(*x)) != 0) {
337                         delete *x;
338                         x = route_observers.erase (x);
339                 } else {
340                         ++x;
341                 }
342         }
343 // Should maybe do global_observers too
344         for (GlobalObservers::iterator x = global_observers.begin(); x != global_observers.end();) {
345
346                 OSCGlobalObserver* gc;
347
348                 if ((gc = dynamic_cast<OSCGlobalObserver*>(*x)) != 0) {
349                         delete *x;
350                         x = global_observers.erase (x);
351                 } else {
352                         ++x;
353                 }
354         }
355
356 // delete select observers
357         for (uint32_t it = 0; it < _surface.size(); ++it) {
358                 OSCSurface* sur = &_surface[it];
359                 OSCSelectObserver* so;
360                 if ((so = dynamic_cast<OSCSelectObserver*>(sur->sel_obs)) != 0) {
361                         delete so;
362                 }
363         }
364
365 // delete cue observers
366         for (CueObservers::iterator x = cue_observers.begin(); x != cue_observers.end();) {
367
368                 OSCCueObserver* co;
369
370                 if ((co = dynamic_cast<OSCCueObserver*>(*x)) != 0) {
371                         delete *x;
372                         x = cue_observers.erase (x);
373                 } else {
374                         ++x;
375                 }
376         }
377
378         return 0;
379 }
380
381 void
382 OSC::register_callbacks()
383 {
384         lo_server srvs[2];
385         lo_server serv;
386
387         srvs[0] = _osc_server;
388         srvs[1] = _osc_unix_server;
389
390         for (size_t i = 0; i < 2; ++i) {
391
392                 if (!srvs[i]) {
393                         continue;
394                 }
395
396                 serv = srvs[i];
397
398
399 #define REGISTER_CALLBACK(serv,path,types, function) lo_server_add_method (serv, path, types, OSC::_ ## function, this)
400
401                 // Some controls have optional "f" for feedback or touchosc
402                 // http://hexler.net/docs/touchosc-controls-reference
403
404                 REGISTER_CALLBACK (serv, "/set_surface", "iiii", set_surface);
405                 REGISTER_CALLBACK (serv, "/set_surface/feedback", "i", set_surface_feedback);
406                 REGISTER_CALLBACK (serv, "/set_surface/bank_size", "i", set_surface_bank_size);
407                 REGISTER_CALLBACK (serv, "/set_surface/gainmode", "i", set_surface_gainmode);
408                 REGISTER_CALLBACK (serv, "/set_surface/strip_types", "i", set_surface_strip_types);
409                 REGISTER_CALLBACK (serv, "/refresh", "", refresh_surface);
410                 REGISTER_CALLBACK (serv, "/refresh", "f", refresh_surface);
411                 REGISTER_CALLBACK (serv, "/strip/list", "", routes_list);
412                 REGISTER_CALLBACK (serv, "/add_marker", "", add_marker);
413                 REGISTER_CALLBACK (serv, "/add_marker", "f", add_marker);
414                 REGISTER_CALLBACK (serv, "/access_action", "s", access_action);
415                 REGISTER_CALLBACK (serv, "/loop_toggle", "", loop_toggle);
416                 REGISTER_CALLBACK (serv, "/loop_toggle", "f", loop_toggle);
417                 REGISTER_CALLBACK (serv, "/loop_location", "ii", loop_location);
418                 REGISTER_CALLBACK (serv, "/goto_start", "", goto_start);
419                 REGISTER_CALLBACK (serv, "/goto_start", "f", goto_start);
420                 REGISTER_CALLBACK (serv, "/goto_end", "", goto_end);
421                 REGISTER_CALLBACK (serv, "/goto_end", "f", goto_end);
422                 REGISTER_CALLBACK (serv, "/scrub", "f", scrub);
423                 REGISTER_CALLBACK (serv, "/jog", "f", jog);
424                 REGISTER_CALLBACK (serv, "/jog/mode", "f", jog_mode);
425                 REGISTER_CALLBACK (serv, "/rewind", "", rewind);
426                 REGISTER_CALLBACK (serv, "/rewind", "f", rewind);
427                 REGISTER_CALLBACK (serv, "/ffwd", "", ffwd);
428                 REGISTER_CALLBACK (serv, "/ffwd", "f", ffwd);
429                 REGISTER_CALLBACK (serv, "/transport_stop", "", transport_stop);
430                 REGISTER_CALLBACK (serv, "/transport_stop", "f", transport_stop);
431                 REGISTER_CALLBACK (serv, "/transport_play", "", transport_play);
432                 REGISTER_CALLBACK (serv, "/transport_play", "f", transport_play);
433                 REGISTER_CALLBACK (serv, "/transport_frame", "", transport_frame);
434                 REGISTER_CALLBACK (serv, "/transport_speed", "", transport_speed);
435                 REGISTER_CALLBACK (serv, "/record_enabled", "", record_enabled);
436                 REGISTER_CALLBACK (serv, "/set_transport_speed", "f", set_transport_speed);
437                 // locate ii is position and bool roll
438                 REGISTER_CALLBACK (serv, "/locate", "ii", locate);
439                 REGISTER_CALLBACK (serv, "/save_state", "", save_state);
440                 REGISTER_CALLBACK (serv, "/save_state", "f", save_state);
441                 REGISTER_CALLBACK (serv, "/prev_marker", "", prev_marker);
442                 REGISTER_CALLBACK (serv, "/prev_marker", "f", prev_marker);
443                 REGISTER_CALLBACK (serv, "/next_marker", "", next_marker);
444                 REGISTER_CALLBACK (serv, "/next_marker", "f", next_marker);
445                 REGISTER_CALLBACK (serv, "/undo", "", undo);
446                 REGISTER_CALLBACK (serv, "/undo", "f", undo);
447                 REGISTER_CALLBACK (serv, "/redo", "", redo);
448                 REGISTER_CALLBACK (serv, "/redo", "f", redo);
449                 REGISTER_CALLBACK (serv, "/toggle_punch_in", "", toggle_punch_in);
450                 REGISTER_CALLBACK (serv, "/toggle_punch_in", "f", toggle_punch_in);
451                 REGISTER_CALLBACK (serv, "/toggle_punch_out", "", toggle_punch_out);
452                 REGISTER_CALLBACK (serv, "/toggle_punch_out", "f", toggle_punch_out);
453                 REGISTER_CALLBACK (serv, "/rec_enable_toggle", "", rec_enable_toggle);
454                 REGISTER_CALLBACK (serv, "/rec_enable_toggle", "f", rec_enable_toggle);
455                 REGISTER_CALLBACK (serv, "/toggle_all_rec_enables", "", toggle_all_rec_enables);
456                 REGISTER_CALLBACK (serv, "/toggle_all_rec_enables", "f", toggle_all_rec_enables);
457                 REGISTER_CALLBACK (serv, "/all_tracks_rec_in", "f", all_tracks_rec_in);
458                 REGISTER_CALLBACK (serv, "/all_tracks_rec_out", "f", all_tracks_rec_out);
459                 REGISTER_CALLBACK (serv, "/cancel_all_solos", "f", cancel_all_solos);
460                 REGISTER_CALLBACK (serv, "/remove_marker", "", remove_marker_at_playhead);
461                 REGISTER_CALLBACK (serv, "/remove_marker", "f", remove_marker_at_playhead);
462                 REGISTER_CALLBACK (serv, "/jump_bars", "f", jump_by_bars);
463                 REGISTER_CALLBACK (serv, "/jump_seconds", "f", jump_by_seconds);
464                 REGISTER_CALLBACK (serv, "/mark_in", "", mark_in);
465                 REGISTER_CALLBACK (serv, "/mark_in", "f", mark_in);
466                 REGISTER_CALLBACK (serv, "/mark_out", "", mark_out);
467                 REGISTER_CALLBACK (serv, "/mark_out", "f", mark_out);
468                 REGISTER_CALLBACK (serv, "/toggle_click", "", toggle_click);
469                 REGISTER_CALLBACK (serv, "/toggle_click", "f", toggle_click);
470                 REGISTER_CALLBACK (serv, "/midi_panic", "", midi_panic);
471                 REGISTER_CALLBACK (serv, "/midi_panic", "f", midi_panic);
472                 REGISTER_CALLBACK (serv, "/toggle_roll", "", toggle_roll);
473                 REGISTER_CALLBACK (serv, "/toggle_roll", "f", toggle_roll);
474                 REGISTER_CALLBACK (serv, "/stop_forget", "", stop_forget);
475                 REGISTER_CALLBACK (serv, "/stop_forget", "f", stop_forget);
476                 REGISTER_CALLBACK (serv, "/set_punch_range", "", set_punch_range);
477                 REGISTER_CALLBACK (serv, "/set_punch_range", "f", set_punch_range);
478                 REGISTER_CALLBACK (serv, "/set_loop_range", "", set_loop_range);
479                 REGISTER_CALLBACK (serv, "/set_loop_range", "f", set_loop_range);
480                 REGISTER_CALLBACK (serv, "/set_session_range", "", set_session_range);
481                 REGISTER_CALLBACK (serv, "/set_session_range", "f", set_session_range);
482                 REGISTER_CALLBACK (serv, "/toggle_monitor_mute", "", toggle_monitor_mute);
483                 REGISTER_CALLBACK (serv, "/toggle_monitor_mute", "f", toggle_monitor_mute);
484                 REGISTER_CALLBACK (serv, "/toggle_monitor_dim", "", toggle_monitor_dim);
485                 REGISTER_CALLBACK (serv, "/toggle_monitor_dim", "f", toggle_monitor_dim);
486                 REGISTER_CALLBACK (serv, "/toggle_monitor_mono", "", toggle_monitor_mono);
487                 REGISTER_CALLBACK (serv, "/toggle_monitor_mono", "f", toggle_monitor_mono);
488                 REGISTER_CALLBACK (serv, "/quick_snapshot_switch", "", quick_snapshot_switch);
489                 REGISTER_CALLBACK (serv, "/quick_snapshot_switch", "f", quick_snapshot_switch);
490                 REGISTER_CALLBACK (serv, "/quick_snapshot_stay", "", quick_snapshot_stay);
491                 REGISTER_CALLBACK (serv, "/quick_snapshot_stay", "f", quick_snapshot_stay);
492                 REGISTER_CALLBACK (serv, "/fit_1_track", "", fit_1_track);
493                 REGISTER_CALLBACK (serv, "/fit_1_track", "f", fit_1_track);
494                 REGISTER_CALLBACK (serv, "/fit_2_tracks", "", fit_2_tracks);
495                 REGISTER_CALLBACK (serv, "/fit_2_tracks", "f", fit_2_tracks);
496                 REGISTER_CALLBACK (serv, "/fit_4_tracks", "", fit_4_tracks);
497                 REGISTER_CALLBACK (serv, "/fit_4_tracks", "f", fit_4_tracks);
498                 REGISTER_CALLBACK (serv, "/fit_8_tracks", "", fit_8_tracks);
499                 REGISTER_CALLBACK (serv, "/fit_8_tracks", "f", fit_8_tracks);
500                 REGISTER_CALLBACK (serv, "/fit_16_tracks", "", fit_16_tracks);
501                 REGISTER_CALLBACK (serv, "/fit_16_tracks", "f", fit_16_tracks);
502                 REGISTER_CALLBACK (serv, "/fit_32_tracks", "", fit_32_tracks);
503                 REGISTER_CALLBACK (serv, "/fit_32_tracks", "f", fit_32_tracks);
504                 REGISTER_CALLBACK (serv, "/fit_all_tracks", "", fit_all_tracks);
505                 REGISTER_CALLBACK (serv, "/fit_all_tracks", "f", fit_all_tracks);
506                 REGISTER_CALLBACK (serv, "/zoom_100_ms", "", zoom_100_ms);
507                 REGISTER_CALLBACK (serv, "/zoom_100_ms", "f", zoom_100_ms);
508                 REGISTER_CALLBACK (serv, "/zoom_1_sec", "", zoom_1_sec);
509                 REGISTER_CALLBACK (serv, "/zoom_1_sec", "f", zoom_1_sec);
510                 REGISTER_CALLBACK (serv, "/zoom_10_sec", "", zoom_10_sec);
511                 REGISTER_CALLBACK (serv, "/zoom_10_sec", "f", zoom_10_sec);
512                 REGISTER_CALLBACK (serv, "/zoom_1_min", "", zoom_1_min);
513                 REGISTER_CALLBACK (serv, "/zoom_1_min", "f", zoom_1_min);
514                 REGISTER_CALLBACK (serv, "/zoom_5_min", "", zoom_5_min);
515                 REGISTER_CALLBACK (serv, "/zoom_5_min", "f", zoom_5_min);
516                 REGISTER_CALLBACK (serv, "/zoom_10_min", "", zoom_10_min);
517                 REGISTER_CALLBACK (serv, "/zoom_10_min", "f", zoom_10_min);
518                 REGISTER_CALLBACK (serv, "/zoom_to_session", "", zoom_to_session);
519                 REGISTER_CALLBACK (serv, "/zoom_to_session", "f", zoom_to_session);
520                 REGISTER_CALLBACK (serv, "/temporal_zoom_in", "f", temporal_zoom_in);
521                 REGISTER_CALLBACK (serv, "/temporal_zoom_in", "", temporal_zoom_in);
522                 REGISTER_CALLBACK (serv, "/temporal_zoom_out", "", temporal_zoom_out);
523                 REGISTER_CALLBACK (serv, "/temporal_zoom_out", "f", temporal_zoom_out);
524                 REGISTER_CALLBACK (serv, "/scroll_up_1_track", "f", scroll_up_1_track);
525                 REGISTER_CALLBACK (serv, "/scroll_up_1_track", "", scroll_up_1_track);
526                 REGISTER_CALLBACK (serv, "/scroll_dn_1_track", "f", scroll_dn_1_track);
527                 REGISTER_CALLBACK (serv, "/scroll_dn_1_track", "", scroll_dn_1_track);
528                 REGISTER_CALLBACK (serv, "/scroll_up_1_page", "f", scroll_up_1_page);
529                 REGISTER_CALLBACK (serv, "/scroll_up_1_page", "", scroll_up_1_page);
530                 REGISTER_CALLBACK (serv, "/scroll_dn_1_page", "f", scroll_dn_1_page);
531                 REGISTER_CALLBACK (serv, "/scroll_dn_1_page", "", scroll_dn_1_page);
532                 REGISTER_CALLBACK (serv, "/bank_up", "", bank_up);
533                 REGISTER_CALLBACK (serv, "/bank_up", "f", bank_up);
534                 REGISTER_CALLBACK (serv, "/bank_down", "", bank_down);
535                 REGISTER_CALLBACK (serv, "/bank_down", "f", bank_down);
536
537                 // controls for "special" strips
538                 REGISTER_CALLBACK (serv, "/master/gain", "f", master_set_gain);
539                 REGISTER_CALLBACK (serv, "/master/fader", "f", master_set_fader);
540                 REGISTER_CALLBACK (serv, "/master/mute", "i", master_set_mute);
541                 REGISTER_CALLBACK (serv, "/master/trimdB", "f", master_set_trim);
542                 REGISTER_CALLBACK (serv, "/master/pan_stereo_position", "f", master_set_pan_stereo_position);
543                 REGISTER_CALLBACK (serv, "/monitor/gain", "f", monitor_set_gain);
544                 REGISTER_CALLBACK (serv, "/monitor/fader", "f", monitor_set_fader);
545                 REGISTER_CALLBACK (serv, "/monitor/mute", "i", monitor_set_mute);
546                 REGISTER_CALLBACK (serv, "/monitor/dim", "i", monitor_set_dim);
547                 REGISTER_CALLBACK (serv, "/monitor/mono", "i", monitor_set_mono);
548
549                 // Controls for the Selected strip
550                 REGISTER_CALLBACK (serv, "/select/recenable", "i", sel_recenable);
551                 REGISTER_CALLBACK (serv, "/select/record_safe", "i", sel_recsafe);
552                 REGISTER_CALLBACK (serv, "/select/mute", "i", sel_mute);
553                 REGISTER_CALLBACK (serv, "/select/solo", "i", sel_solo);
554                 REGISTER_CALLBACK (serv, "/select/solo_iso", "i", sel_solo_iso);
555                 REGISTER_CALLBACK (serv, "/select/solo_safe", "i", sel_solo_safe);
556                 REGISTER_CALLBACK (serv, "/select/monitor_input", "i", sel_monitor_input);
557                 REGISTER_CALLBACK (serv, "/select/monitor_disk", "i", sel_monitor_disk);
558                 REGISTER_CALLBACK (serv, "/select/polarity", "i", sel_phase);
559                 REGISTER_CALLBACK (serv, "/select/gain", "f", sel_gain);
560                 REGISTER_CALLBACK (serv, "/select/fader", "f", sel_fader);
561                 REGISTER_CALLBACK (serv, "/select/trimdB", "f", sel_trim);
562                 REGISTER_CALLBACK (serv, "/select/pan_stereo_position", "f", sel_pan_position);
563                 REGISTER_CALLBACK (serv, "/select/pan_stereo_width", "f", sel_pan_width);
564                 REGISTER_CALLBACK (serv, "/select/send_gain", "if", sel_sendgain);
565                 REGISTER_CALLBACK (serv, "/select/send_fader", "if", sel_sendfader);
566                 REGISTER_CALLBACK (serv, "/select/send_enable", "if", sel_sendenable);
567                 REGISTER_CALLBACK (serv, "/select/expand", "i", sel_expand);
568                 REGISTER_CALLBACK (serv, "/select/pan_elevation_position", "f", sel_pan_elevation);
569                 REGISTER_CALLBACK (serv, "/select/pan_frontback_position", "f", sel_pan_frontback);
570                 REGISTER_CALLBACK (serv, "/select/pan_lfe_control", "f", sel_pan_lfe);
571                 REGISTER_CALLBACK (serv, "/select/comp_enable", "f", sel_comp_enable);
572                 REGISTER_CALLBACK (serv, "/select/comp_threshold", "f", sel_comp_threshold);
573                 REGISTER_CALLBACK (serv, "/select/comp_speed", "f", sel_comp_speed);
574                 REGISTER_CALLBACK (serv, "/select/comp_mode", "f", sel_comp_mode);
575                 REGISTER_CALLBACK (serv, "/select/comp_makeup", "f", sel_comp_makeup);
576                 REGISTER_CALLBACK (serv, "/select/eq_enable", "f", sel_eq_enable);
577                 REGISTER_CALLBACK (serv, "/select/eq_hpf", "f", sel_eq_hpf);
578                 REGISTER_CALLBACK (serv, "/select/eq_gain", "if", sel_eq_gain);
579                 REGISTER_CALLBACK (serv, "/select/eq_freq", "if", sel_eq_freq);
580                 REGISTER_CALLBACK (serv, "/select/eq_q", "if", sel_eq_q);
581                 REGISTER_CALLBACK (serv, "/select/eq_shape", "if", sel_eq_shape);
582
583                 /* These commands require the route index in addition to the arg; TouchOSC (et al) can't use these  */ 
584                 REGISTER_CALLBACK (serv, "/strip/mute", "ii", route_mute);
585                 REGISTER_CALLBACK (serv, "/strip/solo", "ii", route_solo);
586                 REGISTER_CALLBACK (serv, "/strip/solo_iso", "ii", route_solo_iso);
587                 REGISTER_CALLBACK (serv, "/strip/solo_safe", "ii", route_solo_safe);
588                 REGISTER_CALLBACK (serv, "/strip/recenable", "ii", route_recenable);
589                 REGISTER_CALLBACK (serv, "/strip/record_safe", "ii", route_recsafe);
590                 REGISTER_CALLBACK (serv, "/strip/monitor_input", "ii", route_monitor_input);
591                 REGISTER_CALLBACK (serv, "/strip/monitor_disk", "ii", route_monitor_disk);
592                 REGISTER_CALLBACK (serv, "/strip/expand", "ii", strip_expand);
593                 REGISTER_CALLBACK (serv, "/strip/select", "ii", strip_gui_select);
594                 REGISTER_CALLBACK (serv, "/strip/polarity", "ii", strip_phase);
595                 REGISTER_CALLBACK (serv, "/strip/gain", "if", route_set_gain_dB);
596                 REGISTER_CALLBACK (serv, "/strip/fader", "if", route_set_gain_fader);
597                 REGISTER_CALLBACK (serv, "/strip/trimdB", "if", route_set_trim_dB);
598                 REGISTER_CALLBACK (serv, "/strip/pan_stereo_position", "if", route_set_pan_stereo_position);
599                 REGISTER_CALLBACK (serv, "/strip/pan_stereo_width", "if", route_set_pan_stereo_width);
600                 REGISTER_CALLBACK (serv, "/strip/plugin/parameter", "iiif", route_plugin_parameter);
601                 // prints to cerr only
602                 REGISTER_CALLBACK (serv, "/strip/plugin/parameter/print", "iii", route_plugin_parameter_print);
603                 REGISTER_CALLBACK (serv, "/strip/plugin/activate", "ii", route_plugin_activate);
604                 REGISTER_CALLBACK (serv, "/strip/plugin/deactivate", "ii", route_plugin_deactivate);
605                 REGISTER_CALLBACK (serv, "/strip/send/gain", "iif", route_set_send_gain_dB);
606                 REGISTER_CALLBACK (serv, "/strip/send/fader", "iif", route_set_send_fader);
607                 REGISTER_CALLBACK (serv, "/strip/send/enable", "iif", route_set_send_enable);
608                 REGISTER_CALLBACK(serv, "/strip/name", "is", route_rename);
609                 REGISTER_CALLBACK(serv, "/strip/sends", "i", route_get_sends);
610                 REGISTER_CALLBACK(serv, "/strip/receives", "i", route_get_receives);                
611                 REGISTER_CALLBACK(serv, "/strip/plugin/list", "i", route_plugin_list);
612                 REGISTER_CALLBACK(serv, "/strip/plugin/descriptor", "ii", route_plugin_descriptor);
613                 REGISTER_CALLBACK(serv, "/strip/plugin/reset", "ii", route_plugin_reset);
614
615                 /* still not-really-standardized query interface */
616                 //REGISTER_CALLBACK (serv, "/ardour/*/#current_value", "", current_value);
617                 //REGISTER_CALLBACK (serv, "/ardour/set", "", set);
618
619                 // un/register_update args= s:ctrl s:returl s:retpath
620                 //lo_server_add_method(serv, "/register_update", "sss", OSC::global_register_update_handler, this);
621                 //lo_server_add_method(serv, "/unregister_update", "sss", OSC::global_unregister_update_handler, this);
622                 //lo_server_add_method(serv, "/register_auto_update", "siss", OSC::global_register_auto_update_handler, this);
623                 //lo_server_add_method(serv, "/unregister_auto_update", "sss", OSC::_global_unregister_auto_update_handler, this);
624
625                 /* this is a special catchall handler,
626                  * register at the end so this is only called if no
627                  * other handler matches (used for debug) */
628                 lo_server_add_method (serv, 0, 0, _catchall, this);
629         }
630 }
631
632 bool
633 OSC::osc_input_handler (IOCondition ioc, lo_server srv)
634 {
635         if (ioc & ~IO_IN) {
636                 return false;
637         }
638
639         if (ioc & IO_IN) {
640                 lo_server_recv (srv);
641         }
642
643         return true;
644 }
645
646 std::string
647 OSC::get_server_url()
648 {
649         string url;
650         char * urlstr;
651
652         if (_osc_server) {
653                 urlstr = lo_server_get_url (_osc_server);
654                 url = urlstr;
655                 free (urlstr);
656         }
657
658         return url;
659 }
660
661 std::string
662 OSC::get_unix_server_url()
663 {
664         string url;
665         char * urlstr;
666
667         if (_osc_unix_server) {
668                 urlstr = lo_server_get_url (_osc_unix_server);
669                 url = urlstr;
670                 free (urlstr);
671         }
672
673         return url;
674 }
675
676 void
677 OSC::gui_changed ()
678 {
679         session->set_dirty();
680 }
681
682 void
683 OSC::listen_to_route (boost::shared_ptr<Stripable> strip, lo_address addr)
684 {
685         if (!strip) {
686                 return;
687         }
688         /* avoid duplicate listens */
689
690         for (RouteObservers::iterator x = route_observers.begin(); x != route_observers.end(); ++x) {
691
692                 OSCRouteObserver* ro;
693
694                 if ((ro = dynamic_cast<OSCRouteObserver*>(*x)) != 0) {
695
696                         int res = strcmp(lo_address_get_url(ro->address()), lo_address_get_url(addr));
697
698                         if (ro->strip() == strip && res == 0) {
699                                 return;
700                         }
701                 }
702         }
703
704         OSCSurface *s = get_surface(addr);
705         uint32_t ssid = get_sid (strip, addr);
706         OSCRouteObserver* o = new OSCRouteObserver (strip, addr, ssid, s);
707         route_observers.push_back (o);
708
709         strip->DropReferences.connect (*this, MISSING_INVALIDATOR, boost::bind (&OSC::route_lost, this, boost::weak_ptr<Stripable> (strip)), this);
710 }
711
712 void
713 OSC::route_lost (boost::weak_ptr<Stripable> wr)
714 {
715         tick = false;
716         drop_route (wr);
717         bank_dirty = true;
718 }
719
720 void
721 OSC::drop_route (boost::weak_ptr<Stripable> wr)
722 {
723         boost::shared_ptr<Stripable> r = wr.lock ();
724
725         if (!r) {
726                 return;
727         }
728
729         for (RouteObservers::iterator x = route_observers.begin(); x != route_observers.end();) {
730
731                 OSCRouteObserver* rc;
732
733                 if ((rc = dynamic_cast<OSCRouteObserver*>(*x)) != 0) {
734
735                         if (rc->strip() == r) {
736                                 delete *x;
737                                 x = route_observers.erase (x);
738                         } else {
739                                 ++x;
740                         }
741                 } else {
742                         ++x;
743                 }
744         }
745 }
746
747 void
748 OSC::end_listen (boost::shared_ptr<Stripable> r, lo_address addr)
749 {
750         RouteObservers::iterator x;
751
752         // Remove the route observers
753         for (x = route_observers.begin(); x != route_observers.end();) {
754
755                 OSCRouteObserver* ro;
756
757                 if ((ro = dynamic_cast<OSCRouteObserver*>(*x)) != 0) {
758
759                         int res = strcmp(lo_address_get_url(ro->address()), lo_address_get_url(addr));
760
761                         if (ro->strip() == r && res == 0) {
762                                 delete *x;
763                                 x = route_observers.erase (x);
764                         }
765                         else {
766                                 ++x;
767                         }
768                 }
769                 else {
770                         ++x;
771                 }
772         }
773 }
774
775 void
776 OSC::current_value_query (const char* path, size_t len, lo_arg **argv, int argc, lo_message msg)
777 {
778         char* subpath;
779
780         subpath = (char*) malloc (len-15+1);
781         memcpy (subpath, path, len-15);
782         subpath[len-15] = '\0';
783
784         send_current_value (subpath, argv, argc, msg);
785
786         free (subpath);
787 }
788
789 void
790 OSC::send_current_value (const char* path, lo_arg** argv, int argc, lo_message msg)
791 {
792         if (!session) {
793                 return;
794         }
795
796         lo_message reply = lo_message_new ();
797         boost::shared_ptr<Route> r;
798         int id;
799
800         lo_message_add_string (reply, path);
801
802         if (argc == 0) {
803                 lo_message_add_string (reply, "bad syntax");
804         } else {
805                 id = argv[0]->i;
806                 r = session->get_remote_nth_route (id);
807
808                 if (!r) {
809                         lo_message_add_string (reply, "not found");
810                 } else {
811
812                         if (strcmp (path, "/strip/state") == 0) {
813
814                                 if (boost::dynamic_pointer_cast<AudioTrack>(r)) {
815                                         lo_message_add_string (reply, "AT");
816                                 } else if (boost::dynamic_pointer_cast<MidiTrack>(r)) {
817                                         lo_message_add_string (reply, "MT");
818                                 } else {
819                                         lo_message_add_string (reply, "B");
820                                 }
821
822                                 lo_message_add_string (reply, r->name().c_str());
823                                 lo_message_add_int32 (reply, r->n_inputs().n_audio());
824                                 lo_message_add_int32 (reply, r->n_outputs().n_audio());
825                                 lo_message_add_int32 (reply, r->muted());
826                                 lo_message_add_int32 (reply, r->soloed());
827
828                         } else if (strcmp (path, "/strip/mute") == 0) {
829
830                                 lo_message_add_int32 (reply, (float) r->muted());
831
832                         } else if (strcmp (path, "/strip/solo") == 0) {
833
834                                 lo_message_add_int32 (reply, r->soloed());
835                         }
836                 }
837         }
838
839         lo_send_message (get_address (msg), "#reply", reply);
840         lo_message_free (reply);
841 }
842
843 int
844 OSC::_catchall (const char *path, const char *types, lo_arg **argv, int argc, void *data, void *user_data)
845 {
846         return ((OSC*)user_data)->catchall (path, types, argv, argc, data);
847 }
848
849 int
850 OSC::catchall (const char *path, const char* types, lo_arg **argv, int argc, lo_message msg)
851 {
852         size_t len;
853         int ret = 1; /* unhandled */
854
855         //cerr << "Received a message, path = " << path << " types = \""
856         //     << (types ? types : "NULL") << '"' << endl;
857
858         /* 15 for /#current_value plus 2 for /<path> */
859
860         len = strlen (path);
861
862         if (strstr (path, "/automation")) {
863                 ret = set_automation (path, argv, argc, msg);
864
865         } else
866         if (len >= 17 && !strcmp (&path[len-15], "/#current_value")) {
867                 current_value_query (path, len, argv, argc, msg);
868                 ret = 0;
869
870         } else
871         if (!strncmp (path, "/cue/", 5)) {
872
873                 ret = cue_parse (path, types, argv, argc, msg);
874
875         } else
876         if (!strncmp (path, "/access_action/", 15)) {
877                 check_surface (msg);
878                 if (!(argc && !argv[0]->i)) {
879                         std::string action_path = path;
880
881                         access_action (action_path.substr(15));
882                 }
883
884                 ret = 0;
885         } else
886         if (strcmp (path, "/strip/listen") == 0) {
887                 check_surface (msg);
888
889                 cerr << "set up listener\n";
890
891                 lo_message reply = lo_message_new ();
892
893                 if (argc <= 0) {
894                         lo_message_add_string (reply, "syntax error");
895                 } else {
896                         for (int n = 0; n < argc; ++n) {
897
898                                 boost::shared_ptr<Route> r = session->get_remote_nth_route (argv[n]->i);
899
900                                 if (!r) {
901                                         lo_message_add_string (reply, "not found");
902                                         cerr << "no such route\n";
903                                         break;
904                                 } else {
905                                         cerr << "add listener\n";
906                                         listen_to_route (r, get_address (msg));
907                                         lo_message_add_int32 (reply, argv[n]->i);
908                                 }
909                         }
910                 }
911
912                 lo_send_message (get_address (msg), "#reply", reply);
913                 lo_message_free (reply);
914
915                 ret = 0;
916
917         } else
918         if (strcmp (path, "/strip/ignore") == 0) {
919                 check_surface (msg);
920
921                 for (int n = 0; n < argc; ++n) {
922
923                         boost::shared_ptr<Route> r = session->get_remote_nth_route (argv[n]->i);
924
925                         if (r) {
926                                 end_listen (r, get_address (msg));
927                         }
928                 }
929
930                 ret = 0;
931         } else
932         if (!strncmp (path, "/strip/gain/", 12) && strlen (path) > 12) {
933                 // in dB
934                 int ssid = atoi (&path[12]);
935                 ret = route_set_gain_dB (ssid, argv[0]->f, msg);
936         }
937         else if (!strncmp (path, "/strip/fader/", 13) && strlen (path) > 13) {
938                 // in fader position
939                 int ssid = atoi (&path[13]);
940                 ret = route_set_gain_fader (ssid, argv[0]->f, msg);
941         }
942         else if (!strncmp (path, "/strip/trimdB/", 14) && strlen (path) > 14) {
943                 int ssid = atoi (&path[14]);
944                 ret = route_set_trim_dB (ssid, argv[0]->f, msg);
945         }
946         else if (!strncmp (path, "/strip/pan_stereo_position/", 27) && strlen (path) > 27) {
947                 int ssid = atoi (&path[27]);
948                 ret = route_set_pan_stereo_position (ssid, argv[0]->f, msg);
949         }
950         else if (!strncmp (path, "/strip/mute/", 12) && strlen (path) > 12) {
951                 int ssid = atoi (&path[12]);
952                 ret = route_mute (ssid, argv[0]->i, msg);
953         }
954         else if (!strncmp (path, "/strip/solo/", 12) && strlen (path) > 12) {
955                 int ssid = atoi (&path[12]);
956                 ret = route_solo (ssid, argv[0]->i, msg);
957         }
958         else if (!strncmp (path, "/strip/monitor_input/", 21) && strlen (path) > 21) {
959                 int ssid = atoi (&path[21]);
960                 ret = route_monitor_input (ssid, argv[0]->i, msg);
961         }
962         else if (!strncmp (path, "/strip/monitor_disk/", 20) && strlen (path) > 20) {
963                 int ssid = atoi (&path[20]);
964                 ret = route_monitor_disk (ssid, argv[0]->i, msg);
965         }
966         else if (!strncmp (path, "/strip/recenable/", 17) && strlen (path) > 17) {
967                 int ssid = atoi (&path[17]);
968                 ret = route_recenable (ssid, argv[0]->i, msg);
969         }
970         else if (!strncmp (path, "/strip/record_safe/", 19) && strlen (path) > 19) {
971                 int ssid = atoi (&path[19]);
972                 ret = route_recsafe (ssid, argv[0]->i, msg);
973         }
974         else if (!strncmp (path, "/strip/expand/", 14) && strlen (path) > 14) {
975                 int ssid = atoi (&path[14]);
976                 ret = strip_expand (ssid, argv[0]->i, msg);
977         }
978         else if (!strncmp (path, "/strip/select/", 14) && strlen (path) > 14) {
979                 int ssid = atoi (&path[14]);
980                 ret = strip_gui_select (ssid, argv[0]->i, msg);
981         }
982         else if (!strncmp (path, "/select/send_gain/", 18) && strlen (path) > 18) {
983                 int ssid = atoi (&path[18]);
984                 ret = sel_sendgain (ssid, argv[0]->f, msg);
985         }
986         else if (!strncmp (path, "/select/send_fader/", 19) && strlen (path) > 19) {
987                 int ssid = atoi (&path[19]);
988                 ret = sel_sendfader (ssid, argv[0]->f, msg);
989         }
990         else if (!strncmp (path, "/select/send_enable/", 20) && strlen (path) > 20) {
991                 int ssid = atoi (&path[20]);
992                 ret = sel_sendenable (ssid, argv[0]->f, msg);
993         }
994         else if (!strncmp (path, "/select/eq_gain/", 16) && strlen (path) > 16) {
995                 int ssid = atoi (&path[16]);
996                 ret = sel_eq_gain (ssid, argv[0]->f, msg);
997         }
998         else if (!strncmp (path, "/select/eq_freq/", 16) && strlen (path) > 16) {
999                 int ssid = atoi (&path[16]);
1000                 ret = sel_eq_freq (ssid, argv[0]->f , msg);
1001         }
1002         else if (!strncmp (path, "/select/eq_q/", 13) && strlen (path) > 13) {
1003                 int ssid = atoi (&path[13]);
1004                 ret = sel_eq_q (ssid, argv[0]->f, msg);
1005         }
1006         else if (!strncmp (path, "/select/eq_shape/", 17) && strlen (path) > 17) {
1007                 int ssid = atoi (&path[17]);
1008                 ret = sel_eq_shape (ssid, argv[0]->f, msg);
1009         }
1010         if (ret) {
1011                 check_surface (msg);
1012         }
1013
1014         if ((ret && _debugmode != Off)) {
1015                 debugmsg (_("Unhandled OSC message"), path, types, argv, argc);
1016         } else if (!ret && _debugmode == All) {
1017                 debugmsg (_("OSC"), path, types, argv, argc);
1018         }
1019
1020         return ret;
1021 }
1022
1023 void
1024 OSC::debugmsg (const char *prefix, const char *path, const char* types, lo_arg **argv, int argc)
1025 {
1026         std::stringstream ss;
1027         for (int i = 0; i < argc; ++i) {
1028                 lo_type type = (lo_type)types[i];
1029                         ss << " ";
1030                 switch (type) {
1031                         case LO_INT32:
1032                                 ss << "i:" << argv[i]->i;
1033                                 break;
1034                         case LO_FLOAT:
1035                                 ss << "f:" << argv[i]->f;
1036                                 break;
1037                         case LO_DOUBLE:
1038                                 ss << "d:" << argv[i]->d;
1039                                 break;
1040                         case LO_STRING:
1041                                 ss << "s:" << &argv[i]->s;
1042                                 break;
1043                         case LO_INT64:
1044                                 ss << "h:" << argv[i]->h;
1045                                 break;
1046                         case LO_CHAR:
1047                                 ss << "c:" << argv[i]->s;
1048                                 break;
1049                         case LO_TIMETAG:
1050                                 ss << "<Timetag>";
1051                                 break;
1052                         case LO_BLOB:
1053                                 ss << "<BLOB>";
1054                                 break;
1055                         case LO_TRUE:
1056                                 ss << "#T";
1057                                 break;
1058                         case LO_FALSE:
1059                                 ss << "#F";
1060                                 break;
1061                         case LO_NIL:
1062                                 ss << "NIL";
1063                                 break;
1064                         case LO_INFINITUM:
1065                                 ss << "#inf";
1066                                 break;
1067                         case LO_MIDI:
1068                                 ss << "<MIDI>";
1069                                 break;
1070                         case LO_SYMBOL:
1071                                 ss << "<SYMBOL>";
1072                                 break;
1073                         default:
1074                                 ss << "< ?? >";
1075                                 break;
1076                 }
1077         }
1078         PBD::info << prefix << ": " << path << ss.str() << endmsg;
1079 }
1080
1081 // "Application Hook" Handlers //
1082 void
1083 OSC::session_loaded (Session& s)
1084 {
1085 //      lo_address listener = lo_address_new (NULL, "7770");
1086 //      lo_send (listener, "/session/loaded", "ss", s.path().c_str(), s.name().c_str());
1087 }
1088
1089 void
1090 OSC::session_exported (std::string path, std::string name)
1091 {
1092         lo_address listener = lo_address_new (NULL, "7770");
1093         lo_send (listener, "/session/exported", "ss", path.c_str(), name.c_str());
1094         lo_address_free (listener);
1095 }
1096
1097 // end "Application Hook" Handlers //
1098
1099 /* path callbacks */
1100
1101 int
1102 OSC::current_value (const char */*path*/, const char */*types*/, lo_arg **/*argv*/, int /*argc*/, void */*data*/, void* /*user_data*/)
1103 {
1104 #if 0
1105         const char* returl;
1106
1107         if (argc < 3 || types == 0 || strlen (types) < 3 || types[0] != 's' || types[1] != 's' || types[2] != s) {
1108                 return 1;
1109         }
1110
1111         const char *returl = argv[1]->s;
1112         lo_address addr = find_or_cache_addr (returl);
1113
1114         const char *retpath = argv[2]->s;
1115
1116
1117         if (strcmp (argv[0]->s, "transport_frame") == 0) {
1118
1119                 if (session) {
1120                         lo_send (addr, retpath, "i", session->transport_frame());
1121                 }
1122
1123         } else if (strcmp (argv[0]->s, "transport_speed") == 0) {
1124
1125                 if (session) {
1126                         lo_send (addr, retpath, "i", session->transport_frame());
1127                 }
1128
1129         } else if (strcmp (argv[0]->s, "transport_locked") == 0) {
1130
1131                 if (session) {
1132                         lo_send (addr, retpath, "i", session->transport_frame());
1133                 }
1134
1135         } else if (strcmp (argv[0]->s, "punch_in") == 0) {
1136
1137                 if (session) {
1138                         lo_send (addr, retpath, "i", session->transport_frame());
1139                 }
1140
1141         } else if (strcmp (argv[0]->s, "punch_out") == 0) {
1142
1143                 if (session) {
1144                         lo_send (addr, retpath, "i", session->transport_frame());
1145                 }
1146
1147         } else if (strcmp (argv[0]->s, "rec_enable") == 0) {
1148
1149                 if (session) {
1150                         lo_send (addr, retpath, "i", session->transport_frame());
1151                 }
1152
1153         } else {
1154
1155                 /* error */
1156         }
1157 #endif
1158         return 0;
1159 }
1160
1161 void
1162 OSC::routes_list (lo_message msg)
1163 {
1164         if (!session) {
1165                 return;
1166         }
1167         OSCSurface *sur = get_surface(get_address (msg));
1168         sur->no_clear = true;
1169
1170         for (int n = 0; n < (int) sur->nstrips; ++n) {
1171
1172                 boost::shared_ptr<Stripable> s = get_strip (n + 1, get_address (msg));
1173
1174                 if (s) {
1175                         // some things need the route
1176                         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
1177
1178                         lo_message reply = lo_message_new ();
1179
1180                         if (s->presentation_info().flags() & PresentationInfo::AudioTrack) {
1181                                 lo_message_add_string (reply, "AT");
1182                         } else if (s->presentation_info().flags() & PresentationInfo::MidiTrack) {
1183                                 lo_message_add_string (reply, "MT");
1184                         } else if (s->presentation_info().flags() & PresentationInfo::AudioBus) {
1185                                 // r->feeds (session->master_out()) may make more sense
1186                                 if (r->direct_feeds_according_to_reality (session->master_out())) {
1187                                         // this is a bus
1188                                         lo_message_add_string (reply, "B");
1189                                 } else {
1190                                         // this is an Aux out
1191                                         lo_message_add_string (reply, "AX");
1192                                 }
1193                         } else if (s->presentation_info().flags() & PresentationInfo::MidiBus) {
1194                                 lo_message_add_string (reply, "MB");
1195                         } else if (s->presentation_info().flags() & PresentationInfo::VCA) {
1196                                 lo_message_add_string (reply, "V");
1197                         }
1198
1199                         lo_message_add_string (reply, s->name().c_str());
1200                         if (r) {
1201                                 // routes have inputs and outputs
1202                                 lo_message_add_int32 (reply, r->n_inputs().n_audio());
1203                                 lo_message_add_int32 (reply, r->n_outputs().n_audio());
1204                         } else {
1205                                 // non-routes like VCAs don't
1206                                 lo_message_add_int32 (reply, 0);
1207                                 lo_message_add_int32 (reply, 0);
1208                         }
1209                         if (s->mute_control()) {
1210                                 lo_message_add_int32 (reply, s->mute_control()->get_value());
1211                         } else {
1212                                 lo_message_add_int32 (reply, 0);
1213                         }
1214                         if (s->solo_control()) {
1215                                 lo_message_add_int32 (reply, s->solo_control()->get_value());
1216                         } else {
1217                                 lo_message_add_int32 (reply, 0);
1218                         }
1219                         lo_message_add_int32 (reply, n + 1);
1220                         if (s->rec_enable_control()) {
1221                                 lo_message_add_int32 (reply, s->rec_enable_control()->get_value());
1222                         }
1223
1224                         //Automatically listen to stripables listed
1225                         listen_to_route(s, get_address (msg));
1226
1227                         lo_send_message (get_address (msg), "#reply", reply);
1228                         lo_message_free (reply);
1229                 }
1230         }
1231
1232         // Send end of listing message
1233         lo_message reply = lo_message_new ();
1234
1235         lo_message_add_string (reply, "end_route_list");
1236         lo_message_add_int64 (reply, session->frame_rate());
1237         lo_message_add_int64 (reply, session->current_end_frame());
1238         if (session->monitor_out()) {
1239                 // this session has a monitor section
1240                 lo_message_add_int32 (reply, 1);
1241         } else {
1242                 lo_message_add_int32 (reply, 0);
1243         }
1244
1245         lo_send_message (get_address (msg), "#reply", reply);
1246
1247         lo_message_free (reply);
1248 }
1249
1250 int
1251 OSC::cancel_all_solos ()
1252 {
1253         session->cancel_all_solo ();
1254         return 0;
1255 }
1256
1257 lo_address
1258 OSC::get_address (lo_message msg)
1259 {
1260         if (address_only) {
1261                 lo_address addr = lo_message_get_source (msg);
1262                 string host = lo_address_get_hostname (addr);
1263                 int protocol = lo_address_get_protocol (addr);
1264                 return lo_address_new_with_proto (protocol, host.c_str(), remote_port.c_str());
1265         } else {
1266                 return lo_message_get_source (msg);
1267         }
1268 }
1269
1270 int
1271 OSC::refresh_surface (lo_message msg)
1272 {
1273         if (address_only) {
1274                 // get rid of all surfaces and observers.
1275                 // needs change to only clear those for this address on all ports
1276                 clear_devices();
1277         }
1278         OSCSurface *s = get_surface(get_address (msg));
1279         // restart all observers
1280         set_surface (s->bank_size, (uint32_t) s->strip_types.to_ulong(), (uint32_t) s->feedback.to_ulong(), (uint32_t) s->gainmode, msg);
1281         return 0;
1282 }
1283
1284 void
1285 OSC::clear_devices ()
1286 {
1287         for (RouteObservers::iterator x = route_observers.begin(); x != route_observers.end();) {
1288
1289                 OSCRouteObserver* rc;
1290
1291                 if ((rc = dynamic_cast<OSCRouteObserver*>(*x)) != 0) {
1292                         delete *x;
1293                         x = route_observers.erase (x);
1294                 } else {
1295                         ++x;
1296                 }
1297                 // slow devices need time to clear buffers
1298                 usleep ((uint32_t) 10);
1299         }
1300         // Should maybe do global_observers too
1301         for (GlobalObservers::iterator x = global_observers.begin(); x != global_observers.end();) {
1302
1303                 OSCGlobalObserver* gc;
1304
1305                 if ((gc = dynamic_cast<OSCGlobalObserver*>(*x)) != 0) {
1306                         delete *x;
1307                         x = global_observers.erase (x);
1308                 } else {
1309                         ++x;
1310                 }
1311         }
1312         // delete select observers
1313         for (uint32_t it = 0; it < _surface.size(); ++it) {
1314                 OSCSurface* sur = &_surface[it];
1315                 OSCSelectObserver* so;
1316                 if ((so = dynamic_cast<OSCSelectObserver*>(sur->sel_obs)) != 0) {
1317                         delete so;
1318                 }
1319         }
1320         // delete cue observers
1321         for (CueObservers::iterator x = cue_observers.begin(); x != cue_observers.end();) {
1322                 OSCCueObserver* co;
1323                 if ((co = dynamic_cast<OSCCueObserver*>(*x)) != 0) {
1324                         delete *x;
1325                         x = cue_observers.erase (x);
1326                 } else {
1327                         ++x;
1328                 }
1329         }
1330
1331         // clear out surfaces
1332         _surface.clear();
1333 }
1334
1335 int
1336 OSC::set_surface (uint32_t b_size, uint32_t strips, uint32_t fb, uint32_t gm, lo_message msg)
1337 {
1338         OSCSurface *s = get_surface(get_address (msg));
1339         s->bank_size = b_size;
1340         s->strip_types = strips;
1341         s->feedback = fb;
1342         s->gainmode = gm;
1343         // set bank and strip feedback
1344         set_bank(s->bank, msg);
1345
1346         global_feedback (s->feedback, get_address (msg), s->gainmode);
1347         return 0;
1348 }
1349
1350 int
1351 OSC::set_surface_bank_size (uint32_t bs, lo_message msg)
1352 {
1353         OSCSurface *s = get_surface(get_address (msg));
1354         s->bank_size = bs;
1355
1356         // set bank and strip feedback
1357         set_bank(s->bank, msg);
1358         return 0;
1359 }
1360
1361 int
1362 OSC::set_surface_strip_types (uint32_t st, lo_message msg)
1363 {
1364         OSCSurface *s = get_surface(get_address (msg));
1365         s->strip_types = st;
1366
1367         // set bank and strip feedback
1368         set_bank(s->bank, msg);
1369         return 0;
1370 }
1371
1372
1373 int
1374 OSC::set_surface_feedback (uint32_t fb, lo_message msg)
1375 {
1376         OSCSurface *s = get_surface(get_address (msg));
1377         s->feedback = fb;
1378
1379         // set bank and strip feedback
1380         set_bank(s->bank, msg);
1381
1382         // Set global/master feedback
1383         global_feedback (s->feedback, get_address (msg), s->gainmode);
1384         return 0;
1385 }
1386
1387
1388 int
1389 OSC::set_surface_gainmode (uint32_t gm, lo_message msg)
1390 {
1391         OSCSurface *s = get_surface(get_address (msg));
1392         s->gainmode = gm;
1393
1394         // set bank and strip feedback
1395         set_bank(s->bank, msg);
1396
1397         // Set global/master feedback
1398         global_feedback (s->feedback, get_address (msg), s->gainmode);
1399         return 0;
1400 }
1401
1402 int
1403 OSC::check_surface (lo_message msg)
1404 {
1405         if (!session) {
1406                 return -1;
1407         }
1408         get_surface(get_address (msg));
1409         return 0;
1410 }
1411
1412 OSC::OSCSurface *
1413 OSC::get_surface (lo_address addr)
1414 {
1415         string r_url;
1416         char * rurl;
1417         if (address_only) {
1418                 string host = lo_address_get_hostname (addr);
1419                 int protocol = lo_address_get_protocol (addr);
1420                 addr = lo_address_new_with_proto (protocol, host.c_str(), remote_port.c_str());
1421         }
1422
1423         rurl = lo_address_get_url (addr);
1424         r_url = rurl;
1425         free (rurl);
1426         for (uint32_t it = 0; it < _surface.size(); ++it) {
1427                 //find setup for this server
1428                 if (!_surface[it].remote_url.find(r_url)){
1429                         return &_surface[it];
1430                 }
1431         }
1432         // if we do this when OSC is started we get the wrong stripable
1433         // we don't need this until we actually have a surface to deal with
1434         if (!_select || (_select != ControlProtocol::first_selected_stripable())) {
1435                 gui_selection_changed();
1436         }
1437
1438         // No surface create one with default values
1439         OSCSurface s;
1440         s.remote_url = r_url;
1441         s.no_clear = false;
1442         s.jogmode = JOG;
1443         s.bank = 1;
1444         s.bank_size = default_banksize; // need to find out how many strips there are
1445         s.strip_types = default_strip; // 159 is tracks, busses, and VCAs (no master/monitor)
1446         s.feedback = default_feedback;
1447         s.gainmode = default_gainmode;
1448         s.sel_obs = 0;
1449         s.expand = 0;
1450         s.expand_enable = false;
1451         s.cue = false;
1452         s.aux = 0;
1453         s.strips = get_sorted_stripables(s.strip_types, s.cue);
1454
1455         s.nstrips = s.strips.size();
1456         _surface.push_back (s);
1457
1458         // set bank and strip feedback
1459         _set_bank(s.bank, addr);
1460
1461         // Set global/master feedback
1462         global_feedback (s.feedback, addr, s.gainmode);
1463
1464         return &_surface[_surface.size() - 1];
1465 }
1466
1467 // setup global feedback for a surface
1468 void
1469 OSC::global_feedback (bitset<32> feedback, lo_address addr, uint32_t gainmode)
1470 {
1471         // first destroy global observer for this surface
1472         GlobalObservers::iterator x;
1473         for (x = global_observers.begin(); x != global_observers.end();) {
1474
1475                 OSCGlobalObserver* ro;
1476
1477                 if ((ro = dynamic_cast<OSCGlobalObserver*>(*x)) != 0) {
1478
1479                         int res = strcmp(lo_address_get_url(ro->address()), lo_address_get_url(addr));
1480
1481                         if (res == 0) {
1482                                 delete *x;
1483                                 x = global_observers.erase (x);
1484                         } else {
1485                                 ++x;
1486                         }
1487                 } else {
1488                         ++x;
1489                 }
1490         }
1491         if (feedback[4] || feedback[3] || feedback[5] || feedback[6]) {
1492                 // create a new Global Observer for this surface
1493                 OSCGlobalObserver* o = new OSCGlobalObserver (*session, addr, gainmode, /*s->*/feedback);
1494                 global_observers.push_back (o);
1495         }
1496 }
1497
1498 void
1499 OSC::notify_routes_added (ARDOUR::RouteList &)
1500 {
1501         // not sure if we need this PI change seems to cover
1502         //recalcbanks();
1503 }
1504
1505 void
1506 OSC::notify_vca_added (ARDOUR::VCAList &)
1507 {
1508         // not sure if we need this PI change seems to cover
1509         //recalcbanks();
1510 }
1511
1512 void
1513 OSC::recalcbanks ()
1514 {
1515         tick = false;
1516         bank_dirty = true;
1517 }
1518
1519 void
1520 OSC::_recalcbanks ()
1521 {
1522         if (!_select || (_select != ControlProtocol::first_selected_stripable())) {
1523                 _select = ControlProtocol::first_selected_stripable();
1524         }
1525
1526         // do a set_bank for each surface we know about.
1527         for (uint32_t it = 0; it < _surface.size(); ++it) {
1528                 OSCSurface* sur = &_surface[it];
1529                 // find lo_address
1530                 lo_address addr = lo_address_new_from_url (sur->remote_url.c_str());
1531                 if (sur->cue) {
1532                         _cue_set (sur->aux, addr);
1533                 } else {
1534                         _set_bank (sur->bank, addr);
1535                 }
1536                 if (sur->no_clear) {
1537                         // This surface uses /strip/list tell it routes have changed
1538                         lo_message reply;
1539                         reply = lo_message_new ();
1540                         lo_send_message (addr, "/strip/list", reply);
1541                         lo_message_free (reply);
1542                 }
1543         }
1544 }
1545
1546 /*
1547  * This gets called not only when bank changes but also:
1548  *  - bank size change
1549  *  - feedback change
1550  *  - strip types changes
1551  *  - fadermode changes
1552  *  - stripable creation/deletion/flag
1553  *  - to refresh what is "displayed"
1554  * Basically any time the bank needs to be rebuilt
1555  */
1556 int
1557 OSC::set_bank (uint32_t bank_start, lo_message msg)
1558 {
1559         return _set_bank (bank_start, get_address (msg));
1560 }
1561
1562 // set bank is callable with either message or address
1563 int
1564 OSC::_set_bank (uint32_t bank_start, lo_address addr)
1565 {
1566         if (!session) {
1567                 return -1;
1568         }
1569         // no nstripables yet
1570         if (!session->nroutes()) {
1571                 return -1;
1572         }
1573
1574         OSCSurface *s = get_surface (addr);
1575
1576         // revert any expand to select
1577          s->expand = 0;
1578          s->expand_enable = false;
1579         _strip_select (ControlProtocol::first_selected_stripable(), addr);
1580
1581         // undo all listeners for this url
1582         StripableList stripables;
1583         session->get_stripables (stripables);
1584         for (StripableList::iterator it = stripables.begin(); it != stripables.end(); ++it) {
1585
1586                 boost::shared_ptr<Stripable> stp = *it;
1587                 if (stp) {
1588                         end_listen (stp, addr);
1589                 }
1590                 // slow devices need time to clear buffers
1591                 usleep ((uint32_t) 10);
1592         }
1593
1594         s->strips = get_sorted_stripables(s->strip_types, s->cue);
1595         s->nstrips = s->strips.size();
1596
1597         uint32_t b_size;
1598         if (!s->bank_size) {
1599                 // no banking - bank includes all stripables
1600                 b_size = s->nstrips;
1601         } else {
1602                 b_size = s->bank_size;
1603         }
1604
1605         // Do limits checking
1606         if (bank_start < 1) bank_start = 1;
1607         if (b_size >= s->nstrips)  {
1608                 bank_start = 1;
1609         } else if (bank_start > ((s->nstrips - b_size) + 1)) {
1610                 // top bank is always filled if there are enough strips for at least one bank
1611                 bank_start = (uint32_t)((s->nstrips - b_size) + 1);
1612         }
1613         //save bank after bank limit checks
1614         s->bank = bank_start;
1615
1616         if (s->feedback[0] || s->feedback[1]) {
1617
1618                 for (uint32_t n = bank_start; n < (min ((b_size + bank_start), s->nstrips + 1)); ++n) {
1619                         if (n <= s->strips.size()) {
1620                                 boost::shared_ptr<Stripable> stp = s->strips[n - 1];
1621
1622                                 if (stp) {
1623                                         listen_to_route(stp, addr);
1624                                 }
1625                         }
1626                         // slow devices need time to clear buffers
1627                         usleep ((uint32_t) 10);
1628                 }
1629         }
1630         // light bankup or bankdown buttons if it is possible to bank in that direction
1631         if (s->feedback[4] && !s->no_clear) {
1632                 lo_message reply;
1633                 reply = lo_message_new ();
1634                 if ((s->bank > (s->nstrips - s->bank_size)) || (s->nstrips < s->bank_size)) {
1635                         lo_message_add_int32 (reply, 0);
1636                 } else {
1637                         lo_message_add_int32 (reply, 1);
1638                 }
1639                 lo_send_message (addr, "/bank_up", reply);
1640                 lo_message_free (reply);
1641                 reply = lo_message_new ();
1642                 if (s->bank > 1) {
1643                         lo_message_add_int32 (reply, 1);
1644                 } else {
1645                         lo_message_add_int32 (reply, 0);
1646                 }
1647                 lo_send_message (addr, "/bank_down", reply);
1648                 lo_message_free (reply);
1649         }
1650         bank_dirty = false;
1651         tick = true;
1652         return 0;
1653 }
1654
1655 int
1656 OSC::bank_up (lo_message msg)
1657 {
1658         if (!session) {
1659                 return -1;
1660         }
1661         OSCSurface *s = get_surface(get_address (msg));
1662         set_bank (s->bank + s->bank_size, msg);
1663         return 0;
1664 }
1665
1666 int
1667 OSC::bank_down (lo_message msg)
1668 {
1669         if (!session) {
1670                 return -1;
1671         }
1672         OSCSurface *s = get_surface(get_address (msg));
1673         if (s->bank < s->bank_size) {
1674                 set_bank (1, msg);
1675         } else {
1676                 set_bank (s->bank - s->bank_size, msg);
1677         }
1678         return 0;
1679 }
1680
1681 uint32_t
1682 OSC::get_sid (boost::shared_ptr<ARDOUR::Stripable> strip, lo_address addr)
1683 {
1684         if (!strip) {
1685                 return 0;
1686         }
1687
1688         OSCSurface *s = get_surface(addr);
1689
1690         uint32_t b_size;
1691         if (!s->bank_size) {
1692                 // no banking
1693                 b_size = s->nstrips;
1694         } else {
1695                 b_size = s->bank_size;
1696         }
1697
1698         for (uint32_t n = s->bank; n < (min ((b_size + s->bank), s->nstrips + 1)); ++n) {
1699                 if (n <= s->strips.size()) {
1700                         if (strip == s->strips[n-1]) {
1701                                 return n - s->bank + 1;
1702                         }
1703                 }
1704         }
1705         // failsafe... should never get here.
1706         return 0;
1707 }
1708
1709 boost::shared_ptr<ARDOUR::Stripable>
1710 OSC::get_strip (uint32_t ssid, lo_address addr)
1711 {
1712         OSCSurface *s = get_surface(addr);
1713         if (ssid && ((ssid + s->bank - 2) < s->nstrips)) {
1714                 return s->strips[ssid + s->bank - 2];
1715         }
1716         // guess it is out of range
1717         return boost::shared_ptr<ARDOUR::Stripable>();
1718 }
1719
1720 void
1721 OSC::transport_frame (lo_message msg)
1722 {
1723         if (!session) {
1724                 return;
1725         }
1726         check_surface (msg);
1727         framepos_t pos = session->transport_frame ();
1728
1729         lo_message reply = lo_message_new ();
1730         lo_message_add_int64 (reply, pos);
1731
1732         lo_send_message (get_address (msg), "/transport_frame", reply);
1733
1734         lo_message_free (reply);
1735 }
1736
1737 void
1738 OSC::transport_speed (lo_message msg)
1739 {
1740         if (!session) {
1741                 return;
1742         }
1743         check_surface (msg);
1744         double ts = session->transport_speed ();
1745
1746         lo_message reply = lo_message_new ();
1747         lo_message_add_double (reply, ts);
1748
1749         lo_send_message (get_address (msg), "/transport_speed", reply);
1750
1751         lo_message_free (reply);
1752 }
1753
1754 void
1755 OSC::record_enabled (lo_message msg)
1756 {
1757         if (!session) {
1758                 return;
1759         }
1760         check_surface (msg);
1761         int re = (int)session->get_record_enabled ();
1762
1763         lo_message reply = lo_message_new ();
1764         lo_message_add_int32 (reply, re);
1765
1766         lo_send_message (get_address (msg), "/record_enabled", reply);
1767
1768         lo_message_free (reply);
1769 }
1770
1771 int
1772 OSC::scrub (float delta, lo_message msg)
1773 {
1774         if (!session) return -1;
1775         check_surface (msg);
1776
1777         scrub_place = session->transport_frame ();
1778
1779         float speed;
1780
1781         int64_t now = ARDOUR::get_microseconds ();
1782         int64_t diff = now - scrub_time;
1783         if (diff > 35000) {
1784                 // speed 1 (or 0 if jog wheel supports touch)
1785                 speed = delta;
1786         } else if ((diff > 20000) && (fabs(scrub_speed) == 1)) {
1787                 // add some hysteresis to stop excess speed jumps
1788                 speed = delta;
1789         } else {
1790                 speed = (int)(delta * 2);
1791         }
1792         scrub_time = now;
1793         if (scrub_speed == speed) {
1794                 // Already at that speed no change
1795                 return 0;
1796         }
1797         scrub_speed = speed;
1798
1799         if (speed > 0) {
1800                 if (speed == 1) {
1801                         session->request_transport_speed (.5);
1802                 } else {
1803                         session->request_transport_speed (1);
1804                 }
1805         } else if (speed < 0) {
1806                 if (speed == -1) {
1807                         session->request_transport_speed (-.5);
1808                 } else {
1809                         session->request_transport_speed (-1);
1810                 }
1811         } else {
1812                 session->request_transport_speed (0);
1813         }
1814
1815         return 0;
1816 }
1817
1818 int
1819 OSC::jog (float delta, lo_message msg)
1820 {
1821         if (!session) return -1;
1822
1823         OSCSurface *s = get_surface(get_address (msg));
1824
1825         string path = "/jog/mode/name";
1826         switch(s->jogmode)
1827         {
1828                 case JOG  :
1829                         text_message (path, "Jog", get_address (msg));
1830                         if (delta) {
1831                                 jump_by_seconds (delta / 5);
1832                         }
1833                         break;
1834                 case SCRUB:
1835                         text_message (path, "Scrub", get_address (msg));
1836                         scrub (delta, msg);
1837                         break;
1838                 case SHUTTLE:
1839                         text_message (path, "Shuttle", get_address (msg));
1840                         if (delta) {
1841                                 double speed = get_transport_speed ();
1842                                 set_transport_speed (speed + (delta / 8));
1843                         } else {
1844                                 set_transport_speed (0);
1845                         }
1846                         break;
1847                 case SCROLL:
1848                         text_message (path, "Scroll", get_address (msg));
1849                         if (delta > 0) {
1850                                 access_action ("Editor/scroll-forward");
1851                         } else if (delta < 0) {
1852                                 access_action ("Editor/scroll-backward");
1853                         }
1854                         break;
1855                 case TRACK:
1856                         text_message (path, "Track", get_address (msg));
1857                         if (delta > 0) {
1858                                 set_bank (s->bank + 1, msg);
1859                         } else if (delta < 0) {
1860                                 set_bank (s->bank - 1, msg);
1861                         }
1862                         break;
1863                 case BANK:
1864                         text_message (path, "Bank", get_address (msg));
1865                         if (delta > 0) {
1866                                 bank_up (msg);
1867                         } else if (delta < 0) {
1868                                 bank_down (msg);
1869                         }
1870                         break;
1871                 case NUDGE:
1872                         text_message (path, "Nudge", get_address (msg));
1873                         if (delta > 0) {
1874                                 access_action ("Common/nudge-playhead-forward");
1875                         } else if (delta < 0) {
1876                                 access_action ("Common/nudge-playhead-backward");
1877                         }
1878                         break;
1879                 case MARKER:
1880                         text_message (path, "Marker", get_address (msg));
1881                         if (delta > 0) {
1882                                 next_marker ();
1883                         } else if (delta < 0) {
1884                                 prev_marker ();
1885                         }
1886                         break;
1887                 default:
1888                         break;
1889
1890         }
1891         return 0;
1892
1893 }
1894
1895 int
1896 OSC::jog_mode (float mode, lo_message msg)
1897 {
1898         if (!session) return -1;
1899
1900         OSCSurface *s = get_surface(get_address (msg));
1901
1902         switch((uint32_t)mode)
1903         {
1904                 case JOG  :
1905                         s->jogmode = JOG;
1906                         break;
1907                 case SCRUB:
1908                         s->jogmode = SCRUB;
1909                         break;
1910                 case SHUTTLE:
1911                         s->jogmode = SHUTTLE;
1912                         break;
1913                 case SCROLL:
1914                         s->jogmode = SCROLL;
1915                         break;
1916                 case TRACK:
1917                         s->jogmode = TRACK;
1918                         break;
1919                 case BANK:
1920                         s->jogmode = BANK;
1921                         break;
1922                 case NUDGE:
1923                         s->jogmode = NUDGE;
1924                         break;
1925                 case MARKER:
1926                         s->jogmode = MARKER;
1927                         break;
1928                 default:
1929                         PBD::warning << "Jog Mode: " << mode << " is not valid." << endmsg;
1930                         break;
1931         lo_message reply = lo_message_new ();
1932         lo_message_add_int32 (reply, s->jogmode);
1933         lo_send_message (get_address(msg), "/jog/mode", reply);
1934         lo_message_free (reply);
1935
1936         }
1937         jog (0, msg);
1938         return 0;
1939
1940 }
1941
1942 // master and monitor calls
1943 int
1944 OSC::master_set_gain (float dB)
1945 {
1946         if (!session) return -1;
1947         boost::shared_ptr<Stripable> s = session->master_out();
1948         if (s) {
1949                 if (dB < -192) {
1950                         s->gain_control()->set_value (0.0, PBD::Controllable::NoGroup);
1951                 } else {
1952                         s->gain_control()->set_value (dB_to_coefficient (dB), PBD::Controllable::NoGroup);
1953                 }
1954         }
1955         return 0;
1956 }
1957
1958 int
1959 OSC::master_set_fader (float position)
1960 {
1961         if (!session) return -1;
1962         boost::shared_ptr<Stripable> s = session->master_out();
1963         if (s) {
1964                 s->gain_control()->set_value (slider_position_to_gain_with_max (position, 2.0), PBD::Controllable::NoGroup);
1965         }
1966         return 0;
1967 }
1968
1969 int
1970 OSC::master_set_trim (float dB)
1971 {
1972         if (!session) return -1;
1973         boost::shared_ptr<Stripable> s = session->master_out();
1974
1975         if (s) {
1976                 s->trim_control()->set_value (dB_to_coefficient (dB), PBD::Controllable::NoGroup);
1977         }
1978
1979         return 0;
1980 }
1981
1982 int
1983 OSC::master_set_pan_stereo_position (float position, lo_message msg)
1984 {
1985         if (!session) return -1;
1986         OSCSurface *sur = get_surface(get_address (msg));
1987
1988         float endposition = .5;
1989         boost::shared_ptr<Stripable> s = session->master_out();
1990
1991         if (s) {
1992                 if (s->pan_azimuth_control()) {
1993                         s->pan_azimuth_control()->set_value (s->pan_azimuth_control()->interface_to_internal (position), PBD::Controllable::NoGroup);
1994                         endposition = s->pan_azimuth_control()->internal_to_interface (s->pan_azimuth_control()->get_value ());
1995                 }
1996         }
1997
1998         if (sur->feedback[4]) {
1999                 lo_message reply = lo_message_new ();
2000                 lo_message_add_float (reply, endposition);
2001
2002                 lo_send_message (get_address (msg), "/master/pan_stereo_position", reply);
2003                 lo_message_free (reply);
2004         }
2005
2006         return 0;
2007 }
2008
2009 int
2010 OSC::master_set_mute (uint32_t state)
2011 {
2012         if (!session) return -1;
2013
2014         boost::shared_ptr<Stripable> s = session->master_out();
2015
2016         if (s) {
2017                 s->mute_control()->set_value (state, PBD::Controllable::NoGroup);
2018         }
2019
2020         return 0;
2021 }
2022
2023 int
2024 OSC::monitor_set_gain (float dB)
2025 {
2026         if (!session) return -1;
2027         boost::shared_ptr<Stripable> s = session->monitor_out();
2028
2029         if (s) {
2030                 if (dB < -192) {
2031                         s->gain_control()->set_value (0.0, PBD::Controllable::NoGroup);
2032                 } else {
2033                         s->gain_control()->set_value (dB_to_coefficient (dB), PBD::Controllable::NoGroup);
2034                 }
2035         }
2036         return 0;
2037 }
2038
2039 int
2040 OSC::monitor_set_fader (float position)
2041 {
2042         if (!session) return -1;
2043         boost::shared_ptr<Stripable> s = session->monitor_out();
2044         if (s) {
2045                 s->gain_control()->set_value (slider_position_to_gain_with_max (position, 2.0), PBD::Controllable::NoGroup);
2046         }
2047         return 0;
2048 }
2049
2050 int
2051 OSC::monitor_set_mute (uint32_t state)
2052 {
2053         if (!session) return -1;
2054
2055         if (session->monitor_out()) {
2056                 boost::shared_ptr<MonitorProcessor> mon = session->monitor_out()->monitor_control();
2057                 mon->set_cut_all (state);
2058         }
2059         return 0;
2060 }
2061
2062 int
2063 OSC::monitor_set_dim (uint32_t state)
2064 {
2065         if (!session) return -1;
2066
2067         if (session->monitor_out()) {
2068                 boost::shared_ptr<MonitorProcessor> mon = session->monitor_out()->monitor_control();
2069                 mon->set_dim_all (state);
2070         }
2071         return 0;
2072 }
2073
2074 int
2075 OSC::monitor_set_mono (uint32_t state)
2076 {
2077         if (!session) return -1;
2078
2079         if (session->monitor_out()) {
2080                 boost::shared_ptr<MonitorProcessor> mon = session->monitor_out()->monitor_control();
2081                 mon->set_mono (state);
2082         }
2083         return 0;
2084 }
2085
2086 int
2087 OSC::route_get_sends(lo_message msg) {
2088         if (!session) {
2089                 return -1;
2090         }
2091
2092         lo_arg **argv = lo_message_get_argv(msg);
2093
2094         int rid = argv[0]->i;
2095
2096         boost::shared_ptr<Stripable> strip = get_strip(rid, get_address(msg));
2097         if (!strip) {
2098                 return -1;
2099         }
2100
2101         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (strip);
2102         if (!r) {
2103                 return -1;
2104         }
2105
2106         lo_message reply = lo_message_new();
2107         lo_message_add_int32(reply, rid);
2108
2109         int i = 0;
2110         for (;;) {
2111                 boost::shared_ptr<Processor> p = r->nth_send(i++);
2112
2113                 if (!p) {
2114                         break;
2115                 }
2116
2117                 boost::shared_ptr<InternalSend> isend = boost::dynamic_pointer_cast<InternalSend> (p);
2118                 if (isend) {
2119                         lo_message_add_int32(reply, get_sid(isend->target_route(), get_address(msg)));
2120                         lo_message_add_string(reply, isend->name().c_str());
2121                         lo_message_add_int32(reply, i);
2122                         boost::shared_ptr<Amp> a = isend->amp();
2123                         lo_message_add_float(reply, gain_to_slider_position(a->gain_control()->get_value()));
2124                         lo_message_add_int32(reply, p->active() ? 1 : 0);
2125                 }
2126         }
2127         // if used dedicated message path to identify this reply in async operation.
2128         // Naming it #reply wont help the client to identify the content.
2129         lo_send_message(get_address (msg), "/strip/sends", reply);
2130
2131         lo_message_free(reply);
2132
2133         return 0;
2134 }
2135
2136 int
2137 OSC::route_get_receives(lo_message msg) {
2138         if (!session) {
2139                 return -1;
2140         }
2141
2142         lo_arg **argv = lo_message_get_argv(msg);
2143
2144         uint32_t rid = argv[0]->i;
2145
2146
2147         boost::shared_ptr<Stripable> strip = get_strip(rid, get_address(msg));
2148         if (!strip) {
2149                 return -1;
2150         }
2151
2152         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (strip);
2153         if (!r) {
2154                 return -1;
2155         }
2156
2157         boost::shared_ptr<RouteList> route_list = session->get_routes();
2158
2159         lo_message reply = lo_message_new();
2160
2161         for (RouteList::iterator i = route_list->begin(); i != route_list->end(); ++i) {
2162                 boost::shared_ptr<Route> tr = boost::dynamic_pointer_cast<Route> (*i);
2163                 if (!tr) {
2164                         continue;
2165                 }
2166                 int j = 0;
2167
2168                 for (;;) {
2169                         boost::shared_ptr<Processor> p = tr->nth_send(j++);
2170
2171                         if (!p) {
2172                                 break;
2173                         }
2174
2175                         boost::shared_ptr<InternalSend> isend = boost::dynamic_pointer_cast<InternalSend> (p);
2176                         if (isend) {
2177                                 if( isend->target_route()->id() == r->id()){
2178                                         boost::shared_ptr<Amp> a = isend->amp();
2179
2180                                         lo_message_add_int32(reply, get_sid(tr, get_address(msg)));
2181                                         lo_message_add_string(reply, tr->name().c_str());
2182                                         lo_message_add_int32(reply, j);
2183                                         lo_message_add_float(reply, gain_to_slider_position(a->gain_control()->get_value()));
2184                                         lo_message_add_int32(reply, p->active() ? 1 : 0);
2185                                 }
2186                         }
2187                 }
2188         }
2189
2190         // I have used a dedicated message path to identify this reply in async operation.
2191         // Naming it #reply wont help the client to identify the content.
2192         lo_send_message(get_address (msg), "/strip/receives", reply);
2193         lo_message_free(reply);
2194         return 0;
2195 }
2196
2197 // strip calls
2198
2199 int
2200 OSC::set_automation (const char *path, lo_arg **argv, int argc, lo_message msg)
2201 {
2202         if (!session) return -1;
2203
2204         int ret = 1;
2205         OSCSurface *sur = get_surface(get_address (msg));
2206         boost::shared_ptr<Stripable> strp = boost::shared_ptr<Stripable>();
2207         uint32_t ctr = 0;
2208         uint32_t aut = 0;
2209
2210         //parse path first to find stripable
2211         if (!strncmp (path, "/strip/", 7)) {
2212                 // find ssid and stripable
2213                 if (argc > 1) {
2214                         strp = get_strip (argv[0]->i, get_address (msg));
2215                         if (argv[1]->f) {
2216                                 aut = (int)argv[1]->f;
2217                         } else {
2218                                 aut = argv[1]->i;
2219                         }
2220                 } else {
2221                         uint32_t ssid = atoi (&(strrchr (path, '/' ))[1]);
2222                         strp = get_strip (ssid, get_address (msg));
2223                         if (argv[0]->f) {
2224                                 aut = (int)argv[0]->f;
2225                         } else {
2226                                 aut = argv[0]->i;
2227                         }
2228                 }
2229                 ctr = 7;
2230         } else if (!strncmp (path, "/select/", 8)) {
2231                 if (sur->expand_enable && sur->expand) {
2232                         strp = get_strip (sur->expand, get_address (msg));
2233                 } else {
2234                         strp = ControlProtocol::first_selected_stripable();
2235                 }
2236                 if (argv[0]->f) {
2237                         aut = (int)argv[0]->f;
2238                 } else {
2239                         aut = argv[0]->i;
2240                 }
2241                 ctr = 8;
2242         } else {
2243                 return ret;
2244         }
2245         if (strp) {
2246                 boost::shared_ptr<AutomationControl> control = boost::shared_ptr<AutomationControl>();
2247                 // other automatable controls can be added by repeating the next 6.5 lines
2248                 if ((!strncmp (&path[ctr], "fader", 5)) || (!strncmp (&path[ctr], "gain", 4))) {
2249                         if (strp->gain_control ()) {
2250                                 control = strp->gain_control ();
2251                         } else {
2252                                 PBD::warning << "No fader for this strip" << endmsg;
2253                         }
2254                 } else {
2255                         PBD::warning << "Automation not available for " << path << endmsg;
2256                 }
2257
2258                 if (control) {
2259
2260                         switch (aut) {
2261                                 case 0:
2262                                         control->set_automation_state (ARDOUR::Off);
2263                                         ret = 0;
2264                                         break;
2265                                 case 1:
2266                                         control->set_automation_state (ARDOUR::Play);
2267                                         ret = 0;
2268                                         break;
2269                                 case 2:
2270                                         control->set_automation_state (ARDOUR::Write);
2271                                         ret = 0;
2272                                         break;
2273                                 case 3:
2274                                         control->set_automation_state (ARDOUR::Touch);
2275                                         ret = 0;
2276                                         break;
2277                                 default:
2278                                         break;
2279                         }
2280                 }
2281         }
2282
2283         return ret;
2284 }
2285
2286 int
2287 OSC::route_mute (int ssid, int yn, lo_message msg)
2288 {
2289         if (!session) return -1;
2290         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2291
2292         if (s) {
2293                 if (s->mute_control()) {
2294                         s->mute_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2295                         return 0;
2296                 }
2297         }
2298
2299         return route_send_fail ("mute", ssid, 0, get_address (msg));
2300 }
2301
2302 int
2303 OSC::sel_mute (uint32_t yn, lo_message msg)
2304 {
2305         OSCSurface *sur = get_surface(get_address (msg));
2306         boost::shared_ptr<Stripable> s;
2307         if (sur->expand_enable) {
2308                 s = get_strip (sur->expand, get_address (msg));
2309         } else {
2310                 s = _select;
2311         }
2312         if (s) {
2313                 if (s->mute_control()) {
2314                         s->mute_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2315                         return 0;
2316                 }
2317         }
2318         return sel_fail ("mute", 0, get_address (msg));
2319 }
2320
2321 int
2322 OSC::route_solo (int ssid, int yn, lo_message msg)
2323 {
2324         if (!session) return -1;
2325         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2326
2327         if (s) {
2328                 if (s->solo_control()) {
2329                         s->solo_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2330                 }
2331         }
2332
2333         return route_send_fail ("solo", ssid, 0, get_address (msg));
2334 }
2335
2336 int
2337 OSC::route_solo_iso (int ssid, int yn, lo_message msg)
2338 {
2339         if (!session) return -1;
2340         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2341
2342         if (s) {
2343                 if (s->solo_isolate_control()) {
2344                         s->solo_isolate_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2345                         return 0;
2346                 }
2347         }
2348
2349         return route_send_fail ("solo_iso", ssid, 0, get_address (msg));
2350 }
2351
2352 int
2353 OSC::route_solo_safe (int ssid, int yn, lo_message msg)
2354 {
2355         if (!session) return -1;
2356         boost::shared_ptr<Stripable> s = get_strip (ssid, lo_message_get_source (msg));
2357
2358         if (s) {
2359                 if (s->solo_safe_control()) {
2360                         s->solo_safe_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2361                         return 0;
2362                 }
2363         }
2364
2365         return route_send_fail ("solo_safe", ssid, 0, get_address (msg));
2366 }
2367
2368 int
2369 OSC::sel_solo (uint32_t yn, lo_message msg)
2370 {
2371         OSCSurface *sur = get_surface(get_address (msg));
2372         boost::shared_ptr<Stripable> s;
2373         if (sur->expand_enable) {
2374                 s = get_strip (sur->expand, get_address (msg));
2375         } else {
2376                 s = _select;
2377         }
2378         if (s) {
2379                 if (s->solo_control()) {
2380                         session->set_control (s->solo_control(), yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2381                 }
2382         }
2383         return sel_fail ("solo", 0, get_address (msg));
2384 }
2385
2386 int
2387 OSC::sel_solo_iso (uint32_t yn, lo_message msg)
2388 {
2389         OSCSurface *sur = get_surface(get_address (msg));
2390         boost::shared_ptr<Stripable> s;
2391         if (sur->expand_enable) {
2392                 s = get_strip (sur->expand, get_address (msg));
2393         } else {
2394                 s = _select;
2395         }
2396         if (s) {
2397                 if (s->solo_isolate_control()) {
2398                         s->solo_isolate_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2399                         return 0;
2400                 }
2401         }
2402         return sel_fail ("solo_iso", 0, get_address (msg));
2403 }
2404
2405 int
2406 OSC::sel_solo_safe (uint32_t yn, lo_message msg)
2407 {
2408         OSCSurface *sur = get_surface(get_address (msg));
2409         boost::shared_ptr<Stripable> s;
2410         if (sur->expand_enable) {
2411                 s = get_strip (sur->expand, get_address (msg));
2412         } else {
2413                 s = _select;
2414         }
2415         if (s) {
2416                 if (s->solo_safe_control()) {
2417                         s->solo_safe_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2418                         return 0;
2419                 }
2420         }
2421         return sel_fail ("solo_safe", 0, get_address (msg));
2422 }
2423
2424 int
2425 OSC::sel_recenable (uint32_t yn, lo_message msg)
2426 {
2427         OSCSurface *sur = get_surface(get_address (msg));
2428         boost::shared_ptr<Stripable> s;
2429         if (sur->expand_enable) {
2430                 s = get_strip (sur->expand, get_address (msg));
2431         } else {
2432                 s = _select;
2433         }
2434         if (s) {
2435                 if (s->rec_enable_control()) {
2436                         s->rec_enable_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2437                         if (s->rec_enable_control()->get_value()) {
2438                                 return 0;
2439                         }
2440                 }
2441         }
2442         return sel_fail ("recenable", 0, get_address (msg));
2443 }
2444
2445 int
2446 OSC::route_recenable (int ssid, int yn, lo_message msg)
2447 {
2448         if (!session) return -1;
2449         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2450
2451         if (s) {
2452                 if (s->rec_enable_control()) {
2453                         s->rec_enable_control()->set_value (yn, PBD::Controllable::UseGroup);
2454                         if (s->rec_enable_control()->get_value()) {
2455                                 return 0;
2456                         }
2457                 }
2458         }
2459         return route_send_fail ("recenable", ssid, 0, get_address (msg));
2460 }
2461
2462 int
2463 OSC::route_rename(int ssid, char *newname, lo_message msg) {
2464     if (!session) {
2465         return -1;
2466     }
2467
2468     boost::shared_ptr<Stripable> s = get_strip(ssid, get_address(msg));
2469
2470     if (s) {
2471         s->set_name(std::string(newname));
2472     }
2473
2474     return 0;
2475 }
2476
2477 int
2478 OSC::sel_recsafe (uint32_t yn, lo_message msg)
2479 {
2480         OSCSurface *sur = get_surface(get_address (msg));
2481         boost::shared_ptr<Stripable> s;
2482         if (sur->expand_enable) {
2483                 s = get_strip (sur->expand, get_address (msg));
2484         } else {
2485                 s = _select;
2486         }
2487         if (s) {
2488                 if (s->rec_safe_control()) {
2489                         s->rec_safe_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2490                         if (s->rec_safe_control()->get_value()) {
2491                                 return 0;
2492                         }
2493                 }
2494         }
2495         return sel_fail ("record_safe", 0, get_address (msg));
2496 }
2497
2498 int
2499 OSC::route_recsafe (int ssid, int yn, lo_message msg)
2500 {
2501         if (!session) return -1;
2502         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2503         if (s) {
2504                 if (s->rec_safe_control()) {
2505                         s->rec_safe_control()->set_value (yn, PBD::Controllable::UseGroup);
2506                         if (s->rec_safe_control()->get_value()) {
2507                                 return 0;
2508                         }
2509                 }
2510         }
2511         return route_send_fail ("record_safe", ssid, 0,get_address (msg));
2512 }
2513
2514 int
2515 OSC::route_monitor_input (int ssid, int yn, lo_message msg)
2516 {
2517         if (!session) return -1;
2518         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2519
2520         if (s) {
2521                 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (s);
2522                 if (track) {
2523                         if (track->monitoring_control()) {
2524                                 track->monitoring_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2525                                 return 0;
2526                         }
2527                 }
2528         }
2529
2530         return route_send_fail ("monitor_input", ssid, 0, get_address (msg));
2531 }
2532
2533 int
2534 OSC::sel_monitor_input (uint32_t yn, lo_message msg)
2535 {
2536         OSCSurface *sur = get_surface(get_address (msg));
2537         boost::shared_ptr<Stripable> s;
2538         if (sur->expand_enable) {
2539                 s = get_strip (sur->expand, get_address (msg));
2540         } else {
2541                 s = _select;
2542         }
2543         if (s) {
2544                 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (s);
2545                 if (track) {
2546                         if (track->monitoring_control()) {
2547                                 track->monitoring_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2548                                 return 0;
2549                         }
2550                 }
2551         }
2552         return sel_fail ("monitor_input", 0, get_address (msg));
2553 }
2554
2555 int
2556 OSC::route_monitor_disk (int ssid, int yn, lo_message msg)
2557 {
2558         if (!session) return -1;
2559         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2560
2561         if (s) {
2562                 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (s);
2563                 if (track) {
2564                         if (track->monitoring_control()) {
2565                                 track->monitoring_control()->set_value (yn ? 2.0 : 0.0, PBD::Controllable::NoGroup);
2566                                 return 0;
2567                         }
2568                 }
2569         }
2570
2571         return route_send_fail ("monitor_disk", ssid, 0, get_address (msg));
2572 }
2573
2574 int
2575 OSC::sel_monitor_disk (uint32_t yn, lo_message msg)
2576 {
2577         OSCSurface *sur = get_surface(get_address (msg));
2578         boost::shared_ptr<Stripable> s;
2579         if (sur->expand_enable) {
2580                 s = get_strip (sur->expand, get_address (msg));
2581         } else {
2582                 s = _select;
2583         }
2584         if (s) {
2585                 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (s);
2586                 if (track) {
2587                         if (track->monitoring_control()) {
2588                                 track->monitoring_control()->set_value (yn ? 2.0 : 0.0, PBD::Controllable::NoGroup);
2589                                 return 0;
2590                         }
2591                 }
2592         }
2593         return sel_fail ("monitor_disk", 0, get_address (msg));
2594 }
2595
2596
2597 int
2598 OSC::strip_phase (int ssid, int yn, lo_message msg)
2599 {
2600         if (!session) return -1;
2601         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2602
2603         if (s) {
2604                 if (s->phase_control()) {
2605                         s->phase_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2606                         return 0;
2607                 }
2608         }
2609
2610         return route_send_fail ("polarity", ssid, 0, get_address (msg));
2611 }
2612
2613 int
2614 OSC::sel_phase (uint32_t yn, lo_message msg)
2615 {
2616         OSCSurface *sur = get_surface(get_address (msg));
2617         boost::shared_ptr<Stripable> s;
2618         if (sur->expand_enable) {
2619                 s = get_strip (sur->expand, get_address (msg));
2620         } else {
2621                 s = _select;
2622         }
2623         if (s) {
2624                 if (s->phase_control()) {
2625                         s->phase_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2626                         return 0;
2627                 }
2628         }
2629         return sel_fail ("polarity", 0, get_address (msg));
2630 }
2631
2632 int
2633 OSC::strip_expand (int ssid, int yn, lo_message msg)
2634 {
2635         OSCSurface *sur = get_surface(get_address (msg));
2636         sur->expand_enable = (bool) yn;
2637         sur->expand = ssid;
2638         boost::shared_ptr<Stripable> s;
2639         if (yn) {
2640                 s = get_strip (ssid, get_address (msg));
2641         } else {
2642                 s = ControlProtocol::first_selected_stripable();
2643         }
2644
2645         return _strip_select (s, get_address (msg));
2646 }
2647
2648 int
2649 OSC::_strip_select (boost::shared_ptr<Stripable> s, lo_address addr)
2650 {
2651         if (!session) {
2652                 return -1;
2653         }
2654         OSCSurface *sur = get_surface(addr);
2655         if (sur->sel_obs) {
2656                 delete sur->sel_obs;
2657                 sur->sel_obs = 0;
2658         }
2659         bool feedback_on = sur->feedback.to_ulong();
2660         if (s && feedback_on) {
2661                 OSCSelectObserver* sel_fb = new OSCSelectObserver (s, addr, sur->gainmode, sur->feedback);
2662                 s->DropReferences.connect (*this, MISSING_INVALIDATOR, boost::bind (&OSC::recalcbanks, this), this);
2663                 sur->sel_obs = sel_fb;
2664         } else if (sur->expand_enable) {
2665                 sur->expand = 0;
2666                 sur->expand_enable = false;
2667                 if (_select && feedback_on) {
2668                         OSCSelectObserver* sel_fb = new OSCSelectObserver (_select, addr, sur->gainmode, sur->feedback);
2669                         _select->DropReferences.connect (*this, MISSING_INVALIDATOR, boost::bind (&OSC::recalcbanks, this), this);
2670                         sur->sel_obs = sel_fb;
2671                 }
2672         } else if (feedback_on) {
2673                 route_send_fail ("select", sur->expand, 0 , addr);
2674         }
2675         if (!feedback_on) {
2676                 return 0;
2677         }
2678         //update buttons on surface
2679         int b_s = sur->bank_size;
2680         if (!b_s) { // bank size 0 means we need to know how many strips there are.
2681                 b_s = sur->nstrips;
2682         }
2683         for (int i = 1;  i <= b_s; i++) {
2684                 string path = "expand";
2685
2686                 if ((i == (int) sur->expand) && sur->expand_enable) {
2687                         lo_message reply = lo_message_new ();
2688                         if (sur->feedback[2]) {
2689                                 ostringstream os;
2690                                 os << "/strip/" << path << "/" << i;
2691                                 path = os.str();
2692                         } else {
2693                                 ostringstream os;
2694                                 os << "/strip/" << path;
2695                                 path = os.str();
2696                                 lo_message_add_int32 (reply, i);
2697                         }
2698                         lo_message_add_float (reply, (float) 1);
2699
2700                         lo_send_message (addr, path.c_str(), reply);
2701                         lo_message_free (reply);
2702                         reply = lo_message_new ();
2703                         lo_message_add_float (reply, 1.0);
2704                         lo_send_message (addr, "/select/expand", reply);
2705                         lo_message_free (reply);
2706
2707                 } else {
2708                         lo_message reply = lo_message_new ();
2709                         lo_message_add_int32 (reply, i);
2710                         lo_message_add_float (reply, 0.0);
2711                         lo_send_message (addr, "/strip/expand", reply);
2712                         lo_message_free (reply);
2713                 }
2714         }
2715         if (!sur->expand_enable) {
2716                 lo_message reply = lo_message_new ();
2717                 lo_message_add_float (reply, 0.0);
2718                 lo_send_message (addr, "/select/expand", reply);
2719                 lo_message_free (reply);
2720         }
2721
2722         return 0;
2723 }
2724
2725 int
2726 OSC::strip_gui_select (int ssid, int yn, lo_message msg)
2727 {
2728         //ignore button release
2729         if (!yn) return 0;
2730
2731         if (!session) {
2732                 return -1;
2733         }
2734         OSCSurface *sur = get_surface(get_address (msg));
2735         sur->expand_enable = false;
2736         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2737         if (s) {
2738                 SetStripableSelection (s);
2739         } else {
2740                 if ((int) (sur->feedback.to_ulong())) {
2741                         route_send_fail ("select", ssid, 0, get_address (msg));
2742                 }
2743         }
2744
2745         return 0;
2746 }
2747
2748 int
2749 OSC::sel_expand (uint32_t state, lo_message msg)
2750 {
2751         OSCSurface *sur = get_surface(get_address (msg));
2752         boost::shared_ptr<Stripable> s;
2753         sur->expand_enable = (bool) state;
2754         if (state && sur->expand) {
2755                 s = get_strip (sur->expand, get_address (msg));
2756         } else {
2757                 s = ControlProtocol::first_selected_stripable();
2758         }
2759
2760         return _strip_select (s, get_address (msg));
2761 }
2762
2763 int
2764 OSC::route_set_gain_abs (int ssid, float level, lo_message msg)
2765 {
2766         if (!session) return -1;
2767         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2768
2769         if (s) {
2770                 if (s->gain_control()) {
2771                         s->gain_control()->set_value (level, PBD::Controllable::NoGroup);
2772                 } else {
2773                         return 1;
2774                 }
2775         } else {
2776                 return 1;
2777         }
2778
2779         return 0;
2780 }
2781
2782 int
2783 OSC::route_set_gain_dB (int ssid, float dB, lo_message msg)
2784 {
2785         if (!session) {
2786                 route_send_fail ("gain", ssid, -193, get_address (msg));
2787                 return -1;
2788         }
2789         int ret;
2790         if (dB < -192) {
2791                 ret = route_set_gain_abs (ssid, 0.0, msg);
2792         } else {
2793                 ret = route_set_gain_abs (ssid, dB_to_coefficient (dB), msg);
2794         }
2795         if (ret != 0) {
2796                 return route_send_fail ("gain", ssid, -193, get_address (msg));
2797         }
2798         return 0;
2799 }
2800
2801 int
2802 OSC::sel_gain (float val, lo_message msg)
2803 {
2804         OSCSurface *sur = get_surface(get_address (msg));
2805         boost::shared_ptr<Stripable> s;
2806         if (sur->expand_enable) {
2807                 s = get_strip (sur->expand, get_address (msg));
2808         } else {
2809                 s = _select;
2810         }
2811         if (s) {
2812                 float abs;
2813                 if (val < -192) {
2814                         abs = 0;
2815                 } else {
2816                         abs = dB_to_coefficient (val);
2817                 }
2818                 if (s->gain_control()) {
2819                         s->gain_control()->set_value (abs, PBD::Controllable::NoGroup);
2820                         return 0;
2821                 }
2822         }
2823         return sel_fail ("gain", -193, get_address (msg));
2824 }
2825
2826 int
2827 OSC::route_set_gain_fader (int ssid, float pos, lo_message msg)
2828 {
2829         if (!session) {
2830                 route_send_fail ("fader", ssid, 0, get_address (msg));
2831                 return -1;
2832         }
2833         int ret;
2834         ret = route_set_gain_abs (ssid, slider_position_to_gain_with_max (pos, 2.0), msg);
2835         if (ret != 0) {
2836                 return route_send_fail ("fader", ssid, 0, get_address (msg));
2837         }
2838         return 0;
2839 }
2840
2841 int
2842 OSC::sel_fader (float val, lo_message msg)
2843 {
2844         OSCSurface *sur = get_surface(get_address (msg));
2845         boost::shared_ptr<Stripable> s;
2846         if (sur->expand_enable) {
2847                 s = get_strip (sur->expand, get_address (msg));
2848         } else {
2849                 s = _select;
2850         }
2851         if (s) {
2852                 float abs;
2853                 abs = slider_position_to_gain_with_max (val, 2.0);
2854                 if (s->gain_control()) {
2855                         s->gain_control()->set_value (abs, PBD::Controllable::NoGroup);
2856                         return 0;
2857                 }
2858         }
2859         return sel_fail ("fader", 0, get_address (msg));
2860 }
2861
2862 int
2863 OSC::route_set_trim_abs (int ssid, float level, lo_message msg)
2864 {
2865         if (!session) return -1;
2866         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2867
2868         if (s) {
2869                 if (s->trim_control()) {
2870                         s->trim_control()->set_value (level, PBD::Controllable::NoGroup);
2871                         return 0;
2872                 }
2873
2874         }
2875
2876         return -1;
2877 }
2878
2879 int
2880 OSC::route_set_trim_dB (int ssid, float dB, lo_message msg)
2881 {
2882         int ret;
2883         ret = route_set_trim_abs(ssid, dB_to_coefficient (dB), msg);
2884         if (ret != 0) {
2885                 return route_send_fail ("trimdB", ssid, 0, get_address (msg));
2886         }
2887
2888 return 0;
2889 }
2890
2891 int
2892 OSC::sel_trim (float val, lo_message msg)
2893 {
2894         OSCSurface *sur = get_surface(get_address (msg));
2895         boost::shared_ptr<Stripable> s;
2896         if (sur->expand_enable) {
2897                 s = get_strip (sur->expand, get_address (msg));
2898         } else {
2899                 s = _select;
2900         }
2901         if (s) {
2902                 if (s->trim_control()) {
2903                         s->trim_control()->set_value (dB_to_coefficient (val), PBD::Controllable::NoGroup);
2904                         return 0;
2905                 }
2906         }
2907         return sel_fail ("trimdB", 0, get_address (msg));
2908 }
2909
2910 int
2911 OSC::sel_pan_position (float val, lo_message msg)
2912 {
2913         OSCSurface *sur = get_surface(get_address (msg));
2914         boost::shared_ptr<Stripable> s;
2915         if (sur->expand_enable) {
2916                 s = get_strip (sur->expand, get_address (msg));
2917         } else {
2918                 s = _select;
2919         }
2920         if (s) {
2921                 if(s->pan_azimuth_control()) {
2922                         s->pan_azimuth_control()->set_value (s->pan_azimuth_control()->interface_to_internal (val), PBD::Controllable::NoGroup);
2923                         return 0;
2924                 }
2925         }
2926         return sel_fail ("pan_stereo_position", 0.5, get_address (msg));
2927 }
2928
2929 int
2930 OSC::sel_pan_width (float val, lo_message msg)
2931 {
2932         OSCSurface *sur = get_surface(get_address (msg));
2933         boost::shared_ptr<Stripable> s;
2934         if (sur->expand_enable) {
2935                 s = get_strip (sur->expand, get_address (msg));
2936         } else {
2937                 s = _select;
2938         }
2939         if (s) {
2940                 if (s->pan_width_control()) {
2941                         s->pan_width_control()->set_value (s->pan_width_control()->interface_to_internal (val), PBD::Controllable::NoGroup);
2942                         return 0;
2943                 }
2944         }
2945         return sel_fail ("pan_stereo_width", 1, get_address (msg));
2946 }
2947
2948 int
2949 OSC::route_set_pan_stereo_position (int ssid, float pos, lo_message msg)
2950 {
2951         if (!session) return -1;
2952         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2953
2954         if (s) {
2955                 if(s->pan_azimuth_control()) {
2956                         s->pan_azimuth_control()->set_value (s->pan_azimuth_control()->interface_to_internal (pos), PBD::Controllable::NoGroup);
2957                         return 0;
2958                 }
2959         }
2960
2961         return route_send_fail ("pan_stereo_position", ssid, 0.5, get_address (msg));
2962 }
2963
2964 int
2965 OSC::route_set_pan_stereo_width (int ssid, float pos, lo_message msg)
2966 {
2967         if (!session) return -1;
2968         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2969
2970         if (s) {
2971                 if (s->pan_width_control()) {
2972                         s->pan_width_control()->set_value (pos, PBD::Controllable::NoGroup);
2973                         return 0;
2974                 }
2975         }
2976
2977         return route_send_fail ("pan_stereo_width", ssid, 1, get_address (msg));
2978 }
2979
2980 int
2981 OSC::route_set_send_gain_dB (int ssid, int id, float val, lo_message msg)
2982 {
2983         if (!session) {
2984                 return -1;
2985         }
2986         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2987         float abs;
2988         if (s) {
2989                 if (id > 0) {
2990                         --id;
2991                 }
2992 #ifdef MIXBUS
2993                 abs = val;
2994 #else
2995                 if (val < -192) {
2996                         abs = 0;
2997                 } else {
2998                         abs = dB_to_coefficient (val);
2999                 }
3000 #endif
3001                 if (s->send_level_controllable (id)) {
3002                         s->send_level_controllable (id)->set_value (abs, PBD::Controllable::NoGroup);
3003                         return 0;
3004                 }
3005         }
3006         return 0;
3007 }
3008
3009 int
3010 OSC::route_set_send_fader (int ssid, int id, float val, lo_message msg)
3011 {
3012         if (!session) {
3013                 return -1;
3014         }
3015         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3016         float abs;
3017         if (s) {
3018
3019                 if (id > 0) {
3020                         --id;
3021                 }
3022
3023                 if (s->send_level_controllable (id)) {
3024 #ifdef MIXBUS
3025                         abs = s->send_level_controllable(id)->interface_to_internal (val);
3026 #else
3027                         abs = slider_position_to_gain_with_max (val, 2.0);
3028 #endif
3029                         s->send_level_controllable (id)->set_value (abs, PBD::Controllable::NoGroup);
3030                         return 0;
3031                 }
3032         }
3033         return 0;
3034 }
3035
3036 int
3037 OSC::sel_sendgain (int id, float val, lo_message msg)
3038 {
3039         OSCSurface *sur = get_surface(get_address (msg));
3040         boost::shared_ptr<Stripable> s;
3041         if (sur->expand_enable) {
3042                 s = get_strip (sur->expand, get_address (msg));
3043         } else {
3044                 s = _select;
3045         }
3046         float abs;
3047         if (s) {
3048                 if (id > 0) {
3049                         --id;
3050                 }
3051 #ifdef MIXBUS
3052                 abs = val;
3053 #else
3054                 if (val < -192) {
3055                         abs = 0;
3056                 } else {
3057                         abs = dB_to_coefficient (val);
3058                 }
3059 #endif
3060                 if (s->send_level_controllable (id)) {
3061                         s->send_level_controllable (id)->set_value (abs, PBD::Controllable::NoGroup);
3062                         return 0;
3063                 }
3064         }
3065         return sel_send_fail ("send_gain", id + 1, -193, get_address (msg));
3066 }
3067
3068 int
3069 OSC::sel_sendfader (int id, float val, lo_message msg)
3070 {
3071         OSCSurface *sur = get_surface(get_address (msg));
3072         boost::shared_ptr<Stripable> s;
3073         if (sur->expand_enable) {
3074                 s = get_strip (sur->expand, get_address (msg));
3075         } else {
3076                 s = _select;
3077         }
3078         float abs;
3079         if (s) {
3080
3081                 if (id > 0) {
3082                         --id;
3083                 }
3084
3085                 if (s->send_level_controllable (id)) {
3086 #ifdef MIXBUS
3087                         abs = s->send_level_controllable(id)->interface_to_internal (val);
3088 #else
3089                         abs = slider_position_to_gain_with_max (val, 2.0);
3090 #endif
3091                         s->send_level_controllable (id)->set_value (abs, PBD::Controllable::NoGroup);
3092                         return 0;
3093                 }
3094         }
3095         return sel_send_fail ("send_fader", id, 0, get_address (msg));
3096 }
3097
3098 int
3099 OSC::route_set_send_enable (int ssid, int sid, float val, lo_message msg)
3100 {
3101         if (!session) {
3102                 return -1;
3103         }
3104         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3105
3106         if (s) {
3107
3108                 /* revert to zero-based counting */
3109
3110                 if (sid > 0) {
3111                         --sid;
3112                 }
3113
3114                 if (s->send_enable_controllable (sid)) {
3115                         s->send_enable_controllable (sid)->set_value (val, PBD::Controllable::NoGroup);
3116                         return 0;
3117                 }
3118
3119                 if (s->send_level_controllable (sid)) {
3120                         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
3121                         if (!r) {
3122                                 return 0;
3123                         }
3124                         boost::shared_ptr<Send> snd = boost::dynamic_pointer_cast<Send> (r->nth_send(sid));
3125                         if (snd) {
3126                                 if (val) {
3127                                         snd->activate();
3128                                 } else {
3129                                         snd->deactivate();
3130                                 }
3131                         }
3132                         return 0;
3133                 }
3134
3135         }
3136
3137         return -1;
3138 }
3139
3140 int
3141 OSC::sel_sendenable (int id, float val, lo_message msg)
3142 {
3143         OSCSurface *sur = get_surface(get_address (msg));
3144         boost::shared_ptr<Stripable> s;
3145         if (sur->expand_enable) {
3146                 s = get_strip (sur->expand, get_address (msg));
3147         } else {
3148                 s = _select;
3149         }
3150         if (s) {
3151                 if (id > 0) {
3152                         --id;
3153                 }
3154                 if (s->send_enable_controllable (id)) {
3155                         s->send_enable_controllable (id)->set_value (val, PBD::Controllable::NoGroup);
3156                         return 0;
3157                 }
3158                 if (s->send_level_controllable (id)) {
3159                         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
3160                         if (!r) {
3161                                 // should never get here
3162                                 return sel_send_fail ("send_enable", id + 1, 0, get_address (msg));
3163                         }
3164                         boost::shared_ptr<Send> snd = boost::dynamic_pointer_cast<Send> (r->nth_send(id));
3165                         if (snd) {
3166                                 if (val) {
3167                                         snd->activate();
3168                                 } else {
3169                                         snd->deactivate();
3170                                 }
3171                         }
3172                         return 0;
3173                 }
3174         }
3175         return sel_send_fail ("send_enable", id + 1, 0, get_address (msg));
3176 }
3177
3178 int
3179 OSC::route_plugin_list (int ssid, lo_message msg) {
3180         if (!session) {
3181                 return -1;
3182         }
3183
3184         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route>(get_strip (ssid, get_address (msg)));
3185
3186         if (!r) {
3187                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
3188                 return -1;
3189         }
3190         int piid = 0;
3191
3192         lo_message reply = lo_message_new ();
3193         lo_message_add_int32 (reply, ssid);
3194
3195
3196         for (;;) {
3197                 boost::shared_ptr<Processor> redi = r->nth_plugin(piid);
3198                 if ( !redi ) {
3199                         break;
3200                 }
3201
3202                 boost::shared_ptr<PluginInsert> pi;
3203
3204                 if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
3205                         PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
3206                         continue;
3207                 }
3208                 lo_message_add_int32 (reply, piid + 1);
3209
3210                 boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
3211                 lo_message_add_string (reply, pip->name());
3212
3213                 piid++;
3214         }
3215
3216         lo_send_message (get_address (msg), "/strip/plugin/list", reply);
3217         lo_message_free (reply);
3218         return 0;
3219 }
3220
3221 int
3222 OSC::route_plugin_descriptor (int ssid, int piid, lo_message msg) {
3223         if (!session) {
3224                 return -1;
3225         }
3226
3227         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route>(get_strip (ssid, get_address (msg)));
3228
3229         if (!r) {
3230                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
3231                 return -1;
3232         }
3233
3234         boost::shared_ptr<Processor> redi = r->nth_plugin(piid - 1);
3235
3236         if (!redi) {
3237                 PBD::error << "OSC: cannot find plugin # " << piid << " for RID '" << ssid << "'" << endmsg;
3238                 return -1;
3239         }
3240
3241         boost::shared_ptr<PluginInsert> pi;
3242
3243         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
3244                 PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
3245                 return -1;
3246         }
3247
3248         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
3249         bool ok = false;
3250
3251         lo_message reply = lo_message_new();
3252         lo_message_add_int32 (reply, ssid);
3253         lo_message_add_int32 (reply, piid);
3254         lo_message_add_string (reply, pip->name());
3255         for ( uint32_t ppi = 0; ppi < pip->parameter_count(); ppi++) {
3256
3257                 uint32_t controlid = pip->nth_parameter(ppi, ok);
3258                 if (!ok) {
3259                         continue;
3260                 }
3261                 if ( pip->parameter_is_input(controlid) || pip->parameter_is_control(controlid) ) {
3262                         boost::shared_ptr<AutomationControl> c = pi->automation_control(Evoral::Parameter(PluginAutomation, 0, controlid));
3263
3264                                 lo_message_add_int32 (reply, ppi + 1);
3265                                 ParameterDescriptor pd;
3266                                 pi->plugin()->get_parameter_descriptor(controlid, pd);
3267                                 lo_message_add_string (reply, pd.label.c_str());
3268
3269                                 // I've combined those binary descriptor parts in a bit-field to reduce lilo message elements
3270                                 int flags = 0;
3271                                 flags |= pd.enumeration ? 1 : 0;
3272                                 flags |= pd.integer_step ? 2 : 0;
3273                                 flags |= pd.logarithmic ? 4 : 0;
3274                                 flags |= pd.max_unbound ? 8 : 0;
3275                                 flags |= pd.min_unbound ? 16 : 0;
3276                                 flags |= pd.sr_dependent ? 32 : 0;
3277                                 flags |= pd.toggled ? 64 : 0;
3278                                 flags |= c != NULL ? 128 : 0; // bit 7 indicates in input control
3279                                 lo_message_add_int32 (reply, flags);
3280
3281                                 lo_message_add_int32 (reply, pd.datatype);
3282                                 lo_message_add_float (reply, pd.lower);
3283                                 lo_message_add_float (reply, pd.upper);
3284                                 lo_message_add_string (reply, pd.print_fmt.c_str());
3285                                 if ( pd.scale_points ) {
3286                                         lo_message_add_int32 (reply, pd.scale_points->size());
3287                                         for ( ARDOUR::ScalePoints::const_iterator i = pd.scale_points->begin(); i != pd.scale_points->end(); ++i) {
3288                                                 lo_message_add_int32 (reply, i->second);
3289                                                 lo_message_add_string (reply, ((std::string)i->first).c_str());
3290                                         }
3291                                 }
3292                                 else {
3293                                         lo_message_add_int32 (reply, 0);
3294                                 }
3295                                 if ( c ) {
3296                                         lo_message_add_double (reply, c->get_value());
3297                                 }
3298                                 else {
3299                                         lo_message_add_double (reply, 0);
3300                         }
3301                 }
3302         }
3303
3304         lo_send_message (get_address (msg), "/strip/plugin/descriptor", reply);
3305         lo_message_free (reply);
3306
3307         return 0;
3308 }
3309
3310 int
3311 OSC::route_plugin_reset (int ssid, int piid, lo_message msg) {
3312         if (!session) {
3313                 return -1;
3314         }
3315
3316         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route>(get_strip (ssid, get_address (msg)));
3317
3318         if (!r) {
3319                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
3320                 return -1;
3321         }
3322
3323         boost::shared_ptr<Processor> redi = r->nth_plugin(piid - 1);
3324
3325         if (!redi) {
3326                 PBD::error << "OSC: cannot find plugin # " << piid << " for RID '" << ssid << "'" << endmsg;
3327                 return -1;
3328         }
3329
3330         boost::shared_ptr<PluginInsert> pi;
3331
3332         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
3333                 PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
3334                 return -1;
3335         }
3336
3337         pi->reset_parameters_to_default ();
3338
3339         return 0;
3340 }
3341
3342 int
3343 OSC::route_plugin_parameter (int ssid, int piid, int par, float val, lo_message msg)
3344 {
3345         if (!session)
3346                 return -1;
3347         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3348
3349         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
3350
3351         if (!r) {
3352                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
3353                 return -1;
3354         }
3355
3356         boost::shared_ptr<Processor> redi=r->nth_plugin (piid - 1);
3357
3358         if (!redi) {
3359                 PBD::error << "OSC: cannot find plugin # " << piid << " for RID '" << ssid << "'" << endmsg;
3360                 return -1;
3361         }
3362
3363         boost::shared_ptr<PluginInsert> pi;
3364
3365         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
3366                 PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
3367                 return -1;
3368         }
3369
3370         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
3371         bool ok=false;
3372
3373         uint32_t controlid = pip->nth_parameter (par - 1,ok);
3374
3375         if (!ok) {
3376                 PBD::error << "OSC: Cannot find parameter # " << par <<  " for plugin # " << piid << " on RID '" << ssid << "'" << endmsg;
3377                 return -1;
3378         }
3379
3380         if (!pip->parameter_is_input(controlid)) {
3381                 PBD::error << "OSC: Parameter # " << par <<  " for plugin # " << piid << " on RID '" << ssid << "' is not a control input" << endmsg;
3382                 return -1;
3383         }
3384
3385         ParameterDescriptor pd;
3386         pi->plugin()->get_parameter_descriptor (controlid,pd);
3387
3388         if (val >= pd.lower && val <= pd.upper) {
3389
3390                 boost::shared_ptr<AutomationControl> c = pi->automation_control (Evoral::Parameter(PluginAutomation, 0, controlid));
3391                 // cerr << "parameter:" << redi->describe_parameter(controlid) << " val:" << val << "\n";
3392                 c->set_value (val, PBD::Controllable::NoGroup);
3393         } else {
3394                 PBD::warning << "OSC: Parameter # " << par <<  " for plugin # " << piid << " on RID '" << ssid << "' is out of range" << endmsg;
3395                 PBD::info << "OSC: Valid range min=" << pd.lower << " max=" << pd.upper << endmsg;
3396         }
3397
3398         return 0;
3399 }
3400
3401 //prints to cerr only
3402 int
3403 OSC::route_plugin_parameter_print (int ssid, int piid, int par, lo_message msg)
3404 {
3405         if (!session) {
3406                 return -1;
3407         }
3408         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3409
3410         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
3411
3412         if (!r) {
3413                 return -1;
3414         }
3415
3416         boost::shared_ptr<Processor> redi=r->nth_plugin (piid - 1);
3417
3418         if (!redi) {
3419                 return -1;
3420         }
3421
3422         boost::shared_ptr<PluginInsert> pi;
3423
3424         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
3425                 return -1;
3426         }
3427
3428         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
3429         bool ok=false;
3430
3431         uint32_t controlid = pip->nth_parameter (par - 1,ok);
3432
3433         if (!ok) {
3434                 return -1;
3435         }
3436
3437         ParameterDescriptor pd;
3438
3439         if (pi->plugin()->get_parameter_descriptor (controlid, pd) == 0) {
3440                 boost::shared_ptr<AutomationControl> c = pi->automation_control (Evoral::Parameter(PluginAutomation, 0, controlid));
3441
3442                 cerr << "parameter:     " << pd.label  << "\n";
3443                 if (c) {
3444                         cerr << "current value: " << c->get_value () << "\n";
3445                 } else {
3446                         cerr << "current value not available, control does not exist\n";
3447                 }
3448                 cerr << "lower value:   " << pd.lower << "\n";
3449                 cerr << "upper value:   " << pd.upper << "\n";
3450         }
3451
3452         return 0;
3453 }
3454
3455 int
3456 OSC::route_plugin_activate (int ssid, int piid, lo_message msg)
3457 {
3458         if (!session)
3459                 return -1;
3460         boost::shared_ptr<Stripable> s = get_strip (ssid, lo_message_get_source (msg));
3461
3462         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
3463
3464         if (!r) {
3465                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
3466                 return -1;
3467         }
3468
3469         boost::shared_ptr<Processor> redi=r->nth_plugin (piid - 1);
3470
3471         if (!redi) {
3472                 PBD::error << "OSC: cannot find plugin # " << piid << " for RID '" << ssid << "'" << endmsg;
3473                 return -1;
3474         }
3475
3476         boost::shared_ptr<PluginInsert> pi;
3477
3478         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
3479                 PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
3480                 return -1;
3481         }
3482
3483         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
3484         pi->activate();
3485
3486         return 0;
3487 }
3488
3489 int
3490 OSC::route_plugin_deactivate (int ssid, int piid, lo_message msg)
3491 {
3492         if (!session)
3493                 return -1;
3494         boost::shared_ptr<Stripable> s = get_strip (ssid, lo_message_get_source (msg));
3495
3496         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
3497
3498         if (!r) {
3499                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
3500                 return -1;
3501         }
3502
3503         boost::shared_ptr<Processor> redi=r->nth_plugin (piid - 1);
3504
3505         if (!redi) {
3506                 PBD::error << "OSC: cannot find plugin # " << piid << " for RID '" << ssid << "'" << endmsg;
3507                 return -1;
3508         }
3509
3510         boost::shared_ptr<PluginInsert> pi;
3511
3512         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
3513                 PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
3514                 return -1;
3515         }
3516
3517         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
3518         pi->deactivate();
3519
3520         return 0;
3521 }
3522
3523 // select
3524
3525 int
3526 OSC::sel_pan_elevation (float val, lo_message msg)
3527 {
3528         OSCSurface *sur = get_surface(get_address (msg));
3529         boost::shared_ptr<Stripable> s;
3530         if (sur->expand_enable) {
3531                 s = get_strip (sur->expand, get_address (msg));
3532         } else {
3533                 s = _select;
3534         }
3535         if (s) {
3536                 if (s->pan_elevation_control()) {
3537                         s->pan_elevation_control()->set_value (s->pan_elevation_control()->interface_to_internal (val), PBD::Controllable::NoGroup);
3538                         return 0;
3539                 }
3540         }
3541         return sel_fail ("pan_elevation_position", 0, get_address (msg));
3542 }
3543
3544 int
3545 OSC::sel_pan_frontback (float val, lo_message msg)
3546 {
3547         OSCSurface *sur = get_surface(get_address (msg));
3548         boost::shared_ptr<Stripable> s;
3549         if (sur->expand_enable) {
3550                 s = get_strip (sur->expand, get_address (msg));
3551         } else {
3552                 s = _select;
3553         }
3554         if (s) {
3555                 if (s->pan_frontback_control()) {
3556                         s->pan_frontback_control()->set_value (s->pan_frontback_control()->interface_to_internal (val), PBD::Controllable::NoGroup);
3557                         return 0;
3558                 }
3559         }
3560         return sel_fail ("pan_frontback_position", 0.5, get_address (msg));
3561 }
3562
3563 int
3564 OSC::sel_pan_lfe (float val, lo_message msg)
3565 {
3566         OSCSurface *sur = get_surface(get_address (msg));
3567         boost::shared_ptr<Stripable> s;
3568         if (sur->expand_enable) {
3569                 s = get_strip (sur->expand, get_address (msg));
3570         } else {
3571                 s = _select;
3572         }
3573         if (s) {
3574                 if (s->pan_lfe_control()) {
3575                         s->pan_lfe_control()->set_value (s->pan_lfe_control()->interface_to_internal (val), PBD::Controllable::NoGroup);
3576                         return 0;
3577                 }
3578         }
3579         return sel_fail ("pan_lfe_control", 0, get_address (msg));
3580 }
3581
3582 // compressor control
3583 int
3584 OSC::sel_comp_enable (float val, lo_message msg)
3585 {
3586         OSCSurface *sur = get_surface(get_address (msg));
3587         boost::shared_ptr<Stripable> s;
3588         if (sur->expand_enable) {
3589                 s = get_strip (sur->expand, get_address (msg));
3590         } else {
3591                 s = _select;
3592         }
3593         if (s) {
3594                 if (s->comp_enable_controllable()) {
3595                         s->comp_enable_controllable()->set_value (s->comp_enable_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
3596                         return 0;
3597                 }
3598         }
3599         return sel_fail ("comp_enable", 0, get_address (msg));
3600 }
3601
3602 int
3603 OSC::sel_comp_threshold (float val, lo_message msg)
3604 {
3605         OSCSurface *sur = get_surface(get_address (msg));
3606         boost::shared_ptr<Stripable> s;
3607         if (sur->expand_enable) {
3608                 s = get_strip (sur->expand, get_address (msg));
3609         } else {
3610                 s = _select;
3611         }
3612         if (s) {
3613                 if (s->comp_threshold_controllable()) {
3614                         s->comp_threshold_controllable()->set_value (s->comp_threshold_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
3615                         return 0;
3616                 }
3617         }
3618         return sel_fail ("comp_threshold", 0, get_address (msg));
3619 }
3620
3621 int
3622 OSC::sel_comp_speed (float val, lo_message msg)
3623 {
3624         OSCSurface *sur = get_surface(get_address (msg));
3625         boost::shared_ptr<Stripable> s;
3626         if (sur->expand_enable) {
3627                 s = get_strip (sur->expand, get_address (msg));
3628         } else {
3629                 s = _select;
3630         }
3631         if (s) {
3632                 if (s->comp_speed_controllable()) {
3633                         s->comp_speed_controllable()->set_value (s->comp_speed_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
3634                         return 0;
3635                 }
3636         }
3637         return sel_fail ("comp_speed", 0, get_address (msg));
3638 }
3639
3640 int
3641 OSC::sel_comp_mode (float val, lo_message msg)
3642 {
3643         OSCSurface *sur = get_surface(get_address (msg));
3644         boost::shared_ptr<Stripable> s;
3645         if (sur->expand_enable) {
3646                 s = get_strip (sur->expand, get_address (msg));
3647         } else {
3648                 s = _select;
3649         }
3650         if (s) {
3651                 if (s->comp_mode_controllable()) {
3652                         s->comp_mode_controllable()->set_value (s->comp_mode_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
3653                         return 0;
3654                 }
3655         }
3656         return sel_fail ("comp_mode", 0, get_address (msg));
3657 }
3658
3659 int
3660 OSC::sel_comp_makeup (float val, lo_message msg)
3661 {
3662         OSCSurface *sur = get_surface(get_address (msg));
3663         boost::shared_ptr<Stripable> s;
3664         if (sur->expand_enable) {
3665                 s = get_strip (sur->expand, get_address (msg));
3666         } else {
3667                 s = _select;
3668         }
3669         if (s) {
3670                 if (s->comp_makeup_controllable()) {
3671                         s->comp_makeup_controllable()->set_value (s->comp_makeup_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
3672                         return 0;
3673                 }
3674         }
3675         return sel_fail ("comp_makeup", 0, get_address (msg));
3676 }
3677
3678 // EQ control
3679
3680 int
3681 OSC::sel_eq_enable (float val, lo_message msg)
3682 {
3683         OSCSurface *sur = get_surface(get_address (msg));
3684         boost::shared_ptr<Stripable> s;
3685         if (sur->expand_enable) {
3686                 s = get_strip (sur->expand, get_address (msg));
3687         } else {
3688                 s = _select;
3689         }
3690         if (s) {
3691                 if (s->eq_enable_controllable()) {
3692                         s->eq_enable_controllable()->set_value (s->eq_enable_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
3693                         return 0;
3694                 }
3695         }
3696         return sel_fail ("eq_enable", 0, get_address (msg));
3697 }
3698
3699 int
3700 OSC::sel_eq_hpf (float val, lo_message msg)
3701 {
3702         OSCSurface *sur = get_surface(get_address (msg));
3703         boost::shared_ptr<Stripable> s;
3704         if (sur->expand_enable) {
3705                 s = get_strip (sur->expand, get_address (msg));
3706         } else {
3707                 s = _select;
3708         }
3709         if (s) {
3710                 if (s->filter_freq_controllable(true)) {
3711                         s->filter_freq_controllable(true)->set_value (s->filter_freq_controllable(true)->interface_to_internal (val), PBD::Controllable::NoGroup);
3712                         return 0;
3713                 }
3714         }
3715         return sel_fail ("eq_hpf", 0, get_address (msg));
3716 }
3717 // need to add two more filter controls
3718
3719 int
3720 OSC::sel_eq_gain (int id, float val, lo_message msg)
3721 {
3722         OSCSurface *sur = get_surface(get_address (msg));
3723         boost::shared_ptr<Stripable> s;
3724         if (sur->expand_enable) {
3725                 s = get_strip (sur->expand, get_address (msg));
3726         } else {
3727                 s = _select;
3728         }
3729         if (s) {
3730                 if (id > 0) {
3731                         --id;
3732                 }
3733                 if (s->eq_gain_controllable (id)) {
3734                         s->eq_gain_controllable (id)->set_value (s->eq_gain_controllable(id)->interface_to_internal (val), PBD::Controllable::NoGroup);
3735                         return 0;
3736                 }
3737         }
3738         return sel_send_fail ("eq_gain", id + 1, 0, get_address (msg));
3739 }
3740
3741 int
3742 OSC::sel_eq_freq (int id, float val, lo_message msg)
3743 {
3744         OSCSurface *sur = get_surface(get_address (msg));
3745         boost::shared_ptr<Stripable> s;
3746         if (sur->expand_enable) {
3747                 s = get_strip (sur->expand, get_address (msg));
3748         } else {
3749                 s = _select;
3750         }
3751         if (s) {
3752                 if (id > 0) {
3753                         --id;
3754                 }
3755                 if (s->eq_freq_controllable (id)) {
3756                         s->eq_freq_controllable (id)->set_value (s->eq_freq_controllable(id)->interface_to_internal (val), PBD::Controllable::NoGroup);
3757                         return 0;
3758                 }
3759         }
3760         return sel_send_fail ("eq_freq", id + 1, 0, get_address (msg));
3761 }
3762
3763 int
3764 OSC::sel_eq_q (int id, float val, lo_message msg)
3765 {
3766         OSCSurface *sur = get_surface(get_address (msg));
3767         boost::shared_ptr<Stripable> s;
3768         if (sur->expand_enable) {
3769                 s = get_strip (sur->expand, get_address (msg));
3770         } else {
3771                 s = _select;
3772         }
3773         if (s) {
3774                 if (id > 0) {
3775                         --id;
3776                 }
3777                 if (s->eq_q_controllable (id)) {
3778                         s->eq_q_controllable (id)->set_value (s->eq_q_controllable(id)->interface_to_internal (val), PBD::Controllable::NoGroup);
3779                         return 0;
3780                 }
3781         }
3782         return sel_send_fail ("eq_q", id + 1, 0, get_address (msg));
3783 }
3784
3785 int
3786 OSC::sel_eq_shape (int id, float val, lo_message msg)
3787 {
3788         OSCSurface *sur = get_surface(get_address (msg));
3789         boost::shared_ptr<Stripable> s;
3790         if (sur->expand_enable) {
3791                 s = get_strip (sur->expand, get_address (msg));
3792         } else {
3793                 s = _select;
3794         }
3795         if (s) {
3796                 if (id > 0) {
3797                         --id;
3798                 }
3799                 if (s->eq_shape_controllable (id)) {
3800                         s->eq_shape_controllable (id)->set_value (s->eq_shape_controllable(id)->interface_to_internal (val), PBD::Controllable::NoGroup);
3801                         return 0;
3802                 }
3803         }
3804         return sel_send_fail ("eq_shape", id + 1, 0, get_address (msg));
3805 }
3806
3807 void
3808 OSC::gui_selection_changed ()
3809 {
3810         boost::shared_ptr<Stripable> strip = ControlProtocol::first_selected_stripable();
3811
3812         if (strip) {
3813                 _select = strip;
3814                 for (uint32_t it = 0; it < _surface.size(); ++it) {
3815                         OSCSurface* sur = &_surface[it];
3816                         if(!sur->expand_enable) {
3817                                 lo_address addr = lo_address_new_from_url (sur->remote_url.c_str());
3818                                 _strip_select (strip, addr);
3819                         }
3820                 }
3821         }
3822 }
3823
3824 // timer callbacks
3825 bool
3826 OSC::periodic (void)
3827 {
3828         if (!tick) {
3829                 Glib::usleep(100); // let flurry of signals subside
3830                 if (global_init) {
3831                         for (uint32_t it = 0; it < _surface.size(); it++) {
3832                                 OSCSurface* sur = &_surface[it];
3833                                 lo_address addr = lo_address_new_from_url (sur->remote_url.c_str());
3834                                 global_feedback (sur->feedback, addr, sur->gainmode);
3835                         }
3836                         global_init = false;
3837                         tick = true;
3838                 }
3839                 if (bank_dirty) {
3840                         _recalcbanks ();
3841                         bank_dirty = false;
3842                         tick = true;
3843                 }
3844         }
3845
3846         if (scrub_speed != 0) {
3847                 // for those jog wheels that don't have 0 on release (touch), time out.
3848                 int64_t now = ARDOUR::get_microseconds ();
3849                 int64_t diff = now - scrub_time;
3850                 if (diff > 120000) {
3851                         scrub_speed = 0;
3852                         session->request_transport_speed (0);
3853                         // locate to the place PH was at last tick
3854                         session->request_locate (scrub_place, false);
3855                 }
3856         }
3857
3858         for (GlobalObservers::iterator x = global_observers.begin(); x != global_observers.end(); x++) {
3859
3860                 OSCGlobalObserver* go;
3861
3862                 if ((go = dynamic_cast<OSCGlobalObserver*>(*x)) != 0) {
3863                         go->tick();
3864                 }
3865         }
3866         for (RouteObservers::iterator x = route_observers.begin(); x != route_observers.end(); x++) {
3867
3868                 OSCRouteObserver* ro;
3869
3870                 if ((ro = dynamic_cast<OSCRouteObserver*>(*x)) != 0) {
3871                         ro->tick();
3872                 }
3873         }
3874         for (uint32_t it = 0; it < _surface.size(); it++) {
3875                 OSCSurface* sur = &_surface[it];
3876                 OSCSelectObserver* so;
3877                 if ((so = dynamic_cast<OSCSelectObserver*>(sur->sel_obs)) != 0) {
3878                         so->tick();
3879                 }
3880         }
3881         for (CueObservers::iterator x = cue_observers.begin(); x != cue_observers.end(); x++) {
3882
3883                 OSCCueObserver* co;
3884
3885                 if ((co = dynamic_cast<OSCCueObserver*>(*x)) != 0) {
3886                         co->tick();
3887                 }
3888         }
3889         return true;
3890 }
3891
3892 int
3893 OSC::route_send_fail (string path, uint32_t ssid, float val, lo_address addr)
3894 {
3895         OSCSurface *sur = get_surface(addr);
3896
3897         ostringstream os;
3898         lo_message reply;
3899         if (ssid) {
3900                 reply = lo_message_new ();
3901                 if (sur->feedback[2]) {
3902                         os << "/strip/" << path << "/" << ssid;
3903                 } else {
3904                         os << "/strip/" << path;
3905                         lo_message_add_int32 (reply, ssid);
3906                 }
3907                 string str_pth = os.str();
3908                 lo_message_add_float (reply, (float) val);
3909
3910                 lo_send_message (addr, str_pth.c_str(), reply);
3911                 lo_message_free (reply);
3912         }
3913         if ((_select == get_strip (ssid, addr)) || ((sur->expand == ssid) && (sur->expand_enable))) {
3914                 os.str("");
3915                 os << "/select/" << path;
3916                 string sel_pth = os.str();
3917                 reply = lo_message_new ();
3918                 lo_message_add_float (reply, (float) val);
3919                 lo_send_message (addr, sel_pth.c_str(), reply);
3920                 lo_message_free (reply);
3921         }
3922
3923         return 0;
3924 }
3925
3926 int
3927 OSC::sel_fail (string path, float val, lo_address addr)
3928 {
3929         ostringstream os;
3930         os.str("");
3931         os << "/select/" << path;
3932         string sel_pth = os.str();
3933         lo_message reply = lo_message_new ();
3934         lo_message_add_float (reply, (float) val);
3935         lo_send_message (addr, sel_pth.c_str(), reply);
3936         lo_message_free (reply);
3937
3938         return 0;
3939 }
3940
3941 int
3942 OSC::sel_send_fail (string path, uint32_t id, float val, lo_address addr)
3943 {
3944         OSCSurface *sur = get_surface(addr);
3945
3946         ostringstream os;
3947         lo_message reply;
3948         reply = lo_message_new ();
3949         if (sur->feedback[2]) {
3950                 os << "/select/" << path << "/" << id;
3951         } else {
3952                 os << "/select/" << path;
3953                 lo_message_add_int32 (reply, id);
3954         }
3955         string str_pth = os.str();
3956         lo_message_add_float (reply, (float) val);
3957
3958         lo_send_message (addr, str_pth.c_str(), reply);
3959         lo_message_free (reply);
3960
3961         return 0;
3962 }
3963
3964 XMLNode&
3965 OSC::get_state ()
3966 {
3967         XMLNode& node (ControlProtocol::get_state());
3968         node.set_property ("debugmode", (int32_t) _debugmode); // TODO: enum2str
3969         node.set_property ("address-only", address_only);
3970         node.set_property ("remote-port", remote_port);
3971         node.set_property ("banksize", default_banksize);
3972         node.set_property ("striptypes", default_strip);
3973         node.set_property ("feedback", default_feedback);
3974         node.set_property ("gainmode", default_gainmode);
3975         if (_surface.size()) {
3976                 XMLNode* config = new XMLNode (X_("Configurations"));
3977                 for (uint32_t it = 0; it < _surface.size(); ++it) {
3978                         OSCSurface* sur = &_surface[it];
3979                         XMLNode* devnode = new XMLNode (X_("Configuration"));
3980                         devnode->set_property (X_("url"), sur->remote_url);
3981                         devnode->set_property (X_("bank-size"), sur->bank_size);
3982                         devnode->set_property (X_("strip-types"), (uint64_t)sur->strip_types.to_ulong());
3983                         devnode->set_property (X_("feedback"), (uint64_t)sur->feedback.to_ulong());
3984                         devnode->set_property (X_("gainmode"), sur->gainmode);
3985                         config->add_child_nocopy (*devnode);
3986                 }
3987                 node.add_child_nocopy (*config);
3988         }
3989         return node;
3990 }
3991
3992 int
3993 OSC::set_state (const XMLNode& node, int version)
3994 {
3995         if (ControlProtocol::set_state (node, version)) {
3996                 return -1;
3997         }
3998         int32_t debugmode;
3999         if (node.get_property (X_("debugmode"), debugmode)) {
4000                 _debugmode = OSCDebugMode (debugmode);
4001         }
4002
4003         node.get_property (X_("address-only"), address_only);
4004         node.get_property (X_("remote-port"), remote_port);
4005         node.get_property (X_("banksize"), default_banksize);
4006         node.get_property (X_("striptypes"), default_strip);
4007         node.get_property (X_("feedback"), default_feedback);
4008         node.get_property (X_("gainmode"), default_gainmode);
4009
4010         XMLNode* cnode = node.child (X_("Configurations"));
4011
4012         if (cnode) {
4013                 XMLNodeList const& devices = cnode->children();
4014                 for (XMLNodeList::const_iterator d = devices.begin(); d != devices.end(); ++d) {
4015                         OSCSurface s;
4016                         if (!(*d)->get_property (X_("url"), s.remote_url)) {
4017                                 continue;
4018                         }
4019
4020                         bank_dirty = true;
4021
4022                         (*d)->get_property (X_("bank-size"), s.bank_size);
4023
4024                         uint64_t bits;
4025                         if ((*d)->get_property (X_ ("strip-types"), bits)) {
4026                                 s.strip_types = bits;
4027                         }
4028                         if ((*d)->get_property (X_("feedback"), bits)) {
4029                                 s.feedback = bits;
4030                         }
4031                         (*d)->get_property (X_("gainmode"), s.gainmode);
4032
4033                         s.bank = 1;
4034                         s.sel_obs = 0;
4035                         s.expand = 0;
4036                         s.expand_enable = false;
4037                         s.strips = get_sorted_stripables (s.strip_types, s.cue);
4038                         s.nstrips = s.strips.size ();
4039                         s.no_clear = false;
4040                         s.jogmode = JOG;
4041                         s.cue = false;
4042                         s.aux = 0;
4043                         _surface.push_back (s);
4044                 }
4045         }
4046         global_init = true;
4047         tick = false;
4048
4049         return 0;
4050 }
4051
4052 // predicate for sort call in get_sorted_stripables
4053 struct StripableByPresentationOrder
4054 {
4055         bool operator () (const boost::shared_ptr<Stripable> & a, const boost::shared_ptr<Stripable> & b) const
4056         {
4057                 return a->presentation_info().order() < b->presentation_info().order();
4058         }
4059
4060         bool operator () (const Stripable & a, const Stripable & b) const
4061         {
4062                 return a.presentation_info().order() < b.presentation_info().order();
4063         }
4064
4065         bool operator () (const Stripable * a, const Stripable * b) const
4066         {
4067                 return a->presentation_info().order() < b->presentation_info().order();
4068         }
4069 };
4070
4071 OSC::Sorted
4072 OSC::get_sorted_stripables(std::bitset<32> types, bool cue)
4073 {
4074         Sorted sorted;
4075
4076         // fetch all stripables
4077         StripableList stripables;
4078
4079         session->get_stripables (stripables);
4080
4081         // Look for stripables that match bit in sur->strip_types
4082         for (StripableList::iterator it = stripables.begin(); it != stripables.end(); ++it) {
4083
4084                 boost::shared_ptr<Stripable> s = *it;
4085                 if ((!cue) && (!types[9]) && (s->presentation_info().flags() & PresentationInfo::Hidden)) {
4086                         // do nothing... skip it
4087                 } else {
4088
4089                         if (types[0] && (s->presentation_info().flags() & PresentationInfo::AudioTrack)) {
4090                                 sorted.push_back (s);
4091                         } else
4092                         if (types[1] && (s->presentation_info().flags() & PresentationInfo::MidiTrack)) {
4093                                 sorted.push_back (s);
4094                         } else
4095                         if ((s->presentation_info().flags() & PresentationInfo::AudioBus)) {
4096                                 boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
4097                                 // r->feeds (session->master_out()) may make more sense
4098                                 if (r->direct_feeds_according_to_reality (session->master_out())) {
4099                                         // this is a bus
4100                                         if (types[2]) {
4101                                                 sorted.push_back (s);
4102                                         }
4103                                 } else {
4104                                         // this is an Aux out
4105                                         if (types[7]) {
4106                                                 sorted.push_back (s);
4107                                         }
4108                                 }
4109                         } else if (types[3] && (s->presentation_info().flags() & PresentationInfo::MidiBus)) {
4110                                 sorted.push_back (s);
4111                         } else if (types[4] && (s->presentation_info().flags() & PresentationInfo::VCA)) {
4112                                 sorted.push_back (s);
4113                         } else if (types[8] && (s->is_selected())) {
4114                                 sorted.push_back (s);
4115                         } else if (types[9] && (s->presentation_info().flags() & PresentationInfo::Hidden)) {
4116                                 sorted.push_back (s);
4117                         }
4118                 }
4119         }
4120         sort (sorted.begin(), sorted.end(), StripableByPresentationOrder());
4121         // Master/Monitor might be anywhere... we put them at the end - Sorry ;)
4122         if (types[5]) {
4123                 sorted.push_back (session->master_out());
4124         }
4125         if (types[6]) {
4126                 sorted.push_back (session->monitor_out());
4127         }
4128         return sorted;
4129 }
4130
4131 int
4132 OSC::cue_parse (const char *path, const char* types, lo_arg **argv, int argc, lo_message msg)
4133 {
4134         int ret = 1; /* unhandled */
4135
4136         if (!strncmp (path, "/cue/aux", 8)) {
4137                 // set our Aux bus
4138                 ret = cue_set (argv[0]->i, msg);
4139         }
4140         else if (!strncmp (path, "/cue/connect", 12)) {
4141                 // Connect to default Aux bus
4142                 if ((!argc) || argv[0]->i) {
4143                         ret = cue_set (1, msg);
4144                 }
4145         }
4146         else if (!strncmp (path, "/cue/next_aux", 13)) {
4147                 // switch to next Aux bus
4148                 if ((!argc) || argv[0]->i) {
4149                         ret = cue_next (msg);
4150                 }
4151         }
4152         else if (!strncmp (path, "/cue/previous_aux", 17)) {
4153                 // switch to previous Aux bus
4154                 if ((!argc) || argv[0]->i) {
4155                         ret = cue_previous (msg);
4156                 }
4157         }
4158         else if (!strncmp (path, "/cue/send/fader/", 16) && strlen (path) > 16) {
4159                 int id = atoi (&path[16]);
4160                 ret = cue_send_fader (id, argv[0]->f, msg);
4161         }
4162         else if (!strncmp (path, "/cue/send/enable/", 17) && strlen (path) > 17) {
4163                 int id = atoi (&path[17]);
4164                 ret = cue_send_enable (id, argv[0]->f, msg);
4165         }
4166         else if (!strncmp (path, "/cue/fader", 10)) {
4167                 ret = cue_aux_fader (argv[0]->f, msg);
4168         }
4169         else if (!strncmp (path, "/cue/mute", 9)) {
4170                 ret = cue_aux_mute (argv[0]->f, msg);
4171         }
4172
4173         return ret;
4174 }
4175
4176 int
4177 OSC::cue_set (uint32_t aux, lo_message msg)
4178 {
4179         return _cue_set (aux, get_address (msg));
4180 }
4181
4182 int
4183 OSC::_cue_set (uint32_t aux, lo_address addr)
4184 {
4185         int ret = 1;
4186         OSCSurface *s = get_surface(addr);
4187         s->bank_size = 0;
4188         s->strip_types = 128;
4189         s->feedback = 0;
4190         s->gainmode = 1;
4191         s->cue = true;
4192         s->strips = get_sorted_stripables(s->strip_types, s->cue);
4193
4194         s->nstrips = s->strips.size();
4195
4196         if (aux < 1) {
4197                 aux = 1;
4198         } else if (aux > s->nstrips) {
4199                 aux = s->nstrips;
4200         }
4201         s->aux = aux;
4202
4203         // get rid of any old CueObsevers for this address
4204         //cueobserver_connections.drop_connections ();
4205         CueObservers::iterator x;
4206         for (x = cue_observers.begin(); x != cue_observers.end();) {
4207
4208                 OSCCueObserver* co;
4209
4210                 if ((co = dynamic_cast<OSCCueObserver*>(*x)) != 0) {
4211
4212                         int res = strcmp(lo_address_get_url(co->address()), lo_address_get_url(addr));
4213
4214                         if (res == 0) {
4215                                 delete *x;
4216                                 x = cue_observers.erase (x);
4217                         } else {
4218                                 ++x;
4219                         }
4220                 } else {
4221                         ++x;
4222                 }
4223         }
4224
4225         // get a list of Auxes
4226         for (uint32_t n = 0; n < s->nstrips; ++n) {
4227                 boost::shared_ptr<Stripable> stp = s->strips[n];
4228                 if (stp) {
4229                         text_message (string_compose ("/cue/name/%1", n+1), stp->name(), addr);
4230                         if (aux == n+1) {
4231                                 // aux must be at least one
4232                                 // need a signal if aux vanishes
4233                                 stp->DropReferences.connect (*this, MISSING_INVALIDATOR, boost::bind (&OSC::_cue_set, this, aux, addr), this);
4234
4235                                 // make a list of stripables with sends that go to this bus
4236                                 s->sends = cue_get_sorted_stripables(stp, aux, addr);
4237                                 // start cue observer
4238                                 OSCCueObserver* co = new OSCCueObserver (stp, s->sends, addr);
4239                                 cue_observers.push_back (co);
4240                                 ret = 0;
4241                         }
4242
4243                 }
4244         }
4245
4246         return ret;
4247 }
4248
4249 int
4250 OSC::cue_next (lo_message msg)
4251 {
4252         OSCSurface *s = get_surface(get_address (msg));
4253         int ret = 1;
4254
4255         if (!s->cue) {
4256                 ret = cue_set (1, msg);
4257         }
4258         if (s->aux < s->nstrips) {
4259                 ret = cue_set (s->aux + 1, msg);
4260         } else {
4261                 ret = cue_set (s->nstrips, msg);
4262         }
4263         return ret;
4264 }
4265
4266 int
4267 OSC::cue_previous (lo_message msg)
4268 {
4269         OSCSurface *s = get_surface(get_address (msg));
4270         int ret = 1;
4271         if (!s->cue) {
4272                 ret = cue_set (1, msg);
4273         }
4274         if (s->aux > 1) {
4275                 ret = cue_set (s->aux - 1, msg);
4276         }
4277         return ret;
4278 }
4279
4280 boost::shared_ptr<Send>
4281 OSC::cue_get_send (uint32_t id, lo_address addr)
4282 {
4283         OSCSurface *s = get_surface(addr);
4284         if (id && s->aux > 0 && id <= s->sends.size()) {
4285                 boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s->sends[id - 1]);
4286                 boost::shared_ptr<Stripable> aux = get_strip (s->aux, addr);
4287                 if (r && aux) {
4288                         return r->internal_send_for (boost::dynamic_pointer_cast<Route> (aux));
4289                 }
4290         }
4291         return boost::shared_ptr<Send>();
4292
4293 }
4294
4295 int
4296 OSC::cue_aux_fader (float position, lo_message msg)
4297 {
4298         if (!session) return -1;
4299
4300         OSCSurface *sur = get_surface(get_address (msg));
4301         if (sur->cue) {
4302                 if (sur->aux) {
4303                         boost::shared_ptr<Stripable> s = get_strip (sur->aux, get_address (msg));
4304
4305                         if (s) {
4306                                 float abs;
4307                                 abs = slider_position_to_gain_with_max (position, 2.0);
4308                                 if (s->gain_control()) {
4309                                         s->gain_control()->set_value (abs, PBD::Controllable::NoGroup);
4310                                         return 0;
4311                                 }
4312                         }
4313                 }
4314         }
4315         cue_float_message ("/cue/fader", 0, get_address (msg));
4316         return -1;
4317 }
4318
4319 int
4320 OSC::cue_aux_mute (float state, lo_message msg)
4321 {
4322         if (!session) return -1;
4323
4324         OSCSurface *sur = get_surface(get_address (msg));
4325         if (sur->cue) {
4326                 if (sur->aux) {
4327                         boost::shared_ptr<Stripable> s = get_strip (sur->aux, get_address (msg));
4328                         if (s) {
4329                                 if (s->mute_control()) {
4330                                         s->mute_control()->set_value (state ? 1.0 : 0.0, PBD::Controllable::NoGroup);
4331                                         return 0;
4332                                 }
4333                         }
4334                 }
4335         }
4336         cue_float_message ("/cue/mute", 0, get_address (msg));
4337         return -1;
4338 }
4339
4340 int
4341 OSC::cue_send_fader (uint32_t id, float val, lo_message msg)
4342 {
4343         if (!session) {
4344                 return -1;
4345         }
4346         boost::shared_ptr<Send> s = cue_get_send (id, get_address (msg));
4347         float abs;
4348         if (s) {
4349                 if (s->gain_control()) {
4350                         abs = slider_position_to_gain_with_max (val, 2.0);
4351                         s->gain_control()->set_value (abs, PBD::Controllable::NoGroup);
4352                         return 0;
4353                 }
4354         }
4355         cue_float_message (string_compose ("/cue/send/fader/%1", id), 0, get_address (msg));
4356         return -1;
4357 }
4358
4359 int
4360 OSC::cue_send_enable (uint32_t id, float state, lo_message msg)
4361 {
4362         if (!session)
4363                 return -1;
4364         boost::shared_ptr<Send> s = cue_get_send (id, get_address (msg));
4365         if (s) {
4366                 if (state) {
4367                         s->activate ();
4368                 } else {
4369                         s->deactivate ();
4370                 }
4371                 return 0;
4372         }
4373         cue_float_message (string_compose ("/cue/send/enable/%1", id), 0, get_address (msg));
4374         return -1;
4375 }
4376
4377 int
4378 OSC::cue_float_message (string path, float val, lo_address addr)
4379 {
4380
4381         lo_message reply;
4382         reply = lo_message_new ();
4383         lo_message_add_float (reply, (float) val);
4384
4385         lo_send_message (addr, path.c_str(), reply);
4386         lo_message_free (reply);
4387
4388         return 0;
4389 }
4390
4391 int
4392 OSC::text_message (string path, string val, lo_address addr)
4393 {
4394
4395         lo_message reply;
4396         reply = lo_message_new ();
4397         lo_message_add_string (reply, val.c_str());
4398
4399         lo_send_message (addr, path.c_str(), reply);
4400         lo_message_free (reply);
4401
4402         return 0;
4403 }
4404
4405
4406 // we have to have a sorted list of stripables that have sends pointed at our aux
4407 // we can use the one in osc.cc to get an aux list
4408 OSC::Sorted
4409 OSC::cue_get_sorted_stripables(boost::shared_ptr<Stripable> aux, uint32_t id, lo_message msg)
4410 {
4411         Sorted sorted;
4412         cueobserver_connections.drop_connections ();
4413         // fetch all stripables
4414         StripableList stripables;
4415
4416         session->get_stripables (stripables);
4417
4418         // Look for stripables that have a send to aux
4419         for (StripableList::iterator it = stripables.begin(); it != stripables.end(); ++it) {
4420
4421                 boost::shared_ptr<Stripable> s = *it;
4422                 // we only want routes
4423                 boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
4424                 if (r) {
4425                         r->processors_changed.connect  (*this, MISSING_INVALIDATOR, boost::bind (&OSC::recalcbanks, this), this);
4426                         boost::shared_ptr<Send> snd = r->internal_send_for (boost::dynamic_pointer_cast<Route> (aux));
4427                         if (snd) { // test for send to aux
4428                                 sorted.push_back (s);
4429                                 s->DropReferences.connect (*this, MISSING_INVALIDATOR, boost::bind (&OSC::cue_set, this, id, msg), this);
4430                         }
4431                 }
4432
4433
4434         }
4435         sort (sorted.begin(), sorted.end(), StripableByPresentationOrder());
4436
4437         return sorted;
4438 }
4439