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