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