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