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