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