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