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