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