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