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