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