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