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