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