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