c480512b8837ee1e956cd96ee41f78ef26bee4fb
[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                                         ret = 0;
2347                                 }
2348                         } else {
2349                                 // end touch
2350                                 control->stop_touch (true, control->session().transport_frame());
2351                                 ret = 0;
2352                         }
2353                         // just in case some crazy surface starts sending control values before touch
2354                         FakeTouchMap::iterator x = _touch_timeout.find(control);
2355                         if (x != _touch_timeout.end()) {
2356                                 _touch_timeout.erase (x);
2357                         }
2358                 }
2359         }
2360
2361         return ret;
2362 }
2363
2364 int
2365 OSC::fake_touch (boost::shared_ptr<ARDOUR::AutomationControl> ctrl)
2366 {
2367         if (ctrl) {
2368                 //start touch
2369                 if (ctrl->automation_state() == Touch && !ctrl->touching ()) {
2370                 ctrl->start_touch (ctrl->session().transport_frame());
2371                 _touch_timeout[ctrl] = 10;
2372                 }
2373         }
2374
2375         return 0;
2376 }
2377
2378 int
2379 OSC::route_mute (int ssid, int yn, lo_message msg)
2380 {
2381         if (!session) return -1;
2382         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2383
2384         if (s) {
2385                 if (s->mute_control()) {
2386                         s->mute_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2387                         return 0;
2388                 }
2389         }
2390
2391         return route_send_fail ("mute", ssid, 0, get_address (msg));
2392 }
2393
2394 int
2395 OSC::sel_mute (uint32_t yn, lo_message msg)
2396 {
2397         OSCSurface *sur = get_surface(get_address (msg));
2398         boost::shared_ptr<Stripable> s;
2399         if (sur->expand_enable) {
2400                 s = get_strip (sur->expand, get_address (msg));
2401         } else {
2402                 s = _select;
2403         }
2404         if (s) {
2405                 if (s->mute_control()) {
2406                         s->mute_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2407                         return 0;
2408                 }
2409         }
2410         return sel_fail ("mute", 0, get_address (msg));
2411 }
2412
2413 int
2414 OSC::route_solo (int ssid, int yn, lo_message msg)
2415 {
2416         if (!session) return -1;
2417         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2418
2419         if (s) {
2420                 if (s->solo_control()) {
2421                         s->solo_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2422                 }
2423         }
2424
2425         return route_send_fail ("solo", ssid, 0, get_address (msg));
2426 }
2427
2428 int
2429 OSC::route_solo_iso (int ssid, int yn, lo_message msg)
2430 {
2431         if (!session) return -1;
2432         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2433
2434         if (s) {
2435                 if (s->solo_isolate_control()) {
2436                         s->solo_isolate_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2437                         return 0;
2438                 }
2439         }
2440
2441         return route_send_fail ("solo_iso", ssid, 0, get_address (msg));
2442 }
2443
2444 int
2445 OSC::route_solo_safe (int ssid, int yn, lo_message msg)
2446 {
2447         if (!session) return -1;
2448         boost::shared_ptr<Stripable> s = get_strip (ssid, lo_message_get_source (msg));
2449
2450         if (s) {
2451                 if (s->solo_safe_control()) {
2452                         s->solo_safe_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2453                         return 0;
2454                 }
2455         }
2456
2457         return route_send_fail ("solo_safe", ssid, 0, get_address (msg));
2458 }
2459
2460 int
2461 OSC::sel_solo (uint32_t yn, lo_message msg)
2462 {
2463         OSCSurface *sur = get_surface(get_address (msg));
2464         boost::shared_ptr<Stripable> s;
2465         if (sur->expand_enable) {
2466                 s = get_strip (sur->expand, get_address (msg));
2467         } else {
2468                 s = _select;
2469         }
2470         if (s) {
2471                 if (s->solo_control()) {
2472                         session->set_control (s->solo_control(), yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2473                 }
2474         }
2475         return sel_fail ("solo", 0, get_address (msg));
2476 }
2477
2478 int
2479 OSC::sel_solo_iso (uint32_t yn, lo_message msg)
2480 {
2481         OSCSurface *sur = get_surface(get_address (msg));
2482         boost::shared_ptr<Stripable> s;
2483         if (sur->expand_enable) {
2484                 s = get_strip (sur->expand, get_address (msg));
2485         } else {
2486                 s = _select;
2487         }
2488         if (s) {
2489                 if (s->solo_isolate_control()) {
2490                         s->solo_isolate_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2491                         return 0;
2492                 }
2493         }
2494         return sel_fail ("solo_iso", 0, get_address (msg));
2495 }
2496
2497 int
2498 OSC::sel_solo_safe (uint32_t yn, lo_message msg)
2499 {
2500         OSCSurface *sur = get_surface(get_address (msg));
2501         boost::shared_ptr<Stripable> s;
2502         if (sur->expand_enable) {
2503                 s = get_strip (sur->expand, get_address (msg));
2504         } else {
2505                 s = _select;
2506         }
2507         if (s) {
2508                 if (s->solo_safe_control()) {
2509                         s->solo_safe_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2510                         return 0;
2511                 }
2512         }
2513         return sel_fail ("solo_safe", 0, get_address (msg));
2514 }
2515
2516 int
2517 OSC::sel_recenable (uint32_t yn, lo_message msg)
2518 {
2519         OSCSurface *sur = get_surface(get_address (msg));
2520         boost::shared_ptr<Stripable> s;
2521         if (sur->expand_enable) {
2522                 s = get_strip (sur->expand, get_address (msg));
2523         } else {
2524                 s = _select;
2525         }
2526         if (s) {
2527                 if (s->rec_enable_control()) {
2528                         s->rec_enable_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2529                         if (s->rec_enable_control()->get_value()) {
2530                                 return 0;
2531                         }
2532                 }
2533         }
2534         return sel_fail ("recenable", 0, get_address (msg));
2535 }
2536
2537 int
2538 OSC::route_recenable (int ssid, int yn, lo_message msg)
2539 {
2540         if (!session) return -1;
2541         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2542
2543         if (s) {
2544                 if (s->rec_enable_control()) {
2545                         s->rec_enable_control()->set_value (yn, PBD::Controllable::UseGroup);
2546                         if (s->rec_enable_control()->get_value()) {
2547                                 return 0;
2548                         }
2549                 }
2550         }
2551         return route_send_fail ("recenable", ssid, 0, get_address (msg));
2552 }
2553
2554 int
2555 OSC::route_rename(int ssid, char *newname, lo_message msg) {
2556     if (!session) {
2557         return -1;
2558     }
2559
2560     boost::shared_ptr<Stripable> s = get_strip(ssid, get_address(msg));
2561
2562     if (s) {
2563         s->set_name(std::string(newname));
2564     }
2565
2566     return 0;
2567 }
2568
2569 int
2570 OSC::sel_recsafe (uint32_t yn, lo_message msg)
2571 {
2572         OSCSurface *sur = get_surface(get_address (msg));
2573         boost::shared_ptr<Stripable> s;
2574         if (sur->expand_enable) {
2575                 s = get_strip (sur->expand, get_address (msg));
2576         } else {
2577                 s = _select;
2578         }
2579         if (s) {
2580                 if (s->rec_safe_control()) {
2581                         s->rec_safe_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2582                         if (s->rec_safe_control()->get_value()) {
2583                                 return 0;
2584                         }
2585                 }
2586         }
2587         return sel_fail ("record_safe", 0, get_address (msg));
2588 }
2589
2590 int
2591 OSC::route_recsafe (int ssid, int yn, lo_message msg)
2592 {
2593         if (!session) return -1;
2594         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2595         if (s) {
2596                 if (s->rec_safe_control()) {
2597                         s->rec_safe_control()->set_value (yn, PBD::Controllable::UseGroup);
2598                         if (s->rec_safe_control()->get_value()) {
2599                                 return 0;
2600                         }
2601                 }
2602         }
2603         return route_send_fail ("record_safe", ssid, 0,get_address (msg));
2604 }
2605
2606 int
2607 OSC::route_monitor_input (int ssid, int yn, lo_message msg)
2608 {
2609         if (!session) return -1;
2610         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2611
2612         if (s) {
2613                 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (s);
2614                 if (track) {
2615                         if (track->monitoring_control()) {
2616                                 track->monitoring_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2617                                 return 0;
2618                         }
2619                 }
2620         }
2621
2622         return route_send_fail ("monitor_input", ssid, 0, get_address (msg));
2623 }
2624
2625 int
2626 OSC::sel_monitor_input (uint32_t yn, lo_message msg)
2627 {
2628         OSCSurface *sur = get_surface(get_address (msg));
2629         boost::shared_ptr<Stripable> s;
2630         if (sur->expand_enable) {
2631                 s = get_strip (sur->expand, get_address (msg));
2632         } else {
2633                 s = _select;
2634         }
2635         if (s) {
2636                 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (s);
2637                 if (track) {
2638                         if (track->monitoring_control()) {
2639                                 track->monitoring_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2640                                 return 0;
2641                         }
2642                 }
2643         }
2644         return sel_fail ("monitor_input", 0, get_address (msg));
2645 }
2646
2647 int
2648 OSC::route_monitor_disk (int ssid, int yn, lo_message msg)
2649 {
2650         if (!session) return -1;
2651         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2652
2653         if (s) {
2654                 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (s);
2655                 if (track) {
2656                         if (track->monitoring_control()) {
2657                                 track->monitoring_control()->set_value (yn ? 2.0 : 0.0, PBD::Controllable::NoGroup);
2658                                 return 0;
2659                         }
2660                 }
2661         }
2662
2663         return route_send_fail ("monitor_disk", ssid, 0, get_address (msg));
2664 }
2665
2666 int
2667 OSC::sel_monitor_disk (uint32_t yn, lo_message msg)
2668 {
2669         OSCSurface *sur = get_surface(get_address (msg));
2670         boost::shared_ptr<Stripable> s;
2671         if (sur->expand_enable) {
2672                 s = get_strip (sur->expand, get_address (msg));
2673         } else {
2674                 s = _select;
2675         }
2676         if (s) {
2677                 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (s);
2678                 if (track) {
2679                         if (track->monitoring_control()) {
2680                                 track->monitoring_control()->set_value (yn ? 2.0 : 0.0, PBD::Controllable::NoGroup);
2681                                 return 0;
2682                         }
2683                 }
2684         }
2685         return sel_fail ("monitor_disk", 0, get_address (msg));
2686 }
2687
2688
2689 int
2690 OSC::strip_phase (int ssid, int yn, lo_message msg)
2691 {
2692         if (!session) return -1;
2693         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2694
2695         if (s) {
2696                 if (s->phase_control()) {
2697                         s->phase_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2698                         return 0;
2699                 }
2700         }
2701
2702         return route_send_fail ("polarity", ssid, 0, get_address (msg));
2703 }
2704
2705 int
2706 OSC::sel_phase (uint32_t yn, lo_message msg)
2707 {
2708         OSCSurface *sur = get_surface(get_address (msg));
2709         boost::shared_ptr<Stripable> s;
2710         if (sur->expand_enable) {
2711                 s = get_strip (sur->expand, get_address (msg));
2712         } else {
2713                 s = _select;
2714         }
2715         if (s) {
2716                 if (s->phase_control()) {
2717                         s->phase_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2718                         return 0;
2719                 }
2720         }
2721         return sel_fail ("polarity", 0, get_address (msg));
2722 }
2723
2724 int
2725 OSC::strip_expand (int ssid, int yn, lo_message msg)
2726 {
2727         OSCSurface *sur = get_surface(get_address (msg));
2728         sur->expand_enable = (bool) yn;
2729         sur->expand = ssid;
2730         boost::shared_ptr<Stripable> s;
2731         if (yn) {
2732                 s = get_strip (ssid, get_address (msg));
2733         } else {
2734                 s = ControlProtocol::first_selected_stripable();
2735         }
2736
2737         return _strip_select (s, get_address (msg));
2738 }
2739
2740 int
2741 OSC::_strip_select (boost::shared_ptr<Stripable> s, lo_address addr)
2742 {
2743         if (!session) {
2744                 return -1;
2745         }
2746         OSCSurface *sur = get_surface(addr);
2747         if (sur->sel_obs) {
2748                 delete sur->sel_obs;
2749                 sur->sel_obs = 0;
2750         }
2751         bool feedback_on = sur->feedback.to_ulong();
2752         if (s && feedback_on) {
2753                 OSCSelectObserver* sel_fb = new OSCSelectObserver (s, addr, sur->gainmode, sur->feedback);
2754                 s->DropReferences.connect (*this, MISSING_INVALIDATOR, boost::bind (&OSC::recalcbanks, this), this);
2755                 sur->sel_obs = sel_fb;
2756         } else if (sur->expand_enable) {
2757                 sur->expand = 0;
2758                 sur->expand_enable = false;
2759                 if (_select && feedback_on) {
2760                         OSCSelectObserver* sel_fb = new OSCSelectObserver (_select, addr, sur->gainmode, sur->feedback);
2761                         _select->DropReferences.connect (*this, MISSING_INVALIDATOR, boost::bind (&OSC::recalcbanks, this), this);
2762                         sur->sel_obs = sel_fb;
2763                 }
2764         } else if (feedback_on) {
2765                 route_send_fail ("select", sur->expand, 0 , addr);
2766         }
2767         if (!feedback_on) {
2768                 return 0;
2769         }
2770         //update buttons on surface
2771         int b_s = sur->bank_size;
2772         if (!b_s) { // bank size 0 means we need to know how many strips there are.
2773                 b_s = sur->nstrips;
2774         }
2775         for (int i = 1;  i <= b_s; i++) {
2776                 string path = "expand";
2777
2778                 if ((i == (int) sur->expand) && sur->expand_enable) {
2779                         lo_message reply = lo_message_new ();
2780                         if (sur->feedback[2]) {
2781                                 ostringstream os;
2782                                 os << "/strip/" << path << "/" << i;
2783                                 path = os.str();
2784                         } else {
2785                                 ostringstream os;
2786                                 os << "/strip/" << path;
2787                                 path = os.str();
2788                                 lo_message_add_int32 (reply, i);
2789                         }
2790                         lo_message_add_float (reply, (float) 1);
2791
2792                         lo_send_message (addr, path.c_str(), reply);
2793                         lo_message_free (reply);
2794                         reply = lo_message_new ();
2795                         lo_message_add_float (reply, 1.0);
2796                         lo_send_message (addr, "/select/expand", reply);
2797                         lo_message_free (reply);
2798
2799                 } else {
2800                         lo_message reply = lo_message_new ();
2801                         lo_message_add_int32 (reply, i);
2802                         lo_message_add_float (reply, 0.0);
2803                         lo_send_message (addr, "/strip/expand", reply);
2804                         lo_message_free (reply);
2805                 }
2806         }
2807         if (!sur->expand_enable) {
2808                 lo_message reply = lo_message_new ();
2809                 lo_message_add_float (reply, 0.0);
2810                 lo_send_message (addr, "/select/expand", reply);
2811                 lo_message_free (reply);
2812         }
2813
2814         return 0;
2815 }
2816
2817 int
2818 OSC::strip_gui_select (int ssid, int yn, lo_message msg)
2819 {
2820         //ignore button release
2821         if (!yn) return 0;
2822
2823         if (!session) {
2824                 return -1;
2825         }
2826         OSCSurface *sur = get_surface(get_address (msg));
2827         sur->expand_enable = false;
2828         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2829         if (s) {
2830                 SetStripableSelection (s);
2831         } else {
2832                 if ((int) (sur->feedback.to_ulong())) {
2833                         route_send_fail ("select", ssid, 0, get_address (msg));
2834                 }
2835         }
2836
2837         return 0;
2838 }
2839
2840 int
2841 OSC::sel_expand (uint32_t state, lo_message msg)
2842 {
2843         OSCSurface *sur = get_surface(get_address (msg));
2844         boost::shared_ptr<Stripable> s;
2845         sur->expand_enable = (bool) state;
2846         if (state && sur->expand) {
2847                 s = get_strip (sur->expand, get_address (msg));
2848         } else {
2849                 s = ControlProtocol::first_selected_stripable();
2850         }
2851
2852         return _strip_select (s, get_address (msg));
2853 }
2854
2855 int
2856 OSC::route_set_gain_abs (int ssid, float level, lo_message msg)
2857 {
2858         if (!session) return -1;
2859         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2860
2861         if (s) {
2862                 if (s->gain_control()) {
2863                         fake_touch (s->gain_control());
2864                         s->gain_control()->set_value (level, PBD::Controllable::NoGroup);
2865                 } else {
2866                         return 1;
2867                 }
2868         } else {
2869                 return 1;
2870         }
2871
2872         return 0;
2873 }
2874
2875 int
2876 OSC::route_set_gain_dB (int ssid, float dB, lo_message msg)
2877 {
2878         if (!session) {
2879                 route_send_fail ("gain", ssid, -193, get_address (msg));
2880                 return -1;
2881         }
2882         int ret;
2883         if (dB < -192) {
2884                 ret = route_set_gain_abs (ssid, 0.0, msg);
2885         } else {
2886                 ret = route_set_gain_abs (ssid, dB_to_coefficient (dB), msg);
2887         }
2888         if (ret != 0) {
2889                 return route_send_fail ("gain", ssid, -193, get_address (msg));
2890         }
2891         return 0;
2892 }
2893
2894 int
2895 OSC::sel_gain (float val, lo_message msg)
2896 {
2897         OSCSurface *sur = get_surface(get_address (msg));
2898         boost::shared_ptr<Stripable> s;
2899         if (sur->expand_enable) {
2900                 s = get_strip (sur->expand, get_address (msg));
2901         } else {
2902                 s = _select;
2903         }
2904         if (s) {
2905                 float abs;
2906                 if (val < -192) {
2907                         abs = 0;
2908                 } else {
2909                         abs = dB_to_coefficient (val);
2910                 }
2911                 if (s->gain_control()) {
2912                         fake_touch (s->gain_control());
2913                         s->gain_control()->set_value (abs, PBD::Controllable::NoGroup);
2914                         return 0;
2915                 }
2916         }
2917         return sel_fail ("gain", -193, get_address (msg));
2918 }
2919
2920 int
2921 OSC::route_set_gain_fader (int ssid, float pos, lo_message msg)
2922 {
2923         if (!session) {
2924                 route_send_fail ("fader", ssid, 0, get_address (msg));
2925                 return -1;
2926         }
2927         int ret;
2928         ret = route_set_gain_abs (ssid, slider_position_to_gain_with_max (pos, 2.0), msg);
2929         if (ret != 0) {
2930                 return route_send_fail ("fader", ssid, 0, get_address (msg));
2931         }
2932         return 0;
2933 }
2934
2935 int
2936 OSC::sel_fader (float val, lo_message msg)
2937 {
2938         OSCSurface *sur = get_surface(get_address (msg));
2939         boost::shared_ptr<Stripable> s;
2940         if (sur->expand_enable) {
2941                 s = get_strip (sur->expand, get_address (msg));
2942         } else {
2943                 s = _select;
2944         }
2945         if (s) {
2946                 float abs;
2947                 abs = slider_position_to_gain_with_max (val, 2.0);
2948                 if (s->gain_control()) {
2949                         fake_touch (s->gain_control());
2950                         s->gain_control()->set_value (abs, PBD::Controllable::NoGroup);
2951                         return 0;
2952                 }
2953         }
2954         return sel_fail ("fader", 0, get_address (msg));
2955 }
2956
2957 int
2958 OSC::route_set_trim_abs (int ssid, float level, lo_message msg)
2959 {
2960         if (!session) return -1;
2961         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2962
2963         if (s) {
2964                 if (s->trim_control()) {
2965                         s->trim_control()->set_value (level, PBD::Controllable::NoGroup);
2966                         return 0;
2967                 }
2968
2969         }
2970
2971         return -1;
2972 }
2973
2974 int
2975 OSC::route_set_trim_dB (int ssid, float dB, lo_message msg)
2976 {
2977         int ret;
2978         ret = route_set_trim_abs(ssid, dB_to_coefficient (dB), msg);
2979         if (ret != 0) {
2980                 return route_send_fail ("trimdB", ssid, 0, get_address (msg));
2981         }
2982
2983 return 0;
2984 }
2985
2986 int
2987 OSC::sel_trim (float val, lo_message msg)
2988 {
2989         OSCSurface *sur = get_surface(get_address (msg));
2990         boost::shared_ptr<Stripable> s;
2991         if (sur->expand_enable) {
2992                 s = get_strip (sur->expand, get_address (msg));
2993         } else {
2994                 s = _select;
2995         }
2996         if (s) {
2997                 if (s->trim_control()) {
2998                         s->trim_control()->set_value (dB_to_coefficient (val), PBD::Controllable::NoGroup);
2999                         return 0;
3000                 }
3001         }
3002         return sel_fail ("trimdB", 0, get_address (msg));
3003 }
3004
3005 int
3006 OSC::sel_pan_position (float val, lo_message msg)
3007 {
3008         OSCSurface *sur = get_surface(get_address (msg));
3009         boost::shared_ptr<Stripable> s;
3010         if (sur->expand_enable) {
3011                 s = get_strip (sur->expand, get_address (msg));
3012         } else {
3013                 s = _select;
3014         }
3015         if (s) {
3016                 if(s->pan_azimuth_control()) {
3017                         s->pan_azimuth_control()->set_value (s->pan_azimuth_control()->interface_to_internal (val), PBD::Controllable::NoGroup);
3018                         return 0;
3019                 }
3020         }
3021         return sel_fail ("pan_stereo_position", 0.5, get_address (msg));
3022 }
3023
3024 int
3025 OSC::sel_pan_width (float val, lo_message msg)
3026 {
3027         OSCSurface *sur = get_surface(get_address (msg));
3028         boost::shared_ptr<Stripable> s;
3029         if (sur->expand_enable) {
3030                 s = get_strip (sur->expand, get_address (msg));
3031         } else {
3032                 s = _select;
3033         }
3034         if (s) {
3035                 if (s->pan_width_control()) {
3036                         s->pan_width_control()->set_value (s->pan_width_control()->interface_to_internal (val), PBD::Controllable::NoGroup);
3037                         return 0;
3038                 }
3039         }
3040         return sel_fail ("pan_stereo_width", 1, get_address (msg));
3041 }
3042
3043 int
3044 OSC::route_set_pan_stereo_position (int ssid, float pos, lo_message msg)
3045 {
3046         if (!session) return -1;
3047         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3048
3049         if (s) {
3050                 if(s->pan_azimuth_control()) {
3051                         s->pan_azimuth_control()->set_value (s->pan_azimuth_control()->interface_to_internal (pos), PBD::Controllable::NoGroup);
3052                         return 0;
3053                 }
3054         }
3055
3056         return route_send_fail ("pan_stereo_position", ssid, 0.5, get_address (msg));
3057 }
3058
3059 int
3060 OSC::route_set_pan_stereo_width (int ssid, float pos, lo_message msg)
3061 {
3062         if (!session) return -1;
3063         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3064
3065         if (s) {
3066                 if (s->pan_width_control()) {
3067                         s->pan_width_control()->set_value (pos, PBD::Controllable::NoGroup);
3068                         return 0;
3069                 }
3070         }
3071
3072         return route_send_fail ("pan_stereo_width", ssid, 1, get_address (msg));
3073 }
3074
3075 int
3076 OSC::route_set_send_gain_dB (int ssid, int id, float val, lo_message msg)
3077 {
3078         if (!session) {
3079                 return -1;
3080         }
3081         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3082         float abs;
3083         if (s) {
3084                 if (id > 0) {
3085                         --id;
3086                 }
3087 #ifdef MIXBUS
3088                 abs = val;
3089 #else
3090                 if (val < -192) {
3091                         abs = 0;
3092                 } else {
3093                         abs = dB_to_coefficient (val);
3094                 }
3095 #endif
3096                 if (s->send_level_controllable (id)) {
3097                         s->send_level_controllable (id)->set_value (abs, PBD::Controllable::NoGroup);
3098                         return 0;
3099                 }
3100         }
3101         return 0;
3102 }
3103
3104 int
3105 OSC::route_set_send_fader (int ssid, int id, float val, lo_message msg)
3106 {
3107         if (!session) {
3108                 return -1;
3109         }
3110         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3111         float abs;
3112         if (s) {
3113
3114                 if (id > 0) {
3115                         --id;
3116                 }
3117
3118                 if (s->send_level_controllable (id)) {
3119 #ifdef MIXBUS
3120                         abs = s->send_level_controllable(id)->interface_to_internal (val);
3121 #else
3122                         abs = slider_position_to_gain_with_max (val, 2.0);
3123 #endif
3124                         s->send_level_controllable (id)->set_value (abs, PBD::Controllable::NoGroup);
3125                         return 0;
3126                 }
3127         }
3128         return 0;
3129 }
3130
3131 int
3132 OSC::sel_sendgain (int id, float val, lo_message msg)
3133 {
3134         OSCSurface *sur = get_surface(get_address (msg));
3135         boost::shared_ptr<Stripable> s;
3136         if (sur->expand_enable) {
3137                 s = get_strip (sur->expand, get_address (msg));
3138         } else {
3139                 s = _select;
3140         }
3141         float abs;
3142         if (s) {
3143                 if (id > 0) {
3144                         --id;
3145                 }
3146 #ifdef MIXBUS
3147                 abs = val;
3148 #else
3149                 if (val < -192) {
3150                         abs = 0;
3151                 } else {
3152                         abs = dB_to_coefficient (val);
3153                 }
3154 #endif
3155                 if (s->send_level_controllable (id)) {
3156                         s->send_level_controllable (id)->set_value (abs, PBD::Controllable::NoGroup);
3157                         return 0;
3158                 }
3159         }
3160         return sel_send_fail ("send_gain", id + 1, -193, get_address (msg));
3161 }
3162
3163 int
3164 OSC::sel_sendfader (int id, float val, lo_message msg)
3165 {
3166         OSCSurface *sur = get_surface(get_address (msg));
3167         boost::shared_ptr<Stripable> s;
3168         if (sur->expand_enable) {
3169                 s = get_strip (sur->expand, get_address (msg));
3170         } else {
3171                 s = _select;
3172         }
3173         float abs;
3174         if (s) {
3175
3176                 if (id > 0) {
3177                         --id;
3178                 }
3179
3180                 if (s->send_level_controllable (id)) {
3181 #ifdef MIXBUS
3182                         abs = s->send_level_controllable(id)->interface_to_internal (val);
3183 #else
3184                         abs = slider_position_to_gain_with_max (val, 2.0);
3185 #endif
3186                         s->send_level_controllable (id)->set_value (abs, PBD::Controllable::NoGroup);
3187                         return 0;
3188                 }
3189         }
3190         return sel_send_fail ("send_fader", id, 0, get_address (msg));
3191 }
3192
3193 int
3194 OSC::route_set_send_enable (int ssid, int sid, float val, lo_message msg)
3195 {
3196         if (!session) {
3197                 return -1;
3198         }
3199         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3200
3201         if (s) {
3202
3203                 /* revert to zero-based counting */
3204
3205                 if (sid > 0) {
3206                         --sid;
3207                 }
3208
3209                 if (s->send_enable_controllable (sid)) {
3210                         s->send_enable_controllable (sid)->set_value (val, PBD::Controllable::NoGroup);
3211                         return 0;
3212                 }
3213
3214                 if (s->send_level_controllable (sid)) {
3215                         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
3216                         if (!r) {
3217                                 return 0;
3218                         }
3219                         boost::shared_ptr<Send> snd = boost::dynamic_pointer_cast<Send> (r->nth_send(sid));
3220                         if (snd) {
3221                                 if (val) {
3222                                         snd->activate();
3223                                 } else {
3224                                         snd->deactivate();
3225                                 }
3226                         }
3227                         return 0;
3228                 }
3229
3230         }
3231
3232         return -1;
3233 }
3234
3235 int
3236 OSC::sel_sendenable (int id, float val, lo_message msg)
3237 {
3238         OSCSurface *sur = get_surface(get_address (msg));
3239         boost::shared_ptr<Stripable> s;
3240         if (sur->expand_enable) {
3241                 s = get_strip (sur->expand, get_address (msg));
3242         } else {
3243                 s = _select;
3244         }
3245         if (s) {
3246                 if (id > 0) {
3247                         --id;
3248                 }
3249                 if (s->send_enable_controllable (id)) {
3250                         s->send_enable_controllable (id)->set_value (val, PBD::Controllable::NoGroup);
3251                         return 0;
3252                 }
3253                 if (s->send_level_controllable (id)) {
3254                         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
3255                         if (!r) {
3256                                 // should never get here
3257                                 return sel_send_fail ("send_enable", id + 1, 0, get_address (msg));
3258                         }
3259                         boost::shared_ptr<Send> snd = boost::dynamic_pointer_cast<Send> (r->nth_send(id));
3260                         if (snd) {
3261                                 if (val) {
3262                                         snd->activate();
3263                                 } else {
3264                                         snd->deactivate();
3265                                 }
3266                         }
3267                         return 0;
3268                 }
3269         }
3270         return sel_send_fail ("send_enable", id + 1, 0, get_address (msg));
3271 }
3272
3273 int
3274 OSC::route_plugin_list (int ssid, lo_message msg) {
3275         if (!session) {
3276                 return -1;
3277         }
3278
3279         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route>(get_strip (ssid, get_address (msg)));
3280
3281         if (!r) {
3282                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
3283                 return -1;
3284         }
3285         int piid = 0;
3286
3287         lo_message reply = lo_message_new ();
3288         lo_message_add_int32 (reply, ssid);
3289
3290
3291         for (;;) {
3292                 boost::shared_ptr<Processor> redi = r->nth_plugin(piid);
3293                 if ( !redi ) {
3294                         break;
3295                 }
3296
3297                 boost::shared_ptr<PluginInsert> pi;
3298
3299                 if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
3300                         PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
3301                         continue;
3302                 }
3303                 lo_message_add_int32 (reply, piid + 1);
3304
3305                 boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
3306                 lo_message_add_string (reply, pip->name());
3307
3308                 piid++;
3309         }
3310
3311         lo_send_message (get_address (msg), "/strip/plugin/list", reply);
3312         lo_message_free (reply);
3313         return 0;
3314 }
3315
3316 int
3317 OSC::route_plugin_descriptor (int ssid, int piid, lo_message msg) {
3318         if (!session) {
3319                 return -1;
3320         }
3321
3322         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route>(get_strip (ssid, get_address (msg)));
3323
3324         if (!r) {
3325                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
3326                 return -1;
3327         }
3328
3329         boost::shared_ptr<Processor> redi = r->nth_plugin(piid - 1);
3330
3331         if (!redi) {
3332                 PBD::error << "OSC: cannot find plugin # " << piid << " for RID '" << ssid << "'" << endmsg;
3333                 return -1;
3334         }
3335
3336         boost::shared_ptr<PluginInsert> pi;
3337
3338         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
3339                 PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
3340                 return -1;
3341         }
3342
3343         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
3344         bool ok = false;
3345
3346         lo_message reply = lo_message_new();
3347         lo_message_add_int32 (reply, ssid);
3348         lo_message_add_int32 (reply, piid);
3349         lo_message_add_string (reply, pip->name());
3350         for ( uint32_t ppi = 0; ppi < pip->parameter_count(); ppi++) {
3351
3352                 uint32_t controlid = pip->nth_parameter(ppi, ok);
3353                 if (!ok) {
3354                         continue;
3355                 }
3356                 if ( pip->parameter_is_input(controlid) || pip->parameter_is_control(controlid) ) {
3357                         boost::shared_ptr<AutomationControl> c = pi->automation_control(Evoral::Parameter(PluginAutomation, 0, controlid));
3358
3359                                 lo_message_add_int32 (reply, ppi + 1);
3360                                 ParameterDescriptor pd;
3361                                 pi->plugin()->get_parameter_descriptor(controlid, pd);
3362                                 lo_message_add_string (reply, pd.label.c_str());
3363
3364                                 // I've combined those binary descriptor parts in a bit-field to reduce lilo message elements
3365                                 int flags = 0;
3366                                 flags |= pd.enumeration ? 1 : 0;
3367                                 flags |= pd.integer_step ? 2 : 0;
3368                                 flags |= pd.logarithmic ? 4 : 0;
3369                                 flags |= pd.max_unbound ? 8 : 0;
3370                                 flags |= pd.min_unbound ? 16 : 0;
3371                                 flags |= pd.sr_dependent ? 32 : 0;
3372                                 flags |= pd.toggled ? 64 : 0;
3373                                 flags |= c != NULL ? 128 : 0; // bit 7 indicates in input control
3374                                 lo_message_add_int32 (reply, flags);
3375
3376                                 lo_message_add_int32 (reply, pd.datatype);
3377                                 lo_message_add_float (reply, pd.lower);
3378                                 lo_message_add_float (reply, pd.upper);
3379                                 lo_message_add_string (reply, pd.print_fmt.c_str());
3380                                 if ( pd.scale_points ) {
3381                                         lo_message_add_int32 (reply, pd.scale_points->size());
3382                                         for ( ARDOUR::ScalePoints::const_iterator i = pd.scale_points->begin(); i != pd.scale_points->end(); ++i) {
3383                                                 lo_message_add_int32 (reply, i->second);
3384                                                 lo_message_add_string (reply, ((std::string)i->first).c_str());
3385                                         }
3386                                 }
3387                                 else {
3388                                         lo_message_add_int32 (reply, 0);
3389                                 }
3390                                 if ( c ) {
3391                                         lo_message_add_double (reply, c->get_value());
3392                                 }
3393                                 else {
3394                                         lo_message_add_double (reply, 0);
3395                         }
3396                 }
3397         }
3398
3399         lo_send_message (get_address (msg), "/strip/plugin/descriptor", reply);
3400         lo_message_free (reply);
3401
3402         return 0;
3403 }
3404
3405 int
3406 OSC::route_plugin_reset (int ssid, int piid, lo_message msg) {
3407         if (!session) {
3408                 return -1;
3409         }
3410
3411         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route>(get_strip (ssid, get_address (msg)));
3412
3413         if (!r) {
3414                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
3415                 return -1;
3416         }
3417
3418         boost::shared_ptr<Processor> redi = r->nth_plugin(piid - 1);
3419
3420         if (!redi) {
3421                 PBD::error << "OSC: cannot find plugin # " << piid << " for RID '" << ssid << "'" << endmsg;
3422                 return -1;
3423         }
3424
3425         boost::shared_ptr<PluginInsert> pi;
3426
3427         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
3428                 PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
3429                 return -1;
3430         }
3431
3432         pi->reset_parameters_to_default ();
3433
3434         return 0;
3435 }
3436
3437 int
3438 OSC::route_plugin_parameter (int ssid, int piid, int par, float val, lo_message msg)
3439 {
3440         if (!session)
3441                 return -1;
3442         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3443
3444         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
3445
3446         if (!r) {
3447                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
3448                 return -1;
3449         }
3450
3451         boost::shared_ptr<Processor> redi=r->nth_plugin (piid - 1);
3452
3453         if (!redi) {
3454                 PBD::error << "OSC: cannot find plugin # " << piid << " for RID '" << ssid << "'" << endmsg;
3455                 return -1;
3456         }
3457
3458         boost::shared_ptr<PluginInsert> pi;
3459
3460         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
3461                 PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
3462                 return -1;
3463         }
3464
3465         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
3466         bool ok=false;
3467
3468         uint32_t controlid = pip->nth_parameter (par - 1,ok);
3469
3470         if (!ok) {
3471                 PBD::error << "OSC: Cannot find parameter # " << par <<  " for plugin # " << piid << " on RID '" << ssid << "'" << endmsg;
3472                 return -1;
3473         }
3474
3475         if (!pip->parameter_is_input(controlid)) {
3476                 PBD::error << "OSC: Parameter # " << par <<  " for plugin # " << piid << " on RID '" << ssid << "' is not a control input" << endmsg;
3477                 return -1;
3478         }
3479
3480         ParameterDescriptor pd;
3481         pi->plugin()->get_parameter_descriptor (controlid,pd);
3482
3483         if (val >= pd.lower && val <= pd.upper) {
3484
3485                 boost::shared_ptr<AutomationControl> c = pi->automation_control (Evoral::Parameter(PluginAutomation, 0, controlid));
3486                 // cerr << "parameter:" << redi->describe_parameter(controlid) << " val:" << val << "\n";
3487                 c->set_value (val, PBD::Controllable::NoGroup);
3488         } else {
3489                 PBD::warning << "OSC: Parameter # " << par <<  " for plugin # " << piid << " on RID '" << ssid << "' is out of range" << endmsg;
3490                 PBD::info << "OSC: Valid range min=" << pd.lower << " max=" << pd.upper << endmsg;
3491         }
3492
3493         return 0;
3494 }
3495
3496 //prints to cerr only
3497 int
3498 OSC::route_plugin_parameter_print (int ssid, int piid, int par, lo_message msg)
3499 {
3500         if (!session) {
3501                 return -1;
3502         }
3503         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3504
3505         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
3506
3507         if (!r) {
3508                 return -1;
3509         }
3510
3511         boost::shared_ptr<Processor> redi=r->nth_plugin (piid - 1);
3512
3513         if (!redi) {
3514                 return -1;
3515         }
3516
3517         boost::shared_ptr<PluginInsert> pi;
3518
3519         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
3520                 return -1;
3521         }
3522
3523         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
3524         bool ok=false;
3525
3526         uint32_t controlid = pip->nth_parameter (par - 1,ok);
3527
3528         if (!ok) {
3529                 return -1;
3530         }
3531
3532         ParameterDescriptor pd;
3533
3534         if (pi->plugin()->get_parameter_descriptor (controlid, pd) == 0) {
3535                 boost::shared_ptr<AutomationControl> c = pi->automation_control (Evoral::Parameter(PluginAutomation, 0, controlid));
3536
3537                 cerr << "parameter:     " << pd.label  << "\n";
3538                 if (c) {
3539                         cerr << "current value: " << c->get_value () << "\n";
3540                 } else {
3541                         cerr << "current value not available, control does not exist\n";
3542                 }
3543                 cerr << "lower value:   " << pd.lower << "\n";
3544                 cerr << "upper value:   " << pd.upper << "\n";
3545         }
3546
3547         return 0;
3548 }
3549
3550 int
3551 OSC::route_plugin_activate (int ssid, int piid, lo_message msg)
3552 {
3553         if (!session)
3554                 return -1;
3555         boost::shared_ptr<Stripable> s = get_strip (ssid, lo_message_get_source (msg));
3556
3557         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
3558
3559         if (!r) {
3560                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
3561                 return -1;
3562         }
3563
3564         boost::shared_ptr<Processor> redi=r->nth_plugin (piid - 1);
3565
3566         if (!redi) {
3567                 PBD::error << "OSC: cannot find plugin # " << piid << " for RID '" << ssid << "'" << endmsg;
3568                 return -1;
3569         }
3570
3571         boost::shared_ptr<PluginInsert> pi;
3572
3573         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
3574                 PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
3575                 return -1;
3576         }
3577
3578         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
3579         pi->activate();
3580
3581         return 0;
3582 }
3583
3584 int
3585 OSC::route_plugin_deactivate (int ssid, int piid, lo_message msg)
3586 {
3587         if (!session)
3588                 return -1;
3589         boost::shared_ptr<Stripable> s = get_strip (ssid, lo_message_get_source (msg));
3590
3591         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
3592
3593         if (!r) {
3594                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
3595                 return -1;
3596         }
3597
3598         boost::shared_ptr<Processor> redi=r->nth_plugin (piid - 1);
3599
3600         if (!redi) {
3601                 PBD::error << "OSC: cannot find plugin # " << piid << " for RID '" << ssid << "'" << endmsg;
3602                 return -1;
3603         }
3604
3605         boost::shared_ptr<PluginInsert> pi;
3606
3607         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
3608                 PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
3609                 return -1;
3610         }
3611
3612         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
3613         pi->deactivate();
3614
3615         return 0;
3616 }
3617
3618 // select
3619
3620 int
3621 OSC::sel_pan_elevation (float val, lo_message msg)
3622 {
3623         OSCSurface *sur = get_surface(get_address (msg));
3624         boost::shared_ptr<Stripable> s;
3625         if (sur->expand_enable) {
3626                 s = get_strip (sur->expand, get_address (msg));
3627         } else {
3628                 s = _select;
3629         }
3630         if (s) {
3631                 if (s->pan_elevation_control()) {
3632                         s->pan_elevation_control()->set_value (s->pan_elevation_control()->interface_to_internal (val), PBD::Controllable::NoGroup);
3633                         return 0;
3634                 }
3635         }
3636         return sel_fail ("pan_elevation_position", 0, get_address (msg));
3637 }
3638
3639 int
3640 OSC::sel_pan_frontback (float val, lo_message msg)
3641 {
3642         OSCSurface *sur = get_surface(get_address (msg));
3643         boost::shared_ptr<Stripable> s;
3644         if (sur->expand_enable) {
3645                 s = get_strip (sur->expand, get_address (msg));
3646         } else {
3647                 s = _select;
3648         }
3649         if (s) {
3650                 if (s->pan_frontback_control()) {
3651                         s->pan_frontback_control()->set_value (s->pan_frontback_control()->interface_to_internal (val), PBD::Controllable::NoGroup);
3652                         return 0;
3653                 }
3654         }
3655         return sel_fail ("pan_frontback_position", 0.5, get_address (msg));
3656 }
3657
3658 int
3659 OSC::sel_pan_lfe (float val, lo_message msg)
3660 {
3661         OSCSurface *sur = get_surface(get_address (msg));
3662         boost::shared_ptr<Stripable> s;
3663         if (sur->expand_enable) {
3664                 s = get_strip (sur->expand, get_address (msg));
3665         } else {
3666                 s = _select;
3667         }
3668         if (s) {
3669                 if (s->pan_lfe_control()) {
3670                         s->pan_lfe_control()->set_value (s->pan_lfe_control()->interface_to_internal (val), PBD::Controllable::NoGroup);
3671                         return 0;
3672                 }
3673         }
3674         return sel_fail ("pan_lfe_control", 0, get_address (msg));
3675 }
3676
3677 // compressor control
3678 int
3679 OSC::sel_comp_enable (float val, lo_message msg)
3680 {
3681         OSCSurface *sur = get_surface(get_address (msg));
3682         boost::shared_ptr<Stripable> s;
3683         if (sur->expand_enable) {
3684                 s = get_strip (sur->expand, get_address (msg));
3685         } else {
3686                 s = _select;
3687         }
3688         if (s) {
3689                 if (s->comp_enable_controllable()) {
3690                         s->comp_enable_controllable()->set_value (s->comp_enable_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
3691                         return 0;
3692                 }
3693         }
3694         return sel_fail ("comp_enable", 0, get_address (msg));
3695 }
3696
3697 int
3698 OSC::sel_comp_threshold (float val, lo_message msg)
3699 {
3700         OSCSurface *sur = get_surface(get_address (msg));
3701         boost::shared_ptr<Stripable> s;
3702         if (sur->expand_enable) {
3703                 s = get_strip (sur->expand, get_address (msg));
3704         } else {
3705                 s = _select;
3706         }
3707         if (s) {
3708                 if (s->comp_threshold_controllable()) {
3709                         s->comp_threshold_controllable()->set_value (s->comp_threshold_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
3710                         return 0;
3711                 }
3712         }
3713         return sel_fail ("comp_threshold", 0, get_address (msg));
3714 }
3715
3716 int
3717 OSC::sel_comp_speed (float val, lo_message msg)
3718 {
3719         OSCSurface *sur = get_surface(get_address (msg));
3720         boost::shared_ptr<Stripable> s;
3721         if (sur->expand_enable) {
3722                 s = get_strip (sur->expand, get_address (msg));
3723         } else {
3724                 s = _select;
3725         }
3726         if (s) {
3727                 if (s->comp_speed_controllable()) {
3728                         s->comp_speed_controllable()->set_value (s->comp_speed_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
3729                         return 0;
3730                 }
3731         }
3732         return sel_fail ("comp_speed", 0, get_address (msg));
3733 }
3734
3735 int
3736 OSC::sel_comp_mode (float val, lo_message msg)
3737 {
3738         OSCSurface *sur = get_surface(get_address (msg));
3739         boost::shared_ptr<Stripable> s;
3740         if (sur->expand_enable) {
3741                 s = get_strip (sur->expand, get_address (msg));
3742         } else {
3743                 s = _select;
3744         }
3745         if (s) {
3746                 if (s->comp_mode_controllable()) {
3747                         s->comp_mode_controllable()->set_value (s->comp_mode_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
3748                         return 0;
3749                 }
3750         }
3751         return sel_fail ("comp_mode", 0, get_address (msg));
3752 }
3753
3754 int
3755 OSC::sel_comp_makeup (float val, lo_message msg)
3756 {
3757         OSCSurface *sur = get_surface(get_address (msg));
3758         boost::shared_ptr<Stripable> s;
3759         if (sur->expand_enable) {
3760                 s = get_strip (sur->expand, get_address (msg));
3761         } else {
3762                 s = _select;
3763         }
3764         if (s) {
3765                 if (s->comp_makeup_controllable()) {
3766                         s->comp_makeup_controllable()->set_value (s->comp_makeup_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
3767                         return 0;
3768                 }
3769         }
3770         return sel_fail ("comp_makeup", 0, get_address (msg));
3771 }
3772
3773 // EQ control
3774
3775 int
3776 OSC::sel_eq_enable (float val, lo_message msg)
3777 {
3778         OSCSurface *sur = get_surface(get_address (msg));
3779         boost::shared_ptr<Stripable> s;
3780         if (sur->expand_enable) {
3781                 s = get_strip (sur->expand, get_address (msg));
3782         } else {
3783                 s = _select;
3784         }
3785         if (s) {
3786                 if (s->eq_enable_controllable()) {
3787                         s->eq_enable_controllable()->set_value (s->eq_enable_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
3788                         return 0;
3789                 }
3790         }
3791         return sel_fail ("eq_enable", 0, get_address (msg));
3792 }
3793
3794 int
3795 OSC::sel_eq_hpf_freq (float val, lo_message msg)
3796 {
3797         OSCSurface *sur = get_surface(get_address (msg));
3798         boost::shared_ptr<Stripable> s;
3799         if (sur->expand_enable) {
3800                 s = get_strip (sur->expand, get_address (msg));
3801         } else {
3802                 s = _select;
3803         }
3804         if (s) {
3805                 if (s->filter_freq_controllable(true)) {
3806                         s->filter_freq_controllable(true)->set_value (s->filter_freq_controllable(true)->interface_to_internal (val), PBD::Controllable::NoGroup);
3807                         return 0;
3808                 }
3809         }
3810         return sel_fail ("eq_hpf/freq", 0, get_address (msg));
3811 }
3812
3813 int
3814 OSC::sel_eq_lpf_freq (float val, lo_message msg)
3815 {
3816         OSCSurface *sur = get_surface(get_address (msg));
3817         boost::shared_ptr<Stripable> s;
3818         if (sur->expand_enable) {
3819                 s = get_strip (sur->expand, get_address (msg));
3820         } else {
3821                 s = _select;
3822         }
3823         if (s) {
3824                 if (s->filter_freq_controllable(false)) {
3825                         s->filter_freq_controllable(false)->set_value (s->filter_freq_controllable(false)->interface_to_internal (val), PBD::Controllable::NoGroup);
3826                         return 0;
3827                 }
3828         }
3829         return sel_fail ("eq_lpf/freq", 0, get_address (msg));
3830 }
3831
3832 int
3833 OSC::sel_eq_hpf_enable (float val, lo_message msg)
3834 {
3835         OSCSurface *sur = get_surface(get_address (msg));
3836         boost::shared_ptr<Stripable> s;
3837         if (sur->expand_enable) {
3838                 s = get_strip (sur->expand, get_address (msg));
3839         } else {
3840                 s = _select;
3841         }
3842         if (s) {
3843                 if (s->filter_enable_controllable(true)) {
3844                         s->filter_enable_controllable(true)->set_value (s->filter_enable_controllable(true)->interface_to_internal (val), PBD::Controllable::NoGroup);
3845                         return 0;
3846                 }
3847         }
3848         return sel_fail ("eq_hpf/enable", 0, get_address (msg));
3849 }
3850
3851 int
3852 OSC::sel_eq_lpf_enable (float val, lo_message msg)
3853 {
3854         OSCSurface *sur = get_surface(get_address (msg));
3855         boost::shared_ptr<Stripable> s;
3856         if (sur->expand_enable) {
3857                 s = get_strip (sur->expand, get_address (msg));
3858         } else {
3859                 s = _select;
3860         }
3861         if (s) {
3862                 if (s->filter_enable_controllable(false)) {
3863                         s->filter_enable_controllable(false)->set_value (s->filter_enable_controllable(false)->interface_to_internal (val), PBD::Controllable::NoGroup);
3864                         return 0;
3865                 }
3866         }
3867         return sel_fail ("eq_lpf/enable", 0, get_address (msg));
3868 }
3869
3870 int
3871 OSC::sel_eq_hpf_slope (float val, lo_message msg)
3872 {
3873         OSCSurface *sur = get_surface(get_address (msg));
3874         boost::shared_ptr<Stripable> s;
3875         if (sur->expand_enable) {
3876                 s = get_strip (sur->expand, get_address (msg));
3877         } else {
3878                 s = _select;
3879         }
3880         if (s) {
3881                 if (s->filter_slope_controllable(true)) {
3882                         s->filter_slope_controllable(true)->set_value (s->filter_slope_controllable(true)->interface_to_internal (val), PBD::Controllable::NoGroup);
3883                         return 0;
3884                 }
3885         }
3886         return sel_fail ("eq_hpf/slope", 0, get_address (msg));
3887 }
3888
3889 int
3890 OSC::sel_eq_lpf_slope (float val, lo_message msg)
3891 {
3892         OSCSurface *sur = get_surface(get_address (msg));
3893         boost::shared_ptr<Stripable> s;
3894         if (sur->expand_enable) {
3895                 s = get_strip (sur->expand, get_address (msg));
3896         } else {
3897                 s = _select;
3898         }
3899         if (s) {
3900                 if (s->filter_slope_controllable(false)) {
3901                         s->filter_slope_controllable(false)->set_value (s->filter_slope_controllable(false)->interface_to_internal (val), PBD::Controllable::NoGroup);
3902                         return 0;
3903                 }
3904         }
3905         return sel_fail ("eq_lpf/slope", 0, get_address (msg));
3906 }
3907
3908 int
3909 OSC::sel_eq_gain (int id, float val, lo_message msg)
3910 {
3911         OSCSurface *sur = get_surface(get_address (msg));
3912         boost::shared_ptr<Stripable> s;
3913         if (sur->expand_enable) {
3914                 s = get_strip (sur->expand, get_address (msg));
3915         } else {
3916                 s = _select;
3917         }
3918         if (s) {
3919                 if (id > 0) {
3920                         --id;
3921                 }
3922                 if (s->eq_gain_controllable (id)) {
3923                         s->eq_gain_controllable (id)->set_value (s->eq_gain_controllable(id)->interface_to_internal (val), PBD::Controllable::NoGroup);
3924                         return 0;
3925                 }
3926         }
3927         return sel_send_fail ("eq_gain", id + 1, 0, get_address (msg));
3928 }
3929
3930 int
3931 OSC::sel_eq_freq (int id, float val, lo_message msg)
3932 {
3933         OSCSurface *sur = get_surface(get_address (msg));
3934         boost::shared_ptr<Stripable> s;
3935         if (sur->expand_enable) {
3936                 s = get_strip (sur->expand, get_address (msg));
3937         } else {
3938                 s = _select;
3939         }
3940         if (s) {
3941                 if (id > 0) {
3942                         --id;
3943                 }
3944                 if (s->eq_freq_controllable (id)) {
3945                         s->eq_freq_controllable (id)->set_value (s->eq_freq_controllable(id)->interface_to_internal (val), PBD::Controllable::NoGroup);
3946                         return 0;
3947                 }
3948         }
3949         return sel_send_fail ("eq_freq", id + 1, 0, get_address (msg));
3950 }
3951
3952 int
3953 OSC::sel_eq_q (int id, float val, lo_message msg)
3954 {
3955         OSCSurface *sur = get_surface(get_address (msg));
3956         boost::shared_ptr<Stripable> s;
3957         if (sur->expand_enable) {
3958                 s = get_strip (sur->expand, get_address (msg));
3959         } else {
3960                 s = _select;
3961         }
3962         if (s) {
3963                 if (id > 0) {
3964                         --id;
3965                 }
3966                 if (s->eq_q_controllable (id)) {
3967                         s->eq_q_controllable (id)->set_value (s->eq_q_controllable(id)->interface_to_internal (val), PBD::Controllable::NoGroup);
3968                         return 0;
3969                 }
3970         }
3971         return sel_send_fail ("eq_q", id + 1, 0, get_address (msg));
3972 }
3973
3974 int
3975 OSC::sel_eq_shape (int id, float val, lo_message msg)
3976 {
3977         OSCSurface *sur = get_surface(get_address (msg));
3978         boost::shared_ptr<Stripable> s;
3979         if (sur->expand_enable) {
3980                 s = get_strip (sur->expand, get_address (msg));
3981         } else {
3982                 s = _select;
3983         }
3984         if (s) {
3985                 if (id > 0) {
3986                         --id;
3987                 }
3988                 if (s->eq_shape_controllable (id)) {
3989                         s->eq_shape_controllable (id)->set_value (s->eq_shape_controllable(id)->interface_to_internal (val), PBD::Controllable::NoGroup);
3990                         return 0;
3991                 }
3992         }
3993         return sel_send_fail ("eq_shape", id + 1, 0, get_address (msg));
3994 }
3995
3996 void
3997 OSC::gui_selection_changed ()
3998 {
3999         boost::shared_ptr<Stripable> strip = ControlProtocol::first_selected_stripable();
4000
4001         if (strip) {
4002                 _select = strip;
4003                 for (uint32_t it = 0; it < _surface.size(); ++it) {
4004                         OSCSurface* sur = &_surface[it];
4005                         if(!sur->expand_enable) {
4006                                 lo_address addr = lo_address_new_from_url (sur->remote_url.c_str());
4007                                 _strip_select (strip, addr);
4008                         }
4009                 }
4010         }
4011 }
4012
4013 // timer callbacks
4014 bool
4015 OSC::periodic (void)
4016 {
4017         if (!tick) {
4018                 Glib::usleep(100); // let flurry of signals subside
4019                 if (global_init) {
4020                         for (uint32_t it = 0; it < _surface.size(); it++) {
4021                                 OSCSurface* sur = &_surface[it];
4022                                 lo_address addr = lo_address_new_from_url (sur->remote_url.c_str());
4023                                 global_feedback (sur->feedback, addr, sur->gainmode);
4024                         }
4025                         global_init = false;
4026                         tick = true;
4027                 }
4028                 if (bank_dirty) {
4029                         _recalcbanks ();
4030                         bank_dirty = false;
4031                         tick = true;
4032                 }
4033         }
4034
4035         if (scrub_speed != 0) {
4036                 // for those jog wheels that don't have 0 on release (touch), time out.
4037                 int64_t now = ARDOUR::get_microseconds ();
4038                 int64_t diff = now - scrub_time;
4039                 if (diff > 120000) {
4040                         scrub_speed = 0;
4041                         session->request_transport_speed (0);
4042                         // locate to the place PH was at last tick
4043                         session->request_locate (scrub_place, false);
4044                 }
4045         }
4046
4047         for (GlobalObservers::iterator x = global_observers.begin(); x != global_observers.end(); x++) {
4048
4049                 OSCGlobalObserver* go;
4050
4051                 if ((go = dynamic_cast<OSCGlobalObserver*>(*x)) != 0) {
4052                         go->tick();
4053                 }
4054         }
4055         for (RouteObservers::iterator x = route_observers.begin(); x != route_observers.end(); x++) {
4056
4057                 OSCRouteObserver* ro;
4058
4059                 if ((ro = dynamic_cast<OSCRouteObserver*>(*x)) != 0) {
4060                         ro->tick();
4061                 }
4062         }
4063         for (uint32_t it = 0; it < _surface.size(); it++) {
4064                 OSCSurface* sur = &_surface[it];
4065                 OSCSelectObserver* so;
4066                 if ((so = dynamic_cast<OSCSelectObserver*>(sur->sel_obs)) != 0) {
4067                         so->tick();
4068                 }
4069         }
4070         for (CueObservers::iterator x = cue_observers.begin(); x != cue_observers.end(); x++) {
4071
4072                 OSCCueObserver* co;
4073
4074                 if ((co = dynamic_cast<OSCCueObserver*>(*x)) != 0) {
4075                         co->tick();
4076                 }
4077         }
4078         for (FakeTouchMap::iterator x = _touch_timeout.begin(); x != _touch_timeout.end();) {
4079                 _touch_timeout[(*x).first] = (*x).second - 1;
4080                 if (!(*x).second) {
4081                         boost::shared_ptr<ARDOUR::AutomationControl> ctrl = (*x).first;
4082                         // turn touch off
4083                         ctrl->stop_touch (true, ctrl->session().transport_frame());
4084                         _touch_timeout.erase (x++);
4085                 } else {
4086                         x++;
4087                 }
4088         }
4089         return true;
4090 }
4091
4092 int
4093 OSC::route_send_fail (string path, uint32_t ssid, float val, lo_address addr)
4094 {
4095         OSCSurface *sur = get_surface(addr);
4096
4097         ostringstream os;
4098         lo_message reply;
4099         if (ssid) {
4100                 reply = lo_message_new ();
4101                 if (sur->feedback[2]) {
4102                         os << "/strip/" << path << "/" << ssid;
4103                 } else {
4104                         os << "/strip/" << path;
4105                         lo_message_add_int32 (reply, ssid);
4106                 }
4107                 string str_pth = os.str();
4108                 lo_message_add_float (reply, (float) val);
4109
4110                 lo_send_message (addr, str_pth.c_str(), reply);
4111                 lo_message_free (reply);
4112         }
4113         if ((_select == get_strip (ssid, addr)) || ((sur->expand == ssid) && (sur->expand_enable))) {
4114                 os.str("");
4115                 os << "/select/" << path;
4116                 string sel_pth = os.str();
4117                 reply = lo_message_new ();
4118                 lo_message_add_float (reply, (float) val);
4119                 lo_send_message (addr, sel_pth.c_str(), reply);
4120                 lo_message_free (reply);
4121         }
4122
4123         return 0;
4124 }
4125
4126 int
4127 OSC::sel_fail (string path, float val, lo_address addr)
4128 {
4129         ostringstream os;
4130         os.str("");
4131         os << "/select/" << path;
4132         string sel_pth = os.str();
4133         lo_message reply = lo_message_new ();
4134         lo_message_add_float (reply, (float) val);
4135         lo_send_message (addr, sel_pth.c_str(), reply);
4136         lo_message_free (reply);
4137
4138         return 0;
4139 }
4140
4141 int
4142 OSC::sel_send_fail (string path, uint32_t id, float val, lo_address addr)
4143 {
4144         OSCSurface *sur = get_surface(addr);
4145
4146         ostringstream os;
4147         lo_message reply;
4148         reply = lo_message_new ();
4149         if (sur->feedback[2]) {
4150                 os << "/select/" << path << "/" << id;
4151         } else {
4152                 os << "/select/" << path;
4153                 lo_message_add_int32 (reply, id);
4154         }
4155         string str_pth = os.str();
4156         lo_message_add_float (reply, (float) val);
4157
4158         lo_send_message (addr, str_pth.c_str(), reply);
4159         lo_message_free (reply);
4160
4161         return 0;
4162 }
4163
4164 XMLNode&
4165 OSC::get_state ()
4166 {
4167         XMLNode& node (ControlProtocol::get_state());
4168         node.set_property ("debugmode", (int32_t) _debugmode); // TODO: enum2str
4169         node.set_property ("address-only", address_only);
4170         node.set_property ("remote-port", remote_port);
4171         node.set_property ("banksize", default_banksize);
4172         node.set_property ("striptypes", default_strip);
4173         node.set_property ("feedback", default_feedback);
4174         node.set_property ("gainmode", default_gainmode);
4175         if (_surface.size()) {
4176                 XMLNode* config = new XMLNode (X_("Configurations"));
4177                 for (uint32_t it = 0; it < _surface.size(); ++it) {
4178                         OSCSurface* sur = &_surface[it];
4179                         XMLNode* devnode = new XMLNode (X_("Configuration"));
4180                         devnode->set_property (X_("url"), sur->remote_url);
4181                         devnode->set_property (X_("bank-size"), sur->bank_size);
4182                         devnode->set_property (X_("strip-types"), (uint64_t)sur->strip_types.to_ulong());
4183                         devnode->set_property (X_("feedback"), (uint64_t)sur->feedback.to_ulong());
4184                         devnode->set_property (X_("gainmode"), sur->gainmode);
4185                         config->add_child_nocopy (*devnode);
4186                 }
4187                 node.add_child_nocopy (*config);
4188         }
4189         return node;
4190 }
4191
4192 int
4193 OSC::set_state (const XMLNode& node, int version)
4194 {
4195         if (ControlProtocol::set_state (node, version)) {
4196                 return -1;
4197         }
4198         int32_t debugmode;
4199         if (node.get_property (X_("debugmode"), debugmode)) {
4200                 _debugmode = OSCDebugMode (debugmode);
4201         }
4202
4203         node.get_property (X_("address-only"), address_only);
4204         node.get_property (X_("remote-port"), remote_port);
4205         node.get_property (X_("banksize"), default_banksize);
4206         node.get_property (X_("striptypes"), default_strip);
4207         node.get_property (X_("feedback"), default_feedback);
4208         node.get_property (X_("gainmode"), default_gainmode);
4209
4210         XMLNode* cnode = node.child (X_("Configurations"));
4211
4212         if (cnode) {
4213                 XMLNodeList const& devices = cnode->children();
4214                 for (XMLNodeList::const_iterator d = devices.begin(); d != devices.end(); ++d) {
4215                         OSCSurface s;
4216                         if (!(*d)->get_property (X_("url"), s.remote_url)) {
4217                                 continue;
4218                         }
4219
4220                         bank_dirty = true;
4221
4222                         (*d)->get_property (X_("bank-size"), s.bank_size);
4223
4224                         uint64_t bits;
4225                         if ((*d)->get_property (X_ ("strip-types"), bits)) {
4226                                 s.strip_types = bits;
4227                         }
4228                         if ((*d)->get_property (X_("feedback"), bits)) {
4229                                 s.feedback = bits;
4230                         }
4231                         (*d)->get_property (X_("gainmode"), s.gainmode);
4232
4233                         s.bank = 1;
4234                         s.sel_obs = 0;
4235                         s.expand = 0;
4236                         s.expand_enable = false;
4237                         s.strips = get_sorted_stripables (s.strip_types, s.cue);
4238                         s.nstrips = s.strips.size ();
4239                         s.no_clear = false;
4240                         s.jogmode = JOG;
4241                         s.cue = false;
4242                         s.aux = 0;
4243                         _surface.push_back (s);
4244                 }
4245         }
4246         global_init = true;
4247         tick = false;
4248
4249         return 0;
4250 }
4251
4252 // predicate for sort call in get_sorted_stripables
4253 struct StripableByPresentationOrder
4254 {
4255         bool operator () (const boost::shared_ptr<Stripable> & a, const boost::shared_ptr<Stripable> & b) const
4256         {
4257                 return a->presentation_info().order() < b->presentation_info().order();
4258         }
4259
4260         bool operator () (const Stripable & a, const Stripable & b) const
4261         {
4262                 return a.presentation_info().order() < b.presentation_info().order();
4263         }
4264
4265         bool operator () (const Stripable * a, const Stripable * b) const
4266         {
4267                 return a->presentation_info().order() < b->presentation_info().order();
4268         }
4269 };
4270
4271 OSC::Sorted
4272 OSC::get_sorted_stripables(std::bitset<32> types, bool cue)
4273 {
4274         Sorted sorted;
4275
4276         // fetch all stripables
4277         StripableList stripables;
4278
4279         session->get_stripables (stripables);
4280
4281         // Look for stripables that match bit in sur->strip_types
4282         for (StripableList::iterator it = stripables.begin(); it != stripables.end(); ++it) {
4283
4284                 boost::shared_ptr<Stripable> s = *it;
4285                 if ((!cue) && (!types[9]) && (s->presentation_info().flags() & PresentationInfo::Hidden)) {
4286                         // do nothing... skip it
4287                 } else {
4288
4289                         if (types[0] && (s->presentation_info().flags() & PresentationInfo::AudioTrack)) {
4290                                 sorted.push_back (s);
4291                         } else
4292                         if (types[1] && (s->presentation_info().flags() & PresentationInfo::MidiTrack)) {
4293                                 sorted.push_back (s);
4294                         } else
4295                         if ((s->presentation_info().flags() & PresentationInfo::AudioBus)) {
4296                                 boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
4297                                 // r->feeds (session->master_out()) may make more sense
4298                                 if (r->direct_feeds_according_to_reality (session->master_out())) {
4299                                         // this is a bus
4300                                         if (types[2]) {
4301                                                 sorted.push_back (s);
4302                                         }
4303                                 } else {
4304                                         // this is an Aux out
4305                                         if (types[7]) {
4306                                                 sorted.push_back (s);
4307                                         }
4308                                 }
4309                         } else if (types[3] && (s->presentation_info().flags() & PresentationInfo::MidiBus)) {
4310                                 sorted.push_back (s);
4311                         } else if (types[4] && (s->presentation_info().flags() & PresentationInfo::VCA)) {
4312                                 sorted.push_back (s);
4313                         } else if (types[8] && (s->is_selected())) {
4314                                 sorted.push_back (s);
4315                         } else if (types[9] && (s->presentation_info().flags() & PresentationInfo::Hidden)) {
4316                                 sorted.push_back (s);
4317                         }
4318                 }
4319         }
4320         sort (sorted.begin(), sorted.end(), StripableByPresentationOrder());
4321         // Master/Monitor might be anywhere... we put them at the end - Sorry ;)
4322         if (types[5]) {
4323                 sorted.push_back (session->master_out());
4324         }
4325         if (types[6]) {
4326                 sorted.push_back (session->monitor_out());
4327         }
4328         return sorted;
4329 }
4330
4331 int
4332 OSC::cue_parse (const char *path, const char* types, lo_arg **argv, int argc, lo_message msg)
4333 {
4334         int ret = 1; /* unhandled */
4335
4336         if (!strncmp (path, "/cue/aux", 8)) {
4337                 // set our Aux bus
4338                 ret = cue_set (argv[0]->i, msg);
4339         }
4340         else if (!strncmp (path, "/cue/connect", 12)) {
4341                 // Connect to default Aux bus
4342                 if ((!argc) || argv[0]->i) {
4343                         ret = cue_set (1, msg);
4344                 }
4345         }
4346         else if (!strncmp (path, "/cue/next_aux", 13)) {
4347                 // switch to next Aux bus
4348                 if ((!argc) || argv[0]->i) {
4349                         ret = cue_next (msg);
4350                 }
4351         }
4352         else if (!strncmp (path, "/cue/previous_aux", 17)) {
4353                 // switch to previous Aux bus
4354                 if ((!argc) || argv[0]->i) {
4355                         ret = cue_previous (msg);
4356                 }
4357         }
4358         else if (!strncmp (path, "/cue/send/fader/", 16) && strlen (path) > 16) {
4359                 int id = atoi (&path[16]);
4360                 ret = cue_send_fader (id, argv[0]->f, msg);
4361         }
4362         else if (!strncmp (path, "/cue/send/enable/", 17) && strlen (path) > 17) {
4363                 int id = atoi (&path[17]);
4364                 ret = cue_send_enable (id, argv[0]->f, msg);
4365         }
4366         else if (!strncmp (path, "/cue/fader", 10)) {
4367                 ret = cue_aux_fader (argv[0]->f, msg);
4368         }
4369         else if (!strncmp (path, "/cue/mute", 9)) {
4370                 ret = cue_aux_mute (argv[0]->f, msg);
4371         }
4372
4373         return ret;
4374 }
4375
4376 int
4377 OSC::cue_set (uint32_t aux, lo_message msg)
4378 {
4379         return _cue_set (aux, get_address (msg));
4380 }
4381
4382 int
4383 OSC::_cue_set (uint32_t aux, lo_address addr)
4384 {
4385         int ret = 1;
4386         OSCSurface *s = get_surface(addr);
4387         s->bank_size = 0;
4388         s->strip_types = 128;
4389         s->feedback = 0;
4390         s->gainmode = 1;
4391         s->cue = true;
4392         s->strips = get_sorted_stripables(s->strip_types, s->cue);
4393
4394         s->nstrips = s->strips.size();
4395
4396         if (aux < 1) {
4397                 aux = 1;
4398         } else if (aux > s->nstrips) {
4399                 aux = s->nstrips;
4400         }
4401         s->aux = aux;
4402
4403         // get rid of any old CueObsevers for this address
4404         //cueobserver_connections.drop_connections ();
4405         CueObservers::iterator x;
4406         for (x = cue_observers.begin(); x != cue_observers.end();) {
4407
4408                 OSCCueObserver* co;
4409
4410                 if ((co = dynamic_cast<OSCCueObserver*>(*x)) != 0) {
4411
4412                         int res = strcmp(lo_address_get_url(co->address()), lo_address_get_url(addr));
4413
4414                         if (res == 0) {
4415                                 delete *x;
4416                                 x = cue_observers.erase (x);
4417                         } else {
4418                                 ++x;
4419                         }
4420                 } else {
4421                         ++x;
4422                 }
4423         }
4424
4425         // get a list of Auxes
4426         for (uint32_t n = 0; n < s->nstrips; ++n) {
4427                 boost::shared_ptr<Stripable> stp = s->strips[n];
4428                 if (stp) {
4429                         text_message (string_compose ("/cue/name/%1", n+1), stp->name(), addr);
4430                         if (aux == n+1) {
4431                                 // aux must be at least one
4432                                 // need a signal if aux vanishes
4433                                 stp->DropReferences.connect (*this, MISSING_INVALIDATOR, boost::bind (&OSC::_cue_set, this, aux, addr), this);
4434
4435                                 // make a list of stripables with sends that go to this bus
4436                                 s->sends = cue_get_sorted_stripables(stp, aux, addr);
4437                                 // start cue observer
4438                                 OSCCueObserver* co = new OSCCueObserver (stp, s->sends, addr);
4439                                 cue_observers.push_back (co);
4440                                 ret = 0;
4441                         }
4442
4443                 }
4444         }
4445
4446         return ret;
4447 }
4448
4449 int
4450 OSC::cue_next (lo_message msg)
4451 {
4452         OSCSurface *s = get_surface(get_address (msg));
4453         int ret = 1;
4454
4455         if (!s->cue) {
4456                 ret = cue_set (1, msg);
4457         }
4458         if (s->aux < s->nstrips) {
4459                 ret = cue_set (s->aux + 1, msg);
4460         } else {
4461                 ret = cue_set (s->nstrips, msg);
4462         }
4463         return ret;
4464 }
4465
4466 int
4467 OSC::cue_previous (lo_message msg)
4468 {
4469         OSCSurface *s = get_surface(get_address (msg));
4470         int ret = 1;
4471         if (!s->cue) {
4472                 ret = cue_set (1, msg);
4473         }
4474         if (s->aux > 1) {
4475                 ret = cue_set (s->aux - 1, msg);
4476         }
4477         return ret;
4478 }
4479
4480 boost::shared_ptr<Send>
4481 OSC::cue_get_send (uint32_t id, lo_address addr)
4482 {
4483         OSCSurface *s = get_surface(addr);
4484         if (id && s->aux > 0 && id <= s->sends.size()) {
4485                 boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s->sends[id - 1]);
4486                 boost::shared_ptr<Stripable> aux = get_strip (s->aux, addr);
4487                 if (r && aux) {
4488                         return r->internal_send_for (boost::dynamic_pointer_cast<Route> (aux));
4489                 }
4490         }
4491         return boost::shared_ptr<Send>();
4492
4493 }
4494
4495 int
4496 OSC::cue_aux_fader (float position, lo_message msg)
4497 {
4498         if (!session) return -1;
4499
4500         OSCSurface *sur = get_surface(get_address (msg));
4501         if (sur->cue) {
4502                 if (sur->aux) {
4503                         boost::shared_ptr<Stripable> s = get_strip (sur->aux, get_address (msg));
4504
4505                         if (s) {
4506                                 float abs;
4507                                 abs = slider_position_to_gain_with_max (position, 2.0);
4508                                 if (s->gain_control()) {
4509                                         s->gain_control()->set_value (abs, PBD::Controllable::NoGroup);
4510                                         return 0;
4511                                 }
4512                         }
4513                 }
4514         }
4515         cue_float_message ("/cue/fader", 0, get_address (msg));
4516         return -1;
4517 }
4518
4519 int
4520 OSC::cue_aux_mute (float state, lo_message msg)
4521 {
4522         if (!session) return -1;
4523
4524         OSCSurface *sur = get_surface(get_address (msg));
4525         if (sur->cue) {
4526                 if (sur->aux) {
4527                         boost::shared_ptr<Stripable> s = get_strip (sur->aux, get_address (msg));
4528                         if (s) {
4529                                 if (s->mute_control()) {
4530                                         s->mute_control()->set_value (state ? 1.0 : 0.0, PBD::Controllable::NoGroup);
4531                                         return 0;
4532                                 }
4533                         }
4534                 }
4535         }
4536         cue_float_message ("/cue/mute", 0, get_address (msg));
4537         return -1;
4538 }
4539
4540 int
4541 OSC::cue_send_fader (uint32_t id, float val, lo_message msg)
4542 {
4543         if (!session) {
4544                 return -1;
4545         }
4546         boost::shared_ptr<Send> s = cue_get_send (id, get_address (msg));
4547         float abs;
4548         if (s) {
4549                 if (s->gain_control()) {
4550                         abs = slider_position_to_gain_with_max (val, 2.0);
4551                         s->gain_control()->set_value (abs, PBD::Controllable::NoGroup);
4552                         return 0;
4553                 }
4554         }
4555         cue_float_message (string_compose ("/cue/send/fader/%1", id), 0, get_address (msg));
4556         return -1;
4557 }
4558
4559 int
4560 OSC::cue_send_enable (uint32_t id, float state, lo_message msg)
4561 {
4562         if (!session)
4563                 return -1;
4564         boost::shared_ptr<Send> s = cue_get_send (id, get_address (msg));
4565         if (s) {
4566                 if (state) {
4567                         s->activate ();
4568                 } else {
4569                         s->deactivate ();
4570                 }
4571                 return 0;
4572         }
4573         cue_float_message (string_compose ("/cue/send/enable/%1", id), 0, get_address (msg));
4574         return -1;
4575 }
4576
4577 int
4578 OSC::cue_float_message (string path, float val, lo_address addr)
4579 {
4580
4581         lo_message reply;
4582         reply = lo_message_new ();
4583         lo_message_add_float (reply, (float) val);
4584
4585         lo_send_message (addr, path.c_str(), reply);
4586         lo_message_free (reply);
4587
4588         return 0;
4589 }
4590
4591 int
4592 OSC::text_message (string path, string val, lo_address addr)
4593 {
4594
4595         lo_message reply;
4596         reply = lo_message_new ();
4597         lo_message_add_string (reply, val.c_str());
4598
4599         lo_send_message (addr, path.c_str(), reply);
4600         lo_message_free (reply);
4601
4602         return 0;
4603 }
4604
4605
4606 // we have to have a sorted list of stripables that have sends pointed at our aux
4607 // we can use the one in osc.cc to get an aux list
4608 OSC::Sorted
4609 OSC::cue_get_sorted_stripables(boost::shared_ptr<Stripable> aux, uint32_t id, lo_message msg)
4610 {
4611         Sorted sorted;
4612         cueobserver_connections.drop_connections ();
4613         // fetch all stripables
4614         StripableList stripables;
4615
4616         session->get_stripables (stripables);
4617
4618         // Look for stripables that have a send to aux
4619         for (StripableList::iterator it = stripables.begin(); it != stripables.end(); ++it) {
4620
4621                 boost::shared_ptr<Stripable> s = *it;
4622                 // we only want routes
4623                 boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
4624                 if (r) {
4625                         r->processors_changed.connect  (*this, MISSING_INVALIDATOR, boost::bind (&OSC::recalcbanks, this), this);
4626                         boost::shared_ptr<Send> snd = r->internal_send_for (boost::dynamic_pointer_cast<Route> (aux));
4627                         if (snd) { // test for send to aux
4628                                 sorted.push_back (s);
4629                                 s->DropReferences.connect (*this, MISSING_INVALIDATOR, boost::bind (&OSC::cue_set, this, id, msg), this);
4630                         }
4631                 }
4632
4633
4634         }
4635         sort (sorted.begin(), sorted.end(), StripableByPresentationOrder());
4636
4637         return sorted;
4638 }
4639