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