OSC: add param to plugin_list response which indicates plugin enable state (OnkelDead)
[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                 lo_message_add_int32(reply, redi->enabled() ? 1 : 0);
4041
4042                 piid++;
4043         }
4044
4045         lo_send_message (get_address (msg), "/strip/plugin/list", reply);
4046         lo_message_free (reply);
4047         return 0;
4048 }
4049
4050 int
4051 OSC::route_plugin_descriptor (int ssid, int piid, lo_message msg) {
4052         if (!session) {
4053                 return -1;
4054         }
4055
4056         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route>(get_strip (ssid, get_address (msg)));
4057
4058         if (!r) {
4059                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
4060                 return -1;
4061         }
4062
4063         boost::shared_ptr<Processor> redi = r->nth_plugin(piid - 1);
4064
4065         if (!redi) {
4066                 PBD::error << "OSC: cannot find plugin # " << piid << " for RID '" << ssid << "'" << endmsg;
4067                 return -1;
4068         }
4069
4070         boost::shared_ptr<PluginInsert> pi;
4071
4072         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
4073                 PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
4074                 return -1;
4075         }
4076
4077         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
4078         bool ok = false;
4079
4080         lo_message reply = lo_message_new();
4081         lo_message_add_int32 (reply, ssid);
4082         lo_message_add_int32 (reply, piid);
4083         lo_message_add_int32(reply, redi->enabled() ? 1 : 0);
4084
4085         for ( uint32_t ppi = 0; ppi < pip->parameter_count(); ppi++) {
4086
4087                 uint32_t controlid = pip->nth_parameter(ppi, ok);
4088                 if (!ok) {
4089                         continue;
4090                 }
4091                 boost::shared_ptr<AutomationControl> c = pi->automation_control(Evoral::Parameter(PluginAutomation, 0, controlid));
4092
4093                 lo_message_add_int32 (reply, ppi + 1);
4094                 ParameterDescriptor pd;
4095                 pi->plugin()->get_parameter_descriptor(controlid, pd);
4096                 lo_message_add_string (reply, pd.label.c_str());
4097
4098                 // I've combined those binary descriptor parts in a bit-field to reduce lilo message elements
4099                 int flags = 0;
4100                 flags |= pd.enumeration ? 1 : 0;
4101                 flags |= pd.integer_step ? 2 : 0;
4102                 flags |= pd.logarithmic ? 4 : 0;
4103                 flags |= pd.sr_dependent ? 32 : 0;
4104                 flags |= pd.toggled ? 64 : 0;
4105                 lo_message_add_int32 (reply, flags);
4106
4107                 switch(pd.datatype) {
4108                         case ARDOUR::Variant::BEATS:
4109                                 lo_message_add_string(reply, _("BEATS"));
4110                                 break;
4111                         case ARDOUR::Variant::BOOL:
4112                                 lo_message_add_string(reply, _("BOOL"));
4113                                 break;
4114                         case ARDOUR::Variant::DOUBLE:
4115                                 lo_message_add_string(reply, _("DOUBLE"));
4116                                 break;
4117                         case ARDOUR::Variant::FLOAT:
4118                                 lo_message_add_string(reply, _("FLOAT"));
4119                                 break;
4120                         case ARDOUR::Variant::INT:
4121                                 lo_message_add_string(reply, _("INT"));
4122                                 break;
4123                         case ARDOUR::Variant::LONG:
4124                                 lo_message_add_string(reply, _("LONG"));
4125                                 break;
4126                         case ARDOUR::Variant::NOTHING:
4127                                 lo_message_add_string(reply, _("NOTHING"));
4128                                 break;
4129                         case ARDOUR::Variant::PATH:
4130                                 lo_message_add_string(reply, _("PATH"));
4131                                 break;
4132                         case ARDOUR::Variant::STRING:
4133                                 lo_message_add_string(reply, _("STRING"));
4134                                 break;
4135                         case ARDOUR::Variant::URI:
4136                                 lo_message_add_string(reply, _("URI"));
4137                                 break;
4138                         default:
4139                                 lo_message_add_string(reply, _("UNKNOWN"));
4140                                 break;
4141                 }
4142
4143                 lo_message_add_float (reply, pd.lower);
4144                 lo_message_add_float (reply, pd.upper);
4145                 lo_message_add_string (reply, pd.print_fmt.c_str());
4146                 if ( pd.scale_points ) {
4147                         lo_message_add_int32 (reply, pd.scale_points->size());
4148                         for ( ARDOUR::ScalePoints::const_iterator i = pd.scale_points->begin(); i != pd.scale_points->end(); ++i) {
4149                                 lo_message_add_int32 (reply, i->second);
4150                                 lo_message_add_string (reply, ((std::string)i->first).c_str());
4151                         }
4152                 } else {
4153                         lo_message_add_int32 (reply, 0);
4154                 }
4155                 if ( c ) {
4156                         lo_message_add_double (reply, c->get_value());
4157                 } else {
4158                         lo_message_add_double (reply, 0);
4159                 }
4160         }
4161
4162         lo_send_message (get_address (msg), "/strip/plugin/descriptor", reply);
4163         lo_message_free (reply);
4164
4165         return 0;
4166 }
4167
4168 int
4169 OSC::route_plugin_reset (int ssid, int piid, lo_message msg) {
4170         if (!session) {
4171                 return -1;
4172         }
4173
4174         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route>(get_strip (ssid, get_address (msg)));
4175
4176         if (!r) {
4177                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
4178                 return -1;
4179         }
4180
4181         boost::shared_ptr<Processor> redi = r->nth_plugin(piid - 1);
4182
4183         if (!redi) {
4184                 PBD::error << "OSC: cannot find plugin # " << piid << " for RID '" << ssid << "'" << endmsg;
4185                 return -1;
4186         }
4187
4188         boost::shared_ptr<PluginInsert> pi;
4189
4190         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
4191                 PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
4192                 return -1;
4193         }
4194
4195         pi->reset_parameters_to_default ();
4196
4197         return 0;
4198 }
4199
4200 int
4201 OSC::route_plugin_parameter (int ssid, int piid, int par, float val, lo_message msg)
4202 {
4203         if (!session)
4204                 return -1;
4205         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
4206
4207         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
4208
4209         if (!r) {
4210                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
4211                 return -1;
4212         }
4213
4214         boost::shared_ptr<Processor> redi=r->nth_plugin (piid - 1);
4215
4216         if (!redi) {
4217                 PBD::error << "OSC: cannot find plugin # " << piid << " for RID '" << ssid << "'" << endmsg;
4218                 return -1;
4219         }
4220
4221         boost::shared_ptr<PluginInsert> pi;
4222
4223         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
4224                 PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
4225                 return -1;
4226         }
4227
4228         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
4229         bool ok=false;
4230
4231         uint32_t controlid = pip->nth_parameter (par - 1,ok);
4232
4233         if (!ok) {
4234                 PBD::error << "OSC: Cannot find parameter # " << par <<  " for plugin # " << piid << " on RID '" << ssid << "'" << endmsg;
4235                 return -1;
4236         }
4237
4238         if (!pip->parameter_is_input(controlid)) {
4239                 PBD::error << "OSC: Parameter # " << par <<  " for plugin # " << piid << " on RID '" << ssid << "' is not a control input" << endmsg;
4240                 return -1;
4241         }
4242
4243         ParameterDescriptor pd;
4244         pi->plugin()->get_parameter_descriptor (controlid,pd);
4245
4246         if (val >= pd.lower && val <= pd.upper) {
4247
4248                 boost::shared_ptr<AutomationControl> c = pi->automation_control (Evoral::Parameter(PluginAutomation, 0, controlid));
4249                 // cerr << "parameter:" << redi->describe_parameter(controlid) << " val:" << val << "\n";
4250                 c->set_value (val, PBD::Controllable::NoGroup);
4251         } else {
4252                 PBD::warning << "OSC: Parameter # " << par <<  " for plugin # " << piid << " on RID '" << ssid << "' is out of range" << endmsg;
4253                 PBD::info << "OSC: Valid range min=" << pd.lower << " max=" << pd.upper << endmsg;
4254         }
4255
4256         return 0;
4257 }
4258
4259 //prints to cerr only
4260 int
4261 OSC::route_plugin_parameter_print (int ssid, int piid, int par, lo_message msg)
4262 {
4263         if (!session) {
4264                 return -1;
4265         }
4266         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
4267
4268         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
4269
4270         if (!r) {
4271                 return -1;
4272         }
4273
4274         boost::shared_ptr<Processor> redi=r->nth_plugin (piid - 1);
4275
4276         if (!redi) {
4277                 return -1;
4278         }
4279
4280         boost::shared_ptr<PluginInsert> pi;
4281
4282         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
4283                 return -1;
4284         }
4285
4286         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
4287         bool ok=false;
4288
4289         uint32_t controlid = pip->nth_parameter (par - 1,ok);
4290
4291         if (!ok) {
4292                 return -1;
4293         }
4294
4295         ParameterDescriptor pd;
4296
4297         if (pi->plugin()->get_parameter_descriptor (controlid, pd) == 0) {
4298                 boost::shared_ptr<AutomationControl> c = pi->automation_control (Evoral::Parameter(PluginAutomation, 0, controlid));
4299
4300                 cerr << "parameter:     " << pd.label  << "\n";
4301                 if (c) {
4302                         cerr << "current value: " << c->get_value () << "\n";
4303                 } else {
4304                         cerr << "current value not available, control does not exist\n";
4305                 }
4306                 cerr << "lower value:   " << pd.lower << "\n";
4307                 cerr << "upper value:   " << pd.upper << "\n";
4308         }
4309
4310         return 0;
4311 }
4312
4313 int
4314 OSC::route_plugin_activate (int ssid, int piid, lo_message msg)
4315 {
4316         if (!session)
4317                 return -1;
4318         boost::shared_ptr<Stripable> s = get_strip (ssid, lo_message_get_source (msg));
4319
4320         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
4321
4322         if (!r) {
4323                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
4324                 return -1;
4325         }
4326
4327         boost::shared_ptr<Processor> redi=r->nth_plugin (piid - 1);
4328
4329         if (!redi) {
4330                 PBD::error << "OSC: cannot find plugin # " << piid << " for RID '" << ssid << "'" << endmsg;
4331                 return -1;
4332         }
4333
4334         boost::shared_ptr<PluginInsert> pi;
4335
4336         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
4337                 PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
4338                 return -1;
4339         }
4340
4341         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
4342         pi->activate();
4343
4344         return 0;
4345 }
4346
4347 int
4348 OSC::route_plugin_deactivate (int ssid, int piid, lo_message msg)
4349 {
4350         if (!session)
4351                 return -1;
4352         boost::shared_ptr<Stripable> s = get_strip (ssid, lo_message_get_source (msg));
4353
4354         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
4355
4356         if (!r) {
4357                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
4358                 return -1;
4359         }
4360
4361         boost::shared_ptr<Processor> redi=r->nth_plugin (piid - 1);
4362
4363         if (!redi) {
4364                 PBD::error << "OSC: cannot find plugin # " << piid << " for RID '" << ssid << "'" << endmsg;
4365                 return -1;
4366         }
4367
4368         boost::shared_ptr<PluginInsert> pi;
4369
4370         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
4371                 PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
4372                 return -1;
4373         }
4374
4375         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
4376         pi->deactivate();
4377
4378         return 0;
4379 }
4380
4381 // select
4382
4383 int
4384 OSC::sel_pan_elevation (float val, lo_message msg)
4385 {
4386         OSCSurface *sur = get_surface(get_address (msg));
4387         boost::shared_ptr<Stripable> s;
4388         if (sur->expand_enable) {
4389                 s = get_strip (sur->expand, get_address (msg));
4390         } else {
4391                 s = _select;
4392         }
4393         if (s) {
4394                 if (s->pan_elevation_control()) {
4395                         s->pan_elevation_control()->set_value (s->pan_elevation_control()->interface_to_internal (val), PBD::Controllable::NoGroup);
4396                         return 0;
4397                 }
4398         }
4399         return sel_fail ("pan_elevation_position", 0, get_address (msg));
4400 }
4401
4402 int
4403 OSC::sel_pan_frontback (float val, lo_message msg)
4404 {
4405         OSCSurface *sur = get_surface(get_address (msg));
4406         boost::shared_ptr<Stripable> s;
4407         if (sur->expand_enable) {
4408                 s = get_strip (sur->expand, get_address (msg));
4409         } else {
4410                 s = _select;
4411         }
4412         if (s) {
4413                 if (s->pan_frontback_control()) {
4414                         s->pan_frontback_control()->set_value (s->pan_frontback_control()->interface_to_internal (val), PBD::Controllable::NoGroup);
4415                         return 0;
4416                 }
4417         }
4418         return sel_fail ("pan_frontback_position", 0.5, get_address (msg));
4419 }
4420
4421 int
4422 OSC::sel_pan_lfe (float val, lo_message msg)
4423 {
4424         OSCSurface *sur = get_surface(get_address (msg));
4425         boost::shared_ptr<Stripable> s;
4426         if (sur->expand_enable) {
4427                 s = get_strip (sur->expand, get_address (msg));
4428         } else {
4429                 s = _select;
4430         }
4431         if (s) {
4432                 if (s->pan_lfe_control()) {
4433                         s->pan_lfe_control()->set_value (s->pan_lfe_control()->interface_to_internal (val), PBD::Controllable::NoGroup);
4434                         return 0;
4435                 }
4436         }
4437         return sel_fail ("pan_lfe_control", 0, get_address (msg));
4438 }
4439
4440 // compressor control
4441 int
4442 OSC::sel_comp_enable (float val, lo_message msg)
4443 {
4444         OSCSurface *sur = get_surface(get_address (msg));
4445         boost::shared_ptr<Stripable> s;
4446         if (sur->expand_enable) {
4447                 s = get_strip (sur->expand, get_address (msg));
4448         } else {
4449                 s = _select;
4450         }
4451         if (s) {
4452                 if (s->comp_enable_controllable()) {
4453                         s->comp_enable_controllable()->set_value (s->comp_enable_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
4454                         return 0;
4455                 }
4456         }
4457         return sel_fail ("comp_enable", 0, get_address (msg));
4458 }
4459
4460 int
4461 OSC::sel_comp_threshold (float val, lo_message msg)
4462 {
4463         OSCSurface *sur = get_surface(get_address (msg));
4464         boost::shared_ptr<Stripable> s;
4465         if (sur->expand_enable) {
4466                 s = get_strip (sur->expand, get_address (msg));
4467         } else {
4468                 s = _select;
4469         }
4470         if (s) {
4471                 if (s->comp_threshold_controllable()) {
4472                         s->comp_threshold_controllable()->set_value (s->comp_threshold_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
4473                         return 0;
4474                 }
4475         }
4476         return sel_fail ("comp_threshold", 0, get_address (msg));
4477 }
4478
4479 int
4480 OSC::sel_comp_speed (float val, lo_message msg)
4481 {
4482         OSCSurface *sur = get_surface(get_address (msg));
4483         boost::shared_ptr<Stripable> s;
4484         if (sur->expand_enable) {
4485                 s = get_strip (sur->expand, get_address (msg));
4486         } else {
4487                 s = _select;
4488         }
4489         if (s) {
4490                 if (s->comp_speed_controllable()) {
4491                         s->comp_speed_controllable()->set_value (s->comp_speed_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
4492                         return 0;
4493                 }
4494         }
4495         return sel_fail ("comp_speed", 0, get_address (msg));
4496 }
4497
4498 int
4499 OSC::sel_comp_mode (float val, lo_message msg)
4500 {
4501         OSCSurface *sur = get_surface(get_address (msg));
4502         boost::shared_ptr<Stripable> s;
4503         if (sur->expand_enable) {
4504                 s = get_strip (sur->expand, get_address (msg));
4505         } else {
4506                 s = _select;
4507         }
4508         if (s) {
4509                 if (s->comp_mode_controllable()) {
4510                         s->comp_mode_controllable()->set_value (s->comp_mode_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
4511                         return 0;
4512                 }
4513         }
4514         return sel_fail ("comp_mode", 0, get_address (msg));
4515 }
4516
4517 int
4518 OSC::sel_comp_makeup (float val, lo_message msg)
4519 {
4520         OSCSurface *sur = get_surface(get_address (msg));
4521         boost::shared_ptr<Stripable> s;
4522         if (sur->expand_enable) {
4523                 s = get_strip (sur->expand, get_address (msg));
4524         } else {
4525                 s = _select;
4526         }
4527         if (s) {
4528                 if (s->comp_makeup_controllable()) {
4529                         s->comp_makeup_controllable()->set_value (s->comp_makeup_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
4530                         return 0;
4531                 }
4532         }
4533         return sel_fail ("comp_makeup", 0, get_address (msg));
4534 }
4535
4536 // EQ control
4537
4538 int
4539 OSC::sel_eq_enable (float val, lo_message msg)
4540 {
4541         OSCSurface *sur = get_surface(get_address (msg));
4542         boost::shared_ptr<Stripable> s;
4543         if (sur->expand_enable) {
4544                 s = get_strip (sur->expand, get_address (msg));
4545         } else {
4546                 s = _select;
4547         }
4548         if (s) {
4549                 if (s->eq_enable_controllable()) {
4550                         s->eq_enable_controllable()->set_value (s->eq_enable_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
4551                         return 0;
4552                 }
4553         }
4554         return sel_fail ("eq_enable", 0, get_address (msg));
4555 }
4556
4557 int
4558 OSC::sel_eq_hpf_freq (float val, lo_message msg)
4559 {
4560         OSCSurface *sur = get_surface(get_address (msg));
4561         boost::shared_ptr<Stripable> s;
4562         if (sur->expand_enable) {
4563                 s = get_strip (sur->expand, get_address (msg));
4564         } else {
4565                 s = _select;
4566         }
4567         if (s) {
4568                 if (s->filter_freq_controllable(true)) {
4569                         s->filter_freq_controllable(true)->set_value (s->filter_freq_controllable(true)->interface_to_internal (val), PBD::Controllable::NoGroup);
4570                         return 0;
4571                 }
4572         }
4573         return sel_fail ("eq_hpf/freq", 0, get_address (msg));
4574 }
4575
4576 int
4577 OSC::sel_eq_lpf_freq (float val, lo_message msg)
4578 {
4579         OSCSurface *sur = get_surface(get_address (msg));
4580         boost::shared_ptr<Stripable> s;
4581         if (sur->expand_enable) {
4582                 s = get_strip (sur->expand, get_address (msg));
4583         } else {
4584                 s = _select;
4585         }
4586         if (s) {
4587                 if (s->filter_freq_controllable(false)) {
4588                         s->filter_freq_controllable(false)->set_value (s->filter_freq_controllable(false)->interface_to_internal (val), PBD::Controllable::NoGroup);
4589                         return 0;
4590                 }
4591         }
4592         return sel_fail ("eq_lpf/freq", 0, get_address (msg));
4593 }
4594
4595 int
4596 OSC::sel_eq_hpf_enable (float val, lo_message msg)
4597 {
4598         OSCSurface *sur = get_surface(get_address (msg));
4599         boost::shared_ptr<Stripable> s;
4600         if (sur->expand_enable) {
4601                 s = get_strip (sur->expand, get_address (msg));
4602         } else {
4603                 s = _select;
4604         }
4605         if (s) {
4606                 if (s->filter_enable_controllable(true)) {
4607                         s->filter_enable_controllable(true)->set_value (s->filter_enable_controllable(true)->interface_to_internal (val), PBD::Controllable::NoGroup);
4608                         return 0;
4609                 }
4610         }
4611         return sel_fail ("eq_hpf/enable", 0, get_address (msg));
4612 }
4613
4614 int
4615 OSC::sel_eq_lpf_enable (float val, lo_message msg)
4616 {
4617         OSCSurface *sur = get_surface(get_address (msg));
4618         boost::shared_ptr<Stripable> s;
4619         if (sur->expand_enable) {
4620                 s = get_strip (sur->expand, get_address (msg));
4621         } else {
4622                 s = _select;
4623         }
4624         if (s) {
4625                 if (s->filter_enable_controllable(false)) {
4626                         s->filter_enable_controllable(false)->set_value (s->filter_enable_controllable(false)->interface_to_internal (val), PBD::Controllable::NoGroup);
4627                         return 0;
4628                 }
4629         }
4630         return sel_fail ("eq_lpf/enable", 0, get_address (msg));
4631 }
4632
4633 int
4634 OSC::sel_eq_hpf_slope (float val, lo_message msg)
4635 {
4636         OSCSurface *sur = get_surface(get_address (msg));
4637         boost::shared_ptr<Stripable> s;
4638         if (sur->expand_enable) {
4639                 s = get_strip (sur->expand, get_address (msg));
4640         } else {
4641                 s = _select;
4642         }
4643         if (s) {
4644                 if (s->filter_slope_controllable(true)) {
4645                         s->filter_slope_controllable(true)->set_value (s->filter_slope_controllable(true)->interface_to_internal (val), PBD::Controllable::NoGroup);
4646                         return 0;
4647                 }
4648         }
4649         return sel_fail ("eq_hpf/slope", 0, get_address (msg));
4650 }
4651
4652 int
4653 OSC::sel_eq_lpf_slope (float val, lo_message msg)
4654 {
4655         OSCSurface *sur = get_surface(get_address (msg));
4656         boost::shared_ptr<Stripable> s;
4657         if (sur->expand_enable) {
4658                 s = get_strip (sur->expand, get_address (msg));
4659         } else {
4660                 s = _select;
4661         }
4662         if (s) {
4663                 if (s->filter_slope_controllable(false)) {
4664                         s->filter_slope_controllable(false)->set_value (s->filter_slope_controllable(false)->interface_to_internal (val), PBD::Controllable::NoGroup);
4665                         return 0;
4666                 }
4667         }
4668         return sel_fail ("eq_lpf/slope", 0, get_address (msg));
4669 }
4670
4671 int
4672 OSC::sel_eq_gain (int id, float val, lo_message msg)
4673 {
4674         OSCSurface *sur = get_surface(get_address (msg));
4675         boost::shared_ptr<Stripable> s;
4676         if (sur->expand_enable) {
4677                 s = get_strip (sur->expand, get_address (msg));
4678         } else {
4679                 s = _select;
4680         }
4681         if (s) {
4682                 if (id > 0) {
4683                         --id;
4684                 }
4685                 if (s->eq_gain_controllable (id)) {
4686                         s->eq_gain_controllable (id)->set_value (s->eq_gain_controllable(id)->interface_to_internal (val), PBD::Controllable::NoGroup);
4687                         return 0;
4688                 }
4689         }
4690         return sel_send_fail ("eq_gain", id + 1, 0, get_address (msg));
4691 }
4692
4693 int
4694 OSC::sel_eq_freq (int id, float val, lo_message msg)
4695 {
4696         OSCSurface *sur = get_surface(get_address (msg));
4697         boost::shared_ptr<Stripable> s;
4698         if (sur->expand_enable) {
4699                 s = get_strip (sur->expand, get_address (msg));
4700         } else {
4701                 s = _select;
4702         }
4703         if (s) {
4704                 if (id > 0) {
4705                         --id;
4706                 }
4707                 if (s->eq_freq_controllable (id)) {
4708                         s->eq_freq_controllable (id)->set_value (s->eq_freq_controllable(id)->interface_to_internal (val), PBD::Controllable::NoGroup);
4709                         return 0;
4710                 }
4711         }
4712         return sel_send_fail ("eq_freq", id + 1, 0, get_address (msg));
4713 }
4714
4715 int
4716 OSC::sel_eq_q (int id, float val, lo_message msg)
4717 {
4718         OSCSurface *sur = get_surface(get_address (msg));
4719         boost::shared_ptr<Stripable> s;
4720         if (sur->expand_enable) {
4721                 s = get_strip (sur->expand, get_address (msg));
4722         } else {
4723                 s = _select;
4724         }
4725         if (s) {
4726                 if (id > 0) {
4727                         --id;
4728                 }
4729                 if (s->eq_q_controllable (id)) {
4730                         s->eq_q_controllable (id)->set_value (s->eq_q_controllable(id)->interface_to_internal (val), PBD::Controllable::NoGroup);
4731                         return 0;
4732                 }
4733         }
4734         return sel_send_fail ("eq_q", id + 1, 0, get_address (msg));
4735 }
4736
4737 int
4738 OSC::sel_eq_shape (int id, float val, lo_message msg)
4739 {
4740         OSCSurface *sur = get_surface(get_address (msg));
4741         boost::shared_ptr<Stripable> s;
4742         if (sur->expand_enable) {
4743                 s = get_strip (sur->expand, get_address (msg));
4744         } else {
4745                 s = _select;
4746         }
4747         if (s) {
4748                 if (id > 0) {
4749                         --id;
4750                 }
4751                 if (s->eq_shape_controllable (id)) {
4752                         s->eq_shape_controllable (id)->set_value (s->eq_shape_controllable(id)->interface_to_internal (val), PBD::Controllable::NoGroup);
4753                         return 0;
4754                 }
4755         }
4756         return sel_send_fail ("eq_shape", id + 1, 0, get_address (msg));
4757 }
4758
4759 void
4760 OSC::gui_selection_changed ()
4761 {
4762         boost::shared_ptr<Stripable> strip = ControlProtocol::first_selected_stripable();
4763
4764         if (strip) {
4765                 _select = strip;
4766                 for (uint32_t it = 0; it < _surface.size(); ++it) {
4767                         OSCSurface* sur = &_surface[it];
4768                         if(!sur->expand_enable) {
4769                                 lo_address addr = lo_address_new_from_url (sur->remote_url.c_str());
4770                                 _strip_select (strip, addr);
4771                         }
4772                 }
4773         }
4774 }
4775
4776 // timer callbacks
4777 bool
4778 OSC::periodic (void)
4779 {
4780         if (!tick) {
4781                 Glib::usleep(100); // let flurry of signals subside
4782                 if (global_init) {
4783                         Glib::Threads::Mutex::Lock lm (surfaces_lock);
4784                         for (uint32_t it = 0; it < _surface.size(); it++) {
4785                                 OSCSurface* sur = &_surface[it];
4786                                 lo_address addr = lo_address_new_from_url (sur->remote_url.c_str());
4787                                 global_feedback (*sur, addr);
4788                         }
4789                         global_init = false;
4790                         tick = true;
4791                 }
4792                 if (bank_dirty) {
4793                         _recalcbanks ();
4794                         bank_dirty = false;
4795                         tick = true;
4796                 }
4797         }
4798
4799         if (scrub_speed != 0) {
4800                 // for those jog wheels that don't have 0 on release (touch), time out.
4801                 int64_t now = ARDOUR::get_microseconds ();
4802                 int64_t diff = now - scrub_time;
4803                 if (diff > 120000) {
4804                         scrub_speed = 0;
4805                         session->request_transport_speed (0);
4806                         // locate to the place PH was at last tick
4807                         session->request_locate (scrub_place, false);
4808                 }
4809         }
4810
4811         for (GlobalObservers::iterator x = global_observers.begin(); x != global_observers.end(); x++) {
4812
4813                 OSCGlobalObserver* go;
4814
4815                 if ((go = dynamic_cast<OSCGlobalObserver*>(*x)) != 0) {
4816                         go->tick();
4817                 }
4818         }
4819         for (RouteObservers::iterator x = route_observers.begin(); x != route_observers.end(); x++) {
4820
4821                 OSCRouteObserver* ro;
4822
4823                 if ((ro = dynamic_cast<OSCRouteObserver*>(*x)) != 0) {
4824                         ro->tick();
4825                 }
4826         }
4827         for (uint32_t it = 0; it < _surface.size(); it++) {
4828                 OSCSurface* sur = &_surface[it];
4829                 OSCSelectObserver* so;
4830                 if ((so = dynamic_cast<OSCSelectObserver*>(sur->sel_obs)) != 0) {
4831                         so->tick();
4832                 }
4833         }
4834         for (CueObservers::iterator x = cue_observers.begin(); x != cue_observers.end(); x++) {
4835
4836                 OSCCueObserver* co;
4837
4838                 if ((co = dynamic_cast<OSCCueObserver*>(*x)) != 0) {
4839                         co->tick();
4840                 }
4841         }
4842         for (FakeTouchMap::iterator x = _touch_timeout.begin(); x != _touch_timeout.end();) {
4843                 _touch_timeout[(*x).first] = (*x).second - 1;
4844                 if (!(*x).second) {
4845                         boost::shared_ptr<ARDOUR::AutomationControl> ctrl = (*x).first;
4846                         // turn touch off
4847                         ctrl->stop_touch (ctrl->session().transport_frame());
4848                         _touch_timeout.erase (x++);
4849                 } else {
4850                         x++;
4851                 }
4852         }
4853         return true;
4854 }
4855
4856 int
4857 OSC::route_send_fail (string path, uint32_t ssid, float val, lo_address addr)
4858 {
4859         OSCSurface *sur = get_surface(addr);
4860
4861         ostringstream os;
4862         lo_message reply;
4863         if (ssid) {
4864                 reply = lo_message_new ();
4865                 if (sur->feedback[2]) {
4866                         os << "/strip/" << path << "/" << ssid;
4867                 } else {
4868                         os << "/strip/" << path;
4869                         lo_message_add_int32 (reply, ssid);
4870                 }
4871                 string str_pth = os.str();
4872                 lo_message_add_float (reply, (float) val);
4873
4874                 lo_send_message (addr, str_pth.c_str(), reply);
4875                 lo_message_free (reply);
4876         }
4877         if ((_select == get_strip (ssid, addr)) || ((sur->expand == ssid) && (sur->expand_enable))) {
4878                 os.str("");
4879                 os << "/select/" << path;
4880                 string sel_pth = os.str();
4881                 reply = lo_message_new ();
4882                 lo_message_add_float (reply, (float) val);
4883                 lo_send_message (addr, sel_pth.c_str(), reply);
4884                 lo_message_free (reply);
4885         }
4886
4887         return 0;
4888 }
4889
4890 int
4891 OSC::sel_fail (string path, float val, lo_address addr)
4892 {
4893         ostringstream os;
4894         os.str("");
4895         os << "/select/" << path;
4896         string sel_pth = os.str();
4897         lo_message reply = lo_message_new ();
4898         lo_message_add_float (reply, (float) val);
4899         lo_send_message (addr, sel_pth.c_str(), reply);
4900         lo_message_free (reply);
4901
4902         return 0;
4903 }
4904
4905 int
4906 OSC::sel_send_fail (string path, uint32_t id, float val, lo_address addr)
4907 {
4908         OSCSurface *sur = get_surface(addr);
4909
4910         ostringstream os;
4911         lo_message reply;
4912         reply = lo_message_new ();
4913         if (sur->feedback[2]) {
4914                 os << "/select/" << path << "/" << id;
4915         } else {
4916                 os << "/select/" << path;
4917                 lo_message_add_int32 (reply, id);
4918         }
4919         string str_pth = os.str();
4920         lo_message_add_float (reply, (float) val);
4921
4922         lo_send_message (addr, str_pth.c_str(), reply);
4923         lo_message_free (reply);
4924
4925         return 0;
4926 }
4927
4928 XMLNode&
4929 OSC::get_state ()
4930 {
4931         XMLNode& node (ControlProtocol::get_state());
4932         node.set_property ("debugmode", (int32_t) _debugmode); // TODO: enum2str
4933         node.set_property ("address-only", address_only);
4934         node.set_property ("remote-port", remote_port);
4935         node.set_property ("banksize", default_banksize);
4936         node.set_property ("striptypes", default_strip);
4937         node.set_property ("feedback", default_feedback);
4938         node.set_property ("gainmode", default_gainmode);
4939         node.set_property ("send-page-size", default_send_size);
4940         node.set_property ("plug-page-size", default_plugin_size);
4941         return node;
4942 }
4943
4944 int
4945 OSC::set_state (const XMLNode& node, int version)
4946 {
4947         if (ControlProtocol::set_state (node, version)) {
4948                 return -1;
4949         }
4950         int32_t debugmode;
4951         if (node.get_property (X_("debugmode"), debugmode)) {
4952                 _debugmode = OSCDebugMode (debugmode);
4953         }
4954
4955         node.get_property (X_("address-only"), address_only);
4956         node.get_property (X_("remote-port"), remote_port);
4957         node.get_property (X_("banksize"), default_banksize);
4958         node.get_property (X_("striptypes"), default_strip);
4959         node.get_property (X_("feedback"), default_feedback);
4960         node.get_property (X_("gainmode"), default_gainmode);
4961         node.get_property (X_("send-page-size"), default_send_size);
4962         node.get_property (X_("plugin-page-size"), default_plugin_size);
4963
4964         global_init = true;
4965         tick = false;
4966
4967         return 0;
4968 }
4969
4970 // predicate for sort call in get_sorted_stripables
4971 struct StripableByPresentationOrder
4972 {
4973         bool operator () (const boost::shared_ptr<Stripable> & a, const boost::shared_ptr<Stripable> & b) const
4974         {
4975                 return a->presentation_info().order() < b->presentation_info().order();
4976         }
4977
4978         bool operator () (const Stripable & a, const Stripable & b) const
4979         {
4980                 return a.presentation_info().order() < b.presentation_info().order();
4981         }
4982
4983         bool operator () (const Stripable * a, const Stripable * b) const
4984         {
4985                 return a->presentation_info().order() < b->presentation_info().order();
4986         }
4987 };
4988
4989 OSC::Sorted
4990 OSC::get_sorted_stripables(std::bitset<32> types, bool cue)
4991 {
4992         Sorted sorted;
4993
4994         // fetch all stripables
4995         StripableList stripables;
4996
4997         session->get_stripables (stripables);
4998
4999         // Look for stripables that match bit in sur->strip_types
5000         for (StripableList::iterator it = stripables.begin(); it != stripables.end(); ++it) {
5001
5002                 boost::shared_ptr<Stripable> s = *it;
5003                 if ((!cue) && (!types[9]) && (s->presentation_info().flags() & PresentationInfo::Hidden)) {
5004                         // do nothing... skip it
5005                 } else {
5006
5007                         if (types[0] && (s->presentation_info().flags() & PresentationInfo::AudioTrack)) {
5008                                 sorted.push_back (s);
5009                         } else
5010                         if (types[1] && (s->presentation_info().flags() & PresentationInfo::MidiTrack)) {
5011                                 sorted.push_back (s);
5012                         } else
5013                         if ((s->presentation_info().flags() & PresentationInfo::AudioBus)) {
5014                                 boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
5015                                 // r->feeds (session->master_out()) may make more sense
5016                                 if (r->direct_feeds_according_to_reality (session->master_out())) {
5017                                         // this is a bus
5018                                         if (types[2]) {
5019                                                 sorted.push_back (s);
5020                                         }
5021                                 } else {
5022                                         // this is an Aux out
5023                                         if (types[7]) {
5024                                                 sorted.push_back (s);
5025                                         }
5026                                 }
5027                         } else if (types[3] && (s->presentation_info().flags() & PresentationInfo::MidiBus)) {
5028                                 sorted.push_back (s);
5029                         } else if (types[4] && (s->presentation_info().flags() & PresentationInfo::VCA)) {
5030                                 sorted.push_back (s);
5031                         } else if (types[8] && (s->is_selected())) {
5032                                 sorted.push_back (s);
5033                         } else if (types[9] && (s->presentation_info().flags() & PresentationInfo::Hidden)) {
5034                                 sorted.push_back (s);
5035                         }
5036 #ifdef MIXBUS
5037                         else if (types[2]) {
5038                                 if (Profile->get_mixbus()) {
5039                                         if (s->mixbus()) {
5040                                                 sorted.push_back (s);
5041                                         }
5042                                 }
5043                         }
5044 #endif
5045                 }
5046         }
5047         sort (sorted.begin(), sorted.end(), StripableByPresentationOrder());
5048         // Master/Monitor might be anywhere... we put them at the end - Sorry ;)
5049         if (types[5]) {
5050                 sorted.push_back (session->master_out());
5051         }
5052         if (types[6]) {
5053                 sorted.push_back (session->monitor_out());
5054         }
5055         return sorted;
5056 }
5057
5058 int
5059 OSC::cue_parse (const char *path, const char* types, lo_arg **argv, int argc, lo_message msg)
5060 {
5061         int ret = 1; /* unhandled */
5062
5063         if (!strncmp (path, "/cue/aux", 8)) {
5064                 // set our Aux bus
5065                 ret = cue_set (argv[0]->i, msg);
5066         }
5067         else if (!strncmp (path, "/cue/connect", 12)) {
5068                 // Connect to default Aux bus
5069                 if ((!argc) || argv[0]->i) {
5070                         ret = cue_set (1, msg);
5071                 }
5072         }
5073         else if (!strncmp (path, "/cue/next_aux", 13)) {
5074                 // switch to next Aux bus
5075                 if ((!argc) || argv[0]->i) {
5076                         ret = cue_next (msg);
5077                 }
5078         }
5079         else if (!strncmp (path, "/cue/previous_aux", 17)) {
5080                 // switch to previous Aux bus
5081                 if ((!argc) || argv[0]->i) {
5082                         ret = cue_previous (msg);
5083                 }
5084         }
5085         else if (!strncmp (path, "/cue/send/fader/", 16) && strlen (path) > 16) {
5086                 int id = atoi (&path[16]);
5087                 ret = cue_send_fader (id, argv[0]->f, msg);
5088         }
5089         else if (!strncmp (path, "/cue/send/enable/", 17) && strlen (path) > 17) {
5090                 int id = atoi (&path[17]);
5091                 ret = cue_send_enable (id, argv[0]->f, msg);
5092         }
5093         else if (!strncmp (path, "/cue/fader", 10)) {
5094                 ret = cue_aux_fader (argv[0]->f, msg);
5095         }
5096         else if (!strncmp (path, "/cue/mute", 9)) {
5097                 ret = cue_aux_mute (argv[0]->f, msg);
5098         }
5099
5100         return ret;
5101 }
5102
5103 int
5104 OSC::cue_set (uint32_t aux, lo_message msg)
5105 {
5106         return _cue_set (aux, get_address (msg));
5107 }
5108
5109 int
5110 OSC::_cue_set (uint32_t aux, lo_address addr)
5111 {
5112         int ret = 1;
5113         OSCSurface *s = get_surface(addr);
5114         s->bank_size = 0;
5115         s->strip_types = 128;
5116         s->feedback = 0;
5117         s->gainmode = 1;
5118         s->cue = true;
5119         s->strips = get_sorted_stripables(s->strip_types, s->cue);
5120
5121         s->nstrips = s->strips.size();
5122
5123         if (aux < 1) {
5124                 aux = 1;
5125         } else if (aux > s->nstrips) {
5126                 aux = s->nstrips;
5127         }
5128         s->aux = aux;
5129
5130         // get rid of any old CueObsevers for this address
5131         //cueobserver_connections.drop_connections ();
5132         CueObservers::iterator x;
5133         for (x = cue_observers.begin(); x != cue_observers.end();) {
5134
5135                 OSCCueObserver* co;
5136
5137                 if ((co = dynamic_cast<OSCCueObserver*>(*x)) != 0) {
5138
5139                         int res = strcmp(lo_address_get_url(co->address()), lo_address_get_url(addr));
5140
5141                         if (res == 0) {
5142                                 delete *x;
5143                                 x = cue_observers.erase (x);
5144                         } else {
5145                                 ++x;
5146                         }
5147                 } else {
5148                         ++x;
5149                 }
5150         }
5151
5152         // get a list of Auxes
5153         for (uint32_t n = 0; n < s->nstrips; ++n) {
5154                 boost::shared_ptr<Stripable> stp = s->strips[n];
5155                 if (stp) {
5156                         text_message (string_compose ("/cue/name/%1", n+1), stp->name(), addr);
5157                         if (aux == n+1) {
5158                                 // aux must be at least one
5159                                 // need a signal if aux vanishes
5160                                 stp->DropReferences.connect (*this, MISSING_INVALIDATOR, boost::bind (&OSC::_cue_set, this, aux, addr), this);
5161
5162                                 // make a list of stripables with sends that go to this bus
5163                                 s->sends = cue_get_sorted_stripables(stp, aux, addr);
5164                                 // start cue observer
5165                                 OSCCueObserver* co = new OSCCueObserver (stp, s->sends, addr);
5166                                 cue_observers.push_back (co);
5167                                 ret = 0;
5168                         }
5169
5170                 }
5171         }
5172
5173         return ret;
5174 }
5175
5176 int
5177 OSC::cue_next (lo_message msg)
5178 {
5179         OSCSurface *s = get_surface(get_address (msg));
5180         int ret = 1;
5181
5182         if (!s->cue) {
5183                 ret = cue_set (1, msg);
5184         }
5185         if (s->aux < s->nstrips) {
5186                 ret = cue_set (s->aux + 1, msg);
5187         } else {
5188                 ret = cue_set (s->nstrips, msg);
5189         }
5190         return ret;
5191 }
5192
5193 int
5194 OSC::cue_previous (lo_message msg)
5195 {
5196         OSCSurface *s = get_surface(get_address (msg));
5197         int ret = 1;
5198         if (!s->cue) {
5199                 ret = cue_set (1, msg);
5200         }
5201         if (s->aux > 1) {
5202                 ret = cue_set (s->aux - 1, msg);
5203         }
5204         return ret;
5205 }
5206
5207 boost::shared_ptr<Send>
5208 OSC::cue_get_send (uint32_t id, lo_address addr)
5209 {
5210         OSCSurface *s = get_surface(addr);
5211         if (id && s->aux > 0 && id <= s->sends.size()) {
5212                 boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s->sends[id - 1]);
5213                 boost::shared_ptr<Stripable> aux = get_strip (s->aux, addr);
5214                 if (r && aux) {
5215                         return r->internal_send_for (boost::dynamic_pointer_cast<Route> (aux));
5216                 }
5217         }
5218         return boost::shared_ptr<Send>();
5219
5220 }
5221
5222 int
5223 OSC::cue_aux_fader (float position, lo_message msg)
5224 {
5225         if (!session) return -1;
5226
5227         OSCSurface *sur = get_surface(get_address (msg));
5228         if (sur->cue) {
5229                 if (sur->aux) {
5230                         boost::shared_ptr<Stripable> s = get_strip (sur->aux, get_address (msg));
5231
5232                         if (s) {
5233                                 if (s->gain_control()) {
5234                                         s->gain_control()->set_value (s->gain_control()->interface_to_internal (position), PBD::Controllable::NoGroup);
5235                                         return 0;
5236                                 }
5237                         }
5238                 }
5239         }
5240         cue_float_message ("/cue/fader", 0, get_address (msg));
5241         return -1;
5242 }
5243
5244 int
5245 OSC::cue_aux_mute (float state, lo_message msg)
5246 {
5247         if (!session) return -1;
5248
5249         OSCSurface *sur = get_surface(get_address (msg));
5250         if (sur->cue) {
5251                 if (sur->aux) {
5252                         boost::shared_ptr<Stripable> s = get_strip (sur->aux, get_address (msg));
5253                         if (s) {
5254                                 if (s->mute_control()) {
5255                                         s->mute_control()->set_value (state ? 1.0 : 0.0, PBD::Controllable::NoGroup);
5256                                         return 0;
5257                                 }
5258                         }
5259                 }
5260         }
5261         cue_float_message ("/cue/mute", 0, get_address (msg));
5262         return -1;
5263 }
5264
5265 int
5266 OSC::cue_send_fader (uint32_t id, float val, lo_message msg)
5267 {
5268         if (!session) {
5269                 return -1;
5270         }
5271         boost::shared_ptr<Send> s = cue_get_send (id, get_address (msg));
5272         if (s) {
5273                 if (s->gain_control()) {
5274                         s->gain_control()->set_value (s->gain_control()->interface_to_internal(val), PBD::Controllable::NoGroup);
5275                         return 0;
5276                 }
5277         }
5278         cue_float_message (string_compose ("/cue/send/fader/%1", id), 0, get_address (msg));
5279         return -1;
5280 }
5281
5282 int
5283 OSC::cue_send_enable (uint32_t id, float state, lo_message msg)
5284 {
5285         if (!session)
5286                 return -1;
5287         boost::shared_ptr<Send> s = cue_get_send (id, get_address (msg));
5288         if (s) {
5289                 if (state) {
5290                         s->activate ();
5291                 } else {
5292                         s->deactivate ();
5293                 }
5294                 return 0;
5295         }
5296         cue_float_message (string_compose ("/cue/send/enable/%1", id), 0, get_address (msg));
5297         return -1;
5298 }
5299
5300 int
5301 OSC::cue_float_message (string path, float val, lo_address addr)
5302 {
5303
5304         lo_message reply;
5305         reply = lo_message_new ();
5306         lo_message_add_float (reply, (float) val);
5307
5308         lo_send_message (addr, path.c_str(), reply);
5309         lo_message_free (reply);
5310
5311         return 0;
5312 }
5313
5314 int
5315 OSC::text_message (string path, string val, lo_address addr)
5316 {
5317
5318         lo_message reply;
5319         reply = lo_message_new ();
5320         lo_message_add_string (reply, val.c_str());
5321
5322         lo_send_message (addr, path.c_str(), reply);
5323         lo_message_free (reply);
5324
5325         return 0;
5326 }
5327
5328
5329 // we have to have a sorted list of stripables that have sends pointed at our aux
5330 // we can use the one in osc.cc to get an aux list
5331 OSC::Sorted
5332 OSC::cue_get_sorted_stripables(boost::shared_ptr<Stripable> aux, uint32_t id, lo_message msg)
5333 {
5334         Sorted sorted;
5335         cueobserver_connections.drop_connections ();
5336         // fetch all stripables
5337         StripableList stripables;
5338
5339         session->get_stripables (stripables);
5340
5341         // Look for stripables that have a send to aux
5342         for (StripableList::iterator it = stripables.begin(); it != stripables.end(); ++it) {
5343
5344                 boost::shared_ptr<Stripable> s = *it;
5345                 // we only want routes
5346                 boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
5347                 if (r) {
5348                         r->processors_changed.connect  (*this, MISSING_INVALIDATOR, boost::bind (&OSC::recalcbanks, this), this);
5349                         boost::shared_ptr<Send> snd = r->internal_send_for (boost::dynamic_pointer_cast<Route> (aux));
5350                         if (snd) { // test for send to aux
5351                                 sorted.push_back (s);
5352                                 s->DropReferences.connect (*this, MISSING_INVALIDATOR, boost::bind (&OSC::cue_set, this, id, msg), this);
5353                         }
5354                 }
5355
5356
5357         }
5358         sort (sorted.begin(), sorted.end(), StripableByPresentationOrder());
5359
5360         return sorted;
5361 }
5362