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