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