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