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