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