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